file.c 73 KB

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