file.c 75 KB

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