dev.c 46 KB

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