dev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/poll.h>
  11. #include <linux/uio.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/file.h>
  15. #include <linux/slab.h>
  16. MODULE_ALIAS_MISCDEV(FUSE_MINOR);
  17. static kmem_cache_t *fuse_req_cachep;
  18. static inline struct fuse_conn *fuse_get_conn(struct file *file)
  19. {
  20. struct fuse_conn *fc;
  21. spin_lock(&fuse_lock);
  22. fc = file->private_data;
  23. if (fc && !fc->mounted)
  24. fc = NULL;
  25. spin_unlock(&fuse_lock);
  26. return fc;
  27. }
  28. static inline void fuse_request_init(struct fuse_req *req)
  29. {
  30. memset(req, 0, sizeof(*req));
  31. INIT_LIST_HEAD(&req->list);
  32. init_waitqueue_head(&req->waitq);
  33. atomic_set(&req->count, 1);
  34. }
  35. struct fuse_req *fuse_request_alloc(void)
  36. {
  37. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL);
  38. if (req)
  39. fuse_request_init(req);
  40. return req;
  41. }
  42. void fuse_request_free(struct fuse_req *req)
  43. {
  44. kmem_cache_free(fuse_req_cachep, req);
  45. }
  46. static inline void block_sigs(sigset_t *oldset)
  47. {
  48. sigset_t mask;
  49. siginitsetinv(&mask, sigmask(SIGKILL));
  50. sigprocmask(SIG_BLOCK, &mask, oldset);
  51. }
  52. static inline void restore_sigs(sigset_t *oldset)
  53. {
  54. sigprocmask(SIG_SETMASK, oldset, NULL);
  55. }
  56. void fuse_reset_request(struct fuse_req *req)
  57. {
  58. int preallocated = req->preallocated;
  59. BUG_ON(atomic_read(&req->count) != 1);
  60. fuse_request_init(req);
  61. req->preallocated = preallocated;
  62. }
  63. static void __fuse_get_request(struct fuse_req *req)
  64. {
  65. atomic_inc(&req->count);
  66. }
  67. /* Must be called with > 1 refcount */
  68. static void __fuse_put_request(struct fuse_req *req)
  69. {
  70. BUG_ON(atomic_read(&req->count) < 2);
  71. atomic_dec(&req->count);
  72. }
  73. static struct fuse_req *do_get_request(struct fuse_conn *fc)
  74. {
  75. struct fuse_req *req;
  76. spin_lock(&fuse_lock);
  77. BUG_ON(list_empty(&fc->unused_list));
  78. req = list_entry(fc->unused_list.next, struct fuse_req, list);
  79. list_del_init(&req->list);
  80. spin_unlock(&fuse_lock);
  81. fuse_request_init(req);
  82. req->preallocated = 1;
  83. req->in.h.uid = current->fsuid;
  84. req->in.h.gid = current->fsgid;
  85. req->in.h.pid = current->pid;
  86. return req;
  87. }
  88. /* This can return NULL, but only in case it's interrupted by a SIGKILL */
  89. struct fuse_req *fuse_get_request(struct fuse_conn *fc)
  90. {
  91. int intr;
  92. sigset_t oldset;
  93. block_sigs(&oldset);
  94. intr = down_interruptible(&fc->outstanding_sem);
  95. restore_sigs(&oldset);
  96. return intr ? NULL : do_get_request(fc);
  97. }
  98. static void fuse_putback_request(struct fuse_conn *fc, struct fuse_req *req)
  99. {
  100. spin_lock(&fuse_lock);
  101. if (req->preallocated)
  102. list_add(&req->list, &fc->unused_list);
  103. else
  104. fuse_request_free(req);
  105. /* If we are in debt decrease that first */
  106. if (fc->outstanding_debt)
  107. fc->outstanding_debt--;
  108. else
  109. up(&fc->outstanding_sem);
  110. spin_unlock(&fuse_lock);
  111. }
  112. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
  113. {
  114. if (atomic_dec_and_test(&req->count))
  115. fuse_putback_request(fc, req);
  116. }
  117. void fuse_release_background(struct fuse_req *req)
  118. {
  119. iput(req->inode);
  120. iput(req->inode2);
  121. if (req->file)
  122. fput(req->file);
  123. spin_lock(&fuse_lock);
  124. list_del(&req->bg_entry);
  125. spin_unlock(&fuse_lock);
  126. }
  127. /*
  128. * This function is called when a request is finished. Either a reply
  129. * has arrived or it was interrupted (and not yet sent) or some error
  130. * occurred during communication with userspace, or the device file was
  131. * closed. It decreases the reference count for the request. In case
  132. * of a background request the reference to the stored objects are
  133. * released. The requester thread is woken up (if still waiting), and
  134. * finally the request is either freed or put on the unused_list
  135. *
  136. * Called with fuse_lock, unlocks it
  137. */
  138. static void request_end(struct fuse_conn *fc, struct fuse_req *req)
  139. {
  140. int putback;
  141. req->finished = 1;
  142. putback = atomic_dec_and_test(&req->count);
  143. spin_unlock(&fuse_lock);
  144. if (req->background) {
  145. down_read(&fc->sbput_sem);
  146. if (fc->mounted)
  147. fuse_release_background(req);
  148. up_read(&fc->sbput_sem);
  149. }
  150. wake_up(&req->waitq);
  151. if (req->in.h.opcode == FUSE_INIT) {
  152. int i;
  153. if (req->misc.init_in_out.major != FUSE_KERNEL_VERSION)
  154. fc->conn_error = 1;
  155. /* After INIT reply is received other requests can go
  156. out. So do (FUSE_MAX_OUTSTANDING - 1) number of
  157. up()s on outstanding_sem. The last up() is done in
  158. fuse_putback_request() */
  159. for (i = 1; i < FUSE_MAX_OUTSTANDING; i++)
  160. up(&fc->outstanding_sem);
  161. } else if (req->in.h.opcode == FUSE_RELEASE && req->inode == NULL) {
  162. /* Special case for failed iget in CREATE */
  163. u64 nodeid = req->in.h.nodeid;
  164. __fuse_get_request(req);
  165. fuse_reset_request(req);
  166. fuse_send_forget(fc, req, nodeid, 1);
  167. putback = 0;
  168. }
  169. if (putback)
  170. fuse_putback_request(fc, req);
  171. }
  172. /*
  173. * Unfortunately request interruption not just solves the deadlock
  174. * problem, it causes problems too. These stem from the fact, that an
  175. * interrupted request is continued to be processed in userspace,
  176. * while all the locks and object references (inode and file) held
  177. * during the operation are released.
  178. *
  179. * To release the locks is exactly why there's a need to interrupt the
  180. * request, so there's not a lot that can be done about this, except
  181. * introduce additional locking in userspace.
  182. *
  183. * More important is to keep inode and file references until userspace
  184. * has replied, otherwise FORGET and RELEASE could be sent while the
  185. * inode/file is still used by the filesystem.
  186. *
  187. * For this reason the concept of "background" request is introduced.
  188. * An interrupted request is backgrounded if it has been already sent
  189. * to userspace. Backgrounding involves getting an extra reference to
  190. * inode(s) or file used in the request, and adding the request to
  191. * fc->background list. When a reply is received for a background
  192. * request, the object references are released, and the request is
  193. * removed from the list. If the filesystem is unmounted while there
  194. * are still background requests, the list is walked and references
  195. * are released as if a reply was received.
  196. *
  197. * There's one more use for a background request. The RELEASE message is
  198. * always sent as background, since it doesn't return an error or
  199. * data.
  200. */
  201. static void background_request(struct fuse_conn *fc, struct fuse_req *req)
  202. {
  203. req->background = 1;
  204. list_add(&req->bg_entry, &fc->background);
  205. if (req->inode)
  206. req->inode = igrab(req->inode);
  207. if (req->inode2)
  208. req->inode2 = igrab(req->inode2);
  209. if (req->file)
  210. get_file(req->file);
  211. }
  212. /* Called with fuse_lock held. Releases, and then reacquires it. */
  213. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  214. {
  215. sigset_t oldset;
  216. spin_unlock(&fuse_lock);
  217. block_sigs(&oldset);
  218. wait_event_interruptible(req->waitq, req->finished);
  219. restore_sigs(&oldset);
  220. spin_lock(&fuse_lock);
  221. if (req->finished)
  222. return;
  223. req->out.h.error = -EINTR;
  224. req->interrupted = 1;
  225. if (req->locked) {
  226. /* This is uninterruptible sleep, because data is
  227. being copied to/from the buffers of req. During
  228. locked state, there mustn't be any filesystem
  229. operation (e.g. page fault), since that could lead
  230. to deadlock */
  231. spin_unlock(&fuse_lock);
  232. wait_event(req->waitq, !req->locked);
  233. spin_lock(&fuse_lock);
  234. }
  235. if (!req->sent && !list_empty(&req->list)) {
  236. list_del(&req->list);
  237. __fuse_put_request(req);
  238. } else if (!req->finished && req->sent)
  239. background_request(fc, req);
  240. }
  241. static unsigned len_args(unsigned numargs, struct fuse_arg *args)
  242. {
  243. unsigned nbytes = 0;
  244. unsigned i;
  245. for (i = 0; i < numargs; i++)
  246. nbytes += args[i].size;
  247. return nbytes;
  248. }
  249. static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
  250. {
  251. fc->reqctr++;
  252. /* zero is special */
  253. if (fc->reqctr == 0)
  254. fc->reqctr = 1;
  255. req->in.h.unique = fc->reqctr;
  256. req->in.h.len = sizeof(struct fuse_in_header) +
  257. len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
  258. if (!req->preallocated) {
  259. /* If request is not preallocated (either FORGET or
  260. RELEASE), then still decrease outstanding_sem, so
  261. user can't open infinite number of files while not
  262. processing the RELEASE requests. However for
  263. efficiency do it without blocking, so if down()
  264. would block, just increase the debt instead */
  265. if (down_trylock(&fc->outstanding_sem))
  266. fc->outstanding_debt++;
  267. }
  268. list_add_tail(&req->list, &fc->pending);
  269. wake_up(&fc->waitq);
  270. }
  271. /*
  272. * This can only be interrupted by a SIGKILL
  273. */
  274. void request_send(struct fuse_conn *fc, struct fuse_req *req)
  275. {
  276. req->isreply = 1;
  277. spin_lock(&fuse_lock);
  278. if (!fc->connected)
  279. req->out.h.error = -ENOTCONN;
  280. else if (fc->conn_error)
  281. req->out.h.error = -ECONNREFUSED;
  282. else {
  283. queue_request(fc, req);
  284. /* acquire extra reference, since request is still needed
  285. after request_end() */
  286. __fuse_get_request(req);
  287. request_wait_answer(fc, req);
  288. }
  289. spin_unlock(&fuse_lock);
  290. }
  291. static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
  292. {
  293. spin_lock(&fuse_lock);
  294. if (fc->connected) {
  295. queue_request(fc, req);
  296. spin_unlock(&fuse_lock);
  297. } else {
  298. req->out.h.error = -ENOTCONN;
  299. request_end(fc, req);
  300. }
  301. }
  302. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
  303. {
  304. req->isreply = 0;
  305. request_send_nowait(fc, req);
  306. }
  307. void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  308. {
  309. req->isreply = 1;
  310. spin_lock(&fuse_lock);
  311. background_request(fc, req);
  312. spin_unlock(&fuse_lock);
  313. request_send_nowait(fc, req);
  314. }
  315. void fuse_send_init(struct fuse_conn *fc)
  316. {
  317. /* This is called from fuse_read_super() so there's guaranteed
  318. to be a request available */
  319. struct fuse_req *req = do_get_request(fc);
  320. struct fuse_init_in_out *arg = &req->misc.init_in_out;
  321. arg->major = FUSE_KERNEL_VERSION;
  322. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  323. req->in.h.opcode = FUSE_INIT;
  324. req->in.numargs = 1;
  325. req->in.args[0].size = sizeof(*arg);
  326. req->in.args[0].value = arg;
  327. req->out.numargs = 1;
  328. req->out.args[0].size = sizeof(*arg);
  329. req->out.args[0].value = arg;
  330. request_send_background(fc, req);
  331. }
  332. /*
  333. * Lock the request. Up to the next unlock_request() there mustn't be
  334. * anything that could cause a page-fault. If the request was already
  335. * interrupted bail out.
  336. */
  337. static inline int lock_request(struct fuse_req *req)
  338. {
  339. int err = 0;
  340. if (req) {
  341. spin_lock(&fuse_lock);
  342. if (req->interrupted)
  343. err = -ENOENT;
  344. else
  345. req->locked = 1;
  346. spin_unlock(&fuse_lock);
  347. }
  348. return err;
  349. }
  350. /*
  351. * Unlock request. If it was interrupted during being locked, the
  352. * requester thread is currently waiting for it to be unlocked, so
  353. * wake it up.
  354. */
  355. static inline void unlock_request(struct fuse_req *req)
  356. {
  357. if (req) {
  358. spin_lock(&fuse_lock);
  359. req->locked = 0;
  360. if (req->interrupted)
  361. wake_up(&req->waitq);
  362. spin_unlock(&fuse_lock);
  363. }
  364. }
  365. struct fuse_copy_state {
  366. int write;
  367. struct fuse_req *req;
  368. const struct iovec *iov;
  369. unsigned long nr_segs;
  370. unsigned long seglen;
  371. unsigned long addr;
  372. struct page *pg;
  373. void *mapaddr;
  374. void *buf;
  375. unsigned len;
  376. };
  377. static void fuse_copy_init(struct fuse_copy_state *cs, int write,
  378. struct fuse_req *req, const struct iovec *iov,
  379. unsigned long nr_segs)
  380. {
  381. memset(cs, 0, sizeof(*cs));
  382. cs->write = write;
  383. cs->req = req;
  384. cs->iov = iov;
  385. cs->nr_segs = nr_segs;
  386. }
  387. /* Unmap and put previous page of userspace buffer */
  388. static inline void fuse_copy_finish(struct fuse_copy_state *cs)
  389. {
  390. if (cs->mapaddr) {
  391. kunmap_atomic(cs->mapaddr, KM_USER0);
  392. if (cs->write) {
  393. flush_dcache_page(cs->pg);
  394. set_page_dirty_lock(cs->pg);
  395. }
  396. put_page(cs->pg);
  397. cs->mapaddr = NULL;
  398. }
  399. }
  400. /*
  401. * Get another pagefull of userspace buffer, and map it to kernel
  402. * address space, and lock request
  403. */
  404. static int fuse_copy_fill(struct fuse_copy_state *cs)
  405. {
  406. unsigned long offset;
  407. int err;
  408. unlock_request(cs->req);
  409. fuse_copy_finish(cs);
  410. if (!cs->seglen) {
  411. BUG_ON(!cs->nr_segs);
  412. cs->seglen = cs->iov[0].iov_len;
  413. cs->addr = (unsigned long) cs->iov[0].iov_base;
  414. cs->iov ++;
  415. cs->nr_segs --;
  416. }
  417. down_read(&current->mm->mmap_sem);
  418. err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
  419. &cs->pg, NULL);
  420. up_read(&current->mm->mmap_sem);
  421. if (err < 0)
  422. return err;
  423. BUG_ON(err != 1);
  424. offset = cs->addr % PAGE_SIZE;
  425. cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
  426. cs->buf = cs->mapaddr + offset;
  427. cs->len = min(PAGE_SIZE - offset, cs->seglen);
  428. cs->seglen -= cs->len;
  429. cs->addr += cs->len;
  430. return lock_request(cs->req);
  431. }
  432. /* Do as much copy to/from userspace buffer as we can */
  433. static inline int fuse_copy_do(struct fuse_copy_state *cs, void **val,
  434. unsigned *size)
  435. {
  436. unsigned ncpy = min(*size, cs->len);
  437. if (val) {
  438. if (cs->write)
  439. memcpy(cs->buf, *val, ncpy);
  440. else
  441. memcpy(*val, cs->buf, ncpy);
  442. *val += ncpy;
  443. }
  444. *size -= ncpy;
  445. cs->len -= ncpy;
  446. cs->buf += ncpy;
  447. return ncpy;
  448. }
  449. /*
  450. * Copy a page in the request to/from the userspace buffer. Must be
  451. * done atomically
  452. */
  453. static inline int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
  454. unsigned offset, unsigned count, int zeroing)
  455. {
  456. if (page && zeroing && count < PAGE_SIZE) {
  457. void *mapaddr = kmap_atomic(page, KM_USER1);
  458. memset(mapaddr, 0, PAGE_SIZE);
  459. kunmap_atomic(mapaddr, KM_USER1);
  460. }
  461. while (count) {
  462. int err;
  463. if (!cs->len && (err = fuse_copy_fill(cs)))
  464. return err;
  465. if (page) {
  466. void *mapaddr = kmap_atomic(page, KM_USER1);
  467. void *buf = mapaddr + offset;
  468. offset += fuse_copy_do(cs, &buf, &count);
  469. kunmap_atomic(mapaddr, KM_USER1);
  470. } else
  471. offset += fuse_copy_do(cs, NULL, &count);
  472. }
  473. if (page && !cs->write)
  474. flush_dcache_page(page);
  475. return 0;
  476. }
  477. /* Copy pages in the request to/from userspace buffer */
  478. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  479. int zeroing)
  480. {
  481. unsigned i;
  482. struct fuse_req *req = cs->req;
  483. unsigned offset = req->page_offset;
  484. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  485. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  486. struct page *page = req->pages[i];
  487. int err = fuse_copy_page(cs, page, offset, count, zeroing);
  488. if (err)
  489. return err;
  490. nbytes -= count;
  491. count = min(nbytes, (unsigned) PAGE_SIZE);
  492. offset = 0;
  493. }
  494. return 0;
  495. }
  496. /* Copy a single argument in the request to/from userspace buffer */
  497. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  498. {
  499. while (size) {
  500. int err;
  501. if (!cs->len && (err = fuse_copy_fill(cs)))
  502. return err;
  503. fuse_copy_do(cs, &val, &size);
  504. }
  505. return 0;
  506. }
  507. /* Copy request arguments to/from userspace buffer */
  508. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  509. unsigned argpages, struct fuse_arg *args,
  510. int zeroing)
  511. {
  512. int err = 0;
  513. unsigned i;
  514. for (i = 0; !err && i < numargs; i++) {
  515. struct fuse_arg *arg = &args[i];
  516. if (i == numargs - 1 && argpages)
  517. err = fuse_copy_pages(cs, arg->size, zeroing);
  518. else
  519. err = fuse_copy_one(cs, arg->value, arg->size);
  520. }
  521. return err;
  522. }
  523. /* Wait until a request is available on the pending list */
  524. static void request_wait(struct fuse_conn *fc)
  525. {
  526. DECLARE_WAITQUEUE(wait, current);
  527. add_wait_queue_exclusive(&fc->waitq, &wait);
  528. while (fc->mounted && list_empty(&fc->pending)) {
  529. set_current_state(TASK_INTERRUPTIBLE);
  530. if (signal_pending(current))
  531. break;
  532. spin_unlock(&fuse_lock);
  533. schedule();
  534. spin_lock(&fuse_lock);
  535. }
  536. set_current_state(TASK_RUNNING);
  537. remove_wait_queue(&fc->waitq, &wait);
  538. }
  539. /*
  540. * Read a single request into the userspace filesystem's buffer. This
  541. * function waits until a request is available, then removes it from
  542. * the pending list and copies request data to userspace buffer. If
  543. * no reply is needed (FORGET) or request has been interrupted or
  544. * there was an error during the copying then it's finished by calling
  545. * request_end(). Otherwise add it to the processing list, and set
  546. * the 'sent' flag.
  547. */
  548. static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
  549. unsigned long nr_segs, loff_t *off)
  550. {
  551. int err;
  552. struct fuse_conn *fc;
  553. struct fuse_req *req;
  554. struct fuse_in *in;
  555. struct fuse_copy_state cs;
  556. unsigned reqsize;
  557. spin_lock(&fuse_lock);
  558. fc = file->private_data;
  559. err = -EPERM;
  560. if (!fc)
  561. goto err_unlock;
  562. request_wait(fc);
  563. err = -ENODEV;
  564. if (!fc->mounted)
  565. goto err_unlock;
  566. err = -ERESTARTSYS;
  567. if (list_empty(&fc->pending))
  568. goto err_unlock;
  569. req = list_entry(fc->pending.next, struct fuse_req, list);
  570. list_del_init(&req->list);
  571. spin_unlock(&fuse_lock);
  572. in = &req->in;
  573. reqsize = req->in.h.len;
  574. fuse_copy_init(&cs, 1, req, iov, nr_segs);
  575. err = -EINVAL;
  576. if (iov_length(iov, nr_segs) >= reqsize) {
  577. err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
  578. if (!err)
  579. err = fuse_copy_args(&cs, in->numargs, in->argpages,
  580. (struct fuse_arg *) in->args, 0);
  581. }
  582. fuse_copy_finish(&cs);
  583. spin_lock(&fuse_lock);
  584. req->locked = 0;
  585. if (!err && req->interrupted)
  586. err = -ENOENT;
  587. if (err) {
  588. if (!req->interrupted)
  589. req->out.h.error = -EIO;
  590. request_end(fc, req);
  591. return err;
  592. }
  593. if (!req->isreply)
  594. request_end(fc, req);
  595. else {
  596. req->sent = 1;
  597. list_add_tail(&req->list, &fc->processing);
  598. spin_unlock(&fuse_lock);
  599. }
  600. return reqsize;
  601. err_unlock:
  602. spin_unlock(&fuse_lock);
  603. return err;
  604. }
  605. static ssize_t fuse_dev_read(struct file *file, char __user *buf,
  606. size_t nbytes, loff_t *off)
  607. {
  608. struct iovec iov;
  609. iov.iov_len = nbytes;
  610. iov.iov_base = buf;
  611. return fuse_dev_readv(file, &iov, 1, off);
  612. }
  613. /* Look up request on processing list by unique ID */
  614. static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
  615. {
  616. struct list_head *entry;
  617. list_for_each(entry, &fc->processing) {
  618. struct fuse_req *req;
  619. req = list_entry(entry, struct fuse_req, list);
  620. if (req->in.h.unique == unique)
  621. return req;
  622. }
  623. return NULL;
  624. }
  625. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  626. unsigned nbytes)
  627. {
  628. unsigned reqsize = sizeof(struct fuse_out_header);
  629. if (out->h.error)
  630. return nbytes != reqsize ? -EINVAL : 0;
  631. reqsize += len_args(out->numargs, out->args);
  632. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  633. return -EINVAL;
  634. else if (reqsize > nbytes) {
  635. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  636. unsigned diffsize = reqsize - nbytes;
  637. if (diffsize > lastarg->size)
  638. return -EINVAL;
  639. lastarg->size -= diffsize;
  640. }
  641. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  642. out->page_zeroing);
  643. }
  644. /*
  645. * Write a single reply to a request. First the header is copied from
  646. * the write buffer. The request is then searched on the processing
  647. * list by the unique ID found in the header. If found, then remove
  648. * it from the list and copy the rest of the buffer to the request.
  649. * The request is finished by calling request_end()
  650. */
  651. static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
  652. unsigned long nr_segs, loff_t *off)
  653. {
  654. int err;
  655. unsigned nbytes = iov_length(iov, nr_segs);
  656. struct fuse_req *req;
  657. struct fuse_out_header oh;
  658. struct fuse_copy_state cs;
  659. struct fuse_conn *fc = fuse_get_conn(file);
  660. if (!fc)
  661. return -ENODEV;
  662. fuse_copy_init(&cs, 0, NULL, iov, nr_segs);
  663. if (nbytes < sizeof(struct fuse_out_header))
  664. return -EINVAL;
  665. err = fuse_copy_one(&cs, &oh, sizeof(oh));
  666. if (err)
  667. goto err_finish;
  668. err = -EINVAL;
  669. if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
  670. oh.len != nbytes)
  671. goto err_finish;
  672. spin_lock(&fuse_lock);
  673. req = request_find(fc, oh.unique);
  674. err = -EINVAL;
  675. if (!req)
  676. goto err_unlock;
  677. list_del_init(&req->list);
  678. if (req->interrupted) {
  679. request_end(fc, req);
  680. fuse_copy_finish(&cs);
  681. return -ENOENT;
  682. }
  683. req->out.h = oh;
  684. req->locked = 1;
  685. cs.req = req;
  686. spin_unlock(&fuse_lock);
  687. err = copy_out_args(&cs, &req->out, nbytes);
  688. fuse_copy_finish(&cs);
  689. spin_lock(&fuse_lock);
  690. req->locked = 0;
  691. if (!err) {
  692. if (req->interrupted)
  693. err = -ENOENT;
  694. } else if (!req->interrupted)
  695. req->out.h.error = -EIO;
  696. request_end(fc, req);
  697. return err ? err : nbytes;
  698. err_unlock:
  699. spin_unlock(&fuse_lock);
  700. err_finish:
  701. fuse_copy_finish(&cs);
  702. return err;
  703. }
  704. static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
  705. size_t nbytes, loff_t *off)
  706. {
  707. struct iovec iov;
  708. iov.iov_len = nbytes;
  709. iov.iov_base = (char __user *) buf;
  710. return fuse_dev_writev(file, &iov, 1, off);
  711. }
  712. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  713. {
  714. struct fuse_conn *fc = fuse_get_conn(file);
  715. unsigned mask = POLLOUT | POLLWRNORM;
  716. if (!fc)
  717. return -ENODEV;
  718. poll_wait(file, &fc->waitq, wait);
  719. spin_lock(&fuse_lock);
  720. if (!list_empty(&fc->pending))
  721. mask |= POLLIN | POLLRDNORM;
  722. spin_unlock(&fuse_lock);
  723. return mask;
  724. }
  725. /* Abort all requests on the given list (pending or processing) */
  726. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  727. {
  728. while (!list_empty(head)) {
  729. struct fuse_req *req;
  730. req = list_entry(head->next, struct fuse_req, list);
  731. list_del_init(&req->list);
  732. req->out.h.error = -ECONNABORTED;
  733. request_end(fc, req);
  734. spin_lock(&fuse_lock);
  735. }
  736. }
  737. static int fuse_dev_release(struct inode *inode, struct file *file)
  738. {
  739. struct fuse_conn *fc;
  740. spin_lock(&fuse_lock);
  741. fc = file->private_data;
  742. if (fc) {
  743. fc->connected = 0;
  744. end_requests(fc, &fc->pending);
  745. end_requests(fc, &fc->processing);
  746. fuse_release_conn(fc);
  747. }
  748. spin_unlock(&fuse_lock);
  749. return 0;
  750. }
  751. struct file_operations fuse_dev_operations = {
  752. .owner = THIS_MODULE,
  753. .llseek = no_llseek,
  754. .read = fuse_dev_read,
  755. .readv = fuse_dev_readv,
  756. .write = fuse_dev_write,
  757. .writev = fuse_dev_writev,
  758. .poll = fuse_dev_poll,
  759. .release = fuse_dev_release,
  760. };
  761. static struct miscdevice fuse_miscdevice = {
  762. .minor = FUSE_MINOR,
  763. .name = "fuse",
  764. .fops = &fuse_dev_operations,
  765. };
  766. int __init fuse_dev_init(void)
  767. {
  768. int err = -ENOMEM;
  769. fuse_req_cachep = kmem_cache_create("fuse_request",
  770. sizeof(struct fuse_req),
  771. 0, 0, NULL, NULL);
  772. if (!fuse_req_cachep)
  773. goto out;
  774. err = misc_register(&fuse_miscdevice);
  775. if (err)
  776. goto out_cache_clean;
  777. return 0;
  778. out_cache_clean:
  779. kmem_cache_destroy(fuse_req_cachep);
  780. out:
  781. return err;
  782. }
  783. void fuse_dev_cleanup(void)
  784. {
  785. misc_deregister(&fuse_miscdevice);
  786. kmem_cache_destroy(fuse_req_cachep);
  787. }