file.c 51 KB

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