libfusd.c 19 KB

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