dev.c 24 KB

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