file.c 52 KB

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