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/xattr.h>
  14#include <linux/cred.h>
  15#include <linux/sched.h>
  16#include <linux/exportfs.h>
  17#include <linux/uuid.h>
  18#include <linux/namei.h>
  19#include <linux/ratelimit.h>
  20#include "overlayfs.h"
  21#include "ovl_entry.h"
  22
  23int ovl_want_write(struct dentry *dentry)
  24{
  25        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  26        return mnt_want_write(ofs->upper_mnt);
  27}
  28
  29void ovl_drop_write(struct dentry *dentry)
  30{
  31        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  32        mnt_drop_write(ofs->upper_mnt);
  33}
  34
  35struct dentry *ovl_workdir(struct dentry *dentry)
  36{
  37        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  38        return ofs->workdir;
  39}
  40
  41const struct cred *ovl_override_creds(struct super_block *sb)
  42{
  43        struct ovl_fs *ofs = sb->s_fs_info;
  44
  45        return override_creds(ofs->creator_cred);
  46}
  47
  48struct super_block *ovl_same_sb(struct super_block *sb)
  49{
  50        struct ovl_fs *ofs = sb->s_fs_info;
  51
  52        return ofs->same_sb;
  53}
  54
  55bool ovl_can_decode_fh(struct super_block *sb)
  56{
  57        uuid_be *uuid = (uuid_be *) &sb->s_uuid;
  58
  59        return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
  60                uuid_be_cmp(*uuid, NULL_UUID_BE));
  61}
  62
  63struct dentry *ovl_indexdir(struct super_block *sb)
  64{
  65        struct ovl_fs *ofs = sb->s_fs_info;
  66
  67        return ofs->indexdir;
  68}
  69
  70struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  71{
  72        size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  73        struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  74
  75        if (oe)
  76                oe->numlower = numlower;
  77
  78        return oe;
  79}
  80
  81bool ovl_dentry_remote(struct dentry *dentry)
  82{
  83        return dentry->d_flags &
  84                (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
  85                 DCACHE_OP_REAL);
  86}
  87
  88bool ovl_dentry_weird(struct dentry *dentry)
  89{
  90        return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  91                                  DCACHE_MANAGE_TRANSIT |
  92                                  DCACHE_OP_HASH |
  93                                  DCACHE_OP_COMPARE);
  94}
  95
  96enum ovl_path_type ovl_path_type(struct dentry *dentry)
  97{
  98        struct ovl_entry *oe = dentry->d_fsdata;
  99        enum ovl_path_type type = 0;
 100
 101        if (ovl_dentry_upper(dentry)) {
 102                type = __OVL_PATH_UPPER;
 103
 104                /*
 105                 * Non-dir dentry can hold lower dentry of its copy up origin.
 106                 */
 107                if (oe->numlower) {
 108                        type |= __OVL_PATH_ORIGIN;
 109                        if (d_is_dir(dentry))
 110                                type |= __OVL_PATH_MERGE;
 111                }
 112        } else {
 113                if (oe->numlower > 1)
 114                        type |= __OVL_PATH_MERGE;
 115        }
 116        return type;
 117}
 118
 119void ovl_path_upper(struct dentry *dentry, struct path *path)
 120{
 121        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 122
 123        path->mnt = ofs->upper_mnt;
 124        path->dentry = ovl_dentry_upper(dentry);
 125}
 126
 127void ovl_path_lower(struct dentry *dentry, struct path *path)
 128{
 129        struct ovl_entry *oe = dentry->d_fsdata;
 130
 131        *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
 132}
 133
 134enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
 135{
 136        enum ovl_path_type type = ovl_path_type(dentry);
 137
 138        if (!OVL_TYPE_UPPER(type))
 139                ovl_path_lower(dentry, path);
 140        else
 141                ovl_path_upper(dentry, path);
 142
 143        return type;
 144}
 145
 146struct dentry *ovl_dentry_upper(struct dentry *dentry)
 147{
 148        return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
 149}
 150
 151struct dentry *ovl_dentry_lower(struct dentry *dentry)
 152{
 153        struct ovl_entry *oe = dentry->d_fsdata;
 154
 155        return oe->numlower ? oe->lowerstack[0].dentry : NULL;
 156}
 157
 158struct dentry *ovl_dentry_real(struct dentry *dentry)
 159{
 160        return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
 161}
 162
 163struct dentry *ovl_i_dentry_upper(struct inode *inode)
 164{
 165        return ovl_upperdentry_dereference(OVL_I(inode));
 166}
 167
 168struct inode *ovl_inode_upper(struct inode *inode)
 169{
 170        struct dentry *upperdentry = ovl_i_dentry_upper(inode);
 171
 172        return upperdentry ? d_inode(upperdentry) : NULL;
 173}
 174
 175struct inode *ovl_inode_lower(struct inode *inode)
 176{
 177        return OVL_I(inode)->lower;
 178}
 179
 180struct inode *ovl_inode_real(struct inode *inode)
 181{
 182        return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
 183}
 184
 185
 186struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
 187{
 188        return OVL_I(d_inode(dentry))->cache;
 189}
 190
 191void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
 192{
 193        OVL_I(d_inode(dentry))->cache = cache;
 194}
 195
 196bool ovl_dentry_is_opaque(struct dentry *dentry)
 197{
 198        struct ovl_entry *oe = dentry->d_fsdata;
 199        return oe->opaque;
 200}
 201
 202bool ovl_dentry_is_whiteout(struct dentry *dentry)
 203{
 204        return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
 205}
 206
 207void ovl_dentry_set_opaque(struct dentry *dentry)
 208{
 209        struct ovl_entry *oe = dentry->d_fsdata;
 210
 211        oe->opaque = true;
 212}
 213
 214/*
 215 * For hard links it's possible for ovl_dentry_upper() to return positive, while
 216 * there's no actual upper alias for the inode.  Copy up code needs to know
 217 * about the existence of the upper alias, so it can't use ovl_dentry_upper().
 218 */
 219bool ovl_dentry_has_upper_alias(struct dentry *dentry)
 220{
 221        struct ovl_entry *oe = dentry->d_fsdata;
 222
 223        return oe->has_upper;
 224}
 225
 226void ovl_dentry_set_upper_alias(struct dentry *dentry)
 227{
 228        struct ovl_entry *oe = dentry->d_fsdata;
 229
 230        oe->has_upper = true;
 231}
 232
 233bool ovl_redirect_dir(struct super_block *sb)
 234{
 235        struct ovl_fs *ofs = sb->s_fs_info;
 236
 237        return ofs->config.redirect_dir && !ofs->noxattr;
 238}
 239
 240const char *ovl_dentry_get_redirect(struct dentry *dentry)
 241{
 242        return OVL_I(d_inode(dentry))->redirect;
 243}
 244
 245void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
 246{
 247        struct ovl_inode *oi = OVL_I(d_inode(dentry));
 248
 249        kfree(oi->redirect);
 250        oi->redirect = redirect;
 251}
 252
 253void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
 254                    struct dentry *lowerdentry)
 255{
 256        if (upperdentry)
 257                OVL_I(inode)->__upperdentry = upperdentry;
 258        if (lowerdentry)
 259                OVL_I(inode)->lower = d_inode(lowerdentry);
 260
 261        ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
 262}
 263
 264void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
 265{
 266        struct inode *upperinode = d_inode(upperdentry);
 267
 268        WARN_ON(OVL_I(inode)->__upperdentry);
 269
 270        /*
 271         * Make sure upperdentry is consistent before making it visible
 272         */
 273        smp_wmb();
 274        OVL_I(inode)->__upperdentry = upperdentry;
 275        if (!S_ISDIR(upperinode->i_mode) && inode_unhashed(inode)) {
 276                inode->i_private = upperinode;
 277                __insert_inode_hash(inode, (unsigned long) upperinode);
 278        }
 279}
 280
 281void ovl_dentry_version_inc(struct dentry *dentry)
 282{
 283        struct inode *inode = d_inode(dentry);
 284
 285        WARN_ON(!inode_is_locked(inode));
 286        OVL_I(inode)->version++;
 287}
 288
 289u64 ovl_dentry_version_get(struct dentry *dentry)
 290{
 291        struct inode *inode = d_inode(dentry);
 292
 293        WARN_ON(!inode_is_locked(inode));
 294        return OVL_I(inode)->version;
 295}
 296
 297bool ovl_is_whiteout(struct dentry *dentry)
 298{
 299        struct inode *inode = dentry->d_inode;
 300
 301        return inode && IS_WHITEOUT(inode);
 302}
 303
 304struct file *ovl_path_open(struct path *path, int flags)
 305{
 306        return dentry_open(path, flags | O_NOATIME, current_cred());
 307}
 308
 309int ovl_copy_up_start(struct dentry *dentry)
 310{
 311        struct ovl_inode *oi = OVL_I(d_inode(dentry));
 312        int err;
 313
 314        err = mutex_lock_interruptible(&oi->lock);
 315        if (!err && ovl_dentry_has_upper_alias(dentry)) {
 316                err = 1; /* Already copied up */
 317                mutex_unlock(&oi->lock);
 318        }
 319
 320        return err;
 321}
 322
 323void ovl_copy_up_end(struct dentry *dentry)
 324{
 325        mutex_unlock(&OVL_I(d_inode(dentry))->lock);
 326}
 327
 328bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
 329{
 330        int res;
 331        char val;
 332
 333        if (!d_is_dir(dentry))
 334                return false;
 335
 336        res = vfs_getxattr(dentry, name, &val, 1);
 337        if (res == 1 && val == 'y')
 338                return true;
 339
 340        return false;
 341}
 342
 343int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
 344                       const char *name, const void *value, size_t size,
 345                       int xerr)
 346{
 347        int err;
 348        struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 349
 350        if (ofs->noxattr)
 351                return xerr;
 352
 353        err = ovl_do_setxattr(upperdentry, name, value, size, 0);
 354
 355        if (err == -EOPNOTSUPP) {
 356                pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
 357                ofs->noxattr = true;
 358                return xerr;
 359        }
 360
 361        return err;
 362}
 363
 364int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
 365{
 366        int err;
 367
 368        if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
 369                return 0;
 370
 371        /*
 372         * Do not fail when upper doesn't support xattrs.
 373         * Upper inodes won't have origin nor redirect xattr anyway.
 374         */
 375        err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
 376                                 "y", 1, 0);
 377        if (!err)
 378                ovl_set_flag(OVL_IMPURE, d_inode(dentry));
 379
 380        return err;
 381}
 382
 383void ovl_set_flag(unsigned long flag, struct inode *inode)
 384{
 385        set_bit(flag, &OVL_I(inode)->flags);
 386}
 387
 388bool ovl_test_flag(unsigned long flag, struct inode *inode)
 389{
 390        return test_bit(flag, &OVL_I(inode)->flags);
 391}
 392
 393/**
 394 * Caller must hold a reference to inode to prevent it from being freed while
 395 * it is marked inuse.
 396 */
 397bool ovl_inuse_trylock(struct dentry *dentry)
 398{
 399        struct inode *inode = d_inode(dentry);
 400        bool locked = false;
 401
 402        spin_lock(&inode->i_lock);
 403        if (!(inode->i_state & I_OVL_INUSE)) {
 404                inode->i_state |= I_OVL_INUSE;
 405                locked = true;
 406        }
 407        spin_unlock(&inode->i_lock);
 408
 409        return locked;
 410}
 411
 412void ovl_inuse_unlock(struct dentry *dentry)
 413{
 414        if (dentry) {
 415                struct inode *inode = d_inode(dentry);
 416
 417                spin_lock(&inode->i_lock);
 418                WARN_ON(!(inode->i_state & I_OVL_INUSE));
 419                inode->i_state &= ~I_OVL_INUSE;
 420                spin_unlock(&inode->i_lock);
 421        }
 422}
 423
 424/* Caller must hold OVL_I(inode)->lock */
 425static void ovl_cleanup_index(struct dentry *dentry)
 426{
 427        struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
 428        struct dentry *lowerdentry = ovl_dentry_lower(dentry);
 429        struct dentry *upperdentry = ovl_dentry_upper(dentry);
 430        struct dentry *index = NULL;
 431        struct inode *inode;
 432        struct qstr name;
 433        int err;
 434
 435        err = ovl_get_index_name(lowerdentry, &name);
 436        if (err)
 437                goto fail;
 438
 439        inode = d_inode(upperdentry);
 440        if (inode->i_nlink != 1) {
 441                pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
 442                                    upperdentry, inode->i_ino, inode->i_nlink);
 443                /*
 444                 * We either have a bug with persistent union nlink or a lower
 445                 * hardlink was added while overlay is mounted. Adding a lower
 446                 * hardlink and then unlinking all overlay hardlinks would drop
 447                 * overlay nlink to zero before all upper inodes are unlinked.
 448                 * As a safety measure, when that situation is detected, set
 449                 * the overlay nlink to the index inode nlink minus one for the
 450                 * index entry itself.
 451                 */
 452                set_nlink(d_inode(dentry), inode->i_nlink - 1);
 453                ovl_set_nlink_upper(dentry);
 454                goto out;
 455        }
 456
 457        inode_lock_nested(dir, I_MUTEX_PARENT);
 458        /* TODO: whiteout instead of cleanup to block future open by handle */
 459        index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
 460        err = PTR_ERR(index);
 461        if (!IS_ERR(index))
 462                err = ovl_cleanup(dir, index);
 463        else
 464                index = NULL;
 465
 466        inode_unlock(dir);
 467        if (err)
 468                goto fail;
 469
 470out:
 471        dput(index);
 472        return;
 473
 474fail:
 475        pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
 476        goto out;
 477}
 478
 479/*
 480 * Operations that change overlay inode and upper inode nlink need to be
 481 * synchronized with copy up for persistent nlink accounting.
 482 */
 483int ovl_nlink_start(struct dentry *dentry, bool *locked)
 484{
 485        struct ovl_inode *oi = OVL_I(d_inode(dentry));
 486        const struct cred *old_cred;
 487        int err;
 488
 489        if (!d_inode(dentry) || d_is_dir(dentry))
 490                return 0;
 491
 492        /*
 493         * With inodes index is enabled, we store the union overlay nlink
 494         * in an xattr on the index inode. When whiting out lower hardlinks
 495         * we need to decrement the overlay persistent nlink, but before the
 496         * first copy up, we have no upper index inode to store the xattr.
 497         *
 498         * As a workaround, before whiteout/rename over of a lower hardlink,
 499         * copy up to create the upper index. Creating the upper index will
 500         * initialize the overlay nlink, so it could be dropped if unlink
 501         * or rename succeeds.
 502         *
 503         * TODO: implement metadata only index copy up when called with
 504         *       ovl_copy_up_flags(dentry, O_PATH).
 505         */
 506        if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
 507            d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
 508                err = ovl_copy_up(dentry);
 509                if (err)
 510                        return err;
 511        }
 512
 513        err = mutex_lock_interruptible(&oi->lock);
 514        if (err)
 515                return err;
 516
 517        if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
 518                goto out;
 519
 520        old_cred = ovl_override_creds(dentry->d_sb);
 521        /*
 522         * The overlay inode nlink should be incremented/decremented IFF the
 523         * upper operation succeeds, along with nlink change of upper inode.
 524         * Therefore, before link/unlink/rename, we store the union nlink
 525         * value relative to the upper inode nlink in an upper inode xattr.
 526         */
 527        err = ovl_set_nlink_upper(dentry);
 528        revert_creds(old_cred);
 529
 530out:
 531        if (err)
 532                mutex_unlock(&oi->lock);
 533        else
 534                *locked = true;
 535
 536        return err;
 537}
 538
 539void ovl_nlink_end(struct dentry *dentry, bool locked)
 540{
 541        if (locked) {
 542                if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
 543                    d_inode(dentry)->i_nlink == 0) {
 544                        const struct cred *old_cred;
 545
 546                        old_cred = ovl_override_creds(dentry->d_sb);
 547                        ovl_cleanup_index(dentry);
 548                        revert_creds(old_cred);
 549                }
 550
 551                mutex_unlock(&OVL_I(d_inode(dentry))->lock);
 552        }
 553}
 554
 555int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
 556{
 557        /* Workdir should not be the same as upperdir */
 558        if (workdir == upperdir)
 559                goto err;
 560
 561        /* Workdir should not be subdir of upperdir and vice versa */
 562        if (lock_rename(workdir, upperdir) != NULL)
 563                goto err_unlock;
 564
 565        return 0;
 566
 567err_unlock:
 568        unlock_rename(workdir, upperdir);
 569err:
 570        pr_err("overlayfs: failed to lock workdir+upperdir\n");
 571        return -EIO;
 572}
 573