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 86163db0daSAmir Goldstein struct ovl_path *ovl_stack_alloc(unsigned int n) 87163db0daSAmir Goldstein { 88163db0daSAmir Goldstein return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL); 89163db0daSAmir Goldstein } 90163db0daSAmir Goldstein 91163db0daSAmir Goldstein void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n) 92163db0daSAmir Goldstein { 93163db0daSAmir Goldstein unsigned int i; 94163db0daSAmir Goldstein 95163db0daSAmir Goldstein memcpy(dst, src, sizeof(struct ovl_path) * n); 96163db0daSAmir Goldstein for (i = 0; i < n; i++) 97163db0daSAmir Goldstein dget(src[i].dentry); 98163db0daSAmir Goldstein } 99163db0daSAmir Goldstein 100163db0daSAmir Goldstein void ovl_stack_put(struct ovl_path *stack, unsigned int n) 101163db0daSAmir Goldstein { 102163db0daSAmir Goldstein unsigned int i; 103163db0daSAmir Goldstein 104163db0daSAmir Goldstein for (i = 0; stack && i < n; i++) 105163db0daSAmir Goldstein dput(stack[i].dentry); 106163db0daSAmir Goldstein } 107163db0daSAmir Goldstein 108163db0daSAmir Goldstein void ovl_stack_free(struct ovl_path *stack, unsigned int n) 109163db0daSAmir Goldstein { 110163db0daSAmir Goldstein ovl_stack_put(stack, n); 111163db0daSAmir Goldstein kfree(stack); 112163db0daSAmir Goldstein } 113163db0daSAmir Goldstein 114bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 115bbb1e54dSMiklos Szeredi { 1165522c9c7SAmir Goldstein size_t size = offsetof(struct ovl_entry, __lowerstack[numlower]); 117bbb1e54dSMiklos Szeredi struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 118bbb1e54dSMiklos Szeredi 119bbb1e54dSMiklos Szeredi if (oe) 1205522c9c7SAmir Goldstein oe->__numlower = numlower; 121bbb1e54dSMiklos Szeredi 122bbb1e54dSMiklos Szeredi return oe; 123bbb1e54dSMiklos Szeredi } 124bbb1e54dSMiklos Szeredi 125163db0daSAmir Goldstein void ovl_free_entry(struct ovl_entry *oe) 126163db0daSAmir Goldstein { 127163db0daSAmir Goldstein ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe)); 128163db0daSAmir Goldstein kfree(oe); 129163db0daSAmir Goldstein } 130163db0daSAmir Goldstein 131b07d5cc9SAmir Goldstein #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE) 132b07d5cc9SAmir Goldstein 133bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry) 134bbb1e54dSMiklos Szeredi { 135b07d5cc9SAmir Goldstein return dentry->d_flags & OVL_D_REVALIDATE; 136bbb1e54dSMiklos Szeredi } 137bbb1e54dSMiklos Szeredi 138b07d5cc9SAmir Goldstein void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry) 139b07d5cc9SAmir Goldstein { 140b07d5cc9SAmir Goldstein if (!ovl_dentry_remote(realdentry)) 141b07d5cc9SAmir Goldstein return; 142b07d5cc9SAmir Goldstein 143b07d5cc9SAmir Goldstein spin_lock(&dentry->d_lock); 144b07d5cc9SAmir Goldstein dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE; 145b07d5cc9SAmir Goldstein spin_unlock(&dentry->d_lock); 146b07d5cc9SAmir Goldstein } 147b07d5cc9SAmir Goldstein 1480af950f5SAmir Goldstein void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry, 1490af950f5SAmir Goldstein struct ovl_entry *oe) 150b07d5cc9SAmir Goldstein { 1510af950f5SAmir Goldstein return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE); 152b07d5cc9SAmir Goldstein } 153b07d5cc9SAmir Goldstein 154b07d5cc9SAmir Goldstein void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry, 1550af950f5SAmir Goldstein struct ovl_entry *oe, unsigned int mask) 156f4288844SMiklos Szeredi { 1575522c9c7SAmir Goldstein struct ovl_path *lowerstack = ovl_lowerstack(oe); 158f4288844SMiklos Szeredi unsigned int i, flags = 0; 159f4288844SMiklos Szeredi 160bccece1eSMiklos Szeredi if (upperdentry) 161bccece1eSMiklos Szeredi flags |= upperdentry->d_flags; 1625522c9c7SAmir Goldstein for (i = 0; i < ovl_numlower(oe); i++) 1635522c9c7SAmir Goldstein flags |= lowerstack[i].dentry->d_flags; 164f4288844SMiklos Szeredi 165f4288844SMiklos Szeredi spin_lock(&dentry->d_lock); 166f4288844SMiklos Szeredi dentry->d_flags &= ~mask; 167f4288844SMiklos Szeredi dentry->d_flags |= flags & mask; 168f4288844SMiklos Szeredi spin_unlock(&dentry->d_lock); 169f4288844SMiklos Szeredi } 170f4288844SMiklos Szeredi 171bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry) 172bbb1e54dSMiklos Szeredi { 173bbb1e54dSMiklos Szeredi return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 174bbb1e54dSMiklos Szeredi DCACHE_MANAGE_TRANSIT | 175bbb1e54dSMiklos Szeredi DCACHE_OP_HASH | 176bbb1e54dSMiklos Szeredi DCACHE_OP_COMPARE); 177bbb1e54dSMiklos Szeredi } 178bbb1e54dSMiklos Szeredi 179bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry) 180bbb1e54dSMiklos Szeredi { 181a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 182bbb1e54dSMiklos Szeredi enum ovl_path_type type = 0; 183bbb1e54dSMiklos Szeredi 18409d8b586SMiklos Szeredi if (ovl_dentry_upper(dentry)) { 185bbb1e54dSMiklos Szeredi type = __OVL_PATH_UPPER; 186bbb1e54dSMiklos Szeredi 187bbb1e54dSMiklos Szeredi /* 18859548503SAmir Goldstein * Non-dir dentry can hold lower dentry of its copy up origin. 189bbb1e54dSMiklos Szeredi */ 1905522c9c7SAmir Goldstein if (ovl_numlower(oe)) { 19160124877SVivek Goyal if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) 19259548503SAmir Goldstein type |= __OVL_PATH_ORIGIN; 1930b17c28aSVivek Goyal if (d_is_dir(dentry) || 1940b17c28aSVivek Goyal !ovl_has_upperdata(d_inode(dentry))) 195bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 19659548503SAmir Goldstein } 197bbb1e54dSMiklos Szeredi } else { 1985522c9c7SAmir Goldstein if (ovl_numlower(oe) > 1) 199bbb1e54dSMiklos Szeredi type |= __OVL_PATH_MERGE; 200bbb1e54dSMiklos Szeredi } 201bbb1e54dSMiklos Szeredi return type; 202bbb1e54dSMiklos Szeredi } 203bbb1e54dSMiklos Szeredi 204bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path) 205bbb1e54dSMiklos Szeredi { 206bbb1e54dSMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 207bbb1e54dSMiklos Szeredi 20808f4c7c8SMiklos Szeredi path->mnt = ovl_upper_mnt(ofs); 20909d8b586SMiklos Szeredi path->dentry = ovl_dentry_upper(dentry); 210bbb1e54dSMiklos Szeredi } 211bbb1e54dSMiklos Szeredi 212bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path) 213bbb1e54dSMiklos Szeredi { 214a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 2155522c9c7SAmir Goldstein struct ovl_path *lowerpath = ovl_lowerstack(oe); 216bbb1e54dSMiklos Szeredi 2175522c9c7SAmir Goldstein if (ovl_numlower(oe)) { 2185522c9c7SAmir Goldstein path->mnt = lowerpath->layer->mnt; 2195522c9c7SAmir Goldstein path->dentry = lowerpath->dentry; 220b9343632SChandan Rajendra } else { 221b9343632SChandan Rajendra *path = (struct path) { }; 222b9343632SChandan Rajendra } 223bbb1e54dSMiklos Szeredi } 224bbb1e54dSMiklos Szeredi 2254f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path) 2264f93b426SVivek Goyal { 227a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 228ab1eb5ffSAmir Goldstein struct ovl_path *lowerdata = ovl_lowerdata(oe); 229*2b21da92SAmir Goldstein struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe); 2304f93b426SVivek Goyal 231*2b21da92SAmir Goldstein if (lowerdata_dentry) { 232ab1eb5ffSAmir Goldstein path->mnt = lowerdata->layer->mnt; 233*2b21da92SAmir Goldstein path->dentry = lowerdata_dentry; 2344f93b426SVivek Goyal } else { 2354f93b426SVivek Goyal *path = (struct path) { }; 2364f93b426SVivek Goyal } 2374f93b426SVivek Goyal } 2384f93b426SVivek Goyal 239bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 240bbb1e54dSMiklos Szeredi { 241bbb1e54dSMiklos Szeredi enum ovl_path_type type = ovl_path_type(dentry); 242bbb1e54dSMiklos Szeredi 243bbb1e54dSMiklos Szeredi if (!OVL_TYPE_UPPER(type)) 244bbb1e54dSMiklos Szeredi ovl_path_lower(dentry, path); 245bbb1e54dSMiklos Szeredi else 246bbb1e54dSMiklos Szeredi ovl_path_upper(dentry, path); 247bbb1e54dSMiklos Szeredi 248bbb1e54dSMiklos Szeredi return type; 249bbb1e54dSMiklos Szeredi } 250bbb1e54dSMiklos Szeredi 2511248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path) 2521248ea4bSAmir Goldstein { 2531248ea4bSAmir Goldstein enum ovl_path_type type = ovl_path_type(dentry); 2541248ea4bSAmir Goldstein 2551248ea4bSAmir Goldstein WARN_ON_ONCE(d_is_dir(dentry)); 2561248ea4bSAmir Goldstein 2571248ea4bSAmir Goldstein if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type)) 2581248ea4bSAmir Goldstein ovl_path_lowerdata(dentry, path); 2591248ea4bSAmir Goldstein else 2601248ea4bSAmir Goldstein ovl_path_upper(dentry, path); 2611248ea4bSAmir Goldstein 2621248ea4bSAmir Goldstein return type; 2631248ea4bSAmir Goldstein } 2641248ea4bSAmir Goldstein 265bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry) 266bbb1e54dSMiklos Szeredi { 26709d8b586SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); 268bbb1e54dSMiklos Szeredi } 269bbb1e54dSMiklos Szeredi 270bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry) 271bbb1e54dSMiklos Szeredi { 272a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 273bbb1e54dSMiklos Szeredi 2745522c9c7SAmir Goldstein return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL; 275bbb1e54dSMiklos Szeredi } 276bbb1e54dSMiklos Szeredi 27713464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) 278da309e8cSAmir Goldstein { 279a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 280da309e8cSAmir Goldstein 2815522c9c7SAmir Goldstein return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL; 282da309e8cSAmir Goldstein } 283da309e8cSAmir Goldstein 284647d253fSVivek Goyal /* 285647d253fSVivek Goyal * ovl_dentry_lower() could return either a data dentry or metacopy dentry 286597534e7SXiong Zhenwu * depending on what is stored in lowerstack[0]. At times we need to find 287647d253fSVivek Goyal * lower dentry which has data (and not metacopy dentry). This helper 288647d253fSVivek Goyal * returns the lower data dentry. 289647d253fSVivek Goyal */ 290647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) 291647d253fSVivek Goyal { 292ab1eb5ffSAmir Goldstein return ovl_lowerdata_dentry(OVL_E(dentry)); 293647d253fSVivek Goyal } 294647d253fSVivek Goyal 295bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry) 296bbb1e54dSMiklos Szeredi { 29709d8b586SMiklos Szeredi return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 298bbb1e54dSMiklos Szeredi } 299bbb1e54dSMiklos Szeredi 3001d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode) 3011d88f183SMiklos Szeredi { 3021d88f183SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(inode)); 3031d88f183SMiklos Szeredi } 3041d88f183SMiklos Szeredi 305b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path) 306ffa5723cSAmir Goldstein { 307ac900ed4SAmir Goldstein struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 308ac900ed4SAmir Goldstein 309ffa5723cSAmir Goldstein path->dentry = ovl_i_dentry_upper(inode); 310ffa5723cSAmir Goldstein if (!path->dentry) { 311ac900ed4SAmir Goldstein path->dentry = lowerpath->dentry; 312ac900ed4SAmir Goldstein path->mnt = lowerpath->layer->mnt; 313ffa5723cSAmir Goldstein } else { 314ffa5723cSAmir Goldstein path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb)); 315ffa5723cSAmir Goldstein } 316b2dd05f1SZhihao Cheng 317b2dd05f1SZhihao Cheng return path->dentry ? d_inode_rcu(path->dentry) : NULL; 318ffa5723cSAmir Goldstein } 319ffa5723cSAmir Goldstein 32009d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode) 32125b7713aSMiklos Szeredi { 3221d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 32325b7713aSMiklos Szeredi 32409d8b586SMiklos Szeredi return upperdentry ? d_inode(upperdentry) : NULL; 32525b7713aSMiklos Szeredi } 32625b7713aSMiklos Szeredi 32709d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode) 32809d8b586SMiklos Szeredi { 329ac900ed4SAmir Goldstein struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 330ffa5723cSAmir Goldstein 331ac900ed4SAmir Goldstein return lowerpath ? d_inode(lowerpath->dentry) : NULL; 33209d8b586SMiklos Szeredi } 33309d8b586SMiklos Szeredi 33409d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode) 33509d8b586SMiklos Szeredi { 33609d8b586SMiklos Szeredi return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 33709d8b586SMiklos Szeredi } 33809d8b586SMiklos Szeredi 3392664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */ 3402664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode) 3412664bd08SVivek Goyal { 342ab1eb5ffSAmir Goldstein struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode)); 343ab1eb5ffSAmir Goldstein 3442664bd08SVivek Goyal if (WARN_ON(!S_ISREG(inode->i_mode))) 3452664bd08SVivek Goyal return NULL; 3462664bd08SVivek Goyal 347ab1eb5ffSAmir Goldstein return lowerdata ? d_inode(lowerdata) : NULL; 3482664bd08SVivek Goyal } 34909d8b586SMiklos Szeredi 3504823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */ 3514823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode) 3524823d49cSVivek Goyal { 3534823d49cSVivek Goyal struct inode *upperinode; 3544823d49cSVivek Goyal 3554823d49cSVivek Goyal upperinode = ovl_inode_upper(inode); 3564823d49cSVivek Goyal if (upperinode && ovl_has_upperdata(inode)) 3574823d49cSVivek Goyal return upperinode; 3584823d49cSVivek Goyal 3594823d49cSVivek Goyal return ovl_inode_lowerdata(inode); 3604823d49cSVivek Goyal } 3614823d49cSVivek Goyal 362*2b21da92SAmir Goldstein const char *ovl_lowerdata_redirect(struct inode *inode) 363*2b21da92SAmir Goldstein { 364*2b21da92SAmir Goldstein return inode && S_ISREG(inode->i_mode) ? 365*2b21da92SAmir Goldstein OVL_I(inode)->lowerdata_redirect : NULL; 366*2b21da92SAmir Goldstein } 367*2b21da92SAmir Goldstein 3684edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 369bbb1e54dSMiklos Szeredi { 370*2b21da92SAmir Goldstein return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL; 371bbb1e54dSMiklos Szeredi } 372bbb1e54dSMiklos Szeredi 3734edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 374bbb1e54dSMiklos Szeredi { 3754edb83bbSMiklos Szeredi OVL_I(inode)->cache = cache; 376bbb1e54dSMiklos Szeredi } 377bbb1e54dSMiklos Szeredi 378c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 379c62520a8SAmir Goldstein { 380a6ff2bc0SAmir Goldstein set_bit(flag, OVL_E_FLAGS(dentry)); 381c62520a8SAmir Goldstein } 382c62520a8SAmir Goldstein 383c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 384c62520a8SAmir Goldstein { 385a6ff2bc0SAmir Goldstein clear_bit(flag, OVL_E_FLAGS(dentry)); 386c62520a8SAmir Goldstein } 387c62520a8SAmir Goldstein 388c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 389c62520a8SAmir Goldstein { 390a6ff2bc0SAmir Goldstein return test_bit(flag, OVL_E_FLAGS(dentry)); 391c62520a8SAmir Goldstein } 392c62520a8SAmir Goldstein 393bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry) 394bbb1e54dSMiklos Szeredi { 395c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 396bbb1e54dSMiklos Szeredi } 397bbb1e54dSMiklos Szeredi 398bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry) 399bbb1e54dSMiklos Szeredi { 400bbb1e54dSMiklos Szeredi return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 401bbb1e54dSMiklos Szeredi } 402bbb1e54dSMiklos Szeredi 4035cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry) 404bbb1e54dSMiklos Szeredi { 405c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 406bbb1e54dSMiklos Szeredi } 407bbb1e54dSMiklos Szeredi 40855acc661SMiklos Szeredi /* 409aa3ff3c1SAmir Goldstein * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 410aa3ff3c1SAmir Goldstein * to return positive, while there's no actual upper alias for the inode. 411aa3ff3c1SAmir Goldstein * Copy up code needs to know about the existence of the upper alias, so it 412aa3ff3c1SAmir Goldstein * can't use ovl_dentry_upper(). 41355acc661SMiklos Szeredi */ 41455acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry) 41555acc661SMiklos Szeredi { 416c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 41755acc661SMiklos Szeredi } 41855acc661SMiklos Szeredi 41955acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry) 42055acc661SMiklos Szeredi { 421c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 42255acc661SMiklos Szeredi } 42355acc661SMiklos Szeredi 4240c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode) 4250c288874SVivek Goyal { 4260c288874SVivek Goyal if (!S_ISREG(inode->i_mode)) 4270c288874SVivek Goyal return false; 4280c288874SVivek Goyal 4290c288874SVivek Goyal if (!ovl_inode_lower(inode)) 4300c288874SVivek Goyal return false; 4310c288874SVivek Goyal 4320c288874SVivek Goyal return true; 4330c288874SVivek Goyal } 4340c288874SVivek Goyal 4350c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode) 4360c288874SVivek Goyal { 4370c288874SVivek Goyal if (!ovl_should_check_upperdata(inode)) 4380c288874SVivek Goyal return true; 4390c288874SVivek Goyal 4400c288874SVivek Goyal if (!ovl_test_flag(OVL_UPPERDATA, inode)) 4410c288874SVivek Goyal return false; 4420c288874SVivek Goyal /* 4430c288874SVivek Goyal * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 4440c288874SVivek Goyal * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 4450c288874SVivek Goyal * if setting of OVL_UPPERDATA is visible, then effects of writes 4460c288874SVivek Goyal * before that are visible too. 4470c288874SVivek Goyal */ 4480c288874SVivek Goyal smp_rmb(); 4490c288874SVivek Goyal return true; 4500c288874SVivek Goyal } 4510c288874SVivek Goyal 4520c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode) 4530c288874SVivek Goyal { 4540c288874SVivek Goyal /* 4550c288874SVivek Goyal * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 4560c288874SVivek Goyal * if OVL_UPPERDATA flag is visible, then effects of write operations 4570c288874SVivek Goyal * before it are visible as well. 4580c288874SVivek Goyal */ 4590c288874SVivek Goyal smp_wmb(); 4600c288874SVivek Goyal ovl_set_flag(OVL_UPPERDATA, inode); 4610c288874SVivek Goyal } 4620c288874SVivek Goyal 4630c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 4640c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 4650c288874SVivek Goyal { 4660c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 4670c288874SVivek Goyal return false; 4680c288874SVivek Goyal 4690c288874SVivek Goyal return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 4700c288874SVivek Goyal } 4710c288874SVivek Goyal 4720c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 4730c288874SVivek Goyal { 4740c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 4750c288874SVivek Goyal return false; 4760c288874SVivek Goyal 4770c288874SVivek Goyal return !ovl_has_upperdata(d_inode(dentry)); 4780c288874SVivek Goyal } 4790c288874SVivek Goyal 480a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb) 481a6c60655SMiklos Szeredi { 482a6c60655SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 483a6c60655SMiklos Szeredi 48421a22878SAmir Goldstein return ofs->config.redirect_dir && !ofs->noxattr; 485a6c60655SMiklos Szeredi } 486a6c60655SMiklos Szeredi 487a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry) 488a6c60655SMiklos Szeredi { 489cf31c463SMiklos Szeredi return OVL_I(d_inode(dentry))->redirect; 490a6c60655SMiklos Szeredi } 491a6c60655SMiklos Szeredi 492a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 493a6c60655SMiklos Szeredi { 494cf31c463SMiklos Szeredi struct ovl_inode *oi = OVL_I(d_inode(dentry)); 495a6c60655SMiklos Szeredi 496cf31c463SMiklos Szeredi kfree(oi->redirect); 497cf31c463SMiklos Szeredi oi->redirect = redirect; 498a6c60655SMiklos Szeredi } 499a6c60655SMiklos Szeredi 50009d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 501bbb1e54dSMiklos Szeredi { 50209d8b586SMiklos Szeredi struct inode *upperinode = d_inode(upperdentry); 503e6d2ebddSMiklos Szeredi 50409d8b586SMiklos Szeredi WARN_ON(OVL_I(inode)->__upperdentry); 50509d8b586SMiklos Szeredi 50625b7713aSMiklos Szeredi /* 50709d8b586SMiklos Szeredi * Make sure upperdentry is consistent before making it visible 50825b7713aSMiklos Szeredi */ 50925b7713aSMiklos Szeredi smp_wmb(); 51009d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 51131747edaSAmir Goldstein if (inode_unhashed(inode)) { 51225b7713aSMiklos Szeredi inode->i_private = upperinode; 513bbb1e54dSMiklos Szeredi __insert_inode_hash(inode, (unsigned long) upperinode); 514bbb1e54dSMiklos Szeredi } 51525b7713aSMiklos Szeredi } 516bbb1e54dSMiklos Szeredi 51765cd913eSAmir Goldstein static void ovl_dir_version_inc(struct dentry *dentry, bool impurity) 518bbb1e54dSMiklos Szeredi { 51904a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 520bbb1e54dSMiklos Szeredi 52104a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 52265cd913eSAmir Goldstein WARN_ON(!d_is_dir(dentry)); 5234edb83bbSMiklos Szeredi /* 52465cd913eSAmir Goldstein * Version is used by readdir code to keep cache consistent. 52565cd913eSAmir Goldstein * For merge dirs (or dirs with origin) all changes need to be noted. 52665cd913eSAmir Goldstein * For non-merge dirs, cache contains only impure entries (i.e. ones 52765cd913eSAmir Goldstein * which have been copied up and have origins), so only need to note 52865cd913eSAmir Goldstein * changes to impure entries. 5294edb83bbSMiklos Szeredi */ 5301fa9c5c5SMiklos Szeredi if (!ovl_dir_is_real(inode) || impurity) 53104a01ac7SMiklos Szeredi OVL_I(inode)->version++; 532bbb1e54dSMiklos Szeredi } 533bbb1e54dSMiklos Szeredi 534d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity) 535d9854c87SMiklos Szeredi { 536d9854c87SMiklos Szeredi /* Copy mtime/ctime */ 5372878dffcSChristian Brauner ovl_copyattr(d_inode(dentry)); 538d9854c87SMiklos Szeredi 53965cd913eSAmir Goldstein ovl_dir_version_inc(dentry, impurity); 540d9854c87SMiklos Szeredi } 541d9854c87SMiklos Szeredi 5421fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode) 543bbb1e54dSMiklos Szeredi { 54404a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 54504a01ac7SMiklos Szeredi return OVL_I(inode)->version; 546bbb1e54dSMiklos Szeredi } 547bbb1e54dSMiklos Szeredi 548bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry) 549bbb1e54dSMiklos Szeredi { 550bbb1e54dSMiklos Szeredi struct inode *inode = dentry->d_inode; 551bbb1e54dSMiklos Szeredi 552bbb1e54dSMiklos Szeredi return inode && IS_WHITEOUT(inode); 553bbb1e54dSMiklos Szeredi } 554bbb1e54dSMiklos Szeredi 5552d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags) 556bbb1e54dSMiklos Szeredi { 55756230d95SMiklos Szeredi struct inode *inode = d_inode(path->dentry); 5584609e1f1SChristian Brauner struct mnt_idmap *real_idmap = mnt_idmap(path->mnt); 55956230d95SMiklos Szeredi int err, acc_mode; 56056230d95SMiklos Szeredi 56156230d95SMiklos Szeredi if (flags & ~(O_ACCMODE | O_LARGEFILE)) 56256230d95SMiklos Szeredi BUG(); 56356230d95SMiklos Szeredi 56456230d95SMiklos Szeredi switch (flags & O_ACCMODE) { 56556230d95SMiklos Szeredi case O_RDONLY: 56656230d95SMiklos Szeredi acc_mode = MAY_READ; 56756230d95SMiklos Szeredi break; 56856230d95SMiklos Szeredi case O_WRONLY: 56956230d95SMiklos Szeredi acc_mode = MAY_WRITE; 57056230d95SMiklos Szeredi break; 57156230d95SMiklos Szeredi default: 57256230d95SMiklos Szeredi BUG(); 57356230d95SMiklos Szeredi } 57456230d95SMiklos Szeredi 5754609e1f1SChristian Brauner err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN); 57656230d95SMiklos Szeredi if (err) 57756230d95SMiklos Szeredi return ERR_PTR(err); 57856230d95SMiklos Szeredi 57956230d95SMiklos Szeredi /* O_NOATIME is an optimization, don't fail if not permitted */ 58001beba79SChristian Brauner if (inode_owner_or_capable(real_idmap, inode)) 58156230d95SMiklos Szeredi flags |= O_NOATIME; 58256230d95SMiklos Szeredi 58356230d95SMiklos Szeredi return dentry_open(path, flags, current_cred()); 584bbb1e54dSMiklos Szeredi } 58539d3d60aSAmir Goldstein 5860c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 5870c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 5880c288874SVivek Goyal { 5890c288874SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5900c288874SVivek Goyal 5910c288874SVivek Goyal if (ovl_dentry_upper(dentry) && 5920c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 5930c288874SVivek Goyal !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 5940c288874SVivek Goyal return true; 5950c288874SVivek Goyal 5960c288874SVivek Goyal return false; 5970c288874SVivek Goyal } 5980c288874SVivek Goyal 5990c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags) 6002002df85SVivek Goyal { 6012002df85SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 6022002df85SVivek Goyal 6032002df85SVivek Goyal /* 6042002df85SVivek Goyal * Check if copy-up has happened as well as for upper alias (in 6052002df85SVivek Goyal * case of hard links) is there. 6062002df85SVivek Goyal * 6072002df85SVivek Goyal * Both checks are lockless: 6082002df85SVivek Goyal * - false negatives: will recheck under oi->lock 6092002df85SVivek Goyal * - false positives: 6102002df85SVivek Goyal * + ovl_dentry_upper() uses memory barriers to ensure the 6112002df85SVivek Goyal * upper dentry is up-to-date 6122002df85SVivek Goyal * + ovl_dentry_has_upper_alias() relies on locking of 6132002df85SVivek Goyal * upper parent i_rwsem to prevent reordering copy-up 6142002df85SVivek Goyal * with rename. 6152002df85SVivek Goyal */ 6162002df85SVivek Goyal if (ovl_dentry_upper(dentry) && 6170c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 6180c288874SVivek Goyal !ovl_dentry_needs_data_copy_up(dentry, flags)) 6192002df85SVivek Goyal return true; 6202002df85SVivek Goyal 6212002df85SVivek Goyal return false; 6222002df85SVivek Goyal } 6232002df85SVivek Goyal 6240c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags) 62539d3d60aSAmir Goldstein { 6261e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 62739d3d60aSAmir Goldstein int err; 62839d3d60aSAmir Goldstein 629531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 6300c288874SVivek Goyal if (!err && ovl_already_copied_up_locked(dentry, flags)) { 63139d3d60aSAmir Goldstein err = 1; /* Already copied up */ 6321e92e307SAmir Goldstein ovl_inode_unlock(inode); 63339d3d60aSAmir Goldstein } 63439d3d60aSAmir Goldstein 63539d3d60aSAmir Goldstein return err; 63639d3d60aSAmir Goldstein } 63739d3d60aSAmir Goldstein 63839d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry) 63939d3d60aSAmir Goldstein { 6401e92e307SAmir Goldstein ovl_inode_unlock(d_inode(dentry)); 64139d3d60aSAmir Goldstein } 64282b749b2SAmir Goldstein 6432d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path) 644b79e05aaSAmir Goldstein { 645b79e05aaSAmir Goldstein int res; 646b79e05aaSAmir Goldstein 647dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0); 648b79e05aaSAmir Goldstein 649b79e05aaSAmir Goldstein /* Zero size value means "copied up but origin unknown" */ 650b79e05aaSAmir Goldstein if (res >= 0) 651b79e05aaSAmir Goldstein return true; 652b79e05aaSAmir Goldstein 653b79e05aaSAmir Goldstein return false; 654b79e05aaSAmir Goldstein } 655b79e05aaSAmir Goldstein 6562d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path, 65743d193f8SMiklos Szeredi enum ovl_xattr ox) 658f3a15685SAmir Goldstein { 659f3a15685SAmir Goldstein int res; 660f3a15685SAmir Goldstein char val; 661f3a15685SAmir Goldstein 662dad7017aSChristian Brauner if (!d_is_dir(path->dentry)) 663f3a15685SAmir Goldstein return false; 664f3a15685SAmir Goldstein 665dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, ox, &val, 1); 666f3a15685SAmir Goldstein if (res == 1 && val == 'y') 667f3a15685SAmir Goldstein return true; 668f3a15685SAmir Goldstein 669f3a15685SAmir Goldstein return false; 670f3a15685SAmir Goldstein } 671f3a15685SAmir Goldstein 67243d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX "opaque" 67343d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX "redirect" 67443d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX "origin" 67543d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX "impure" 67643d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX "nlink" 67743d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX "upper" 67843d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX "metacopy" 679096a218aSAmir Goldstein #define OVL_XATTR_PROTATTR_POSTFIX "protattr" 68043d193f8SMiklos Szeredi 68143d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \ 6822d2f2d73SMiklos Szeredi [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \ 6832d2f2d73SMiklos Szeredi [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX } 68443d193f8SMiklos Szeredi 6852d2f2d73SMiklos Szeredi const char *const ovl_xattr_table[][2] = { 68643d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE), 68743d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT), 68843d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN), 68943d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE), 69043d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK), 69143d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER), 69243d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY), 693096a218aSAmir Goldstein OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR), 69443d193f8SMiklos Szeredi }; 69543d193f8SMiklos Szeredi 696a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry, 69743d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, size_t size, 69882b749b2SAmir Goldstein int xerr) 69982b749b2SAmir Goldstein { 70082b749b2SAmir Goldstein int err; 70182b749b2SAmir Goldstein 70282b749b2SAmir Goldstein if (ofs->noxattr) 70382b749b2SAmir Goldstein return xerr; 70482b749b2SAmir Goldstein 705c914c0e2SAmir Goldstein err = ovl_setxattr(ofs, upperdentry, ox, value, size); 70682b749b2SAmir Goldstein 70782b749b2SAmir Goldstein if (err == -EOPNOTSUPP) { 70843d193f8SMiklos Szeredi pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox)); 70982b749b2SAmir Goldstein ofs->noxattr = true; 71082b749b2SAmir Goldstein return xerr; 71182b749b2SAmir Goldstein } 71282b749b2SAmir Goldstein 71382b749b2SAmir Goldstein return err; 71482b749b2SAmir Goldstein } 715f3a15685SAmir Goldstein 716f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 717f3a15685SAmir Goldstein { 718a0c236b1SAmir Goldstein struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 719f3a15685SAmir Goldstein int err; 720f3a15685SAmir Goldstein 72113c72075SMiklos Szeredi if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 722f3a15685SAmir Goldstein return 0; 723f3a15685SAmir Goldstein 724f3a15685SAmir Goldstein /* 725f3a15685SAmir Goldstein * Do not fail when upper doesn't support xattrs. 726f3a15685SAmir Goldstein * Upper inodes won't have origin nor redirect xattr anyway. 727f3a15685SAmir Goldstein */ 728a0c236b1SAmir Goldstein err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0); 729f3a15685SAmir Goldstein if (!err) 73013c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 731f3a15685SAmir Goldstein 732f3a15685SAmir Goldstein return err; 733f3a15685SAmir Goldstein } 73413c72075SMiklos Szeredi 735096a218aSAmir Goldstein 736096a218aSAmir Goldstein #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */ 737096a218aSAmir Goldstein 738096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper) 739096a218aSAmir Goldstein { 740096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 741096a218aSAmir Goldstein u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK; 742096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX+1]; 743096a218aSAmir Goldstein int res, n; 744096a218aSAmir Goldstein 745dad7017aSChristian Brauner res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf, 746096a218aSAmir Goldstein OVL_PROTATTR_MAX); 747096a218aSAmir Goldstein if (res < 0) 748096a218aSAmir Goldstein return; 749096a218aSAmir Goldstein 750096a218aSAmir Goldstein /* 751096a218aSAmir Goldstein * Initialize inode flags from overlay.protattr xattr and upper inode 752096a218aSAmir Goldstein * flags. If upper inode has those fileattr flags set (i.e. from old 753096a218aSAmir Goldstein * kernel), we do not clear them on ovl_get_inode(), but we will clear 754096a218aSAmir Goldstein * them on next fileattr_set(). 755096a218aSAmir Goldstein */ 756096a218aSAmir Goldstein for (n = 0; n < res; n++) { 757096a218aSAmir Goldstein if (buf[n] == 'a') 758096a218aSAmir Goldstein iflags |= S_APPEND; 759096a218aSAmir Goldstein else if (buf[n] == 'i') 760096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 761096a218aSAmir Goldstein else 762096a218aSAmir Goldstein break; 763096a218aSAmir Goldstein } 764096a218aSAmir Goldstein 765096a218aSAmir Goldstein if (!res || n < res) { 766096a218aSAmir Goldstein pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n", 767096a218aSAmir Goldstein upper, res); 768096a218aSAmir Goldstein } else { 769096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 770096a218aSAmir Goldstein } 771096a218aSAmir Goldstein } 772096a218aSAmir Goldstein 773096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper, 774096a218aSAmir Goldstein struct fileattr *fa) 775096a218aSAmir Goldstein { 776096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 777096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX]; 778096a218aSAmir Goldstein int len = 0, err = 0; 779096a218aSAmir Goldstein u32 iflags = 0; 780096a218aSAmir Goldstein 781096a218aSAmir Goldstein BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX); 782096a218aSAmir Goldstein 783096a218aSAmir Goldstein if (fa->flags & FS_APPEND_FL) { 784096a218aSAmir Goldstein buf[len++] = 'a'; 785096a218aSAmir Goldstein iflags |= S_APPEND; 786096a218aSAmir Goldstein } 787096a218aSAmir Goldstein if (fa->flags & FS_IMMUTABLE_FL) { 788096a218aSAmir Goldstein buf[len++] = 'i'; 789096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 790096a218aSAmir Goldstein } 791096a218aSAmir Goldstein 792096a218aSAmir Goldstein /* 793096a218aSAmir Goldstein * Do not allow to set protection flags when upper doesn't support 794096a218aSAmir Goldstein * xattrs, because we do not set those fileattr flags on upper inode. 795096a218aSAmir Goldstein * Remove xattr if it exist and all protection flags are cleared. 796096a218aSAmir Goldstein */ 797096a218aSAmir Goldstein if (len) { 798096a218aSAmir Goldstein err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR, 799096a218aSAmir Goldstein buf, len, -EPERM); 800096a218aSAmir Goldstein } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) { 801c914c0e2SAmir Goldstein err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR); 802096a218aSAmir Goldstein if (err == -EOPNOTSUPP || err == -ENODATA) 803096a218aSAmir Goldstein err = 0; 804096a218aSAmir Goldstein } 805096a218aSAmir Goldstein if (err) 806096a218aSAmir Goldstein return err; 807096a218aSAmir Goldstein 808096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 809096a218aSAmir Goldstein 810096a218aSAmir Goldstein /* Mask out the fileattr flags that should not be set in upper inode */ 811096a218aSAmir Goldstein fa->flags &= ~OVL_PROT_FS_FLAGS_MASK; 812096a218aSAmir Goldstein fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK; 813096a218aSAmir Goldstein 814096a218aSAmir Goldstein return 0; 815096a218aSAmir Goldstein } 816096a218aSAmir Goldstein 817ad0af710SAmir Goldstein /** 818ad0af710SAmir Goldstein * Caller must hold a reference to inode to prevent it from being freed while 819ad0af710SAmir Goldstein * it is marked inuse. 820ad0af710SAmir Goldstein */ 821ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry) 822ad0af710SAmir Goldstein { 823ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 824ad0af710SAmir Goldstein bool locked = false; 825ad0af710SAmir Goldstein 826ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 827ad0af710SAmir Goldstein if (!(inode->i_state & I_OVL_INUSE)) { 828ad0af710SAmir Goldstein inode->i_state |= I_OVL_INUSE; 829ad0af710SAmir Goldstein locked = true; 830ad0af710SAmir Goldstein } 831ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 832ad0af710SAmir Goldstein 833ad0af710SAmir Goldstein return locked; 834ad0af710SAmir Goldstein } 835ad0af710SAmir Goldstein 836ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry) 837ad0af710SAmir Goldstein { 838ad0af710SAmir Goldstein if (dentry) { 839ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 840ad0af710SAmir Goldstein 841ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 842ad0af710SAmir Goldstein WARN_ON(!(inode->i_state & I_OVL_INUSE)); 843ad0af710SAmir Goldstein inode->i_state &= ~I_OVL_INUSE; 844ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 845ad0af710SAmir Goldstein } 846ad0af710SAmir Goldstein } 8475f8415d6SAmir Goldstein 848146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry) 849146d62e5SAmir Goldstein { 850146d62e5SAmir Goldstein struct inode *inode = d_inode(dentry); 851146d62e5SAmir Goldstein bool inuse; 852146d62e5SAmir Goldstein 853146d62e5SAmir Goldstein spin_lock(&inode->i_lock); 854146d62e5SAmir Goldstein inuse = (inode->i_state & I_OVL_INUSE); 855146d62e5SAmir Goldstein spin_unlock(&inode->i_lock); 856146d62e5SAmir Goldstein 857146d62e5SAmir Goldstein return inuse; 858146d62e5SAmir Goldstein } 859146d62e5SAmir Goldstein 86024b33ee1SAmir Goldstein /* 86124b33ee1SAmir Goldstein * Does this overlay dentry need to be indexed on copy up? 86224b33ee1SAmir Goldstein */ 86324b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry) 86424b33ee1SAmir Goldstein { 86524b33ee1SAmir Goldstein struct dentry *lower = ovl_dentry_lower(dentry); 86624b33ee1SAmir Goldstein 86724b33ee1SAmir Goldstein if (!lower || !ovl_indexdir(dentry->d_sb)) 86824b33ee1SAmir Goldstein return false; 86924b33ee1SAmir Goldstein 870fbd2d207SAmir Goldstein /* Index all files for NFS export and consistency verification */ 871016b720fSAmir Goldstein if (ovl_index_all(dentry->d_sb)) 872fbd2d207SAmir Goldstein return true; 873fbd2d207SAmir Goldstein 87424b33ee1SAmir Goldstein /* Index only lower hardlinks on copy up */ 87524b33ee1SAmir Goldstein if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 87624b33ee1SAmir Goldstein return true; 87724b33ee1SAmir Goldstein 87824b33ee1SAmir Goldstein return false; 87924b33ee1SAmir Goldstein } 88024b33ee1SAmir Goldstein 8819f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */ 882caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry) 883caf70cb2SAmir Goldstein { 8841cdb0cb6SPavel Tikhomirov struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 885e7dd0e71SAmir Goldstein struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 886e7dd0e71SAmir Goldstein struct inode *dir = indexdir->d_inode; 887caf70cb2SAmir Goldstein struct dentry *lowerdentry = ovl_dentry_lower(dentry); 888caf70cb2SAmir Goldstein struct dentry *upperdentry = ovl_dentry_upper(dentry); 889caf70cb2SAmir Goldstein struct dentry *index = NULL; 890caf70cb2SAmir Goldstein struct inode *inode; 89163e13252SAmir Goldstein struct qstr name = { }; 892caf70cb2SAmir Goldstein int err; 893caf70cb2SAmir Goldstein 8941cdb0cb6SPavel Tikhomirov err = ovl_get_index_name(ofs, lowerdentry, &name); 895caf70cb2SAmir Goldstein if (err) 896caf70cb2SAmir Goldstein goto fail; 897caf70cb2SAmir Goldstein 898caf70cb2SAmir Goldstein inode = d_inode(upperdentry); 89989a17556SAmir Goldstein if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 9001bd0a3aeSlijiazi pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 901caf70cb2SAmir Goldstein upperdentry, inode->i_ino, inode->i_nlink); 902caf70cb2SAmir Goldstein /* 903caf70cb2SAmir Goldstein * We either have a bug with persistent union nlink or a lower 904caf70cb2SAmir Goldstein * hardlink was added while overlay is mounted. Adding a lower 905caf70cb2SAmir Goldstein * hardlink and then unlinking all overlay hardlinks would drop 906caf70cb2SAmir Goldstein * overlay nlink to zero before all upper inodes are unlinked. 907caf70cb2SAmir Goldstein * As a safety measure, when that situation is detected, set 908caf70cb2SAmir Goldstein * the overlay nlink to the index inode nlink minus one for the 909caf70cb2SAmir Goldstein * index entry itself. 910caf70cb2SAmir Goldstein */ 911caf70cb2SAmir Goldstein set_nlink(d_inode(dentry), inode->i_nlink - 1); 912caf70cb2SAmir Goldstein ovl_set_nlink_upper(dentry); 913caf70cb2SAmir Goldstein goto out; 914caf70cb2SAmir Goldstein } 915caf70cb2SAmir Goldstein 916caf70cb2SAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 91722f289ceSChristian Brauner index = ovl_lookup_upper(ofs, name.name, indexdir, name.len); 918caf70cb2SAmir Goldstein err = PTR_ERR(index); 919e7dd0e71SAmir Goldstein if (IS_ERR(index)) { 9209f4ec904SAmir Goldstein index = NULL; 921e7dd0e71SAmir Goldstein } else if (ovl_index_all(dentry->d_sb)) { 922e7dd0e71SAmir Goldstein /* Whiteout orphan index to block future open by handle */ 923c21c839bSChengguang Xu err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), 924c21c839bSChengguang Xu dir, index); 925e7dd0e71SAmir Goldstein } else { 926e7dd0e71SAmir Goldstein /* Cleanup orphan index entries */ 927576bb263SChristian Brauner err = ovl_cleanup(ofs, dir, index); 928e7dd0e71SAmir Goldstein } 9299f4ec904SAmir Goldstein 930caf70cb2SAmir Goldstein inode_unlock(dir); 931caf70cb2SAmir Goldstein if (err) 932caf70cb2SAmir Goldstein goto fail; 933caf70cb2SAmir Goldstein 934caf70cb2SAmir Goldstein out: 93563e13252SAmir Goldstein kfree(name.name); 936caf70cb2SAmir Goldstein dput(index); 937caf70cb2SAmir Goldstein return; 938caf70cb2SAmir Goldstein 939caf70cb2SAmir Goldstein fail: 9401bd0a3aeSlijiazi pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); 941caf70cb2SAmir Goldstein goto out; 942caf70cb2SAmir Goldstein } 943caf70cb2SAmir Goldstein 9445f8415d6SAmir Goldstein /* 9455f8415d6SAmir Goldstein * Operations that change overlay inode and upper inode nlink need to be 9465f8415d6SAmir Goldstein * synchronized with copy up for persistent nlink accounting. 9475f8415d6SAmir Goldstein */ 9480e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry) 9495f8415d6SAmir Goldstein { 9501e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 9515f8415d6SAmir Goldstein const struct cred *old_cred; 9525f8415d6SAmir Goldstein int err; 9535f8415d6SAmir Goldstein 9541e92e307SAmir Goldstein if (WARN_ON(!inode)) 9550e32992fSAmir Goldstein return -ENOENT; 9565f8415d6SAmir Goldstein 9575f8415d6SAmir Goldstein /* 9585f8415d6SAmir Goldstein * With inodes index is enabled, we store the union overlay nlink 95924b33ee1SAmir Goldstein * in an xattr on the index inode. When whiting out an indexed lower, 9605f8415d6SAmir Goldstein * we need to decrement the overlay persistent nlink, but before the 9615f8415d6SAmir Goldstein * first copy up, we have no upper index inode to store the xattr. 9625f8415d6SAmir Goldstein * 96324b33ee1SAmir Goldstein * As a workaround, before whiteout/rename over an indexed lower, 9645f8415d6SAmir Goldstein * copy up to create the upper index. Creating the upper index will 9655f8415d6SAmir Goldstein * initialize the overlay nlink, so it could be dropped if unlink 9665f8415d6SAmir Goldstein * or rename succeeds. 9675f8415d6SAmir Goldstein * 9685f8415d6SAmir Goldstein * TODO: implement metadata only index copy up when called with 9695f8415d6SAmir Goldstein * ovl_copy_up_flags(dentry, O_PATH). 9705f8415d6SAmir Goldstein */ 97124b33ee1SAmir Goldstein if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 9725f8415d6SAmir Goldstein err = ovl_copy_up(dentry); 9735f8415d6SAmir Goldstein if (err) 9745f8415d6SAmir Goldstein return err; 9755f8415d6SAmir Goldstein } 9765f8415d6SAmir Goldstein 977531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 9785f8415d6SAmir Goldstein if (err) 9795f8415d6SAmir Goldstein return err; 9805f8415d6SAmir Goldstein 9811e92e307SAmir Goldstein if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 9825f8415d6SAmir Goldstein goto out; 9835f8415d6SAmir Goldstein 9845f8415d6SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 9855f8415d6SAmir Goldstein /* 9865f8415d6SAmir Goldstein * The overlay inode nlink should be incremented/decremented IFF the 9875f8415d6SAmir Goldstein * upper operation succeeds, along with nlink change of upper inode. 9885f8415d6SAmir Goldstein * Therefore, before link/unlink/rename, we store the union nlink 9895f8415d6SAmir Goldstein * value relative to the upper inode nlink in an upper inode xattr. 9905f8415d6SAmir Goldstein */ 9915f8415d6SAmir Goldstein err = ovl_set_nlink_upper(dentry); 9925f8415d6SAmir Goldstein revert_creds(old_cred); 9935f8415d6SAmir Goldstein 9945f8415d6SAmir Goldstein out: 9955f8415d6SAmir Goldstein if (err) 9961e92e307SAmir Goldstein ovl_inode_unlock(inode); 9975f8415d6SAmir Goldstein 9985f8415d6SAmir Goldstein return err; 9995f8415d6SAmir Goldstein } 10005f8415d6SAmir Goldstein 10010e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry) 10025f8415d6SAmir Goldstein { 10031e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 10041e92e307SAmir Goldstein 10051e92e307SAmir Goldstein if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 1006caf70cb2SAmir Goldstein const struct cred *old_cred; 1007caf70cb2SAmir Goldstein 1008caf70cb2SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 1009caf70cb2SAmir Goldstein ovl_cleanup_index(dentry); 1010caf70cb2SAmir Goldstein revert_creds(old_cred); 1011caf70cb2SAmir Goldstein } 1012caf70cb2SAmir Goldstein 10131e92e307SAmir Goldstein ovl_inode_unlock(inode); 10145f8415d6SAmir Goldstein } 10155820dc08SAmir Goldstein 10165820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) 10175820dc08SAmir Goldstein { 10185820dc08SAmir Goldstein /* Workdir should not be the same as upperdir */ 10195820dc08SAmir Goldstein if (workdir == upperdir) 10205820dc08SAmir Goldstein goto err; 10215820dc08SAmir Goldstein 10225820dc08SAmir Goldstein /* Workdir should not be subdir of upperdir and vice versa */ 10235820dc08SAmir Goldstein if (lock_rename(workdir, upperdir) != NULL) 10245820dc08SAmir Goldstein goto err_unlock; 10255820dc08SAmir Goldstein 10265820dc08SAmir Goldstein return 0; 10275820dc08SAmir Goldstein 10285820dc08SAmir Goldstein err_unlock: 10295820dc08SAmir Goldstein unlock_rename(workdir, upperdir); 10305820dc08SAmir Goldstein err: 10311bd0a3aeSlijiazi pr_err("failed to lock workdir+upperdir\n"); 10325820dc08SAmir Goldstein return -EIO; 10335820dc08SAmir Goldstein } 10349d3dfea3SVivek Goyal 10359d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ 10362d343087SAl Viro int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path) 10379d3dfea3SVivek Goyal { 10389d3dfea3SVivek Goyal int res; 10399d3dfea3SVivek Goyal 10409d3dfea3SVivek Goyal /* Only regular files can have metacopy xattr */ 1041dad7017aSChristian Brauner if (!S_ISREG(d_inode(path->dentry)->i_mode)) 10429d3dfea3SVivek Goyal return 0; 10439d3dfea3SVivek Goyal 1044dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0); 10459d3dfea3SVivek Goyal if (res < 0) { 10469d3dfea3SVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 10479d3dfea3SVivek Goyal return 0; 104887b2c60cSMiklos Szeredi /* 104987b2c60cSMiklos Szeredi * getxattr on user.* may fail with EACCES in case there's no 105087b2c60cSMiklos Szeredi * read permission on the inode. Not much we can do, other than 105187b2c60cSMiklos Szeredi * tell the caller that this is not a metacopy inode. 105287b2c60cSMiklos Szeredi */ 105387b2c60cSMiklos Szeredi if (ofs->config.userxattr && res == -EACCES) 105487b2c60cSMiklos Szeredi return 0; 10559d3dfea3SVivek Goyal goto out; 10569d3dfea3SVivek Goyal } 10579d3dfea3SVivek Goyal 10589d3dfea3SVivek Goyal return 1; 10599d3dfea3SVivek Goyal out: 10601bd0a3aeSlijiazi pr_warn_ratelimited("failed to get metacopy (%i)\n", res); 10619d3dfea3SVivek Goyal return res; 10629d3dfea3SVivek Goyal } 106367d756c2SVivek Goyal 106467d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry) 106567d756c2SVivek Goyal { 1066a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 106767d756c2SVivek Goyal 106867d756c2SVivek Goyal if (!d_is_reg(dentry)) 106967d756c2SVivek Goyal return false; 107067d756c2SVivek Goyal 107167d756c2SVivek Goyal if (ovl_dentry_upper(dentry)) { 107267d756c2SVivek Goyal if (!ovl_has_upperdata(d_inode(dentry))) 107367d756c2SVivek Goyal return true; 107467d756c2SVivek Goyal return false; 107567d756c2SVivek Goyal } 107667d756c2SVivek Goyal 10775522c9c7SAmir Goldstein return (ovl_numlower(oe) > 1); 107867d756c2SVivek Goyal } 10790a2d0d3fSVivek Goyal 10802d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding) 10810a2d0d3fSVivek Goyal { 10820a2d0d3fSVivek Goyal int res; 10830a2d0d3fSVivek Goyal char *s, *next, *buf = NULL; 10840a2d0d3fSVivek Goyal 1085dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0); 108692f0d6c9SMiklos Szeredi if (res == -ENODATA || res == -EOPNOTSUPP) 10870a2d0d3fSVivek Goyal return NULL; 10880a2d0d3fSVivek Goyal if (res < 0) 108992f0d6c9SMiklos Szeredi goto fail; 109092f0d6c9SMiklos Szeredi if (res == 0) 109192f0d6c9SMiklos Szeredi goto invalid; 109292f0d6c9SMiklos Szeredi 109392f0d6c9SMiklos Szeredi buf = kzalloc(res + padding + 1, GFP_KERNEL); 109492f0d6c9SMiklos Szeredi if (!buf) 109592f0d6c9SMiklos Szeredi return ERR_PTR(-ENOMEM); 109692f0d6c9SMiklos Szeredi 1097dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res); 109892f0d6c9SMiklos Szeredi if (res < 0) 109992f0d6c9SMiklos Szeredi goto fail; 11000a2d0d3fSVivek Goyal if (res == 0) 11010a2d0d3fSVivek Goyal goto invalid; 11020a2d0d3fSVivek Goyal 11030a2d0d3fSVivek Goyal if (buf[0] == '/') { 11040a2d0d3fSVivek Goyal for (s = buf; *s++ == '/'; s = next) { 11050a2d0d3fSVivek Goyal next = strchrnul(s, '/'); 11060a2d0d3fSVivek Goyal if (s == next) 11070a2d0d3fSVivek Goyal goto invalid; 11080a2d0d3fSVivek Goyal } 11090a2d0d3fSVivek Goyal } else { 11100a2d0d3fSVivek Goyal if (strchr(buf, '/') != NULL) 11110a2d0d3fSVivek Goyal goto invalid; 11120a2d0d3fSVivek Goyal } 11130a2d0d3fSVivek Goyal 11140a2d0d3fSVivek Goyal return buf; 11150a2d0d3fSVivek Goyal invalid: 11161bd0a3aeSlijiazi pr_warn_ratelimited("invalid redirect (%s)\n", buf); 11170a2d0d3fSVivek Goyal res = -EINVAL; 111892f0d6c9SMiklos Szeredi goto err_free; 111992f0d6c9SMiklos Szeredi fail: 112092f0d6c9SMiklos Szeredi pr_warn_ratelimited("failed to get redirect (%i)\n", res); 112192f0d6c9SMiklos Szeredi err_free: 1122993a0b2aSVivek Goyal kfree(buf); 1123993a0b2aSVivek Goyal return ERR_PTR(res); 11240a2d0d3fSVivek Goyal } 1125335d3fc5SSargun Dhillon 1126335d3fc5SSargun Dhillon /* 1127335d3fc5SSargun Dhillon * ovl_sync_status() - Check fs sync status for volatile mounts 1128335d3fc5SSargun Dhillon * 1129335d3fc5SSargun Dhillon * Returns 1 if this is not a volatile mount and a real sync is required. 1130335d3fc5SSargun Dhillon * 1131335d3fc5SSargun Dhillon * Returns 0 if syncing can be skipped because mount is volatile, and no errors 1132335d3fc5SSargun Dhillon * have occurred on the upperdir since the mount. 1133335d3fc5SSargun Dhillon * 1134335d3fc5SSargun Dhillon * Returns -errno if it is a volatile mount, and the error that occurred since 1135335d3fc5SSargun Dhillon * the last mount. If the error code changes, it'll return the latest error 1136335d3fc5SSargun Dhillon * code. 1137335d3fc5SSargun Dhillon */ 1138335d3fc5SSargun Dhillon 1139335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs) 1140335d3fc5SSargun Dhillon { 1141335d3fc5SSargun Dhillon struct vfsmount *mnt; 1142335d3fc5SSargun Dhillon 1143335d3fc5SSargun Dhillon if (ovl_should_sync(ofs)) 1144335d3fc5SSargun Dhillon return 1; 1145335d3fc5SSargun Dhillon 1146335d3fc5SSargun Dhillon mnt = ovl_upper_mnt(ofs); 1147335d3fc5SSargun Dhillon if (!mnt) 1148335d3fc5SSargun Dhillon return 0; 1149335d3fc5SSargun Dhillon 1150335d3fc5SSargun Dhillon return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq); 1151335d3fc5SSargun Dhillon } 11522878dffcSChristian Brauner 11532878dffcSChristian Brauner /* 11542878dffcSChristian Brauner * ovl_copyattr() - copy inode attributes from layer to ovl inode 11552878dffcSChristian Brauner * 11562878dffcSChristian Brauner * When overlay copies inode information from an upper or lower layer to the 11572878dffcSChristian Brauner * relevant overlay inode it will apply the idmapping of the upper or lower 11582878dffcSChristian Brauner * layer when doing so ensuring that the ovl inode ownership will correctly 11592878dffcSChristian Brauner * reflect the ownership of the idmapped upper or lower layer. For example, an 11602878dffcSChristian Brauner * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to 11612878dffcSChristian Brauner * map any lower or upper inode owned by id 1001 to id 1000. These mapping 11622878dffcSChristian Brauner * helpers are nops when the relevant layer isn't idmapped. 11632878dffcSChristian Brauner */ 11642878dffcSChristian Brauner void ovl_copyattr(struct inode *inode) 11652878dffcSChristian Brauner { 11662878dffcSChristian Brauner struct path realpath; 11672878dffcSChristian Brauner struct inode *realinode; 1168e67fe633SChristian Brauner struct mnt_idmap *real_idmap; 116973db6a06SChristian Brauner vfsuid_t vfsuid; 117073db6a06SChristian Brauner vfsgid_t vfsgid; 11712878dffcSChristian Brauner 1172b2dd05f1SZhihao Cheng realinode = ovl_i_path_real(inode, &realpath); 1173e67fe633SChristian Brauner real_idmap = mnt_idmap(realpath.mnt); 11742878dffcSChristian Brauner 1175e67fe633SChristian Brauner vfsuid = i_uid_into_vfsuid(real_idmap, realinode); 1176e67fe633SChristian Brauner vfsgid = i_gid_into_vfsgid(real_idmap, realinode); 117773db6a06SChristian Brauner 117873db6a06SChristian Brauner inode->i_uid = vfsuid_into_kuid(vfsuid); 117973db6a06SChristian Brauner inode->i_gid = vfsgid_into_kgid(vfsgid); 11802878dffcSChristian Brauner inode->i_mode = realinode->i_mode; 11812878dffcSChristian Brauner inode->i_atime = realinode->i_atime; 11822878dffcSChristian Brauner inode->i_mtime = realinode->i_mtime; 11832878dffcSChristian Brauner inode->i_ctime = realinode->i_ctime; 11842878dffcSChristian Brauner i_size_write(inode, i_size_read(realinode)); 11852878dffcSChristian Brauner } 1186