dev.c 29 KB

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