dev.c 24 KB

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