dev.c 47 KB

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