file.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054
  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/module.h>
  13. #include <linux/compat.h>
  14. #include <linux/swap.h>
  15. #include <linux/falloc.h>
  16. #include <linux/uio.h>
  17. static const struct file_operations fuse_direct_io_file_operations;
  18. static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
  19. int opcode, struct fuse_open_out *outargp)
  20. {
  21. struct fuse_open_in inarg;
  22. FUSE_ARGS(args);
  23. memset(&inarg, 0, sizeof(inarg));
  24. inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
  25. if (!fc->atomic_o_trunc)
  26. inarg.flags &= ~O_TRUNC;
  27. args.in.h.opcode = opcode;
  28. args.in.h.nodeid = nodeid;
  29. args.in.numargs = 1;
  30. args.in.args[0].size = sizeof(inarg);
  31. args.in.args[0].value = &inarg;
  32. args.out.numargs = 1;
  33. args.out.args[0].size = sizeof(*outargp);
  34. args.out.args[0].value = outargp;
  35. return fuse_simple_request(fc, &args);
  36. }
  37. struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
  38. {
  39. struct fuse_file *ff;
  40. ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
  41. if (unlikely(!ff))
  42. return NULL;
  43. ff->fc = fc;
  44. ff->reserved_req = fuse_request_alloc(0);
  45. if (unlikely(!ff->reserved_req)) {
  46. kfree(ff);
  47. return NULL;
  48. }
  49. INIT_LIST_HEAD(&ff->write_entry);
  50. atomic_set(&ff->count, 0);
  51. RB_CLEAR_NODE(&ff->polled_node);
  52. init_waitqueue_head(&ff->poll_wait);
  53. spin_lock(&fc->lock);
  54. ff->kh = ++fc->khctr;
  55. spin_unlock(&fc->lock);
  56. return ff;
  57. }
  58. void fuse_file_free(struct fuse_file *ff)
  59. {
  60. fuse_request_free(ff->reserved_req);
  61. kfree(ff);
  62. }
  63. struct fuse_file *fuse_file_get(struct fuse_file *ff)
  64. {
  65. atomic_inc(&ff->count);
  66. return ff;
  67. }
  68. static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
  69. {
  70. iput(req->misc.release.inode);
  71. }
  72. static void fuse_file_put(struct fuse_file *ff, bool sync)
  73. {
  74. if (atomic_dec_and_test(&ff->count)) {
  75. struct fuse_req *req = ff->reserved_req;
  76. if (ff->fc->no_open) {
  77. /*
  78. * Drop the release request when client does not
  79. * implement 'open'
  80. */
  81. __clear_bit(FR_BACKGROUND, &req->flags);
  82. iput(req->misc.release.inode);
  83. fuse_put_request(ff->fc, req);
  84. } else if (sync) {
  85. __set_bit(FR_FORCE, &req->flags);
  86. __clear_bit(FR_BACKGROUND, &req->flags);
  87. fuse_request_send(ff->fc, req);
  88. iput(req->misc.release.inode);
  89. fuse_put_request(ff->fc, req);
  90. } else {
  91. req->end = fuse_release_end;
  92. __set_bit(FR_BACKGROUND, &req->flags);
  93. fuse_request_send_background(ff->fc, req);
  94. }
  95. kfree(ff);
  96. }
  97. }
  98. int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
  99. bool isdir)
  100. {
  101. struct fuse_file *ff;
  102. int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
  103. ff = fuse_file_alloc(fc);
  104. if (!ff)
  105. return -ENOMEM;
  106. ff->fh = 0;
  107. ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
  108. if (!fc->no_open || isdir) {
  109. struct fuse_open_out outarg;
  110. int err;
  111. err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
  112. if (!err) {
  113. ff->fh = outarg.fh;
  114. ff->open_flags = outarg.open_flags;
  115. } else if (err != -ENOSYS || isdir) {
  116. fuse_file_free(ff);
  117. return err;
  118. } else {
  119. fc->no_open = 1;
  120. }
  121. }
  122. if (isdir)
  123. ff->open_flags &= ~FOPEN_DIRECT_IO;
  124. ff->nodeid = nodeid;
  125. file->private_data = fuse_file_get(ff);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(fuse_do_open);
  129. static void fuse_link_write_file(struct file *file)
  130. {
  131. struct inode *inode = file_inode(file);
  132. struct fuse_conn *fc = get_fuse_conn(inode);
  133. struct fuse_inode *fi = get_fuse_inode(inode);
  134. struct fuse_file *ff = file->private_data;
  135. /*
  136. * file may be written through mmap, so chain it onto the
  137. * inodes's write_file list
  138. */
  139. spin_lock(&fc->lock);
  140. if (list_empty(&ff->write_entry))
  141. list_add(&ff->write_entry, &fi->write_files);
  142. spin_unlock(&fc->lock);
  143. }
  144. void fuse_finish_open(struct inode *inode, struct file *file)
  145. {
  146. struct fuse_file *ff = file->private_data;
  147. struct fuse_conn *fc = get_fuse_conn(inode);
  148. if (ff->open_flags & FOPEN_DIRECT_IO)
  149. file->f_op = &fuse_direct_io_file_operations;
  150. if (!(ff->open_flags & FOPEN_KEEP_CACHE))
  151. invalidate_inode_pages2(inode->i_mapping);
  152. if (ff->open_flags & FOPEN_NONSEEKABLE)
  153. nonseekable_open(inode, file);
  154. if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
  155. struct fuse_inode *fi = get_fuse_inode(inode);
  156. spin_lock(&fc->lock);
  157. fi->attr_version = ++fc->attr_version;
  158. i_size_write(inode, 0);
  159. spin_unlock(&fc->lock);
  160. fuse_invalidate_attr(inode);
  161. if (fc->writeback_cache)
  162. file_update_time(file);
  163. }
  164. if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
  165. fuse_link_write_file(file);
  166. }
  167. int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
  168. {
  169. struct fuse_conn *fc = get_fuse_conn(inode);
  170. int err;
  171. bool lock_inode = (file->f_flags & O_TRUNC) &&
  172. fc->atomic_o_trunc &&
  173. fc->writeback_cache;
  174. err = generic_file_open(inode, file);
  175. if (err)
  176. return err;
  177. if (lock_inode)
  178. inode_lock(inode);
  179. err = fuse_do_open(fc, get_node_id(inode), file, isdir);
  180. if (!err)
  181. fuse_finish_open(inode, file);
  182. if (lock_inode)
  183. inode_unlock(inode);
  184. return err;
  185. }
  186. static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
  187. {
  188. struct fuse_conn *fc = ff->fc;
  189. struct fuse_req *req = ff->reserved_req;
  190. struct fuse_release_in *inarg = &req->misc.release.in;
  191. spin_lock(&fc->lock);
  192. list_del(&ff->write_entry);
  193. if (!RB_EMPTY_NODE(&ff->polled_node))
  194. rb_erase(&ff->polled_node, &fc->polled_files);
  195. spin_unlock(&fc->lock);
  196. wake_up_interruptible_all(&ff->poll_wait);
  197. inarg->fh = ff->fh;
  198. inarg->flags = flags;
  199. req->in.h.opcode = opcode;
  200. req->in.h.nodeid = ff->nodeid;
  201. req->in.numargs = 1;
  202. req->in.args[0].size = sizeof(struct fuse_release_in);
  203. req->in.args[0].value = inarg;
  204. }
  205. void fuse_release_common(struct file *file, int opcode)
  206. {
  207. struct fuse_file *ff;
  208. struct fuse_req *req;
  209. ff = file->private_data;
  210. if (unlikely(!ff))
  211. return;
  212. req = ff->reserved_req;
  213. fuse_prepare_release(ff, file->f_flags, opcode);
  214. if (ff->flock) {
  215. struct fuse_release_in *inarg = &req->misc.release.in;
  216. inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
  217. inarg->lock_owner = fuse_lock_owner_id(ff->fc,
  218. (fl_owner_t) file);
  219. }
  220. /* Hold inode until release is finished */
  221. req->misc.release.inode = igrab(file_inode(file));
  222. /*
  223. * Normally this will send the RELEASE request, however if
  224. * some asynchronous READ or WRITE requests are outstanding,
  225. * the sending will be delayed.
  226. *
  227. * Make the release synchronous if this is a fuseblk mount,
  228. * synchronous RELEASE is allowed (and desirable) in this case
  229. * because the server can be trusted not to screw up.
  230. */
  231. fuse_file_put(ff, ff->fc->destroy_req != NULL);
  232. }
  233. static int fuse_open(struct inode *inode, struct file *file)
  234. {
  235. return fuse_open_common(inode, file, false);
  236. }
  237. static int fuse_release(struct inode *inode, struct file *file)
  238. {
  239. struct fuse_conn *fc = get_fuse_conn(inode);
  240. /* see fuse_vma_close() for !writeback_cache case */
  241. if (fc->writeback_cache)
  242. write_inode_now(inode, 1);
  243. fuse_release_common(file, FUSE_RELEASE);
  244. /* return value is ignored by VFS */
  245. return 0;
  246. }
  247. void fuse_sync_release(struct fuse_file *ff, int flags)
  248. {
  249. WARN_ON(atomic_read(&ff->count) > 1);
  250. fuse_prepare_release(ff, flags, FUSE_RELEASE);
  251. __set_bit(FR_FORCE, &ff->reserved_req->flags);
  252. __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
  253. fuse_request_send(ff->fc, ff->reserved_req);
  254. fuse_put_request(ff->fc, ff->reserved_req);
  255. kfree(ff);
  256. }
  257. EXPORT_SYMBOL_GPL(fuse_sync_release);
  258. /*
  259. * Scramble the ID space with XTEA, so that the value of the files_struct
  260. * pointer is not exposed to userspace.
  261. */
  262. u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
  263. {
  264. u32 *k = fc->scramble_key;
  265. u64 v = (unsigned long) id;
  266. u32 v0 = v;
  267. u32 v1 = v >> 32;
  268. u32 sum = 0;
  269. int i;
  270. for (i = 0; i < 32; i++) {
  271. v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
  272. sum += 0x9E3779B9;
  273. v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
  274. }
  275. return (u64) v0 + ((u64) v1 << 32);
  276. }
  277. /*
  278. * Check if any page in a range is under writeback
  279. *
  280. * This is currently done by walking the list of writepage requests
  281. * for the inode, which can be pretty inefficient.
  282. */
  283. static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
  284. pgoff_t idx_to)
  285. {
  286. struct fuse_conn *fc = get_fuse_conn(inode);
  287. struct fuse_inode *fi = get_fuse_inode(inode);
  288. struct fuse_req *req;
  289. bool found = false;
  290. spin_lock(&fc->lock);
  291. list_for_each_entry(req, &fi->writepages, writepages_entry) {
  292. pgoff_t curr_index;
  293. BUG_ON(req->inode != inode);
  294. curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
  295. if (idx_from < curr_index + req->num_pages &&
  296. curr_index <= idx_to) {
  297. found = true;
  298. break;
  299. }
  300. }
  301. spin_unlock(&fc->lock);
  302. return found;
  303. }
  304. static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
  305. {
  306. return fuse_range_is_writeback(inode, index, index);
  307. }
  308. /*
  309. * Wait for page writeback to be completed.
  310. *
  311. * Since fuse doesn't rely on the VM writeback tracking, this has to
  312. * use some other means.
  313. */
  314. static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
  315. {
  316. struct fuse_inode *fi = get_fuse_inode(inode);
  317. wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
  318. return 0;
  319. }
  320. /*
  321. * Wait for all pending writepages on the inode to finish.
  322. *
  323. * This is currently done by blocking further writes with FUSE_NOWRITE
  324. * and waiting for all sent writes to complete.
  325. *
  326. * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
  327. * could conflict with truncation.
  328. */
  329. static void fuse_sync_writes(struct inode *inode)
  330. {
  331. fuse_set_nowrite(inode);
  332. fuse_release_nowrite(inode);
  333. }
  334. static int fuse_flush(struct file *file, fl_owner_t id)
  335. {
  336. struct inode *inode = file_inode(file);
  337. struct fuse_conn *fc = get_fuse_conn(inode);
  338. struct fuse_file *ff = file->private_data;
  339. struct fuse_req *req;
  340. struct fuse_flush_in inarg;
  341. int err;
  342. if (is_bad_inode(inode))
  343. return -EIO;
  344. if (fc->no_flush)
  345. return 0;
  346. err = write_inode_now(inode, 1);
  347. if (err)
  348. return err;
  349. inode_lock(inode);
  350. fuse_sync_writes(inode);
  351. inode_unlock(inode);
  352. err = filemap_check_errors(file->f_mapping);
  353. if (err)
  354. return err;
  355. req = fuse_get_req_nofail_nopages(fc, file);
  356. memset(&inarg, 0, sizeof(inarg));
  357. inarg.fh = ff->fh;
  358. inarg.lock_owner = fuse_lock_owner_id(fc, id);
  359. req->in.h.opcode = FUSE_FLUSH;
  360. req->in.h.nodeid = get_node_id(inode);
  361. req->in.numargs = 1;
  362. req->in.args[0].size = sizeof(inarg);
  363. req->in.args[0].value = &inarg;
  364. __set_bit(FR_FORCE, &req->flags);
  365. fuse_request_send(fc, req);
  366. err = req->out.h.error;
  367. fuse_put_request(fc, req);
  368. if (err == -ENOSYS) {
  369. fc->no_flush = 1;
  370. err = 0;
  371. }
  372. return err;
  373. }
  374. int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
  375. int datasync, int isdir)
  376. {
  377. struct inode *inode = file->f_mapping->host;
  378. struct fuse_conn *fc = get_fuse_conn(inode);
  379. struct fuse_file *ff = file->private_data;
  380. FUSE_ARGS(args);
  381. struct fuse_fsync_in inarg;
  382. int err;
  383. if (is_bad_inode(inode))
  384. return -EIO;
  385. inode_lock(inode);
  386. /*
  387. * Start writeback against all dirty pages of the inode, then
  388. * wait for all outstanding writes, before sending the FSYNC
  389. * request.
  390. */
  391. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  392. if (err)
  393. goto out;
  394. fuse_sync_writes(inode);
  395. /*
  396. * Due to implementation of fuse writeback
  397. * filemap_write_and_wait_range() does not catch errors.
  398. * We have to do this directly after fuse_sync_writes()
  399. */
  400. err = filemap_check_errors(file->f_mapping);
  401. if (err)
  402. goto out;
  403. err = sync_inode_metadata(inode, 1);
  404. if (err)
  405. goto out;
  406. if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
  407. goto out;
  408. memset(&inarg, 0, sizeof(inarg));
  409. inarg.fh = ff->fh;
  410. inarg.fsync_flags = datasync ? 1 : 0;
  411. args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
  412. args.in.h.nodeid = get_node_id(inode);
  413. args.in.numargs = 1;
  414. args.in.args[0].size = sizeof(inarg);
  415. args.in.args[0].value = &inarg;
  416. err = fuse_simple_request(fc, &args);
  417. if (err == -ENOSYS) {
  418. if (isdir)
  419. fc->no_fsyncdir = 1;
  420. else
  421. fc->no_fsync = 1;
  422. err = 0;
  423. }
  424. out:
  425. inode_unlock(inode);
  426. return err;
  427. }
  428. static int fuse_fsync(struct file *file, loff_t start, loff_t end,
  429. int datasync)
  430. {
  431. return fuse_fsync_common(file, start, end, datasync, 0);
  432. }
  433. void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
  434. size_t count, int opcode)
  435. {
  436. struct fuse_read_in *inarg = &req->misc.read.in;
  437. struct fuse_file *ff = file->private_data;
  438. inarg->fh = ff->fh;
  439. inarg->offset = pos;
  440. inarg->size = count;
  441. inarg->flags = file->f_flags;
  442. req->in.h.opcode = opcode;
  443. req->in.h.nodeid = ff->nodeid;
  444. req->in.numargs = 1;
  445. req->in.args[0].size = sizeof(struct fuse_read_in);
  446. req->in.args[0].value = inarg;
  447. req->out.argvar = 1;
  448. req->out.numargs = 1;
  449. req->out.args[0].size = count;
  450. }
  451. static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
  452. {
  453. unsigned i;
  454. for (i = 0; i < req->num_pages; i++) {
  455. struct page *page = req->pages[i];
  456. if (should_dirty)
  457. set_page_dirty_lock(page);
  458. put_page(page);
  459. }
  460. }
  461. static void fuse_io_release(struct kref *kref)
  462. {
  463. kfree(container_of(kref, struct fuse_io_priv, refcnt));
  464. }
  465. static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
  466. {
  467. if (io->err)
  468. return io->err;
  469. if (io->bytes >= 0 && io->write)
  470. return -EIO;
  471. return io->bytes < 0 ? io->size : io->bytes;
  472. }
  473. /**
  474. * In case of short read, the caller sets 'pos' to the position of
  475. * actual end of fuse request in IO request. Otherwise, if bytes_requested
  476. * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
  477. *
  478. * An example:
  479. * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
  480. * both submitted asynchronously. The first of them was ACKed by userspace as
  481. * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
  482. * second request was ACKed as short, e.g. only 1K was read, resulting in
  483. * pos == 33K.
  484. *
  485. * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
  486. * will be equal to the length of the longest contiguous fragment of
  487. * transferred data starting from the beginning of IO request.
  488. */
  489. static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
  490. {
  491. int left;
  492. spin_lock(&io->lock);
  493. if (err)
  494. io->err = io->err ? : err;
  495. else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
  496. io->bytes = pos;
  497. left = --io->reqs;
  498. if (!left && io->blocking)
  499. complete(io->done);
  500. spin_unlock(&io->lock);
  501. if (!left && !io->blocking) {
  502. ssize_t res = fuse_get_res_by_io(io);
  503. if (res >= 0) {
  504. struct inode *inode = file_inode(io->iocb->ki_filp);
  505. struct fuse_conn *fc = get_fuse_conn(inode);
  506. struct fuse_inode *fi = get_fuse_inode(inode);
  507. spin_lock(&fc->lock);
  508. fi->attr_version = ++fc->attr_version;
  509. spin_unlock(&fc->lock);
  510. }
  511. io->iocb->ki_complete(io->iocb, res, 0);
  512. }
  513. kref_put(&io->refcnt, fuse_io_release);
  514. }
  515. static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
  516. {
  517. struct fuse_io_priv *io = req->io;
  518. ssize_t pos = -1;
  519. fuse_release_user_pages(req, !io->write);
  520. if (io->write) {
  521. if (req->misc.write.in.size != req->misc.write.out.size)
  522. pos = req->misc.write.in.offset - io->offset +
  523. req->misc.write.out.size;
  524. } else {
  525. if (req->misc.read.in.size != req->out.args[0].size)
  526. pos = req->misc.read.in.offset - io->offset +
  527. req->out.args[0].size;
  528. }
  529. fuse_aio_complete(io, req->out.h.error, pos);
  530. }
  531. static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
  532. size_t num_bytes, struct fuse_io_priv *io)
  533. {
  534. spin_lock(&io->lock);
  535. kref_get(&io->refcnt);
  536. io->size += num_bytes;
  537. io->reqs++;
  538. spin_unlock(&io->lock);
  539. req->io = io;
  540. req->end = fuse_aio_complete_req;
  541. __fuse_get_request(req);
  542. fuse_request_send_background(fc, req);
  543. return num_bytes;
  544. }
  545. static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
  546. loff_t pos, size_t count, fl_owner_t owner)
  547. {
  548. struct file *file = io->file;
  549. struct fuse_file *ff = file->private_data;
  550. struct fuse_conn *fc = ff->fc;
  551. fuse_read_fill(req, file, pos, count, FUSE_READ);
  552. if (owner != NULL) {
  553. struct fuse_read_in *inarg = &req->misc.read.in;
  554. inarg->read_flags |= FUSE_READ_LOCKOWNER;
  555. inarg->lock_owner = fuse_lock_owner_id(fc, owner);
  556. }
  557. if (io->async)
  558. return fuse_async_req_send(fc, req, count, io);
  559. fuse_request_send(fc, req);
  560. return req->out.args[0].size;
  561. }
  562. static void fuse_read_update_size(struct inode *inode, loff_t size,
  563. u64 attr_ver)
  564. {
  565. struct fuse_conn *fc = get_fuse_conn(inode);
  566. struct fuse_inode *fi = get_fuse_inode(inode);
  567. spin_lock(&fc->lock);
  568. if (attr_ver == fi->attr_version && size < inode->i_size &&
  569. !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
  570. fi->attr_version = ++fc->attr_version;
  571. i_size_write(inode, size);
  572. }
  573. spin_unlock(&fc->lock);
  574. }
  575. static void fuse_short_read(struct fuse_req *req, struct inode *inode,
  576. u64 attr_ver)
  577. {
  578. size_t num_read = req->out.args[0].size;
  579. struct fuse_conn *fc = get_fuse_conn(inode);
  580. if (fc->writeback_cache) {
  581. /*
  582. * A hole in a file. Some data after the hole are in page cache,
  583. * but have not reached the client fs yet. So, the hole is not
  584. * present there.
  585. */
  586. int i;
  587. int start_idx = num_read >> PAGE_SHIFT;
  588. size_t off = num_read & (PAGE_SIZE - 1);
  589. for (i = start_idx; i < req->num_pages; i++) {
  590. zero_user_segment(req->pages[i], off, PAGE_SIZE);
  591. off = 0;
  592. }
  593. } else {
  594. loff_t pos = page_offset(req->pages[0]) + num_read;
  595. fuse_read_update_size(inode, pos, attr_ver);
  596. }
  597. }
  598. static int fuse_do_readpage(struct file *file, struct page *page)
  599. {
  600. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
  601. struct inode *inode = page->mapping->host;
  602. struct fuse_conn *fc = get_fuse_conn(inode);
  603. struct fuse_req *req;
  604. size_t num_read;
  605. loff_t pos = page_offset(page);
  606. size_t count = PAGE_SIZE;
  607. u64 attr_ver;
  608. int err;
  609. /*
  610. * Page writeback can extend beyond the lifetime of the
  611. * page-cache page, so make sure we read a properly synced
  612. * page.
  613. */
  614. fuse_wait_on_page_writeback(inode, page->index);
  615. req = fuse_get_req(fc, 1);
  616. if (IS_ERR(req))
  617. return PTR_ERR(req);
  618. attr_ver = fuse_get_attr_version(fc);
  619. req->out.page_zeroing = 1;
  620. req->out.argpages = 1;
  621. req->num_pages = 1;
  622. req->pages[0] = page;
  623. req->page_descs[0].length = count;
  624. num_read = fuse_send_read(req, &io, pos, count, NULL);
  625. err = req->out.h.error;
  626. if (!err) {
  627. /*
  628. * Short read means EOF. If file size is larger, truncate it
  629. */
  630. if (num_read < count)
  631. fuse_short_read(req, inode, attr_ver);
  632. SetPageUptodate(page);
  633. }
  634. fuse_put_request(fc, req);
  635. return err;
  636. }
  637. static int fuse_readpage(struct file *file, struct page *page)
  638. {
  639. struct inode *inode = page->mapping->host;
  640. int err;
  641. err = -EIO;
  642. if (is_bad_inode(inode))
  643. goto out;
  644. err = fuse_do_readpage(file, page);
  645. fuse_invalidate_atime(inode);
  646. out:
  647. unlock_page(page);
  648. return err;
  649. }
  650. static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
  651. {
  652. int i;
  653. size_t count = req->misc.read.in.size;
  654. size_t num_read = req->out.args[0].size;
  655. struct address_space *mapping = NULL;
  656. for (i = 0; mapping == NULL && i < req->num_pages; i++)
  657. mapping = req->pages[i]->mapping;
  658. if (mapping) {
  659. struct inode *inode = mapping->host;
  660. /*
  661. * Short read means EOF. If file size is larger, truncate it
  662. */
  663. if (!req->out.h.error && num_read < count)
  664. fuse_short_read(req, inode, req->misc.read.attr_ver);
  665. fuse_invalidate_atime(inode);
  666. }
  667. for (i = 0; i < req->num_pages; i++) {
  668. struct page *page = req->pages[i];
  669. if (!req->out.h.error)
  670. SetPageUptodate(page);
  671. else
  672. SetPageError(page);
  673. unlock_page(page);
  674. put_page(page);
  675. }
  676. if (req->ff)
  677. fuse_file_put(req->ff, false);
  678. }
  679. static void fuse_send_readpages(struct fuse_req *req, struct file *file)
  680. {
  681. struct fuse_file *ff = file->private_data;
  682. struct fuse_conn *fc = ff->fc;
  683. loff_t pos = page_offset(req->pages[0]);
  684. size_t count = req->num_pages << PAGE_SHIFT;
  685. req->out.argpages = 1;
  686. req->out.page_zeroing = 1;
  687. req->out.page_replace = 1;
  688. fuse_read_fill(req, file, pos, count, FUSE_READ);
  689. req->misc.read.attr_ver = fuse_get_attr_version(fc);
  690. if (fc->async_read) {
  691. req->ff = fuse_file_get(ff);
  692. req->end = fuse_readpages_end;
  693. fuse_request_send_background(fc, req);
  694. } else {
  695. fuse_request_send(fc, req);
  696. fuse_readpages_end(fc, req);
  697. fuse_put_request(fc, req);
  698. }
  699. }
  700. struct fuse_fill_data {
  701. struct fuse_req *req;
  702. struct file *file;
  703. struct inode *inode;
  704. unsigned nr_pages;
  705. };
  706. static int fuse_readpages_fill(void *_data, struct page *page)
  707. {
  708. struct fuse_fill_data *data = _data;
  709. struct fuse_req *req = data->req;
  710. struct inode *inode = data->inode;
  711. struct fuse_conn *fc = get_fuse_conn(inode);
  712. fuse_wait_on_page_writeback(inode, page->index);
  713. if (req->num_pages &&
  714. (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
  715. (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
  716. req->pages[req->num_pages - 1]->index + 1 != page->index)) {
  717. int nr_alloc = min_t(unsigned, data->nr_pages,
  718. FUSE_MAX_PAGES_PER_REQ);
  719. fuse_send_readpages(req, data->file);
  720. if (fc->async_read)
  721. req = fuse_get_req_for_background(fc, nr_alloc);
  722. else
  723. req = fuse_get_req(fc, nr_alloc);
  724. data->req = req;
  725. if (IS_ERR(req)) {
  726. unlock_page(page);
  727. return PTR_ERR(req);
  728. }
  729. }
  730. if (WARN_ON(req->num_pages >= req->max_pages)) {
  731. fuse_put_request(fc, req);
  732. return -EIO;
  733. }
  734. get_page(page);
  735. req->pages[req->num_pages] = page;
  736. req->page_descs[req->num_pages].length = PAGE_SIZE;
  737. req->num_pages++;
  738. data->nr_pages--;
  739. return 0;
  740. }
  741. static int fuse_readpages(struct file *file, struct address_space *mapping,
  742. struct list_head *pages, unsigned nr_pages)
  743. {
  744. struct inode *inode = mapping->host;
  745. struct fuse_conn *fc = get_fuse_conn(inode);
  746. struct fuse_fill_data data;
  747. int err;
  748. int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
  749. err = -EIO;
  750. if (is_bad_inode(inode))
  751. goto out;
  752. data.file = file;
  753. data.inode = inode;
  754. if (fc->async_read)
  755. data.req = fuse_get_req_for_background(fc, nr_alloc);
  756. else
  757. data.req = fuse_get_req(fc, nr_alloc);
  758. data.nr_pages = nr_pages;
  759. err = PTR_ERR(data.req);
  760. if (IS_ERR(data.req))
  761. goto out;
  762. err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
  763. if (!err) {
  764. if (data.req->num_pages)
  765. fuse_send_readpages(data.req, file);
  766. else
  767. fuse_put_request(fc, data.req);
  768. }
  769. out:
  770. return err;
  771. }
  772. static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  773. {
  774. struct inode *inode = iocb->ki_filp->f_mapping->host;
  775. struct fuse_conn *fc = get_fuse_conn(inode);
  776. /*
  777. * In auto invalidate mode, always update attributes on read.
  778. * Otherwise, only update if we attempt to read past EOF (to ensure
  779. * i_size is up to date).
  780. */
  781. if (fc->auto_inval_data ||
  782. (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
  783. int err;
  784. err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
  785. if (err)
  786. return err;
  787. }
  788. return generic_file_read_iter(iocb, to);
  789. }
  790. static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
  791. loff_t pos, size_t count)
  792. {
  793. struct fuse_write_in *inarg = &req->misc.write.in;
  794. struct fuse_write_out *outarg = &req->misc.write.out;
  795. inarg->fh = ff->fh;
  796. inarg->offset = pos;
  797. inarg->size = count;
  798. req->in.h.opcode = FUSE_WRITE;
  799. req->in.h.nodeid = ff->nodeid;
  800. req->in.numargs = 2;
  801. if (ff->fc->minor < 9)
  802. req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
  803. else
  804. req->in.args[0].size = sizeof(struct fuse_write_in);
  805. req->in.args[0].value = inarg;
  806. req->in.args[1].size = count;
  807. req->out.numargs = 1;
  808. req->out.args[0].size = sizeof(struct fuse_write_out);
  809. req->out.args[0].value = outarg;
  810. }
  811. static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
  812. loff_t pos, size_t count, fl_owner_t owner)
  813. {
  814. struct file *file = io->file;
  815. struct fuse_file *ff = file->private_data;
  816. struct fuse_conn *fc = ff->fc;
  817. struct fuse_write_in *inarg = &req->misc.write.in;
  818. fuse_write_fill(req, ff, pos, count);
  819. inarg->flags = file->f_flags;
  820. if (owner != NULL) {
  821. inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
  822. inarg->lock_owner = fuse_lock_owner_id(fc, owner);
  823. }
  824. if (io->async)
  825. return fuse_async_req_send(fc, req, count, io);
  826. fuse_request_send(fc, req);
  827. return req->misc.write.out.size;
  828. }
  829. bool fuse_write_update_size(struct inode *inode, loff_t pos)
  830. {
  831. struct fuse_conn *fc = get_fuse_conn(inode);
  832. struct fuse_inode *fi = get_fuse_inode(inode);
  833. bool ret = false;
  834. spin_lock(&fc->lock);
  835. fi->attr_version = ++fc->attr_version;
  836. if (pos > inode->i_size) {
  837. i_size_write(inode, pos);
  838. ret = true;
  839. }
  840. spin_unlock(&fc->lock);
  841. return ret;
  842. }
  843. static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
  844. struct inode *inode, loff_t pos,
  845. size_t count)
  846. {
  847. size_t res;
  848. unsigned offset;
  849. unsigned i;
  850. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
  851. for (i = 0; i < req->num_pages; i++)
  852. fuse_wait_on_page_writeback(inode, req->pages[i]->index);
  853. res = fuse_send_write(req, &io, pos, count, NULL);
  854. offset = req->page_descs[0].offset;
  855. count = res;
  856. for (i = 0; i < req->num_pages; i++) {
  857. struct page *page = req->pages[i];
  858. if (!req->out.h.error && !offset && count >= PAGE_SIZE)
  859. SetPageUptodate(page);
  860. if (count > PAGE_SIZE - offset)
  861. count -= PAGE_SIZE - offset;
  862. else
  863. count = 0;
  864. offset = 0;
  865. unlock_page(page);
  866. put_page(page);
  867. }
  868. return res;
  869. }
  870. static ssize_t fuse_fill_write_pages(struct fuse_req *req,
  871. struct address_space *mapping,
  872. struct iov_iter *ii, loff_t pos)
  873. {
  874. struct fuse_conn *fc = get_fuse_conn(mapping->host);
  875. unsigned offset = pos & (PAGE_SIZE - 1);
  876. size_t count = 0;
  877. int err;
  878. req->in.argpages = 1;
  879. req->page_descs[0].offset = offset;
  880. do {
  881. size_t tmp;
  882. struct page *page;
  883. pgoff_t index = pos >> PAGE_SHIFT;
  884. size_t bytes = min_t(size_t, PAGE_SIZE - offset,
  885. iov_iter_count(ii));
  886. bytes = min_t(size_t, bytes, fc->max_write - count);
  887. again:
  888. err = -EFAULT;
  889. if (iov_iter_fault_in_readable(ii, bytes))
  890. break;
  891. err = -ENOMEM;
  892. page = grab_cache_page_write_begin(mapping, index, 0);
  893. if (!page)
  894. break;
  895. if (mapping_writably_mapped(mapping))
  896. flush_dcache_page(page);
  897. tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
  898. flush_dcache_page(page);
  899. iov_iter_advance(ii, tmp);
  900. if (!tmp) {
  901. unlock_page(page);
  902. put_page(page);
  903. bytes = min(bytes, iov_iter_single_seg_count(ii));
  904. goto again;
  905. }
  906. err = 0;
  907. req->pages[req->num_pages] = page;
  908. req->page_descs[req->num_pages].length = tmp;
  909. req->num_pages++;
  910. count += tmp;
  911. pos += tmp;
  912. offset += tmp;
  913. if (offset == PAGE_SIZE)
  914. offset = 0;
  915. if (!fc->big_writes)
  916. break;
  917. } while (iov_iter_count(ii) && count < fc->max_write &&
  918. req->num_pages < req->max_pages && offset == 0);
  919. return count > 0 ? count : err;
  920. }
  921. static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
  922. {
  923. return min_t(unsigned,
  924. ((pos + len - 1) >> PAGE_SHIFT) -
  925. (pos >> PAGE_SHIFT) + 1,
  926. FUSE_MAX_PAGES_PER_REQ);
  927. }
  928. static ssize_t fuse_perform_write(struct file *file,
  929. struct address_space *mapping,
  930. struct iov_iter *ii, loff_t pos)
  931. {
  932. struct inode *inode = mapping->host;
  933. struct fuse_conn *fc = get_fuse_conn(inode);
  934. struct fuse_inode *fi = get_fuse_inode(inode);
  935. int err = 0;
  936. ssize_t res = 0;
  937. if (is_bad_inode(inode))
  938. return -EIO;
  939. if (inode->i_size < pos + iov_iter_count(ii))
  940. set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  941. do {
  942. struct fuse_req *req;
  943. ssize_t count;
  944. unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
  945. req = fuse_get_req(fc, nr_pages);
  946. if (IS_ERR(req)) {
  947. err = PTR_ERR(req);
  948. break;
  949. }
  950. count = fuse_fill_write_pages(req, mapping, ii, pos);
  951. if (count <= 0) {
  952. err = count;
  953. } else {
  954. size_t num_written;
  955. num_written = fuse_send_write_pages(req, file, inode,
  956. pos, count);
  957. err = req->out.h.error;
  958. if (!err) {
  959. res += num_written;
  960. pos += num_written;
  961. /* break out of the loop on short write */
  962. if (num_written != count)
  963. err = -EIO;
  964. }
  965. }
  966. fuse_put_request(fc, req);
  967. } while (!err && iov_iter_count(ii));
  968. if (res > 0)
  969. fuse_write_update_size(inode, pos);
  970. clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  971. fuse_invalidate_attr(inode);
  972. return res > 0 ? res : err;
  973. }
  974. static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  975. {
  976. struct file *file = iocb->ki_filp;
  977. struct address_space *mapping = file->f_mapping;
  978. ssize_t written = 0;
  979. ssize_t written_buffered = 0;
  980. struct inode *inode = mapping->host;
  981. ssize_t err;
  982. loff_t endbyte = 0;
  983. if (get_fuse_conn(inode)->writeback_cache) {
  984. /* Update size (EOF optimization) and mode (SUID clearing) */
  985. err = fuse_update_attributes(mapping->host, NULL, file, NULL);
  986. if (err)
  987. return err;
  988. return generic_file_write_iter(iocb, from);
  989. }
  990. inode_lock(inode);
  991. /* We can write back this queue in page reclaim */
  992. current->backing_dev_info = inode_to_bdi(inode);
  993. err = generic_write_checks(iocb, from);
  994. if (err <= 0)
  995. goto out;
  996. err = file_remove_privs(file);
  997. if (err)
  998. goto out;
  999. err = file_update_time(file);
  1000. if (err)
  1001. goto out;
  1002. if (iocb->ki_flags & IOCB_DIRECT) {
  1003. loff_t pos = iocb->ki_pos;
  1004. written = generic_file_direct_write(iocb, from);
  1005. if (written < 0 || !iov_iter_count(from))
  1006. goto out;
  1007. pos += written;
  1008. written_buffered = fuse_perform_write(file, mapping, from, pos);
  1009. if (written_buffered < 0) {
  1010. err = written_buffered;
  1011. goto out;
  1012. }
  1013. endbyte = pos + written_buffered - 1;
  1014. err = filemap_write_and_wait_range(file->f_mapping, pos,
  1015. endbyte);
  1016. if (err)
  1017. goto out;
  1018. invalidate_mapping_pages(file->f_mapping,
  1019. pos >> PAGE_SHIFT,
  1020. endbyte >> PAGE_SHIFT);
  1021. written += written_buffered;
  1022. iocb->ki_pos = pos + written_buffered;
  1023. } else {
  1024. written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
  1025. if (written >= 0)
  1026. iocb->ki_pos += written;
  1027. }
  1028. out:
  1029. current->backing_dev_info = NULL;
  1030. inode_unlock(inode);
  1031. return written ? written : err;
  1032. }
  1033. static inline void fuse_page_descs_length_init(struct fuse_req *req,
  1034. unsigned index, unsigned nr_pages)
  1035. {
  1036. int i;
  1037. for (i = index; i < index + nr_pages; i++)
  1038. req->page_descs[i].length = PAGE_SIZE -
  1039. req->page_descs[i].offset;
  1040. }
  1041. static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
  1042. {
  1043. return (unsigned long)ii->iov->iov_base + ii->iov_offset;
  1044. }
  1045. static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
  1046. size_t max_size)
  1047. {
  1048. return min(iov_iter_single_seg_count(ii), max_size);
  1049. }
  1050. static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
  1051. size_t *nbytesp, int write)
  1052. {
  1053. size_t nbytes = 0; /* # bytes already packed in req */
  1054. ssize_t ret = 0;
  1055. /* Special case for kernel I/O: can copy directly into the buffer */
  1056. if (ii->type & ITER_KVEC) {
  1057. unsigned long user_addr = fuse_get_user_addr(ii);
  1058. size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
  1059. if (write)
  1060. req->in.args[1].value = (void *) user_addr;
  1061. else
  1062. req->out.args[0].value = (void *) user_addr;
  1063. iov_iter_advance(ii, frag_size);
  1064. *nbytesp = frag_size;
  1065. return 0;
  1066. }
  1067. while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
  1068. unsigned npages;
  1069. size_t start;
  1070. ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
  1071. *nbytesp - nbytes,
  1072. req->max_pages - req->num_pages,
  1073. &start);
  1074. if (ret < 0)
  1075. break;
  1076. iov_iter_advance(ii, ret);
  1077. nbytes += ret;
  1078. ret += start;
  1079. npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
  1080. req->page_descs[req->num_pages].offset = start;
  1081. fuse_page_descs_length_init(req, req->num_pages, npages);
  1082. req->num_pages += npages;
  1083. req->page_descs[req->num_pages - 1].length -=
  1084. (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
  1085. }
  1086. if (write)
  1087. req->in.argpages = 1;
  1088. else
  1089. req->out.argpages = 1;
  1090. *nbytesp = nbytes;
  1091. return ret < 0 ? ret : 0;
  1092. }
  1093. static inline int fuse_iter_npages(const struct iov_iter *ii_p)
  1094. {
  1095. return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
  1096. }
  1097. ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
  1098. loff_t *ppos, int flags)
  1099. {
  1100. int write = flags & FUSE_DIO_WRITE;
  1101. bool should_dirty = !write && iter_is_iovec(iter);
  1102. int cuse = flags & FUSE_DIO_CUSE;
  1103. struct file *file = io->file;
  1104. struct inode *inode = file->f_mapping->host;
  1105. struct fuse_file *ff = file->private_data;
  1106. struct fuse_conn *fc = ff->fc;
  1107. size_t nmax = write ? fc->max_write : fc->max_read;
  1108. loff_t pos = *ppos;
  1109. size_t count = iov_iter_count(iter);
  1110. pgoff_t idx_from = pos >> PAGE_SHIFT;
  1111. pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
  1112. ssize_t res = 0;
  1113. struct fuse_req *req;
  1114. int err = 0;
  1115. if (io->async)
  1116. req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
  1117. else
  1118. req = fuse_get_req(fc, fuse_iter_npages(iter));
  1119. if (IS_ERR(req))
  1120. return PTR_ERR(req);
  1121. if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
  1122. if (!write)
  1123. inode_lock(inode);
  1124. fuse_sync_writes(inode);
  1125. if (!write)
  1126. inode_unlock(inode);
  1127. }
  1128. while (count) {
  1129. size_t nres;
  1130. fl_owner_t owner = current->files;
  1131. size_t nbytes = min(count, nmax);
  1132. err = fuse_get_user_pages(req, iter, &nbytes, write);
  1133. if (err && !nbytes)
  1134. break;
  1135. if (write)
  1136. nres = fuse_send_write(req, io, pos, nbytes, owner);
  1137. else
  1138. nres = fuse_send_read(req, io, pos, nbytes, owner);
  1139. if (!io->async)
  1140. fuse_release_user_pages(req, should_dirty);
  1141. if (req->out.h.error) {
  1142. err = req->out.h.error;
  1143. break;
  1144. } else if (nres > nbytes) {
  1145. res = 0;
  1146. err = -EIO;
  1147. break;
  1148. }
  1149. count -= nres;
  1150. res += nres;
  1151. pos += nres;
  1152. if (nres != nbytes)
  1153. break;
  1154. if (count) {
  1155. fuse_put_request(fc, req);
  1156. if (io->async)
  1157. req = fuse_get_req_for_background(fc,
  1158. fuse_iter_npages(iter));
  1159. else
  1160. req = fuse_get_req(fc, fuse_iter_npages(iter));
  1161. if (IS_ERR(req))
  1162. break;
  1163. }
  1164. }
  1165. if (!IS_ERR(req))
  1166. fuse_put_request(fc, req);
  1167. if (res > 0)
  1168. *ppos = pos;
  1169. return res > 0 ? res : err;
  1170. }
  1171. EXPORT_SYMBOL_GPL(fuse_direct_io);
  1172. static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
  1173. struct iov_iter *iter,
  1174. loff_t *ppos)
  1175. {
  1176. ssize_t res;
  1177. struct file *file = io->file;
  1178. struct inode *inode = file_inode(file);
  1179. if (is_bad_inode(inode))
  1180. return -EIO;
  1181. res = fuse_direct_io(io, iter, ppos, 0);
  1182. fuse_invalidate_attr(inode);
  1183. return res;
  1184. }
  1185. static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1186. {
  1187. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
  1188. return __fuse_direct_read(&io, to, &iocb->ki_pos);
  1189. }
  1190. static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1191. {
  1192. struct file *file = iocb->ki_filp;
  1193. struct inode *inode = file_inode(file);
  1194. struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
  1195. ssize_t res;
  1196. if (is_bad_inode(inode))
  1197. return -EIO;
  1198. /* Don't allow parallel writes to the same file */
  1199. inode_lock(inode);
  1200. res = generic_write_checks(iocb, from);
  1201. if (res > 0)
  1202. res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
  1203. fuse_invalidate_attr(inode);
  1204. if (res > 0)
  1205. fuse_write_update_size(inode, iocb->ki_pos);
  1206. inode_unlock(inode);
  1207. return res;
  1208. }
  1209. static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
  1210. {
  1211. int i;
  1212. for (i = 0; i < req->num_pages; i++)
  1213. __free_page(req->pages[i]);
  1214. if (req->ff)
  1215. fuse_file_put(req->ff, false);
  1216. }
  1217. static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
  1218. {
  1219. struct inode *inode = req->inode;
  1220. struct fuse_inode *fi = get_fuse_inode(inode);
  1221. struct backing_dev_info *bdi = inode_to_bdi(inode);
  1222. int i;
  1223. list_del(&req->writepages_entry);
  1224. for (i = 0; i < req->num_pages; i++) {
  1225. dec_wb_stat(&bdi->wb, WB_WRITEBACK);
  1226. dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
  1227. wb_writeout_inc(&bdi->wb);
  1228. }
  1229. wake_up(&fi->page_waitq);
  1230. }
  1231. /* Called under fc->lock, may release and reacquire it */
  1232. static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
  1233. loff_t size)
  1234. __releases(fc->lock)
  1235. __acquires(fc->lock)
  1236. {
  1237. struct fuse_inode *fi = get_fuse_inode(req->inode);
  1238. struct fuse_write_in *inarg = &req->misc.write.in;
  1239. __u64 data_size = req->num_pages * PAGE_SIZE;
  1240. if (!fc->connected)
  1241. goto out_free;
  1242. if (inarg->offset + data_size <= size) {
  1243. inarg->size = data_size;
  1244. } else if (inarg->offset < size) {
  1245. inarg->size = size - inarg->offset;
  1246. } else {
  1247. /* Got truncated off completely */
  1248. goto out_free;
  1249. }
  1250. req->in.args[1].size = inarg->size;
  1251. fi->writectr++;
  1252. fuse_request_send_background_locked(fc, req);
  1253. return;
  1254. out_free:
  1255. fuse_writepage_finish(fc, req);
  1256. spin_unlock(&fc->lock);
  1257. fuse_writepage_free(fc, req);
  1258. fuse_put_request(fc, req);
  1259. spin_lock(&fc->lock);
  1260. }
  1261. /*
  1262. * If fi->writectr is positive (no truncate or fsync going on) send
  1263. * all queued writepage requests.
  1264. *
  1265. * Called with fc->lock
  1266. */
  1267. void fuse_flush_writepages(struct inode *inode)
  1268. __releases(fc->lock)
  1269. __acquires(fc->lock)
  1270. {
  1271. struct fuse_conn *fc = get_fuse_conn(inode);
  1272. struct fuse_inode *fi = get_fuse_inode(inode);
  1273. size_t crop = i_size_read(inode);
  1274. struct fuse_req *req;
  1275. while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
  1276. req = list_entry(fi->queued_writes.next, struct fuse_req, list);
  1277. list_del_init(&req->list);
  1278. fuse_send_writepage(fc, req, crop);
  1279. }
  1280. }
  1281. static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
  1282. {
  1283. struct inode *inode = req->inode;
  1284. struct fuse_inode *fi = get_fuse_inode(inode);
  1285. mapping_set_error(inode->i_mapping, req->out.h.error);
  1286. spin_lock(&fc->lock);
  1287. while (req->misc.write.next) {
  1288. struct fuse_conn *fc = get_fuse_conn(inode);
  1289. struct fuse_write_in *inarg = &req->misc.write.in;
  1290. struct fuse_req *next = req->misc.write.next;
  1291. req->misc.write.next = next->misc.write.next;
  1292. next->misc.write.next = NULL;
  1293. next->ff = fuse_file_get(req->ff);
  1294. list_add(&next->writepages_entry, &fi->writepages);
  1295. /*
  1296. * Skip fuse_flush_writepages() to make it easy to crop requests
  1297. * based on primary request size.
  1298. *
  1299. * 1st case (trivial): there are no concurrent activities using
  1300. * fuse_set/release_nowrite. Then we're on safe side because
  1301. * fuse_flush_writepages() would call fuse_send_writepage()
  1302. * anyway.
  1303. *
  1304. * 2nd case: someone called fuse_set_nowrite and it is waiting
  1305. * now for completion of all in-flight requests. This happens
  1306. * rarely and no more than once per page, so this should be
  1307. * okay.
  1308. *
  1309. * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
  1310. * of fuse_set_nowrite..fuse_release_nowrite section. The fact
  1311. * that fuse_set_nowrite returned implies that all in-flight
  1312. * requests were completed along with all of their secondary
  1313. * requests. Further primary requests are blocked by negative
  1314. * writectr. Hence there cannot be any in-flight requests and
  1315. * no invocations of fuse_writepage_end() while we're in
  1316. * fuse_set_nowrite..fuse_release_nowrite section.
  1317. */
  1318. fuse_send_writepage(fc, next, inarg->offset + inarg->size);
  1319. }
  1320. fi->writectr--;
  1321. fuse_writepage_finish(fc, req);
  1322. spin_unlock(&fc->lock);
  1323. fuse_writepage_free(fc, req);
  1324. }
  1325. static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
  1326. struct fuse_inode *fi)
  1327. {
  1328. struct fuse_file *ff = NULL;
  1329. spin_lock(&fc->lock);
  1330. if (!list_empty(&fi->write_files)) {
  1331. ff = list_entry(fi->write_files.next, struct fuse_file,
  1332. write_entry);
  1333. fuse_file_get(ff);
  1334. }
  1335. spin_unlock(&fc->lock);
  1336. return ff;
  1337. }
  1338. static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
  1339. struct fuse_inode *fi)
  1340. {
  1341. struct fuse_file *ff = __fuse_write_file_get(fc, fi);
  1342. WARN_ON(!ff);
  1343. return ff;
  1344. }
  1345. int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
  1346. {
  1347. struct fuse_conn *fc = get_fuse_conn(inode);
  1348. struct fuse_inode *fi = get_fuse_inode(inode);
  1349. struct fuse_file *ff;
  1350. int err;
  1351. ff = __fuse_write_file_get(fc, fi);
  1352. err = fuse_flush_times(inode, ff);
  1353. if (ff)
  1354. fuse_file_put(ff, 0);
  1355. return err;
  1356. }
  1357. static int fuse_writepage_locked(struct page *page)
  1358. {
  1359. struct address_space *mapping = page->mapping;
  1360. struct inode *inode = mapping->host;
  1361. struct fuse_conn *fc = get_fuse_conn(inode);
  1362. struct fuse_inode *fi = get_fuse_inode(inode);
  1363. struct fuse_req *req;
  1364. struct page *tmp_page;
  1365. int error = -ENOMEM;
  1366. set_page_writeback(page);
  1367. req = fuse_request_alloc_nofs(1);
  1368. if (!req)
  1369. goto err;
  1370. /* writeback always goes to bg_queue */
  1371. __set_bit(FR_BACKGROUND, &req->flags);
  1372. tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  1373. if (!tmp_page)
  1374. goto err_free;
  1375. error = -EIO;
  1376. req->ff = fuse_write_file_get(fc, fi);
  1377. if (!req->ff)
  1378. goto err_nofile;
  1379. fuse_write_fill(req, req->ff, page_offset(page), 0);
  1380. copy_highpage(tmp_page, page);
  1381. req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
  1382. req->misc.write.next = NULL;
  1383. req->in.argpages = 1;
  1384. req->num_pages = 1;
  1385. req->pages[0] = tmp_page;
  1386. req->page_descs[0].offset = 0;
  1387. req->page_descs[0].length = PAGE_SIZE;
  1388. req->end = fuse_writepage_end;
  1389. req->inode = inode;
  1390. inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
  1391. inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
  1392. spin_lock(&fc->lock);
  1393. list_add(&req->writepages_entry, &fi->writepages);
  1394. list_add_tail(&req->list, &fi->queued_writes);
  1395. fuse_flush_writepages(inode);
  1396. spin_unlock(&fc->lock);
  1397. end_page_writeback(page);
  1398. return 0;
  1399. err_nofile:
  1400. __free_page(tmp_page);
  1401. err_free:
  1402. fuse_request_free(req);
  1403. err:
  1404. end_page_writeback(page);
  1405. return error;
  1406. }
  1407. static int fuse_writepage(struct page *page, struct writeback_control *wbc)
  1408. {
  1409. int err;
  1410. if (fuse_page_is_writeback(page->mapping->host, page->index)) {
  1411. /*
  1412. * ->writepages() should be called for sync() and friends. We
  1413. * should only get here on direct reclaim and then we are
  1414. * allowed to skip a page which is already in flight
  1415. */
  1416. WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
  1417. redirty_page_for_writepage(wbc, page);
  1418. return 0;
  1419. }
  1420. err = fuse_writepage_locked(page);
  1421. unlock_page(page);
  1422. return err;
  1423. }
  1424. struct fuse_fill_wb_data {
  1425. struct fuse_req *req;
  1426. struct fuse_file *ff;
  1427. struct inode *inode;
  1428. struct page **orig_pages;
  1429. };
  1430. static void fuse_writepages_send(struct fuse_fill_wb_data *data)
  1431. {
  1432. struct fuse_req *req = data->req;
  1433. struct inode *inode = data->inode;
  1434. struct fuse_conn *fc = get_fuse_conn(inode);
  1435. struct fuse_inode *fi = get_fuse_inode(inode);
  1436. int num_pages = req->num_pages;
  1437. int i;
  1438. req->ff = fuse_file_get(data->ff);
  1439. spin_lock(&fc->lock);
  1440. list_add_tail(&req->list, &fi->queued_writes);
  1441. fuse_flush_writepages(inode);
  1442. spin_unlock(&fc->lock);
  1443. for (i = 0; i < num_pages; i++)
  1444. end_page_writeback(data->orig_pages[i]);
  1445. }
  1446. static bool fuse_writepage_in_flight(struct fuse_req *new_req,
  1447. struct page *page)
  1448. {
  1449. struct fuse_conn *fc = get_fuse_conn(new_req->inode);
  1450. struct fuse_inode *fi = get_fuse_inode(new_req->inode);
  1451. struct fuse_req *tmp;
  1452. struct fuse_req *old_req;
  1453. bool found = false;
  1454. pgoff_t curr_index;
  1455. BUG_ON(new_req->num_pages != 0);
  1456. spin_lock(&fc->lock);
  1457. list_del(&new_req->writepages_entry);
  1458. list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
  1459. BUG_ON(old_req->inode != new_req->inode);
  1460. curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
  1461. if (curr_index <= page->index &&
  1462. page->index < curr_index + old_req->num_pages) {
  1463. found = true;
  1464. break;
  1465. }
  1466. }
  1467. if (!found) {
  1468. list_add(&new_req->writepages_entry, &fi->writepages);
  1469. goto out_unlock;
  1470. }
  1471. new_req->num_pages = 1;
  1472. for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
  1473. BUG_ON(tmp->inode != new_req->inode);
  1474. curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
  1475. if (tmp->num_pages == 1 &&
  1476. curr_index == page->index) {
  1477. old_req = tmp;
  1478. }
  1479. }
  1480. if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
  1481. struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
  1482. copy_highpage(old_req->pages[0], page);
  1483. spin_unlock(&fc->lock);
  1484. dec_wb_stat(&bdi->wb, WB_WRITEBACK);
  1485. dec_node_page_state(page, NR_WRITEBACK_TEMP);
  1486. wb_writeout_inc(&bdi->wb);
  1487. fuse_writepage_free(fc, new_req);
  1488. fuse_request_free(new_req);
  1489. goto out;
  1490. } else {
  1491. new_req->misc.write.next = old_req->misc.write.next;
  1492. old_req->misc.write.next = new_req;
  1493. }
  1494. out_unlock:
  1495. spin_unlock(&fc->lock);
  1496. out:
  1497. return found;
  1498. }
  1499. static int fuse_writepages_fill(struct page *page,
  1500. struct writeback_control *wbc, void *_data)
  1501. {
  1502. struct fuse_fill_wb_data *data = _data;
  1503. struct fuse_req *req = data->req;
  1504. struct inode *inode = data->inode;
  1505. struct fuse_conn *fc = get_fuse_conn(inode);
  1506. struct page *tmp_page;
  1507. bool is_writeback;
  1508. int err;
  1509. if (!data->ff) {
  1510. err = -EIO;
  1511. data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
  1512. if (!data->ff)
  1513. goto out_unlock;
  1514. }
  1515. /*
  1516. * Being under writeback is unlikely but possible. For example direct
  1517. * read to an mmaped fuse file will set the page dirty twice; once when
  1518. * the pages are faulted with get_user_pages(), and then after the read
  1519. * completed.
  1520. */
  1521. is_writeback = fuse_page_is_writeback(inode, page->index);
  1522. if (req && req->num_pages &&
  1523. (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
  1524. (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
  1525. data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
  1526. fuse_writepages_send(data);
  1527. data->req = NULL;
  1528. }
  1529. err = -ENOMEM;
  1530. tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  1531. if (!tmp_page)
  1532. goto out_unlock;
  1533. /*
  1534. * The page must not be redirtied until the writeout is completed
  1535. * (i.e. userspace has sent a reply to the write request). Otherwise
  1536. * there could be more than one temporary page instance for each real
  1537. * page.
  1538. *
  1539. * This is ensured by holding the page lock in page_mkwrite() while
  1540. * checking fuse_page_is_writeback(). We already hold the page lock
  1541. * since clear_page_dirty_for_io() and keep it held until we add the
  1542. * request to the fi->writepages list and increment req->num_pages.
  1543. * After this fuse_page_is_writeback() will indicate that the page is
  1544. * under writeback, so we can release the page lock.
  1545. */
  1546. if (data->req == NULL) {
  1547. struct fuse_inode *fi = get_fuse_inode(inode);
  1548. err = -ENOMEM;
  1549. req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
  1550. if (!req) {
  1551. __free_page(tmp_page);
  1552. goto out_unlock;
  1553. }
  1554. fuse_write_fill(req, data->ff, page_offset(page), 0);
  1555. req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
  1556. req->misc.write.next = NULL;
  1557. req->in.argpages = 1;
  1558. __set_bit(FR_BACKGROUND, &req->flags);
  1559. req->num_pages = 0;
  1560. req->end = fuse_writepage_end;
  1561. req->inode = inode;
  1562. spin_lock(&fc->lock);
  1563. list_add(&req->writepages_entry, &fi->writepages);
  1564. spin_unlock(&fc->lock);
  1565. data->req = req;
  1566. }
  1567. set_page_writeback(page);
  1568. copy_highpage(tmp_page, page);
  1569. req->pages[req->num_pages] = tmp_page;
  1570. req->page_descs[req->num_pages].offset = 0;
  1571. req->page_descs[req->num_pages].length = PAGE_SIZE;
  1572. inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
  1573. inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
  1574. err = 0;
  1575. if (is_writeback && fuse_writepage_in_flight(req, page)) {
  1576. end_page_writeback(page);
  1577. data->req = NULL;
  1578. goto out_unlock;
  1579. }
  1580. data->orig_pages[req->num_pages] = page;
  1581. /*
  1582. * Protected by fc->lock against concurrent access by
  1583. * fuse_page_is_writeback().
  1584. */
  1585. spin_lock(&fc->lock);
  1586. req->num_pages++;
  1587. spin_unlock(&fc->lock);
  1588. out_unlock:
  1589. unlock_page(page);
  1590. return err;
  1591. }
  1592. static int fuse_writepages(struct address_space *mapping,
  1593. struct writeback_control *wbc)
  1594. {
  1595. struct inode *inode = mapping->host;
  1596. struct fuse_fill_wb_data data;
  1597. int err;
  1598. err = -EIO;
  1599. if (is_bad_inode(inode))
  1600. goto out;
  1601. data.inode = inode;
  1602. data.req = NULL;
  1603. data.ff = NULL;
  1604. err = -ENOMEM;
  1605. data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
  1606. sizeof(struct page *),
  1607. GFP_NOFS);
  1608. if (!data.orig_pages)
  1609. goto out;
  1610. err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
  1611. if (data.req) {
  1612. /* Ignore errors if we can write at least one page */
  1613. BUG_ON(!data.req->num_pages);
  1614. fuse_writepages_send(&data);
  1615. err = 0;
  1616. }
  1617. if (data.ff)
  1618. fuse_file_put(data.ff, false);
  1619. kfree(data.orig_pages);
  1620. out:
  1621. return err;
  1622. }
  1623. /*
  1624. * It's worthy to make sure that space is reserved on disk for the write,
  1625. * but how to implement it without killing performance need more thinking.
  1626. */
  1627. static int fuse_write_begin(struct file *file, struct address_space *mapping,
  1628. loff_t pos, unsigned len, unsigned flags,
  1629. struct page **pagep, void **fsdata)
  1630. {
  1631. pgoff_t index = pos >> PAGE_SHIFT;
  1632. struct fuse_conn *fc = get_fuse_conn(file_inode(file));
  1633. struct page *page;
  1634. loff_t fsize;
  1635. int err = -ENOMEM;
  1636. WARN_ON(!fc->writeback_cache);
  1637. page = grab_cache_page_write_begin(mapping, index, flags);
  1638. if (!page)
  1639. goto error;
  1640. fuse_wait_on_page_writeback(mapping->host, page->index);
  1641. if (PageUptodate(page) || len == PAGE_SIZE)
  1642. goto success;
  1643. /*
  1644. * Check if the start this page comes after the end of file, in which
  1645. * case the readpage can be optimized away.
  1646. */
  1647. fsize = i_size_read(mapping->host);
  1648. if (fsize <= (pos & PAGE_MASK)) {
  1649. size_t off = pos & ~PAGE_MASK;
  1650. if (off)
  1651. zero_user_segment(page, 0, off);
  1652. goto success;
  1653. }
  1654. err = fuse_do_readpage(file, page);
  1655. if (err)
  1656. goto cleanup;
  1657. success:
  1658. *pagep = page;
  1659. return 0;
  1660. cleanup:
  1661. unlock_page(page);
  1662. put_page(page);
  1663. error:
  1664. return err;
  1665. }
  1666. static int fuse_write_end(struct file *file, struct address_space *mapping,
  1667. loff_t pos, unsigned len, unsigned copied,
  1668. struct page *page, void *fsdata)
  1669. {
  1670. struct inode *inode = page->mapping->host;
  1671. /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
  1672. if (!copied)
  1673. goto unlock;
  1674. if (!PageUptodate(page)) {
  1675. /* Zero any unwritten bytes at the end of the page */
  1676. size_t endoff = (pos + copied) & ~PAGE_MASK;
  1677. if (endoff)
  1678. zero_user_segment(page, endoff, PAGE_SIZE);
  1679. SetPageUptodate(page);
  1680. }
  1681. fuse_write_update_size(inode, pos + copied);
  1682. set_page_dirty(page);
  1683. unlock:
  1684. unlock_page(page);
  1685. put_page(page);
  1686. return copied;
  1687. }
  1688. static int fuse_launder_page(struct page *page)
  1689. {
  1690. int err = 0;
  1691. if (clear_page_dirty_for_io(page)) {
  1692. struct inode *inode = page->mapping->host;
  1693. err = fuse_writepage_locked(page);
  1694. if (!err)
  1695. fuse_wait_on_page_writeback(inode, page->index);
  1696. }
  1697. return err;
  1698. }
  1699. /*
  1700. * Write back dirty pages now, because there may not be any suitable
  1701. * open files later
  1702. */
  1703. static void fuse_vma_close(struct vm_area_struct *vma)
  1704. {
  1705. filemap_write_and_wait(vma->vm_file->f_mapping);
  1706. }
  1707. /*
  1708. * Wait for writeback against this page to complete before allowing it
  1709. * to be marked dirty again, and hence written back again, possibly
  1710. * before the previous writepage completed.
  1711. *
  1712. * Block here, instead of in ->writepage(), so that the userspace fs
  1713. * can only block processes actually operating on the filesystem.
  1714. *
  1715. * Otherwise unprivileged userspace fs would be able to block
  1716. * unrelated:
  1717. *
  1718. * - page migration
  1719. * - sync(2)
  1720. * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
  1721. */
  1722. static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  1723. {
  1724. struct page *page = vmf->page;
  1725. struct inode *inode = file_inode(vma->vm_file);
  1726. file_update_time(vma->vm_file);
  1727. lock_page(page);
  1728. if (page->mapping != inode->i_mapping) {
  1729. unlock_page(page);
  1730. return VM_FAULT_NOPAGE;
  1731. }
  1732. fuse_wait_on_page_writeback(inode, page->index);
  1733. return VM_FAULT_LOCKED;
  1734. }
  1735. static const struct vm_operations_struct fuse_file_vm_ops = {
  1736. .close = fuse_vma_close,
  1737. .fault = filemap_fault,
  1738. .map_pages = filemap_map_pages,
  1739. .page_mkwrite = fuse_page_mkwrite,
  1740. };
  1741. static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
  1742. {
  1743. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  1744. fuse_link_write_file(file);
  1745. file_accessed(file);
  1746. vma->vm_ops = &fuse_file_vm_ops;
  1747. return 0;
  1748. }
  1749. static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
  1750. {
  1751. /* Can't provide the coherency needed for MAP_SHARED */
  1752. if (vma->vm_flags & VM_MAYSHARE)
  1753. return -ENODEV;
  1754. invalidate_inode_pages2(file->f_mapping);
  1755. return generic_file_mmap(file, vma);
  1756. }
  1757. static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
  1758. struct file_lock *fl)
  1759. {
  1760. switch (ffl->type) {
  1761. case F_UNLCK:
  1762. break;
  1763. case F_RDLCK:
  1764. case F_WRLCK:
  1765. if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
  1766. ffl->end < ffl->start)
  1767. return -EIO;
  1768. fl->fl_start = ffl->start;
  1769. fl->fl_end = ffl->end;
  1770. fl->fl_pid = ffl->pid;
  1771. break;
  1772. default:
  1773. return -EIO;
  1774. }
  1775. fl->fl_type = ffl->type;
  1776. return 0;
  1777. }
  1778. static void fuse_lk_fill(struct fuse_args *args, struct file *file,
  1779. const struct file_lock *fl, int opcode, pid_t pid,
  1780. int flock, struct fuse_lk_in *inarg)
  1781. {
  1782. struct inode *inode = file_inode(file);
  1783. struct fuse_conn *fc = get_fuse_conn(inode);
  1784. struct fuse_file *ff = file->private_data;
  1785. memset(inarg, 0, sizeof(*inarg));
  1786. inarg->fh = ff->fh;
  1787. inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
  1788. inarg->lk.start = fl->fl_start;
  1789. inarg->lk.end = fl->fl_end;
  1790. inarg->lk.type = fl->fl_type;
  1791. inarg->lk.pid = pid;
  1792. if (flock)
  1793. inarg->lk_flags |= FUSE_LK_FLOCK;
  1794. args->in.h.opcode = opcode;
  1795. args->in.h.nodeid = get_node_id(inode);
  1796. args->in.numargs = 1;
  1797. args->in.args[0].size = sizeof(*inarg);
  1798. args->in.args[0].value = inarg;
  1799. }
  1800. static int fuse_getlk(struct file *file, struct file_lock *fl)
  1801. {
  1802. struct inode *inode = file_inode(file);
  1803. struct fuse_conn *fc = get_fuse_conn(inode);
  1804. FUSE_ARGS(args);
  1805. struct fuse_lk_in inarg;
  1806. struct fuse_lk_out outarg;
  1807. int err;
  1808. fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
  1809. args.out.numargs = 1;
  1810. args.out.args[0].size = sizeof(outarg);
  1811. args.out.args[0].value = &outarg;
  1812. err = fuse_simple_request(fc, &args);
  1813. if (!err)
  1814. err = convert_fuse_file_lock(&outarg.lk, fl);
  1815. return err;
  1816. }
  1817. static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
  1818. {
  1819. struct inode *inode = file_inode(file);
  1820. struct fuse_conn *fc = get_fuse_conn(inode);
  1821. FUSE_ARGS(args);
  1822. struct fuse_lk_in inarg;
  1823. int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
  1824. pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
  1825. int err;
  1826. if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
  1827. /* NLM needs asynchronous locks, which we don't support yet */
  1828. return -ENOLCK;
  1829. }
  1830. /* Unlock on close is handled by the flush method */
  1831. if (fl->fl_flags & FL_CLOSE)
  1832. return 0;
  1833. fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
  1834. err = fuse_simple_request(fc, &args);
  1835. /* locking is restartable */
  1836. if (err == -EINTR)
  1837. err = -ERESTARTSYS;
  1838. return err;
  1839. }
  1840. static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
  1841. {
  1842. struct inode *inode = file_inode(file);
  1843. struct fuse_conn *fc = get_fuse_conn(inode);
  1844. int err;
  1845. if (cmd == F_CANCELLK) {
  1846. err = 0;
  1847. } else if (cmd == F_GETLK) {
  1848. if (fc->no_lock) {
  1849. posix_test_lock(file, fl);
  1850. err = 0;
  1851. } else
  1852. err = fuse_getlk(file, fl);
  1853. } else {
  1854. if (fc->no_lock)
  1855. err = posix_lock_file(file, fl, NULL);
  1856. else
  1857. err = fuse_setlk(file, fl, 0);
  1858. }
  1859. return err;
  1860. }
  1861. static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
  1862. {
  1863. struct inode *inode = file_inode(file);
  1864. struct fuse_conn *fc = get_fuse_conn(inode);
  1865. int err;
  1866. if (fc->no_flock) {
  1867. err = locks_lock_file_wait(file, fl);
  1868. } else {
  1869. struct fuse_file *ff = file->private_data;
  1870. /* emulate flock with POSIX locks */
  1871. ff->flock = true;
  1872. err = fuse_setlk(file, fl, 1);
  1873. }
  1874. return err;
  1875. }
  1876. static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
  1877. {
  1878. struct inode *inode = mapping->host;
  1879. struct fuse_conn *fc = get_fuse_conn(inode);
  1880. FUSE_ARGS(args);
  1881. struct fuse_bmap_in inarg;
  1882. struct fuse_bmap_out outarg;
  1883. int err;
  1884. if (!inode->i_sb->s_bdev || fc->no_bmap)
  1885. return 0;
  1886. memset(&inarg, 0, sizeof(inarg));
  1887. inarg.block = block;
  1888. inarg.blocksize = inode->i_sb->s_blocksize;
  1889. args.in.h.opcode = FUSE_BMAP;
  1890. args.in.h.nodeid = get_node_id(inode);
  1891. args.in.numargs = 1;
  1892. args.in.args[0].size = sizeof(inarg);
  1893. args.in.args[0].value = &inarg;
  1894. args.out.numargs = 1;
  1895. args.out.args[0].size = sizeof(outarg);
  1896. args.out.args[0].value = &outarg;
  1897. err = fuse_simple_request(fc, &args);
  1898. if (err == -ENOSYS)
  1899. fc->no_bmap = 1;
  1900. return err ? 0 : outarg.block;
  1901. }
  1902. static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
  1903. {
  1904. struct inode *inode = file->f_mapping->host;
  1905. struct fuse_conn *fc = get_fuse_conn(inode);
  1906. struct fuse_file *ff = file->private_data;
  1907. FUSE_ARGS(args);
  1908. struct fuse_lseek_in inarg = {
  1909. .fh = ff->fh,
  1910. .offset = offset,
  1911. .whence = whence
  1912. };
  1913. struct fuse_lseek_out outarg;
  1914. int err;
  1915. if (fc->no_lseek)
  1916. goto fallback;
  1917. args.in.h.opcode = FUSE_LSEEK;
  1918. args.in.h.nodeid = ff->nodeid;
  1919. args.in.numargs = 1;
  1920. args.in.args[0].size = sizeof(inarg);
  1921. args.in.args[0].value = &inarg;
  1922. args.out.numargs = 1;
  1923. args.out.args[0].size = sizeof(outarg);
  1924. args.out.args[0].value = &outarg;
  1925. err = fuse_simple_request(fc, &args);
  1926. if (err) {
  1927. if (err == -ENOSYS) {
  1928. fc->no_lseek = 1;
  1929. goto fallback;
  1930. }
  1931. return err;
  1932. }
  1933. return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
  1934. fallback:
  1935. err = fuse_update_attributes(inode, NULL, file, NULL);
  1936. if (!err)
  1937. return generic_file_llseek(file, offset, whence);
  1938. else
  1939. return err;
  1940. }
  1941. static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
  1942. {
  1943. loff_t retval;
  1944. struct inode *inode = file_inode(file);
  1945. switch (whence) {
  1946. case SEEK_SET:
  1947. case SEEK_CUR:
  1948. /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
  1949. retval = generic_file_llseek(file, offset, whence);
  1950. break;
  1951. case SEEK_END:
  1952. inode_lock(inode);
  1953. retval = fuse_update_attributes(inode, NULL, file, NULL);
  1954. if (!retval)
  1955. retval = generic_file_llseek(file, offset, whence);
  1956. inode_unlock(inode);
  1957. break;
  1958. case SEEK_HOLE:
  1959. case SEEK_DATA:
  1960. inode_lock(inode);
  1961. retval = fuse_lseek(file, offset, whence);
  1962. inode_unlock(inode);
  1963. break;
  1964. default:
  1965. retval = -EINVAL;
  1966. }
  1967. return retval;
  1968. }
  1969. /*
  1970. * CUSE servers compiled on 32bit broke on 64bit kernels because the
  1971. * ABI was defined to be 'struct iovec' which is different on 32bit
  1972. * and 64bit. Fortunately we can determine which structure the server
  1973. * used from the size of the reply.
  1974. */
  1975. static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
  1976. size_t transferred, unsigned count,
  1977. bool is_compat)
  1978. {
  1979. #ifdef CONFIG_COMPAT
  1980. if (count * sizeof(struct compat_iovec) == transferred) {
  1981. struct compat_iovec *ciov = src;
  1982. unsigned i;
  1983. /*
  1984. * With this interface a 32bit server cannot support
  1985. * non-compat (i.e. ones coming from 64bit apps) ioctl
  1986. * requests
  1987. */
  1988. if (!is_compat)
  1989. return -EINVAL;
  1990. for (i = 0; i < count; i++) {
  1991. dst[i].iov_base = compat_ptr(ciov[i].iov_base);
  1992. dst[i].iov_len = ciov[i].iov_len;
  1993. }
  1994. return 0;
  1995. }
  1996. #endif
  1997. if (count * sizeof(struct iovec) != transferred)
  1998. return -EIO;
  1999. memcpy(dst, src, transferred);
  2000. return 0;
  2001. }
  2002. /* Make sure iov_length() won't overflow */
  2003. static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
  2004. {
  2005. size_t n;
  2006. u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
  2007. for (n = 0; n < count; n++, iov++) {
  2008. if (iov->iov_len > (size_t) max)
  2009. return -ENOMEM;
  2010. max -= iov->iov_len;
  2011. }
  2012. return 0;
  2013. }
  2014. static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
  2015. void *src, size_t transferred, unsigned count,
  2016. bool is_compat)
  2017. {
  2018. unsigned i;
  2019. struct fuse_ioctl_iovec *fiov = src;
  2020. if (fc->minor < 16) {
  2021. return fuse_copy_ioctl_iovec_old(dst, src, transferred,
  2022. count, is_compat);
  2023. }
  2024. if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
  2025. return -EIO;
  2026. for (i = 0; i < count; i++) {
  2027. /* Did the server supply an inappropriate value? */
  2028. if (fiov[i].base != (unsigned long) fiov[i].base ||
  2029. fiov[i].len != (unsigned long) fiov[i].len)
  2030. return -EIO;
  2031. dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
  2032. dst[i].iov_len = (size_t) fiov[i].len;
  2033. #ifdef CONFIG_COMPAT
  2034. if (is_compat &&
  2035. (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
  2036. (compat_size_t) dst[i].iov_len != fiov[i].len))
  2037. return -EIO;
  2038. #endif
  2039. }
  2040. return 0;
  2041. }
  2042. /*
  2043. * For ioctls, there is no generic way to determine how much memory
  2044. * needs to be read and/or written. Furthermore, ioctls are allowed
  2045. * to dereference the passed pointer, so the parameter requires deep
  2046. * copying but FUSE has no idea whatsoever about what to copy in or
  2047. * out.
  2048. *
  2049. * This is solved by allowing FUSE server to retry ioctl with
  2050. * necessary in/out iovecs. Let's assume the ioctl implementation
  2051. * needs to read in the following structure.
  2052. *
  2053. * struct a {
  2054. * char *buf;
  2055. * size_t buflen;
  2056. * }
  2057. *
  2058. * On the first callout to FUSE server, inarg->in_size and
  2059. * inarg->out_size will be NULL; then, the server completes the ioctl
  2060. * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
  2061. * the actual iov array to
  2062. *
  2063. * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
  2064. *
  2065. * which tells FUSE to copy in the requested area and retry the ioctl.
  2066. * On the second round, the server has access to the structure and
  2067. * from that it can tell what to look for next, so on the invocation,
  2068. * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
  2069. *
  2070. * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
  2071. * { .iov_base = a.buf, .iov_len = a.buflen } }
  2072. *
  2073. * FUSE will copy both struct a and the pointed buffer from the
  2074. * process doing the ioctl and retry ioctl with both struct a and the
  2075. * buffer.
  2076. *
  2077. * This time, FUSE server has everything it needs and completes ioctl
  2078. * without FUSE_IOCTL_RETRY which finishes the ioctl call.
  2079. *
  2080. * Copying data out works the same way.
  2081. *
  2082. * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
  2083. * automatically initializes in and out iovs by decoding @cmd with
  2084. * _IOC_* macros and the server is not allowed to request RETRY. This
  2085. * limits ioctl data transfers to well-formed ioctls and is the forced
  2086. * behavior for all FUSE servers.
  2087. */
  2088. long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
  2089. unsigned int flags)
  2090. {
  2091. struct fuse_file *ff = file->private_data;
  2092. struct fuse_conn *fc = ff->fc;
  2093. struct fuse_ioctl_in inarg = {
  2094. .fh = ff->fh,
  2095. .cmd = cmd,
  2096. .arg = arg,
  2097. .flags = flags
  2098. };
  2099. struct fuse_ioctl_out outarg;
  2100. struct fuse_req *req = NULL;
  2101. struct page **pages = NULL;
  2102. struct iovec *iov_page = NULL;
  2103. struct iovec *in_iov = NULL, *out_iov = NULL;
  2104. unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
  2105. size_t in_size, out_size, transferred, c;
  2106. int err, i;
  2107. struct iov_iter ii;
  2108. #if BITS_PER_LONG == 32
  2109. inarg.flags |= FUSE_IOCTL_32BIT;
  2110. #else
  2111. if (flags & FUSE_IOCTL_COMPAT)
  2112. inarg.flags |= FUSE_IOCTL_32BIT;
  2113. #endif
  2114. /* assume all the iovs returned by client always fits in a page */
  2115. BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
  2116. err = -ENOMEM;
  2117. pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
  2118. iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
  2119. if (!pages || !iov_page)
  2120. goto out;
  2121. /*
  2122. * If restricted, initialize IO parameters as encoded in @cmd.
  2123. * RETRY from server is not allowed.
  2124. */
  2125. if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
  2126. struct iovec *iov = iov_page;
  2127. iov->iov_base = (void __user *)arg;
  2128. iov->iov_len = _IOC_SIZE(cmd);
  2129. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  2130. in_iov = iov;
  2131. in_iovs = 1;
  2132. }
  2133. if (_IOC_DIR(cmd) & _IOC_READ) {
  2134. out_iov = iov;
  2135. out_iovs = 1;
  2136. }
  2137. }
  2138. retry:
  2139. inarg.in_size = in_size = iov_length(in_iov, in_iovs);
  2140. inarg.out_size = out_size = iov_length(out_iov, out_iovs);
  2141. /*
  2142. * Out data can be used either for actual out data or iovs,
  2143. * make sure there always is at least one page.
  2144. */
  2145. out_size = max_t(size_t, out_size, PAGE_SIZE);
  2146. max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
  2147. /* make sure there are enough buffer pages and init request with them */
  2148. err = -ENOMEM;
  2149. if (max_pages > FUSE_MAX_PAGES_PER_REQ)
  2150. goto out;
  2151. while (num_pages < max_pages) {
  2152. pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  2153. if (!pages[num_pages])
  2154. goto out;
  2155. num_pages++;
  2156. }
  2157. req = fuse_get_req(fc, num_pages);
  2158. if (IS_ERR(req)) {
  2159. err = PTR_ERR(req);
  2160. req = NULL;
  2161. goto out;
  2162. }
  2163. memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
  2164. req->num_pages = num_pages;
  2165. fuse_page_descs_length_init(req, 0, req->num_pages);
  2166. /* okay, let's send it to the client */
  2167. req->in.h.opcode = FUSE_IOCTL;
  2168. req->in.h.nodeid = ff->nodeid;
  2169. req->in.numargs = 1;
  2170. req->in.args[0].size = sizeof(inarg);
  2171. req->in.args[0].value = &inarg;
  2172. if (in_size) {
  2173. req->in.numargs++;
  2174. req->in.args[1].size = in_size;
  2175. req->in.argpages = 1;
  2176. err = -EFAULT;
  2177. iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
  2178. for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
  2179. c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
  2180. if (c != PAGE_SIZE && iov_iter_count(&ii))
  2181. goto out;
  2182. }
  2183. }
  2184. req->out.numargs = 2;
  2185. req->out.args[0].size = sizeof(outarg);
  2186. req->out.args[0].value = &outarg;
  2187. req->out.args[1].size = out_size;
  2188. req->out.argpages = 1;
  2189. req->out.argvar = 1;
  2190. fuse_request_send(fc, req);
  2191. err = req->out.h.error;
  2192. transferred = req->out.args[1].size;
  2193. fuse_put_request(fc, req);
  2194. req = NULL;
  2195. if (err)
  2196. goto out;
  2197. /* did it ask for retry? */
  2198. if (outarg.flags & FUSE_IOCTL_RETRY) {
  2199. void *vaddr;
  2200. /* no retry if in restricted mode */
  2201. err = -EIO;
  2202. if (!(flags & FUSE_IOCTL_UNRESTRICTED))
  2203. goto out;
  2204. in_iovs = outarg.in_iovs;
  2205. out_iovs = outarg.out_iovs;
  2206. /*
  2207. * Make sure things are in boundary, separate checks
  2208. * are to protect against overflow.
  2209. */
  2210. err = -ENOMEM;
  2211. if (in_iovs > FUSE_IOCTL_MAX_IOV ||
  2212. out_iovs > FUSE_IOCTL_MAX_IOV ||
  2213. in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
  2214. goto out;
  2215. vaddr = kmap_atomic(pages[0]);
  2216. err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
  2217. transferred, in_iovs + out_iovs,
  2218. (flags & FUSE_IOCTL_COMPAT) != 0);
  2219. kunmap_atomic(vaddr);
  2220. if (err)
  2221. goto out;
  2222. in_iov = iov_page;
  2223. out_iov = in_iov + in_iovs;
  2224. err = fuse_verify_ioctl_iov(in_iov, in_iovs);
  2225. if (err)
  2226. goto out;
  2227. err = fuse_verify_ioctl_iov(out_iov, out_iovs);
  2228. if (err)
  2229. goto out;
  2230. goto retry;
  2231. }
  2232. err = -EIO;
  2233. if (transferred > inarg.out_size)
  2234. goto out;
  2235. err = -EFAULT;
  2236. iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
  2237. for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
  2238. c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
  2239. if (c != PAGE_SIZE && iov_iter_count(&ii))
  2240. goto out;
  2241. }
  2242. err = 0;
  2243. out:
  2244. if (req)
  2245. fuse_put_request(fc, req);
  2246. free_page((unsigned long) iov_page);
  2247. while (num_pages)
  2248. __free_page(pages[--num_pages]);
  2249. kfree(pages);
  2250. return err ? err : outarg.result;
  2251. }
  2252. EXPORT_SYMBOL_GPL(fuse_do_ioctl);
  2253. long fuse_ioctl_common(struct file *file, unsigned int cmd,
  2254. unsigned long arg, unsigned int flags)
  2255. {
  2256. struct inode *inode = file_inode(file);
  2257. struct fuse_conn *fc = get_fuse_conn(inode);
  2258. if (!fuse_allow_current_process(fc))
  2259. return -EACCES;
  2260. if (is_bad_inode(inode))
  2261. return -EIO;
  2262. return fuse_do_ioctl(file, cmd, arg, flags);
  2263. }
  2264. static long fuse_file_ioctl(struct file *file, unsigned int cmd,
  2265. unsigned long arg)
  2266. {
  2267. return fuse_ioctl_common(file, cmd, arg, 0);
  2268. }
  2269. static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
  2270. unsigned long arg)
  2271. {
  2272. return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
  2273. }
  2274. /*
  2275. * All files which have been polled are linked to RB tree
  2276. * fuse_conn->polled_files which is indexed by kh. Walk the tree and
  2277. * find the matching one.
  2278. */
  2279. static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
  2280. struct rb_node **parent_out)
  2281. {
  2282. struct rb_node **link = &fc->polled_files.rb_node;
  2283. struct rb_node *last = NULL;
  2284. while (*link) {
  2285. struct fuse_file *ff;
  2286. last = *link;
  2287. ff = rb_entry(last, struct fuse_file, polled_node);
  2288. if (kh < ff->kh)
  2289. link = &last->rb_left;
  2290. else if (kh > ff->kh)
  2291. link = &last->rb_right;
  2292. else
  2293. return link;
  2294. }
  2295. if (parent_out)
  2296. *parent_out = last;
  2297. return link;
  2298. }
  2299. /*
  2300. * The file is about to be polled. Make sure it's on the polled_files
  2301. * RB tree. Note that files once added to the polled_files tree are
  2302. * not removed before the file is released. This is because a file
  2303. * polled once is likely to be polled again.
  2304. */
  2305. static void fuse_register_polled_file(struct fuse_conn *fc,
  2306. struct fuse_file *ff)
  2307. {
  2308. spin_lock(&fc->lock);
  2309. if (RB_EMPTY_NODE(&ff->polled_node)) {
  2310. struct rb_node **link, *uninitialized_var(parent);
  2311. link = fuse_find_polled_node(fc, ff->kh, &parent);
  2312. BUG_ON(*link);
  2313. rb_link_node(&ff->polled_node, parent, link);
  2314. rb_insert_color(&ff->polled_node, &fc->polled_files);
  2315. }
  2316. spin_unlock(&fc->lock);
  2317. }
  2318. unsigned fuse_file_poll(struct file *file, poll_table *wait)
  2319. {
  2320. struct fuse_file *ff = file->private_data;
  2321. struct fuse_conn *fc = ff->fc;
  2322. struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
  2323. struct fuse_poll_out outarg;
  2324. FUSE_ARGS(args);
  2325. int err;
  2326. if (fc->no_poll)
  2327. return DEFAULT_POLLMASK;
  2328. poll_wait(file, &ff->poll_wait, wait);
  2329. inarg.events = (__u32)poll_requested_events(wait);
  2330. /*
  2331. * Ask for notification iff there's someone waiting for it.
  2332. * The client may ignore the flag and always notify.
  2333. */
  2334. if (waitqueue_active(&ff->poll_wait)) {
  2335. inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
  2336. fuse_register_polled_file(fc, ff);
  2337. }
  2338. args.in.h.opcode = FUSE_POLL;
  2339. args.in.h.nodeid = ff->nodeid;
  2340. args.in.numargs = 1;
  2341. args.in.args[0].size = sizeof(inarg);
  2342. args.in.args[0].value = &inarg;
  2343. args.out.numargs = 1;
  2344. args.out.args[0].size = sizeof(outarg);
  2345. args.out.args[0].value = &outarg;
  2346. err = fuse_simple_request(fc, &args);
  2347. if (!err)
  2348. return outarg.revents;
  2349. if (err == -ENOSYS) {
  2350. fc->no_poll = 1;
  2351. return DEFAULT_POLLMASK;
  2352. }
  2353. return POLLERR;
  2354. }
  2355. EXPORT_SYMBOL_GPL(fuse_file_poll);
  2356. /*
  2357. * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
  2358. * wakes up the poll waiters.
  2359. */
  2360. int fuse_notify_poll_wakeup(struct fuse_conn *fc,
  2361. struct fuse_notify_poll_wakeup_out *outarg)
  2362. {
  2363. u64 kh = outarg->kh;
  2364. struct rb_node **link;
  2365. spin_lock(&fc->lock);
  2366. link = fuse_find_polled_node(fc, kh, NULL);
  2367. if (*link) {
  2368. struct fuse_file *ff;
  2369. ff = rb_entry(*link, struct fuse_file, polled_node);
  2370. wake_up_interruptible_sync(&ff->poll_wait);
  2371. }
  2372. spin_unlock(&fc->lock);
  2373. return 0;
  2374. }
  2375. static void fuse_do_truncate(struct file *file)
  2376. {
  2377. struct inode *inode = file->f_mapping->host;
  2378. struct iattr attr;
  2379. attr.ia_valid = ATTR_SIZE;
  2380. attr.ia_size = i_size_read(inode);
  2381. attr.ia_file = file;
  2382. attr.ia_valid |= ATTR_FILE;
  2383. fuse_do_setattr(file_dentry(file), &attr, file);
  2384. }
  2385. static inline loff_t fuse_round_up(loff_t off)
  2386. {
  2387. return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
  2388. }
  2389. static ssize_t
  2390. fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  2391. {
  2392. DECLARE_COMPLETION_ONSTACK(wait);
  2393. ssize_t ret = 0;
  2394. struct file *file = iocb->ki_filp;
  2395. struct fuse_file *ff = file->private_data;
  2396. bool async_dio = ff->fc->async_dio;
  2397. loff_t pos = 0;
  2398. struct inode *inode;
  2399. loff_t i_size;
  2400. size_t count = iov_iter_count(iter);
  2401. loff_t offset = iocb->ki_pos;
  2402. struct fuse_io_priv *io;
  2403. pos = offset;
  2404. inode = file->f_mapping->host;
  2405. i_size = i_size_read(inode);
  2406. if ((iov_iter_rw(iter) == READ) && (offset > i_size))
  2407. return 0;
  2408. /* optimization for short read */
  2409. if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
  2410. if (offset >= i_size)
  2411. return 0;
  2412. iov_iter_truncate(iter, fuse_round_up(i_size - offset));
  2413. count = iov_iter_count(iter);
  2414. }
  2415. io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
  2416. if (!io)
  2417. return -ENOMEM;
  2418. spin_lock_init(&io->lock);
  2419. kref_init(&io->refcnt);
  2420. io->reqs = 1;
  2421. io->bytes = -1;
  2422. io->size = 0;
  2423. io->offset = offset;
  2424. io->write = (iov_iter_rw(iter) == WRITE);
  2425. io->err = 0;
  2426. io->file = file;
  2427. /*
  2428. * By default, we want to optimize all I/Os with async request
  2429. * submission to the client filesystem if supported.
  2430. */
  2431. io->async = async_dio;
  2432. io->iocb = iocb;
  2433. io->blocking = is_sync_kiocb(iocb);
  2434. /*
  2435. * We cannot asynchronously extend the size of a file.
  2436. * In such case the aio will behave exactly like sync io.
  2437. */
  2438. if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
  2439. io->blocking = true;
  2440. if (io->async && io->blocking) {
  2441. /*
  2442. * Additional reference to keep io around after
  2443. * calling fuse_aio_complete()
  2444. */
  2445. kref_get(&io->refcnt);
  2446. io->done = &wait;
  2447. }
  2448. if (iov_iter_rw(iter) == WRITE) {
  2449. ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
  2450. fuse_invalidate_attr(inode);
  2451. } else {
  2452. ret = __fuse_direct_read(io, iter, &pos);
  2453. }
  2454. if (io->async) {
  2455. fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
  2456. /* we have a non-extending, async request, so return */
  2457. if (!io->blocking)
  2458. return -EIOCBQUEUED;
  2459. wait_for_completion(&wait);
  2460. ret = fuse_get_res_by_io(io);
  2461. }
  2462. kref_put(&io->refcnt, fuse_io_release);
  2463. if (iov_iter_rw(iter) == WRITE) {
  2464. if (ret > 0)
  2465. fuse_write_update_size(inode, pos);
  2466. else if (ret < 0 && offset + count > i_size)
  2467. fuse_do_truncate(file);
  2468. }
  2469. return ret;
  2470. }
  2471. static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
  2472. loff_t length)
  2473. {
  2474. struct fuse_file *ff = file->private_data;
  2475. struct inode *inode = file_inode(file);
  2476. struct fuse_inode *fi = get_fuse_inode(inode);
  2477. struct fuse_conn *fc = ff->fc;
  2478. FUSE_ARGS(args);
  2479. struct fuse_fallocate_in inarg = {
  2480. .fh = ff->fh,
  2481. .offset = offset,
  2482. .length = length,
  2483. .mode = mode
  2484. };
  2485. int err;
  2486. bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
  2487. (mode & FALLOC_FL_PUNCH_HOLE);
  2488. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  2489. return -EOPNOTSUPP;
  2490. if (fc->no_fallocate)
  2491. return -EOPNOTSUPP;
  2492. if (lock_inode) {
  2493. inode_lock(inode);
  2494. if (mode & FALLOC_FL_PUNCH_HOLE) {
  2495. loff_t endbyte = offset + length - 1;
  2496. err = filemap_write_and_wait_range(inode->i_mapping,
  2497. offset, endbyte);
  2498. if (err)
  2499. goto out;
  2500. fuse_sync_writes(inode);
  2501. }
  2502. }
  2503. if (!(mode & FALLOC_FL_KEEP_SIZE))
  2504. set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  2505. args.in.h.opcode = FUSE_FALLOCATE;
  2506. args.in.h.nodeid = ff->nodeid;
  2507. args.in.numargs = 1;
  2508. args.in.args[0].size = sizeof(inarg);
  2509. args.in.args[0].value = &inarg;
  2510. err = fuse_simple_request(fc, &args);
  2511. if (err == -ENOSYS) {
  2512. fc->no_fallocate = 1;
  2513. err = -EOPNOTSUPP;
  2514. }
  2515. if (err)
  2516. goto out;
  2517. /* we could have extended the file */
  2518. if (!(mode & FALLOC_FL_KEEP_SIZE)) {
  2519. bool changed = fuse_write_update_size(inode, offset + length);
  2520. if (changed && fc->writeback_cache)
  2521. file_update_time(file);
  2522. }
  2523. if (mode & FALLOC_FL_PUNCH_HOLE)
  2524. truncate_pagecache_range(inode, offset, offset + length - 1);
  2525. fuse_invalidate_attr(inode);
  2526. out:
  2527. if (!(mode & FALLOC_FL_KEEP_SIZE))
  2528. clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  2529. if (lock_inode)
  2530. inode_unlock(inode);
  2531. return err;
  2532. }
  2533. static const struct file_operations fuse_file_operations = {
  2534. .llseek = fuse_file_llseek,
  2535. .read_iter = fuse_file_read_iter,
  2536. .write_iter = fuse_file_write_iter,
  2537. .mmap = fuse_file_mmap,
  2538. .open = fuse_open,
  2539. .flush = fuse_flush,
  2540. .release = fuse_release,
  2541. .fsync = fuse_fsync,
  2542. .lock = fuse_file_lock,
  2543. .flock = fuse_file_flock,
  2544. .splice_read = generic_file_splice_read,
  2545. .unlocked_ioctl = fuse_file_ioctl,
  2546. .compat_ioctl = fuse_file_compat_ioctl,
  2547. .poll = fuse_file_poll,
  2548. .fallocate = fuse_file_fallocate,
  2549. };
  2550. static const struct file_operations fuse_direct_io_file_operations = {
  2551. .llseek = fuse_file_llseek,
  2552. .read_iter = fuse_direct_read_iter,
  2553. .write_iter = fuse_direct_write_iter,
  2554. .mmap = fuse_direct_mmap,
  2555. .open = fuse_open,
  2556. .flush = fuse_flush,
  2557. .release = fuse_release,
  2558. .fsync = fuse_fsync,
  2559. .lock = fuse_file_lock,
  2560. .flock = fuse_file_flock,
  2561. .unlocked_ioctl = fuse_file_ioctl,
  2562. .compat_ioctl = fuse_file_compat_ioctl,
  2563. .poll = fuse_file_poll,
  2564. .fallocate = fuse_file_fallocate,
  2565. /* no splice_read */
  2566. };
  2567. static const struct address_space_operations fuse_file_aops = {
  2568. .readpage = fuse_readpage,
  2569. .writepage = fuse_writepage,
  2570. .writepages = fuse_writepages,
  2571. .launder_page = fuse_launder_page,
  2572. .readpages = fuse_readpages,
  2573. .set_page_dirty = __set_page_dirty_nobuffers,
  2574. .bmap = fuse_bmap,
  2575. .direct_IO = fuse_direct_IO,
  2576. .write_begin = fuse_write_begin,
  2577. .write_end = fuse_write_end,
  2578. };
  2579. void fuse_init_file_inode(struct inode *inode)
  2580. {
  2581. inode->i_fop = &fuse_file_operations;
  2582. inode->i_data.a_ops = &fuse_file_aops;
  2583. }