dev.c 26 KB

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