file.c 54 KB

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