dev.c 25 KB

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