file.c 49 KB

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