dev.c 45 KB

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