file.c 63 KB

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