libfusd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. *
  3. * Copyright (c) 2003 The Regents of the University of California. All
  4. * rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Neither the name of the University nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * fusd userspace library: functions that know how to properly talk
  32. * to the fusd kernel module
  33. *
  34. * authors: jelson and girod
  35. *
  36. * $Id: libfusd.c 12351 2007-01-19 07:22:54Z xiphmont $
  37. */
  38. char libfusd_c_id[] = "$Id: libfusd.c 12351 2007-01-19 07:22:54Z xiphmont $";
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <sys/time.h>
  45. #include <sys/uio.h>
  46. #include <sys/ioctl.h>
  47. #include <fcntl.h>
  48. #include <errno.h>
  49. #include <string.h>
  50. #include <time.h>
  51. #include <pthread.h>
  52. #include <unistd.h>
  53. #include <sys/syscall.h>
  54. #include "fusd.h"
  55. #include "fusd_msg.h"
  56. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  57. /* maximum number of messages processed by a single call to fusd_dispatch */
  58. #define MAX_MESSAGES_PER_DISPATCH 40
  59. /* used for fusd_run */
  60. static fd_set fusd_fds;
  61. /* default prefix of devices (often "/dev/") */
  62. char *dev_root = NULL;
  63. /*
  64. * fusd_fops_set is an array that keeps track of the file operations
  65. * struct for each fusd fd.
  66. */
  67. static fusd_file_operations_t fusd_fops_set[FD_SETSIZE];
  68. fusd_file_operations_t null_fops = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  69. /*
  70. * accessor macros
  71. */
  72. #define FUSD_GET_FOPS(fd) \
  73. (fusd_fops_set + (fd))
  74. #define FUSD_SET_FOPS(fd,fi) \
  75. (fusd_fops_set[(fd)]=(*fi))
  76. #define FUSD_FD_VALID(fd) \
  77. (((fd)>=0) && \
  78. ((fd)<FD_SETSIZE) && \
  79. (memcmp(FUSD_GET_FOPS(fd), &null_fops, sizeof(fusd_file_operations_t))))
  80. /*
  81. * fusd_init
  82. *
  83. * this is called automatically before the first
  84. * register call
  85. */
  86. void fusd_init()
  87. {
  88. static int fusd_init_needed = 1;
  89. if (fusd_init_needed)
  90. {
  91. int i;
  92. fusd_init_needed = 0;
  93. for (i = 0; i < FD_SETSIZE; i++)
  94. FUSD_SET_FOPS(i, &null_fops);
  95. FD_ZERO(&fusd_fds);
  96. dev_root = DEFAULT_DEV_ROOT;
  97. }
  98. }
  99. int fusd_register(const char *name, const char* clazz, const char* devname, mode_t mode, void *device_info,
  100. struct fusd_file_operations *fops)
  101. {
  102. int fd = -1, retval = 0;
  103. fusd_msg_t message;
  104. /* need initialization? */
  105. fusd_init();
  106. /* make sure the name is valid and we have a valid set of fops... */
  107. if (name == NULL || fops == NULL)
  108. {
  109. fprintf(stderr, "fusd_register: invalid name or fops argument\n");
  110. retval = -EINVAL;
  111. goto done;
  112. }
  113. /*
  114. * convenience: if the first characters of the name you're trying
  115. * to register are SKIP_PREFIX (usually "/dev/"), skip over them.
  116. */
  117. if (dev_root != NULL && strlen(name) > strlen(dev_root) &&
  118. !strncmp(name, dev_root, strlen(dev_root)))
  119. {
  120. name += strlen(dev_root);
  121. }
  122. if (strlen(name) > FUSD_MAX_NAME_LENGTH)
  123. {
  124. fprintf(stderr, "libfusd: name '%s' too long, sorry :(\n", name);
  125. retval = -EINVAL;
  126. goto done;
  127. }
  128. /* open the fusd control channel */
  129. if ((fd = open(FUSD_CONTROL_DEVNAME, O_RDWR | O_NONBLOCK)) < 0)
  130. {
  131. /* if the problem is that /dev/fusd does not exist, return the
  132. * message "Package not installed", which is hopefully more
  133. * illuminating than "no such file or directory" */
  134. if (errno == ENOENT)
  135. {
  136. fprintf(stderr, "libfusd: %s does not exist; ensure FUSD's kernel module is installed\n",
  137. FUSD_CONTROL_DEVNAME);
  138. retval = -ENOPKG;
  139. }
  140. else
  141. {
  142. perror("libfusd: trying to open FUSD control channel");
  143. retval = -errno;
  144. }
  145. goto done;
  146. }
  147. /* fd in use? */
  148. if (FUSD_FD_VALID(fd))
  149. {
  150. retval = -EBADF;
  151. goto done;
  152. }
  153. /* set up the message */
  154. memset(&message, 0, sizeof(message));
  155. message.magic = FUSD_MSG_MAGIC;
  156. message.cmd = FUSD_REGISTER_DEVICE;
  157. message.datalen = 0;
  158. strcpy(message.parm.register_msg.name, name);
  159. strcpy(message.parm.register_msg.clazz, clazz);
  160. strcpy(message.parm.register_msg.devname, devname);
  161. message.parm.register_msg.mode = mode;
  162. message.parm.register_msg.device_info = device_info;
  163. /* make the request */
  164. if (write(fd, &message, sizeof(fusd_msg_t)) < 0)
  165. {
  166. retval = -errno;
  167. goto done;
  168. }
  169. /* OK, store the new file state */
  170. FUSD_SET_FOPS(fd, fops);
  171. FD_SET(fd, &fusd_fds);
  172. /* success! */
  173. done:
  174. if (retval < 0)
  175. {
  176. if (fd >= 0)
  177. close(fd);
  178. errno = -retval;
  179. retval = -1;
  180. }
  181. else
  182. {
  183. errno = 0;
  184. retval = fd;
  185. }
  186. return retval;
  187. }
  188. int fusd_unregister(int fd)
  189. {
  190. int ret = -1;
  191. if (FUSD_FD_VALID(fd))
  192. {
  193. /* clear fd location */
  194. FUSD_SET_FOPS(fd, &null_fops);
  195. FD_CLR(fd, &fusd_fds);
  196. /* close */
  197. ret = close(fd);
  198. }
  199. else
  200. {
  201. errno = EBADF;
  202. }
  203. return ret;
  204. }
  205. /*
  206. * fusd_run: a convenience function for automatically running a FUSD
  207. * driver, for drivers that don't want to manually select on file
  208. * descriptors and call fusd_dispatch. This function will
  209. * automatically select on all devices the user has registered and
  210. * call fusd_dispatch on any one that becomes readable.
  211. */
  212. void fusd_run(void)
  213. {
  214. fd_set tfds;
  215. int status;
  216. int maxfd;
  217. int i;
  218. /* locate maxmimum fd in use */
  219. for (maxfd=0, i=0; i < FD_SETSIZE; i++)
  220. {
  221. if (FD_ISSET(i, &fusd_fds))
  222. {
  223. maxfd = i;
  224. }
  225. }
  226. maxfd++;
  227. while (1)
  228. {
  229. /* select */
  230. memmove(&tfds, &fusd_fds, sizeof(fd_set));
  231. status = select(maxfd, &tfds, NULL, NULL, NULL);
  232. /* error? */
  233. if (status < 0)
  234. {
  235. perror("libfusd: fusd_run: error on select");
  236. continue;
  237. }
  238. /* readable? */
  239. for (i = 0; i < maxfd; i++)
  240. if (FD_ISSET(i, &tfds))
  241. fusd_dispatch(i);
  242. }
  243. }
  244. /************************************************************************/
  245. /* reads a fusd kernel-to-userspace message from fd, and puts a
  246. * fusd_msg into the memory pointed to by msg (we assume we are passed
  247. * a buffer managed by the caller). if there is a data portion to the
  248. * message (msg->datalen > 0), we allocate memory for it, set data to
  249. * point to that memory. the returned data pointer must also be
  250. * managed by the caller. */
  251. static int fusd_get_message(int fd, fusd_msg_t *msg)
  252. {
  253. int ret;
  254. /* read the header part into the kernel */
  255. if (read(fd, msg, sizeof(fusd_msg_t)) < 0)
  256. {
  257. if (errno != EAGAIN)
  258. perror("error talking to FUSD control channel on header read");
  259. ret = -errno;
  260. goto exit;
  261. }
  262. msg->data = NULL; /* pointers in kernelspace have no meaning */
  263. if (msg->magic != FUSD_MSG_MAGIC)
  264. {
  265. fprintf(stderr, "libfusd: magic number failure\n");
  266. ret = -EINVAL;
  267. goto exit;
  268. }
  269. /* if there's a data part to the message, read it from the kernel. */
  270. if (msg->datalen)
  271. {
  272. if ((msg->data = malloc(msg->datalen + 1)) == NULL)
  273. {
  274. fprintf(stderr, "libfusd: can't allocate memory\n");
  275. ret = -ENOMEM; /* this is bad, we are now unsynced */
  276. goto exit;
  277. }
  278. if (read(fd, msg->data, msg->datalen) < 0)
  279. {
  280. perror("error talking to FUSD control channel on data read");
  281. free(msg->data);
  282. msg->data = NULL;
  283. ret = -EIO;
  284. goto exit;
  285. }
  286. /* For convenience, we now ensure that the byte *after* the buffer
  287. * is set to 0. (Note we malloc'd one extra byte above.) */
  288. msg->data[msg->datalen] = '\0';
  289. }
  290. ret = 0;
  291. exit:
  292. return ret;
  293. }
  294. /*
  295. * fusd_fdset_add: given an FDSET and "max", add the currently valid
  296. * FUSD fds to the set and update max accordingly.
  297. */
  298. void fusd_fdset_add(fd_set *set, int *max)
  299. {
  300. int i;
  301. for (i = 0; i < FD_SETSIZE; i++)
  302. {
  303. if (FD_ISSET(i, &fusd_fds))
  304. {
  305. FD_SET(i, set);
  306. if (i > *max)
  307. {
  308. *max = i;
  309. }
  310. }
  311. }
  312. }
  313. /*
  314. * fusd_dispatch_fdset: given an fd_set full of descriptors, call
  315. * fusd_dispatch on every descriptor in the set which is a valid FUSD
  316. * fd.
  317. */
  318. void fusd_dispatch_fdset(fd_set *set)
  319. {
  320. int i;
  321. for (i = 0; i < FD_SETSIZE; i++)
  322. if (FD_ISSET(i, set) && FD_ISSET(i, &fusd_fds))
  323. fusd_dispatch(i);
  324. }
  325. /*
  326. * fusd_dispatch_one() -- read a single kernel-to-userspace message
  327. * from fd, then call the appropriate userspace callback function,
  328. * based on the message that was read. finally, return the result
  329. * back to the kernel, IF the return value from the callback is not
  330. * FUSD_NOREPLY.
  331. *
  332. * On success, returns 0.
  333. * On failure, returns a negative number indicating the errno.
  334. */
  335. static int fusd_dispatch_one(int fd, fusd_file_operations_t *fops)
  336. {
  337. fusd_file_info_t *file = NULL;
  338. fusd_msg_t *msg = NULL;
  339. int driver_retval = 0; /* returned to the FUSD driver */
  340. int user_retval = 0; /* returned to the user who made the syscall */
  341. /* check for valid, look up ops */
  342. if (fops == NULL)
  343. {
  344. fprintf(stderr, "fusd_dispatch: no fops provided!\n");
  345. driver_retval = -EBADF;
  346. goto out_noreply;
  347. }
  348. /* allocate memory for fusd_msg_t */
  349. if ((msg = malloc(sizeof(fusd_msg_t))) == NULL)
  350. {
  351. driver_retval = -ENOMEM;
  352. fprintf(stderr, "libfusd: can't allocate memory\n");
  353. goto out_noreply;
  354. }
  355. memset(msg, '\0', sizeof(fusd_msg_t));
  356. /* read header and data, if it's there */
  357. if ((driver_retval = fusd_get_message(fd, msg)) < 0)
  358. goto out_noreply;
  359. /* allocate file info struct */
  360. file = malloc(sizeof(fusd_file_info_t));
  361. if (NULL == file)
  362. {
  363. fprintf(stderr, "libfusd: can't allocate memory\n");
  364. driver_retval = -ENOMEM;
  365. goto out_noreply;
  366. }
  367. /* fill the file info struct */
  368. memset(file, '\0', sizeof(fusd_file_info_t));
  369. pthread_mutex_init(&file->lock, NULL);
  370. FILE_LOCK(file);
  371. file->fd = fd;
  372. file->device_info = msg->parm.fops_msg.device_info;
  373. file->private_data = msg->parm.fops_msg.private_info;
  374. file->flags = msg->parm.fops_msg.flags;
  375. file->pid = msg->parm.fops_msg.pid;
  376. file->uid = msg->parm.fops_msg.uid;
  377. file->gid = msg->parm.fops_msg.gid;
  378. file->fusd_msg = msg;
  379. FILE_UNLOCK(file);
  380. /* right now we only handle fops requests */
  381. if (msg->cmd != FUSD_FOPS_CALL && msg->cmd != FUSD_FOPS_NONBLOCK &&
  382. msg->cmd != FUSD_FOPS_CALL_DROPREPLY)
  383. {
  384. fprintf(stderr, "libfusd: got unknown msg->cmd from kernel\n");
  385. user_retval = -EINVAL;
  386. goto send_reply;
  387. }
  388. /* dispatch on operation type */
  389. user_retval = -ENOSYS;
  390. //printf("dispatch_one: subcmd: %d - ", msg->subcmd);
  391. switch (msg->subcmd)
  392. {
  393. case FUSD_OPEN:
  394. //printf("FUSD_OPEN\n");
  395. if (fops && fops->open)
  396. user_retval = fops->open(file);
  397. break;
  398. case FUSD_CLOSE:
  399. //printf("FUSD_CLOSE\n");
  400. if (fops && fops->close)
  401. user_retval = fops->close(file);
  402. break;
  403. case FUSD_READ:
  404. //printf("FUSD_READ\n");
  405. /* allocate a buffer and make the call */
  406. if (fops && fops->read)
  407. {
  408. if ((msg->data = malloc(msg->parm.fops_msg.length)) == NULL)
  409. {
  410. user_retval = -ENOMEM;
  411. fprintf(stderr, "libfusd: can't allocate memory\n");
  412. }
  413. else
  414. {
  415. msg->datalen = msg->parm.fops_msg.length;
  416. user_retval = fops->read(file, msg->data, msg->datalen,
  417. &msg->parm.fops_msg.offset);
  418. }
  419. }
  420. break;
  421. case FUSD_WRITE:
  422. //printf("FUSD_WRITE\n");
  423. if (fops && fops->write)
  424. user_retval = fops->write(file, msg->data, msg->datalen,
  425. &msg->parm.fops_msg.offset);
  426. break;
  427. case FUSD_MMAP:
  428. //printf("FUSD_MMAP\n");
  429. if (fops && fops->mmap)
  430. {
  431. user_retval = fops->mmap(file, msg->parm.fops_msg.offset, msg->parm.fops_msg.length, msg->parm.fops_msg.flags,
  432. &msg->parm.fops_msg.arg.ptr_arg, &msg->parm.fops_msg.length);
  433. }
  434. break;
  435. case FUSD_IOCTL:
  436. //printf("FUSD_IOCTL\n");
  437. if (fops && fops->ioctl)
  438. {
  439. /* in the case of an ioctl read, allocate a buffer for the
  440. * driver to write to, IF there isn't already a buffer. (there
  441. * might already be a buffer if this is a read+write) */
  442. if ((_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) &&
  443. msg->data == NULL)
  444. {
  445. msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
  446. if ((msg->data = malloc(msg->datalen)) == NULL)
  447. {
  448. user_retval = -ENOMEM;
  449. break;
  450. }
  451. }
  452. if (msg->data != NULL)
  453. user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd, msg->data);
  454. else
  455. user_retval = fops->ioctl(file, msg->parm.fops_msg.cmd,
  456. (void *) msg->parm.fops_msg.arg.ptr_arg);
  457. }
  458. break;
  459. case FUSD_POLL_DIFF:
  460. //printf("FUSD_POLL_DIFF\n");
  461. /* This callback requests notification when an event occurs on a file,
  462. * e.g. becoming readable or writable */
  463. if (fops && fops->poll_diff)
  464. user_retval = fops->poll_diff(file, msg->parm.fops_msg.cmd);
  465. break;
  466. case FUSD_UNBLOCK:
  467. //printf("FUSD_UNBLOCK\n");
  468. /* This callback is called when a system call is interrupted */
  469. if (fops && fops->unblock)
  470. user_retval = fops->unblock(file);
  471. break;
  472. default:
  473. fprintf(stderr, "libfusd: Got unsupported operation\n");
  474. user_retval = -ENOSYS;
  475. break;
  476. }
  477. goto send_reply;
  478. /* out_noreply is only used for handling errors */
  479. out_noreply:
  480. if (msg->data != NULL)
  481. free(msg->data);
  482. if (msg != NULL)
  483. free(msg);
  484. goto done;
  485. /* send_reply is only used for success */
  486. send_reply:
  487. if (-user_retval <= 0xff)
  488. {
  489. /* 0xff is the maximum legal return value (?) - return val to user */
  490. driver_retval = fusd_return(file, user_retval);
  491. }
  492. else
  493. {
  494. /* if we got a FUSD_NOREPLY, don't free the msg structure */
  495. driver_retval = 0;
  496. }
  497. /* this is common to both errors and success */
  498. done:
  499. if (driver_retval < 0)
  500. {
  501. errno = -driver_retval;
  502. driver_retval = -1;
  503. }
  504. return driver_retval;
  505. }
  506. /* fusd_dispatch is now a wrapper around fusd_dispatch_one that calls
  507. * it repeatedly, until it fails. this helps a lot with bulk data
  508. * transfer since there is no intermediate select in between the
  509. * reads. (the kernel module helps by running the user process in
  510. * between).
  511. *
  512. * This function now prints an error to stderr in case of error,
  513. * instead of returning a -1.
  514. */
  515. void fusd_dispatch(int fd)
  516. {
  517. int retval, num_dispatches = 0;
  518. fusd_file_operations_t *fops = NULL;
  519. /* make sure we have a valid FD, and get its fops structure */
  520. if (!FUSD_FD_VALID(fd))
  521. {
  522. errno = EBADF;
  523. retval = -1;
  524. fprintf(stderr, "libfusd: not a valid FUSD FD\n");
  525. goto out;
  526. }
  527. fops = FUSD_GET_FOPS(fd);
  528. /* now keep dispatching until a dispatch returns an error */
  529. do
  530. {
  531. retval = fusd_dispatch_one(fd, fops);
  532. if (retval >= 0)
  533. num_dispatches++;
  534. } while (retval >= 0 && num_dispatches <= MAX_MESSAGES_PER_DISPATCH);
  535. /* if we've dispatched at least one message successfully, and then
  536. * stopped because of EAGAIN - do not report an error. this is the
  537. * common case. */
  538. if (num_dispatches > 0 && errno == EAGAIN)
  539. {
  540. retval = 0;
  541. errno = 0;
  542. }
  543. out:
  544. if (retval < 0 && errno != EPIPE)
  545. fprintf(stderr, "libfusd: fusd_dispatch error on fd %d: [%d] %m\n", fd, retval);
  546. }
  547. /*
  548. * fusd_destroy destroys all state associated with a fusd_file_info
  549. * pointer. (It is implicitly called by fusd_return.) If a driver
  550. * saves a fusd_file_info pointer by calling -FUSD_NOREPLY in order to
  551. * block a read, but gets a "close" request on the file before the
  552. * pointer is returned with fusd_return, it should be thrown away
  553. * using fusd_destroy.
  554. */
  555. void fusd_destroy(struct fusd_file_info *file)
  556. {
  557. if (file == NULL)
  558. return;
  559. FILE_LOCK(file);
  560. if (file->fusd_msg != NULL)
  561. {
  562. if (file->fusd_msg->data != NULL)
  563. {
  564. free(file->fusd_msg->data);
  565. file->fusd_msg->data = NULL;
  566. }
  567. free(file->fusd_msg);
  568. file->fusd_msg = NULL;
  569. }
  570. FILE_UNLOCK(file);
  571. pthread_mutex_destroy(&file->lock);
  572. free(file);
  573. }
  574. /*
  575. * construct a user-to-kernel message in reply to a file function
  576. * call.
  577. *
  578. * On success, returns 0.
  579. * On failure, returns a negative number indicating the errno.
  580. */
  581. int fusd_return(fusd_file_info_t *file, ssize_t retval)
  582. {
  583. fusd_msg_t *msg = NULL;
  584. int fd;
  585. int driver_retval = 0;
  586. int ret;
  587. struct iovec iov[2];
  588. if (file == NULL)
  589. {
  590. fprintf(stderr, "fusd_return: NULL file\n");
  591. ret = -EINVAL;
  592. goto exit;
  593. }
  594. FILE_LOCK(file);
  595. fd = file->fd;
  596. if (!FUSD_FD_VALID(fd))
  597. {
  598. fprintf(stderr, "fusd_return: badfd (fd %d)\n", fd);
  599. ret = -EBADF;
  600. goto exit_unlock;
  601. }
  602. if ((msg = file->fusd_msg) == NULL)
  603. {
  604. fprintf(stderr, "fusd_return: fusd_msg is gone\n");
  605. ret = -EINVAL;
  606. goto exit_unlock;
  607. }
  608. /* if this was a "DONTREPLY" message, just free the struct */
  609. if (msg->cmd == FUSD_FOPS_CALL_DROPREPLY)
  610. goto free_memory;
  611. /* do we copy data back to kernel? how much? */
  612. switch(msg->subcmd)
  613. {
  614. case FUSD_READ:
  615. /* these operations can return data to userspace */
  616. if (retval > 0)
  617. {
  618. msg->datalen = MIN((int)retval, (int)msg->parm.fops_msg.length);
  619. retval = msg->datalen;
  620. }
  621. else
  622. {
  623. msg->datalen = 0;
  624. }
  625. break;
  626. case FUSD_IOCTL:
  627. /* ioctl CAN (in read mode) return data to userspace */
  628. if (/*(retval == 0) && */ (_IOC_DIR(msg->parm.fops_msg.cmd) & _IOC_READ) )
  629. msg->datalen = _IOC_SIZE(msg->parm.fops_msg.cmd);
  630. else
  631. msg->datalen = 0;
  632. break;
  633. default:
  634. /* open, close, write, etc. do not return data */
  635. msg->datalen = 0;
  636. break;
  637. }
  638. /* fill the file info struct */
  639. msg->cmd++; /* change FOPS_CALL to FOPS_REPLY; NONBLOCK to NONBLOCK_REPLY */
  640. msg->parm.fops_msg.retval = retval;
  641. msg->parm.fops_msg.device_info = file->device_info;
  642. msg->parm.fops_msg.private_info = file->private_data;
  643. msg->parm.fops_msg.flags = file->flags;
  644. /* pid is NOT copied back. */
  645. /* send message to kernel */
  646. if (msg->datalen && msg->data != NULL)
  647. {
  648. //printf("(msg->datalen [%d] && msg->data != NULL [%p]", msg->datalen, msg->data);
  649. iov[0].iov_base = msg;
  650. iov[0].iov_len = sizeof(fusd_msg_t);
  651. iov[1].iov_base = msg->data;
  652. iov[1].iov_len = msg->datalen;
  653. driver_retval = writev(fd, iov, 2);
  654. }
  655. else
  656. {
  657. driver_retval = write(fd, msg, sizeof(fusd_msg_t));
  658. }
  659. free_memory:
  660. FILE_UNLOCK(file);
  661. fusd_destroy(file);
  662. ret = 0;
  663. if (driver_retval < 0)
  664. ret = -errno;
  665. goto exit;
  666. exit_unlock:
  667. FILE_UNLOCK(file);
  668. exit:
  669. return ret;
  670. }
  671. /* returns static string representing the flagset (e.g. RWE) */
  672. #define RING 5
  673. char *fusd_unparse_flags(int flags)
  674. {
  675. static int i = 0;
  676. static char ringbuf[RING][5];
  677. char *s = ringbuf[i];
  678. i = (i + 1) % RING;
  679. sprintf(s, "%c%c%c",
  680. (flags & FUSD_NOTIFY_INPUT)?'R':'-',
  681. (flags & FUSD_NOTIFY_OUTPUT)?'W':'-',
  682. (flags & FUSD_NOTIFY_EXCEPT)?'E':'-');
  683. return s;
  684. }
  685. #undef RING