dev.c 25 KB

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