file.c 54 KB

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