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> 1302bcd157SAmir Goldstein #include <linux/uuid.h> 14caf70cb2SAmir Goldstein #include <linux/namei.h> 15caf70cb2SAmir Goldstein #include <linux/ratelimit.h> 16bbb1e54dSMiklos Szeredi #include "overlayfs.h" 17bbb1e54dSMiklos Szeredi 18bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry) 19bbb1e54dSMiklos Szeredi { 20bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 21bbb1e54dSMiklos Szeredi return mnt_want_write(ofs->upper_mnt); 22bbb1e54dSMiklos Szeredi } 23bbb1e54dSMiklos Szeredi 24bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry) 25bbb1e54dSMiklos Szeredi { 26bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 27bbb1e54dSMiklos Szeredi mnt_drop_write(ofs->upper_mnt); 28bbb1e54dSMiklos Szeredi } 29bbb1e54dSMiklos Szeredi 30bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry) 31bbb1e54dSMiklos Szeredi { 32bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 33bbb1e54dSMiklos Szeredi return ofs->workdir; 34bbb1e54dSMiklos Szeredi } 35bbb1e54dSMiklos Szeredi 36bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb) 37bbb1e54dSMiklos Szeredi { 38bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 39bbb1e54dSMiklos Szeredi 40bbb1e54dSMiklos Szeredi return override_creds(ofs->creator_cred); 41bbb1e54dSMiklos Szeredi } 42bbb1e54dSMiklos Szeredi 43e487d889SAmir Goldstein /* 44e487d889SAmir Goldstein * Check if underlying fs supports file handles and try to determine encoding 45e487d889SAmir Goldstein * type, in order to deduce maximum inode number used by fs. 46e487d889SAmir Goldstein * 47e487d889SAmir Goldstein * Return 0 if file handles are not supported. 48e487d889SAmir Goldstein * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. 49e487d889SAmir Goldstein * Return -1 if fs uses a non default encoding with unknown inode size. 50e487d889SAmir Goldstein */ 51e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb) 5202bcd157SAmir Goldstein { 539df085f3SAmir Goldstein if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry) 54e487d889SAmir Goldstein return 0; 55e487d889SAmir Goldstein 56e487d889SAmir Goldstein return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; 5702bcd157SAmir Goldstein } 5802bcd157SAmir Goldstein 5902bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb) 6002bcd157SAmir Goldstein { 6102bcd157SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 6202bcd157SAmir Goldstein 6302bcd157SAmir Goldstein return ofs->indexdir; 6402bcd157SAmir Goldstein } 6502bcd157SAmir Goldstein 66f168f109SAmir Goldstein /* Index all files on copy up. For now only enabled for NFS export */ 67f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb) 68f168f109SAmir Goldstein { 69f168f109SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 70f168f109SAmir Goldstein 71f168f109SAmir Goldstein return ofs->config.nfs_export && ofs->config.index; 72f168f109SAmir Goldstein } 73f168f109SAmir Goldstein 74f168f109SAmir Goldstein /* Verify lower origin on lookup. For now only enabled for NFS export */ 75f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb) 76f168f109SAmir Goldstein { 77f168f109SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 78f168f109SAmir Goldstein 79f168f109SAmir Goldstein return ofs->config.nfs_export && ofs->config.index; 80f168f109SAmir Goldstein } 81f168f109SAmir Goldstein 82bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 83bbb1e54dSMiklos Szeredi { 84bbb1e54dSMiklos Szeredi size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 85bbb1e54dSMiklos Szeredi struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 86bbb1e54dSMiklos Szeredi 87bbb1e54dSMiklos Szeredi if (oe) 88bbb1e54dSMiklos Szeredi oe->numlower = numlower; 89bbb1e54dSMiklos Szeredi 90bbb1e54dSMiklos Szeredi return oe; 91bbb1e54dSMiklos Szeredi } 92bbb1e54dSMiklos Szeredi 93bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry) 94bbb1e54dSMiklos Szeredi { 95bbb1e54dSMiklos Szeredi return dentry->d_flags & 96bbb1e54dSMiklos Szeredi (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | 97bbb1e54dSMiklos Szeredi DCACHE_OP_REAL); 98bbb1e54dSMiklos Szeredi } 99bbb1e54dSMiklos Szeredi 100bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry) 101bbb1e54dSMiklos Szeredi { 102bbb1e54dSMiklos Szeredi return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 103bbb1e54dSMiklos Szeredi DCACHE_MANAGE_TRANSIT | 104bbb1e54dSMiklos Szeredi DCACHE_OP_HASH | 105bbb1e54dSMiklos Szeredi DCACHE_OP_COMPARE); 106bbb1e54dSMiklos Szeredi } 107bbb1e54dSMiklos Szeredi 108bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry) 109bbb1e54dSMiklos Szeredi { 110bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 111bbb1e54dSMiklos Szeredi enum ovl_path_type type = 0; 112bbb1e54dSMiklos Szeredi 11309d8b586SMiklos Szeredi if (ovl_dentry_upper(dentry)) { 114bbb1e54dSMiklos Szeredi type = __OVL_PATH_UPPER; 115bbb1e54dSMiklos Szeredi 116bbb1e54dSMiklos Szeredi /* 11759548503SAmir Goldstein * Non-dir dentry can hold lower dentry of its copy up origin. 118bbb1e54dSMiklos Szeredi */ 11959548503SAmir Goldstein if (oe->numlower) { 12060124877SVivek Goyal if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) 12159548503SAmir Goldstein type |= __OVL_PATH_ORIGIN; 1220b17c28aSVivek Goyal if (d_is_dir(dentry) || 1230b17c28aSVivek Goyal !ovl_has_upperdata(d_inode(dentry))) 124bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 12559548503SAmir Goldstein } 126bbb1e54dSMiklos Szeredi } else { 127bbb1e54dSMiklos Szeredi if (oe->numlower > 1) 128bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 129bbb1e54dSMiklos Szeredi } 130bbb1e54dSMiklos Szeredi return type; 131bbb1e54dSMiklos Szeredi } 132bbb1e54dSMiklos Szeredi 133bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path) 134bbb1e54dSMiklos Szeredi { 135bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 136bbb1e54dSMiklos Szeredi 137bbb1e54dSMiklos Szeredi path->mnt = ofs->upper_mnt; 13809d8b586SMiklos Szeredi path->dentry = ovl_dentry_upper(dentry); 139bbb1e54dSMiklos Szeredi } 140bbb1e54dSMiklos Szeredi 141bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path) 142bbb1e54dSMiklos Szeredi { 143bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 144bbb1e54dSMiklos Szeredi 145b9343632SChandan Rajendra if (oe->numlower) { 146b9343632SChandan Rajendra path->mnt = oe->lowerstack[0].layer->mnt; 147b9343632SChandan Rajendra path->dentry = oe->lowerstack[0].dentry; 148b9343632SChandan Rajendra } else { 149b9343632SChandan Rajendra *path = (struct path) { }; 150b9343632SChandan Rajendra } 151bbb1e54dSMiklos Szeredi } 152bbb1e54dSMiklos Szeredi 1534f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path) 1544f93b426SVivek Goyal { 1554f93b426SVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 1564f93b426SVivek Goyal 1574f93b426SVivek Goyal if (oe->numlower) { 1584f93b426SVivek Goyal path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt; 1594f93b426SVivek Goyal path->dentry = oe->lowerstack[oe->numlower - 1].dentry; 1604f93b426SVivek Goyal } else { 1614f93b426SVivek Goyal *path = (struct path) { }; 1624f93b426SVivek Goyal } 1634f93b426SVivek Goyal } 1644f93b426SVivek Goyal 165bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 166bbb1e54dSMiklos Szeredi { 167bbb1e54dSMiklos Szeredi enum ovl_path_type type = ovl_path_type(dentry); 168bbb1e54dSMiklos Szeredi 169bbb1e54dSMiklos Szeredi if (!OVL_TYPE_UPPER(type)) 170bbb1e54dSMiklos Szeredi ovl_path_lower(dentry, path); 171bbb1e54dSMiklos Szeredi else 172bbb1e54dSMiklos Szeredi ovl_path_upper(dentry, path); 173bbb1e54dSMiklos Szeredi 174bbb1e54dSMiklos Szeredi return type; 175bbb1e54dSMiklos Szeredi } 176bbb1e54dSMiklos Szeredi 177bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry) 178bbb1e54dSMiklos Szeredi { 17909d8b586SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); 180bbb1e54dSMiklos Szeredi } 181bbb1e54dSMiklos Szeredi 182bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry) 183bbb1e54dSMiklos Szeredi { 184bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 185bbb1e54dSMiklos Szeredi 18609d8b586SMiklos Szeredi return oe->numlower ? oe->lowerstack[0].dentry : NULL; 187bbb1e54dSMiklos Szeredi } 188bbb1e54dSMiklos Szeredi 18913464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) 190da309e8cSAmir Goldstein { 191da309e8cSAmir Goldstein struct ovl_entry *oe = dentry->d_fsdata; 192da309e8cSAmir Goldstein 193da309e8cSAmir Goldstein return oe->numlower ? oe->lowerstack[0].layer : NULL; 194da309e8cSAmir Goldstein } 195da309e8cSAmir Goldstein 196647d253fSVivek Goyal /* 197647d253fSVivek Goyal * ovl_dentry_lower() could return either a data dentry or metacopy dentry 198647d253fSVivek Goyal * dependig on what is stored in lowerstack[0]. At times we need to find 199647d253fSVivek Goyal * lower dentry which has data (and not metacopy dentry). This helper 200647d253fSVivek Goyal * returns the lower data dentry. 201647d253fSVivek Goyal */ 202647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) 203647d253fSVivek Goyal { 204647d253fSVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 205647d253fSVivek Goyal 206647d253fSVivek Goyal return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL; 207647d253fSVivek Goyal } 208647d253fSVivek Goyal 209bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry) 210bbb1e54dSMiklos Szeredi { 21109d8b586SMiklos Szeredi return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 212bbb1e54dSMiklos Szeredi } 213bbb1e54dSMiklos Szeredi 2141d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode) 2151d88f183SMiklos Szeredi { 2161d88f183SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(inode)); 2171d88f183SMiklos Szeredi } 2181d88f183SMiklos Szeredi 21909d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode) 22025b7713aSMiklos Szeredi { 2211d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 22225b7713aSMiklos Szeredi 22309d8b586SMiklos Szeredi return upperdentry ? d_inode(upperdentry) : NULL; 22425b7713aSMiklos Szeredi } 22525b7713aSMiklos Szeredi 22609d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode) 22709d8b586SMiklos Szeredi { 22809d8b586SMiklos Szeredi return OVL_I(inode)->lower; 22909d8b586SMiklos Szeredi } 23009d8b586SMiklos Szeredi 23109d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode) 23209d8b586SMiklos Szeredi { 23309d8b586SMiklos Szeredi return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 23409d8b586SMiklos Szeredi } 23509d8b586SMiklos Szeredi 2362664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */ 2372664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode) 2382664bd08SVivek Goyal { 2392664bd08SVivek Goyal if (WARN_ON(!S_ISREG(inode->i_mode))) 2402664bd08SVivek Goyal return NULL; 2412664bd08SVivek Goyal 2422664bd08SVivek Goyal return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); 2432664bd08SVivek Goyal } 24409d8b586SMiklos Szeredi 2454823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */ 2464823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode) 2474823d49cSVivek Goyal { 2484823d49cSVivek Goyal struct inode *upperinode; 2494823d49cSVivek Goyal 2504823d49cSVivek Goyal upperinode = ovl_inode_upper(inode); 2514823d49cSVivek Goyal if (upperinode && ovl_has_upperdata(inode)) 2524823d49cSVivek Goyal return upperinode; 2534823d49cSVivek Goyal 2544823d49cSVivek Goyal return ovl_inode_lowerdata(inode); 2554823d49cSVivek Goyal } 2564823d49cSVivek Goyal 2574edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 258bbb1e54dSMiklos Szeredi { 2594edb83bbSMiklos Szeredi return OVL_I(inode)->cache; 260bbb1e54dSMiklos Szeredi } 261bbb1e54dSMiklos Szeredi 2624edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 263bbb1e54dSMiklos Szeredi { 2644edb83bbSMiklos Szeredi OVL_I(inode)->cache = cache; 265bbb1e54dSMiklos Szeredi } 266bbb1e54dSMiklos Szeredi 267c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 268c62520a8SAmir Goldstein { 269c62520a8SAmir Goldstein set_bit(flag, &OVL_E(dentry)->flags); 270c62520a8SAmir Goldstein } 271c62520a8SAmir Goldstein 272c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 273c62520a8SAmir Goldstein { 274c62520a8SAmir Goldstein clear_bit(flag, &OVL_E(dentry)->flags); 275c62520a8SAmir Goldstein } 276c62520a8SAmir Goldstein 277c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 278c62520a8SAmir Goldstein { 279c62520a8SAmir Goldstein return test_bit(flag, &OVL_E(dentry)->flags); 280c62520a8SAmir Goldstein } 281c62520a8SAmir Goldstein 282bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry) 283bbb1e54dSMiklos Szeredi { 284c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 285bbb1e54dSMiklos Szeredi } 286bbb1e54dSMiklos Szeredi 287bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry) 288bbb1e54dSMiklos Szeredi { 289bbb1e54dSMiklos Szeredi return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 290bbb1e54dSMiklos Szeredi } 291bbb1e54dSMiklos Szeredi 2925cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry) 293bbb1e54dSMiklos Szeredi { 294c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 295bbb1e54dSMiklos Szeredi } 296bbb1e54dSMiklos Szeredi 29755acc661SMiklos Szeredi /* 298aa3ff3c1SAmir Goldstein * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 299aa3ff3c1SAmir Goldstein * to return positive, while there's no actual upper alias for the inode. 300aa3ff3c1SAmir Goldstein * Copy up code needs to know about the existence of the upper alias, so it 301aa3ff3c1SAmir Goldstein * can't use ovl_dentry_upper(). 30255acc661SMiklos Szeredi */ 30355acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry) 30455acc661SMiklos Szeredi { 305c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 30655acc661SMiklos Szeredi } 30755acc661SMiklos Szeredi 30855acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry) 30955acc661SMiklos Szeredi { 310c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 31155acc661SMiklos Szeredi } 31255acc661SMiklos Szeredi 3130c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode) 3140c288874SVivek Goyal { 3150c288874SVivek Goyal if (!S_ISREG(inode->i_mode)) 3160c288874SVivek Goyal return false; 3170c288874SVivek Goyal 3180c288874SVivek Goyal if (!ovl_inode_lower(inode)) 3190c288874SVivek Goyal return false; 3200c288874SVivek Goyal 3210c288874SVivek Goyal return true; 3220c288874SVivek Goyal } 3230c288874SVivek Goyal 3240c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode) 3250c288874SVivek Goyal { 3260c288874SVivek Goyal if (!ovl_should_check_upperdata(inode)) 3270c288874SVivek Goyal return true; 3280c288874SVivek Goyal 3290c288874SVivek Goyal if (!ovl_test_flag(OVL_UPPERDATA, inode)) 3300c288874SVivek Goyal return false; 3310c288874SVivek Goyal /* 3320c288874SVivek Goyal * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 3330c288874SVivek Goyal * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 3340c288874SVivek Goyal * if setting of OVL_UPPERDATA is visible, then effects of writes 3350c288874SVivek Goyal * before that are visible too. 3360c288874SVivek Goyal */ 3370c288874SVivek Goyal smp_rmb(); 3380c288874SVivek Goyal return true; 3390c288874SVivek Goyal } 3400c288874SVivek Goyal 3410c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode) 3420c288874SVivek Goyal { 3430c288874SVivek Goyal /* 3440c288874SVivek Goyal * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 3450c288874SVivek Goyal * if OVL_UPPERDATA flag is visible, then effects of write operations 3460c288874SVivek Goyal * before it are visible as well. 3470c288874SVivek Goyal */ 3480c288874SVivek Goyal smp_wmb(); 3490c288874SVivek Goyal ovl_set_flag(OVL_UPPERDATA, inode); 3500c288874SVivek Goyal } 3510c288874SVivek Goyal 3520c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 3530c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 3540c288874SVivek Goyal { 3550c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 3560c288874SVivek Goyal return false; 3570c288874SVivek Goyal 3580c288874SVivek Goyal return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 3590c288874SVivek Goyal } 3600c288874SVivek Goyal 3610c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 3620c288874SVivek Goyal { 3630c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 3640c288874SVivek Goyal return false; 3650c288874SVivek Goyal 3660c288874SVivek Goyal return !ovl_has_upperdata(d_inode(dentry)); 3670c288874SVivek Goyal } 3680c288874SVivek Goyal 369a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb) 370a6c60655SMiklos Szeredi { 371a6c60655SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 372a6c60655SMiklos Szeredi 37321a22878SAmir Goldstein return ofs->config.redirect_dir && !ofs->noxattr; 374a6c60655SMiklos Szeredi } 375a6c60655SMiklos Szeredi 376a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry) 377a6c60655SMiklos Szeredi { 378cf31c463SMiklos Szeredi return OVL_I(d_inode(dentry))->redirect; 379a6c60655SMiklos Szeredi } 380a6c60655SMiklos Szeredi 381a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 382a6c60655SMiklos Szeredi { 383cf31c463SMiklos Szeredi struct ovl_inode *oi = OVL_I(d_inode(dentry)); 384a6c60655SMiklos Szeredi 385cf31c463SMiklos Szeredi kfree(oi->redirect); 386cf31c463SMiklos Szeredi oi->redirect = redirect; 387a6c60655SMiklos Szeredi } 388a6c60655SMiklos Szeredi 38909d8b586SMiklos Szeredi void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, 3902664bd08SVivek Goyal struct dentry *lowerdentry, struct dentry *lowerdata) 391bbb1e54dSMiklos Szeredi { 392695b46e7SAmir Goldstein struct inode *realinode = d_inode(upperdentry ?: lowerdentry); 393695b46e7SAmir Goldstein 39409d8b586SMiklos Szeredi if (upperdentry) 39509d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 39609d8b586SMiklos Szeredi if (lowerdentry) 39731747edaSAmir Goldstein OVL_I(inode)->lower = igrab(d_inode(lowerdentry)); 3982664bd08SVivek Goyal if (lowerdata) 3992664bd08SVivek Goyal OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata)); 400bbb1e54dSMiklos Szeredi 401695b46e7SAmir Goldstein ovl_copyattr(realinode, inode); 4024f357295SMiklos Szeredi ovl_copyflags(realinode, inode); 403695b46e7SAmir Goldstein if (!inode->i_ino) 404695b46e7SAmir Goldstein inode->i_ino = realinode->i_ino; 405bbb1e54dSMiklos Szeredi } 406bbb1e54dSMiklos Szeredi 40709d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 408bbb1e54dSMiklos Szeredi { 40909d8b586SMiklos Szeredi struct inode *upperinode = d_inode(upperdentry); 410e6d2ebddSMiklos Szeredi 41109d8b586SMiklos Szeredi WARN_ON(OVL_I(inode)->__upperdentry); 41209d8b586SMiklos Szeredi 41325b7713aSMiklos Szeredi /* 41409d8b586SMiklos Szeredi * Make sure upperdentry is consistent before making it visible 41525b7713aSMiklos Szeredi */ 41625b7713aSMiklos Szeredi smp_wmb(); 41709d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 41831747edaSAmir Goldstein if (inode_unhashed(inode)) { 419695b46e7SAmir Goldstein if (!inode->i_ino) 420695b46e7SAmir Goldstein inode->i_ino = upperinode->i_ino; 42125b7713aSMiklos Szeredi inode->i_private = upperinode; 422bbb1e54dSMiklos Szeredi __insert_inode_hash(inode, (unsigned long) upperinode); 423bbb1e54dSMiklos Szeredi } 42425b7713aSMiklos Szeredi } 425bbb1e54dSMiklos Szeredi 426d9854c87SMiklos Szeredi static void ovl_dentry_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)); 4314edb83bbSMiklos Szeredi /* 4324edb83bbSMiklos Szeredi * Version is used by readdir code to keep cache consistent. For merge 4334edb83bbSMiklos Szeredi * dirs all changes need to be noted. For non-merge dirs, cache only 4344edb83bbSMiklos Szeredi * contains impure (ones which have been copied up and have origins) 4354edb83bbSMiklos Szeredi * entries, so only need to note changes to impure entries. 4364edb83bbSMiklos Szeredi */ 4374edb83bbSMiklos Szeredi if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity) 43804a01ac7SMiklos Szeredi OVL_I(inode)->version++; 439bbb1e54dSMiklos Szeredi } 440bbb1e54dSMiklos Szeredi 441d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity) 442d9854c87SMiklos Szeredi { 443d9854c87SMiklos Szeredi /* Copy mtime/ctime */ 444d9854c87SMiklos Szeredi ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry)); 445d9854c87SMiklos Szeredi 446d9854c87SMiklos Szeredi ovl_dentry_version_inc(dentry, impurity); 447d9854c87SMiklos Szeredi } 448d9854c87SMiklos Szeredi 449bbb1e54dSMiklos Szeredi u64 ovl_dentry_version_get(struct dentry *dentry) 450bbb1e54dSMiklos Szeredi { 45104a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 452bbb1e54dSMiklos Szeredi 45304a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 45404a01ac7SMiklos Szeredi return OVL_I(inode)->version; 455bbb1e54dSMiklos Szeredi } 456bbb1e54dSMiklos Szeredi 457bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry) 458bbb1e54dSMiklos Szeredi { 459bbb1e54dSMiklos Szeredi struct inode *inode = dentry->d_inode; 460bbb1e54dSMiklos Szeredi 461bbb1e54dSMiklos Szeredi return inode && IS_WHITEOUT(inode); 462bbb1e54dSMiklos Szeredi } 463bbb1e54dSMiklos Szeredi 464bbb1e54dSMiklos Szeredi struct file *ovl_path_open(struct path *path, int flags) 465bbb1e54dSMiklos Szeredi { 466bbb1e54dSMiklos Szeredi return dentry_open(path, flags | O_NOATIME, current_cred()); 467bbb1e54dSMiklos Szeredi } 46839d3d60aSAmir Goldstein 4690c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 4700c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 4710c288874SVivek Goyal { 4720c288874SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 4730c288874SVivek Goyal 4740c288874SVivek Goyal if (ovl_dentry_upper(dentry) && 4750c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 4760c288874SVivek Goyal !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 4770c288874SVivek Goyal return true; 4780c288874SVivek Goyal 4790c288874SVivek Goyal return false; 4800c288874SVivek Goyal } 4810c288874SVivek Goyal 4820c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags) 4832002df85SVivek Goyal { 4842002df85SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 4852002df85SVivek Goyal 4862002df85SVivek Goyal /* 4872002df85SVivek Goyal * Check if copy-up has happened as well as for upper alias (in 4882002df85SVivek Goyal * case of hard links) is there. 4892002df85SVivek Goyal * 4902002df85SVivek Goyal * Both checks are lockless: 4912002df85SVivek Goyal * - false negatives: will recheck under oi->lock 4922002df85SVivek Goyal * - false positives: 4932002df85SVivek Goyal * + ovl_dentry_upper() uses memory barriers to ensure the 4942002df85SVivek Goyal * upper dentry is up-to-date 4952002df85SVivek Goyal * + ovl_dentry_has_upper_alias() relies on locking of 4962002df85SVivek Goyal * upper parent i_rwsem to prevent reordering copy-up 4972002df85SVivek Goyal * with rename. 4982002df85SVivek Goyal */ 4992002df85SVivek Goyal if (ovl_dentry_upper(dentry) && 5000c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 5010c288874SVivek Goyal !ovl_dentry_needs_data_copy_up(dentry, flags)) 5022002df85SVivek Goyal return true; 5032002df85SVivek Goyal 5042002df85SVivek Goyal return false; 5052002df85SVivek Goyal } 5062002df85SVivek Goyal 5070c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags) 50839d3d60aSAmir Goldstein { 5091e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 51039d3d60aSAmir Goldstein int err; 51139d3d60aSAmir Goldstein 5121e92e307SAmir Goldstein err = ovl_inode_lock(inode); 5130c288874SVivek Goyal if (!err && ovl_already_copied_up_locked(dentry, flags)) { 51439d3d60aSAmir Goldstein err = 1; /* Already copied up */ 5151e92e307SAmir Goldstein ovl_inode_unlock(inode); 51639d3d60aSAmir Goldstein } 51739d3d60aSAmir Goldstein 51839d3d60aSAmir Goldstein return err; 51939d3d60aSAmir Goldstein } 52039d3d60aSAmir Goldstein 52139d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry) 52239d3d60aSAmir Goldstein { 5231e92e307SAmir Goldstein ovl_inode_unlock(d_inode(dentry)); 52439d3d60aSAmir Goldstein } 52582b749b2SAmir Goldstein 526b79e05aaSAmir Goldstein bool ovl_check_origin_xattr(struct dentry *dentry) 527b79e05aaSAmir Goldstein { 528b79e05aaSAmir Goldstein int res; 529b79e05aaSAmir Goldstein 530b79e05aaSAmir Goldstein res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); 531b79e05aaSAmir Goldstein 532b79e05aaSAmir Goldstein /* Zero size value means "copied up but origin unknown" */ 533b79e05aaSAmir Goldstein if (res >= 0) 534b79e05aaSAmir Goldstein return true; 535b79e05aaSAmir Goldstein 536b79e05aaSAmir Goldstein return false; 537b79e05aaSAmir Goldstein } 538b79e05aaSAmir Goldstein 539f3a15685SAmir Goldstein bool ovl_check_dir_xattr(struct dentry *dentry, const char *name) 540f3a15685SAmir Goldstein { 541f3a15685SAmir Goldstein int res; 542f3a15685SAmir Goldstein char val; 543f3a15685SAmir Goldstein 544f3a15685SAmir Goldstein if (!d_is_dir(dentry)) 545f3a15685SAmir Goldstein return false; 546f3a15685SAmir Goldstein 547f3a15685SAmir Goldstein res = vfs_getxattr(dentry, name, &val, 1); 548f3a15685SAmir Goldstein if (res == 1 && val == 'y') 549f3a15685SAmir Goldstein return true; 550f3a15685SAmir Goldstein 551f3a15685SAmir Goldstein return false; 552f3a15685SAmir Goldstein } 553f3a15685SAmir Goldstein 55482b749b2SAmir Goldstein int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, 55582b749b2SAmir Goldstein const char *name, const void *value, size_t size, 55682b749b2SAmir Goldstein int xerr) 55782b749b2SAmir Goldstein { 55882b749b2SAmir Goldstein int err; 55982b749b2SAmir Goldstein struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 56082b749b2SAmir Goldstein 56182b749b2SAmir Goldstein if (ofs->noxattr) 56282b749b2SAmir Goldstein return xerr; 56382b749b2SAmir Goldstein 56482b749b2SAmir Goldstein err = ovl_do_setxattr(upperdentry, name, value, size, 0); 56582b749b2SAmir Goldstein 56682b749b2SAmir Goldstein if (err == -EOPNOTSUPP) { 5671bd0a3aeSlijiazi pr_warn("cannot set %s xattr on upper\n", name); 56882b749b2SAmir Goldstein ofs->noxattr = true; 56982b749b2SAmir Goldstein return xerr; 57082b749b2SAmir Goldstein } 57182b749b2SAmir Goldstein 57282b749b2SAmir Goldstein return err; 57382b749b2SAmir Goldstein } 574f3a15685SAmir Goldstein 575f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 576f3a15685SAmir Goldstein { 577f3a15685SAmir Goldstein int err; 578f3a15685SAmir Goldstein 57913c72075SMiklos Szeredi if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 580f3a15685SAmir Goldstein return 0; 581f3a15685SAmir Goldstein 582f3a15685SAmir Goldstein /* 583f3a15685SAmir Goldstein * Do not fail when upper doesn't support xattrs. 584f3a15685SAmir Goldstein * Upper inodes won't have origin nor redirect xattr anyway. 585f3a15685SAmir Goldstein */ 586f3a15685SAmir Goldstein err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, 587f3a15685SAmir Goldstein "y", 1, 0); 588f3a15685SAmir Goldstein if (!err) 58913c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 590f3a15685SAmir Goldstein 591f3a15685SAmir Goldstein return err; 592f3a15685SAmir Goldstein } 59313c72075SMiklos Szeredi 59413c72075SMiklos Szeredi void ovl_set_flag(unsigned long flag, struct inode *inode) 59513c72075SMiklos Szeredi { 59613c72075SMiklos Szeredi set_bit(flag, &OVL_I(inode)->flags); 59713c72075SMiklos Szeredi } 59813c72075SMiklos Szeredi 5994edb83bbSMiklos Szeredi void ovl_clear_flag(unsigned long flag, struct inode *inode) 6004edb83bbSMiklos Szeredi { 6014edb83bbSMiklos Szeredi clear_bit(flag, &OVL_I(inode)->flags); 6024edb83bbSMiklos Szeredi } 6034edb83bbSMiklos Szeredi 60413c72075SMiklos Szeredi bool ovl_test_flag(unsigned long flag, struct inode *inode) 60513c72075SMiklos Szeredi { 60613c72075SMiklos Szeredi return test_bit(flag, &OVL_I(inode)->flags); 60713c72075SMiklos Szeredi } 608ad0af710SAmir Goldstein 609ad0af710SAmir Goldstein /** 610ad0af710SAmir Goldstein * Caller must hold a reference to inode to prevent it from being freed while 611ad0af710SAmir Goldstein * it is marked inuse. 612ad0af710SAmir Goldstein */ 613ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry) 614ad0af710SAmir Goldstein { 615ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 616ad0af710SAmir Goldstein bool locked = false; 617ad0af710SAmir Goldstein 618ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 619ad0af710SAmir Goldstein if (!(inode->i_state & I_OVL_INUSE)) { 620ad0af710SAmir Goldstein inode->i_state |= I_OVL_INUSE; 621ad0af710SAmir Goldstein locked = true; 622ad0af710SAmir Goldstein } 623ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 624ad0af710SAmir Goldstein 625ad0af710SAmir Goldstein return locked; 626ad0af710SAmir Goldstein } 627ad0af710SAmir Goldstein 628ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry) 629ad0af710SAmir Goldstein { 630ad0af710SAmir Goldstein if (dentry) { 631ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 632ad0af710SAmir Goldstein 633ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 634ad0af710SAmir Goldstein WARN_ON(!(inode->i_state & I_OVL_INUSE)); 635ad0af710SAmir Goldstein inode->i_state &= ~I_OVL_INUSE; 636ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 637ad0af710SAmir Goldstein } 638ad0af710SAmir Goldstein } 6395f8415d6SAmir Goldstein 640146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry) 641146d62e5SAmir Goldstein { 642146d62e5SAmir Goldstein struct inode *inode = d_inode(dentry); 643146d62e5SAmir Goldstein bool inuse; 644146d62e5SAmir Goldstein 645146d62e5SAmir Goldstein spin_lock(&inode->i_lock); 646146d62e5SAmir Goldstein inuse = (inode->i_state & I_OVL_INUSE); 647146d62e5SAmir Goldstein spin_unlock(&inode->i_lock); 648146d62e5SAmir Goldstein 649146d62e5SAmir Goldstein return inuse; 650146d62e5SAmir Goldstein } 651146d62e5SAmir Goldstein 65224b33ee1SAmir Goldstein /* 65324b33ee1SAmir Goldstein * Does this overlay dentry need to be indexed on copy up? 65424b33ee1SAmir Goldstein */ 65524b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry) 65624b33ee1SAmir Goldstein { 65724b33ee1SAmir Goldstein struct dentry *lower = ovl_dentry_lower(dentry); 65824b33ee1SAmir Goldstein 65924b33ee1SAmir Goldstein if (!lower || !ovl_indexdir(dentry->d_sb)) 66024b33ee1SAmir Goldstein return false; 66124b33ee1SAmir Goldstein 662fbd2d207SAmir Goldstein /* Index all files for NFS export and consistency verification */ 663016b720fSAmir Goldstein if (ovl_index_all(dentry->d_sb)) 664fbd2d207SAmir Goldstein return true; 665fbd2d207SAmir Goldstein 66624b33ee1SAmir Goldstein /* Index only lower hardlinks on copy up */ 66724b33ee1SAmir Goldstein if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 66824b33ee1SAmir Goldstein return true; 66924b33ee1SAmir Goldstein 67024b33ee1SAmir Goldstein return false; 67124b33ee1SAmir Goldstein } 67224b33ee1SAmir Goldstein 6739f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */ 674caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry) 675caf70cb2SAmir Goldstein { 676e7dd0e71SAmir Goldstein struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 677e7dd0e71SAmir Goldstein struct inode *dir = indexdir->d_inode; 678caf70cb2SAmir Goldstein struct dentry *lowerdentry = ovl_dentry_lower(dentry); 679caf70cb2SAmir Goldstein struct dentry *upperdentry = ovl_dentry_upper(dentry); 680caf70cb2SAmir Goldstein struct dentry *index = NULL; 681caf70cb2SAmir Goldstein struct inode *inode; 68263e13252SAmir Goldstein struct qstr name = { }; 683caf70cb2SAmir Goldstein int err; 684caf70cb2SAmir Goldstein 685caf70cb2SAmir Goldstein err = ovl_get_index_name(lowerdentry, &name); 686caf70cb2SAmir Goldstein if (err) 687caf70cb2SAmir Goldstein goto fail; 688caf70cb2SAmir Goldstein 689caf70cb2SAmir Goldstein inode = d_inode(upperdentry); 69089a17556SAmir Goldstein if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 6911bd0a3aeSlijiazi pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 692caf70cb2SAmir Goldstein upperdentry, inode->i_ino, inode->i_nlink); 693caf70cb2SAmir Goldstein /* 694caf70cb2SAmir Goldstein * We either have a bug with persistent union nlink or a lower 695caf70cb2SAmir Goldstein * hardlink was added while overlay is mounted. Adding a lower 696caf70cb2SAmir Goldstein * hardlink and then unlinking all overlay hardlinks would drop 697caf70cb2SAmir Goldstein * overlay nlink to zero before all upper inodes are unlinked. 698caf70cb2SAmir Goldstein * As a safety measure, when that situation is detected, set 699caf70cb2SAmir Goldstein * the overlay nlink to the index inode nlink minus one for the 700caf70cb2SAmir Goldstein * index entry itself. 701caf70cb2SAmir Goldstein */ 702caf70cb2SAmir Goldstein set_nlink(d_inode(dentry), inode->i_nlink - 1); 703caf70cb2SAmir Goldstein ovl_set_nlink_upper(dentry); 704caf70cb2SAmir Goldstein goto out; 705caf70cb2SAmir Goldstein } 706caf70cb2SAmir Goldstein 707caf70cb2SAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 708e7dd0e71SAmir Goldstein index = lookup_one_len(name.name, indexdir, name.len); 709caf70cb2SAmir Goldstein err = PTR_ERR(index); 710e7dd0e71SAmir Goldstein if (IS_ERR(index)) { 7119f4ec904SAmir Goldstein index = NULL; 712e7dd0e71SAmir Goldstein } else if (ovl_index_all(dentry->d_sb)) { 713e7dd0e71SAmir Goldstein /* Whiteout orphan index to block future open by handle */ 714e7dd0e71SAmir Goldstein err = ovl_cleanup_and_whiteout(indexdir, dir, index); 715e7dd0e71SAmir Goldstein } else { 716e7dd0e71SAmir Goldstein /* Cleanup orphan index entries */ 717e7dd0e71SAmir Goldstein err = ovl_cleanup(dir, index); 718e7dd0e71SAmir Goldstein } 7199f4ec904SAmir Goldstein 720caf70cb2SAmir Goldstein inode_unlock(dir); 721caf70cb2SAmir Goldstein if (err) 722caf70cb2SAmir Goldstein goto fail; 723caf70cb2SAmir Goldstein 724caf70cb2SAmir Goldstein out: 72563e13252SAmir Goldstein kfree(name.name); 726caf70cb2SAmir Goldstein dput(index); 727caf70cb2SAmir Goldstein return; 728caf70cb2SAmir Goldstein 729caf70cb2SAmir Goldstein fail: 7301bd0a3aeSlijiazi pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); 731caf70cb2SAmir Goldstein goto out; 732caf70cb2SAmir Goldstein } 733caf70cb2SAmir Goldstein 7345f8415d6SAmir Goldstein /* 7355f8415d6SAmir Goldstein * Operations that change overlay inode and upper inode nlink need to be 7365f8415d6SAmir Goldstein * synchronized with copy up for persistent nlink accounting. 7375f8415d6SAmir Goldstein */ 7380e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry) 7395f8415d6SAmir Goldstein { 7401e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 7415f8415d6SAmir Goldstein const struct cred *old_cred; 7425f8415d6SAmir Goldstein int err; 7435f8415d6SAmir Goldstein 7441e92e307SAmir Goldstein if (WARN_ON(!inode)) 7450e32992fSAmir Goldstein return -ENOENT; 7465f8415d6SAmir Goldstein 7475f8415d6SAmir Goldstein /* 7485f8415d6SAmir Goldstein * With inodes index is enabled, we store the union overlay nlink 74924b33ee1SAmir Goldstein * in an xattr on the index inode. When whiting out an indexed lower, 7505f8415d6SAmir Goldstein * we need to decrement the overlay persistent nlink, but before the 7515f8415d6SAmir Goldstein * first copy up, we have no upper index inode to store the xattr. 7525f8415d6SAmir Goldstein * 75324b33ee1SAmir Goldstein * As a workaround, before whiteout/rename over an indexed lower, 7545f8415d6SAmir Goldstein * copy up to create the upper index. Creating the upper index will 7555f8415d6SAmir Goldstein * initialize the overlay nlink, so it could be dropped if unlink 7565f8415d6SAmir Goldstein * or rename succeeds. 7575f8415d6SAmir Goldstein * 7585f8415d6SAmir Goldstein * TODO: implement metadata only index copy up when called with 7595f8415d6SAmir Goldstein * ovl_copy_up_flags(dentry, O_PATH). 7605f8415d6SAmir Goldstein */ 76124b33ee1SAmir Goldstein if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 7625f8415d6SAmir Goldstein err = ovl_copy_up(dentry); 7635f8415d6SAmir Goldstein if (err) 7645f8415d6SAmir Goldstein return err; 7655f8415d6SAmir Goldstein } 7665f8415d6SAmir Goldstein 7671e92e307SAmir Goldstein err = ovl_inode_lock(inode); 7685f8415d6SAmir Goldstein if (err) 7695f8415d6SAmir Goldstein return err; 7705f8415d6SAmir Goldstein 7711e92e307SAmir Goldstein if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 7725f8415d6SAmir Goldstein goto out; 7735f8415d6SAmir Goldstein 7745f8415d6SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 7755f8415d6SAmir Goldstein /* 7765f8415d6SAmir Goldstein * The overlay inode nlink should be incremented/decremented IFF the 7775f8415d6SAmir Goldstein * upper operation succeeds, along with nlink change of upper inode. 7785f8415d6SAmir Goldstein * Therefore, before link/unlink/rename, we store the union nlink 7795f8415d6SAmir Goldstein * value relative to the upper inode nlink in an upper inode xattr. 7805f8415d6SAmir Goldstein */ 7815f8415d6SAmir Goldstein err = ovl_set_nlink_upper(dentry); 7825f8415d6SAmir Goldstein revert_creds(old_cred); 7835f8415d6SAmir Goldstein 7845f8415d6SAmir Goldstein out: 7855f8415d6SAmir Goldstein if (err) 7861e92e307SAmir Goldstein ovl_inode_unlock(inode); 7875f8415d6SAmir Goldstein 7885f8415d6SAmir Goldstein return err; 7895f8415d6SAmir Goldstein } 7905f8415d6SAmir Goldstein 7910e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry) 7925f8415d6SAmir Goldstein { 7931e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 7941e92e307SAmir Goldstein 7951e92e307SAmir Goldstein if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 796caf70cb2SAmir Goldstein const struct cred *old_cred; 797caf70cb2SAmir Goldstein 798caf70cb2SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 799caf70cb2SAmir Goldstein ovl_cleanup_index(dentry); 800caf70cb2SAmir Goldstein revert_creds(old_cred); 801caf70cb2SAmir Goldstein } 802caf70cb2SAmir Goldstein 8031e92e307SAmir Goldstein ovl_inode_unlock(inode); 8045f8415d6SAmir Goldstein } 8055820dc08SAmir Goldstein 8065820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) 8075820dc08SAmir Goldstein { 8085820dc08SAmir Goldstein /* Workdir should not be the same as upperdir */ 8095820dc08SAmir Goldstein if (workdir == upperdir) 8105820dc08SAmir Goldstein goto err; 8115820dc08SAmir Goldstein 8125820dc08SAmir Goldstein /* Workdir should not be subdir of upperdir and vice versa */ 8135820dc08SAmir Goldstein if (lock_rename(workdir, upperdir) != NULL) 8145820dc08SAmir Goldstein goto err_unlock; 8155820dc08SAmir Goldstein 8165820dc08SAmir Goldstein return 0; 8175820dc08SAmir Goldstein 8185820dc08SAmir Goldstein err_unlock: 8195820dc08SAmir Goldstein unlock_rename(workdir, upperdir); 8205820dc08SAmir Goldstein err: 8211bd0a3aeSlijiazi pr_err("failed to lock workdir+upperdir\n"); 8225820dc08SAmir Goldstein return -EIO; 8235820dc08SAmir Goldstein } 8249d3dfea3SVivek Goyal 8259d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ 8269d3dfea3SVivek Goyal int ovl_check_metacopy_xattr(struct dentry *dentry) 8279d3dfea3SVivek Goyal { 8289d3dfea3SVivek Goyal int res; 8299d3dfea3SVivek Goyal 8309d3dfea3SVivek Goyal /* Only regular files can have metacopy xattr */ 8319d3dfea3SVivek Goyal if (!S_ISREG(d_inode(dentry)->i_mode)) 8329d3dfea3SVivek Goyal return 0; 8339d3dfea3SVivek Goyal 8349d3dfea3SVivek Goyal res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0); 8359d3dfea3SVivek Goyal if (res < 0) { 8369d3dfea3SVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 8379d3dfea3SVivek Goyal return 0; 8389d3dfea3SVivek Goyal goto out; 8399d3dfea3SVivek Goyal } 8409d3dfea3SVivek Goyal 8419d3dfea3SVivek Goyal return 1; 8429d3dfea3SVivek Goyal out: 8431bd0a3aeSlijiazi pr_warn_ratelimited("failed to get metacopy (%i)\n", res); 8449d3dfea3SVivek Goyal return res; 8459d3dfea3SVivek Goyal } 84667d756c2SVivek Goyal 84767d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry) 84867d756c2SVivek Goyal { 84967d756c2SVivek Goyal struct ovl_entry *oe = dentry->d_fsdata; 85067d756c2SVivek Goyal 85167d756c2SVivek Goyal if (!d_is_reg(dentry)) 85267d756c2SVivek Goyal return false; 85367d756c2SVivek Goyal 85467d756c2SVivek Goyal if (ovl_dentry_upper(dentry)) { 85567d756c2SVivek Goyal if (!ovl_has_upperdata(d_inode(dentry))) 85667d756c2SVivek Goyal return true; 85767d756c2SVivek Goyal return false; 85867d756c2SVivek Goyal } 85967d756c2SVivek Goyal 86067d756c2SVivek Goyal return (oe->numlower > 1); 86167d756c2SVivek Goyal } 8620a2d0d3fSVivek Goyal 863993a0b2aSVivek Goyal ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value, 864993a0b2aSVivek Goyal size_t padding) 865993a0b2aSVivek Goyal { 866993a0b2aSVivek Goyal ssize_t res; 867993a0b2aSVivek Goyal char *buf = NULL; 868993a0b2aSVivek Goyal 869993a0b2aSVivek Goyal res = vfs_getxattr(dentry, name, NULL, 0); 870993a0b2aSVivek Goyal if (res < 0) { 871993a0b2aSVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 872993a0b2aSVivek Goyal return -ENODATA; 873993a0b2aSVivek Goyal goto fail; 874993a0b2aSVivek Goyal } 875993a0b2aSVivek Goyal 876993a0b2aSVivek Goyal if (res != 0) { 877993a0b2aSVivek Goyal buf = kzalloc(res + padding, GFP_KERNEL); 878993a0b2aSVivek Goyal if (!buf) 879993a0b2aSVivek Goyal return -ENOMEM; 880993a0b2aSVivek Goyal 881993a0b2aSVivek Goyal res = vfs_getxattr(dentry, name, buf, res); 882993a0b2aSVivek Goyal if (res < 0) 883993a0b2aSVivek Goyal goto fail; 884993a0b2aSVivek Goyal } 885993a0b2aSVivek Goyal *value = buf; 886993a0b2aSVivek Goyal 887993a0b2aSVivek Goyal return res; 888993a0b2aSVivek Goyal 889993a0b2aSVivek Goyal fail: 8901bd0a3aeSlijiazi pr_warn_ratelimited("failed to get xattr %s: err=%zi)\n", 891993a0b2aSVivek Goyal name, res); 892993a0b2aSVivek Goyal kfree(buf); 893993a0b2aSVivek Goyal return res; 894993a0b2aSVivek Goyal } 895993a0b2aSVivek Goyal 8960a2d0d3fSVivek Goyal char *ovl_get_redirect_xattr(struct dentry *dentry, int padding) 8970a2d0d3fSVivek Goyal { 8980a2d0d3fSVivek Goyal int res; 8990a2d0d3fSVivek Goyal char *s, *next, *buf = NULL; 9000a2d0d3fSVivek Goyal 901993a0b2aSVivek Goyal res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1); 902993a0b2aSVivek Goyal if (res == -ENODATA) 9030a2d0d3fSVivek Goyal return NULL; 9040a2d0d3fSVivek Goyal if (res < 0) 905993a0b2aSVivek Goyal return ERR_PTR(res); 9060a2d0d3fSVivek Goyal if (res == 0) 9070a2d0d3fSVivek Goyal goto invalid; 9080a2d0d3fSVivek Goyal 9090a2d0d3fSVivek Goyal if (buf[0] == '/') { 9100a2d0d3fSVivek Goyal for (s = buf; *s++ == '/'; s = next) { 9110a2d0d3fSVivek Goyal next = strchrnul(s, '/'); 9120a2d0d3fSVivek Goyal if (s == next) 9130a2d0d3fSVivek Goyal goto invalid; 9140a2d0d3fSVivek Goyal } 9150a2d0d3fSVivek Goyal } else { 9160a2d0d3fSVivek Goyal if (strchr(buf, '/') != NULL) 9170a2d0d3fSVivek Goyal goto invalid; 9180a2d0d3fSVivek Goyal } 9190a2d0d3fSVivek Goyal 9200a2d0d3fSVivek Goyal return buf; 9210a2d0d3fSVivek Goyal invalid: 9221bd0a3aeSlijiazi pr_warn_ratelimited("invalid redirect (%s)\n", buf); 9230a2d0d3fSVivek Goyal res = -EINVAL; 924993a0b2aSVivek Goyal kfree(buf); 925993a0b2aSVivek Goyal return ERR_PTR(res); 9260a2d0d3fSVivek Goyal } 927