linux/fs/fuse/dir.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/pagemap.h>
  12#include <linux/file.h>
  13#include <linux/sched.h>
  14#include <linux/namei.h>
  15#include <linux/slab.h>
  16
  17static bool fuse_use_readdirplus(struct inode *dir, struct file *filp)
  18{
  19        struct fuse_conn *fc = get_fuse_conn(dir);
  20        struct fuse_inode *fi = get_fuse_inode(dir);
  21
  22        if (!fc->do_readdirplus)
  23                return false;
  24        if (!fc->readdirplus_auto)
  25                return true;
  26        if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
  27                return true;
  28        if (filp->f_pos == 0)
  29                return true;
  30        return false;
  31}
  32
  33static void fuse_advise_use_readdirplus(struct inode *dir)
  34{
  35        struct fuse_inode *fi = get_fuse_inode(dir);
  36
  37        set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
  38}
  39
  40#if BITS_PER_LONG >= 64
  41static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
  42{
  43        entry->d_time = time;
  44}
  45
  46static inline u64 fuse_dentry_time(struct dentry *entry)
  47{
  48        return entry->d_time;
  49}
  50#else
  51/*
  52 * On 32 bit archs store the high 32 bits of time in d_fsdata
  53 */
  54static void fuse_dentry_settime(struct dentry *entry, u64 time)
  55{
  56        entry->d_time = time;
  57        entry->d_fsdata = (void *) (unsigned long) (time >> 32);
  58}
  59
  60static u64 fuse_dentry_time(struct dentry *entry)
  61{
  62        return (u64) entry->d_time +
  63                ((u64) (unsigned long) entry->d_fsdata << 32);
  64}
  65#endif
  66
  67/*
  68 * FUSE caches dentries and attributes with separate timeout.  The
  69 * time in jiffies until the dentry/attributes are valid is stored in
  70 * dentry->d_time and fuse_inode->i_time respectively.
  71 */
  72
  73/*
  74 * Calculate the time in jiffies until a dentry/attributes are valid
  75 */
  76static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
  77{
  78        if (sec || nsec) {
  79                struct timespec ts = {sec, nsec};
  80                return get_jiffies_64() + timespec_to_jiffies(&ts);
  81        } else
  82                return 0;
  83}
  84
  85/*
  86 * Set dentry and possibly attribute timeouts from the lookup/mk*
  87 * replies
  88 */
  89static void fuse_change_entry_timeout(struct dentry *entry,
  90                                      struct fuse_entry_out *o)
  91{
  92        fuse_dentry_settime(entry,
  93                time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
  94}
  95
  96static u64 attr_timeout(struct fuse_attr_out *o)
  97{
  98        return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
  99}
 100
 101static u64 entry_attr_timeout(struct fuse_entry_out *o)
 102{
 103        return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
 104}
 105
 106/*
 107 * Mark the attributes as stale, so that at the next call to
 108 * ->getattr() they will be fetched from userspace
 109 */
 110void fuse_invalidate_attr(struct inode *inode)
 111{
 112        get_fuse_inode(inode)->i_time = 0;
 113}
 114
 115/**
 116 * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
 117 * atime is not used.
 118 */
 119void fuse_invalidate_atime(struct inode *inode)
 120{
 121        if (!IS_RDONLY(inode))
 122                WRITE_ONCE(get_fuse_inode(inode)->inval_atime, 1);
 123}
 124
 125/*
 126 * Just mark the entry as stale, so that a next attempt to look it up
 127 * will result in a new lookup call to userspace
 128 *
 129 * This is called when a dentry is about to become negative and the
 130 * timeout is unknown (unlink, rmdir, rename and in some cases
 131 * lookup)
 132 */
 133void fuse_invalidate_entry_cache(struct dentry *entry)
 134{
 135        fuse_dentry_settime(entry, 0);
 136}
 137
 138/*
 139 * Same as fuse_invalidate_entry_cache(), but also try to remove the
 140 * dentry from the hash
 141 */
 142static void fuse_invalidate_entry(struct dentry *entry)
 143{
 144        d_invalidate(entry);
 145        fuse_invalidate_entry_cache(entry);
 146}
 147
 148static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
 149                             u64 nodeid, struct qstr *name,
 150                             struct fuse_entry_out *outarg)
 151{
 152        memset(outarg, 0, sizeof(struct fuse_entry_out));
 153        req->in.h.opcode = FUSE_LOOKUP;
 154        req->in.h.nodeid = nodeid;
 155        req->in.numargs = 1;
 156        req->in.args[0].size = name->len + 1;
 157        req->in.args[0].value = name->name;
 158        req->out.numargs = 1;
 159        if (fc->minor < 9)
 160                req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
 161        else
 162                req->out.args[0].size = sizeof(struct fuse_entry_out);
 163        req->out.args[0].value = outarg;
 164}
 165
 166u64 fuse_get_attr_version(struct fuse_conn *fc)
 167{
 168        u64 curr_version;
 169
 170        /*
 171         * The spin lock isn't actually needed on 64bit archs, but we
 172         * don't yet care too much about such optimizations.
 173         */
 174        spin_lock(&fc->lock);
 175        curr_version = fc->attr_version;
 176        spin_unlock(&fc->lock);
 177
 178        return curr_version;
 179}
 180
 181/*
 182 * Check whether the dentry is still valid
 183 *
 184 * If the entry validity timeout has expired and the dentry is
 185 * positive, try to redo the lookup.  If the lookup results in a
 186 * different inode, then let the VFS invalidate the dentry and redo
 187 * the lookup once more.  If the lookup results in the same inode,
 188 * then refresh the attributes, timeouts and mark the dentry valid.
 189 */
 190static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
 191{
 192        struct inode *inode;
 193        struct dentry *parent;
 194        struct fuse_conn *fc;
 195        struct fuse_inode *fi;
 196        int ret;
 197
 198        inode = ACCESS_ONCE(entry->d_inode);
 199        if (inode && is_bad_inode(inode))
 200                goto invalid;
 201        else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
 202                 (flags & LOOKUP_REVAL)) {
 203                int err;
 204                struct fuse_entry_out outarg;
 205                struct fuse_req *req;
 206                struct fuse_forget_link *forget;
 207                u64 attr_version;
 208
 209                /* For negative dentries, always do a fresh lookup */
 210                if (!inode)
 211                        goto invalid;
 212
 213                ret = -ECHILD;
 214                if (flags & LOOKUP_RCU)
 215                        goto out;
 216
 217                fc = get_fuse_conn(inode);
 218                req = fuse_get_req_nopages(fc);
 219                ret = PTR_ERR(req);
 220                if (IS_ERR(req))
 221                        goto out;
 222
 223                forget = fuse_alloc_forget();
 224                if (!forget) {
 225                        fuse_put_request(fc, req);
 226                        ret = -ENOMEM;
 227                        goto out;
 228                }
 229
 230                attr_version = fuse_get_attr_version(fc);
 231
 232                parent = dget_parent(entry);
 233                fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
 234                                 &entry->d_name, &outarg);
 235                fuse_request_send(fc, req);
 236                dput(parent);
 237                err = req->out.h.error;
 238                fuse_put_request(fc, req);
 239                /* Zero nodeid is same as -ENOENT */
 240                if (!err && !outarg.nodeid)
 241                        err = -ENOENT;
 242                if (!err) {
 243                        fi = get_fuse_inode(inode);
 244                        if (outarg.nodeid != get_node_id(inode)) {
 245                                fuse_queue_forget(fc, forget, outarg.nodeid, 1);
 246                                goto invalid;
 247                        }
 248                        spin_lock(&fc->lock);
 249                        fi->nlookup++;
 250                        spin_unlock(&fc->lock);
 251                }
 252                kfree(forget);
 253                if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
 254                        goto invalid;
 255
 256                fuse_change_attributes(inode, &outarg.attr,
 257                                       entry_attr_timeout(&outarg),
 258                                       attr_version);
 259                fuse_change_entry_timeout(entry, &outarg);
 260        } else if (inode) {
 261                fi = get_fuse_inode(inode);
 262                if (flags & LOOKUP_RCU) {
 263                        if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
 264                                return -ECHILD;
 265                } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
 266                        parent = dget_parent(entry);
 267                        fuse_advise_use_readdirplus(parent->d_inode);
 268                        dput(parent);
 269                }
 270        }
 271        ret = 1;
 272out:
 273        return ret;
 274
 275invalid:
 276        ret = 0;
 277        goto out;
 278}
 279
 280static int invalid_nodeid(u64 nodeid)
 281{
 282        return !nodeid || nodeid == FUSE_ROOT_ID;
 283}
 284
 285const struct dentry_operations fuse_dentry_operations = {
 286        .d_revalidate   = fuse_dentry_revalidate,
 287};
 288
 289int fuse_valid_type(int m)
 290{
 291        return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
 292                S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
 293}
 294
 295int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
 296                     struct fuse_entry_out *outarg, struct inode **inode)
 297{
 298        struct fuse_conn *fc = get_fuse_conn_super(sb);
 299        struct fuse_req *req;
 300        struct fuse_forget_link *forget;
 301        u64 attr_version;
 302        int err;
 303
 304        *inode = NULL;
 305        err = -ENAMETOOLONG;
 306        if (name->len > FUSE_NAME_MAX)
 307                goto out;
 308
 309        req = fuse_get_req_nopages(fc);
 310        err = PTR_ERR(req);
 311        if (IS_ERR(req))
 312                goto out;
 313
 314        forget = fuse_alloc_forget();
 315        err = -ENOMEM;
 316        if (!forget) {
 317                fuse_put_request(fc, req);
 318                goto out;
 319        }
 320
 321        attr_version = fuse_get_attr_version(fc);
 322
 323        fuse_lookup_init(fc, req, nodeid, name, outarg);
 324        fuse_request_send(fc, req);
 325        err = req->out.h.error;
 326        fuse_put_request(fc, req);
 327        /* Zero nodeid is same as -ENOENT, but with valid timeout */
 328        if (err || !outarg->nodeid)
 329                goto out_put_forget;
 330
 331        err = -EIO;
 332        if (!outarg->nodeid)
 333                goto out_put_forget;
 334        if (!fuse_valid_type(outarg->attr.mode))
 335                goto out_put_forget;
 336
 337        *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
 338                           &outarg->attr, entry_attr_timeout(outarg),
 339                           attr_version);
 340        err = -ENOMEM;
 341        if (!*inode) {
 342                fuse_queue_forget(fc, forget, outarg->nodeid, 1);
 343                goto out;
 344        }
 345        err = 0;
 346
 347 out_put_forget:
 348        kfree(forget);
 349 out:
 350        return err;
 351}
 352
 353static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
 354                                  unsigned int flags)
 355{
 356        int err;
 357        struct fuse_entry_out outarg;
 358        struct inode *inode;
 359        struct dentry *newent;
 360        bool outarg_valid = true;
 361
 362        err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
 363                               &outarg, &inode);
 364        if (err == -ENOENT) {
 365                outarg_valid = false;
 366                err = 0;
 367        }
 368        if (err)
 369                goto out_err;
 370
 371        err = -EIO;
 372        if (inode && get_node_id(inode) == FUSE_ROOT_ID)
 373                goto out_iput;
 374
 375        newent = d_materialise_unique(entry, inode);
 376        err = PTR_ERR(newent);
 377        if (IS_ERR(newent))
 378                goto out_err;
 379
 380        entry = newent ? newent : entry;
 381        if (outarg_valid)
 382                fuse_change_entry_timeout(entry, &outarg);
 383        else
 384                fuse_invalidate_entry_cache(entry);
 385
 386        fuse_advise_use_readdirplus(dir);
 387        return newent;
 388
 389 out_iput:
 390        iput(inode);
 391 out_err:
 392        return ERR_PTR(err);
 393}
 394
 395/*
 396 * Atomic create+open operation
 397 *
 398 * If the filesystem doesn't support this, then fall back to separate
 399 * 'mknod' + 'open' requests.
 400 */
 401static int fuse_create_open(struct inode *dir, struct dentry *entry,
 402                            struct file *file, unsigned flags,
 403                            umode_t mode, int *opened)
 404{
 405        int err;
 406        struct inode *inode;
 407        struct fuse_conn *fc = get_fuse_conn(dir);
 408        struct fuse_req *req;
 409        struct fuse_forget_link *forget;
 410        struct fuse_create_in inarg;
 411        struct fuse_open_out outopen;
 412        struct fuse_entry_out outentry;
 413        struct fuse_file *ff;
 414
 415        /* Userspace expects S_IFREG in create mode */
 416        BUG_ON((mode & S_IFMT) != S_IFREG);
 417
 418        forget = fuse_alloc_forget();
 419        err = -ENOMEM;
 420        if (!forget)
 421                goto out_err;
 422
 423        req = fuse_get_req_nopages(fc);
 424        err = PTR_ERR(req);
 425        if (IS_ERR(req))
 426                goto out_put_forget_req;
 427
 428        err = -ENOMEM;
 429        ff = fuse_file_alloc(fc);
 430        if (!ff)
 431                goto out_put_request;
 432
 433        if (!fc->dont_mask)
 434                mode &= ~current_umask();
 435
 436        flags &= ~O_NOCTTY;
 437        memset(&inarg, 0, sizeof(inarg));
 438        memset(&outentry, 0, sizeof(outentry));
 439        inarg.flags = flags;
 440        inarg.mode = mode;
 441        inarg.umask = current_umask();
 442        req->in.h.opcode = FUSE_CREATE;
 443        req->in.h.nodeid = get_node_id(dir);
 444        req->in.numargs = 2;
 445        req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
 446                                                sizeof(inarg);
 447        req->in.args[0].value = &inarg;
 448        req->in.args[1].size = entry->d_name.len + 1;
 449        req->in.args[1].value = entry->d_name.name;
 450        req->out.numargs = 2;
 451        if (fc->minor < 9)
 452                req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
 453        else
 454                req->out.args[0].size = sizeof(outentry);
 455        req->out.args[0].value = &outentry;
 456        req->out.args[1].size = sizeof(outopen);
 457        req->out.args[1].value = &outopen;
 458        fuse_request_send(fc, req);
 459        err = req->out.h.error;
 460        if (err)
 461                goto out_free_ff;
 462
 463        err = -EIO;
 464        if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
 465                goto out_free_ff;
 466
 467        fuse_put_request(fc, req);
 468        ff->fh = outopen.fh;
 469        ff->nodeid = outentry.nodeid;
 470        ff->open_flags = outopen.open_flags;
 471        inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
 472                          &outentry.attr, entry_attr_timeout(&outentry), 0);
 473        if (!inode) {
 474                flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
 475                fuse_sync_release(ff, flags);
 476                fuse_queue_forget(fc, forget, outentry.nodeid, 1);
 477                err = -ENOMEM;
 478                goto out_err;
 479        }
 480        kfree(forget);
 481        d_instantiate(entry, inode);
 482        fuse_change_entry_timeout(entry, &outentry);
 483        fuse_invalidate_attr(dir);
 484        err = finish_open(file, entry, generic_file_open, opened);
 485        if (err) {
 486                fuse_sync_release(ff, flags);
 487        } else {
 488                file->private_data = fuse_file_get(ff);
 489                fuse_finish_open(inode, file);
 490        }
 491        return err;
 492
 493out_free_ff:
 494        fuse_file_free(ff);
 495out_put_request:
 496        fuse_put_request(fc, req);
 497out_put_forget_req:
 498        kfree(forget);
 499out_err:
 500        return err;
 501}
 502
 503static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
 504static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
 505                            struct file *file, unsigned flags,
 506                            umode_t mode, int *opened)
 507{
 508        int err;
 509        struct fuse_conn *fc = get_fuse_conn(dir);
 510        struct dentry *res = NULL;
 511
 512        if (d_unhashed(entry)) {
 513                res = fuse_lookup(dir, entry, 0);
 514                if (IS_ERR(res))
 515                        return PTR_ERR(res);
 516
 517                if (res)
 518                        entry = res;
 519        }
 520
 521        if (!(flags & O_CREAT) || entry->d_inode)
 522                goto no_open;
 523
 524        /* Only creates */
 525        *opened |= FILE_CREATED;
 526
 527        if (fc->no_create)
 528                goto mknod;
 529
 530        err = fuse_create_open(dir, entry, file, flags, mode, opened);
 531        if (err == -ENOSYS) {
 532                fc->no_create = 1;
 533                goto mknod;
 534        }
 535out_dput:
 536        dput(res);
 537        return err;
 538
 539mknod:
 540        err = fuse_mknod(dir, entry, mode, 0);
 541        if (err)
 542                goto out_dput;
 543no_open:
 544        return finish_no_open(file, res);
 545}
 546
 547/*
 548 * Code shared between mknod, mkdir, symlink and link
 549 */
 550static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
 551                            struct inode *dir, struct dentry *entry,
 552                            umode_t mode)
 553{
 554        struct fuse_entry_out outarg;
 555        struct inode *inode;
 556        int err;
 557        struct fuse_forget_link *forget;
 558
 559        forget = fuse_alloc_forget();
 560        if (!forget) {
 561                fuse_put_request(fc, req);
 562                return -ENOMEM;
 563        }
 564
 565        memset(&outarg, 0, sizeof(outarg));
 566        req->in.h.nodeid = get_node_id(dir);
 567        req->out.numargs = 1;
 568        if (fc->minor < 9)
 569                req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
 570        else
 571                req->out.args[0].size = sizeof(outarg);
 572        req->out.args[0].value = &outarg;
 573        fuse_request_send(fc, req);
 574        err = req->out.h.error;
 575        fuse_put_request(fc, req);
 576        if (err)
 577                goto out_put_forget_req;
 578
 579        err = -EIO;
 580        if (invalid_nodeid(outarg.nodeid))
 581                goto out_put_forget_req;
 582
 583        if ((outarg.attr.mode ^ mode) & S_IFMT)
 584                goto out_put_forget_req;
 585
 586        inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
 587                          &outarg.attr, entry_attr_timeout(&outarg), 0);
 588        if (!inode) {
 589                fuse_queue_forget(fc, forget, outarg.nodeid, 1);
 590                return -ENOMEM;
 591        }
 592        kfree(forget);
 593
 594        err = d_instantiate_no_diralias(entry, inode);
 595        if (err)
 596                return err;
 597
 598        fuse_change_entry_timeout(entry, &outarg);
 599        fuse_invalidate_attr(dir);
 600        return 0;
 601
 602 out_put_forget_req:
 603        kfree(forget);
 604        return err;
 605}
 606
 607static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
 608                      dev_t rdev)
 609{
 610        struct fuse_mknod_in inarg;
 611        struct fuse_conn *fc = get_fuse_conn(dir);
 612        struct fuse_req *req = fuse_get_req_nopages(fc);
 613        if (IS_ERR(req))
 614                return PTR_ERR(req);
 615
 616        if (!fc->dont_mask)
 617                mode &= ~current_umask();
 618
 619        memset(&inarg, 0, sizeof(inarg));
 620        inarg.mode = mode;
 621        inarg.rdev = new_encode_dev(rdev);
 622        inarg.umask = current_umask();
 623        req->in.h.opcode = FUSE_MKNOD;
 624        req->in.numargs = 2;
 625        req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
 626                                                sizeof(inarg);
 627        req->in.args[0].value = &inarg;
 628        req->in.args[1].size = entry->d_name.len + 1;
 629        req->in.args[1].value = entry->d_name.name;
 630        return create_new_entry(fc, req, dir, entry, mode);
 631}
 632
 633static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
 634                       bool excl)
 635{
 636        return fuse_mknod(dir, entry, mode, 0);
 637}
 638
 639static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
 640{
 641        struct fuse_mkdir_in inarg;
 642        struct fuse_conn *fc = get_fuse_conn(dir);
 643        struct fuse_req *req = fuse_get_req_nopages(fc);
 644        if (IS_ERR(req))
 645                return PTR_ERR(req);
 646
 647        if (!fc->dont_mask)
 648                mode &= ~current_umask();
 649
 650        memset(&inarg, 0, sizeof(inarg));
 651        inarg.mode = mode;
 652        inarg.umask = current_umask();
 653        req->in.h.opcode = FUSE_MKDIR;
 654        req->in.numargs = 2;
 655        req->in.args[0].size = sizeof(inarg);
 656        req->in.args[0].value = &inarg;
 657        req->in.args[1].size = entry->d_name.len + 1;
 658        req->in.args[1].value = entry->d_name.name;
 659        return create_new_entry(fc, req, dir, entry, S_IFDIR);
 660}
 661
 662static int fuse_symlink(struct inode *dir, struct dentry *entry,
 663                        const char *link)
 664{
 665        struct fuse_conn *fc = get_fuse_conn(dir);
 666        unsigned len = strlen(link) + 1;
 667        struct fuse_req *req = fuse_get_req_nopages(fc);
 668        if (IS_ERR(req))
 669                return PTR_ERR(req);
 670
 671        req->in.h.opcode = FUSE_SYMLINK;
 672        req->in.numargs = 2;
 673        req->in.args[0].size = entry->d_name.len + 1;
 674        req->in.args[0].value = entry->d_name.name;
 675        req->in.args[1].size = len;
 676        req->in.args[1].value = link;
 677        return create_new_entry(fc, req, dir, entry, S_IFLNK);
 678}
 679
 680static inline void fuse_update_ctime(struct inode *inode)
 681{
 682        if (!IS_NOCMTIME(inode)) {
 683                inode->i_ctime = current_fs_time(inode->i_sb);
 684                mark_inode_dirty_sync(inode);
 685        }
 686}
 687
 688static int fuse_unlink(struct inode *dir, struct dentry *entry)
 689{
 690        int err;
 691        struct fuse_conn *fc = get_fuse_conn(dir);
 692        struct fuse_req *req = fuse_get_req_nopages(fc);
 693        if (IS_ERR(req))
 694                return PTR_ERR(req);
 695
 696        req->in.h.opcode = FUSE_UNLINK;
 697        req->in.h.nodeid = get_node_id(dir);
 698        req->in.numargs = 1;
 699        req->in.args[0].size = entry->d_name.len + 1;
 700        req->in.args[0].value = entry->d_name.name;
 701        fuse_request_send(fc, req);
 702        err = req->out.h.error;
 703        fuse_put_request(fc, req);
 704        if (!err) {
 705                struct inode *inode = entry->d_inode;
 706                struct fuse_inode *fi = get_fuse_inode(inode);
 707
 708                spin_lock(&fc->lock);
 709                fi->attr_version = ++fc->attr_version;
 710                /*
 711                 * If i_nlink == 0 then unlink doesn't make sense, yet this can
 712                 * happen if userspace filesystem is careless.  It would be
 713                 * difficult to enforce correct nlink usage so just ignore this
 714                 * condition here
 715                 */
 716                if (inode->i_nlink > 0)
 717                        drop_nlink(inode);
 718                spin_unlock(&fc->lock);
 719                fuse_invalidate_attr(inode);
 720                fuse_invalidate_attr(dir);
 721                fuse_invalidate_entry_cache(entry);
 722                fuse_update_ctime(inode);
 723        } else if (err == -EINTR)
 724                fuse_invalidate_entry(entry);
 725        return err;
 726}
 727
 728static int fuse_rmdir(struct inode *dir, struct dentry *entry)
 729{
 730        int err;
 731        struct fuse_conn *fc = get_fuse_conn(dir);
 732        struct fuse_req *req = fuse_get_req_nopages(fc);
 733        if (IS_ERR(req))
 734                return PTR_ERR(req);
 735
 736        req->in.h.opcode = FUSE_RMDIR;
 737        req->in.h.nodeid = get_node_id(dir);
 738        req->in.numargs = 1;
 739        req->in.args[0].size = entry->d_name.len + 1;
 740        req->in.args[0].value = entry->d_name.name;
 741        fuse_request_send(fc, req);
 742        err = req->out.h.error;
 743        fuse_put_request(fc, req);
 744        if (!err) {
 745                clear_nlink(entry->d_inode);
 746                fuse_invalidate_attr(dir);
 747                fuse_invalidate_entry_cache(entry);
 748        } else if (err == -EINTR)
 749                fuse_invalidate_entry(entry);
 750        return err;
 751}
 752
 753static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
 754                              struct inode *newdir, struct dentry *newent,
 755                              unsigned int flags, int opcode, size_t argsize)
 756{
 757        int err;
 758        struct fuse_rename2_in inarg;
 759        struct fuse_conn *fc = get_fuse_conn(olddir);
 760        struct fuse_req *req;
 761
 762        req = fuse_get_req_nopages(fc);
 763        if (IS_ERR(req))
 764                return PTR_ERR(req);
 765
 766        memset(&inarg, 0, argsize);
 767        inarg.newdir = get_node_id(newdir);
 768        inarg.flags = flags;
 769        req->in.h.opcode = opcode;
 770        req->in.h.nodeid = get_node_id(olddir);
 771        req->in.numargs = 3;
 772        req->in.args[0].size = argsize;
 773        req->in.args[0].value = &inarg;
 774        req->in.args[1].size = oldent->d_name.len + 1;
 775        req->in.args[1].value = oldent->d_name.name;
 776        req->in.args[2].size = newent->d_name.len + 1;
 777        req->in.args[2].value = newent->d_name.name;
 778        fuse_request_send(fc, req);
 779        err = req->out.h.error;
 780        fuse_put_request(fc, req);
 781        if (!err) {
 782                /* ctime changes */
 783                fuse_invalidate_attr(oldent->d_inode);
 784                fuse_update_ctime(oldent->d_inode);
 785
 786                if (flags & RENAME_EXCHANGE) {
 787                        fuse_invalidate_attr(newent->d_inode);
 788                }
 789
 790                fuse_invalidate_attr(olddir);
 791                if (olddir != newdir)
 792                        fuse_invalidate_attr(newdir);
 793
 794                /* newent will end up negative */
 795                if (!(flags & RENAME_EXCHANGE) && newent->d_inode) {
 796                        fuse_invalidate_attr(newent->d_inode);
 797                        fuse_invalidate_entry_cache(newent);
 798                        fuse_update_ctime(newent->d_inode);
 799                }
 800        } else if (err == -EINTR) {
 801                /* If request was interrupted, DEITY only knows if the
 802                   rename actually took place.  If the invalidation
 803                   fails (e.g. some process has CWD under the renamed
 804                   directory), then there can be inconsistency between
 805                   the dcache and the real filesystem.  Tough luck. */
 806                fuse_invalidate_entry(oldent);
 807                if (newent->d_inode)
 808                        fuse_invalidate_entry(newent);
 809        }
 810
 811        return err;
 812}
 813
 814static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
 815                        struct inode *newdir, struct dentry *newent,
 816                        unsigned int flags)
 817{
 818        struct fuse_conn *fc = get_fuse_conn(olddir);
 819        int err;
 820
 821        if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
 822                return -EINVAL;
 823
 824        if (flags) {
 825                if (fc->no_rename2 || fc->minor < 23)
 826                        return -EINVAL;
 827
 828                err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
 829                                         FUSE_RENAME2,
 830                                         sizeof(struct fuse_rename2_in));
 831                if (err == -ENOSYS) {
 832                        fc->no_rename2 = 1;
 833                        err = -EINVAL;
 834                }
 835        } else {
 836                err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
 837                                         FUSE_RENAME,
 838                                         sizeof(struct fuse_rename_in));
 839        }
 840
 841        return err;
 842}
 843
 844static int fuse_rename(struct inode *olddir, struct dentry *oldent,
 845                       struct inode *newdir, struct dentry *newent)
 846{
 847        return fuse_rename_common(olddir, oldent, newdir, newent, 0,
 848                                  FUSE_RENAME,
 849                                  sizeof(struct fuse_rename_in));
 850}
 851
 852static int fuse_link(struct dentry *entry, struct inode *newdir,
 853                     struct dentry *newent)
 854{
 855        int err;
 856        struct fuse_link_in inarg;
 857        struct inode *inode = entry->d_inode;
 858        struct fuse_conn *fc = get_fuse_conn(inode);
 859        struct fuse_req *req = fuse_get_req_nopages(fc);
 860        if (IS_ERR(req))
 861                return PTR_ERR(req);
 862
 863        memset(&inarg, 0, sizeof(inarg));
 864        inarg.oldnodeid = get_node_id(inode);
 865        req->in.h.opcode = FUSE_LINK;
 866        req->in.numargs = 2;
 867        req->in.args[0].size = sizeof(inarg);
 868        req->in.args[0].value = &inarg;
 869        req->in.args[1].size = newent->d_name.len + 1;
 870        req->in.args[1].value = newent->d_name.name;
 871        err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
 872        /* Contrary to "normal" filesystems it can happen that link
 873           makes two "logical" inodes point to the same "physical"
 874           inode.  We invalidate the attributes of the old one, so it
 875           will reflect changes in the backing inode (link count,
 876           etc.)
 877        */
 878        if (!err) {
 879                struct fuse_inode *fi = get_fuse_inode(inode);
 880
 881                spin_lock(&fc->lock);
 882                fi->attr_version = ++fc->attr_version;
 883                inc_nlink(inode);
 884                spin_unlock(&fc->lock);
 885                fuse_invalidate_attr(inode);
 886                fuse_update_ctime(inode);
 887        } else if (err == -EINTR) {
 888                fuse_invalidate_attr(inode);
 889        }
 890        return err;
 891}
 892
 893static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
 894                          struct kstat *stat)
 895{
 896        unsigned int blkbits;
 897        struct fuse_conn *fc = get_fuse_conn(inode);
 898
 899        /* see the comment in fuse_change_attributes() */
 900        if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
 901                attr->size = i_size_read(inode);
 902                attr->mtime = inode->i_mtime.tv_sec;
 903                attr->mtimensec = inode->i_mtime.tv_nsec;
 904                attr->ctime = inode->i_ctime.tv_sec;
 905                attr->ctimensec = inode->i_ctime.tv_nsec;
 906        }
 907
 908        stat->dev = inode->i_sb->s_dev;
 909        stat->ino = attr->ino;
 910        stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
 911        stat->nlink = attr->nlink;
 912        stat->uid = make_kuid(fc->user_ns, attr->uid);
 913        stat->gid = make_kgid(fc->user_ns, attr->gid);
 914        stat->rdev = inode->i_rdev;
 915        stat->atime.tv_sec = attr->atime;
 916        stat->atime.tv_nsec = attr->atimensec;
 917        stat->mtime.tv_sec = attr->mtime;
 918        stat->mtime.tv_nsec = attr->mtimensec;
 919        stat->ctime.tv_sec = attr->ctime;
 920        stat->ctime.tv_nsec = attr->ctimensec;
 921        stat->size = attr->size;
 922        stat->blocks = attr->blocks;
 923
 924        if (attr->blksize != 0)
 925                blkbits = ilog2(attr->blksize);
 926        else
 927                blkbits = inode->i_sb->s_blocksize_bits;
 928
 929        stat->blksize = 1 << blkbits;
 930}
 931
 932static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
 933                           struct file *file)
 934{
 935        int err;
 936        struct fuse_getattr_in inarg;
 937        struct fuse_attr_out outarg;
 938        struct fuse_conn *fc = get_fuse_conn(inode);
 939        struct fuse_req *req;
 940        u64 attr_version;
 941
 942        req = fuse_get_req_nopages(fc);
 943        if (IS_ERR(req))
 944                return PTR_ERR(req);
 945
 946        attr_version = fuse_get_attr_version(fc);
 947
 948        memset(&inarg, 0, sizeof(inarg));
 949        memset(&outarg, 0, sizeof(outarg));
 950        /* Directories have separate file-handle space */
 951        if (file && S_ISREG(inode->i_mode)) {
 952                struct fuse_file *ff = file->private_data;
 953
 954                inarg.getattr_flags |= FUSE_GETATTR_FH;
 955                inarg.fh = ff->fh;
 956        }
 957        req->in.h.opcode = FUSE_GETATTR;
 958        req->in.h.nodeid = get_node_id(inode);
 959        req->in.numargs = 1;
 960        req->in.args[0].size = sizeof(inarg);
 961        req->in.args[0].value = &inarg;
 962        req->out.numargs = 1;
 963        if (fc->minor < 9)
 964                req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
 965        else
 966                req->out.args[0].size = sizeof(outarg);
 967        req->out.args[0].value = &outarg;
 968        fuse_request_send(fc, req);
 969        err = req->out.h.error;
 970        fuse_put_request(fc, req);
 971        if (!err) {
 972                if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
 973                        make_bad_inode(inode);
 974                        err = -EIO;
 975                } else {
 976                        fuse_change_attributes(inode, &outarg.attr,
 977                                               attr_timeout(&outarg),
 978                                               attr_version);
 979                        if (stat)
 980                                fuse_fillattr(inode, &outarg.attr, stat);
 981                }
 982        }
 983        return err;
 984}
 985
 986int fuse_update_attributes(struct inode *inode, struct kstat *stat,
 987                           struct file *file, bool *refreshed)
 988{
 989        struct fuse_inode *fi = get_fuse_inode(inode);
 990        int err;
 991        bool r;
 992
 993        if (time_before64(fi->i_time, get_jiffies_64())) {
 994                r = true;
 995                err = fuse_do_getattr(inode, stat, file);
 996        } else {
 997                r = false;
 998                err = 0;
 999                if (stat) {
1000                        generic_fillattr(inode, stat);
1001                        stat->mode = fi->orig_i_mode;
1002                        stat->ino = fi->orig_ino;
1003                }
1004        }
1005
1006        if (refreshed != NULL)
1007                *refreshed = r;
1008
1009        return err;
1010}
1011
1012int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
1013                             u64 child_nodeid, struct qstr *name)
1014{
1015        int err = -ENOTDIR;
1016        struct inode *parent;
1017        struct dentry *dir;
1018        struct dentry *entry;
1019
1020        parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
1021        if (!parent)
1022                return -ENOENT;
1023
1024        mutex_lock(&parent->i_mutex);
1025        if (!S_ISDIR(parent->i_mode))
1026                goto unlock;
1027
1028        err = -ENOENT;
1029        dir = d_find_alias(parent);
1030        if (!dir)
1031                goto unlock;
1032
1033        entry = d_lookup(dir, name);
1034        dput(dir);
1035        if (!entry)
1036                goto unlock;
1037
1038        fuse_invalidate_attr(parent);
1039        fuse_invalidate_entry(entry);
1040
1041        if (child_nodeid != 0 && entry->d_inode) {
1042                mutex_lock(&entry->d_inode->i_mutex);
1043                if (get_node_id(entry->d_inode) != child_nodeid) {
1044                        err = -ENOENT;
1045                        goto badentry;
1046                }
1047                if (d_mountpoint(entry)) {
1048                        err = -EBUSY;
1049                        goto badentry;
1050                }
1051                if (S_ISDIR(entry->d_inode->i_mode)) {
1052                        shrink_dcache_parent(entry);
1053                        if (!simple_empty(entry)) {
1054                                err = -ENOTEMPTY;
1055                                goto badentry;
1056                        }
1057                        entry->d_inode->i_flags |= S_DEAD;
1058                }
1059                dont_mount(entry);
1060                clear_nlink(entry->d_inode);
1061                err = 0;
1062 badentry:
1063                mutex_unlock(&entry->d_inode->i_mutex);
1064                if (!err)
1065                        d_delete(entry);
1066        } else {
1067                err = 0;
1068        }
1069        dput(entry);
1070
1071 unlock:
1072        mutex_unlock(&parent->i_mutex);
1073        iput(parent);
1074        return err;
1075}
1076
1077/*
1078 * Calling into a user-controlled filesystem gives the filesystem
1079 * daemon ptrace-like capabilities over the current process.  This
1080 * means, that the filesystem daemon is able to record the exact
1081 * filesystem operations performed, and can also control the behavior
1082 * of the requester process in otherwise impossible ways.  For example
1083 * it can delay the operation for arbitrary length of time allowing
1084 * DoS against the requester.
1085 *
1086 * For this reason only those processes can call into the filesystem,
1087 * for which the owner of the mount has ptrace privilege.  This
1088 * excludes processes started by other users, suid or sgid processes.
1089 */
1090int fuse_allow_current_process(struct fuse_conn *fc)
1091{
1092        const struct cred *cred;
1093
1094        if (fc->flags & FUSE_ALLOW_OTHER)
1095                return current_in_userns(fc->user_ns);
1096
1097        cred = current_cred();
1098        if (uid_eq(cred->euid, fc->user_id) &&
1099            uid_eq(cred->suid, fc->user_id) &&
1100            uid_eq(cred->uid,  fc->user_id) &&
1101            gid_eq(cred->egid, fc->group_id) &&
1102            gid_eq(cred->sgid, fc->group_id) &&
1103            gid_eq(cred->gid,  fc->group_id))
1104                return 1;
1105
1106        return 0;
1107}
1108
1109static int fuse_access(struct inode *inode, int mask)
1110{
1111        struct fuse_conn *fc = get_fuse_conn(inode);
1112        struct fuse_req *req;
1113        struct fuse_access_in inarg;
1114        int err;
1115
1116        if (fc->no_access)
1117                return 0;
1118
1119        req = fuse_get_req_nopages(fc);
1120        if (IS_ERR(req))
1121                return PTR_ERR(req);
1122
1123        memset(&inarg, 0, sizeof(inarg));
1124        inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1125        req->in.h.opcode = FUSE_ACCESS;
1126        req->in.h.nodeid = get_node_id(inode);
1127        req->in.numargs = 1;
1128        req->in.args[0].size = sizeof(inarg);
1129        req->in.args[0].value = &inarg;
1130        fuse_request_send(fc, req);
1131        err = req->out.h.error;
1132        fuse_put_request(fc, req);
1133        if (err == -ENOSYS) {
1134                fc->no_access = 1;
1135                err = 0;
1136        }
1137        return err;
1138}
1139
1140static int fuse_perm_getattr(struct inode *inode, int mask)
1141{
1142        if (mask & MAY_NOT_BLOCK)
1143                return -ECHILD;
1144
1145        return fuse_do_getattr(inode, NULL, NULL);
1146}
1147
1148/*
1149 * Check permission.  The two basic access models of FUSE are:
1150 *
1151 * 1) Local access checking ('default_permissions' mount option) based
1152 * on file mode.  This is the plain old disk filesystem permission
1153 * modell.
1154 *
1155 * 2) "Remote" access checking, where server is responsible for
1156 * checking permission in each inode operation.  An exception to this
1157 * is if ->permission() was invoked from sys_access() in which case an
1158 * access request is sent.  Execute permission is still checked
1159 * locally based on file mode.
1160 */
1161static int fuse_permission(struct inode *inode, int mask)
1162{
1163        struct fuse_conn *fc = get_fuse_conn(inode);
1164        bool refreshed = false;
1165        int err = 0;
1166
1167        if (!fuse_allow_current_process(fc))
1168                return -EACCES;
1169
1170        /*
1171         * If attributes are needed, refresh them before proceeding
1172         */
1173        if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1174            ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1175                struct fuse_inode *fi = get_fuse_inode(inode);
1176
1177                if (time_before64(fi->i_time, get_jiffies_64())) {
1178                        refreshed = true;
1179
1180                        err = fuse_perm_getattr(inode, mask);
1181                        if (err)
1182                                return err;
1183                }
1184        }
1185
1186        if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1187                err = generic_permission(inode, mask);
1188
1189                /* If permission is denied, try to refresh file
1190                   attributes.  This is also needed, because the root
1191                   node will at first have no permissions */
1192                if (err == -EACCES && !refreshed) {
1193                        err = fuse_perm_getattr(inode, mask);
1194                        if (!err)
1195                                err = generic_permission(inode, mask);
1196                }
1197
1198                /* Note: the opposite of the above test does not
1199                   exist.  So if permissions are revoked this won't be
1200                   noticed immediately, only after the attribute
1201                   timeout has expired */
1202        } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1203                if (mask & MAY_NOT_BLOCK)
1204                        return -ECHILD;
1205
1206                err = fuse_access(inode, mask);
1207        } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1208                if (!(inode->i_mode & S_IXUGO)) {
1209                        if (refreshed)
1210                                return -EACCES;
1211
1212                        err = fuse_perm_getattr(inode, mask);
1213                        if (!err && !(inode->i_mode & S_IXUGO))
1214                                return -EACCES;
1215                }
1216        }
1217        return err;
1218}
1219
1220static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1221                         void *dstbuf, filldir_t filldir)
1222{
1223        while (nbytes >= FUSE_NAME_OFFSET) {
1224                struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1225                size_t reclen = FUSE_DIRENT_SIZE(dirent);
1226                int over;
1227                if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1228                        return -EIO;
1229                if (reclen > nbytes)
1230                        break;
1231                if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1232                        return -EIO;
1233
1234                over = filldir(dstbuf, dirent->name, dirent->namelen,
1235                               file->f_pos, dirent->ino, dirent->type);
1236                if (over)
1237                        break;
1238
1239                buf += reclen;
1240                nbytes -= reclen;
1241                file->f_pos = dirent->off;
1242        }
1243
1244        return 0;
1245}
1246
1247static int fuse_direntplus_link(struct file *file,
1248                                struct fuse_direntplus *direntplus,
1249                                u64 attr_version)
1250{
1251        int err;
1252        struct fuse_entry_out *o = &direntplus->entry_out;
1253        struct fuse_dirent *dirent = &direntplus->dirent;
1254        struct dentry *parent = file->f_path.dentry;
1255        struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1256        struct dentry *dentry;
1257        struct dentry *alias;
1258        struct inode *dir = parent->d_inode;
1259        struct fuse_conn *fc;
1260        struct inode *inode;
1261
1262        if (!o->nodeid) {
1263                /*
1264                 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1265                 * ENOENT. Instead, it only means the userspace filesystem did
1266                 * not want to return attributes/handle for this entry.
1267                 *
1268                 * So do nothing.
1269                 */
1270                return 0;
1271        }
1272
1273        if (name.name[0] == '.') {
1274                /*
1275                 * We could potentially refresh the attributes of the directory
1276                 * and its parent?
1277                 */
1278                if (name.len == 1)
1279                        return 0;
1280                if (name.name[1] == '.' && name.len == 2)
1281                        return 0;
1282        }
1283
1284        if (invalid_nodeid(o->nodeid))
1285                return -EIO;
1286        if (!fuse_valid_type(o->attr.mode))
1287                return -EIO;
1288
1289        fc = get_fuse_conn(dir);
1290
1291        name.hash = full_name_hash(name.name, name.len);
1292        dentry = d_lookup(parent, &name);
1293        if (dentry) {
1294                inode = dentry->d_inode;
1295                if (!inode) {
1296                        d_drop(dentry);
1297                } else if (get_node_id(inode) != o->nodeid ||
1298                           ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1299                        err = d_invalidate(dentry);
1300                        if (err)
1301                                goto out;
1302                } else if (is_bad_inode(inode)) {
1303                        err = -EIO;
1304                        goto out;
1305                } else {
1306                        struct fuse_inode *fi;
1307                        fi = get_fuse_inode(inode);
1308                        spin_lock(&fc->lock);
1309                        fi->nlookup++;
1310                        spin_unlock(&fc->lock);
1311
1312                        fuse_change_attributes(inode, &o->attr,
1313                                               entry_attr_timeout(o),
1314                                               attr_version);
1315
1316                        /*
1317                         * The other branch to 'found' comes via fuse_iget()
1318                         * which bumps nlookup inside
1319                         */
1320                        goto found;
1321                }
1322                dput(dentry);
1323        }
1324
1325        dentry = d_alloc(parent, &name);
1326        err = -ENOMEM;
1327        if (!dentry)
1328                goto out;
1329
1330        inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1331                          &o->attr, entry_attr_timeout(o), attr_version);
1332        if (!inode)
1333                goto out;
1334
1335        alias = d_materialise_unique(dentry, inode);
1336        err = PTR_ERR(alias);
1337        if (IS_ERR(alias))
1338                goto out;
1339
1340        if (alias) {
1341                dput(dentry);
1342                dentry = alias;
1343        }
1344
1345found:
1346        if (fc->readdirplus_auto)
1347                set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1348        fuse_change_entry_timeout(dentry, o);
1349
1350        err = 0;
1351out:
1352        dput(dentry);
1353        return err;
1354}
1355
1356static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1357                             void *dstbuf, filldir_t filldir, u64 attr_version)
1358{
1359        struct fuse_direntplus *direntplus;
1360        struct fuse_dirent *dirent;
1361        size_t reclen;
1362        int over = 0;
1363        int ret;
1364
1365        while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1366                direntplus = (struct fuse_direntplus *) buf;
1367                dirent = &direntplus->dirent;
1368                reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1369
1370                if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1371                        return -EIO;
1372                if (reclen > nbytes)
1373                        break;
1374                if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1375                        return -EIO;
1376
1377                if (!over) {
1378                        /* We fill entries into dstbuf only as much as
1379                           it can hold. But we still continue iterating
1380                           over remaining entries to link them. If not,
1381                           we need to send a FORGET for each of those
1382                           which we did not link.
1383                        */
1384                        over = filldir(dstbuf, dirent->name, dirent->namelen,
1385                                       file->f_pos, dirent->ino,
1386                                       dirent->type);
1387                        if (!over)
1388                                file->f_pos = dirent->off;
1389                }
1390
1391                buf += reclen;
1392                nbytes -= reclen;
1393
1394                ret = fuse_direntplus_link(file, direntplus, attr_version);
1395                if (ret)
1396                        fuse_force_forget(file, direntplus->entry_out.nodeid);
1397        }
1398
1399        return 0;
1400}
1401
1402static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1403{
1404        int plus, err;
1405        size_t nbytes;
1406        struct page *page;
1407        struct inode *inode = file_inode(file);
1408        struct fuse_conn *fc = get_fuse_conn(inode);
1409        struct fuse_req *req;
1410        u64 attr_version = 0;
1411
1412        if (is_bad_inode(inode))
1413                return -EIO;
1414
1415        req = fuse_get_req(fc, 1);
1416        if (IS_ERR(req))
1417                return PTR_ERR(req);
1418
1419        page = alloc_page(GFP_KERNEL);
1420        if (!page) {
1421                fuse_put_request(fc, req);
1422                return -ENOMEM;
1423        }
1424
1425        plus = fuse_use_readdirplus(inode, file);
1426        req->out.argpages = 1;
1427        req->num_pages = 1;
1428        req->pages[0] = page;
1429        req->page_descs[0].length = PAGE_SIZE;
1430        if (plus) {
1431                attr_version = fuse_get_attr_version(fc);
1432                fuse_read_fill(req, file, file->f_pos, PAGE_SIZE,
1433                               FUSE_READDIRPLUS);
1434        } else {
1435                fuse_read_fill(req, file, file->f_pos, PAGE_SIZE,
1436                               FUSE_READDIR);
1437        }
1438        fuse_request_send(fc, req);
1439        nbytes = req->out.args[0].size;
1440        err = req->out.h.error;
1441        fuse_put_request(fc, req);
1442        if (!err) {
1443                if (plus) {
1444                        err = parse_dirplusfile(page_address(page), nbytes,
1445                                                file, dstbuf, filldir,
1446                                                attr_version);
1447                } else {
1448                        err = parse_dirfile(page_address(page), nbytes, file,
1449                                            dstbuf, filldir);
1450                }
1451        }
1452
1453        __free_page(page);
1454        fuse_invalidate_atime(inode);
1455        return err;
1456}
1457
1458static char *read_link(struct dentry *dentry)
1459{
1460        struct inode *inode = dentry->d_inode;
1461        struct fuse_conn *fc = get_fuse_conn(inode);
1462        struct fuse_req *req = fuse_get_req_nopages(fc);
1463        char *link;
1464
1465        if (IS_ERR(req))
1466                return ERR_CAST(req);
1467
1468        link = (char *) __get_free_page(GFP_KERNEL);
1469        if (!link) {
1470                link = ERR_PTR(-ENOMEM);
1471                goto out;
1472        }
1473        req->in.h.opcode = FUSE_READLINK;
1474        req->in.h.nodeid = get_node_id(inode);
1475        req->out.argvar = 1;
1476        req->out.numargs = 1;
1477        req->out.args[0].size = PAGE_SIZE - 1;
1478        req->out.args[0].value = link;
1479        fuse_request_send(fc, req);
1480        if (req->out.h.error) {
1481                free_page((unsigned long) link);
1482                link = ERR_PTR(req->out.h.error);
1483        } else
1484                link[req->out.args[0].size] = '\0';
1485 out:
1486        fuse_put_request(fc, req);
1487        fuse_invalidate_atime(inode);
1488        return link;
1489}
1490
1491static void free_link(char *link)
1492{
1493        if (!IS_ERR(link))
1494                free_page((unsigned long) link);
1495}
1496
1497static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1498{
1499        nd_set_link(nd, read_link(dentry));
1500        return NULL;
1501}
1502
1503static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1504{
1505        free_link(nd_get_link(nd));
1506}
1507
1508static int fuse_dir_open(struct inode *inode, struct file *file)
1509{
1510        return fuse_open_common(inode, file, true);
1511}
1512
1513static int fuse_dir_release(struct inode *inode, struct file *file)
1514{
1515        fuse_release_common(file, true);
1516
1517        return 0;
1518}
1519
1520static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1521                          int datasync)
1522{
1523        return fuse_fsync_common(file, start, end, datasync, 1);
1524}
1525
1526static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1527                            unsigned long arg)
1528{
1529        struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1530
1531        /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1532        if (fc->minor < 18)
1533                return -ENOTTY;
1534
1535        return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1536}
1537
1538static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1539                                   unsigned long arg)
1540{
1541        struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1542
1543        if (fc->minor < 18)
1544                return -ENOTTY;
1545
1546        return fuse_ioctl_common(file, cmd, arg,
1547                                 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1548}
1549
1550static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1551{
1552        /* Always update if mtime is explicitly set  */
1553        if (ivalid & ATTR_MTIME_SET)
1554                return true;
1555
1556        /* Or if kernel i_mtime is the official one */
1557        if (trust_local_mtime)
1558                return true;
1559
1560        /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1561        if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1562                return false;
1563
1564        /* In all other cases update */
1565        return true;
1566}
1567
1568static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1569                           struct fuse_setattr_in *arg, bool trust_local_cmtime)
1570{
1571        unsigned ivalid = iattr->ia_valid;
1572
1573        if (ivalid & ATTR_MODE)
1574                arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1575        if (ivalid & ATTR_UID)
1576                arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1577        if (ivalid & ATTR_GID)
1578                arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1579        if (ivalid & ATTR_SIZE)
1580                arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1581        if (ivalid & ATTR_ATIME) {
1582                arg->valid |= FATTR_ATIME;
1583                arg->atime = iattr->ia_atime.tv_sec;
1584                arg->atimensec = iattr->ia_atime.tv_nsec;
1585                if (!(ivalid & ATTR_ATIME_SET))
1586                        arg->valid |= FATTR_ATIME_NOW;
1587        }
1588        if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1589                arg->valid |= FATTR_MTIME;
1590                arg->mtime = iattr->ia_mtime.tv_sec;
1591                arg->mtimensec = iattr->ia_mtime.tv_nsec;
1592                if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1593                        arg->valid |= FATTR_MTIME_NOW;
1594        }
1595        if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1596                arg->valid |= FATTR_CTIME;
1597                arg->ctime = iattr->ia_ctime.tv_sec;
1598                arg->ctimensec = iattr->ia_ctime.tv_nsec;
1599        }
1600}
1601
1602/*
1603 * Prevent concurrent writepages on inode
1604 *
1605 * This is done by adding a negative bias to the inode write counter
1606 * and waiting for all pending writes to finish.
1607 */
1608void fuse_set_nowrite(struct inode *inode)
1609{
1610        struct fuse_conn *fc = get_fuse_conn(inode);
1611        struct fuse_inode *fi = get_fuse_inode(inode);
1612
1613        BUG_ON(!mutex_is_locked(&inode->i_mutex));
1614
1615        spin_lock(&fc->lock);
1616        BUG_ON(fi->writectr < 0);
1617        fi->writectr += FUSE_NOWRITE;
1618        spin_unlock(&fc->lock);
1619        wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1620}
1621
1622/*
1623 * Allow writepages on inode
1624 *
1625 * Remove the bias from the writecounter and send any queued
1626 * writepages.
1627 */
1628static void __fuse_release_nowrite(struct inode *inode)
1629{
1630        struct fuse_inode *fi = get_fuse_inode(inode);
1631
1632        BUG_ON(fi->writectr != FUSE_NOWRITE);
1633        fi->writectr = 0;
1634        fuse_flush_writepages(inode);
1635}
1636
1637void fuse_release_nowrite(struct inode *inode)
1638{
1639        struct fuse_conn *fc = get_fuse_conn(inode);
1640
1641        spin_lock(&fc->lock);
1642        __fuse_release_nowrite(inode);
1643        spin_unlock(&fc->lock);
1644}
1645
1646static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_req *req,
1647                              struct inode *inode,
1648                              struct fuse_setattr_in *inarg_p,
1649                              struct fuse_attr_out *outarg_p)
1650{
1651        req->in.h.opcode = FUSE_SETATTR;
1652        req->in.h.nodeid = get_node_id(inode);
1653        req->in.numargs = 1;
1654        req->in.args[0].size = sizeof(*inarg_p);
1655        req->in.args[0].value = inarg_p;
1656        req->out.numargs = 1;
1657        if (fc->minor < 9)
1658                req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1659        else
1660                req->out.args[0].size = sizeof(*outarg_p);
1661        req->out.args[0].value = outarg_p;
1662}
1663
1664/*
1665 * Flush inode->i_mtime to the server
1666 */
1667int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1668{
1669        struct fuse_conn *fc = get_fuse_conn(inode);
1670        struct fuse_req *req;
1671        struct fuse_setattr_in inarg;
1672        struct fuse_attr_out outarg;
1673        int err;
1674
1675        req = fuse_get_req_nopages(fc);
1676        if (IS_ERR(req))
1677                return PTR_ERR(req);
1678
1679        memset(&inarg, 0, sizeof(inarg));
1680        memset(&outarg, 0, sizeof(outarg));
1681
1682        inarg.valid = FATTR_MTIME;
1683        inarg.mtime = inode->i_mtime.tv_sec;
1684        inarg.mtimensec = inode->i_mtime.tv_nsec;
1685        if (fc->minor >= 23) {
1686                inarg.valid |= FATTR_CTIME;
1687                inarg.ctime = inode->i_ctime.tv_sec;
1688                inarg.ctimensec = inode->i_ctime.tv_nsec;
1689        }
1690        if (ff) {
1691                inarg.valid |= FATTR_FH;
1692                inarg.fh = ff->fh;
1693        }
1694        fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1695        fuse_request_send(fc, req);
1696        err = req->out.h.error;
1697        fuse_put_request(fc, req);
1698
1699        return err;
1700}
1701
1702/*
1703 * Set attributes, and at the same time refresh them.
1704 *
1705 * Truncation is slightly complicated, because the 'truncate' request
1706 * may fail, in which case we don't want to touch the mapping.
1707 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1708 * and the actual truncation by hand.
1709 */
1710int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1711                    struct file *file)
1712{
1713        struct fuse_conn *fc = get_fuse_conn(inode);
1714        struct fuse_inode *fi = get_fuse_inode(inode);
1715        struct fuse_req *req;
1716        struct fuse_setattr_in inarg;
1717        struct fuse_attr_out outarg;
1718        bool is_truncate = false;
1719        bool is_wb = fc->writeback_cache;
1720        loff_t oldsize;
1721        int err;
1722        bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1723
1724        if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1725                attr->ia_valid |= ATTR_FORCE;
1726
1727        err = inode_change_ok(inode, attr);
1728        if (err)
1729                return err;
1730
1731        if (attr->ia_valid & ATTR_OPEN) {
1732                /* This is coming from open(..., ... | O_TRUNC); */
1733                WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1734                WARN_ON(attr->ia_size != 0);
1735                if (fc->atomic_o_trunc) {
1736                        /*
1737                         * No need to send request to userspace, since actual
1738                         * truncation has already been done by OPEN.  But still
1739                         * need to truncate page cache.
1740                         */
1741                        i_size_write(inode, 0);
1742                        truncate_pagecache(inode, 0);
1743                        return 0;
1744                }
1745                file = NULL;
1746        }
1747
1748        if (attr->ia_valid & ATTR_SIZE)
1749                is_truncate = true;
1750
1751        req = fuse_get_req_nopages(fc);
1752        if (IS_ERR(req))
1753                return PTR_ERR(req);
1754
1755        if (is_truncate) {
1756                fuse_set_nowrite(inode);
1757                set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1758                if (trust_local_cmtime && attr->ia_size != inode->i_size)
1759                        attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1760        }
1761
1762        memset(&inarg, 0, sizeof(inarg));
1763        memset(&outarg, 0, sizeof(outarg));
1764        iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1765        if (file) {
1766                struct fuse_file *ff = file->private_data;
1767                inarg.valid |= FATTR_FH;
1768                inarg.fh = ff->fh;
1769        }
1770        if (attr->ia_valid & ATTR_SIZE) {
1771                /* For mandatory locking in truncate */
1772                inarg.valid |= FATTR_LOCKOWNER;
1773                inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1774        }
1775        fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1776        fuse_request_send(fc, req);
1777        err = req->out.h.error;
1778        fuse_put_request(fc, req);
1779        if (err) {
1780                if (err == -EINTR)
1781                        fuse_invalidate_attr(inode);
1782                goto error;
1783        }
1784
1785        if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1786                make_bad_inode(inode);
1787                err = -EIO;
1788                goto error;
1789        }
1790
1791        spin_lock(&fc->lock);
1792        /* the kernel maintains i_mtime locally */
1793        if (trust_local_cmtime) {
1794                if (attr->ia_valid & ATTR_MTIME)
1795                        inode->i_mtime = attr->ia_mtime;
1796                if (attr->ia_valid & ATTR_CTIME)
1797                        inode->i_ctime = attr->ia_ctime;
1798                /* FIXME: clear I_DIRTY_SYNC? */
1799        }
1800
1801        fuse_change_attributes_common(inode, &outarg.attr,
1802                                      attr_timeout(&outarg));
1803        oldsize = inode->i_size;
1804        /* see the comment in fuse_change_attributes() */
1805        if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1806                i_size_write(inode, outarg.attr.size);
1807
1808        if (is_truncate) {
1809                /* NOTE: this may release/reacquire fc->lock */
1810                __fuse_release_nowrite(inode);
1811        }
1812        spin_unlock(&fc->lock);
1813
1814        /*
1815         * Only call invalidate_inode_pages2() after removing
1816         * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1817         */
1818        if ((is_truncate || !is_wb) &&
1819            S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1820                truncate_pagecache(inode, outarg.attr.size);
1821                invalidate_inode_pages2(inode->i_mapping);
1822        }
1823
1824        clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1825        return 0;
1826
1827error:
1828        if (is_truncate)
1829                fuse_release_nowrite(inode);
1830
1831        clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1832        return err;
1833}
1834
1835static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1836{
1837        struct inode *inode = entry->d_inode;
1838        int ret;
1839
1840        if (!fuse_allow_current_process(get_fuse_conn(inode)))
1841                return -EACCES;
1842
1843        if (attr->ia_valid & ATTR_FILE)
1844                ret = fuse_do_setattr(inode, attr, attr->ia_file);
1845        else
1846                ret = fuse_do_setattr(inode, attr, NULL);
1847
1848        if (!ret) {
1849                /* Directory mode changed, may need to revalidate access */
1850                if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1851                        fuse_invalidate_entry_cache(entry);
1852        }
1853        return ret;
1854}
1855
1856static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1857                        struct kstat *stat)
1858{
1859        struct inode *inode = entry->d_inode;
1860        struct fuse_conn *fc = get_fuse_conn(inode);
1861
1862        if (!fuse_allow_current_process(fc))
1863                return -EACCES;
1864
1865        if (READ_ONCE(get_fuse_inode(inode)->inval_atime))
1866                fuse_invalidate_attr(inode);
1867
1868        return fuse_update_attributes(inode, stat, NULL, NULL);
1869}
1870
1871static int fuse_setxattr(struct dentry *entry, const char *name,
1872                         const void *value, size_t size, int flags)
1873{
1874        struct inode *inode = entry->d_inode;
1875        struct fuse_conn *fc = get_fuse_conn(inode);
1876        struct fuse_req *req;
1877        struct fuse_setxattr_in inarg;
1878        int err;
1879
1880        if (fc->no_setxattr)
1881                return -EOPNOTSUPP;
1882
1883        req = fuse_get_req_nopages(fc);
1884        if (IS_ERR(req))
1885                return PTR_ERR(req);
1886
1887        memset(&inarg, 0, sizeof(inarg));
1888        inarg.size = size;
1889        inarg.flags = flags;
1890        req->in.h.opcode = FUSE_SETXATTR;
1891        req->in.h.nodeid = get_node_id(inode);
1892        req->in.numargs = 3;
1893        req->in.args[0].size = sizeof(inarg);
1894        req->in.args[0].value = &inarg;
1895        req->in.args[1].size = strlen(name) + 1;
1896        req->in.args[1].value = name;
1897        req->in.args[2].size = size;
1898        req->in.args[2].value = value;
1899        fuse_request_send(fc, req);
1900        err = req->out.h.error;
1901        fuse_put_request(fc, req);
1902        if (err == -ENOSYS) {
1903                fc->no_setxattr = 1;
1904                err = -EOPNOTSUPP;
1905        }
1906        if (!err) {
1907                fuse_invalidate_attr(inode);
1908                fuse_update_ctime(inode);
1909        }
1910        return err;
1911}
1912
1913static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1914                             void *value, size_t size)
1915{
1916        struct inode *inode = entry->d_inode;
1917        struct fuse_conn *fc = get_fuse_conn(inode);
1918        struct fuse_req *req;
1919        struct fuse_getxattr_in inarg;
1920        struct fuse_getxattr_out outarg;
1921        ssize_t ret;
1922
1923        if (fc->no_getxattr)
1924                return -EOPNOTSUPP;
1925
1926        req = fuse_get_req_nopages(fc);
1927        if (IS_ERR(req))
1928                return PTR_ERR(req);
1929
1930        memset(&inarg, 0, sizeof(inarg));
1931        inarg.size = size;
1932        req->in.h.opcode = FUSE_GETXATTR;
1933        req->in.h.nodeid = get_node_id(inode);
1934        req->in.numargs = 2;
1935        req->in.args[0].size = sizeof(inarg);
1936        req->in.args[0].value = &inarg;
1937        req->in.args[1].size = strlen(name) + 1;
1938        req->in.args[1].value = name;
1939        /* This is really two different operations rolled into one */
1940        req->out.numargs = 1;
1941        if (size) {
1942                req->out.argvar = 1;
1943                req->out.args[0].size = size;
1944                req->out.args[0].value = value;
1945        } else {
1946                req->out.args[0].size = sizeof(outarg);
1947                req->out.args[0].value = &outarg;
1948        }
1949        fuse_request_send(fc, req);
1950        ret = req->out.h.error;
1951        if (!ret)
1952                ret = size ? req->out.args[0].size : outarg.size;
1953        else {
1954                if (ret == -ENOSYS) {
1955                        fc->no_getxattr = 1;
1956                        ret = -EOPNOTSUPP;
1957                }
1958        }
1959        fuse_put_request(fc, req);
1960        return ret;
1961}
1962
1963static int fuse_verify_xattr_list(char *list, size_t size)
1964{
1965        size_t origsize = size;
1966
1967        while (size) {
1968                size_t thislen = strnlen(list, size);
1969
1970                if (!thislen || thislen == size)
1971                        return -EIO;
1972
1973                size -= thislen + 1;
1974                list += thislen + 1;
1975        }
1976
1977        return origsize;
1978}
1979
1980static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1981{
1982        struct inode *inode = entry->d_inode;
1983        struct fuse_conn *fc = get_fuse_conn(inode);
1984        struct fuse_req *req;
1985        struct fuse_getxattr_in inarg;
1986        struct fuse_getxattr_out outarg;
1987        ssize_t ret;
1988
1989        if (!fuse_allow_current_process(fc))
1990                return -EACCES;
1991
1992        if (fc->no_listxattr)
1993                return -EOPNOTSUPP;
1994
1995        req = fuse_get_req_nopages(fc);
1996        if (IS_ERR(req))
1997                return PTR_ERR(req);
1998
1999        memset(&inarg, 0, sizeof(inarg));
2000        inarg.size = size;
2001        req->in.h.opcode = FUSE_LISTXATTR;
2002        req->in.h.nodeid = get_node_id(inode);
2003        req->in.numargs = 1;
2004        req->in.args[0].size = sizeof(inarg);
2005        req->in.args[0].value = &inarg;
2006        /* This is really two different operations rolled into one */
2007        req->out.numargs = 1;
2008        if (size) {
2009                req->out.argvar = 1;
2010                req->out.args[0].size = size;
2011                req->out.args[0].value = list;
2012        } else {
2013                req->out.args[0].size = sizeof(outarg);
2014                req->out.args[0].value = &outarg;
2015        }
2016        fuse_request_send(fc, req);
2017        ret = req->out.h.error;
2018        if (!ret) {
2019                ret = size ? req->out.args[0].size : outarg.size;
2020                if (ret > 0 && size)
2021                        ret = fuse_verify_xattr_list(list, ret);
2022        } else {
2023                if (ret == -ENOSYS) {
2024                        fc->no_listxattr = 1;
2025                        ret = -EOPNOTSUPP;
2026                }
2027        }
2028        fuse_put_request(fc, req);
2029        return ret;
2030}
2031
2032static int fuse_removexattr(struct dentry *entry, const char *name)
2033{
2034        struct inode *inode = entry->d_inode;
2035        struct fuse_conn *fc = get_fuse_conn(inode);
2036        struct fuse_req *req;
2037        int err;
2038
2039        if (fc->no_removexattr)
2040                return -EOPNOTSUPP;
2041
2042        req = fuse_get_req_nopages(fc);
2043        if (IS_ERR(req))
2044                return PTR_ERR(req);
2045
2046        req->in.h.opcode = FUSE_REMOVEXATTR;
2047        req->in.h.nodeid = get_node_id(inode);
2048        req->in.numargs = 1;
2049        req->in.args[0].size = strlen(name) + 1;
2050        req->in.args[0].value = name;
2051        fuse_request_send(fc, req);
2052        err = req->out.h.error;
2053        fuse_put_request(fc, req);
2054        if (err == -ENOSYS) {
2055                fc->no_removexattr = 1;
2056                err = -EOPNOTSUPP;
2057        }
2058        if (!err) {
2059                fuse_invalidate_attr(inode);
2060                fuse_update_ctime(inode);
2061        }
2062        return err;
2063}
2064
2065static const struct inode_operations_wrapper fuse_dir_inode_operations = {
2066        .ops = {
2067        .lookup         = fuse_lookup,
2068        .mkdir          = fuse_mkdir,
2069        .symlink        = fuse_symlink,
2070        .unlink         = fuse_unlink,
2071        .rmdir          = fuse_rmdir,
2072        .rename         = fuse_rename,
2073        .link           = fuse_link,
2074        .setattr        = fuse_setattr,
2075        .create         = fuse_create,
2076        .atomic_open    = fuse_atomic_open,
2077        .mknod          = fuse_mknod,
2078        .permission     = fuse_permission,
2079        .getattr        = fuse_getattr,
2080        .setxattr       = fuse_setxattr,
2081        .getxattr       = fuse_getxattr,
2082        .listxattr      = fuse_listxattr,
2083        .removexattr    = fuse_removexattr,
2084        },
2085        .rename2        = fuse_rename2,
2086};
2087
2088static const struct file_operations fuse_dir_operations = {
2089        .llseek         = generic_file_llseek,
2090        .read           = generic_read_dir,
2091        .readdir        = fuse_readdir,
2092        .open           = fuse_dir_open,
2093        .release        = fuse_dir_release,
2094        .fsync          = fuse_dir_fsync,
2095        .unlocked_ioctl = fuse_dir_ioctl,
2096        .compat_ioctl   = fuse_dir_compat_ioctl,
2097};
2098
2099static const struct inode_operations fuse_common_inode_operations = {
2100        .setattr        = fuse_setattr,
2101        .permission     = fuse_permission,
2102        .getattr        = fuse_getattr,
2103        .setxattr       = fuse_setxattr,
2104        .getxattr       = fuse_getxattr,
2105        .listxattr      = fuse_listxattr,
2106        .removexattr    = fuse_removexattr,
2107};
2108
2109static const struct inode_operations fuse_symlink_inode_operations = {
2110        .setattr        = fuse_setattr,
2111        .follow_link    = fuse_follow_link,
2112        .put_link       = fuse_put_link,
2113        .readlink       = generic_readlink,
2114        .getattr        = fuse_getattr,
2115        .setxattr       = fuse_setxattr,
2116        .getxattr       = fuse_getxattr,
2117        .listxattr      = fuse_listxattr,
2118        .removexattr    = fuse_removexattr,
2119};
2120
2121void fuse_init_common(struct inode *inode)
2122{
2123        inode->i_op = &fuse_common_inode_operations;
2124}
2125
2126void fuse_init_dir(struct inode *inode)
2127{
2128        inode->i_op = &fuse_dir_inode_operations.ops;
2129        inode->i_fop = &fuse_dir_operations;
2130        inode->i_flags |= S_IOPS_WRAPPER;
2131}
2132
2133void fuse_init_symlink(struct inode *inode)
2134{
2135        inode->i_op = &fuse_symlink_inode_operations;
2136}
2137