dev.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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. MODULE_ALIAS_MISCDEV(FUSE_MINOR);
  18. static struct kmem_cache *fuse_req_cachep;
  19. static struct fuse_conn *fuse_get_conn(struct file *file)
  20. {
  21. /*
  22. * Lockless access is OK, because file->private data is set
  23. * once during mount and is valid until the file is released.
  24. */
  25. return file->private_data;
  26. }
  27. static void fuse_request_init(struct fuse_req *req)
  28. {
  29. memset(req, 0, sizeof(*req));
  30. INIT_LIST_HEAD(&req->list);
  31. INIT_LIST_HEAD(&req->intr_entry);
  32. init_waitqueue_head(&req->waitq);
  33. atomic_set(&req->count, 1);
  34. }
  35. struct fuse_req *fuse_request_alloc(void)
  36. {
  37. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
  38. if (req)
  39. fuse_request_init(req);
  40. return req;
  41. }
  42. EXPORT_SYMBOL_GPL(fuse_request_alloc);
  43. struct fuse_req *fuse_request_alloc_nofs(void)
  44. {
  45. struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
  46. if (req)
  47. fuse_request_init(req);
  48. return req;
  49. }
  50. void fuse_request_free(struct fuse_req *req)
  51. {
  52. kmem_cache_free(fuse_req_cachep, req);
  53. }
  54. static void block_sigs(sigset_t *oldset)
  55. {
  56. sigset_t mask;
  57. siginitsetinv(&mask, sigmask(SIGKILL));
  58. sigprocmask(SIG_BLOCK, &mask, oldset);
  59. }
  60. static void restore_sigs(sigset_t *oldset)
  61. {
  62. sigprocmask(SIG_SETMASK, oldset, NULL);
  63. }
  64. static void __fuse_get_request(struct fuse_req *req)
  65. {
  66. atomic_inc(&req->count);
  67. }
  68. /* Must be called with > 1 refcount */
  69. static void __fuse_put_request(struct fuse_req *req)
  70. {
  71. BUG_ON(atomic_read(&req->count) < 2);
  72. atomic_dec(&req->count);
  73. }
  74. static void fuse_req_init_context(struct fuse_req *req)
  75. {
  76. req->in.h.uid = current_fsuid();
  77. req->in.h.gid = current_fsgid();
  78. req->in.h.pid = current->pid;
  79. }
  80. struct fuse_req *fuse_get_req(struct fuse_conn *fc)
  81. {
  82. struct fuse_req *req;
  83. sigset_t oldset;
  84. int intr;
  85. int err;
  86. atomic_inc(&fc->num_waiting);
  87. block_sigs(&oldset);
  88. intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
  89. restore_sigs(&oldset);
  90. err = -EINTR;
  91. if (intr)
  92. goto out;
  93. err = -ENOTCONN;
  94. if (!fc->connected)
  95. goto out;
  96. req = fuse_request_alloc();
  97. err = -ENOMEM;
  98. if (!req)
  99. goto out;
  100. fuse_req_init_context(req);
  101. req->waiting = 1;
  102. return req;
  103. out:
  104. atomic_dec(&fc->num_waiting);
  105. return ERR_PTR(err);
  106. }
  107. EXPORT_SYMBOL_GPL(fuse_get_req);
  108. /*
  109. * Return request in fuse_file->reserved_req. However that may
  110. * currently be in use. If that is the case, wait for it to become
  111. * available.
  112. */
  113. static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
  114. struct file *file)
  115. {
  116. struct fuse_req *req = NULL;
  117. struct fuse_file *ff = file->private_data;
  118. do {
  119. wait_event(fc->reserved_req_waitq, ff->reserved_req);
  120. spin_lock(&fc->lock);
  121. if (ff->reserved_req) {
  122. req = ff->reserved_req;
  123. ff->reserved_req = NULL;
  124. get_file(file);
  125. req->stolen_file = file;
  126. }
  127. spin_unlock(&fc->lock);
  128. } while (!req);
  129. return req;
  130. }
  131. /*
  132. * Put stolen request back into fuse_file->reserved_req
  133. */
  134. static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
  135. {
  136. struct file *file = req->stolen_file;
  137. struct fuse_file *ff = file->private_data;
  138. spin_lock(&fc->lock);
  139. fuse_request_init(req);
  140. BUG_ON(ff->reserved_req);
  141. ff->reserved_req = req;
  142. wake_up_all(&fc->reserved_req_waitq);
  143. spin_unlock(&fc->lock);
  144. fput(file);
  145. }
  146. /*
  147. * Gets a requests for a file operation, always succeeds
  148. *
  149. * This is used for sending the FLUSH request, which must get to
  150. * userspace, due to POSIX locks which may need to be unlocked.
  151. *
  152. * If allocation fails due to OOM, use the reserved request in
  153. * fuse_file.
  154. *
  155. * This is very unlikely to deadlock accidentally, since the
  156. * filesystem should not have it's own file open. If deadlock is
  157. * intentional, it can still be broken by "aborting" the filesystem.
  158. */
  159. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
  160. {
  161. struct fuse_req *req;
  162. atomic_inc(&fc->num_waiting);
  163. wait_event(fc->blocked_waitq, !fc->blocked);
  164. req = fuse_request_alloc();
  165. if (!req)
  166. req = get_reserved_req(fc, file);
  167. fuse_req_init_context(req);
  168. req->waiting = 1;
  169. return req;
  170. }
  171. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
  172. {
  173. if (atomic_dec_and_test(&req->count)) {
  174. if (req->waiting)
  175. atomic_dec(&fc->num_waiting);
  176. if (req->stolen_file)
  177. put_reserved_req(fc, req);
  178. else
  179. fuse_request_free(req);
  180. }
  181. }
  182. EXPORT_SYMBOL_GPL(fuse_put_request);
  183. static unsigned len_args(unsigned numargs, struct fuse_arg *args)
  184. {
  185. unsigned nbytes = 0;
  186. unsigned i;
  187. for (i = 0; i < numargs; i++)
  188. nbytes += args[i].size;
  189. return nbytes;
  190. }
  191. static u64 fuse_get_unique(struct fuse_conn *fc)
  192. {
  193. fc->reqctr++;
  194. /* zero is special */
  195. if (fc->reqctr == 0)
  196. fc->reqctr = 1;
  197. return fc->reqctr;
  198. }
  199. static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
  200. {
  201. req->in.h.unique = fuse_get_unique(fc);
  202. req->in.h.len = sizeof(struct fuse_in_header) +
  203. len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
  204. list_add_tail(&req->list, &fc->pending);
  205. req->state = FUSE_REQ_PENDING;
  206. if (!req->waiting) {
  207. req->waiting = 1;
  208. atomic_inc(&fc->num_waiting);
  209. }
  210. wake_up(&fc->waitq);
  211. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  212. }
  213. static void flush_bg_queue(struct fuse_conn *fc)
  214. {
  215. while (fc->active_background < fc->max_background &&
  216. !list_empty(&fc->bg_queue)) {
  217. struct fuse_req *req;
  218. req = list_entry(fc->bg_queue.next, struct fuse_req, list);
  219. list_del(&req->list);
  220. fc->active_background++;
  221. queue_request(fc, req);
  222. }
  223. }
  224. /*
  225. * This function is called when a request is finished. Either a reply
  226. * has arrived or it was aborted (and not yet sent) or some error
  227. * occurred during communication with userspace, or the device file
  228. * was closed. The requester thread is woken up (if still waiting),
  229. * the 'end' callback is called if given, else the reference to the
  230. * request is released
  231. *
  232. * Called with fc->lock, unlocks it
  233. */
  234. static void request_end(struct fuse_conn *fc, struct fuse_req *req)
  235. __releases(&fc->lock)
  236. {
  237. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  238. req->end = NULL;
  239. list_del(&req->list);
  240. list_del(&req->intr_entry);
  241. req->state = FUSE_REQ_FINISHED;
  242. if (req->background) {
  243. if (fc->num_background == fc->max_background) {
  244. fc->blocked = 0;
  245. wake_up_all(&fc->blocked_waitq);
  246. }
  247. if (fc->num_background == fc->congestion_threshold &&
  248. fc->connected && fc->bdi_initialized) {
  249. clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  250. clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  251. }
  252. fc->num_background--;
  253. fc->active_background--;
  254. flush_bg_queue(fc);
  255. }
  256. spin_unlock(&fc->lock);
  257. wake_up(&req->waitq);
  258. if (end)
  259. end(fc, req);
  260. fuse_put_request(fc, req);
  261. }
  262. static void wait_answer_interruptible(struct fuse_conn *fc,
  263. struct fuse_req *req)
  264. __releases(&fc->lock)
  265. __acquires(&fc->lock)
  266. {
  267. if (signal_pending(current))
  268. return;
  269. spin_unlock(&fc->lock);
  270. wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
  271. spin_lock(&fc->lock);
  272. }
  273. static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
  274. {
  275. list_add_tail(&req->intr_entry, &fc->interrupts);
  276. wake_up(&fc->waitq);
  277. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  278. }
  279. static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
  280. __releases(&fc->lock)
  281. __acquires(&fc->lock)
  282. {
  283. if (!fc->no_interrupt) {
  284. /* Any signal may interrupt this */
  285. wait_answer_interruptible(fc, req);
  286. if (req->aborted)
  287. goto aborted;
  288. if (req->state == FUSE_REQ_FINISHED)
  289. return;
  290. req->interrupted = 1;
  291. if (req->state == FUSE_REQ_SENT)
  292. queue_interrupt(fc, req);
  293. }
  294. if (!req->force) {
  295. sigset_t oldset;
  296. /* Only fatal signals may interrupt this */
  297. block_sigs(&oldset);
  298. wait_answer_interruptible(fc, req);
  299. restore_sigs(&oldset);
  300. if (req->aborted)
  301. goto aborted;
  302. if (req->state == FUSE_REQ_FINISHED)
  303. return;
  304. /* Request is not yet in userspace, bail out */
  305. if (req->state == FUSE_REQ_PENDING) {
  306. list_del(&req->list);
  307. __fuse_put_request(req);
  308. req->out.h.error = -EINTR;
  309. return;
  310. }
  311. }
  312. /*
  313. * Either request is already in userspace, or it was forced.
  314. * Wait it out.
  315. */
  316. spin_unlock(&fc->lock);
  317. wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
  318. spin_lock(&fc->lock);
  319. if (!req->aborted)
  320. return;
  321. aborted:
  322. BUG_ON(req->state != FUSE_REQ_FINISHED);
  323. if (req->locked) {
  324. /* This is uninterruptible sleep, because data is
  325. being copied to/from the buffers of req. During
  326. locked state, there mustn't be any filesystem
  327. operation (e.g. page fault), since that could lead
  328. to deadlock */
  329. spin_unlock(&fc->lock);
  330. wait_event(req->waitq, !req->locked);
  331. spin_lock(&fc->lock);
  332. }
  333. }
  334. void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
  335. {
  336. req->isreply = 1;
  337. spin_lock(&fc->lock);
  338. if (!fc->connected)
  339. req->out.h.error = -ENOTCONN;
  340. else if (fc->conn_error)
  341. req->out.h.error = -ECONNREFUSED;
  342. else {
  343. queue_request(fc, req);
  344. /* acquire extra reference, since request is still needed
  345. after request_end() */
  346. __fuse_get_request(req);
  347. request_wait_answer(fc, req);
  348. }
  349. spin_unlock(&fc->lock);
  350. }
  351. EXPORT_SYMBOL_GPL(fuse_request_send);
  352. static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
  353. struct fuse_req *req)
  354. {
  355. req->background = 1;
  356. fc->num_background++;
  357. if (fc->num_background == fc->max_background)
  358. fc->blocked = 1;
  359. if (fc->num_background == fc->congestion_threshold &&
  360. fc->bdi_initialized) {
  361. set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
  362. set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
  363. }
  364. list_add_tail(&req->list, &fc->bg_queue);
  365. flush_bg_queue(fc);
  366. }
  367. static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
  368. {
  369. spin_lock(&fc->lock);
  370. if (fc->connected) {
  371. fuse_request_send_nowait_locked(fc, req);
  372. spin_unlock(&fc->lock);
  373. } else {
  374. req->out.h.error = -ENOTCONN;
  375. request_end(fc, req);
  376. }
  377. }
  378. void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
  379. {
  380. req->isreply = 0;
  381. fuse_request_send_nowait(fc, req);
  382. }
  383. void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
  384. {
  385. req->isreply = 1;
  386. fuse_request_send_nowait(fc, req);
  387. }
  388. EXPORT_SYMBOL_GPL(fuse_request_send_background);
  389. /*
  390. * Called under fc->lock
  391. *
  392. * fc->connected must have been checked previously
  393. */
  394. void fuse_request_send_background_locked(struct fuse_conn *fc,
  395. struct fuse_req *req)
  396. {
  397. req->isreply = 1;
  398. fuse_request_send_nowait_locked(fc, req);
  399. }
  400. /*
  401. * Lock the request. Up to the next unlock_request() there mustn't be
  402. * anything that could cause a page-fault. If the request was already
  403. * aborted bail out.
  404. */
  405. static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
  406. {
  407. int err = 0;
  408. if (req) {
  409. spin_lock(&fc->lock);
  410. if (req->aborted)
  411. err = -ENOENT;
  412. else
  413. req->locked = 1;
  414. spin_unlock(&fc->lock);
  415. }
  416. return err;
  417. }
  418. /*
  419. * Unlock request. If it was aborted during being locked, the
  420. * requester thread is currently waiting for it to be unlocked, so
  421. * wake it up.
  422. */
  423. static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
  424. {
  425. if (req) {
  426. spin_lock(&fc->lock);
  427. req->locked = 0;
  428. if (req->aborted)
  429. wake_up(&req->waitq);
  430. spin_unlock(&fc->lock);
  431. }
  432. }
  433. struct fuse_copy_state {
  434. struct fuse_conn *fc;
  435. int write;
  436. struct fuse_req *req;
  437. const struct iovec *iov;
  438. struct pipe_buffer *pipebufs;
  439. struct pipe_buffer *currbuf;
  440. struct pipe_inode_info *pipe;
  441. unsigned long nr_segs;
  442. unsigned long seglen;
  443. unsigned long addr;
  444. struct page *pg;
  445. void *mapaddr;
  446. void *buf;
  447. unsigned len;
  448. };
  449. static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
  450. int write, struct fuse_req *req,
  451. const struct iovec *iov, unsigned long nr_segs)
  452. {
  453. memset(cs, 0, sizeof(*cs));
  454. cs->fc = fc;
  455. cs->write = write;
  456. cs->req = req;
  457. cs->iov = iov;
  458. cs->nr_segs = nr_segs;
  459. }
  460. /* Unmap and put previous page of userspace buffer */
  461. static void fuse_copy_finish(struct fuse_copy_state *cs)
  462. {
  463. if (cs->currbuf) {
  464. struct pipe_buffer *buf = cs->currbuf;
  465. buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
  466. cs->currbuf = NULL;
  467. cs->mapaddr = NULL;
  468. } else if (cs->mapaddr) {
  469. kunmap_atomic(cs->mapaddr, KM_USER0);
  470. if (cs->write) {
  471. flush_dcache_page(cs->pg);
  472. set_page_dirty_lock(cs->pg);
  473. }
  474. put_page(cs->pg);
  475. cs->mapaddr = NULL;
  476. }
  477. }
  478. /*
  479. * Get another pagefull of userspace buffer, and map it to kernel
  480. * address space, and lock request
  481. */
  482. static int fuse_copy_fill(struct fuse_copy_state *cs)
  483. {
  484. unsigned long offset;
  485. int err;
  486. unlock_request(cs->fc, cs->req);
  487. fuse_copy_finish(cs);
  488. if (cs->pipebufs) {
  489. struct pipe_buffer *buf = cs->pipebufs;
  490. err = buf->ops->confirm(cs->pipe, buf);
  491. if (err)
  492. return err;
  493. BUG_ON(!cs->nr_segs);
  494. cs->currbuf = buf;
  495. cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
  496. cs->len = buf->len;
  497. cs->buf = cs->mapaddr + buf->offset;
  498. cs->pipebufs++;
  499. cs->nr_segs--;
  500. } else {
  501. if (!cs->seglen) {
  502. BUG_ON(!cs->nr_segs);
  503. cs->seglen = cs->iov[0].iov_len;
  504. cs->addr = (unsigned long) cs->iov[0].iov_base;
  505. cs->iov++;
  506. cs->nr_segs--;
  507. }
  508. err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
  509. if (err < 0)
  510. return err;
  511. BUG_ON(err != 1);
  512. offset = cs->addr % PAGE_SIZE;
  513. cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
  514. cs->buf = cs->mapaddr + offset;
  515. cs->len = min(PAGE_SIZE - offset, cs->seglen);
  516. cs->seglen -= cs->len;
  517. cs->addr += cs->len;
  518. }
  519. return lock_request(cs->fc, cs->req);
  520. }
  521. /* Do as much copy to/from userspace buffer as we can */
  522. static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
  523. {
  524. unsigned ncpy = min(*size, cs->len);
  525. if (val) {
  526. if (cs->write)
  527. memcpy(cs->buf, *val, ncpy);
  528. else
  529. memcpy(*val, cs->buf, ncpy);
  530. *val += ncpy;
  531. }
  532. *size -= ncpy;
  533. cs->len -= ncpy;
  534. cs->buf += ncpy;
  535. return ncpy;
  536. }
  537. /*
  538. * Copy a page in the request to/from the userspace buffer. Must be
  539. * done atomically
  540. */
  541. static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
  542. unsigned offset, unsigned count, int zeroing)
  543. {
  544. if (page && zeroing && count < PAGE_SIZE) {
  545. void *mapaddr = kmap_atomic(page, KM_USER1);
  546. memset(mapaddr, 0, PAGE_SIZE);
  547. kunmap_atomic(mapaddr, KM_USER1);
  548. }
  549. while (count) {
  550. if (!cs->len) {
  551. int err = fuse_copy_fill(cs);
  552. if (err)
  553. return err;
  554. }
  555. if (page) {
  556. void *mapaddr = kmap_atomic(page, KM_USER1);
  557. void *buf = mapaddr + offset;
  558. offset += fuse_copy_do(cs, &buf, &count);
  559. kunmap_atomic(mapaddr, KM_USER1);
  560. } else
  561. offset += fuse_copy_do(cs, NULL, &count);
  562. }
  563. if (page && !cs->write)
  564. flush_dcache_page(page);
  565. return 0;
  566. }
  567. /* Copy pages in the request to/from userspace buffer */
  568. static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
  569. int zeroing)
  570. {
  571. unsigned i;
  572. struct fuse_req *req = cs->req;
  573. unsigned offset = req->page_offset;
  574. unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
  575. for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
  576. struct page *page = req->pages[i];
  577. int err = fuse_copy_page(cs, page, offset, count, zeroing);
  578. if (err)
  579. return err;
  580. nbytes -= count;
  581. count = min(nbytes, (unsigned) PAGE_SIZE);
  582. offset = 0;
  583. }
  584. return 0;
  585. }
  586. /* Copy a single argument in the request to/from userspace buffer */
  587. static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
  588. {
  589. while (size) {
  590. if (!cs->len) {
  591. int err = fuse_copy_fill(cs);
  592. if (err)
  593. return err;
  594. }
  595. fuse_copy_do(cs, &val, &size);
  596. }
  597. return 0;
  598. }
  599. /* Copy request arguments to/from userspace buffer */
  600. static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
  601. unsigned argpages, struct fuse_arg *args,
  602. int zeroing)
  603. {
  604. int err = 0;
  605. unsigned i;
  606. for (i = 0; !err && i < numargs; i++) {
  607. struct fuse_arg *arg = &args[i];
  608. if (i == numargs - 1 && argpages)
  609. err = fuse_copy_pages(cs, arg->size, zeroing);
  610. else
  611. err = fuse_copy_one(cs, arg->value, arg->size);
  612. }
  613. return err;
  614. }
  615. static int request_pending(struct fuse_conn *fc)
  616. {
  617. return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
  618. }
  619. /* Wait until a request is available on the pending list */
  620. static void request_wait(struct fuse_conn *fc)
  621. __releases(&fc->lock)
  622. __acquires(&fc->lock)
  623. {
  624. DECLARE_WAITQUEUE(wait, current);
  625. add_wait_queue_exclusive(&fc->waitq, &wait);
  626. while (fc->connected && !request_pending(fc)) {
  627. set_current_state(TASK_INTERRUPTIBLE);
  628. if (signal_pending(current))
  629. break;
  630. spin_unlock(&fc->lock);
  631. schedule();
  632. spin_lock(&fc->lock);
  633. }
  634. set_current_state(TASK_RUNNING);
  635. remove_wait_queue(&fc->waitq, &wait);
  636. }
  637. /*
  638. * Transfer an interrupt request to userspace
  639. *
  640. * Unlike other requests this is assembled on demand, without a need
  641. * to allocate a separate fuse_req structure.
  642. *
  643. * Called with fc->lock held, releases it
  644. */
  645. static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
  646. const struct iovec *iov, unsigned long nr_segs)
  647. __releases(&fc->lock)
  648. {
  649. struct fuse_copy_state cs;
  650. struct fuse_in_header ih;
  651. struct fuse_interrupt_in arg;
  652. unsigned reqsize = sizeof(ih) + sizeof(arg);
  653. int err;
  654. list_del_init(&req->intr_entry);
  655. req->intr_unique = fuse_get_unique(fc);
  656. memset(&ih, 0, sizeof(ih));
  657. memset(&arg, 0, sizeof(arg));
  658. ih.len = reqsize;
  659. ih.opcode = FUSE_INTERRUPT;
  660. ih.unique = req->intr_unique;
  661. arg.unique = req->in.h.unique;
  662. spin_unlock(&fc->lock);
  663. if (iov_length(iov, nr_segs) < reqsize)
  664. return -EINVAL;
  665. fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
  666. err = fuse_copy_one(&cs, &ih, sizeof(ih));
  667. if (!err)
  668. err = fuse_copy_one(&cs, &arg, sizeof(arg));
  669. fuse_copy_finish(&cs);
  670. return err ? err : reqsize;
  671. }
  672. /*
  673. * Read a single request into the userspace filesystem's buffer. This
  674. * function waits until a request is available, then removes it from
  675. * the pending list and copies request data to userspace buffer. If
  676. * no reply is needed (FORGET) or request has been aborted or there
  677. * was an error during the copying then it's finished by calling
  678. * request_end(). Otherwise add it to the processing list, and set
  679. * the 'sent' flag.
  680. */
  681. static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
  682. unsigned long nr_segs, loff_t pos)
  683. {
  684. int err;
  685. struct fuse_req *req;
  686. struct fuse_in *in;
  687. struct fuse_copy_state cs;
  688. unsigned reqsize;
  689. struct file *file = iocb->ki_filp;
  690. struct fuse_conn *fc = fuse_get_conn(file);
  691. if (!fc)
  692. return -EPERM;
  693. restart:
  694. spin_lock(&fc->lock);
  695. err = -EAGAIN;
  696. if ((file->f_flags & O_NONBLOCK) && fc->connected &&
  697. !request_pending(fc))
  698. goto err_unlock;
  699. request_wait(fc);
  700. err = -ENODEV;
  701. if (!fc->connected)
  702. goto err_unlock;
  703. err = -ERESTARTSYS;
  704. if (!request_pending(fc))
  705. goto err_unlock;
  706. if (!list_empty(&fc->interrupts)) {
  707. req = list_entry(fc->interrupts.next, struct fuse_req,
  708. intr_entry);
  709. return fuse_read_interrupt(fc, req, iov, nr_segs);
  710. }
  711. req = list_entry(fc->pending.next, struct fuse_req, list);
  712. req->state = FUSE_REQ_READING;
  713. list_move(&req->list, &fc->io);
  714. in = &req->in;
  715. reqsize = in->h.len;
  716. /* If request is too large, reply with an error and restart the read */
  717. if (iov_length(iov, nr_segs) < reqsize) {
  718. req->out.h.error = -EIO;
  719. /* SETXATTR is special, since it may contain too large data */
  720. if (in->h.opcode == FUSE_SETXATTR)
  721. req->out.h.error = -E2BIG;
  722. request_end(fc, req);
  723. goto restart;
  724. }
  725. spin_unlock(&fc->lock);
  726. fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
  727. err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
  728. if (!err)
  729. err = fuse_copy_args(&cs, in->numargs, in->argpages,
  730. (struct fuse_arg *) in->args, 0);
  731. fuse_copy_finish(&cs);
  732. spin_lock(&fc->lock);
  733. req->locked = 0;
  734. if (req->aborted) {
  735. request_end(fc, req);
  736. return -ENODEV;
  737. }
  738. if (err) {
  739. req->out.h.error = -EIO;
  740. request_end(fc, req);
  741. return err;
  742. }
  743. if (!req->isreply)
  744. request_end(fc, req);
  745. else {
  746. req->state = FUSE_REQ_SENT;
  747. list_move_tail(&req->list, &fc->processing);
  748. if (req->interrupted)
  749. queue_interrupt(fc, req);
  750. spin_unlock(&fc->lock);
  751. }
  752. return reqsize;
  753. err_unlock:
  754. spin_unlock(&fc->lock);
  755. return err;
  756. }
  757. static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
  758. struct fuse_copy_state *cs)
  759. {
  760. struct fuse_notify_poll_wakeup_out outarg;
  761. int err = -EINVAL;
  762. if (size != sizeof(outarg))
  763. goto err;
  764. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  765. if (err)
  766. goto err;
  767. fuse_copy_finish(cs);
  768. return fuse_notify_poll_wakeup(fc, &outarg);
  769. err:
  770. fuse_copy_finish(cs);
  771. return err;
  772. }
  773. static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
  774. struct fuse_copy_state *cs)
  775. {
  776. struct fuse_notify_inval_inode_out outarg;
  777. int err = -EINVAL;
  778. if (size != sizeof(outarg))
  779. goto err;
  780. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  781. if (err)
  782. goto err;
  783. fuse_copy_finish(cs);
  784. down_read(&fc->killsb);
  785. err = -ENOENT;
  786. if (fc->sb) {
  787. err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
  788. outarg.off, outarg.len);
  789. }
  790. up_read(&fc->killsb);
  791. return err;
  792. err:
  793. fuse_copy_finish(cs);
  794. return err;
  795. }
  796. static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
  797. struct fuse_copy_state *cs)
  798. {
  799. struct fuse_notify_inval_entry_out outarg;
  800. int err = -ENOMEM;
  801. char *buf;
  802. struct qstr name;
  803. buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
  804. if (!buf)
  805. goto err;
  806. err = -EINVAL;
  807. if (size < sizeof(outarg))
  808. goto err;
  809. err = fuse_copy_one(cs, &outarg, sizeof(outarg));
  810. if (err)
  811. goto err;
  812. err = -ENAMETOOLONG;
  813. if (outarg.namelen > FUSE_NAME_MAX)
  814. goto err;
  815. name.name = buf;
  816. name.len = outarg.namelen;
  817. err = fuse_copy_one(cs, buf, outarg.namelen + 1);
  818. if (err)
  819. goto err;
  820. fuse_copy_finish(cs);
  821. buf[outarg.namelen] = 0;
  822. name.hash = full_name_hash(name.name, name.len);
  823. down_read(&fc->killsb);
  824. err = -ENOENT;
  825. if (fc->sb)
  826. err = fuse_reverse_inval_entry(fc->sb, outarg.parent, &name);
  827. up_read(&fc->killsb);
  828. kfree(buf);
  829. return err;
  830. err:
  831. kfree(buf);
  832. fuse_copy_finish(cs);
  833. return err;
  834. }
  835. static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
  836. unsigned int size, struct fuse_copy_state *cs)
  837. {
  838. switch (code) {
  839. case FUSE_NOTIFY_POLL:
  840. return fuse_notify_poll(fc, size, cs);
  841. case FUSE_NOTIFY_INVAL_INODE:
  842. return fuse_notify_inval_inode(fc, size, cs);
  843. case FUSE_NOTIFY_INVAL_ENTRY:
  844. return fuse_notify_inval_entry(fc, size, cs);
  845. default:
  846. fuse_copy_finish(cs);
  847. return -EINVAL;
  848. }
  849. }
  850. /* Look up request on processing list by unique ID */
  851. static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
  852. {
  853. struct list_head *entry;
  854. list_for_each(entry, &fc->processing) {
  855. struct fuse_req *req;
  856. req = list_entry(entry, struct fuse_req, list);
  857. if (req->in.h.unique == unique || req->intr_unique == unique)
  858. return req;
  859. }
  860. return NULL;
  861. }
  862. static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
  863. unsigned nbytes)
  864. {
  865. unsigned reqsize = sizeof(struct fuse_out_header);
  866. if (out->h.error)
  867. return nbytes != reqsize ? -EINVAL : 0;
  868. reqsize += len_args(out->numargs, out->args);
  869. if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
  870. return -EINVAL;
  871. else if (reqsize > nbytes) {
  872. struct fuse_arg *lastarg = &out->args[out->numargs-1];
  873. unsigned diffsize = reqsize - nbytes;
  874. if (diffsize > lastarg->size)
  875. return -EINVAL;
  876. lastarg->size -= diffsize;
  877. }
  878. return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
  879. out->page_zeroing);
  880. }
  881. /*
  882. * Write a single reply to a request. First the header is copied from
  883. * the write buffer. The request is then searched on the processing
  884. * list by the unique ID found in the header. If found, then remove
  885. * it from the list and copy the rest of the buffer to the request.
  886. * The request is finished by calling request_end()
  887. */
  888. static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
  889. struct fuse_copy_state *cs, size_t nbytes)
  890. {
  891. int err;
  892. struct fuse_req *req;
  893. struct fuse_out_header oh;
  894. if (nbytes < sizeof(struct fuse_out_header))
  895. return -EINVAL;
  896. err = fuse_copy_one(cs, &oh, sizeof(oh));
  897. if (err)
  898. goto err_finish;
  899. err = -EINVAL;
  900. if (oh.len != nbytes)
  901. goto err_finish;
  902. /*
  903. * Zero oh.unique indicates unsolicited notification message
  904. * and error contains notification code.
  905. */
  906. if (!oh.unique) {
  907. err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
  908. return err ? err : nbytes;
  909. }
  910. err = -EINVAL;
  911. if (oh.error <= -1000 || oh.error > 0)
  912. goto err_finish;
  913. spin_lock(&fc->lock);
  914. err = -ENOENT;
  915. if (!fc->connected)
  916. goto err_unlock;
  917. req = request_find(fc, oh.unique);
  918. if (!req)
  919. goto err_unlock;
  920. if (req->aborted) {
  921. spin_unlock(&fc->lock);
  922. fuse_copy_finish(cs);
  923. spin_lock(&fc->lock);
  924. request_end(fc, req);
  925. return -ENOENT;
  926. }
  927. /* Is it an interrupt reply? */
  928. if (req->intr_unique == oh.unique) {
  929. err = -EINVAL;
  930. if (nbytes != sizeof(struct fuse_out_header))
  931. goto err_unlock;
  932. if (oh.error == -ENOSYS)
  933. fc->no_interrupt = 1;
  934. else if (oh.error == -EAGAIN)
  935. queue_interrupt(fc, req);
  936. spin_unlock(&fc->lock);
  937. fuse_copy_finish(cs);
  938. return nbytes;
  939. }
  940. req->state = FUSE_REQ_WRITING;
  941. list_move(&req->list, &fc->io);
  942. req->out.h = oh;
  943. req->locked = 1;
  944. cs->req = req;
  945. spin_unlock(&fc->lock);
  946. err = copy_out_args(cs, &req->out, nbytes);
  947. fuse_copy_finish(cs);
  948. spin_lock(&fc->lock);
  949. req->locked = 0;
  950. if (!err) {
  951. if (req->aborted)
  952. err = -ENOENT;
  953. } else if (!req->aborted)
  954. req->out.h.error = -EIO;
  955. request_end(fc, req);
  956. return err ? err : nbytes;
  957. err_unlock:
  958. spin_unlock(&fc->lock);
  959. err_finish:
  960. fuse_copy_finish(cs);
  961. return err;
  962. }
  963. static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
  964. unsigned long nr_segs, loff_t pos)
  965. {
  966. struct fuse_copy_state cs;
  967. struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
  968. if (!fc)
  969. return -EPERM;
  970. fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
  971. return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
  972. }
  973. static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
  974. struct file *out, loff_t *ppos,
  975. size_t len, unsigned int flags)
  976. {
  977. unsigned nbuf;
  978. unsigned idx;
  979. struct pipe_buffer *bufs;
  980. struct fuse_copy_state cs;
  981. struct fuse_conn *fc;
  982. size_t rem;
  983. ssize_t ret;
  984. fc = fuse_get_conn(out);
  985. if (!fc)
  986. return -EPERM;
  987. bufs = kmalloc(pipe->buffers * sizeof (struct pipe_buffer), GFP_KERNEL);
  988. if (!bufs)
  989. return -ENOMEM;
  990. pipe_lock(pipe);
  991. nbuf = 0;
  992. rem = 0;
  993. for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
  994. rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
  995. ret = -EINVAL;
  996. if (rem < len) {
  997. pipe_unlock(pipe);
  998. goto out;
  999. }
  1000. rem = len;
  1001. while (rem) {
  1002. struct pipe_buffer *ibuf;
  1003. struct pipe_buffer *obuf;
  1004. BUG_ON(nbuf >= pipe->buffers);
  1005. BUG_ON(!pipe->nrbufs);
  1006. ibuf = &pipe->bufs[pipe->curbuf];
  1007. obuf = &bufs[nbuf];
  1008. if (rem >= ibuf->len) {
  1009. *obuf = *ibuf;
  1010. ibuf->ops = NULL;
  1011. pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
  1012. pipe->nrbufs--;
  1013. } else {
  1014. ibuf->ops->get(pipe, ibuf);
  1015. *obuf = *ibuf;
  1016. obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
  1017. obuf->len = rem;
  1018. ibuf->offset += obuf->len;
  1019. ibuf->len -= obuf->len;
  1020. }
  1021. nbuf++;
  1022. rem -= obuf->len;
  1023. }
  1024. pipe_unlock(pipe);
  1025. memset(&cs, 0, sizeof(struct fuse_copy_state));
  1026. cs.fc = fc;
  1027. cs.write = 0;
  1028. cs.pipebufs = bufs;
  1029. cs.nr_segs = nbuf;
  1030. cs.pipe = pipe;
  1031. ret = fuse_dev_do_write(fc, &cs, len);
  1032. for (idx = 0; idx < nbuf; idx++) {
  1033. struct pipe_buffer *buf = &bufs[idx];
  1034. buf->ops->release(pipe, buf);
  1035. }
  1036. out:
  1037. kfree(bufs);
  1038. return ret;
  1039. }
  1040. static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
  1041. {
  1042. unsigned mask = POLLOUT | POLLWRNORM;
  1043. struct fuse_conn *fc = fuse_get_conn(file);
  1044. if (!fc)
  1045. return POLLERR;
  1046. poll_wait(file, &fc->waitq, wait);
  1047. spin_lock(&fc->lock);
  1048. if (!fc->connected)
  1049. mask = POLLERR;
  1050. else if (request_pending(fc))
  1051. mask |= POLLIN | POLLRDNORM;
  1052. spin_unlock(&fc->lock);
  1053. return mask;
  1054. }
  1055. /*
  1056. * Abort all requests on the given list (pending or processing)
  1057. *
  1058. * This function releases and reacquires fc->lock
  1059. */
  1060. static void end_requests(struct fuse_conn *fc, struct list_head *head)
  1061. __releases(&fc->lock)
  1062. __acquires(&fc->lock)
  1063. {
  1064. while (!list_empty(head)) {
  1065. struct fuse_req *req;
  1066. req = list_entry(head->next, struct fuse_req, list);
  1067. req->out.h.error = -ECONNABORTED;
  1068. request_end(fc, req);
  1069. spin_lock(&fc->lock);
  1070. }
  1071. }
  1072. /*
  1073. * Abort requests under I/O
  1074. *
  1075. * The requests are set to aborted and finished, and the request
  1076. * waiter is woken up. This will make request_wait_answer() wait
  1077. * until the request is unlocked and then return.
  1078. *
  1079. * If the request is asynchronous, then the end function needs to be
  1080. * called after waiting for the request to be unlocked (if it was
  1081. * locked).
  1082. */
  1083. static void end_io_requests(struct fuse_conn *fc)
  1084. __releases(&fc->lock)
  1085. __acquires(&fc->lock)
  1086. {
  1087. while (!list_empty(&fc->io)) {
  1088. struct fuse_req *req =
  1089. list_entry(fc->io.next, struct fuse_req, list);
  1090. void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
  1091. req->aborted = 1;
  1092. req->out.h.error = -ECONNABORTED;
  1093. req->state = FUSE_REQ_FINISHED;
  1094. list_del_init(&req->list);
  1095. wake_up(&req->waitq);
  1096. if (end) {
  1097. req->end = NULL;
  1098. __fuse_get_request(req);
  1099. spin_unlock(&fc->lock);
  1100. wait_event(req->waitq, !req->locked);
  1101. end(fc, req);
  1102. fuse_put_request(fc, req);
  1103. spin_lock(&fc->lock);
  1104. }
  1105. }
  1106. }
  1107. /*
  1108. * Abort all requests.
  1109. *
  1110. * Emergency exit in case of a malicious or accidental deadlock, or
  1111. * just a hung filesystem.
  1112. *
  1113. * The same effect is usually achievable through killing the
  1114. * filesystem daemon and all users of the filesystem. The exception
  1115. * is the combination of an asynchronous request and the tricky
  1116. * deadlock (see Documentation/filesystems/fuse.txt).
  1117. *
  1118. * During the aborting, progression of requests from the pending and
  1119. * processing lists onto the io list, and progression of new requests
  1120. * onto the pending list is prevented by req->connected being false.
  1121. *
  1122. * Progression of requests under I/O to the processing list is
  1123. * prevented by the req->aborted flag being true for these requests.
  1124. * For this reason requests on the io list must be aborted first.
  1125. */
  1126. void fuse_abort_conn(struct fuse_conn *fc)
  1127. {
  1128. spin_lock(&fc->lock);
  1129. if (fc->connected) {
  1130. fc->connected = 0;
  1131. fc->blocked = 0;
  1132. end_io_requests(fc);
  1133. end_requests(fc, &fc->pending);
  1134. end_requests(fc, &fc->processing);
  1135. wake_up_all(&fc->waitq);
  1136. wake_up_all(&fc->blocked_waitq);
  1137. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  1138. }
  1139. spin_unlock(&fc->lock);
  1140. }
  1141. EXPORT_SYMBOL_GPL(fuse_abort_conn);
  1142. int fuse_dev_release(struct inode *inode, struct file *file)
  1143. {
  1144. struct fuse_conn *fc = fuse_get_conn(file);
  1145. if (fc) {
  1146. spin_lock(&fc->lock);
  1147. fc->connected = 0;
  1148. end_requests(fc, &fc->pending);
  1149. end_requests(fc, &fc->processing);
  1150. spin_unlock(&fc->lock);
  1151. fuse_conn_put(fc);
  1152. }
  1153. return 0;
  1154. }
  1155. EXPORT_SYMBOL_GPL(fuse_dev_release);
  1156. static int fuse_dev_fasync(int fd, struct file *file, int on)
  1157. {
  1158. struct fuse_conn *fc = fuse_get_conn(file);
  1159. if (!fc)
  1160. return -EPERM;
  1161. /* No locking - fasync_helper does its own locking */
  1162. return fasync_helper(fd, file, on, &fc->fasync);
  1163. }
  1164. const struct file_operations fuse_dev_operations = {
  1165. .owner = THIS_MODULE,
  1166. .llseek = no_llseek,
  1167. .read = do_sync_read,
  1168. .aio_read = fuse_dev_read,
  1169. .write = do_sync_write,
  1170. .aio_write = fuse_dev_write,
  1171. .splice_write = fuse_dev_splice_write,
  1172. .poll = fuse_dev_poll,
  1173. .release = fuse_dev_release,
  1174. .fasync = fuse_dev_fasync,
  1175. };
  1176. EXPORT_SYMBOL_GPL(fuse_dev_operations);
  1177. static struct miscdevice fuse_miscdevice = {
  1178. .minor = FUSE_MINOR,
  1179. .name = "fuse",
  1180. .fops = &fuse_dev_operations,
  1181. };
  1182. int __init fuse_dev_init(void)
  1183. {
  1184. int err = -ENOMEM;
  1185. fuse_req_cachep = kmem_cache_create("fuse_request",
  1186. sizeof(struct fuse_req),
  1187. 0, 0, NULL);
  1188. if (!fuse_req_cachep)
  1189. goto out;
  1190. err = misc_register(&fuse_miscdevice);
  1191. if (err)
  1192. goto out_cache_clean;
  1193. return 0;
  1194. out_cache_clean:
  1195. kmem_cache_destroy(fuse_req_cachep);
  1196. out:
  1197. return err;
  1198. }
  1199. void fuse_dev_cleanup(void)
  1200. {
  1201. misc_deregister(&fuse_miscdevice);
  1202. kmem_cache_destroy(fuse_req_cachep);
  1203. }