dev.c 27 KB

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