dev.c 35 KB

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