file.c 54 KB

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