file.c 48 KB

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