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); 2285522c9c7SAmir Goldstein struct ovl_path *lowerstack = ovl_lowerstack(oe); 2294f93b426SVivek Goyal 2305522c9c7SAmir Goldstein if (ovl_numlower(oe)) { 2315522c9c7SAmir Goldstein path->mnt = lowerstack[ovl_numlower(oe) - 1].layer->mnt; 2325522c9c7SAmir Goldstein path->dentry = lowerstack[ovl_numlower(oe) - 1].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 { 291a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 292647d253fSVivek Goyal 2935522c9c7SAmir Goldstein return ovl_numlower(oe) ? 2945522c9c7SAmir Goldstein ovl_lowerstack(oe)[ovl_numlower(oe) - 1].dentry : NULL; 295647d253fSVivek Goyal } 296647d253fSVivek Goyal 297bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry) 298bbb1e54dSMiklos Szeredi { 29909d8b586SMiklos Szeredi return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 300bbb1e54dSMiklos Szeredi } 301bbb1e54dSMiklos Szeredi 3021d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode) 3031d88f183SMiklos Szeredi { 3041d88f183SMiklos Szeredi return ovl_upperdentry_dereference(OVL_I(inode)); 3051d88f183SMiklos Szeredi } 3061d88f183SMiklos Szeredi 307b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path) 308ffa5723cSAmir Goldstein { 309*ac900ed4SAmir Goldstein struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 310*ac900ed4SAmir Goldstein 311ffa5723cSAmir Goldstein path->dentry = ovl_i_dentry_upper(inode); 312ffa5723cSAmir Goldstein if (!path->dentry) { 313*ac900ed4SAmir Goldstein path->dentry = lowerpath->dentry; 314*ac900ed4SAmir Goldstein path->mnt = lowerpath->layer->mnt; 315ffa5723cSAmir Goldstein } else { 316ffa5723cSAmir Goldstein path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb)); 317ffa5723cSAmir Goldstein } 318b2dd05f1SZhihao Cheng 319b2dd05f1SZhihao Cheng return path->dentry ? d_inode_rcu(path->dentry) : NULL; 320ffa5723cSAmir Goldstein } 321ffa5723cSAmir Goldstein 32209d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode) 32325b7713aSMiklos Szeredi { 3241d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 32525b7713aSMiklos Szeredi 32609d8b586SMiklos Szeredi return upperdentry ? d_inode(upperdentry) : NULL; 32725b7713aSMiklos Szeredi } 32825b7713aSMiklos Szeredi 32909d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode) 33009d8b586SMiklos Szeredi { 331*ac900ed4SAmir Goldstein struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 332ffa5723cSAmir Goldstein 333*ac900ed4SAmir Goldstein return lowerpath ? d_inode(lowerpath->dentry) : NULL; 33409d8b586SMiklos Szeredi } 33509d8b586SMiklos Szeredi 33609d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode) 33709d8b586SMiklos Szeredi { 33809d8b586SMiklos Szeredi return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 33909d8b586SMiklos Szeredi } 34009d8b586SMiklos Szeredi 3412664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */ 3422664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode) 3432664bd08SVivek Goyal { 3442664bd08SVivek Goyal if (WARN_ON(!S_ISREG(inode->i_mode))) 3452664bd08SVivek Goyal return NULL; 3462664bd08SVivek Goyal 3472664bd08SVivek Goyal return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); 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 3624edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 363bbb1e54dSMiklos Szeredi { 3644edb83bbSMiklos Szeredi return OVL_I(inode)->cache; 365bbb1e54dSMiklos Szeredi } 366bbb1e54dSMiklos Szeredi 3674edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 368bbb1e54dSMiklos Szeredi { 3694edb83bbSMiklos Szeredi OVL_I(inode)->cache = cache; 370bbb1e54dSMiklos Szeredi } 371bbb1e54dSMiklos Szeredi 372c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 373c62520a8SAmir Goldstein { 374a6ff2bc0SAmir Goldstein set_bit(flag, OVL_E_FLAGS(dentry)); 375c62520a8SAmir Goldstein } 376c62520a8SAmir Goldstein 377c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 378c62520a8SAmir Goldstein { 379a6ff2bc0SAmir Goldstein clear_bit(flag, OVL_E_FLAGS(dentry)); 380c62520a8SAmir Goldstein } 381c62520a8SAmir Goldstein 382c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 383c62520a8SAmir Goldstein { 384a6ff2bc0SAmir Goldstein return test_bit(flag, OVL_E_FLAGS(dentry)); 385c62520a8SAmir Goldstein } 386c62520a8SAmir Goldstein 387bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry) 388bbb1e54dSMiklos Szeredi { 389c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 390bbb1e54dSMiklos Szeredi } 391bbb1e54dSMiklos Szeredi 392bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry) 393bbb1e54dSMiklos Szeredi { 394bbb1e54dSMiklos Szeredi return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 395bbb1e54dSMiklos Szeredi } 396bbb1e54dSMiklos Szeredi 3975cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry) 398bbb1e54dSMiklos Szeredi { 399c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 400bbb1e54dSMiklos Szeredi } 401bbb1e54dSMiklos Szeredi 40255acc661SMiklos Szeredi /* 403aa3ff3c1SAmir Goldstein * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 404aa3ff3c1SAmir Goldstein * to return positive, while there's no actual upper alias for the inode. 405aa3ff3c1SAmir Goldstein * Copy up code needs to know about the existence of the upper alias, so it 406aa3ff3c1SAmir Goldstein * can't use ovl_dentry_upper(). 40755acc661SMiklos Szeredi */ 40855acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry) 40955acc661SMiklos Szeredi { 410c62520a8SAmir Goldstein return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 41155acc661SMiklos Szeredi } 41255acc661SMiklos Szeredi 41355acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry) 41455acc661SMiklos Szeredi { 415c62520a8SAmir Goldstein ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 41655acc661SMiklos Szeredi } 41755acc661SMiklos Szeredi 4180c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode) 4190c288874SVivek Goyal { 4200c288874SVivek Goyal if (!S_ISREG(inode->i_mode)) 4210c288874SVivek Goyal return false; 4220c288874SVivek Goyal 4230c288874SVivek Goyal if (!ovl_inode_lower(inode)) 4240c288874SVivek Goyal return false; 4250c288874SVivek Goyal 4260c288874SVivek Goyal return true; 4270c288874SVivek Goyal } 4280c288874SVivek Goyal 4290c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode) 4300c288874SVivek Goyal { 4310c288874SVivek Goyal if (!ovl_should_check_upperdata(inode)) 4320c288874SVivek Goyal return true; 4330c288874SVivek Goyal 4340c288874SVivek Goyal if (!ovl_test_flag(OVL_UPPERDATA, inode)) 4350c288874SVivek Goyal return false; 4360c288874SVivek Goyal /* 4370c288874SVivek Goyal * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 4380c288874SVivek Goyal * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 4390c288874SVivek Goyal * if setting of OVL_UPPERDATA is visible, then effects of writes 4400c288874SVivek Goyal * before that are visible too. 4410c288874SVivek Goyal */ 4420c288874SVivek Goyal smp_rmb(); 4430c288874SVivek Goyal return true; 4440c288874SVivek Goyal } 4450c288874SVivek Goyal 4460c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode) 4470c288874SVivek Goyal { 4480c288874SVivek Goyal /* 4490c288874SVivek Goyal * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 4500c288874SVivek Goyal * if OVL_UPPERDATA flag is visible, then effects of write operations 4510c288874SVivek Goyal * before it are visible as well. 4520c288874SVivek Goyal */ 4530c288874SVivek Goyal smp_wmb(); 4540c288874SVivek Goyal ovl_set_flag(OVL_UPPERDATA, inode); 4550c288874SVivek Goyal } 4560c288874SVivek Goyal 4570c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 4580c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 4590c288874SVivek Goyal { 4600c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 4610c288874SVivek Goyal return false; 4620c288874SVivek Goyal 4630c288874SVivek Goyal return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 4640c288874SVivek Goyal } 4650c288874SVivek Goyal 4660c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 4670c288874SVivek Goyal { 4680c288874SVivek Goyal if (!ovl_open_flags_need_copy_up(flags)) 4690c288874SVivek Goyal return false; 4700c288874SVivek Goyal 4710c288874SVivek Goyal return !ovl_has_upperdata(d_inode(dentry)); 4720c288874SVivek Goyal } 4730c288874SVivek Goyal 474a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb) 475a6c60655SMiklos Szeredi { 476a6c60655SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 477a6c60655SMiklos Szeredi 47821a22878SAmir Goldstein return ofs->config.redirect_dir && !ofs->noxattr; 479a6c60655SMiklos Szeredi } 480a6c60655SMiklos Szeredi 481a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry) 482a6c60655SMiklos Szeredi { 483cf31c463SMiklos Szeredi return OVL_I(d_inode(dentry))->redirect; 484a6c60655SMiklos Szeredi } 485a6c60655SMiklos Szeredi 486a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 487a6c60655SMiklos Szeredi { 488cf31c463SMiklos Szeredi struct ovl_inode *oi = OVL_I(d_inode(dentry)); 489a6c60655SMiklos Szeredi 490cf31c463SMiklos Szeredi kfree(oi->redirect); 491cf31c463SMiklos Szeredi oi->redirect = redirect; 492a6c60655SMiklos Szeredi } 493a6c60655SMiklos Szeredi 49409d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 495bbb1e54dSMiklos Szeredi { 49609d8b586SMiklos Szeredi struct inode *upperinode = d_inode(upperdentry); 497e6d2ebddSMiklos Szeredi 49809d8b586SMiklos Szeredi WARN_ON(OVL_I(inode)->__upperdentry); 49909d8b586SMiklos Szeredi 50025b7713aSMiklos Szeredi /* 50109d8b586SMiklos Szeredi * Make sure upperdentry is consistent before making it visible 50225b7713aSMiklos Szeredi */ 50325b7713aSMiklos Szeredi smp_wmb(); 50409d8b586SMiklos Szeredi OVL_I(inode)->__upperdentry = upperdentry; 50531747edaSAmir Goldstein if (inode_unhashed(inode)) { 50625b7713aSMiklos Szeredi inode->i_private = upperinode; 507bbb1e54dSMiklos Szeredi __insert_inode_hash(inode, (unsigned long) upperinode); 508bbb1e54dSMiklos Szeredi } 50925b7713aSMiklos Szeredi } 510bbb1e54dSMiklos Szeredi 51165cd913eSAmir Goldstein static void ovl_dir_version_inc(struct dentry *dentry, bool impurity) 512bbb1e54dSMiklos Szeredi { 51304a01ac7SMiklos Szeredi struct inode *inode = d_inode(dentry); 514bbb1e54dSMiklos Szeredi 51504a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 51665cd913eSAmir Goldstein WARN_ON(!d_is_dir(dentry)); 5174edb83bbSMiklos Szeredi /* 51865cd913eSAmir Goldstein * Version is used by readdir code to keep cache consistent. 51965cd913eSAmir Goldstein * For merge dirs (or dirs with origin) all changes need to be noted. 52065cd913eSAmir Goldstein * For non-merge dirs, cache contains only impure entries (i.e. ones 52165cd913eSAmir Goldstein * which have been copied up and have origins), so only need to note 52265cd913eSAmir Goldstein * changes to impure entries. 5234edb83bbSMiklos Szeredi */ 5241fa9c5c5SMiklos Szeredi if (!ovl_dir_is_real(inode) || impurity) 52504a01ac7SMiklos Szeredi OVL_I(inode)->version++; 526bbb1e54dSMiklos Szeredi } 527bbb1e54dSMiklos Szeredi 528d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity) 529d9854c87SMiklos Szeredi { 530d9854c87SMiklos Szeredi /* Copy mtime/ctime */ 5312878dffcSChristian Brauner ovl_copyattr(d_inode(dentry)); 532d9854c87SMiklos Szeredi 53365cd913eSAmir Goldstein ovl_dir_version_inc(dentry, impurity); 534d9854c87SMiklos Szeredi } 535d9854c87SMiklos Szeredi 5361fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode) 537bbb1e54dSMiklos Szeredi { 53804a01ac7SMiklos Szeredi WARN_ON(!inode_is_locked(inode)); 53904a01ac7SMiklos Szeredi return OVL_I(inode)->version; 540bbb1e54dSMiklos Szeredi } 541bbb1e54dSMiklos Szeredi 542bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry) 543bbb1e54dSMiklos Szeredi { 544bbb1e54dSMiklos Szeredi struct inode *inode = dentry->d_inode; 545bbb1e54dSMiklos Szeredi 546bbb1e54dSMiklos Szeredi return inode && IS_WHITEOUT(inode); 547bbb1e54dSMiklos Szeredi } 548bbb1e54dSMiklos Szeredi 5492d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags) 550bbb1e54dSMiklos Szeredi { 55156230d95SMiklos Szeredi struct inode *inode = d_inode(path->dentry); 5524609e1f1SChristian Brauner struct mnt_idmap *real_idmap = mnt_idmap(path->mnt); 55356230d95SMiklos Szeredi int err, acc_mode; 55456230d95SMiklos Szeredi 55556230d95SMiklos Szeredi if (flags & ~(O_ACCMODE | O_LARGEFILE)) 55656230d95SMiklos Szeredi BUG(); 55756230d95SMiklos Szeredi 55856230d95SMiklos Szeredi switch (flags & O_ACCMODE) { 55956230d95SMiklos Szeredi case O_RDONLY: 56056230d95SMiklos Szeredi acc_mode = MAY_READ; 56156230d95SMiklos Szeredi break; 56256230d95SMiklos Szeredi case O_WRONLY: 56356230d95SMiklos Szeredi acc_mode = MAY_WRITE; 56456230d95SMiklos Szeredi break; 56556230d95SMiklos Szeredi default: 56656230d95SMiklos Szeredi BUG(); 56756230d95SMiklos Szeredi } 56856230d95SMiklos Szeredi 5694609e1f1SChristian Brauner err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN); 57056230d95SMiklos Szeredi if (err) 57156230d95SMiklos Szeredi return ERR_PTR(err); 57256230d95SMiklos Szeredi 57356230d95SMiklos Szeredi /* O_NOATIME is an optimization, don't fail if not permitted */ 57401beba79SChristian Brauner if (inode_owner_or_capable(real_idmap, inode)) 57556230d95SMiklos Szeredi flags |= O_NOATIME; 57656230d95SMiklos Szeredi 57756230d95SMiklos Szeredi return dentry_open(path, flags, current_cred()); 578bbb1e54dSMiklos Szeredi } 57939d3d60aSAmir Goldstein 5800c288874SVivek Goyal /* Caller should hold ovl_inode->lock */ 5810c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 5820c288874SVivek Goyal { 5830c288874SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5840c288874SVivek Goyal 5850c288874SVivek Goyal if (ovl_dentry_upper(dentry) && 5860c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 5870c288874SVivek Goyal !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 5880c288874SVivek Goyal return true; 5890c288874SVivek Goyal 5900c288874SVivek Goyal return false; 5910c288874SVivek Goyal } 5920c288874SVivek Goyal 5930c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags) 5942002df85SVivek Goyal { 5952002df85SVivek Goyal bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 5962002df85SVivek Goyal 5972002df85SVivek Goyal /* 5982002df85SVivek Goyal * Check if copy-up has happened as well as for upper alias (in 5992002df85SVivek Goyal * case of hard links) is there. 6002002df85SVivek Goyal * 6012002df85SVivek Goyal * Both checks are lockless: 6022002df85SVivek Goyal * - false negatives: will recheck under oi->lock 6032002df85SVivek Goyal * - false positives: 6042002df85SVivek Goyal * + ovl_dentry_upper() uses memory barriers to ensure the 6052002df85SVivek Goyal * upper dentry is up-to-date 6062002df85SVivek Goyal * + ovl_dentry_has_upper_alias() relies on locking of 6072002df85SVivek Goyal * upper parent i_rwsem to prevent reordering copy-up 6082002df85SVivek Goyal * with rename. 6092002df85SVivek Goyal */ 6102002df85SVivek Goyal if (ovl_dentry_upper(dentry) && 6110c288874SVivek Goyal (ovl_dentry_has_upper_alias(dentry) || disconnected) && 6120c288874SVivek Goyal !ovl_dentry_needs_data_copy_up(dentry, flags)) 6132002df85SVivek Goyal return true; 6142002df85SVivek Goyal 6152002df85SVivek Goyal return false; 6162002df85SVivek Goyal } 6172002df85SVivek Goyal 6180c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags) 61939d3d60aSAmir Goldstein { 6201e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 62139d3d60aSAmir Goldstein int err; 62239d3d60aSAmir Goldstein 623531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 6240c288874SVivek Goyal if (!err && ovl_already_copied_up_locked(dentry, flags)) { 62539d3d60aSAmir Goldstein err = 1; /* Already copied up */ 6261e92e307SAmir Goldstein ovl_inode_unlock(inode); 62739d3d60aSAmir Goldstein } 62839d3d60aSAmir Goldstein 62939d3d60aSAmir Goldstein return err; 63039d3d60aSAmir Goldstein } 63139d3d60aSAmir Goldstein 63239d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry) 63339d3d60aSAmir Goldstein { 6341e92e307SAmir Goldstein ovl_inode_unlock(d_inode(dentry)); 63539d3d60aSAmir Goldstein } 63682b749b2SAmir Goldstein 6372d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path) 638b79e05aaSAmir Goldstein { 639b79e05aaSAmir Goldstein int res; 640b79e05aaSAmir Goldstein 641dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0); 642b79e05aaSAmir Goldstein 643b79e05aaSAmir Goldstein /* Zero size value means "copied up but origin unknown" */ 644b79e05aaSAmir Goldstein if (res >= 0) 645b79e05aaSAmir Goldstein return true; 646b79e05aaSAmir Goldstein 647b79e05aaSAmir Goldstein return false; 648b79e05aaSAmir Goldstein } 649b79e05aaSAmir Goldstein 6502d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path, 65143d193f8SMiklos Szeredi enum ovl_xattr ox) 652f3a15685SAmir Goldstein { 653f3a15685SAmir Goldstein int res; 654f3a15685SAmir Goldstein char val; 655f3a15685SAmir Goldstein 656dad7017aSChristian Brauner if (!d_is_dir(path->dentry)) 657f3a15685SAmir Goldstein return false; 658f3a15685SAmir Goldstein 659dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, ox, &val, 1); 660f3a15685SAmir Goldstein if (res == 1 && val == 'y') 661f3a15685SAmir Goldstein return true; 662f3a15685SAmir Goldstein 663f3a15685SAmir Goldstein return false; 664f3a15685SAmir Goldstein } 665f3a15685SAmir Goldstein 66643d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX "opaque" 66743d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX "redirect" 66843d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX "origin" 66943d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX "impure" 67043d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX "nlink" 67143d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX "upper" 67243d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX "metacopy" 673096a218aSAmir Goldstein #define OVL_XATTR_PROTATTR_POSTFIX "protattr" 67443d193f8SMiklos Szeredi 67543d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \ 6762d2f2d73SMiklos Szeredi [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \ 6772d2f2d73SMiklos Szeredi [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX } 67843d193f8SMiklos Szeredi 6792d2f2d73SMiklos Szeredi const char *const ovl_xattr_table[][2] = { 68043d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE), 68143d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT), 68243d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN), 68343d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE), 68443d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK), 68543d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER), 68643d193f8SMiklos Szeredi OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY), 687096a218aSAmir Goldstein OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR), 68843d193f8SMiklos Szeredi }; 68943d193f8SMiklos Szeredi 690a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry, 69143d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, size_t size, 69282b749b2SAmir Goldstein int xerr) 69382b749b2SAmir Goldstein { 69482b749b2SAmir Goldstein int err; 69582b749b2SAmir Goldstein 69682b749b2SAmir Goldstein if (ofs->noxattr) 69782b749b2SAmir Goldstein return xerr; 69882b749b2SAmir Goldstein 699c914c0e2SAmir Goldstein err = ovl_setxattr(ofs, upperdentry, ox, value, size); 70082b749b2SAmir Goldstein 70182b749b2SAmir Goldstein if (err == -EOPNOTSUPP) { 70243d193f8SMiklos Szeredi pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox)); 70382b749b2SAmir Goldstein ofs->noxattr = true; 70482b749b2SAmir Goldstein return xerr; 70582b749b2SAmir Goldstein } 70682b749b2SAmir Goldstein 70782b749b2SAmir Goldstein return err; 70882b749b2SAmir Goldstein } 709f3a15685SAmir Goldstein 710f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 711f3a15685SAmir Goldstein { 712a0c236b1SAmir Goldstein struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 713f3a15685SAmir Goldstein int err; 714f3a15685SAmir Goldstein 71513c72075SMiklos Szeredi if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 716f3a15685SAmir Goldstein return 0; 717f3a15685SAmir Goldstein 718f3a15685SAmir Goldstein /* 719f3a15685SAmir Goldstein * Do not fail when upper doesn't support xattrs. 720f3a15685SAmir Goldstein * Upper inodes won't have origin nor redirect xattr anyway. 721f3a15685SAmir Goldstein */ 722a0c236b1SAmir Goldstein err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0); 723f3a15685SAmir Goldstein if (!err) 72413c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 725f3a15685SAmir Goldstein 726f3a15685SAmir Goldstein return err; 727f3a15685SAmir Goldstein } 72813c72075SMiklos Szeredi 729096a218aSAmir Goldstein 730096a218aSAmir Goldstein #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */ 731096a218aSAmir Goldstein 732096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper) 733096a218aSAmir Goldstein { 734096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 735096a218aSAmir Goldstein u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK; 736096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX+1]; 737096a218aSAmir Goldstein int res, n; 738096a218aSAmir Goldstein 739dad7017aSChristian Brauner res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf, 740096a218aSAmir Goldstein OVL_PROTATTR_MAX); 741096a218aSAmir Goldstein if (res < 0) 742096a218aSAmir Goldstein return; 743096a218aSAmir Goldstein 744096a218aSAmir Goldstein /* 745096a218aSAmir Goldstein * Initialize inode flags from overlay.protattr xattr and upper inode 746096a218aSAmir Goldstein * flags. If upper inode has those fileattr flags set (i.e. from old 747096a218aSAmir Goldstein * kernel), we do not clear them on ovl_get_inode(), but we will clear 748096a218aSAmir Goldstein * them on next fileattr_set(). 749096a218aSAmir Goldstein */ 750096a218aSAmir Goldstein for (n = 0; n < res; n++) { 751096a218aSAmir Goldstein if (buf[n] == 'a') 752096a218aSAmir Goldstein iflags |= S_APPEND; 753096a218aSAmir Goldstein else if (buf[n] == 'i') 754096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 755096a218aSAmir Goldstein else 756096a218aSAmir Goldstein break; 757096a218aSAmir Goldstein } 758096a218aSAmir Goldstein 759096a218aSAmir Goldstein if (!res || n < res) { 760096a218aSAmir Goldstein pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n", 761096a218aSAmir Goldstein upper, res); 762096a218aSAmir Goldstein } else { 763096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 764096a218aSAmir Goldstein } 765096a218aSAmir Goldstein } 766096a218aSAmir Goldstein 767096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper, 768096a218aSAmir Goldstein struct fileattr *fa) 769096a218aSAmir Goldstein { 770096a218aSAmir Goldstein struct ovl_fs *ofs = OVL_FS(inode->i_sb); 771096a218aSAmir Goldstein char buf[OVL_PROTATTR_MAX]; 772096a218aSAmir Goldstein int len = 0, err = 0; 773096a218aSAmir Goldstein u32 iflags = 0; 774096a218aSAmir Goldstein 775096a218aSAmir Goldstein BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX); 776096a218aSAmir Goldstein 777096a218aSAmir Goldstein if (fa->flags & FS_APPEND_FL) { 778096a218aSAmir Goldstein buf[len++] = 'a'; 779096a218aSAmir Goldstein iflags |= S_APPEND; 780096a218aSAmir Goldstein } 781096a218aSAmir Goldstein if (fa->flags & FS_IMMUTABLE_FL) { 782096a218aSAmir Goldstein buf[len++] = 'i'; 783096a218aSAmir Goldstein iflags |= S_IMMUTABLE; 784096a218aSAmir Goldstein } 785096a218aSAmir Goldstein 786096a218aSAmir Goldstein /* 787096a218aSAmir Goldstein * Do not allow to set protection flags when upper doesn't support 788096a218aSAmir Goldstein * xattrs, because we do not set those fileattr flags on upper inode. 789096a218aSAmir Goldstein * Remove xattr if it exist and all protection flags are cleared. 790096a218aSAmir Goldstein */ 791096a218aSAmir Goldstein if (len) { 792096a218aSAmir Goldstein err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR, 793096a218aSAmir Goldstein buf, len, -EPERM); 794096a218aSAmir Goldstein } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) { 795c914c0e2SAmir Goldstein err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR); 796096a218aSAmir Goldstein if (err == -EOPNOTSUPP || err == -ENODATA) 797096a218aSAmir Goldstein err = 0; 798096a218aSAmir Goldstein } 799096a218aSAmir Goldstein if (err) 800096a218aSAmir Goldstein return err; 801096a218aSAmir Goldstein 802096a218aSAmir Goldstein inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 803096a218aSAmir Goldstein 804096a218aSAmir Goldstein /* Mask out the fileattr flags that should not be set in upper inode */ 805096a218aSAmir Goldstein fa->flags &= ~OVL_PROT_FS_FLAGS_MASK; 806096a218aSAmir Goldstein fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK; 807096a218aSAmir Goldstein 808096a218aSAmir Goldstein return 0; 809096a218aSAmir Goldstein } 810096a218aSAmir Goldstein 811ad0af710SAmir Goldstein /** 812ad0af710SAmir Goldstein * Caller must hold a reference to inode to prevent it from being freed while 813ad0af710SAmir Goldstein * it is marked inuse. 814ad0af710SAmir Goldstein */ 815ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry) 816ad0af710SAmir Goldstein { 817ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 818ad0af710SAmir Goldstein bool locked = false; 819ad0af710SAmir Goldstein 820ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 821ad0af710SAmir Goldstein if (!(inode->i_state & I_OVL_INUSE)) { 822ad0af710SAmir Goldstein inode->i_state |= I_OVL_INUSE; 823ad0af710SAmir Goldstein locked = true; 824ad0af710SAmir Goldstein } 825ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 826ad0af710SAmir Goldstein 827ad0af710SAmir Goldstein return locked; 828ad0af710SAmir Goldstein } 829ad0af710SAmir Goldstein 830ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry) 831ad0af710SAmir Goldstein { 832ad0af710SAmir Goldstein if (dentry) { 833ad0af710SAmir Goldstein struct inode *inode = d_inode(dentry); 834ad0af710SAmir Goldstein 835ad0af710SAmir Goldstein spin_lock(&inode->i_lock); 836ad0af710SAmir Goldstein WARN_ON(!(inode->i_state & I_OVL_INUSE)); 837ad0af710SAmir Goldstein inode->i_state &= ~I_OVL_INUSE; 838ad0af710SAmir Goldstein spin_unlock(&inode->i_lock); 839ad0af710SAmir Goldstein } 840ad0af710SAmir Goldstein } 8415f8415d6SAmir Goldstein 842146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry) 843146d62e5SAmir Goldstein { 844146d62e5SAmir Goldstein struct inode *inode = d_inode(dentry); 845146d62e5SAmir Goldstein bool inuse; 846146d62e5SAmir Goldstein 847146d62e5SAmir Goldstein spin_lock(&inode->i_lock); 848146d62e5SAmir Goldstein inuse = (inode->i_state & I_OVL_INUSE); 849146d62e5SAmir Goldstein spin_unlock(&inode->i_lock); 850146d62e5SAmir Goldstein 851146d62e5SAmir Goldstein return inuse; 852146d62e5SAmir Goldstein } 853146d62e5SAmir Goldstein 85424b33ee1SAmir Goldstein /* 85524b33ee1SAmir Goldstein * Does this overlay dentry need to be indexed on copy up? 85624b33ee1SAmir Goldstein */ 85724b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry) 85824b33ee1SAmir Goldstein { 85924b33ee1SAmir Goldstein struct dentry *lower = ovl_dentry_lower(dentry); 86024b33ee1SAmir Goldstein 86124b33ee1SAmir Goldstein if (!lower || !ovl_indexdir(dentry->d_sb)) 86224b33ee1SAmir Goldstein return false; 86324b33ee1SAmir Goldstein 864fbd2d207SAmir Goldstein /* Index all files for NFS export and consistency verification */ 865016b720fSAmir Goldstein if (ovl_index_all(dentry->d_sb)) 866fbd2d207SAmir Goldstein return true; 867fbd2d207SAmir Goldstein 86824b33ee1SAmir Goldstein /* Index only lower hardlinks on copy up */ 86924b33ee1SAmir Goldstein if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 87024b33ee1SAmir Goldstein return true; 87124b33ee1SAmir Goldstein 87224b33ee1SAmir Goldstein return false; 87324b33ee1SAmir Goldstein } 87424b33ee1SAmir Goldstein 8759f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */ 876caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry) 877caf70cb2SAmir Goldstein { 8781cdb0cb6SPavel Tikhomirov struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 879e7dd0e71SAmir Goldstein struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 880e7dd0e71SAmir Goldstein struct inode *dir = indexdir->d_inode; 881caf70cb2SAmir Goldstein struct dentry *lowerdentry = ovl_dentry_lower(dentry); 882caf70cb2SAmir Goldstein struct dentry *upperdentry = ovl_dentry_upper(dentry); 883caf70cb2SAmir Goldstein struct dentry *index = NULL; 884caf70cb2SAmir Goldstein struct inode *inode; 88563e13252SAmir Goldstein struct qstr name = { }; 886caf70cb2SAmir Goldstein int err; 887caf70cb2SAmir Goldstein 8881cdb0cb6SPavel Tikhomirov err = ovl_get_index_name(ofs, lowerdentry, &name); 889caf70cb2SAmir Goldstein if (err) 890caf70cb2SAmir Goldstein goto fail; 891caf70cb2SAmir Goldstein 892caf70cb2SAmir Goldstein inode = d_inode(upperdentry); 89389a17556SAmir Goldstein if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 8941bd0a3aeSlijiazi pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 895caf70cb2SAmir Goldstein upperdentry, inode->i_ino, inode->i_nlink); 896caf70cb2SAmir Goldstein /* 897caf70cb2SAmir Goldstein * We either have a bug with persistent union nlink or a lower 898caf70cb2SAmir Goldstein * hardlink was added while overlay is mounted. Adding a lower 899caf70cb2SAmir Goldstein * hardlink and then unlinking all overlay hardlinks would drop 900caf70cb2SAmir Goldstein * overlay nlink to zero before all upper inodes are unlinked. 901caf70cb2SAmir Goldstein * As a safety measure, when that situation is detected, set 902caf70cb2SAmir Goldstein * the overlay nlink to the index inode nlink minus one for the 903caf70cb2SAmir Goldstein * index entry itself. 904caf70cb2SAmir Goldstein */ 905caf70cb2SAmir Goldstein set_nlink(d_inode(dentry), inode->i_nlink - 1); 906caf70cb2SAmir Goldstein ovl_set_nlink_upper(dentry); 907caf70cb2SAmir Goldstein goto out; 908caf70cb2SAmir Goldstein } 909caf70cb2SAmir Goldstein 910caf70cb2SAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 91122f289ceSChristian Brauner index = ovl_lookup_upper(ofs, name.name, indexdir, name.len); 912caf70cb2SAmir Goldstein err = PTR_ERR(index); 913e7dd0e71SAmir Goldstein if (IS_ERR(index)) { 9149f4ec904SAmir Goldstein index = NULL; 915e7dd0e71SAmir Goldstein } else if (ovl_index_all(dentry->d_sb)) { 916e7dd0e71SAmir Goldstein /* Whiteout orphan index to block future open by handle */ 917c21c839bSChengguang Xu err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), 918c21c839bSChengguang Xu dir, index); 919e7dd0e71SAmir Goldstein } else { 920e7dd0e71SAmir Goldstein /* Cleanup orphan index entries */ 921576bb263SChristian Brauner err = ovl_cleanup(ofs, dir, index); 922e7dd0e71SAmir Goldstein } 9239f4ec904SAmir Goldstein 924caf70cb2SAmir Goldstein inode_unlock(dir); 925caf70cb2SAmir Goldstein if (err) 926caf70cb2SAmir Goldstein goto fail; 927caf70cb2SAmir Goldstein 928caf70cb2SAmir Goldstein out: 92963e13252SAmir Goldstein kfree(name.name); 930caf70cb2SAmir Goldstein dput(index); 931caf70cb2SAmir Goldstein return; 932caf70cb2SAmir Goldstein 933caf70cb2SAmir Goldstein fail: 9341bd0a3aeSlijiazi pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); 935caf70cb2SAmir Goldstein goto out; 936caf70cb2SAmir Goldstein } 937caf70cb2SAmir Goldstein 9385f8415d6SAmir Goldstein /* 9395f8415d6SAmir Goldstein * Operations that change overlay inode and upper inode nlink need to be 9405f8415d6SAmir Goldstein * synchronized with copy up for persistent nlink accounting. 9415f8415d6SAmir Goldstein */ 9420e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry) 9435f8415d6SAmir Goldstein { 9441e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 9455f8415d6SAmir Goldstein const struct cred *old_cred; 9465f8415d6SAmir Goldstein int err; 9475f8415d6SAmir Goldstein 9481e92e307SAmir Goldstein if (WARN_ON(!inode)) 9490e32992fSAmir Goldstein return -ENOENT; 9505f8415d6SAmir Goldstein 9515f8415d6SAmir Goldstein /* 9525f8415d6SAmir Goldstein * With inodes index is enabled, we store the union overlay nlink 95324b33ee1SAmir Goldstein * in an xattr on the index inode. When whiting out an indexed lower, 9545f8415d6SAmir Goldstein * we need to decrement the overlay persistent nlink, but before the 9555f8415d6SAmir Goldstein * first copy up, we have no upper index inode to store the xattr. 9565f8415d6SAmir Goldstein * 95724b33ee1SAmir Goldstein * As a workaround, before whiteout/rename over an indexed lower, 9585f8415d6SAmir Goldstein * copy up to create the upper index. Creating the upper index will 9595f8415d6SAmir Goldstein * initialize the overlay nlink, so it could be dropped if unlink 9605f8415d6SAmir Goldstein * or rename succeeds. 9615f8415d6SAmir Goldstein * 9625f8415d6SAmir Goldstein * TODO: implement metadata only index copy up when called with 9635f8415d6SAmir Goldstein * ovl_copy_up_flags(dentry, O_PATH). 9645f8415d6SAmir Goldstein */ 96524b33ee1SAmir Goldstein if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 9665f8415d6SAmir Goldstein err = ovl_copy_up(dentry); 9675f8415d6SAmir Goldstein if (err) 9685f8415d6SAmir Goldstein return err; 9695f8415d6SAmir Goldstein } 9705f8415d6SAmir Goldstein 971531d3040SAmir Goldstein err = ovl_inode_lock_interruptible(inode); 9725f8415d6SAmir Goldstein if (err) 9735f8415d6SAmir Goldstein return err; 9745f8415d6SAmir Goldstein 9751e92e307SAmir Goldstein if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 9765f8415d6SAmir Goldstein goto out; 9775f8415d6SAmir Goldstein 9785f8415d6SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 9795f8415d6SAmir Goldstein /* 9805f8415d6SAmir Goldstein * The overlay inode nlink should be incremented/decremented IFF the 9815f8415d6SAmir Goldstein * upper operation succeeds, along with nlink change of upper inode. 9825f8415d6SAmir Goldstein * Therefore, before link/unlink/rename, we store the union nlink 9835f8415d6SAmir Goldstein * value relative to the upper inode nlink in an upper inode xattr. 9845f8415d6SAmir Goldstein */ 9855f8415d6SAmir Goldstein err = ovl_set_nlink_upper(dentry); 9865f8415d6SAmir Goldstein revert_creds(old_cred); 9875f8415d6SAmir Goldstein 9885f8415d6SAmir Goldstein out: 9895f8415d6SAmir Goldstein if (err) 9901e92e307SAmir Goldstein ovl_inode_unlock(inode); 9915f8415d6SAmir Goldstein 9925f8415d6SAmir Goldstein return err; 9935f8415d6SAmir Goldstein } 9945f8415d6SAmir Goldstein 9950e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry) 9965f8415d6SAmir Goldstein { 9971e92e307SAmir Goldstein struct inode *inode = d_inode(dentry); 9981e92e307SAmir Goldstein 9991e92e307SAmir Goldstein if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 1000caf70cb2SAmir Goldstein const struct cred *old_cred; 1001caf70cb2SAmir Goldstein 1002caf70cb2SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 1003caf70cb2SAmir Goldstein ovl_cleanup_index(dentry); 1004caf70cb2SAmir Goldstein revert_creds(old_cred); 1005caf70cb2SAmir Goldstein } 1006caf70cb2SAmir Goldstein 10071e92e307SAmir Goldstein ovl_inode_unlock(inode); 10085f8415d6SAmir Goldstein } 10095820dc08SAmir Goldstein 10105820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) 10115820dc08SAmir Goldstein { 10125820dc08SAmir Goldstein /* Workdir should not be the same as upperdir */ 10135820dc08SAmir Goldstein if (workdir == upperdir) 10145820dc08SAmir Goldstein goto err; 10155820dc08SAmir Goldstein 10165820dc08SAmir Goldstein /* Workdir should not be subdir of upperdir and vice versa */ 10175820dc08SAmir Goldstein if (lock_rename(workdir, upperdir) != NULL) 10185820dc08SAmir Goldstein goto err_unlock; 10195820dc08SAmir Goldstein 10205820dc08SAmir Goldstein return 0; 10215820dc08SAmir Goldstein 10225820dc08SAmir Goldstein err_unlock: 10235820dc08SAmir Goldstein unlock_rename(workdir, upperdir); 10245820dc08SAmir Goldstein err: 10251bd0a3aeSlijiazi pr_err("failed to lock workdir+upperdir\n"); 10265820dc08SAmir Goldstein return -EIO; 10275820dc08SAmir Goldstein } 10289d3dfea3SVivek Goyal 10299d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ 10302d343087SAl Viro int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path) 10319d3dfea3SVivek Goyal { 10329d3dfea3SVivek Goyal int res; 10339d3dfea3SVivek Goyal 10349d3dfea3SVivek Goyal /* Only regular files can have metacopy xattr */ 1035dad7017aSChristian Brauner if (!S_ISREG(d_inode(path->dentry)->i_mode)) 10369d3dfea3SVivek Goyal return 0; 10379d3dfea3SVivek Goyal 1038dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0); 10399d3dfea3SVivek Goyal if (res < 0) { 10409d3dfea3SVivek Goyal if (res == -ENODATA || res == -EOPNOTSUPP) 10419d3dfea3SVivek Goyal return 0; 104287b2c60cSMiklos Szeredi /* 104387b2c60cSMiklos Szeredi * getxattr on user.* may fail with EACCES in case there's no 104487b2c60cSMiklos Szeredi * read permission on the inode. Not much we can do, other than 104587b2c60cSMiklos Szeredi * tell the caller that this is not a metacopy inode. 104687b2c60cSMiklos Szeredi */ 104787b2c60cSMiklos Szeredi if (ofs->config.userxattr && res == -EACCES) 104887b2c60cSMiklos Szeredi return 0; 10499d3dfea3SVivek Goyal goto out; 10509d3dfea3SVivek Goyal } 10519d3dfea3SVivek Goyal 10529d3dfea3SVivek Goyal return 1; 10539d3dfea3SVivek Goyal out: 10541bd0a3aeSlijiazi pr_warn_ratelimited("failed to get metacopy (%i)\n", res); 10559d3dfea3SVivek Goyal return res; 10569d3dfea3SVivek Goyal } 105767d756c2SVivek Goyal 105867d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry) 105967d756c2SVivek Goyal { 1060a6ff2bc0SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 106167d756c2SVivek Goyal 106267d756c2SVivek Goyal if (!d_is_reg(dentry)) 106367d756c2SVivek Goyal return false; 106467d756c2SVivek Goyal 106567d756c2SVivek Goyal if (ovl_dentry_upper(dentry)) { 106667d756c2SVivek Goyal if (!ovl_has_upperdata(d_inode(dentry))) 106767d756c2SVivek Goyal return true; 106867d756c2SVivek Goyal return false; 106967d756c2SVivek Goyal } 107067d756c2SVivek Goyal 10715522c9c7SAmir Goldstein return (ovl_numlower(oe) > 1); 107267d756c2SVivek Goyal } 10730a2d0d3fSVivek Goyal 10742d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding) 10750a2d0d3fSVivek Goyal { 10760a2d0d3fSVivek Goyal int res; 10770a2d0d3fSVivek Goyal char *s, *next, *buf = NULL; 10780a2d0d3fSVivek Goyal 1079dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0); 108092f0d6c9SMiklos Szeredi if (res == -ENODATA || res == -EOPNOTSUPP) 10810a2d0d3fSVivek Goyal return NULL; 10820a2d0d3fSVivek Goyal if (res < 0) 108392f0d6c9SMiklos Szeredi goto fail; 108492f0d6c9SMiklos Szeredi if (res == 0) 108592f0d6c9SMiklos Szeredi goto invalid; 108692f0d6c9SMiklos Szeredi 108792f0d6c9SMiklos Szeredi buf = kzalloc(res + padding + 1, GFP_KERNEL); 108892f0d6c9SMiklos Szeredi if (!buf) 108992f0d6c9SMiklos Szeredi return ERR_PTR(-ENOMEM); 109092f0d6c9SMiklos Szeredi 1091dad7017aSChristian Brauner res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res); 109292f0d6c9SMiklos Szeredi if (res < 0) 109392f0d6c9SMiklos Szeredi goto fail; 10940a2d0d3fSVivek Goyal if (res == 0) 10950a2d0d3fSVivek Goyal goto invalid; 10960a2d0d3fSVivek Goyal 10970a2d0d3fSVivek Goyal if (buf[0] == '/') { 10980a2d0d3fSVivek Goyal for (s = buf; *s++ == '/'; s = next) { 10990a2d0d3fSVivek Goyal next = strchrnul(s, '/'); 11000a2d0d3fSVivek Goyal if (s == next) 11010a2d0d3fSVivek Goyal goto invalid; 11020a2d0d3fSVivek Goyal } 11030a2d0d3fSVivek Goyal } else { 11040a2d0d3fSVivek Goyal if (strchr(buf, '/') != NULL) 11050a2d0d3fSVivek Goyal goto invalid; 11060a2d0d3fSVivek Goyal } 11070a2d0d3fSVivek Goyal 11080a2d0d3fSVivek Goyal return buf; 11090a2d0d3fSVivek Goyal invalid: 11101bd0a3aeSlijiazi pr_warn_ratelimited("invalid redirect (%s)\n", buf); 11110a2d0d3fSVivek Goyal res = -EINVAL; 111292f0d6c9SMiklos Szeredi goto err_free; 111392f0d6c9SMiklos Szeredi fail: 111492f0d6c9SMiklos Szeredi pr_warn_ratelimited("failed to get redirect (%i)\n", res); 111592f0d6c9SMiklos Szeredi err_free: 1116993a0b2aSVivek Goyal kfree(buf); 1117993a0b2aSVivek Goyal return ERR_PTR(res); 11180a2d0d3fSVivek Goyal } 1119335d3fc5SSargun Dhillon 1120335d3fc5SSargun Dhillon /* 1121335d3fc5SSargun Dhillon * ovl_sync_status() - Check fs sync status for volatile mounts 1122335d3fc5SSargun Dhillon * 1123335d3fc5SSargun Dhillon * Returns 1 if this is not a volatile mount and a real sync is required. 1124335d3fc5SSargun Dhillon * 1125335d3fc5SSargun Dhillon * Returns 0 if syncing can be skipped because mount is volatile, and no errors 1126335d3fc5SSargun Dhillon * have occurred on the upperdir since the mount. 1127335d3fc5SSargun Dhillon * 1128335d3fc5SSargun Dhillon * Returns -errno if it is a volatile mount, and the error that occurred since 1129335d3fc5SSargun Dhillon * the last mount. If the error code changes, it'll return the latest error 1130335d3fc5SSargun Dhillon * code. 1131335d3fc5SSargun Dhillon */ 1132335d3fc5SSargun Dhillon 1133335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs) 1134335d3fc5SSargun Dhillon { 1135335d3fc5SSargun Dhillon struct vfsmount *mnt; 1136335d3fc5SSargun Dhillon 1137335d3fc5SSargun Dhillon if (ovl_should_sync(ofs)) 1138335d3fc5SSargun Dhillon return 1; 1139335d3fc5SSargun Dhillon 1140335d3fc5SSargun Dhillon mnt = ovl_upper_mnt(ofs); 1141335d3fc5SSargun Dhillon if (!mnt) 1142335d3fc5SSargun Dhillon return 0; 1143335d3fc5SSargun Dhillon 1144335d3fc5SSargun Dhillon return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq); 1145335d3fc5SSargun Dhillon } 11462878dffcSChristian Brauner 11472878dffcSChristian Brauner /* 11482878dffcSChristian Brauner * ovl_copyattr() - copy inode attributes from layer to ovl inode 11492878dffcSChristian Brauner * 11502878dffcSChristian Brauner * When overlay copies inode information from an upper or lower layer to the 11512878dffcSChristian Brauner * relevant overlay inode it will apply the idmapping of the upper or lower 11522878dffcSChristian Brauner * layer when doing so ensuring that the ovl inode ownership will correctly 11532878dffcSChristian Brauner * reflect the ownership of the idmapped upper or lower layer. For example, an 11542878dffcSChristian Brauner * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to 11552878dffcSChristian Brauner * map any lower or upper inode owned by id 1001 to id 1000. These mapping 11562878dffcSChristian Brauner * helpers are nops when the relevant layer isn't idmapped. 11572878dffcSChristian Brauner */ 11582878dffcSChristian Brauner void ovl_copyattr(struct inode *inode) 11592878dffcSChristian Brauner { 11602878dffcSChristian Brauner struct path realpath; 11612878dffcSChristian Brauner struct inode *realinode; 1162e67fe633SChristian Brauner struct mnt_idmap *real_idmap; 116373db6a06SChristian Brauner vfsuid_t vfsuid; 116473db6a06SChristian Brauner vfsgid_t vfsgid; 11652878dffcSChristian Brauner 1166b2dd05f1SZhihao Cheng realinode = ovl_i_path_real(inode, &realpath); 1167e67fe633SChristian Brauner real_idmap = mnt_idmap(realpath.mnt); 11682878dffcSChristian Brauner 1169e67fe633SChristian Brauner vfsuid = i_uid_into_vfsuid(real_idmap, realinode); 1170e67fe633SChristian Brauner vfsgid = i_gid_into_vfsgid(real_idmap, realinode); 117173db6a06SChristian Brauner 117273db6a06SChristian Brauner inode->i_uid = vfsuid_into_kuid(vfsuid); 117373db6a06SChristian Brauner inode->i_gid = vfsgid_into_kgid(vfsgid); 11742878dffcSChristian Brauner inode->i_mode = realinode->i_mode; 11752878dffcSChristian Brauner inode->i_atime = realinode->i_atime; 11762878dffcSChristian Brauner inode->i_mtime = realinode->i_mtime; 11772878dffcSChristian Brauner inode->i_ctime = realinode->i_ctime; 11782878dffcSChristian Brauner i_size_write(inode, i_size_read(realinode)); 11792878dffcSChristian Brauner } 1180