dev.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366
  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/init.h>
  9. #include <linux/module.h>
  10. #include <linux/poll.h>
  11. #include <linux/uio.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/file.h>
  15. #include <linux/slab.h>
  16. #include <linux/pipe_fs_i.h>
  17. #include <linux/swap.h>
  18. #include <linux/splice.h>
  19. MODULE_ALIAS_MISCDEV(FUSE_MINOR);
  20. MODULE_ALIAS("devname:fuse");
  21. static struct kmem_cache *fuse_req_cachep;
  22. static struct fuse_dev *fuse_get_dev(struct file *file)
  23. {
  24. /*
  25. * Lockless access is OK, because file->private data is set
  26. * once during mount and is valid until the file is released.
  27. */
  28. return ACCESS_ONCE(file->private_data);
  29. }
  30. static void fuse_request_init(struct fuse_req *req, struct page **pages,
  31. struct fuse_page_desc *page_descs,
  32. unsigned npages)
  33. {
  34. memset(req, 0, sizeof(*req));
  35. memset(pages, 0, sizeof(*pages) * npages);
  36. memset(page_descs, 0, sizeof(*page_descs) * npages);
  37. INIT_LIST_HEAD(&req->list);
  38. INIT_LIST_HEAD(&req->intr_entry);
  39. init_waitqueue_head(&req->waitq);
  40. atomic_set(&req->count, 1);
  41. req->pages = pages;
  42. req->page_descs = page_descs;
  43. req->max_pages = npages;
  44. __set_bit(FR_PENDING, &req->flags);
  45. }
  46. static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
  47. {
  48. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
  49. if (req) {
  50. struct page **pages;
  51. struct fuse_page_desc *page_descs;
  52. if (npages <= FUSE_REQ_INLINE_PAGES) {
  53. pages = req->inline_pages;
  54. page_descs = req->inline_page_descs;
  55. } else {
  56. pages = kmalloc(sizeof(struct page *) * npages, flags);
  57. page_descs = kmalloc(sizeof(struct fuse_page_desc) *
  58. npages, flags);
  59. }
  60. if (!pages || !page_descs) {
  61. kfree(pages);
  62. kfree(page_descs);
  63. kmem_cache_free(fuse_req_cachep, req);
  64. return NULL;
  65. }
  66. fuse_request_init(req, pages, page_descs, npages);
  67. }
  68. return req;
  69. }
  70. struct fuse_req *fuse_request_alloc(unsigned npages)
  71. {
  72. return __fuse_request_alloc(npages, GFP_KERNEL);
  73. }
  74. EXPORT_SYMBOL_GPL(fuse_request_alloc);
  75. struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
  76. {
  77. return __fuse_request_alloc(npages, GFP_NOFS);
  78. }
  79. void fuse_request_free(struct fuse_req *req)
  80. {
  81. if (req->pages != req->inline_pages) {
  82. kfree(req->pages);
  83. kfree(req->page_descs);
  84. }
  85. kmem_cache_free(fuse_req_cachep, req);
  86. }
  87. static void block_sigs(sigset_t *oldset)
  88. {
  89. sigset_t mask;
  90. siginitsetinv(&mask, sigmask(SIGKILL));
  91. sigprocmask(SIG_BLOCK, &mask, oldset);
  92. }
  93. static void restore_sigs(sigset_t *oldset)
  94. {
  95. sigprocmask(SIG_SETMASK, oldset, NULL);
  96. }
  97. void __fuse_get_request(struct fuse_req *req)
  98. {
  99. atomic_inc(&req->count);
  100. }
  101. /* Must be called with > 1 refcount */
  102. static void __fuse_put_request(struct fuse_req *req)
  103. {
  104. BUG_ON(atomic_read(&req->count) < 2);
  105. atomic_dec(&req->count);
  106. }
  107. static void fuse_req_init_context(struct fuse_req *req)
  108. {
  109. req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
  110. req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
  111. req->in.h.pid = current->pid;
  112. }
  113. void fuse_set_initialized(struct fuse_conn *fc)
  114. {
  115. /* Make sure stores before this are seen on another CPU */
  116. smp_wmb();
  117. fc->initialized = 1;
  118. }
  119. static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
  120. {
  121. return !fc->initialized || (for_background && fc->blocked);
  122. }
  123. static void fuse_drop_waiting(struct fuse_conn *fc)
  124. {
  125. if (fc->connected) {
  126. atomic_dec(&fc->num_waiting);
  127. } else if (atomic_dec_and_test(&fc->num_waiting)) {
  128. /* wake up aborters */
  129. wake_up_all(&fc->blocked_waitq);
  130. }
  131. }
  132. static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
  133. bool for_background)
  134. {
  135. struct fuse_req *req;
  136. int err;
  137. atomic_inc(&fc->num_waiting);
  138. if (fuse_block_alloc(fc, for_background)) {
  139. sigset_t oldset;
  140. int intr;
  141. block_sigs(&oldset);
  142. intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
  143. !fuse_block_alloc(fc, for_background));
  144. restore_sigs(&oldset);
  145. err = -EINTR;
  146. if (intr)
  147. goto out;
  148. }
  149. /* Matches smp_wmb() in fuse_set_initialized() */
  150. smp_rmb();
  151. err = -ENOTCONN;
  152. if (!fc->connected)
  153. goto out;
  154. err = -ECONNREFUSED;
  155. if (fc->conn_error)
  156. goto out;
  157. req = fuse_request_alloc(npages);
  158. err = -ENOMEM;
  159. if (!req) {
  160. if (for_background)
  161. wake_up(&fc->blocked_waitq);
  162. goto out;
  163. }
  164. fuse_req_init_context(req);
  165. __set_bit(FR_WAITING, &req->flags);
  166. if (for_background)
  167. __set_bit(FR_BACKGROUND, &req->flags);
  168. return req;
  169. out:
  170. fuse_drop_waiting(fc);
  171. return ERR_PTR(err);
  172. }
  173. struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
  174. {
  175. return __fuse_get_req(fc, npages, false);
  176. }
  177. EXPORT_SYMBOL_GPL(fuse_get_req);
  178. struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
  179. unsigned npages)
  180. {
  181. return __fuse_get_req(fc, npages, true);
  182. }
  183. EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
  184. /*
  185. * Return request in fuse_file->reserved_req. However that may
  186. * currently be in use. If that is the case, wait for it to become
  187. * available.
  188. */
  189. static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
  190. struct file *file)
  191. {
  192. struct fuse_req *req = NULL;
  193. struct fuse_file *ff = file->private_data;
  194. do {
  195. wait_event(fc->reserved_req_waitq, ff->reserved_req);
  196. spin_lock(&fc->lock);
  197. if (ff->reserved_req) {
  198. req = ff->reserved_req;
  199. ff->reserved_req = NULL;
  200. req->stolen_file = get_file(file);
  201. }
  202. spin_unlock(&fc->lock);
  203. } while (!req);
  204. return req;
  205. }
  206. /*
  207. * Put stolen request back into fuse_file->reserved_req
  208. */
  209. static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
  210. {
  211. struct file *file = req->stolen_file;
  212. struct fuse_file *ff = file->private_data;
  213. spin_lock(&fc->lock);
  214. fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
  215. BUG_ON(ff->reserved_req);
  216. ff->reserved_req = req;
  217. wake_up_all(&fc->reserved_req_waitq);
  218. spin_unlock(&fc->lock);
  219. fput(file);
  220. }
  221. /*
  222. * Gets a requests for a file operation, always succeeds
  223. *
  224. * This is used for sending the FLUSH request, which must get to
  225. * userspace, due to POSIX locks which may need to be unlocked.
  226. *
  227. * If allocation fails due to OOM, use the reserved request in
  228. * fuse_file.
  229. *
  230. * This is very unlikely to deadlock accidentally, since the
  231. * filesystem should not have it's own file open. If deadlock is
  232. * intentional, it can still be broken by "aborting" the filesystem.
  233. */
  234. struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
  235. struct file *file)
  236. {
  237. struct fuse_req *req;
  238. atomic_inc(&fc->num_waiting);
  239. wait_event(fc->blocked_waitq, fc->initialized);
  240. /* Matches smp_wmb() in fuse_set_initialized() */
  241. smp_rmb();
  242. req = fuse_request_alloc(0);
  243. if (!req)
  244. req = get_reserved_req(fc, file);
  245. fuse_req_init_context(req);
  246. __set_bit(FR_WAITING, &req->flags);
  247. __clear_bit(FR_BACKGROUND, &req->flags);
  248. return req;
  249. }
  250. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
  251. {
  252. if (atomic_dec_and_test(&req->count)) {
  253. if (test_bit(FR_BACKGROUND, &req->flags)) {
  254. /*
  255. * We get here in the unlikely case that a background
  256. * request was allocated but not sent
  257. */
  258. spin_lock(&fc->lock);
  259. if (!fc->blocked)
  260. wake_up(&fc->blocked_waitq);
  261. spin_unlock(&fc->lock);
  262. }
  263. if (test_bit(FR_WAITING, &req->flags)) {
  264. __clear_bit(FR_WAITING, &req->flags);
  265. fuse_drop_waiting(fc);
  266. }
  267. if (req->stolen_file)
  268. put_reserved_req(fc, req);
  269. else
  270. fuse_request_free(req);
  271. }
  272. }
  273. EXPORT_SYMBOL_GPL(fuse_put_request);
  274. static unsigned len_args(unsigned numargs, struct fuse_arg *args)
  275. {
  276. unsigned nbytes = 0;
  277. unsigned i;
  278. for (i = 0; i < numargs; i++)
  279. nbytes += args[i].size;
  280. return nbytes;
  281. }
  282. static u64 fuse_get_unique(struct fuse_iqueue *fiq)
  283. {
  284. return ++fiq->reqctr;
  285. }
  286. static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
  287. {
  288. req->in.h.len = sizeof(struct fuse_in_header) +
  289. len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
  290. list_add_tail(&req->list, &fiq->pending);
  291. wake_up_locked(&fiq->waitq);
  292. kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
  293. }
  294. void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
  295. u64 nodeid, u64 nlookup)
  296. {
  297. struct fuse_iqueue *fiq = &fc->iq;
  298. forget->forget_one.nodeid = nodeid;
  299. forget->forget_one.nlookup = nlookup;
  300. spin_lock(&fiq->waitq.lock);
  301. if (fiq->connected) {
  302. fiq->forget_list_tail->next = forget;
  303. fiq->forget_list_tail = forget;
  304. wake_up_locked(&fiq->waitq);
  305. kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
  306. } else {
  307. kfree(forget);
  308. }
  309. spin_unlock(&fiq->waitq.lock);
  310. }
  311. static void flush_bg_queue(struct fuse_conn *fc)
  312. {
  313. while (fc->active_background < fc->max_background &&
  314. !list_empty(&fc->bg_queue)) {
  315. struct fuse_req *req;
  316. struct fuse_iqueue *fiq = &fc->iq;
  317. req = list_entry(fc->bg_queue.next, struct fuse_req, list);
  318. list_del(&req->list);
  319. fc->active_background++;
  320. spin_lock(&fiq->waitq.lock);
  321. req->in.h.unique = fuse_get_unique(fiq);
  322. queue_request(fiq, req);
  323. spin_unlock(&fiq->waitq.lock);
  324. }
  325. }
  326. /*
  327. * This function is called when a request is finished. Either a reply
  328. * has arrived or it was aborted (and not yet sent) or some error
  329. * occurred during communication with userspace, or the device file
  330. * was closed. The requester thread is woken up (if still waiting),
  331. * the 'end' callback is called if given, else the reference to the
  332. * request is released
  333. */
  334. static void request_end(struct fuse_conn *fc, struct fuse_req *req)
  335. {
  336. struct fuse_iqueue *fiq = &fc->iq;
  337. if (test_and_set_bit(FR_FINISHED, &req->flags))
  338. goto put_request;
  339. spin_lock(&fiq->waitq.lock);
  340. list_del_init(&req->intr_entry);
  341. spin_unlock(&fiq->waitq.lock);
  342. WARN_ON(test_bit(FR_PENDING, &req->flags));
  343. WARN_ON(test_bit(FR_SENT, &req->flags));
  344. if (test_bit(FR_BACKGROUND, &req->flags)) {
  345. spin_lock(&fc->lock);
  346. clear_bit(FR_BACKGROUND, &req->flags);
  347. if (fc->num_background == fc->max_background) {
  348. fc->blocked = 0;
  349. wake_up(&fc->blocked_waitq);
  350. } else if (!fc->blocked) {
  351. /*
  352. * Wake up next waiter, if any. It's okay to use
  353. * waitqueue_active(), as we've already synced up
  354. * fc->blocked with waiters with the wake_up() call
  355. * above.
  356. */
  357. if (waitqueue_active(&fc->blocked_waitq))
  358. wake_up(&fc->blocked_waitq);
  359. }
  360. if (fc->num_background == fc->congestion_threshold &&
  361. fc->connected && fc->bdi_initialized) {
  362. clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  363. clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  364. }
  365. fc->num_background--;
  366. fc->active_background--;
  367. flush_bg_queue(fc);
  368. spin_unlock(&fc->lock);
  369. }
  370. wake_up(&req->waitq);
  371. if (req->end)
  372. req->end(fc, req);
  373. put_request:
  374. fuse_put_request(fc, req);
  375. }
  376. static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
  377. {
  378. spin_lock(&fiq->waitq.lock);
  379. if (test_bit(FR_FINISHED, &req->flags)) {
  380. spin_unlock(&fiq->waitq.lock);
  381. return;
  382. }
  383. if (list_empty(&req->intr_entry)) {
  384. list_add_tail(&req->intr_entry, &fiq->interrupts);
  385. wake_up_locked(&fiq->waitq);
  386. }
  387. spin_unlock(&fiq->waitq.lock);
  388. kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
  389. }
  390. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  391. {
  392. struct fuse_iqueue *fiq = &fc->iq;
  393. int err;
  394. if (!fc->no_interrupt) {
  395. /* Any signal may interrupt this */
  396. err = wait_event_interruptible(req->waitq,
  397. test_bit(FR_FINISHED, &req->flags));
  398. if (!err)
  399. return;
  400. set_bit(FR_INTERRUPTED, &req->flags);
  401. /* matches barrier in fuse_dev_do_read() */
  402. smp_mb__after_atomic();
  403. if (test_bit(FR_SENT, &req->flags))
  404. queue_interrupt(fiq, req);
  405. }
  406. if (!test_bit(FR_FORCE, &req->flags)) {
  407. sigset_t oldset;
  408. /* Only fatal signals may interrupt this */
  409. block_sigs(&oldset);
  410. err = wait_event_interruptible(req->waitq,
  411. test_bit(FR_FINISHED, &req->flags));
  412. restore_sigs(&oldset);
  413. if (!err)
  414. return;
  415. spin_lock(&fiq->waitq.lock);
  416. /* Request is not yet in userspace, bail out */
  417. if (test_bit(FR_PENDING, &req->flags)) {
  418. list_del(&req->list);
  419. spin_unlock(&fiq->waitq.lock);
  420. __fuse_put_request(req);
  421. req->out.h.error = -EINTR;
  422. return;
  423. }
  424. spin_unlock(&fiq->waitq.lock);
  425. }
  426. /*
  427. * Either request is already in userspace, or it was forced.
  428. * Wait it out.
  429. */
  430. wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
  431. }
  432. static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
  433. {
  434. struct fuse_iqueue *fiq = &fc->iq;
  435. BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
  436. spin_lock(&fiq->waitq.lock);
  437. if (!fiq->connected) {
  438. spin_unlock(&fiq->waitq.lock);
  439. req->out.h.error = -ENOTCONN;
  440. } else {
  441. req->in.h.unique = fuse_get_unique(fiq);
  442. queue_request(fiq, req);
  443. /* acquire extra reference, since request is still needed
  444. after request_end() */
  445. __fuse_get_request(req);
  446. spin_unlock(&fiq->waitq.lock);
  447. request_wait_answer(fc, req);
  448. /* Pairs with smp_wmb() in request_end() */
  449. smp_rmb();
  450. }
  451. }
  452. void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
  453. {
  454. __set_bit(FR_ISREPLY, &req->flags);
  455. if (!test_bit(FR_WAITING, &req->flags)) {
  456. __set_bit(FR_WAITING, &req->flags);
  457. atomic_inc(&fc->num_waiting);
  458. }
  459. __fuse_request_send(fc, req);
  460. }
  461. EXPORT_SYMBOL_GPL(fuse_request_send);
  462. static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
  463. {
  464. if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
  465. args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
  466. if (fc->minor < 9) {
  467. switch (args->in.h.opcode) {
  468. case FUSE_LOOKUP:
  469. case FUSE_CREATE:
  470. case FUSE_MKNOD:
  471. case FUSE_MKDIR:
  472. case FUSE_SYMLINK:
  473. case FUSE_LINK:
  474. args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
  475. break;
  476. case FUSE_GETATTR:
  477. case FUSE_SETATTR:
  478. args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
  479. break;
  480. }
  481. }
  482. if (fc->minor < 12) {
  483. switch (args->in.h.opcode) {
  484. case FUSE_CREATE:
  485. args->in.args[0].size = sizeof(struct fuse_open_in);
  486. break;
  487. case FUSE_MKNOD:
  488. args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
  489. break;
  490. }
  491. }
  492. }
  493. ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
  494. {
  495. struct fuse_req *req;
  496. ssize_t ret;
  497. req = fuse_get_req(fc, 0);
  498. if (IS_ERR(req))
  499. return PTR_ERR(req);
  500. /* Needs to be done after fuse_get_req() so that fc->minor is valid */
  501. fuse_adjust_compat(fc, args);
  502. req->in.h.opcode = args->in.h.opcode;
  503. req->in.h.nodeid = args->in.h.nodeid;
  504. req->in.numargs = args->in.numargs;
  505. memcpy(req->in.args, args->in.args,
  506. args->in.numargs * sizeof(struct fuse_in_arg));
  507. req->out.argvar = args->out.argvar;
  508. req->out.numargs = args->out.numargs;
  509. memcpy(req->out.args, args->out.args,
  510. args->out.numargs * sizeof(struct fuse_arg));
  511. fuse_request_send(fc, req);
  512. ret = req->out.h.error;
  513. if (!ret && args->out.argvar) {
  514. BUG_ON(args->out.numargs != 1);
  515. ret = req->out.args[0].size;
  516. }
  517. fuse_put_request(fc, req);
  518. return ret;
  519. }
  520. /*
  521. * Called under fc->lock
  522. *
  523. * fc->connected must have been checked previously
  524. */
  525. void fuse_request_send_background_locked(struct fuse_conn *fc,
  526. struct fuse_req *req)
  527. {
  528. BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
  529. if (!test_bit(FR_WAITING, &req->flags)) {
  530. __set_bit(FR_WAITING, &req->flags);
  531. atomic_inc(&fc->num_waiting);
  532. }
  533. __set_bit(FR_ISREPLY, &req->flags);
  534. fc->num_background++;
  535. if (fc->num_background == fc->max_background)
  536. fc->blocked = 1;
  537. if (fc->num_background == fc->congestion_threshold &&
  538. fc->bdi_initialized) {
  539. set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  540. set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  541. }
  542. list_add_tail(&req->list, &fc->bg_queue);
  543. flush_bg_queue(fc);
  544. }
  545. void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  546. {
  547. BUG_ON(!req->end);
  548. spin_lock(&fc->lock);
  549. if (fc->connected) {
  550. fuse_request_send_background_locked(fc, req);
  551. spin_unlock(&fc->lock);
  552. } else {
  553. spin_unlock(&fc->lock);
  554. req->out.h.error = -ENOTCONN;
  555. req->end(fc, req);
  556. fuse_put_request(fc, req);
  557. }
  558. }
  559. EXPORT_SYMBOL_GPL(fuse_request_send_background);
  560. static int fuse_request_send_notify_reply(struct fuse_conn *fc,
  561. struct fuse_req *req, u64 unique)
  562. {
  563. int err = -ENODEV;
  564. struct fuse_iqueue *fiq = &fc->iq;
  565. __clear_bit(FR_ISREPLY, &req->flags);
  566. req->in.h.unique = unique;
  567. spin_lock(&fiq->waitq.lock);
  568. if (fiq->connected) {
  569. queue_request(fiq, req);
  570. err = 0;
  571. }
  572. spin_unlock(&fiq->waitq.lock);
  573. return err;
  574. }
  575. void fuse_force_forget(struct file *file, u64 nodeid)
  576. {
  577. struct inode *inode = file_inode(file);
  578. struct fuse_conn *fc = get_fuse_conn(inode);
  579. struct fuse_req *req;
  580. struct fuse_forget_in inarg;
  581. memset(&inarg, 0, sizeof(inarg));
  582. inarg.nlookup = 1;
  583. req = fuse_get_req_nofail_nopages(fc, file);
  584. req->in.h.opcode = FUSE_FORGET;
  585. req->in.h.nodeid = nodeid;
  586. req->in.numargs = 1;
  587. req->in.args[0].size = sizeof(inarg);
  588. req->in.args[0].value = &inarg;
  589. __clear_bit(FR_ISREPLY, &req->flags);
  590. __fuse_request_send(fc, req);
  591. /* ignore errors */
  592. fuse_put_request(fc, req);
  593. }
  594. /*
  595. * Lock the request. Up to the next unlock_request() there mustn't be
  596. * anything that could cause a page-fault. If the request was already
  597. * aborted bail out.
  598. */
  599. static int lock_request(struct fuse_req *req)
  600. {
  601. int err = 0;
  602. if (req) {
  603. spin_lock(&req->waitq.lock);
  604. if (test_bit(FR_ABORTED, &req->flags))
  605. err = -ENOENT;
  606. else
  607. set_bit(FR_LOCKED, &req->flags);
  608. spin_unlock(&req->waitq.lock);
  609. }
  610. return err;
  611. }
  612. /*
  613. * Unlock request. If it was aborted while locked, caller is responsible
  614. * for unlocking and ending the request.
  615. */
  616. static int unlock_request(struct fuse_req *req)
  617. {
  618. int err = 0;
  619. if (req) {
  620. spin_lock(&req->waitq.lock);
  621. if (test_bit(FR_ABORTED, &req->flags))
  622. err = -ENOENT;
  623. else
  624. clear_bit(FR_LOCKED, &req->flags);
  625. spin_unlock(&req->waitq.lock);
  626. }
  627. return err;
  628. }
  629. struct fuse_copy_state {
  630. int write;
  631. struct fuse_req *req;
  632. struct iov_iter *iter;
  633. struct pipe_buffer *pipebufs;
  634. struct pipe_buffer *currbuf;
  635. struct pipe_inode_info *pipe;
  636. unsigned long nr_segs;
  637. struct page *pg;
  638. unsigned len;
  639. unsigned offset;
  640. unsigned move_pages:1;
  641. };
  642. static void fuse_copy_init(struct fuse_copy_state *cs, int write,
  643. struct iov_iter *iter)
  644. {
  645. memset(cs, 0, sizeof(*cs));
  646. cs->write = write;
  647. cs->iter = iter;
  648. }
  649. /* Unmap and put previous page of userspace buffer */
  650. static void fuse_copy_finish(struct fuse_copy_state *cs)
  651. {
  652. if (cs->currbuf) {
  653. struct pipe_buffer *buf = cs->currbuf;
  654. if (cs->write)
  655. buf->len = PAGE_SIZE - cs->len;
  656. cs->currbuf = NULL;
  657. } else if (cs->pg) {
  658. if (cs->write) {
  659. flush_dcache_page(cs->pg);
  660. set_page_dirty_lock(cs->pg);
  661. }
  662. put_page(cs->pg);
  663. }
  664. cs->pg = NULL;
  665. }
  666. /*
  667. * Get another pagefull of userspace buffer, and map it to kernel
  668. * address space, and lock request
  669. */
  670. static int fuse_copy_fill(struct fuse_copy_state *cs)
  671. {
  672. struct page *page;
  673. int err;
  674. err = unlock_request(cs->req);
  675. if (err)
  676. return err;
  677. fuse_copy_finish(cs);
  678. if (cs->pipebufs) {
  679. struct pipe_buffer *buf = cs->pipebufs;
  680. if (!cs->write) {
  681. err = buf->ops->confirm(cs->pipe, buf);
  682. if (err)
  683. return err;
  684. BUG_ON(!cs->nr_segs);
  685. cs->currbuf = buf;
  686. cs->pg = buf->page;
  687. cs->offset = buf->offset;
  688. cs->len = buf->len;
  689. cs->pipebufs++;
  690. cs->nr_segs--;
  691. } else {
  692. if (cs->nr_segs == cs->pipe->buffers)
  693. return -EIO;
  694. page = alloc_page(GFP_HIGHUSER);
  695. if (!page)
  696. return -ENOMEM;
  697. buf->page = page;
  698. buf->offset = 0;
  699. buf->len = 0;
  700. cs->currbuf = buf;
  701. cs->pg = page;
  702. cs->offset = 0;
  703. cs->len = PAGE_SIZE;
  704. cs->pipebufs++;
  705. cs->nr_segs++;
  706. }
  707. } else {
  708. size_t off;
  709. err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
  710. if (err < 0)
  711. return err;
  712. BUG_ON(!err);
  713. cs->len = err;
  714. cs->offset = off;
  715. cs->pg = page;
  716. cs->offset = off;
  717. iov_iter_advance(cs->iter, err);
  718. }
  719. return lock_request(cs->req);
  720. }
  721. /* Do as much copy to/from userspace buffer as we can */
  722. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  723. {
  724. unsigned ncpy = min(*size, cs->len);
  725. if (val) {
  726. void *pgaddr = kmap_atomic(cs->pg);
  727. void *buf = pgaddr + cs->offset;
  728. if (cs->write)
  729. memcpy(buf, *val, ncpy);
  730. else
  731. memcpy(*val, buf, ncpy);
  732. kunmap_atomic(pgaddr);
  733. *val += ncpy;
  734. }
  735. *size -= ncpy;
  736. cs->len -= ncpy;
  737. cs->offset += ncpy;
  738. return ncpy;
  739. }
  740. static int fuse_check_page(struct page *page)
  741. {
  742. if (page_mapcount(page) ||
  743. page->mapping != NULL ||
  744. page_count(page) != 1 ||
  745. (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
  746. ~(1 << PG_locked |
  747. 1 << PG_referenced |
  748. 1 << PG_uptodate |
  749. 1 << PG_lru |
  750. 1 << PG_active |
  751. 1 << PG_reclaim))) {
  752. printk(KERN_WARNING "fuse: trying to steal weird page\n");
  753. printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
  754. return 1;
  755. }
  756. return 0;
  757. }
  758. static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
  759. {
  760. int err;
  761. struct page *oldpage = *pagep;
  762. struct page *newpage;
  763. struct pipe_buffer *buf = cs->pipebufs;
  764. err = unlock_request(cs->req);
  765. if (err)
  766. return err;
  767. fuse_copy_finish(cs);
  768. err = buf->ops->confirm(cs->pipe, buf);
  769. if (err)
  770. return err;
  771. BUG_ON(!cs->nr_segs);
  772. cs->currbuf = buf;
  773. cs->len = buf->len;
  774. cs->pipebufs++;
  775. cs->nr_segs--;
  776. if (cs->len != PAGE_SIZE)
  777. goto out_fallback;
  778. if (buf->ops->steal(cs->pipe, buf) != 0)
  779. goto out_fallback;
  780. newpage = buf->page;
  781. if (!PageUptodate(newpage))
  782. SetPageUptodate(newpage);
  783. ClearPageMappedToDisk(newpage);
  784. if (fuse_check_page(newpage) != 0)
  785. goto out_fallback_unlock;
  786. /*
  787. * This is a new and locked page, it shouldn't be mapped or
  788. * have any special flags on it
  789. */
  790. if (WARN_ON(page_mapped(oldpage)))
  791. goto out_fallback_unlock;
  792. if (WARN_ON(page_has_private(oldpage)))
  793. goto out_fallback_unlock;
  794. if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
  795. goto out_fallback_unlock;
  796. if (WARN_ON(PageMlocked(oldpage)))
  797. goto out_fallback_unlock;
  798. err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
  799. if (err) {
  800. unlock_page(newpage);
  801. return err;
  802. }
  803. page_cache_get(newpage);
  804. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  805. lru_cache_add_file(newpage);
  806. err = 0;
  807. spin_lock(&cs->req->waitq.lock);
  808. if (test_bit(FR_ABORTED, &cs->req->flags))
  809. err = -ENOENT;
  810. else
  811. *pagep = newpage;
  812. spin_unlock(&cs->req->waitq.lock);
  813. if (err) {
  814. unlock_page(newpage);
  815. page_cache_release(newpage);
  816. return err;
  817. }
  818. unlock_page(oldpage);
  819. page_cache_release(oldpage);
  820. cs->len = 0;
  821. return 0;
  822. out_fallback_unlock:
  823. unlock_page(newpage);
  824. out_fallback:
  825. cs->pg = buf->page;
  826. cs->offset = buf->offset;
  827. err = lock_request(cs->req);
  828. if (err)
  829. return err;
  830. return 1;
  831. }
  832. static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
  833. unsigned offset, unsigned count)
  834. {
  835. struct pipe_buffer *buf;
  836. int err;
  837. if (cs->nr_segs == cs->pipe->buffers)
  838. return -EIO;
  839. err = unlock_request(cs->req);
  840. if (err)
  841. return err;
  842. fuse_copy_finish(cs);
  843. buf = cs->pipebufs;
  844. page_cache_get(page);
  845. buf->page = page;
  846. buf->offset = offset;
  847. buf->len = count;
  848. cs->pipebufs++;
  849. cs->nr_segs++;
  850. cs->len = 0;
  851. return 0;
  852. }
  853. /*
  854. * Copy a page in the request to/from the userspace buffer. Must be
  855. * done atomically
  856. */
  857. static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
  858. unsigned offset, unsigned count, int zeroing)
  859. {
  860. int err;
  861. struct page *page = *pagep;
  862. if (page && zeroing && count < PAGE_SIZE)
  863. clear_highpage(page);
  864. while (count) {
  865. if (cs->write && cs->pipebufs && page) {
  866. return fuse_ref_page(cs, page, offset, count);
  867. } else if (!cs->len) {
  868. if (cs->move_pages && page &&
  869. offset == 0 && count == PAGE_SIZE) {
  870. err = fuse_try_move_page(cs, pagep);
  871. if (err <= 0)
  872. return err;
  873. } else {
  874. err = fuse_copy_fill(cs);
  875. if (err)
  876. return err;
  877. }
  878. }
  879. if (page) {
  880. void *mapaddr = kmap_atomic(page);
  881. void *buf = mapaddr + offset;
  882. offset += fuse_copy_do(cs, &buf, &count);
  883. kunmap_atomic(mapaddr);
  884. } else
  885. offset += fuse_copy_do(cs, NULL, &count);
  886. }
  887. if (page && !cs->write)
  888. flush_dcache_page(page);
  889. return 0;
  890. }
  891. /* Copy pages in the request to/from userspace buffer */
  892. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  893. int zeroing)
  894. {
  895. unsigned i;
  896. struct fuse_req *req = cs->req;
  897. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  898. int err;
  899. unsigned offset = req->page_descs[i].offset;
  900. unsigned count = min(nbytes, req->page_descs[i].length);
  901. err = fuse_copy_page(cs, &req->pages[i], offset, count,
  902. zeroing);
  903. if (err)
  904. return err;
  905. nbytes -= count;
  906. }
  907. return 0;
  908. }
  909. /* Copy a single argument in the request to/from userspace buffer */
  910. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  911. {
  912. while (size) {
  913. if (!cs->len) {
  914. int err = fuse_copy_fill(cs);
  915. if (err)
  916. return err;
  917. }
  918. fuse_copy_do(cs, &val, &size);
  919. }
  920. return 0;
  921. }
  922. /* Copy request arguments to/from userspace buffer */
  923. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  924. unsigned argpages, struct fuse_arg *args,
  925. int zeroing)
  926. {
  927. int err = 0;
  928. unsigned i;
  929. for (i = 0; !err && i < numargs; i++) {
  930. struct fuse_arg *arg = &args[i];
  931. if (i == numargs - 1 && argpages)
  932. err = fuse_copy_pages(cs, arg->size, zeroing);
  933. else
  934. err = fuse_copy_one(cs, arg->value, arg->size);
  935. }
  936. return err;
  937. }
  938. static int forget_pending(struct fuse_iqueue *fiq)
  939. {
  940. return fiq->forget_list_head.next != NULL;
  941. }
  942. static int request_pending(struct fuse_iqueue *fiq)
  943. {
  944. return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
  945. forget_pending(fiq);
  946. }
  947. /*
  948. * Transfer an interrupt request to userspace
  949. *
  950. * Unlike other requests this is assembled on demand, without a need
  951. * to allocate a separate fuse_req structure.
  952. *
  953. * Called with fiq->waitq.lock held, releases it
  954. */
  955. static int fuse_read_interrupt(struct fuse_iqueue *fiq,
  956. struct fuse_copy_state *cs,
  957. size_t nbytes, struct fuse_req *req)
  958. __releases(fiq->waitq.lock)
  959. {
  960. struct fuse_in_header ih;
  961. struct fuse_interrupt_in arg;
  962. unsigned reqsize = sizeof(ih) + sizeof(arg);
  963. int err;
  964. list_del_init(&req->intr_entry);
  965. req->intr_unique = fuse_get_unique(fiq);
  966. memset(&ih, 0, sizeof(ih));
  967. memset(&arg, 0, sizeof(arg));
  968. ih.len = reqsize;
  969. ih.opcode = FUSE_INTERRUPT;
  970. ih.unique = req->intr_unique;
  971. arg.unique = req->in.h.unique;
  972. spin_unlock(&fiq->waitq.lock);
  973. if (nbytes < reqsize)
  974. return -EINVAL;
  975. err = fuse_copy_one(cs, &ih, sizeof(ih));
  976. if (!err)
  977. err = fuse_copy_one(cs, &arg, sizeof(arg));
  978. fuse_copy_finish(cs);
  979. return err ? err : reqsize;
  980. }
  981. static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
  982. unsigned max,
  983. unsigned *countp)
  984. {
  985. struct fuse_forget_link *head = fiq->forget_list_head.next;
  986. struct fuse_forget_link **newhead = &head;
  987. unsigned count;
  988. for (count = 0; *newhead != NULL && count < max; count++)
  989. newhead = &(*newhead)->next;
  990. fiq->forget_list_head.next = *newhead;
  991. *newhead = NULL;
  992. if (fiq->forget_list_head.next == NULL)
  993. fiq->forget_list_tail = &fiq->forget_list_head;
  994. if (countp != NULL)
  995. *countp = count;
  996. return head;
  997. }
  998. static int fuse_read_single_forget(struct fuse_iqueue *fiq,
  999. struct fuse_copy_state *cs,
  1000. size_t nbytes)
  1001. __releases(fiq->waitq.lock)
  1002. {
  1003. int err;
  1004. struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
  1005. struct fuse_forget_in arg = {
  1006. .nlookup = forget->forget_one.nlookup,
  1007. };
  1008. struct fuse_in_header ih = {
  1009. .opcode = FUSE_FORGET,
  1010. .nodeid = forget->forget_one.nodeid,
  1011. .unique = fuse_get_unique(fiq),
  1012. .len = sizeof(ih) + sizeof(arg),
  1013. };
  1014. spin_unlock(&fiq->waitq.lock);
  1015. kfree(forget);
  1016. if (nbytes < ih.len)
  1017. return -EINVAL;
  1018. err = fuse_copy_one(cs, &ih, sizeof(ih));
  1019. if (!err)
  1020. err = fuse_copy_one(cs, &arg, sizeof(arg));
  1021. fuse_copy_finish(cs);
  1022. if (err)
  1023. return err;
  1024. return ih.len;
  1025. }
  1026. static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
  1027. struct fuse_copy_state *cs, size_t nbytes)
  1028. __releases(fiq->waitq.lock)
  1029. {
  1030. int err;
  1031. unsigned max_forgets;
  1032. unsigned count;
  1033. struct fuse_forget_link *head;
  1034. struct fuse_batch_forget_in arg = { .count = 0 };
  1035. struct fuse_in_header ih = {
  1036. .opcode = FUSE_BATCH_FORGET,
  1037. .unique = fuse_get_unique(fiq),
  1038. .len = sizeof(ih) + sizeof(arg),
  1039. };
  1040. if (nbytes < ih.len) {
  1041. spin_unlock(&fiq->waitq.lock);
  1042. return -EINVAL;
  1043. }
  1044. max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
  1045. head = dequeue_forget(fiq, max_forgets, &count);
  1046. spin_unlock(&fiq->waitq.lock);
  1047. arg.count = count;
  1048. ih.len += count * sizeof(struct fuse_forget_one);
  1049. err = fuse_copy_one(cs, &ih, sizeof(ih));
  1050. if (!err)
  1051. err = fuse_copy_one(cs, &arg, sizeof(arg));
  1052. while (head) {
  1053. struct fuse_forget_link *forget = head;
  1054. if (!err) {
  1055. err = fuse_copy_one(cs, &forget->forget_one,
  1056. sizeof(forget->forget_one));
  1057. }
  1058. head = forget->next;
  1059. kfree(forget);
  1060. }
  1061. fuse_copy_finish(cs);
  1062. if (err)
  1063. return err;
  1064. return ih.len;
  1065. }
  1066. static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
  1067. struct fuse_copy_state *cs,
  1068. size_t nbytes)
  1069. __releases(fiq->waitq.lock)
  1070. {
  1071. if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
  1072. return fuse_read_single_forget(fiq, cs, nbytes);
  1073. else
  1074. return fuse_read_batch_forget(fiq, cs, nbytes);
  1075. }
  1076. /*
  1077. * Read a single request into the userspace filesystem's buffer. This
  1078. * function waits until a request is available, then removes it from
  1079. * the pending list and copies request data to userspace buffer. If
  1080. * no reply is needed (FORGET) or request has been aborted or there
  1081. * was an error during the copying then it's finished by calling
  1082. * request_end(). Otherwise add it to the processing list, and set
  1083. * the 'sent' flag.
  1084. */
  1085. static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
  1086. struct fuse_copy_state *cs, size_t nbytes)
  1087. {
  1088. ssize_t err;
  1089. struct fuse_conn *fc = fud->fc;
  1090. struct fuse_iqueue *fiq = &fc->iq;
  1091. struct fuse_pqueue *fpq = &fud->pq;
  1092. struct fuse_req *req;
  1093. struct fuse_in *in;
  1094. unsigned reqsize;
  1095. restart:
  1096. spin_lock(&fiq->waitq.lock);
  1097. err = -EAGAIN;
  1098. if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
  1099. !request_pending(fiq))
  1100. goto err_unlock;
  1101. err = wait_event_interruptible_exclusive_locked(fiq->waitq,
  1102. !fiq->connected || request_pending(fiq));
  1103. if (err)
  1104. goto err_unlock;
  1105. err = -ENODEV;
  1106. if (!fiq->connected)
  1107. goto err_unlock;
  1108. if (!list_empty(&fiq->interrupts)) {
  1109. req = list_entry(fiq->interrupts.next, struct fuse_req,
  1110. intr_entry);
  1111. return fuse_read_interrupt(fiq, cs, nbytes, req);
  1112. }
  1113. if (forget_pending(fiq)) {
  1114. if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
  1115. return fuse_read_forget(fc, fiq, cs, nbytes);
  1116. if (fiq->forget_batch <= -8)
  1117. fiq->forget_batch = 16;
  1118. }
  1119. req = list_entry(fiq->pending.next, struct fuse_req, list);
  1120. clear_bit(FR_PENDING, &req->flags);
  1121. list_del_init(&req->list);
  1122. spin_unlock(&fiq->waitq.lock);
  1123. in = &req->in;
  1124. reqsize = in->h.len;
  1125. /* If request is too large, reply with an error and restart the read */
  1126. if (nbytes < reqsize) {
  1127. req->out.h.error = -EIO;
  1128. /* SETXATTR is special, since it may contain too large data */
  1129. if (in->h.opcode == FUSE_SETXATTR)
  1130. req->out.h.error = -E2BIG;
  1131. request_end(fc, req);
  1132. goto restart;
  1133. }
  1134. spin_lock(&fpq->lock);
  1135. list_add(&req->list, &fpq->io);
  1136. spin_unlock(&fpq->lock);
  1137. cs->req = req;
  1138. err = fuse_copy_one(cs, &in->h, sizeof(in->h));
  1139. if (!err)
  1140. err = fuse_copy_args(cs, in->numargs, in->argpages,
  1141. (struct fuse_arg *) in->args, 0);
  1142. fuse_copy_finish(cs);
  1143. spin_lock(&fpq->lock);
  1144. clear_bit(FR_LOCKED, &req->flags);
  1145. if (!fpq->connected) {
  1146. err = -ENODEV;
  1147. goto out_end;
  1148. }
  1149. if (err) {
  1150. req->out.h.error = -EIO;
  1151. goto out_end;
  1152. }
  1153. if (!test_bit(FR_ISREPLY, &req->flags)) {
  1154. err = reqsize;
  1155. goto out_end;
  1156. }
  1157. list_move_tail(&req->list, &fpq->processing);
  1158. __fuse_get_request(req);
  1159. set_bit(FR_SENT, &req->flags);
  1160. spin_unlock(&fpq->lock);
  1161. /* matches barrier in request_wait_answer() */
  1162. smp_mb__after_atomic();
  1163. if (test_bit(FR_INTERRUPTED, &req->flags))
  1164. queue_interrupt(fiq, req);
  1165. fuse_put_request(fc, req);
  1166. return reqsize;
  1167. out_end:
  1168. if (!test_bit(FR_PRIVATE, &req->flags))
  1169. list_del_init(&req->list);
  1170. spin_unlock(&fpq->lock);
  1171. request_end(fc, req);
  1172. return err;
  1173. err_unlock:
  1174. spin_unlock(&fiq->waitq.lock);
  1175. return err;
  1176. }
  1177. static int fuse_dev_open(struct inode *inode, struct file *file)
  1178. {
  1179. /*
  1180. * The fuse device's file's private_data is used to hold
  1181. * the fuse_conn(ection) when it is mounted, and is used to
  1182. * keep track of whether the file has been mounted already.
  1183. */
  1184. file->private_data = NULL;
  1185. return 0;
  1186. }
  1187. static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
  1188. {
  1189. struct fuse_copy_state cs;
  1190. struct file *file = iocb->ki_filp;
  1191. struct fuse_dev *fud = fuse_get_dev(file);
  1192. if (!fud)
  1193. return -EPERM;
  1194. if (!iter_is_iovec(to))
  1195. return -EINVAL;
  1196. fuse_copy_init(&cs, 1, to);
  1197. return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
  1198. }
  1199. static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
  1200. struct pipe_inode_info *pipe,
  1201. size_t len, unsigned int flags)
  1202. {
  1203. int ret;
  1204. int page_nr = 0;
  1205. int do_wakeup = 0;
  1206. struct pipe_buffer *bufs;
  1207. struct fuse_copy_state cs;
  1208. struct fuse_dev *fud = fuse_get_dev(in);
  1209. if (!fud)
  1210. return -EPERM;
  1211. bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
  1212. if (!bufs)
  1213. return -ENOMEM;
  1214. fuse_copy_init(&cs, 1, NULL);
  1215. cs.pipebufs = bufs;
  1216. cs.pipe = pipe;
  1217. ret = fuse_dev_do_read(fud, in, &cs, len);
  1218. if (ret < 0)
  1219. goto out;
  1220. ret = 0;
  1221. pipe_lock(pipe);
  1222. if (!pipe->readers) {
  1223. send_sig(SIGPIPE, current, 0);
  1224. if (!ret)
  1225. ret = -EPIPE;
  1226. goto out_unlock;
  1227. }
  1228. if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
  1229. ret = -EIO;
  1230. goto out_unlock;
  1231. }
  1232. while (page_nr < cs.nr_segs) {
  1233. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  1234. struct pipe_buffer *buf = pipe->bufs + newbuf;
  1235. buf->page = bufs[page_nr].page;
  1236. buf->offset = bufs[page_nr].offset;
  1237. buf->len = bufs[page_nr].len;
  1238. /*
  1239. * Need to be careful about this. Having buf->ops in module
  1240. * code can Oops if the buffer persists after module unload.
  1241. */
  1242. buf->ops = &nosteal_pipe_buf_ops;
  1243. pipe->nrbufs++;
  1244. page_nr++;
  1245. ret += buf->len;
  1246. if (pipe->files)
  1247. do_wakeup = 1;
  1248. }
  1249. out_unlock:
  1250. pipe_unlock(pipe);
  1251. if (do_wakeup) {
  1252. smp_mb();
  1253. if (waitqueue_active(&pipe->wait))
  1254. wake_up_interruptible(&pipe->wait);
  1255. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  1256. }
  1257. out:
  1258. for (; page_nr < cs.nr_segs; page_nr++)
  1259. page_cache_release(bufs[page_nr].page);
  1260. kfree(bufs);
  1261. return ret;
  1262. }
  1263. static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
  1264. struct fuse_copy_state *cs)
  1265. {
  1266. struct fuse_notify_poll_wakeup_out outarg;
  1267. int err = -EINVAL;
  1268. if (size != sizeof(outarg))
  1269. goto err;
  1270. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1271. if (err)
  1272. goto err;
  1273. fuse_copy_finish(cs);
  1274. return fuse_notify_poll_wakeup(fc, &outarg);
  1275. err:
  1276. fuse_copy_finish(cs);
  1277. return err;
  1278. }
  1279. static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
  1280. struct fuse_copy_state *cs)
  1281. {
  1282. struct fuse_notify_inval_inode_out outarg;
  1283. int err = -EINVAL;
  1284. if (size != sizeof(outarg))
  1285. goto err;
  1286. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1287. if (err)
  1288. goto err;
  1289. fuse_copy_finish(cs);
  1290. down_read(&fc->killsb);
  1291. err = -ENOENT;
  1292. if (fc->sb) {
  1293. err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
  1294. outarg.off, outarg.len);
  1295. }
  1296. up_read(&fc->killsb);
  1297. return err;
  1298. err:
  1299. fuse_copy_finish(cs);
  1300. return err;
  1301. }
  1302. static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
  1303. struct fuse_copy_state *cs)
  1304. {
  1305. struct fuse_notify_inval_entry_out outarg;
  1306. int err = -ENOMEM;
  1307. char *buf;
  1308. struct qstr name;
  1309. buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
  1310. if (!buf)
  1311. goto err;
  1312. err = -EINVAL;
  1313. if (size < sizeof(outarg))
  1314. goto err;
  1315. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1316. if (err)
  1317. goto err;
  1318. err = -ENAMETOOLONG;
  1319. if (outarg.namelen > FUSE_NAME_MAX)
  1320. goto err;
  1321. err = -EINVAL;
  1322. if (size != sizeof(outarg) + outarg.namelen + 1)
  1323. goto err;
  1324. name.name = buf;
  1325. name.len = outarg.namelen;
  1326. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1327. if (err)
  1328. goto err;
  1329. fuse_copy_finish(cs);
  1330. buf[outarg.namelen] = 0;
  1331. name.hash = full_name_hash(name.name, name.len);
  1332. down_read(&fc->killsb);
  1333. err = -ENOENT;
  1334. if (fc->sb)
  1335. err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
  1336. up_read(&fc->killsb);
  1337. kfree(buf);
  1338. return err;
  1339. err:
  1340. kfree(buf);
  1341. fuse_copy_finish(cs);
  1342. return err;
  1343. }
  1344. static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
  1345. struct fuse_copy_state *cs)
  1346. {
  1347. struct fuse_notify_delete_out outarg;
  1348. int err = -ENOMEM;
  1349. char *buf;
  1350. struct qstr name;
  1351. buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
  1352. if (!buf)
  1353. goto err;
  1354. err = -EINVAL;
  1355. if (size < sizeof(outarg))
  1356. goto err;
  1357. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1358. if (err)
  1359. goto err;
  1360. err = -ENAMETOOLONG;
  1361. if (outarg.namelen > FUSE_NAME_MAX)
  1362. goto err;
  1363. err = -EINVAL;
  1364. if (size != sizeof(outarg) + outarg.namelen + 1)
  1365. goto err;
  1366. name.name = buf;
  1367. name.len = outarg.namelen;
  1368. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1369. if (err)
  1370. goto err;
  1371. fuse_copy_finish(cs);
  1372. buf[outarg.namelen] = 0;
  1373. name.hash = full_name_hash(name.name, name.len);
  1374. down_read(&fc->killsb);
  1375. err = -ENOENT;
  1376. if (fc->sb)
  1377. err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
  1378. outarg.child, &name);
  1379. up_read(&fc->killsb);
  1380. kfree(buf);
  1381. return err;
  1382. err:
  1383. kfree(buf);
  1384. fuse_copy_finish(cs);
  1385. return err;
  1386. }
  1387. static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
  1388. struct fuse_copy_state *cs)
  1389. {
  1390. struct fuse_notify_store_out outarg;
  1391. struct inode *inode;
  1392. struct address_space *mapping;
  1393. u64 nodeid;
  1394. int err;
  1395. pgoff_t index;
  1396. unsigned int offset;
  1397. unsigned int num;
  1398. loff_t file_size;
  1399. loff_t end;
  1400. err = -EINVAL;
  1401. if (size < sizeof(outarg))
  1402. goto out_finish;
  1403. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1404. if (err)
  1405. goto out_finish;
  1406. err = -EINVAL;
  1407. if (size - sizeof(outarg) != outarg.size)
  1408. goto out_finish;
  1409. nodeid = outarg.nodeid;
  1410. down_read(&fc->killsb);
  1411. err = -ENOENT;
  1412. if (!fc->sb)
  1413. goto out_up_killsb;
  1414. inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
  1415. if (!inode)
  1416. goto out_up_killsb;
  1417. mapping = inode->i_mapping;
  1418. index = outarg.offset >> PAGE_CACHE_SHIFT;
  1419. offset = outarg.offset & ~PAGE_CACHE_MASK;
  1420. file_size = i_size_read(inode);
  1421. end = outarg.offset + outarg.size;
  1422. if (end > file_size) {
  1423. file_size = end;
  1424. fuse_write_update_size(inode, file_size);
  1425. }
  1426. num = outarg.size;
  1427. while (num) {
  1428. struct page *page;
  1429. unsigned int this_num;
  1430. err = -ENOMEM;
  1431. page = find_or_create_page(mapping, index,
  1432. mapping_gfp_mask(mapping));
  1433. if (!page)
  1434. goto out_iput;
  1435. this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
  1436. err = fuse_copy_page(cs, &page, offset, this_num, 0);
  1437. if (!err && offset == 0 &&
  1438. (this_num == PAGE_CACHE_SIZE || file_size == end))
  1439. SetPageUptodate(page);
  1440. unlock_page(page);
  1441. page_cache_release(page);
  1442. if (err)
  1443. goto out_iput;
  1444. num -= this_num;
  1445. offset = 0;
  1446. index++;
  1447. }
  1448. err = 0;
  1449. out_iput:
  1450. iput(inode);
  1451. out_up_killsb:
  1452. up_read(&fc->killsb);
  1453. out_finish:
  1454. fuse_copy_finish(cs);
  1455. return err;
  1456. }
  1457. static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
  1458. {
  1459. release_pages(req->pages, req->num_pages, false);
  1460. }
  1461. static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
  1462. struct fuse_notify_retrieve_out *outarg)
  1463. {
  1464. int err;
  1465. struct address_space *mapping = inode->i_mapping;
  1466. struct fuse_req *req;
  1467. pgoff_t index;
  1468. loff_t file_size;
  1469. unsigned int num;
  1470. unsigned int offset;
  1471. size_t total_len = 0;
  1472. int num_pages;
  1473. offset = outarg->offset & ~PAGE_CACHE_MASK;
  1474. file_size = i_size_read(inode);
  1475. num = outarg->size;
  1476. if (outarg->offset > file_size)
  1477. num = 0;
  1478. else if (outarg->offset + num > file_size)
  1479. num = file_size - outarg->offset;
  1480. num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1481. num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
  1482. req = fuse_get_req(fc, num_pages);
  1483. if (IS_ERR(req))
  1484. return PTR_ERR(req);
  1485. req->in.h.opcode = FUSE_NOTIFY_REPLY;
  1486. req->in.h.nodeid = outarg->nodeid;
  1487. req->in.numargs = 2;
  1488. req->in.argpages = 1;
  1489. req->end = fuse_retrieve_end;
  1490. index = outarg->offset >> PAGE_CACHE_SHIFT;
  1491. while (num && req->num_pages < num_pages) {
  1492. struct page *page;
  1493. unsigned int this_num;
  1494. page = find_get_page(mapping, index);
  1495. if (!page)
  1496. break;
  1497. this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
  1498. req->pages[req->num_pages] = page;
  1499. req->page_descs[req->num_pages].offset = offset;
  1500. req->page_descs[req->num_pages].length = this_num;
  1501. req->num_pages++;
  1502. offset = 0;
  1503. num -= this_num;
  1504. total_len += this_num;
  1505. index++;
  1506. }
  1507. req->misc.retrieve_in.offset = outarg->offset;
  1508. req->misc.retrieve_in.size = total_len;
  1509. req->in.args[0].size = sizeof(req->misc.retrieve_in);
  1510. req->in.args[0].value = &req->misc.retrieve_in;
  1511. req->in.args[1].size = total_len;
  1512. err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
  1513. if (err) {
  1514. fuse_retrieve_end(fc, req);
  1515. fuse_put_request(fc, req);
  1516. }
  1517. return err;
  1518. }
  1519. static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
  1520. struct fuse_copy_state *cs)
  1521. {
  1522. struct fuse_notify_retrieve_out outarg;
  1523. struct inode *inode;
  1524. int err;
  1525. err = -EINVAL;
  1526. if (size != sizeof(outarg))
  1527. goto copy_finish;
  1528. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1529. if (err)
  1530. goto copy_finish;
  1531. fuse_copy_finish(cs);
  1532. down_read(&fc->killsb);
  1533. err = -ENOENT;
  1534. if (fc->sb) {
  1535. u64 nodeid = outarg.nodeid;
  1536. inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
  1537. if (inode) {
  1538. err = fuse_retrieve(fc, inode, &outarg);
  1539. iput(inode);
  1540. }
  1541. }
  1542. up_read(&fc->killsb);
  1543. return err;
  1544. copy_finish:
  1545. fuse_copy_finish(cs);
  1546. return err;
  1547. }
  1548. static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
  1549. unsigned int size, struct fuse_copy_state *cs)
  1550. {
  1551. /* Don't try to move pages (yet) */
  1552. cs->move_pages = 0;
  1553. switch (code) {
  1554. case FUSE_NOTIFY_POLL:
  1555. return fuse_notify_poll(fc, size, cs);
  1556. case FUSE_NOTIFY_INVAL_INODE:
  1557. return fuse_notify_inval_inode(fc, size, cs);
  1558. case FUSE_NOTIFY_INVAL_ENTRY:
  1559. return fuse_notify_inval_entry(fc, size, cs);
  1560. case FUSE_NOTIFY_STORE:
  1561. return fuse_notify_store(fc, size, cs);
  1562. case FUSE_NOTIFY_RETRIEVE:
  1563. return fuse_notify_retrieve(fc, size, cs);
  1564. case FUSE_NOTIFY_DELETE:
  1565. return fuse_notify_delete(fc, size, cs);
  1566. default:
  1567. fuse_copy_finish(cs);
  1568. return -EINVAL;
  1569. }
  1570. }
  1571. /* Look up request on processing list by unique ID */
  1572. static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
  1573. {
  1574. struct fuse_req *req;
  1575. list_for_each_entry(req, &fpq->processing, list) {
  1576. if (req->in.h.unique == unique || req->intr_unique == unique)
  1577. return req;
  1578. }
  1579. return NULL;
  1580. }
  1581. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  1582. unsigned nbytes)
  1583. {
  1584. unsigned reqsize = sizeof(struct fuse_out_header);
  1585. if (out->h.error)
  1586. return nbytes != reqsize ? -EINVAL : 0;
  1587. reqsize += len_args(out->numargs, out->args);
  1588. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  1589. return -EINVAL;
  1590. else if (reqsize > nbytes) {
  1591. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  1592. unsigned diffsize = reqsize - nbytes;
  1593. if (diffsize > lastarg->size)
  1594. return -EINVAL;
  1595. lastarg->size -= diffsize;
  1596. }
  1597. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  1598. out->page_zeroing);
  1599. }
  1600. /*
  1601. * Write a single reply to a request. First the header is copied from
  1602. * the write buffer. The request is then searched on the processing
  1603. * list by the unique ID found in the header. If found, then remove
  1604. * it from the list and copy the rest of the buffer to the request.
  1605. * The request is finished by calling request_end()
  1606. */
  1607. static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
  1608. struct fuse_copy_state *cs, size_t nbytes)
  1609. {
  1610. int err;
  1611. struct fuse_conn *fc = fud->fc;
  1612. struct fuse_pqueue *fpq = &fud->pq;
  1613. struct fuse_req *req;
  1614. struct fuse_out_header oh;
  1615. if (nbytes < sizeof(struct fuse_out_header))
  1616. return -EINVAL;
  1617. err = fuse_copy_one(cs, &oh, sizeof(oh));
  1618. if (err)
  1619. goto err_finish;
  1620. err = -EINVAL;
  1621. if (oh.len != nbytes)
  1622. goto err_finish;
  1623. /*
  1624. * Zero oh.unique indicates unsolicited notification message
  1625. * and error contains notification code.
  1626. */
  1627. if (!oh.unique) {
  1628. err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
  1629. return err ? err : nbytes;
  1630. }
  1631. err = -EINVAL;
  1632. if (oh.error <= -1000 || oh.error > 0)
  1633. goto err_finish;
  1634. spin_lock(&fpq->lock);
  1635. err = -ENOENT;
  1636. if (!fpq->connected)
  1637. goto err_unlock_pq;
  1638. req = request_find(fpq, oh.unique);
  1639. if (!req)
  1640. goto err_unlock_pq;
  1641. /* Is it an interrupt reply? */
  1642. if (req->intr_unique == oh.unique) {
  1643. __fuse_get_request(req);
  1644. spin_unlock(&fpq->lock);
  1645. err = -EINVAL;
  1646. if (nbytes != sizeof(struct fuse_out_header)) {
  1647. fuse_put_request(fc, req);
  1648. goto err_finish;
  1649. }
  1650. if (oh.error == -ENOSYS)
  1651. fc->no_interrupt = 1;
  1652. else if (oh.error == -EAGAIN)
  1653. queue_interrupt(&fc->iq, req);
  1654. fuse_put_request(fc, req);
  1655. fuse_copy_finish(cs);
  1656. return nbytes;
  1657. }
  1658. clear_bit(FR_SENT, &req->flags);
  1659. list_move(&req->list, &fpq->io);
  1660. req->out.h = oh;
  1661. set_bit(FR_LOCKED, &req->flags);
  1662. spin_unlock(&fpq->lock);
  1663. cs->req = req;
  1664. if (!req->out.page_replace)
  1665. cs->move_pages = 0;
  1666. err = copy_out_args(cs, &req->out, nbytes);
  1667. fuse_copy_finish(cs);
  1668. spin_lock(&fpq->lock);
  1669. clear_bit(FR_LOCKED, &req->flags);
  1670. if (!fpq->connected)
  1671. err = -ENOENT;
  1672. else if (err)
  1673. req->out.h.error = -EIO;
  1674. if (!test_bit(FR_PRIVATE, &req->flags))
  1675. list_del_init(&req->list);
  1676. spin_unlock(&fpq->lock);
  1677. request_end(fc, req);
  1678. return err ? err : nbytes;
  1679. err_unlock_pq:
  1680. spin_unlock(&fpq->lock);
  1681. err_finish:
  1682. fuse_copy_finish(cs);
  1683. return err;
  1684. }
  1685. static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
  1686. {
  1687. struct fuse_copy_state cs;
  1688. struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
  1689. if (!fud)
  1690. return -EPERM;
  1691. if (!iter_is_iovec(from))
  1692. return -EINVAL;
  1693. fuse_copy_init(&cs, 0, from);
  1694. return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
  1695. }
  1696. static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
  1697. struct file *out, loff_t *ppos,
  1698. size_t len, unsigned int flags)
  1699. {
  1700. unsigned nbuf;
  1701. unsigned idx;
  1702. struct pipe_buffer *bufs;
  1703. struct fuse_copy_state cs;
  1704. struct fuse_dev *fud;
  1705. size_t rem;
  1706. ssize_t ret;
  1707. fud = fuse_get_dev(out);
  1708. if (!fud)
  1709. return -EPERM;
  1710. pipe_lock(pipe);
  1711. bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
  1712. if (!bufs) {
  1713. pipe_unlock(pipe);
  1714. return -ENOMEM;
  1715. }
  1716. nbuf = 0;
  1717. rem = 0;
  1718. for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
  1719. rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
  1720. ret = -EINVAL;
  1721. if (rem < len) {
  1722. pipe_unlock(pipe);
  1723. goto out;
  1724. }
  1725. rem = len;
  1726. while (rem) {
  1727. struct pipe_buffer *ibuf;
  1728. struct pipe_buffer *obuf;
  1729. BUG_ON(nbuf >= pipe->buffers);
  1730. BUG_ON(!pipe->nrbufs);
  1731. ibuf = &pipe->bufs[pipe->curbuf];
  1732. obuf = &bufs[nbuf];
  1733. if (rem >= ibuf->len) {
  1734. *obuf = *ibuf;
  1735. ibuf->ops = NULL;
  1736. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  1737. pipe->nrbufs--;
  1738. } else {
  1739. ibuf->ops->get(pipe, ibuf);
  1740. *obuf = *ibuf;
  1741. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1742. obuf->len = rem;
  1743. ibuf->offset += obuf->len;
  1744. ibuf->len -= obuf->len;
  1745. }
  1746. nbuf++;
  1747. rem -= obuf->len;
  1748. }
  1749. pipe_unlock(pipe);
  1750. fuse_copy_init(&cs, 0, NULL);
  1751. cs.pipebufs = bufs;
  1752. cs.nr_segs = nbuf;
  1753. cs.pipe = pipe;
  1754. if (flags & SPLICE_F_MOVE)
  1755. cs.move_pages = 1;
  1756. ret = fuse_dev_do_write(fud, &cs, len);
  1757. pipe_lock(pipe);
  1758. for (idx = 0; idx < nbuf; idx++) {
  1759. struct pipe_buffer *buf = &bufs[idx];
  1760. buf->ops->release(pipe, buf);
  1761. }
  1762. pipe_unlock(pipe);
  1763. out:
  1764. kfree(bufs);
  1765. return ret;
  1766. }
  1767. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  1768. {
  1769. unsigned mask = POLLOUT | POLLWRNORM;
  1770. struct fuse_iqueue *fiq;
  1771. struct fuse_dev *fud = fuse_get_dev(file);
  1772. if (!fud)
  1773. return POLLERR;
  1774. fiq = &fud->fc->iq;
  1775. poll_wait(file, &fiq->waitq, wait);
  1776. spin_lock(&fiq->waitq.lock);
  1777. if (!fiq->connected)
  1778. mask = POLLERR;
  1779. else if (request_pending(fiq))
  1780. mask |= POLLIN | POLLRDNORM;
  1781. spin_unlock(&fiq->waitq.lock);
  1782. return mask;
  1783. }
  1784. /*
  1785. * Abort all requests on the given list (pending or processing)
  1786. *
  1787. * This function releases and reacquires fc->lock
  1788. */
  1789. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  1790. {
  1791. while (!list_empty(head)) {
  1792. struct fuse_req *req;
  1793. req = list_entry(head->next, struct fuse_req, list);
  1794. req->out.h.error = -ECONNABORTED;
  1795. clear_bit(FR_SENT, &req->flags);
  1796. list_del_init(&req->list);
  1797. request_end(fc, req);
  1798. }
  1799. }
  1800. static void end_polls(struct fuse_conn *fc)
  1801. {
  1802. struct rb_node *p;
  1803. p = rb_first(&fc->polled_files);
  1804. while (p) {
  1805. struct fuse_file *ff;
  1806. ff = rb_entry(p, struct fuse_file, polled_node);
  1807. wake_up_interruptible_all(&ff->poll_wait);
  1808. p = rb_next(p);
  1809. }
  1810. }
  1811. /*
  1812. * Abort all requests.
  1813. *
  1814. * Emergency exit in case of a malicious or accidental deadlock, or just a hung
  1815. * filesystem.
  1816. *
  1817. * The same effect is usually achievable through killing the filesystem daemon
  1818. * and all users of the filesystem. The exception is the combination of an
  1819. * asynchronous request and the tricky deadlock (see
  1820. * Documentation/filesystems/fuse.txt).
  1821. *
  1822. * Aborting requests under I/O goes as follows: 1: Separate out unlocked
  1823. * requests, they should be finished off immediately. Locked requests will be
  1824. * finished after unlock; see unlock_request(). 2: Finish off the unlocked
  1825. * requests. It is possible that some request will finish before we can. This
  1826. * is OK, the request will in that case be removed from the list before we touch
  1827. * it.
  1828. */
  1829. void fuse_abort_conn(struct fuse_conn *fc)
  1830. {
  1831. struct fuse_iqueue *fiq = &fc->iq;
  1832. spin_lock(&fc->lock);
  1833. if (fc->connected) {
  1834. struct fuse_dev *fud;
  1835. struct fuse_req *req, *next;
  1836. LIST_HEAD(to_end1);
  1837. LIST_HEAD(to_end2);
  1838. fc->connected = 0;
  1839. fc->blocked = 0;
  1840. fuse_set_initialized(fc);
  1841. list_for_each_entry(fud, &fc->devices, entry) {
  1842. struct fuse_pqueue *fpq = &fud->pq;
  1843. spin_lock(&fpq->lock);
  1844. fpq->connected = 0;
  1845. list_for_each_entry_safe(req, next, &fpq->io, list) {
  1846. req->out.h.error = -ECONNABORTED;
  1847. spin_lock(&req->waitq.lock);
  1848. set_bit(FR_ABORTED, &req->flags);
  1849. if (!test_bit(FR_LOCKED, &req->flags)) {
  1850. set_bit(FR_PRIVATE, &req->flags);
  1851. __fuse_get_request(req);
  1852. list_move(&req->list, &to_end1);
  1853. }
  1854. spin_unlock(&req->waitq.lock);
  1855. }
  1856. list_splice_init(&fpq->processing, &to_end2);
  1857. spin_unlock(&fpq->lock);
  1858. }
  1859. fc->max_background = UINT_MAX;
  1860. flush_bg_queue(fc);
  1861. spin_lock(&fiq->waitq.lock);
  1862. fiq->connected = 0;
  1863. list_splice_init(&fiq->pending, &to_end2);
  1864. list_for_each_entry(req, &to_end2, list)
  1865. clear_bit(FR_PENDING, &req->flags);
  1866. while (forget_pending(fiq))
  1867. kfree(dequeue_forget(fiq, 1, NULL));
  1868. wake_up_all_locked(&fiq->waitq);
  1869. spin_unlock(&fiq->waitq.lock);
  1870. kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
  1871. end_polls(fc);
  1872. wake_up_all(&fc->blocked_waitq);
  1873. spin_unlock(&fc->lock);
  1874. while (!list_empty(&to_end1)) {
  1875. req = list_first_entry(&to_end1, struct fuse_req, list);
  1876. list_del_init(&req->list);
  1877. request_end(fc, req);
  1878. }
  1879. end_requests(fc, &to_end2);
  1880. } else {
  1881. spin_unlock(&fc->lock);
  1882. }
  1883. }
  1884. EXPORT_SYMBOL_GPL(fuse_abort_conn);
  1885. void fuse_wait_aborted(struct fuse_conn *fc)
  1886. {
  1887. wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
  1888. }
  1889. int fuse_dev_release(struct inode *inode, struct file *file)
  1890. {
  1891. struct fuse_dev *fud = fuse_get_dev(file);
  1892. if (fud) {
  1893. struct fuse_conn *fc = fud->fc;
  1894. struct fuse_pqueue *fpq = &fud->pq;
  1895. LIST_HEAD(to_end);
  1896. spin_lock(&fpq->lock);
  1897. WARN_ON(!list_empty(&fpq->io));
  1898. list_splice_init(&fpq->processing, &to_end);
  1899. spin_unlock(&fpq->lock);
  1900. end_requests(fc, &to_end);
  1901. /* Are we the last open device? */
  1902. if (atomic_dec_and_test(&fc->dev_count)) {
  1903. WARN_ON(fc->iq.fasync != NULL);
  1904. fuse_abort_conn(fc);
  1905. }
  1906. fuse_dev_free(fud);
  1907. }
  1908. return 0;
  1909. }
  1910. EXPORT_SYMBOL_GPL(fuse_dev_release);
  1911. static int fuse_dev_fasync(int fd, struct file *file, int on)
  1912. {
  1913. struct fuse_dev *fud = fuse_get_dev(file);
  1914. if (!fud)
  1915. return -EPERM;
  1916. /* No locking - fasync_helper does its own locking */
  1917. return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
  1918. }
  1919. static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
  1920. {
  1921. struct fuse_dev *fud;
  1922. if (new->private_data)
  1923. return -EINVAL;
  1924. fud = fuse_dev_alloc(fc);
  1925. if (!fud)
  1926. return -ENOMEM;
  1927. new->private_data = fud;
  1928. atomic_inc(&fc->dev_count);
  1929. return 0;
  1930. }
  1931. static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
  1932. unsigned long arg)
  1933. {
  1934. int err = -ENOTTY;
  1935. if (cmd == FUSE_DEV_IOC_CLONE) {
  1936. int oldfd;
  1937. err = -EFAULT;
  1938. if (!get_user(oldfd, (__u32 __user *) arg)) {
  1939. struct file *old = fget(oldfd);
  1940. err = -EINVAL;
  1941. if (old) {
  1942. struct fuse_dev *fud = NULL;
  1943. /*
  1944. * Check against file->f_op because CUSE
  1945. * uses the same ioctl handler.
  1946. */
  1947. if (old->f_op == file->f_op &&
  1948. old->f_cred->user_ns == file->f_cred->user_ns)
  1949. fud = fuse_get_dev(old);
  1950. if (fud) {
  1951. mutex_lock(&fuse_mutex);
  1952. err = fuse_device_clone(fud->fc, file);
  1953. mutex_unlock(&fuse_mutex);
  1954. }
  1955. fput(old);
  1956. }
  1957. }
  1958. }
  1959. return err;
  1960. }
  1961. const struct file_operations fuse_dev_operations = {
  1962. .owner = THIS_MODULE,
  1963. .open = fuse_dev_open,
  1964. .llseek = no_llseek,
  1965. .read_iter = fuse_dev_read,
  1966. .splice_read = fuse_dev_splice_read,
  1967. .write_iter = fuse_dev_write,
  1968. .splice_write = fuse_dev_splice_write,
  1969. .poll = fuse_dev_poll,
  1970. .release = fuse_dev_release,
  1971. .fasync = fuse_dev_fasync,
  1972. .unlocked_ioctl = fuse_dev_ioctl,
  1973. .compat_ioctl = fuse_dev_ioctl,
  1974. };
  1975. EXPORT_SYMBOL_GPL(fuse_dev_operations);
  1976. static struct miscdevice fuse_miscdevice = {
  1977. .minor = FUSE_MINOR,
  1978. .name = "fuse",
  1979. .fops = &fuse_dev_operations,
  1980. };
  1981. int __init fuse_dev_init(void)
  1982. {
  1983. int err = -ENOMEM;
  1984. fuse_req_cachep = kmem_cache_create("fuse_request",
  1985. sizeof(struct fuse_req),
  1986. 0, 0, NULL);
  1987. if (!fuse_req_cachep)
  1988. goto out;
  1989. err = misc_register(&fuse_miscdevice);
  1990. if (err)
  1991. goto out_cache_clean;
  1992. return 0;
  1993. out_cache_clean:
  1994. kmem_cache_destroy(fuse_req_cachep);
  1995. out:
  1996. return err;
  1997. }
  1998. void fuse_dev_cleanup(void)
  1999. {
  2000. misc_deregister(&fuse_miscdevice);
  2001. kmem_cache_destroy(fuse_req_cachep);
  2002. }