dev.c 26 KB

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