dev.c 26 KB

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