file.c 48 KB

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