dev.c 23 KB

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