file.c 76 KB

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