file.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216
  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/pagemap.h>
  9. #include <linux/slab.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/module.h>
  14. #include <linux/swap.h>
  15. #include <linux/falloc.h>
  16. #include <linux/uio.h>
  17. #include <linux/fs.h>
  18. #include <linux/filelock.h>
  19. #include <linux/splice.h>
  20. #include <linux/task_io_accounting_ops.h>
  21. #include <linux/iomap.h>
  22. static int fuse_send_open(struct fuse_mount *fm, u64 nodeid,
  23. unsigned int open_flags, int opcode,
  24. struct fuse_open_out *outargp)
  25. {
  26. struct fuse_open_in inarg;
  27. FUSE_ARGS(args);
  28. memset(&inarg, 0, sizeof(inarg));
  29. inarg.flags = open_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
  30. if (!fm->fc->atomic_o_trunc)
  31. inarg.flags &= ~O_TRUNC;
  32. if (fm->fc->handle_killpriv_v2 &&
  33. (inarg.flags & O_TRUNC) && !capable(CAP_FSETID)) {
  34. inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
  35. }
  36. args.opcode = opcode;
  37. args.nodeid = nodeid;
  38. args.in_numargs = 1;
  39. args.in_args[0].size = sizeof(inarg);
  40. args.in_args[0].value = &inarg;
  41. args.out_numargs = 1;
  42. args.out_args[0].size = sizeof(*outargp);
  43. args.out_args[0].value = outargp;
  44. return fuse_simple_request(fm, &args);
  45. }
  46. struct fuse_file *fuse_file_alloc(struct fuse_mount *fm, bool release)
  47. {
  48. struct fuse_file *ff;
  49. ff = kzalloc_obj(struct fuse_file, GFP_KERNEL_ACCOUNT);
  50. if (unlikely(!ff))
  51. return NULL;
  52. ff->fm = fm;
  53. if (release) {
  54. ff->args = kzalloc_obj(*ff->args, GFP_KERNEL_ACCOUNT);
  55. if (!ff->args) {
  56. kfree(ff);
  57. return NULL;
  58. }
  59. }
  60. INIT_LIST_HEAD(&ff->write_entry);
  61. refcount_set(&ff->count, 1);
  62. RB_CLEAR_NODE(&ff->polled_node);
  63. init_waitqueue_head(&ff->poll_wait);
  64. ff->kh = atomic64_inc_return(&fm->fc->khctr);
  65. return ff;
  66. }
  67. void fuse_file_free(struct fuse_file *ff)
  68. {
  69. kfree(ff->args);
  70. kfree(ff);
  71. }
  72. static struct fuse_file *fuse_file_get(struct fuse_file *ff)
  73. {
  74. refcount_inc(&ff->count);
  75. return ff;
  76. }
  77. static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
  78. int error)
  79. {
  80. struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
  81. iput(ra->inode);
  82. kfree(ra);
  83. }
  84. static void fuse_file_put(struct fuse_file *ff, bool sync)
  85. {
  86. if (refcount_dec_and_test(&ff->count)) {
  87. struct fuse_release_args *ra = &ff->args->release_args;
  88. struct fuse_args *args = (ra ? &ra->args : NULL);
  89. if (ra && ra->inode)
  90. fuse_file_io_release(ff, ra->inode);
  91. if (!args) {
  92. /* Do nothing when server does not implement 'opendir' */
  93. } else if (args->opcode == FUSE_RELEASE && ff->fm->fc->no_open) {
  94. fuse_release_end(ff->fm, args, 0);
  95. } else if (sync) {
  96. fuse_simple_request(ff->fm, args);
  97. fuse_release_end(ff->fm, args, 0);
  98. } else {
  99. args->end = fuse_release_end;
  100. if (fuse_simple_background(ff->fm, args,
  101. GFP_KERNEL | __GFP_NOFAIL))
  102. fuse_release_end(ff->fm, args, -ENOTCONN);
  103. }
  104. kfree(ff);
  105. }
  106. }
  107. struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
  108. unsigned int open_flags, bool isdir)
  109. {
  110. struct fuse_conn *fc = fm->fc;
  111. struct fuse_file *ff;
  112. int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
  113. bool open = isdir ? !fc->no_opendir : !fc->no_open;
  114. bool release = !isdir || open;
  115. /*
  116. * ff->args->release_args still needs to be allocated (so we can hold an
  117. * inode reference while there are pending inflight file operations when
  118. * ->release() is called, see fuse_prepare_release()) even if
  119. * fc->no_open is set else it becomes possible for reclaim to deadlock
  120. * if while servicing the readahead request the server triggers reclaim
  121. * and reclaim evicts the inode of the file being read ahead.
  122. */
  123. ff = fuse_file_alloc(fm, release);
  124. if (!ff)
  125. return ERR_PTR(-ENOMEM);
  126. ff->fh = 0;
  127. /* Default for no-open */
  128. ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
  129. if (open) {
  130. /* Store outarg for fuse_finish_open() */
  131. struct fuse_open_out *outargp = &ff->args->open_outarg;
  132. int err;
  133. err = fuse_send_open(fm, nodeid, open_flags, opcode, outargp);
  134. if (!err) {
  135. ff->fh = outargp->fh;
  136. ff->open_flags = outargp->open_flags;
  137. } else if (err != -ENOSYS) {
  138. fuse_file_free(ff);
  139. return ERR_PTR(err);
  140. } else {
  141. if (isdir) {
  142. /* No release needed */
  143. kfree(ff->args);
  144. ff->args = NULL;
  145. fc->no_opendir = 1;
  146. } else {
  147. fc->no_open = 1;
  148. }
  149. }
  150. }
  151. if (isdir)
  152. ff->open_flags &= ~FOPEN_DIRECT_IO;
  153. ff->nodeid = nodeid;
  154. return ff;
  155. }
  156. int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
  157. bool isdir)
  158. {
  159. struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir);
  160. if (!IS_ERR(ff))
  161. file->private_data = ff;
  162. return PTR_ERR_OR_ZERO(ff);
  163. }
  164. EXPORT_SYMBOL_GPL(fuse_do_open);
  165. static void fuse_link_write_file(struct file *file)
  166. {
  167. struct inode *inode = file_inode(file);
  168. struct fuse_inode *fi = get_fuse_inode(inode);
  169. struct fuse_file *ff = file->private_data;
  170. /*
  171. * file may be written through mmap, so chain it onto the
  172. * inodes's write_file list
  173. */
  174. spin_lock(&fi->lock);
  175. if (list_empty(&ff->write_entry))
  176. list_add(&ff->write_entry, &fi->write_files);
  177. spin_unlock(&fi->lock);
  178. }
  179. int fuse_finish_open(struct inode *inode, struct file *file)
  180. {
  181. struct fuse_file *ff = file->private_data;
  182. struct fuse_conn *fc = get_fuse_conn(inode);
  183. int err;
  184. err = fuse_file_io_open(file, inode);
  185. if (err)
  186. return err;
  187. if (ff->open_flags & FOPEN_STREAM)
  188. stream_open(inode, file);
  189. else if (ff->open_flags & FOPEN_NONSEEKABLE)
  190. nonseekable_open(inode, file);
  191. if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
  192. fuse_link_write_file(file);
  193. return 0;
  194. }
  195. static void fuse_truncate_update_attr(struct inode *inode, struct file *file)
  196. {
  197. struct fuse_conn *fc = get_fuse_conn(inode);
  198. struct fuse_inode *fi = get_fuse_inode(inode);
  199. spin_lock(&fi->lock);
  200. fi->attr_version = atomic64_inc_return(&fc->attr_version);
  201. i_size_write(inode, 0);
  202. spin_unlock(&fi->lock);
  203. file_update_time(file);
  204. fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
  205. }
  206. static int fuse_open(struct inode *inode, struct file *file)
  207. {
  208. struct fuse_mount *fm = get_fuse_mount(inode);
  209. struct fuse_inode *fi = get_fuse_inode(inode);
  210. struct fuse_conn *fc = fm->fc;
  211. struct fuse_file *ff;
  212. int err;
  213. bool is_truncate = (file->f_flags & O_TRUNC) && fc->atomic_o_trunc;
  214. bool is_wb_truncate = is_truncate && fc->writeback_cache;
  215. bool dax_truncate = is_truncate && FUSE_IS_DAX(inode);
  216. if (fuse_is_bad(inode))
  217. return -EIO;
  218. err = generic_file_open(inode, file);
  219. if (err)
  220. return err;
  221. if (is_wb_truncate || dax_truncate)
  222. inode_lock(inode);
  223. if (dax_truncate) {
  224. filemap_invalidate_lock(inode->i_mapping);
  225. err = fuse_dax_break_layouts(inode, 0, -1);
  226. if (err)
  227. goto out_inode_unlock;
  228. }
  229. if (is_wb_truncate || dax_truncate)
  230. fuse_set_nowrite(inode);
  231. err = fuse_do_open(fm, get_node_id(inode), file, false);
  232. if (!err) {
  233. ff = file->private_data;
  234. err = fuse_finish_open(inode, file);
  235. if (err)
  236. fuse_sync_release(fi, ff, file->f_flags);
  237. else if (is_truncate)
  238. fuse_truncate_update_attr(inode, file);
  239. }
  240. if (is_wb_truncate || dax_truncate)
  241. fuse_release_nowrite(inode);
  242. if (!err) {
  243. if (is_truncate)
  244. truncate_pagecache(inode, 0);
  245. else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
  246. invalidate_inode_pages2(inode->i_mapping);
  247. }
  248. if (dax_truncate)
  249. filemap_invalidate_unlock(inode->i_mapping);
  250. out_inode_unlock:
  251. if (is_wb_truncate || dax_truncate)
  252. inode_unlock(inode);
  253. return err;
  254. }
  255. static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
  256. unsigned int flags, int opcode, bool sync)
  257. {
  258. struct fuse_conn *fc = ff->fm->fc;
  259. struct fuse_release_args *ra = &ff->args->release_args;
  260. if (fuse_file_passthrough(ff))
  261. fuse_passthrough_release(ff, fuse_inode_backing(fi));
  262. /* Inode is NULL on error path of fuse_create_open() */
  263. if (likely(fi)) {
  264. spin_lock(&fi->lock);
  265. list_del(&ff->write_entry);
  266. spin_unlock(&fi->lock);
  267. }
  268. spin_lock(&fc->lock);
  269. if (!RB_EMPTY_NODE(&ff->polled_node))
  270. rb_erase(&ff->polled_node, &fc->polled_files);
  271. spin_unlock(&fc->lock);
  272. wake_up_interruptible_all(&ff->poll_wait);
  273. if (!ra)
  274. return;
  275. /* ff->args was used for open outarg */
  276. memset(ff->args, 0, sizeof(*ff->args));
  277. ra->inarg.fh = ff->fh;
  278. ra->inarg.flags = flags;
  279. ra->args.in_numargs = 1;
  280. ra->args.in_args[0].size = sizeof(struct fuse_release_in);
  281. ra->args.in_args[0].value = &ra->inarg;
  282. ra->args.opcode = opcode;
  283. ra->args.nodeid = ff->nodeid;
  284. ra->args.force = true;
  285. ra->args.nocreds = true;
  286. /*
  287. * Hold inode until release is finished.
  288. * From fuse_sync_release() the refcount is 1 and everything's
  289. * synchronous, so we are fine with not doing igrab() here.
  290. */
  291. ra->inode = sync ? NULL : igrab(&fi->inode);
  292. }
  293. void fuse_file_release(struct inode *inode, struct fuse_file *ff,
  294. unsigned int open_flags, fl_owner_t id, bool isdir)
  295. {
  296. struct fuse_inode *fi = get_fuse_inode(inode);
  297. struct fuse_release_args *ra = &ff->args->release_args;
  298. int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
  299. fuse_prepare_release(fi, ff, open_flags, opcode, false);
  300. if (ra && ff->flock) {
  301. ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
  302. ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id);
  303. }
  304. /*
  305. * Normally this will send the RELEASE request, however if
  306. * some asynchronous READ or WRITE requests are outstanding,
  307. * the sending will be delayed.
  308. *
  309. * Make the release synchronous if this is a fuseblk mount,
  310. * synchronous RELEASE is allowed (and desirable) in this case
  311. * because the server can be trusted not to screw up.
  312. *
  313. * Always use the asynchronous file put because the current thread
  314. * might be the fuse server. This can happen if a process starts some
  315. * aio and closes the fd before the aio completes. Since aio takes its
  316. * own ref to the file, the IO completion has to drop the ref, which is
  317. * how the fuse server can end up closing its clients' files.
  318. */
  319. fuse_file_put(ff, false);
  320. }
  321. void fuse_release_common(struct file *file, bool isdir)
  322. {
  323. fuse_file_release(file_inode(file), file->private_data, file->f_flags,
  324. (fl_owner_t) file, isdir);
  325. }
  326. static int fuse_release(struct inode *inode, struct file *file)
  327. {
  328. struct fuse_conn *fc = get_fuse_conn(inode);
  329. /*
  330. * Dirty pages might remain despite write_inode_now() call from
  331. * fuse_flush() due to writes racing with the close.
  332. */
  333. if (fc->writeback_cache)
  334. write_inode_now(inode, 1);
  335. fuse_release_common(file, false);
  336. /* return value is ignored by VFS */
  337. return 0;
  338. }
  339. void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
  340. unsigned int flags)
  341. {
  342. WARN_ON(refcount_read(&ff->count) > 1);
  343. fuse_prepare_release(fi, ff, flags, FUSE_RELEASE, true);
  344. fuse_file_put(ff, true);
  345. }
  346. EXPORT_SYMBOL_GPL(fuse_sync_release);
  347. /*
  348. * Scramble the ID space with XTEA, so that the value of the files_struct
  349. * pointer is not exposed to userspace.
  350. */
  351. u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
  352. {
  353. u32 *k = fc->scramble_key;
  354. u64 v = (unsigned long) id;
  355. u32 v0 = v;
  356. u32 v1 = v >> 32;
  357. u32 sum = 0;
  358. int i;
  359. for (i = 0; i < 32; i++) {
  360. v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
  361. sum += 0x9E3779B9;
  362. v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
  363. }
  364. return (u64) v0 + ((u64) v1 << 32);
  365. }
  366. struct fuse_writepage_args {
  367. struct fuse_io_args ia;
  368. struct list_head queue_entry;
  369. struct inode *inode;
  370. struct fuse_sync_bucket *bucket;
  371. };
  372. /*
  373. * Wait for all pending writepages on the inode to finish.
  374. *
  375. * This is currently done by blocking further writes with FUSE_NOWRITE
  376. * and waiting for all sent writes to complete.
  377. *
  378. * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
  379. * could conflict with truncation.
  380. */
  381. static void fuse_sync_writes(struct inode *inode)
  382. {
  383. fuse_set_nowrite(inode);
  384. fuse_release_nowrite(inode);
  385. }
  386. static int fuse_flush(struct file *file, fl_owner_t id)
  387. {
  388. struct inode *inode = file_inode(file);
  389. struct fuse_mount *fm = get_fuse_mount(inode);
  390. struct fuse_file *ff = file->private_data;
  391. struct fuse_flush_in inarg;
  392. FUSE_ARGS(args);
  393. int err;
  394. if (fuse_is_bad(inode))
  395. return -EIO;
  396. if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
  397. return 0;
  398. err = write_inode_now(inode, 1);
  399. if (err)
  400. return err;
  401. err = filemap_check_errors(file->f_mapping);
  402. if (err)
  403. return err;
  404. err = 0;
  405. if (fm->fc->no_flush)
  406. goto inval_attr_out;
  407. memset(&inarg, 0, sizeof(inarg));
  408. inarg.fh = ff->fh;
  409. inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
  410. args.opcode = FUSE_FLUSH;
  411. args.nodeid = get_node_id(inode);
  412. args.in_numargs = 1;
  413. args.in_args[0].size = sizeof(inarg);
  414. args.in_args[0].value = &inarg;
  415. args.force = true;
  416. err = fuse_simple_request(fm, &args);
  417. if (err == -ENOSYS) {
  418. fm->fc->no_flush = 1;
  419. err = 0;
  420. }
  421. inval_attr_out:
  422. /*
  423. * In memory i_blocks is not maintained by fuse, if writeback cache is
  424. * enabled, i_blocks from cached attr may not be accurate.
  425. */
  426. if (!err && fm->fc->writeback_cache)
  427. fuse_invalidate_attr_mask(inode, STATX_BLOCKS);
  428. return err;
  429. }
  430. int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
  431. int datasync, int opcode)
  432. {
  433. struct inode *inode = file->f_mapping->host;
  434. struct fuse_mount *fm = get_fuse_mount(inode);
  435. struct fuse_file *ff = file->private_data;
  436. FUSE_ARGS(args);
  437. struct fuse_fsync_in inarg;
  438. memset(&inarg, 0, sizeof(inarg));
  439. inarg.fh = ff->fh;
  440. inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
  441. args.opcode = opcode;
  442. args.nodeid = get_node_id(inode);
  443. args.in_numargs = 1;
  444. args.in_args[0].size = sizeof(inarg);
  445. args.in_args[0].value = &inarg;
  446. return fuse_simple_request(fm, &args);
  447. }
  448. static int fuse_fsync(struct file *file, loff_t start, loff_t end,
  449. int datasync)
  450. {
  451. struct inode *inode = file->f_mapping->host;
  452. struct fuse_conn *fc = get_fuse_conn(inode);
  453. int err;
  454. if (fuse_is_bad(inode))
  455. return -EIO;
  456. inode_lock(inode);
  457. /*
  458. * Start writeback against all dirty pages of the inode, then
  459. * wait for all outstanding writes, before sending the FSYNC
  460. * request.
  461. */
  462. err = file_write_and_wait_range(file, start, end);
  463. if (err)
  464. goto out;
  465. fuse_sync_writes(inode);
  466. /*
  467. * Due to implementation of fuse writeback
  468. * file_write_and_wait_range() does not catch errors.
  469. * We have to do this directly after fuse_sync_writes()
  470. */
  471. err = file_check_and_advance_wb_err(file);
  472. if (err)
  473. goto out;
  474. err = sync_inode_metadata(inode, 1);
  475. if (err)
  476. goto out;
  477. if (fc->no_fsync)
  478. goto out;
  479. err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
  480. if (err == -ENOSYS) {
  481. fc->no_fsync = 1;
  482. err = 0;
  483. }
  484. out:
  485. inode_unlock(inode);
  486. return err;
  487. }
  488. void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
  489. size_t count, int opcode)
  490. {
  491. struct fuse_file *ff = file->private_data;
  492. struct fuse_args *args = &ia->ap.args;
  493. ia->read.in.fh = ff->fh;
  494. ia->read.in.offset = pos;
  495. ia->read.in.size = count;
  496. ia->read.in.flags = file->f_flags;
  497. args->opcode = opcode;
  498. args->nodeid = ff->nodeid;
  499. args->in_numargs = 1;
  500. args->in_args[0].size = sizeof(ia->read.in);
  501. args->in_args[0].value = &ia->read.in;
  502. args->out_argvar = true;
  503. args->out_numargs = 1;
  504. args->out_args[0].size = count;
  505. }
  506. static void fuse_release_user_pages(struct fuse_args_pages *ap, ssize_t nres,
  507. bool should_dirty)
  508. {
  509. unsigned int i;
  510. for (i = 0; i < ap->num_folios; i++) {
  511. if (should_dirty)
  512. folio_mark_dirty_lock(ap->folios[i]);
  513. if (ap->args.is_pinned)
  514. unpin_folio(ap->folios[i]);
  515. }
  516. if (nres > 0 && ap->args.invalidate_vmap)
  517. invalidate_kernel_vmap_range(ap->args.vmap_base, nres);
  518. }
  519. static void fuse_io_release(struct kref *kref)
  520. {
  521. kfree(container_of(kref, struct fuse_io_priv, refcnt));
  522. }
  523. static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
  524. {
  525. if (io->err)
  526. return io->err;
  527. if (io->bytes >= 0 && io->write)
  528. return -EIO;
  529. return io->bytes < 0 ? io->size : io->bytes;
  530. }
  531. /*
  532. * In case of short read, the caller sets 'pos' to the position of
  533. * actual end of fuse request in IO request. Otherwise, if bytes_requested
  534. * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
  535. *
  536. * An example:
  537. * User requested DIO read of 64K. It was split into two 32K fuse requests,
  538. * both submitted asynchronously. The first of them was ACKed by userspace as
  539. * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
  540. * second request was ACKed as short, e.g. only 1K was read, resulting in
  541. * pos == 33K.
  542. *
  543. * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
  544. * will be equal to the length of the longest contiguous fragment of
  545. * transferred data starting from the beginning of IO request.
  546. */
  547. static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
  548. {
  549. int left;
  550. spin_lock(&io->lock);
  551. if (err)
  552. io->err = io->err ? : err;
  553. else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
  554. io->bytes = pos;
  555. left = --io->reqs;
  556. if (!left && io->blocking)
  557. complete(io->done);
  558. spin_unlock(&io->lock);
  559. if (!left && !io->blocking) {
  560. ssize_t res = fuse_get_res_by_io(io);
  561. if (res >= 0) {
  562. struct inode *inode = file_inode(io->iocb->ki_filp);
  563. struct fuse_conn *fc = get_fuse_conn(inode);
  564. struct fuse_inode *fi = get_fuse_inode(inode);
  565. spin_lock(&fi->lock);
  566. fi->attr_version = atomic64_inc_return(&fc->attr_version);
  567. spin_unlock(&fi->lock);
  568. }
  569. io->iocb->ki_complete(io->iocb, res);
  570. }
  571. kref_put(&io->refcnt, fuse_io_release);
  572. }
  573. static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
  574. unsigned int nfolios)
  575. {
  576. struct fuse_io_args *ia;
  577. ia = kzalloc_obj(*ia);
  578. if (ia) {
  579. ia->io = io;
  580. ia->ap.folios = fuse_folios_alloc(nfolios, GFP_KERNEL,
  581. &ia->ap.descs);
  582. if (!ia->ap.folios) {
  583. kfree(ia);
  584. ia = NULL;
  585. }
  586. }
  587. return ia;
  588. }
  589. static void fuse_io_free(struct fuse_io_args *ia)
  590. {
  591. kfree(ia->ap.folios);
  592. kfree(ia);
  593. }
  594. static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
  595. int err)
  596. {
  597. struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
  598. struct fuse_io_priv *io = ia->io;
  599. ssize_t pos = -1;
  600. size_t nres;
  601. if (err) {
  602. /* Nothing */
  603. } else if (io->write) {
  604. if (ia->write.out.size > ia->write.in.size) {
  605. err = -EIO;
  606. } else {
  607. nres = ia->write.out.size;
  608. if (ia->write.in.size != ia->write.out.size)
  609. pos = ia->write.in.offset - io->offset +
  610. ia->write.out.size;
  611. }
  612. } else {
  613. u32 outsize = args->out_args[0].size;
  614. nres = outsize;
  615. if (ia->read.in.size != outsize)
  616. pos = ia->read.in.offset - io->offset + outsize;
  617. }
  618. fuse_release_user_pages(&ia->ap, err ?: nres, io->should_dirty);
  619. fuse_aio_complete(io, err, pos);
  620. fuse_io_free(ia);
  621. }
  622. static ssize_t fuse_async_req_send(struct fuse_mount *fm,
  623. struct fuse_io_args *ia, size_t num_bytes)
  624. {
  625. ssize_t err;
  626. struct fuse_io_priv *io = ia->io;
  627. spin_lock(&io->lock);
  628. kref_get(&io->refcnt);
  629. io->size += num_bytes;
  630. io->reqs++;
  631. spin_unlock(&io->lock);
  632. ia->ap.args.end = fuse_aio_complete_req;
  633. ia->ap.args.may_block = io->should_dirty;
  634. err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
  635. if (err)
  636. fuse_aio_complete_req(fm, &ia->ap.args, err);
  637. return num_bytes;
  638. }
  639. static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
  640. fl_owner_t owner)
  641. {
  642. struct file *file = ia->io->iocb->ki_filp;
  643. struct fuse_file *ff = file->private_data;
  644. struct fuse_mount *fm = ff->fm;
  645. fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
  646. if (owner != NULL) {
  647. ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
  648. ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
  649. }
  650. if (ia->io->async)
  651. return fuse_async_req_send(fm, ia, count);
  652. return fuse_simple_request(fm, &ia->ap.args);
  653. }
  654. static void fuse_read_update_size(struct inode *inode, loff_t size,
  655. u64 attr_ver)
  656. {
  657. struct fuse_conn *fc = get_fuse_conn(inode);
  658. struct fuse_inode *fi = get_fuse_inode(inode);
  659. spin_lock(&fi->lock);
  660. if (attr_ver >= fi->attr_version && size < inode->i_size &&
  661. !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
  662. fi->attr_version = atomic64_inc_return(&fc->attr_version);
  663. i_size_write(inode, size);
  664. }
  665. spin_unlock(&fi->lock);
  666. }
  667. static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
  668. struct fuse_args_pages *ap)
  669. {
  670. struct fuse_conn *fc = get_fuse_conn(inode);
  671. /*
  672. * If writeback_cache is enabled, a short read means there's a hole in
  673. * the file. Some data after the hole is in page cache, but has not
  674. * reached the client fs yet. So the hole is not present there.
  675. */
  676. if (!fc->writeback_cache) {
  677. loff_t pos = folio_pos(ap->folios[0]) + num_read;
  678. fuse_read_update_size(inode, pos, attr_ver);
  679. }
  680. }
  681. static int fuse_do_readfolio(struct file *file, struct folio *folio,
  682. size_t off, size_t len)
  683. {
  684. struct inode *inode = folio->mapping->host;
  685. struct fuse_mount *fm = get_fuse_mount(inode);
  686. loff_t pos = folio_pos(folio) + off;
  687. struct fuse_folio_desc desc = {
  688. .offset = off,
  689. .length = len,
  690. };
  691. struct fuse_io_args ia = {
  692. .ap.args.page_zeroing = true,
  693. .ap.args.out_pages = true,
  694. .ap.num_folios = 1,
  695. .ap.folios = &folio,
  696. .ap.descs = &desc,
  697. };
  698. ssize_t res;
  699. u64 attr_ver;
  700. attr_ver = fuse_get_attr_version(fm->fc);
  701. /* Don't overflow end offset */
  702. if (pos + (desc.length - 1) == LLONG_MAX)
  703. desc.length--;
  704. fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
  705. res = fuse_simple_request(fm, &ia.ap.args);
  706. if (res < 0)
  707. return res;
  708. /*
  709. * Short read means EOF. If file size is larger, truncate it
  710. */
  711. if (res < desc.length)
  712. fuse_short_read(inode, attr_ver, res, &ia.ap);
  713. return 0;
  714. }
  715. static int fuse_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  716. unsigned int flags, struct iomap *iomap,
  717. struct iomap *srcmap)
  718. {
  719. iomap->type = IOMAP_MAPPED;
  720. iomap->length = length;
  721. iomap->offset = offset;
  722. return 0;
  723. }
  724. static const struct iomap_ops fuse_iomap_ops = {
  725. .iomap_begin = fuse_iomap_begin,
  726. };
  727. struct fuse_fill_read_data {
  728. struct file *file;
  729. /* Fields below are used if sending the read request asynchronously */
  730. struct fuse_conn *fc;
  731. struct fuse_io_args *ia;
  732. unsigned int nr_bytes;
  733. };
  734. /* forward declarations */
  735. static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos,
  736. unsigned len, struct fuse_args_pages *ap,
  737. unsigned cur_bytes, bool write);
  738. static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
  739. unsigned int count, bool async);
  740. static int fuse_handle_readahead(struct folio *folio,
  741. struct readahead_control *rac,
  742. struct fuse_fill_read_data *data, loff_t pos,
  743. size_t len)
  744. {
  745. struct fuse_io_args *ia = data->ia;
  746. size_t off = offset_in_folio(folio, pos);
  747. struct fuse_conn *fc = data->fc;
  748. struct fuse_args_pages *ap;
  749. unsigned int nr_pages;
  750. if (ia && fuse_folios_need_send(fc, pos, len, &ia->ap, data->nr_bytes,
  751. false)) {
  752. fuse_send_readpages(ia, data->file, data->nr_bytes,
  753. fc->async_read);
  754. data->nr_bytes = 0;
  755. data->ia = NULL;
  756. ia = NULL;
  757. }
  758. if (!ia) {
  759. if (fc->num_background >= fc->congestion_threshold &&
  760. rac->ra->async_size >= readahead_count(rac))
  761. /*
  762. * Congested and only async pages left, so skip the
  763. * rest.
  764. */
  765. return -EAGAIN;
  766. nr_pages = min(fc->max_pages, readahead_count(rac));
  767. data->ia = fuse_io_alloc(NULL, nr_pages);
  768. if (!data->ia)
  769. return -ENOMEM;
  770. ia = data->ia;
  771. }
  772. folio_get(folio);
  773. ap = &ia->ap;
  774. ap->folios[ap->num_folios] = folio;
  775. ap->descs[ap->num_folios].offset = off;
  776. ap->descs[ap->num_folios].length = len;
  777. data->nr_bytes += len;
  778. ap->num_folios++;
  779. return 0;
  780. }
  781. static int fuse_iomap_read_folio_range_async(const struct iomap_iter *iter,
  782. struct iomap_read_folio_ctx *ctx,
  783. size_t len)
  784. {
  785. struct fuse_fill_read_data *data = ctx->read_ctx;
  786. struct folio *folio = ctx->cur_folio;
  787. loff_t pos = iter->pos;
  788. size_t off = offset_in_folio(folio, pos);
  789. struct file *file = data->file;
  790. int ret;
  791. if (ctx->rac) {
  792. ret = fuse_handle_readahead(folio, ctx->rac, data, pos, len);
  793. } else {
  794. /*
  795. * for non-readahead read requests, do reads synchronously
  796. * since it's not guaranteed that the server can handle
  797. * out-of-order reads
  798. */
  799. ret = fuse_do_readfolio(file, folio, off, len);
  800. if (!ret)
  801. iomap_finish_folio_read(folio, off, len, ret);
  802. }
  803. return ret;
  804. }
  805. static void fuse_iomap_read_submit(struct iomap_read_folio_ctx *ctx)
  806. {
  807. struct fuse_fill_read_data *data = ctx->read_ctx;
  808. if (data->ia)
  809. fuse_send_readpages(data->ia, data->file, data->nr_bytes,
  810. data->fc->async_read);
  811. }
  812. static const struct iomap_read_ops fuse_iomap_read_ops = {
  813. .read_folio_range = fuse_iomap_read_folio_range_async,
  814. .submit_read = fuse_iomap_read_submit,
  815. };
  816. static int fuse_read_folio(struct file *file, struct folio *folio)
  817. {
  818. struct inode *inode = folio->mapping->host;
  819. struct fuse_fill_read_data data = {
  820. .file = file,
  821. };
  822. struct iomap_read_folio_ctx ctx = {
  823. .cur_folio = folio,
  824. .ops = &fuse_iomap_read_ops,
  825. .read_ctx = &data,
  826. };
  827. if (fuse_is_bad(inode)) {
  828. folio_unlock(folio);
  829. return -EIO;
  830. }
  831. iomap_read_folio(&fuse_iomap_ops, &ctx, NULL);
  832. fuse_invalidate_atime(inode);
  833. return 0;
  834. }
  835. static int fuse_iomap_read_folio_range(const struct iomap_iter *iter,
  836. struct folio *folio, loff_t pos,
  837. size_t len)
  838. {
  839. struct file *file = iter->private;
  840. size_t off = offset_in_folio(folio, pos);
  841. return fuse_do_readfolio(file, folio, off, len);
  842. }
  843. static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
  844. int err)
  845. {
  846. int i;
  847. struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
  848. struct fuse_args_pages *ap = &ia->ap;
  849. size_t count = ia->read.in.size;
  850. size_t num_read = args->out_args[0].size;
  851. struct address_space *mapping;
  852. struct inode *inode;
  853. WARN_ON_ONCE(!ap->num_folios);
  854. mapping = ap->folios[0]->mapping;
  855. inode = mapping->host;
  856. /*
  857. * Short read means EOF. If file size is larger, truncate it
  858. */
  859. if (!err && num_read < count)
  860. fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
  861. fuse_invalidate_atime(inode);
  862. for (i = 0; i < ap->num_folios; i++) {
  863. iomap_finish_folio_read(ap->folios[i], ap->descs[i].offset,
  864. ap->descs[i].length, err);
  865. folio_put(ap->folios[i]);
  866. }
  867. if (ia->ff)
  868. fuse_file_put(ia->ff, false);
  869. fuse_io_free(ia);
  870. }
  871. static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
  872. unsigned int count, bool async)
  873. {
  874. struct fuse_file *ff = file->private_data;
  875. struct fuse_mount *fm = ff->fm;
  876. struct fuse_args_pages *ap = &ia->ap;
  877. loff_t pos = folio_pos(ap->folios[0]);
  878. ssize_t res;
  879. int err;
  880. ap->args.out_pages = true;
  881. ap->args.page_zeroing = true;
  882. ap->args.page_replace = true;
  883. /* Don't overflow end offset */
  884. if (pos + (count - 1) == LLONG_MAX) {
  885. count--;
  886. ap->descs[ap->num_folios - 1].length--;
  887. }
  888. WARN_ON((loff_t) (pos + count) < 0);
  889. fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
  890. ia->read.attr_ver = fuse_get_attr_version(fm->fc);
  891. if (async) {
  892. ia->ff = fuse_file_get(ff);
  893. ap->args.end = fuse_readpages_end;
  894. err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
  895. if (!err)
  896. return;
  897. } else {
  898. res = fuse_simple_request(fm, &ap->args);
  899. err = res < 0 ? res : 0;
  900. }
  901. fuse_readpages_end(fm, &ap->args, err);
  902. }
  903. static void fuse_readahead(struct readahead_control *rac)
  904. {
  905. struct inode *inode = rac->mapping->host;
  906. struct fuse_conn *fc = get_fuse_conn(inode);
  907. struct fuse_fill_read_data data = {
  908. .file = rac->file,
  909. .fc = fc,
  910. };
  911. struct iomap_read_folio_ctx ctx = {
  912. .ops = &fuse_iomap_read_ops,
  913. .rac = rac,
  914. .read_ctx = &data
  915. };
  916. if (fuse_is_bad(inode))
  917. return;
  918. iomap_readahead(&fuse_iomap_ops, &ctx, NULL);
  919. }
  920. static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
  921. {
  922. struct inode *inode = iocb->ki_filp->f_mapping->host;
  923. struct fuse_conn *fc = get_fuse_conn(inode);
  924. /*
  925. * In auto invalidate mode, always update attributes on read.
  926. * Otherwise, only update if we attempt to read past EOF (to ensure
  927. * i_size is up to date).
  928. */
  929. if (fc->auto_inval_data ||
  930. (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
  931. int err;
  932. err = fuse_update_attributes(inode, iocb->ki_filp, STATX_SIZE);
  933. if (err)
  934. return err;
  935. }
  936. return generic_file_read_iter(iocb, to);
  937. }
  938. static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
  939. loff_t pos, size_t count)
  940. {
  941. struct fuse_args *args = &ia->ap.args;
  942. ia->write.in.fh = ff->fh;
  943. ia->write.in.offset = pos;
  944. ia->write.in.size = count;
  945. args->opcode = FUSE_WRITE;
  946. args->nodeid = ff->nodeid;
  947. args->in_numargs = 2;
  948. if (ff->fm->fc->minor < 9)
  949. args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
  950. else
  951. args->in_args[0].size = sizeof(ia->write.in);
  952. args->in_args[0].value = &ia->write.in;
  953. args->in_args[1].size = count;
  954. args->out_numargs = 1;
  955. args->out_args[0].size = sizeof(ia->write.out);
  956. args->out_args[0].value = &ia->write.out;
  957. }
  958. static unsigned int fuse_write_flags(struct kiocb *iocb)
  959. {
  960. unsigned int flags = iocb->ki_filp->f_flags;
  961. if (iocb_is_dsync(iocb))
  962. flags |= O_DSYNC;
  963. if (iocb->ki_flags & IOCB_SYNC)
  964. flags |= O_SYNC;
  965. return flags;
  966. }
  967. static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
  968. size_t count, fl_owner_t owner)
  969. {
  970. struct kiocb *iocb = ia->io->iocb;
  971. struct file *file = iocb->ki_filp;
  972. struct fuse_file *ff = file->private_data;
  973. struct fuse_mount *fm = ff->fm;
  974. struct fuse_write_in *inarg = &ia->write.in;
  975. ssize_t err;
  976. fuse_write_args_fill(ia, ff, pos, count);
  977. inarg->flags = fuse_write_flags(iocb);
  978. if (owner != NULL) {
  979. inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
  980. inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
  981. }
  982. if (ia->io->async)
  983. return fuse_async_req_send(fm, ia, count);
  984. err = fuse_simple_request(fm, &ia->ap.args);
  985. if (!err && ia->write.out.size > count)
  986. err = -EIO;
  987. return err ?: ia->write.out.size;
  988. }
  989. bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written)
  990. {
  991. struct fuse_conn *fc = get_fuse_conn(inode);
  992. struct fuse_inode *fi = get_fuse_inode(inode);
  993. bool ret = false;
  994. spin_lock(&fi->lock);
  995. fi->attr_version = atomic64_inc_return(&fc->attr_version);
  996. if (written > 0 && pos > inode->i_size) {
  997. i_size_write(inode, pos);
  998. ret = true;
  999. }
  1000. spin_unlock(&fi->lock);
  1001. fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
  1002. return ret;
  1003. }
  1004. static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
  1005. struct kiocb *iocb, struct inode *inode,
  1006. loff_t pos, size_t count)
  1007. {
  1008. struct fuse_args_pages *ap = &ia->ap;
  1009. struct file *file = iocb->ki_filp;
  1010. struct fuse_file *ff = file->private_data;
  1011. struct fuse_mount *fm = ff->fm;
  1012. unsigned int offset, i;
  1013. bool short_write;
  1014. int err;
  1015. for (i = 0; i < ap->num_folios; i++)
  1016. folio_wait_writeback(ap->folios[i]);
  1017. fuse_write_args_fill(ia, ff, pos, count);
  1018. ia->write.in.flags = fuse_write_flags(iocb);
  1019. if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
  1020. ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
  1021. err = fuse_simple_request(fm, &ap->args);
  1022. if (!err && ia->write.out.size > count)
  1023. err = -EIO;
  1024. short_write = ia->write.out.size < count;
  1025. offset = ap->descs[0].offset;
  1026. count = ia->write.out.size;
  1027. for (i = 0; i < ap->num_folios; i++) {
  1028. struct folio *folio = ap->folios[i];
  1029. if (err) {
  1030. folio_clear_uptodate(folio);
  1031. } else {
  1032. if (count >= folio_size(folio) - offset)
  1033. count -= folio_size(folio) - offset;
  1034. else {
  1035. if (short_write)
  1036. folio_clear_uptodate(folio);
  1037. count = 0;
  1038. }
  1039. offset = 0;
  1040. }
  1041. if (ia->write.folio_locked && (i == ap->num_folios - 1))
  1042. folio_unlock(folio);
  1043. folio_put(folio);
  1044. }
  1045. return err;
  1046. }
  1047. static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
  1048. struct address_space *mapping,
  1049. struct iov_iter *ii, loff_t pos,
  1050. unsigned int max_folios)
  1051. {
  1052. struct fuse_args_pages *ap = &ia->ap;
  1053. struct fuse_conn *fc = get_fuse_conn(mapping->host);
  1054. unsigned offset = pos & (PAGE_SIZE - 1);
  1055. size_t count = 0;
  1056. unsigned int num;
  1057. int err = 0;
  1058. num = min(iov_iter_count(ii), fc->max_write);
  1059. ap->args.in_pages = true;
  1060. while (num && ap->num_folios < max_folios) {
  1061. size_t tmp;
  1062. struct folio *folio;
  1063. pgoff_t index = pos >> PAGE_SHIFT;
  1064. unsigned int bytes;
  1065. unsigned int folio_offset;
  1066. again:
  1067. folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
  1068. mapping_gfp_mask(mapping));
  1069. if (IS_ERR(folio)) {
  1070. err = PTR_ERR(folio);
  1071. break;
  1072. }
  1073. if (mapping_writably_mapped(mapping))
  1074. flush_dcache_folio(folio);
  1075. folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
  1076. bytes = min(folio_size(folio) - folio_offset, num);
  1077. tmp = copy_folio_from_iter_atomic(folio, folio_offset, bytes, ii);
  1078. flush_dcache_folio(folio);
  1079. if (!tmp) {
  1080. folio_unlock(folio);
  1081. folio_put(folio);
  1082. /*
  1083. * Ensure forward progress by faulting in
  1084. * while not holding the folio lock:
  1085. */
  1086. if (fault_in_iov_iter_readable(ii, bytes)) {
  1087. err = -EFAULT;
  1088. break;
  1089. }
  1090. goto again;
  1091. }
  1092. ap->folios[ap->num_folios] = folio;
  1093. ap->descs[ap->num_folios].offset = folio_offset;
  1094. ap->descs[ap->num_folios].length = tmp;
  1095. ap->num_folios++;
  1096. count += tmp;
  1097. pos += tmp;
  1098. num -= tmp;
  1099. offset += tmp;
  1100. if (offset == folio_size(folio))
  1101. offset = 0;
  1102. /* If we copied full folio, mark it uptodate */
  1103. if (tmp == folio_size(folio))
  1104. folio_mark_uptodate(folio);
  1105. if (folio_test_uptodate(folio)) {
  1106. folio_unlock(folio);
  1107. } else {
  1108. ia->write.folio_locked = true;
  1109. break;
  1110. }
  1111. if (!fc->big_writes || offset != 0)
  1112. break;
  1113. }
  1114. return count > 0 ? count : err;
  1115. }
  1116. static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
  1117. unsigned int max_pages)
  1118. {
  1119. unsigned int pages = ((pos + len - 1) >> PAGE_SHIFT) -
  1120. (pos >> PAGE_SHIFT) + 1;
  1121. return min(pages, max_pages);
  1122. }
  1123. static ssize_t fuse_perform_write(struct kiocb *iocb, struct iov_iter *ii)
  1124. {
  1125. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1126. struct inode *inode = mapping->host;
  1127. struct fuse_conn *fc = get_fuse_conn(inode);
  1128. struct fuse_inode *fi = get_fuse_inode(inode);
  1129. loff_t pos = iocb->ki_pos;
  1130. int err = 0;
  1131. ssize_t res = 0;
  1132. if (inode->i_size < pos + iov_iter_count(ii))
  1133. set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  1134. do {
  1135. ssize_t count;
  1136. struct fuse_io_args ia = {};
  1137. struct fuse_args_pages *ap = &ia.ap;
  1138. unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
  1139. fc->max_pages);
  1140. ap->folios = fuse_folios_alloc(nr_pages, GFP_KERNEL, &ap->descs);
  1141. if (!ap->folios) {
  1142. err = -ENOMEM;
  1143. break;
  1144. }
  1145. count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
  1146. if (count <= 0) {
  1147. err = count;
  1148. } else {
  1149. err = fuse_send_write_pages(&ia, iocb, inode,
  1150. pos, count);
  1151. if (!err) {
  1152. size_t num_written = ia.write.out.size;
  1153. res += num_written;
  1154. pos += num_written;
  1155. /* break out of the loop on short write */
  1156. if (num_written != count)
  1157. err = -EIO;
  1158. }
  1159. }
  1160. kfree(ap->folios);
  1161. } while (!err && iov_iter_count(ii));
  1162. fuse_write_update_attr(inode, pos, res);
  1163. clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  1164. if (!res)
  1165. return err;
  1166. iocb->ki_pos += res;
  1167. return res;
  1168. }
  1169. static bool fuse_io_past_eof(struct kiocb *iocb, struct iov_iter *iter)
  1170. {
  1171. struct inode *inode = file_inode(iocb->ki_filp);
  1172. return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode);
  1173. }
  1174. /*
  1175. * @return true if an exclusive lock for direct IO writes is needed
  1176. */
  1177. static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from)
  1178. {
  1179. struct file *file = iocb->ki_filp;
  1180. struct fuse_file *ff = file->private_data;
  1181. struct inode *inode = file_inode(iocb->ki_filp);
  1182. struct fuse_inode *fi = get_fuse_inode(inode);
  1183. /* Server side has to advise that it supports parallel dio writes. */
  1184. if (!(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES))
  1185. return true;
  1186. /*
  1187. * Append will need to know the eventual EOF - always needs an
  1188. * exclusive lock.
  1189. */
  1190. if (iocb->ki_flags & IOCB_APPEND)
  1191. return true;
  1192. /* shared locks are not allowed with parallel page cache IO */
  1193. if (test_bit(FUSE_I_CACHE_IO_MODE, &fi->state))
  1194. return true;
  1195. /* Parallel dio beyond EOF is not supported, at least for now. */
  1196. if (fuse_io_past_eof(iocb, from))
  1197. return true;
  1198. return false;
  1199. }
  1200. static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from,
  1201. bool *exclusive)
  1202. {
  1203. struct inode *inode = file_inode(iocb->ki_filp);
  1204. struct fuse_inode *fi = get_fuse_inode(inode);
  1205. *exclusive = fuse_dio_wr_exclusive_lock(iocb, from);
  1206. if (*exclusive) {
  1207. inode_lock(inode);
  1208. } else {
  1209. inode_lock_shared(inode);
  1210. /*
  1211. * New parallal dio allowed only if inode is not in caching
  1212. * mode and denies new opens in caching mode. This check
  1213. * should be performed only after taking shared inode lock.
  1214. * Previous past eof check was without inode lock and might
  1215. * have raced, so check it again.
  1216. */
  1217. if (fuse_io_past_eof(iocb, from) ||
  1218. fuse_inode_uncached_io_start(fi, NULL) != 0) {
  1219. inode_unlock_shared(inode);
  1220. inode_lock(inode);
  1221. *exclusive = true;
  1222. }
  1223. }
  1224. }
  1225. static void fuse_dio_unlock(struct kiocb *iocb, bool exclusive)
  1226. {
  1227. struct inode *inode = file_inode(iocb->ki_filp);
  1228. struct fuse_inode *fi = get_fuse_inode(inode);
  1229. if (exclusive) {
  1230. inode_unlock(inode);
  1231. } else {
  1232. /* Allow opens in caching mode after last parallel dio end */
  1233. fuse_inode_uncached_io_end(fi);
  1234. inode_unlock_shared(inode);
  1235. }
  1236. }
  1237. static const struct iomap_write_ops fuse_iomap_write_ops = {
  1238. .read_folio_range = fuse_iomap_read_folio_range,
  1239. };
  1240. static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1241. {
  1242. struct file *file = iocb->ki_filp;
  1243. struct mnt_idmap *idmap = file_mnt_idmap(file);
  1244. struct address_space *mapping = file->f_mapping;
  1245. ssize_t written = 0;
  1246. struct inode *inode = mapping->host;
  1247. ssize_t err, count;
  1248. struct fuse_conn *fc = get_fuse_conn(inode);
  1249. bool writeback = false;
  1250. if (fc->writeback_cache) {
  1251. /* Update size (EOF optimization) and mode (SUID clearing) */
  1252. err = fuse_update_attributes(mapping->host, file,
  1253. STATX_SIZE | STATX_MODE);
  1254. if (err)
  1255. return err;
  1256. if (!fc->handle_killpriv_v2 ||
  1257. !setattr_should_drop_suidgid(idmap, file_inode(file)))
  1258. writeback = true;
  1259. }
  1260. inode_lock(inode);
  1261. err = count = generic_write_checks(iocb, from);
  1262. if (err <= 0)
  1263. goto out;
  1264. task_io_account_write(count);
  1265. err = kiocb_modified(iocb);
  1266. if (err)
  1267. goto out;
  1268. if (iocb->ki_flags & IOCB_DIRECT) {
  1269. written = generic_file_direct_write(iocb, from);
  1270. if (written < 0 || !iov_iter_count(from))
  1271. goto out;
  1272. written = direct_write_fallback(iocb, from, written,
  1273. fuse_perform_write(iocb, from));
  1274. } else if (writeback) {
  1275. /*
  1276. * Use iomap so that we can do granular uptodate reads
  1277. * and granular dirty tracking for large folios.
  1278. */
  1279. written = iomap_file_buffered_write(iocb, from,
  1280. &fuse_iomap_ops,
  1281. &fuse_iomap_write_ops,
  1282. file);
  1283. } else {
  1284. written = fuse_perform_write(iocb, from);
  1285. }
  1286. out:
  1287. inode_unlock(inode);
  1288. if (written > 0)
  1289. written = generic_write_sync(iocb, written);
  1290. return written ? written : err;
  1291. }
  1292. static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
  1293. {
  1294. return (unsigned long)iter_iov(ii)->iov_base + ii->iov_offset;
  1295. }
  1296. static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
  1297. size_t max_size)
  1298. {
  1299. return min(iov_iter_single_seg_count(ii), max_size);
  1300. }
  1301. static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
  1302. size_t *nbytesp, int write,
  1303. unsigned int max_pages,
  1304. bool use_pages_for_kvec_io)
  1305. {
  1306. bool flush_or_invalidate = false;
  1307. unsigned int nr_pages = 0;
  1308. size_t nbytes = 0; /* # bytes already packed in req */
  1309. ssize_t ret = 0;
  1310. /* Special case for kernel I/O: can copy directly into the buffer.
  1311. * However if the implementation of fuse_conn requires pages instead of
  1312. * pointer (e.g., virtio-fs), use iov_iter_extract_pages() instead.
  1313. */
  1314. if (iov_iter_is_kvec(ii)) {
  1315. void *user_addr = (void *)fuse_get_user_addr(ii);
  1316. if (!use_pages_for_kvec_io) {
  1317. size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
  1318. if (write)
  1319. ap->args.in_args[1].value = user_addr;
  1320. else
  1321. ap->args.out_args[0].value = user_addr;
  1322. iov_iter_advance(ii, frag_size);
  1323. *nbytesp = frag_size;
  1324. return 0;
  1325. }
  1326. if (is_vmalloc_addr(user_addr)) {
  1327. ap->args.vmap_base = user_addr;
  1328. flush_or_invalidate = true;
  1329. }
  1330. }
  1331. /*
  1332. * Until there is support for iov_iter_extract_folios(), we have to
  1333. * manually extract pages using iov_iter_extract_pages() and then
  1334. * copy that to a folios array.
  1335. */
  1336. struct page **pages = kzalloc(max_pages * sizeof(struct page *),
  1337. GFP_KERNEL);
  1338. if (!pages) {
  1339. ret = -ENOMEM;
  1340. goto out;
  1341. }
  1342. while (nbytes < *nbytesp && nr_pages < max_pages) {
  1343. unsigned nfolios, i;
  1344. size_t start;
  1345. ret = iov_iter_extract_pages(ii, &pages,
  1346. *nbytesp - nbytes,
  1347. max_pages - nr_pages,
  1348. 0, &start);
  1349. if (ret < 0)
  1350. break;
  1351. nbytes += ret;
  1352. nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
  1353. for (i = 0; i < nfolios; i++) {
  1354. struct folio *folio = page_folio(pages[i]);
  1355. unsigned int offset = start +
  1356. (folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
  1357. unsigned int len = umin(ret, PAGE_SIZE - start);
  1358. ap->descs[ap->num_folios].offset = offset;
  1359. ap->descs[ap->num_folios].length = len;
  1360. ap->folios[ap->num_folios] = folio;
  1361. start = 0;
  1362. ret -= len;
  1363. ap->num_folios++;
  1364. }
  1365. nr_pages += nfolios;
  1366. }
  1367. kfree(pages);
  1368. if (write && flush_or_invalidate)
  1369. flush_kernel_vmap_range(ap->args.vmap_base, nbytes);
  1370. ap->args.invalidate_vmap = !write && flush_or_invalidate;
  1371. ap->args.is_pinned = iov_iter_extract_will_pin(ii);
  1372. ap->args.user_pages = true;
  1373. if (write)
  1374. ap->args.in_pages = true;
  1375. else
  1376. ap->args.out_pages = true;
  1377. out:
  1378. *nbytesp = nbytes;
  1379. return ret < 0 ? ret : 0;
  1380. }
  1381. ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
  1382. loff_t *ppos, int flags)
  1383. {
  1384. int write = flags & FUSE_DIO_WRITE;
  1385. int cuse = flags & FUSE_DIO_CUSE;
  1386. struct file *file = io->iocb->ki_filp;
  1387. struct address_space *mapping = file->f_mapping;
  1388. struct inode *inode = mapping->host;
  1389. struct fuse_file *ff = file->private_data;
  1390. struct fuse_conn *fc = ff->fm->fc;
  1391. size_t nmax = write ? fc->max_write : fc->max_read;
  1392. loff_t pos = *ppos;
  1393. size_t count = iov_iter_count(iter);
  1394. pgoff_t idx_from = pos >> PAGE_SHIFT;
  1395. pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
  1396. ssize_t res = 0;
  1397. int err = 0;
  1398. struct fuse_io_args *ia;
  1399. unsigned int max_pages;
  1400. bool fopen_direct_io = ff->open_flags & FOPEN_DIRECT_IO;
  1401. max_pages = iov_iter_npages(iter, fc->max_pages);
  1402. ia = fuse_io_alloc(io, max_pages);
  1403. if (!ia)
  1404. return -ENOMEM;
  1405. if (fopen_direct_io) {
  1406. res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);
  1407. if (res) {
  1408. fuse_io_free(ia);
  1409. return res;
  1410. }
  1411. }
  1412. if (!cuse && filemap_range_has_writeback(mapping, pos, (pos + count - 1))) {
  1413. if (!write)
  1414. inode_lock(inode);
  1415. fuse_sync_writes(inode);
  1416. if (!write)
  1417. inode_unlock(inode);
  1418. }
  1419. if (fopen_direct_io && write) {
  1420. res = invalidate_inode_pages2_range(mapping, idx_from, idx_to);
  1421. if (res) {
  1422. fuse_io_free(ia);
  1423. return res;
  1424. }
  1425. }
  1426. io->should_dirty = !write && user_backed_iter(iter);
  1427. while (count) {
  1428. ssize_t nres;
  1429. fl_owner_t owner = current->files;
  1430. size_t nbytes = min(count, nmax);
  1431. err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
  1432. max_pages, fc->use_pages_for_kvec_io);
  1433. if (err && !nbytes)
  1434. break;
  1435. if (write) {
  1436. if (!capable(CAP_FSETID))
  1437. ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
  1438. nres = fuse_send_write(ia, pos, nbytes, owner);
  1439. } else {
  1440. nres = fuse_send_read(ia, pos, nbytes, owner);
  1441. }
  1442. if (!io->async || nres < 0) {
  1443. fuse_release_user_pages(&ia->ap, nres, io->should_dirty);
  1444. fuse_io_free(ia);
  1445. }
  1446. ia = NULL;
  1447. if (nres < 0) {
  1448. iov_iter_revert(iter, nbytes);
  1449. err = nres;
  1450. break;
  1451. }
  1452. WARN_ON(nres > nbytes);
  1453. count -= nres;
  1454. res += nres;
  1455. pos += nres;
  1456. if (nres != nbytes) {
  1457. iov_iter_revert(iter, nbytes - nres);
  1458. break;
  1459. }
  1460. if (count) {
  1461. max_pages = iov_iter_npages(iter, fc->max_pages);
  1462. ia = fuse_io_alloc(io, max_pages);
  1463. if (!ia)
  1464. break;
  1465. }
  1466. }
  1467. if (ia)
  1468. fuse_io_free(ia);
  1469. if (res > 0)
  1470. *ppos = pos;
  1471. if (res > 0 && write && fopen_direct_io) {
  1472. /*
  1473. * As in generic_file_direct_write(), invalidate after the
  1474. * write, to invalidate read-ahead cache that may have competed
  1475. * with the write.
  1476. */
  1477. invalidate_inode_pages2_range(mapping, idx_from, idx_to);
  1478. }
  1479. return res > 0 ? res : err;
  1480. }
  1481. EXPORT_SYMBOL_GPL(fuse_direct_io);
  1482. static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
  1483. struct iov_iter *iter,
  1484. loff_t *ppos)
  1485. {
  1486. ssize_t res;
  1487. struct inode *inode = file_inode(io->iocb->ki_filp);
  1488. res = fuse_direct_io(io, iter, ppos, 0);
  1489. fuse_invalidate_atime(inode);
  1490. return res;
  1491. }
  1492. static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
  1493. static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1494. {
  1495. ssize_t res;
  1496. if (!is_sync_kiocb(iocb)) {
  1497. res = fuse_direct_IO(iocb, to);
  1498. } else {
  1499. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
  1500. res = __fuse_direct_read(&io, to, &iocb->ki_pos);
  1501. }
  1502. return res;
  1503. }
  1504. static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1505. {
  1506. struct inode *inode = file_inode(iocb->ki_filp);
  1507. ssize_t res;
  1508. bool exclusive;
  1509. fuse_dio_lock(iocb, from, &exclusive);
  1510. res = generic_write_checks(iocb, from);
  1511. if (res > 0) {
  1512. task_io_account_write(res);
  1513. if (!is_sync_kiocb(iocb)) {
  1514. res = fuse_direct_IO(iocb, from);
  1515. } else {
  1516. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
  1517. res = fuse_direct_io(&io, from, &iocb->ki_pos,
  1518. FUSE_DIO_WRITE);
  1519. fuse_write_update_attr(inode, iocb->ki_pos, res);
  1520. }
  1521. }
  1522. fuse_dio_unlock(iocb, exclusive);
  1523. return res;
  1524. }
  1525. static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1526. {
  1527. struct file *file = iocb->ki_filp;
  1528. struct fuse_file *ff = file->private_data;
  1529. struct inode *inode = file_inode(file);
  1530. if (fuse_is_bad(inode))
  1531. return -EIO;
  1532. if (FUSE_IS_DAX(inode))
  1533. return fuse_dax_read_iter(iocb, to);
  1534. /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
  1535. if (ff->open_flags & FOPEN_DIRECT_IO)
  1536. return fuse_direct_read_iter(iocb, to);
  1537. else if (fuse_file_passthrough(ff))
  1538. return fuse_passthrough_read_iter(iocb, to);
  1539. else
  1540. return fuse_cache_read_iter(iocb, to);
  1541. }
  1542. static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1543. {
  1544. struct file *file = iocb->ki_filp;
  1545. struct fuse_file *ff = file->private_data;
  1546. struct inode *inode = file_inode(file);
  1547. if (fuse_is_bad(inode))
  1548. return -EIO;
  1549. if (FUSE_IS_DAX(inode))
  1550. return fuse_dax_write_iter(iocb, from);
  1551. /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
  1552. if (ff->open_flags & FOPEN_DIRECT_IO)
  1553. return fuse_direct_write_iter(iocb, from);
  1554. else if (fuse_file_passthrough(ff))
  1555. return fuse_passthrough_write_iter(iocb, from);
  1556. else
  1557. return fuse_cache_write_iter(iocb, from);
  1558. }
  1559. static ssize_t fuse_splice_read(struct file *in, loff_t *ppos,
  1560. struct pipe_inode_info *pipe, size_t len,
  1561. unsigned int flags)
  1562. {
  1563. struct fuse_file *ff = in->private_data;
  1564. /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
  1565. if (fuse_file_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO))
  1566. return fuse_passthrough_splice_read(in, ppos, pipe, len, flags);
  1567. else
  1568. return filemap_splice_read(in, ppos, pipe, len, flags);
  1569. }
  1570. static ssize_t fuse_splice_write(struct pipe_inode_info *pipe, struct file *out,
  1571. loff_t *ppos, size_t len, unsigned int flags)
  1572. {
  1573. struct fuse_file *ff = out->private_data;
  1574. /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
  1575. if (fuse_file_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO))
  1576. return fuse_passthrough_splice_write(pipe, out, ppos, len, flags);
  1577. else
  1578. return iter_file_splice_write(pipe, out, ppos, len, flags);
  1579. }
  1580. static void fuse_writepage_free(struct fuse_writepage_args *wpa)
  1581. {
  1582. struct fuse_args_pages *ap = &wpa->ia.ap;
  1583. if (wpa->bucket)
  1584. fuse_sync_bucket_dec(wpa->bucket);
  1585. fuse_file_put(wpa->ia.ff, false);
  1586. kfree(ap->folios);
  1587. kfree(wpa);
  1588. }
  1589. static void fuse_writepage_finish(struct fuse_writepage_args *wpa)
  1590. {
  1591. struct fuse_args_pages *ap = &wpa->ia.ap;
  1592. struct inode *inode = wpa->inode;
  1593. struct fuse_inode *fi = get_fuse_inode(inode);
  1594. int i;
  1595. for (i = 0; i < ap->num_folios; i++)
  1596. /*
  1597. * Benchmarks showed that ending writeback within the
  1598. * scope of the fi->lock alleviates xarray lock
  1599. * contention and noticeably improves performance.
  1600. */
  1601. iomap_finish_folio_write(inode, ap->folios[i],
  1602. ap->descs[i].length);
  1603. wake_up(&fi->page_waitq);
  1604. }
  1605. /* Called under fi->lock, may release and reacquire it */
  1606. static void fuse_send_writepage(struct fuse_mount *fm,
  1607. struct fuse_writepage_args *wpa, loff_t size)
  1608. __releases(fi->lock)
  1609. __acquires(fi->lock)
  1610. {
  1611. struct fuse_inode *fi = get_fuse_inode(wpa->inode);
  1612. struct fuse_args_pages *ap = &wpa->ia.ap;
  1613. struct fuse_write_in *inarg = &wpa->ia.write.in;
  1614. struct fuse_args *args = &ap->args;
  1615. __u64 data_size = 0;
  1616. int err, i;
  1617. for (i = 0; i < ap->num_folios; i++)
  1618. data_size += ap->descs[i].length;
  1619. fi->writectr++;
  1620. if (inarg->offset + data_size <= size) {
  1621. inarg->size = data_size;
  1622. } else if (inarg->offset < size) {
  1623. inarg->size = size - inarg->offset;
  1624. } else {
  1625. /* Got truncated off completely */
  1626. goto out_free;
  1627. }
  1628. args->in_args[1].size = inarg->size;
  1629. args->force = true;
  1630. args->nocreds = true;
  1631. err = fuse_simple_background(fm, args, GFP_ATOMIC);
  1632. if (err == -ENOMEM) {
  1633. spin_unlock(&fi->lock);
  1634. err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
  1635. spin_lock(&fi->lock);
  1636. }
  1637. /* Fails on broken connection only */
  1638. if (unlikely(err))
  1639. goto out_free;
  1640. return;
  1641. out_free:
  1642. fi->writectr--;
  1643. fuse_writepage_finish(wpa);
  1644. spin_unlock(&fi->lock);
  1645. fuse_writepage_free(wpa);
  1646. spin_lock(&fi->lock);
  1647. }
  1648. /*
  1649. * If fi->writectr is positive (no truncate or fsync going on) send
  1650. * all queued writepage requests.
  1651. *
  1652. * Called with fi->lock
  1653. */
  1654. void fuse_flush_writepages(struct inode *inode)
  1655. __releases(fi->lock)
  1656. __acquires(fi->lock)
  1657. {
  1658. struct fuse_mount *fm = get_fuse_mount(inode);
  1659. struct fuse_inode *fi = get_fuse_inode(inode);
  1660. loff_t crop = i_size_read(inode);
  1661. struct fuse_writepage_args *wpa;
  1662. while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
  1663. wpa = list_entry(fi->queued_writes.next,
  1664. struct fuse_writepage_args, queue_entry);
  1665. list_del_init(&wpa->queue_entry);
  1666. fuse_send_writepage(fm, wpa, crop);
  1667. }
  1668. }
  1669. static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
  1670. int error)
  1671. {
  1672. struct fuse_writepage_args *wpa =
  1673. container_of(args, typeof(*wpa), ia.ap.args);
  1674. struct inode *inode = wpa->inode;
  1675. struct fuse_inode *fi = get_fuse_inode(inode);
  1676. struct fuse_conn *fc = get_fuse_conn(inode);
  1677. mapping_set_error(inode->i_mapping, error);
  1678. /*
  1679. * A writeback finished and this might have updated mtime/ctime on
  1680. * server making local mtime/ctime stale. Hence invalidate attrs.
  1681. * Do this only if writeback_cache is not enabled. If writeback_cache
  1682. * is enabled, we trust local ctime/mtime.
  1683. */
  1684. if (!fc->writeback_cache)
  1685. fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY);
  1686. spin_lock(&fi->lock);
  1687. fi->writectr--;
  1688. fuse_writepage_finish(wpa);
  1689. spin_unlock(&fi->lock);
  1690. fuse_writepage_free(wpa);
  1691. }
  1692. static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi)
  1693. {
  1694. struct fuse_file *ff;
  1695. spin_lock(&fi->lock);
  1696. ff = list_first_entry_or_null(&fi->write_files, struct fuse_file,
  1697. write_entry);
  1698. if (ff)
  1699. fuse_file_get(ff);
  1700. spin_unlock(&fi->lock);
  1701. return ff;
  1702. }
  1703. static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi)
  1704. {
  1705. struct fuse_file *ff = __fuse_write_file_get(fi);
  1706. WARN_ON(!ff);
  1707. return ff;
  1708. }
  1709. int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
  1710. {
  1711. struct fuse_inode *fi = get_fuse_inode(inode);
  1712. struct fuse_file *ff;
  1713. int err;
  1714. ff = __fuse_write_file_get(fi);
  1715. err = fuse_flush_times(inode, ff);
  1716. if (ff)
  1717. fuse_file_put(ff, false);
  1718. return err;
  1719. }
  1720. static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
  1721. {
  1722. struct fuse_writepage_args *wpa;
  1723. struct fuse_args_pages *ap;
  1724. wpa = kzalloc_obj(*wpa, GFP_NOFS);
  1725. if (wpa) {
  1726. ap = &wpa->ia.ap;
  1727. ap->num_folios = 0;
  1728. ap->folios = fuse_folios_alloc(1, GFP_NOFS, &ap->descs);
  1729. if (!ap->folios) {
  1730. kfree(wpa);
  1731. wpa = NULL;
  1732. }
  1733. }
  1734. return wpa;
  1735. }
  1736. static void fuse_writepage_add_to_bucket(struct fuse_conn *fc,
  1737. struct fuse_writepage_args *wpa)
  1738. {
  1739. if (!fc->sync_fs)
  1740. return;
  1741. rcu_read_lock();
  1742. /* Prevent resurrection of dead bucket in unlikely race with syncfs */
  1743. do {
  1744. wpa->bucket = rcu_dereference(fc->curr_bucket);
  1745. } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count)));
  1746. rcu_read_unlock();
  1747. }
  1748. static void fuse_writepage_args_page_fill(struct fuse_writepage_args *wpa, struct folio *folio,
  1749. uint32_t folio_index, loff_t offset, unsigned len)
  1750. {
  1751. struct fuse_args_pages *ap = &wpa->ia.ap;
  1752. ap->folios[folio_index] = folio;
  1753. ap->descs[folio_index].offset = offset;
  1754. ap->descs[folio_index].length = len;
  1755. }
  1756. static struct fuse_writepage_args *fuse_writepage_args_setup(struct folio *folio,
  1757. size_t offset,
  1758. struct fuse_file *ff)
  1759. {
  1760. struct inode *inode = folio->mapping->host;
  1761. struct fuse_conn *fc = get_fuse_conn(inode);
  1762. struct fuse_writepage_args *wpa;
  1763. struct fuse_args_pages *ap;
  1764. wpa = fuse_writepage_args_alloc();
  1765. if (!wpa)
  1766. return NULL;
  1767. fuse_writepage_add_to_bucket(fc, wpa);
  1768. fuse_write_args_fill(&wpa->ia, ff, folio_pos(folio) + offset, 0);
  1769. wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
  1770. wpa->inode = inode;
  1771. wpa->ia.ff = ff;
  1772. ap = &wpa->ia.ap;
  1773. ap->args.in_pages = true;
  1774. ap->args.end = fuse_writepage_end;
  1775. return wpa;
  1776. }
  1777. struct fuse_fill_wb_data {
  1778. struct fuse_writepage_args *wpa;
  1779. struct fuse_file *ff;
  1780. unsigned int max_folios;
  1781. /*
  1782. * nr_bytes won't overflow since fuse_folios_need_send() caps
  1783. * wb requests to never exceed fc->max_pages (which has an upper bound
  1784. * of U16_MAX).
  1785. */
  1786. unsigned int nr_bytes;
  1787. };
  1788. static bool fuse_pages_realloc(struct fuse_fill_wb_data *data,
  1789. unsigned int max_pages)
  1790. {
  1791. struct fuse_args_pages *ap = &data->wpa->ia.ap;
  1792. struct folio **folios;
  1793. struct fuse_folio_desc *descs;
  1794. unsigned int nfolios = min_t(unsigned int,
  1795. max_t(unsigned int, data->max_folios * 2,
  1796. FUSE_DEFAULT_MAX_PAGES_PER_REQ),
  1797. max_pages);
  1798. WARN_ON(nfolios <= data->max_folios);
  1799. folios = fuse_folios_alloc(nfolios, GFP_NOFS, &descs);
  1800. if (!folios)
  1801. return false;
  1802. memcpy(folios, ap->folios, sizeof(struct folio *) * ap->num_folios);
  1803. memcpy(descs, ap->descs, sizeof(struct fuse_folio_desc) * ap->num_folios);
  1804. kfree(ap->folios);
  1805. ap->folios = folios;
  1806. ap->descs = descs;
  1807. data->max_folios = nfolios;
  1808. return true;
  1809. }
  1810. static void fuse_writepages_send(struct inode *inode,
  1811. struct fuse_fill_wb_data *data)
  1812. {
  1813. struct fuse_writepage_args *wpa = data->wpa;
  1814. struct fuse_inode *fi = get_fuse_inode(inode);
  1815. spin_lock(&fi->lock);
  1816. list_add_tail(&wpa->queue_entry, &fi->queued_writes);
  1817. fuse_flush_writepages(inode);
  1818. spin_unlock(&fi->lock);
  1819. }
  1820. static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos,
  1821. unsigned len, struct fuse_args_pages *ap,
  1822. unsigned cur_bytes, bool write)
  1823. {
  1824. struct folio *prev_folio;
  1825. struct fuse_folio_desc prev_desc;
  1826. unsigned bytes = cur_bytes + len;
  1827. loff_t prev_pos;
  1828. size_t max_bytes = write ? fc->max_write : fc->max_read;
  1829. WARN_ON(!ap->num_folios);
  1830. /* Reached max pages */
  1831. if ((bytes + PAGE_SIZE - 1) >> PAGE_SHIFT > fc->max_pages)
  1832. return true;
  1833. if (bytes > max_bytes)
  1834. return true;
  1835. /* Discontinuity */
  1836. prev_folio = ap->folios[ap->num_folios - 1];
  1837. prev_desc = ap->descs[ap->num_folios - 1];
  1838. prev_pos = folio_pos(prev_folio) + prev_desc.offset + prev_desc.length;
  1839. if (prev_pos != pos)
  1840. return true;
  1841. return false;
  1842. }
  1843. static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc,
  1844. struct folio *folio, u64 pos,
  1845. unsigned len, u64 end_pos)
  1846. {
  1847. struct fuse_fill_wb_data *data = wpc->wb_ctx;
  1848. struct fuse_writepage_args *wpa = data->wpa;
  1849. struct fuse_args_pages *ap = &wpa->ia.ap;
  1850. struct inode *inode = wpc->inode;
  1851. struct fuse_inode *fi = get_fuse_inode(inode);
  1852. struct fuse_conn *fc = get_fuse_conn(inode);
  1853. loff_t offset = offset_in_folio(folio, pos);
  1854. WARN_ON_ONCE(!data);
  1855. if (!data->ff) {
  1856. data->ff = fuse_write_file_get(fi);
  1857. if (!data->ff)
  1858. return -EIO;
  1859. }
  1860. if (wpa) {
  1861. bool send = fuse_folios_need_send(fc, pos, len, ap,
  1862. data->nr_bytes, true);
  1863. if (!send) {
  1864. /*
  1865. * Need to grow the pages array? If so, did the
  1866. * expansion fail?
  1867. */
  1868. send = (ap->num_folios == data->max_folios) &&
  1869. !fuse_pages_realloc(data, fc->max_pages);
  1870. }
  1871. if (send) {
  1872. fuse_writepages_send(inode, data);
  1873. data->wpa = NULL;
  1874. data->nr_bytes = 0;
  1875. }
  1876. }
  1877. if (data->wpa == NULL) {
  1878. wpa = fuse_writepage_args_setup(folio, offset, data->ff);
  1879. if (!wpa)
  1880. return -ENOMEM;
  1881. fuse_file_get(wpa->ia.ff);
  1882. data->max_folios = 1;
  1883. ap = &wpa->ia.ap;
  1884. }
  1885. fuse_writepage_args_page_fill(wpa, folio, ap->num_folios,
  1886. offset, len);
  1887. data->nr_bytes += len;
  1888. ap->num_folios++;
  1889. if (!data->wpa)
  1890. data->wpa = wpa;
  1891. return len;
  1892. }
  1893. static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc,
  1894. int error)
  1895. {
  1896. struct fuse_fill_wb_data *data = wpc->wb_ctx;
  1897. WARN_ON_ONCE(!data);
  1898. if (data->wpa) {
  1899. WARN_ON(!data->wpa->ia.ap.num_folios);
  1900. fuse_writepages_send(wpc->inode, data);
  1901. }
  1902. if (data->ff)
  1903. fuse_file_put(data->ff, false);
  1904. return error;
  1905. }
  1906. static const struct iomap_writeback_ops fuse_writeback_ops = {
  1907. .writeback_range = fuse_iomap_writeback_range,
  1908. .writeback_submit = fuse_iomap_writeback_submit,
  1909. };
  1910. static int fuse_writepages(struct address_space *mapping,
  1911. struct writeback_control *wbc)
  1912. {
  1913. struct inode *inode = mapping->host;
  1914. struct fuse_conn *fc = get_fuse_conn(inode);
  1915. struct fuse_fill_wb_data data = {};
  1916. struct iomap_writepage_ctx wpc = {
  1917. .inode = inode,
  1918. .iomap.type = IOMAP_MAPPED,
  1919. .wbc = wbc,
  1920. .ops = &fuse_writeback_ops,
  1921. .wb_ctx = &data,
  1922. };
  1923. if (fuse_is_bad(inode))
  1924. return -EIO;
  1925. if (wbc->sync_mode == WB_SYNC_NONE &&
  1926. fc->num_background >= fc->congestion_threshold)
  1927. return 0;
  1928. return iomap_writepages(&wpc);
  1929. }
  1930. static int fuse_launder_folio(struct folio *folio)
  1931. {
  1932. int err = 0;
  1933. struct fuse_fill_wb_data data = {};
  1934. struct iomap_writepage_ctx wpc = {
  1935. .inode = folio->mapping->host,
  1936. .iomap.type = IOMAP_MAPPED,
  1937. .ops = &fuse_writeback_ops,
  1938. .wb_ctx = &data,
  1939. };
  1940. if (folio_clear_dirty_for_io(folio)) {
  1941. err = iomap_writeback_folio(&wpc, folio);
  1942. err = fuse_iomap_writeback_submit(&wpc, err);
  1943. if (!err)
  1944. folio_wait_writeback(folio);
  1945. }
  1946. return err;
  1947. }
  1948. /*
  1949. * Write back dirty data/metadata now (there may not be any suitable
  1950. * open files later for data)
  1951. */
  1952. static void fuse_vma_close(struct vm_area_struct *vma)
  1953. {
  1954. int err;
  1955. err = write_inode_now(vma->vm_file->f_mapping->host, 1);
  1956. mapping_set_error(vma->vm_file->f_mapping, err);
  1957. }
  1958. /*
  1959. * Wait for writeback against this page to complete before allowing it
  1960. * to be marked dirty again, and hence written back again, possibly
  1961. * before the previous writepage completed.
  1962. *
  1963. * Block here, instead of in ->writepage(), so that the userspace fs
  1964. * can only block processes actually operating on the filesystem.
  1965. *
  1966. * Otherwise unprivileged userspace fs would be able to block
  1967. * unrelated:
  1968. *
  1969. * - page migration
  1970. * - sync(2)
  1971. * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
  1972. */
  1973. static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
  1974. {
  1975. struct folio *folio = page_folio(vmf->page);
  1976. struct inode *inode = file_inode(vmf->vma->vm_file);
  1977. file_update_time(vmf->vma->vm_file);
  1978. folio_lock(folio);
  1979. if (folio->mapping != inode->i_mapping) {
  1980. folio_unlock(folio);
  1981. return VM_FAULT_NOPAGE;
  1982. }
  1983. folio_wait_writeback(folio);
  1984. return VM_FAULT_LOCKED;
  1985. }
  1986. static const struct vm_operations_struct fuse_file_vm_ops = {
  1987. .close = fuse_vma_close,
  1988. .fault = filemap_fault,
  1989. .map_pages = filemap_map_pages,
  1990. .page_mkwrite = fuse_page_mkwrite,
  1991. };
  1992. static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
  1993. {
  1994. struct fuse_file *ff = file->private_data;
  1995. struct fuse_conn *fc = ff->fm->fc;
  1996. struct inode *inode = file_inode(file);
  1997. int rc;
  1998. /* DAX mmap is superior to direct_io mmap */
  1999. if (FUSE_IS_DAX(inode))
  2000. return fuse_dax_mmap(file, vma);
  2001. /*
  2002. * If inode is in passthrough io mode, because it has some file open
  2003. * in passthrough mode, either mmap to backing file or fail mmap,
  2004. * because mixing cached mmap and passthrough io mode is not allowed.
  2005. */
  2006. if (fuse_file_passthrough(ff))
  2007. return fuse_passthrough_mmap(file, vma);
  2008. else if (fuse_inode_backing(get_fuse_inode(inode)))
  2009. return -ENODEV;
  2010. /*
  2011. * FOPEN_DIRECT_IO handling is special compared to O_DIRECT,
  2012. * as does not allow MAP_SHARED mmap without FUSE_DIRECT_IO_ALLOW_MMAP.
  2013. */
  2014. if (ff->open_flags & FOPEN_DIRECT_IO) {
  2015. /*
  2016. * Can't provide the coherency needed for MAP_SHARED
  2017. * if FUSE_DIRECT_IO_ALLOW_MMAP isn't set.
  2018. */
  2019. if ((vma->vm_flags & VM_MAYSHARE) && !fc->direct_io_allow_mmap)
  2020. return -ENODEV;
  2021. invalidate_inode_pages2(file->f_mapping);
  2022. if (!(vma->vm_flags & VM_MAYSHARE)) {
  2023. /* MAP_PRIVATE */
  2024. return generic_file_mmap(file, vma);
  2025. }
  2026. /*
  2027. * First mmap of direct_io file enters caching inode io mode.
  2028. * Also waits for parallel dio writers to go into serial mode
  2029. * (exclusive instead of shared lock).
  2030. * After first mmap, the inode stays in caching io mode until
  2031. * the direct_io file release.
  2032. */
  2033. rc = fuse_file_cached_io_open(inode, ff);
  2034. if (rc)
  2035. return rc;
  2036. }
  2037. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  2038. fuse_link_write_file(file);
  2039. file_accessed(file);
  2040. vma->vm_ops = &fuse_file_vm_ops;
  2041. return 0;
  2042. }
  2043. static int convert_fuse_file_lock(struct fuse_conn *fc,
  2044. const struct fuse_file_lock *ffl,
  2045. struct file_lock *fl)
  2046. {
  2047. switch (ffl->type) {
  2048. case F_UNLCK:
  2049. break;
  2050. case F_RDLCK:
  2051. case F_WRLCK:
  2052. if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
  2053. ffl->end < ffl->start)
  2054. return -EIO;
  2055. fl->fl_start = ffl->start;
  2056. fl->fl_end = ffl->end;
  2057. /*
  2058. * Convert pid into init's pid namespace. The locks API will
  2059. * translate it into the caller's pid namespace.
  2060. */
  2061. rcu_read_lock();
  2062. fl->c.flc_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
  2063. rcu_read_unlock();
  2064. break;
  2065. default:
  2066. return -EIO;
  2067. }
  2068. fl->c.flc_type = ffl->type;
  2069. return 0;
  2070. }
  2071. static void fuse_lk_fill(struct fuse_args *args, struct file *file,
  2072. const struct file_lock *fl, int opcode, pid_t pid,
  2073. int flock, struct fuse_lk_in *inarg)
  2074. {
  2075. struct inode *inode = file_inode(file);
  2076. struct fuse_conn *fc = get_fuse_conn(inode);
  2077. struct fuse_file *ff = file->private_data;
  2078. memset(inarg, 0, sizeof(*inarg));
  2079. inarg->fh = ff->fh;
  2080. inarg->owner = fuse_lock_owner_id(fc, fl->c.flc_owner);
  2081. inarg->lk.start = fl->fl_start;
  2082. inarg->lk.end = fl->fl_end;
  2083. inarg->lk.type = fl->c.flc_type;
  2084. inarg->lk.pid = pid;
  2085. if (flock)
  2086. inarg->lk_flags |= FUSE_LK_FLOCK;
  2087. args->opcode = opcode;
  2088. args->nodeid = get_node_id(inode);
  2089. args->in_numargs = 1;
  2090. args->in_args[0].size = sizeof(*inarg);
  2091. args->in_args[0].value = inarg;
  2092. }
  2093. static int fuse_getlk(struct file *file, struct file_lock *fl)
  2094. {
  2095. struct inode *inode = file_inode(file);
  2096. struct fuse_mount *fm = get_fuse_mount(inode);
  2097. FUSE_ARGS(args);
  2098. struct fuse_lk_in inarg;
  2099. struct fuse_lk_out outarg;
  2100. int err;
  2101. fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
  2102. args.out_numargs = 1;
  2103. args.out_args[0].size = sizeof(outarg);
  2104. args.out_args[0].value = &outarg;
  2105. err = fuse_simple_request(fm, &args);
  2106. if (!err)
  2107. err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
  2108. return err;
  2109. }
  2110. static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
  2111. {
  2112. struct inode *inode = file_inode(file);
  2113. struct fuse_mount *fm = get_fuse_mount(inode);
  2114. FUSE_ARGS(args);
  2115. struct fuse_lk_in inarg;
  2116. int opcode = (fl->c.flc_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
  2117. struct pid *pid = fl->c.flc_type != F_UNLCK ? task_tgid(current) : NULL;
  2118. pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
  2119. int err;
  2120. if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
  2121. /* NLM needs asynchronous locks, which we don't support yet */
  2122. return -ENOLCK;
  2123. }
  2124. fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
  2125. err = fuse_simple_request(fm, &args);
  2126. /* locking is restartable */
  2127. if (err == -EINTR)
  2128. err = -ERESTARTSYS;
  2129. return err;
  2130. }
  2131. static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
  2132. {
  2133. struct inode *inode = file_inode(file);
  2134. struct fuse_conn *fc = get_fuse_conn(inode);
  2135. int err;
  2136. if (cmd == F_CANCELLK) {
  2137. err = 0;
  2138. } else if (cmd == F_GETLK) {
  2139. if (fc->no_lock) {
  2140. posix_test_lock(file, fl);
  2141. err = 0;
  2142. } else
  2143. err = fuse_getlk(file, fl);
  2144. } else {
  2145. if (fc->no_lock)
  2146. err = posix_lock_file(file, fl, NULL);
  2147. else
  2148. err = fuse_setlk(file, fl, 0);
  2149. }
  2150. return err;
  2151. }
  2152. static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
  2153. {
  2154. struct inode *inode = file_inode(file);
  2155. struct fuse_conn *fc = get_fuse_conn(inode);
  2156. int err;
  2157. if (fc->no_flock) {
  2158. err = locks_lock_file_wait(file, fl);
  2159. } else {
  2160. struct fuse_file *ff = file->private_data;
  2161. /* emulate flock with POSIX locks */
  2162. ff->flock = true;
  2163. err = fuse_setlk(file, fl, 1);
  2164. }
  2165. return err;
  2166. }
  2167. static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
  2168. {
  2169. struct inode *inode = mapping->host;
  2170. struct fuse_mount *fm = get_fuse_mount(inode);
  2171. FUSE_ARGS(args);
  2172. struct fuse_bmap_in inarg;
  2173. struct fuse_bmap_out outarg;
  2174. int err;
  2175. if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
  2176. return 0;
  2177. memset(&inarg, 0, sizeof(inarg));
  2178. inarg.block = block;
  2179. inarg.blocksize = inode->i_sb->s_blocksize;
  2180. args.opcode = FUSE_BMAP;
  2181. args.nodeid = get_node_id(inode);
  2182. args.in_numargs = 1;
  2183. args.in_args[0].size = sizeof(inarg);
  2184. args.in_args[0].value = &inarg;
  2185. args.out_numargs = 1;
  2186. args.out_args[0].size = sizeof(outarg);
  2187. args.out_args[0].value = &outarg;
  2188. err = fuse_simple_request(fm, &args);
  2189. if (err == -ENOSYS)
  2190. fm->fc->no_bmap = 1;
  2191. return err ? 0 : outarg.block;
  2192. }
  2193. static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
  2194. {
  2195. struct inode *inode = file->f_mapping->host;
  2196. struct fuse_mount *fm = get_fuse_mount(inode);
  2197. struct fuse_file *ff = file->private_data;
  2198. FUSE_ARGS(args);
  2199. struct fuse_lseek_in inarg = {
  2200. .fh = ff->fh,
  2201. .offset = offset,
  2202. .whence = whence
  2203. };
  2204. struct fuse_lseek_out outarg;
  2205. int err;
  2206. if (fm->fc->no_lseek)
  2207. goto fallback;
  2208. args.opcode = FUSE_LSEEK;
  2209. args.nodeid = ff->nodeid;
  2210. args.in_numargs = 1;
  2211. args.in_args[0].size = sizeof(inarg);
  2212. args.in_args[0].value = &inarg;
  2213. args.out_numargs = 1;
  2214. args.out_args[0].size = sizeof(outarg);
  2215. args.out_args[0].value = &outarg;
  2216. err = fuse_simple_request(fm, &args);
  2217. if (err) {
  2218. if (err == -ENOSYS) {
  2219. fm->fc->no_lseek = 1;
  2220. goto fallback;
  2221. }
  2222. return err;
  2223. }
  2224. return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
  2225. fallback:
  2226. err = fuse_update_attributes(inode, file, STATX_SIZE);
  2227. if (!err)
  2228. return generic_file_llseek(file, offset, whence);
  2229. else
  2230. return err;
  2231. }
  2232. static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
  2233. {
  2234. loff_t retval;
  2235. struct inode *inode = file_inode(file);
  2236. switch (whence) {
  2237. case SEEK_SET:
  2238. case SEEK_CUR:
  2239. /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
  2240. retval = generic_file_llseek(file, offset, whence);
  2241. break;
  2242. case SEEK_END:
  2243. inode_lock(inode);
  2244. retval = fuse_update_attributes(inode, file, STATX_SIZE);
  2245. if (!retval)
  2246. retval = generic_file_llseek(file, offset, whence);
  2247. inode_unlock(inode);
  2248. break;
  2249. case SEEK_HOLE:
  2250. case SEEK_DATA:
  2251. inode_lock(inode);
  2252. retval = fuse_lseek(file, offset, whence);
  2253. inode_unlock(inode);
  2254. break;
  2255. default:
  2256. retval = -EINVAL;
  2257. }
  2258. return retval;
  2259. }
  2260. /*
  2261. * All files which have been polled are linked to RB tree
  2262. * fuse_conn->polled_files which is indexed by kh. Walk the tree and
  2263. * find the matching one.
  2264. */
  2265. static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
  2266. struct rb_node **parent_out)
  2267. {
  2268. struct rb_node **link = &fc->polled_files.rb_node;
  2269. struct rb_node *last = NULL;
  2270. while (*link) {
  2271. struct fuse_file *ff;
  2272. last = *link;
  2273. ff = rb_entry(last, struct fuse_file, polled_node);
  2274. if (kh < ff->kh)
  2275. link = &last->rb_left;
  2276. else if (kh > ff->kh)
  2277. link = &last->rb_right;
  2278. else
  2279. return link;
  2280. }
  2281. if (parent_out)
  2282. *parent_out = last;
  2283. return link;
  2284. }
  2285. /*
  2286. * The file is about to be polled. Make sure it's on the polled_files
  2287. * RB tree. Note that files once added to the polled_files tree are
  2288. * not removed before the file is released. This is because a file
  2289. * polled once is likely to be polled again.
  2290. */
  2291. static void fuse_register_polled_file(struct fuse_conn *fc,
  2292. struct fuse_file *ff)
  2293. {
  2294. spin_lock(&fc->lock);
  2295. if (RB_EMPTY_NODE(&ff->polled_node)) {
  2296. struct rb_node **link, *parent;
  2297. link = fuse_find_polled_node(fc, ff->kh, &parent);
  2298. BUG_ON(*link);
  2299. rb_link_node(&ff->polled_node, parent, link);
  2300. rb_insert_color(&ff->polled_node, &fc->polled_files);
  2301. }
  2302. spin_unlock(&fc->lock);
  2303. }
  2304. __poll_t fuse_file_poll(struct file *file, poll_table *wait)
  2305. {
  2306. struct fuse_file *ff = file->private_data;
  2307. struct fuse_mount *fm = ff->fm;
  2308. struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
  2309. struct fuse_poll_out outarg;
  2310. FUSE_ARGS(args);
  2311. int err;
  2312. if (fm->fc->no_poll)
  2313. return DEFAULT_POLLMASK;
  2314. poll_wait(file, &ff->poll_wait, wait);
  2315. inarg.events = mangle_poll(poll_requested_events(wait));
  2316. /*
  2317. * Ask for notification iff there's someone waiting for it.
  2318. * The client may ignore the flag and always notify.
  2319. */
  2320. if (waitqueue_active(&ff->poll_wait)) {
  2321. inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
  2322. fuse_register_polled_file(fm->fc, ff);
  2323. }
  2324. args.opcode = FUSE_POLL;
  2325. args.nodeid = ff->nodeid;
  2326. args.in_numargs = 1;
  2327. args.in_args[0].size = sizeof(inarg);
  2328. args.in_args[0].value = &inarg;
  2329. args.out_numargs = 1;
  2330. args.out_args[0].size = sizeof(outarg);
  2331. args.out_args[0].value = &outarg;
  2332. err = fuse_simple_request(fm, &args);
  2333. if (!err)
  2334. return demangle_poll(outarg.revents);
  2335. if (err == -ENOSYS) {
  2336. fm->fc->no_poll = 1;
  2337. return DEFAULT_POLLMASK;
  2338. }
  2339. return EPOLLERR;
  2340. }
  2341. EXPORT_SYMBOL_GPL(fuse_file_poll);
  2342. /*
  2343. * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
  2344. * wakes up the poll waiters.
  2345. */
  2346. int fuse_notify_poll_wakeup(struct fuse_conn *fc,
  2347. struct fuse_notify_poll_wakeup_out *outarg)
  2348. {
  2349. u64 kh = outarg->kh;
  2350. struct rb_node **link;
  2351. spin_lock(&fc->lock);
  2352. link = fuse_find_polled_node(fc, kh, NULL);
  2353. if (*link) {
  2354. struct fuse_file *ff;
  2355. ff = rb_entry(*link, struct fuse_file, polled_node);
  2356. wake_up_interruptible_sync(&ff->poll_wait);
  2357. }
  2358. spin_unlock(&fc->lock);
  2359. return 0;
  2360. }
  2361. static void fuse_do_truncate(struct file *file)
  2362. {
  2363. struct inode *inode = file->f_mapping->host;
  2364. struct iattr attr;
  2365. attr.ia_valid = ATTR_SIZE;
  2366. attr.ia_size = i_size_read(inode);
  2367. attr.ia_file = file;
  2368. attr.ia_valid |= ATTR_FILE;
  2369. fuse_do_setattr(file_mnt_idmap(file), file_dentry(file), &attr, file);
  2370. }
  2371. static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
  2372. {
  2373. return round_up(off, fc->max_pages << PAGE_SHIFT);
  2374. }
  2375. static ssize_t
  2376. fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  2377. {
  2378. DECLARE_COMPLETION_ONSTACK(wait);
  2379. ssize_t ret = 0;
  2380. struct file *file = iocb->ki_filp;
  2381. struct fuse_file *ff = file->private_data;
  2382. loff_t pos = 0;
  2383. struct inode *inode;
  2384. loff_t i_size;
  2385. size_t count = iov_iter_count(iter), shortened = 0;
  2386. loff_t offset = iocb->ki_pos;
  2387. struct fuse_io_priv *io;
  2388. pos = offset;
  2389. inode = file->f_mapping->host;
  2390. i_size = i_size_read(inode);
  2391. if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
  2392. return 0;
  2393. io = kmalloc_obj(struct fuse_io_priv);
  2394. if (!io)
  2395. return -ENOMEM;
  2396. spin_lock_init(&io->lock);
  2397. kref_init(&io->refcnt);
  2398. io->reqs = 1;
  2399. io->bytes = -1;
  2400. io->size = 0;
  2401. io->offset = offset;
  2402. io->write = (iov_iter_rw(iter) == WRITE);
  2403. io->err = 0;
  2404. /*
  2405. * By default, we want to optimize all I/Os with async request
  2406. * submission to the client filesystem if supported.
  2407. */
  2408. io->async = ff->fm->fc->async_dio;
  2409. io->iocb = iocb;
  2410. io->blocking = is_sync_kiocb(iocb);
  2411. /* optimization for short read */
  2412. if (io->async && !io->write && offset + count > i_size) {
  2413. iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
  2414. shortened = count - iov_iter_count(iter);
  2415. count -= shortened;
  2416. }
  2417. /*
  2418. * We cannot asynchronously extend the size of a file.
  2419. * In such case the aio will behave exactly like sync io.
  2420. */
  2421. if ((offset + count > i_size) && io->write)
  2422. io->blocking = true;
  2423. if (io->async && io->blocking) {
  2424. /*
  2425. * Additional reference to keep io around after
  2426. * calling fuse_aio_complete()
  2427. */
  2428. kref_get(&io->refcnt);
  2429. io->done = &wait;
  2430. }
  2431. if (iov_iter_rw(iter) == WRITE) {
  2432. ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
  2433. fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
  2434. } else {
  2435. ret = __fuse_direct_read(io, iter, &pos);
  2436. }
  2437. iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
  2438. if (io->async) {
  2439. bool blocking = io->blocking;
  2440. fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
  2441. /* we have a non-extending, async request, so return */
  2442. if (!blocking)
  2443. return -EIOCBQUEUED;
  2444. wait_for_completion(&wait);
  2445. ret = fuse_get_res_by_io(io);
  2446. }
  2447. kref_put(&io->refcnt, fuse_io_release);
  2448. if (iov_iter_rw(iter) == WRITE) {
  2449. fuse_write_update_attr(inode, pos, ret);
  2450. /* For extending writes we already hold exclusive lock */
  2451. if (ret < 0 && offset + count > i_size)
  2452. fuse_do_truncate(file);
  2453. }
  2454. return ret;
  2455. }
  2456. static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
  2457. {
  2458. int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX);
  2459. if (!err)
  2460. fuse_sync_writes(inode);
  2461. return err;
  2462. }
  2463. static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
  2464. loff_t length)
  2465. {
  2466. struct fuse_file *ff = file->private_data;
  2467. struct inode *inode = file_inode(file);
  2468. struct fuse_inode *fi = get_fuse_inode(inode);
  2469. struct fuse_mount *fm = ff->fm;
  2470. FUSE_ARGS(args);
  2471. struct fuse_fallocate_in inarg = {
  2472. .fh = ff->fh,
  2473. .offset = offset,
  2474. .length = length,
  2475. .mode = mode
  2476. };
  2477. int err;
  2478. bool block_faults = FUSE_IS_DAX(inode) &&
  2479. (!(mode & FALLOC_FL_KEEP_SIZE) ||
  2480. (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)));
  2481. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  2482. FALLOC_FL_ZERO_RANGE))
  2483. return -EOPNOTSUPP;
  2484. if (fm->fc->no_fallocate)
  2485. return -EOPNOTSUPP;
  2486. inode_lock(inode);
  2487. if (block_faults) {
  2488. filemap_invalidate_lock(inode->i_mapping);
  2489. err = fuse_dax_break_layouts(inode, 0, -1);
  2490. if (err)
  2491. goto out;
  2492. }
  2493. if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) {
  2494. loff_t endbyte = offset + length - 1;
  2495. err = fuse_writeback_range(inode, offset, endbyte);
  2496. if (err)
  2497. goto out;
  2498. }
  2499. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  2500. offset + length > i_size_read(inode)) {
  2501. err = inode_newsize_ok(inode, offset + length);
  2502. if (err)
  2503. goto out;
  2504. }
  2505. err = file_modified(file);
  2506. if (err)
  2507. goto out;
  2508. if (!(mode & FALLOC_FL_KEEP_SIZE))
  2509. set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  2510. args.opcode = FUSE_FALLOCATE;
  2511. args.nodeid = ff->nodeid;
  2512. args.in_numargs = 1;
  2513. args.in_args[0].size = sizeof(inarg);
  2514. args.in_args[0].value = &inarg;
  2515. err = fuse_simple_request(fm, &args);
  2516. if (err == -ENOSYS) {
  2517. fm->fc->no_fallocate = 1;
  2518. err = -EOPNOTSUPP;
  2519. }
  2520. if (err)
  2521. goto out;
  2522. /* we could have extended the file */
  2523. if (!(mode & FALLOC_FL_KEEP_SIZE)) {
  2524. if (fuse_write_update_attr(inode, offset + length, length))
  2525. file_update_time(file);
  2526. }
  2527. if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
  2528. truncate_pagecache_range(inode, offset, offset + length - 1);
  2529. fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
  2530. out:
  2531. if (!(mode & FALLOC_FL_KEEP_SIZE))
  2532. clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  2533. if (block_faults)
  2534. filemap_invalidate_unlock(inode->i_mapping);
  2535. inode_unlock(inode);
  2536. fuse_flush_time_update(inode);
  2537. return err;
  2538. }
  2539. static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
  2540. struct file *file_out, loff_t pos_out,
  2541. size_t len, unsigned int flags)
  2542. {
  2543. struct fuse_file *ff_in = file_in->private_data;
  2544. struct fuse_file *ff_out = file_out->private_data;
  2545. struct inode *inode_in = file_inode(file_in);
  2546. struct inode *inode_out = file_inode(file_out);
  2547. struct fuse_inode *fi_out = get_fuse_inode(inode_out);
  2548. struct fuse_mount *fm = ff_in->fm;
  2549. struct fuse_conn *fc = fm->fc;
  2550. FUSE_ARGS(args);
  2551. struct fuse_copy_file_range_in inarg = {
  2552. .fh_in = ff_in->fh,
  2553. .off_in = pos_in,
  2554. .nodeid_out = ff_out->nodeid,
  2555. .fh_out = ff_out->fh,
  2556. .off_out = pos_out,
  2557. .len = len,
  2558. .flags = flags
  2559. };
  2560. struct fuse_write_out outarg;
  2561. struct fuse_copy_file_range_out outarg_64;
  2562. u64 bytes_copied;
  2563. ssize_t err;
  2564. /* mark unstable when write-back is not used, and file_out gets
  2565. * extended */
  2566. bool is_unstable = (!fc->writeback_cache) &&
  2567. ((pos_out + len) > inode_out->i_size);
  2568. if (fc->no_copy_file_range)
  2569. return -EOPNOTSUPP;
  2570. if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
  2571. return -EXDEV;
  2572. inode_lock(inode_in);
  2573. err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
  2574. inode_unlock(inode_in);
  2575. if (err)
  2576. return err;
  2577. inode_lock(inode_out);
  2578. err = file_modified(file_out);
  2579. if (err)
  2580. goto out;
  2581. /*
  2582. * Write out dirty pages in the destination file before sending the COPY
  2583. * request to userspace. After the request is completed, truncate off
  2584. * pages (including partial ones) from the cache that have been copied,
  2585. * since these contain stale data at that point.
  2586. *
  2587. * This should be mostly correct, but if the COPY writes to partial
  2588. * pages (at the start or end) and the parts not covered by the COPY are
  2589. * written through a memory map after calling fuse_writeback_range(),
  2590. * then these partial page modifications will be lost on truncation.
  2591. *
  2592. * It is unlikely that someone would rely on such mixed style
  2593. * modifications. Yet this does give less guarantees than if the
  2594. * copying was performed with write(2).
  2595. *
  2596. * To fix this a mapping->invalidate_lock could be used to prevent new
  2597. * faults while the copy is ongoing.
  2598. */
  2599. err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
  2600. if (err)
  2601. goto out;
  2602. if (is_unstable)
  2603. set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
  2604. args.opcode = FUSE_COPY_FILE_RANGE_64;
  2605. args.nodeid = ff_in->nodeid;
  2606. args.in_numargs = 1;
  2607. args.in_args[0].size = sizeof(inarg);
  2608. args.in_args[0].value = &inarg;
  2609. args.out_numargs = 1;
  2610. args.out_args[0].size = sizeof(outarg_64);
  2611. args.out_args[0].value = &outarg_64;
  2612. if (fc->no_copy_file_range_64) {
  2613. fallback:
  2614. /* Fall back to old op that can't handle large copy length */
  2615. args.opcode = FUSE_COPY_FILE_RANGE;
  2616. args.out_args[0].size = sizeof(outarg);
  2617. args.out_args[0].value = &outarg;
  2618. inarg.len = len = min_t(size_t, len, UINT_MAX & PAGE_MASK);
  2619. }
  2620. err = fuse_simple_request(fm, &args);
  2621. if (err == -ENOSYS) {
  2622. if (fc->no_copy_file_range_64) {
  2623. fc->no_copy_file_range = 1;
  2624. err = -EOPNOTSUPP;
  2625. } else {
  2626. fc->no_copy_file_range_64 = 1;
  2627. goto fallback;
  2628. }
  2629. }
  2630. if (err)
  2631. goto out;
  2632. bytes_copied = fc->no_copy_file_range_64 ?
  2633. outarg.size : outarg_64.bytes_copied;
  2634. if (bytes_copied > len) {
  2635. err = -EIO;
  2636. goto out;
  2637. }
  2638. truncate_inode_pages_range(inode_out->i_mapping,
  2639. ALIGN_DOWN(pos_out, PAGE_SIZE),
  2640. ALIGN(pos_out + bytes_copied, PAGE_SIZE) - 1);
  2641. file_update_time(file_out);
  2642. fuse_write_update_attr(inode_out, pos_out + bytes_copied, bytes_copied);
  2643. err = bytes_copied;
  2644. out:
  2645. if (is_unstable)
  2646. clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
  2647. inode_unlock(inode_out);
  2648. file_accessed(file_in);
  2649. fuse_flush_time_update(inode_out);
  2650. return err;
  2651. }
  2652. static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
  2653. struct file *dst_file, loff_t dst_off,
  2654. size_t len, unsigned int flags)
  2655. {
  2656. ssize_t ret;
  2657. ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
  2658. len, flags);
  2659. if (ret == -EOPNOTSUPP || ret == -EXDEV)
  2660. ret = splice_copy_file_range(src_file, src_off, dst_file,
  2661. dst_off, len);
  2662. return ret;
  2663. }
  2664. static const struct file_operations fuse_file_operations = {
  2665. .llseek = fuse_file_llseek,
  2666. .read_iter = fuse_file_read_iter,
  2667. .write_iter = fuse_file_write_iter,
  2668. .mmap = fuse_file_mmap,
  2669. .open = fuse_open,
  2670. .flush = fuse_flush,
  2671. .release = fuse_release,
  2672. .fsync = fuse_fsync,
  2673. .lock = fuse_file_lock,
  2674. .get_unmapped_area = thp_get_unmapped_area,
  2675. .flock = fuse_file_flock,
  2676. .splice_read = fuse_splice_read,
  2677. .splice_write = fuse_splice_write,
  2678. .unlocked_ioctl = fuse_file_ioctl,
  2679. .compat_ioctl = fuse_file_compat_ioctl,
  2680. .poll = fuse_file_poll,
  2681. .fallocate = fuse_file_fallocate,
  2682. .copy_file_range = fuse_copy_file_range,
  2683. .setlease = generic_setlease,
  2684. };
  2685. static const struct address_space_operations fuse_file_aops = {
  2686. .read_folio = fuse_read_folio,
  2687. .readahead = fuse_readahead,
  2688. .writepages = fuse_writepages,
  2689. .launder_folio = fuse_launder_folio,
  2690. .dirty_folio = iomap_dirty_folio,
  2691. .release_folio = iomap_release_folio,
  2692. .invalidate_folio = iomap_invalidate_folio,
  2693. .is_partially_uptodate = iomap_is_partially_uptodate,
  2694. .migrate_folio = filemap_migrate_folio,
  2695. .bmap = fuse_bmap,
  2696. .direct_IO = fuse_direct_IO,
  2697. };
  2698. void fuse_init_file_inode(struct inode *inode, unsigned int flags)
  2699. {
  2700. struct fuse_inode *fi = get_fuse_inode(inode);
  2701. struct fuse_conn *fc = get_fuse_conn(inode);
  2702. inode->i_fop = &fuse_file_operations;
  2703. inode->i_data.a_ops = &fuse_file_aops;
  2704. if (fc->writeback_cache)
  2705. mapping_set_writeback_may_deadlock_on_reclaim(&inode->i_data);
  2706. INIT_LIST_HEAD(&fi->write_files);
  2707. INIT_LIST_HEAD(&fi->queued_writes);
  2708. fi->writectr = 0;
  2709. fi->iocachectr = 0;
  2710. init_waitqueue_head(&fi->page_waitq);
  2711. init_waitqueue_head(&fi->direct_io_waitq);
  2712. if (IS_ENABLED(CONFIG_FUSE_DAX))
  2713. fuse_dax_inode_init(inode, flags);
  2714. }