1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e9be9d5eSMiklos Szeredi /* 3e9be9d5eSMiklos Szeredi * 4e9be9d5eSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 5e9be9d5eSMiklos Szeredi */ 6e9be9d5eSMiklos Szeredi 7e9be9d5eSMiklos Szeredi #include <linux/fs.h> 8e9be9d5eSMiklos Szeredi #include <linux/slab.h> 95b825c3aSIngo Molnar #include <linux/cred.h> 10e9be9d5eSMiklos Szeredi #include <linux/xattr.h> 115201dc44SMiklos Szeredi #include <linux/posix_acl.h> 125f8415d6SAmir Goldstein #include <linux/ratelimit.h> 1310c5db28SChristoph Hellwig #include <linux/fiemap.h> 1466dbfabfSMiklos Szeredi #include <linux/fileattr.h> 1566dbfabfSMiklos Szeredi #include <linux/security.h> 16332f606bSMiklos Szeredi #include <linux/namei.h> 176c0a8bfbSChristian Brauner #include <linux/posix_acl.h> 186c0a8bfbSChristian Brauner #include <linux/posix_acl_xattr.h> 19e9be9d5eSMiklos Szeredi #include "overlayfs.h" 20e9be9d5eSMiklos Szeredi 21ba1e563cSChandan Rajendra 22c1632a0fSChristian Brauner int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 23549c7297SChristian Brauner struct iattr *attr) 24e9be9d5eSMiklos Szeredi { 25e9be9d5eSMiklos Szeredi int err; 26a15506eaSChristian Brauner struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 27997336f2SVivek Goyal bool full_copy_up = false; 28e9be9d5eSMiklos Szeredi struct dentry *upperdentry; 291175b6b8SVivek Goyal const struct cred *old_cred; 30e9be9d5eSMiklos Szeredi 31c1632a0fSChristian Brauner err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 32cf9a6784SMiklos Szeredi if (err) 33cf9a6784SMiklos Szeredi return err; 34cf9a6784SMiklos Szeredi 35e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 36e9be9d5eSMiklos Szeredi if (err) 37e9be9d5eSMiklos Szeredi goto out; 38e9be9d5eSMiklos Szeredi 395812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 40997336f2SVivek Goyal /* Truncate should trigger data copy up as well */ 41997336f2SVivek Goyal full_copy_up = true; 425812160eSMiklos Szeredi } 435812160eSMiklos Szeredi 44997336f2SVivek Goyal if (!full_copy_up) 45acff81ecSMiklos Szeredi err = ovl_copy_up(dentry); 46997336f2SVivek Goyal else 47997336f2SVivek Goyal err = ovl_copy_up_with_data(dentry); 48acff81ecSMiklos Szeredi if (!err) { 495812160eSMiklos Szeredi struct inode *winode = NULL; 505812160eSMiklos Szeredi 51e9be9d5eSMiklos Szeredi upperdentry = ovl_dentry_upper(dentry); 52acff81ecSMiklos Szeredi 535812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 545812160eSMiklos Szeredi winode = d_inode(upperdentry); 555812160eSMiklos Szeredi err = get_write_access(winode); 565812160eSMiklos Szeredi if (err) 575812160eSMiklos Szeredi goto out_drop_write; 585812160eSMiklos Szeredi } 595812160eSMiklos Szeredi 60b99c2d91SMiklos Szeredi if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) 61b99c2d91SMiklos Szeredi attr->ia_valid &= ~ATTR_MODE; 62b99c2d91SMiklos Szeredi 63e67f0216SVivek Goyal /* 6415fd2ea9SVivek Goyal * We might have to translate ovl file into real file object 6515fd2ea9SVivek Goyal * once use cases emerge. For now, simply don't let underlying 6615fd2ea9SVivek Goyal * filesystem rely on attr->ia_file 67e67f0216SVivek Goyal */ 68e67f0216SVivek Goyal attr->ia_valid &= ~ATTR_FILE; 69e67f0216SVivek Goyal 7015fd2ea9SVivek Goyal /* 7115fd2ea9SVivek Goyal * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN 7215fd2ea9SVivek Goyal * set. Overlayfs does not pass O_TRUNC flag to underlying 7315fd2ea9SVivek Goyal * filesystem during open -> do not pass ATTR_OPEN. This 7415fd2ea9SVivek Goyal * disables optimization in fuse which assumes open(O_TRUNC) 7515fd2ea9SVivek Goyal * already set file size to 0. But we never passed O_TRUNC to 7615fd2ea9SVivek Goyal * fuse. So by clearing ATTR_OPEN, fuse will be forced to send 7715fd2ea9SVivek Goyal * setattr request to server. 7815fd2ea9SVivek Goyal */ 7915fd2ea9SVivek Goyal attr->ia_valid &= ~ATTR_OPEN; 8015fd2ea9SVivek Goyal 815955102cSAl Viro inode_lock(upperdentry->d_inode); 821175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 83a15506eaSChristian Brauner err = ovl_do_notify_change(ofs, upperdentry, attr); 841175b6b8SVivek Goyal revert_creds(old_cred); 85b81de061SKonstantin Khlebnikov if (!err) 862878dffcSChristian Brauner ovl_copyattr(dentry->d_inode); 875955102cSAl Viro inode_unlock(upperdentry->d_inode); 885812160eSMiklos Szeredi 895812160eSMiklos Szeredi if (winode) 905812160eSMiklos Szeredi put_write_access(winode); 91e9be9d5eSMiklos Szeredi } 925812160eSMiklos Szeredi out_drop_write: 93e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 94e9be9d5eSMiklos Szeredi out: 95e9be9d5eSMiklos Szeredi return err; 96e9be9d5eSMiklos Szeredi } 97e9be9d5eSMiklos Szeredi 98c68e7ec5Syoungjun static void ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid) 99da309e8cSAmir Goldstein { 1000f831ec8SAmir Goldstein bool samefs = ovl_same_fs(dentry->d_sb); 101e487d889SAmir Goldstein unsigned int xinobits = ovl_xino_bits(dentry->d_sb); 102dfe51d47SAmir Goldstein unsigned int xinoshift = 64 - xinobits; 103da309e8cSAmir Goldstein 104da309e8cSAmir Goldstein if (samefs) { 105da309e8cSAmir Goldstein /* 106da309e8cSAmir Goldstein * When all layers are on the same fs, all real inode 107da309e8cSAmir Goldstein * number are unique, so we use the overlay st_dev, 108da309e8cSAmir Goldstein * which is friendly to du -x. 109da309e8cSAmir Goldstein */ 110da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 111c68e7ec5Syoungjun return; 112e487d889SAmir Goldstein } else if (xinobits) { 113e487d889SAmir Goldstein /* 114e487d889SAmir Goldstein * All inode numbers of underlying fs should not be using the 115e487d889SAmir Goldstein * high xinobits, so we use high xinobits to partition the 116e487d889SAmir Goldstein * overlay st_ino address space. The high bits holds the fsid 117dfe51d47SAmir Goldstein * (upper fsid is 0). The lowest xinobit is reserved for mapping 118f48bbfb2SBhaskar Chowdhury * the non-persistent inode numbers range in case of overflow. 119dfe51d47SAmir Goldstein * This way all overlay inode numbers are unique and use the 120dfe51d47SAmir Goldstein * overlay st_dev. 121e487d889SAmir Goldstein */ 122926e94d7SAmir Goldstein if (likely(!(stat->ino >> xinoshift))) { 123dfe51d47SAmir Goldstein stat->ino |= ((u64)fsid) << (xinoshift + 1); 124e487d889SAmir Goldstein stat->dev = dentry->d_sb->s_dev; 125c68e7ec5Syoungjun return; 126926e94d7SAmir Goldstein } else if (ovl_xino_warn(dentry->d_sb)) { 127926e94d7SAmir Goldstein pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n", 128926e94d7SAmir Goldstein dentry, stat->ino, xinobits); 129e487d889SAmir Goldstein } 130e487d889SAmir Goldstein } 131e487d889SAmir Goldstein 132e487d889SAmir Goldstein /* The inode could not be mapped to a unified st_ino address space */ 133e487d889SAmir Goldstein if (S_ISDIR(dentry->d_inode->i_mode)) { 134da309e8cSAmir Goldstein /* 135da309e8cSAmir Goldstein * Always use the overlay st_dev for directories, so 'find 136da309e8cSAmir Goldstein * -xdev' will scan the entire overlay mount and won't cross the 137da309e8cSAmir Goldstein * overlay mount boundaries. 138da309e8cSAmir Goldstein * 139da309e8cSAmir Goldstein * If not all layers are on the same fs the pair {real st_ino; 140da309e8cSAmir Goldstein * overlay st_dev} is not unique, so use the non persistent 141da309e8cSAmir Goldstein * overlay st_ino for directories. 142da309e8cSAmir Goldstein */ 143da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 144da309e8cSAmir Goldstein stat->ino = dentry->d_inode->i_ino; 145b7bf9908SAmir Goldstein } else { 146da309e8cSAmir Goldstein /* 147da309e8cSAmir Goldstein * For non-samefs setup, if we cannot map all layers st_ino 148da309e8cSAmir Goldstein * to a unified address space, we need to make sure that st_dev 149b7bf9908SAmir Goldstein * is unique per underlying fs, so we use the unique anonymous 150b7bf9908SAmir Goldstein * bdev assigned to the underlying fs. 151da309e8cSAmir Goldstein */ 15207f1e596SAmir Goldstein stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev; 153da309e8cSAmir Goldstein } 154da309e8cSAmir Goldstein } 155da309e8cSAmir Goldstein 156b74d24f7SChristian Brauner int ovl_getattr(struct mnt_idmap *idmap, const struct path *path, 157549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int flags) 158e9be9d5eSMiklos Szeredi { 159a528d35eSDavid Howells struct dentry *dentry = path->dentry; 16072b608f0SAmir Goldstein enum ovl_path_type type; 161e9be9d5eSMiklos Szeredi struct path realpath; 1621175b6b8SVivek Goyal const struct cred *old_cred; 163096a218aSAmir Goldstein struct inode *inode = d_inode(dentry); 164096a218aSAmir Goldstein bool is_dir = S_ISDIR(inode->i_mode); 16507f1e596SAmir Goldstein int fsid = 0; 1661175b6b8SVivek Goyal int err; 16767d756c2SVivek Goyal bool metacopy_blocks = false; 16867d756c2SVivek Goyal 16967d756c2SVivek Goyal metacopy_blocks = ovl_is_metacopy_dentry(dentry); 170e9be9d5eSMiklos Szeredi 17172b608f0SAmir Goldstein type = ovl_path_real(dentry, &realpath); 1721175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 173a528d35eSDavid Howells err = vfs_getattr(&realpath, stat, request_mask, flags); 17472b608f0SAmir Goldstein if (err) 17572b608f0SAmir Goldstein goto out; 17672b608f0SAmir Goldstein 177096a218aSAmir Goldstein /* Report the effective immutable/append-only STATX flags */ 178096a218aSAmir Goldstein generic_fill_statx_attr(inode, stat); 179096a218aSAmir Goldstein 18072b608f0SAmir Goldstein /* 181da309e8cSAmir Goldstein * For non-dir or same fs, we use st_ino of the copy up origin. 182da309e8cSAmir Goldstein * This guaranties constant st_dev/st_ino across copy up. 183e487d889SAmir Goldstein * With xino feature and non-samefs, we use st_ino of the copy up 184e487d889SAmir Goldstein * origin masked with high bits that represent the layer id. 18572b608f0SAmir Goldstein * 186da309e8cSAmir Goldstein * If lower filesystem supports NFS file handles, this also guaranties 18772b608f0SAmir Goldstein * persistent st_ino across mount cycle. 18872b608f0SAmir Goldstein */ 1890f831ec8SAmir Goldstein if (!is_dir || ovl_same_dev(dentry->d_sb)) { 190da309e8cSAmir Goldstein if (!OVL_TYPE_UPPER(type)) { 19107f1e596SAmir Goldstein fsid = ovl_layer_lower(dentry)->fsid; 192da309e8cSAmir Goldstein } else if (OVL_TYPE_ORIGIN(type)) { 19372b608f0SAmir Goldstein struct kstat lowerstat; 19467d756c2SVivek Goyal u32 lowermask = STATX_INO | STATX_BLOCKS | 19567d756c2SVivek Goyal (!is_dir ? STATX_NLINK : 0); 19672b608f0SAmir Goldstein 19772b608f0SAmir Goldstein ovl_path_lower(dentry, &realpath); 19872b608f0SAmir Goldstein err = vfs_getattr(&realpath, &lowerstat, 1995b712091SMiklos Szeredi lowermask, flags); 20072b608f0SAmir Goldstein if (err) 20172b608f0SAmir Goldstein goto out; 20272b608f0SAmir Goldstein 20372b608f0SAmir Goldstein /* 204359f392cSAmir Goldstein * Lower hardlinks may be broken on copy up to different 20572b608f0SAmir Goldstein * upper files, so we cannot use the lower origin st_ino 20672b608f0SAmir Goldstein * for those different files, even for the same fs case. 20786eaa130SAmir Goldstein * 20886eaa130SAmir Goldstein * Similarly, several redirected dirs can point to the 20986eaa130SAmir Goldstein * same dir on a lower layer. With the "verify_lower" 21086eaa130SAmir Goldstein * feature, we do not use the lower origin st_ino, if 21186eaa130SAmir Goldstein * we haven't verified that this redirect is unique. 21286eaa130SAmir Goldstein * 213359f392cSAmir Goldstein * With inodes index enabled, it is safe to use st_ino 21486eaa130SAmir Goldstein * of an indexed origin. The index validates that the 21586eaa130SAmir Goldstein * upper hardlink is not broken and that a redirected 21686eaa130SAmir Goldstein * dir is the only redirect to that origin. 21772b608f0SAmir Goldstein */ 21886eaa130SAmir Goldstein if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || 21986eaa130SAmir Goldstein (!ovl_verify_lower(dentry->d_sb) && 2209f99e50dSAmir Goldstein (is_dir || lowerstat.nlink == 1))) { 22107f1e596SAmir Goldstein fsid = ovl_layer_lower(dentry)->fsid; 2229c6d8f13SAmir Goldstein stat->ino = lowerstat.ino; 22372b608f0SAmir Goldstein } 22467d756c2SVivek Goyal 22567d756c2SVivek Goyal /* 22667d756c2SVivek Goyal * If we are querying a metacopy dentry and lower 22767d756c2SVivek Goyal * dentry is data dentry, then use the blocks we 22867d756c2SVivek Goyal * queried just now. We don't have to do additional 22967d756c2SVivek Goyal * vfs_getattr(). If lower itself is metacopy, then 23067d756c2SVivek Goyal * additional vfs_getattr() is unavoidable. 23167d756c2SVivek Goyal */ 23267d756c2SVivek Goyal if (metacopy_blocks && 23367d756c2SVivek Goyal realpath.dentry == ovl_dentry_lowerdata(dentry)) { 23467d756c2SVivek Goyal stat->blocks = lowerstat.blocks; 23567d756c2SVivek Goyal metacopy_blocks = false; 23667d756c2SVivek Goyal } 23767d756c2SVivek Goyal } 23867d756c2SVivek Goyal 23967d756c2SVivek Goyal if (metacopy_blocks) { 24067d756c2SVivek Goyal /* 24167d756c2SVivek Goyal * If lower is not same as lowerdata or if there was 24267d756c2SVivek Goyal * no origin on upper, we can end up here. 24367d756c2SVivek Goyal */ 24467d756c2SVivek Goyal struct kstat lowerdatastat; 24567d756c2SVivek Goyal u32 lowermask = STATX_BLOCKS; 24667d756c2SVivek Goyal 24767d756c2SVivek Goyal ovl_path_lowerdata(dentry, &realpath); 24867d756c2SVivek Goyal err = vfs_getattr(&realpath, &lowerdatastat, 24967d756c2SVivek Goyal lowermask, flags); 25067d756c2SVivek Goyal if (err) 25167d756c2SVivek Goyal goto out; 25267d756c2SVivek Goyal stat->blocks = lowerdatastat.blocks; 2539f99e50dSAmir Goldstein } 254a0c5ad30SAmir Goldstein } 255da309e8cSAmir Goldstein 256c68e7ec5Syoungjun ovl_map_dev_ino(dentry, stat, fsid); 2575b712091SMiklos Szeredi 2585b712091SMiklos Szeredi /* 2595b712091SMiklos Szeredi * It's probably not worth it to count subdirs to get the 2605b712091SMiklos Szeredi * correct link count. nlink=1 seems to pacify 'find' and 2615b712091SMiklos Szeredi * other utilities. 2625b712091SMiklos Szeredi */ 2635b712091SMiklos Szeredi if (is_dir && OVL_TYPE_MERGE(type)) 2645b712091SMiklos Szeredi stat->nlink = 1; 2655b712091SMiklos Szeredi 2665f8415d6SAmir Goldstein /* 2675f8415d6SAmir Goldstein * Return the overlay inode nlinks for indexed upper inodes. 2685f8415d6SAmir Goldstein * Overlay inode nlink counts the union of the upper hardlinks 2695f8415d6SAmir Goldstein * and non-covered lower hardlinks. It does not include the upper 2705f8415d6SAmir Goldstein * index hardlink. 2715f8415d6SAmir Goldstein */ 2725f8415d6SAmir Goldstein if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) 2735f8415d6SAmir Goldstein stat->nlink = dentry->d_inode->i_nlink; 2745f8415d6SAmir Goldstein 27572b608f0SAmir Goldstein out: 2761175b6b8SVivek Goyal revert_creds(old_cred); 27772b608f0SAmir Goldstein 2781175b6b8SVivek Goyal return err; 279e9be9d5eSMiklos Szeredi } 280e9be9d5eSMiklos Szeredi 281549c7297SChristian Brauner int ovl_permission(struct user_namespace *mnt_userns, 282549c7297SChristian Brauner struct inode *inode, int mask) 283e9be9d5eSMiklos Szeredi { 28409d8b586SMiklos Szeredi struct inode *upperinode = ovl_inode_upper(inode); 2854b7791b2SChristian Brauner struct inode *realinode; 2864b7791b2SChristian Brauner struct path realpath; 287c0ca3d70SVivek Goyal const struct cred *old_cred; 288e9be9d5eSMiklos Szeredi int err; 289e9be9d5eSMiklos Szeredi 290e9be9d5eSMiklos Szeredi /* Careful in RCU walk mode */ 2914b7791b2SChristian Brauner ovl_i_path_real(inode, &realpath); 2924b7791b2SChristian Brauner if (!realpath.dentry) { 293e9be9d5eSMiklos Szeredi WARN_ON(!(mask & MAY_NOT_BLOCK)); 294a999d7e1SMiklos Szeredi return -ECHILD; 295e9be9d5eSMiklos Szeredi } 296e9be9d5eSMiklos Szeredi 297c0ca3d70SVivek Goyal /* 298c0ca3d70SVivek Goyal * Check overlay inode with the creds of task and underlying inode 299c0ca3d70SVivek Goyal * with creds of mounter 300c0ca3d70SVivek Goyal */ 30147291baaSChristian Brauner err = generic_permission(&init_user_ns, inode, mask); 302c0ca3d70SVivek Goyal if (err) 303c0ca3d70SVivek Goyal return err; 304c0ca3d70SVivek Goyal 3054b7791b2SChristian Brauner realinode = d_inode(realpath.dentry); 306ec7ba118SMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 307ec7ba118SMiklos Szeredi if (!upperinode && 308ec7ba118SMiklos Szeredi !special_file(realinode->i_mode) && mask & MAY_WRITE) { 309754f8cb7SVivek Goyal mask &= ~(MAY_WRITE | MAY_APPEND); 310ec7ba118SMiklos Szeredi /* Make sure mounter can read file for copy up later */ 311500cac3cSVivek Goyal mask |= MAY_READ; 312500cac3cSVivek Goyal } 3134b7791b2SChristian Brauner err = inode_permission(mnt_user_ns(realpath.mnt), realinode, mask); 314c0ca3d70SVivek Goyal revert_creds(old_cred); 315c0ca3d70SVivek Goyal 316c0ca3d70SVivek Goyal return err; 317e9be9d5eSMiklos Szeredi } 318e9be9d5eSMiklos Szeredi 3196b255391SAl Viro static const char *ovl_get_link(struct dentry *dentry, 320fceef393SAl Viro struct inode *inode, 321fceef393SAl Viro struct delayed_call *done) 322e9be9d5eSMiklos Szeredi { 3231175b6b8SVivek Goyal const struct cred *old_cred; 3241175b6b8SVivek Goyal const char *p; 325e9be9d5eSMiklos Szeredi 3266b255391SAl Viro if (!dentry) 3276b255391SAl Viro return ERR_PTR(-ECHILD); 3286b255391SAl Viro 3291175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3307764235bSMiklos Szeredi p = vfs_get_link(ovl_dentry_real(dentry), done); 3311175b6b8SVivek Goyal revert_creds(old_cred); 3321175b6b8SVivek Goyal return p; 333e9be9d5eSMiklos Szeredi } 334e9be9d5eSMiklos Szeredi 335610afc0bSMiklos Szeredi bool ovl_is_private_xattr(struct super_block *sb, const char *name) 336e9be9d5eSMiklos Szeredi { 3372d2f2d73SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 3382d2f2d73SMiklos Szeredi 3392d2f2d73SMiklos Szeredi if (ofs->config.userxattr) 3402d2f2d73SMiklos Szeredi return strncmp(name, OVL_XATTR_USER_PREFIX, 3412d2f2d73SMiklos Szeredi sizeof(OVL_XATTR_USER_PREFIX) - 1) == 0; 3422d2f2d73SMiklos Szeredi else 3432d2f2d73SMiklos Szeredi return strncmp(name, OVL_XATTR_TRUSTED_PREFIX, 3442d2f2d73SMiklos Szeredi sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1) == 0; 345e9be9d5eSMiklos Szeredi } 346e9be9d5eSMiklos Szeredi 3471d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 3481d88f183SMiklos Szeredi const void *value, size_t size, int flags) 349e9be9d5eSMiklos Szeredi { 350e9be9d5eSMiklos Szeredi int err; 351c914c0e2SAmir Goldstein struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 3521d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 3531d88f183SMiklos Szeredi struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); 3548bc0095dSChristian Brauner struct path realpath; 3551175b6b8SVivek Goyal const struct cred *old_cred; 356e9be9d5eSMiklos Szeredi 357e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 358e9be9d5eSMiklos Szeredi if (err) 359e9be9d5eSMiklos Szeredi goto out; 360e9be9d5eSMiklos Szeredi 3611d88f183SMiklos Szeredi if (!value && !upperdentry) { 3628bc0095dSChristian Brauner ovl_path_lower(dentry, &realpath); 363554677b9SMiklos Szeredi old_cred = ovl_override_creds(dentry->d_sb); 3648bc0095dSChristian Brauner err = vfs_getxattr(mnt_user_ns(realpath.mnt), realdentry, name, NULL, 0); 365554677b9SMiklos Szeredi revert_creds(old_cred); 3660e585cccSAndreas Gruenbacher if (err < 0) 3670e585cccSAndreas Gruenbacher goto out_drop_write; 3680e585cccSAndreas Gruenbacher } 3690e585cccSAndreas Gruenbacher 3701d88f183SMiklos Szeredi if (!upperdentry) { 371e9be9d5eSMiklos Szeredi err = ovl_copy_up(dentry); 372e9be9d5eSMiklos Szeredi if (err) 373e9be9d5eSMiklos Szeredi goto out_drop_write; 374e9be9d5eSMiklos Szeredi 3751d88f183SMiklos Szeredi realdentry = ovl_dentry_upper(dentry); 3761d88f183SMiklos Szeredi } 3770e585cccSAndreas Gruenbacher 3781175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 379c914c0e2SAmir Goldstein if (value) { 380c914c0e2SAmir Goldstein err = ovl_do_setxattr(ofs, realdentry, name, value, size, 381c7c7a1a1STycho Andersen flags); 382c914c0e2SAmir Goldstein } else { 3830e585cccSAndreas Gruenbacher WARN_ON(flags != XATTR_REPLACE); 384c914c0e2SAmir Goldstein err = ovl_do_removexattr(ofs, realdentry, name); 3850e585cccSAndreas Gruenbacher } 3861175b6b8SVivek Goyal revert_creds(old_cred); 387e9be9d5eSMiklos Szeredi 388d9854c87SMiklos Szeredi /* copy c/mtime */ 3892878dffcSChristian Brauner ovl_copyattr(inode); 390d9854c87SMiklos Szeredi 391e9be9d5eSMiklos Szeredi out_drop_write: 392e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 393e9be9d5eSMiklos Szeredi out: 394e9be9d5eSMiklos Szeredi return err; 395e9be9d5eSMiklos Szeredi } 396e9be9d5eSMiklos Szeredi 3971d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 3980eb45fc3SAndreas Gruenbacher void *value, size_t size) 399e9be9d5eSMiklos Szeredi { 4001175b6b8SVivek Goyal ssize_t res; 4011175b6b8SVivek Goyal const struct cred *old_cred; 4028bc0095dSChristian Brauner struct path realpath; 40352148463SMiklos Szeredi 4048bc0095dSChristian Brauner ovl_i_path_real(inode, &realpath); 4051175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 4068bc0095dSChristian Brauner res = vfs_getxattr(mnt_user_ns(realpath.mnt), realpath.dentry, name, value, size); 4071175b6b8SVivek Goyal revert_creds(old_cred); 4081175b6b8SVivek Goyal return res; 409e9be9d5eSMiklos Szeredi } 410e9be9d5eSMiklos Szeredi 411610afc0bSMiklos Szeredi static bool ovl_can_list(struct super_block *sb, const char *s) 412a082c6f6SMiklos Szeredi { 4138f6ee74cSMiklos Szeredi /* Never list private (.overlay) */ 4148f6ee74cSMiklos Szeredi if (ovl_is_private_xattr(sb, s)) 4158f6ee74cSMiklos Szeredi return false; 4168f6ee74cSMiklos Szeredi 417f48bbfb2SBhaskar Chowdhury /* List all non-trusted xattrs */ 418a082c6f6SMiklos Szeredi if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) 419a082c6f6SMiklos Szeredi return true; 420a082c6f6SMiklos Szeredi 4218f6ee74cSMiklos Szeredi /* list other trusted for superuser only */ 4228f6ee74cSMiklos Szeredi return ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN); 423a082c6f6SMiklos Szeredi } 424a082c6f6SMiklos Szeredi 425e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) 426e9be9d5eSMiklos Szeredi { 427b581755bSMiklos Szeredi struct dentry *realdentry = ovl_dentry_real(dentry); 428e9be9d5eSMiklos Szeredi ssize_t res; 4297cb35119SMiklos Szeredi size_t len; 4307cb35119SMiklos Szeredi char *s; 4311175b6b8SVivek Goyal const struct cred *old_cred; 432e9be9d5eSMiklos Szeredi 4331175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 434b581755bSMiklos Szeredi res = vfs_listxattr(realdentry, list, size); 4351175b6b8SVivek Goyal revert_creds(old_cred); 436e9be9d5eSMiklos Szeredi if (res <= 0 || size == 0) 437e9be9d5eSMiklos Szeredi return res; 438e9be9d5eSMiklos Szeredi 439e9be9d5eSMiklos Szeredi /* filter out private xattrs */ 4407cb35119SMiklos Szeredi for (s = list, len = res; len;) { 4417cb35119SMiklos Szeredi size_t slen = strnlen(s, len) + 1; 442e9be9d5eSMiklos Szeredi 4437cb35119SMiklos Szeredi /* underlying fs providing us with an broken xattr list? */ 4447cb35119SMiklos Szeredi if (WARN_ON(slen > len)) 4457cb35119SMiklos Szeredi return -EIO; 446e9be9d5eSMiklos Szeredi 4477cb35119SMiklos Szeredi len -= slen; 448610afc0bSMiklos Szeredi if (!ovl_can_list(dentry->d_sb, s)) { 449e9be9d5eSMiklos Szeredi res -= slen; 4507cb35119SMiklos Szeredi memmove(s, s + slen, len); 451e9be9d5eSMiklos Szeredi } else { 4527cb35119SMiklos Szeredi s += slen; 453e9be9d5eSMiklos Szeredi } 454e9be9d5eSMiklos Szeredi } 455e9be9d5eSMiklos Szeredi 456e9be9d5eSMiklos Szeredi return res; 457e9be9d5eSMiklos Szeredi } 458e9be9d5eSMiklos Szeredi 459ded53656SYang Xu #ifdef CONFIG_FS_POSIX_ACL 4601aa5fef5SChristian Brauner /* 4611aa5fef5SChristian Brauner * Apply the idmapping of the layer to POSIX ACLs. The caller must pass a clone 4621aa5fef5SChristian Brauner * of the POSIX ACLs retrieved from the lower layer to this function to not 4631aa5fef5SChristian Brauner * alter the POSIX ACLs for the underlying filesystem. 4641aa5fef5SChristian Brauner */ 4656c0a8bfbSChristian Brauner static void ovl_idmap_posix_acl(const struct inode *realinode, 466abfcf55dSChristian Brauner struct user_namespace *mnt_userns, 4671aa5fef5SChristian Brauner struct posix_acl *acl) 4681aa5fef5SChristian Brauner { 469abfcf55dSChristian Brauner struct user_namespace *fs_userns = i_user_ns(realinode); 470abfcf55dSChristian Brauner 4711aa5fef5SChristian Brauner for (unsigned int i = 0; i < acl->a_count; i++) { 4721aa5fef5SChristian Brauner vfsuid_t vfsuid; 4731aa5fef5SChristian Brauner vfsgid_t vfsgid; 4741aa5fef5SChristian Brauner 4751aa5fef5SChristian Brauner struct posix_acl_entry *e = &acl->a_entries[i]; 4761aa5fef5SChristian Brauner switch (e->e_tag) { 4771aa5fef5SChristian Brauner case ACL_USER: 478abfcf55dSChristian Brauner vfsuid = make_vfsuid(mnt_userns, fs_userns, e->e_uid); 4791aa5fef5SChristian Brauner e->e_uid = vfsuid_into_kuid(vfsuid); 4801aa5fef5SChristian Brauner break; 4811aa5fef5SChristian Brauner case ACL_GROUP: 482abfcf55dSChristian Brauner vfsgid = make_vfsgid(mnt_userns, fs_userns, e->e_gid); 4831aa5fef5SChristian Brauner e->e_gid = vfsgid_into_kgid(vfsgid); 4841aa5fef5SChristian Brauner break; 4851aa5fef5SChristian Brauner } 4861aa5fef5SChristian Brauner } 4871aa5fef5SChristian Brauner } 4881aa5fef5SChristian Brauner 4891aa5fef5SChristian Brauner /* 4906c0a8bfbSChristian Brauner * The @noperm argument is used to skip permission checking and is a temporary 4916c0a8bfbSChristian Brauner * measure. Quoting Miklos from an earlier discussion: 4926c0a8bfbSChristian Brauner * 4936c0a8bfbSChristian Brauner * > So there are two paths to getting an acl: 4946c0a8bfbSChristian Brauner * > 1) permission checking and 2) retrieving the value via getxattr(2). 4956c0a8bfbSChristian Brauner * > This is a similar situation as reading a symlink vs. following it. 4966c0a8bfbSChristian Brauner * > When following a symlink overlayfs always reads the link on the 4976c0a8bfbSChristian Brauner * > underlying fs just as if it was a readlink(2) call, calling 4986c0a8bfbSChristian Brauner * > security_inode_readlink() instead of security_inode_follow_link(). 4996c0a8bfbSChristian Brauner * > This is logical: we are reading the link from the underlying storage, 5006c0a8bfbSChristian Brauner * > and following it on overlayfs. 5016c0a8bfbSChristian Brauner * > 5026c0a8bfbSChristian Brauner * > Applying the same logic to acl: we do need to call the 5036c0a8bfbSChristian Brauner * > security_inode_getxattr() on the underlying fs, even if just want to 5046c0a8bfbSChristian Brauner * > check permissions on overlay. This is currently not done, which is an 5056c0a8bfbSChristian Brauner * > inconsistency. 5066c0a8bfbSChristian Brauner * > 5076c0a8bfbSChristian Brauner * > Maybe adding the check to ovl_get_acl() is the right way to go, but 5086c0a8bfbSChristian Brauner * > I'm a little afraid of a performance regression. Will look into that. 5096c0a8bfbSChristian Brauner * 5106c0a8bfbSChristian Brauner * Until we have made a decision allow this helper to take the @noperm 5116c0a8bfbSChristian Brauner * argument. We should hopefully be able to remove it soon. 5126c0a8bfbSChristian Brauner */ 51331acceb9SChristian Brauner struct posix_acl *ovl_get_acl_path(const struct path *path, 5146c0a8bfbSChristian Brauner const char *acl_name, bool noperm) 5156c0a8bfbSChristian Brauner { 5166c0a8bfbSChristian Brauner struct posix_acl *real_acl, *clone; 5176c0a8bfbSChristian Brauner struct user_namespace *mnt_userns; 518*77435322SChristian Brauner struct mnt_idmap *idmap; 5196c0a8bfbSChristian Brauner struct inode *realinode = d_inode(path->dentry); 5206c0a8bfbSChristian Brauner 521*77435322SChristian Brauner idmap = mnt_idmap(path->mnt); 522*77435322SChristian Brauner mnt_userns = mnt_idmap_owner(idmap); 5236c0a8bfbSChristian Brauner 5246c0a8bfbSChristian Brauner if (noperm) 5256c0a8bfbSChristian Brauner real_acl = get_inode_acl(realinode, posix_acl_type(acl_name)); 5266c0a8bfbSChristian Brauner else 527*77435322SChristian Brauner real_acl = vfs_get_acl(idmap, path->dentry, acl_name); 5286c0a8bfbSChristian Brauner if (IS_ERR_OR_NULL(real_acl)) 5296c0a8bfbSChristian Brauner return real_acl; 5306c0a8bfbSChristian Brauner 5316c0a8bfbSChristian Brauner if (!is_idmapped_mnt(path->mnt)) 5326c0a8bfbSChristian Brauner return real_acl; 5336c0a8bfbSChristian Brauner 5346c0a8bfbSChristian Brauner /* 5356c0a8bfbSChristian Brauner * We cannot alter the ACLs returned from the relevant layer as that 5366c0a8bfbSChristian Brauner * would alter the cached values filesystem wide for the lower 5376c0a8bfbSChristian Brauner * filesystem. Instead we can clone the ACLs and then apply the 5386c0a8bfbSChristian Brauner * relevant idmapping of the layer. 5396c0a8bfbSChristian Brauner */ 5406c0a8bfbSChristian Brauner clone = posix_acl_clone(real_acl, GFP_KERNEL); 5416c0a8bfbSChristian Brauner posix_acl_release(real_acl); /* release original acl */ 5426c0a8bfbSChristian Brauner if (!clone) 5436c0a8bfbSChristian Brauner return ERR_PTR(-ENOMEM); 5446c0a8bfbSChristian Brauner 5456c0a8bfbSChristian Brauner ovl_idmap_posix_acl(realinode, mnt_userns, clone); 5466c0a8bfbSChristian Brauner return clone; 5476c0a8bfbSChristian Brauner } 5486c0a8bfbSChristian Brauner 5496c0a8bfbSChristian Brauner /* 5501aa5fef5SChristian Brauner * When the relevant layer is an idmapped mount we need to take the idmapping 5511aa5fef5SChristian Brauner * of the layer into account and translate any ACL_{GROUP,USER} values 5521aa5fef5SChristian Brauner * according to the idmapped mount. 5531aa5fef5SChristian Brauner * 5541aa5fef5SChristian Brauner * We cannot alter the ACLs returned from the relevant layer as that would 5551aa5fef5SChristian Brauner * alter the cached values filesystem wide for the lower filesystem. Instead we 5561aa5fef5SChristian Brauner * can clone the ACLs and then apply the relevant idmapping of the layer. 5571aa5fef5SChristian Brauner * 5581aa5fef5SChristian Brauner * This is obviously only relevant when idmapped layers are used. 5591aa5fef5SChristian Brauner */ 560*77435322SChristian Brauner struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap, 5616c0a8bfbSChristian Brauner struct inode *inode, int type, 5626c0a8bfbSChristian Brauner bool rcu, bool noperm) 56339a25b2bSVivek Goyal { 56409d8b586SMiklos Szeredi struct inode *realinode = ovl_inode_real(inode); 5656c0a8bfbSChristian Brauner struct posix_acl *acl; 5661aa5fef5SChristian Brauner struct path realpath; 56739a25b2bSVivek Goyal 568ded53656SYang Xu if (!IS_POSIXACL(realinode)) 56939a25b2bSVivek Goyal return NULL; 57039a25b2bSVivek Goyal 5711aa5fef5SChristian Brauner /* Careful in RCU walk mode */ 5721aa5fef5SChristian Brauner ovl_i_path_real(inode, &realpath); 5731aa5fef5SChristian Brauner if (!realpath.dentry) { 5741aa5fef5SChristian Brauner WARN_ON(!rcu); 5751aa5fef5SChristian Brauner return ERR_PTR(-ECHILD); 5761aa5fef5SChristian Brauner } 5771aa5fef5SChristian Brauner 5781aa5fef5SChristian Brauner if (rcu) { 5796c0a8bfbSChristian Brauner /* 5806c0a8bfbSChristian Brauner * If the layer is idmapped drop out of RCU path walk 5816c0a8bfbSChristian Brauner * so we can clone the ACLs. 5826c0a8bfbSChristian Brauner */ 5836c0a8bfbSChristian Brauner if (is_idmapped_mnt(realpath.mnt)) 5846c0a8bfbSChristian Brauner return ERR_PTR(-ECHILD); 5856c0a8bfbSChristian Brauner 5861aa5fef5SChristian Brauner acl = get_cached_acl_rcu(realinode, type); 5871aa5fef5SChristian Brauner } else { 5881aa5fef5SChristian Brauner const struct cred *old_cred; 589332f606bSMiklos Szeredi 5901175b6b8SVivek Goyal old_cred = ovl_override_creds(inode->i_sb); 5916c0a8bfbSChristian Brauner acl = ovl_get_acl_path(&realpath, posix_acl_xattr_name(type), noperm); 5921175b6b8SVivek Goyal revert_creds(old_cred); 5931aa5fef5SChristian Brauner } 5946c0a8bfbSChristian Brauner 5951175b6b8SVivek Goyal return acl; 59639a25b2bSVivek Goyal } 5970e641857SChristian Brauner 5980e641857SChristian Brauner static int ovl_set_or_remove_acl(struct dentry *dentry, struct inode *inode, 5990e641857SChristian Brauner struct posix_acl *acl, int type) 6000e641857SChristian Brauner { 6010e641857SChristian Brauner int err; 6020e641857SChristian Brauner struct path realpath; 6030e641857SChristian Brauner const char *acl_name; 6040e641857SChristian Brauner const struct cred *old_cred; 6050e641857SChristian Brauner struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 6060e641857SChristian Brauner struct dentry *upperdentry = ovl_dentry_upper(dentry); 6070e641857SChristian Brauner struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); 6080e641857SChristian Brauner 6090e641857SChristian Brauner err = ovl_want_write(dentry); 6100e641857SChristian Brauner if (err) 6110e641857SChristian Brauner return err; 6120e641857SChristian Brauner 6130e641857SChristian Brauner /* 6140e641857SChristian Brauner * If ACL is to be removed from a lower file, check if it exists in 6150e641857SChristian Brauner * the first place before copying it up. 6160e641857SChristian Brauner */ 6170e641857SChristian Brauner acl_name = posix_acl_xattr_name(type); 6180e641857SChristian Brauner if (!acl && !upperdentry) { 6190e641857SChristian Brauner struct posix_acl *real_acl; 6200e641857SChristian Brauner 6210e641857SChristian Brauner ovl_path_lower(dentry, &realpath); 6220e641857SChristian Brauner old_cred = ovl_override_creds(dentry->d_sb); 623*77435322SChristian Brauner real_acl = vfs_get_acl(mnt_idmap(realpath.mnt), realdentry, 6240e641857SChristian Brauner acl_name); 6250e641857SChristian Brauner revert_creds(old_cred); 6260e641857SChristian Brauner if (IS_ERR(real_acl)) { 6270e641857SChristian Brauner err = PTR_ERR(real_acl); 6280e641857SChristian Brauner goto out_drop_write; 6290e641857SChristian Brauner } 6305b52aebeSChristian Brauner posix_acl_release(real_acl); 6310e641857SChristian Brauner } 6320e641857SChristian Brauner 6330e641857SChristian Brauner if (!upperdentry) { 6340e641857SChristian Brauner err = ovl_copy_up(dentry); 6350e641857SChristian Brauner if (err) 6360e641857SChristian Brauner goto out_drop_write; 6370e641857SChristian Brauner 6380e641857SChristian Brauner realdentry = ovl_dentry_upper(dentry); 6390e641857SChristian Brauner } 6400e641857SChristian Brauner 6410e641857SChristian Brauner old_cred = ovl_override_creds(dentry->d_sb); 6420e641857SChristian Brauner if (acl) 6430e641857SChristian Brauner err = ovl_do_set_acl(ofs, realdentry, acl_name, acl); 6440e641857SChristian Brauner else 6450e641857SChristian Brauner err = ovl_do_remove_acl(ofs, realdentry, acl_name); 6460e641857SChristian Brauner revert_creds(old_cred); 6470e641857SChristian Brauner 6480e641857SChristian Brauner /* copy c/mtime */ 6490e641857SChristian Brauner ovl_copyattr(inode); 6500e641857SChristian Brauner 6510e641857SChristian Brauner out_drop_write: 6520e641857SChristian Brauner ovl_drop_write(dentry); 6530e641857SChristian Brauner return err; 6540e641857SChristian Brauner } 6550e641857SChristian Brauner 6560e641857SChristian Brauner int ovl_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry, 6570e641857SChristian Brauner struct posix_acl *acl, int type) 6580e641857SChristian Brauner { 6590e641857SChristian Brauner int err; 6600e641857SChristian Brauner struct inode *inode = d_inode(dentry); 6610e641857SChristian Brauner struct dentry *workdir = ovl_workdir(dentry); 6620e641857SChristian Brauner struct inode *realinode = ovl_inode_real(inode); 6630e641857SChristian Brauner 6640e641857SChristian Brauner if (!IS_POSIXACL(d_inode(workdir))) 6650e641857SChristian Brauner return -EOPNOTSUPP; 6660e641857SChristian Brauner if (!realinode->i_op->set_acl) 6670e641857SChristian Brauner return -EOPNOTSUPP; 6680e641857SChristian Brauner if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) 6690e641857SChristian Brauner return acl ? -EACCES : 0; 6700e641857SChristian Brauner if (!inode_owner_or_capable(&init_user_ns, inode)) 6710e641857SChristian Brauner return -EPERM; 6720e641857SChristian Brauner 6730e641857SChristian Brauner /* 6740e641857SChristian Brauner * Check if sgid bit needs to be cleared (actual setacl operation will 6750e641857SChristian Brauner * be done with mounter's capabilities and so that won't do it for us). 6760e641857SChristian Brauner */ 6770e641857SChristian Brauner if (unlikely(inode->i_mode & S_ISGID) && type == ACL_TYPE_ACCESS && 6780e641857SChristian Brauner !in_group_p(inode->i_gid) && 6790e641857SChristian Brauner !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FSETID)) { 6800e641857SChristian Brauner struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; 6810e641857SChristian Brauner 682c1632a0fSChristian Brauner err = ovl_setattr(&nop_mnt_idmap, dentry, &iattr); 6830e641857SChristian Brauner if (err) 6840e641857SChristian Brauner return err; 6850e641857SChristian Brauner } 6860e641857SChristian Brauner 6870e641857SChristian Brauner return ovl_set_or_remove_acl(dentry, inode, acl, type); 6880e641857SChristian Brauner } 689ded53656SYang Xu #endif 69039a25b2bSVivek Goyal 69195582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags) 692d719e8f2SMiklos Szeredi { 6938f35cf51SMiklos Szeredi if (flags & S_ATIME) { 6948f35cf51SMiklos Szeredi struct ovl_fs *ofs = inode->i_sb->s_fs_info; 6958f35cf51SMiklos Szeredi struct path upperpath = { 69608f4c7c8SMiklos Szeredi .mnt = ovl_upper_mnt(ofs), 6978f35cf51SMiklos Szeredi .dentry = ovl_upperdentry_dereference(OVL_I(inode)), 6988f35cf51SMiklos Szeredi }; 699d719e8f2SMiklos Szeredi 700d719e8f2SMiklos Szeredi if (upperpath.dentry) { 701d719e8f2SMiklos Szeredi touch_atime(&upperpath); 702d719e8f2SMiklos Szeredi inode->i_atime = d_inode(upperpath.dentry)->i_atime; 703d719e8f2SMiklos Szeredi } 7048f35cf51SMiklos Szeredi } 705d719e8f2SMiklos Szeredi return 0; 706d719e8f2SMiklos Szeredi } 707d719e8f2SMiklos Szeredi 7089e142c41SMiklos Szeredi static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 7099e142c41SMiklos Szeredi u64 start, u64 len) 7109e142c41SMiklos Szeredi { 7119e142c41SMiklos Szeredi int err; 712c11faf32SChengguang Xu struct inode *realinode = ovl_inode_realdata(inode); 7139e142c41SMiklos Szeredi const struct cred *old_cred; 7149e142c41SMiklos Szeredi 7159e142c41SMiklos Szeredi if (!realinode->i_op->fiemap) 7169e142c41SMiklos Szeredi return -EOPNOTSUPP; 7179e142c41SMiklos Szeredi 7189e142c41SMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 7199e142c41SMiklos Szeredi err = realinode->i_op->fiemap(realinode, fieinfo, start, len); 7209e142c41SMiklos Szeredi revert_creds(old_cred); 7219e142c41SMiklos Szeredi 7229e142c41SMiklos Szeredi return err; 7239e142c41SMiklos Szeredi } 7249e142c41SMiklos Szeredi 72566dbfabfSMiklos Szeredi /* 72666dbfabfSMiklos Szeredi * Work around the fact that security_file_ioctl() takes a file argument. 72766dbfabfSMiklos Szeredi * Introducing security_inode_fileattr_get/set() hooks would solve this issue 72866dbfabfSMiklos Szeredi * properly. 72966dbfabfSMiklos Szeredi */ 7302d343087SAl Viro static int ovl_security_fileattr(const struct path *realpath, struct fileattr *fa, 73166dbfabfSMiklos Szeredi bool set) 73266dbfabfSMiklos Szeredi { 73366dbfabfSMiklos Szeredi struct file *file; 73466dbfabfSMiklos Szeredi unsigned int cmd; 73566dbfabfSMiklos Szeredi int err; 73666dbfabfSMiklos Szeredi 73772db8211SAmir Goldstein file = dentry_open(realpath, O_RDONLY, current_cred()); 73866dbfabfSMiklos Szeredi if (IS_ERR(file)) 73966dbfabfSMiklos Szeredi return PTR_ERR(file); 74066dbfabfSMiklos Szeredi 74166dbfabfSMiklos Szeredi if (set) 74266dbfabfSMiklos Szeredi cmd = fa->fsx_valid ? FS_IOC_FSSETXATTR : FS_IOC_SETFLAGS; 74366dbfabfSMiklos Szeredi else 74466dbfabfSMiklos Szeredi cmd = fa->fsx_valid ? FS_IOC_FSGETXATTR : FS_IOC_GETFLAGS; 74566dbfabfSMiklos Szeredi 74666dbfabfSMiklos Szeredi err = security_file_ioctl(file, cmd, 0); 74766dbfabfSMiklos Szeredi fput(file); 74866dbfabfSMiklos Szeredi 74966dbfabfSMiklos Szeredi return err; 75066dbfabfSMiklos Szeredi } 75166dbfabfSMiklos Szeredi 7522d343087SAl Viro int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa) 75372db8211SAmir Goldstein { 75472db8211SAmir Goldstein int err; 75572db8211SAmir Goldstein 75672db8211SAmir Goldstein err = ovl_security_fileattr(realpath, fa, true); 75772db8211SAmir Goldstein if (err) 75872db8211SAmir Goldstein return err; 75972db8211SAmir Goldstein 76050db8d02SChristian Brauner return vfs_fileattr_set(mnt_user_ns(realpath->mnt), realpath->dentry, fa); 76172db8211SAmir Goldstein } 76272db8211SAmir Goldstein 76366dbfabfSMiklos Szeredi int ovl_fileattr_set(struct user_namespace *mnt_userns, 76466dbfabfSMiklos Szeredi struct dentry *dentry, struct fileattr *fa) 76566dbfabfSMiklos Szeredi { 76666dbfabfSMiklos Szeredi struct inode *inode = d_inode(dentry); 76772db8211SAmir Goldstein struct path upperpath; 76866dbfabfSMiklos Szeredi const struct cred *old_cred; 769096a218aSAmir Goldstein unsigned int flags; 77066dbfabfSMiklos Szeredi int err; 77166dbfabfSMiklos Szeredi 77266dbfabfSMiklos Szeredi err = ovl_want_write(dentry); 77366dbfabfSMiklos Szeredi if (err) 77466dbfabfSMiklos Szeredi goto out; 77566dbfabfSMiklos Szeredi 77666dbfabfSMiklos Szeredi err = ovl_copy_up(dentry); 77766dbfabfSMiklos Szeredi if (!err) { 77872db8211SAmir Goldstein ovl_path_real(dentry, &upperpath); 77966dbfabfSMiklos Szeredi 78066dbfabfSMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 781096a218aSAmir Goldstein /* 782096a218aSAmir Goldstein * Store immutable/append-only flags in xattr and clear them 783096a218aSAmir Goldstein * in upper fileattr (in case they were set by older kernel) 784096a218aSAmir Goldstein * so children of "ovl-immutable" directories lower aliases of 785096a218aSAmir Goldstein * "ovl-immutable" hardlinks could be copied up. 786096a218aSAmir Goldstein * Clear xattr when flags are cleared. 787096a218aSAmir Goldstein */ 788096a218aSAmir Goldstein err = ovl_set_protattr(inode, upperpath.dentry, fa); 789096a218aSAmir Goldstein if (!err) 79072db8211SAmir Goldstein err = ovl_real_fileattr_set(&upperpath, fa); 79166dbfabfSMiklos Szeredi revert_creds(old_cred); 792096a218aSAmir Goldstein 793096a218aSAmir Goldstein /* 794096a218aSAmir Goldstein * Merge real inode flags with inode flags read from 795096a218aSAmir Goldstein * overlay.protattr xattr 796096a218aSAmir Goldstein */ 797096a218aSAmir Goldstein flags = ovl_inode_real(inode)->i_flags & OVL_COPY_I_FLAGS_MASK; 798096a218aSAmir Goldstein 799096a218aSAmir Goldstein BUILD_BUG_ON(OVL_PROT_I_FLAGS_MASK & ~OVL_COPY_I_FLAGS_MASK); 800096a218aSAmir Goldstein flags |= inode->i_flags & OVL_PROT_I_FLAGS_MASK; 801096a218aSAmir Goldstein inode_set_flags(inode, flags, OVL_COPY_I_FLAGS_MASK); 802d8991e86SChengguang Xu 803d8991e86SChengguang Xu /* Update ctime */ 8042878dffcSChristian Brauner ovl_copyattr(inode); 80566dbfabfSMiklos Szeredi } 80666dbfabfSMiklos Szeredi ovl_drop_write(dentry); 80766dbfabfSMiklos Szeredi out: 80866dbfabfSMiklos Szeredi return err; 80966dbfabfSMiklos Szeredi } 81066dbfabfSMiklos Szeredi 811096a218aSAmir Goldstein /* Convert inode protection flags to fileattr flags */ 812096a218aSAmir Goldstein static void ovl_fileattr_prot_flags(struct inode *inode, struct fileattr *fa) 813096a218aSAmir Goldstein { 814096a218aSAmir Goldstein BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL); 815096a218aSAmir Goldstein BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON); 816096a218aSAmir Goldstein 817096a218aSAmir Goldstein if (inode->i_flags & S_APPEND) { 818096a218aSAmir Goldstein fa->flags |= FS_APPEND_FL; 819096a218aSAmir Goldstein fa->fsx_xflags |= FS_XFLAG_APPEND; 820096a218aSAmir Goldstein } 821096a218aSAmir Goldstein if (inode->i_flags & S_IMMUTABLE) { 822096a218aSAmir Goldstein fa->flags |= FS_IMMUTABLE_FL; 823096a218aSAmir Goldstein fa->fsx_xflags |= FS_XFLAG_IMMUTABLE; 824096a218aSAmir Goldstein } 825096a218aSAmir Goldstein } 826096a218aSAmir Goldstein 8272d343087SAl Viro int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa) 82872db8211SAmir Goldstein { 82972db8211SAmir Goldstein int err; 83072db8211SAmir Goldstein 83172db8211SAmir Goldstein err = ovl_security_fileattr(realpath, fa, false); 83272db8211SAmir Goldstein if (err) 83372db8211SAmir Goldstein return err; 83472db8211SAmir Goldstein 8355b0a414dSMiklos Szeredi err = vfs_fileattr_get(realpath->dentry, fa); 8365b0a414dSMiklos Szeredi if (err == -ENOIOCTLCMD) 8375b0a414dSMiklos Szeredi err = -ENOTTY; 8385b0a414dSMiklos Szeredi return err; 83972db8211SAmir Goldstein } 84072db8211SAmir Goldstein 84166dbfabfSMiklos Szeredi int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa) 84266dbfabfSMiklos Szeredi { 84366dbfabfSMiklos Szeredi struct inode *inode = d_inode(dentry); 84472db8211SAmir Goldstein struct path realpath; 84566dbfabfSMiklos Szeredi const struct cred *old_cred; 84666dbfabfSMiklos Szeredi int err; 84766dbfabfSMiklos Szeredi 84872db8211SAmir Goldstein ovl_path_real(dentry, &realpath); 84972db8211SAmir Goldstein 85066dbfabfSMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 85172db8211SAmir Goldstein err = ovl_real_fileattr_get(&realpath, fa); 852096a218aSAmir Goldstein ovl_fileattr_prot_flags(inode, fa); 85366dbfabfSMiklos Szeredi revert_creds(old_cred); 85466dbfabfSMiklos Szeredi 85566dbfabfSMiklos Szeredi return err; 85666dbfabfSMiklos Szeredi } 85766dbfabfSMiklos Szeredi 858e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_file_inode_operations = { 859e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 860e9be9d5eSMiklos Szeredi .permission = ovl_permission, 861e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 862e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 8636c0a8bfbSChristian Brauner .get_inode_acl = ovl_get_inode_acl, 8646c0a8bfbSChristian Brauner .get_acl = ovl_get_acl, 8650e641857SChristian Brauner .set_acl = ovl_set_acl, 866d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 8679e142c41SMiklos Szeredi .fiemap = ovl_fiemap, 86866dbfabfSMiklos Szeredi .fileattr_get = ovl_fileattr_get, 86966dbfabfSMiklos Szeredi .fileattr_set = ovl_fileattr_set, 870e9be9d5eSMiklos Szeredi }; 871e9be9d5eSMiklos Szeredi 872e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_symlink_inode_operations = { 873e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 8746b255391SAl Viro .get_link = ovl_get_link, 875e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 876e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 877d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 878e9be9d5eSMiklos Szeredi }; 879e9be9d5eSMiklos Szeredi 8809e142c41SMiklos Szeredi static const struct inode_operations ovl_special_inode_operations = { 8819e142c41SMiklos Szeredi .setattr = ovl_setattr, 8829e142c41SMiklos Szeredi .permission = ovl_permission, 8839e142c41SMiklos Szeredi .getattr = ovl_getattr, 8849e142c41SMiklos Szeredi .listxattr = ovl_listxattr, 8856c0a8bfbSChristian Brauner .get_inode_acl = ovl_get_inode_acl, 8866c0a8bfbSChristian Brauner .get_acl = ovl_get_acl, 8870e641857SChristian Brauner .set_acl = ovl_set_acl, 8889e142c41SMiklos Szeredi .update_time = ovl_update_time, 8899e142c41SMiklos Szeredi }; 8909e142c41SMiklos Szeredi 89169383c59SWei Yongjun static const struct address_space_operations ovl_aops = { 8925b910bd6SAmir Goldstein /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */ 8935b910bd6SAmir Goldstein .direct_IO = noop_direct_IO, 8945b910bd6SAmir Goldstein }; 8955b910bd6SAmir Goldstein 896b1eaa950SAmir Goldstein /* 897b1eaa950SAmir Goldstein * It is possible to stack overlayfs instance on top of another 898a5a84682SChengguang Xu * overlayfs instance as lower layer. We need to annotate the 899b1eaa950SAmir Goldstein * stackable i_mutex locks according to stack level of the super 900b1eaa950SAmir Goldstein * block instance. An overlayfs instance can never be in stack 901b1eaa950SAmir Goldstein * depth 0 (there is always a real fs below it). An overlayfs 902f48bbfb2SBhaskar Chowdhury * inode lock will use the lockdep annotation ovl_i_mutex_key[depth]. 903b1eaa950SAmir Goldstein * 904b1eaa950SAmir Goldstein * For example, here is a snip from /proc/lockdep_chains after 905b1eaa950SAmir Goldstein * dir_iterate of nested overlayfs: 906b1eaa950SAmir Goldstein * 907b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) 908b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) 909b1eaa950SAmir Goldstein * [...] &type->i_mutex_dir_key (stack_depth=0) 910b1f9d385SAmir Goldstein * 911b1f9d385SAmir Goldstein * Locking order w.r.t ovl_want_write() is important for nested overlayfs. 912b1f9d385SAmir Goldstein * 913b1f9d385SAmir Goldstein * This chain is valid: 914b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[2]) 915b1f9d385SAmir Goldstein * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 916b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[2]) 917b1f9d385SAmir Goldstein * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 918b1f9d385SAmir Goldstein * 919b1f9d385SAmir Goldstein * And this chain is valid: 920b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[2]) 921b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[2]) 922b1f9d385SAmir Goldstein * - lowerinode->i_rwsem (inode_lock[1]) 923b1f9d385SAmir Goldstein * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 924b1f9d385SAmir Goldstein * 925b1f9d385SAmir Goldstein * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is 926b1f9d385SAmir Goldstein * held, because it is in reverse order of the non-nested case using the same 927b1f9d385SAmir Goldstein * upper fs: 928b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[1]) 929b1f9d385SAmir Goldstein * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 930b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[1]) 931b1eaa950SAmir Goldstein */ 932b1eaa950SAmir Goldstein #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH 933b1eaa950SAmir Goldstein 934b1eaa950SAmir Goldstein static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) 935b1eaa950SAmir Goldstein { 936b1eaa950SAmir Goldstein #ifdef CONFIG_LOCKDEP 937b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; 938b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; 9394eae06deSAmir Goldstein static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; 940b1eaa950SAmir Goldstein 941b1eaa950SAmir Goldstein int depth = inode->i_sb->s_stack_depth - 1; 942b1eaa950SAmir Goldstein 943b1eaa950SAmir Goldstein if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) 944b1eaa950SAmir Goldstein depth = 0; 945b1eaa950SAmir Goldstein 946b1eaa950SAmir Goldstein if (S_ISDIR(inode->i_mode)) 947b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); 948b1eaa950SAmir Goldstein else 949b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); 9504eae06deSAmir Goldstein 9514eae06deSAmir Goldstein lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); 952b1eaa950SAmir Goldstein #endif 953b1eaa950SAmir Goldstein } 954b1eaa950SAmir Goldstein 9554d314f78SAmir Goldstein static void ovl_next_ino(struct inode *inode) 9564d314f78SAmir Goldstein { 9574d314f78SAmir Goldstein struct ovl_fs *ofs = inode->i_sb->s_fs_info; 9584d314f78SAmir Goldstein 9594d314f78SAmir Goldstein inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 9604d314f78SAmir Goldstein if (unlikely(!inode->i_ino)) 9614d314f78SAmir Goldstein inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 9624d314f78SAmir Goldstein } 9634d314f78SAmir Goldstein 96462c832edSAmir Goldstein static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) 965e9be9d5eSMiklos Szeredi { 96612574a9fSAmir Goldstein int xinobits = ovl_xino_bits(inode->i_sb); 967dfe51d47SAmir Goldstein unsigned int xinoshift = 64 - xinobits; 96812574a9fSAmir Goldstein 969695b46e7SAmir Goldstein /* 9706dde1e42SAmir Goldstein * When d_ino is consistent with st_ino (samefs or i_ino has enough 9716dde1e42SAmir Goldstein * bits to encode layer), set the same value used for st_ino to i_ino, 9726dde1e42SAmir Goldstein * so inode number exposed via /proc/locks and a like will be 9736dde1e42SAmir Goldstein * consistent with d_ino and st_ino values. An i_ino value inconsistent 974735c907dSAmir Goldstein * with d_ino also causes nfsd readdirplus to fail. 975695b46e7SAmir Goldstein */ 976695b46e7SAmir Goldstein inode->i_ino = ino; 977dfe51d47SAmir Goldstein if (ovl_same_fs(inode->i_sb)) { 978dfe51d47SAmir Goldstein return; 979dfe51d47SAmir Goldstein } else if (xinobits && likely(!(ino >> xinoshift))) { 980dfe51d47SAmir Goldstein inode->i_ino |= (unsigned long)fsid << (xinoshift + 1); 981dfe51d47SAmir Goldstein return; 982dfe51d47SAmir Goldstein } 983dfe51d47SAmir Goldstein 984dfe51d47SAmir Goldstein /* 985dfe51d47SAmir Goldstein * For directory inodes on non-samefs with xino disabled or xino 986dfe51d47SAmir Goldstein * overflow, we allocate a non-persistent inode number, to be used for 987dfe51d47SAmir Goldstein * resolving st_ino collisions in ovl_map_dev_ino(). 988dfe51d47SAmir Goldstein * 989dfe51d47SAmir Goldstein * To avoid ino collision with legitimate xino values from upper 990dfe51d47SAmir Goldstein * layer (fsid 0), use the lowest xinobit to map the non 991dfe51d47SAmir Goldstein * persistent inode numbers to the unified st_ino address space. 992dfe51d47SAmir Goldstein */ 993dfe51d47SAmir Goldstein if (S_ISDIR(inode->i_mode)) { 9944d314f78SAmir Goldstein ovl_next_ino(inode); 995dfe51d47SAmir Goldstein if (xinobits) { 996dfe51d47SAmir Goldstein inode->i_ino &= ~0UL >> xinobits; 997dfe51d47SAmir Goldstein inode->i_ino |= 1UL << xinoshift; 998dfe51d47SAmir Goldstein } 99912574a9fSAmir Goldstein } 100062c832edSAmir Goldstein } 100162c832edSAmir Goldstein 100262c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 100362c832edSAmir Goldstein unsigned long ino, int fsid) 100462c832edSAmir Goldstein { 100562c832edSAmir Goldstein struct inode *realinode; 1006ffa5723cSAmir Goldstein struct ovl_inode *oi = OVL_I(inode); 100762c832edSAmir Goldstein 100862c832edSAmir Goldstein if (oip->upperdentry) 1009ffa5723cSAmir Goldstein oi->__upperdentry = oip->upperdentry; 1010ffa5723cSAmir Goldstein if (oip->lowerpath && oip->lowerpath->dentry) { 1011ffa5723cSAmir Goldstein oi->lowerpath.dentry = dget(oip->lowerpath->dentry); 1012ffa5723cSAmir Goldstein oi->lowerpath.layer = oip->lowerpath->layer; 1013ffa5723cSAmir Goldstein } 101462c832edSAmir Goldstein if (oip->lowerdata) 1015ffa5723cSAmir Goldstein oi->lowerdata = igrab(d_inode(oip->lowerdata)); 101662c832edSAmir Goldstein 101762c832edSAmir Goldstein realinode = ovl_inode_real(inode); 10182878dffcSChristian Brauner ovl_copyattr(inode); 101962c832edSAmir Goldstein ovl_copyflags(realinode, inode); 102062c832edSAmir Goldstein ovl_map_ino(inode, ino, fsid); 102162c832edSAmir Goldstein } 102262c832edSAmir Goldstein 102362c832edSAmir Goldstein static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) 102462c832edSAmir Goldstein { 1025e9be9d5eSMiklos Szeredi inode->i_mode = mode; 1026d719e8f2SMiklos Szeredi inode->i_flags |= S_NOCMTIME; 10272a3a2a3fSMiklos Szeredi #ifdef CONFIG_FS_POSIX_ACL 10282a3a2a3fSMiklos Szeredi inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; 10292a3a2a3fSMiklos Szeredi #endif 1030e9be9d5eSMiklos Szeredi 1031b1eaa950SAmir Goldstein ovl_lockdep_annotate_inode_mutex_key(inode); 1032b1eaa950SAmir Goldstein 1033ca4c8a3aSMiklos Szeredi switch (mode & S_IFMT) { 1034ca4c8a3aSMiklos Szeredi case S_IFREG: 1035ca4c8a3aSMiklos Szeredi inode->i_op = &ovl_file_inode_operations; 1036d1d04ef8SMiklos Szeredi inode->i_fop = &ovl_file_operations; 10375b910bd6SAmir Goldstein inode->i_mapping->a_ops = &ovl_aops; 1038ca4c8a3aSMiklos Szeredi break; 1039ca4c8a3aSMiklos Szeredi 1040e9be9d5eSMiklos Szeredi case S_IFDIR: 1041e9be9d5eSMiklos Szeredi inode->i_op = &ovl_dir_inode_operations; 1042e9be9d5eSMiklos Szeredi inode->i_fop = &ovl_dir_operations; 1043e9be9d5eSMiklos Szeredi break; 1044e9be9d5eSMiklos Szeredi 1045e9be9d5eSMiklos Szeredi case S_IFLNK: 1046e9be9d5eSMiklos Szeredi inode->i_op = &ovl_symlink_inode_operations; 1047e9be9d5eSMiklos Szeredi break; 1048e9be9d5eSMiklos Szeredi 104951f7e52dSMiklos Szeredi default: 10509e142c41SMiklos Szeredi inode->i_op = &ovl_special_inode_operations; 1051ca4c8a3aSMiklos Szeredi init_special_inode(inode, mode, rdev); 1052e9be9d5eSMiklos Szeredi break; 105351f7e52dSMiklos Szeredi } 105451f7e52dSMiklos Szeredi } 1055e9be9d5eSMiklos Szeredi 10565f8415d6SAmir Goldstein /* 10575f8415d6SAmir Goldstein * With inodes index enabled, an overlay inode nlink counts the union of upper 10585f8415d6SAmir Goldstein * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure 10595f8415d6SAmir Goldstein * upper inode, the following nlink modifying operations can happen: 10605f8415d6SAmir Goldstein * 10615f8415d6SAmir Goldstein * 1. Lower hardlink copy up 10625f8415d6SAmir Goldstein * 2. Upper hardlink created, unlinked or renamed over 10635f8415d6SAmir Goldstein * 3. Lower hardlink whiteout or renamed over 10645f8415d6SAmir Goldstein * 10655f8415d6SAmir Goldstein * For the first, copy up case, the union nlink does not change, whether the 10665f8415d6SAmir Goldstein * operation succeeds or fails, but the upper inode nlink may change. 10675f8415d6SAmir Goldstein * Therefore, before copy up, we store the union nlink value relative to the 10682d2f2d73SMiklos Szeredi * lower inode nlink in the index inode xattr .overlay.nlink. 10695f8415d6SAmir Goldstein * 10705f8415d6SAmir Goldstein * For the second, upper hardlink case, the union nlink should be incremented 10715f8415d6SAmir Goldstein * or decremented IFF the operation succeeds, aligned with nlink change of the 10725f8415d6SAmir Goldstein * upper inode. Therefore, before link/unlink/rename, we store the union nlink 10735f8415d6SAmir Goldstein * value relative to the upper inode nlink in the index inode. 10745f8415d6SAmir Goldstein * 10755f8415d6SAmir Goldstein * For the last, lower cover up case, we simplify things by preceding the 10765f8415d6SAmir Goldstein * whiteout or cover up with copy up. This makes sure that there is an index 10775f8415d6SAmir Goldstein * upper inode where the nlink xattr can be stored before the copied up upper 10785f8415d6SAmir Goldstein * entry is unlink. 10795f8415d6SAmir Goldstein */ 10805f8415d6SAmir Goldstein #define OVL_NLINK_ADD_UPPER (1 << 0) 10815f8415d6SAmir Goldstein 10825f8415d6SAmir Goldstein /* 10835f8415d6SAmir Goldstein * On-disk format for indexed nlink: 10845f8415d6SAmir Goldstein * 10855f8415d6SAmir Goldstein * nlink relative to the upper inode - "U[+-]NUM" 10865f8415d6SAmir Goldstein * nlink relative to the lower inode - "L[+-]NUM" 10875f8415d6SAmir Goldstein */ 10885f8415d6SAmir Goldstein 10895f8415d6SAmir Goldstein static int ovl_set_nlink_common(struct dentry *dentry, 10905f8415d6SAmir Goldstein struct dentry *realdentry, const char *format) 10915f8415d6SAmir Goldstein { 10925f8415d6SAmir Goldstein struct inode *inode = d_inode(dentry); 10935f8415d6SAmir Goldstein struct inode *realinode = d_inode(realdentry); 10945f8415d6SAmir Goldstein char buf[13]; 10955f8415d6SAmir Goldstein int len; 10965f8415d6SAmir Goldstein 10975f8415d6SAmir Goldstein len = snprintf(buf, sizeof(buf), format, 10985f8415d6SAmir Goldstein (int) (inode->i_nlink - realinode->i_nlink)); 10995f8415d6SAmir Goldstein 11006787341aSMiklos Szeredi if (WARN_ON(len >= sizeof(buf))) 11016787341aSMiklos Szeredi return -EIO; 11026787341aSMiklos Szeredi 1103c914c0e2SAmir Goldstein return ovl_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry), 110426150ab5SMiklos Szeredi OVL_XATTR_NLINK, buf, len); 11055f8415d6SAmir Goldstein } 11065f8415d6SAmir Goldstein 11075f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry) 11085f8415d6SAmir Goldstein { 11095f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); 11105f8415d6SAmir Goldstein } 11115f8415d6SAmir Goldstein 11125f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry) 11135f8415d6SAmir Goldstein { 11145f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); 11155f8415d6SAmir Goldstein } 11165f8415d6SAmir Goldstein 1117610afc0bSMiklos Szeredi unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, 11185f8415d6SAmir Goldstein struct dentry *upperdentry, 11195f8415d6SAmir Goldstein unsigned int fallback) 11205f8415d6SAmir Goldstein { 11215f8415d6SAmir Goldstein int nlink_diff; 11225f8415d6SAmir Goldstein int nlink; 11235f8415d6SAmir Goldstein char buf[13]; 11245f8415d6SAmir Goldstein int err; 11255f8415d6SAmir Goldstein 11265f8415d6SAmir Goldstein if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) 11275f8415d6SAmir Goldstein return fallback; 11285f8415d6SAmir Goldstein 1129dad7017aSChristian Brauner err = ovl_getxattr_upper(ofs, upperdentry, OVL_XATTR_NLINK, 1130d5dc7486SMiklos Szeredi &buf, sizeof(buf) - 1); 11315f8415d6SAmir Goldstein if (err < 0) 11325f8415d6SAmir Goldstein goto fail; 11335f8415d6SAmir Goldstein 11345f8415d6SAmir Goldstein buf[err] = '\0'; 11355f8415d6SAmir Goldstein if ((buf[0] != 'L' && buf[0] != 'U') || 11365f8415d6SAmir Goldstein (buf[1] != '+' && buf[1] != '-')) 11375f8415d6SAmir Goldstein goto fail; 11385f8415d6SAmir Goldstein 11395f8415d6SAmir Goldstein err = kstrtoint(buf + 1, 10, &nlink_diff); 11405f8415d6SAmir Goldstein if (err < 0) 11415f8415d6SAmir Goldstein goto fail; 11425f8415d6SAmir Goldstein 11435f8415d6SAmir Goldstein nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; 11445f8415d6SAmir Goldstein nlink += nlink_diff; 11455f8415d6SAmir Goldstein 11465f8415d6SAmir Goldstein if (nlink <= 0) 11475f8415d6SAmir Goldstein goto fail; 11485f8415d6SAmir Goldstein 11495f8415d6SAmir Goldstein return nlink; 11505f8415d6SAmir Goldstein 11515f8415d6SAmir Goldstein fail: 11521bd0a3aeSlijiazi pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n", 11535f8415d6SAmir Goldstein upperdentry, err); 11545f8415d6SAmir Goldstein return fallback; 11555f8415d6SAmir Goldstein } 11565f8415d6SAmir Goldstein 1157ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) 115851f7e52dSMiklos Szeredi { 115951f7e52dSMiklos Szeredi struct inode *inode; 116051f7e52dSMiklos Szeredi 116151f7e52dSMiklos Szeredi inode = new_inode(sb); 116251f7e52dSMiklos Szeredi if (inode) 116362c832edSAmir Goldstein ovl_fill_inode(inode, mode, rdev); 116451f7e52dSMiklos Szeredi 116551f7e52dSMiklos Szeredi return inode; 116651f7e52dSMiklos Szeredi } 116751f7e52dSMiklos Szeredi 116851f7e52dSMiklos Szeredi static int ovl_inode_test(struct inode *inode, void *data) 116951f7e52dSMiklos Szeredi { 117025b7713aSMiklos Szeredi return inode->i_private == data; 117151f7e52dSMiklos Szeredi } 117251f7e52dSMiklos Szeredi 117351f7e52dSMiklos Szeredi static int ovl_inode_set(struct inode *inode, void *data) 117451f7e52dSMiklos Szeredi { 117525b7713aSMiklos Szeredi inode->i_private = data; 117651f7e52dSMiklos Szeredi return 0; 117751f7e52dSMiklos Szeredi } 117851f7e52dSMiklos Szeredi 1179b9ac5c27SMiklos Szeredi static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, 11804b91c30aSAmir Goldstein struct dentry *upperdentry, bool strict) 1181b9ac5c27SMiklos Szeredi { 11824b91c30aSAmir Goldstein /* 11834b91c30aSAmir Goldstein * For directories, @strict verify from lookup path performs consistency 11844b91c30aSAmir Goldstein * checks, so NULL lower/upper in dentry must match NULL lower/upper in 11854b91c30aSAmir Goldstein * inode. Non @strict verify from NFS handle decode path passes NULL for 11864b91c30aSAmir Goldstein * 'unknown' lower/upper. 11874b91c30aSAmir Goldstein */ 11884b91c30aSAmir Goldstein if (S_ISDIR(inode->i_mode) && strict) { 118931747edaSAmir Goldstein /* Real lower dir moved to upper layer under us? */ 119031747edaSAmir Goldstein if (!lowerdentry && ovl_inode_lower(inode)) 119131747edaSAmir Goldstein return false; 119231747edaSAmir Goldstein 119331747edaSAmir Goldstein /* Lookup of an uncovered redirect origin? */ 119431747edaSAmir Goldstein if (!upperdentry && ovl_inode_upper(inode)) 119531747edaSAmir Goldstein return false; 119631747edaSAmir Goldstein } 119731747edaSAmir Goldstein 1198939ae4efSAmir Goldstein /* 1199939ae4efSAmir Goldstein * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. 1200939ae4efSAmir Goldstein * This happens when finding a copied up overlay inode for a renamed 1201939ae4efSAmir Goldstein * or hardlinked overlay dentry and lower dentry cannot be followed 1202939ae4efSAmir Goldstein * by origin because lower fs does not support file handles. 1203939ae4efSAmir Goldstein */ 1204939ae4efSAmir Goldstein if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) 1205b9ac5c27SMiklos Szeredi return false; 1206b9ac5c27SMiklos Szeredi 1207b9ac5c27SMiklos Szeredi /* 1208b9ac5c27SMiklos Szeredi * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. 1209b9ac5c27SMiklos Szeredi * This happens when finding a lower alias for a copied up hard link. 1210b9ac5c27SMiklos Szeredi */ 1211b9ac5c27SMiklos Szeredi if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) 1212b9ac5c27SMiklos Szeredi return false; 1213b9ac5c27SMiklos Szeredi 1214b9ac5c27SMiklos Szeredi return true; 1215b9ac5c27SMiklos Szeredi } 1216b9ac5c27SMiklos Szeredi 12174b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 12184b91c30aSAmir Goldstein bool is_upper) 12199436a1a3SAmir Goldstein { 12204b91c30aSAmir Goldstein struct inode *inode, *key = d_inode(real); 12219436a1a3SAmir Goldstein 12229436a1a3SAmir Goldstein inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 12239436a1a3SAmir Goldstein if (!inode) 12249436a1a3SAmir Goldstein return NULL; 12259436a1a3SAmir Goldstein 12264b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, is_upper ? NULL : real, 12274b91c30aSAmir Goldstein is_upper ? real : NULL, false)) { 12289436a1a3SAmir Goldstein iput(inode); 12299436a1a3SAmir Goldstein return ERR_PTR(-ESTALE); 12309436a1a3SAmir Goldstein } 12319436a1a3SAmir Goldstein 12329436a1a3SAmir Goldstein return inode; 12339436a1a3SAmir Goldstein } 12349436a1a3SAmir Goldstein 1235146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir) 1236146d62e5SAmir Goldstein { 1237146d62e5SAmir Goldstein struct inode *key = d_inode(dir); 1238146d62e5SAmir Goldstein struct inode *trap; 1239146d62e5SAmir Goldstein bool res; 1240146d62e5SAmir Goldstein 1241146d62e5SAmir Goldstein trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 1242146d62e5SAmir Goldstein if (!trap) 1243146d62e5SAmir Goldstein return false; 1244146d62e5SAmir Goldstein 1245146d62e5SAmir Goldstein res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) && 1246146d62e5SAmir Goldstein !ovl_inode_lower(trap); 1247146d62e5SAmir Goldstein 1248146d62e5SAmir Goldstein iput(trap); 1249146d62e5SAmir Goldstein return res; 1250146d62e5SAmir Goldstein } 1251146d62e5SAmir Goldstein 1252146d62e5SAmir Goldstein /* 1253146d62e5SAmir Goldstein * Create an inode cache entry for layer root dir, that will intentionally 1254146d62e5SAmir Goldstein * fail ovl_verify_inode(), so any lookup that will find some layer root 1255146d62e5SAmir Goldstein * will fail. 1256146d62e5SAmir Goldstein */ 1257146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir) 1258146d62e5SAmir Goldstein { 1259146d62e5SAmir Goldstein struct inode *key = d_inode(dir); 1260146d62e5SAmir Goldstein struct inode *trap; 1261146d62e5SAmir Goldstein 1262146d62e5SAmir Goldstein if (!d_is_dir(dir)) 1263146d62e5SAmir Goldstein return ERR_PTR(-ENOTDIR); 1264146d62e5SAmir Goldstein 1265146d62e5SAmir Goldstein trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test, 1266146d62e5SAmir Goldstein ovl_inode_set, key); 1267146d62e5SAmir Goldstein if (!trap) 1268146d62e5SAmir Goldstein return ERR_PTR(-ENOMEM); 1269146d62e5SAmir Goldstein 1270146d62e5SAmir Goldstein if (!(trap->i_state & I_NEW)) { 1271146d62e5SAmir Goldstein /* Conflicting layer roots? */ 1272146d62e5SAmir Goldstein iput(trap); 1273146d62e5SAmir Goldstein return ERR_PTR(-ELOOP); 1274146d62e5SAmir Goldstein } 1275146d62e5SAmir Goldstein 1276146d62e5SAmir Goldstein trap->i_mode = S_IFDIR; 1277146d62e5SAmir Goldstein trap->i_flags = S_DEAD; 1278146d62e5SAmir Goldstein unlock_new_inode(trap); 1279146d62e5SAmir Goldstein 1280146d62e5SAmir Goldstein return trap; 1281146d62e5SAmir Goldstein } 1282146d62e5SAmir Goldstein 1283764baba8SAmir Goldstein /* 1284764baba8SAmir Goldstein * Does overlay inode need to be hashed by lower inode? 1285764baba8SAmir Goldstein */ 1286764baba8SAmir Goldstein static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, 128774c6e384SMiklos Szeredi struct dentry *lower, bool index) 1288764baba8SAmir Goldstein { 1289764baba8SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 1290764baba8SAmir Goldstein 1291764baba8SAmir Goldstein /* No, if pure upper */ 1292764baba8SAmir Goldstein if (!lower) 1293764baba8SAmir Goldstein return false; 1294764baba8SAmir Goldstein 1295764baba8SAmir Goldstein /* Yes, if already indexed */ 1296764baba8SAmir Goldstein if (index) 1297764baba8SAmir Goldstein return true; 1298764baba8SAmir Goldstein 1299764baba8SAmir Goldstein /* Yes, if won't be copied up */ 130008f4c7c8SMiklos Szeredi if (!ovl_upper_mnt(ofs)) 1301764baba8SAmir Goldstein return true; 1302764baba8SAmir Goldstein 1303764baba8SAmir Goldstein /* No, if lower hardlink is or will be broken on copy up */ 1304764baba8SAmir Goldstein if ((upper || !ovl_indexdir(sb)) && 1305764baba8SAmir Goldstein !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 1306764baba8SAmir Goldstein return false; 1307764baba8SAmir Goldstein 1308764baba8SAmir Goldstein /* No, if non-indexed upper with NFS export */ 1309764baba8SAmir Goldstein if (sb->s_export_op && upper) 1310764baba8SAmir Goldstein return false; 1311764baba8SAmir Goldstein 1312764baba8SAmir Goldstein /* Otherwise, hash by lower inode for fsnotify */ 1313764baba8SAmir Goldstein return true; 1314764baba8SAmir Goldstein } 1315764baba8SAmir Goldstein 131601b39dccSAmir Goldstein static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode, 131701b39dccSAmir Goldstein struct inode *key) 131801b39dccSAmir Goldstein { 131901b39dccSAmir Goldstein return newinode ? inode_insert5(newinode, (unsigned long) key, 132001b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key) : 132101b39dccSAmir Goldstein iget5_locked(sb, (unsigned long) key, 132201b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key); 132301b39dccSAmir Goldstein } 132401b39dccSAmir Goldstein 1325ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb, 1326ac6a52ebSVivek Goyal struct ovl_inode_params *oip) 132751f7e52dSMiklos Szeredi { 1328610afc0bSMiklos Szeredi struct ovl_fs *ofs = OVL_FS(sb); 1329ac6a52ebSVivek Goyal struct dentry *upperdentry = oip->upperdentry; 1330ac6a52ebSVivek Goyal struct ovl_path *lowerpath = oip->lowerpath; 133109d8b586SMiklos Szeredi struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; 133251f7e52dSMiklos Szeredi struct inode *inode; 133312574a9fSAmir Goldstein struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL; 1334dad7017aSChristian Brauner struct path realpath = { 1335dad7017aSChristian Brauner .dentry = upperdentry ?: lowerdentry, 1336dad7017aSChristian Brauner .mnt = upperdentry ? ovl_upper_mnt(ofs) : lowerpath->layer->mnt, 1337dad7017aSChristian Brauner }; 1338ac6a52ebSVivek Goyal bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, 1339ac6a52ebSVivek Goyal oip->index); 1340300b124fSAmir Goldstein int fsid = bylower ? lowerpath->layer->fsid : 0; 134128166ab3SVivek Goyal bool is_dir; 1342695b46e7SAmir Goldstein unsigned long ino = 0; 1343acf3062aSAmir Goldstein int err = oip->newinode ? -EEXIST : -ENOMEM; 13446eaf0111SAmir Goldstein 134509d8b586SMiklos Szeredi if (!realinode) 134609d8b586SMiklos Szeredi realinode = d_inode(lowerdentry); 134709d8b586SMiklos Szeredi 13486eaf0111SAmir Goldstein /* 1349764baba8SAmir Goldstein * Copy up origin (lower) may exist for non-indexed upper, but we must 1350764baba8SAmir Goldstein * not use lower as hash key if this is a broken hardlink. 13516eaf0111SAmir Goldstein */ 135231747edaSAmir Goldstein is_dir = S_ISDIR(realinode->i_mode); 1353764baba8SAmir Goldstein if (upperdentry || bylower) { 1354764baba8SAmir Goldstein struct inode *key = d_inode(bylower ? lowerdentry : 1355764baba8SAmir Goldstein upperdentry); 135631747edaSAmir Goldstein unsigned int nlink = is_dir ? 1 : realinode->i_nlink; 1357b9ac5c27SMiklos Szeredi 135801b39dccSAmir Goldstein inode = ovl_iget5(sb, oip->newinode, key); 135909d8b586SMiklos Szeredi if (!inode) 1360027065b7SVivek Goyal goto out_err; 136109d8b586SMiklos Szeredi if (!(inode->i_state & I_NEW)) { 1362b9ac5c27SMiklos Szeredi /* 1363b9ac5c27SMiklos Szeredi * Verify that the underlying files stored in the inode 1364b9ac5c27SMiklos Szeredi * match those in the dentry. 1365b9ac5c27SMiklos Szeredi */ 13664b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, lowerdentry, upperdentry, 13674b91c30aSAmir Goldstein true)) { 1368b9ac5c27SMiklos Szeredi iput(inode); 1369027065b7SVivek Goyal err = -ESTALE; 1370027065b7SVivek Goyal goto out_err; 1371b9ac5c27SMiklos Szeredi } 1372b9ac5c27SMiklos Szeredi 137309d8b586SMiklos Szeredi dput(upperdentry); 13749cec54c8SVivek Goyal kfree(oip->redirect); 137509d8b586SMiklos Szeredi goto out; 137609d8b586SMiklos Szeredi } 1377e9be9d5eSMiklos Szeredi 137831747edaSAmir Goldstein /* Recalculate nlink for non-dir due to indexing */ 137931747edaSAmir Goldstein if (!is_dir) 1380610afc0bSMiklos Szeredi nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry, 1381610afc0bSMiklos Szeredi nlink); 13825f8415d6SAmir Goldstein set_nlink(inode, nlink); 1383695b46e7SAmir Goldstein ino = key->i_ino; 1384e6d2ebddSMiklos Szeredi } else { 1385764baba8SAmir Goldstein /* Lower hardlink that will be broken on copy up */ 13860aceb53eSAmir Goldstein inode = new_inode(sb); 1387027065b7SVivek Goyal if (!inode) { 1388027065b7SVivek Goyal err = -ENOMEM; 1389027065b7SVivek Goyal goto out_err; 1390027065b7SVivek Goyal } 1391300b124fSAmir Goldstein ino = realinode->i_ino; 1392300b124fSAmir Goldstein fsid = lowerpath->layer->fsid; 1393e6d2ebddSMiklos Szeredi } 139462c832edSAmir Goldstein ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); 139562c832edSAmir Goldstein ovl_inode_init(inode, oip, ino, fsid); 139613c72075SMiklos Szeredi 1397610afc0bSMiklos Szeredi if (upperdentry && ovl_is_impuredir(sb, upperdentry)) 139813c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, inode); 139913c72075SMiklos Szeredi 1400ac6a52ebSVivek Goyal if (oip->index) 14010471a9cdSVivek Goyal ovl_set_flag(OVL_INDEX, inode); 14020471a9cdSVivek Goyal 14039cec54c8SVivek Goyal OVL_I(inode)->redirect = oip->redirect; 14049cec54c8SVivek Goyal 1405a00c2d59SVivek Goyal if (bylower) 1406a00c2d59SVivek Goyal ovl_set_flag(OVL_CONST_INO, inode); 1407a00c2d59SVivek Goyal 1408b79e05aaSAmir Goldstein /* Check for non-merge dir that may have whiteouts */ 140931747edaSAmir Goldstein if (is_dir) { 1410ac6a52ebSVivek Goyal if (((upperdentry && lowerdentry) || oip->numlower > 1) || 1411dad7017aSChristian Brauner ovl_path_check_origin_xattr(ofs, &realpath)) { 1412b79e05aaSAmir Goldstein ovl_set_flag(OVL_WHITEOUTS, inode); 1413b79e05aaSAmir Goldstein } 1414b79e05aaSAmir Goldstein } 1415b79e05aaSAmir Goldstein 1416096a218aSAmir Goldstein /* Check for immutable/append-only inode flags in xattr */ 1417096a218aSAmir Goldstein if (upperdentry) 1418096a218aSAmir Goldstein ovl_check_protattr(inode, upperdentry); 1419096a218aSAmir Goldstein 1420e6d2ebddSMiklos Szeredi if (inode->i_state & I_NEW) 1421e6d2ebddSMiklos Szeredi unlock_new_inode(inode); 1422e6d2ebddSMiklos Szeredi out: 1423e9be9d5eSMiklos Szeredi return inode; 1424b9ac5c27SMiklos Szeredi 1425027065b7SVivek Goyal out_err: 14261bd0a3aeSlijiazi pr_warn_ratelimited("failed to get inode (%i)\n", err); 1427027065b7SVivek Goyal inode = ERR_PTR(err); 1428b9ac5c27SMiklos Szeredi goto out; 1429e9be9d5eSMiklos Szeredi } 1430