dev.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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. #include <linux/pipe_fs_i.h>
  17. #include <linux/swap.h>
  18. #include <linux/splice.h>
  19. MODULE_ALIAS_MISCDEV(FUSE_MINOR);
  20. MODULE_ALIAS("devname:fuse");
  21. static struct kmem_cache *fuse_req_cachep;
  22. static struct fuse_conn *fuse_get_conn(struct file *file)
  23. {
  24. /*
  25. * Lockless access is OK, because file->private data is set
  26. * once during mount and is valid until the file is released.
  27. */
  28. return file->private_data;
  29. }
  30. static void fuse_request_init(struct fuse_req *req)
  31. {
  32. memset(req, 0, sizeof(*req));
  33. INIT_LIST_HEAD(&req->list);
  34. INIT_LIST_HEAD(&req->intr_entry);
  35. init_waitqueue_head(&req->waitq);
  36. atomic_set(&req->count, 1);
  37. }
  38. struct fuse_req *fuse_request_alloc(void)
  39. {
  40. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
  41. if (req)
  42. fuse_request_init(req);
  43. return req;
  44. }
  45. EXPORT_SYMBOL_GPL(fuse_request_alloc);
  46. struct fuse_req *fuse_request_alloc_nofs(void)
  47. {
  48. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
  49. if (req)
  50. fuse_request_init(req);
  51. return req;
  52. }
  53. void fuse_request_free(struct fuse_req *req)
  54. {
  55. kmem_cache_free(fuse_req_cachep, req);
  56. }
  57. static void block_sigs(sigset_t *oldset)
  58. {
  59. sigset_t mask;
  60. siginitsetinv(&mask, sigmask(SIGKILL));
  61. sigprocmask(SIG_BLOCK, &mask, oldset);
  62. }
  63. static void restore_sigs(sigset_t *oldset)
  64. {
  65. sigprocmask(SIG_SETMASK, oldset, NULL);
  66. }
  67. static void __fuse_get_request(struct fuse_req *req)
  68. {
  69. atomic_inc(&req->count);
  70. }
  71. /* Must be called with > 1 refcount */
  72. static void __fuse_put_request(struct fuse_req *req)
  73. {
  74. BUG_ON(atomic_read(&req->count) < 2);
  75. atomic_dec(&req->count);
  76. }
  77. static void fuse_req_init_context(struct fuse_req *req)
  78. {
  79. req->in.h.uid = current_fsuid();
  80. req->in.h.gid = current_fsgid();
  81. req->in.h.pid = current->pid;
  82. }
  83. struct fuse_req *fuse_get_req(struct fuse_conn *fc)
  84. {
  85. struct fuse_req *req;
  86. sigset_t oldset;
  87. int intr;
  88. int err;
  89. atomic_inc(&fc->num_waiting);
  90. block_sigs(&oldset);
  91. intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
  92. restore_sigs(&oldset);
  93. err = -EINTR;
  94. if (intr)
  95. goto out;
  96. err = -ENOTCONN;
  97. if (!fc->connected)
  98. goto out;
  99. req = fuse_request_alloc();
  100. err = -ENOMEM;
  101. if (!req)
  102. goto out;
  103. fuse_req_init_context(req);
  104. req->waiting = 1;
  105. return req;
  106. out:
  107. atomic_dec(&fc->num_waiting);
  108. return ERR_PTR(err);
  109. }
  110. EXPORT_SYMBOL_GPL(fuse_get_req);
  111. /*
  112. * Return request in fuse_file->reserved_req. However that may
  113. * currently be in use. If that is the case, wait for it to become
  114. * available.
  115. */
  116. static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
  117. struct file *file)
  118. {
  119. struct fuse_req *req = NULL;
  120. struct fuse_file *ff = file->private_data;
  121. do {
  122. wait_event(fc->reserved_req_waitq, ff->reserved_req);
  123. spin_lock(&fc->lock);
  124. if (ff->reserved_req) {
  125. req = ff->reserved_req;
  126. ff->reserved_req = NULL;
  127. get_file(file);
  128. req->stolen_file = file;
  129. }
  130. spin_unlock(&fc->lock);
  131. } while (!req);
  132. return req;
  133. }
  134. /*
  135. * Put stolen request back into fuse_file->reserved_req
  136. */
  137. static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
  138. {
  139. struct file *file = req->stolen_file;
  140. struct fuse_file *ff = file->private_data;
  141. spin_lock(&fc->lock);
  142. fuse_request_init(req);
  143. BUG_ON(ff->reserved_req);
  144. ff->reserved_req = req;
  145. wake_up_all(&fc->reserved_req_waitq);
  146. spin_unlock(&fc->lock);
  147. fput(file);
  148. }
  149. /*
  150. * Gets a requests for a file operation, always succeeds
  151. *
  152. * This is used for sending the FLUSH request, which must get to
  153. * userspace, due to POSIX locks which may need to be unlocked.
  154. *
  155. * If allocation fails due to OOM, use the reserved request in
  156. * fuse_file.
  157. *
  158. * This is very unlikely to deadlock accidentally, since the
  159. * filesystem should not have it's own file open. If deadlock is
  160. * intentional, it can still be broken by "aborting" the filesystem.
  161. */
  162. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
  163. {
  164. struct fuse_req *req;
  165. atomic_inc(&fc->num_waiting);
  166. wait_event(fc->blocked_waitq, !fc->blocked);
  167. req = fuse_request_alloc();
  168. if (!req)
  169. req = get_reserved_req(fc, file);
  170. fuse_req_init_context(req);
  171. req->waiting = 1;
  172. return req;
  173. }
  174. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
  175. {
  176. if (atomic_dec_and_test(&req->count)) {
  177. if (req->waiting)
  178. atomic_dec(&fc->num_waiting);
  179. if (req->stolen_file)
  180. put_reserved_req(fc, req);
  181. else
  182. fuse_request_free(req);
  183. }
  184. }
  185. EXPORT_SYMBOL_GPL(fuse_put_request);
  186. static unsigned len_args(unsigned numargs, struct fuse_arg *args)
  187. {
  188. unsigned nbytes = 0;
  189. unsigned i;
  190. for (i = 0; i < numargs; i++)
  191. nbytes += args[i].size;
  192. return nbytes;
  193. }
  194. static u64 fuse_get_unique(struct fuse_conn *fc)
  195. {
  196. fc->reqctr++;
  197. /* zero is special */
  198. if (fc->reqctr == 0)
  199. fc->reqctr = 1;
  200. return fc->reqctr;
  201. }
  202. static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
  203. {
  204. req->in.h.unique = fuse_get_unique(fc);
  205. req->in.h.len = sizeof(struct fuse_in_header) +
  206. len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
  207. list_add_tail(&req->list, &fc->pending);
  208. req->state = FUSE_REQ_PENDING;
  209. if (!req->waiting) {
  210. req->waiting = 1;
  211. atomic_inc(&fc->num_waiting);
  212. }
  213. wake_up(&fc->waitq);
  214. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  215. }
  216. static void flush_bg_queue(struct fuse_conn *fc)
  217. {
  218. while (fc->active_background < fc->max_background &&
  219. !list_empty(&fc->bg_queue)) {
  220. struct fuse_req *req;
  221. req = list_entry(fc->bg_queue.next, struct fuse_req, list);
  222. list_del(&req->list);
  223. fc->active_background++;
  224. queue_request(fc, req);
  225. }
  226. }
  227. /*
  228. * This function is called when a request is finished. Either a reply
  229. * has arrived or it was aborted (and not yet sent) or some error
  230. * occurred during communication with userspace, or the device file
  231. * was closed. The requester thread is woken up (if still waiting),
  232. * the 'end' callback is called if given, else the reference to the
  233. * request is released
  234. *
  235. * Called with fc->lock, unlocks it
  236. */
  237. static void request_end(struct fuse_conn *fc, struct fuse_req *req)
  238. __releases(&fc->lock)
  239. {
  240. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  241. req->end = NULL;
  242. list_del(&req->list);
  243. list_del(&req->intr_entry);
  244. req->state = FUSE_REQ_FINISHED;
  245. if (req->background) {
  246. if (fc->num_background == fc->max_background) {
  247. fc->blocked = 0;
  248. wake_up_all(&fc->blocked_waitq);
  249. }
  250. if (fc->num_background == fc->congestion_threshold &&
  251. fc->connected && fc->bdi_initialized) {
  252. clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  253. clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  254. }
  255. fc->num_background--;
  256. fc->active_background--;
  257. flush_bg_queue(fc);
  258. }
  259. spin_unlock(&fc->lock);
  260. wake_up(&req->waitq);
  261. if (end)
  262. end(fc, req);
  263. fuse_put_request(fc, req);
  264. }
  265. static void wait_answer_interruptible(struct fuse_conn *fc,
  266. struct fuse_req *req)
  267. __releases(&fc->lock)
  268. __acquires(&fc->lock)
  269. {
  270. if (signal_pending(current))
  271. return;
  272. spin_unlock(&fc->lock);
  273. wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
  274. spin_lock(&fc->lock);
  275. }
  276. static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
  277. {
  278. list_add_tail(&req->intr_entry, &fc->interrupts);
  279. wake_up(&fc->waitq);
  280. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  281. }
  282. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  283. __releases(&fc->lock)
  284. __acquires(&fc->lock)
  285. {
  286. if (!fc->no_interrupt) {
  287. /* Any signal may interrupt this */
  288. wait_answer_interruptible(fc, req);
  289. if (req->aborted)
  290. goto aborted;
  291. if (req->state == FUSE_REQ_FINISHED)
  292. return;
  293. req->interrupted = 1;
  294. if (req->state == FUSE_REQ_SENT)
  295. queue_interrupt(fc, req);
  296. }
  297. if (!req->force) {
  298. sigset_t oldset;
  299. /* Only fatal signals may interrupt this */
  300. block_sigs(&oldset);
  301. wait_answer_interruptible(fc, req);
  302. restore_sigs(&oldset);
  303. if (req->aborted)
  304. goto aborted;
  305. if (req->state == FUSE_REQ_FINISHED)
  306. return;
  307. /* Request is not yet in userspace, bail out */
  308. if (req->state == FUSE_REQ_PENDING) {
  309. list_del(&req->list);
  310. __fuse_put_request(req);
  311. req->out.h.error = -EINTR;
  312. return;
  313. }
  314. }
  315. /*
  316. * Either request is already in userspace, or it was forced.
  317. * Wait it out.
  318. */
  319. spin_unlock(&fc->lock);
  320. wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
  321. spin_lock(&fc->lock);
  322. if (!req->aborted)
  323. return;
  324. aborted:
  325. BUG_ON(req->state != FUSE_REQ_FINISHED);
  326. if (req->locked) {
  327. /* This is uninterruptible sleep, because data is
  328. being copied to/from the buffers of req. During
  329. locked state, there mustn't be any filesystem
  330. operation (e.g. page fault), since that could lead
  331. to deadlock */
  332. spin_unlock(&fc->lock);
  333. wait_event(req->waitq, !req->locked);
  334. spin_lock(&fc->lock);
  335. }
  336. }
  337. void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
  338. {
  339. req->isreply = 1;
  340. spin_lock(&fc->lock);
  341. if (!fc->connected)
  342. req->out.h.error = -ENOTCONN;
  343. else if (fc->conn_error)
  344. req->out.h.error = -ECONNREFUSED;
  345. else {
  346. queue_request(fc, req);
  347. /* acquire extra reference, since request is still needed
  348. after request_end() */
  349. __fuse_get_request(req);
  350. request_wait_answer(fc, req);
  351. }
  352. spin_unlock(&fc->lock);
  353. }
  354. EXPORT_SYMBOL_GPL(fuse_request_send);
  355. static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
  356. struct fuse_req *req)
  357. {
  358. req->background = 1;
  359. fc->num_background++;
  360. if (fc->num_background == fc->max_background)
  361. fc->blocked = 1;
  362. if (fc->num_background == fc->congestion_threshold &&
  363. fc->bdi_initialized) {
  364. set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  365. set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  366. }
  367. list_add_tail(&req->list, &fc->bg_queue);
  368. flush_bg_queue(fc);
  369. }
  370. static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
  371. {
  372. spin_lock(&fc->lock);
  373. if (fc->connected) {
  374. fuse_request_send_nowait_locked(fc, req);
  375. spin_unlock(&fc->lock);
  376. } else {
  377. req->out.h.error = -ENOTCONN;
  378. request_end(fc, req);
  379. }
  380. }
  381. void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
  382. {
  383. req->isreply = 0;
  384. fuse_request_send_nowait(fc, req);
  385. }
  386. void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  387. {
  388. req->isreply = 1;
  389. fuse_request_send_nowait(fc, req);
  390. }
  391. EXPORT_SYMBOL_GPL(fuse_request_send_background);
  392. /*
  393. * Called under fc->lock
  394. *
  395. * fc->connected must have been checked previously
  396. */
  397. void fuse_request_send_background_locked(struct fuse_conn *fc,
  398. struct fuse_req *req)
  399. {
  400. req->isreply = 1;
  401. fuse_request_send_nowait_locked(fc, req);
  402. }
  403. /*
  404. * Lock the request. Up to the next unlock_request() there mustn't be
  405. * anything that could cause a page-fault. If the request was already
  406. * aborted bail out.
  407. */
  408. static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
  409. {
  410. int err = 0;
  411. if (req) {
  412. spin_lock(&fc->lock);
  413. if (req->aborted)
  414. err = -ENOENT;
  415. else
  416. req->locked = 1;
  417. spin_unlock(&fc->lock);
  418. }
  419. return err;
  420. }
  421. /*
  422. * Unlock request. If it was aborted during being locked, the
  423. * requester thread is currently waiting for it to be unlocked, so
  424. * wake it up.
  425. */
  426. static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
  427. {
  428. if (req) {
  429. spin_lock(&fc->lock);
  430. req->locked = 0;
  431. if (req->aborted)
  432. wake_up(&req->waitq);
  433. spin_unlock(&fc->lock);
  434. }
  435. }
  436. struct fuse_copy_state {
  437. struct fuse_conn *fc;
  438. int write;
  439. struct fuse_req *req;
  440. const struct iovec *iov;
  441. struct pipe_buffer *pipebufs;
  442. struct pipe_buffer *currbuf;
  443. struct pipe_inode_info *pipe;
  444. unsigned long nr_segs;
  445. unsigned long seglen;
  446. unsigned long addr;
  447. struct page *pg;
  448. void *mapaddr;
  449. void *buf;
  450. unsigned len;
  451. unsigned move_pages:1;
  452. };
  453. static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
  454. int write,
  455. const struct iovec *iov, unsigned long nr_segs)
  456. {
  457. memset(cs, 0, sizeof(*cs));
  458. cs->fc = fc;
  459. cs->write = write;
  460. cs->iov = iov;
  461. cs->nr_segs = nr_segs;
  462. }
  463. /* Unmap and put previous page of userspace buffer */
  464. static void fuse_copy_finish(struct fuse_copy_state *cs)
  465. {
  466. if (cs->currbuf) {
  467. struct pipe_buffer *buf = cs->currbuf;
  468. if (!cs->write) {
  469. buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
  470. } else {
  471. kunmap_atomic(cs->mapaddr, KM_USER0);
  472. buf->len = PAGE_SIZE - cs->len;
  473. }
  474. cs->currbuf = NULL;
  475. cs->mapaddr = NULL;
  476. } else if (cs->mapaddr) {
  477. kunmap_atomic(cs->mapaddr, KM_USER0);
  478. if (cs->write) {
  479. flush_dcache_page(cs->pg);
  480. set_page_dirty_lock(cs->pg);
  481. }
  482. put_page(cs->pg);
  483. cs->mapaddr = NULL;
  484. }
  485. }
  486. /*
  487. * Get another pagefull of userspace buffer, and map it to kernel
  488. * address space, and lock request
  489. */
  490. static int fuse_copy_fill(struct fuse_copy_state *cs)
  491. {
  492. unsigned long offset;
  493. int err;
  494. unlock_request(cs->fc, cs->req);
  495. fuse_copy_finish(cs);
  496. if (cs->pipebufs) {
  497. struct pipe_buffer *buf = cs->pipebufs;
  498. if (!cs->write) {
  499. err = buf->ops->confirm(cs->pipe, buf);
  500. if (err)
  501. return err;
  502. BUG_ON(!cs->nr_segs);
  503. cs->currbuf = buf;
  504. cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
  505. cs->len = buf->len;
  506. cs->buf = cs->mapaddr + buf->offset;
  507. cs->pipebufs++;
  508. cs->nr_segs--;
  509. } else {
  510. struct page *page;
  511. if (cs->nr_segs == cs->pipe->buffers)
  512. return -EIO;
  513. page = alloc_page(GFP_HIGHUSER);
  514. if (!page)
  515. return -ENOMEM;
  516. buf->page = page;
  517. buf->offset = 0;
  518. buf->len = 0;
  519. cs->currbuf = buf;
  520. cs->mapaddr = kmap_atomic(page, KM_USER0);
  521. cs->buf = cs->mapaddr;
  522. cs->len = PAGE_SIZE;
  523. cs->pipebufs++;
  524. cs->nr_segs++;
  525. }
  526. } else {
  527. if (!cs->seglen) {
  528. BUG_ON(!cs->nr_segs);
  529. cs->seglen = cs->iov[0].iov_len;
  530. cs->addr = (unsigned long) cs->iov[0].iov_base;
  531. cs->iov++;
  532. cs->nr_segs--;
  533. }
  534. err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
  535. if (err < 0)
  536. return err;
  537. BUG_ON(err != 1);
  538. offset = cs->addr % PAGE_SIZE;
  539. cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
  540. cs->buf = cs->mapaddr + offset;
  541. cs->len = min(PAGE_SIZE - offset, cs->seglen);
  542. cs->seglen -= cs->len;
  543. cs->addr += cs->len;
  544. }
  545. return lock_request(cs->fc, cs->req);
  546. }
  547. /* Do as much copy to/from userspace buffer as we can */
  548. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  549. {
  550. unsigned ncpy = min(*size, cs->len);
  551. if (val) {
  552. if (cs->write)
  553. memcpy(cs->buf, *val, ncpy);
  554. else
  555. memcpy(*val, cs->buf, ncpy);
  556. *val += ncpy;
  557. }
  558. *size -= ncpy;
  559. cs->len -= ncpy;
  560. cs->buf += ncpy;
  561. return ncpy;
  562. }
  563. static int fuse_check_page(struct page *page)
  564. {
  565. if (page_mapcount(page) ||
  566. page->mapping != NULL ||
  567. page_count(page) != 1 ||
  568. (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
  569. ~(1 << PG_locked |
  570. 1 << PG_referenced |
  571. 1 << PG_uptodate |
  572. 1 << PG_lru |
  573. 1 << PG_active |
  574. 1 << PG_reclaim))) {
  575. printk(KERN_WARNING "fuse: trying to steal weird page\n");
  576. printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
  577. return 1;
  578. }
  579. return 0;
  580. }
  581. static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
  582. {
  583. int err;
  584. struct page *oldpage = *pagep;
  585. struct page *newpage;
  586. struct pipe_buffer *buf = cs->pipebufs;
  587. struct address_space *mapping;
  588. pgoff_t index;
  589. unlock_request(cs->fc, cs->req);
  590. fuse_copy_finish(cs);
  591. err = buf->ops->confirm(cs->pipe, buf);
  592. if (err)
  593. return err;
  594. BUG_ON(!cs->nr_segs);
  595. cs->currbuf = buf;
  596. cs->len = buf->len;
  597. cs->pipebufs++;
  598. cs->nr_segs--;
  599. if (cs->len != PAGE_SIZE)
  600. goto out_fallback;
  601. if (buf->ops->steal(cs->pipe, buf) != 0)
  602. goto out_fallback;
  603. newpage = buf->page;
  604. if (WARN_ON(!PageUptodate(newpage)))
  605. return -EIO;
  606. ClearPageMappedToDisk(newpage);
  607. if (fuse_check_page(newpage) != 0)
  608. goto out_fallback_unlock;
  609. mapping = oldpage->mapping;
  610. index = oldpage->index;
  611. /*
  612. * This is a new and locked page, it shouldn't be mapped or
  613. * have any special flags on it
  614. */
  615. if (WARN_ON(page_mapped(oldpage)))
  616. goto out_fallback_unlock;
  617. if (WARN_ON(page_has_private(oldpage)))
  618. goto out_fallback_unlock;
  619. if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
  620. goto out_fallback_unlock;
  621. if (WARN_ON(PageMlocked(oldpage)))
  622. goto out_fallback_unlock;
  623. remove_from_page_cache(oldpage);
  624. page_cache_release(oldpage);
  625. err = add_to_page_cache_locked(newpage, mapping, index, GFP_KERNEL);
  626. if (err) {
  627. printk(KERN_WARNING "fuse_try_move_page: failed to add page");
  628. goto out_fallback_unlock;
  629. }
  630. page_cache_get(newpage);
  631. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  632. lru_cache_add_file(newpage);
  633. err = 0;
  634. spin_lock(&cs->fc->lock);
  635. if (cs->req->aborted)
  636. err = -ENOENT;
  637. else
  638. *pagep = newpage;
  639. spin_unlock(&cs->fc->lock);
  640. if (err) {
  641. unlock_page(newpage);
  642. page_cache_release(newpage);
  643. return err;
  644. }
  645. unlock_page(oldpage);
  646. page_cache_release(oldpage);
  647. cs->len = 0;
  648. return 0;
  649. out_fallback_unlock:
  650. unlock_page(newpage);
  651. out_fallback:
  652. cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
  653. cs->buf = cs->mapaddr + buf->offset;
  654. err = lock_request(cs->fc, cs->req);
  655. if (err)
  656. return err;
  657. return 1;
  658. }
  659. static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
  660. unsigned offset, unsigned count)
  661. {
  662. struct pipe_buffer *buf;
  663. if (cs->nr_segs == cs->pipe->buffers)
  664. return -EIO;
  665. unlock_request(cs->fc, cs->req);
  666. fuse_copy_finish(cs);
  667. buf = cs->pipebufs;
  668. page_cache_get(page);
  669. buf->page = page;
  670. buf->offset = offset;
  671. buf->len = count;
  672. cs->pipebufs++;
  673. cs->nr_segs++;
  674. cs->len = 0;
  675. return 0;
  676. }
  677. /*
  678. * Copy a page in the request to/from the userspace buffer. Must be
  679. * done atomically
  680. */
  681. static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
  682. unsigned offset, unsigned count, int zeroing)
  683. {
  684. int err;
  685. struct page *page = *pagep;
  686. if (page && zeroing && count < PAGE_SIZE) {
  687. void *mapaddr = kmap_atomic(page, KM_USER1);
  688. memset(mapaddr, 0, PAGE_SIZE);
  689. kunmap_atomic(mapaddr, KM_USER1);
  690. }
  691. while (count) {
  692. if (cs->write && cs->pipebufs && page) {
  693. return fuse_ref_page(cs, page, offset, count);
  694. } else if (!cs->len) {
  695. if (cs->move_pages && page &&
  696. offset == 0 && count == PAGE_SIZE) {
  697. err = fuse_try_move_page(cs, pagep);
  698. if (err <= 0)
  699. return err;
  700. } else {
  701. err = fuse_copy_fill(cs);
  702. if (err)
  703. return err;
  704. }
  705. }
  706. if (page) {
  707. void *mapaddr = kmap_atomic(page, KM_USER1);
  708. void *buf = mapaddr + offset;
  709. offset += fuse_copy_do(cs, &buf, &count);
  710. kunmap_atomic(mapaddr, KM_USER1);
  711. } else
  712. offset += fuse_copy_do(cs, NULL, &count);
  713. }
  714. if (page && !cs->write)
  715. flush_dcache_page(page);
  716. return 0;
  717. }
  718. /* Copy pages in the request to/from userspace buffer */
  719. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  720. int zeroing)
  721. {
  722. unsigned i;
  723. struct fuse_req *req = cs->req;
  724. unsigned offset = req->page_offset;
  725. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  726. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  727. int err;
  728. err = fuse_copy_page(cs, &req->pages[i], offset, count,
  729. zeroing);
  730. if (err)
  731. return err;
  732. nbytes -= count;
  733. count = min(nbytes, (unsigned) PAGE_SIZE);
  734. offset = 0;
  735. }
  736. return 0;
  737. }
  738. /* Copy a single argument in the request to/from userspace buffer */
  739. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  740. {
  741. while (size) {
  742. if (!cs->len) {
  743. int err = fuse_copy_fill(cs);
  744. if (err)
  745. return err;
  746. }
  747. fuse_copy_do(cs, &val, &size);
  748. }
  749. return 0;
  750. }
  751. /* Copy request arguments to/from userspace buffer */
  752. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  753. unsigned argpages, struct fuse_arg *args,
  754. int zeroing)
  755. {
  756. int err = 0;
  757. unsigned i;
  758. for (i = 0; !err && i < numargs; i++) {
  759. struct fuse_arg *arg = &args[i];
  760. if (i == numargs - 1 && argpages)
  761. err = fuse_copy_pages(cs, arg->size, zeroing);
  762. else
  763. err = fuse_copy_one(cs, arg->value, arg->size);
  764. }
  765. return err;
  766. }
  767. static int request_pending(struct fuse_conn *fc)
  768. {
  769. return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
  770. }
  771. /* Wait until a request is available on the pending list */
  772. static void request_wait(struct fuse_conn *fc)
  773. __releases(&fc->lock)
  774. __acquires(&fc->lock)
  775. {
  776. DECLARE_WAITQUEUE(wait, current);
  777. add_wait_queue_exclusive(&fc->waitq, &wait);
  778. while (fc->connected && !request_pending(fc)) {
  779. set_current_state(TASK_INTERRUPTIBLE);
  780. if (signal_pending(current))
  781. break;
  782. spin_unlock(&fc->lock);
  783. schedule();
  784. spin_lock(&fc->lock);
  785. }
  786. set_current_state(TASK_RUNNING);
  787. remove_wait_queue(&fc->waitq, &wait);
  788. }
  789. /*
  790. * Transfer an interrupt request to userspace
  791. *
  792. * Unlike other requests this is assembled on demand, without a need
  793. * to allocate a separate fuse_req structure.
  794. *
  795. * Called with fc->lock held, releases it
  796. */
  797. static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
  798. size_t nbytes, struct fuse_req *req)
  799. __releases(&fc->lock)
  800. {
  801. struct fuse_in_header ih;
  802. struct fuse_interrupt_in arg;
  803. unsigned reqsize = sizeof(ih) + sizeof(arg);
  804. int err;
  805. list_del_init(&req->intr_entry);
  806. req->intr_unique = fuse_get_unique(fc);
  807. memset(&ih, 0, sizeof(ih));
  808. memset(&arg, 0, sizeof(arg));
  809. ih.len = reqsize;
  810. ih.opcode = FUSE_INTERRUPT;
  811. ih.unique = req->intr_unique;
  812. arg.unique = req->in.h.unique;
  813. spin_unlock(&fc->lock);
  814. if (nbytes < reqsize)
  815. return -EINVAL;
  816. err = fuse_copy_one(cs, &ih, sizeof(ih));
  817. if (!err)
  818. err = fuse_copy_one(cs, &arg, sizeof(arg));
  819. fuse_copy_finish(cs);
  820. return err ? err : reqsize;
  821. }
  822. /*
  823. * Read a single request into the userspace filesystem's buffer. This
  824. * function waits until a request is available, then removes it from
  825. * the pending list and copies request data to userspace buffer. If
  826. * no reply is needed (FORGET) or request has been aborted or there
  827. * was an error during the copying then it's finished by calling
  828. * request_end(). Otherwise add it to the processing list, and set
  829. * the 'sent' flag.
  830. */
  831. static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
  832. struct fuse_copy_state *cs, size_t nbytes)
  833. {
  834. int err;
  835. struct fuse_req *req;
  836. struct fuse_in *in;
  837. unsigned reqsize;
  838. restart:
  839. spin_lock(&fc->lock);
  840. err = -EAGAIN;
  841. if ((file->f_flags & O_NONBLOCK) && fc->connected &&
  842. !request_pending(fc))
  843. goto err_unlock;
  844. request_wait(fc);
  845. err = -ENODEV;
  846. if (!fc->connected)
  847. goto err_unlock;
  848. err = -ERESTARTSYS;
  849. if (!request_pending(fc))
  850. goto err_unlock;
  851. if (!list_empty(&fc->interrupts)) {
  852. req = list_entry(fc->interrupts.next, struct fuse_req,
  853. intr_entry);
  854. return fuse_read_interrupt(fc, cs, nbytes, req);
  855. }
  856. req = list_entry(fc->pending.next, struct fuse_req, list);
  857. req->state = FUSE_REQ_READING;
  858. list_move(&req->list, &fc->io);
  859. in = &req->in;
  860. reqsize = in->h.len;
  861. /* If request is too large, reply with an error and restart the read */
  862. if (nbytes < reqsize) {
  863. req->out.h.error = -EIO;
  864. /* SETXATTR is special, since it may contain too large data */
  865. if (in->h.opcode == FUSE_SETXATTR)
  866. req->out.h.error = -E2BIG;
  867. request_end(fc, req);
  868. goto restart;
  869. }
  870. spin_unlock(&fc->lock);
  871. cs->req = req;
  872. err = fuse_copy_one(cs, &in->h, sizeof(in->h));
  873. if (!err)
  874. err = fuse_copy_args(cs, in->numargs, in->argpages,
  875. (struct fuse_arg *) in->args, 0);
  876. fuse_copy_finish(cs);
  877. spin_lock(&fc->lock);
  878. req->locked = 0;
  879. if (req->aborted) {
  880. request_end(fc, req);
  881. return -ENODEV;
  882. }
  883. if (err) {
  884. req->out.h.error = -EIO;
  885. request_end(fc, req);
  886. return err;
  887. }
  888. if (!req->isreply)
  889. request_end(fc, req);
  890. else {
  891. req->state = FUSE_REQ_SENT;
  892. list_move_tail(&req->list, &fc->processing);
  893. if (req->interrupted)
  894. queue_interrupt(fc, req);
  895. spin_unlock(&fc->lock);
  896. }
  897. return reqsize;
  898. err_unlock:
  899. spin_unlock(&fc->lock);
  900. return err;
  901. }
  902. static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
  903. unsigned long nr_segs, loff_t pos)
  904. {
  905. struct fuse_copy_state cs;
  906. struct file *file = iocb->ki_filp;
  907. struct fuse_conn *fc = fuse_get_conn(file);
  908. if (!fc)
  909. return -EPERM;
  910. fuse_copy_init(&cs, fc, 1, iov, nr_segs);
  911. return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
  912. }
  913. static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
  914. struct pipe_buffer *buf)
  915. {
  916. return 1;
  917. }
  918. static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
  919. .can_merge = 0,
  920. .map = generic_pipe_buf_map,
  921. .unmap = generic_pipe_buf_unmap,
  922. .confirm = generic_pipe_buf_confirm,
  923. .release = generic_pipe_buf_release,
  924. .steal = fuse_dev_pipe_buf_steal,
  925. .get = generic_pipe_buf_get,
  926. };
  927. static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
  928. struct pipe_inode_info *pipe,
  929. size_t len, unsigned int flags)
  930. {
  931. int ret;
  932. int page_nr = 0;
  933. int do_wakeup = 0;
  934. struct pipe_buffer *bufs;
  935. struct fuse_copy_state cs;
  936. struct fuse_conn *fc = fuse_get_conn(in);
  937. if (!fc)
  938. return -EPERM;
  939. bufs = kmalloc(pipe->buffers * sizeof (struct pipe_buffer), GFP_KERNEL);
  940. if (!bufs)
  941. return -ENOMEM;
  942. fuse_copy_init(&cs, fc, 1, NULL, 0);
  943. cs.pipebufs = bufs;
  944. cs.pipe = pipe;
  945. ret = fuse_dev_do_read(fc, in, &cs, len);
  946. if (ret < 0)
  947. goto out;
  948. ret = 0;
  949. pipe_lock(pipe);
  950. if (!pipe->readers) {
  951. send_sig(SIGPIPE, current, 0);
  952. if (!ret)
  953. ret = -EPIPE;
  954. goto out_unlock;
  955. }
  956. if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
  957. ret = -EIO;
  958. goto out_unlock;
  959. }
  960. while (page_nr < cs.nr_segs) {
  961. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  962. struct pipe_buffer *buf = pipe->bufs + newbuf;
  963. buf->page = bufs[page_nr].page;
  964. buf->offset = bufs[page_nr].offset;
  965. buf->len = bufs[page_nr].len;
  966. buf->ops = &fuse_dev_pipe_buf_ops;
  967. pipe->nrbufs++;
  968. page_nr++;
  969. ret += buf->len;
  970. if (pipe->inode)
  971. do_wakeup = 1;
  972. }
  973. out_unlock:
  974. pipe_unlock(pipe);
  975. if (do_wakeup) {
  976. smp_mb();
  977. if (waitqueue_active(&pipe->wait))
  978. wake_up_interruptible(&pipe->wait);
  979. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  980. }
  981. out:
  982. for (; page_nr < cs.nr_segs; page_nr++)
  983. page_cache_release(bufs[page_nr].page);
  984. kfree(bufs);
  985. return ret;
  986. }
  987. static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
  988. struct fuse_copy_state *cs)
  989. {
  990. struct fuse_notify_poll_wakeup_out outarg;
  991. int err = -EINVAL;
  992. if (size != sizeof(outarg))
  993. goto err;
  994. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  995. if (err)
  996. goto err;
  997. fuse_copy_finish(cs);
  998. return fuse_notify_poll_wakeup(fc, &outarg);
  999. err:
  1000. fuse_copy_finish(cs);
  1001. return err;
  1002. }
  1003. static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
  1004. struct fuse_copy_state *cs)
  1005. {
  1006. struct fuse_notify_inval_inode_out outarg;
  1007. int err = -EINVAL;
  1008. if (size != sizeof(outarg))
  1009. goto err;
  1010. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1011. if (err)
  1012. goto err;
  1013. fuse_copy_finish(cs);
  1014. down_read(&fc->killsb);
  1015. err = -ENOENT;
  1016. if (fc->sb) {
  1017. err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
  1018. outarg.off, outarg.len);
  1019. }
  1020. up_read(&fc->killsb);
  1021. return err;
  1022. err:
  1023. fuse_copy_finish(cs);
  1024. return err;
  1025. }
  1026. static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
  1027. struct fuse_copy_state *cs)
  1028. {
  1029. struct fuse_notify_inval_entry_out outarg;
  1030. int err = -ENOMEM;
  1031. char *buf;
  1032. struct qstr name;
  1033. buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
  1034. if (!buf)
  1035. goto err;
  1036. err = -EINVAL;
  1037. if (size < sizeof(outarg))
  1038. goto err;
  1039. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1040. if (err)
  1041. goto err;
  1042. err = -ENAMETOOLONG;
  1043. if (outarg.namelen > FUSE_NAME_MAX)
  1044. goto err;
  1045. name.name = buf;
  1046. name.len = outarg.namelen;
  1047. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1048. if (err)
  1049. goto err;
  1050. fuse_copy_finish(cs);
  1051. buf[outarg.namelen] = 0;
  1052. name.hash = full_name_hash(name.name, name.len);
  1053. down_read(&fc->killsb);
  1054. err = -ENOENT;
  1055. if (fc->sb)
  1056. err = fuse_reverse_inval_entry(fc->sb, outarg.parent, &name);
  1057. up_read(&fc->killsb);
  1058. kfree(buf);
  1059. return err;
  1060. err:
  1061. kfree(buf);
  1062. fuse_copy_finish(cs);
  1063. return err;
  1064. }
  1065. static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
  1066. unsigned int size, struct fuse_copy_state *cs)
  1067. {
  1068. switch (code) {
  1069. case FUSE_NOTIFY_POLL:
  1070. return fuse_notify_poll(fc, size, cs);
  1071. case FUSE_NOTIFY_INVAL_INODE:
  1072. return fuse_notify_inval_inode(fc, size, cs);
  1073. case FUSE_NOTIFY_INVAL_ENTRY:
  1074. return fuse_notify_inval_entry(fc, size, cs);
  1075. default:
  1076. fuse_copy_finish(cs);
  1077. return -EINVAL;
  1078. }
  1079. }
  1080. /* Look up request on processing list by unique ID */
  1081. static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
  1082. {
  1083. struct list_head *entry;
  1084. list_for_each(entry, &fc->processing) {
  1085. struct fuse_req *req;
  1086. req = list_entry(entry, struct fuse_req, list);
  1087. if (req->in.h.unique == unique || req->intr_unique == unique)
  1088. return req;
  1089. }
  1090. return NULL;
  1091. }
  1092. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  1093. unsigned nbytes)
  1094. {
  1095. unsigned reqsize = sizeof(struct fuse_out_header);
  1096. if (out->h.error)
  1097. return nbytes != reqsize ? -EINVAL : 0;
  1098. reqsize += len_args(out->numargs, out->args);
  1099. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  1100. return -EINVAL;
  1101. else if (reqsize > nbytes) {
  1102. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  1103. unsigned diffsize = reqsize - nbytes;
  1104. if (diffsize > lastarg->size)
  1105. return -EINVAL;
  1106. lastarg->size -= diffsize;
  1107. }
  1108. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  1109. out->page_zeroing);
  1110. }
  1111. /*
  1112. * Write a single reply to a request. First the header is copied from
  1113. * the write buffer. The request is then searched on the processing
  1114. * list by the unique ID found in the header. If found, then remove
  1115. * it from the list and copy the rest of the buffer to the request.
  1116. * The request is finished by calling request_end()
  1117. */
  1118. static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
  1119. struct fuse_copy_state *cs, size_t nbytes)
  1120. {
  1121. int err;
  1122. struct fuse_req *req;
  1123. struct fuse_out_header oh;
  1124. if (nbytes < sizeof(struct fuse_out_header))
  1125. return -EINVAL;
  1126. err = fuse_copy_one(cs, &oh, sizeof(oh));
  1127. if (err)
  1128. goto err_finish;
  1129. err = -EINVAL;
  1130. if (oh.len != nbytes)
  1131. goto err_finish;
  1132. /*
  1133. * Zero oh.unique indicates unsolicited notification message
  1134. * and error contains notification code.
  1135. */
  1136. if (!oh.unique) {
  1137. err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
  1138. return err ? err : nbytes;
  1139. }
  1140. err = -EINVAL;
  1141. if (oh.error <= -1000 || oh.error > 0)
  1142. goto err_finish;
  1143. spin_lock(&fc->lock);
  1144. err = -ENOENT;
  1145. if (!fc->connected)
  1146. goto err_unlock;
  1147. req = request_find(fc, oh.unique);
  1148. if (!req)
  1149. goto err_unlock;
  1150. if (req->aborted) {
  1151. spin_unlock(&fc->lock);
  1152. fuse_copy_finish(cs);
  1153. spin_lock(&fc->lock);
  1154. request_end(fc, req);
  1155. return -ENOENT;
  1156. }
  1157. /* Is it an interrupt reply? */
  1158. if (req->intr_unique == oh.unique) {
  1159. err = -EINVAL;
  1160. if (nbytes != sizeof(struct fuse_out_header))
  1161. goto err_unlock;
  1162. if (oh.error == -ENOSYS)
  1163. fc->no_interrupt = 1;
  1164. else if (oh.error == -EAGAIN)
  1165. queue_interrupt(fc, req);
  1166. spin_unlock(&fc->lock);
  1167. fuse_copy_finish(cs);
  1168. return nbytes;
  1169. }
  1170. req->state = FUSE_REQ_WRITING;
  1171. list_move(&req->list, &fc->io);
  1172. req->out.h = oh;
  1173. req->locked = 1;
  1174. cs->req = req;
  1175. if (!req->out.page_replace)
  1176. cs->move_pages = 0;
  1177. spin_unlock(&fc->lock);
  1178. err = copy_out_args(cs, &req->out, nbytes);
  1179. fuse_copy_finish(cs);
  1180. spin_lock(&fc->lock);
  1181. req->locked = 0;
  1182. if (!err) {
  1183. if (req->aborted)
  1184. err = -ENOENT;
  1185. } else if (!req->aborted)
  1186. req->out.h.error = -EIO;
  1187. request_end(fc, req);
  1188. return err ? err : nbytes;
  1189. err_unlock:
  1190. spin_unlock(&fc->lock);
  1191. err_finish:
  1192. fuse_copy_finish(cs);
  1193. return err;
  1194. }
  1195. static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
  1196. unsigned long nr_segs, loff_t pos)
  1197. {
  1198. struct fuse_copy_state cs;
  1199. struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
  1200. if (!fc)
  1201. return -EPERM;
  1202. fuse_copy_init(&cs, fc, 0, iov, nr_segs);
  1203. return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
  1204. }
  1205. static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
  1206. struct file *out, loff_t *ppos,
  1207. size_t len, unsigned int flags)
  1208. {
  1209. unsigned nbuf;
  1210. unsigned idx;
  1211. struct pipe_buffer *bufs;
  1212. struct fuse_copy_state cs;
  1213. struct fuse_conn *fc;
  1214. size_t rem;
  1215. ssize_t ret;
  1216. fc = fuse_get_conn(out);
  1217. if (!fc)
  1218. return -EPERM;
  1219. bufs = kmalloc(pipe->buffers * sizeof (struct pipe_buffer), GFP_KERNEL);
  1220. if (!bufs)
  1221. return -ENOMEM;
  1222. pipe_lock(pipe);
  1223. nbuf = 0;
  1224. rem = 0;
  1225. for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
  1226. rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
  1227. ret = -EINVAL;
  1228. if (rem < len) {
  1229. pipe_unlock(pipe);
  1230. goto out;
  1231. }
  1232. rem = len;
  1233. while (rem) {
  1234. struct pipe_buffer *ibuf;
  1235. struct pipe_buffer *obuf;
  1236. BUG_ON(nbuf >= pipe->buffers);
  1237. BUG_ON(!pipe->nrbufs);
  1238. ibuf = &pipe->bufs[pipe->curbuf];
  1239. obuf = &bufs[nbuf];
  1240. if (rem >= ibuf->len) {
  1241. *obuf = *ibuf;
  1242. ibuf->ops = NULL;
  1243. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  1244. pipe->nrbufs--;
  1245. } else {
  1246. ibuf->ops->get(pipe, ibuf);
  1247. *obuf = *ibuf;
  1248. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1249. obuf->len = rem;
  1250. ibuf->offset += obuf->len;
  1251. ibuf->len -= obuf->len;
  1252. }
  1253. nbuf++;
  1254. rem -= obuf->len;
  1255. }
  1256. pipe_unlock(pipe);
  1257. fuse_copy_init(&cs, fc, 0, NULL, nbuf);
  1258. cs.pipebufs = bufs;
  1259. cs.pipe = pipe;
  1260. if (flags & SPLICE_F_MOVE)
  1261. cs.move_pages = 1;
  1262. ret = fuse_dev_do_write(fc, &cs, len);
  1263. for (idx = 0; idx < nbuf; idx++) {
  1264. struct pipe_buffer *buf = &bufs[idx];
  1265. buf->ops->release(pipe, buf);
  1266. }
  1267. out:
  1268. kfree(bufs);
  1269. return ret;
  1270. }
  1271. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  1272. {
  1273. unsigned mask = POLLOUT | POLLWRNORM;
  1274. struct fuse_conn *fc = fuse_get_conn(file);
  1275. if (!fc)
  1276. return POLLERR;
  1277. poll_wait(file, &fc->waitq, wait);
  1278. spin_lock(&fc->lock);
  1279. if (!fc->connected)
  1280. mask = POLLERR;
  1281. else if (request_pending(fc))
  1282. mask |= POLLIN | POLLRDNORM;
  1283. spin_unlock(&fc->lock);
  1284. return mask;
  1285. }
  1286. /*
  1287. * Abort all requests on the given list (pending or processing)
  1288. *
  1289. * This function releases and reacquires fc->lock
  1290. */
  1291. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  1292. __releases(&fc->lock)
  1293. __acquires(&fc->lock)
  1294. {
  1295. while (!list_empty(head)) {
  1296. struct fuse_req *req;
  1297. req = list_entry(head->next, struct fuse_req, list);
  1298. req->out.h.error = -ECONNABORTED;
  1299. request_end(fc, req);
  1300. spin_lock(&fc->lock);
  1301. }
  1302. }
  1303. /*
  1304. * Abort requests under I/O
  1305. *
  1306. * The requests are set to aborted and finished, and the request
  1307. * waiter is woken up. This will make request_wait_answer() wait
  1308. * until the request is unlocked and then return.
  1309. *
  1310. * If the request is asynchronous, then the end function needs to be
  1311. * called after waiting for the request to be unlocked (if it was
  1312. * locked).
  1313. */
  1314. static void end_io_requests(struct fuse_conn *fc)
  1315. __releases(&fc->lock)
  1316. __acquires(&fc->lock)
  1317. {
  1318. while (!list_empty(&fc->io)) {
  1319. struct fuse_req *req =
  1320. list_entry(fc->io.next, struct fuse_req, list);
  1321. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  1322. req->aborted = 1;
  1323. req->out.h.error = -ECONNABORTED;
  1324. req->state = FUSE_REQ_FINISHED;
  1325. list_del_init(&req->list);
  1326. wake_up(&req->waitq);
  1327. if (end) {
  1328. req->end = NULL;
  1329. __fuse_get_request(req);
  1330. spin_unlock(&fc->lock);
  1331. wait_event(req->waitq, !req->locked);
  1332. end(fc, req);
  1333. fuse_put_request(fc, req);
  1334. spin_lock(&fc->lock);
  1335. }
  1336. }
  1337. }
  1338. /*
  1339. * Abort all requests.
  1340. *
  1341. * Emergency exit in case of a malicious or accidental deadlock, or
  1342. * just a hung filesystem.
  1343. *
  1344. * The same effect is usually achievable through killing the
  1345. * filesystem daemon and all users of the filesystem. The exception
  1346. * is the combination of an asynchronous request and the tricky
  1347. * deadlock (see Documentation/filesystems/fuse.txt).
  1348. *
  1349. * During the aborting, progression of requests from the pending and
  1350. * processing lists onto the io list, and progression of new requests
  1351. * onto the pending list is prevented by req->connected being false.
  1352. *
  1353. * Progression of requests under I/O to the processing list is
  1354. * prevented by the req->aborted flag being true for these requests.
  1355. * For this reason requests on the io list must be aborted first.
  1356. */
  1357. void fuse_abort_conn(struct fuse_conn *fc)
  1358. {
  1359. spin_lock(&fc->lock);
  1360. if (fc->connected) {
  1361. fc->connected = 0;
  1362. fc->blocked = 0;
  1363. end_io_requests(fc);
  1364. end_requests(fc, &fc->pending);
  1365. end_requests(fc, &fc->processing);
  1366. wake_up_all(&fc->waitq);
  1367. wake_up_all(&fc->blocked_waitq);
  1368. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  1369. }
  1370. spin_unlock(&fc->lock);
  1371. }
  1372. EXPORT_SYMBOL_GPL(fuse_abort_conn);
  1373. int fuse_dev_release(struct inode *inode, struct file *file)
  1374. {
  1375. struct fuse_conn *fc = fuse_get_conn(file);
  1376. if (fc) {
  1377. spin_lock(&fc->lock);
  1378. fc->connected = 0;
  1379. end_requests(fc, &fc->pending);
  1380. end_requests(fc, &fc->processing);
  1381. spin_unlock(&fc->lock);
  1382. fuse_conn_put(fc);
  1383. }
  1384. return 0;
  1385. }
  1386. EXPORT_SYMBOL_GPL(fuse_dev_release);
  1387. static int fuse_dev_fasync(int fd, struct file *file, int on)
  1388. {
  1389. struct fuse_conn *fc = fuse_get_conn(file);
  1390. if (!fc)
  1391. return -EPERM;
  1392. /* No locking - fasync_helper does its own locking */
  1393. return fasync_helper(fd, file, on, &fc->fasync);
  1394. }
  1395. const struct file_operations fuse_dev_operations = {
  1396. .owner = THIS_MODULE,
  1397. .llseek = no_llseek,
  1398. .read = do_sync_read,
  1399. .aio_read = fuse_dev_read,
  1400. .splice_read = fuse_dev_splice_read,
  1401. .write = do_sync_write,
  1402. .aio_write = fuse_dev_write,
  1403. .splice_write = fuse_dev_splice_write,
  1404. .poll = fuse_dev_poll,
  1405. .release = fuse_dev_release,
  1406. .fasync = fuse_dev_fasync,
  1407. };
  1408. EXPORT_SYMBOL_GPL(fuse_dev_operations);
  1409. static struct miscdevice fuse_miscdevice = {
  1410. .minor = FUSE_MINOR,
  1411. .name = "fuse",
  1412. .fops = &fuse_dev_operations,
  1413. };
  1414. int __init fuse_dev_init(void)
  1415. {
  1416. int err = -ENOMEM;
  1417. fuse_req_cachep = kmem_cache_create("fuse_request",
  1418. sizeof(struct fuse_req),
  1419. 0, 0, NULL);
  1420. if (!fuse_req_cachep)
  1421. goto out;
  1422. err = misc_register(&fuse_miscdevice);
  1423. if (err)
  1424. goto out_cache_clean;
  1425. return 0;
  1426. out_cache_clean:
  1427. kmem_cache_destroy(fuse_req_cachep);
  1428. out:
  1429. return err;
  1430. }
  1431. void fuse_dev_cleanup(void)
  1432. {
  1433. misc_deregister(&fuse_miscdevice);
  1434. kmem_cache_destroy(fuse_req_cachep);
  1435. }