dev.c 42 KB

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