file.c 53 KB

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