dev.c 45 KB

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