file.c 51 KB

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