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); 228*ab1eb5ffSAmir Goldstein struct ovl_path *lowerdata = ovl_lowerdata(oe); 2294f93b426SVivek Goyal 2305522c9c7SAmir Goldstein if (ovl_numlower(oe)) { 231*ab1eb5ffSAmir Goldstein path->mnt = lowerdata->layer->mnt; 232*ab1eb5ffSAmir Goldstein path->dentry = lowerdata->dentry; 2334f93b426SVivek Goyal } else { 2344f93b426SVivek Goyal *path = (struct path) { }; 2354f93b426SVivek Goyal } 2364f93b426SVivek Goyal } 2374f93b426SVivek Goyal 238bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 239bbb1e54dSMiklos Szeredi { 240bbb1e54dSMiklos Szeredi enum ovl_path_type type = ovl_path_type(dentry); 241bbb1e54dSMiklos Szeredi 242bbb1e54dSMiklos Szeredi if (!OVL_TYPE_UPPER(type)) 243bbb1e54dSMiklos Szeredi ovl_path_lower(dentry, path); 244bbb1e54dSMiklos Szeredi else 245bbb1e54dSMiklos Szeredi ovl_path_upper(dentry, path); 246bbb1e54dSMiklos Szeredi 247bbb1e54dSMiklos Szeredi return type; 248bbb1e54dSMiklos Szeredi } 249bbb1e54dSMiklos Szeredi 2501248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path) 2511248ea4bSAmir Goldstein { 2521248ea4bSAmir Goldstein enum ovl_path_type type = ovl_path_type(dentry); 2531248ea4bSAmir Goldstein 2541248ea4bSAmir Goldstein WARN_ON_ONCE(d_is_dir(dentry)); 2551248ea4bSAmir Goldstein 2561248ea4bSAmir Goldstein if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type)) 2571248ea4bSAmir Goldstein ovl_path_lowerdata(dentry, path); 2581248ea4bSAmir Goldstein else 2591248ea4bSAmir Goldstein ovl_path_upper(dentry, path); 2601248ea4bSAmir Goldstein 2611248ea4bSAmir Goldstein return type; 2621248ea4bSAmir Goldstein } 2631248ea4bSAmir Goldstein 264bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry) 265bbb1e54dSMiklos Szeredi { 26609d8b586SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); 267bbb1e54dSMiklos Szeredi } 268bbb1e54dSMiklos Szeredi 269bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry) 270bbb1e54dSMiklos Szeredi { 271a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 272bbb1e54dSMiklos Szeredi 2735522c9c7SAmir Goldstein return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL; 274bbb1e54dSMiklos Szeredi } 275bbb1e54dSMiklos Szeredi 27613464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) 277da309e8cSAmir Goldstein { 278a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 279da309e8cSAmir Goldstein 2805522c9c7SAmir Goldstein return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL; 281da309e8cSAmir Goldstein } 282da309e8cSAmir Goldstein 283647d253fSVivek Goyal /* 284647d253fSVivek Goyal * ovl_dentry_lower() could return either a data dentry or metacopy dentry 285597534e7SXiong Zhenwu * depending on what is stored in lowerstack[0]. At times we need to find 286647d253fSVivek Goyal * lower dentry which has data (and not metacopy dentry). This helper 287647d253fSVivek Goyal * returns the lower data dentry. 288647d253fSVivek Goyal */ 289647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) 290647d253fSVivek Goyal { 291*ab1eb5ffSAmir Goldstein return ovl_lowerdata_dentry(OVL_E(dentry)); 292647d253fSVivek Goyal } 293647d253fSVivek Goyal 294bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry) 295bbb1e54dSMiklos Szeredi { 29609d8b586SMiklos Szeredi return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 297bbb1e54dSMiklos Szeredi } 298bbb1e54dSMiklos Szeredi 2991d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode) 3001d88f183SMiklos Szeredi { 3011d88f183SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(inode)); 3021d88f183SMiklos Szeredi } 3031d88f183SMiklos Szeredi 304b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path) 305ffa5723cSAmir Goldstein { 306ac900ed4SAmir Goldstein struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 307ac900ed4SAmir Goldstein 308ffa5723cSAmir Goldstein path->dentry = ovl_i_dentry_upper(inode); 309ffa5723cSAmir Goldstein if (!path->dentry) { 310ac900ed4SAmir Goldstein path->dentry = lowerpath->dentry; 311ac900ed4SAmir Goldstein path->mnt = lowerpath->layer->mnt; 312ffa5723cSAmir Goldstein } else { 313ffa5723cSAmir Goldstein path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb)); 314ffa5723cSAmir Goldstein } 315b2dd05f1SZhihao Cheng 316b2dd05f1SZhihao Cheng return path->dentry ? d_inode_rcu(path->dentry) : NULL; 317ffa5723cSAmir Goldstein } 318ffa5723cSAmir Goldstein 31909d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode) 32025b7713aSMiklos Szeredi { 3211d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 32225b7713aSMiklos Szeredi 32309d8b586SMiklos Szeredi return upperdentry ? d_inode(upperdentry) : NULL; 32425b7713aSMiklos Szeredi } 32525b7713aSMiklos Szeredi 32609d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode) 32709d8b586SMiklos Szeredi { 328ac900ed4SAmir Goldstein struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 329ffa5723cSAmir Goldstein 330ac900ed4SAmir Goldstein return lowerpath ? d_inode(lowerpath->dentry) : NULL; 33109d8b586SMiklos Szeredi } 33209d8b586SMiklos Szeredi 33309d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode) 33409d8b586SMiklos Szeredi { 33509d8b586SMiklos Szeredi return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 33609d8b586SMiklos Szeredi } 33709d8b586SMiklos Szeredi 3382664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */ 3392664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode) 3402664bd08SVivek Goyal { 341*ab1eb5ffSAmir Goldstein struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode)); 342*ab1eb5ffSAmir Goldstein 3432664bd08SVivek Goyal if (WARN_ON(!S_ISREG(inode->i_mode))) 3442664bd08SVivek Goyal return NULL; 3452664bd08SVivek Goyal 346*ab1eb5ffSAmir Goldstein return lowerdata ? d_inode(lowerdata) : NULL; 3472664bd08SVivek Goyal } 34809d8b586SMiklos Szeredi 3494823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */ 3504823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode) 3514823d49cSVivek Goyal { 3524823d49cSVivek Goyal struct inode *upperinode; 3534823d49cSVivek Goyal 3544823d49cSVivek Goyal upperinode = ovl_inode_upper(inode); 3554823d49cSVivek Goyal if (upperinode && ovl_has_upperdata(inode)) 3564823d49cSVivek Goyal return upperinode; 3574823d49cSVivek Goyal 3584823d49cSVivek Goyal return ovl_inode_lowerdata(inode); 3594823d49cSVivek Goyal } 3604823d49cSVivek Goyal 3614edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 362bbb1e54dSMiklos Szeredi { 3634edb83bbSMiklos Szeredi return OVL_I(inode)->cache; 364bbb1e54dSMiklos Szeredi } 365bbb1e54dSMiklos Szeredi 3664edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 367bbb1e54dSMiklos Szeredi { 3684edb83bbSMiklos Szeredi OVL_I(inode)->cache = cache; 369bbb1e54dSMiklos Szeredi } 370bbb1e54dSMiklos Szeredi 371c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 372c62520a8SAmir Goldstein { 373a6ff2bc0SAmir Goldstein set_bit(flag, OVL_E_FLAGS(dentry)); 374c62520a8SAmir Goldstein } 375c62520a8SAmir Goldstein 376c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 377c62520a8SAmir Goldstein { 378a6ff2bc0SAmir Goldstein clear_bit(flag, OVL_E_FLAGS(dentry)); 379c62520a8SAmir Goldstein } 380c62520a8SAmir Goldstein 381c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 382c62520a8SAmir Goldstein { 383a6ff2bc0SAmir Goldstein return test_bit(flag, OVL_E_FLAGS(dentry)); 384c62520a8SAmir Goldstein } 385c62520a8SAmir Goldstein 386bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry) 387bbb1e54dSMiklos Szeredi { 388c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 389bbb1e54dSMiklos Szeredi } 390bbb1e54dSMiklos Szeredi 391bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry) 392bbb1e54dSMiklos Szeredi { 393bbb1e54dSMiklos Szeredi return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 394bbb1e54dSMiklos Szeredi } 395bbb1e54dSMiklos Szeredi 3965cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry) 397bbb1e54dSMiklos Szeredi { 398c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 399bbb1e54dSMiklos Szeredi } 400bbb1e54dSMiklos Szeredi 40155acc661SMiklos Szeredi /* 402aa3ff3c1SAmir Goldstein * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 403aa3ff3c1SAmir Goldstein * to return positive, while there's no actual upper alias for the inode. 404aa3ff3c1SAmir Goldstein * Copy up code needs to know about the existence of the upper alias, so it 405aa3ff3c1SAmir Goldstein * can't use ovl_dentry_upper(). 40655acc661SMiklos Szeredi */ 40755acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry) 40855acc661SMiklos Szeredi { 409c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 41055acc661SMiklos Szeredi } 41155acc661SMiklos Szeredi 41255acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry) 41355acc661SMiklos Szeredi { 414c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 41555acc661SMiklos Szeredi } 41655acc661SMiklos Szeredi 4170c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode) 4180c288874SVivek Goyal { 4190c288874SVivek Goyal if (!S_ISREG(inode->i_mode)) 4200c288874SVivek Goyal return false; 4210c288874SVivek Goyal 4220c288874SVivek Goyal if (!ovl_inode_lower(inode)) 4230c288874SVivek Goyal return false; 4240c288874SVivek Goyal 4250c288874SVivek Goyal return true; 4260c288874SVivek Goyal } 4270c288874SVivek Goyal 4280c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode) 4290c288874SVivek Goyal { 4300c288874SVivek Goyal if (!ovl_should_check_upperdata(inode)) 4310c288874SVivek Goyal return true; 4320c288874SVivek Goyal 4330c288874SVivek Goyal if (!ovl_test_flag(OVL_UPPERDATA, inode)) 4340c288874SVivek Goyal return false; 4350c288874SVivek Goyal /* 4360c288874SVivek Goyal * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 4370c288874SVivek Goyal * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 4380c288874SVivek Goyal * if setting of OVL_UPPERDATA is visible, then effects of writes 4390c288874SVivek Goyal * before that are visible too. 4400c288874SVivek Goyal */ 4410c288874SVivek Goyal smp_rmb(); 4420c288874SVivek Goyal return true; 4430c288874SVivek Goyal } 4440c288874SVivek Goyal 4450c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode) 4460c288874SVivek Goyal { 4470c288874SVivek Goyal /* 4480c288874SVivek Goyal * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 4490c288874SVivek Goyal * if OVL_UPPERDATA flag is visible, then effects of write operations 4500c288874SVivek Goyal * before it are visible as well. 4510c288874SVivek Goyal */ 4520c288874SVivek Goyal smp_wmb(); 4530c288874SVivek Goyal ovl_set_flag(OVL_UPPERDATA, inode); 4540c288874SVivek Goyal } 4550c288874SVivek Goyal 4560c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 4570c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 4580c288874SVivek Goyal { 4590c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 4600c288874SVivek Goyal return false; 4610c288874SVivek Goyal 4620c288874SVivek Goyal return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 4630c288874SVivek Goyal } 4640c288874SVivek Goyal 4650c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 4660c288874SVivek Goyal { 4670c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 4680c288874SVivek Goyal return false; 4690c288874SVivek Goyal 4700c288874SVivek Goyal return !ovl_has_upperdata(d_inode(dentry)); 4710c288874SVivek Goyal } 4720c288874SVivek Goyal 473a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb) 474a6c60655SMiklos Szeredi { 475a6c60655SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 476a6c60655SMiklos Szeredi 47721a22878SAmir Goldstein return ofs->config.redirect_dir && !ofs->noxattr; 478a6c60655SMiklos Szeredi } 479a6c60655SMiklos Szeredi 480a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry) 481a6c60655SMiklos Szeredi { 482cf31c463SMiklos Szeredi return OVL_I(d_inode(dentry))->redirect; 483a6c60655SMiklos Szeredi } 484a6c60655SMiklos Szeredi 485a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 486a6c60655SMiklos Szeredi { 487cf31c463SMiklos Szeredi struct ovl_inode *oi = OVL_I(d_inode(dentry)); 488a6c60655SMiklos Szeredi 489cf31c463SMiklos Szeredi kfree(oi->redirect); 490cf31c463SMiklos Szeredi oi->redirect = redirect; 491a6c60655SMiklos Szeredi } 492a6c60655SMiklos Szeredi 49309d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 494bbb1e54dSMiklos Szeredi { 49509d8b586SMiklos Szeredi struct inode *upperinode = d_inode(upperdentry); 496e6d2ebddSMiklos Szeredi 49709d8b586SMiklos Szeredi WARN_ON(OVL_I(inode)->__upperdentry); 49809d8b586SMiklos Szeredi 49925b7713aSMiklos Szeredi /* 50009d8b586SMiklos Szeredi * Make sure upperdentry is consistent before making it visible 50125b7713aSMiklos Szeredi */ 50225b7713aSMiklos Szeredi smp_wmb(); 50309d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 50431747edaSAmir Goldstein if (inode_unhashed(inode)) { 50525b7713aSMiklos Szeredi inode->i_private = upperinode; 506bbb1e54dSMiklos Szeredi __insert_inode_hash(inode, (unsigned long) upperinode); 507bbb1e54dSMiklos Szeredi } 50825b7713aSMiklos Szeredi } 509bbb1e54dSMiklos Szeredi 51065cd913eSAmir Goldstein static void ovl_dir_version_inc(struct dentry *dentry, bool impurity) 511bbb1e54dSMiklos Szeredi { 51204a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 513bbb1e54dSMiklos Szeredi 51404a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 51565cd913eSAmir Goldstein WARN_ON(!d_is_dir(dentry)); 5164edb83bbSMiklos Szeredi /* 51765cd913eSAmir Goldstein * Version is used by readdir code to keep cache consistent. 51865cd913eSAmir Goldstein * For merge dirs (or dirs with origin) all changes need to be noted. 51965cd913eSAmir Goldstein * For non-merge dirs, cache contains only impure entries (i.e. ones 52065cd913eSAmir Goldstein * which have been copied up and have origins), so only need to note 52165cd913eSAmir Goldstein * changes to impure entries. 5224edb83bbSMiklos Szeredi */ 5231fa9c5c5SMiklos Szeredi if (!ovl_dir_is_real(inode) || impurity) 52404a01ac7SMiklos Szeredi OVL_I(inode)->version++; 525bbb1e54dSMiklos Szeredi } 526bbb1e54dSMiklos Szeredi 527d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity) 528d9854c87SMiklos Szeredi { 529d9854c87SMiklos Szeredi /* Copy mtime/ctime */ 5302878dffcSChristian Brauner ovl_copyattr(d_inode(dentry)); 531d9854c87SMiklos Szeredi 53265cd913eSAmir Goldstein ovl_dir_version_inc(dentry, impurity); 533d9854c87SMiklos Szeredi } 534d9854c87SMiklos Szeredi 5351fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode) 536bbb1e54dSMiklos Szeredi { 53704a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 53804a01ac7SMiklos Szeredi return OVL_I(inode)->version; 539bbb1e54dSMiklos Szeredi } 540bbb1e54dSMiklos Szeredi 541bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry) 542bbb1e54dSMiklos Szeredi { 543bbb1e54dSMiklos Szeredi struct inode *inode = dentry->d_inode; 544bbb1e54dSMiklos Szeredi 545bbb1e54dSMiklos Szeredi return inode && IS_WHITEOUT(inode); 546bbb1e54dSMiklos Szeredi } 547bbb1e54dSMiklos Szeredi 5482d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags) 549bbb1e54dSMiklos Szeredi { 55056230d95SMiklos Szeredi struct inode *inode = d_inode(path->dentry); 5514609e1f1SChristian Brauner struct mnt_idmap *real_idmap = mnt_idmap(path->mnt); 55256230d95SMiklos Szeredi int err, acc_mode; 55356230d95SMiklos Szeredi 55456230d95SMiklos Szeredi if (flags & ~(O_ACCMODE | O_LARGEFILE)) 55556230d95SMiklos Szeredi BUG(); 55656230d95SMiklos Szeredi 55756230d95SMiklos Szeredi switch (flags & O_ACCMODE) { 55856230d95SMiklos Szeredi case O_RDONLY: 55956230d95SMiklos Szeredi acc_mode = MAY_READ; 56056230d95SMiklos Szeredi break; 56156230d95SMiklos Szeredi case O_WRONLY: 56256230d95SMiklos Szeredi acc_mode = MAY_WRITE; 56356230d95SMiklos Szeredi break; 56456230d95SMiklos Szeredi default: 56556230d95SMiklos Szeredi BUG(); 56656230d95SMiklos Szeredi } 56756230d95SMiklos Szeredi 5684609e1f1SChristian Brauner err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN); 56956230d95SMiklos Szeredi if (err) 57056230d95SMiklos Szeredi return ERR_PTR(err); 57156230d95SMiklos Szeredi 57256230d95SMiklos Szeredi /* O_NOATIME is an optimization, don't fail if not permitted */ 57301beba79SChristian Brauner if (inode_owner_or_capable(real_idmap, inode)) 57456230d95SMiklos Szeredi flags |= O_NOATIME; 57556230d95SMiklos Szeredi 57656230d95SMiklos Szeredi return dentry_open(path, flags, current_cred()); 577bbb1e54dSMiklos Szeredi } 57839d3d60aSAmir Goldstein 5790c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 5800c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 5810c288874SVivek Goyal { 5820c288874SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5830c288874SVivek Goyal 5840c288874SVivek Goyal if (ovl_dentry_upper(dentry) && 5850c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 5860c288874SVivek Goyal !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 5870c288874SVivek Goyal return true; 5880c288874SVivek Goyal 5890c288874SVivek Goyal return false; 5900c288874SVivek Goyal } 5910c288874SVivek Goyal 5920c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags) 5932002df85SVivek Goyal { 5942002df85SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5952002df85SVivek Goyal 5962002df85SVivek Goyal /* 5972002df85SVivek Goyal * Check if copy-up has happened as well as for upper alias (in 5982002df85SVivek Goyal * case of hard links) is there. 5992002df85SVivek Goyal * 6002002df85SVivek Goyal * Both checks are lockless: 6012002df85SVivek Goyal * - false negatives: will recheck under oi->lock 6022002df85SVivek Goyal * - false positives: 6032002df85SVivek Goyal * + ovl_dentry_upper() uses memory barriers to ensure the 6042002df85SVivek Goyal * upper dentry is up-to-date 6052002df85SVivek Goyal * + ovl_dentry_has_upper_alias() relies on locking of 6062002df85SVivek Goyal * upper parent i_rwsem to prevent reordering copy-up 6072002df85SVivek Goyal * with rename. 6082002df85SVivek Goyal */ 6092002df85SVivek Goyal if (ovl_dentry_upper(dentry) && 6100c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 6110c288874SVivek Goyal !ovl_dentry_needs_data_copy_up(dentry, flags)) 6122002df85SVivek Goyal return true; 6132002df85SVivek Goyal 6142002df85SVivek Goyal return false; 6152002df85SVivek Goyal } 6162002df85SVivek Goyal 6170c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags) 61839d3d60aSAmir Goldstein { 6191e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 62039d3d60aSAmir Goldstein int err; 62139d3d60aSAmir Goldstein 622531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 6230c288874SVivek Goyal if (!err && ovl_already_copied_up_locked(dentry, flags)) { 62439d3d60aSAmir Goldstein err = 1; /* Already copied up */ 6251e92e307SAmir Goldstein ovl_inode_unlock(inode); 62639d3d60aSAmir Goldstein } 62739d3d60aSAmir Goldstein 62839d3d60aSAmir Goldstein return err; 62939d3d60aSAmir Goldstein } 63039d3d60aSAmir Goldstein 63139d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry) 63239d3d60aSAmir Goldstein { 6331e92e307SAmir Goldstein ovl_inode_unlock(d_inode(dentry)); 63439d3d60aSAmir Goldstein } 63582b749b2SAmir Goldstein 6362d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path) 637b79e05aaSAmir Goldstein { 638b79e05aaSAmir Goldstein int res; 639b79e05aaSAmir Goldstein 640dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0); 641b79e05aaSAmir Goldstein 642b79e05aaSAmir Goldstein /* Zero size value means "copied up but origin unknown" */ 643b79e05aaSAmir Goldstein if (res >= 0) 644b79e05aaSAmir Goldstein return true; 645b79e05aaSAmir Goldstein 646b79e05aaSAmir Goldstein return false; 647b79e05aaSAmir Goldstein } 648b79e05aaSAmir Goldstein 6492d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path, 65043d193f8SMiklos Szeredi enum ovl_xattr ox) 651f3a15685SAmir Goldstein { 652f3a15685SAmir Goldstein int res; 653f3a15685SAmir Goldstein char val; 654f3a15685SAmir Goldstein 655dad7017aSChristian Brauner if (!d_is_dir(path->dentry)) 656f3a15685SAmir Goldstein return false; 657f3a15685SAmir Goldstein 658dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, ox, &val, 1); 659f3a15685SAmir Goldstein if (res == 1 && val == 'y') 660f3a15685SAmir Goldstein return true; 661f3a15685SAmir Goldstein 662f3a15685SAmir Goldstein return false; 663f3a15685SAmir Goldstein } 664f3a15685SAmir Goldstein 66543d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX "opaque" 66643d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX "redirect" 66743d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX "origin" 66843d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX "impure" 66943d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX "nlink" 67043d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX "upper" 67143d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX "metacopy" 672096a218aSAmir Goldstein #define OVL_XATTR_PROTATTR_POSTFIX "protattr" 67343d193f8SMiklos Szeredi 67443d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \ 6752d2f2d73SMiklos Szeredi [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \ 6762d2f2d73SMiklos Szeredi [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX } 67743d193f8SMiklos Szeredi 6782d2f2d73SMiklos Szeredi const char *const ovl_xattr_table[][2] = { 67943d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE), 68043d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT), 68143d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN), 68243d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE), 68343d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK), 68443d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER), 68543d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY), 686096a218aSAmir Goldstein OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR), 68743d193f8SMiklos Szeredi }; 68843d193f8SMiklos Szeredi 689a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry, 69043d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, size_t size, 69182b749b2SAmir Goldstein int xerr) 69282b749b2SAmir Goldstein { 69382b749b2SAmir Goldstein int err; 69482b749b2SAmir Goldstein 69582b749b2SAmir Goldstein if (ofs->noxattr) 69682b749b2SAmir Goldstein return xerr; 69782b749b2SAmir Goldstein 698c914c0e2SAmir Goldstein err = ovl_setxattr(ofs, upperdentry, ox, value, size); 69982b749b2SAmir Goldstein 70082b749b2SAmir Goldstein if (err == -EOPNOTSUPP) { 70143d193f8SMiklos Szeredi pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox)); 70282b749b2SAmir Goldstein ofs->noxattr = true; 70382b749b2SAmir Goldstein return xerr; 70482b749b2SAmir Goldstein } 70582b749b2SAmir Goldstein 70682b749b2SAmir Goldstein return err; 70782b749b2SAmir Goldstein } 708f3a15685SAmir Goldstein 709f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 710f3a15685SAmir Goldstein { 711a0c236b1SAmir Goldstein struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 712f3a15685SAmir Goldstein int err; 713f3a15685SAmir Goldstein 71413c72075SMiklos Szeredi if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 715f3a15685SAmir Goldstein return 0; 716f3a15685SAmir Goldstein 717f3a15685SAmir Goldstein /* 718f3a15685SAmir Goldstein * Do not fail when upper doesn't support xattrs. 719f3a15685SAmir Goldstein * Upper inodes won't have origin nor redirect xattr anyway. 720f3a15685SAmir Goldstein */ 721a0c236b1SAmir Goldstein err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0); 722f3a15685SAmir Goldstein if (!err) 72313c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 724f3a15685SAmir Goldstein 725f3a15685SAmir Goldstein return err; 726f3a15685SAmir Goldstein } 72713c72075SMiklos Szeredi 728096a218aSAmir Goldstein 729096a218aSAmir Goldstein #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */ 730096a218aSAmir Goldstein 731096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper) 732096a218aSAmir Goldstein { 733096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 734096a218aSAmir Goldstein u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK; 735096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX+1]; 736096a218aSAmir Goldstein int res, n; 737096a218aSAmir Goldstein 738dad7017aSChristian Brauner res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf, 739096a218aSAmir Goldstein OVL_PROTATTR_MAX); 740096a218aSAmir Goldstein if (res < 0) 741096a218aSAmir Goldstein return; 742096a218aSAmir Goldstein 743096a218aSAmir Goldstein /* 744096a218aSAmir Goldstein * Initialize inode flags from overlay.protattr xattr and upper inode 745096a218aSAmir Goldstein * flags. If upper inode has those fileattr flags set (i.e. from old 746096a218aSAmir Goldstein * kernel), we do not clear them on ovl_get_inode(), but we will clear 747096a218aSAmir Goldstein * them on next fileattr_set(). 748096a218aSAmir Goldstein */ 749096a218aSAmir Goldstein for (n = 0; n < res; n++) { 750096a218aSAmir Goldstein if (buf[n] == 'a') 751096a218aSAmir Goldstein iflags |= S_APPEND; 752096a218aSAmir Goldstein else if (buf[n] == 'i') 753096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 754096a218aSAmir Goldstein else 755096a218aSAmir Goldstein break; 756096a218aSAmir Goldstein } 757096a218aSAmir Goldstein 758096a218aSAmir Goldstein if (!res || n < res) { 759096a218aSAmir Goldstein pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n", 760096a218aSAmir Goldstein upper, res); 761096a218aSAmir Goldstein } else { 762096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 763096a218aSAmir Goldstein } 764096a218aSAmir Goldstein } 765096a218aSAmir Goldstein 766096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper, 767096a218aSAmir Goldstein struct fileattr *fa) 768096a218aSAmir Goldstein { 769096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 770096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX]; 771096a218aSAmir Goldstein int len = 0, err = 0; 772096a218aSAmir Goldstein u32 iflags = 0; 773096a218aSAmir Goldstein 774096a218aSAmir Goldstein BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX); 775096a218aSAmir Goldstein 776096a218aSAmir Goldstein if (fa->flags & FS_APPEND_FL) { 777096a218aSAmir Goldstein buf[len++] = 'a'; 778096a218aSAmir Goldstein iflags |= S_APPEND; 779096a218aSAmir Goldstein } 780096a218aSAmir Goldstein if (fa->flags & FS_IMMUTABLE_FL) { 781096a218aSAmir Goldstein buf[len++] = 'i'; 782096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 783096a218aSAmir Goldstein } 784096a218aSAmir Goldstein 785096a218aSAmir Goldstein /* 786096a218aSAmir Goldstein * Do not allow to set protection flags when upper doesn't support 787096a218aSAmir Goldstein * xattrs, because we do not set those fileattr flags on upper inode. 788096a218aSAmir Goldstein * Remove xattr if it exist and all protection flags are cleared. 789096a218aSAmir Goldstein */ 790096a218aSAmir Goldstein if (len) { 791096a218aSAmir Goldstein err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR, 792096a218aSAmir Goldstein buf, len, -EPERM); 793096a218aSAmir Goldstein } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) { 794c914c0e2SAmir Goldstein err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR); 795096a218aSAmir Goldstein if (err == -EOPNOTSUPP || err == -ENODATA) 796096a218aSAmir Goldstein err = 0; 797096a218aSAmir Goldstein } 798096a218aSAmir Goldstein if (err) 799096a218aSAmir Goldstein return err; 800096a218aSAmir Goldstein 801096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 802096a218aSAmir Goldstein 803096a218aSAmir Goldstein /* Mask out the fileattr flags that should not be set in upper inode */ 804096a218aSAmir Goldstein fa->flags &= ~OVL_PROT_FS_FLAGS_MASK; 805096a218aSAmir Goldstein fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK; 806096a218aSAmir Goldstein 807096a218aSAmir Goldstein return 0; 808096a218aSAmir Goldstein } 809096a218aSAmir Goldstein 810ad0af710SAmir Goldstein /** 811ad0af710SAmir Goldstein * Caller must hold a reference to inode to prevent it from being freed while 812ad0af710SAmir Goldstein * it is marked inuse. 813ad0af710SAmir Goldstein */ 814ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry) 815ad0af710SAmir Goldstein { 816ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 817ad0af710SAmir Goldstein bool locked = false; 818ad0af710SAmir Goldstein 819ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 820ad0af710SAmir Goldstein if (!(inode->i_state & I_OVL_INUSE)) { 821ad0af710SAmir Goldstein inode->i_state |= I_OVL_INUSE; 822ad0af710SAmir Goldstein locked = true; 823ad0af710SAmir Goldstein } 824ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 825ad0af710SAmir Goldstein 826ad0af710SAmir Goldstein return locked; 827ad0af710SAmir Goldstein } 828ad0af710SAmir Goldstein 829ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry) 830ad0af710SAmir Goldstein { 831ad0af710SAmir Goldstein if (dentry) { 832ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 833ad0af710SAmir Goldstein 834ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 835ad0af710SAmir Goldstein WARN_ON(!(inode->i_state & I_OVL_INUSE)); 836ad0af710SAmir Goldstein inode->i_state &= ~I_OVL_INUSE; 837ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 838ad0af710SAmir Goldstein } 839ad0af710SAmir Goldstein } 8405f8415d6SAmir Goldstein 841146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry) 842146d62e5SAmir Goldstein { 843146d62e5SAmir Goldstein struct inode *inode = d_inode(dentry); 844146d62e5SAmir Goldstein bool inuse; 845146d62e5SAmir Goldstein 846146d62e5SAmir Goldstein spin_lock(&inode->i_lock); 847146d62e5SAmir Goldstein inuse = (inode->i_state & I_OVL_INUSE); 848146d62e5SAmir Goldstein spin_unlock(&inode->i_lock); 849146d62e5SAmir Goldstein 850146d62e5SAmir Goldstein return inuse; 851146d62e5SAmir Goldstein } 852146d62e5SAmir Goldstein 85324b33ee1SAmir Goldstein /* 85424b33ee1SAmir Goldstein * Does this overlay dentry need to be indexed on copy up? 85524b33ee1SAmir Goldstein */ 85624b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry) 85724b33ee1SAmir Goldstein { 85824b33ee1SAmir Goldstein struct dentry *lower = ovl_dentry_lower(dentry); 85924b33ee1SAmir Goldstein 86024b33ee1SAmir Goldstein if (!lower || !ovl_indexdir(dentry->d_sb)) 86124b33ee1SAmir Goldstein return false; 86224b33ee1SAmir Goldstein 863fbd2d207SAmir Goldstein /* Index all files for NFS export and consistency verification */ 864016b720fSAmir Goldstein if (ovl_index_all(dentry->d_sb)) 865fbd2d207SAmir Goldstein return true; 866fbd2d207SAmir Goldstein 86724b33ee1SAmir Goldstein /* Index only lower hardlinks on copy up */ 86824b33ee1SAmir Goldstein if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 86924b33ee1SAmir Goldstein return true; 87024b33ee1SAmir Goldstein 87124b33ee1SAmir Goldstein return false; 87224b33ee1SAmir Goldstein } 87324b33ee1SAmir Goldstein 8749f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */ 875caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry) 876caf70cb2SAmir Goldstein { 8771cdb0cb6SPavel Tikhomirov struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 878e7dd0e71SAmir Goldstein struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 879e7dd0e71SAmir Goldstein struct inode *dir = indexdir->d_inode; 880caf70cb2SAmir Goldstein struct dentry *lowerdentry = ovl_dentry_lower(dentry); 881caf70cb2SAmir Goldstein struct dentry *upperdentry = ovl_dentry_upper(dentry); 882caf70cb2SAmir Goldstein struct dentry *index = NULL; 883caf70cb2SAmir Goldstein struct inode *inode; 88463e13252SAmir Goldstein struct qstr name = { }; 885caf70cb2SAmir Goldstein int err; 886caf70cb2SAmir Goldstein 8871cdb0cb6SPavel Tikhomirov err = ovl_get_index_name(ofs, lowerdentry, &name); 888caf70cb2SAmir Goldstein if (err) 889caf70cb2SAmir Goldstein goto fail; 890caf70cb2SAmir Goldstein 891caf70cb2SAmir Goldstein inode = d_inode(upperdentry); 89289a17556SAmir Goldstein if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 8931bd0a3aeSlijiazi pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 894caf70cb2SAmir Goldstein upperdentry, inode->i_ino, inode->i_nlink); 895caf70cb2SAmir Goldstein /* 896caf70cb2SAmir Goldstein * We either have a bug with persistent union nlink or a lower 897caf70cb2SAmir Goldstein * hardlink was added while overlay is mounted. Adding a lower 898caf70cb2SAmir Goldstein * hardlink and then unlinking all overlay hardlinks would drop 899caf70cb2SAmir Goldstein * overlay nlink to zero before all upper inodes are unlinked. 900caf70cb2SAmir Goldstein * As a safety measure, when that situation is detected, set 901caf70cb2SAmir Goldstein * the overlay nlink to the index inode nlink minus one for the 902caf70cb2SAmir Goldstein * index entry itself. 903caf70cb2SAmir Goldstein */ 904caf70cb2SAmir Goldstein set_nlink(d_inode(dentry), inode->i_nlink - 1); 905caf70cb2SAmir Goldstein ovl_set_nlink_upper(dentry); 906caf70cb2SAmir Goldstein goto out; 907caf70cb2SAmir Goldstein } 908caf70cb2SAmir Goldstein 909caf70cb2SAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 91022f289ceSChristian Brauner index = ovl_lookup_upper(ofs, name.name, indexdir, name.len); 911caf70cb2SAmir Goldstein err = PTR_ERR(index); 912e7dd0e71SAmir Goldstein if (IS_ERR(index)) { 9139f4ec904SAmir Goldstein index = NULL; 914e7dd0e71SAmir Goldstein } else if (ovl_index_all(dentry->d_sb)) { 915e7dd0e71SAmir Goldstein /* Whiteout orphan index to block future open by handle */ 916c21c839bSChengguang Xu err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), 917c21c839bSChengguang Xu dir, index); 918e7dd0e71SAmir Goldstein } else { 919e7dd0e71SAmir Goldstein /* Cleanup orphan index entries */ 920576bb263SChristian Brauner err = ovl_cleanup(ofs, dir, index); 921e7dd0e71SAmir Goldstein } 9229f4ec904SAmir Goldstein 923caf70cb2SAmir Goldstein inode_unlock(dir); 924caf70cb2SAmir Goldstein if (err) 925caf70cb2SAmir Goldstein goto fail; 926caf70cb2SAmir Goldstein 927caf70cb2SAmir Goldstein out: 92863e13252SAmir Goldstein kfree(name.name); 929caf70cb2SAmir Goldstein dput(index); 930caf70cb2SAmir Goldstein return; 931caf70cb2SAmir Goldstein 932caf70cb2SAmir Goldstein fail: 9331bd0a3aeSlijiazi pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); 934caf70cb2SAmir Goldstein goto out; 935caf70cb2SAmir Goldstein } 936caf70cb2SAmir Goldstein 9375f8415d6SAmir Goldstein /* 9385f8415d6SAmir Goldstein * Operations that change overlay inode and upper inode nlink need to be 9395f8415d6SAmir Goldstein * synchronized with copy up for persistent nlink accounting. 9405f8415d6SAmir Goldstein */ 9410e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry) 9425f8415d6SAmir Goldstein { 9431e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 9445f8415d6SAmir Goldstein const struct cred *old_cred; 9455f8415d6SAmir Goldstein int err; 9465f8415d6SAmir Goldstein 9471e92e307SAmir Goldstein if (WARN_ON(!inode)) 9480e32992fSAmir Goldstein return -ENOENT; 9495f8415d6SAmir Goldstein 9505f8415d6SAmir Goldstein /* 9515f8415d6SAmir Goldstein * With inodes index is enabled, we store the union overlay nlink 95224b33ee1SAmir Goldstein * in an xattr on the index inode. When whiting out an indexed lower, 9535f8415d6SAmir Goldstein * we need to decrement the overlay persistent nlink, but before the 9545f8415d6SAmir Goldstein * first copy up, we have no upper index inode to store the xattr. 9555f8415d6SAmir Goldstein * 95624b33ee1SAmir Goldstein * As a workaround, before whiteout/rename over an indexed lower, 9575f8415d6SAmir Goldstein * copy up to create the upper index. Creating the upper index will 9585f8415d6SAmir Goldstein * initialize the overlay nlink, so it could be dropped if unlink 9595f8415d6SAmir Goldstein * or rename succeeds. 9605f8415d6SAmir Goldstein * 9615f8415d6SAmir Goldstein * TODO: implement metadata only index copy up when called with 9625f8415d6SAmir Goldstein * ovl_copy_up_flags(dentry, O_PATH). 9635f8415d6SAmir Goldstein */ 96424b33ee1SAmir Goldstein if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 9655f8415d6SAmir Goldstein err = ovl_copy_up(dentry); 9665f8415d6SAmir Goldstein if (err) 9675f8415d6SAmir Goldstein return err; 9685f8415d6SAmir Goldstein } 9695f8415d6SAmir Goldstein 970531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 9715f8415d6SAmir Goldstein if (err) 9725f8415d6SAmir Goldstein return err; 9735f8415d6SAmir Goldstein 9741e92e307SAmir Goldstein if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 9755f8415d6SAmir Goldstein goto out; 9765f8415d6SAmir Goldstein 9775f8415d6SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 9785f8415d6SAmir Goldstein /* 9795f8415d6SAmir Goldstein * The overlay inode nlink should be incremented/decremented IFF the 9805f8415d6SAmir Goldstein * upper operation succeeds, along with nlink change of upper inode. 9815f8415d6SAmir Goldstein * Therefore, before link/unlink/rename, we store the union nlink 9825f8415d6SAmir Goldstein * value relative to the upper inode nlink in an upper inode xattr. 9835f8415d6SAmir Goldstein */ 9845f8415d6SAmir Goldstein err = ovl_set_nlink_upper(dentry); 9855f8415d6SAmir Goldstein revert_creds(old_cred); 9865f8415d6SAmir Goldstein 9875f8415d6SAmir Goldstein out: 9885f8415d6SAmir Goldstein if (err) 9891e92e307SAmir Goldstein ovl_inode_unlock(inode); 9905f8415d6SAmir Goldstein 9915f8415d6SAmir Goldstein return err; 9925f8415d6SAmir Goldstein } 9935f8415d6SAmir Goldstein 9940e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry) 9955f8415d6SAmir Goldstein { 9961e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 9971e92e307SAmir Goldstein 9981e92e307SAmir Goldstein if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 999caf70cb2SAmir Goldstein const struct cred *old_cred; 1000caf70cb2SAmir Goldstein 1001caf70cb2SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 1002caf70cb2SAmir Goldstein ovl_cleanup_index(dentry); 1003caf70cb2SAmir Goldstein revert_creds(old_cred); 1004caf70cb2SAmir Goldstein } 1005caf70cb2SAmir Goldstein 10061e92e307SAmir Goldstein ovl_inode_unlock(inode); 10075f8415d6SAmir Goldstein } 10085820dc08SAmir Goldstein 10095820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) 10105820dc08SAmir Goldstein { 10115820dc08SAmir Goldstein /* Workdir should not be the same as upperdir */ 10125820dc08SAmir Goldstein if (workdir == upperdir) 10135820dc08SAmir Goldstein goto err; 10145820dc08SAmir Goldstein 10155820dc08SAmir Goldstein /* Workdir should not be subdir of upperdir and vice versa */ 10165820dc08SAmir Goldstein if (lock_rename(workdir, upperdir) != NULL) 10175820dc08SAmir Goldstein goto err_unlock; 10185820dc08SAmir Goldstein 10195820dc08SAmir Goldstein return 0; 10205820dc08SAmir Goldstein 10215820dc08SAmir Goldstein err_unlock: 10225820dc08SAmir Goldstein unlock_rename(workdir, upperdir); 10235820dc08SAmir Goldstein err: 10241bd0a3aeSlijiazi pr_err("failed to lock workdir+upperdir\n"); 10255820dc08SAmir Goldstein return -EIO; 10265820dc08SAmir Goldstein } 10279d3dfea3SVivek Goyal 10289d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ 10292d343087SAl Viro int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path) 10309d3dfea3SVivek Goyal { 10319d3dfea3SVivek Goyal int res; 10329d3dfea3SVivek Goyal 10339d3dfea3SVivek Goyal /* Only regular files can have metacopy xattr */ 1034dad7017aSChristian Brauner if (!S_ISREG(d_inode(path->dentry)->i_mode)) 10359d3dfea3SVivek Goyal return 0; 10369d3dfea3SVivek Goyal 1037dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0); 10389d3dfea3SVivek Goyal if (res < 0) { 10399d3dfea3SVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 10409d3dfea3SVivek Goyal return 0; 104187b2c60cSMiklos Szeredi /* 104287b2c60cSMiklos Szeredi * getxattr on user.* may fail with EACCES in case there's no 104387b2c60cSMiklos Szeredi * read permission on the inode. Not much we can do, other than 104487b2c60cSMiklos Szeredi * tell the caller that this is not a metacopy inode. 104587b2c60cSMiklos Szeredi */ 104687b2c60cSMiklos Szeredi if (ofs->config.userxattr && res == -EACCES) 104787b2c60cSMiklos Szeredi return 0; 10489d3dfea3SVivek Goyal goto out; 10499d3dfea3SVivek Goyal } 10509d3dfea3SVivek Goyal 10519d3dfea3SVivek Goyal return 1; 10529d3dfea3SVivek Goyal out: 10531bd0a3aeSlijiazi pr_warn_ratelimited("failed to get metacopy (%i)\n", res); 10549d3dfea3SVivek Goyal return res; 10559d3dfea3SVivek Goyal } 105667d756c2SVivek Goyal 105767d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry) 105867d756c2SVivek Goyal { 1059a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 106067d756c2SVivek Goyal 106167d756c2SVivek Goyal if (!d_is_reg(dentry)) 106267d756c2SVivek Goyal return false; 106367d756c2SVivek Goyal 106467d756c2SVivek Goyal if (ovl_dentry_upper(dentry)) { 106567d756c2SVivek Goyal if (!ovl_has_upperdata(d_inode(dentry))) 106667d756c2SVivek Goyal return true; 106767d756c2SVivek Goyal return false; 106867d756c2SVivek Goyal } 106967d756c2SVivek Goyal 10705522c9c7SAmir Goldstein return (ovl_numlower(oe) > 1); 107167d756c2SVivek Goyal } 10720a2d0d3fSVivek Goyal 10732d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding) 10740a2d0d3fSVivek Goyal { 10750a2d0d3fSVivek Goyal int res; 10760a2d0d3fSVivek Goyal char *s, *next, *buf = NULL; 10770a2d0d3fSVivek Goyal 1078dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0); 107992f0d6c9SMiklos Szeredi if (res == -ENODATA || res == -EOPNOTSUPP) 10800a2d0d3fSVivek Goyal return NULL; 10810a2d0d3fSVivek Goyal if (res < 0) 108292f0d6c9SMiklos Szeredi goto fail; 108392f0d6c9SMiklos Szeredi if (res == 0) 108492f0d6c9SMiklos Szeredi goto invalid; 108592f0d6c9SMiklos Szeredi 108692f0d6c9SMiklos Szeredi buf = kzalloc(res + padding + 1, GFP_KERNEL); 108792f0d6c9SMiklos Szeredi if (!buf) 108892f0d6c9SMiklos Szeredi return ERR_PTR(-ENOMEM); 108992f0d6c9SMiklos Szeredi 1090dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res); 109192f0d6c9SMiklos Szeredi if (res < 0) 109292f0d6c9SMiklos Szeredi goto fail; 10930a2d0d3fSVivek Goyal if (res == 0) 10940a2d0d3fSVivek Goyal goto invalid; 10950a2d0d3fSVivek Goyal 10960a2d0d3fSVivek Goyal if (buf[0] == '/') { 10970a2d0d3fSVivek Goyal for (s = buf; *s++ == '/'; s = next) { 10980a2d0d3fSVivek Goyal next = strchrnul(s, '/'); 10990a2d0d3fSVivek Goyal if (s == next) 11000a2d0d3fSVivek Goyal goto invalid; 11010a2d0d3fSVivek Goyal } 11020a2d0d3fSVivek Goyal } else { 11030a2d0d3fSVivek Goyal if (strchr(buf, '/') != NULL) 11040a2d0d3fSVivek Goyal goto invalid; 11050a2d0d3fSVivek Goyal } 11060a2d0d3fSVivek Goyal 11070a2d0d3fSVivek Goyal return buf; 11080a2d0d3fSVivek Goyal invalid: 11091bd0a3aeSlijiazi pr_warn_ratelimited("invalid redirect (%s)\n", buf); 11100a2d0d3fSVivek Goyal res = -EINVAL; 111192f0d6c9SMiklos Szeredi goto err_free; 111292f0d6c9SMiklos Szeredi fail: 111392f0d6c9SMiklos Szeredi pr_warn_ratelimited("failed to get redirect (%i)\n", res); 111492f0d6c9SMiklos Szeredi err_free: 1115993a0b2aSVivek Goyal kfree(buf); 1116993a0b2aSVivek Goyal return ERR_PTR(res); 11170a2d0d3fSVivek Goyal } 1118335d3fc5SSargun Dhillon 1119335d3fc5SSargun Dhillon /* 1120335d3fc5SSargun Dhillon * ovl_sync_status() - Check fs sync status for volatile mounts 1121335d3fc5SSargun Dhillon * 1122335d3fc5SSargun Dhillon * Returns 1 if this is not a volatile mount and a real sync is required. 1123335d3fc5SSargun Dhillon * 1124335d3fc5SSargun Dhillon * Returns 0 if syncing can be skipped because mount is volatile, and no errors 1125335d3fc5SSargun Dhillon * have occurred on the upperdir since the mount. 1126335d3fc5SSargun Dhillon * 1127335d3fc5SSargun Dhillon * Returns -errno if it is a volatile mount, and the error that occurred since 1128335d3fc5SSargun Dhillon * the last mount. If the error code changes, it'll return the latest error 1129335d3fc5SSargun Dhillon * code. 1130335d3fc5SSargun Dhillon */ 1131335d3fc5SSargun Dhillon 1132335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs) 1133335d3fc5SSargun Dhillon { 1134335d3fc5SSargun Dhillon struct vfsmount *mnt; 1135335d3fc5SSargun Dhillon 1136335d3fc5SSargun Dhillon if (ovl_should_sync(ofs)) 1137335d3fc5SSargun Dhillon return 1; 1138335d3fc5SSargun Dhillon 1139335d3fc5SSargun Dhillon mnt = ovl_upper_mnt(ofs); 1140335d3fc5SSargun Dhillon if (!mnt) 1141335d3fc5SSargun Dhillon return 0; 1142335d3fc5SSargun Dhillon 1143335d3fc5SSargun Dhillon return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq); 1144335d3fc5SSargun Dhillon } 11452878dffcSChristian Brauner 11462878dffcSChristian Brauner /* 11472878dffcSChristian Brauner * ovl_copyattr() - copy inode attributes from layer to ovl inode 11482878dffcSChristian Brauner * 11492878dffcSChristian Brauner * When overlay copies inode information from an upper or lower layer to the 11502878dffcSChristian Brauner * relevant overlay inode it will apply the idmapping of the upper or lower 11512878dffcSChristian Brauner * layer when doing so ensuring that the ovl inode ownership will correctly 11522878dffcSChristian Brauner * reflect the ownership of the idmapped upper or lower layer. For example, an 11532878dffcSChristian Brauner * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to 11542878dffcSChristian Brauner * map any lower or upper inode owned by id 1001 to id 1000. These mapping 11552878dffcSChristian Brauner * helpers are nops when the relevant layer isn't idmapped. 11562878dffcSChristian Brauner */ 11572878dffcSChristian Brauner void ovl_copyattr(struct inode *inode) 11582878dffcSChristian Brauner { 11592878dffcSChristian Brauner struct path realpath; 11602878dffcSChristian Brauner struct inode *realinode; 1161e67fe633SChristian Brauner struct mnt_idmap *real_idmap; 116273db6a06SChristian Brauner vfsuid_t vfsuid; 116373db6a06SChristian Brauner vfsgid_t vfsgid; 11642878dffcSChristian Brauner 1165b2dd05f1SZhihao Cheng realinode = ovl_i_path_real(inode, &realpath); 1166e67fe633SChristian Brauner real_idmap = mnt_idmap(realpath.mnt); 11672878dffcSChristian Brauner 1168e67fe633SChristian Brauner vfsuid = i_uid_into_vfsuid(real_idmap, realinode); 1169e67fe633SChristian Brauner vfsgid = i_gid_into_vfsgid(real_idmap, realinode); 117073db6a06SChristian Brauner 117173db6a06SChristian Brauner inode->i_uid = vfsuid_into_kuid(vfsuid); 117273db6a06SChristian Brauner inode->i_gid = vfsgid_into_kgid(vfsgid); 11732878dffcSChristian Brauner inode->i_mode = realinode->i_mode; 11742878dffcSChristian Brauner inode->i_atime = realinode->i_atime; 11752878dffcSChristian Brauner inode->i_mtime = realinode->i_mtime; 11762878dffcSChristian Brauner inode->i_ctime = realinode->i_ctime; 11772878dffcSChristian Brauner i_size_write(inode, i_size_read(realinode)); 11782878dffcSChristian Brauner } 1179