dev.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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. void *mapaddr = kmap_atomic(page, KM_USER1);
  703. memset(mapaddr, 0, PAGE_SIZE);
  704. kunmap_atomic(mapaddr, KM_USER1);
  705. }
  706. while (count) {
  707. if (cs->write && cs->pipebufs && page) {
  708. return fuse_ref_page(cs, page, offset, count);
  709. } else if (!cs->len) {
  710. if (cs->move_pages && page &&
  711. offset == 0 && count == PAGE_SIZE) {
  712. err = fuse_try_move_page(cs, pagep);
  713. if (err <= 0)
  714. return err;
  715. } else {
  716. err = fuse_copy_fill(cs);
  717. if (err)
  718. return err;
  719. }
  720. }
  721. if (page) {
  722. void *mapaddr = kmap_atomic(page, KM_USER1);
  723. void *buf = mapaddr + offset;
  724. offset += fuse_copy_do(cs, &buf, &count);
  725. kunmap_atomic(mapaddr, KM_USER1);
  726. } else
  727. offset += fuse_copy_do(cs, NULL, &count);
  728. }
  729. if (page && !cs->write)
  730. flush_dcache_page(page);
  731. return 0;
  732. }
  733. /* Copy pages in the request to/from userspace buffer */
  734. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  735. int zeroing)
  736. {
  737. unsigned i;
  738. struct fuse_req *req = cs->req;
  739. unsigned offset = req->page_offset;
  740. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  741. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  742. int err;
  743. err = fuse_copy_page(cs, &req->pages[i], offset, count,
  744. zeroing);
  745. if (err)
  746. return err;
  747. nbytes -= count;
  748. count = min(nbytes, (unsigned) PAGE_SIZE);
  749. offset = 0;
  750. }
  751. return 0;
  752. }
  753. /* Copy a single argument in the request to/from userspace buffer */
  754. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  755. {
  756. while (size) {
  757. if (!cs->len) {
  758. int err = fuse_copy_fill(cs);
  759. if (err)
  760. return err;
  761. }
  762. fuse_copy_do(cs, &val, &size);
  763. }
  764. return 0;
  765. }
  766. /* Copy request arguments to/from userspace buffer */
  767. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  768. unsigned argpages, struct fuse_arg *args,
  769. int zeroing)
  770. {
  771. int err = 0;
  772. unsigned i;
  773. for (i = 0; !err && i < numargs; i++) {
  774. struct fuse_arg *arg = &args[i];
  775. if (i == numargs - 1 && argpages)
  776. err = fuse_copy_pages(cs, arg->size, zeroing);
  777. else
  778. err = fuse_copy_one(cs, arg->value, arg->size);
  779. }
  780. return err;
  781. }
  782. static int request_pending(struct fuse_conn *fc)
  783. {
  784. return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
  785. }
  786. /* Wait until a request is available on the pending list */
  787. static void request_wait(struct fuse_conn *fc)
  788. __releases(fc->lock)
  789. __acquires(fc->lock)
  790. {
  791. DECLARE_WAITQUEUE(wait, current);
  792. add_wait_queue_exclusive(&fc->waitq, &wait);
  793. while (fc->connected && !request_pending(fc)) {
  794. set_current_state(TASK_INTERRUPTIBLE);
  795. if (signal_pending(current))
  796. break;
  797. spin_unlock(&fc->lock);
  798. schedule();
  799. spin_lock(&fc->lock);
  800. }
  801. set_current_state(TASK_RUNNING);
  802. remove_wait_queue(&fc->waitq, &wait);
  803. }
  804. /*
  805. * Transfer an interrupt request to userspace
  806. *
  807. * Unlike other requests this is assembled on demand, without a need
  808. * to allocate a separate fuse_req structure.
  809. *
  810. * Called with fc->lock held, releases it
  811. */
  812. static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
  813. size_t nbytes, struct fuse_req *req)
  814. __releases(fc->lock)
  815. {
  816. struct fuse_in_header ih;
  817. struct fuse_interrupt_in arg;
  818. unsigned reqsize = sizeof(ih) + sizeof(arg);
  819. int err;
  820. list_del_init(&req->intr_entry);
  821. req->intr_unique = fuse_get_unique(fc);
  822. memset(&ih, 0, sizeof(ih));
  823. memset(&arg, 0, sizeof(arg));
  824. ih.len = reqsize;
  825. ih.opcode = FUSE_INTERRUPT;
  826. ih.unique = req->intr_unique;
  827. arg.unique = req->in.h.unique;
  828. spin_unlock(&fc->lock);
  829. if (nbytes < reqsize)
  830. return -EINVAL;
  831. err = fuse_copy_one(cs, &ih, sizeof(ih));
  832. if (!err)
  833. err = fuse_copy_one(cs, &arg, sizeof(arg));
  834. fuse_copy_finish(cs);
  835. return err ? err : reqsize;
  836. }
  837. /*
  838. * Read a single request into the userspace filesystem's buffer. This
  839. * function waits until a request is available, then removes it from
  840. * the pending list and copies request data to userspace buffer. If
  841. * no reply is needed (FORGET) or request has been aborted or there
  842. * was an error during the copying then it's finished by calling
  843. * request_end(). Otherwise add it to the processing list, and set
  844. * the 'sent' flag.
  845. */
  846. static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
  847. struct fuse_copy_state *cs, size_t nbytes)
  848. {
  849. int err;
  850. struct fuse_req *req;
  851. struct fuse_in *in;
  852. unsigned reqsize;
  853. restart:
  854. spin_lock(&fc->lock);
  855. err = -EAGAIN;
  856. if ((file->f_flags & O_NONBLOCK) && fc->connected &&
  857. !request_pending(fc))
  858. goto err_unlock;
  859. request_wait(fc);
  860. err = -ENODEV;
  861. if (!fc->connected)
  862. goto err_unlock;
  863. err = -ERESTARTSYS;
  864. if (!request_pending(fc))
  865. goto err_unlock;
  866. if (!list_empty(&fc->interrupts)) {
  867. req = list_entry(fc->interrupts.next, struct fuse_req,
  868. intr_entry);
  869. return fuse_read_interrupt(fc, cs, nbytes, req);
  870. }
  871. req = list_entry(fc->pending.next, struct fuse_req, list);
  872. req->state = FUSE_REQ_READING;
  873. list_move(&req->list, &fc->io);
  874. in = &req->in;
  875. reqsize = in->h.len;
  876. /* If request is too large, reply with an error and restart the read */
  877. if (nbytes < reqsize) {
  878. req->out.h.error = -EIO;
  879. /* SETXATTR is special, since it may contain too large data */
  880. if (in->h.opcode == FUSE_SETXATTR)
  881. req->out.h.error = -E2BIG;
  882. request_end(fc, req);
  883. goto restart;
  884. }
  885. spin_unlock(&fc->lock);
  886. cs->req = req;
  887. err = fuse_copy_one(cs, &in->h, sizeof(in->h));
  888. if (!err)
  889. err = fuse_copy_args(cs, in->numargs, in->argpages,
  890. (struct fuse_arg *) in->args, 0);
  891. fuse_copy_finish(cs);
  892. spin_lock(&fc->lock);
  893. req->locked = 0;
  894. if (req->aborted) {
  895. request_end(fc, req);
  896. return -ENODEV;
  897. }
  898. if (err) {
  899. req->out.h.error = -EIO;
  900. request_end(fc, req);
  901. return err;
  902. }
  903. if (!req->isreply)
  904. request_end(fc, req);
  905. else {
  906. req->state = FUSE_REQ_SENT;
  907. list_move_tail(&req->list, &fc->processing);
  908. if (req->interrupted)
  909. queue_interrupt(fc, req);
  910. spin_unlock(&fc->lock);
  911. }
  912. return reqsize;
  913. err_unlock:
  914. spin_unlock(&fc->lock);
  915. return err;
  916. }
  917. static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
  918. unsigned long nr_segs, loff_t pos)
  919. {
  920. struct fuse_copy_state cs;
  921. struct file *file = iocb->ki_filp;
  922. struct fuse_conn *fc = fuse_get_conn(file);
  923. if (!fc)
  924. return -EPERM;
  925. fuse_copy_init(&cs, fc, 1, iov, nr_segs);
  926. return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
  927. }
  928. static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
  929. struct pipe_buffer *buf)
  930. {
  931. return 1;
  932. }
  933. static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
  934. .can_merge = 0,
  935. .map = generic_pipe_buf_map,
  936. .unmap = generic_pipe_buf_unmap,
  937. .confirm = generic_pipe_buf_confirm,
  938. .release = generic_pipe_buf_release,
  939. .steal = fuse_dev_pipe_buf_steal,
  940. .get = generic_pipe_buf_get,
  941. };
  942. static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
  943. struct pipe_inode_info *pipe,
  944. size_t len, unsigned int flags)
  945. {
  946. int ret;
  947. int page_nr = 0;
  948. int do_wakeup = 0;
  949. struct pipe_buffer *bufs;
  950. struct fuse_copy_state cs;
  951. struct fuse_conn *fc = fuse_get_conn(in);
  952. if (!fc)
  953. return -EPERM;
  954. bufs = kmalloc(pipe->buffers * sizeof (struct pipe_buffer), GFP_KERNEL);
  955. if (!bufs)
  956. return -ENOMEM;
  957. fuse_copy_init(&cs, fc, 1, NULL, 0);
  958. cs.pipebufs = bufs;
  959. cs.pipe = pipe;
  960. ret = fuse_dev_do_read(fc, in, &cs, len);
  961. if (ret < 0)
  962. goto out;
  963. ret = 0;
  964. pipe_lock(pipe);
  965. if (!pipe->readers) {
  966. send_sig(SIGPIPE, current, 0);
  967. if (!ret)
  968. ret = -EPIPE;
  969. goto out_unlock;
  970. }
  971. if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
  972. ret = -EIO;
  973. goto out_unlock;
  974. }
  975. while (page_nr < cs.nr_segs) {
  976. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  977. struct pipe_buffer *buf = pipe->bufs + newbuf;
  978. buf->page = bufs[page_nr].page;
  979. buf->offset = bufs[page_nr].offset;
  980. buf->len = bufs[page_nr].len;
  981. buf->ops = &fuse_dev_pipe_buf_ops;
  982. pipe->nrbufs++;
  983. page_nr++;
  984. ret += buf->len;
  985. if (pipe->inode)
  986. do_wakeup = 1;
  987. }
  988. out_unlock:
  989. pipe_unlock(pipe);
  990. if (do_wakeup) {
  991. smp_mb();
  992. if (waitqueue_active(&pipe->wait))
  993. wake_up_interruptible(&pipe->wait);
  994. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  995. }
  996. out:
  997. for (; page_nr < cs.nr_segs; page_nr++)
  998. page_cache_release(bufs[page_nr].page);
  999. kfree(bufs);
  1000. return ret;
  1001. }
  1002. static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
  1003. struct fuse_copy_state *cs)
  1004. {
  1005. struct fuse_notify_poll_wakeup_out outarg;
  1006. int err = -EINVAL;
  1007. if (size != sizeof(outarg))
  1008. goto err;
  1009. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1010. if (err)
  1011. goto err;
  1012. fuse_copy_finish(cs);
  1013. return fuse_notify_poll_wakeup(fc, &outarg);
  1014. err:
  1015. fuse_copy_finish(cs);
  1016. return err;
  1017. }
  1018. static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
  1019. struct fuse_copy_state *cs)
  1020. {
  1021. struct fuse_notify_inval_inode_out outarg;
  1022. int err = -EINVAL;
  1023. if (size != sizeof(outarg))
  1024. goto err;
  1025. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1026. if (err)
  1027. goto err;
  1028. fuse_copy_finish(cs);
  1029. down_read(&fc->killsb);
  1030. err = -ENOENT;
  1031. if (fc->sb) {
  1032. err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
  1033. outarg.off, outarg.len);
  1034. }
  1035. up_read(&fc->killsb);
  1036. return err;
  1037. err:
  1038. fuse_copy_finish(cs);
  1039. return err;
  1040. }
  1041. static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
  1042. struct fuse_copy_state *cs)
  1043. {
  1044. struct fuse_notify_inval_entry_out outarg;
  1045. int err = -ENOMEM;
  1046. char *buf;
  1047. struct qstr name;
  1048. buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
  1049. if (!buf)
  1050. goto err;
  1051. err = -EINVAL;
  1052. if (size < sizeof(outarg))
  1053. goto err;
  1054. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1055. if (err)
  1056. goto err;
  1057. err = -ENAMETOOLONG;
  1058. if (outarg.namelen > FUSE_NAME_MAX)
  1059. goto err;
  1060. name.name = buf;
  1061. name.len = outarg.namelen;
  1062. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1063. if (err)
  1064. goto err;
  1065. fuse_copy_finish(cs);
  1066. buf[outarg.namelen] = 0;
  1067. name.hash = full_name_hash(name.name, name.len);
  1068. down_read(&fc->killsb);
  1069. err = -ENOENT;
  1070. if (fc->sb)
  1071. err = fuse_reverse_inval_entry(fc->sb, outarg.parent, &name);
  1072. up_read(&fc->killsb);
  1073. kfree(buf);
  1074. return err;
  1075. err:
  1076. kfree(buf);
  1077. fuse_copy_finish(cs);
  1078. return err;
  1079. }
  1080. static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
  1081. struct fuse_copy_state *cs)
  1082. {
  1083. struct fuse_notify_store_out outarg;
  1084. struct inode *inode;
  1085. struct address_space *mapping;
  1086. u64 nodeid;
  1087. int err;
  1088. pgoff_t index;
  1089. unsigned int offset;
  1090. unsigned int num;
  1091. loff_t file_size;
  1092. loff_t end;
  1093. err = -EINVAL;
  1094. if (size < sizeof(outarg))
  1095. goto out_finish;
  1096. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1097. if (err)
  1098. goto out_finish;
  1099. err = -EINVAL;
  1100. if (size - sizeof(outarg) != outarg.size)
  1101. goto out_finish;
  1102. nodeid = outarg.nodeid;
  1103. down_read(&fc->killsb);
  1104. err = -ENOENT;
  1105. if (!fc->sb)
  1106. goto out_up_killsb;
  1107. inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
  1108. if (!inode)
  1109. goto out_up_killsb;
  1110. mapping = inode->i_mapping;
  1111. index = outarg.offset >> PAGE_CACHE_SHIFT;
  1112. offset = outarg.offset & ~PAGE_CACHE_MASK;
  1113. file_size = i_size_read(inode);
  1114. end = outarg.offset + outarg.size;
  1115. if (end > file_size) {
  1116. file_size = end;
  1117. fuse_write_update_size(inode, file_size);
  1118. }
  1119. num = outarg.size;
  1120. while (num) {
  1121. struct page *page;
  1122. unsigned int this_num;
  1123. err = -ENOMEM;
  1124. page = find_or_create_page(mapping, index,
  1125. mapping_gfp_mask(mapping));
  1126. if (!page)
  1127. goto out_iput;
  1128. this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
  1129. err = fuse_copy_page(cs, &page, offset, this_num, 0);
  1130. if (!err && offset == 0 && (num != 0 || file_size == end))
  1131. SetPageUptodate(page);
  1132. unlock_page(page);
  1133. page_cache_release(page);
  1134. if (err)
  1135. goto out_iput;
  1136. num -= this_num;
  1137. offset = 0;
  1138. index++;
  1139. }
  1140. err = 0;
  1141. out_iput:
  1142. iput(inode);
  1143. out_up_killsb:
  1144. up_read(&fc->killsb);
  1145. out_finish:
  1146. fuse_copy_finish(cs);
  1147. return err;
  1148. }
  1149. static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
  1150. {
  1151. int i;
  1152. for (i = 0; i < req->num_pages; i++) {
  1153. struct page *page = req->pages[i];
  1154. page_cache_release(page);
  1155. }
  1156. }
  1157. static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
  1158. struct fuse_notify_retrieve_out *outarg)
  1159. {
  1160. int err;
  1161. struct address_space *mapping = inode->i_mapping;
  1162. struct fuse_req *req;
  1163. pgoff_t index;
  1164. loff_t file_size;
  1165. unsigned int num;
  1166. unsigned int offset;
  1167. size_t total_len;
  1168. req = fuse_get_req(fc);
  1169. if (IS_ERR(req))
  1170. return PTR_ERR(req);
  1171. offset = outarg->offset & ~PAGE_CACHE_MASK;
  1172. req->in.h.opcode = FUSE_NOTIFY_REPLY;
  1173. req->in.h.nodeid = outarg->nodeid;
  1174. req->in.numargs = 2;
  1175. req->in.argpages = 1;
  1176. req->page_offset = offset;
  1177. req->end = fuse_retrieve_end;
  1178. index = outarg->offset >> PAGE_CACHE_SHIFT;
  1179. file_size = i_size_read(inode);
  1180. num = outarg->size;
  1181. if (outarg->offset > file_size)
  1182. num = 0;
  1183. else if (outarg->offset + num > file_size)
  1184. num = file_size - outarg->offset;
  1185. while (num) {
  1186. struct page *page;
  1187. unsigned int this_num;
  1188. page = find_get_page(mapping, index);
  1189. if (!page)
  1190. break;
  1191. this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
  1192. req->pages[req->num_pages] = page;
  1193. req->num_pages++;
  1194. num -= this_num;
  1195. total_len += this_num;
  1196. }
  1197. req->misc.retrieve_in.offset = outarg->offset;
  1198. req->misc.retrieve_in.size = total_len;
  1199. req->in.args[0].size = sizeof(req->misc.retrieve_in);
  1200. req->in.args[0].value = &req->misc.retrieve_in;
  1201. req->in.args[1].size = total_len;
  1202. err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
  1203. if (err)
  1204. fuse_retrieve_end(fc, req);
  1205. return err;
  1206. }
  1207. static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
  1208. struct fuse_copy_state *cs)
  1209. {
  1210. struct fuse_notify_retrieve_out outarg;
  1211. struct inode *inode;
  1212. int err;
  1213. err = -EINVAL;
  1214. if (size != sizeof(outarg))
  1215. goto copy_finish;
  1216. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1217. if (err)
  1218. goto copy_finish;
  1219. fuse_copy_finish(cs);
  1220. down_read(&fc->killsb);
  1221. err = -ENOENT;
  1222. if (fc->sb) {
  1223. u64 nodeid = outarg.nodeid;
  1224. inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
  1225. if (inode) {
  1226. err = fuse_retrieve(fc, inode, &outarg);
  1227. iput(inode);
  1228. }
  1229. }
  1230. up_read(&fc->killsb);
  1231. return err;
  1232. copy_finish:
  1233. fuse_copy_finish(cs);
  1234. return err;
  1235. }
  1236. static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
  1237. unsigned int size, struct fuse_copy_state *cs)
  1238. {
  1239. switch (code) {
  1240. case FUSE_NOTIFY_POLL:
  1241. return fuse_notify_poll(fc, size, cs);
  1242. case FUSE_NOTIFY_INVAL_INODE:
  1243. return fuse_notify_inval_inode(fc, size, cs);
  1244. case FUSE_NOTIFY_INVAL_ENTRY:
  1245. return fuse_notify_inval_entry(fc, size, cs);
  1246. case FUSE_NOTIFY_STORE:
  1247. return fuse_notify_store(fc, size, cs);
  1248. case FUSE_NOTIFY_RETRIEVE:
  1249. return fuse_notify_retrieve(fc, size, cs);
  1250. default:
  1251. fuse_copy_finish(cs);
  1252. return -EINVAL;
  1253. }
  1254. }
  1255. /* Look up request on processing list by unique ID */
  1256. static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
  1257. {
  1258. struct list_head *entry;
  1259. list_for_each(entry, &fc->processing) {
  1260. struct fuse_req *req;
  1261. req = list_entry(entry, struct fuse_req, list);
  1262. if (req->in.h.unique == unique || req->intr_unique == unique)
  1263. return req;
  1264. }
  1265. return NULL;
  1266. }
  1267. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  1268. unsigned nbytes)
  1269. {
  1270. unsigned reqsize = sizeof(struct fuse_out_header);
  1271. if (out->h.error)
  1272. return nbytes != reqsize ? -EINVAL : 0;
  1273. reqsize += len_args(out->numargs, out->args);
  1274. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  1275. return -EINVAL;
  1276. else if (reqsize > nbytes) {
  1277. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  1278. unsigned diffsize = reqsize - nbytes;
  1279. if (diffsize > lastarg->size)
  1280. return -EINVAL;
  1281. lastarg->size -= diffsize;
  1282. }
  1283. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  1284. out->page_zeroing);
  1285. }
  1286. /*
  1287. * Write a single reply to a request. First the header is copied from
  1288. * the write buffer. The request is then searched on the processing
  1289. * list by the unique ID found in the header. If found, then remove
  1290. * it from the list and copy the rest of the buffer to the request.
  1291. * The request is finished by calling request_end()
  1292. */
  1293. static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
  1294. struct fuse_copy_state *cs, size_t nbytes)
  1295. {
  1296. int err;
  1297. struct fuse_req *req;
  1298. struct fuse_out_header oh;
  1299. if (nbytes < sizeof(struct fuse_out_header))
  1300. return -EINVAL;
  1301. err = fuse_copy_one(cs, &oh, sizeof(oh));
  1302. if (err)
  1303. goto err_finish;
  1304. err = -EINVAL;
  1305. if (oh.len != nbytes)
  1306. goto err_finish;
  1307. /*
  1308. * Zero oh.unique indicates unsolicited notification message
  1309. * and error contains notification code.
  1310. */
  1311. if (!oh.unique) {
  1312. err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
  1313. return err ? err : nbytes;
  1314. }
  1315. err = -EINVAL;
  1316. if (oh.error <= -1000 || oh.error > 0)
  1317. goto err_finish;
  1318. spin_lock(&fc->lock);
  1319. err = -ENOENT;
  1320. if (!fc->connected)
  1321. goto err_unlock;
  1322. req = request_find(fc, oh.unique);
  1323. if (!req)
  1324. goto err_unlock;
  1325. if (req->aborted) {
  1326. spin_unlock(&fc->lock);
  1327. fuse_copy_finish(cs);
  1328. spin_lock(&fc->lock);
  1329. request_end(fc, req);
  1330. return -ENOENT;
  1331. }
  1332. /* Is it an interrupt reply? */
  1333. if (req->intr_unique == oh.unique) {
  1334. err = -EINVAL;
  1335. if (nbytes != sizeof(struct fuse_out_header))
  1336. goto err_unlock;
  1337. if (oh.error == -ENOSYS)
  1338. fc->no_interrupt = 1;
  1339. else if (oh.error == -EAGAIN)
  1340. queue_interrupt(fc, req);
  1341. spin_unlock(&fc->lock);
  1342. fuse_copy_finish(cs);
  1343. return nbytes;
  1344. }
  1345. req->state = FUSE_REQ_WRITING;
  1346. list_move(&req->list, &fc->io);
  1347. req->out.h = oh;
  1348. req->locked = 1;
  1349. cs->req = req;
  1350. if (!req->out.page_replace)
  1351. cs->move_pages = 0;
  1352. spin_unlock(&fc->lock);
  1353. err = copy_out_args(cs, &req->out, nbytes);
  1354. fuse_copy_finish(cs);
  1355. spin_lock(&fc->lock);
  1356. req->locked = 0;
  1357. if (!err) {
  1358. if (req->aborted)
  1359. err = -ENOENT;
  1360. } else if (!req->aborted)
  1361. req->out.h.error = -EIO;
  1362. request_end(fc, req);
  1363. return err ? err : nbytes;
  1364. err_unlock:
  1365. spin_unlock(&fc->lock);
  1366. err_finish:
  1367. fuse_copy_finish(cs);
  1368. return err;
  1369. }
  1370. static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
  1371. unsigned long nr_segs, loff_t pos)
  1372. {
  1373. struct fuse_copy_state cs;
  1374. struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
  1375. if (!fc)
  1376. return -EPERM;
  1377. fuse_copy_init(&cs, fc, 0, iov, nr_segs);
  1378. return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
  1379. }
  1380. static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
  1381. struct file *out, loff_t *ppos,
  1382. size_t len, unsigned int flags)
  1383. {
  1384. unsigned nbuf;
  1385. unsigned idx;
  1386. struct pipe_buffer *bufs;
  1387. struct fuse_copy_state cs;
  1388. struct fuse_conn *fc;
  1389. size_t rem;
  1390. ssize_t ret;
  1391. fc = fuse_get_conn(out);
  1392. if (!fc)
  1393. return -EPERM;
  1394. bufs = kmalloc(pipe->buffers * sizeof (struct pipe_buffer), GFP_KERNEL);
  1395. if (!bufs)
  1396. return -ENOMEM;
  1397. pipe_lock(pipe);
  1398. nbuf = 0;
  1399. rem = 0;
  1400. for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
  1401. rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
  1402. ret = -EINVAL;
  1403. if (rem < len) {
  1404. pipe_unlock(pipe);
  1405. goto out;
  1406. }
  1407. rem = len;
  1408. while (rem) {
  1409. struct pipe_buffer *ibuf;
  1410. struct pipe_buffer *obuf;
  1411. BUG_ON(nbuf >= pipe->buffers);
  1412. BUG_ON(!pipe->nrbufs);
  1413. ibuf = &pipe->bufs[pipe->curbuf];
  1414. obuf = &bufs[nbuf];
  1415. if (rem >= ibuf->len) {
  1416. *obuf = *ibuf;
  1417. ibuf->ops = NULL;
  1418. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  1419. pipe->nrbufs--;
  1420. } else {
  1421. ibuf->ops->get(pipe, ibuf);
  1422. *obuf = *ibuf;
  1423. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1424. obuf->len = rem;
  1425. ibuf->offset += obuf->len;
  1426. ibuf->len -= obuf->len;
  1427. }
  1428. nbuf++;
  1429. rem -= obuf->len;
  1430. }
  1431. pipe_unlock(pipe);
  1432. fuse_copy_init(&cs, fc, 0, NULL, nbuf);
  1433. cs.pipebufs = bufs;
  1434. cs.pipe = pipe;
  1435. if (flags & SPLICE_F_MOVE)
  1436. cs.move_pages = 1;
  1437. ret = fuse_dev_do_write(fc, &cs, len);
  1438. for (idx = 0; idx < nbuf; idx++) {
  1439. struct pipe_buffer *buf = &bufs[idx];
  1440. buf->ops->release(pipe, buf);
  1441. }
  1442. out:
  1443. kfree(bufs);
  1444. return ret;
  1445. }
  1446. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  1447. {
  1448. unsigned mask = POLLOUT | POLLWRNORM;
  1449. struct fuse_conn *fc = fuse_get_conn(file);
  1450. if (!fc)
  1451. return POLLERR;
  1452. poll_wait(file, &fc->waitq, wait);
  1453. spin_lock(&fc->lock);
  1454. if (!fc->connected)
  1455. mask = POLLERR;
  1456. else if (request_pending(fc))
  1457. mask |= POLLIN | POLLRDNORM;
  1458. spin_unlock(&fc->lock);
  1459. return mask;
  1460. }
  1461. /*
  1462. * Abort all requests on the given list (pending or processing)
  1463. *
  1464. * This function releases and reacquires fc->lock
  1465. */
  1466. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  1467. __releases(fc->lock)
  1468. __acquires(fc->lock)
  1469. {
  1470. while (!list_empty(head)) {
  1471. struct fuse_req *req;
  1472. req = list_entry(head->next, struct fuse_req, list);
  1473. req->out.h.error = -ECONNABORTED;
  1474. request_end(fc, req);
  1475. spin_lock(&fc->lock);
  1476. }
  1477. }
  1478. /*
  1479. * Abort requests under I/O
  1480. *
  1481. * The requests are set to aborted and finished, and the request
  1482. * waiter is woken up. This will make request_wait_answer() wait
  1483. * until the request is unlocked and then return.
  1484. *
  1485. * If the request is asynchronous, then the end function needs to be
  1486. * called after waiting for the request to be unlocked (if it was
  1487. * locked).
  1488. */
  1489. static void end_io_requests(struct fuse_conn *fc)
  1490. __releases(fc->lock)
  1491. __acquires(fc->lock)
  1492. {
  1493. while (!list_empty(&fc->io)) {
  1494. struct fuse_req *req =
  1495. list_entry(fc->io.next, struct fuse_req, list);
  1496. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  1497. req->aborted = 1;
  1498. req->out.h.error = -ECONNABORTED;
  1499. req->state = FUSE_REQ_FINISHED;
  1500. list_del_init(&req->list);
  1501. wake_up(&req->waitq);
  1502. if (end) {
  1503. req->end = NULL;
  1504. __fuse_get_request(req);
  1505. spin_unlock(&fc->lock);
  1506. wait_event(req->waitq, !req->locked);
  1507. end(fc, req);
  1508. fuse_put_request(fc, req);
  1509. spin_lock(&fc->lock);
  1510. }
  1511. }
  1512. }
  1513. static void end_queued_requests(struct fuse_conn *fc)
  1514. __releases(fc->lock)
  1515. __acquires(fc->lock)
  1516. {
  1517. fc->max_background = UINT_MAX;
  1518. flush_bg_queue(fc);
  1519. end_requests(fc, &fc->pending);
  1520. end_requests(fc, &fc->processing);
  1521. }
  1522. /*
  1523. * Abort all requests.
  1524. *
  1525. * Emergency exit in case of a malicious or accidental deadlock, or
  1526. * just a hung filesystem.
  1527. *
  1528. * The same effect is usually achievable through killing the
  1529. * filesystem daemon and all users of the filesystem. The exception
  1530. * is the combination of an asynchronous request and the tricky
  1531. * deadlock (see Documentation/filesystems/fuse.txt).
  1532. *
  1533. * During the aborting, progression of requests from the pending and
  1534. * processing lists onto the io list, and progression of new requests
  1535. * onto the pending list is prevented by req->connected being false.
  1536. *
  1537. * Progression of requests under I/O to the processing list is
  1538. * prevented by the req->aborted flag being true for these requests.
  1539. * For this reason requests on the io list must be aborted first.
  1540. */
  1541. void fuse_abort_conn(struct fuse_conn *fc)
  1542. {
  1543. spin_lock(&fc->lock);
  1544. if (fc->connected) {
  1545. fc->connected = 0;
  1546. fc->blocked = 0;
  1547. end_io_requests(fc);
  1548. end_queued_requests(fc);
  1549. wake_up_all(&fc->waitq);
  1550. wake_up_all(&fc->blocked_waitq);
  1551. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  1552. }
  1553. spin_unlock(&fc->lock);
  1554. }
  1555. EXPORT_SYMBOL_GPL(fuse_abort_conn);
  1556. int fuse_dev_release(struct inode *inode, struct file *file)
  1557. {
  1558. struct fuse_conn *fc = fuse_get_conn(file);
  1559. if (fc) {
  1560. spin_lock(&fc->lock);
  1561. fc->connected = 0;
  1562. fc->blocked = 0;
  1563. end_queued_requests(fc);
  1564. wake_up_all(&fc->blocked_waitq);
  1565. spin_unlock(&fc->lock);
  1566. fuse_conn_put(fc);
  1567. }
  1568. return 0;
  1569. }
  1570. EXPORT_SYMBOL_GPL(fuse_dev_release);
  1571. static int fuse_dev_fasync(int fd, struct file *file, int on)
  1572. {
  1573. struct fuse_conn *fc = fuse_get_conn(file);
  1574. if (!fc)
  1575. return -EPERM;
  1576. /* No locking - fasync_helper does its own locking */
  1577. return fasync_helper(fd, file, on, &fc->fasync);
  1578. }
  1579. const struct file_operations fuse_dev_operations = {
  1580. .owner = THIS_MODULE,
  1581. .llseek = no_llseek,
  1582. .read = do_sync_read,
  1583. .aio_read = fuse_dev_read,
  1584. .splice_read = fuse_dev_splice_read,
  1585. .write = do_sync_write,
  1586. .aio_write = fuse_dev_write,
  1587. .splice_write = fuse_dev_splice_write,
  1588. .poll = fuse_dev_poll,
  1589. .release = fuse_dev_release,
  1590. .fasync = fuse_dev_fasync,
  1591. };
  1592. EXPORT_SYMBOL_GPL(fuse_dev_operations);
  1593. static struct miscdevice fuse_miscdevice = {
  1594. .minor = FUSE_MINOR,
  1595. .name = "fuse",
  1596. .fops = &fuse_dev_operations,
  1597. };
  1598. int __init fuse_dev_init(void)
  1599. {
  1600. int err = -ENOMEM;
  1601. fuse_req_cachep = kmem_cache_create("fuse_request",
  1602. sizeof(struct fuse_req),
  1603. 0, 0, NULL);
  1604. if (!fuse_req_cachep)
  1605. goto out;
  1606. err = misc_register(&fuse_miscdevice);
  1607. if (err)
  1608. goto out_cache_clean;
  1609. return 0;
  1610. out_cache_clean:
  1611. kmem_cache_destroy(fuse_req_cachep);
  1612. out:
  1613. return err;
  1614. }
  1615. void fuse_dev_cleanup(void)
  1616. {
  1617. misc_deregister(&fuse_miscdevice);
  1618. kmem_cache_destroy(fuse_req_cachep);
  1619. }