dev.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  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 "dev_uring_i.h"
  8. #include "fuse_i.h"
  9. #include "fuse_dev_i.h"
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/poll.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/uio.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/file.h>
  18. #include <linux/slab.h>
  19. #include <linux/pipe_fs_i.h>
  20. #include <linux/swap.h>
  21. #include <linux/splice.h>
  22. #include <linux/sched.h>
  23. #include <linux/seq_file.h>
  24. #include "fuse_trace.h"
  25. MODULE_ALIAS_MISCDEV(FUSE_MINOR);
  26. MODULE_ALIAS("devname:fuse");
  27. static struct kmem_cache *fuse_req_cachep;
  28. const unsigned long fuse_timeout_timer_freq =
  29. secs_to_jiffies(FUSE_TIMEOUT_TIMER_FREQ);
  30. bool fuse_request_expired(struct fuse_conn *fc, struct list_head *list)
  31. {
  32. struct fuse_req *req;
  33. req = list_first_entry_or_null(list, struct fuse_req, list);
  34. if (!req)
  35. return false;
  36. return time_is_before_jiffies(req->create_time + fc->timeout.req_timeout);
  37. }
  38. static bool fuse_fpq_processing_expired(struct fuse_conn *fc, struct list_head *processing)
  39. {
  40. int i;
  41. for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
  42. if (fuse_request_expired(fc, &processing[i]))
  43. return true;
  44. return false;
  45. }
  46. /*
  47. * Check if any requests aren't being completed by the time the request timeout
  48. * elapses. To do so, we:
  49. * - check the fiq pending list
  50. * - check the bg queue
  51. * - check the fpq io and processing lists
  52. *
  53. * To make this fast, we only check against the head request on each list since
  54. * these are generally queued in order of creation time (eg newer requests get
  55. * queued to the tail). We might miss a few edge cases (eg requests transitioning
  56. * between lists, re-sent requests at the head of the pending list having a
  57. * later creation time than other requests on that list, etc.) but that is fine
  58. * since if the request never gets fulfilled, it will eventually be caught.
  59. */
  60. void fuse_check_timeout(struct work_struct *work)
  61. {
  62. struct delayed_work *dwork = to_delayed_work(work);
  63. struct fuse_conn *fc = container_of(dwork, struct fuse_conn,
  64. timeout.work);
  65. struct fuse_iqueue *fiq = &fc->iq;
  66. struct fuse_dev *fud;
  67. struct fuse_pqueue *fpq;
  68. bool expired = false;
  69. if (!atomic_read(&fc->num_waiting))
  70. goto out;
  71. spin_lock(&fiq->lock);
  72. expired = fuse_request_expired(fc, &fiq->pending);
  73. spin_unlock(&fiq->lock);
  74. if (expired)
  75. goto abort_conn;
  76. spin_lock(&fc->bg_lock);
  77. expired = fuse_request_expired(fc, &fc->bg_queue);
  78. spin_unlock(&fc->bg_lock);
  79. if (expired)
  80. goto abort_conn;
  81. spin_lock(&fc->lock);
  82. if (!fc->connected) {
  83. spin_unlock(&fc->lock);
  84. return;
  85. }
  86. list_for_each_entry(fud, &fc->devices, entry) {
  87. fpq = &fud->pq;
  88. spin_lock(&fpq->lock);
  89. if (fuse_request_expired(fc, &fpq->io) ||
  90. fuse_fpq_processing_expired(fc, fpq->processing)) {
  91. spin_unlock(&fpq->lock);
  92. spin_unlock(&fc->lock);
  93. goto abort_conn;
  94. }
  95. spin_unlock(&fpq->lock);
  96. }
  97. spin_unlock(&fc->lock);
  98. if (fuse_uring_request_expired(fc))
  99. goto abort_conn;
  100. out:
  101. queue_delayed_work(system_percpu_wq, &fc->timeout.work,
  102. fuse_timeout_timer_freq);
  103. return;
  104. abort_conn:
  105. fuse_abort_conn(fc);
  106. }
  107. static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req)
  108. {
  109. INIT_LIST_HEAD(&req->list);
  110. INIT_LIST_HEAD(&req->intr_entry);
  111. init_waitqueue_head(&req->waitq);
  112. refcount_set(&req->count, 1);
  113. __set_bit(FR_PENDING, &req->flags);
  114. req->fm = fm;
  115. req->create_time = jiffies;
  116. }
  117. static struct fuse_req *fuse_request_alloc(struct fuse_mount *fm, gfp_t flags)
  118. {
  119. struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
  120. if (req)
  121. fuse_request_init(fm, req);
  122. return req;
  123. }
  124. static void fuse_request_free(struct fuse_req *req)
  125. {
  126. kmem_cache_free(fuse_req_cachep, req);
  127. }
  128. static void __fuse_get_request(struct fuse_req *req)
  129. {
  130. refcount_inc(&req->count);
  131. }
  132. /* Must be called with > 1 refcount */
  133. static void __fuse_put_request(struct fuse_req *req)
  134. {
  135. refcount_dec(&req->count);
  136. }
  137. void fuse_set_initialized(struct fuse_conn *fc)
  138. {
  139. /* Make sure stores before this are seen on another CPU */
  140. smp_wmb();
  141. fc->initialized = 1;
  142. }
  143. static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
  144. {
  145. return !fc->initialized || (for_background && fc->blocked) ||
  146. (fc->io_uring && fc->connected && !fuse_uring_ready(fc));
  147. }
  148. static void fuse_drop_waiting(struct fuse_conn *fc)
  149. {
  150. /*
  151. * lockess check of fc->connected is okay, because atomic_dec_and_test()
  152. * provides a memory barrier matched with the one in fuse_wait_aborted()
  153. * to ensure no wake-up is missed.
  154. */
  155. if (atomic_dec_and_test(&fc->num_waiting) &&
  156. !READ_ONCE(fc->connected)) {
  157. /* wake up aborters */
  158. wake_up_all(&fc->blocked_waitq);
  159. }
  160. }
  161. static void fuse_put_request(struct fuse_req *req);
  162. static struct fuse_req *fuse_get_req(struct mnt_idmap *idmap,
  163. struct fuse_mount *fm,
  164. bool for_background)
  165. {
  166. struct fuse_conn *fc = fm->fc;
  167. struct fuse_req *req;
  168. bool no_idmap = !fm->sb || (fm->sb->s_iflags & SB_I_NOIDMAP);
  169. kuid_t fsuid;
  170. kgid_t fsgid;
  171. int err;
  172. atomic_inc(&fc->num_waiting);
  173. if (fuse_block_alloc(fc, for_background)) {
  174. err = -EINTR;
  175. if (wait_event_state_exclusive(fc->blocked_waitq,
  176. !fuse_block_alloc(fc, for_background),
  177. (TASK_KILLABLE | TASK_FREEZABLE)))
  178. goto out;
  179. }
  180. /* Matches smp_wmb() in fuse_set_initialized() */
  181. smp_rmb();
  182. err = -ENOTCONN;
  183. if (!fc->connected)
  184. goto out;
  185. err = -ECONNREFUSED;
  186. if (fc->conn_error)
  187. goto out;
  188. req = fuse_request_alloc(fm, GFP_KERNEL);
  189. err = -ENOMEM;
  190. if (!req) {
  191. if (for_background)
  192. wake_up(&fc->blocked_waitq);
  193. goto out;
  194. }
  195. req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
  196. __set_bit(FR_WAITING, &req->flags);
  197. if (for_background)
  198. __set_bit(FR_BACKGROUND, &req->flags);
  199. /*
  200. * Keep the old behavior when idmappings support was not
  201. * declared by a FUSE server.
  202. *
  203. * For those FUSE servers who support idmapped mounts,
  204. * we send UID/GID only along with "inode creation"
  205. * fuse requests, otherwise idmap == &invalid_mnt_idmap and
  206. * req->in.h.{u,g}id will be equal to FUSE_INVALID_UIDGID.
  207. */
  208. fsuid = no_idmap ? current_fsuid() : mapped_fsuid(idmap, fc->user_ns);
  209. fsgid = no_idmap ? current_fsgid() : mapped_fsgid(idmap, fc->user_ns);
  210. req->in.h.uid = from_kuid(fc->user_ns, fsuid);
  211. req->in.h.gid = from_kgid(fc->user_ns, fsgid);
  212. if (no_idmap && unlikely(req->in.h.uid == ((uid_t)-1) ||
  213. req->in.h.gid == ((gid_t)-1))) {
  214. fuse_put_request(req);
  215. return ERR_PTR(-EOVERFLOW);
  216. }
  217. return req;
  218. out:
  219. fuse_drop_waiting(fc);
  220. return ERR_PTR(err);
  221. }
  222. static void fuse_put_request(struct fuse_req *req)
  223. {
  224. struct fuse_conn *fc = req->fm->fc;
  225. if (refcount_dec_and_test(&req->count)) {
  226. if (test_bit(FR_BACKGROUND, &req->flags)) {
  227. /*
  228. * We get here in the unlikely case that a background
  229. * request was allocated but not sent
  230. */
  231. spin_lock(&fc->bg_lock);
  232. if (!fc->blocked)
  233. wake_up(&fc->blocked_waitq);
  234. spin_unlock(&fc->bg_lock);
  235. }
  236. if (test_bit(FR_WAITING, &req->flags)) {
  237. __clear_bit(FR_WAITING, &req->flags);
  238. fuse_drop_waiting(fc);
  239. }
  240. fuse_request_free(req);
  241. }
  242. }
  243. unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
  244. {
  245. unsigned nbytes = 0;
  246. unsigned i;
  247. for (i = 0; i < numargs; i++)
  248. nbytes += args[i].size;
  249. return nbytes;
  250. }
  251. EXPORT_SYMBOL_GPL(fuse_len_args);
  252. static u64 fuse_get_unique_locked(struct fuse_iqueue *fiq)
  253. {
  254. fiq->reqctr += FUSE_REQ_ID_STEP;
  255. return fiq->reqctr;
  256. }
  257. u64 fuse_get_unique(struct fuse_iqueue *fiq)
  258. {
  259. u64 ret;
  260. spin_lock(&fiq->lock);
  261. ret = fuse_get_unique_locked(fiq);
  262. spin_unlock(&fiq->lock);
  263. return ret;
  264. }
  265. EXPORT_SYMBOL_GPL(fuse_get_unique);
  266. unsigned int fuse_req_hash(u64 unique)
  267. {
  268. return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
  269. }
  270. EXPORT_SYMBOL_GPL(fuse_req_hash);
  271. /*
  272. * A new request is available, wake fiq->waitq
  273. */
  274. static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq)
  275. __releases(fiq->lock)
  276. {
  277. wake_up(&fiq->waitq);
  278. kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
  279. spin_unlock(&fiq->lock);
  280. }
  281. void fuse_dev_queue_forget(struct fuse_iqueue *fiq,
  282. struct fuse_forget_link *forget)
  283. {
  284. spin_lock(&fiq->lock);
  285. if (fiq->connected) {
  286. fiq->forget_list_tail->next = forget;
  287. fiq->forget_list_tail = forget;
  288. fuse_dev_wake_and_unlock(fiq);
  289. } else {
  290. kfree(forget);
  291. spin_unlock(&fiq->lock);
  292. }
  293. }
  294. void fuse_dev_queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
  295. {
  296. spin_lock(&fiq->lock);
  297. if (list_empty(&req->intr_entry)) {
  298. list_add_tail(&req->intr_entry, &fiq->interrupts);
  299. /*
  300. * Pairs with smp_mb() implied by test_and_set_bit()
  301. * from fuse_request_end().
  302. */
  303. smp_mb();
  304. if (test_bit(FR_FINISHED, &req->flags)) {
  305. list_del_init(&req->intr_entry);
  306. spin_unlock(&fiq->lock);
  307. } else {
  308. fuse_dev_wake_and_unlock(fiq);
  309. }
  310. } else {
  311. spin_unlock(&fiq->lock);
  312. }
  313. }
  314. static inline void fuse_request_assign_unique_locked(struct fuse_iqueue *fiq,
  315. struct fuse_req *req)
  316. {
  317. if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
  318. req->in.h.unique = fuse_get_unique_locked(fiq);
  319. /* tracepoint captures in.h.unique and in.h.len */
  320. trace_fuse_request_send(req);
  321. }
  322. inline void fuse_request_assign_unique(struct fuse_iqueue *fiq,
  323. struct fuse_req *req)
  324. {
  325. if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
  326. req->in.h.unique = fuse_get_unique(fiq);
  327. /* tracepoint captures in.h.unique and in.h.len */
  328. trace_fuse_request_send(req);
  329. }
  330. EXPORT_SYMBOL_GPL(fuse_request_assign_unique);
  331. static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)
  332. {
  333. spin_lock(&fiq->lock);
  334. if (fiq->connected) {
  335. fuse_request_assign_unique_locked(fiq, req);
  336. list_add_tail(&req->list, &fiq->pending);
  337. fuse_dev_wake_and_unlock(fiq);
  338. } else {
  339. spin_unlock(&fiq->lock);
  340. req->out.h.error = -ENOTCONN;
  341. clear_bit(FR_PENDING, &req->flags);
  342. fuse_request_end(req);
  343. }
  344. }
  345. const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
  346. .send_forget = fuse_dev_queue_forget,
  347. .send_interrupt = fuse_dev_queue_interrupt,
  348. .send_req = fuse_dev_queue_req,
  349. };
  350. EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops);
  351. static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req)
  352. {
  353. req->in.h.len = sizeof(struct fuse_in_header) +
  354. fuse_len_args(req->args->in_numargs,
  355. (struct fuse_arg *) req->args->in_args);
  356. fiq->ops->send_req(fiq, req);
  357. }
  358. void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
  359. u64 nodeid, u64 nlookup)
  360. {
  361. struct fuse_iqueue *fiq = &fc->iq;
  362. forget->forget_one.nodeid = nodeid;
  363. forget->forget_one.nlookup = nlookup;
  364. fiq->ops->send_forget(fiq, forget);
  365. }
  366. static void flush_bg_queue(struct fuse_conn *fc)
  367. {
  368. struct fuse_iqueue *fiq = &fc->iq;
  369. while (fc->active_background < fc->max_background &&
  370. !list_empty(&fc->bg_queue)) {
  371. struct fuse_req *req;
  372. req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
  373. list_del(&req->list);
  374. fc->active_background++;
  375. fuse_send_one(fiq, req);
  376. }
  377. }
  378. /*
  379. * This function is called when a request is finished. Either a reply
  380. * has arrived or it was aborted (and not yet sent) or some error
  381. * occurred during communication with userspace, or the device file
  382. * was closed. The requester thread is woken up (if still waiting),
  383. * the 'end' callback is called if given, else the reference to the
  384. * request is released
  385. */
  386. void fuse_request_end(struct fuse_req *req)
  387. {
  388. struct fuse_mount *fm = req->fm;
  389. struct fuse_conn *fc = fm->fc;
  390. struct fuse_iqueue *fiq = &fc->iq;
  391. if (test_and_set_bit(FR_FINISHED, &req->flags))
  392. goto put_request;
  393. trace_fuse_request_end(req);
  394. /*
  395. * test_and_set_bit() implies smp_mb() between bit
  396. * changing and below FR_INTERRUPTED check. Pairs with
  397. * smp_mb() from queue_interrupt().
  398. */
  399. if (test_bit(FR_INTERRUPTED, &req->flags)) {
  400. spin_lock(&fiq->lock);
  401. list_del_init(&req->intr_entry);
  402. spin_unlock(&fiq->lock);
  403. }
  404. WARN_ON(test_bit(FR_PENDING, &req->flags));
  405. WARN_ON(test_bit(FR_SENT, &req->flags));
  406. if (test_bit(FR_BACKGROUND, &req->flags)) {
  407. spin_lock(&fc->bg_lock);
  408. clear_bit(FR_BACKGROUND, &req->flags);
  409. if (fc->num_background == fc->max_background) {
  410. fc->blocked = 0;
  411. wake_up(&fc->blocked_waitq);
  412. } else if (!fc->blocked) {
  413. /*
  414. * Wake up next waiter, if any. It's okay to use
  415. * waitqueue_active(), as we've already synced up
  416. * fc->blocked with waiters with the wake_up() call
  417. * above.
  418. */
  419. if (waitqueue_active(&fc->blocked_waitq))
  420. wake_up(&fc->blocked_waitq);
  421. }
  422. fc->num_background--;
  423. fc->active_background--;
  424. flush_bg_queue(fc);
  425. spin_unlock(&fc->bg_lock);
  426. } else {
  427. /* Wake up waiter sleeping in request_wait_answer() */
  428. wake_up(&req->waitq);
  429. }
  430. if (test_bit(FR_ASYNC, &req->flags))
  431. req->args->end(fm, req->args, req->out.h.error);
  432. put_request:
  433. fuse_put_request(req);
  434. }
  435. EXPORT_SYMBOL_GPL(fuse_request_end);
  436. static int queue_interrupt(struct fuse_req *req)
  437. {
  438. struct fuse_iqueue *fiq = &req->fm->fc->iq;
  439. /* Check for we've sent request to interrupt this req */
  440. if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags)))
  441. return -EINVAL;
  442. fiq->ops->send_interrupt(fiq, req);
  443. return 0;
  444. }
  445. bool fuse_remove_pending_req(struct fuse_req *req, spinlock_t *lock)
  446. {
  447. spin_lock(lock);
  448. if (test_bit(FR_PENDING, &req->flags)) {
  449. /*
  450. * FR_PENDING does not get cleared as the request will end
  451. * up in destruction anyway.
  452. */
  453. list_del(&req->list);
  454. spin_unlock(lock);
  455. __fuse_put_request(req);
  456. req->out.h.error = -EINTR;
  457. return true;
  458. }
  459. spin_unlock(lock);
  460. return false;
  461. }
  462. static void request_wait_answer(struct fuse_req *req)
  463. {
  464. struct fuse_conn *fc = req->fm->fc;
  465. struct fuse_iqueue *fiq = &fc->iq;
  466. int err;
  467. if (!fc->no_interrupt) {
  468. /* Any signal may interrupt this */
  469. err = wait_event_interruptible(req->waitq,
  470. test_bit(FR_FINISHED, &req->flags));
  471. if (!err)
  472. return;
  473. set_bit(FR_INTERRUPTED, &req->flags);
  474. /* matches barrier in fuse_dev_do_read() */
  475. smp_mb__after_atomic();
  476. if (test_bit(FR_SENT, &req->flags))
  477. queue_interrupt(req);
  478. }
  479. if (!test_bit(FR_FORCE, &req->flags)) {
  480. bool removed;
  481. /* Only fatal signals may interrupt this */
  482. err = wait_event_killable(req->waitq,
  483. test_bit(FR_FINISHED, &req->flags));
  484. if (!err)
  485. return;
  486. if (test_bit(FR_URING, &req->flags))
  487. removed = fuse_uring_remove_pending_req(req);
  488. else
  489. removed = fuse_remove_pending_req(req, &fiq->lock);
  490. if (removed)
  491. return;
  492. }
  493. /*
  494. * Either request is already in userspace, or it was forced.
  495. * Wait it out.
  496. */
  497. wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
  498. }
  499. static void __fuse_request_send(struct fuse_req *req)
  500. {
  501. struct fuse_iqueue *fiq = &req->fm->fc->iq;
  502. BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
  503. /* acquire extra reference, since request is still needed after
  504. fuse_request_end() */
  505. __fuse_get_request(req);
  506. fuse_send_one(fiq, req);
  507. request_wait_answer(req);
  508. /* Pairs with smp_wmb() in fuse_request_end() */
  509. smp_rmb();
  510. }
  511. static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
  512. {
  513. if (fc->minor < 4 && args->opcode == FUSE_STATFS)
  514. args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
  515. if (fc->minor < 9) {
  516. switch (args->opcode) {
  517. case FUSE_LOOKUP:
  518. case FUSE_CREATE:
  519. case FUSE_MKNOD:
  520. case FUSE_MKDIR:
  521. case FUSE_SYMLINK:
  522. case FUSE_LINK:
  523. args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
  524. break;
  525. case FUSE_GETATTR:
  526. case FUSE_SETATTR:
  527. args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
  528. break;
  529. }
  530. }
  531. if (fc->minor < 12) {
  532. switch (args->opcode) {
  533. case FUSE_CREATE:
  534. args->in_args[0].size = sizeof(struct fuse_open_in);
  535. break;
  536. case FUSE_MKNOD:
  537. args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
  538. break;
  539. }
  540. }
  541. }
  542. static void fuse_force_creds(struct fuse_req *req)
  543. {
  544. struct fuse_conn *fc = req->fm->fc;
  545. if (!req->fm->sb || req->fm->sb->s_iflags & SB_I_NOIDMAP) {
  546. req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
  547. req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
  548. } else {
  549. req->in.h.uid = FUSE_INVALID_UIDGID;
  550. req->in.h.gid = FUSE_INVALID_UIDGID;
  551. }
  552. req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
  553. }
  554. static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
  555. {
  556. req->in.h.opcode = args->opcode;
  557. req->in.h.nodeid = args->nodeid;
  558. req->args = args;
  559. if (args->is_ext)
  560. req->in.h.total_extlen = args->in_args[args->ext_idx].size / 8;
  561. if (args->end)
  562. __set_bit(FR_ASYNC, &req->flags);
  563. }
  564. ssize_t __fuse_simple_request(struct mnt_idmap *idmap,
  565. struct fuse_mount *fm,
  566. struct fuse_args *args)
  567. {
  568. struct fuse_conn *fc = fm->fc;
  569. struct fuse_req *req;
  570. ssize_t ret;
  571. if (args->force) {
  572. atomic_inc(&fc->num_waiting);
  573. req = fuse_request_alloc(fm, GFP_KERNEL | __GFP_NOFAIL);
  574. if (!args->nocreds)
  575. fuse_force_creds(req);
  576. __set_bit(FR_WAITING, &req->flags);
  577. __set_bit(FR_FORCE, &req->flags);
  578. } else {
  579. WARN_ON(args->nocreds);
  580. req = fuse_get_req(idmap, fm, false);
  581. if (IS_ERR(req))
  582. return PTR_ERR(req);
  583. }
  584. /* Needs to be done after fuse_get_req() so that fc->minor is valid */
  585. fuse_adjust_compat(fc, args);
  586. fuse_args_to_req(req, args);
  587. if (!args->noreply)
  588. __set_bit(FR_ISREPLY, &req->flags);
  589. __fuse_request_send(req);
  590. ret = req->out.h.error;
  591. if (!ret && args->out_argvar) {
  592. BUG_ON(args->out_numargs == 0);
  593. ret = args->out_args[args->out_numargs - 1].size;
  594. }
  595. fuse_put_request(req);
  596. return ret;
  597. }
  598. #ifdef CONFIG_FUSE_IO_URING
  599. static bool fuse_request_queue_background_uring(struct fuse_conn *fc,
  600. struct fuse_req *req)
  601. {
  602. struct fuse_iqueue *fiq = &fc->iq;
  603. req->in.h.len = sizeof(struct fuse_in_header) +
  604. fuse_len_args(req->args->in_numargs,
  605. (struct fuse_arg *) req->args->in_args);
  606. fuse_request_assign_unique(fiq, req);
  607. return fuse_uring_queue_bq_req(req);
  608. }
  609. #endif
  610. /*
  611. * @return true if queued
  612. */
  613. static int fuse_request_queue_background(struct fuse_req *req)
  614. {
  615. struct fuse_mount *fm = req->fm;
  616. struct fuse_conn *fc = fm->fc;
  617. bool queued = false;
  618. WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
  619. if (!test_bit(FR_WAITING, &req->flags)) {
  620. __set_bit(FR_WAITING, &req->flags);
  621. atomic_inc(&fc->num_waiting);
  622. }
  623. __set_bit(FR_ISREPLY, &req->flags);
  624. #ifdef CONFIG_FUSE_IO_URING
  625. if (fuse_uring_ready(fc))
  626. return fuse_request_queue_background_uring(fc, req);
  627. #endif
  628. spin_lock(&fc->bg_lock);
  629. if (likely(fc->connected)) {
  630. fc->num_background++;
  631. if (fc->num_background == fc->max_background)
  632. fc->blocked = 1;
  633. list_add_tail(&req->list, &fc->bg_queue);
  634. flush_bg_queue(fc);
  635. queued = true;
  636. }
  637. spin_unlock(&fc->bg_lock);
  638. return queued;
  639. }
  640. int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
  641. gfp_t gfp_flags)
  642. {
  643. struct fuse_req *req;
  644. if (args->force) {
  645. WARN_ON(!args->nocreds);
  646. req = fuse_request_alloc(fm, gfp_flags);
  647. if (!req)
  648. return -ENOMEM;
  649. __set_bit(FR_BACKGROUND, &req->flags);
  650. } else {
  651. WARN_ON(args->nocreds);
  652. req = fuse_get_req(&invalid_mnt_idmap, fm, true);
  653. if (IS_ERR(req))
  654. return PTR_ERR(req);
  655. }
  656. fuse_args_to_req(req, args);
  657. if (!fuse_request_queue_background(req)) {
  658. fuse_put_request(req);
  659. return -ENOTCONN;
  660. }
  661. return 0;
  662. }
  663. EXPORT_SYMBOL_GPL(fuse_simple_background);
  664. static int fuse_simple_notify_reply(struct fuse_mount *fm,
  665. struct fuse_args *args, u64 unique)
  666. {
  667. struct fuse_req *req;
  668. struct fuse_iqueue *fiq = &fm->fc->iq;
  669. req = fuse_get_req(&invalid_mnt_idmap, fm, false);
  670. if (IS_ERR(req))
  671. return PTR_ERR(req);
  672. __clear_bit(FR_ISREPLY, &req->flags);
  673. req->in.h.unique = unique;
  674. fuse_args_to_req(req, args);
  675. fuse_send_one(fiq, req);
  676. return 0;
  677. }
  678. /*
  679. * Lock the request. Up to the next unlock_request() there mustn't be
  680. * anything that could cause a page-fault. If the request was already
  681. * aborted bail out.
  682. */
  683. static int lock_request(struct fuse_req *req)
  684. {
  685. int err = 0;
  686. if (req) {
  687. spin_lock(&req->waitq.lock);
  688. if (test_bit(FR_ABORTED, &req->flags))
  689. err = -ENOENT;
  690. else
  691. set_bit(FR_LOCKED, &req->flags);
  692. spin_unlock(&req->waitq.lock);
  693. }
  694. return err;
  695. }
  696. /*
  697. * Unlock request. If it was aborted while locked, caller is responsible
  698. * for unlocking and ending the request.
  699. */
  700. static int unlock_request(struct fuse_req *req)
  701. {
  702. int err = 0;
  703. if (req) {
  704. spin_lock(&req->waitq.lock);
  705. if (test_bit(FR_ABORTED, &req->flags))
  706. err = -ENOENT;
  707. else
  708. clear_bit(FR_LOCKED, &req->flags);
  709. spin_unlock(&req->waitq.lock);
  710. }
  711. return err;
  712. }
  713. void fuse_copy_init(struct fuse_copy_state *cs, bool write,
  714. struct iov_iter *iter)
  715. {
  716. memset(cs, 0, sizeof(*cs));
  717. cs->write = write;
  718. cs->iter = iter;
  719. }
  720. /* Unmap and put previous page of userspace buffer */
  721. void fuse_copy_finish(struct fuse_copy_state *cs)
  722. {
  723. if (cs->currbuf) {
  724. struct pipe_buffer *buf = cs->currbuf;
  725. if (cs->write)
  726. buf->len = PAGE_SIZE - cs->len;
  727. cs->currbuf = NULL;
  728. } else if (cs->pg) {
  729. if (cs->write) {
  730. flush_dcache_page(cs->pg);
  731. set_page_dirty_lock(cs->pg);
  732. }
  733. put_page(cs->pg);
  734. }
  735. cs->pg = NULL;
  736. }
  737. /*
  738. * Get another pagefull of userspace buffer, and map it to kernel
  739. * address space, and lock request
  740. */
  741. static int fuse_copy_fill(struct fuse_copy_state *cs)
  742. {
  743. struct page *page;
  744. int err;
  745. err = unlock_request(cs->req);
  746. if (err)
  747. return err;
  748. fuse_copy_finish(cs);
  749. if (cs->pipebufs) {
  750. struct pipe_buffer *buf = cs->pipebufs;
  751. if (!cs->write) {
  752. err = pipe_buf_confirm(cs->pipe, buf);
  753. if (err)
  754. return err;
  755. BUG_ON(!cs->nr_segs);
  756. cs->currbuf = buf;
  757. cs->pg = buf->page;
  758. cs->offset = buf->offset;
  759. cs->len = buf->len;
  760. cs->pipebufs++;
  761. cs->nr_segs--;
  762. } else {
  763. if (cs->nr_segs >= cs->pipe->max_usage)
  764. return -EIO;
  765. page = alloc_page(GFP_HIGHUSER);
  766. if (!page)
  767. return -ENOMEM;
  768. buf->page = page;
  769. buf->offset = 0;
  770. buf->len = 0;
  771. cs->currbuf = buf;
  772. cs->pg = page;
  773. cs->offset = 0;
  774. cs->len = PAGE_SIZE;
  775. cs->pipebufs++;
  776. cs->nr_segs++;
  777. }
  778. } else {
  779. size_t off;
  780. err = iov_iter_get_pages2(cs->iter, &page, PAGE_SIZE, 1, &off);
  781. if (err < 0)
  782. return err;
  783. BUG_ON(!err);
  784. cs->len = err;
  785. cs->offset = off;
  786. cs->pg = page;
  787. }
  788. return lock_request(cs->req);
  789. }
  790. /* Do as much copy to/from userspace buffer as we can */
  791. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  792. {
  793. unsigned ncpy = min(*size, cs->len);
  794. if (val) {
  795. void *pgaddr = kmap_local_page(cs->pg);
  796. void *buf = pgaddr + cs->offset;
  797. if (cs->write)
  798. memcpy(buf, *val, ncpy);
  799. else
  800. memcpy(*val, buf, ncpy);
  801. kunmap_local(pgaddr);
  802. *val += ncpy;
  803. }
  804. *size -= ncpy;
  805. cs->len -= ncpy;
  806. cs->offset += ncpy;
  807. if (cs->is_uring)
  808. cs->ring.copied_sz += ncpy;
  809. return ncpy;
  810. }
  811. static int fuse_check_folio(struct folio *folio)
  812. {
  813. if (folio_mapped(folio) ||
  814. folio->mapping != NULL ||
  815. (folio->flags.f & PAGE_FLAGS_CHECK_AT_PREP &
  816. ~(1 << PG_locked |
  817. 1 << PG_referenced |
  818. 1 << PG_lru |
  819. 1 << PG_active |
  820. 1 << PG_workingset |
  821. 1 << PG_reclaim |
  822. 1 << PG_waiters |
  823. LRU_GEN_MASK | LRU_REFS_MASK))) {
  824. dump_page(&folio->page, "fuse: trying to steal weird page");
  825. return 1;
  826. }
  827. return 0;
  828. }
  829. /*
  830. * Attempt to steal a page from the splice() pipe and move it into the
  831. * pagecache. If successful, the pointer in @pagep will be updated. The
  832. * folio that was originally in @pagep will lose a reference and the new
  833. * folio returned in @pagep will carry a reference.
  834. */
  835. static int fuse_try_move_folio(struct fuse_copy_state *cs, struct folio **foliop)
  836. {
  837. int err;
  838. struct folio *oldfolio = *foliop;
  839. struct folio *newfolio;
  840. struct pipe_buffer *buf = cs->pipebufs;
  841. folio_get(oldfolio);
  842. err = unlock_request(cs->req);
  843. if (err)
  844. goto out_put_old;
  845. fuse_copy_finish(cs);
  846. err = pipe_buf_confirm(cs->pipe, buf);
  847. if (err)
  848. goto out_put_old;
  849. BUG_ON(!cs->nr_segs);
  850. cs->currbuf = buf;
  851. cs->len = buf->len;
  852. cs->pipebufs++;
  853. cs->nr_segs--;
  854. if (cs->len != folio_size(oldfolio))
  855. goto out_fallback;
  856. if (!pipe_buf_try_steal(cs->pipe, buf))
  857. goto out_fallback;
  858. newfolio = page_folio(buf->page);
  859. folio_clear_uptodate(newfolio);
  860. folio_clear_mappedtodisk(newfolio);
  861. if (fuse_check_folio(newfolio) != 0)
  862. goto out_fallback_unlock;
  863. /*
  864. * This is a new and locked page, it shouldn't be mapped or
  865. * have any special flags on it
  866. */
  867. if (WARN_ON(folio_mapped(oldfolio)))
  868. goto out_fallback_unlock;
  869. if (WARN_ON(folio_has_private(oldfolio)))
  870. goto out_fallback_unlock;
  871. if (WARN_ON(folio_test_dirty(oldfolio) ||
  872. folio_test_writeback(oldfolio)))
  873. goto out_fallback_unlock;
  874. if (WARN_ON(folio_test_mlocked(oldfolio)))
  875. goto out_fallback_unlock;
  876. replace_page_cache_folio(oldfolio, newfolio);
  877. folio_get(newfolio);
  878. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  879. folio_add_lru(newfolio);
  880. /*
  881. * Release while we have extra ref on stolen page. Otherwise
  882. * anon_pipe_buf_release() might think the page can be reused.
  883. */
  884. pipe_buf_release(cs->pipe, buf);
  885. err = 0;
  886. spin_lock(&cs->req->waitq.lock);
  887. if (test_bit(FR_ABORTED, &cs->req->flags))
  888. err = -ENOENT;
  889. else
  890. *foliop = newfolio;
  891. spin_unlock(&cs->req->waitq.lock);
  892. if (err) {
  893. folio_unlock(newfolio);
  894. folio_put(newfolio);
  895. goto out_put_old;
  896. }
  897. folio_unlock(oldfolio);
  898. /* Drop ref for ap->pages[] array */
  899. folio_put(oldfolio);
  900. cs->len = 0;
  901. err = 0;
  902. out_put_old:
  903. /* Drop ref obtained in this function */
  904. folio_put(oldfolio);
  905. return err;
  906. out_fallback_unlock:
  907. folio_unlock(newfolio);
  908. out_fallback:
  909. cs->pg = buf->page;
  910. cs->offset = buf->offset;
  911. err = lock_request(cs->req);
  912. if (!err)
  913. err = 1;
  914. goto out_put_old;
  915. }
  916. static int fuse_ref_folio(struct fuse_copy_state *cs, struct folio *folio,
  917. unsigned offset, unsigned count)
  918. {
  919. struct pipe_buffer *buf;
  920. int err;
  921. if (cs->nr_segs >= cs->pipe->max_usage)
  922. return -EIO;
  923. folio_get(folio);
  924. err = unlock_request(cs->req);
  925. if (err) {
  926. folio_put(folio);
  927. return err;
  928. }
  929. fuse_copy_finish(cs);
  930. buf = cs->pipebufs;
  931. buf->page = &folio->page;
  932. buf->offset = offset;
  933. buf->len = count;
  934. cs->pipebufs++;
  935. cs->nr_segs++;
  936. cs->len = 0;
  937. return 0;
  938. }
  939. /*
  940. * Copy a folio in the request to/from the userspace buffer. Must be
  941. * done atomically
  942. */
  943. static int fuse_copy_folio(struct fuse_copy_state *cs, struct folio **foliop,
  944. unsigned offset, unsigned count, int zeroing)
  945. {
  946. int err;
  947. struct folio *folio = *foliop;
  948. size_t size;
  949. if (folio) {
  950. size = folio_size(folio);
  951. if (zeroing && count < size)
  952. folio_zero_range(folio, 0, size);
  953. }
  954. while (count) {
  955. if (cs->write && cs->pipebufs && folio) {
  956. /*
  957. * Can't control lifetime of pipe buffers, so always
  958. * copy user pages.
  959. */
  960. if (cs->req->args->user_pages) {
  961. err = fuse_copy_fill(cs);
  962. if (err)
  963. return err;
  964. } else {
  965. return fuse_ref_folio(cs, folio, offset, count);
  966. }
  967. } else if (!cs->len) {
  968. if (cs->move_folios && folio &&
  969. offset == 0 && count == size) {
  970. err = fuse_try_move_folio(cs, foliop);
  971. if (err <= 0)
  972. return err;
  973. } else {
  974. err = fuse_copy_fill(cs);
  975. if (err)
  976. return err;
  977. }
  978. }
  979. if (folio) {
  980. void *mapaddr = kmap_local_folio(folio, offset);
  981. void *buf = mapaddr;
  982. unsigned int copy = count;
  983. unsigned int bytes_copied;
  984. if (folio_test_highmem(folio) && count > PAGE_SIZE - offset_in_page(offset))
  985. copy = PAGE_SIZE - offset_in_page(offset);
  986. bytes_copied = fuse_copy_do(cs, &buf, &copy);
  987. kunmap_local(mapaddr);
  988. offset += bytes_copied;
  989. count -= bytes_copied;
  990. } else
  991. offset += fuse_copy_do(cs, NULL, &count);
  992. }
  993. if (folio && !cs->write)
  994. flush_dcache_folio(folio);
  995. return 0;
  996. }
  997. /* Copy folios in the request to/from userspace buffer */
  998. static int fuse_copy_folios(struct fuse_copy_state *cs, unsigned nbytes,
  999. int zeroing)
  1000. {
  1001. unsigned i;
  1002. struct fuse_req *req = cs->req;
  1003. struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
  1004. for (i = 0; i < ap->num_folios && (nbytes || zeroing); i++) {
  1005. int err;
  1006. unsigned int offset = ap->descs[i].offset;
  1007. unsigned int count = min(nbytes, ap->descs[i].length);
  1008. err = fuse_copy_folio(cs, &ap->folios[i], offset, count, zeroing);
  1009. if (err)
  1010. return err;
  1011. nbytes -= count;
  1012. }
  1013. return 0;
  1014. }
  1015. /* Copy a single argument in the request to/from userspace buffer */
  1016. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  1017. {
  1018. while (size) {
  1019. if (!cs->len) {
  1020. int err = fuse_copy_fill(cs);
  1021. if (err)
  1022. return err;
  1023. }
  1024. fuse_copy_do(cs, &val, &size);
  1025. }
  1026. return 0;
  1027. }
  1028. /* Copy request arguments to/from userspace buffer */
  1029. int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  1030. unsigned argpages, struct fuse_arg *args,
  1031. int zeroing)
  1032. {
  1033. int err = 0;
  1034. unsigned i;
  1035. for (i = 0; !err && i < numargs; i++) {
  1036. struct fuse_arg *arg = &args[i];
  1037. if (i == numargs - 1 && argpages)
  1038. err = fuse_copy_folios(cs, arg->size, zeroing);
  1039. else
  1040. err = fuse_copy_one(cs, arg->value, arg->size);
  1041. }
  1042. return err;
  1043. }
  1044. static int forget_pending(struct fuse_iqueue *fiq)
  1045. {
  1046. return fiq->forget_list_head.next != NULL;
  1047. }
  1048. static int request_pending(struct fuse_iqueue *fiq)
  1049. {
  1050. return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
  1051. forget_pending(fiq);
  1052. }
  1053. /*
  1054. * Transfer an interrupt request to userspace
  1055. *
  1056. * Unlike other requests this is assembled on demand, without a need
  1057. * to allocate a separate fuse_req structure.
  1058. *
  1059. * Called with fiq->lock held, releases it
  1060. */
  1061. static int fuse_read_interrupt(struct fuse_iqueue *fiq,
  1062. struct fuse_copy_state *cs,
  1063. size_t nbytes, struct fuse_req *req)
  1064. __releases(fiq->lock)
  1065. {
  1066. struct fuse_in_header ih;
  1067. struct fuse_interrupt_in arg;
  1068. unsigned reqsize = sizeof(ih) + sizeof(arg);
  1069. int err;
  1070. list_del_init(&req->intr_entry);
  1071. memset(&ih, 0, sizeof(ih));
  1072. memset(&arg, 0, sizeof(arg));
  1073. ih.len = reqsize;
  1074. ih.opcode = FUSE_INTERRUPT;
  1075. ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
  1076. arg.unique = req->in.h.unique;
  1077. spin_unlock(&fiq->lock);
  1078. if (nbytes < reqsize)
  1079. return -EINVAL;
  1080. err = fuse_copy_one(cs, &ih, sizeof(ih));
  1081. if (!err)
  1082. err = fuse_copy_one(cs, &arg, sizeof(arg));
  1083. fuse_copy_finish(cs);
  1084. return err ? err : reqsize;
  1085. }
  1086. static struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
  1087. unsigned int max,
  1088. unsigned int *countp)
  1089. {
  1090. struct fuse_forget_link *head = fiq->forget_list_head.next;
  1091. struct fuse_forget_link **newhead = &head;
  1092. unsigned count;
  1093. for (count = 0; *newhead != NULL && count < max; count++)
  1094. newhead = &(*newhead)->next;
  1095. fiq->forget_list_head.next = *newhead;
  1096. *newhead = NULL;
  1097. if (fiq->forget_list_head.next == NULL)
  1098. fiq->forget_list_tail = &fiq->forget_list_head;
  1099. if (countp != NULL)
  1100. *countp = count;
  1101. return head;
  1102. }
  1103. static int fuse_read_single_forget(struct fuse_iqueue *fiq,
  1104. struct fuse_copy_state *cs,
  1105. size_t nbytes)
  1106. __releases(fiq->lock)
  1107. {
  1108. int err;
  1109. struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL);
  1110. struct fuse_forget_in arg = {
  1111. .nlookup = forget->forget_one.nlookup,
  1112. };
  1113. struct fuse_in_header ih = {
  1114. .opcode = FUSE_FORGET,
  1115. .nodeid = forget->forget_one.nodeid,
  1116. .unique = fuse_get_unique_locked(fiq),
  1117. .len = sizeof(ih) + sizeof(arg),
  1118. };
  1119. spin_unlock(&fiq->lock);
  1120. kfree(forget);
  1121. if (nbytes < ih.len)
  1122. return -EINVAL;
  1123. err = fuse_copy_one(cs, &ih, sizeof(ih));
  1124. if (!err)
  1125. err = fuse_copy_one(cs, &arg, sizeof(arg));
  1126. fuse_copy_finish(cs);
  1127. if (err)
  1128. return err;
  1129. return ih.len;
  1130. }
  1131. static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
  1132. struct fuse_copy_state *cs, size_t nbytes)
  1133. __releases(fiq->lock)
  1134. {
  1135. int err;
  1136. unsigned max_forgets;
  1137. unsigned count;
  1138. struct fuse_forget_link *head;
  1139. struct fuse_batch_forget_in arg = { .count = 0 };
  1140. struct fuse_in_header ih = {
  1141. .opcode = FUSE_BATCH_FORGET,
  1142. .unique = fuse_get_unique_locked(fiq),
  1143. .len = sizeof(ih) + sizeof(arg),
  1144. };
  1145. if (nbytes < ih.len) {
  1146. spin_unlock(&fiq->lock);
  1147. return -EINVAL;
  1148. }
  1149. max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
  1150. head = fuse_dequeue_forget(fiq, max_forgets, &count);
  1151. spin_unlock(&fiq->lock);
  1152. arg.count = count;
  1153. ih.len += count * sizeof(struct fuse_forget_one);
  1154. err = fuse_copy_one(cs, &ih, sizeof(ih));
  1155. if (!err)
  1156. err = fuse_copy_one(cs, &arg, sizeof(arg));
  1157. while (head) {
  1158. struct fuse_forget_link *forget = head;
  1159. if (!err) {
  1160. err = fuse_copy_one(cs, &forget->forget_one,
  1161. sizeof(forget->forget_one));
  1162. }
  1163. head = forget->next;
  1164. kfree(forget);
  1165. }
  1166. fuse_copy_finish(cs);
  1167. if (err)
  1168. return err;
  1169. return ih.len;
  1170. }
  1171. static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
  1172. struct fuse_copy_state *cs,
  1173. size_t nbytes)
  1174. __releases(fiq->lock)
  1175. {
  1176. if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
  1177. return fuse_read_single_forget(fiq, cs, nbytes);
  1178. else
  1179. return fuse_read_batch_forget(fiq, cs, nbytes);
  1180. }
  1181. /*
  1182. * Read a single request into the userspace filesystem's buffer. This
  1183. * function waits until a request is available, then removes it from
  1184. * the pending list and copies request data to userspace buffer. If
  1185. * no reply is needed (FORGET) or request has been aborted or there
  1186. * was an error during the copying then it's finished by calling
  1187. * fuse_request_end(). Otherwise add it to the processing list, and set
  1188. * the 'sent' flag.
  1189. */
  1190. static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
  1191. struct fuse_copy_state *cs, size_t nbytes)
  1192. {
  1193. ssize_t err;
  1194. struct fuse_conn *fc = fud->fc;
  1195. struct fuse_iqueue *fiq = &fc->iq;
  1196. struct fuse_pqueue *fpq = &fud->pq;
  1197. struct fuse_req *req;
  1198. struct fuse_args *args;
  1199. unsigned reqsize;
  1200. unsigned int hash;
  1201. /*
  1202. * Require sane minimum read buffer - that has capacity for fixed part
  1203. * of any request header + negotiated max_write room for data.
  1204. *
  1205. * Historically libfuse reserves 4K for fixed header room, but e.g.
  1206. * GlusterFS reserves only 80 bytes
  1207. *
  1208. * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
  1209. *
  1210. * which is the absolute minimum any sane filesystem should be using
  1211. * for header room.
  1212. */
  1213. if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
  1214. sizeof(struct fuse_in_header) +
  1215. sizeof(struct fuse_write_in) +
  1216. fc->max_write))
  1217. return -EINVAL;
  1218. restart:
  1219. for (;;) {
  1220. spin_lock(&fiq->lock);
  1221. if (!fiq->connected || request_pending(fiq))
  1222. break;
  1223. spin_unlock(&fiq->lock);
  1224. if (file->f_flags & O_NONBLOCK)
  1225. return -EAGAIN;
  1226. err = wait_event_interruptible_exclusive(fiq->waitq,
  1227. !fiq->connected || request_pending(fiq));
  1228. if (err)
  1229. return err;
  1230. }
  1231. if (!fiq->connected) {
  1232. err = fc->aborted ? -ECONNABORTED : -ENODEV;
  1233. goto err_unlock;
  1234. }
  1235. if (!list_empty(&fiq->interrupts)) {
  1236. req = list_entry(fiq->interrupts.next, struct fuse_req,
  1237. intr_entry);
  1238. return fuse_read_interrupt(fiq, cs, nbytes, req);
  1239. }
  1240. if (forget_pending(fiq)) {
  1241. if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
  1242. return fuse_read_forget(fc, fiq, cs, nbytes);
  1243. if (fiq->forget_batch <= -8)
  1244. fiq->forget_batch = 16;
  1245. }
  1246. req = list_entry(fiq->pending.next, struct fuse_req, list);
  1247. clear_bit(FR_PENDING, &req->flags);
  1248. list_del_init(&req->list);
  1249. spin_unlock(&fiq->lock);
  1250. args = req->args;
  1251. reqsize = req->in.h.len;
  1252. /* If request is too large, reply with an error and restart the read */
  1253. if (nbytes < reqsize) {
  1254. req->out.h.error = -EIO;
  1255. /* SETXATTR is special, since it may contain too large data */
  1256. if (args->opcode == FUSE_SETXATTR)
  1257. req->out.h.error = -E2BIG;
  1258. fuse_request_end(req);
  1259. goto restart;
  1260. }
  1261. spin_lock(&fpq->lock);
  1262. /*
  1263. * Must not put request on fpq->io queue after having been shut down by
  1264. * fuse_abort_conn()
  1265. */
  1266. if (!fpq->connected) {
  1267. req->out.h.error = err = -ECONNABORTED;
  1268. goto out_end;
  1269. }
  1270. list_add(&req->list, &fpq->io);
  1271. spin_unlock(&fpq->lock);
  1272. cs->req = req;
  1273. err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
  1274. if (!err)
  1275. err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
  1276. (struct fuse_arg *) args->in_args, 0);
  1277. fuse_copy_finish(cs);
  1278. spin_lock(&fpq->lock);
  1279. clear_bit(FR_LOCKED, &req->flags);
  1280. if (!fpq->connected) {
  1281. err = fc->aborted ? -ECONNABORTED : -ENODEV;
  1282. goto out_end;
  1283. }
  1284. if (err) {
  1285. req->out.h.error = -EIO;
  1286. goto out_end;
  1287. }
  1288. if (!test_bit(FR_ISREPLY, &req->flags)) {
  1289. err = reqsize;
  1290. goto out_end;
  1291. }
  1292. hash = fuse_req_hash(req->in.h.unique);
  1293. list_move_tail(&req->list, &fpq->processing[hash]);
  1294. __fuse_get_request(req);
  1295. set_bit(FR_SENT, &req->flags);
  1296. spin_unlock(&fpq->lock);
  1297. /* matches barrier in request_wait_answer() */
  1298. smp_mb__after_atomic();
  1299. if (test_bit(FR_INTERRUPTED, &req->flags))
  1300. queue_interrupt(req);
  1301. fuse_put_request(req);
  1302. return reqsize;
  1303. out_end:
  1304. if (!test_bit(FR_PRIVATE, &req->flags))
  1305. list_del_init(&req->list);
  1306. spin_unlock(&fpq->lock);
  1307. fuse_request_end(req);
  1308. return err;
  1309. err_unlock:
  1310. spin_unlock(&fiq->lock);
  1311. return err;
  1312. }
  1313. static int fuse_dev_open(struct inode *inode, struct file *file)
  1314. {
  1315. /*
  1316. * The fuse device's file's private_data is used to hold
  1317. * the fuse_conn(ection) when it is mounted, and is used to
  1318. * keep track of whether the file has been mounted already.
  1319. */
  1320. file->private_data = NULL;
  1321. return 0;
  1322. }
  1323. struct fuse_dev *fuse_get_dev(struct file *file)
  1324. {
  1325. struct fuse_dev *fud = __fuse_get_dev(file);
  1326. int err;
  1327. if (likely(fud))
  1328. return fud;
  1329. err = wait_event_interruptible(fuse_dev_waitq,
  1330. READ_ONCE(file->private_data) != FUSE_DEV_SYNC_INIT);
  1331. if (err)
  1332. return ERR_PTR(err);
  1333. fud = __fuse_get_dev(file);
  1334. if (!fud)
  1335. return ERR_PTR(-EPERM);
  1336. return fud;
  1337. }
  1338. static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
  1339. {
  1340. struct fuse_copy_state cs;
  1341. struct file *file = iocb->ki_filp;
  1342. struct fuse_dev *fud = fuse_get_dev(file);
  1343. if (IS_ERR(fud))
  1344. return PTR_ERR(fud);
  1345. if (!user_backed_iter(to))
  1346. return -EINVAL;
  1347. fuse_copy_init(&cs, true, to);
  1348. return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
  1349. }
  1350. static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
  1351. struct pipe_inode_info *pipe,
  1352. size_t len, unsigned int flags)
  1353. {
  1354. int total, ret;
  1355. int page_nr = 0;
  1356. struct pipe_buffer *bufs;
  1357. struct fuse_copy_state cs;
  1358. struct fuse_dev *fud = fuse_get_dev(in);
  1359. if (IS_ERR(fud))
  1360. return PTR_ERR(fud);
  1361. bufs = kvmalloc_objs(struct pipe_buffer, pipe->max_usage);
  1362. if (!bufs)
  1363. return -ENOMEM;
  1364. fuse_copy_init(&cs, true, NULL);
  1365. cs.pipebufs = bufs;
  1366. cs.pipe = pipe;
  1367. ret = fuse_dev_do_read(fud, in, &cs, len);
  1368. if (ret < 0)
  1369. goto out;
  1370. if (pipe_buf_usage(pipe) + cs.nr_segs > pipe->max_usage) {
  1371. ret = -EIO;
  1372. goto out;
  1373. }
  1374. for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
  1375. /*
  1376. * Need to be careful about this. Having buf->ops in module
  1377. * code can Oops if the buffer persists after module unload.
  1378. */
  1379. bufs[page_nr].ops = &nosteal_pipe_buf_ops;
  1380. bufs[page_nr].flags = 0;
  1381. ret = add_to_pipe(pipe, &bufs[page_nr++]);
  1382. if (unlikely(ret < 0))
  1383. break;
  1384. }
  1385. if (total)
  1386. ret = total;
  1387. out:
  1388. for (; page_nr < cs.nr_segs; page_nr++)
  1389. put_page(bufs[page_nr].page);
  1390. kvfree(bufs);
  1391. return ret;
  1392. }
  1393. static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
  1394. struct fuse_copy_state *cs)
  1395. {
  1396. struct fuse_notify_poll_wakeup_out outarg;
  1397. int err;
  1398. if (size != sizeof(outarg))
  1399. return -EINVAL;
  1400. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1401. if (err)
  1402. return err;
  1403. fuse_copy_finish(cs);
  1404. return fuse_notify_poll_wakeup(fc, &outarg);
  1405. }
  1406. static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
  1407. struct fuse_copy_state *cs)
  1408. {
  1409. struct fuse_notify_inval_inode_out outarg;
  1410. int err;
  1411. if (size != sizeof(outarg))
  1412. return -EINVAL;
  1413. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1414. if (err)
  1415. return err;
  1416. fuse_copy_finish(cs);
  1417. down_read(&fc->killsb);
  1418. err = fuse_reverse_inval_inode(fc, outarg.ino,
  1419. outarg.off, outarg.len);
  1420. up_read(&fc->killsb);
  1421. return err;
  1422. }
  1423. static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
  1424. struct fuse_copy_state *cs)
  1425. {
  1426. struct fuse_notify_inval_entry_out outarg;
  1427. int err;
  1428. char *buf;
  1429. struct qstr name;
  1430. if (size < sizeof(outarg))
  1431. return -EINVAL;
  1432. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1433. if (err)
  1434. return err;
  1435. if (outarg.namelen > fc->name_max)
  1436. return -ENAMETOOLONG;
  1437. err = -EINVAL;
  1438. if (size != sizeof(outarg) + outarg.namelen + 1)
  1439. return -EINVAL;
  1440. buf = kzalloc(outarg.namelen + 1, GFP_KERNEL);
  1441. if (!buf)
  1442. return -ENOMEM;
  1443. name.name = buf;
  1444. name.len = outarg.namelen;
  1445. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1446. if (err)
  1447. goto err;
  1448. fuse_copy_finish(cs);
  1449. buf[outarg.namelen] = 0;
  1450. down_read(&fc->killsb);
  1451. err = fuse_reverse_inval_entry(fc, outarg.parent, 0, &name, outarg.flags);
  1452. up_read(&fc->killsb);
  1453. err:
  1454. kfree(buf);
  1455. return err;
  1456. }
  1457. static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
  1458. struct fuse_copy_state *cs)
  1459. {
  1460. struct fuse_notify_delete_out outarg;
  1461. int err;
  1462. char *buf;
  1463. struct qstr name;
  1464. if (size < sizeof(outarg))
  1465. return -EINVAL;
  1466. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1467. if (err)
  1468. return err;
  1469. if (outarg.namelen > fc->name_max)
  1470. return -ENAMETOOLONG;
  1471. if (size != sizeof(outarg) + outarg.namelen + 1)
  1472. return -EINVAL;
  1473. buf = kzalloc(outarg.namelen + 1, GFP_KERNEL);
  1474. if (!buf)
  1475. return -ENOMEM;
  1476. name.name = buf;
  1477. name.len = outarg.namelen;
  1478. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1479. if (err)
  1480. goto err;
  1481. fuse_copy_finish(cs);
  1482. buf[outarg.namelen] = 0;
  1483. down_read(&fc->killsb);
  1484. err = fuse_reverse_inval_entry(fc, outarg.parent, outarg.child, &name, 0);
  1485. up_read(&fc->killsb);
  1486. err:
  1487. kfree(buf);
  1488. return err;
  1489. }
  1490. static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
  1491. struct fuse_copy_state *cs)
  1492. {
  1493. struct fuse_notify_store_out outarg;
  1494. struct inode *inode;
  1495. struct address_space *mapping;
  1496. u64 nodeid;
  1497. int err;
  1498. pgoff_t index;
  1499. unsigned int offset;
  1500. unsigned int num;
  1501. loff_t file_size;
  1502. loff_t end;
  1503. if (size < sizeof(outarg))
  1504. return -EINVAL;
  1505. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1506. if (err)
  1507. return err;
  1508. if (size - sizeof(outarg) != outarg.size)
  1509. return -EINVAL;
  1510. nodeid = outarg.nodeid;
  1511. down_read(&fc->killsb);
  1512. err = -ENOENT;
  1513. inode = fuse_ilookup(fc, nodeid, NULL);
  1514. if (!inode)
  1515. goto out_up_killsb;
  1516. mapping = inode->i_mapping;
  1517. index = outarg.offset >> PAGE_SHIFT;
  1518. offset = outarg.offset & ~PAGE_MASK;
  1519. file_size = i_size_read(inode);
  1520. end = outarg.offset + outarg.size;
  1521. if (end > file_size) {
  1522. file_size = end;
  1523. fuse_write_update_attr(inode, file_size, outarg.size);
  1524. }
  1525. num = outarg.size;
  1526. while (num) {
  1527. struct folio *folio;
  1528. unsigned int folio_offset;
  1529. unsigned int nr_bytes;
  1530. unsigned int nr_pages;
  1531. folio = filemap_grab_folio(mapping, index);
  1532. err = PTR_ERR(folio);
  1533. if (IS_ERR(folio))
  1534. goto out_iput;
  1535. folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
  1536. nr_bytes = min(num, folio_size(folio) - folio_offset);
  1537. nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1538. err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
  1539. if (!folio_test_uptodate(folio) && !err && offset == 0 &&
  1540. (nr_bytes == folio_size(folio) || file_size == end)) {
  1541. folio_zero_segment(folio, nr_bytes, folio_size(folio));
  1542. folio_mark_uptodate(folio);
  1543. }
  1544. folio_unlock(folio);
  1545. folio_put(folio);
  1546. if (err)
  1547. goto out_iput;
  1548. num -= nr_bytes;
  1549. offset = 0;
  1550. index += nr_pages;
  1551. }
  1552. err = 0;
  1553. out_iput:
  1554. iput(inode);
  1555. out_up_killsb:
  1556. up_read(&fc->killsb);
  1557. return err;
  1558. }
  1559. struct fuse_retrieve_args {
  1560. struct fuse_args_pages ap;
  1561. struct fuse_notify_retrieve_in inarg;
  1562. };
  1563. static void fuse_retrieve_end(struct fuse_mount *fm, struct fuse_args *args,
  1564. int error)
  1565. {
  1566. struct fuse_retrieve_args *ra =
  1567. container_of(args, typeof(*ra), ap.args);
  1568. release_pages(ra->ap.folios, ra->ap.num_folios);
  1569. kfree(ra);
  1570. }
  1571. static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
  1572. struct fuse_notify_retrieve_out *outarg)
  1573. {
  1574. int err;
  1575. struct address_space *mapping = inode->i_mapping;
  1576. pgoff_t index;
  1577. loff_t file_size;
  1578. unsigned int num;
  1579. unsigned int offset;
  1580. size_t total_len = 0;
  1581. unsigned int num_pages;
  1582. struct fuse_conn *fc = fm->fc;
  1583. struct fuse_retrieve_args *ra;
  1584. size_t args_size = sizeof(*ra);
  1585. struct fuse_args_pages *ap;
  1586. struct fuse_args *args;
  1587. offset = outarg->offset & ~PAGE_MASK;
  1588. file_size = i_size_read(inode);
  1589. num = min(outarg->size, fc->max_write);
  1590. if (outarg->offset > file_size)
  1591. num = 0;
  1592. else if (outarg->offset + num > file_size)
  1593. num = file_size - outarg->offset;
  1594. num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1595. num_pages = min(num_pages, fc->max_pages);
  1596. num = min(num, num_pages << PAGE_SHIFT);
  1597. args_size += num_pages * (sizeof(ap->folios[0]) + sizeof(ap->descs[0]));
  1598. ra = kzalloc(args_size, GFP_KERNEL);
  1599. if (!ra)
  1600. return -ENOMEM;
  1601. ap = &ra->ap;
  1602. ap->folios = (void *) (ra + 1);
  1603. ap->descs = (void *) (ap->folios + num_pages);
  1604. args = &ap->args;
  1605. args->nodeid = outarg->nodeid;
  1606. args->opcode = FUSE_NOTIFY_REPLY;
  1607. args->in_numargs = 3;
  1608. args->in_pages = true;
  1609. args->end = fuse_retrieve_end;
  1610. index = outarg->offset >> PAGE_SHIFT;
  1611. while (num && ap->num_folios < num_pages) {
  1612. struct folio *folio;
  1613. unsigned int folio_offset;
  1614. unsigned int nr_bytes;
  1615. unsigned int nr_pages;
  1616. folio = filemap_get_folio(mapping, index);
  1617. if (IS_ERR(folio))
  1618. break;
  1619. folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
  1620. nr_bytes = min(folio_size(folio) - folio_offset, num);
  1621. nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1622. ap->folios[ap->num_folios] = folio;
  1623. ap->descs[ap->num_folios].offset = folio_offset;
  1624. ap->descs[ap->num_folios].length = nr_bytes;
  1625. ap->num_folios++;
  1626. offset = 0;
  1627. num -= nr_bytes;
  1628. total_len += nr_bytes;
  1629. index += nr_pages;
  1630. }
  1631. ra->inarg.offset = outarg->offset;
  1632. ra->inarg.size = total_len;
  1633. fuse_set_zero_arg0(args);
  1634. args->in_args[1].size = sizeof(ra->inarg);
  1635. args->in_args[1].value = &ra->inarg;
  1636. args->in_args[2].size = total_len;
  1637. err = fuse_simple_notify_reply(fm, args, outarg->notify_unique);
  1638. if (err)
  1639. fuse_retrieve_end(fm, args, err);
  1640. return err;
  1641. }
  1642. static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
  1643. struct fuse_copy_state *cs)
  1644. {
  1645. struct fuse_notify_retrieve_out outarg;
  1646. struct fuse_mount *fm;
  1647. struct inode *inode;
  1648. u64 nodeid;
  1649. int err;
  1650. if (size != sizeof(outarg))
  1651. return -EINVAL;
  1652. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1653. if (err)
  1654. return err;
  1655. fuse_copy_finish(cs);
  1656. down_read(&fc->killsb);
  1657. err = -ENOENT;
  1658. nodeid = outarg.nodeid;
  1659. inode = fuse_ilookup(fc, nodeid, &fm);
  1660. if (inode) {
  1661. err = fuse_retrieve(fm, inode, &outarg);
  1662. iput(inode);
  1663. }
  1664. up_read(&fc->killsb);
  1665. return err;
  1666. }
  1667. /*
  1668. * Resending all processing queue requests.
  1669. *
  1670. * During a FUSE daemon panics and failover, it is possible for some inflight
  1671. * requests to be lost and never returned. As a result, applications awaiting
  1672. * replies would become stuck forever. To address this, we can use notification
  1673. * to trigger resending of these pending requests to the FUSE daemon, ensuring
  1674. * they are properly processed again.
  1675. *
  1676. * Please note that this strategy is applicable only to idempotent requests or
  1677. * if the FUSE daemon takes careful measures to avoid processing duplicated
  1678. * non-idempotent requests.
  1679. */
  1680. static void fuse_resend(struct fuse_conn *fc)
  1681. {
  1682. struct fuse_dev *fud;
  1683. struct fuse_req *req, *next;
  1684. struct fuse_iqueue *fiq = &fc->iq;
  1685. LIST_HEAD(to_queue);
  1686. unsigned int i;
  1687. spin_lock(&fc->lock);
  1688. if (!fc->connected) {
  1689. spin_unlock(&fc->lock);
  1690. return;
  1691. }
  1692. list_for_each_entry(fud, &fc->devices, entry) {
  1693. struct fuse_pqueue *fpq = &fud->pq;
  1694. spin_lock(&fpq->lock);
  1695. for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
  1696. list_splice_tail_init(&fpq->processing[i], &to_queue);
  1697. spin_unlock(&fpq->lock);
  1698. }
  1699. spin_unlock(&fc->lock);
  1700. list_for_each_entry_safe(req, next, &to_queue, list) {
  1701. set_bit(FR_PENDING, &req->flags);
  1702. clear_bit(FR_SENT, &req->flags);
  1703. /* mark the request as resend request */
  1704. req->in.h.unique |= FUSE_UNIQUE_RESEND;
  1705. }
  1706. spin_lock(&fiq->lock);
  1707. if (!fiq->connected) {
  1708. spin_unlock(&fiq->lock);
  1709. list_for_each_entry(req, &to_queue, list)
  1710. clear_bit(FR_PENDING, &req->flags);
  1711. fuse_dev_end_requests(&to_queue);
  1712. return;
  1713. }
  1714. /* iq and pq requests are both oldest to newest */
  1715. list_splice(&to_queue, &fiq->pending);
  1716. fuse_dev_wake_and_unlock(fiq);
  1717. }
  1718. static int fuse_notify_resend(struct fuse_conn *fc)
  1719. {
  1720. fuse_resend(fc);
  1721. return 0;
  1722. }
  1723. /*
  1724. * Increments the fuse connection epoch. This will result of dentries from
  1725. * previous epochs to be invalidated. Additionally, if inval_wq is set, a work
  1726. * queue is scheduled to trigger the invalidation.
  1727. */
  1728. static int fuse_notify_inc_epoch(struct fuse_conn *fc)
  1729. {
  1730. atomic_inc(&fc->epoch);
  1731. if (inval_wq)
  1732. schedule_work(&fc->epoch_work);
  1733. return 0;
  1734. }
  1735. static int fuse_notify_prune(struct fuse_conn *fc, unsigned int size,
  1736. struct fuse_copy_state *cs)
  1737. {
  1738. struct fuse_notify_prune_out outarg;
  1739. const unsigned int batch = 512;
  1740. u64 *nodeids __free(kfree) = kmalloc(sizeof(u64) * batch, GFP_KERNEL);
  1741. unsigned int num, i;
  1742. int err;
  1743. if (!nodeids)
  1744. return -ENOMEM;
  1745. if (size < sizeof(outarg))
  1746. return -EINVAL;
  1747. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1748. if (err)
  1749. return err;
  1750. if (size - sizeof(outarg) != outarg.count * sizeof(u64))
  1751. return -EINVAL;
  1752. for (; outarg.count; outarg.count -= num) {
  1753. num = min(batch, outarg.count);
  1754. err = fuse_copy_one(cs, nodeids, num * sizeof(u64));
  1755. if (err)
  1756. return err;
  1757. scoped_guard(rwsem_read, &fc->killsb) {
  1758. for (i = 0; i < num; i++)
  1759. fuse_try_prune_one_inode(fc, nodeids[i]);
  1760. }
  1761. }
  1762. return 0;
  1763. }
  1764. static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
  1765. unsigned int size, struct fuse_copy_state *cs)
  1766. {
  1767. /* Don't try to move folios (yet) */
  1768. cs->move_folios = false;
  1769. switch (code) {
  1770. case FUSE_NOTIFY_POLL:
  1771. return fuse_notify_poll(fc, size, cs);
  1772. case FUSE_NOTIFY_INVAL_INODE:
  1773. return fuse_notify_inval_inode(fc, size, cs);
  1774. case FUSE_NOTIFY_INVAL_ENTRY:
  1775. return fuse_notify_inval_entry(fc, size, cs);
  1776. case FUSE_NOTIFY_STORE:
  1777. return fuse_notify_store(fc, size, cs);
  1778. case FUSE_NOTIFY_RETRIEVE:
  1779. return fuse_notify_retrieve(fc, size, cs);
  1780. case FUSE_NOTIFY_DELETE:
  1781. return fuse_notify_delete(fc, size, cs);
  1782. case FUSE_NOTIFY_RESEND:
  1783. return fuse_notify_resend(fc);
  1784. case FUSE_NOTIFY_INC_EPOCH:
  1785. return fuse_notify_inc_epoch(fc);
  1786. case FUSE_NOTIFY_PRUNE:
  1787. return fuse_notify_prune(fc, size, cs);
  1788. default:
  1789. return -EINVAL;
  1790. }
  1791. }
  1792. /* Look up request on processing list by unique ID */
  1793. struct fuse_req *fuse_request_find(struct fuse_pqueue *fpq, u64 unique)
  1794. {
  1795. unsigned int hash = fuse_req_hash(unique);
  1796. struct fuse_req *req;
  1797. list_for_each_entry(req, &fpq->processing[hash], list) {
  1798. if (req->in.h.unique == unique)
  1799. return req;
  1800. }
  1801. return NULL;
  1802. }
  1803. int fuse_copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
  1804. unsigned nbytes)
  1805. {
  1806. unsigned int reqsize = 0;
  1807. /*
  1808. * Uring has all headers separated from args - args is payload only
  1809. */
  1810. if (!cs->is_uring)
  1811. reqsize = sizeof(struct fuse_out_header);
  1812. reqsize += fuse_len_args(args->out_numargs, args->out_args);
  1813. if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar))
  1814. return -EINVAL;
  1815. else if (reqsize > nbytes) {
  1816. struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1];
  1817. unsigned diffsize = reqsize - nbytes;
  1818. if (diffsize > lastarg->size)
  1819. return -EINVAL;
  1820. lastarg->size -= diffsize;
  1821. }
  1822. return fuse_copy_args(cs, args->out_numargs, args->out_pages,
  1823. args->out_args, args->page_zeroing);
  1824. }
  1825. /*
  1826. * Write a single reply to a request. First the header is copied from
  1827. * the write buffer. The request is then searched on the processing
  1828. * list by the unique ID found in the header. If found, then remove
  1829. * it from the list and copy the rest of the buffer to the request.
  1830. * The request is finished by calling fuse_request_end().
  1831. */
  1832. static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
  1833. struct fuse_copy_state *cs, size_t nbytes)
  1834. {
  1835. int err;
  1836. struct fuse_conn *fc = fud->fc;
  1837. struct fuse_pqueue *fpq = &fud->pq;
  1838. struct fuse_req *req;
  1839. struct fuse_out_header oh;
  1840. err = -EINVAL;
  1841. if (nbytes < sizeof(struct fuse_out_header))
  1842. goto out;
  1843. err = fuse_copy_one(cs, &oh, sizeof(oh));
  1844. if (err)
  1845. goto copy_finish;
  1846. err = -EINVAL;
  1847. if (oh.len != nbytes)
  1848. goto copy_finish;
  1849. /*
  1850. * Zero oh.unique indicates unsolicited notification message
  1851. * and error contains notification code.
  1852. */
  1853. if (!oh.unique) {
  1854. err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
  1855. goto copy_finish;
  1856. }
  1857. err = -EINVAL;
  1858. if (oh.error <= -512 || oh.error > 0)
  1859. goto copy_finish;
  1860. spin_lock(&fpq->lock);
  1861. req = NULL;
  1862. if (fpq->connected)
  1863. req = fuse_request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
  1864. err = -ENOENT;
  1865. if (!req) {
  1866. spin_unlock(&fpq->lock);
  1867. goto copy_finish;
  1868. }
  1869. /* Is it an interrupt reply ID? */
  1870. if (oh.unique & FUSE_INT_REQ_BIT) {
  1871. __fuse_get_request(req);
  1872. spin_unlock(&fpq->lock);
  1873. err = 0;
  1874. if (nbytes != sizeof(struct fuse_out_header))
  1875. err = -EINVAL;
  1876. else if (oh.error == -ENOSYS)
  1877. fc->no_interrupt = 1;
  1878. else if (oh.error == -EAGAIN)
  1879. err = queue_interrupt(req);
  1880. fuse_put_request(req);
  1881. goto copy_finish;
  1882. }
  1883. clear_bit(FR_SENT, &req->flags);
  1884. list_move(&req->list, &fpq->io);
  1885. req->out.h = oh;
  1886. set_bit(FR_LOCKED, &req->flags);
  1887. spin_unlock(&fpq->lock);
  1888. cs->req = req;
  1889. if (!req->args->page_replace)
  1890. cs->move_folios = false;
  1891. if (oh.error)
  1892. err = nbytes != sizeof(oh) ? -EINVAL : 0;
  1893. else
  1894. err = fuse_copy_out_args(cs, req->args, nbytes);
  1895. fuse_copy_finish(cs);
  1896. spin_lock(&fpq->lock);
  1897. clear_bit(FR_LOCKED, &req->flags);
  1898. if (!fpq->connected)
  1899. err = -ENOENT;
  1900. else if (err)
  1901. req->out.h.error = -EIO;
  1902. if (!test_bit(FR_PRIVATE, &req->flags))
  1903. list_del_init(&req->list);
  1904. spin_unlock(&fpq->lock);
  1905. fuse_request_end(req);
  1906. out:
  1907. return err ? err : nbytes;
  1908. copy_finish:
  1909. fuse_copy_finish(cs);
  1910. goto out;
  1911. }
  1912. static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
  1913. {
  1914. struct fuse_copy_state cs;
  1915. struct fuse_dev *fud = __fuse_get_dev(iocb->ki_filp);
  1916. if (!fud)
  1917. return -EPERM;
  1918. if (!user_backed_iter(from))
  1919. return -EINVAL;
  1920. fuse_copy_init(&cs, false, from);
  1921. return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
  1922. }
  1923. static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
  1924. struct file *out, loff_t *ppos,
  1925. size_t len, unsigned int flags)
  1926. {
  1927. unsigned int head, tail, count;
  1928. unsigned nbuf;
  1929. unsigned idx;
  1930. struct pipe_buffer *bufs;
  1931. struct fuse_copy_state cs;
  1932. struct fuse_dev *fud = __fuse_get_dev(out);
  1933. size_t rem;
  1934. ssize_t ret;
  1935. if (!fud)
  1936. return -EPERM;
  1937. pipe_lock(pipe);
  1938. head = pipe->head;
  1939. tail = pipe->tail;
  1940. count = pipe_occupancy(head, tail);
  1941. bufs = kvmalloc_objs(struct pipe_buffer, count);
  1942. if (!bufs) {
  1943. pipe_unlock(pipe);
  1944. return -ENOMEM;
  1945. }
  1946. nbuf = 0;
  1947. rem = 0;
  1948. for (idx = tail; !pipe_empty(head, idx) && rem < len; idx++)
  1949. rem += pipe_buf(pipe, idx)->len;
  1950. ret = -EINVAL;
  1951. if (rem < len)
  1952. goto out_free;
  1953. rem = len;
  1954. while (rem) {
  1955. struct pipe_buffer *ibuf;
  1956. struct pipe_buffer *obuf;
  1957. if (WARN_ON(nbuf >= count || pipe_empty(head, tail)))
  1958. goto out_free;
  1959. ibuf = pipe_buf(pipe, tail);
  1960. obuf = &bufs[nbuf];
  1961. if (rem >= ibuf->len) {
  1962. *obuf = *ibuf;
  1963. ibuf->ops = NULL;
  1964. tail++;
  1965. pipe->tail = tail;
  1966. } else {
  1967. if (!pipe_buf_get(pipe, ibuf))
  1968. goto out_free;
  1969. *obuf = *ibuf;
  1970. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1971. obuf->len = rem;
  1972. ibuf->offset += obuf->len;
  1973. ibuf->len -= obuf->len;
  1974. }
  1975. nbuf++;
  1976. rem -= obuf->len;
  1977. }
  1978. pipe_unlock(pipe);
  1979. fuse_copy_init(&cs, false, NULL);
  1980. cs.pipebufs = bufs;
  1981. cs.nr_segs = nbuf;
  1982. cs.pipe = pipe;
  1983. if (flags & SPLICE_F_MOVE)
  1984. cs.move_folios = true;
  1985. ret = fuse_dev_do_write(fud, &cs, len);
  1986. pipe_lock(pipe);
  1987. out_free:
  1988. for (idx = 0; idx < nbuf; idx++) {
  1989. struct pipe_buffer *buf = &bufs[idx];
  1990. if (buf->ops)
  1991. pipe_buf_release(pipe, buf);
  1992. }
  1993. pipe_unlock(pipe);
  1994. kvfree(bufs);
  1995. return ret;
  1996. }
  1997. static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
  1998. {
  1999. __poll_t mask = EPOLLOUT | EPOLLWRNORM;
  2000. struct fuse_iqueue *fiq;
  2001. struct fuse_dev *fud = fuse_get_dev(file);
  2002. if (IS_ERR(fud))
  2003. return EPOLLERR;
  2004. fiq = &fud->fc->iq;
  2005. poll_wait(file, &fiq->waitq, wait);
  2006. spin_lock(&fiq->lock);
  2007. if (!fiq->connected)
  2008. mask = EPOLLERR;
  2009. else if (request_pending(fiq))
  2010. mask |= EPOLLIN | EPOLLRDNORM;
  2011. spin_unlock(&fiq->lock);
  2012. return mask;
  2013. }
  2014. /* Abort all requests on the given list (pending or processing) */
  2015. void fuse_dev_end_requests(struct list_head *head)
  2016. {
  2017. while (!list_empty(head)) {
  2018. struct fuse_req *req;
  2019. req = list_entry(head->next, struct fuse_req, list);
  2020. req->out.h.error = -ECONNABORTED;
  2021. clear_bit(FR_SENT, &req->flags);
  2022. list_del_init(&req->list);
  2023. fuse_request_end(req);
  2024. }
  2025. }
  2026. static void end_polls(struct fuse_conn *fc)
  2027. {
  2028. struct rb_node *p;
  2029. p = rb_first(&fc->polled_files);
  2030. while (p) {
  2031. struct fuse_file *ff;
  2032. ff = rb_entry(p, struct fuse_file, polled_node);
  2033. wake_up_interruptible_all(&ff->poll_wait);
  2034. p = rb_next(p);
  2035. }
  2036. }
  2037. /*
  2038. * Abort all requests.
  2039. *
  2040. * Emergency exit in case of a malicious or accidental deadlock, or just a hung
  2041. * filesystem.
  2042. *
  2043. * The same effect is usually achievable through killing the filesystem daemon
  2044. * and all users of the filesystem. The exception is the combination of an
  2045. * asynchronous request and the tricky deadlock (see
  2046. * Documentation/filesystems/fuse/fuse.rst).
  2047. *
  2048. * Aborting requests under I/O goes as follows: 1: Separate out unlocked
  2049. * requests, they should be finished off immediately. Locked requests will be
  2050. * finished after unlock; see unlock_request(). 2: Finish off the unlocked
  2051. * requests. It is possible that some request will finish before we can. This
  2052. * is OK, the request will in that case be removed from the list before we touch
  2053. * it.
  2054. */
  2055. void fuse_abort_conn(struct fuse_conn *fc)
  2056. {
  2057. struct fuse_iqueue *fiq = &fc->iq;
  2058. spin_lock(&fc->lock);
  2059. if (fc->connected) {
  2060. struct fuse_dev *fud;
  2061. struct fuse_req *req, *next;
  2062. LIST_HEAD(to_end);
  2063. unsigned int i;
  2064. if (fc->timeout.req_timeout)
  2065. cancel_delayed_work(&fc->timeout.work);
  2066. /* Background queuing checks fc->connected under bg_lock */
  2067. spin_lock(&fc->bg_lock);
  2068. fc->connected = 0;
  2069. spin_unlock(&fc->bg_lock);
  2070. fuse_set_initialized(fc);
  2071. list_for_each_entry(fud, &fc->devices, entry) {
  2072. struct fuse_pqueue *fpq = &fud->pq;
  2073. spin_lock(&fpq->lock);
  2074. fpq->connected = 0;
  2075. list_for_each_entry_safe(req, next, &fpq->io, list) {
  2076. req->out.h.error = -ECONNABORTED;
  2077. spin_lock(&req->waitq.lock);
  2078. set_bit(FR_ABORTED, &req->flags);
  2079. if (!test_bit(FR_LOCKED, &req->flags)) {
  2080. set_bit(FR_PRIVATE, &req->flags);
  2081. __fuse_get_request(req);
  2082. list_move(&req->list, &to_end);
  2083. }
  2084. spin_unlock(&req->waitq.lock);
  2085. }
  2086. for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
  2087. list_splice_tail_init(&fpq->processing[i],
  2088. &to_end);
  2089. spin_unlock(&fpq->lock);
  2090. }
  2091. spin_lock(&fc->bg_lock);
  2092. fc->blocked = 0;
  2093. fc->max_background = UINT_MAX;
  2094. flush_bg_queue(fc);
  2095. spin_unlock(&fc->bg_lock);
  2096. spin_lock(&fiq->lock);
  2097. fiq->connected = 0;
  2098. list_for_each_entry(req, &fiq->pending, list)
  2099. clear_bit(FR_PENDING, &req->flags);
  2100. list_splice_tail_init(&fiq->pending, &to_end);
  2101. while (forget_pending(fiq))
  2102. kfree(fuse_dequeue_forget(fiq, 1, NULL));
  2103. wake_up_all(&fiq->waitq);
  2104. spin_unlock(&fiq->lock);
  2105. kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
  2106. end_polls(fc);
  2107. wake_up_all(&fc->blocked_waitq);
  2108. spin_unlock(&fc->lock);
  2109. fuse_dev_end_requests(&to_end);
  2110. /*
  2111. * fc->lock must not be taken to avoid conflicts with io-uring
  2112. * locks
  2113. */
  2114. fuse_uring_abort(fc);
  2115. } else {
  2116. spin_unlock(&fc->lock);
  2117. }
  2118. }
  2119. EXPORT_SYMBOL_GPL(fuse_abort_conn);
  2120. void fuse_wait_aborted(struct fuse_conn *fc)
  2121. {
  2122. /* matches implicit memory barrier in fuse_drop_waiting() */
  2123. smp_mb();
  2124. wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
  2125. fuse_uring_wait_stopped_queues(fc);
  2126. }
  2127. int fuse_dev_release(struct inode *inode, struct file *file)
  2128. {
  2129. struct fuse_dev *fud = __fuse_get_dev(file);
  2130. if (fud) {
  2131. struct fuse_conn *fc = fud->fc;
  2132. struct fuse_pqueue *fpq = &fud->pq;
  2133. LIST_HEAD(to_end);
  2134. unsigned int i;
  2135. spin_lock(&fpq->lock);
  2136. WARN_ON(!list_empty(&fpq->io));
  2137. for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
  2138. list_splice_init(&fpq->processing[i], &to_end);
  2139. spin_unlock(&fpq->lock);
  2140. fuse_dev_end_requests(&to_end);
  2141. /* Are we the last open device? */
  2142. if (atomic_dec_and_test(&fc->dev_count)) {
  2143. WARN_ON(fc->iq.fasync != NULL);
  2144. fuse_abort_conn(fc);
  2145. }
  2146. fuse_dev_free(fud);
  2147. }
  2148. return 0;
  2149. }
  2150. EXPORT_SYMBOL_GPL(fuse_dev_release);
  2151. static int fuse_dev_fasync(int fd, struct file *file, int on)
  2152. {
  2153. struct fuse_dev *fud = fuse_get_dev(file);
  2154. if (IS_ERR(fud))
  2155. return PTR_ERR(fud);
  2156. /* No locking - fasync_helper does its own locking */
  2157. return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
  2158. }
  2159. static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
  2160. {
  2161. struct fuse_dev *fud;
  2162. if (__fuse_get_dev(new))
  2163. return -EINVAL;
  2164. fud = fuse_dev_alloc_install(fc);
  2165. if (!fud)
  2166. return -ENOMEM;
  2167. new->private_data = fud;
  2168. atomic_inc(&fc->dev_count);
  2169. return 0;
  2170. }
  2171. static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
  2172. {
  2173. int res;
  2174. int oldfd;
  2175. struct fuse_dev *fud = NULL;
  2176. if (get_user(oldfd, argp))
  2177. return -EFAULT;
  2178. CLASS(fd, f)(oldfd);
  2179. if (fd_empty(f))
  2180. return -EINVAL;
  2181. /*
  2182. * Check against file->f_op because CUSE
  2183. * uses the same ioctl handler.
  2184. */
  2185. if (fd_file(f)->f_op == file->f_op)
  2186. fud = __fuse_get_dev(fd_file(f));
  2187. res = -EINVAL;
  2188. if (fud) {
  2189. mutex_lock(&fuse_mutex);
  2190. res = fuse_device_clone(fud->fc, file);
  2191. mutex_unlock(&fuse_mutex);
  2192. }
  2193. return res;
  2194. }
  2195. static long fuse_dev_ioctl_backing_open(struct file *file,
  2196. struct fuse_backing_map __user *argp)
  2197. {
  2198. struct fuse_dev *fud = fuse_get_dev(file);
  2199. struct fuse_backing_map map;
  2200. if (IS_ERR(fud))
  2201. return PTR_ERR(fud);
  2202. if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
  2203. return -EOPNOTSUPP;
  2204. if (copy_from_user(&map, argp, sizeof(map)))
  2205. return -EFAULT;
  2206. return fuse_backing_open(fud->fc, &map);
  2207. }
  2208. static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp)
  2209. {
  2210. struct fuse_dev *fud = fuse_get_dev(file);
  2211. int backing_id;
  2212. if (IS_ERR(fud))
  2213. return PTR_ERR(fud);
  2214. if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
  2215. return -EOPNOTSUPP;
  2216. if (get_user(backing_id, argp))
  2217. return -EFAULT;
  2218. return fuse_backing_close(fud->fc, backing_id);
  2219. }
  2220. static long fuse_dev_ioctl_sync_init(struct file *file)
  2221. {
  2222. int err = -EINVAL;
  2223. mutex_lock(&fuse_mutex);
  2224. if (!__fuse_get_dev(file)) {
  2225. WRITE_ONCE(file->private_data, FUSE_DEV_SYNC_INIT);
  2226. err = 0;
  2227. }
  2228. mutex_unlock(&fuse_mutex);
  2229. return err;
  2230. }
  2231. static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
  2232. unsigned long arg)
  2233. {
  2234. void __user *argp = (void __user *)arg;
  2235. switch (cmd) {
  2236. case FUSE_DEV_IOC_CLONE:
  2237. return fuse_dev_ioctl_clone(file, argp);
  2238. case FUSE_DEV_IOC_BACKING_OPEN:
  2239. return fuse_dev_ioctl_backing_open(file, argp);
  2240. case FUSE_DEV_IOC_BACKING_CLOSE:
  2241. return fuse_dev_ioctl_backing_close(file, argp);
  2242. case FUSE_DEV_IOC_SYNC_INIT:
  2243. return fuse_dev_ioctl_sync_init(file);
  2244. default:
  2245. return -ENOTTY;
  2246. }
  2247. }
  2248. #ifdef CONFIG_PROC_FS
  2249. static void fuse_dev_show_fdinfo(struct seq_file *seq, struct file *file)
  2250. {
  2251. struct fuse_dev *fud = __fuse_get_dev(file);
  2252. if (!fud)
  2253. return;
  2254. seq_printf(seq, "fuse_connection:\t%u\n", fud->fc->dev);
  2255. }
  2256. #endif
  2257. const struct file_operations fuse_dev_operations = {
  2258. .owner = THIS_MODULE,
  2259. .open = fuse_dev_open,
  2260. .read_iter = fuse_dev_read,
  2261. .splice_read = fuse_dev_splice_read,
  2262. .write_iter = fuse_dev_write,
  2263. .splice_write = fuse_dev_splice_write,
  2264. .poll = fuse_dev_poll,
  2265. .release = fuse_dev_release,
  2266. .fasync = fuse_dev_fasync,
  2267. .unlocked_ioctl = fuse_dev_ioctl,
  2268. .compat_ioctl = compat_ptr_ioctl,
  2269. #ifdef CONFIG_FUSE_IO_URING
  2270. .uring_cmd = fuse_uring_cmd,
  2271. #endif
  2272. #ifdef CONFIG_PROC_FS
  2273. .show_fdinfo = fuse_dev_show_fdinfo,
  2274. #endif
  2275. };
  2276. EXPORT_SYMBOL_GPL(fuse_dev_operations);
  2277. static struct miscdevice fuse_miscdevice = {
  2278. .minor = FUSE_MINOR,
  2279. .name = "fuse",
  2280. .fops = &fuse_dev_operations,
  2281. };
  2282. int __init fuse_dev_init(void)
  2283. {
  2284. int err = -ENOMEM;
  2285. fuse_req_cachep = kmem_cache_create("fuse_request",
  2286. sizeof(struct fuse_req),
  2287. 0, 0, NULL);
  2288. if (!fuse_req_cachep)
  2289. goto out;
  2290. err = misc_register(&fuse_miscdevice);
  2291. if (err)
  2292. goto out_cache_clean;
  2293. return 0;
  2294. out_cache_clean:
  2295. kmem_cache_destroy(fuse_req_cachep);
  2296. out:
  2297. return err;
  2298. }
  2299. void fuse_dev_cleanup(void)
  2300. {
  2301. misc_deregister(&fuse_miscdevice);
  2302. kmem_cache_destroy(fuse_req_cachep);
  2303. }