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. req->stolen_file = get_file(file);
  128. }
  129. spin_unlock(&fc->lock);
  130. } while (!req);
  131. return req;
  132. }
  133. /*
  134. * Put stolen request back into fuse_file->reserved_req
  135. */
  136. static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
  137. {
  138. struct file *file = req->stolen_file;
  139. struct fuse_file *ff = file->private_data;
  140. spin_lock(&fc->lock);
  141. fuse_request_init(req);
  142. BUG_ON(ff->reserved_req);
  143. ff->reserved_req = req;
  144. wake_up_all(&fc->reserved_req_waitq);
  145. spin_unlock(&fc->lock);
  146. fput(file);
  147. }
  148. /*
  149. * Gets a requests for a file operation, always succeeds
  150. *
  151. * This is used for sending the FLUSH request, which must get to
  152. * userspace, due to POSIX locks which may need to be unlocked.
  153. *
  154. * If allocation fails due to OOM, use the reserved request in
  155. * fuse_file.
  156. *
  157. * This is very unlikely to deadlock accidentally, since the
  158. * filesystem should not have it's own file open. If deadlock is
  159. * intentional, it can still be broken by "aborting" the filesystem.
  160. */
  161. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
  162. {
  163. struct fuse_req *req;
  164. atomic_inc(&fc->num_waiting);
  165. wait_event(fc->blocked_waitq, !fc->blocked);
  166. req = fuse_request_alloc();
  167. if (!req)
  168. req = get_reserved_req(fc, file);
  169. fuse_req_init_context(req);
  170. req->waiting = 1;
  171. return req;
  172. }
  173. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
  174. {
  175. if (atomic_dec_and_test(&req->count)) {
  176. if (req->waiting)
  177. atomic_dec(&fc->num_waiting);
  178. if (req->stolen_file)
  179. put_reserved_req(fc, req);
  180. else
  181. fuse_request_free(req);
  182. }
  183. }
  184. EXPORT_SYMBOL_GPL(fuse_put_request);
  185. static unsigned len_args(unsigned numargs, struct fuse_arg *args)
  186. {
  187. unsigned nbytes = 0;
  188. unsigned i;
  189. for (i = 0; i < numargs; i++)
  190. nbytes += args[i].size;
  191. return nbytes;
  192. }
  193. static u64 fuse_get_unique(struct fuse_conn *fc)
  194. {
  195. fc->reqctr++;
  196. /* zero is special */
  197. if (fc->reqctr == 0)
  198. fc->reqctr = 1;
  199. return fc->reqctr;
  200. }
  201. static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
  202. {
  203. req->in.h.len = sizeof(struct fuse_in_header) +
  204. len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
  205. list_add_tail(&req->list, &fc->pending);
  206. req->state = FUSE_REQ_PENDING;
  207. if (!req->waiting) {
  208. req->waiting = 1;
  209. atomic_inc(&fc->num_waiting);
  210. }
  211. wake_up(&fc->waitq);
  212. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  213. }
  214. void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
  215. u64 nodeid, u64 nlookup)
  216. {
  217. forget->forget_one.nodeid = nodeid;
  218. forget->forget_one.nlookup = nlookup;
  219. spin_lock(&fc->lock);
  220. if (fc->connected) {
  221. fc->forget_list_tail->next = forget;
  222. fc->forget_list_tail = forget;
  223. wake_up(&fc->waitq);
  224. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  225. } else {
  226. kfree(forget);
  227. }
  228. spin_unlock(&fc->lock);
  229. }
  230. static void flush_bg_queue(struct fuse_conn *fc)
  231. {
  232. while (fc->active_background < fc->max_background &&
  233. !list_empty(&fc->bg_queue)) {
  234. struct fuse_req *req;
  235. req = list_entry(fc->bg_queue.next, struct fuse_req, list);
  236. list_del(&req->list);
  237. fc->active_background++;
  238. req->in.h.unique = fuse_get_unique(fc);
  239. queue_request(fc, req);
  240. }
  241. }
  242. /*
  243. * This function is called when a request is finished. Either a reply
  244. * has arrived or it was aborted (and not yet sent) or some error
  245. * occurred during communication with userspace, or the device file
  246. * was closed. The requester thread is woken up (if still waiting),
  247. * the 'end' callback is called if given, else the reference to the
  248. * request is released
  249. *
  250. * Called with fc->lock, unlocks it
  251. */
  252. static void request_end(struct fuse_conn *fc, struct fuse_req *req)
  253. __releases(fc->lock)
  254. {
  255. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  256. req->end = NULL;
  257. list_del(&req->list);
  258. list_del(&req->intr_entry);
  259. req->state = FUSE_REQ_FINISHED;
  260. if (req->background) {
  261. if (fc->num_background == fc->max_background) {
  262. fc->blocked = 0;
  263. wake_up_all(&fc->blocked_waitq);
  264. }
  265. if (fc->num_background == fc->congestion_threshold &&
  266. fc->connected && fc->bdi_initialized) {
  267. clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  268. clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  269. }
  270. fc->num_background--;
  271. fc->active_background--;
  272. flush_bg_queue(fc);
  273. }
  274. spin_unlock(&fc->lock);
  275. wake_up(&req->waitq);
  276. if (end)
  277. end(fc, req);
  278. fuse_put_request(fc, req);
  279. }
  280. static void wait_answer_interruptible(struct fuse_conn *fc,
  281. struct fuse_req *req)
  282. __releases(fc->lock)
  283. __acquires(fc->lock)
  284. {
  285. if (signal_pending(current))
  286. return;
  287. spin_unlock(&fc->lock);
  288. wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
  289. spin_lock(&fc->lock);
  290. }
  291. static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
  292. {
  293. list_add_tail(&req->intr_entry, &fc->interrupts);
  294. wake_up(&fc->waitq);
  295. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  296. }
  297. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  298. __releases(fc->lock)
  299. __acquires(fc->lock)
  300. {
  301. if (!fc->no_interrupt) {
  302. /* Any signal may interrupt this */
  303. wait_answer_interruptible(fc, req);
  304. if (req->aborted)
  305. goto aborted;
  306. if (req->state == FUSE_REQ_FINISHED)
  307. return;
  308. req->interrupted = 1;
  309. if (req->state == FUSE_REQ_SENT)
  310. queue_interrupt(fc, req);
  311. }
  312. if (!req->force) {
  313. sigset_t oldset;
  314. /* Only fatal signals may interrupt this */
  315. block_sigs(&oldset);
  316. wait_answer_interruptible(fc, req);
  317. restore_sigs(&oldset);
  318. if (req->aborted)
  319. goto aborted;
  320. if (req->state == FUSE_REQ_FINISHED)
  321. return;
  322. /* Request is not yet in userspace, bail out */
  323. if (req->state == FUSE_REQ_PENDING) {
  324. list_del(&req->list);
  325. __fuse_put_request(req);
  326. req->out.h.error = -EINTR;
  327. return;
  328. }
  329. }
  330. /*
  331. * Either request is already in userspace, or it was forced.
  332. * Wait it out.
  333. */
  334. spin_unlock(&fc->lock);
  335. wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
  336. spin_lock(&fc->lock);
  337. if (!req->aborted)
  338. return;
  339. aborted:
  340. BUG_ON(req->state != FUSE_REQ_FINISHED);
  341. if (req->locked) {
  342. /* This is uninterruptible sleep, because data is
  343. being copied to/from the buffers of req. During
  344. locked state, there mustn't be any filesystem
  345. operation (e.g. page fault), since that could lead
  346. to deadlock */
  347. spin_unlock(&fc->lock);
  348. wait_event(req->waitq, !req->locked);
  349. spin_lock(&fc->lock);
  350. }
  351. }
  352. void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
  353. {
  354. req->isreply = 1;
  355. spin_lock(&fc->lock);
  356. if (!fc->connected)
  357. req->out.h.error = -ENOTCONN;
  358. else if (fc->conn_error)
  359. req->out.h.error = -ECONNREFUSED;
  360. else {
  361. req->in.h.unique = fuse_get_unique(fc);
  362. queue_request(fc, req);
  363. /* acquire extra reference, since request is still needed
  364. after request_end() */
  365. __fuse_get_request(req);
  366. request_wait_answer(fc, req);
  367. }
  368. spin_unlock(&fc->lock);
  369. }
  370. EXPORT_SYMBOL_GPL(fuse_request_send);
  371. static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
  372. struct fuse_req *req)
  373. {
  374. req->background = 1;
  375. fc->num_background++;
  376. if (fc->num_background == fc->max_background)
  377. fc->blocked = 1;
  378. if (fc->num_background == fc->congestion_threshold &&
  379. fc->bdi_initialized) {
  380. set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  381. set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  382. }
  383. list_add_tail(&req->list, &fc->bg_queue);
  384. flush_bg_queue(fc);
  385. }
  386. static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
  387. {
  388. spin_lock(&fc->lock);
  389. if (fc->connected) {
  390. fuse_request_send_nowait_locked(fc, req);
  391. spin_unlock(&fc->lock);
  392. } else {
  393. req->out.h.error = -ENOTCONN;
  394. request_end(fc, req);
  395. }
  396. }
  397. void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  398. {
  399. req->isreply = 1;
  400. fuse_request_send_nowait(fc, req);
  401. }
  402. EXPORT_SYMBOL_GPL(fuse_request_send_background);
  403. static int fuse_request_send_notify_reply(struct fuse_conn *fc,
  404. struct fuse_req *req, u64 unique)
  405. {
  406. int err = -ENODEV;
  407. req->isreply = 0;
  408. req->in.h.unique = unique;
  409. spin_lock(&fc->lock);
  410. if (fc->connected) {
  411. queue_request(fc, req);
  412. err = 0;
  413. }
  414. spin_unlock(&fc->lock);
  415. return err;
  416. }
  417. /*
  418. * Called under fc->lock
  419. *
  420. * fc->connected must have been checked previously
  421. */
  422. void fuse_request_send_background_locked(struct fuse_conn *fc,
  423. struct fuse_req *req)
  424. {
  425. req->isreply = 1;
  426. fuse_request_send_nowait_locked(fc, req);
  427. }
  428. /*
  429. * Lock the request. Up to the next unlock_request() there mustn't be
  430. * anything that could cause a page-fault. If the request was already
  431. * aborted bail out.
  432. */
  433. static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
  434. {
  435. int err = 0;
  436. if (req) {
  437. spin_lock(&fc->lock);
  438. if (req->aborted)
  439. err = -ENOENT;
  440. else
  441. req->locked = 1;
  442. spin_unlock(&fc->lock);
  443. }
  444. return err;
  445. }
  446. /*
  447. * Unlock request. If it was aborted during being locked, the
  448. * requester thread is currently waiting for it to be unlocked, so
  449. * wake it up.
  450. */
  451. static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
  452. {
  453. if (req) {
  454. spin_lock(&fc->lock);
  455. req->locked = 0;
  456. if (req->aborted)
  457. wake_up(&req->waitq);
  458. spin_unlock(&fc->lock);
  459. }
  460. }
  461. struct fuse_copy_state {
  462. struct fuse_conn *fc;
  463. int write;
  464. struct fuse_req *req;
  465. const struct iovec *iov;
  466. struct pipe_buffer *pipebufs;
  467. struct pipe_buffer *currbuf;
  468. struct pipe_inode_info *pipe;
  469. unsigned long nr_segs;
  470. unsigned long seglen;
  471. unsigned long addr;
  472. struct page *pg;
  473. void *mapaddr;
  474. void *buf;
  475. unsigned len;
  476. unsigned move_pages:1;
  477. };
  478. static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
  479. int write,
  480. const struct iovec *iov, unsigned long nr_segs)
  481. {
  482. memset(cs, 0, sizeof(*cs));
  483. cs->fc = fc;
  484. cs->write = write;
  485. cs->iov = iov;
  486. cs->nr_segs = nr_segs;
  487. }
  488. /* Unmap and put previous page of userspace buffer */
  489. static void fuse_copy_finish(struct fuse_copy_state *cs)
  490. {
  491. if (cs->currbuf) {
  492. struct pipe_buffer *buf = cs->currbuf;
  493. if (!cs->write) {
  494. buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
  495. } else {
  496. kunmap(buf->page);
  497. buf->len = PAGE_SIZE - cs->len;
  498. }
  499. cs->currbuf = NULL;
  500. cs->mapaddr = NULL;
  501. } else if (cs->mapaddr) {
  502. kunmap(cs->pg);
  503. if (cs->write) {
  504. flush_dcache_page(cs->pg);
  505. set_page_dirty_lock(cs->pg);
  506. }
  507. put_page(cs->pg);
  508. cs->mapaddr = NULL;
  509. }
  510. }
  511. /*
  512. * Get another pagefull of userspace buffer, and map it to kernel
  513. * address space, and lock request
  514. */
  515. static int fuse_copy_fill(struct fuse_copy_state *cs)
  516. {
  517. unsigned long offset;
  518. int err;
  519. unlock_request(cs->fc, cs->req);
  520. fuse_copy_finish(cs);
  521. if (cs->pipebufs) {
  522. struct pipe_buffer *buf = cs->pipebufs;
  523. if (!cs->write) {
  524. err = buf->ops->confirm(cs->pipe, buf);
  525. if (err)
  526. return err;
  527. BUG_ON(!cs->nr_segs);
  528. cs->currbuf = buf;
  529. cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
  530. cs->len = buf->len;
  531. cs->buf = cs->mapaddr + buf->offset;
  532. cs->pipebufs++;
  533. cs->nr_segs--;
  534. } else {
  535. struct page *page;
  536. if (cs->nr_segs == cs->pipe->buffers)
  537. return -EIO;
  538. page = alloc_page(GFP_HIGHUSER);
  539. if (!page)
  540. return -ENOMEM;
  541. buf->page = page;
  542. buf->offset = 0;
  543. buf->len = 0;
  544. cs->currbuf = buf;
  545. cs->mapaddr = kmap(page);
  546. cs->buf = cs->mapaddr;
  547. cs->len = PAGE_SIZE;
  548. cs->pipebufs++;
  549. cs->nr_segs++;
  550. }
  551. } else {
  552. if (!cs->seglen) {
  553. BUG_ON(!cs->nr_segs);
  554. cs->seglen = cs->iov[0].iov_len;
  555. cs->addr = (unsigned long) cs->iov[0].iov_base;
  556. cs->iov++;
  557. cs->nr_segs--;
  558. }
  559. err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
  560. if (err < 0)
  561. return err;
  562. BUG_ON(err != 1);
  563. offset = cs->addr % PAGE_SIZE;
  564. cs->mapaddr = kmap(cs->pg);
  565. cs->buf = cs->mapaddr + offset;
  566. cs->len = min(PAGE_SIZE - offset, cs->seglen);
  567. cs->seglen -= cs->len;
  568. cs->addr += cs->len;
  569. }
  570. return lock_request(cs->fc, cs->req);
  571. }
  572. /* Do as much copy to/from userspace buffer as we can */
  573. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  574. {
  575. unsigned ncpy = min(*size, cs->len);
  576. if (val) {
  577. if (cs->write)
  578. memcpy(cs->buf, *val, ncpy);
  579. else
  580. memcpy(*val, cs->buf, ncpy);
  581. *val += ncpy;
  582. }
  583. *size -= ncpy;
  584. cs->len -= ncpy;
  585. cs->buf += ncpy;
  586. return ncpy;
  587. }
  588. static int fuse_check_page(struct page *page)
  589. {
  590. if (page_mapcount(page) ||
  591. page->mapping != NULL ||
  592. page_count(page) != 1 ||
  593. (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
  594. ~(1 << PG_locked |
  595. 1 << PG_referenced |
  596. 1 << PG_uptodate |
  597. 1 << PG_lru |
  598. 1 << PG_active |
  599. 1 << PG_reclaim))) {
  600. printk(KERN_WARNING "fuse: trying to steal weird page\n");
  601. 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);
  602. return 1;
  603. }
  604. return 0;
  605. }
  606. static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
  607. {
  608. int err;
  609. struct page *oldpage = *pagep;
  610. struct page *newpage;
  611. struct pipe_buffer *buf = cs->pipebufs;
  612. struct address_space *mapping;
  613. pgoff_t index;
  614. unlock_request(cs->fc, cs->req);
  615. fuse_copy_finish(cs);
  616. err = buf->ops->confirm(cs->pipe, buf);
  617. if (err)
  618. return err;
  619. BUG_ON(!cs->nr_segs);
  620. cs->currbuf = buf;
  621. cs->len = buf->len;
  622. cs->pipebufs++;
  623. cs->nr_segs--;
  624. if (cs->len != PAGE_SIZE)
  625. goto out_fallback;
  626. if (buf->ops->steal(cs->pipe, buf) != 0)
  627. goto out_fallback;
  628. newpage = buf->page;
  629. if (WARN_ON(!PageUptodate(newpage)))
  630. return -EIO;
  631. ClearPageMappedToDisk(newpage);
  632. if (fuse_check_page(newpage) != 0)
  633. goto out_fallback_unlock;
  634. mapping = oldpage->mapping;
  635. index = oldpage->index;
  636. /*
  637. * This is a new and locked page, it shouldn't be mapped or
  638. * have any special flags on it
  639. */
  640. if (WARN_ON(page_mapped(oldpage)))
  641. goto out_fallback_unlock;
  642. if (WARN_ON(page_has_private(oldpage)))
  643. goto out_fallback_unlock;
  644. if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
  645. goto out_fallback_unlock;
  646. if (WARN_ON(PageMlocked(oldpage)))
  647. goto out_fallback_unlock;
  648. err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
  649. if (err) {
  650. unlock_page(newpage);
  651. return err;
  652. }
  653. page_cache_get(newpage);
  654. if (!(buf->flags & PIPE_BUF_FLAG_LRU))
  655. lru_cache_add_file(newpage);
  656. err = 0;
  657. spin_lock(&cs->fc->lock);
  658. if (cs->req->aborted)
  659. err = -ENOENT;
  660. else
  661. *pagep = newpage;
  662. spin_unlock(&cs->fc->lock);
  663. if (err) {
  664. unlock_page(newpage);
  665. page_cache_release(newpage);
  666. return err;
  667. }
  668. unlock_page(oldpage);
  669. page_cache_release(oldpage);
  670. cs->len = 0;
  671. return 0;
  672. out_fallback_unlock:
  673. unlock_page(newpage);
  674. out_fallback:
  675. cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
  676. cs->buf = cs->mapaddr + buf->offset;
  677. err = lock_request(cs->fc, cs->req);
  678. if (err)
  679. return err;
  680. return 1;
  681. }
  682. static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
  683. unsigned offset, unsigned count)
  684. {
  685. struct pipe_buffer *buf;
  686. if (cs->nr_segs == cs->pipe->buffers)
  687. return -EIO;
  688. unlock_request(cs->fc, cs->req);
  689. fuse_copy_finish(cs);
  690. buf = cs->pipebufs;
  691. page_cache_get(page);
  692. buf->page = page;
  693. buf->offset = offset;
  694. buf->len = count;
  695. cs->pipebufs++;
  696. cs->nr_segs++;
  697. cs->len = 0;
  698. return 0;
  699. }
  700. /*
  701. * Copy a page in the request to/from the userspace buffer. Must be
  702. * done atomically
  703. */
  704. static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
  705. unsigned offset, unsigned count, int zeroing)
  706. {
  707. int err;
  708. struct page *page = *pagep;
  709. if (page && zeroing && count < PAGE_SIZE)
  710. clear_highpage(page);
  711. while (count) {
  712. if (cs->write && cs->pipebufs && page) {
  713. return fuse_ref_page(cs, page, offset, count);
  714. } else if (!cs->len) {
  715. if (cs->move_pages && page &&
  716. offset == 0 && count == PAGE_SIZE) {
  717. err = fuse_try_move_page(cs, pagep);
  718. if (err <= 0)
  719. return err;
  720. } else {
  721. err = fuse_copy_fill(cs);
  722. if (err)
  723. return err;
  724. }
  725. }
  726. if (page) {
  727. void *mapaddr = kmap_atomic(page);
  728. void *buf = mapaddr + offset;
  729. offset += fuse_copy_do(cs, &buf, &count);
  730. kunmap_atomic(mapaddr);
  731. } else
  732. offset += fuse_copy_do(cs, NULL, &count);
  733. }
  734. if (page && !cs->write)
  735. flush_dcache_page(page);
  736. return 0;
  737. }
  738. /* Copy pages in the request to/from userspace buffer */
  739. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  740. int zeroing)
  741. {
  742. unsigned i;
  743. struct fuse_req *req = cs->req;
  744. unsigned offset = req->page_offset;
  745. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  746. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  747. int err;
  748. err = fuse_copy_page(cs, &req->pages[i], offset, count,
  749. zeroing);
  750. if (err)
  751. return err;
  752. nbytes -= count;
  753. count = min(nbytes, (unsigned) PAGE_SIZE);
  754. offset = 0;
  755. }
  756. return 0;
  757. }
  758. /* Copy a single argument in the request to/from userspace buffer */
  759. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  760. {
  761. while (size) {
  762. if (!cs->len) {
  763. int err = fuse_copy_fill(cs);
  764. if (err)
  765. return err;
  766. }
  767. fuse_copy_do(cs, &val, &size);
  768. }
  769. return 0;
  770. }
  771. /* Copy request arguments to/from userspace buffer */
  772. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  773. unsigned argpages, struct fuse_arg *args,
  774. int zeroing)
  775. {
  776. int err = 0;
  777. unsigned i;
  778. for (i = 0; !err && i < numargs; i++) {
  779. struct fuse_arg *arg = &args[i];
  780. if (i == numargs - 1 && argpages)
  781. err = fuse_copy_pages(cs, arg->size, zeroing);
  782. else
  783. err = fuse_copy_one(cs, arg->value, arg->size);
  784. }
  785. return err;
  786. }
  787. static int forget_pending(struct fuse_conn *fc)
  788. {
  789. return fc->forget_list_head.next != NULL;
  790. }
  791. static int request_pending(struct fuse_conn *fc)
  792. {
  793. return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
  794. forget_pending(fc);
  795. }
  796. /* Wait until a request is available on the pending list */
  797. static void request_wait(struct fuse_conn *fc)
  798. __releases(fc->lock)
  799. __acquires(fc->lock)
  800. {
  801. DECLARE_WAITQUEUE(wait, current);
  802. add_wait_queue_exclusive(&fc->waitq, &wait);
  803. while (fc->connected && !request_pending(fc)) {
  804. set_current_state(TASK_INTERRUPTIBLE);
  805. if (signal_pending(current))
  806. break;
  807. spin_unlock(&fc->lock);
  808. schedule();
  809. spin_lock(&fc->lock);
  810. }
  811. set_current_state(TASK_RUNNING);
  812. remove_wait_queue(&fc->waitq, &wait);
  813. }
  814. /*
  815. * Transfer an interrupt request to userspace
  816. *
  817. * Unlike other requests this is assembled on demand, without a need
  818. * to allocate a separate fuse_req structure.
  819. *
  820. * Called with fc->lock held, releases it
  821. */
  822. static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
  823. size_t nbytes, struct fuse_req *req)
  824. __releases(fc->lock)
  825. {
  826. struct fuse_in_header ih;
  827. struct fuse_interrupt_in arg;
  828. unsigned reqsize = sizeof(ih) + sizeof(arg);
  829. int err;
  830. list_del_init(&req->intr_entry);
  831. req->intr_unique = fuse_get_unique(fc);
  832. memset(&ih, 0, sizeof(ih));
  833. memset(&arg, 0, sizeof(arg));
  834. ih.len = reqsize;
  835. ih.opcode = FUSE_INTERRUPT;
  836. ih.unique = req->intr_unique;
  837. arg.unique = req->in.h.unique;
  838. spin_unlock(&fc->lock);
  839. if (nbytes < reqsize)
  840. return -EINVAL;
  841. err = fuse_copy_one(cs, &ih, sizeof(ih));
  842. if (!err)
  843. err = fuse_copy_one(cs, &arg, sizeof(arg));
  844. fuse_copy_finish(cs);
  845. return err ? err : reqsize;
  846. }
  847. static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
  848. unsigned max,
  849. unsigned *countp)
  850. {
  851. struct fuse_forget_link *head = fc->forget_list_head.next;
  852. struct fuse_forget_link **newhead = &head;
  853. unsigned count;
  854. for (count = 0; *newhead != NULL && count < max; count++)
  855. newhead = &(*newhead)->next;
  856. fc->forget_list_head.next = *newhead;
  857. *newhead = NULL;
  858. if (fc->forget_list_head.next == NULL)
  859. fc->forget_list_tail = &fc->forget_list_head;
  860. if (countp != NULL)
  861. *countp = count;
  862. return head;
  863. }
  864. static int fuse_read_single_forget(struct fuse_conn *fc,
  865. struct fuse_copy_state *cs,
  866. size_t nbytes)
  867. __releases(fc->lock)
  868. {
  869. int err;
  870. struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
  871. struct fuse_forget_in arg = {
  872. .nlookup = forget->forget_one.nlookup,
  873. };
  874. struct fuse_in_header ih = {
  875. .opcode = FUSE_FORGET,
  876. .nodeid = forget->forget_one.nodeid,
  877. .unique = fuse_get_unique(fc),
  878. .len = sizeof(ih) + sizeof(arg),
  879. };
  880. spin_unlock(&fc->lock);
  881. kfree(forget);
  882. if (nbytes < ih.len)
  883. return -EINVAL;
  884. err = fuse_copy_one(cs, &ih, sizeof(ih));
  885. if (!err)
  886. err = fuse_copy_one(cs, &arg, sizeof(arg));
  887. fuse_copy_finish(cs);
  888. if (err)
  889. return err;
  890. return ih.len;
  891. }
  892. static int fuse_read_batch_forget(struct fuse_conn *fc,
  893. struct fuse_copy_state *cs, size_t nbytes)
  894. __releases(fc->lock)
  895. {
  896. int err;
  897. unsigned max_forgets;
  898. unsigned count;
  899. struct fuse_forget_link *head;
  900. struct fuse_batch_forget_in arg = { .count = 0 };
  901. struct fuse_in_header ih = {
  902. .opcode = FUSE_BATCH_FORGET,
  903. .unique = fuse_get_unique(fc),
  904. .len = sizeof(ih) + sizeof(arg),
  905. };
  906. if (nbytes < ih.len) {
  907. spin_unlock(&fc->lock);
  908. return -EINVAL;
  909. }
  910. max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
  911. head = dequeue_forget(fc, max_forgets, &count);
  912. spin_unlock(&fc->lock);
  913. arg.count = count;
  914. ih.len += count * sizeof(struct fuse_forget_one);
  915. err = fuse_copy_one(cs, &ih, sizeof(ih));
  916. if (!err)
  917. err = fuse_copy_one(cs, &arg, sizeof(arg));
  918. while (head) {
  919. struct fuse_forget_link *forget = head;
  920. if (!err) {
  921. err = fuse_copy_one(cs, &forget->forget_one,
  922. sizeof(forget->forget_one));
  923. }
  924. head = forget->next;
  925. kfree(forget);
  926. }
  927. fuse_copy_finish(cs);
  928. if (err)
  929. return err;
  930. return ih.len;
  931. }
  932. static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
  933. size_t nbytes)
  934. __releases(fc->lock)
  935. {
  936. if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
  937. return fuse_read_single_forget(fc, cs, nbytes);
  938. else
  939. return fuse_read_batch_forget(fc, cs, nbytes);
  940. }
  941. /*
  942. * Read a single request into the userspace filesystem's buffer. This
  943. * function waits until a request is available, then removes it from
  944. * the pending list and copies request data to userspace buffer. If
  945. * no reply is needed (FORGET) or request has been aborted or there
  946. * was an error during the copying then it's finished by calling
  947. * request_end(). Otherwise add it to the processing list, and set
  948. * the 'sent' flag.
  949. */
  950. static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
  951. struct fuse_copy_state *cs, size_t nbytes)
  952. {
  953. int err;
  954. struct fuse_req *req;
  955. struct fuse_in *in;
  956. unsigned reqsize;
  957. restart:
  958. spin_lock(&fc->lock);
  959. err = -EAGAIN;
  960. if ((file->f_flags & O_NONBLOCK) && fc->connected &&
  961. !request_pending(fc))
  962. goto err_unlock;
  963. request_wait(fc);
  964. err = -ENODEV;
  965. if (!fc->connected)
  966. goto err_unlock;
  967. err = -ERESTARTSYS;
  968. if (!request_pending(fc))
  969. goto err_unlock;
  970. if (!list_empty(&fc->interrupts)) {
  971. req = list_entry(fc->interrupts.next, struct fuse_req,
  972. intr_entry);
  973. return fuse_read_interrupt(fc, cs, nbytes, req);
  974. }
  975. if (forget_pending(fc)) {
  976. if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
  977. return fuse_read_forget(fc, cs, nbytes);
  978. if (fc->forget_batch <= -8)
  979. fc->forget_batch = 16;
  980. }
  981. req = list_entry(fc->pending.next, struct fuse_req, list);
  982. req->state = FUSE_REQ_READING;
  983. list_move(&req->list, &fc->io);
  984. in = &req->in;
  985. reqsize = in->h.len;
  986. /* If request is too large, reply with an error and restart the read */
  987. if (nbytes < reqsize) {
  988. req->out.h.error = -EIO;
  989. /* SETXATTR is special, since it may contain too large data */
  990. if (in->h.opcode == FUSE_SETXATTR)
  991. req->out.h.error = -E2BIG;
  992. request_end(fc, req);
  993. goto restart;
  994. }
  995. spin_unlock(&fc->lock);
  996. cs->req = req;
  997. err = fuse_copy_one(cs, &in->h, sizeof(in->h));
  998. if (!err)
  999. err = fuse_copy_args(cs, in->numargs, in->argpages,
  1000. (struct fuse_arg *) in->args, 0);
  1001. fuse_copy_finish(cs);
  1002. spin_lock(&fc->lock);
  1003. req->locked = 0;
  1004. if (req->aborted) {
  1005. request_end(fc, req);
  1006. return -ENODEV;
  1007. }
  1008. if (err) {
  1009. req->out.h.error = -EIO;
  1010. request_end(fc, req);
  1011. return err;
  1012. }
  1013. if (!req->isreply)
  1014. request_end(fc, req);
  1015. else {
  1016. req->state = FUSE_REQ_SENT;
  1017. list_move_tail(&req->list, &fc->processing);
  1018. if (req->interrupted)
  1019. queue_interrupt(fc, req);
  1020. spin_unlock(&fc->lock);
  1021. }
  1022. return reqsize;
  1023. err_unlock:
  1024. spin_unlock(&fc->lock);
  1025. return err;
  1026. }
  1027. static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
  1028. unsigned long nr_segs, loff_t pos)
  1029. {
  1030. struct fuse_copy_state cs;
  1031. struct file *file = iocb->ki_filp;
  1032. struct fuse_conn *fc = fuse_get_conn(file);
  1033. if (!fc)
  1034. return -EPERM;
  1035. fuse_copy_init(&cs, fc, 1, iov, nr_segs);
  1036. return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
  1037. }
  1038. static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
  1039. struct pipe_buffer *buf)
  1040. {
  1041. return 1;
  1042. }
  1043. static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
  1044. .can_merge = 0,
  1045. .map = generic_pipe_buf_map,
  1046. .unmap = generic_pipe_buf_unmap,
  1047. .confirm = generic_pipe_buf_confirm,
  1048. .release = generic_pipe_buf_release,
  1049. .steal = fuse_dev_pipe_buf_steal,
  1050. .get = generic_pipe_buf_get,
  1051. };
  1052. static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
  1053. struct pipe_inode_info *pipe,
  1054. size_t len, unsigned int flags)
  1055. {
  1056. int ret;
  1057. int page_nr = 0;
  1058. int do_wakeup = 0;
  1059. struct pipe_buffer *bufs;
  1060. struct fuse_copy_state cs;
  1061. struct fuse_conn *fc = fuse_get_conn(in);
  1062. if (!fc)
  1063. return -EPERM;
  1064. bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
  1065. if (!bufs)
  1066. return -ENOMEM;
  1067. fuse_copy_init(&cs, fc, 1, NULL, 0);
  1068. cs.pipebufs = bufs;
  1069. cs.pipe = pipe;
  1070. ret = fuse_dev_do_read(fc, in, &cs, len);
  1071. if (ret < 0)
  1072. goto out;
  1073. ret = 0;
  1074. pipe_lock(pipe);
  1075. if (!pipe->readers) {
  1076. send_sig(SIGPIPE, current, 0);
  1077. if (!ret)
  1078. ret = -EPIPE;
  1079. goto out_unlock;
  1080. }
  1081. if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
  1082. ret = -EIO;
  1083. goto out_unlock;
  1084. }
  1085. while (page_nr < cs.nr_segs) {
  1086. int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
  1087. struct pipe_buffer *buf = pipe->bufs + newbuf;
  1088. buf->page = bufs[page_nr].page;
  1089. buf->offset = bufs[page_nr].offset;
  1090. buf->len = bufs[page_nr].len;
  1091. buf->ops = &fuse_dev_pipe_buf_ops;
  1092. pipe->nrbufs++;
  1093. page_nr++;
  1094. ret += buf->len;
  1095. if (pipe->inode)
  1096. do_wakeup = 1;
  1097. }
  1098. out_unlock:
  1099. pipe_unlock(pipe);
  1100. if (do_wakeup) {
  1101. smp_mb();
  1102. if (waitqueue_active(&pipe->wait))
  1103. wake_up_interruptible(&pipe->wait);
  1104. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  1105. }
  1106. out:
  1107. for (; page_nr < cs.nr_segs; page_nr++)
  1108. page_cache_release(bufs[page_nr].page);
  1109. kfree(bufs);
  1110. return ret;
  1111. }
  1112. static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
  1113. struct fuse_copy_state *cs)
  1114. {
  1115. struct fuse_notify_poll_wakeup_out outarg;
  1116. int err = -EINVAL;
  1117. if (size != sizeof(outarg))
  1118. goto err;
  1119. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1120. if (err)
  1121. goto err;
  1122. fuse_copy_finish(cs);
  1123. return fuse_notify_poll_wakeup(fc, &outarg);
  1124. err:
  1125. fuse_copy_finish(cs);
  1126. return err;
  1127. }
  1128. static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
  1129. struct fuse_copy_state *cs)
  1130. {
  1131. struct fuse_notify_inval_inode_out outarg;
  1132. int err = -EINVAL;
  1133. if (size != sizeof(outarg))
  1134. goto err;
  1135. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1136. if (err)
  1137. goto err;
  1138. fuse_copy_finish(cs);
  1139. down_read(&fc->killsb);
  1140. err = -ENOENT;
  1141. if (fc->sb) {
  1142. err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
  1143. outarg.off, outarg.len);
  1144. }
  1145. up_read(&fc->killsb);
  1146. return err;
  1147. err:
  1148. fuse_copy_finish(cs);
  1149. return err;
  1150. }
  1151. static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
  1152. struct fuse_copy_state *cs)
  1153. {
  1154. struct fuse_notify_inval_entry_out outarg;
  1155. int err = -ENOMEM;
  1156. char *buf;
  1157. struct qstr name;
  1158. buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
  1159. if (!buf)
  1160. goto err;
  1161. err = -EINVAL;
  1162. if (size < sizeof(outarg))
  1163. goto err;
  1164. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1165. if (err)
  1166. goto err;
  1167. err = -ENAMETOOLONG;
  1168. if (outarg.namelen > FUSE_NAME_MAX)
  1169. goto err;
  1170. err = -EINVAL;
  1171. if (size != sizeof(outarg) + outarg.namelen + 1)
  1172. goto err;
  1173. name.name = buf;
  1174. name.len = outarg.namelen;
  1175. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1176. if (err)
  1177. goto err;
  1178. fuse_copy_finish(cs);
  1179. buf[outarg.namelen] = 0;
  1180. name.hash = full_name_hash(name.name, name.len);
  1181. down_read(&fc->killsb);
  1182. err = -ENOENT;
  1183. if (fc->sb)
  1184. err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
  1185. up_read(&fc->killsb);
  1186. kfree(buf);
  1187. return err;
  1188. err:
  1189. kfree(buf);
  1190. fuse_copy_finish(cs);
  1191. return err;
  1192. }
  1193. static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
  1194. struct fuse_copy_state *cs)
  1195. {
  1196. struct fuse_notify_delete_out outarg;
  1197. int err = -ENOMEM;
  1198. char *buf;
  1199. struct qstr name;
  1200. buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
  1201. if (!buf)
  1202. goto err;
  1203. err = -EINVAL;
  1204. if (size < sizeof(outarg))
  1205. goto err;
  1206. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1207. if (err)
  1208. goto err;
  1209. err = -ENAMETOOLONG;
  1210. if (outarg.namelen > FUSE_NAME_MAX)
  1211. goto err;
  1212. err = -EINVAL;
  1213. if (size != sizeof(outarg) + outarg.namelen + 1)
  1214. goto err;
  1215. name.name = buf;
  1216. name.len = outarg.namelen;
  1217. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  1218. if (err)
  1219. goto err;
  1220. fuse_copy_finish(cs);
  1221. buf[outarg.namelen] = 0;
  1222. name.hash = full_name_hash(name.name, name.len);
  1223. down_read(&fc->killsb);
  1224. err = -ENOENT;
  1225. if (fc->sb)
  1226. err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
  1227. outarg.child, &name);
  1228. up_read(&fc->killsb);
  1229. kfree(buf);
  1230. return err;
  1231. err:
  1232. kfree(buf);
  1233. fuse_copy_finish(cs);
  1234. return err;
  1235. }
  1236. static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
  1237. struct fuse_copy_state *cs)
  1238. {
  1239. struct fuse_notify_store_out outarg;
  1240. struct inode *inode;
  1241. struct address_space *mapping;
  1242. u64 nodeid;
  1243. int err;
  1244. pgoff_t index;
  1245. unsigned int offset;
  1246. unsigned int num;
  1247. loff_t file_size;
  1248. loff_t end;
  1249. err = -EINVAL;
  1250. if (size < sizeof(outarg))
  1251. goto out_finish;
  1252. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  1253. if (err)
  1254. goto out_finish;
  1255. err = -EINVAL;
  1256. if (size - sizeof(outarg) != outarg.size)
  1257. goto out_finish;
  1258. nodeid = outarg.nodeid;
  1259. down_read(&fc->killsb);
  1260. err = -ENOENT;
  1261. if (!fc->sb)
  1262. goto out_up_killsb;
  1263. inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
  1264. if (!inode)
  1265. goto out_up_killsb;
  1266. mapping = inode->i_mapping;
  1267. index = outarg.offset >> PAGE_CACHE_SHIFT;
  1268. offset = outarg.offset & ~PAGE_CACHE_MASK;
  1269. file_size = i_size_read(inode);
  1270. end = outarg.offset + outarg.size;
  1271. if (end > file_size) {
  1272. file_size = end;
  1273. fuse_write_update_size(inode, file_size);
  1274. }
  1275. num = outarg.size;
  1276. while (num) {
  1277. struct page *page;
  1278. unsigned int this_num;
  1279. err = -ENOMEM;
  1280. page = find_or_create_page(mapping, index,
  1281. mapping_gfp_mask(mapping));
  1282. if (!page)
  1283. goto out_iput;
  1284. this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
  1285. err = fuse_copy_page(cs, &page, offset, this_num, 0);
  1286. if (!err && offset == 0 && (num != 0 || file_size == end))
  1287. SetPageUptodate(page);
  1288. unlock_page(page);
  1289. page_cache_release(page);
  1290. if (err)
  1291. goto out_iput;
  1292. num -= this_num;
  1293. offset = 0;
  1294. index++;
  1295. }
  1296. err = 0;
  1297. out_iput:
  1298. iput(inode);
  1299. out_up_killsb:
  1300. up_read(&fc->killsb);
  1301. out_finish:
  1302. fuse_copy_finish(cs);
  1303. return err;
  1304. }
  1305. static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
  1306. {
  1307. release_pages(req->pages, req->num_pages, 0);
  1308. }
  1309. static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
  1310. struct fuse_notify_retrieve_out *outarg)
  1311. {
  1312. int err;
  1313. struct address_space *mapping = inode->i_mapping;
  1314. struct fuse_req *req;
  1315. pgoff_t index;
  1316. loff_t file_size;
  1317. unsigned int num;
  1318. unsigned int offset;
  1319. size_t total_len = 0;
  1320. req = fuse_get_req(fc);
  1321. if (IS_ERR(req))
  1322. return PTR_ERR(req);
  1323. offset = outarg->offset & ~PAGE_CACHE_MASK;
  1324. req->in.h.opcode = FUSE_NOTIFY_REPLY;
  1325. req->in.h.nodeid = outarg->nodeid;
  1326. req->in.numargs = 2;
  1327. req->in.argpages = 1;
  1328. req->page_offset = offset;
  1329. req->end = fuse_retrieve_end;
  1330. index = outarg->offset >> PAGE_CACHE_SHIFT;
  1331. file_size = i_size_read(inode);
  1332. num = outarg->size;
  1333. if (outarg->offset > file_size)
  1334. num = 0;
  1335. else if (outarg->offset + num > file_size)
  1336. num = file_size - outarg->offset;
  1337. while (num && req->num_pages < FUSE_MAX_PAGES_PER_REQ) {
  1338. struct page *page;
  1339. unsigned int this_num;
  1340. page = find_get_page(mapping, index);
  1341. if (!page)
  1342. break;
  1343. this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
  1344. req->pages[req->num_pages] = page;
  1345. req->num_pages++;
  1346. offset = 0;
  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. }