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