1bbb1e54dSMiklos Szeredi /* 2bbb1e54dSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 3bbb1e54dSMiklos Szeredi * Copyright (C) 2016 Red Hat, Inc. 4bbb1e54dSMiklos Szeredi * 5bbb1e54dSMiklos Szeredi * This program is free software; you can redistribute it and/or modify it 6bbb1e54dSMiklos Szeredi * under the terms of the GNU General Public License version 2 as published by 7bbb1e54dSMiklos Szeredi * the Free Software Foundation. 8bbb1e54dSMiklos Szeredi */ 9bbb1e54dSMiklos Szeredi 10bbb1e54dSMiklos Szeredi #include <linux/fs.h> 11bbb1e54dSMiklos Szeredi #include <linux/mount.h> 12bbb1e54dSMiklos Szeredi #include <linux/slab.h> 135b825c3aSIngo Molnar #include <linux/cred.h> 14bbb1e54dSMiklos Szeredi #include <linux/xattr.h> 1502bcd157SAmir Goldstein #include <linux/exportfs.h> 1602bcd157SAmir Goldstein #include <linux/uuid.h> 17caf70cb2SAmir Goldstein #include <linux/namei.h> 18caf70cb2SAmir Goldstein #include <linux/ratelimit.h> 19bbb1e54dSMiklos Szeredi #include "overlayfs.h" 20bbb1e54dSMiklos Szeredi 21bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry) 22bbb1e54dSMiklos Szeredi { 23bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 24bbb1e54dSMiklos Szeredi return mnt_want_write(ofs->upper_mnt); 25bbb1e54dSMiklos Szeredi } 26bbb1e54dSMiklos Szeredi 27bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry) 28bbb1e54dSMiklos Szeredi { 29bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 30bbb1e54dSMiklos Szeredi mnt_drop_write(ofs->upper_mnt); 31bbb1e54dSMiklos Szeredi } 32bbb1e54dSMiklos Szeredi 33bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry) 34bbb1e54dSMiklos Szeredi { 35bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 36bbb1e54dSMiklos Szeredi return ofs->workdir; 37bbb1e54dSMiklos Szeredi } 38bbb1e54dSMiklos Szeredi 39bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb) 40bbb1e54dSMiklos Szeredi { 41bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 42bbb1e54dSMiklos Szeredi 43bbb1e54dSMiklos Szeredi return override_creds(ofs->creator_cred); 44bbb1e54dSMiklos Szeredi } 45bbb1e54dSMiklos Szeredi 467bcd74b9SAmir Goldstein struct super_block *ovl_same_sb(struct super_block *sb) 477bcd74b9SAmir Goldstein { 487bcd74b9SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 497bcd74b9SAmir Goldstein 505148626bSAmir Goldstein if (!ofs->numlowerfs) 515148626bSAmir Goldstein return ofs->upper_mnt->mnt_sb; 525148626bSAmir Goldstein else if (ofs->numlowerfs == 1 && !ofs->upper_mnt) 535148626bSAmir Goldstein return ofs->lower_fs[0].sb; 545148626bSAmir Goldstein else 555148626bSAmir Goldstein return NULL; 567bcd74b9SAmir Goldstein } 577bcd74b9SAmir Goldstein 58e487d889SAmir Goldstein /* 59e487d889SAmir Goldstein * Check if underlying fs supports file handles and try to determine encoding 60e487d889SAmir Goldstein * type, in order to deduce maximum inode number used by fs. 61e487d889SAmir Goldstein * 62e487d889SAmir Goldstein * Return 0 if file handles are not supported. 63e487d889SAmir Goldstein * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. 64e487d889SAmir Goldstein * Return -1 if fs uses a non default encoding with unknown inode size. 65e487d889SAmir Goldstein */ 66e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb) 6702bcd157SAmir Goldstein { 689df085f3SAmir Goldstein if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry) 69e487d889SAmir Goldstein return 0; 70e487d889SAmir Goldstein 71e487d889SAmir Goldstein return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; 7202bcd157SAmir Goldstein } 7302bcd157SAmir Goldstein 7402bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb) 7502bcd157SAmir Goldstein { 7602bcd157SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 7702bcd157SAmir Goldstein 7802bcd157SAmir Goldstein return ofs->indexdir; 7902bcd157SAmir Goldstein } 8002bcd157SAmir Goldstein 81f168f109SAmir Goldstein /* Index all files on copy up. For now only enabled for NFS export */ 82f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb) 83f168f109SAmir Goldstein { 84f168f109SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 85f168f109SAmir Goldstein 86f168f109SAmir Goldstein return ofs->config.nfs_export && ofs->config.index; 87f168f109SAmir Goldstein } 88f168f109SAmir Goldstein 89f168f109SAmir Goldstein /* Verify lower origin on lookup. For now only enabled for NFS export */ 90f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb) 91f168f109SAmir Goldstein { 92f168f109SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 93f168f109SAmir Goldstein 94f168f109SAmir Goldstein return ofs->config.nfs_export && ofs->config.index; 95f168f109SAmir Goldstein } 96f168f109SAmir Goldstein 97bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 98bbb1e54dSMiklos Szeredi { 99bbb1e54dSMiklos Szeredi size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 100bbb1e54dSMiklos Szeredi struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 101bbb1e54dSMiklos Szeredi 102bbb1e54dSMiklos Szeredi if (oe) 103bbb1e54dSMiklos Szeredi oe->numlower = numlower; 104bbb1e54dSMiklos Szeredi 105bbb1e54dSMiklos Szeredi return oe; 106bbb1e54dSMiklos Szeredi } 107bbb1e54dSMiklos Szeredi 108bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry) 109bbb1e54dSMiklos Szeredi { 110bbb1e54dSMiklos Szeredi return dentry->d_flags & 111bbb1e54dSMiklos Szeredi (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | 112bbb1e54dSMiklos Szeredi DCACHE_OP_REAL); 113bbb1e54dSMiklos Szeredi } 114bbb1e54dSMiklos Szeredi 115bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry) 116bbb1e54dSMiklos Szeredi { 117bbb1e54dSMiklos Szeredi return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 118bbb1e54dSMiklos Szeredi DCACHE_MANAGE_TRANSIT | 119bbb1e54dSMiklos Szeredi DCACHE_OP_HASH | 120bbb1e54dSMiklos Szeredi DCACHE_OP_COMPARE); 121bbb1e54dSMiklos Szeredi } 122bbb1e54dSMiklos Szeredi 123bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry) 124bbb1e54dSMiklos Szeredi { 125bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 126bbb1e54dSMiklos Szeredi enum ovl_path_type type = 0; 127bbb1e54dSMiklos Szeredi 12809d8b586SMiklos Szeredi if (ovl_dentry_upper(dentry)) { 129bbb1e54dSMiklos Szeredi type = __OVL_PATH_UPPER; 130bbb1e54dSMiklos Szeredi 131bbb1e54dSMiklos Szeredi /* 13259548503SAmir Goldstein * Non-dir dentry can hold lower dentry of its copy up origin. 133bbb1e54dSMiklos Szeredi */ 13459548503SAmir Goldstein if (oe->numlower) { 13560124877SVivek Goyal if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) 13659548503SAmir Goldstein type |= __OVL_PATH_ORIGIN; 1370b17c28aSVivek Goyal if (d_is_dir(dentry) || 1380b17c28aSVivek Goyal !ovl_has_upperdata(d_inode(dentry))) 139bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 14059548503SAmir Goldstein } 141bbb1e54dSMiklos Szeredi } else { 142bbb1e54dSMiklos Szeredi if (oe->numlower > 1) 143bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 144bbb1e54dSMiklos Szeredi } 145bbb1e54dSMiklos Szeredi return type; 146bbb1e54dSMiklos Szeredi } 147bbb1e54dSMiklos Szeredi 148bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path) 149bbb1e54dSMiklos Szeredi { 150bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 151bbb1e54dSMiklos Szeredi 152bbb1e54dSMiklos Szeredi path->mnt = ofs->upper_mnt; 15309d8b586SMiklos Szeredi path->dentry = ovl_dentry_upper(dentry); 154bbb1e54dSMiklos Szeredi } 155bbb1e54dSMiklos Szeredi 156bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path) 157bbb1e54dSMiklos Szeredi { 158bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 159bbb1e54dSMiklos Szeredi 160b9343632SChandan Rajendra if (oe->numlower) { 161b9343632SChandan Rajendra path->mnt = oe->lowerstack[0].layer->mnt; 162b9343632SChandan Rajendra path->dentry = oe->lowerstack[0].dentry; 163b9343632SChandan Rajendra } else { 164b9343632SChandan Rajendra *path = (struct path) { }; 165b9343632SChandan Rajendra } 166bbb1e54dSMiklos Szeredi } 167bbb1e54dSMiklos Szeredi 1684f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path) 1694f93b426SVivek Goyal { 1704f93b426SVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 1714f93b426SVivek Goyal 1724f93b426SVivek Goyal if (oe->numlower) { 1734f93b426SVivek Goyal path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt; 1744f93b426SVivek Goyal path->dentry = oe->lowerstack[oe->numlower - 1].dentry; 1754f93b426SVivek Goyal } else { 1764f93b426SVivek Goyal *path = (struct path) { }; 1774f93b426SVivek Goyal } 1784f93b426SVivek Goyal } 1794f93b426SVivek Goyal 180bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 181bbb1e54dSMiklos Szeredi { 182bbb1e54dSMiklos Szeredi enum ovl_path_type type = ovl_path_type(dentry); 183bbb1e54dSMiklos Szeredi 184bbb1e54dSMiklos Szeredi if (!OVL_TYPE_UPPER(type)) 185bbb1e54dSMiklos Szeredi ovl_path_lower(dentry, path); 186bbb1e54dSMiklos Szeredi else 187bbb1e54dSMiklos Szeredi ovl_path_upper(dentry, path); 188bbb1e54dSMiklos Szeredi 189bbb1e54dSMiklos Szeredi return type; 190bbb1e54dSMiklos Szeredi } 191bbb1e54dSMiklos Szeredi 192bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry) 193bbb1e54dSMiklos Szeredi { 19409d8b586SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); 195bbb1e54dSMiklos Szeredi } 196bbb1e54dSMiklos Szeredi 197bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry) 198bbb1e54dSMiklos Szeredi { 199bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 200bbb1e54dSMiklos Szeredi 20109d8b586SMiklos Szeredi return oe->numlower ? oe->lowerstack[0].dentry : NULL; 202bbb1e54dSMiklos Szeredi } 203bbb1e54dSMiklos Szeredi 204da309e8cSAmir Goldstein struct ovl_layer *ovl_layer_lower(struct dentry *dentry) 205da309e8cSAmir Goldstein { 206da309e8cSAmir Goldstein struct ovl_entry *oe = dentry->d_fsdata; 207da309e8cSAmir Goldstein 208da309e8cSAmir Goldstein return oe->numlower ? oe->lowerstack[0].layer : NULL; 209da309e8cSAmir Goldstein } 210da309e8cSAmir Goldstein 211647d253fSVivek Goyal /* 212647d253fSVivek Goyal * ovl_dentry_lower() could return either a data dentry or metacopy dentry 213647d253fSVivek Goyal * dependig on what is stored in lowerstack[0]. At times we need to find 214647d253fSVivek Goyal * lower dentry which has data (and not metacopy dentry). This helper 215647d253fSVivek Goyal * returns the lower data dentry. 216647d253fSVivek Goyal */ 217647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) 218647d253fSVivek Goyal { 219647d253fSVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 220647d253fSVivek Goyal 221647d253fSVivek Goyal return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL; 222647d253fSVivek Goyal } 223647d253fSVivek Goyal 224bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry) 225bbb1e54dSMiklos Szeredi { 22609d8b586SMiklos Szeredi return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 227bbb1e54dSMiklos Szeredi } 228bbb1e54dSMiklos Szeredi 2291d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode) 2301d88f183SMiklos Szeredi { 2311d88f183SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(inode)); 2321d88f183SMiklos Szeredi } 2331d88f183SMiklos Szeredi 23409d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode) 23525b7713aSMiklos Szeredi { 2361d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 23725b7713aSMiklos Szeredi 23809d8b586SMiklos Szeredi return upperdentry ? d_inode(upperdentry) : NULL; 23925b7713aSMiklos Szeredi } 24025b7713aSMiklos Szeredi 24109d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode) 24209d8b586SMiklos Szeredi { 24309d8b586SMiklos Szeredi return OVL_I(inode)->lower; 24409d8b586SMiklos Szeredi } 24509d8b586SMiklos Szeredi 24609d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode) 24709d8b586SMiklos Szeredi { 24809d8b586SMiklos Szeredi return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 24909d8b586SMiklos Szeredi } 25009d8b586SMiklos Szeredi 2512664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */ 2522664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode) 2532664bd08SVivek Goyal { 2542664bd08SVivek Goyal if (WARN_ON(!S_ISREG(inode->i_mode))) 2552664bd08SVivek Goyal return NULL; 2562664bd08SVivek Goyal 2572664bd08SVivek Goyal return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); 2582664bd08SVivek Goyal } 25909d8b586SMiklos Szeredi 2604823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */ 2614823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode) 2624823d49cSVivek Goyal { 2634823d49cSVivek Goyal struct inode *upperinode; 2644823d49cSVivek Goyal 2654823d49cSVivek Goyal upperinode = ovl_inode_upper(inode); 2664823d49cSVivek Goyal if (upperinode && ovl_has_upperdata(inode)) 2674823d49cSVivek Goyal return upperinode; 2684823d49cSVivek Goyal 2694823d49cSVivek Goyal return ovl_inode_lowerdata(inode); 2704823d49cSVivek Goyal } 2714823d49cSVivek Goyal 2724edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 273bbb1e54dSMiklos Szeredi { 2744edb83bbSMiklos Szeredi return OVL_I(inode)->cache; 275bbb1e54dSMiklos Szeredi } 276bbb1e54dSMiklos Szeredi 2774edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 278bbb1e54dSMiklos Szeredi { 2794edb83bbSMiklos Szeredi OVL_I(inode)->cache = cache; 280bbb1e54dSMiklos Szeredi } 281bbb1e54dSMiklos Szeredi 282c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 283c62520a8SAmir Goldstein { 284c62520a8SAmir Goldstein set_bit(flag, &OVL_E(dentry)->flags); 285c62520a8SAmir Goldstein } 286c62520a8SAmir Goldstein 287c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 288c62520a8SAmir Goldstein { 289c62520a8SAmir Goldstein clear_bit(flag, &OVL_E(dentry)->flags); 290c62520a8SAmir Goldstein } 291c62520a8SAmir Goldstein 292c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 293c62520a8SAmir Goldstein { 294c62520a8SAmir Goldstein return test_bit(flag, &OVL_E(dentry)->flags); 295c62520a8SAmir Goldstein } 296c62520a8SAmir Goldstein 297bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry) 298bbb1e54dSMiklos Szeredi { 299c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 300bbb1e54dSMiklos Szeredi } 301bbb1e54dSMiklos Szeredi 302bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry) 303bbb1e54dSMiklos Szeredi { 304bbb1e54dSMiklos Szeredi return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 305bbb1e54dSMiklos Szeredi } 306bbb1e54dSMiklos Szeredi 3075cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry) 308bbb1e54dSMiklos Szeredi { 309c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 310bbb1e54dSMiklos Szeredi } 311bbb1e54dSMiklos Szeredi 31255acc661SMiklos Szeredi /* 313aa3ff3c1SAmir Goldstein * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 314aa3ff3c1SAmir Goldstein * to return positive, while there's no actual upper alias for the inode. 315aa3ff3c1SAmir Goldstein * Copy up code needs to know about the existence of the upper alias, so it 316aa3ff3c1SAmir Goldstein * can't use ovl_dentry_upper(). 31755acc661SMiklos Szeredi */ 31855acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry) 31955acc661SMiklos Szeredi { 320c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 32155acc661SMiklos Szeredi } 32255acc661SMiklos Szeredi 32355acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry) 32455acc661SMiklos Szeredi { 325c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 32655acc661SMiklos Szeredi } 32755acc661SMiklos Szeredi 3280c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode) 3290c288874SVivek Goyal { 3300c288874SVivek Goyal if (!S_ISREG(inode->i_mode)) 3310c288874SVivek Goyal return false; 3320c288874SVivek Goyal 3330c288874SVivek Goyal if (!ovl_inode_lower(inode)) 3340c288874SVivek Goyal return false; 3350c288874SVivek Goyal 3360c288874SVivek Goyal return true; 3370c288874SVivek Goyal } 3380c288874SVivek Goyal 3390c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode) 3400c288874SVivek Goyal { 3410c288874SVivek Goyal if (!ovl_should_check_upperdata(inode)) 3420c288874SVivek Goyal return true; 3430c288874SVivek Goyal 3440c288874SVivek Goyal if (!ovl_test_flag(OVL_UPPERDATA, inode)) 3450c288874SVivek Goyal return false; 3460c288874SVivek Goyal /* 3470c288874SVivek Goyal * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 3480c288874SVivek Goyal * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 3490c288874SVivek Goyal * if setting of OVL_UPPERDATA is visible, then effects of writes 3500c288874SVivek Goyal * before that are visible too. 3510c288874SVivek Goyal */ 3520c288874SVivek Goyal smp_rmb(); 3530c288874SVivek Goyal return true; 3540c288874SVivek Goyal } 3550c288874SVivek Goyal 3560c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode) 3570c288874SVivek Goyal { 3580c288874SVivek Goyal /* 3590c288874SVivek Goyal * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 3600c288874SVivek Goyal * if OVL_UPPERDATA flag is visible, then effects of write operations 3610c288874SVivek Goyal * before it are visible as well. 3620c288874SVivek Goyal */ 3630c288874SVivek Goyal smp_wmb(); 3640c288874SVivek Goyal ovl_set_flag(OVL_UPPERDATA, inode); 3650c288874SVivek Goyal } 3660c288874SVivek Goyal 3670c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 3680c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 3690c288874SVivek Goyal { 3700c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 3710c288874SVivek Goyal return false; 3720c288874SVivek Goyal 3730c288874SVivek Goyal return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 3740c288874SVivek Goyal } 3750c288874SVivek Goyal 3760c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 3770c288874SVivek Goyal { 3780c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 3790c288874SVivek Goyal return false; 3800c288874SVivek Goyal 3810c288874SVivek Goyal return !ovl_has_upperdata(d_inode(dentry)); 3820c288874SVivek Goyal } 3830c288874SVivek Goyal 384a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb) 385a6c60655SMiklos Szeredi { 386a6c60655SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 387a6c60655SMiklos Szeredi 38821a22878SAmir Goldstein return ofs->config.redirect_dir && !ofs->noxattr; 389a6c60655SMiklos Szeredi } 390a6c60655SMiklos Szeredi 391a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry) 392a6c60655SMiklos Szeredi { 393cf31c463SMiklos Szeredi return OVL_I(d_inode(dentry))->redirect; 394a6c60655SMiklos Szeredi } 395a6c60655SMiklos Szeredi 396a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 397a6c60655SMiklos Szeredi { 398cf31c463SMiklos Szeredi struct ovl_inode *oi = OVL_I(d_inode(dentry)); 399a6c60655SMiklos Szeredi 400cf31c463SMiklos Szeredi kfree(oi->redirect); 401cf31c463SMiklos Szeredi oi->redirect = redirect; 402a6c60655SMiklos Szeredi } 403a6c60655SMiklos Szeredi 40409d8b586SMiklos Szeredi void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, 4052664bd08SVivek Goyal struct dentry *lowerdentry, struct dentry *lowerdata) 406bbb1e54dSMiklos Szeredi { 407695b46e7SAmir Goldstein struct inode *realinode = d_inode(upperdentry ?: lowerdentry); 408695b46e7SAmir Goldstein 40909d8b586SMiklos Szeredi if (upperdentry) 41009d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 41109d8b586SMiklos Szeredi if (lowerdentry) 41231747edaSAmir Goldstein OVL_I(inode)->lower = igrab(d_inode(lowerdentry)); 4132664bd08SVivek Goyal if (lowerdata) 4142664bd08SVivek Goyal OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata)); 415bbb1e54dSMiklos Szeredi 416695b46e7SAmir Goldstein ovl_copyattr(realinode, inode); 4174f357295SMiklos Szeredi ovl_copyflags(realinode, inode); 418695b46e7SAmir Goldstein if (!inode->i_ino) 419695b46e7SAmir Goldstein inode->i_ino = realinode->i_ino; 420bbb1e54dSMiklos Szeredi } 421bbb1e54dSMiklos Szeredi 42209d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 423bbb1e54dSMiklos Szeredi { 42409d8b586SMiklos Szeredi struct inode *upperinode = d_inode(upperdentry); 425e6d2ebddSMiklos Szeredi 42609d8b586SMiklos Szeredi WARN_ON(OVL_I(inode)->__upperdentry); 42709d8b586SMiklos Szeredi 42825b7713aSMiklos Szeredi /* 42909d8b586SMiklos Szeredi * Make sure upperdentry is consistent before making it visible 43025b7713aSMiklos Szeredi */ 43125b7713aSMiklos Szeredi smp_wmb(); 43209d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 43331747edaSAmir Goldstein if (inode_unhashed(inode)) { 434695b46e7SAmir Goldstein if (!inode->i_ino) 435695b46e7SAmir Goldstein inode->i_ino = upperinode->i_ino; 43625b7713aSMiklos Szeredi inode->i_private = upperinode; 437bbb1e54dSMiklos Szeredi __insert_inode_hash(inode, (unsigned long) upperinode); 438bbb1e54dSMiklos Szeredi } 43925b7713aSMiklos Szeredi } 440bbb1e54dSMiklos Szeredi 441d9854c87SMiklos Szeredi static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity) 442bbb1e54dSMiklos Szeredi { 44304a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 444bbb1e54dSMiklos Szeredi 44504a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 4464edb83bbSMiklos Szeredi /* 4474edb83bbSMiklos Szeredi * Version is used by readdir code to keep cache consistent. For merge 4484edb83bbSMiklos Szeredi * dirs all changes need to be noted. For non-merge dirs, cache only 4494edb83bbSMiklos Szeredi * contains impure (ones which have been copied up and have origins) 4504edb83bbSMiklos Szeredi * entries, so only need to note changes to impure entries. 4514edb83bbSMiklos Szeredi */ 4524edb83bbSMiklos Szeredi if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity) 45304a01ac7SMiklos Szeredi OVL_I(inode)->version++; 454bbb1e54dSMiklos Szeredi } 455bbb1e54dSMiklos Szeredi 456d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity) 457d9854c87SMiklos Szeredi { 458d9854c87SMiklos Szeredi /* Copy mtime/ctime */ 459d9854c87SMiklos Szeredi ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry)); 460d9854c87SMiklos Szeredi 461d9854c87SMiklos Szeredi ovl_dentry_version_inc(dentry, impurity); 462d9854c87SMiklos Szeredi } 463d9854c87SMiklos Szeredi 464bbb1e54dSMiklos Szeredi u64 ovl_dentry_version_get(struct dentry *dentry) 465bbb1e54dSMiklos Szeredi { 46604a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 467bbb1e54dSMiklos Szeredi 46804a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 46904a01ac7SMiklos Szeredi return OVL_I(inode)->version; 470bbb1e54dSMiklos Szeredi } 471bbb1e54dSMiklos Szeredi 472bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry) 473bbb1e54dSMiklos Szeredi { 474bbb1e54dSMiklos Szeredi struct inode *inode = dentry->d_inode; 475bbb1e54dSMiklos Szeredi 476bbb1e54dSMiklos Szeredi return inode && IS_WHITEOUT(inode); 477bbb1e54dSMiklos Szeredi } 478bbb1e54dSMiklos Szeredi 479bbb1e54dSMiklos Szeredi struct file *ovl_path_open(struct path *path, int flags) 480bbb1e54dSMiklos Szeredi { 481bbb1e54dSMiklos Szeredi return dentry_open(path, flags | O_NOATIME, current_cred()); 482bbb1e54dSMiklos Szeredi } 48339d3d60aSAmir Goldstein 4840c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 4850c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 4860c288874SVivek Goyal { 4870c288874SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 4880c288874SVivek Goyal 4890c288874SVivek Goyal if (ovl_dentry_upper(dentry) && 4900c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 4910c288874SVivek Goyal !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 4920c288874SVivek Goyal return true; 4930c288874SVivek Goyal 4940c288874SVivek Goyal return false; 4950c288874SVivek Goyal } 4960c288874SVivek Goyal 4970c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags) 4982002df85SVivek Goyal { 4992002df85SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5002002df85SVivek Goyal 5012002df85SVivek Goyal /* 5022002df85SVivek Goyal * Check if copy-up has happened as well as for upper alias (in 5032002df85SVivek Goyal * case of hard links) is there. 5042002df85SVivek Goyal * 5052002df85SVivek Goyal * Both checks are lockless: 5062002df85SVivek Goyal * - false negatives: will recheck under oi->lock 5072002df85SVivek Goyal * - false positives: 5082002df85SVivek Goyal * + ovl_dentry_upper() uses memory barriers to ensure the 5092002df85SVivek Goyal * upper dentry is up-to-date 5102002df85SVivek Goyal * + ovl_dentry_has_upper_alias() relies on locking of 5112002df85SVivek Goyal * upper parent i_rwsem to prevent reordering copy-up 5122002df85SVivek Goyal * with rename. 5132002df85SVivek Goyal */ 5142002df85SVivek Goyal if (ovl_dentry_upper(dentry) && 5150c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 5160c288874SVivek Goyal !ovl_dentry_needs_data_copy_up(dentry, flags)) 5172002df85SVivek Goyal return true; 5182002df85SVivek Goyal 5192002df85SVivek Goyal return false; 5202002df85SVivek Goyal } 5212002df85SVivek Goyal 5220c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags) 52339d3d60aSAmir Goldstein { 5241e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 52539d3d60aSAmir Goldstein int err; 52639d3d60aSAmir Goldstein 5271e92e307SAmir Goldstein err = ovl_inode_lock(inode); 5280c288874SVivek Goyal if (!err && ovl_already_copied_up_locked(dentry, flags)) { 52939d3d60aSAmir Goldstein err = 1; /* Already copied up */ 5301e92e307SAmir Goldstein ovl_inode_unlock(inode); 53139d3d60aSAmir Goldstein } 53239d3d60aSAmir Goldstein 53339d3d60aSAmir Goldstein return err; 53439d3d60aSAmir Goldstein } 53539d3d60aSAmir Goldstein 53639d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry) 53739d3d60aSAmir Goldstein { 5381e92e307SAmir Goldstein ovl_inode_unlock(d_inode(dentry)); 53939d3d60aSAmir Goldstein } 54082b749b2SAmir Goldstein 541b79e05aaSAmir Goldstein bool ovl_check_origin_xattr(struct dentry *dentry) 542b79e05aaSAmir Goldstein { 543b79e05aaSAmir Goldstein int res; 544b79e05aaSAmir Goldstein 545b79e05aaSAmir Goldstein res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); 546b79e05aaSAmir Goldstein 547b79e05aaSAmir Goldstein /* Zero size value means "copied up but origin unknown" */ 548b79e05aaSAmir Goldstein if (res >= 0) 549b79e05aaSAmir Goldstein return true; 550b79e05aaSAmir Goldstein 551b79e05aaSAmir Goldstein return false; 552b79e05aaSAmir Goldstein } 553b79e05aaSAmir Goldstein 554f3a15685SAmir Goldstein bool ovl_check_dir_xattr(struct dentry *dentry, const char *name) 555f3a15685SAmir Goldstein { 556f3a15685SAmir Goldstein int res; 557f3a15685SAmir Goldstein char val; 558f3a15685SAmir Goldstein 559f3a15685SAmir Goldstein if (!d_is_dir(dentry)) 560f3a15685SAmir Goldstein return false; 561f3a15685SAmir Goldstein 562f3a15685SAmir Goldstein res = vfs_getxattr(dentry, name, &val, 1); 563f3a15685SAmir Goldstein if (res == 1 && val == 'y') 564f3a15685SAmir Goldstein return true; 565f3a15685SAmir Goldstein 566f3a15685SAmir Goldstein return false; 567f3a15685SAmir Goldstein } 568f3a15685SAmir Goldstein 56982b749b2SAmir Goldstein int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, 57082b749b2SAmir Goldstein const char *name, const void *value, size_t size, 57182b749b2SAmir Goldstein int xerr) 57282b749b2SAmir Goldstein { 57382b749b2SAmir Goldstein int err; 57482b749b2SAmir Goldstein struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 57582b749b2SAmir Goldstein 57682b749b2SAmir Goldstein if (ofs->noxattr) 57782b749b2SAmir Goldstein return xerr; 57882b749b2SAmir Goldstein 57982b749b2SAmir Goldstein err = ovl_do_setxattr(upperdentry, name, value, size, 0); 58082b749b2SAmir Goldstein 58182b749b2SAmir Goldstein if (err == -EOPNOTSUPP) { 58282b749b2SAmir Goldstein pr_warn("overlayfs: cannot set %s xattr on upper\n", name); 58382b749b2SAmir Goldstein ofs->noxattr = true; 58482b749b2SAmir Goldstein return xerr; 58582b749b2SAmir Goldstein } 58682b749b2SAmir Goldstein 58782b749b2SAmir Goldstein return err; 58882b749b2SAmir Goldstein } 589f3a15685SAmir Goldstein 590f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 591f3a15685SAmir Goldstein { 592f3a15685SAmir Goldstein int err; 593f3a15685SAmir Goldstein 59413c72075SMiklos Szeredi if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 595f3a15685SAmir Goldstein return 0; 596f3a15685SAmir Goldstein 597f3a15685SAmir Goldstein /* 598f3a15685SAmir Goldstein * Do not fail when upper doesn't support xattrs. 599f3a15685SAmir Goldstein * Upper inodes won't have origin nor redirect xattr anyway. 600f3a15685SAmir Goldstein */ 601f3a15685SAmir Goldstein err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, 602f3a15685SAmir Goldstein "y", 1, 0); 603f3a15685SAmir Goldstein if (!err) 60413c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 605f3a15685SAmir Goldstein 606f3a15685SAmir Goldstein return err; 607f3a15685SAmir Goldstein } 60813c72075SMiklos Szeredi 60913c72075SMiklos Szeredi void ovl_set_flag(unsigned long flag, struct inode *inode) 61013c72075SMiklos Szeredi { 61113c72075SMiklos Szeredi set_bit(flag, &OVL_I(inode)->flags); 61213c72075SMiklos Szeredi } 61313c72075SMiklos Szeredi 6144edb83bbSMiklos Szeredi void ovl_clear_flag(unsigned long flag, struct inode *inode) 6154edb83bbSMiklos Szeredi { 6164edb83bbSMiklos Szeredi clear_bit(flag, &OVL_I(inode)->flags); 6174edb83bbSMiklos Szeredi } 6184edb83bbSMiklos Szeredi 61913c72075SMiklos Szeredi bool ovl_test_flag(unsigned long flag, struct inode *inode) 62013c72075SMiklos Szeredi { 62113c72075SMiklos Szeredi return test_bit(flag, &OVL_I(inode)->flags); 62213c72075SMiklos Szeredi } 623ad0af710SAmir Goldstein 624ad0af710SAmir Goldstein /** 625ad0af710SAmir Goldstein * Caller must hold a reference to inode to prevent it from being freed while 626ad0af710SAmir Goldstein * it is marked inuse. 627ad0af710SAmir Goldstein */ 628ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry) 629ad0af710SAmir Goldstein { 630ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 631ad0af710SAmir Goldstein bool locked = false; 632ad0af710SAmir Goldstein 633ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 634ad0af710SAmir Goldstein if (!(inode->i_state & I_OVL_INUSE)) { 635ad0af710SAmir Goldstein inode->i_state |= I_OVL_INUSE; 636ad0af710SAmir Goldstein locked = true; 637ad0af710SAmir Goldstein } 638ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 639ad0af710SAmir Goldstein 640ad0af710SAmir Goldstein return locked; 641ad0af710SAmir Goldstein } 642ad0af710SAmir Goldstein 643ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry) 644ad0af710SAmir Goldstein { 645ad0af710SAmir Goldstein if (dentry) { 646ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 647ad0af710SAmir Goldstein 648ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 649ad0af710SAmir Goldstein WARN_ON(!(inode->i_state & I_OVL_INUSE)); 650ad0af710SAmir Goldstein inode->i_state &= ~I_OVL_INUSE; 651ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 652ad0af710SAmir Goldstein } 653ad0af710SAmir Goldstein } 6545f8415d6SAmir Goldstein 655146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry) 656146d62e5SAmir Goldstein { 657146d62e5SAmir Goldstein struct inode *inode = d_inode(dentry); 658146d62e5SAmir Goldstein bool inuse; 659146d62e5SAmir Goldstein 660146d62e5SAmir Goldstein spin_lock(&inode->i_lock); 661146d62e5SAmir Goldstein inuse = (inode->i_state & I_OVL_INUSE); 662146d62e5SAmir Goldstein spin_unlock(&inode->i_lock); 663146d62e5SAmir Goldstein 664146d62e5SAmir Goldstein return inuse; 665146d62e5SAmir Goldstein } 666146d62e5SAmir Goldstein 66724b33ee1SAmir Goldstein /* 66824b33ee1SAmir Goldstein * Does this overlay dentry need to be indexed on copy up? 66924b33ee1SAmir Goldstein */ 67024b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry) 67124b33ee1SAmir Goldstein { 67224b33ee1SAmir Goldstein struct dentry *lower = ovl_dentry_lower(dentry); 67324b33ee1SAmir Goldstein 67424b33ee1SAmir Goldstein if (!lower || !ovl_indexdir(dentry->d_sb)) 67524b33ee1SAmir Goldstein return false; 67624b33ee1SAmir Goldstein 677fbd2d207SAmir Goldstein /* Index all files for NFS export and consistency verification */ 678016b720fSAmir Goldstein if (ovl_index_all(dentry->d_sb)) 679fbd2d207SAmir Goldstein return true; 680fbd2d207SAmir Goldstein 68124b33ee1SAmir Goldstein /* Index only lower hardlinks on copy up */ 68224b33ee1SAmir Goldstein if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 68324b33ee1SAmir Goldstein return true; 68424b33ee1SAmir Goldstein 68524b33ee1SAmir Goldstein return false; 68624b33ee1SAmir Goldstein } 68724b33ee1SAmir Goldstein 6889f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */ 689caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry) 690caf70cb2SAmir Goldstein { 691e7dd0e71SAmir Goldstein struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 692e7dd0e71SAmir Goldstein struct inode *dir = indexdir->d_inode; 693caf70cb2SAmir Goldstein struct dentry *lowerdentry = ovl_dentry_lower(dentry); 694caf70cb2SAmir Goldstein struct dentry *upperdentry = ovl_dentry_upper(dentry); 695caf70cb2SAmir Goldstein struct dentry *index = NULL; 696caf70cb2SAmir Goldstein struct inode *inode; 69763e13252SAmir Goldstein struct qstr name = { }; 698caf70cb2SAmir Goldstein int err; 699caf70cb2SAmir Goldstein 700caf70cb2SAmir Goldstein err = ovl_get_index_name(lowerdentry, &name); 701caf70cb2SAmir Goldstein if (err) 702caf70cb2SAmir Goldstein goto fail; 703caf70cb2SAmir Goldstein 704caf70cb2SAmir Goldstein inode = d_inode(upperdentry); 70589a17556SAmir Goldstein if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 706caf70cb2SAmir Goldstein pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 707caf70cb2SAmir Goldstein upperdentry, inode->i_ino, inode->i_nlink); 708caf70cb2SAmir Goldstein /* 709caf70cb2SAmir Goldstein * We either have a bug with persistent union nlink or a lower 710caf70cb2SAmir Goldstein * hardlink was added while overlay is mounted. Adding a lower 711caf70cb2SAmir Goldstein * hardlink and then unlinking all overlay hardlinks would drop 712caf70cb2SAmir Goldstein * overlay nlink to zero before all upper inodes are unlinked. 713caf70cb2SAmir Goldstein * As a safety measure, when that situation is detected, set 714caf70cb2SAmir Goldstein * the overlay nlink to the index inode nlink minus one for the 715caf70cb2SAmir Goldstein * index entry itself. 716caf70cb2SAmir Goldstein */ 717caf70cb2SAmir Goldstein set_nlink(d_inode(dentry), inode->i_nlink - 1); 718caf70cb2SAmir Goldstein ovl_set_nlink_upper(dentry); 719caf70cb2SAmir Goldstein goto out; 720caf70cb2SAmir Goldstein } 721caf70cb2SAmir Goldstein 722caf70cb2SAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 723e7dd0e71SAmir Goldstein index = lookup_one_len(name.name, indexdir, name.len); 724caf70cb2SAmir Goldstein err = PTR_ERR(index); 725e7dd0e71SAmir Goldstein if (IS_ERR(index)) { 7269f4ec904SAmir Goldstein index = NULL; 727e7dd0e71SAmir Goldstein } else if (ovl_index_all(dentry->d_sb)) { 728e7dd0e71SAmir Goldstein /* Whiteout orphan index to block future open by handle */ 729e7dd0e71SAmir Goldstein err = ovl_cleanup_and_whiteout(indexdir, dir, index); 730e7dd0e71SAmir Goldstein } else { 731e7dd0e71SAmir Goldstein /* Cleanup orphan index entries */ 732e7dd0e71SAmir Goldstein err = ovl_cleanup(dir, index); 733e7dd0e71SAmir Goldstein } 7349f4ec904SAmir Goldstein 735caf70cb2SAmir Goldstein inode_unlock(dir); 736caf70cb2SAmir Goldstein if (err) 737caf70cb2SAmir Goldstein goto fail; 738caf70cb2SAmir Goldstein 739caf70cb2SAmir Goldstein out: 74063e13252SAmir Goldstein kfree(name.name); 741caf70cb2SAmir Goldstein dput(index); 742caf70cb2SAmir Goldstein return; 743caf70cb2SAmir Goldstein 744caf70cb2SAmir Goldstein fail: 745caf70cb2SAmir Goldstein pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err); 746caf70cb2SAmir Goldstein goto out; 747caf70cb2SAmir Goldstein } 748caf70cb2SAmir Goldstein 7495f8415d6SAmir Goldstein /* 7505f8415d6SAmir Goldstein * Operations that change overlay inode and upper inode nlink need to be 7515f8415d6SAmir Goldstein * synchronized with copy up for persistent nlink accounting. 7525f8415d6SAmir Goldstein */ 7530e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry) 7545f8415d6SAmir Goldstein { 7551e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 7565f8415d6SAmir Goldstein const struct cred *old_cred; 7575f8415d6SAmir Goldstein int err; 7585f8415d6SAmir Goldstein 7591e92e307SAmir Goldstein if (WARN_ON(!inode)) 7600e32992fSAmir Goldstein return -ENOENT; 7615f8415d6SAmir Goldstein 7625f8415d6SAmir Goldstein /* 7635f8415d6SAmir Goldstein * With inodes index is enabled, we store the union overlay nlink 76424b33ee1SAmir Goldstein * in an xattr on the index inode. When whiting out an indexed lower, 7655f8415d6SAmir Goldstein * we need to decrement the overlay persistent nlink, but before the 7665f8415d6SAmir Goldstein * first copy up, we have no upper index inode to store the xattr. 7675f8415d6SAmir Goldstein * 76824b33ee1SAmir Goldstein * As a workaround, before whiteout/rename over an indexed lower, 7695f8415d6SAmir Goldstein * copy up to create the upper index. Creating the upper index will 7705f8415d6SAmir Goldstein * initialize the overlay nlink, so it could be dropped if unlink 7715f8415d6SAmir Goldstein * or rename succeeds. 7725f8415d6SAmir Goldstein * 7735f8415d6SAmir Goldstein * TODO: implement metadata only index copy up when called with 7745f8415d6SAmir Goldstein * ovl_copy_up_flags(dentry, O_PATH). 7755f8415d6SAmir Goldstein */ 77624b33ee1SAmir Goldstein if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 7775f8415d6SAmir Goldstein err = ovl_copy_up(dentry); 7785f8415d6SAmir Goldstein if (err) 7795f8415d6SAmir Goldstein return err; 7805f8415d6SAmir Goldstein } 7815f8415d6SAmir Goldstein 7821e92e307SAmir Goldstein err = ovl_inode_lock(inode); 7835f8415d6SAmir Goldstein if (err) 7845f8415d6SAmir Goldstein return err; 7855f8415d6SAmir Goldstein 7861e92e307SAmir Goldstein if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 7875f8415d6SAmir Goldstein goto out; 7885f8415d6SAmir Goldstein 7895f8415d6SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 7905f8415d6SAmir Goldstein /* 7915f8415d6SAmir Goldstein * The overlay inode nlink should be incremented/decremented IFF the 7925f8415d6SAmir Goldstein * upper operation succeeds, along with nlink change of upper inode. 7935f8415d6SAmir Goldstein * Therefore, before link/unlink/rename, we store the union nlink 7945f8415d6SAmir Goldstein * value relative to the upper inode nlink in an upper inode xattr. 7955f8415d6SAmir Goldstein */ 7965f8415d6SAmir Goldstein err = ovl_set_nlink_upper(dentry); 7975f8415d6SAmir Goldstein revert_creds(old_cred); 7985f8415d6SAmir Goldstein 7995f8415d6SAmir Goldstein out: 8005f8415d6SAmir Goldstein if (err) 8011e92e307SAmir Goldstein ovl_inode_unlock(inode); 8025f8415d6SAmir Goldstein 8035f8415d6SAmir Goldstein return err; 8045f8415d6SAmir Goldstein } 8055f8415d6SAmir Goldstein 8060e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry) 8075f8415d6SAmir Goldstein { 8081e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 8091e92e307SAmir Goldstein 8101e92e307SAmir Goldstein if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 811caf70cb2SAmir Goldstein const struct cred *old_cred; 812caf70cb2SAmir Goldstein 813caf70cb2SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 814caf70cb2SAmir Goldstein ovl_cleanup_index(dentry); 815caf70cb2SAmir Goldstein revert_creds(old_cred); 816caf70cb2SAmir Goldstein } 817caf70cb2SAmir Goldstein 8181e92e307SAmir Goldstein ovl_inode_unlock(inode); 8195f8415d6SAmir Goldstein } 8205820dc08SAmir Goldstein 8215820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) 8225820dc08SAmir Goldstein { 8235820dc08SAmir Goldstein /* Workdir should not be the same as upperdir */ 8245820dc08SAmir Goldstein if (workdir == upperdir) 8255820dc08SAmir Goldstein goto err; 8265820dc08SAmir Goldstein 8275820dc08SAmir Goldstein /* Workdir should not be subdir of upperdir and vice versa */ 8285820dc08SAmir Goldstein if (lock_rename(workdir, upperdir) != NULL) 8295820dc08SAmir Goldstein goto err_unlock; 8305820dc08SAmir Goldstein 8315820dc08SAmir Goldstein return 0; 8325820dc08SAmir Goldstein 8335820dc08SAmir Goldstein err_unlock: 8345820dc08SAmir Goldstein unlock_rename(workdir, upperdir); 8355820dc08SAmir Goldstein err: 8365820dc08SAmir Goldstein pr_err("overlayfs: failed to lock workdir+upperdir\n"); 8375820dc08SAmir Goldstein return -EIO; 8385820dc08SAmir Goldstein } 8399d3dfea3SVivek Goyal 8409d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ 8419d3dfea3SVivek Goyal int ovl_check_metacopy_xattr(struct dentry *dentry) 8429d3dfea3SVivek Goyal { 8439d3dfea3SVivek Goyal int res; 8449d3dfea3SVivek Goyal 8459d3dfea3SVivek Goyal /* Only regular files can have metacopy xattr */ 8469d3dfea3SVivek Goyal if (!S_ISREG(d_inode(dentry)->i_mode)) 8479d3dfea3SVivek Goyal return 0; 8489d3dfea3SVivek Goyal 8499d3dfea3SVivek Goyal res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0); 8509d3dfea3SVivek Goyal if (res < 0) { 8519d3dfea3SVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 8529d3dfea3SVivek Goyal return 0; 8539d3dfea3SVivek Goyal goto out; 8549d3dfea3SVivek Goyal } 8559d3dfea3SVivek Goyal 8569d3dfea3SVivek Goyal return 1; 8579d3dfea3SVivek Goyal out: 8589d3dfea3SVivek Goyal pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res); 8599d3dfea3SVivek Goyal return res; 8609d3dfea3SVivek Goyal } 86167d756c2SVivek Goyal 86267d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry) 86367d756c2SVivek Goyal { 86467d756c2SVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 86567d756c2SVivek Goyal 86667d756c2SVivek Goyal if (!d_is_reg(dentry)) 86767d756c2SVivek Goyal return false; 86867d756c2SVivek Goyal 86967d756c2SVivek Goyal if (ovl_dentry_upper(dentry)) { 87067d756c2SVivek Goyal if (!ovl_has_upperdata(d_inode(dentry))) 87167d756c2SVivek Goyal return true; 87267d756c2SVivek Goyal return false; 87367d756c2SVivek Goyal } 87467d756c2SVivek Goyal 87567d756c2SVivek Goyal return (oe->numlower > 1); 87667d756c2SVivek Goyal } 8770a2d0d3fSVivek Goyal 878993a0b2aSVivek Goyal ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value, 879993a0b2aSVivek Goyal size_t padding) 880993a0b2aSVivek Goyal { 881993a0b2aSVivek Goyal ssize_t res; 882993a0b2aSVivek Goyal char *buf = NULL; 883993a0b2aSVivek Goyal 884993a0b2aSVivek Goyal res = vfs_getxattr(dentry, name, NULL, 0); 885993a0b2aSVivek Goyal if (res < 0) { 886993a0b2aSVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 887993a0b2aSVivek Goyal return -ENODATA; 888993a0b2aSVivek Goyal goto fail; 889993a0b2aSVivek Goyal } 890993a0b2aSVivek Goyal 891993a0b2aSVivek Goyal if (res != 0) { 892993a0b2aSVivek Goyal buf = kzalloc(res + padding, GFP_KERNEL); 893993a0b2aSVivek Goyal if (!buf) 894993a0b2aSVivek Goyal return -ENOMEM; 895993a0b2aSVivek Goyal 896993a0b2aSVivek Goyal res = vfs_getxattr(dentry, name, buf, res); 897993a0b2aSVivek Goyal if (res < 0) 898993a0b2aSVivek Goyal goto fail; 899993a0b2aSVivek Goyal } 900993a0b2aSVivek Goyal *value = buf; 901993a0b2aSVivek Goyal 902993a0b2aSVivek Goyal return res; 903993a0b2aSVivek Goyal 904993a0b2aSVivek Goyal fail: 905993a0b2aSVivek Goyal pr_warn_ratelimited("overlayfs: failed to get xattr %s: err=%zi)\n", 906993a0b2aSVivek Goyal name, res); 907993a0b2aSVivek Goyal kfree(buf); 908993a0b2aSVivek Goyal return res; 909993a0b2aSVivek Goyal } 910993a0b2aSVivek Goyal 9110a2d0d3fSVivek Goyal char *ovl_get_redirect_xattr(struct dentry *dentry, int padding) 9120a2d0d3fSVivek Goyal { 9130a2d0d3fSVivek Goyal int res; 9140a2d0d3fSVivek Goyal char *s, *next, *buf = NULL; 9150a2d0d3fSVivek Goyal 916993a0b2aSVivek Goyal res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1); 917993a0b2aSVivek Goyal if (res == -ENODATA) 9180a2d0d3fSVivek Goyal return NULL; 9190a2d0d3fSVivek Goyal if (res < 0) 920993a0b2aSVivek Goyal return ERR_PTR(res); 9210a2d0d3fSVivek Goyal if (res == 0) 9220a2d0d3fSVivek Goyal goto invalid; 9230a2d0d3fSVivek Goyal 9240a2d0d3fSVivek Goyal if (buf[0] == '/') { 9250a2d0d3fSVivek Goyal for (s = buf; *s++ == '/'; s = next) { 9260a2d0d3fSVivek Goyal next = strchrnul(s, '/'); 9270a2d0d3fSVivek Goyal if (s == next) 9280a2d0d3fSVivek Goyal goto invalid; 9290a2d0d3fSVivek Goyal } 9300a2d0d3fSVivek Goyal } else { 9310a2d0d3fSVivek Goyal if (strchr(buf, '/') != NULL) 9320a2d0d3fSVivek Goyal goto invalid; 9330a2d0d3fSVivek Goyal } 9340a2d0d3fSVivek Goyal 9350a2d0d3fSVivek Goyal return buf; 9360a2d0d3fSVivek Goyal invalid: 9370a2d0d3fSVivek Goyal pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf); 9380a2d0d3fSVivek Goyal res = -EINVAL; 939993a0b2aSVivek Goyal kfree(buf); 940993a0b2aSVivek Goyal return ERR_PTR(res); 9410a2d0d3fSVivek Goyal } 942