1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2bbb1e54dSMiklos Szeredi /* 3bbb1e54dSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 4bbb1e54dSMiklos Szeredi * Copyright (C) 2016 Red Hat, Inc. 5bbb1e54dSMiklos Szeredi */ 6bbb1e54dSMiklos Szeredi 7bbb1e54dSMiklos Szeredi #include <linux/fs.h> 8bbb1e54dSMiklos Szeredi #include <linux/mount.h> 9bbb1e54dSMiklos Szeredi #include <linux/slab.h> 105b825c3aSIngo Molnar #include <linux/cred.h> 11bbb1e54dSMiklos Szeredi #include <linux/xattr.h> 1202bcd157SAmir Goldstein #include <linux/exportfs.h> 13096a218aSAmir Goldstein #include <linux/fileattr.h> 1402bcd157SAmir Goldstein #include <linux/uuid.h> 15caf70cb2SAmir Goldstein #include <linux/namei.h> 16caf70cb2SAmir Goldstein #include <linux/ratelimit.h> 17bbb1e54dSMiklos Szeredi #include "overlayfs.h" 18bbb1e54dSMiklos Szeredi 19bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry) 20bbb1e54dSMiklos Szeredi { 21bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 2208f4c7c8SMiklos Szeredi return mnt_want_write(ovl_upper_mnt(ofs)); 23bbb1e54dSMiklos Szeredi } 24bbb1e54dSMiklos Szeredi 25bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry) 26bbb1e54dSMiklos Szeredi { 27bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 2808f4c7c8SMiklos Szeredi mnt_drop_write(ovl_upper_mnt(ofs)); 29bbb1e54dSMiklos Szeredi } 30bbb1e54dSMiklos Szeredi 31bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry) 32bbb1e54dSMiklos Szeredi { 33bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 34bbb1e54dSMiklos Szeredi return ofs->workdir; 35bbb1e54dSMiklos Szeredi } 36bbb1e54dSMiklos Szeredi 37bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb) 38bbb1e54dSMiklos Szeredi { 39bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 40bbb1e54dSMiklos Szeredi 41bbb1e54dSMiklos Szeredi return override_creds(ofs->creator_cred); 42bbb1e54dSMiklos Szeredi } 43bbb1e54dSMiklos Szeredi 44e487d889SAmir Goldstein /* 45e487d889SAmir Goldstein * Check if underlying fs supports file handles and try to determine encoding 46e487d889SAmir Goldstein * type, in order to deduce maximum inode number used by fs. 47e487d889SAmir Goldstein * 48e487d889SAmir Goldstein * Return 0 if file handles are not supported. 49e487d889SAmir Goldstein * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. 50e487d889SAmir Goldstein * Return -1 if fs uses a non default encoding with unknown inode size. 51e487d889SAmir Goldstein */ 52e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb) 5302bcd157SAmir Goldstein { 54c846af05SMiklos Szeredi if (!capable(CAP_DAC_READ_SEARCH)) 55c846af05SMiklos Szeredi return 0; 56c846af05SMiklos Szeredi 579df085f3SAmir Goldstein if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry) 58e487d889SAmir Goldstein return 0; 59e487d889SAmir Goldstein 60e487d889SAmir Goldstein return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; 6102bcd157SAmir Goldstein } 6202bcd157SAmir Goldstein 6302bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb) 6402bcd157SAmir Goldstein { 6502bcd157SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 6602bcd157SAmir Goldstein 6702bcd157SAmir Goldstein return ofs->indexdir; 6802bcd157SAmir Goldstein } 6902bcd157SAmir Goldstein 70f168f109SAmir Goldstein /* Index all files on copy up. For now only enabled for NFS export */ 71f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb) 72f168f109SAmir Goldstein { 73f168f109SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 74f168f109SAmir Goldstein 75f168f109SAmir Goldstein return ofs->config.nfs_export && ofs->config.index; 76f168f109SAmir Goldstein } 77f168f109SAmir Goldstein 78f168f109SAmir Goldstein /* Verify lower origin on lookup. For now only enabled for NFS export */ 79f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb) 80f168f109SAmir Goldstein { 81f168f109SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 82f168f109SAmir Goldstein 83f168f109SAmir Goldstein return ofs->config.nfs_export && ofs->config.index; 84f168f109SAmir Goldstein } 85f168f109SAmir Goldstein 86bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 87bbb1e54dSMiklos Szeredi { 88bbb1e54dSMiklos Szeredi size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 89bbb1e54dSMiklos Szeredi struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 90bbb1e54dSMiklos Szeredi 91bbb1e54dSMiklos Szeredi if (oe) 92bbb1e54dSMiklos Szeredi oe->numlower = numlower; 93bbb1e54dSMiklos Szeredi 94bbb1e54dSMiklos Szeredi return oe; 95bbb1e54dSMiklos Szeredi } 96bbb1e54dSMiklos Szeredi 97bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry) 98bbb1e54dSMiklos Szeredi { 99bbb1e54dSMiklos Szeredi return dentry->d_flags & 1007925dad8SMiklos Szeredi (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); 101bbb1e54dSMiklos Szeredi } 102bbb1e54dSMiklos Szeredi 103f4288844SMiklos Szeredi void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry, 104f4288844SMiklos Szeredi unsigned int mask) 105f4288844SMiklos Szeredi { 106f4288844SMiklos Szeredi struct ovl_entry *oe = OVL_E(dentry); 107f4288844SMiklos Szeredi unsigned int i, flags = 0; 108f4288844SMiklos Szeredi 109bccece1eSMiklos Szeredi if (upperdentry) 110bccece1eSMiklos Szeredi flags |= upperdentry->d_flags; 111f4288844SMiklos Szeredi for (i = 0; i < oe->numlower; i++) 112f4288844SMiklos Szeredi flags |= oe->lowerstack[i].dentry->d_flags; 113f4288844SMiklos Szeredi 114f4288844SMiklos Szeredi spin_lock(&dentry->d_lock); 115f4288844SMiklos Szeredi dentry->d_flags &= ~mask; 116f4288844SMiklos Szeredi dentry->d_flags |= flags & mask; 117f4288844SMiklos Szeredi spin_unlock(&dentry->d_lock); 118f4288844SMiklos Szeredi } 119f4288844SMiklos Szeredi 120bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry) 121bbb1e54dSMiklos Szeredi { 122bbb1e54dSMiklos Szeredi return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 123bbb1e54dSMiklos Szeredi DCACHE_MANAGE_TRANSIT | 124bbb1e54dSMiklos Szeredi DCACHE_OP_HASH | 125bbb1e54dSMiklos Szeredi DCACHE_OP_COMPARE); 126bbb1e54dSMiklos Szeredi } 127bbb1e54dSMiklos Szeredi 128bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry) 129bbb1e54dSMiklos Szeredi { 130bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 131bbb1e54dSMiklos Szeredi enum ovl_path_type type = 0; 132bbb1e54dSMiklos Szeredi 13309d8b586SMiklos Szeredi if (ovl_dentry_upper(dentry)) { 134bbb1e54dSMiklos Szeredi type = __OVL_PATH_UPPER; 135bbb1e54dSMiklos Szeredi 136bbb1e54dSMiklos Szeredi /* 13759548503SAmir Goldstein * Non-dir dentry can hold lower dentry of its copy up origin. 138bbb1e54dSMiklos Szeredi */ 13959548503SAmir Goldstein if (oe->numlower) { 14060124877SVivek Goyal if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) 14159548503SAmir Goldstein type |= __OVL_PATH_ORIGIN; 1420b17c28aSVivek Goyal if (d_is_dir(dentry) || 1430b17c28aSVivek Goyal !ovl_has_upperdata(d_inode(dentry))) 144bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 14559548503SAmir Goldstein } 146bbb1e54dSMiklos Szeredi } else { 147bbb1e54dSMiklos Szeredi if (oe->numlower > 1) 148bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 149bbb1e54dSMiklos Szeredi } 150bbb1e54dSMiklos Szeredi return type; 151bbb1e54dSMiklos Szeredi } 152bbb1e54dSMiklos Szeredi 153bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path) 154bbb1e54dSMiklos Szeredi { 155bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 156bbb1e54dSMiklos Szeredi 15708f4c7c8SMiklos Szeredi path->mnt = ovl_upper_mnt(ofs); 15809d8b586SMiklos Szeredi path->dentry = ovl_dentry_upper(dentry); 159bbb1e54dSMiklos Szeredi } 160bbb1e54dSMiklos Szeredi 161bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path) 162bbb1e54dSMiklos Szeredi { 163bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 164bbb1e54dSMiklos Szeredi 165b9343632SChandan Rajendra if (oe->numlower) { 166b9343632SChandan Rajendra path->mnt = oe->lowerstack[0].layer->mnt; 167b9343632SChandan Rajendra path->dentry = oe->lowerstack[0].dentry; 168b9343632SChandan Rajendra } else { 169b9343632SChandan Rajendra *path = (struct path) { }; 170b9343632SChandan Rajendra } 171bbb1e54dSMiklos Szeredi } 172bbb1e54dSMiklos Szeredi 1734f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path) 1744f93b426SVivek Goyal { 1754f93b426SVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 1764f93b426SVivek Goyal 1774f93b426SVivek Goyal if (oe->numlower) { 1784f93b426SVivek Goyal path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt; 1794f93b426SVivek Goyal path->dentry = oe->lowerstack[oe->numlower - 1].dentry; 1804f93b426SVivek Goyal } else { 1814f93b426SVivek Goyal *path = (struct path) { }; 1824f93b426SVivek Goyal } 1834f93b426SVivek Goyal } 1844f93b426SVivek Goyal 185bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 186bbb1e54dSMiklos Szeredi { 187bbb1e54dSMiklos Szeredi enum ovl_path_type type = ovl_path_type(dentry); 188bbb1e54dSMiklos Szeredi 189bbb1e54dSMiklos Szeredi if (!OVL_TYPE_UPPER(type)) 190bbb1e54dSMiklos Szeredi ovl_path_lower(dentry, path); 191bbb1e54dSMiklos Szeredi else 192bbb1e54dSMiklos Szeredi ovl_path_upper(dentry, path); 193bbb1e54dSMiklos Szeredi 194bbb1e54dSMiklos Szeredi return type; 195bbb1e54dSMiklos Szeredi } 196bbb1e54dSMiklos Szeredi 197bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry) 198bbb1e54dSMiklos Szeredi { 19909d8b586SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); 200bbb1e54dSMiklos Szeredi } 201bbb1e54dSMiklos Szeredi 202bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry) 203bbb1e54dSMiklos Szeredi { 204bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 205bbb1e54dSMiklos Szeredi 20609d8b586SMiklos Szeredi return oe->numlower ? oe->lowerstack[0].dentry : NULL; 207bbb1e54dSMiklos Szeredi } 208bbb1e54dSMiklos Szeredi 20913464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) 210da309e8cSAmir Goldstein { 211da309e8cSAmir Goldstein struct ovl_entry *oe = dentry->d_fsdata; 212da309e8cSAmir Goldstein 213da309e8cSAmir Goldstein return oe->numlower ? oe->lowerstack[0].layer : NULL; 214da309e8cSAmir Goldstein } 215da309e8cSAmir Goldstein 216647d253fSVivek Goyal /* 217647d253fSVivek Goyal * ovl_dentry_lower() could return either a data dentry or metacopy dentry 218597534e7SXiong Zhenwu * depending on what is stored in lowerstack[0]. At times we need to find 219647d253fSVivek Goyal * lower dentry which has data (and not metacopy dentry). This helper 220647d253fSVivek Goyal * returns the lower data dentry. 221647d253fSVivek Goyal */ 222647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) 223647d253fSVivek Goyal { 224647d253fSVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 225647d253fSVivek Goyal 226647d253fSVivek Goyal return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL; 227647d253fSVivek Goyal } 228647d253fSVivek Goyal 229bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry) 230bbb1e54dSMiklos Szeredi { 23109d8b586SMiklos Szeredi return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 232bbb1e54dSMiklos Szeredi } 233bbb1e54dSMiklos Szeredi 2341d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode) 2351d88f183SMiklos Szeredi { 2361d88f183SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(inode)); 2371d88f183SMiklos Szeredi } 2381d88f183SMiklos Szeredi 23909d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode) 24025b7713aSMiklos Szeredi { 2411d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 24225b7713aSMiklos Szeredi 24309d8b586SMiklos Szeredi return upperdentry ? d_inode(upperdentry) : NULL; 24425b7713aSMiklos Szeredi } 24525b7713aSMiklos Szeredi 24609d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode) 24709d8b586SMiklos Szeredi { 24809d8b586SMiklos Szeredi return OVL_I(inode)->lower; 24909d8b586SMiklos Szeredi } 25009d8b586SMiklos Szeredi 25109d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode) 25209d8b586SMiklos Szeredi { 25309d8b586SMiklos Szeredi return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 25409d8b586SMiklos Szeredi } 25509d8b586SMiklos Szeredi 2562664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */ 2572664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode) 2582664bd08SVivek Goyal { 2592664bd08SVivek Goyal if (WARN_ON(!S_ISREG(inode->i_mode))) 2602664bd08SVivek Goyal return NULL; 2612664bd08SVivek Goyal 2622664bd08SVivek Goyal return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); 2632664bd08SVivek Goyal } 26409d8b586SMiklos Szeredi 2654823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */ 2664823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode) 2674823d49cSVivek Goyal { 2684823d49cSVivek Goyal struct inode *upperinode; 2694823d49cSVivek Goyal 2704823d49cSVivek Goyal upperinode = ovl_inode_upper(inode); 2714823d49cSVivek Goyal if (upperinode && ovl_has_upperdata(inode)) 2724823d49cSVivek Goyal return upperinode; 2734823d49cSVivek Goyal 2744823d49cSVivek Goyal return ovl_inode_lowerdata(inode); 2754823d49cSVivek Goyal } 2764823d49cSVivek Goyal 2774edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 278bbb1e54dSMiklos Szeredi { 2794edb83bbSMiklos Szeredi return OVL_I(inode)->cache; 280bbb1e54dSMiklos Szeredi } 281bbb1e54dSMiklos Szeredi 2824edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 283bbb1e54dSMiklos Szeredi { 2844edb83bbSMiklos Szeredi OVL_I(inode)->cache = cache; 285bbb1e54dSMiklos Szeredi } 286bbb1e54dSMiklos Szeredi 287c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 288c62520a8SAmir Goldstein { 289c62520a8SAmir Goldstein set_bit(flag, &OVL_E(dentry)->flags); 290c62520a8SAmir Goldstein } 291c62520a8SAmir Goldstein 292c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 293c62520a8SAmir Goldstein { 294c62520a8SAmir Goldstein clear_bit(flag, &OVL_E(dentry)->flags); 295c62520a8SAmir Goldstein } 296c62520a8SAmir Goldstein 297c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 298c62520a8SAmir Goldstein { 299c62520a8SAmir Goldstein return test_bit(flag, &OVL_E(dentry)->flags); 300c62520a8SAmir Goldstein } 301c62520a8SAmir Goldstein 302bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry) 303bbb1e54dSMiklos Szeredi { 304c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 305bbb1e54dSMiklos Szeredi } 306bbb1e54dSMiklos Szeredi 307bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry) 308bbb1e54dSMiklos Szeredi { 309bbb1e54dSMiklos Szeredi return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 310bbb1e54dSMiklos Szeredi } 311bbb1e54dSMiklos Szeredi 3125cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry) 313bbb1e54dSMiklos Szeredi { 314c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 315bbb1e54dSMiklos Szeredi } 316bbb1e54dSMiklos Szeredi 31755acc661SMiklos Szeredi /* 318aa3ff3c1SAmir Goldstein * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 319aa3ff3c1SAmir Goldstein * to return positive, while there's no actual upper alias for the inode. 320aa3ff3c1SAmir Goldstein * Copy up code needs to know about the existence of the upper alias, so it 321aa3ff3c1SAmir Goldstein * can't use ovl_dentry_upper(). 32255acc661SMiklos Szeredi */ 32355acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry) 32455acc661SMiklos Szeredi { 325c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 32655acc661SMiklos Szeredi } 32755acc661SMiklos Szeredi 32855acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry) 32955acc661SMiklos Szeredi { 330c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 33155acc661SMiklos Szeredi } 33255acc661SMiklos Szeredi 3330c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode) 3340c288874SVivek Goyal { 3350c288874SVivek Goyal if (!S_ISREG(inode->i_mode)) 3360c288874SVivek Goyal return false; 3370c288874SVivek Goyal 3380c288874SVivek Goyal if (!ovl_inode_lower(inode)) 3390c288874SVivek Goyal return false; 3400c288874SVivek Goyal 3410c288874SVivek Goyal return true; 3420c288874SVivek Goyal } 3430c288874SVivek Goyal 3440c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode) 3450c288874SVivek Goyal { 3460c288874SVivek Goyal if (!ovl_should_check_upperdata(inode)) 3470c288874SVivek Goyal return true; 3480c288874SVivek Goyal 3490c288874SVivek Goyal if (!ovl_test_flag(OVL_UPPERDATA, inode)) 3500c288874SVivek Goyal return false; 3510c288874SVivek Goyal /* 3520c288874SVivek Goyal * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 3530c288874SVivek Goyal * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 3540c288874SVivek Goyal * if setting of OVL_UPPERDATA is visible, then effects of writes 3550c288874SVivek Goyal * before that are visible too. 3560c288874SVivek Goyal */ 3570c288874SVivek Goyal smp_rmb(); 3580c288874SVivek Goyal return true; 3590c288874SVivek Goyal } 3600c288874SVivek Goyal 3610c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode) 3620c288874SVivek Goyal { 3630c288874SVivek Goyal /* 3640c288874SVivek Goyal * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 3650c288874SVivek Goyal * if OVL_UPPERDATA flag is visible, then effects of write operations 3660c288874SVivek Goyal * before it are visible as well. 3670c288874SVivek Goyal */ 3680c288874SVivek Goyal smp_wmb(); 3690c288874SVivek Goyal ovl_set_flag(OVL_UPPERDATA, inode); 3700c288874SVivek Goyal } 3710c288874SVivek Goyal 3720c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 3730c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 3740c288874SVivek Goyal { 3750c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 3760c288874SVivek Goyal return false; 3770c288874SVivek Goyal 3780c288874SVivek Goyal return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 3790c288874SVivek Goyal } 3800c288874SVivek Goyal 3810c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 3820c288874SVivek Goyal { 3830c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 3840c288874SVivek Goyal return false; 3850c288874SVivek Goyal 3860c288874SVivek Goyal return !ovl_has_upperdata(d_inode(dentry)); 3870c288874SVivek Goyal } 3880c288874SVivek Goyal 389a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb) 390a6c60655SMiklos Szeredi { 391a6c60655SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 392a6c60655SMiklos Szeredi 39321a22878SAmir Goldstein return ofs->config.redirect_dir && !ofs->noxattr; 394a6c60655SMiklos Szeredi } 395a6c60655SMiklos Szeredi 396a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry) 397a6c60655SMiklos Szeredi { 398cf31c463SMiklos Szeredi return OVL_I(d_inode(dentry))->redirect; 399a6c60655SMiklos Szeredi } 400a6c60655SMiklos Szeredi 401a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 402a6c60655SMiklos Szeredi { 403cf31c463SMiklos Szeredi struct ovl_inode *oi = OVL_I(d_inode(dentry)); 404a6c60655SMiklos Szeredi 405cf31c463SMiklos Szeredi kfree(oi->redirect); 406cf31c463SMiklos Szeredi oi->redirect = redirect; 407a6c60655SMiklos Szeredi } 408a6c60655SMiklos Szeredi 40909d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 410bbb1e54dSMiklos Szeredi { 41109d8b586SMiklos Szeredi struct inode *upperinode = d_inode(upperdentry); 412e6d2ebddSMiklos Szeredi 41309d8b586SMiklos Szeredi WARN_ON(OVL_I(inode)->__upperdentry); 41409d8b586SMiklos Szeredi 41525b7713aSMiklos Szeredi /* 41609d8b586SMiklos Szeredi * Make sure upperdentry is consistent before making it visible 41725b7713aSMiklos Szeredi */ 41825b7713aSMiklos Szeredi smp_wmb(); 41909d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 42031747edaSAmir Goldstein if (inode_unhashed(inode)) { 42125b7713aSMiklos Szeredi inode->i_private = upperinode; 422bbb1e54dSMiklos Szeredi __insert_inode_hash(inode, (unsigned long) upperinode); 423bbb1e54dSMiklos Szeredi } 42425b7713aSMiklos Szeredi } 425bbb1e54dSMiklos Szeredi 42665cd913eSAmir Goldstein static void ovl_dir_version_inc(struct dentry *dentry, bool impurity) 427bbb1e54dSMiklos Szeredi { 42804a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 429bbb1e54dSMiklos Szeredi 43004a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 43165cd913eSAmir Goldstein WARN_ON(!d_is_dir(dentry)); 4324edb83bbSMiklos Szeredi /* 43365cd913eSAmir Goldstein * Version is used by readdir code to keep cache consistent. 43465cd913eSAmir Goldstein * For merge dirs (or dirs with origin) all changes need to be noted. 43565cd913eSAmir Goldstein * For non-merge dirs, cache contains only impure entries (i.e. ones 43665cd913eSAmir Goldstein * which have been copied up and have origins), so only need to note 43765cd913eSAmir Goldstein * changes to impure entries. 4384edb83bbSMiklos Szeredi */ 43965cd913eSAmir Goldstein if (!ovl_dir_is_real(dentry) || impurity) 44004a01ac7SMiklos Szeredi OVL_I(inode)->version++; 441bbb1e54dSMiklos Szeredi } 442bbb1e54dSMiklos Szeredi 443d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity) 444d9854c87SMiklos Szeredi { 445d9854c87SMiklos Szeredi /* Copy mtime/ctime */ 446d9854c87SMiklos Szeredi ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry)); 447d9854c87SMiklos Szeredi 44865cd913eSAmir Goldstein ovl_dir_version_inc(dentry, impurity); 449d9854c87SMiklos Szeredi } 450d9854c87SMiklos Szeredi 451bbb1e54dSMiklos Szeredi u64 ovl_dentry_version_get(struct dentry *dentry) 452bbb1e54dSMiklos Szeredi { 45304a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 454bbb1e54dSMiklos Szeredi 45504a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 45604a01ac7SMiklos Szeredi return OVL_I(inode)->version; 457bbb1e54dSMiklos Szeredi } 458bbb1e54dSMiklos Szeredi 459bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry) 460bbb1e54dSMiklos Szeredi { 461bbb1e54dSMiklos Szeredi struct inode *inode = dentry->d_inode; 462bbb1e54dSMiklos Szeredi 463bbb1e54dSMiklos Szeredi return inode && IS_WHITEOUT(inode); 464bbb1e54dSMiklos Szeredi } 465bbb1e54dSMiklos Szeredi 466bbb1e54dSMiklos Szeredi struct file *ovl_path_open(struct path *path, int flags) 467bbb1e54dSMiklos Szeredi { 46856230d95SMiklos Szeredi struct inode *inode = d_inode(path->dentry); 46956230d95SMiklos Szeredi int err, acc_mode; 47056230d95SMiklos Szeredi 47156230d95SMiklos Szeredi if (flags & ~(O_ACCMODE | O_LARGEFILE)) 47256230d95SMiklos Szeredi BUG(); 47356230d95SMiklos Szeredi 47456230d95SMiklos Szeredi switch (flags & O_ACCMODE) { 47556230d95SMiklos Szeredi case O_RDONLY: 47656230d95SMiklos Szeredi acc_mode = MAY_READ; 47756230d95SMiklos Szeredi break; 47856230d95SMiklos Szeredi case O_WRONLY: 47956230d95SMiklos Szeredi acc_mode = MAY_WRITE; 48056230d95SMiklos Szeredi break; 48156230d95SMiklos Szeredi default: 48256230d95SMiklos Szeredi BUG(); 48356230d95SMiklos Szeredi } 48456230d95SMiklos Szeredi 48547291baaSChristian Brauner err = inode_permission(&init_user_ns, inode, acc_mode | MAY_OPEN); 48656230d95SMiklos Szeredi if (err) 48756230d95SMiklos Szeredi return ERR_PTR(err); 48856230d95SMiklos Szeredi 48956230d95SMiklos Szeredi /* O_NOATIME is an optimization, don't fail if not permitted */ 49021cb47beSChristian Brauner if (inode_owner_or_capable(&init_user_ns, inode)) 49156230d95SMiklos Szeredi flags |= O_NOATIME; 49256230d95SMiklos Szeredi 49356230d95SMiklos Szeredi return dentry_open(path, flags, current_cred()); 494bbb1e54dSMiklos Szeredi } 49539d3d60aSAmir Goldstein 4960c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 4970c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 4980c288874SVivek Goyal { 4990c288874SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5000c288874SVivek Goyal 5010c288874SVivek Goyal if (ovl_dentry_upper(dentry) && 5020c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 5030c288874SVivek Goyal !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 5040c288874SVivek Goyal return true; 5050c288874SVivek Goyal 5060c288874SVivek Goyal return false; 5070c288874SVivek Goyal } 5080c288874SVivek Goyal 5090c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags) 5102002df85SVivek Goyal { 5112002df85SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5122002df85SVivek Goyal 5132002df85SVivek Goyal /* 5142002df85SVivek Goyal * Check if copy-up has happened as well as for upper alias (in 5152002df85SVivek Goyal * case of hard links) is there. 5162002df85SVivek Goyal * 5172002df85SVivek Goyal * Both checks are lockless: 5182002df85SVivek Goyal * - false negatives: will recheck under oi->lock 5192002df85SVivek Goyal * - false positives: 5202002df85SVivek Goyal * + ovl_dentry_upper() uses memory barriers to ensure the 5212002df85SVivek Goyal * upper dentry is up-to-date 5222002df85SVivek Goyal * + ovl_dentry_has_upper_alias() relies on locking of 5232002df85SVivek Goyal * upper parent i_rwsem to prevent reordering copy-up 5242002df85SVivek Goyal * with rename. 5252002df85SVivek Goyal */ 5262002df85SVivek Goyal if (ovl_dentry_upper(dentry) && 5270c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 5280c288874SVivek Goyal !ovl_dentry_needs_data_copy_up(dentry, flags)) 5292002df85SVivek Goyal return true; 5302002df85SVivek Goyal 5312002df85SVivek Goyal return false; 5322002df85SVivek Goyal } 5332002df85SVivek Goyal 5340c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags) 53539d3d60aSAmir Goldstein { 5361e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 53739d3d60aSAmir Goldstein int err; 53839d3d60aSAmir Goldstein 539531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 5400c288874SVivek Goyal if (!err && ovl_already_copied_up_locked(dentry, flags)) { 54139d3d60aSAmir Goldstein err = 1; /* Already copied up */ 5421e92e307SAmir Goldstein ovl_inode_unlock(inode); 54339d3d60aSAmir Goldstein } 54439d3d60aSAmir Goldstein 54539d3d60aSAmir Goldstein return err; 54639d3d60aSAmir Goldstein } 54739d3d60aSAmir Goldstein 54839d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry) 54939d3d60aSAmir Goldstein { 5501e92e307SAmir Goldstein ovl_inode_unlock(d_inode(dentry)); 55139d3d60aSAmir Goldstein } 55282b749b2SAmir Goldstein 553610afc0bSMiklos Szeredi bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry) 554b79e05aaSAmir Goldstein { 555b79e05aaSAmir Goldstein int res; 556b79e05aaSAmir Goldstein 557c914c0e2SAmir Goldstein res = ovl_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0); 558b79e05aaSAmir Goldstein 559b79e05aaSAmir Goldstein /* Zero size value means "copied up but origin unknown" */ 560b79e05aaSAmir Goldstein if (res >= 0) 561b79e05aaSAmir Goldstein return true; 562b79e05aaSAmir Goldstein 563b79e05aaSAmir Goldstein return false; 564b79e05aaSAmir Goldstein } 565b79e05aaSAmir Goldstein 566610afc0bSMiklos Szeredi bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry, 56743d193f8SMiklos Szeredi enum ovl_xattr ox) 568f3a15685SAmir Goldstein { 569f3a15685SAmir Goldstein int res; 570f3a15685SAmir Goldstein char val; 571f3a15685SAmir Goldstein 572f3a15685SAmir Goldstein if (!d_is_dir(dentry)) 573f3a15685SAmir Goldstein return false; 574f3a15685SAmir Goldstein 575c914c0e2SAmir Goldstein res = ovl_getxattr(OVL_FS(sb), dentry, ox, &val, 1); 576f3a15685SAmir Goldstein if (res == 1 && val == 'y') 577f3a15685SAmir Goldstein return true; 578f3a15685SAmir Goldstein 579f3a15685SAmir Goldstein return false; 580f3a15685SAmir Goldstein } 581f3a15685SAmir Goldstein 58243d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX "opaque" 58343d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX "redirect" 58443d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX "origin" 58543d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX "impure" 58643d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX "nlink" 58743d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX "upper" 58843d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX "metacopy" 589096a218aSAmir Goldstein #define OVL_XATTR_PROTATTR_POSTFIX "protattr" 59043d193f8SMiklos Szeredi 59143d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \ 5922d2f2d73SMiklos Szeredi [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \ 5932d2f2d73SMiklos Szeredi [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX } 59443d193f8SMiklos Szeredi 5952d2f2d73SMiklos Szeredi const char *const ovl_xattr_table[][2] = { 59643d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE), 59743d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT), 59843d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN), 59943d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE), 60043d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK), 60143d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER), 60243d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY), 603096a218aSAmir Goldstein OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR), 60443d193f8SMiklos Szeredi }; 60543d193f8SMiklos Szeredi 606a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry, 60743d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, size_t size, 60882b749b2SAmir Goldstein int xerr) 60982b749b2SAmir Goldstein { 61082b749b2SAmir Goldstein int err; 61182b749b2SAmir Goldstein 61282b749b2SAmir Goldstein if (ofs->noxattr) 61382b749b2SAmir Goldstein return xerr; 61482b749b2SAmir Goldstein 615c914c0e2SAmir Goldstein err = ovl_setxattr(ofs, upperdentry, ox, value, size); 61682b749b2SAmir Goldstein 61782b749b2SAmir Goldstein if (err == -EOPNOTSUPP) { 61843d193f8SMiklos Szeredi pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox)); 61982b749b2SAmir Goldstein ofs->noxattr = true; 62082b749b2SAmir Goldstein return xerr; 62182b749b2SAmir Goldstein } 62282b749b2SAmir Goldstein 62382b749b2SAmir Goldstein return err; 62482b749b2SAmir Goldstein } 625f3a15685SAmir Goldstein 626f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 627f3a15685SAmir Goldstein { 628a0c236b1SAmir Goldstein struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 629f3a15685SAmir Goldstein int err; 630f3a15685SAmir Goldstein 63113c72075SMiklos Szeredi if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 632f3a15685SAmir Goldstein return 0; 633f3a15685SAmir Goldstein 634f3a15685SAmir Goldstein /* 635f3a15685SAmir Goldstein * Do not fail when upper doesn't support xattrs. 636f3a15685SAmir Goldstein * Upper inodes won't have origin nor redirect xattr anyway. 637f3a15685SAmir Goldstein */ 638a0c236b1SAmir Goldstein err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0); 639f3a15685SAmir Goldstein if (!err) 64013c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 641f3a15685SAmir Goldstein 642f3a15685SAmir Goldstein return err; 643f3a15685SAmir Goldstein } 64413c72075SMiklos Szeredi 645096a218aSAmir Goldstein 646096a218aSAmir Goldstein #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */ 647096a218aSAmir Goldstein 648096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper) 649096a218aSAmir Goldstein { 650096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 651096a218aSAmir Goldstein u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK; 652096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX+1]; 653096a218aSAmir Goldstein int res, n; 654096a218aSAmir Goldstein 655c914c0e2SAmir Goldstein res = ovl_getxattr(ofs, upper, OVL_XATTR_PROTATTR, buf, 656096a218aSAmir Goldstein OVL_PROTATTR_MAX); 657096a218aSAmir Goldstein if (res < 0) 658096a218aSAmir Goldstein return; 659096a218aSAmir Goldstein 660096a218aSAmir Goldstein /* 661096a218aSAmir Goldstein * Initialize inode flags from overlay.protattr xattr and upper inode 662096a218aSAmir Goldstein * flags. If upper inode has those fileattr flags set (i.e. from old 663096a218aSAmir Goldstein * kernel), we do not clear them on ovl_get_inode(), but we will clear 664096a218aSAmir Goldstein * them on next fileattr_set(). 665096a218aSAmir Goldstein */ 666096a218aSAmir Goldstein for (n = 0; n < res; n++) { 667096a218aSAmir Goldstein if (buf[n] == 'a') 668096a218aSAmir Goldstein iflags |= S_APPEND; 669096a218aSAmir Goldstein else if (buf[n] == 'i') 670096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 671096a218aSAmir Goldstein else 672096a218aSAmir Goldstein break; 673096a218aSAmir Goldstein } 674096a218aSAmir Goldstein 675096a218aSAmir Goldstein if (!res || n < res) { 676096a218aSAmir Goldstein pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n", 677096a218aSAmir Goldstein upper, res); 678096a218aSAmir Goldstein } else { 679096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 680096a218aSAmir Goldstein } 681096a218aSAmir Goldstein } 682096a218aSAmir Goldstein 683096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper, 684096a218aSAmir Goldstein struct fileattr *fa) 685096a218aSAmir Goldstein { 686096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 687096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX]; 688096a218aSAmir Goldstein int len = 0, err = 0; 689096a218aSAmir Goldstein u32 iflags = 0; 690096a218aSAmir Goldstein 691096a218aSAmir Goldstein BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX); 692096a218aSAmir Goldstein 693096a218aSAmir Goldstein if (fa->flags & FS_APPEND_FL) { 694096a218aSAmir Goldstein buf[len++] = 'a'; 695096a218aSAmir Goldstein iflags |= S_APPEND; 696096a218aSAmir Goldstein } 697096a218aSAmir Goldstein if (fa->flags & FS_IMMUTABLE_FL) { 698096a218aSAmir Goldstein buf[len++] = 'i'; 699096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 700096a218aSAmir Goldstein } 701096a218aSAmir Goldstein 702096a218aSAmir Goldstein /* 703096a218aSAmir Goldstein * Do not allow to set protection flags when upper doesn't support 704096a218aSAmir Goldstein * xattrs, because we do not set those fileattr flags on upper inode. 705096a218aSAmir Goldstein * Remove xattr if it exist and all protection flags are cleared. 706096a218aSAmir Goldstein */ 707096a218aSAmir Goldstein if (len) { 708096a218aSAmir Goldstein err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR, 709096a218aSAmir Goldstein buf, len, -EPERM); 710096a218aSAmir Goldstein } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) { 711c914c0e2SAmir Goldstein err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR); 712096a218aSAmir Goldstein if (err == -EOPNOTSUPP || err == -ENODATA) 713096a218aSAmir Goldstein err = 0; 714096a218aSAmir Goldstein } 715096a218aSAmir Goldstein if (err) 716096a218aSAmir Goldstein return err; 717096a218aSAmir Goldstein 718096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 719096a218aSAmir Goldstein 720096a218aSAmir Goldstein /* Mask out the fileattr flags that should not be set in upper inode */ 721096a218aSAmir Goldstein fa->flags &= ~OVL_PROT_FS_FLAGS_MASK; 722096a218aSAmir Goldstein fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK; 723096a218aSAmir Goldstein 724096a218aSAmir Goldstein return 0; 725096a218aSAmir Goldstein } 726096a218aSAmir Goldstein 727ad0af710SAmir Goldstein /** 728ad0af710SAmir Goldstein * Caller must hold a reference to inode to prevent it from being freed while 729ad0af710SAmir Goldstein * it is marked inuse. 730ad0af710SAmir Goldstein */ 731ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry) 732ad0af710SAmir Goldstein { 733ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 734ad0af710SAmir Goldstein bool locked = false; 735ad0af710SAmir Goldstein 736ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 737ad0af710SAmir Goldstein if (!(inode->i_state & I_OVL_INUSE)) { 738ad0af710SAmir Goldstein inode->i_state |= I_OVL_INUSE; 739ad0af710SAmir Goldstein locked = true; 740ad0af710SAmir Goldstein } 741ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 742ad0af710SAmir Goldstein 743ad0af710SAmir Goldstein return locked; 744ad0af710SAmir Goldstein } 745ad0af710SAmir Goldstein 746ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry) 747ad0af710SAmir Goldstein { 748ad0af710SAmir Goldstein if (dentry) { 749ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 750ad0af710SAmir Goldstein 751ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 752ad0af710SAmir Goldstein WARN_ON(!(inode->i_state & I_OVL_INUSE)); 753ad0af710SAmir Goldstein inode->i_state &= ~I_OVL_INUSE; 754ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 755ad0af710SAmir Goldstein } 756ad0af710SAmir Goldstein } 7575f8415d6SAmir Goldstein 758146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry) 759146d62e5SAmir Goldstein { 760146d62e5SAmir Goldstein struct inode *inode = d_inode(dentry); 761146d62e5SAmir Goldstein bool inuse; 762146d62e5SAmir Goldstein 763146d62e5SAmir Goldstein spin_lock(&inode->i_lock); 764146d62e5SAmir Goldstein inuse = (inode->i_state & I_OVL_INUSE); 765146d62e5SAmir Goldstein spin_unlock(&inode->i_lock); 766146d62e5SAmir Goldstein 767146d62e5SAmir Goldstein return inuse; 768146d62e5SAmir Goldstein } 769146d62e5SAmir Goldstein 77024b33ee1SAmir Goldstein /* 77124b33ee1SAmir Goldstein * Does this overlay dentry need to be indexed on copy up? 77224b33ee1SAmir Goldstein */ 77324b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry) 77424b33ee1SAmir Goldstein { 77524b33ee1SAmir Goldstein struct dentry *lower = ovl_dentry_lower(dentry); 77624b33ee1SAmir Goldstein 77724b33ee1SAmir Goldstein if (!lower || !ovl_indexdir(dentry->d_sb)) 77824b33ee1SAmir Goldstein return false; 77924b33ee1SAmir Goldstein 780fbd2d207SAmir Goldstein /* Index all files for NFS export and consistency verification */ 781016b720fSAmir Goldstein if (ovl_index_all(dentry->d_sb)) 782fbd2d207SAmir Goldstein return true; 783fbd2d207SAmir Goldstein 78424b33ee1SAmir Goldstein /* Index only lower hardlinks on copy up */ 78524b33ee1SAmir Goldstein if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 78624b33ee1SAmir Goldstein return true; 78724b33ee1SAmir Goldstein 78824b33ee1SAmir Goldstein return false; 78924b33ee1SAmir Goldstein } 79024b33ee1SAmir Goldstein 7919f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */ 792caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry) 793caf70cb2SAmir Goldstein { 7941cdb0cb6SPavel Tikhomirov struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 795e7dd0e71SAmir Goldstein struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 796e7dd0e71SAmir Goldstein struct inode *dir = indexdir->d_inode; 797caf70cb2SAmir Goldstein struct dentry *lowerdentry = ovl_dentry_lower(dentry); 798caf70cb2SAmir Goldstein struct dentry *upperdentry = ovl_dentry_upper(dentry); 799caf70cb2SAmir Goldstein struct dentry *index = NULL; 800caf70cb2SAmir Goldstein struct inode *inode; 80163e13252SAmir Goldstein struct qstr name = { }; 802caf70cb2SAmir Goldstein int err; 803caf70cb2SAmir Goldstein 8041cdb0cb6SPavel Tikhomirov err = ovl_get_index_name(ofs, lowerdentry, &name); 805caf70cb2SAmir Goldstein if (err) 806caf70cb2SAmir Goldstein goto fail; 807caf70cb2SAmir Goldstein 808caf70cb2SAmir Goldstein inode = d_inode(upperdentry); 80989a17556SAmir Goldstein if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 8101bd0a3aeSlijiazi pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 811caf70cb2SAmir Goldstein upperdentry, inode->i_ino, inode->i_nlink); 812caf70cb2SAmir Goldstein /* 813caf70cb2SAmir Goldstein * We either have a bug with persistent union nlink or a lower 814caf70cb2SAmir Goldstein * hardlink was added while overlay is mounted. Adding a lower 815caf70cb2SAmir Goldstein * hardlink and then unlinking all overlay hardlinks would drop 816caf70cb2SAmir Goldstein * overlay nlink to zero before all upper inodes are unlinked. 817caf70cb2SAmir Goldstein * As a safety measure, when that situation is detected, set 818caf70cb2SAmir Goldstein * the overlay nlink to the index inode nlink minus one for the 819caf70cb2SAmir Goldstein * index entry itself. 820caf70cb2SAmir Goldstein */ 821caf70cb2SAmir Goldstein set_nlink(d_inode(dentry), inode->i_nlink - 1); 822caf70cb2SAmir Goldstein ovl_set_nlink_upper(dentry); 823caf70cb2SAmir Goldstein goto out; 824caf70cb2SAmir Goldstein } 825caf70cb2SAmir Goldstein 826caf70cb2SAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 827e7dd0e71SAmir Goldstein index = lookup_one_len(name.name, indexdir, name.len); 828caf70cb2SAmir Goldstein err = PTR_ERR(index); 829e7dd0e71SAmir Goldstein if (IS_ERR(index)) { 8309f4ec904SAmir Goldstein index = NULL; 831e7dd0e71SAmir Goldstein } else if (ovl_index_all(dentry->d_sb)) { 832e7dd0e71SAmir Goldstein /* Whiteout orphan index to block future open by handle */ 833c21c839bSChengguang Xu err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), 834c21c839bSChengguang Xu dir, index); 835e7dd0e71SAmir Goldstein } else { 836e7dd0e71SAmir Goldstein /* Cleanup orphan index entries */ 837*576bb263SChristian Brauner err = ovl_cleanup(ofs, dir, index); 838e7dd0e71SAmir Goldstein } 8399f4ec904SAmir Goldstein 840caf70cb2SAmir Goldstein inode_unlock(dir); 841caf70cb2SAmir Goldstein if (err) 842caf70cb2SAmir Goldstein goto fail; 843caf70cb2SAmir Goldstein 844caf70cb2SAmir Goldstein out: 84563e13252SAmir Goldstein kfree(name.name); 846caf70cb2SAmir Goldstein dput(index); 847caf70cb2SAmir Goldstein return; 848caf70cb2SAmir Goldstein 849caf70cb2SAmir Goldstein fail: 8501bd0a3aeSlijiazi pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); 851caf70cb2SAmir Goldstein goto out; 852caf70cb2SAmir Goldstein } 853caf70cb2SAmir Goldstein 8545f8415d6SAmir Goldstein /* 8555f8415d6SAmir Goldstein * Operations that change overlay inode and upper inode nlink need to be 8565f8415d6SAmir Goldstein * synchronized with copy up for persistent nlink accounting. 8575f8415d6SAmir Goldstein */ 8580e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry) 8595f8415d6SAmir Goldstein { 8601e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 8615f8415d6SAmir Goldstein const struct cred *old_cred; 8625f8415d6SAmir Goldstein int err; 8635f8415d6SAmir Goldstein 8641e92e307SAmir Goldstein if (WARN_ON(!inode)) 8650e32992fSAmir Goldstein return -ENOENT; 8665f8415d6SAmir Goldstein 8675f8415d6SAmir Goldstein /* 8685f8415d6SAmir Goldstein * With inodes index is enabled, we store the union overlay nlink 86924b33ee1SAmir Goldstein * in an xattr on the index inode. When whiting out an indexed lower, 8705f8415d6SAmir Goldstein * we need to decrement the overlay persistent nlink, but before the 8715f8415d6SAmir Goldstein * first copy up, we have no upper index inode to store the xattr. 8725f8415d6SAmir Goldstein * 87324b33ee1SAmir Goldstein * As a workaround, before whiteout/rename over an indexed lower, 8745f8415d6SAmir Goldstein * copy up to create the upper index. Creating the upper index will 8755f8415d6SAmir Goldstein * initialize the overlay nlink, so it could be dropped if unlink 8765f8415d6SAmir Goldstein * or rename succeeds. 8775f8415d6SAmir Goldstein * 8785f8415d6SAmir Goldstein * TODO: implement metadata only index copy up when called with 8795f8415d6SAmir Goldstein * ovl_copy_up_flags(dentry, O_PATH). 8805f8415d6SAmir Goldstein */ 88124b33ee1SAmir Goldstein if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 8825f8415d6SAmir Goldstein err = ovl_copy_up(dentry); 8835f8415d6SAmir Goldstein if (err) 8845f8415d6SAmir Goldstein return err; 8855f8415d6SAmir Goldstein } 8865f8415d6SAmir Goldstein 887531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 8885f8415d6SAmir Goldstein if (err) 8895f8415d6SAmir Goldstein return err; 8905f8415d6SAmir Goldstein 8911e92e307SAmir Goldstein if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 8925f8415d6SAmir Goldstein goto out; 8935f8415d6SAmir Goldstein 8945f8415d6SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 8955f8415d6SAmir Goldstein /* 8965f8415d6SAmir Goldstein * The overlay inode nlink should be incremented/decremented IFF the 8975f8415d6SAmir Goldstein * upper operation succeeds, along with nlink change of upper inode. 8985f8415d6SAmir Goldstein * Therefore, before link/unlink/rename, we store the union nlink 8995f8415d6SAmir Goldstein * value relative to the upper inode nlink in an upper inode xattr. 9005f8415d6SAmir Goldstein */ 9015f8415d6SAmir Goldstein err = ovl_set_nlink_upper(dentry); 9025f8415d6SAmir Goldstein revert_creds(old_cred); 9035f8415d6SAmir Goldstein 9045f8415d6SAmir Goldstein out: 9055f8415d6SAmir Goldstein if (err) 9061e92e307SAmir Goldstein ovl_inode_unlock(inode); 9075f8415d6SAmir Goldstein 9085f8415d6SAmir Goldstein return err; 9095f8415d6SAmir Goldstein } 9105f8415d6SAmir Goldstein 9110e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry) 9125f8415d6SAmir Goldstein { 9131e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 9141e92e307SAmir Goldstein 9151e92e307SAmir Goldstein if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 916caf70cb2SAmir Goldstein const struct cred *old_cred; 917caf70cb2SAmir Goldstein 918caf70cb2SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 919caf70cb2SAmir Goldstein ovl_cleanup_index(dentry); 920caf70cb2SAmir Goldstein revert_creds(old_cred); 921caf70cb2SAmir Goldstein } 922caf70cb2SAmir Goldstein 9231e92e307SAmir Goldstein ovl_inode_unlock(inode); 9245f8415d6SAmir Goldstein } 9255820dc08SAmir Goldstein 9265820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) 9275820dc08SAmir Goldstein { 9285820dc08SAmir Goldstein /* Workdir should not be the same as upperdir */ 9295820dc08SAmir Goldstein if (workdir == upperdir) 9305820dc08SAmir Goldstein goto err; 9315820dc08SAmir Goldstein 9325820dc08SAmir Goldstein /* Workdir should not be subdir of upperdir and vice versa */ 9335820dc08SAmir Goldstein if (lock_rename(workdir, upperdir) != NULL) 9345820dc08SAmir Goldstein goto err_unlock; 9355820dc08SAmir Goldstein 9365820dc08SAmir Goldstein return 0; 9375820dc08SAmir Goldstein 9385820dc08SAmir Goldstein err_unlock: 9395820dc08SAmir Goldstein unlock_rename(workdir, upperdir); 9405820dc08SAmir Goldstein err: 9411bd0a3aeSlijiazi pr_err("failed to lock workdir+upperdir\n"); 9425820dc08SAmir Goldstein return -EIO; 9435820dc08SAmir Goldstein } 9449d3dfea3SVivek Goyal 9459d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ 946610afc0bSMiklos Szeredi int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry) 9479d3dfea3SVivek Goyal { 9489d3dfea3SVivek Goyal int res; 9499d3dfea3SVivek Goyal 9509d3dfea3SVivek Goyal /* Only regular files can have metacopy xattr */ 9519d3dfea3SVivek Goyal if (!S_ISREG(d_inode(dentry)->i_mode)) 9529d3dfea3SVivek Goyal return 0; 9539d3dfea3SVivek Goyal 954c914c0e2SAmir Goldstein res = ovl_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0); 9559d3dfea3SVivek Goyal if (res < 0) { 9569d3dfea3SVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 9579d3dfea3SVivek Goyal return 0; 95887b2c60cSMiklos Szeredi /* 95987b2c60cSMiklos Szeredi * getxattr on user.* may fail with EACCES in case there's no 96087b2c60cSMiklos Szeredi * read permission on the inode. Not much we can do, other than 96187b2c60cSMiklos Szeredi * tell the caller that this is not a metacopy inode. 96287b2c60cSMiklos Szeredi */ 96387b2c60cSMiklos Szeredi if (ofs->config.userxattr && res == -EACCES) 96487b2c60cSMiklos Szeredi return 0; 9659d3dfea3SVivek Goyal goto out; 9669d3dfea3SVivek Goyal } 9679d3dfea3SVivek Goyal 9689d3dfea3SVivek Goyal return 1; 9699d3dfea3SVivek Goyal out: 9701bd0a3aeSlijiazi pr_warn_ratelimited("failed to get metacopy (%i)\n", res); 9719d3dfea3SVivek Goyal return res; 9729d3dfea3SVivek Goyal } 97367d756c2SVivek Goyal 97467d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry) 97567d756c2SVivek Goyal { 97667d756c2SVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 97767d756c2SVivek Goyal 97867d756c2SVivek Goyal if (!d_is_reg(dentry)) 97967d756c2SVivek Goyal return false; 98067d756c2SVivek Goyal 98167d756c2SVivek Goyal if (ovl_dentry_upper(dentry)) { 98267d756c2SVivek Goyal if (!ovl_has_upperdata(d_inode(dentry))) 98367d756c2SVivek Goyal return true; 98467d756c2SVivek Goyal return false; 98567d756c2SVivek Goyal } 98667d756c2SVivek Goyal 98767d756c2SVivek Goyal return (oe->numlower > 1); 98867d756c2SVivek Goyal } 9890a2d0d3fSVivek Goyal 990610afc0bSMiklos Szeredi char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry, 991610afc0bSMiklos Szeredi int padding) 9920a2d0d3fSVivek Goyal { 9930a2d0d3fSVivek Goyal int res; 9940a2d0d3fSVivek Goyal char *s, *next, *buf = NULL; 9950a2d0d3fSVivek Goyal 996c914c0e2SAmir Goldstein res = ovl_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0); 99792f0d6c9SMiklos Szeredi if (res == -ENODATA || res == -EOPNOTSUPP) 9980a2d0d3fSVivek Goyal return NULL; 9990a2d0d3fSVivek Goyal if (res < 0) 100092f0d6c9SMiklos Szeredi goto fail; 100192f0d6c9SMiklos Szeredi if (res == 0) 100292f0d6c9SMiklos Szeredi goto invalid; 100392f0d6c9SMiklos Szeredi 100492f0d6c9SMiklos Szeredi buf = kzalloc(res + padding + 1, GFP_KERNEL); 100592f0d6c9SMiklos Szeredi if (!buf) 100692f0d6c9SMiklos Szeredi return ERR_PTR(-ENOMEM); 100792f0d6c9SMiklos Szeredi 1008c914c0e2SAmir Goldstein res = ovl_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res); 100992f0d6c9SMiklos Szeredi if (res < 0) 101092f0d6c9SMiklos Szeredi goto fail; 10110a2d0d3fSVivek Goyal if (res == 0) 10120a2d0d3fSVivek Goyal goto invalid; 10130a2d0d3fSVivek Goyal 10140a2d0d3fSVivek Goyal if (buf[0] == '/') { 10150a2d0d3fSVivek Goyal for (s = buf; *s++ == '/'; s = next) { 10160a2d0d3fSVivek Goyal next = strchrnul(s, '/'); 10170a2d0d3fSVivek Goyal if (s == next) 10180a2d0d3fSVivek Goyal goto invalid; 10190a2d0d3fSVivek Goyal } 10200a2d0d3fSVivek Goyal } else { 10210a2d0d3fSVivek Goyal if (strchr(buf, '/') != NULL) 10220a2d0d3fSVivek Goyal goto invalid; 10230a2d0d3fSVivek Goyal } 10240a2d0d3fSVivek Goyal 10250a2d0d3fSVivek Goyal return buf; 10260a2d0d3fSVivek Goyal invalid: 10271bd0a3aeSlijiazi pr_warn_ratelimited("invalid redirect (%s)\n", buf); 10280a2d0d3fSVivek Goyal res = -EINVAL; 102992f0d6c9SMiklos Szeredi goto err_free; 103092f0d6c9SMiklos Szeredi fail: 103192f0d6c9SMiklos Szeredi pr_warn_ratelimited("failed to get redirect (%i)\n", res); 103292f0d6c9SMiklos Szeredi err_free: 1033993a0b2aSVivek Goyal kfree(buf); 1034993a0b2aSVivek Goyal return ERR_PTR(res); 10350a2d0d3fSVivek Goyal } 1036335d3fc5SSargun Dhillon 1037335d3fc5SSargun Dhillon /* 1038335d3fc5SSargun Dhillon * ovl_sync_status() - Check fs sync status for volatile mounts 1039335d3fc5SSargun Dhillon * 1040335d3fc5SSargun Dhillon * Returns 1 if this is not a volatile mount and a real sync is required. 1041335d3fc5SSargun Dhillon * 1042335d3fc5SSargun Dhillon * Returns 0 if syncing can be skipped because mount is volatile, and no errors 1043335d3fc5SSargun Dhillon * have occurred on the upperdir since the mount. 1044335d3fc5SSargun Dhillon * 1045335d3fc5SSargun Dhillon * Returns -errno if it is a volatile mount, and the error that occurred since 1046335d3fc5SSargun Dhillon * the last mount. If the error code changes, it'll return the latest error 1047335d3fc5SSargun Dhillon * code. 1048335d3fc5SSargun Dhillon */ 1049335d3fc5SSargun Dhillon 1050335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs) 1051335d3fc5SSargun Dhillon { 1052335d3fc5SSargun Dhillon struct vfsmount *mnt; 1053335d3fc5SSargun Dhillon 1054335d3fc5SSargun Dhillon if (ovl_should_sync(ofs)) 1055335d3fc5SSargun Dhillon return 1; 1056335d3fc5SSargun Dhillon 1057335d3fc5SSargun Dhillon mnt = ovl_upper_mnt(ofs); 1058335d3fc5SSargun Dhillon if (!mnt) 1059335d3fc5SSargun Dhillon return 0; 1060335d3fc5SSargun Dhillon 1061335d3fc5SSargun Dhillon return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq); 1062335d3fc5SSargun Dhillon } 1063