dev.c 23 KB

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