dev.c 56 KB

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