dev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2006 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 struct fuse_conn *fuse_get_conn(struct file *file)
  19. {
  20. /*
  21. * Lockless access is OK, because file->private data is set
  22. * once during mount and is valid until the file is released.
  23. */
  24. return file->private_data;
  25. }
  26. static void fuse_request_init(struct fuse_req *req)
  27. {
  28. memset(req, 0, sizeof(*req));
  29. INIT_LIST_HEAD(&req->list);
  30. init_waitqueue_head(&req->waitq);
  31. atomic_set(&req->count, 1);
  32. }
  33. struct fuse_req *fuse_request_alloc(void)
  34. {
  35. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL);
  36. if (req)
  37. fuse_request_init(req);
  38. return req;
  39. }
  40. void fuse_request_free(struct fuse_req *req)
  41. {
  42. kmem_cache_free(fuse_req_cachep, req);
  43. }
  44. static void block_sigs(sigset_t *oldset)
  45. {
  46. sigset_t mask;
  47. siginitsetinv(&mask, sigmask(SIGKILL));
  48. sigprocmask(SIG_BLOCK, &mask, oldset);
  49. }
  50. static void restore_sigs(sigset_t *oldset)
  51. {
  52. sigprocmask(SIG_SETMASK, oldset, NULL);
  53. }
  54. /*
  55. * Reset request, so that it can be reused
  56. *
  57. * The caller must be _very_ careful to make sure, that it is holding
  58. * the only reference to req
  59. */
  60. void fuse_reset_request(struct fuse_req *req)
  61. {
  62. BUG_ON(atomic_read(&req->count) != 1);
  63. fuse_request_init(req);
  64. }
  65. static void __fuse_get_request(struct fuse_req *req)
  66. {
  67. atomic_inc(&req->count);
  68. }
  69. /* Must be called with > 1 refcount */
  70. static void __fuse_put_request(struct fuse_req *req)
  71. {
  72. BUG_ON(atomic_read(&req->count) < 2);
  73. atomic_dec(&req->count);
  74. }
  75. struct fuse_req *fuse_get_req(struct fuse_conn *fc)
  76. {
  77. struct fuse_req *req;
  78. sigset_t oldset;
  79. int intr;
  80. int err;
  81. atomic_inc(&fc->num_waiting);
  82. block_sigs(&oldset);
  83. intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
  84. restore_sigs(&oldset);
  85. err = -EINTR;
  86. if (intr)
  87. goto out;
  88. req = fuse_request_alloc();
  89. err = -ENOMEM;
  90. if (!req)
  91. goto out;
  92. req->in.h.uid = current->fsuid;
  93. req->in.h.gid = current->fsgid;
  94. req->in.h.pid = current->pid;
  95. req->waiting = 1;
  96. return req;
  97. out:
  98. atomic_dec(&fc->num_waiting);
  99. return ERR_PTR(err);
  100. }
  101. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
  102. {
  103. if (atomic_dec_and_test(&req->count)) {
  104. if (req->waiting)
  105. atomic_dec(&fc->num_waiting);
  106. fuse_request_free(req);
  107. }
  108. }
  109. /*
  110. * Called with sbput_sem held for read (request_end) or write
  111. * (fuse_put_super). By the time fuse_put_super() is finished, all
  112. * inodes belonging to background requests must be released, so the
  113. * iputs have to be done within the locked region.
  114. */
  115. void fuse_release_background(struct fuse_conn *fc, struct fuse_req *req)
  116. {
  117. iput(req->inode);
  118. iput(req->inode2);
  119. spin_lock(&fc->lock);
  120. list_del(&req->bg_entry);
  121. if (fc->num_background == FUSE_MAX_BACKGROUND) {
  122. fc->blocked = 0;
  123. wake_up_all(&fc->blocked_waitq);
  124. }
  125. fc->num_background--;
  126. spin_unlock(&fc->lock);
  127. }
  128. /*
  129. * This function is called when a request is finished. Either a reply
  130. * has arrived or it was interrupted (and not yet sent) or some error
  131. * occurred during communication with userspace, or the device file
  132. * was closed. In case of a background request the reference to the
  133. * stored objects are released. The requester thread is woken up (if
  134. * still waiting), the 'end' callback is called if given, else the
  135. * reference to the request is released
  136. *
  137. * Releasing extra reference for foreground requests must be done
  138. * within the same locked region as setting state to finished. This
  139. * is because fuse_reset_request() may be called after request is
  140. * finished and it must be the sole possessor. If request is
  141. * interrupted and put in the background, it will return with an error
  142. * and hence never be reset and reused.
  143. *
  144. * Called with fc->lock, unlocks it
  145. */
  146. static void request_end(struct fuse_conn *fc, struct fuse_req *req)
  147. {
  148. list_del(&req->list);
  149. req->state = FUSE_REQ_FINISHED;
  150. if (!req->background) {
  151. spin_unlock(&fc->lock);
  152. wake_up(&req->waitq);
  153. fuse_put_request(fc, req);
  154. } else {
  155. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  156. req->end = NULL;
  157. spin_unlock(&fc->lock);
  158. down_read(&fc->sbput_sem);
  159. if (fc->mounted)
  160. fuse_release_background(fc, req);
  161. up_read(&fc->sbput_sem);
  162. /* fput must go outside sbput_sem, otherwise it can deadlock */
  163. if (req->file)
  164. fput(req->file);
  165. if (end)
  166. end(fc, req);
  167. else
  168. fuse_put_request(fc, req);
  169. }
  170. }
  171. /*
  172. * Unfortunately request interruption not just solves the deadlock
  173. * problem, it causes problems too. These stem from the fact, that an
  174. * interrupted request is continued to be processed in userspace,
  175. * while all the locks and object references (inode and file) held
  176. * during the operation are released.
  177. *
  178. * To release the locks is exactly why there's a need to interrupt the
  179. * request, so there's not a lot that can be done about this, except
  180. * introduce additional locking in userspace.
  181. *
  182. * More important is to keep inode and file references until userspace
  183. * has replied, otherwise FORGET and RELEASE could be sent while the
  184. * inode/file is still used by the filesystem.
  185. *
  186. * For this reason the concept of "background" request is introduced.
  187. * An interrupted request is backgrounded if it has been already sent
  188. * to userspace. Backgrounding involves getting an extra reference to
  189. * inode(s) or file used in the request, and adding the request to
  190. * fc->background list. When a reply is received for a background
  191. * request, the object references are released, and the request is
  192. * removed from the list. If the filesystem is unmounted while there
  193. * are still background requests, the list is walked and references
  194. * are released as if a reply was received.
  195. *
  196. * There's one more use for a background request. The RELEASE message is
  197. * always sent as background, since it doesn't return an error or
  198. * data.
  199. */
  200. static void background_request(struct fuse_conn *fc, struct fuse_req *req)
  201. {
  202. req->background = 1;
  203. list_add(&req->bg_entry, &fc->background);
  204. fc->num_background++;
  205. if (fc->num_background == FUSE_MAX_BACKGROUND)
  206. fc->blocked = 1;
  207. if (req->inode)
  208. req->inode = igrab(req->inode);
  209. if (req->inode2)
  210. req->inode2 = igrab(req->inode2);
  211. if (req->file)
  212. get_file(req->file);
  213. }
  214. /* Called with fc->lock held. Releases, and then reacquires it. */
  215. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  216. {
  217. sigset_t oldset;
  218. spin_unlock(&fc->lock);
  219. block_sigs(&oldset);
  220. wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
  221. restore_sigs(&oldset);
  222. spin_lock(&fc->lock);
  223. if (req->state == FUSE_REQ_FINISHED && !req->interrupted)
  224. return;
  225. if (!req->interrupted) {
  226. req->out.h.error = -EINTR;
  227. req->interrupted = 1;
  228. }
  229. if (req->locked) {
  230. /* This is uninterruptible sleep, because data is
  231. being copied to/from the buffers of req. During
  232. locked state, there mustn't be any filesystem
  233. operation (e.g. page fault), since that could lead
  234. to deadlock */
  235. spin_unlock(&fc->lock);
  236. wait_event(req->waitq, !req->locked);
  237. spin_lock(&fc->lock);
  238. }
  239. if (req->state == FUSE_REQ_PENDING) {
  240. list_del(&req->list);
  241. __fuse_put_request(req);
  242. } else if (req->state == FUSE_REQ_SENT)
  243. background_request(fc, req);
  244. }
  245. static unsigned len_args(unsigned numargs, struct fuse_arg *args)
  246. {
  247. unsigned nbytes = 0;
  248. unsigned i;
  249. for (i = 0; i < numargs; i++)
  250. nbytes += args[i].size;
  251. return nbytes;
  252. }
  253. static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
  254. {
  255. fc->reqctr++;
  256. /* zero is special */
  257. if (fc->reqctr == 0)
  258. fc->reqctr = 1;
  259. req->in.h.unique = fc->reqctr;
  260. req->in.h.len = sizeof(struct fuse_in_header) +
  261. len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
  262. list_add_tail(&req->list, &fc->pending);
  263. req->state = FUSE_REQ_PENDING;
  264. if (!req->waiting) {
  265. req->waiting = 1;
  266. atomic_inc(&fc->num_waiting);
  267. }
  268. wake_up(&fc->waitq);
  269. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  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(&fc->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(&fc->lock);
  290. }
  291. static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
  292. {
  293. spin_lock(&fc->lock);
  294. background_request(fc, req);
  295. if (fc->connected) {
  296. queue_request(fc, req);
  297. spin_unlock(&fc->lock);
  298. } else {
  299. req->out.h.error = -ENOTCONN;
  300. request_end(fc, req);
  301. }
  302. }
  303. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
  304. {
  305. req->isreply = 0;
  306. request_send_nowait(fc, req);
  307. }
  308. void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  309. {
  310. req->isreply = 1;
  311. request_send_nowait(fc, req);
  312. }
  313. /*
  314. * Lock the request. Up to the next unlock_request() there mustn't be
  315. * anything that could cause a page-fault. If the request was already
  316. * interrupted bail out.
  317. */
  318. static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
  319. {
  320. int err = 0;
  321. if (req) {
  322. spin_lock(&fc->lock);
  323. if (req->interrupted)
  324. err = -ENOENT;
  325. else
  326. req->locked = 1;
  327. spin_unlock(&fc->lock);
  328. }
  329. return err;
  330. }
  331. /*
  332. * Unlock request. If it was interrupted during being locked, the
  333. * requester thread is currently waiting for it to be unlocked, so
  334. * wake it up.
  335. */
  336. static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
  337. {
  338. if (req) {
  339. spin_lock(&fc->lock);
  340. req->locked = 0;
  341. if (req->interrupted)
  342. wake_up(&req->waitq);
  343. spin_unlock(&fc->lock);
  344. }
  345. }
  346. struct fuse_copy_state {
  347. struct fuse_conn *fc;
  348. int write;
  349. struct fuse_req *req;
  350. const struct iovec *iov;
  351. unsigned long nr_segs;
  352. unsigned long seglen;
  353. unsigned long addr;
  354. struct page *pg;
  355. void *mapaddr;
  356. void *buf;
  357. unsigned len;
  358. };
  359. static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
  360. int write, struct fuse_req *req,
  361. const struct iovec *iov, unsigned long nr_segs)
  362. {
  363. memset(cs, 0, sizeof(*cs));
  364. cs->fc = fc;
  365. cs->write = write;
  366. cs->req = req;
  367. cs->iov = iov;
  368. cs->nr_segs = nr_segs;
  369. }
  370. /* Unmap and put previous page of userspace buffer */
  371. static void fuse_copy_finish(struct fuse_copy_state *cs)
  372. {
  373. if (cs->mapaddr) {
  374. kunmap_atomic(cs->mapaddr, KM_USER0);
  375. if (cs->write) {
  376. flush_dcache_page(cs->pg);
  377. set_page_dirty_lock(cs->pg);
  378. }
  379. put_page(cs->pg);
  380. cs->mapaddr = NULL;
  381. }
  382. }
  383. /*
  384. * Get another pagefull of userspace buffer, and map it to kernel
  385. * address space, and lock request
  386. */
  387. static int fuse_copy_fill(struct fuse_copy_state *cs)
  388. {
  389. unsigned long offset;
  390. int err;
  391. unlock_request(cs->fc, cs->req);
  392. fuse_copy_finish(cs);
  393. if (!cs->seglen) {
  394. BUG_ON(!cs->nr_segs);
  395. cs->seglen = cs->iov[0].iov_len;
  396. cs->addr = (unsigned long) cs->iov[0].iov_base;
  397. cs->iov ++;
  398. cs->nr_segs --;
  399. }
  400. down_read(&current->mm->mmap_sem);
  401. err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
  402. &cs->pg, NULL);
  403. up_read(&current->mm->mmap_sem);
  404. if (err < 0)
  405. return err;
  406. BUG_ON(err != 1);
  407. offset = cs->addr % PAGE_SIZE;
  408. cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
  409. cs->buf = cs->mapaddr + offset;
  410. cs->len = min(PAGE_SIZE - offset, cs->seglen);
  411. cs->seglen -= cs->len;
  412. cs->addr += cs->len;
  413. return lock_request(cs->fc, cs->req);
  414. }
  415. /* Do as much copy to/from userspace buffer as we can */
  416. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  417. {
  418. unsigned ncpy = min(*size, cs->len);
  419. if (val) {
  420. if (cs->write)
  421. memcpy(cs->buf, *val, ncpy);
  422. else
  423. memcpy(*val, cs->buf, ncpy);
  424. *val += ncpy;
  425. }
  426. *size -= ncpy;
  427. cs->len -= ncpy;
  428. cs->buf += ncpy;
  429. return ncpy;
  430. }
  431. /*
  432. * Copy a page in the request to/from the userspace buffer. Must be
  433. * done atomically
  434. */
  435. static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
  436. unsigned offset, unsigned count, int zeroing)
  437. {
  438. if (page && zeroing && count < PAGE_SIZE) {
  439. void *mapaddr = kmap_atomic(page, KM_USER1);
  440. memset(mapaddr, 0, PAGE_SIZE);
  441. kunmap_atomic(mapaddr, KM_USER1);
  442. }
  443. while (count) {
  444. int err;
  445. if (!cs->len && (err = fuse_copy_fill(cs)))
  446. return err;
  447. if (page) {
  448. void *mapaddr = kmap_atomic(page, KM_USER1);
  449. void *buf = mapaddr + offset;
  450. offset += fuse_copy_do(cs, &buf, &count);
  451. kunmap_atomic(mapaddr, KM_USER1);
  452. } else
  453. offset += fuse_copy_do(cs, NULL, &count);
  454. }
  455. if (page && !cs->write)
  456. flush_dcache_page(page);
  457. return 0;
  458. }
  459. /* Copy pages in the request to/from userspace buffer */
  460. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  461. int zeroing)
  462. {
  463. unsigned i;
  464. struct fuse_req *req = cs->req;
  465. unsigned offset = req->page_offset;
  466. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  467. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  468. struct page *page = req->pages[i];
  469. int err = fuse_copy_page(cs, page, offset, count, zeroing);
  470. if (err)
  471. return err;
  472. nbytes -= count;
  473. count = min(nbytes, (unsigned) PAGE_SIZE);
  474. offset = 0;
  475. }
  476. return 0;
  477. }
  478. /* Copy a single argument in the request to/from userspace buffer */
  479. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  480. {
  481. while (size) {
  482. int err;
  483. if (!cs->len && (err = fuse_copy_fill(cs)))
  484. return err;
  485. fuse_copy_do(cs, &val, &size);
  486. }
  487. return 0;
  488. }
  489. /* Copy request arguments to/from userspace buffer */
  490. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  491. unsigned argpages, struct fuse_arg *args,
  492. int zeroing)
  493. {
  494. int err = 0;
  495. unsigned i;
  496. for (i = 0; !err && i < numargs; i++) {
  497. struct fuse_arg *arg = &args[i];
  498. if (i == numargs - 1 && argpages)
  499. err = fuse_copy_pages(cs, arg->size, zeroing);
  500. else
  501. err = fuse_copy_one(cs, arg->value, arg->size);
  502. }
  503. return err;
  504. }
  505. /* Wait until a request is available on the pending list */
  506. static void request_wait(struct fuse_conn *fc)
  507. {
  508. DECLARE_WAITQUEUE(wait, current);
  509. add_wait_queue_exclusive(&fc->waitq, &wait);
  510. while (fc->connected && list_empty(&fc->pending)) {
  511. set_current_state(TASK_INTERRUPTIBLE);
  512. if (signal_pending(current))
  513. break;
  514. spin_unlock(&fc->lock);
  515. schedule();
  516. spin_lock(&fc->lock);
  517. }
  518. set_current_state(TASK_RUNNING);
  519. remove_wait_queue(&fc->waitq, &wait);
  520. }
  521. /*
  522. * Read a single request into the userspace filesystem's buffer. This
  523. * function waits until a request is available, then removes it from
  524. * the pending list and copies request data to userspace buffer. If
  525. * no reply is needed (FORGET) or request has been interrupted or
  526. * there was an error during the copying then it's finished by calling
  527. * request_end(). Otherwise add it to the processing list, and set
  528. * the 'sent' flag.
  529. */
  530. static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
  531. unsigned long nr_segs, loff_t *off)
  532. {
  533. int err;
  534. struct fuse_req *req;
  535. struct fuse_in *in;
  536. struct fuse_copy_state cs;
  537. unsigned reqsize;
  538. struct fuse_conn *fc = fuse_get_conn(file);
  539. if (!fc)
  540. return -EPERM;
  541. restart:
  542. spin_lock(&fc->lock);
  543. err = -EAGAIN;
  544. if ((file->f_flags & O_NONBLOCK) && fc->connected &&
  545. list_empty(&fc->pending))
  546. goto err_unlock;
  547. request_wait(fc);
  548. err = -ENODEV;
  549. if (!fc->connected)
  550. goto err_unlock;
  551. err = -ERESTARTSYS;
  552. if (list_empty(&fc->pending))
  553. goto err_unlock;
  554. req = list_entry(fc->pending.next, struct fuse_req, list);
  555. req->state = FUSE_REQ_READING;
  556. list_move(&req->list, &fc->io);
  557. in = &req->in;
  558. reqsize = in->h.len;
  559. /* If request is too large, reply with an error and restart the read */
  560. if (iov_length(iov, nr_segs) < reqsize) {
  561. req->out.h.error = -EIO;
  562. /* SETXATTR is special, since it may contain too large data */
  563. if (in->h.opcode == FUSE_SETXATTR)
  564. req->out.h.error = -E2BIG;
  565. request_end(fc, req);
  566. goto restart;
  567. }
  568. spin_unlock(&fc->lock);
  569. fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
  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. fuse_copy_finish(&cs);
  575. spin_lock(&fc->lock);
  576. req->locked = 0;
  577. if (!err && req->interrupted)
  578. err = -ENOENT;
  579. if (err) {
  580. if (!req->interrupted)
  581. req->out.h.error = -EIO;
  582. request_end(fc, req);
  583. return err;
  584. }
  585. if (!req->isreply)
  586. request_end(fc, req);
  587. else {
  588. req->state = FUSE_REQ_SENT;
  589. list_move_tail(&req->list, &fc->processing);
  590. spin_unlock(&fc->lock);
  591. }
  592. return reqsize;
  593. err_unlock:
  594. spin_unlock(&fc->lock);
  595. return err;
  596. }
  597. static ssize_t fuse_dev_read(struct file *file, char __user *buf,
  598. size_t nbytes, loff_t *off)
  599. {
  600. struct iovec iov;
  601. iov.iov_len = nbytes;
  602. iov.iov_base = buf;
  603. return fuse_dev_readv(file, &iov, 1, off);
  604. }
  605. /* Look up request on processing list by unique ID */
  606. static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
  607. {
  608. struct list_head *entry;
  609. list_for_each(entry, &fc->processing) {
  610. struct fuse_req *req;
  611. req = list_entry(entry, struct fuse_req, list);
  612. if (req->in.h.unique == unique)
  613. return req;
  614. }
  615. return NULL;
  616. }
  617. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  618. unsigned nbytes)
  619. {
  620. unsigned reqsize = sizeof(struct fuse_out_header);
  621. if (out->h.error)
  622. return nbytes != reqsize ? -EINVAL : 0;
  623. reqsize += len_args(out->numargs, out->args);
  624. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  625. return -EINVAL;
  626. else if (reqsize > nbytes) {
  627. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  628. unsigned diffsize = reqsize - nbytes;
  629. if (diffsize > lastarg->size)
  630. return -EINVAL;
  631. lastarg->size -= diffsize;
  632. }
  633. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  634. out->page_zeroing);
  635. }
  636. /*
  637. * Write a single reply to a request. First the header is copied from
  638. * the write buffer. The request is then searched on the processing
  639. * list by the unique ID found in the header. If found, then remove
  640. * it from the list and copy the rest of the buffer to the request.
  641. * The request is finished by calling request_end()
  642. */
  643. static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
  644. unsigned long nr_segs, loff_t *off)
  645. {
  646. int err;
  647. unsigned nbytes = iov_length(iov, nr_segs);
  648. struct fuse_req *req;
  649. struct fuse_out_header oh;
  650. struct fuse_copy_state cs;
  651. struct fuse_conn *fc = fuse_get_conn(file);
  652. if (!fc)
  653. return -EPERM;
  654. fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
  655. if (nbytes < sizeof(struct fuse_out_header))
  656. return -EINVAL;
  657. err = fuse_copy_one(&cs, &oh, sizeof(oh));
  658. if (err)
  659. goto err_finish;
  660. err = -EINVAL;
  661. if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
  662. oh.len != nbytes)
  663. goto err_finish;
  664. spin_lock(&fc->lock);
  665. err = -ENOENT;
  666. if (!fc->connected)
  667. goto err_unlock;
  668. req = request_find(fc, oh.unique);
  669. err = -EINVAL;
  670. if (!req)
  671. goto err_unlock;
  672. if (req->interrupted) {
  673. spin_unlock(&fc->lock);
  674. fuse_copy_finish(&cs);
  675. spin_lock(&fc->lock);
  676. request_end(fc, req);
  677. return -ENOENT;
  678. }
  679. list_move(&req->list, &fc->io);
  680. req->out.h = oh;
  681. req->locked = 1;
  682. cs.req = req;
  683. spin_unlock(&fc->lock);
  684. err = copy_out_args(&cs, &req->out, nbytes);
  685. fuse_copy_finish(&cs);
  686. spin_lock(&fc->lock);
  687. req->locked = 0;
  688. if (!err) {
  689. if (req->interrupted)
  690. err = -ENOENT;
  691. } else if (!req->interrupted)
  692. req->out.h.error = -EIO;
  693. request_end(fc, req);
  694. return err ? err : nbytes;
  695. err_unlock:
  696. spin_unlock(&fc->lock);
  697. err_finish:
  698. fuse_copy_finish(&cs);
  699. return err;
  700. }
  701. static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
  702. size_t nbytes, loff_t *off)
  703. {
  704. struct iovec iov;
  705. iov.iov_len = nbytes;
  706. iov.iov_base = (char __user *) buf;
  707. return fuse_dev_writev(file, &iov, 1, off);
  708. }
  709. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  710. {
  711. unsigned mask = POLLOUT | POLLWRNORM;
  712. struct fuse_conn *fc = fuse_get_conn(file);
  713. if (!fc)
  714. return POLLERR;
  715. poll_wait(file, &fc->waitq, wait);
  716. spin_lock(&fc->lock);
  717. if (!fc->connected)
  718. mask = POLLERR;
  719. else if (!list_empty(&fc->pending))
  720. mask |= POLLIN | POLLRDNORM;
  721. spin_unlock(&fc->lock);
  722. return mask;
  723. }
  724. /*
  725. * Abort all requests on the given list (pending or processing)
  726. *
  727. * This function releases and reacquires fc->lock
  728. */
  729. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  730. {
  731. while (!list_empty(head)) {
  732. struct fuse_req *req;
  733. req = list_entry(head->next, struct fuse_req, list);
  734. req->out.h.error = -ECONNABORTED;
  735. request_end(fc, req);
  736. spin_lock(&fc->lock);
  737. }
  738. }
  739. /*
  740. * Abort requests under I/O
  741. *
  742. * The requests are set to interrupted and finished, and the request
  743. * waiter is woken up. This will make request_wait_answer() wait
  744. * until the request is unlocked and then return.
  745. *
  746. * If the request is asynchronous, then the end function needs to be
  747. * called after waiting for the request to be unlocked (if it was
  748. * locked).
  749. */
  750. static void end_io_requests(struct fuse_conn *fc)
  751. {
  752. while (!list_empty(&fc->io)) {
  753. struct fuse_req *req =
  754. list_entry(fc->io.next, struct fuse_req, list);
  755. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  756. req->interrupted = 1;
  757. req->out.h.error = -ECONNABORTED;
  758. req->state = FUSE_REQ_FINISHED;
  759. list_del_init(&req->list);
  760. wake_up(&req->waitq);
  761. if (end) {
  762. req->end = NULL;
  763. /* The end function will consume this reference */
  764. __fuse_get_request(req);
  765. spin_unlock(&fc->lock);
  766. wait_event(req->waitq, !req->locked);
  767. end(fc, req);
  768. spin_lock(&fc->lock);
  769. }
  770. }
  771. }
  772. /*
  773. * Abort all requests.
  774. *
  775. * Emergency exit in case of a malicious or accidental deadlock, or
  776. * just a hung filesystem.
  777. *
  778. * The same effect is usually achievable through killing the
  779. * filesystem daemon and all users of the filesystem. The exception
  780. * is the combination of an asynchronous request and the tricky
  781. * deadlock (see Documentation/filesystems/fuse.txt).
  782. *
  783. * During the aborting, progression of requests from the pending and
  784. * processing lists onto the io list, and progression of new requests
  785. * onto the pending list is prevented by req->connected being false.
  786. *
  787. * Progression of requests under I/O to the processing list is
  788. * prevented by the req->interrupted flag being true for these
  789. * requests. For this reason requests on the io list must be aborted
  790. * first.
  791. */
  792. void fuse_abort_conn(struct fuse_conn *fc)
  793. {
  794. spin_lock(&fc->lock);
  795. if (fc->connected) {
  796. fc->connected = 0;
  797. end_io_requests(fc);
  798. end_requests(fc, &fc->pending);
  799. end_requests(fc, &fc->processing);
  800. wake_up_all(&fc->waitq);
  801. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  802. }
  803. spin_unlock(&fc->lock);
  804. }
  805. static int fuse_dev_release(struct inode *inode, struct file *file)
  806. {
  807. struct fuse_conn *fc = fuse_get_conn(file);
  808. if (fc) {
  809. spin_lock(&fc->lock);
  810. fc->connected = 0;
  811. end_requests(fc, &fc->pending);
  812. end_requests(fc, &fc->processing);
  813. spin_unlock(&fc->lock);
  814. fasync_helper(-1, file, 0, &fc->fasync);
  815. kobject_put(&fc->kobj);
  816. }
  817. return 0;
  818. }
  819. static int fuse_dev_fasync(int fd, struct file *file, int on)
  820. {
  821. struct fuse_conn *fc = fuse_get_conn(file);
  822. if (!fc)
  823. return -EPERM;
  824. /* No locking - fasync_helper does its own locking */
  825. return fasync_helper(fd, file, on, &fc->fasync);
  826. }
  827. const struct file_operations fuse_dev_operations = {
  828. .owner = THIS_MODULE,
  829. .llseek = no_llseek,
  830. .read = fuse_dev_read,
  831. .readv = fuse_dev_readv,
  832. .write = fuse_dev_write,
  833. .writev = fuse_dev_writev,
  834. .poll = fuse_dev_poll,
  835. .release = fuse_dev_release,
  836. .fasync = fuse_dev_fasync,
  837. };
  838. static struct miscdevice fuse_miscdevice = {
  839. .minor = FUSE_MINOR,
  840. .name = "fuse",
  841. .fops = &fuse_dev_operations,
  842. };
  843. int __init fuse_dev_init(void)
  844. {
  845. int err = -ENOMEM;
  846. fuse_req_cachep = kmem_cache_create("fuse_request",
  847. sizeof(struct fuse_req),
  848. 0, 0, NULL, NULL);
  849. if (!fuse_req_cachep)
  850. goto out;
  851. err = misc_register(&fuse_miscdevice);
  852. if (err)
  853. goto out_cache_clean;
  854. return 0;
  855. out_cache_clean:
  856. kmem_cache_destroy(fuse_req_cachep);
  857. out:
  858. return err;
  859. }
  860. void fuse_dev_cleanup(void)
  861. {
  862. misc_deregister(&fuse_miscdevice);
  863. kmem_cache_destroy(fuse_req_cachep);
  864. }