file.c 51 KB

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