dev.c 24 KB

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