dev.c 21 KB

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