dev.c 46 KB

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