linux/fs/overlayfs/util.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2011 Novell Inc.
   3 * Copyright (C) 2016 Red Hat, Inc.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of the GNU General Public License version 2 as published by
   7 * the Free Software Foundation.
   8 */
   9
  10#include <linux/fs.h>
  11#include <linux/mount.h>
  12#include <linux/slab.h>
  13#include <linux/cred.h>
  14#include <linux/xattr.h>
  15#include <linux/exportfs.h>
  16#include <linux/uuid.h>
  17#include <linux/namei.h>
  18#include <linux/ratelimit.h>
  19#include "overlayfs.h"
  20
  21int ovl_want_write(struct dentry *dentry)
  22{
  23        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  24        return mnt_want_write(ovl_upper_mnt(ofs));
  25}
  26
  27void ovl_drop_write(struct dentry *dentry)
  28{
  29        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  30        mnt_drop_write(ovl_upper_mnt(ofs));
  31}
  32
  33struct dentry *ovl_workdir(struct dentry *dentry)
  34{
  35        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  36        return ofs->workdir;
  37}
  38
  39const struct cred *ovl_override_creds(struct super_block *sb)
  40{
  41        struct ovl_fs *ofs = sb->s_fs_info;
  42
  43        return override_creds(ofs->creator_cred);
  44}
  45
  46/*
  47 * Check if underlying fs supports file handles and try to determine encoding
  48 * type, in order to deduce maximum inode number used by fs.
  49 *
  50 * Return 0 if file handles are not supported.
  51 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
  52 * Return -1 if fs uses a non default encoding with unknown inode size.
  53 */
  54int ovl_can_decode_fh(struct super_block *sb)
  55{
  56        if (!capable(CAP_DAC_READ_SEARCH))
  57                return 0;
  58
  59        if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
  60                return 0;
  61
  62        return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
  63}
  64
  65struct dentry *ovl_indexdir(struct super_block *sb)
  66{
  67        struct ovl_fs *ofs = sb->s_fs_info;
  68
  69        return ofs->indexdir;
  70}
  71
  72/* Index all files on copy up. For now only enabled for NFS export */
  73bool ovl_index_all(struct super_block *sb)
  74{
  75        struct ovl_fs *ofs = sb->s_fs_info;
  76
  77        return ofs->config.nfs_export && ofs->config.index;
  78}
  79
  80/* Verify lower origin on lookup. For now only enabled for NFS export */
  81bool ovl_verify_lower(struct super_block *sb)
  82{
  83        struct ovl_fs *ofs = sb->s_fs_info;
  84
  85        return ofs->config.nfs_export && ofs->config.index;
  86}
  87
  88struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  89{
  90        size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  91        struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  92
  93        if (oe)
  94                oe->numlower = numlower;
  95
  96        return oe;
  97}
  98
  99bool ovl_dentry_remote(struct dentry *dentry)
 100{
 101        return dentry->d_flags &
 102                (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
 103}
 104
 105void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
 106                             unsigned int mask)
 107{
 108        struct ovl_entry *oe = OVL_E(dentry);
 109        unsigned int i, flags = 0;
 110
 111        if (upperdentry)
 112                flags |= upperdentry->d_flags;
 113        for (i = 0; i < oe->numlower; i++)
 114                flags |= oe->lowerstack[i].dentry->d_flags;
 115
 116        spin_lock(&dentry->d_lock);
 117        dentry->d_flags &= ~mask;
 118        dentry->d_flags |= flags & mask;
 119        spin_unlock(&dentry->d_lock);
 120}
 121
 122bool ovl_dentry_weird(struct dentry *dentry)
 123{
 124        return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
 125                                  DCACHE_MANAGE_TRANSIT |
 126                                  DCACHE_OP_HASH |
 127                                  DCACHE_OP_COMPARE);
 128}
 129
 130enum ovl_path_type ovl_path_type(struct dentry *dentry)
 131{
 132        struct ovl_entry *oe = dentry->d_fsdata;
 133        enum ovl_path_type type = 0;
 134
 135        if (ovl_dentry_upper(dentry)) {
 136                type = __OVL_PATH_UPPER;
 137
 138                /*
 139                 * Non-dir dentry can hold lower dentry of its copy up origin.
 140                 */
 141                if (oe->numlower) {
 142                        if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
 143                                type |= __OVL_PATH_ORIGIN;
 144                        if (d_is_dir(dentry) ||
 145                            !ovl_has_upperdata(d_inode(dentry)))
 146                                type |= __OVL_PATH_MERGE;
 147                }
 148        } else {
 149                if (oe->numlower > 1)
 150                        type |= __OVL_PATH_MERGE;
 151        }
 152        return type;
 153}
 154
 155void ovl_path_upper(struct dentry *dentry, struct path *path)
 156{
 157        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 158
 159        path->mnt = ovl_upper_mnt(ofs);
 160        path->dentry = ovl_dentry_upper(dentry);
 161}
 162
 163void ovl_path_lower(struct dentry *dentry, struct path *path)
 164{
 165        struct ovl_entry *oe = dentry->d_fsdata;
 166
 167        if (oe->numlower) {
 168                path->mnt = oe->lowerstack[0].layer->mnt;
 169                path->dentry = oe->lowerstack[0].dentry;
 170        } else {
 171                *path = (struct path) { };
 172        }
 173}
 174
 175void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
 176{
 177        struct ovl_entry *oe = dentry->d_fsdata;
 178
 179        if (oe->numlower) {
 180                path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
 181                path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
 182        } else {
 183                *path = (struct path) { };
 184        }
 185}
 186
 187enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
 188{
 189        enum ovl_path_type type = ovl_path_type(dentry);
 190
 191        if (!OVL_TYPE_UPPER(type))
 192                ovl_path_lower(dentry, path);
 193        else
 194                ovl_path_upper(dentry, path);
 195
 196        return type;
 197}
 198
 199struct dentry *ovl_dentry_upper(struct dentry *dentry)
 200{
 201        return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
 202}
 203
 204struct dentry *ovl_dentry_lower(struct dentry *dentry)
 205{
 206        struct ovl_entry *oe = dentry->d_fsdata;
 207
 208        return oe->numlower ? oe->lowerstack[0].dentry : NULL;
 209}
 210
 211const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
 212{
 213        struct ovl_entry *oe = dentry->d_fsdata;
 214
 215        return oe->numlower ? oe->lowerstack[0].layer : NULL;
 216}
 217
 218/*
 219 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
 220 * dependig on what is stored in lowerstack[0]. At times we need to find
 221 * lower dentry which has data (and not metacopy dentry). This helper
 222 * returns the lower data dentry.
 223 */
 224struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
 225{
 226        struct ovl_entry *oe = dentry->d_fsdata;
 227
 228        return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
 229}
 230
 231struct dentry *ovl_dentry_real(struct dentry *dentry)
 232{
 233        return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
 234}
 235
 236struct dentry *ovl_i_dentry_upper(struct inode *inode)
 237{
 238        return ovl_upperdentry_dereference(OVL_I(inode));
 239}
 240
 241struct inode *ovl_inode_upper(struct inode *inode)
 242{
 243        struct dentry *upperdentry = ovl_i_dentry_upper(inode);
 244
 245        return upperdentry ? d_inode(upperdentry) : NULL;
 246}
 247
 248struct inode *ovl_inode_lower(struct inode *inode)
 249{
 250        return OVL_I(inode)->lower;
 251}
 252
 253struct inode *ovl_inode_real(struct inode *inode)
 254{
 255        return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
 256}
 257
 258/* Return inode which contains lower data. Do not return metacopy */
 259struct inode *ovl_inode_lowerdata(struct inode *inode)
 260{
 261        if (WARN_ON(!S_ISREG(inode->i_mode)))
 262                return NULL;
 263
 264        return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
 265}
 266
 267/* Return real inode which contains data. Does not return metacopy inode */
 268struct inode *ovl_inode_realdata(struct inode *inode)
 269{
 270        struct inode *upperinode;
 271
 272        upperinode = ovl_inode_upper(inode);
 273        if (upperinode && ovl_has_upperdata(inode))
 274                return upperinode;
 275
 276        return ovl_inode_lowerdata(inode);
 277}
 278
 279struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
 280{
 281        return OVL_I(inode)->cache;
 282}
 283
 284void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
 285{
 286        OVL_I(inode)->cache = cache;
 287}
 288
 289void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
 290{
 291        set_bit(flag, &OVL_E(dentry)->flags);
 292}
 293
 294void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
 295{
 296        clear_bit(flag, &OVL_E(dentry)->flags);
 297}
 298
 299bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
 300{
 301        return test_bit(flag, &OVL_E(dentry)->flags);
 302}
 303
 304bool ovl_dentry_is_opaque(struct dentry *dentry)
 305{
 306        return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
 307}
 308
 309bool ovl_dentry_is_whiteout(struct dentry *dentry)
 310{
 311        return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
 312}
 313
 314void ovl_dentry_set_opaque(struct dentry *dentry)
 315{
 316        ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
 317}
 318
 319/*
 320 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
 321 * to return positive, while there's no actual upper alias for the inode.
 322 * Copy up code needs to know about the existence of the upper alias, so it
 323 * can't use ovl_dentry_upper().
 324 */
 325bool ovl_dentry_has_upper_alias(struct dentry *dentry)
 326{
 327        return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
 328}
 329
 330void ovl_dentry_set_upper_alias(struct dentry *dentry)
 331{
 332        ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
 333}
 334
 335static bool ovl_should_check_upperdata(struct inode *inode)
 336{
 337        if (!S_ISREG(inode->i_mode))
 338                return false;
 339
 340        if (!ovl_inode_lower(inode))
 341                return false;
 342
 343        return true;
 344}
 345
 346bool ovl_has_upperdata(struct inode *inode)
 347{
 348        if (!ovl_should_check_upperdata(inode))
 349                return true;
 350
 351        if (!ovl_test_flag(OVL_UPPERDATA, inode))
 352                return false;
 353        /*
 354         * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
 355         * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
 356         * if setting of OVL_UPPERDATA is visible, then effects of writes
 357         * before that are visible too.
 358         */
 359        smp_rmb();
 360        return true;
 361}
 362
 363void ovl_set_upperdata(struct inode *inode)
 364{
 365        /*
 366         * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
 367         * if OVL_UPPERDATA flag is visible, then effects of write operations
 368         * before it are visible as well.
 369         */
 370        smp_wmb();
 371        ovl_set_flag(OVL_UPPERDATA, inode);
 372}
 373
 374/* Caller should hold ovl_inode->lock */
 375bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
 376{
 377        if (!ovl_open_flags_need_copy_up(flags))
 378                return false;
 379
 380        return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
 381}
 382
 383bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
 384{
 385        if (!ovl_open_flags_need_copy_up(flags))
 386                return false;
 387
 388        return !ovl_has_upperdata(d_inode(dentry));
 389}
 390
 391bool ovl_redirect_dir(struct super_block *sb)
 392{
 393        struct ovl_fs *ofs = sb->s_fs_info;
 394
 395        return ofs->config.redirect_dir && !ofs->noxattr;
 396}
 397
 398const char *ovl_dentry_get_redirect(struct dentry *dentry)
 399{
 400        return OVL_I(d_inode(dentry))->redirect;
 401}
 402
 403void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
 404{
 405        struct ovl_inode *oi = OVL_I(d_inode(dentry));
 406
 407        kfree(oi->redirect);
 408        oi->redirect = redirect;
 409}
 410
 411void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
 412{
 413        struct inode *upperinode = d_inode(upperdentry);
 414
 415        WARN_ON(OVL_I(inode)->__upperdentry);
 416
 417        /*
 418         * Make sure upperdentry is consistent before making it visible
 419         */
 420        smp_wmb();
 421        OVL_I(inode)->__upperdentry = upperdentry;
 422        if (inode_unhashed(inode)) {
 423                inode->i_private = upperinode;
 424                __insert_inode_hash(inode, (unsigned long) upperinode);
 425        }
 426}
 427
 428static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
 429{
 430        struct inode *inode = d_inode(dentry);
 431
 432        WARN_ON(!inode_is_locked(inode));
 433        /*
 434         * Version is used by readdir code to keep cache consistent.  For merge
 435         * dirs all changes need to be noted.  For non-merge dirs, cache only
 436         * contains impure (ones which have been copied up and have origins)
 437         * entries, so only need to note changes to impure entries.
 438         */
 439        if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
 440                OVL_I(inode)->version++;
 441}
 442
 443void ovl_dir_modified(struct dentry *dentry, bool impurity)
 444{
 445        /* Copy mtime/ctime */
 446        ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
 447
 448        ovl_dentry_version_inc(dentry, impurity);
 449}
 450
 451u64 ovl_dentry_version_get(struct dentry *dentry)
 452{
 453        struct inode *inode = d_inode(dentry);
 454
 455        WARN_ON(!inode_is_locked(inode));
 456        return OVL_I(inode)->version;
 457}
 458
 459bool ovl_is_whiteout(struct dentry *dentry)
 460{
 461        struct inode *inode = dentry->d_inode;
 462
 463        return inode && IS_WHITEOUT(inode);
 464}
 465
 466struct file *ovl_path_open(struct path *path, int flags)
 467{
 468        struct inode *inode = d_inode(path->dentry);
 469        int err, acc_mode;
 470
 471        if (flags & ~(O_ACCMODE | O_LARGEFILE))
 472                BUG();
 473
 474        switch (flags & O_ACCMODE) {
 475        case O_RDONLY:
 476                acc_mode = MAY_READ;
 477                break;
 478        case O_WRONLY:
 479                acc_mode = MAY_WRITE;
 480                break;
 481        default:
 482                BUG();
 483        }
 484
 485        err = inode_permission(inode, acc_mode | MAY_OPEN);
 486        if (err)
 487                return ERR_PTR(err);
 488
 489        /* O_NOATIME is an optimization, don't fail if not permitted */
 490        if (inode_owner_or_capable(inode))
 491                flags |= O_NOATIME;
 492
 493        return dentry_open(path, flags, current_cred());
 494}
 495
 496/* Caller should hold ovl_inode->lock */
 497static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
 498{
 499        bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
 500
 501        if (ovl_dentry_upper(dentry) &&
 502            (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
 503            !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
 504                return true;
 505
 506        return false;
 507}
 508
 509bool ovl_already_copied_up(struct dentry *dentry, int flags)
 510{
 511        bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
 512
 513        /*
 514         * Check if copy-up has happened as well as for upper alias (in
 515         * case of hard links) is there.
 516         *
 517         * Both checks are lockless:
 518         *  - false negatives: will recheck under oi->lock
 519         *  - false positives:
 520         *    + ovl_dentry_upper() uses memory barriers to ensure the
 521         *      upper dentry is up-to-date
 522         *    + ovl_dentry_has_upper_alias() relies on locking of
 523         *      upper parent i_rwsem to prevent reordering copy-up
 524         *      with rename.
 525         */
 526        if (ovl_dentry_upper(dentry) &&
 527            (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
 528            !ovl_dentry_needs_data_copy_up(dentry, flags))
 529                return true;
 530
 531        return false;
 532}
 533
 534int ovl_copy_up_start(struct dentry *dentry, int flags)
 535{
 536        struct inode *inode = d_inode(dentry);
 537        int err;
 538
 539        err = ovl_inode_lock_interruptible(inode);
 540        if (!err && ovl_already_copied_up_locked(dentry, flags)) {
 541                err = 1; /* Already copied up */
 542                ovl_inode_unlock(inode);
 543        }
 544
 545        return err;
 546}
 547
 548void ovl_copy_up_end(struct dentry *dentry)
 549{
 550        ovl_inode_unlock(d_inode(dentry));
 551}
 552
 553bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
 554{
 555        int res;
 556
 557        res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
 558
 559        /* Zero size value means "copied up but origin unknown" */
 560        if (res >= 0)
 561                return true;
 562
 563        return false;
 564}
 565
 566bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
 567                         enum ovl_xattr ox)
 568{
 569        int res;
 570        char val;
 571
 572        if (!d_is_dir(dentry))
 573                return false;
 574
 575        res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
 576        if (res == 1 && val == 'y')
 577                return true;
 578
 579        return false;
 580}
 581
 582#define OVL_XATTR_OPAQUE_POSTFIX        "opaque"
 583#define OVL_XATTR_REDIRECT_POSTFIX      "redirect"
 584#define OVL_XATTR_ORIGIN_POSTFIX        "origin"
 585#define OVL_XATTR_IMPURE_POSTFIX        "impure"
 586#define OVL_XATTR_NLINK_POSTFIX         "nlink"
 587#define OVL_XATTR_UPPER_POSTFIX         "upper"
 588#define OVL_XATTR_METACOPY_POSTFIX      "metacopy"
 589
 590#define OVL_XATTR_TAB_ENTRY(x) \
 591        [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
 592                [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
 593
 594const char *const ovl_xattr_table[][2] = {
 595        OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
 596        OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
 597        OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
 598        OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
 599        OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
 600        OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
 601        OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
 602};
 603
 604int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
 605                       enum ovl_xattr ox, const void *value, size_t size,
 606                       int xerr)
 607{
 608        int err;
 609        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 610
 611        if (ofs->noxattr)
 612                return xerr;
 613
 614        err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
 615
 616        if (err == -EOPNOTSUPP) {
 617                pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
 618                ofs->noxattr = true;
 619                return xerr;
 620        }
 621
 622        return err;
 623}
 624
 625int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
 626{
 627        int err;
 628
 629        if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
 630                return 0;
 631
 632        /*
 633         * Do not fail when upper doesn't support xattrs.
 634         * Upper inodes won't have origin nor redirect xattr anyway.
 635         */
 636        err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
 637                                 "y", 1, 0);
 638        if (!err)
 639                ovl_set_flag(OVL_IMPURE, d_inode(dentry));
 640
 641        return err;
 642}
 643
 644void ovl_set_flag(unsigned long flag, struct inode *inode)
 645{
 646        set_bit(flag, &OVL_I(inode)->flags);
 647}
 648
 649void ovl_clear_flag(unsigned long flag, struct inode *inode)
 650{
 651        clear_bit(flag, &OVL_I(inode)->flags);
 652}
 653
 654bool ovl_test_flag(unsigned long flag, struct inode *inode)
 655{
 656        return test_bit(flag, &OVL_I(inode)->flags);
 657}
 658
 659/**
 660 * Caller must hold a reference to inode to prevent it from being freed while
 661 * it is marked inuse.
 662 */
 663bool ovl_inuse_trylock(struct dentry *dentry)
 664{
 665        struct inode *inode = d_inode(dentry);
 666        bool locked = false;
 667
 668        spin_lock(&inode->i_lock);
 669        if (!(inode->i_state & I_OVL_INUSE)) {
 670                inode->i_state |= I_OVL_INUSE;
 671                locked = true;
 672        }
 673        spin_unlock(&inode->i_lock);
 674
 675        return locked;
 676}
 677
 678void ovl_inuse_unlock(struct dentry *dentry)
 679{
 680        if (dentry) {
 681                struct inode *inode = d_inode(dentry);
 682
 683                spin_lock(&inode->i_lock);
 684                WARN_ON(!(inode->i_state & I_OVL_INUSE));
 685                inode->i_state &= ~I_OVL_INUSE;
 686                spin_unlock(&inode->i_lock);
 687        }
 688}
 689
 690bool ovl_is_inuse(struct dentry *dentry)
 691{
 692        struct inode *inode = d_inode(dentry);
 693        bool inuse;
 694
 695        spin_lock(&inode->i_lock);
 696        inuse = (inode->i_state & I_OVL_INUSE);
 697        spin_unlock(&inode->i_lock);
 698
 699        return inuse;
 700}
 701
 702/*
 703 * Does this overlay dentry need to be indexed on copy up?
 704 */
 705bool ovl_need_index(struct dentry *dentry)
 706{
 707        struct dentry *lower = ovl_dentry_lower(dentry);
 708
 709        if (!lower || !ovl_indexdir(dentry->d_sb))
 710                return false;
 711
 712        /* Index all files for NFS export and consistency verification */
 713        if (ovl_index_all(dentry->d_sb))
 714                return true;
 715
 716        /* Index only lower hardlinks on copy up */
 717        if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
 718                return true;
 719
 720        return false;
 721}
 722
 723/* Caller must hold OVL_I(inode)->lock */
 724static void ovl_cleanup_index(struct dentry *dentry)
 725{
 726        struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
 727        struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
 728        struct inode *dir = indexdir->d_inode;
 729        struct dentry *lowerdentry = ovl_dentry_lower(dentry);
 730        struct dentry *upperdentry = ovl_dentry_upper(dentry);
 731        struct dentry *index = NULL;
 732        struct inode *inode;
 733        struct qstr name = { };
 734        int err;
 735
 736        err = ovl_get_index_name(ofs, lowerdentry, &name);
 737        if (err)
 738                goto fail;
 739
 740        inode = d_inode(upperdentry);
 741        if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
 742                pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
 743                                    upperdentry, inode->i_ino, inode->i_nlink);
 744                /*
 745                 * We either have a bug with persistent union nlink or a lower
 746                 * hardlink was added while overlay is mounted. Adding a lower
 747                 * hardlink and then unlinking all overlay hardlinks would drop
 748                 * overlay nlink to zero before all upper inodes are unlinked.
 749                 * As a safety measure, when that situation is detected, set
 750                 * the overlay nlink to the index inode nlink minus one for the
 751                 * index entry itself.
 752                 */
 753                set_nlink(d_inode(dentry), inode->i_nlink - 1);
 754                ovl_set_nlink_upper(dentry);
 755                goto out;
 756        }
 757
 758        inode_lock_nested(dir, I_MUTEX_PARENT);
 759        index = lookup_one_len(name.name, indexdir, name.len);
 760        err = PTR_ERR(index);
 761        if (IS_ERR(index)) {
 762                index = NULL;
 763        } else if (ovl_index_all(dentry->d_sb)) {
 764                /* Whiteout orphan index to block future open by handle */
 765                err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
 766                                               dir, index);
 767        } else {
 768                /* Cleanup orphan index entries */
 769                err = ovl_cleanup(dir, index);
 770        }
 771
 772        inode_unlock(dir);
 773        if (err)
 774                goto fail;
 775
 776out:
 777        kfree(name.name);
 778        dput(index);
 779        return;
 780
 781fail:
 782        pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
 783        goto out;
 784}
 785
 786/*
 787 * Operations that change overlay inode and upper inode nlink need to be
 788 * synchronized with copy up for persistent nlink accounting.
 789 */
 790int ovl_nlink_start(struct dentry *dentry)
 791{
 792        struct inode *inode = d_inode(dentry);
 793        const struct cred *old_cred;
 794        int err;
 795
 796        if (WARN_ON(!inode))
 797                return -ENOENT;
 798
 799        /*
 800         * With inodes index is enabled, we store the union overlay nlink
 801         * in an xattr on the index inode. When whiting out an indexed lower,
 802         * we need to decrement the overlay persistent nlink, but before the
 803         * first copy up, we have no upper index inode to store the xattr.
 804         *
 805         * As a workaround, before whiteout/rename over an indexed lower,
 806         * copy up to create the upper index. Creating the upper index will
 807         * initialize the overlay nlink, so it could be dropped if unlink
 808         * or rename succeeds.
 809         *
 810         * TODO: implement metadata only index copy up when called with
 811         *       ovl_copy_up_flags(dentry, O_PATH).
 812         */
 813        if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
 814                err = ovl_copy_up(dentry);
 815                if (err)
 816                        return err;
 817        }
 818
 819        err = ovl_inode_lock_interruptible(inode);
 820        if (err)
 821                return err;
 822
 823        if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
 824                goto out;
 825
 826        old_cred = ovl_override_creds(dentry->d_sb);
 827        /*
 828         * The overlay inode nlink should be incremented/decremented IFF the
 829         * upper operation succeeds, along with nlink change of upper inode.
 830         * Therefore, before link/unlink/rename, we store the union nlink
 831         * value relative to the upper inode nlink in an upper inode xattr.
 832         */
 833        err = ovl_set_nlink_upper(dentry);
 834        revert_creds(old_cred);
 835
 836out:
 837        if (err)
 838                ovl_inode_unlock(inode);
 839
 840        return err;
 841}
 842
 843void ovl_nlink_end(struct dentry *dentry)
 844{
 845        struct inode *inode = d_inode(dentry);
 846
 847        if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
 848                const struct cred *old_cred;
 849
 850                old_cred = ovl_override_creds(dentry->d_sb);
 851                ovl_cleanup_index(dentry);
 852                revert_creds(old_cred);
 853        }
 854
 855        ovl_inode_unlock(inode);
 856}
 857
 858int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
 859{
 860        /* Workdir should not be the same as upperdir */
 861        if (workdir == upperdir)
 862                goto err;
 863
 864        /* Workdir should not be subdir of upperdir and vice versa */
 865        if (lock_rename(workdir, upperdir) != NULL)
 866                goto err_unlock;
 867
 868        return 0;
 869
 870err_unlock:
 871        unlock_rename(workdir, upperdir);
 872err:
 873        pr_err("failed to lock workdir+upperdir\n");
 874        return -EIO;
 875}
 876
 877/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
 878int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
 879{
 880        int res;
 881
 882        /* Only regular files can have metacopy xattr */
 883        if (!S_ISREG(d_inode(dentry)->i_mode))
 884                return 0;
 885
 886        res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
 887        if (res < 0) {
 888                if (res == -ENODATA || res == -EOPNOTSUPP)
 889                        return 0;
 890                /*
 891                 * getxattr on user.* may fail with EACCES in case there's no
 892                 * read permission on the inode.  Not much we can do, other than
 893                 * tell the caller that this is not a metacopy inode.
 894                 */
 895                if (ofs->config.userxattr && res == -EACCES)
 896                        return 0;
 897                goto out;
 898        }
 899
 900        return 1;
 901out:
 902        pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
 903        return res;
 904}
 905
 906bool ovl_is_metacopy_dentry(struct dentry *dentry)
 907{
 908        struct ovl_entry *oe = dentry->d_fsdata;
 909
 910        if (!d_is_reg(dentry))
 911                return false;
 912
 913        if (ovl_dentry_upper(dentry)) {
 914                if (!ovl_has_upperdata(d_inode(dentry)))
 915                        return true;
 916                return false;
 917        }
 918
 919        return (oe->numlower > 1);
 920}
 921
 922char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
 923                             int padding)
 924{
 925        int res;
 926        char *s, *next, *buf = NULL;
 927
 928        res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
 929        if (res == -ENODATA || res == -EOPNOTSUPP)
 930                return NULL;
 931        if (res < 0)
 932                goto fail;
 933        if (res == 0)
 934                goto invalid;
 935
 936        buf = kzalloc(res + padding + 1, GFP_KERNEL);
 937        if (!buf)
 938                return ERR_PTR(-ENOMEM);
 939
 940        res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
 941        if (res < 0)
 942                goto fail;
 943        if (res == 0)
 944                goto invalid;
 945
 946        if (buf[0] == '/') {
 947                for (s = buf; *s++ == '/'; s = next) {
 948                        next = strchrnul(s, '/');
 949                        if (s == next)
 950                                goto invalid;
 951                }
 952        } else {
 953                if (strchr(buf, '/') != NULL)
 954                        goto invalid;
 955        }
 956
 957        return buf;
 958invalid:
 959        pr_warn_ratelimited("invalid redirect (%s)\n", buf);
 960        res = -EINVAL;
 961        goto err_free;
 962fail:
 963        pr_warn_ratelimited("failed to get redirect (%i)\n", res);
 964err_free:
 965        kfree(buf);
 966        return ERR_PTR(res);
 967}
 968