dev.c 24 KB

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