file.c 52 KB

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