dev.c 23 KB

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