dev.c 51 KB

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