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> 17e9be9d5eSMiklos Szeredi #include "overlayfs.h" 18e9be9d5eSMiklos Szeredi 19ba1e563cSChandan Rajendra 20549c7297SChristian Brauner int ovl_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 21549c7297SChristian Brauner struct iattr *attr) 22e9be9d5eSMiklos Szeredi { 23e9be9d5eSMiklos Szeredi int err; 24a15506eaSChristian Brauner struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 25997336f2SVivek Goyal bool full_copy_up = false; 26e9be9d5eSMiklos Szeredi struct dentry *upperdentry; 271175b6b8SVivek Goyal const struct cred *old_cred; 28e9be9d5eSMiklos Szeredi 292f221d6fSChristian Brauner err = setattr_prepare(&init_user_ns, dentry, attr); 30cf9a6784SMiklos Szeredi if (err) 31cf9a6784SMiklos Szeredi return err; 32cf9a6784SMiklos Szeredi 33e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 34e9be9d5eSMiklos Szeredi if (err) 35e9be9d5eSMiklos Szeredi goto out; 36e9be9d5eSMiklos Szeredi 375812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 38997336f2SVivek Goyal /* Truncate should trigger data copy up as well */ 39997336f2SVivek Goyal full_copy_up = true; 405812160eSMiklos Szeredi } 415812160eSMiklos Szeredi 42997336f2SVivek Goyal if (!full_copy_up) 43acff81ecSMiklos Szeredi err = ovl_copy_up(dentry); 44997336f2SVivek Goyal else 45997336f2SVivek Goyal err = ovl_copy_up_with_data(dentry); 46acff81ecSMiklos Szeredi if (!err) { 475812160eSMiklos Szeredi struct inode *winode = NULL; 485812160eSMiklos Szeredi 49e9be9d5eSMiklos Szeredi upperdentry = ovl_dentry_upper(dentry); 50acff81ecSMiklos Szeredi 515812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 525812160eSMiklos Szeredi winode = d_inode(upperdentry); 535812160eSMiklos Szeredi err = get_write_access(winode); 545812160eSMiklos Szeredi if (err) 555812160eSMiklos Szeredi goto out_drop_write; 565812160eSMiklos Szeredi } 575812160eSMiklos Szeredi 58b99c2d91SMiklos Szeredi if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) 59b99c2d91SMiklos Szeredi attr->ia_valid &= ~ATTR_MODE; 60b99c2d91SMiklos Szeredi 61e67f0216SVivek Goyal /* 6215fd2ea9SVivek Goyal * We might have to translate ovl file into real file object 6315fd2ea9SVivek Goyal * once use cases emerge. For now, simply don't let underlying 6415fd2ea9SVivek Goyal * filesystem rely on attr->ia_file 65e67f0216SVivek Goyal */ 66e67f0216SVivek Goyal attr->ia_valid &= ~ATTR_FILE; 67e67f0216SVivek Goyal 6815fd2ea9SVivek Goyal /* 6915fd2ea9SVivek Goyal * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN 7015fd2ea9SVivek Goyal * set. Overlayfs does not pass O_TRUNC flag to underlying 7115fd2ea9SVivek Goyal * filesystem during open -> do not pass ATTR_OPEN. This 7215fd2ea9SVivek Goyal * disables optimization in fuse which assumes open(O_TRUNC) 7315fd2ea9SVivek Goyal * already set file size to 0. But we never passed O_TRUNC to 7415fd2ea9SVivek Goyal * fuse. So by clearing ATTR_OPEN, fuse will be forced to send 7515fd2ea9SVivek Goyal * setattr request to server. 7615fd2ea9SVivek Goyal */ 7715fd2ea9SVivek Goyal attr->ia_valid &= ~ATTR_OPEN; 7815fd2ea9SVivek Goyal 795955102cSAl Viro inode_lock(upperdentry->d_inode); 801175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 81a15506eaSChristian Brauner err = ovl_do_notify_change(ofs, upperdentry, attr); 821175b6b8SVivek Goyal revert_creds(old_cred); 83b81de061SKonstantin Khlebnikov if (!err) 842878dffcSChristian Brauner ovl_copyattr(dentry->d_inode); 855955102cSAl Viro inode_unlock(upperdentry->d_inode); 865812160eSMiklos Szeredi 875812160eSMiklos Szeredi if (winode) 885812160eSMiklos Szeredi put_write_access(winode); 89e9be9d5eSMiklos Szeredi } 905812160eSMiklos Szeredi out_drop_write: 91e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 92e9be9d5eSMiklos Szeredi out: 93e9be9d5eSMiklos Szeredi return err; 94e9be9d5eSMiklos Szeredi } 95e9be9d5eSMiklos Szeredi 96c68e7ec5Syoungjun static void ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid) 97da309e8cSAmir Goldstein { 980f831ec8SAmir Goldstein bool samefs = ovl_same_fs(dentry->d_sb); 99e487d889SAmir Goldstein unsigned int xinobits = ovl_xino_bits(dentry->d_sb); 100dfe51d47SAmir Goldstein unsigned int xinoshift = 64 - xinobits; 101da309e8cSAmir Goldstein 102da309e8cSAmir Goldstein if (samefs) { 103da309e8cSAmir Goldstein /* 104da309e8cSAmir Goldstein * When all layers are on the same fs, all real inode 105da309e8cSAmir Goldstein * number are unique, so we use the overlay st_dev, 106da309e8cSAmir Goldstein * which is friendly to du -x. 107da309e8cSAmir Goldstein */ 108da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 109c68e7ec5Syoungjun return; 110e487d889SAmir Goldstein } else if (xinobits) { 111e487d889SAmir Goldstein /* 112e487d889SAmir Goldstein * All inode numbers of underlying fs should not be using the 113e487d889SAmir Goldstein * high xinobits, so we use high xinobits to partition the 114e487d889SAmir Goldstein * overlay st_ino address space. The high bits holds the fsid 115dfe51d47SAmir Goldstein * (upper fsid is 0). The lowest xinobit is reserved for mapping 116f48bbfb2SBhaskar Chowdhury * the non-persistent inode numbers range in case of overflow. 117dfe51d47SAmir Goldstein * This way all overlay inode numbers are unique and use the 118dfe51d47SAmir Goldstein * overlay st_dev. 119e487d889SAmir Goldstein */ 120926e94d7SAmir Goldstein if (likely(!(stat->ino >> xinoshift))) { 121dfe51d47SAmir Goldstein stat->ino |= ((u64)fsid) << (xinoshift + 1); 122e487d889SAmir Goldstein stat->dev = dentry->d_sb->s_dev; 123c68e7ec5Syoungjun return; 124926e94d7SAmir Goldstein } else if (ovl_xino_warn(dentry->d_sb)) { 125926e94d7SAmir Goldstein pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n", 126926e94d7SAmir Goldstein dentry, stat->ino, xinobits); 127e487d889SAmir Goldstein } 128e487d889SAmir Goldstein } 129e487d889SAmir Goldstein 130e487d889SAmir Goldstein /* The inode could not be mapped to a unified st_ino address space */ 131e487d889SAmir Goldstein if (S_ISDIR(dentry->d_inode->i_mode)) { 132da309e8cSAmir Goldstein /* 133da309e8cSAmir Goldstein * Always use the overlay st_dev for directories, so 'find 134da309e8cSAmir Goldstein * -xdev' will scan the entire overlay mount and won't cross the 135da309e8cSAmir Goldstein * overlay mount boundaries. 136da309e8cSAmir Goldstein * 137da309e8cSAmir Goldstein * If not all layers are on the same fs the pair {real st_ino; 138da309e8cSAmir Goldstein * overlay st_dev} is not unique, so use the non persistent 139da309e8cSAmir Goldstein * overlay st_ino for directories. 140da309e8cSAmir Goldstein */ 141da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 142da309e8cSAmir Goldstein stat->ino = dentry->d_inode->i_ino; 143b7bf9908SAmir Goldstein } else { 144da309e8cSAmir Goldstein /* 145da309e8cSAmir Goldstein * For non-samefs setup, if we cannot map all layers st_ino 146da309e8cSAmir Goldstein * to a unified address space, we need to make sure that st_dev 147b7bf9908SAmir Goldstein * is unique per underlying fs, so we use the unique anonymous 148b7bf9908SAmir Goldstein * bdev assigned to the underlying fs. 149da309e8cSAmir Goldstein */ 15007f1e596SAmir Goldstein stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev; 151da309e8cSAmir Goldstein } 152da309e8cSAmir Goldstein } 153da309e8cSAmir Goldstein 154549c7297SChristian Brauner int ovl_getattr(struct user_namespace *mnt_userns, const struct path *path, 155549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int flags) 156e9be9d5eSMiklos Szeredi { 157a528d35eSDavid Howells struct dentry *dentry = path->dentry; 15872b608f0SAmir Goldstein enum ovl_path_type type; 159e9be9d5eSMiklos Szeredi struct path realpath; 1601175b6b8SVivek Goyal const struct cred *old_cred; 161096a218aSAmir Goldstein struct inode *inode = d_inode(dentry); 162096a218aSAmir Goldstein bool is_dir = S_ISDIR(inode->i_mode); 16307f1e596SAmir Goldstein int fsid = 0; 1641175b6b8SVivek Goyal int err; 16567d756c2SVivek Goyal bool metacopy_blocks = false; 16667d756c2SVivek Goyal 16767d756c2SVivek Goyal metacopy_blocks = ovl_is_metacopy_dentry(dentry); 168e9be9d5eSMiklos Szeredi 16972b608f0SAmir Goldstein type = ovl_path_real(dentry, &realpath); 1701175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 171a528d35eSDavid Howells err = vfs_getattr(&realpath, stat, request_mask, flags); 17272b608f0SAmir Goldstein if (err) 17372b608f0SAmir Goldstein goto out; 17472b608f0SAmir Goldstein 175096a218aSAmir Goldstein /* Report the effective immutable/append-only STATX flags */ 176096a218aSAmir Goldstein generic_fill_statx_attr(inode, stat); 177096a218aSAmir Goldstein 17872b608f0SAmir Goldstein /* 179da309e8cSAmir Goldstein * For non-dir or same fs, we use st_ino of the copy up origin. 180da309e8cSAmir Goldstein * This guaranties constant st_dev/st_ino across copy up. 181e487d889SAmir Goldstein * With xino feature and non-samefs, we use st_ino of the copy up 182e487d889SAmir Goldstein * origin masked with high bits that represent the layer id. 18372b608f0SAmir Goldstein * 184da309e8cSAmir Goldstein * If lower filesystem supports NFS file handles, this also guaranties 18572b608f0SAmir Goldstein * persistent st_ino across mount cycle. 18672b608f0SAmir Goldstein */ 1870f831ec8SAmir Goldstein if (!is_dir || ovl_same_dev(dentry->d_sb)) { 188da309e8cSAmir Goldstein if (!OVL_TYPE_UPPER(type)) { 18907f1e596SAmir Goldstein fsid = ovl_layer_lower(dentry)->fsid; 190da309e8cSAmir Goldstein } else if (OVL_TYPE_ORIGIN(type)) { 19172b608f0SAmir Goldstein struct kstat lowerstat; 19267d756c2SVivek Goyal u32 lowermask = STATX_INO | STATX_BLOCKS | 19367d756c2SVivek Goyal (!is_dir ? STATX_NLINK : 0); 19472b608f0SAmir Goldstein 19572b608f0SAmir Goldstein ovl_path_lower(dentry, &realpath); 19672b608f0SAmir Goldstein err = vfs_getattr(&realpath, &lowerstat, 1975b712091SMiklos Szeredi lowermask, flags); 19872b608f0SAmir Goldstein if (err) 19972b608f0SAmir Goldstein goto out; 20072b608f0SAmir Goldstein 20172b608f0SAmir Goldstein /* 202359f392cSAmir Goldstein * Lower hardlinks may be broken on copy up to different 20372b608f0SAmir Goldstein * upper files, so we cannot use the lower origin st_ino 20472b608f0SAmir Goldstein * for those different files, even for the same fs case. 20586eaa130SAmir Goldstein * 20686eaa130SAmir Goldstein * Similarly, several redirected dirs can point to the 20786eaa130SAmir Goldstein * same dir on a lower layer. With the "verify_lower" 20886eaa130SAmir Goldstein * feature, we do not use the lower origin st_ino, if 20986eaa130SAmir Goldstein * we haven't verified that this redirect is unique. 21086eaa130SAmir Goldstein * 211359f392cSAmir Goldstein * With inodes index enabled, it is safe to use st_ino 21286eaa130SAmir Goldstein * of an indexed origin. The index validates that the 21386eaa130SAmir Goldstein * upper hardlink is not broken and that a redirected 21486eaa130SAmir Goldstein * dir is the only redirect to that origin. 21572b608f0SAmir Goldstein */ 21686eaa130SAmir Goldstein if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || 21786eaa130SAmir Goldstein (!ovl_verify_lower(dentry->d_sb) && 2189f99e50dSAmir Goldstein (is_dir || lowerstat.nlink == 1))) { 21907f1e596SAmir Goldstein fsid = ovl_layer_lower(dentry)->fsid; 2209c6d8f13SAmir Goldstein stat->ino = lowerstat.ino; 22172b608f0SAmir Goldstein } 22267d756c2SVivek Goyal 22367d756c2SVivek Goyal /* 22467d756c2SVivek Goyal * If we are querying a metacopy dentry and lower 22567d756c2SVivek Goyal * dentry is data dentry, then use the blocks we 22667d756c2SVivek Goyal * queried just now. We don't have to do additional 22767d756c2SVivek Goyal * vfs_getattr(). If lower itself is metacopy, then 22867d756c2SVivek Goyal * additional vfs_getattr() is unavoidable. 22967d756c2SVivek Goyal */ 23067d756c2SVivek Goyal if (metacopy_blocks && 23167d756c2SVivek Goyal realpath.dentry == ovl_dentry_lowerdata(dentry)) { 23267d756c2SVivek Goyal stat->blocks = lowerstat.blocks; 23367d756c2SVivek Goyal metacopy_blocks = false; 23467d756c2SVivek Goyal } 23567d756c2SVivek Goyal } 23667d756c2SVivek Goyal 23767d756c2SVivek Goyal if (metacopy_blocks) { 23867d756c2SVivek Goyal /* 23967d756c2SVivek Goyal * If lower is not same as lowerdata or if there was 24067d756c2SVivek Goyal * no origin on upper, we can end up here. 24167d756c2SVivek Goyal */ 24267d756c2SVivek Goyal struct kstat lowerdatastat; 24367d756c2SVivek Goyal u32 lowermask = STATX_BLOCKS; 24467d756c2SVivek Goyal 24567d756c2SVivek Goyal ovl_path_lowerdata(dentry, &realpath); 24667d756c2SVivek Goyal err = vfs_getattr(&realpath, &lowerdatastat, 24767d756c2SVivek Goyal lowermask, flags); 24867d756c2SVivek Goyal if (err) 24967d756c2SVivek Goyal goto out; 25067d756c2SVivek Goyal stat->blocks = lowerdatastat.blocks; 2519f99e50dSAmir Goldstein } 252a0c5ad30SAmir Goldstein } 253da309e8cSAmir Goldstein 254c68e7ec5Syoungjun ovl_map_dev_ino(dentry, stat, fsid); 2555b712091SMiklos Szeredi 2565b712091SMiklos Szeredi /* 2575b712091SMiklos Szeredi * It's probably not worth it to count subdirs to get the 2585b712091SMiklos Szeredi * correct link count. nlink=1 seems to pacify 'find' and 2595b712091SMiklos Szeredi * other utilities. 2605b712091SMiklos Szeredi */ 2615b712091SMiklos Szeredi if (is_dir && OVL_TYPE_MERGE(type)) 2625b712091SMiklos Szeredi stat->nlink = 1; 2635b712091SMiklos Szeredi 2645f8415d6SAmir Goldstein /* 2655f8415d6SAmir Goldstein * Return the overlay inode nlinks for indexed upper inodes. 2665f8415d6SAmir Goldstein * Overlay inode nlink counts the union of the upper hardlinks 2675f8415d6SAmir Goldstein * and non-covered lower hardlinks. It does not include the upper 2685f8415d6SAmir Goldstein * index hardlink. 2695f8415d6SAmir Goldstein */ 2705f8415d6SAmir Goldstein if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) 2715f8415d6SAmir Goldstein stat->nlink = dentry->d_inode->i_nlink; 2725f8415d6SAmir Goldstein 27372b608f0SAmir Goldstein out: 2741175b6b8SVivek Goyal revert_creds(old_cred); 27572b608f0SAmir Goldstein 2761175b6b8SVivek Goyal return err; 277e9be9d5eSMiklos Szeredi } 278e9be9d5eSMiklos Szeredi 279549c7297SChristian Brauner int ovl_permission(struct user_namespace *mnt_userns, 280549c7297SChristian Brauner struct inode *inode, int mask) 281e9be9d5eSMiklos Szeredi { 28209d8b586SMiklos Szeredi struct inode *upperinode = ovl_inode_upper(inode); 2834b7791b2SChristian Brauner struct inode *realinode; 2844b7791b2SChristian Brauner struct path realpath; 285c0ca3d70SVivek Goyal const struct cred *old_cred; 286e9be9d5eSMiklos Szeredi int err; 287e9be9d5eSMiklos Szeredi 288e9be9d5eSMiklos Szeredi /* Careful in RCU walk mode */ 2894b7791b2SChristian Brauner ovl_i_path_real(inode, &realpath); 2904b7791b2SChristian Brauner if (!realpath.dentry) { 291e9be9d5eSMiklos Szeredi WARN_ON(!(mask & MAY_NOT_BLOCK)); 292a999d7e1SMiklos Szeredi return -ECHILD; 293e9be9d5eSMiklos Szeredi } 294e9be9d5eSMiklos Szeredi 295c0ca3d70SVivek Goyal /* 296c0ca3d70SVivek Goyal * Check overlay inode with the creds of task and underlying inode 297c0ca3d70SVivek Goyal * with creds of mounter 298c0ca3d70SVivek Goyal */ 29947291baaSChristian Brauner err = generic_permission(&init_user_ns, inode, mask); 300c0ca3d70SVivek Goyal if (err) 301c0ca3d70SVivek Goyal return err; 302c0ca3d70SVivek Goyal 3034b7791b2SChristian Brauner realinode = d_inode(realpath.dentry); 304ec7ba118SMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 305ec7ba118SMiklos Szeredi if (!upperinode && 306ec7ba118SMiklos Szeredi !special_file(realinode->i_mode) && mask & MAY_WRITE) { 307754f8cb7SVivek Goyal mask &= ~(MAY_WRITE | MAY_APPEND); 308ec7ba118SMiklos Szeredi /* Make sure mounter can read file for copy up later */ 309500cac3cSVivek Goyal mask |= MAY_READ; 310500cac3cSVivek Goyal } 3114b7791b2SChristian Brauner err = inode_permission(mnt_user_ns(realpath.mnt), realinode, mask); 312c0ca3d70SVivek Goyal revert_creds(old_cred); 313c0ca3d70SVivek Goyal 314c0ca3d70SVivek Goyal return err; 315e9be9d5eSMiklos Szeredi } 316e9be9d5eSMiklos Szeredi 3176b255391SAl Viro static const char *ovl_get_link(struct dentry *dentry, 318fceef393SAl Viro struct inode *inode, 319fceef393SAl Viro struct delayed_call *done) 320e9be9d5eSMiklos Szeredi { 3211175b6b8SVivek Goyal const struct cred *old_cred; 3221175b6b8SVivek Goyal const char *p; 323e9be9d5eSMiklos Szeredi 3246b255391SAl Viro if (!dentry) 3256b255391SAl Viro return ERR_PTR(-ECHILD); 3266b255391SAl Viro 3271175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3287764235bSMiklos Szeredi p = vfs_get_link(ovl_dentry_real(dentry), done); 3291175b6b8SVivek Goyal revert_creds(old_cred); 3301175b6b8SVivek Goyal return p; 331e9be9d5eSMiklos Szeredi } 332e9be9d5eSMiklos Szeredi 333610afc0bSMiklos Szeredi bool ovl_is_private_xattr(struct super_block *sb, const char *name) 334e9be9d5eSMiklos Szeredi { 3352d2f2d73SMiklos Szeredi struct ovl_fs *ofs = sb->s_fs_info; 3362d2f2d73SMiklos Szeredi 3372d2f2d73SMiklos Szeredi if (ofs->config.userxattr) 3382d2f2d73SMiklos Szeredi return strncmp(name, OVL_XATTR_USER_PREFIX, 3392d2f2d73SMiklos Szeredi sizeof(OVL_XATTR_USER_PREFIX) - 1) == 0; 3402d2f2d73SMiklos Szeredi else 3412d2f2d73SMiklos Szeredi return strncmp(name, OVL_XATTR_TRUSTED_PREFIX, 3422d2f2d73SMiklos Szeredi sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1) == 0; 343e9be9d5eSMiklos Szeredi } 344e9be9d5eSMiklos Szeredi 3451d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 3461d88f183SMiklos Szeredi const void *value, size_t size, int flags) 347e9be9d5eSMiklos Szeredi { 348e9be9d5eSMiklos Szeredi int err; 349c914c0e2SAmir Goldstein struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 3501d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 3511d88f183SMiklos Szeredi struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); 3528bc0095dSChristian Brauner struct path realpath; 3531175b6b8SVivek Goyal const struct cred *old_cred; 354e9be9d5eSMiklos Szeredi 355e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 356e9be9d5eSMiklos Szeredi if (err) 357e9be9d5eSMiklos Szeredi goto out; 358e9be9d5eSMiklos Szeredi 3591d88f183SMiklos Szeredi if (!value && !upperdentry) { 3608bc0095dSChristian Brauner ovl_path_lower(dentry, &realpath); 361554677b9SMiklos Szeredi old_cred = ovl_override_creds(dentry->d_sb); 3628bc0095dSChristian Brauner err = vfs_getxattr(mnt_user_ns(realpath.mnt), realdentry, name, NULL, 0); 363554677b9SMiklos Szeredi revert_creds(old_cred); 3640e585cccSAndreas Gruenbacher if (err < 0) 3650e585cccSAndreas Gruenbacher goto out_drop_write; 3660e585cccSAndreas Gruenbacher } 3670e585cccSAndreas Gruenbacher 3681d88f183SMiklos Szeredi if (!upperdentry) { 369e9be9d5eSMiklos Szeredi err = ovl_copy_up(dentry); 370e9be9d5eSMiklos Szeredi if (err) 371e9be9d5eSMiklos Szeredi goto out_drop_write; 372e9be9d5eSMiklos Szeredi 3731d88f183SMiklos Szeredi realdentry = ovl_dentry_upper(dentry); 3741d88f183SMiklos Szeredi } 3750e585cccSAndreas Gruenbacher 3761175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 377c914c0e2SAmir Goldstein if (value) { 378c914c0e2SAmir Goldstein err = ovl_do_setxattr(ofs, realdentry, name, value, size, 379c7c7a1a1STycho Andersen flags); 380c914c0e2SAmir Goldstein } else { 3810e585cccSAndreas Gruenbacher WARN_ON(flags != XATTR_REPLACE); 382c914c0e2SAmir Goldstein err = ovl_do_removexattr(ofs, realdentry, name); 3830e585cccSAndreas Gruenbacher } 3841175b6b8SVivek Goyal revert_creds(old_cred); 385e9be9d5eSMiklos Szeredi 386d9854c87SMiklos Szeredi /* copy c/mtime */ 3872878dffcSChristian Brauner ovl_copyattr(inode); 388d9854c87SMiklos Szeredi 389e9be9d5eSMiklos Szeredi out_drop_write: 390e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 391e9be9d5eSMiklos Szeredi out: 392e9be9d5eSMiklos Szeredi return err; 393e9be9d5eSMiklos Szeredi } 394e9be9d5eSMiklos Szeredi 3951d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 3960eb45fc3SAndreas Gruenbacher void *value, size_t size) 397e9be9d5eSMiklos Szeredi { 3981175b6b8SVivek Goyal ssize_t res; 3991175b6b8SVivek Goyal const struct cred *old_cred; 4008bc0095dSChristian Brauner struct path realpath; 40152148463SMiklos Szeredi 4028bc0095dSChristian Brauner ovl_i_path_real(inode, &realpath); 4031175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 4048bc0095dSChristian Brauner res = vfs_getxattr(mnt_user_ns(realpath.mnt), realpath.dentry, name, value, size); 4051175b6b8SVivek Goyal revert_creds(old_cred); 4061175b6b8SVivek Goyal return res; 407e9be9d5eSMiklos Szeredi } 408e9be9d5eSMiklos Szeredi 409610afc0bSMiklos Szeredi static bool ovl_can_list(struct super_block *sb, const char *s) 410a082c6f6SMiklos Szeredi { 4118f6ee74cSMiklos Szeredi /* Never list private (.overlay) */ 4128f6ee74cSMiklos Szeredi if (ovl_is_private_xattr(sb, s)) 4138f6ee74cSMiklos Szeredi return false; 4148f6ee74cSMiklos Szeredi 415f48bbfb2SBhaskar Chowdhury /* List all non-trusted xattrs */ 416a082c6f6SMiklos Szeredi if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) 417a082c6f6SMiklos Szeredi return true; 418a082c6f6SMiklos Szeredi 4198f6ee74cSMiklos Szeredi /* list other trusted for superuser only */ 4208f6ee74cSMiklos Szeredi return ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN); 421a082c6f6SMiklos Szeredi } 422a082c6f6SMiklos Szeredi 423e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) 424e9be9d5eSMiklos Szeredi { 425b581755bSMiklos Szeredi struct dentry *realdentry = ovl_dentry_real(dentry); 426e9be9d5eSMiklos Szeredi ssize_t res; 4277cb35119SMiklos Szeredi size_t len; 4287cb35119SMiklos Szeredi char *s; 4291175b6b8SVivek Goyal const struct cred *old_cred; 430e9be9d5eSMiklos Szeredi 4311175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 432b581755bSMiklos Szeredi res = vfs_listxattr(realdentry, list, size); 4331175b6b8SVivek Goyal revert_creds(old_cred); 434e9be9d5eSMiklos Szeredi if (res <= 0 || size == 0) 435e9be9d5eSMiklos Szeredi return res; 436e9be9d5eSMiklos Szeredi 437e9be9d5eSMiklos Szeredi /* filter out private xattrs */ 4387cb35119SMiklos Szeredi for (s = list, len = res; len;) { 4397cb35119SMiklos Szeredi size_t slen = strnlen(s, len) + 1; 440e9be9d5eSMiklos Szeredi 4417cb35119SMiklos Szeredi /* underlying fs providing us with an broken xattr list? */ 4427cb35119SMiklos Szeredi if (WARN_ON(slen > len)) 4437cb35119SMiklos Szeredi return -EIO; 444e9be9d5eSMiklos Szeredi 4457cb35119SMiklos Szeredi len -= slen; 446610afc0bSMiklos Szeredi if (!ovl_can_list(dentry->d_sb, s)) { 447e9be9d5eSMiklos Szeredi res -= slen; 4487cb35119SMiklos Szeredi memmove(s, s + slen, len); 449e9be9d5eSMiklos Szeredi } else { 4507cb35119SMiklos Szeredi s += slen; 451e9be9d5eSMiklos Szeredi } 452e9be9d5eSMiklos Szeredi } 453e9be9d5eSMiklos Szeredi 454e9be9d5eSMiklos Szeredi return res; 455e9be9d5eSMiklos Szeredi } 456e9be9d5eSMiklos Szeredi 457ded53656SYang Xu #ifdef CONFIG_FS_POSIX_ACL 4581aa5fef5SChristian Brauner /* 4591aa5fef5SChristian Brauner * Apply the idmapping of the layer to POSIX ACLs. The caller must pass a clone 4601aa5fef5SChristian Brauner * of the POSIX ACLs retrieved from the lower layer to this function to not 4611aa5fef5SChristian Brauner * alter the POSIX ACLs for the underlying filesystem. 4621aa5fef5SChristian Brauner */ 4631aa5fef5SChristian Brauner static void ovl_idmap_posix_acl(struct user_namespace *mnt_userns, 4641aa5fef5SChristian Brauner struct posix_acl *acl) 4651aa5fef5SChristian Brauner { 4661aa5fef5SChristian Brauner for (unsigned int i = 0; i < acl->a_count; i++) { 4671aa5fef5SChristian Brauner vfsuid_t vfsuid; 4681aa5fef5SChristian Brauner vfsgid_t vfsgid; 4691aa5fef5SChristian Brauner 4701aa5fef5SChristian Brauner struct posix_acl_entry *e = &acl->a_entries[i]; 4711aa5fef5SChristian Brauner switch (e->e_tag) { 4721aa5fef5SChristian Brauner case ACL_USER: 4731aa5fef5SChristian Brauner vfsuid = make_vfsuid(mnt_userns, &init_user_ns, e->e_uid); 4741aa5fef5SChristian Brauner e->e_uid = vfsuid_into_kuid(vfsuid); 4751aa5fef5SChristian Brauner break; 4761aa5fef5SChristian Brauner case ACL_GROUP: 4771aa5fef5SChristian Brauner vfsgid = make_vfsgid(mnt_userns, &init_user_ns, e->e_gid); 4781aa5fef5SChristian Brauner e->e_gid = vfsgid_into_kgid(vfsgid); 4791aa5fef5SChristian Brauner break; 4801aa5fef5SChristian Brauner } 4811aa5fef5SChristian Brauner } 4821aa5fef5SChristian Brauner } 4831aa5fef5SChristian Brauner 4841aa5fef5SChristian Brauner /* 4851aa5fef5SChristian Brauner * When the relevant layer is an idmapped mount we need to take the idmapping 4861aa5fef5SChristian Brauner * of the layer into account and translate any ACL_{GROUP,USER} values 4871aa5fef5SChristian Brauner * according to the idmapped mount. 4881aa5fef5SChristian Brauner * 4891aa5fef5SChristian Brauner * We cannot alter the ACLs returned from the relevant layer as that would 4901aa5fef5SChristian Brauner * alter the cached values filesystem wide for the lower filesystem. Instead we 4911aa5fef5SChristian Brauner * can clone the ACLs and then apply the relevant idmapping of the layer. 4921aa5fef5SChristian Brauner * 4931aa5fef5SChristian Brauner * This is obviously only relevant when idmapped layers are used. 4941aa5fef5SChristian Brauner */ 4950cad6246SMiklos Szeredi struct posix_acl *ovl_get_acl(struct inode *inode, int type, bool rcu) 49639a25b2bSVivek Goyal { 49709d8b586SMiklos Szeredi struct inode *realinode = ovl_inode_real(inode); 4981aa5fef5SChristian Brauner struct posix_acl *acl, *clone; 4991aa5fef5SChristian Brauner struct path realpath; 50039a25b2bSVivek Goyal 501ded53656SYang Xu if (!IS_POSIXACL(realinode)) 50239a25b2bSVivek Goyal return NULL; 50339a25b2bSVivek Goyal 5041aa5fef5SChristian Brauner /* Careful in RCU walk mode */ 5051aa5fef5SChristian Brauner ovl_i_path_real(inode, &realpath); 5061aa5fef5SChristian Brauner if (!realpath.dentry) { 5071aa5fef5SChristian Brauner WARN_ON(!rcu); 5081aa5fef5SChristian Brauner return ERR_PTR(-ECHILD); 5091aa5fef5SChristian Brauner } 5101aa5fef5SChristian Brauner 5111aa5fef5SChristian Brauner if (rcu) { 5121aa5fef5SChristian Brauner acl = get_cached_acl_rcu(realinode, type); 5131aa5fef5SChristian Brauner } else { 5141aa5fef5SChristian Brauner const struct cred *old_cred; 515332f606bSMiklos Szeredi 5161175b6b8SVivek Goyal old_cred = ovl_override_creds(inode->i_sb); 5175201dc44SMiklos Szeredi acl = get_acl(realinode, type); 5181175b6b8SVivek Goyal revert_creds(old_cred); 5191aa5fef5SChristian Brauner } 5201aa5fef5SChristian Brauner /* 5211aa5fef5SChristian Brauner * If there are no POSIX ACLs, or we encountered an error, 5221aa5fef5SChristian Brauner * or the layer isn't idmapped we don't need to do anything. 5231aa5fef5SChristian Brauner */ 5241aa5fef5SChristian Brauner if (!is_idmapped_mnt(realpath.mnt) || IS_ERR_OR_NULL(acl)) 5251175b6b8SVivek Goyal return acl; 5261aa5fef5SChristian Brauner 5271aa5fef5SChristian Brauner /* 5281aa5fef5SChristian Brauner * We only get here if the layer is idmapped. So drop out of RCU path 5291aa5fef5SChristian Brauner * walk so we can clone the ACLs. There's no need to release the ACLs 5301aa5fef5SChristian Brauner * since get_cached_acl_rcu() doesn't take a reference on the ACLs. 5311aa5fef5SChristian Brauner */ 5321aa5fef5SChristian Brauner if (rcu) 5331aa5fef5SChristian Brauner return ERR_PTR(-ECHILD); 5341aa5fef5SChristian Brauner 5351aa5fef5SChristian Brauner clone = posix_acl_clone(acl, GFP_KERNEL); 5361aa5fef5SChristian Brauner if (!clone) 5371aa5fef5SChristian Brauner clone = ERR_PTR(-ENOMEM); 5381aa5fef5SChristian Brauner else 5391aa5fef5SChristian Brauner ovl_idmap_posix_acl(mnt_user_ns(realpath.mnt), clone); 5401aa5fef5SChristian Brauner /* 5411aa5fef5SChristian Brauner * Since we're not in RCU path walk we always need to release the 5421aa5fef5SChristian Brauner * original ACLs. 5431aa5fef5SChristian Brauner */ 5441aa5fef5SChristian Brauner posix_acl_release(acl); 5451aa5fef5SChristian Brauner return clone; 54639a25b2bSVivek Goyal } 547ded53656SYang Xu #endif 54839a25b2bSVivek Goyal 54995582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags) 550d719e8f2SMiklos Szeredi { 5518f35cf51SMiklos Szeredi if (flags & S_ATIME) { 5528f35cf51SMiklos Szeredi struct ovl_fs *ofs = inode->i_sb->s_fs_info; 5538f35cf51SMiklos Szeredi struct path upperpath = { 55408f4c7c8SMiklos Szeredi .mnt = ovl_upper_mnt(ofs), 5558f35cf51SMiklos Szeredi .dentry = ovl_upperdentry_dereference(OVL_I(inode)), 5568f35cf51SMiklos Szeredi }; 557d719e8f2SMiklos Szeredi 558d719e8f2SMiklos Szeredi if (upperpath.dentry) { 559d719e8f2SMiklos Szeredi touch_atime(&upperpath); 560d719e8f2SMiklos Szeredi inode->i_atime = d_inode(upperpath.dentry)->i_atime; 561d719e8f2SMiklos Szeredi } 5628f35cf51SMiklos Szeredi } 563d719e8f2SMiklos Szeredi return 0; 564d719e8f2SMiklos Szeredi } 565d719e8f2SMiklos Szeredi 5669e142c41SMiklos Szeredi static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 5679e142c41SMiklos Szeredi u64 start, u64 len) 5689e142c41SMiklos Szeredi { 5699e142c41SMiklos Szeredi int err; 570c11faf32SChengguang Xu struct inode *realinode = ovl_inode_realdata(inode); 5719e142c41SMiklos Szeredi const struct cred *old_cred; 5729e142c41SMiklos Szeredi 5739e142c41SMiklos Szeredi if (!realinode->i_op->fiemap) 5749e142c41SMiklos Szeredi return -EOPNOTSUPP; 5759e142c41SMiklos Szeredi 5769e142c41SMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 5779e142c41SMiklos Szeredi err = realinode->i_op->fiemap(realinode, fieinfo, start, len); 5789e142c41SMiklos Szeredi revert_creds(old_cred); 5799e142c41SMiklos Szeredi 5809e142c41SMiklos Szeredi return err; 5819e142c41SMiklos Szeredi } 5829e142c41SMiklos Szeredi 58366dbfabfSMiklos Szeredi /* 58466dbfabfSMiklos Szeredi * Work around the fact that security_file_ioctl() takes a file argument. 58566dbfabfSMiklos Szeredi * Introducing security_inode_fileattr_get/set() hooks would solve this issue 58666dbfabfSMiklos Szeredi * properly. 58766dbfabfSMiklos Szeredi */ 588*2d343087SAl Viro static int ovl_security_fileattr(const struct path *realpath, struct fileattr *fa, 58966dbfabfSMiklos Szeredi bool set) 59066dbfabfSMiklos Szeredi { 59166dbfabfSMiklos Szeredi struct file *file; 59266dbfabfSMiklos Szeredi unsigned int cmd; 59366dbfabfSMiklos Szeredi int err; 59466dbfabfSMiklos Szeredi 59572db8211SAmir Goldstein file = dentry_open(realpath, O_RDONLY, current_cred()); 59666dbfabfSMiklos Szeredi if (IS_ERR(file)) 59766dbfabfSMiklos Szeredi return PTR_ERR(file); 59866dbfabfSMiklos Szeredi 59966dbfabfSMiklos Szeredi if (set) 60066dbfabfSMiklos Szeredi cmd = fa->fsx_valid ? FS_IOC_FSSETXATTR : FS_IOC_SETFLAGS; 60166dbfabfSMiklos Szeredi else 60266dbfabfSMiklos Szeredi cmd = fa->fsx_valid ? FS_IOC_FSGETXATTR : FS_IOC_GETFLAGS; 60366dbfabfSMiklos Szeredi 60466dbfabfSMiklos Szeredi err = security_file_ioctl(file, cmd, 0); 60566dbfabfSMiklos Szeredi fput(file); 60666dbfabfSMiklos Szeredi 60766dbfabfSMiklos Szeredi return err; 60866dbfabfSMiklos Szeredi } 60966dbfabfSMiklos Szeredi 610*2d343087SAl Viro int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa) 61172db8211SAmir Goldstein { 61272db8211SAmir Goldstein int err; 61372db8211SAmir Goldstein 61472db8211SAmir Goldstein err = ovl_security_fileattr(realpath, fa, true); 61572db8211SAmir Goldstein if (err) 61672db8211SAmir Goldstein return err; 61772db8211SAmir Goldstein 61850db8d02SChristian Brauner return vfs_fileattr_set(mnt_user_ns(realpath->mnt), realpath->dentry, fa); 61972db8211SAmir Goldstein } 62072db8211SAmir Goldstein 62166dbfabfSMiklos Szeredi int ovl_fileattr_set(struct user_namespace *mnt_userns, 62266dbfabfSMiklos Szeredi struct dentry *dentry, struct fileattr *fa) 62366dbfabfSMiklos Szeredi { 62466dbfabfSMiklos Szeredi struct inode *inode = d_inode(dentry); 62572db8211SAmir Goldstein struct path upperpath; 62666dbfabfSMiklos Szeredi const struct cred *old_cred; 627096a218aSAmir Goldstein unsigned int flags; 62866dbfabfSMiklos Szeredi int err; 62966dbfabfSMiklos Szeredi 63066dbfabfSMiklos Szeredi err = ovl_want_write(dentry); 63166dbfabfSMiklos Szeredi if (err) 63266dbfabfSMiklos Szeredi goto out; 63366dbfabfSMiklos Szeredi 63466dbfabfSMiklos Szeredi err = ovl_copy_up(dentry); 63566dbfabfSMiklos Szeredi if (!err) { 63672db8211SAmir Goldstein ovl_path_real(dentry, &upperpath); 63766dbfabfSMiklos Szeredi 63866dbfabfSMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 639096a218aSAmir Goldstein /* 640096a218aSAmir Goldstein * Store immutable/append-only flags in xattr and clear them 641096a218aSAmir Goldstein * in upper fileattr (in case they were set by older kernel) 642096a218aSAmir Goldstein * so children of "ovl-immutable" directories lower aliases of 643096a218aSAmir Goldstein * "ovl-immutable" hardlinks could be copied up. 644096a218aSAmir Goldstein * Clear xattr when flags are cleared. 645096a218aSAmir Goldstein */ 646096a218aSAmir Goldstein err = ovl_set_protattr(inode, upperpath.dentry, fa); 647096a218aSAmir Goldstein if (!err) 64872db8211SAmir Goldstein err = ovl_real_fileattr_set(&upperpath, fa); 64966dbfabfSMiklos Szeredi revert_creds(old_cred); 650096a218aSAmir Goldstein 651096a218aSAmir Goldstein /* 652096a218aSAmir Goldstein * Merge real inode flags with inode flags read from 653096a218aSAmir Goldstein * overlay.protattr xattr 654096a218aSAmir Goldstein */ 655096a218aSAmir Goldstein flags = ovl_inode_real(inode)->i_flags & OVL_COPY_I_FLAGS_MASK; 656096a218aSAmir Goldstein 657096a218aSAmir Goldstein BUILD_BUG_ON(OVL_PROT_I_FLAGS_MASK & ~OVL_COPY_I_FLAGS_MASK); 658096a218aSAmir Goldstein flags |= inode->i_flags & OVL_PROT_I_FLAGS_MASK; 659096a218aSAmir Goldstein inode_set_flags(inode, flags, OVL_COPY_I_FLAGS_MASK); 660d8991e86SChengguang Xu 661d8991e86SChengguang Xu /* Update ctime */ 6622878dffcSChristian Brauner ovl_copyattr(inode); 66366dbfabfSMiklos Szeredi } 66466dbfabfSMiklos Szeredi ovl_drop_write(dentry); 66566dbfabfSMiklos Szeredi out: 66666dbfabfSMiklos Szeredi return err; 66766dbfabfSMiklos Szeredi } 66866dbfabfSMiklos Szeredi 669096a218aSAmir Goldstein /* Convert inode protection flags to fileattr flags */ 670096a218aSAmir Goldstein static void ovl_fileattr_prot_flags(struct inode *inode, struct fileattr *fa) 671096a218aSAmir Goldstein { 672096a218aSAmir Goldstein BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL); 673096a218aSAmir Goldstein BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON); 674096a218aSAmir Goldstein 675096a218aSAmir Goldstein if (inode->i_flags & S_APPEND) { 676096a218aSAmir Goldstein fa->flags |= FS_APPEND_FL; 677096a218aSAmir Goldstein fa->fsx_xflags |= FS_XFLAG_APPEND; 678096a218aSAmir Goldstein } 679096a218aSAmir Goldstein if (inode->i_flags & S_IMMUTABLE) { 680096a218aSAmir Goldstein fa->flags |= FS_IMMUTABLE_FL; 681096a218aSAmir Goldstein fa->fsx_xflags |= FS_XFLAG_IMMUTABLE; 682096a218aSAmir Goldstein } 683096a218aSAmir Goldstein } 684096a218aSAmir Goldstein 685*2d343087SAl Viro int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa) 68672db8211SAmir Goldstein { 68772db8211SAmir Goldstein int err; 68872db8211SAmir Goldstein 68972db8211SAmir Goldstein err = ovl_security_fileattr(realpath, fa, false); 69072db8211SAmir Goldstein if (err) 69172db8211SAmir Goldstein return err; 69272db8211SAmir Goldstein 6935b0a414dSMiklos Szeredi err = vfs_fileattr_get(realpath->dentry, fa); 6945b0a414dSMiklos Szeredi if (err == -ENOIOCTLCMD) 6955b0a414dSMiklos Szeredi err = -ENOTTY; 6965b0a414dSMiklos Szeredi return err; 69772db8211SAmir Goldstein } 69872db8211SAmir Goldstein 69966dbfabfSMiklos Szeredi int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa) 70066dbfabfSMiklos Szeredi { 70166dbfabfSMiklos Szeredi struct inode *inode = d_inode(dentry); 70272db8211SAmir Goldstein struct path realpath; 70366dbfabfSMiklos Szeredi const struct cred *old_cred; 70466dbfabfSMiklos Szeredi int err; 70566dbfabfSMiklos Szeredi 70672db8211SAmir Goldstein ovl_path_real(dentry, &realpath); 70772db8211SAmir Goldstein 70866dbfabfSMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 70972db8211SAmir Goldstein err = ovl_real_fileattr_get(&realpath, fa); 710096a218aSAmir Goldstein ovl_fileattr_prot_flags(inode, fa); 71166dbfabfSMiklos Szeredi revert_creds(old_cred); 71266dbfabfSMiklos Szeredi 71366dbfabfSMiklos Szeredi return err; 71466dbfabfSMiklos Szeredi } 71566dbfabfSMiklos Szeredi 716e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_file_inode_operations = { 717e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 718e9be9d5eSMiklos Szeredi .permission = ovl_permission, 719e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 720e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 72139a25b2bSVivek Goyal .get_acl = ovl_get_acl, 722d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 7239e142c41SMiklos Szeredi .fiemap = ovl_fiemap, 72466dbfabfSMiklos Szeredi .fileattr_get = ovl_fileattr_get, 72566dbfabfSMiklos Szeredi .fileattr_set = ovl_fileattr_set, 726e9be9d5eSMiklos Szeredi }; 727e9be9d5eSMiklos Szeredi 728e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_symlink_inode_operations = { 729e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 7306b255391SAl Viro .get_link = ovl_get_link, 731e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 732e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 733d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 734e9be9d5eSMiklos Szeredi }; 735e9be9d5eSMiklos Szeredi 7369e142c41SMiklos Szeredi static const struct inode_operations ovl_special_inode_operations = { 7379e142c41SMiklos Szeredi .setattr = ovl_setattr, 7389e142c41SMiklos Szeredi .permission = ovl_permission, 7399e142c41SMiklos Szeredi .getattr = ovl_getattr, 7409e142c41SMiklos Szeredi .listxattr = ovl_listxattr, 7419e142c41SMiklos Szeredi .get_acl = ovl_get_acl, 7429e142c41SMiklos Szeredi .update_time = ovl_update_time, 7439e142c41SMiklos Szeredi }; 7449e142c41SMiklos Szeredi 74569383c59SWei Yongjun static const struct address_space_operations ovl_aops = { 7465b910bd6SAmir Goldstein /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */ 7475b910bd6SAmir Goldstein .direct_IO = noop_direct_IO, 7485b910bd6SAmir Goldstein }; 7495b910bd6SAmir Goldstein 750b1eaa950SAmir Goldstein /* 751b1eaa950SAmir Goldstein * It is possible to stack overlayfs instance on top of another 752a5a84682SChengguang Xu * overlayfs instance as lower layer. We need to annotate the 753b1eaa950SAmir Goldstein * stackable i_mutex locks according to stack level of the super 754b1eaa950SAmir Goldstein * block instance. An overlayfs instance can never be in stack 755b1eaa950SAmir Goldstein * depth 0 (there is always a real fs below it). An overlayfs 756f48bbfb2SBhaskar Chowdhury * inode lock will use the lockdep annotation ovl_i_mutex_key[depth]. 757b1eaa950SAmir Goldstein * 758b1eaa950SAmir Goldstein * For example, here is a snip from /proc/lockdep_chains after 759b1eaa950SAmir Goldstein * dir_iterate of nested overlayfs: 760b1eaa950SAmir Goldstein * 761b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) 762b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) 763b1eaa950SAmir Goldstein * [...] &type->i_mutex_dir_key (stack_depth=0) 764b1f9d385SAmir Goldstein * 765b1f9d385SAmir Goldstein * Locking order w.r.t ovl_want_write() is important for nested overlayfs. 766b1f9d385SAmir Goldstein * 767b1f9d385SAmir Goldstein * This chain is valid: 768b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[2]) 769b1f9d385SAmir Goldstein * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 770b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[2]) 771b1f9d385SAmir Goldstein * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 772b1f9d385SAmir Goldstein * 773b1f9d385SAmir Goldstein * And this chain is valid: 774b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[2]) 775b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[2]) 776b1f9d385SAmir Goldstein * - lowerinode->i_rwsem (inode_lock[1]) 777b1f9d385SAmir Goldstein * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 778b1f9d385SAmir Goldstein * 779b1f9d385SAmir Goldstein * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is 780b1f9d385SAmir Goldstein * held, because it is in reverse order of the non-nested case using the same 781b1f9d385SAmir Goldstein * upper fs: 782b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[1]) 783b1f9d385SAmir Goldstein * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 784b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[1]) 785b1eaa950SAmir Goldstein */ 786b1eaa950SAmir Goldstein #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH 787b1eaa950SAmir Goldstein 788b1eaa950SAmir Goldstein static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) 789b1eaa950SAmir Goldstein { 790b1eaa950SAmir Goldstein #ifdef CONFIG_LOCKDEP 791b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; 792b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; 7934eae06deSAmir Goldstein static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; 794b1eaa950SAmir Goldstein 795b1eaa950SAmir Goldstein int depth = inode->i_sb->s_stack_depth - 1; 796b1eaa950SAmir Goldstein 797b1eaa950SAmir Goldstein if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) 798b1eaa950SAmir Goldstein depth = 0; 799b1eaa950SAmir Goldstein 800b1eaa950SAmir Goldstein if (S_ISDIR(inode->i_mode)) 801b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); 802b1eaa950SAmir Goldstein else 803b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); 8044eae06deSAmir Goldstein 8054eae06deSAmir Goldstein lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); 806b1eaa950SAmir Goldstein #endif 807b1eaa950SAmir Goldstein } 808b1eaa950SAmir Goldstein 8094d314f78SAmir Goldstein static void ovl_next_ino(struct inode *inode) 8104d314f78SAmir Goldstein { 8114d314f78SAmir Goldstein struct ovl_fs *ofs = inode->i_sb->s_fs_info; 8124d314f78SAmir Goldstein 8134d314f78SAmir Goldstein inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 8144d314f78SAmir Goldstein if (unlikely(!inode->i_ino)) 8154d314f78SAmir Goldstein inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 8164d314f78SAmir Goldstein } 8174d314f78SAmir Goldstein 81862c832edSAmir Goldstein static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) 819e9be9d5eSMiklos Szeredi { 82012574a9fSAmir Goldstein int xinobits = ovl_xino_bits(inode->i_sb); 821dfe51d47SAmir Goldstein unsigned int xinoshift = 64 - xinobits; 82212574a9fSAmir Goldstein 823695b46e7SAmir Goldstein /* 8246dde1e42SAmir Goldstein * When d_ino is consistent with st_ino (samefs or i_ino has enough 8256dde1e42SAmir Goldstein * bits to encode layer), set the same value used for st_ino to i_ino, 8266dde1e42SAmir Goldstein * so inode number exposed via /proc/locks and a like will be 8276dde1e42SAmir Goldstein * consistent with d_ino and st_ino values. An i_ino value inconsistent 828735c907dSAmir Goldstein * with d_ino also causes nfsd readdirplus to fail. 829695b46e7SAmir Goldstein */ 830695b46e7SAmir Goldstein inode->i_ino = ino; 831dfe51d47SAmir Goldstein if (ovl_same_fs(inode->i_sb)) { 832dfe51d47SAmir Goldstein return; 833dfe51d47SAmir Goldstein } else if (xinobits && likely(!(ino >> xinoshift))) { 834dfe51d47SAmir Goldstein inode->i_ino |= (unsigned long)fsid << (xinoshift + 1); 835dfe51d47SAmir Goldstein return; 836dfe51d47SAmir Goldstein } 837dfe51d47SAmir Goldstein 838dfe51d47SAmir Goldstein /* 839dfe51d47SAmir Goldstein * For directory inodes on non-samefs with xino disabled or xino 840dfe51d47SAmir Goldstein * overflow, we allocate a non-persistent inode number, to be used for 841dfe51d47SAmir Goldstein * resolving st_ino collisions in ovl_map_dev_ino(). 842dfe51d47SAmir Goldstein * 843dfe51d47SAmir Goldstein * To avoid ino collision with legitimate xino values from upper 844dfe51d47SAmir Goldstein * layer (fsid 0), use the lowest xinobit to map the non 845dfe51d47SAmir Goldstein * persistent inode numbers to the unified st_ino address space. 846dfe51d47SAmir Goldstein */ 847dfe51d47SAmir Goldstein if (S_ISDIR(inode->i_mode)) { 8484d314f78SAmir Goldstein ovl_next_ino(inode); 849dfe51d47SAmir Goldstein if (xinobits) { 850dfe51d47SAmir Goldstein inode->i_ino &= ~0UL >> xinobits; 851dfe51d47SAmir Goldstein inode->i_ino |= 1UL << xinoshift; 852dfe51d47SAmir Goldstein } 85312574a9fSAmir Goldstein } 85462c832edSAmir Goldstein } 85562c832edSAmir Goldstein 85662c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 85762c832edSAmir Goldstein unsigned long ino, int fsid) 85862c832edSAmir Goldstein { 85962c832edSAmir Goldstein struct inode *realinode; 860ffa5723cSAmir Goldstein struct ovl_inode *oi = OVL_I(inode); 86162c832edSAmir Goldstein 86262c832edSAmir Goldstein if (oip->upperdentry) 863ffa5723cSAmir Goldstein oi->__upperdentry = oip->upperdentry; 864ffa5723cSAmir Goldstein if (oip->lowerpath && oip->lowerpath->dentry) { 865ffa5723cSAmir Goldstein oi->lowerpath.dentry = dget(oip->lowerpath->dentry); 866ffa5723cSAmir Goldstein oi->lowerpath.layer = oip->lowerpath->layer; 867ffa5723cSAmir Goldstein } 86862c832edSAmir Goldstein if (oip->lowerdata) 869ffa5723cSAmir Goldstein oi->lowerdata = igrab(d_inode(oip->lowerdata)); 87062c832edSAmir Goldstein 87162c832edSAmir Goldstein realinode = ovl_inode_real(inode); 8722878dffcSChristian Brauner ovl_copyattr(inode); 87362c832edSAmir Goldstein ovl_copyflags(realinode, inode); 87462c832edSAmir Goldstein ovl_map_ino(inode, ino, fsid); 87562c832edSAmir Goldstein } 87662c832edSAmir Goldstein 87762c832edSAmir Goldstein static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) 87862c832edSAmir Goldstein { 879e9be9d5eSMiklos Szeredi inode->i_mode = mode; 880d719e8f2SMiklos Szeredi inode->i_flags |= S_NOCMTIME; 8812a3a2a3fSMiklos Szeredi #ifdef CONFIG_FS_POSIX_ACL 8822a3a2a3fSMiklos Szeredi inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; 8832a3a2a3fSMiklos Szeredi #endif 884e9be9d5eSMiklos Szeredi 885b1eaa950SAmir Goldstein ovl_lockdep_annotate_inode_mutex_key(inode); 886b1eaa950SAmir Goldstein 887ca4c8a3aSMiklos Szeredi switch (mode & S_IFMT) { 888ca4c8a3aSMiklos Szeredi case S_IFREG: 889ca4c8a3aSMiklos Szeredi inode->i_op = &ovl_file_inode_operations; 890d1d04ef8SMiklos Szeredi inode->i_fop = &ovl_file_operations; 8915b910bd6SAmir Goldstein inode->i_mapping->a_ops = &ovl_aops; 892ca4c8a3aSMiklos Szeredi break; 893ca4c8a3aSMiklos Szeredi 894e9be9d5eSMiklos Szeredi case S_IFDIR: 895e9be9d5eSMiklos Szeredi inode->i_op = &ovl_dir_inode_operations; 896e9be9d5eSMiklos Szeredi inode->i_fop = &ovl_dir_operations; 897e9be9d5eSMiklos Szeredi break; 898e9be9d5eSMiklos Szeredi 899e9be9d5eSMiklos Szeredi case S_IFLNK: 900e9be9d5eSMiklos Szeredi inode->i_op = &ovl_symlink_inode_operations; 901e9be9d5eSMiklos Szeredi break; 902e9be9d5eSMiklos Szeredi 90351f7e52dSMiklos Szeredi default: 9049e142c41SMiklos Szeredi inode->i_op = &ovl_special_inode_operations; 905ca4c8a3aSMiklos Szeredi init_special_inode(inode, mode, rdev); 906e9be9d5eSMiklos Szeredi break; 90751f7e52dSMiklos Szeredi } 90851f7e52dSMiklos Szeredi } 909e9be9d5eSMiklos Szeredi 9105f8415d6SAmir Goldstein /* 9115f8415d6SAmir Goldstein * With inodes index enabled, an overlay inode nlink counts the union of upper 9125f8415d6SAmir Goldstein * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure 9135f8415d6SAmir Goldstein * upper inode, the following nlink modifying operations can happen: 9145f8415d6SAmir Goldstein * 9155f8415d6SAmir Goldstein * 1. Lower hardlink copy up 9165f8415d6SAmir Goldstein * 2. Upper hardlink created, unlinked or renamed over 9175f8415d6SAmir Goldstein * 3. Lower hardlink whiteout or renamed over 9185f8415d6SAmir Goldstein * 9195f8415d6SAmir Goldstein * For the first, copy up case, the union nlink does not change, whether the 9205f8415d6SAmir Goldstein * operation succeeds or fails, but the upper inode nlink may change. 9215f8415d6SAmir Goldstein * Therefore, before copy up, we store the union nlink value relative to the 9222d2f2d73SMiklos Szeredi * lower inode nlink in the index inode xattr .overlay.nlink. 9235f8415d6SAmir Goldstein * 9245f8415d6SAmir Goldstein * For the second, upper hardlink case, the union nlink should be incremented 9255f8415d6SAmir Goldstein * or decremented IFF the operation succeeds, aligned with nlink change of the 9265f8415d6SAmir Goldstein * upper inode. Therefore, before link/unlink/rename, we store the union nlink 9275f8415d6SAmir Goldstein * value relative to the upper inode nlink in the index inode. 9285f8415d6SAmir Goldstein * 9295f8415d6SAmir Goldstein * For the last, lower cover up case, we simplify things by preceding the 9305f8415d6SAmir Goldstein * whiteout or cover up with copy up. This makes sure that there is an index 9315f8415d6SAmir Goldstein * upper inode where the nlink xattr can be stored before the copied up upper 9325f8415d6SAmir Goldstein * entry is unlink. 9335f8415d6SAmir Goldstein */ 9345f8415d6SAmir Goldstein #define OVL_NLINK_ADD_UPPER (1 << 0) 9355f8415d6SAmir Goldstein 9365f8415d6SAmir Goldstein /* 9375f8415d6SAmir Goldstein * On-disk format for indexed nlink: 9385f8415d6SAmir Goldstein * 9395f8415d6SAmir Goldstein * nlink relative to the upper inode - "U[+-]NUM" 9405f8415d6SAmir Goldstein * nlink relative to the lower inode - "L[+-]NUM" 9415f8415d6SAmir Goldstein */ 9425f8415d6SAmir Goldstein 9435f8415d6SAmir Goldstein static int ovl_set_nlink_common(struct dentry *dentry, 9445f8415d6SAmir Goldstein struct dentry *realdentry, const char *format) 9455f8415d6SAmir Goldstein { 9465f8415d6SAmir Goldstein struct inode *inode = d_inode(dentry); 9475f8415d6SAmir Goldstein struct inode *realinode = d_inode(realdentry); 9485f8415d6SAmir Goldstein char buf[13]; 9495f8415d6SAmir Goldstein int len; 9505f8415d6SAmir Goldstein 9515f8415d6SAmir Goldstein len = snprintf(buf, sizeof(buf), format, 9525f8415d6SAmir Goldstein (int) (inode->i_nlink - realinode->i_nlink)); 9535f8415d6SAmir Goldstein 9546787341aSMiklos Szeredi if (WARN_ON(len >= sizeof(buf))) 9556787341aSMiklos Szeredi return -EIO; 9566787341aSMiklos Szeredi 957c914c0e2SAmir Goldstein return ovl_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry), 95826150ab5SMiklos Szeredi OVL_XATTR_NLINK, buf, len); 9595f8415d6SAmir Goldstein } 9605f8415d6SAmir Goldstein 9615f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry) 9625f8415d6SAmir Goldstein { 9635f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); 9645f8415d6SAmir Goldstein } 9655f8415d6SAmir Goldstein 9665f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry) 9675f8415d6SAmir Goldstein { 9685f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); 9695f8415d6SAmir Goldstein } 9705f8415d6SAmir Goldstein 971610afc0bSMiklos Szeredi unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, 9725f8415d6SAmir Goldstein struct dentry *upperdentry, 9735f8415d6SAmir Goldstein unsigned int fallback) 9745f8415d6SAmir Goldstein { 9755f8415d6SAmir Goldstein int nlink_diff; 9765f8415d6SAmir Goldstein int nlink; 9775f8415d6SAmir Goldstein char buf[13]; 9785f8415d6SAmir Goldstein int err; 9795f8415d6SAmir Goldstein 9805f8415d6SAmir Goldstein if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) 9815f8415d6SAmir Goldstein return fallback; 9825f8415d6SAmir Goldstein 983dad7017aSChristian Brauner err = ovl_getxattr_upper(ofs, upperdentry, OVL_XATTR_NLINK, 984d5dc7486SMiklos Szeredi &buf, sizeof(buf) - 1); 9855f8415d6SAmir Goldstein if (err < 0) 9865f8415d6SAmir Goldstein goto fail; 9875f8415d6SAmir Goldstein 9885f8415d6SAmir Goldstein buf[err] = '\0'; 9895f8415d6SAmir Goldstein if ((buf[0] != 'L' && buf[0] != 'U') || 9905f8415d6SAmir Goldstein (buf[1] != '+' && buf[1] != '-')) 9915f8415d6SAmir Goldstein goto fail; 9925f8415d6SAmir Goldstein 9935f8415d6SAmir Goldstein err = kstrtoint(buf + 1, 10, &nlink_diff); 9945f8415d6SAmir Goldstein if (err < 0) 9955f8415d6SAmir Goldstein goto fail; 9965f8415d6SAmir Goldstein 9975f8415d6SAmir Goldstein nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; 9985f8415d6SAmir Goldstein nlink += nlink_diff; 9995f8415d6SAmir Goldstein 10005f8415d6SAmir Goldstein if (nlink <= 0) 10015f8415d6SAmir Goldstein goto fail; 10025f8415d6SAmir Goldstein 10035f8415d6SAmir Goldstein return nlink; 10045f8415d6SAmir Goldstein 10055f8415d6SAmir Goldstein fail: 10061bd0a3aeSlijiazi pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n", 10075f8415d6SAmir Goldstein upperdentry, err); 10085f8415d6SAmir Goldstein return fallback; 10095f8415d6SAmir Goldstein } 10105f8415d6SAmir Goldstein 1011ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) 101251f7e52dSMiklos Szeredi { 101351f7e52dSMiklos Szeredi struct inode *inode; 101451f7e52dSMiklos Szeredi 101551f7e52dSMiklos Szeredi inode = new_inode(sb); 101651f7e52dSMiklos Szeredi if (inode) 101762c832edSAmir Goldstein ovl_fill_inode(inode, mode, rdev); 101851f7e52dSMiklos Szeredi 101951f7e52dSMiklos Szeredi return inode; 102051f7e52dSMiklos Szeredi } 102151f7e52dSMiklos Szeredi 102251f7e52dSMiklos Szeredi static int ovl_inode_test(struct inode *inode, void *data) 102351f7e52dSMiklos Szeredi { 102425b7713aSMiklos Szeredi return inode->i_private == data; 102551f7e52dSMiklos Szeredi } 102651f7e52dSMiklos Szeredi 102751f7e52dSMiklos Szeredi static int ovl_inode_set(struct inode *inode, void *data) 102851f7e52dSMiklos Szeredi { 102925b7713aSMiklos Szeredi inode->i_private = data; 103051f7e52dSMiklos Szeredi return 0; 103151f7e52dSMiklos Szeredi } 103251f7e52dSMiklos Szeredi 1033b9ac5c27SMiklos Szeredi static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, 10344b91c30aSAmir Goldstein struct dentry *upperdentry, bool strict) 1035b9ac5c27SMiklos Szeredi { 10364b91c30aSAmir Goldstein /* 10374b91c30aSAmir Goldstein * For directories, @strict verify from lookup path performs consistency 10384b91c30aSAmir Goldstein * checks, so NULL lower/upper in dentry must match NULL lower/upper in 10394b91c30aSAmir Goldstein * inode. Non @strict verify from NFS handle decode path passes NULL for 10404b91c30aSAmir Goldstein * 'unknown' lower/upper. 10414b91c30aSAmir Goldstein */ 10424b91c30aSAmir Goldstein if (S_ISDIR(inode->i_mode) && strict) { 104331747edaSAmir Goldstein /* Real lower dir moved to upper layer under us? */ 104431747edaSAmir Goldstein if (!lowerdentry && ovl_inode_lower(inode)) 104531747edaSAmir Goldstein return false; 104631747edaSAmir Goldstein 104731747edaSAmir Goldstein /* Lookup of an uncovered redirect origin? */ 104831747edaSAmir Goldstein if (!upperdentry && ovl_inode_upper(inode)) 104931747edaSAmir Goldstein return false; 105031747edaSAmir Goldstein } 105131747edaSAmir Goldstein 1052939ae4efSAmir Goldstein /* 1053939ae4efSAmir Goldstein * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. 1054939ae4efSAmir Goldstein * This happens when finding a copied up overlay inode for a renamed 1055939ae4efSAmir Goldstein * or hardlinked overlay dentry and lower dentry cannot be followed 1056939ae4efSAmir Goldstein * by origin because lower fs does not support file handles. 1057939ae4efSAmir Goldstein */ 1058939ae4efSAmir Goldstein if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) 1059b9ac5c27SMiklos Szeredi return false; 1060b9ac5c27SMiklos Szeredi 1061b9ac5c27SMiklos Szeredi /* 1062b9ac5c27SMiklos Szeredi * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. 1063b9ac5c27SMiklos Szeredi * This happens when finding a lower alias for a copied up hard link. 1064b9ac5c27SMiklos Szeredi */ 1065b9ac5c27SMiklos Szeredi if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) 1066b9ac5c27SMiklos Szeredi return false; 1067b9ac5c27SMiklos Szeredi 1068b9ac5c27SMiklos Szeredi return true; 1069b9ac5c27SMiklos Szeredi } 1070b9ac5c27SMiklos Szeredi 10714b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 10724b91c30aSAmir Goldstein bool is_upper) 10739436a1a3SAmir Goldstein { 10744b91c30aSAmir Goldstein struct inode *inode, *key = d_inode(real); 10759436a1a3SAmir Goldstein 10769436a1a3SAmir Goldstein inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 10779436a1a3SAmir Goldstein if (!inode) 10789436a1a3SAmir Goldstein return NULL; 10799436a1a3SAmir Goldstein 10804b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, is_upper ? NULL : real, 10814b91c30aSAmir Goldstein is_upper ? real : NULL, false)) { 10829436a1a3SAmir Goldstein iput(inode); 10839436a1a3SAmir Goldstein return ERR_PTR(-ESTALE); 10849436a1a3SAmir Goldstein } 10859436a1a3SAmir Goldstein 10869436a1a3SAmir Goldstein return inode; 10879436a1a3SAmir Goldstein } 10889436a1a3SAmir Goldstein 1089146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir) 1090146d62e5SAmir Goldstein { 1091146d62e5SAmir Goldstein struct inode *key = d_inode(dir); 1092146d62e5SAmir Goldstein struct inode *trap; 1093146d62e5SAmir Goldstein bool res; 1094146d62e5SAmir Goldstein 1095146d62e5SAmir Goldstein trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 1096146d62e5SAmir Goldstein if (!trap) 1097146d62e5SAmir Goldstein return false; 1098146d62e5SAmir Goldstein 1099146d62e5SAmir Goldstein res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) && 1100146d62e5SAmir Goldstein !ovl_inode_lower(trap); 1101146d62e5SAmir Goldstein 1102146d62e5SAmir Goldstein iput(trap); 1103146d62e5SAmir Goldstein return res; 1104146d62e5SAmir Goldstein } 1105146d62e5SAmir Goldstein 1106146d62e5SAmir Goldstein /* 1107146d62e5SAmir Goldstein * Create an inode cache entry for layer root dir, that will intentionally 1108146d62e5SAmir Goldstein * fail ovl_verify_inode(), so any lookup that will find some layer root 1109146d62e5SAmir Goldstein * will fail. 1110146d62e5SAmir Goldstein */ 1111146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir) 1112146d62e5SAmir Goldstein { 1113146d62e5SAmir Goldstein struct inode *key = d_inode(dir); 1114146d62e5SAmir Goldstein struct inode *trap; 1115146d62e5SAmir Goldstein 1116146d62e5SAmir Goldstein if (!d_is_dir(dir)) 1117146d62e5SAmir Goldstein return ERR_PTR(-ENOTDIR); 1118146d62e5SAmir Goldstein 1119146d62e5SAmir Goldstein trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test, 1120146d62e5SAmir Goldstein ovl_inode_set, key); 1121146d62e5SAmir Goldstein if (!trap) 1122146d62e5SAmir Goldstein return ERR_PTR(-ENOMEM); 1123146d62e5SAmir Goldstein 1124146d62e5SAmir Goldstein if (!(trap->i_state & I_NEW)) { 1125146d62e5SAmir Goldstein /* Conflicting layer roots? */ 1126146d62e5SAmir Goldstein iput(trap); 1127146d62e5SAmir Goldstein return ERR_PTR(-ELOOP); 1128146d62e5SAmir Goldstein } 1129146d62e5SAmir Goldstein 1130146d62e5SAmir Goldstein trap->i_mode = S_IFDIR; 1131146d62e5SAmir Goldstein trap->i_flags = S_DEAD; 1132146d62e5SAmir Goldstein unlock_new_inode(trap); 1133146d62e5SAmir Goldstein 1134146d62e5SAmir Goldstein return trap; 1135146d62e5SAmir Goldstein } 1136146d62e5SAmir Goldstein 1137764baba8SAmir Goldstein /* 1138764baba8SAmir Goldstein * Does overlay inode need to be hashed by lower inode? 1139764baba8SAmir Goldstein */ 1140764baba8SAmir Goldstein static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, 114174c6e384SMiklos Szeredi struct dentry *lower, bool index) 1142764baba8SAmir Goldstein { 1143764baba8SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 1144764baba8SAmir Goldstein 1145764baba8SAmir Goldstein /* No, if pure upper */ 1146764baba8SAmir Goldstein if (!lower) 1147764baba8SAmir Goldstein return false; 1148764baba8SAmir Goldstein 1149764baba8SAmir Goldstein /* Yes, if already indexed */ 1150764baba8SAmir Goldstein if (index) 1151764baba8SAmir Goldstein return true; 1152764baba8SAmir Goldstein 1153764baba8SAmir Goldstein /* Yes, if won't be copied up */ 115408f4c7c8SMiklos Szeredi if (!ovl_upper_mnt(ofs)) 1155764baba8SAmir Goldstein return true; 1156764baba8SAmir Goldstein 1157764baba8SAmir Goldstein /* No, if lower hardlink is or will be broken on copy up */ 1158764baba8SAmir Goldstein if ((upper || !ovl_indexdir(sb)) && 1159764baba8SAmir Goldstein !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 1160764baba8SAmir Goldstein return false; 1161764baba8SAmir Goldstein 1162764baba8SAmir Goldstein /* No, if non-indexed upper with NFS export */ 1163764baba8SAmir Goldstein if (sb->s_export_op && upper) 1164764baba8SAmir Goldstein return false; 1165764baba8SAmir Goldstein 1166764baba8SAmir Goldstein /* Otherwise, hash by lower inode for fsnotify */ 1167764baba8SAmir Goldstein return true; 1168764baba8SAmir Goldstein } 1169764baba8SAmir Goldstein 117001b39dccSAmir Goldstein static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode, 117101b39dccSAmir Goldstein struct inode *key) 117201b39dccSAmir Goldstein { 117301b39dccSAmir Goldstein return newinode ? inode_insert5(newinode, (unsigned long) key, 117401b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key) : 117501b39dccSAmir Goldstein iget5_locked(sb, (unsigned long) key, 117601b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key); 117701b39dccSAmir Goldstein } 117801b39dccSAmir Goldstein 1179ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb, 1180ac6a52ebSVivek Goyal struct ovl_inode_params *oip) 118151f7e52dSMiklos Szeredi { 1182610afc0bSMiklos Szeredi struct ovl_fs *ofs = OVL_FS(sb); 1183ac6a52ebSVivek Goyal struct dentry *upperdentry = oip->upperdentry; 1184ac6a52ebSVivek Goyal struct ovl_path *lowerpath = oip->lowerpath; 118509d8b586SMiklos Szeredi struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; 118651f7e52dSMiklos Szeredi struct inode *inode; 118712574a9fSAmir Goldstein struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL; 1188dad7017aSChristian Brauner struct path realpath = { 1189dad7017aSChristian Brauner .dentry = upperdentry ?: lowerdentry, 1190dad7017aSChristian Brauner .mnt = upperdentry ? ovl_upper_mnt(ofs) : lowerpath->layer->mnt, 1191dad7017aSChristian Brauner }; 1192ac6a52ebSVivek Goyal bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, 1193ac6a52ebSVivek Goyal oip->index); 1194300b124fSAmir Goldstein int fsid = bylower ? lowerpath->layer->fsid : 0; 119528166ab3SVivek Goyal bool is_dir; 1196695b46e7SAmir Goldstein unsigned long ino = 0; 1197acf3062aSAmir Goldstein int err = oip->newinode ? -EEXIST : -ENOMEM; 11986eaf0111SAmir Goldstein 119909d8b586SMiklos Szeredi if (!realinode) 120009d8b586SMiklos Szeredi realinode = d_inode(lowerdentry); 120109d8b586SMiklos Szeredi 12026eaf0111SAmir Goldstein /* 1203764baba8SAmir Goldstein * Copy up origin (lower) may exist for non-indexed upper, but we must 1204764baba8SAmir Goldstein * not use lower as hash key if this is a broken hardlink. 12056eaf0111SAmir Goldstein */ 120631747edaSAmir Goldstein is_dir = S_ISDIR(realinode->i_mode); 1207764baba8SAmir Goldstein if (upperdentry || bylower) { 1208764baba8SAmir Goldstein struct inode *key = d_inode(bylower ? lowerdentry : 1209764baba8SAmir Goldstein upperdentry); 121031747edaSAmir Goldstein unsigned int nlink = is_dir ? 1 : realinode->i_nlink; 1211b9ac5c27SMiklos Szeredi 121201b39dccSAmir Goldstein inode = ovl_iget5(sb, oip->newinode, key); 121309d8b586SMiklos Szeredi if (!inode) 1214027065b7SVivek Goyal goto out_err; 121509d8b586SMiklos Szeredi if (!(inode->i_state & I_NEW)) { 1216b9ac5c27SMiklos Szeredi /* 1217b9ac5c27SMiklos Szeredi * Verify that the underlying files stored in the inode 1218b9ac5c27SMiklos Szeredi * match those in the dentry. 1219b9ac5c27SMiklos Szeredi */ 12204b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, lowerdentry, upperdentry, 12214b91c30aSAmir Goldstein true)) { 1222b9ac5c27SMiklos Szeredi iput(inode); 1223027065b7SVivek Goyal err = -ESTALE; 1224027065b7SVivek Goyal goto out_err; 1225b9ac5c27SMiklos Szeredi } 1226b9ac5c27SMiklos Szeredi 122709d8b586SMiklos Szeredi dput(upperdentry); 12289cec54c8SVivek Goyal kfree(oip->redirect); 122909d8b586SMiklos Szeredi goto out; 123009d8b586SMiklos Szeredi } 1231e9be9d5eSMiklos Szeredi 123231747edaSAmir Goldstein /* Recalculate nlink for non-dir due to indexing */ 123331747edaSAmir Goldstein if (!is_dir) 1234610afc0bSMiklos Szeredi nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry, 1235610afc0bSMiklos Szeredi nlink); 12365f8415d6SAmir Goldstein set_nlink(inode, nlink); 1237695b46e7SAmir Goldstein ino = key->i_ino; 1238e6d2ebddSMiklos Szeredi } else { 1239764baba8SAmir Goldstein /* Lower hardlink that will be broken on copy up */ 12400aceb53eSAmir Goldstein inode = new_inode(sb); 1241027065b7SVivek Goyal if (!inode) { 1242027065b7SVivek Goyal err = -ENOMEM; 1243027065b7SVivek Goyal goto out_err; 1244027065b7SVivek Goyal } 1245300b124fSAmir Goldstein ino = realinode->i_ino; 1246300b124fSAmir Goldstein fsid = lowerpath->layer->fsid; 1247e6d2ebddSMiklos Szeredi } 124862c832edSAmir Goldstein ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); 124962c832edSAmir Goldstein ovl_inode_init(inode, oip, ino, fsid); 125013c72075SMiklos Szeredi 1251610afc0bSMiklos Szeredi if (upperdentry && ovl_is_impuredir(sb, upperdentry)) 125213c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, inode); 125313c72075SMiklos Szeredi 1254ac6a52ebSVivek Goyal if (oip->index) 12550471a9cdSVivek Goyal ovl_set_flag(OVL_INDEX, inode); 12560471a9cdSVivek Goyal 12579cec54c8SVivek Goyal OVL_I(inode)->redirect = oip->redirect; 12589cec54c8SVivek Goyal 1259a00c2d59SVivek Goyal if (bylower) 1260a00c2d59SVivek Goyal ovl_set_flag(OVL_CONST_INO, inode); 1261a00c2d59SVivek Goyal 1262b79e05aaSAmir Goldstein /* Check for non-merge dir that may have whiteouts */ 126331747edaSAmir Goldstein if (is_dir) { 1264ac6a52ebSVivek Goyal if (((upperdentry && lowerdentry) || oip->numlower > 1) || 1265dad7017aSChristian Brauner ovl_path_check_origin_xattr(ofs, &realpath)) { 1266b79e05aaSAmir Goldstein ovl_set_flag(OVL_WHITEOUTS, inode); 1267b79e05aaSAmir Goldstein } 1268b79e05aaSAmir Goldstein } 1269b79e05aaSAmir Goldstein 1270096a218aSAmir Goldstein /* Check for immutable/append-only inode flags in xattr */ 1271096a218aSAmir Goldstein if (upperdentry) 1272096a218aSAmir Goldstein ovl_check_protattr(inode, upperdentry); 1273096a218aSAmir Goldstein 1274e6d2ebddSMiklos Szeredi if (inode->i_state & I_NEW) 1275e6d2ebddSMiklos Szeredi unlock_new_inode(inode); 1276e6d2ebddSMiklos Szeredi out: 1277e9be9d5eSMiklos Szeredi return inode; 1278b9ac5c27SMiklos Szeredi 1279027065b7SVivek Goyal out_err: 12801bd0a3aeSlijiazi pr_warn_ratelimited("failed to get inode (%i)\n", err); 1281027065b7SVivek Goyal inode = ERR_PTR(err); 1282b9ac5c27SMiklos Szeredi goto out; 1283e9be9d5eSMiklos Szeredi } 1284