file.c 76 KB

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