dev.c 47 KB

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