dev.c 52 KB

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