file.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  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. if (offset == 0) {
  1322. retval = file->f_pos;
  1323. goto exit;
  1324. }
  1325. offset += file->f_pos;
  1326. break;
  1327. case SEEK_DATA:
  1328. if (offset >= i_size_read(inode)) {
  1329. retval = -ENXIO;
  1330. goto exit;
  1331. }
  1332. break;
  1333. case SEEK_HOLE:
  1334. if (offset >= i_size_read(inode)) {
  1335. retval = -ENXIO;
  1336. goto exit;
  1337. }
  1338. offset = i_size_read(inode);
  1339. break;
  1340. }
  1341. retval = -EINVAL;
  1342. if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
  1343. if (offset != file->f_pos) {
  1344. file->f_pos = offset;
  1345. file->f_version = 0;
  1346. }
  1347. retval = offset;
  1348. }
  1349. exit:
  1350. mutex_unlock(&inode->i_mutex);
  1351. return retval;
  1352. }
  1353. static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
  1354. unsigned int nr_segs, size_t bytes, bool to_user)
  1355. {
  1356. struct iov_iter ii;
  1357. int page_idx = 0;
  1358. if (!bytes)
  1359. return 0;
  1360. iov_iter_init(&ii, iov, nr_segs, bytes, 0);
  1361. while (iov_iter_count(&ii)) {
  1362. struct page *page = pages[page_idx++];
  1363. size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
  1364. void *kaddr;
  1365. kaddr = kmap(page);
  1366. while (todo) {
  1367. char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
  1368. size_t iov_len = ii.iov->iov_len - ii.iov_offset;
  1369. size_t copy = min(todo, iov_len);
  1370. size_t left;
  1371. if (!to_user)
  1372. left = copy_from_user(kaddr, uaddr, copy);
  1373. else
  1374. left = copy_to_user(uaddr, kaddr, copy);
  1375. if (unlikely(left))
  1376. return -EFAULT;
  1377. iov_iter_advance(&ii, copy);
  1378. todo -= copy;
  1379. kaddr += copy;
  1380. }
  1381. kunmap(page);
  1382. }
  1383. return 0;
  1384. }
  1385. /*
  1386. * CUSE servers compiled on 32bit broke on 64bit kernels because the
  1387. * ABI was defined to be 'struct iovec' which is different on 32bit
  1388. * and 64bit. Fortunately we can determine which structure the server
  1389. * used from the size of the reply.
  1390. */
  1391. static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
  1392. size_t transferred, unsigned count,
  1393. bool is_compat)
  1394. {
  1395. #ifdef CONFIG_COMPAT
  1396. if (count * sizeof(struct compat_iovec) == transferred) {
  1397. struct compat_iovec *ciov = src;
  1398. unsigned i;
  1399. /*
  1400. * With this interface a 32bit server cannot support
  1401. * non-compat (i.e. ones coming from 64bit apps) ioctl
  1402. * requests
  1403. */
  1404. if (!is_compat)
  1405. return -EINVAL;
  1406. for (i = 0; i < count; i++) {
  1407. dst[i].iov_base = compat_ptr(ciov[i].iov_base);
  1408. dst[i].iov_len = ciov[i].iov_len;
  1409. }
  1410. return 0;
  1411. }
  1412. #endif
  1413. if (count * sizeof(struct iovec) != transferred)
  1414. return -EIO;
  1415. memcpy(dst, src, transferred);
  1416. return 0;
  1417. }
  1418. /* Make sure iov_length() won't overflow */
  1419. static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
  1420. {
  1421. size_t n;
  1422. u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
  1423. for (n = 0; n < count; n++) {
  1424. if (iov->iov_len > (size_t) max)
  1425. return -ENOMEM;
  1426. max -= iov->iov_len;
  1427. }
  1428. return 0;
  1429. }
  1430. static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
  1431. void *src, size_t transferred, unsigned count,
  1432. bool is_compat)
  1433. {
  1434. unsigned i;
  1435. struct fuse_ioctl_iovec *fiov = src;
  1436. if (fc->minor < 16) {
  1437. return fuse_copy_ioctl_iovec_old(dst, src, transferred,
  1438. count, is_compat);
  1439. }
  1440. if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
  1441. return -EIO;
  1442. for (i = 0; i < count; i++) {
  1443. /* Did the server supply an inappropriate value? */
  1444. if (fiov[i].base != (unsigned long) fiov[i].base ||
  1445. fiov[i].len != (unsigned long) fiov[i].len)
  1446. return -EIO;
  1447. dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
  1448. dst[i].iov_len = (size_t) fiov[i].len;
  1449. #ifdef CONFIG_COMPAT
  1450. if (is_compat &&
  1451. (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
  1452. (compat_size_t) dst[i].iov_len != fiov[i].len))
  1453. return -EIO;
  1454. #endif
  1455. }
  1456. return 0;
  1457. }
  1458. /*
  1459. * For ioctls, there is no generic way to determine how much memory
  1460. * needs to be read and/or written. Furthermore, ioctls are allowed
  1461. * to dereference the passed pointer, so the parameter requires deep
  1462. * copying but FUSE has no idea whatsoever about what to copy in or
  1463. * out.
  1464. *
  1465. * This is solved by allowing FUSE server to retry ioctl with
  1466. * necessary in/out iovecs. Let's assume the ioctl implementation
  1467. * needs to read in the following structure.
  1468. *
  1469. * struct a {
  1470. * char *buf;
  1471. * size_t buflen;
  1472. * }
  1473. *
  1474. * On the first callout to FUSE server, inarg->in_size and
  1475. * inarg->out_size will be NULL; then, the server completes the ioctl
  1476. * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
  1477. * the actual iov array to
  1478. *
  1479. * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
  1480. *
  1481. * which tells FUSE to copy in the requested area and retry the ioctl.
  1482. * On the second round, the server has access to the structure and
  1483. * from that it can tell what to look for next, so on the invocation,
  1484. * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
  1485. *
  1486. * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
  1487. * { .iov_base = a.buf, .iov_len = a.buflen } }
  1488. *
  1489. * FUSE will copy both struct a and the pointed buffer from the
  1490. * process doing the ioctl and retry ioctl with both struct a and the
  1491. * buffer.
  1492. *
  1493. * This time, FUSE server has everything it needs and completes ioctl
  1494. * without FUSE_IOCTL_RETRY which finishes the ioctl call.
  1495. *
  1496. * Copying data out works the same way.
  1497. *
  1498. * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
  1499. * automatically initializes in and out iovs by decoding @cmd with
  1500. * _IOC_* macros and the server is not allowed to request RETRY. This
  1501. * limits ioctl data transfers to well-formed ioctls and is the forced
  1502. * behavior for all FUSE servers.
  1503. */
  1504. long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
  1505. unsigned int flags)
  1506. {
  1507. struct fuse_file *ff = file->private_data;
  1508. struct fuse_conn *fc = ff->fc;
  1509. struct fuse_ioctl_in inarg = {
  1510. .fh = ff->fh,
  1511. .cmd = cmd,
  1512. .arg = arg,
  1513. .flags = flags
  1514. };
  1515. struct fuse_ioctl_out outarg;
  1516. struct fuse_req *req = NULL;
  1517. struct page **pages = NULL;
  1518. struct iovec *iov_page = NULL;
  1519. struct iovec *in_iov = NULL, *out_iov = NULL;
  1520. unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
  1521. size_t in_size, out_size, transferred;
  1522. int err;
  1523. #if BITS_PER_LONG == 32
  1524. inarg.flags |= FUSE_IOCTL_32BIT;
  1525. #else
  1526. if (flags & FUSE_IOCTL_COMPAT)
  1527. inarg.flags |= FUSE_IOCTL_32BIT;
  1528. #endif
  1529. /* assume all the iovs returned by client always fits in a page */
  1530. BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
  1531. err = -ENOMEM;
  1532. pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
  1533. iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
  1534. if (!pages || !iov_page)
  1535. goto out;
  1536. /*
  1537. * If restricted, initialize IO parameters as encoded in @cmd.
  1538. * RETRY from server is not allowed.
  1539. */
  1540. if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
  1541. struct iovec *iov = iov_page;
  1542. iov->iov_base = (void __user *)arg;
  1543. iov->iov_len = _IOC_SIZE(cmd);
  1544. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  1545. in_iov = iov;
  1546. in_iovs = 1;
  1547. }
  1548. if (_IOC_DIR(cmd) & _IOC_READ) {
  1549. out_iov = iov;
  1550. out_iovs = 1;
  1551. }
  1552. }
  1553. retry:
  1554. inarg.in_size = in_size = iov_length(in_iov, in_iovs);
  1555. inarg.out_size = out_size = iov_length(out_iov, out_iovs);
  1556. /*
  1557. * Out data can be used either for actual out data or iovs,
  1558. * make sure there always is at least one page.
  1559. */
  1560. out_size = max_t(size_t, out_size, PAGE_SIZE);
  1561. max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
  1562. /* make sure there are enough buffer pages and init request with them */
  1563. err = -ENOMEM;
  1564. if (max_pages > FUSE_MAX_PAGES_PER_REQ)
  1565. goto out;
  1566. while (num_pages < max_pages) {
  1567. pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  1568. if (!pages[num_pages])
  1569. goto out;
  1570. num_pages++;
  1571. }
  1572. req = fuse_get_req(fc);
  1573. if (IS_ERR(req)) {
  1574. err = PTR_ERR(req);
  1575. req = NULL;
  1576. goto out;
  1577. }
  1578. memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
  1579. req->num_pages = num_pages;
  1580. /* okay, let's send it to the client */
  1581. req->in.h.opcode = FUSE_IOCTL;
  1582. req->in.h.nodeid = ff->nodeid;
  1583. req->in.numargs = 1;
  1584. req->in.args[0].size = sizeof(inarg);
  1585. req->in.args[0].value = &inarg;
  1586. if (in_size) {
  1587. req->in.numargs++;
  1588. req->in.args[1].size = in_size;
  1589. req->in.argpages = 1;
  1590. err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
  1591. false);
  1592. if (err)
  1593. goto out;
  1594. }
  1595. req->out.numargs = 2;
  1596. req->out.args[0].size = sizeof(outarg);
  1597. req->out.args[0].value = &outarg;
  1598. req->out.args[1].size = out_size;
  1599. req->out.argpages = 1;
  1600. req->out.argvar = 1;
  1601. fuse_request_send(fc, req);
  1602. err = req->out.h.error;
  1603. transferred = req->out.args[1].size;
  1604. fuse_put_request(fc, req);
  1605. req = NULL;
  1606. if (err)
  1607. goto out;
  1608. /* did it ask for retry? */
  1609. if (outarg.flags & FUSE_IOCTL_RETRY) {
  1610. void *vaddr;
  1611. /* no retry if in restricted mode */
  1612. err = -EIO;
  1613. if (!(flags & FUSE_IOCTL_UNRESTRICTED))
  1614. goto out;
  1615. in_iovs = outarg.in_iovs;
  1616. out_iovs = outarg.out_iovs;
  1617. /*
  1618. * Make sure things are in boundary, separate checks
  1619. * are to protect against overflow.
  1620. */
  1621. err = -ENOMEM;
  1622. if (in_iovs > FUSE_IOCTL_MAX_IOV ||
  1623. out_iovs > FUSE_IOCTL_MAX_IOV ||
  1624. in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
  1625. goto out;
  1626. vaddr = kmap_atomic(pages[0], KM_USER0);
  1627. err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
  1628. transferred, in_iovs + out_iovs,
  1629. (flags & FUSE_IOCTL_COMPAT) != 0);
  1630. kunmap_atomic(vaddr, KM_USER0);
  1631. if (err)
  1632. goto out;
  1633. in_iov = iov_page;
  1634. out_iov = in_iov + in_iovs;
  1635. err = fuse_verify_ioctl_iov(in_iov, in_iovs);
  1636. if (err)
  1637. goto out;
  1638. err = fuse_verify_ioctl_iov(out_iov, out_iovs);
  1639. if (err)
  1640. goto out;
  1641. goto retry;
  1642. }
  1643. err = -EIO;
  1644. if (transferred > inarg.out_size)
  1645. goto out;
  1646. err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
  1647. out:
  1648. if (req)
  1649. fuse_put_request(fc, req);
  1650. free_page((unsigned long) iov_page);
  1651. while (num_pages)
  1652. __free_page(pages[--num_pages]);
  1653. kfree(pages);
  1654. return err ? err : outarg.result;
  1655. }
  1656. EXPORT_SYMBOL_GPL(fuse_do_ioctl);
  1657. static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
  1658. unsigned long arg, unsigned int flags)
  1659. {
  1660. struct inode *inode = file->f_dentry->d_inode;
  1661. struct fuse_conn *fc = get_fuse_conn(inode);
  1662. if (!fuse_allow_task(fc, current))
  1663. return -EACCES;
  1664. if (is_bad_inode(inode))
  1665. return -EIO;
  1666. return fuse_do_ioctl(file, cmd, arg, flags);
  1667. }
  1668. static long fuse_file_ioctl(struct file *file, unsigned int cmd,
  1669. unsigned long arg)
  1670. {
  1671. return fuse_file_ioctl_common(file, cmd, arg, 0);
  1672. }
  1673. static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
  1674. unsigned long arg)
  1675. {
  1676. return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
  1677. }
  1678. /*
  1679. * All files which have been polled are linked to RB tree
  1680. * fuse_conn->polled_files which is indexed by kh. Walk the tree and
  1681. * find the matching one.
  1682. */
  1683. static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
  1684. struct rb_node **parent_out)
  1685. {
  1686. struct rb_node **link = &fc->polled_files.rb_node;
  1687. struct rb_node *last = NULL;
  1688. while (*link) {
  1689. struct fuse_file *ff;
  1690. last = *link;
  1691. ff = rb_entry(last, struct fuse_file, polled_node);
  1692. if (kh < ff->kh)
  1693. link = &last->rb_left;
  1694. else if (kh > ff->kh)
  1695. link = &last->rb_right;
  1696. else
  1697. return link;
  1698. }
  1699. if (parent_out)
  1700. *parent_out = last;
  1701. return link;
  1702. }
  1703. /*
  1704. * The file is about to be polled. Make sure it's on the polled_files
  1705. * RB tree. Note that files once added to the polled_files tree are
  1706. * not removed before the file is released. This is because a file
  1707. * polled once is likely to be polled again.
  1708. */
  1709. static void fuse_register_polled_file(struct fuse_conn *fc,
  1710. struct fuse_file *ff)
  1711. {
  1712. spin_lock(&fc->lock);
  1713. if (RB_EMPTY_NODE(&ff->polled_node)) {
  1714. struct rb_node **link, *parent;
  1715. link = fuse_find_polled_node(fc, ff->kh, &parent);
  1716. BUG_ON(*link);
  1717. rb_link_node(&ff->polled_node, parent, link);
  1718. rb_insert_color(&ff->polled_node, &fc->polled_files);
  1719. }
  1720. spin_unlock(&fc->lock);
  1721. }
  1722. unsigned fuse_file_poll(struct file *file, poll_table *wait)
  1723. {
  1724. struct fuse_file *ff = file->private_data;
  1725. struct fuse_conn *fc = ff->fc;
  1726. struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
  1727. struct fuse_poll_out outarg;
  1728. struct fuse_req *req;
  1729. int err;
  1730. if (fc->no_poll)
  1731. return DEFAULT_POLLMASK;
  1732. poll_wait(file, &ff->poll_wait, wait);
  1733. /*
  1734. * Ask for notification iff there's someone waiting for it.
  1735. * The client may ignore the flag and always notify.
  1736. */
  1737. if (waitqueue_active(&ff->poll_wait)) {
  1738. inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
  1739. fuse_register_polled_file(fc, ff);
  1740. }
  1741. req = fuse_get_req(fc);
  1742. if (IS_ERR(req))
  1743. return POLLERR;
  1744. req->in.h.opcode = FUSE_POLL;
  1745. req->in.h.nodeid = ff->nodeid;
  1746. req->in.numargs = 1;
  1747. req->in.args[0].size = sizeof(inarg);
  1748. req->in.args[0].value = &inarg;
  1749. req->out.numargs = 1;
  1750. req->out.args[0].size = sizeof(outarg);
  1751. req->out.args[0].value = &outarg;
  1752. fuse_request_send(fc, req);
  1753. err = req->out.h.error;
  1754. fuse_put_request(fc, req);
  1755. if (!err)
  1756. return outarg.revents;
  1757. if (err == -ENOSYS) {
  1758. fc->no_poll = 1;
  1759. return DEFAULT_POLLMASK;
  1760. }
  1761. return POLLERR;
  1762. }
  1763. EXPORT_SYMBOL_GPL(fuse_file_poll);
  1764. /*
  1765. * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
  1766. * wakes up the poll waiters.
  1767. */
  1768. int fuse_notify_poll_wakeup(struct fuse_conn *fc,
  1769. struct fuse_notify_poll_wakeup_out *outarg)
  1770. {
  1771. u64 kh = outarg->kh;
  1772. struct rb_node **link;
  1773. spin_lock(&fc->lock);
  1774. link = fuse_find_polled_node(fc, kh, NULL);
  1775. if (*link) {
  1776. struct fuse_file *ff;
  1777. ff = rb_entry(*link, struct fuse_file, polled_node);
  1778. wake_up_interruptible_sync(&ff->poll_wait);
  1779. }
  1780. spin_unlock(&fc->lock);
  1781. return 0;
  1782. }
  1783. static const struct file_operations fuse_file_operations = {
  1784. .llseek = fuse_file_llseek,
  1785. .read = do_sync_read,
  1786. .aio_read = fuse_file_aio_read,
  1787. .write = do_sync_write,
  1788. .aio_write = fuse_file_aio_write,
  1789. .mmap = fuse_file_mmap,
  1790. .open = fuse_open,
  1791. .flush = fuse_flush,
  1792. .release = fuse_release,
  1793. .fsync = fuse_fsync,
  1794. .lock = fuse_file_lock,
  1795. .flock = fuse_file_flock,
  1796. .splice_read = generic_file_splice_read,
  1797. .unlocked_ioctl = fuse_file_ioctl,
  1798. .compat_ioctl = fuse_file_compat_ioctl,
  1799. .poll = fuse_file_poll,
  1800. };
  1801. static const struct file_operations fuse_direct_io_file_operations = {
  1802. .llseek = fuse_file_llseek,
  1803. .read = fuse_direct_read,
  1804. .write = fuse_direct_write,
  1805. .mmap = fuse_direct_mmap,
  1806. .open = fuse_open,
  1807. .flush = fuse_flush,
  1808. .release = fuse_release,
  1809. .fsync = fuse_fsync,
  1810. .lock = fuse_file_lock,
  1811. .flock = fuse_file_flock,
  1812. .unlocked_ioctl = fuse_file_ioctl,
  1813. .compat_ioctl = fuse_file_compat_ioctl,
  1814. .poll = fuse_file_poll,
  1815. /* no splice_read */
  1816. };
  1817. static const struct address_space_operations fuse_file_aops = {
  1818. .readpage = fuse_readpage,
  1819. .writepage = fuse_writepage,
  1820. .launder_page = fuse_launder_page,
  1821. .readpages = fuse_readpages,
  1822. .set_page_dirty = __set_page_dirty_nobuffers,
  1823. .bmap = fuse_bmap,
  1824. };
  1825. void fuse_init_file_inode(struct inode *inode)
  1826. {
  1827. inode->i_fop = &fuse_file_operations;
  1828. inode->i_data.a_ops = &fuse_file_aops;
  1829. }