file.c 52 KB

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