libfusd.c 19 KB

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