dev.c 27 KB

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