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> 14e9be9d5eSMiklos Szeredi #include "overlayfs.h" 15e9be9d5eSMiklos Szeredi 16ba1e563cSChandan Rajendra 17e9be9d5eSMiklos Szeredi int ovl_setattr(struct dentry *dentry, struct iattr *attr) 18e9be9d5eSMiklos Szeredi { 19e9be9d5eSMiklos Szeredi int err; 20997336f2SVivek Goyal bool full_copy_up = false; 21e9be9d5eSMiklos Szeredi struct dentry *upperdentry; 221175b6b8SVivek Goyal const struct cred *old_cred; 23e9be9d5eSMiklos Szeredi 2431051c85SJan Kara err = setattr_prepare(dentry, attr); 25cf9a6784SMiklos Szeredi if (err) 26cf9a6784SMiklos Szeredi return err; 27cf9a6784SMiklos Szeredi 28e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 29e9be9d5eSMiklos Szeredi if (err) 30e9be9d5eSMiklos Szeredi goto out; 31e9be9d5eSMiklos Szeredi 325812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 335812160eSMiklos Szeredi struct inode *realinode = d_inode(ovl_dentry_real(dentry)); 345812160eSMiklos Szeredi 355812160eSMiklos Szeredi err = -ETXTBSY; 365812160eSMiklos Szeredi if (atomic_read(&realinode->i_writecount) < 0) 375812160eSMiklos Szeredi goto out_drop_write; 38997336f2SVivek Goyal 39997336f2SVivek Goyal /* Truncate should trigger data copy up as well */ 40997336f2SVivek Goyal full_copy_up = true; 415812160eSMiklos Szeredi } 425812160eSMiklos Szeredi 43997336f2SVivek Goyal if (!full_copy_up) 44acff81ecSMiklos Szeredi err = ovl_copy_up(dentry); 45997336f2SVivek Goyal else 46997336f2SVivek Goyal err = ovl_copy_up_with_data(dentry); 47acff81ecSMiklos Szeredi if (!err) { 485812160eSMiklos Szeredi struct inode *winode = NULL; 495812160eSMiklos Szeredi 50e9be9d5eSMiklos Szeredi upperdentry = ovl_dentry_upper(dentry); 51acff81ecSMiklos Szeredi 525812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 535812160eSMiklos Szeredi winode = d_inode(upperdentry); 545812160eSMiklos Szeredi err = get_write_access(winode); 555812160eSMiklos Szeredi if (err) 565812160eSMiklos Szeredi goto out_drop_write; 575812160eSMiklos Szeredi } 585812160eSMiklos Szeredi 59b99c2d91SMiklos Szeredi if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) 60b99c2d91SMiklos Szeredi attr->ia_valid &= ~ATTR_MODE; 61b99c2d91SMiklos Szeredi 62e67f0216SVivek Goyal /* 6315fd2ea9SVivek Goyal * We might have to translate ovl file into real file object 6415fd2ea9SVivek Goyal * once use cases emerge. For now, simply don't let underlying 6515fd2ea9SVivek Goyal * filesystem rely on attr->ia_file 66e67f0216SVivek Goyal */ 67e67f0216SVivek Goyal attr->ia_valid &= ~ATTR_FILE; 68e67f0216SVivek Goyal 6915fd2ea9SVivek Goyal /* 7015fd2ea9SVivek Goyal * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN 7115fd2ea9SVivek Goyal * set. Overlayfs does not pass O_TRUNC flag to underlying 7215fd2ea9SVivek Goyal * filesystem during open -> do not pass ATTR_OPEN. This 7315fd2ea9SVivek Goyal * disables optimization in fuse which assumes open(O_TRUNC) 7415fd2ea9SVivek Goyal * already set file size to 0. But we never passed O_TRUNC to 7515fd2ea9SVivek Goyal * fuse. So by clearing ATTR_OPEN, fuse will be forced to send 7615fd2ea9SVivek Goyal * setattr request to server. 7715fd2ea9SVivek Goyal */ 7815fd2ea9SVivek Goyal attr->ia_valid &= ~ATTR_OPEN; 7915fd2ea9SVivek Goyal 805955102cSAl Viro inode_lock(upperdentry->d_inode); 811175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 82e9be9d5eSMiklos Szeredi err = notify_change(upperdentry, attr, NULL); 831175b6b8SVivek Goyal revert_creds(old_cred); 84b81de061SKonstantin Khlebnikov if (!err) 85b81de061SKonstantin Khlebnikov ovl_copyattr(upperdentry->d_inode, dentry->d_inode); 865955102cSAl Viro inode_unlock(upperdentry->d_inode); 875812160eSMiklos Szeredi 885812160eSMiklos Szeredi if (winode) 895812160eSMiklos Szeredi put_write_access(winode); 90e9be9d5eSMiklos Szeredi } 915812160eSMiklos Szeredi out_drop_write: 92e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 93e9be9d5eSMiklos Szeredi out: 94e9be9d5eSMiklos Szeredi return err; 95e9be9d5eSMiklos Szeredi } 96e9be9d5eSMiklos Szeredi 9707f1e596SAmir Goldstein static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid) 98da309e8cSAmir Goldstein { 990f831ec8SAmir Goldstein bool samefs = ovl_same_fs(dentry->d_sb); 100e487d889SAmir Goldstein unsigned int xinobits = ovl_xino_bits(dentry->d_sb); 101dfe51d47SAmir Goldstein unsigned int xinoshift = 64 - xinobits; 102da309e8cSAmir Goldstein 103da309e8cSAmir Goldstein if (samefs) { 104da309e8cSAmir Goldstein /* 105da309e8cSAmir Goldstein * When all layers are on the same fs, all real inode 106da309e8cSAmir Goldstein * number are unique, so we use the overlay st_dev, 107da309e8cSAmir Goldstein * which is friendly to du -x. 108da309e8cSAmir Goldstein */ 109da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 110e487d889SAmir Goldstein return 0; 111e487d889SAmir Goldstein } else if (xinobits) { 112e487d889SAmir Goldstein /* 113e487d889SAmir Goldstein * All inode numbers of underlying fs should not be using the 114e487d889SAmir Goldstein * high xinobits, so we use high xinobits to partition the 115e487d889SAmir Goldstein * overlay st_ino address space. The high bits holds the fsid 116dfe51d47SAmir Goldstein * (upper fsid is 0). The lowest xinobit is reserved for mapping 117dfe51d47SAmir Goldstein * the non-peresistent inode numbers range in case of overflow. 118dfe51d47SAmir Goldstein * This way all overlay inode numbers are unique and use the 119dfe51d47SAmir Goldstein * overlay st_dev. 120e487d889SAmir Goldstein */ 121926e94d7SAmir Goldstein if (likely(!(stat->ino >> xinoshift))) { 122dfe51d47SAmir Goldstein stat->ino |= ((u64)fsid) << (xinoshift + 1); 123e487d889SAmir Goldstein stat->dev = dentry->d_sb->s_dev; 124e487d889SAmir Goldstein return 0; 125926e94d7SAmir Goldstein } else if (ovl_xino_warn(dentry->d_sb)) { 126926e94d7SAmir Goldstein pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n", 127926e94d7SAmir Goldstein dentry, stat->ino, xinobits); 128e487d889SAmir Goldstein } 129e487d889SAmir Goldstein } 130e487d889SAmir Goldstein 131e487d889SAmir Goldstein /* The inode could not be mapped to a unified st_ino address space */ 132e487d889SAmir Goldstein if (S_ISDIR(dentry->d_inode->i_mode)) { 133da309e8cSAmir Goldstein /* 134da309e8cSAmir Goldstein * Always use the overlay st_dev for directories, so 'find 135da309e8cSAmir Goldstein * -xdev' will scan the entire overlay mount and won't cross the 136da309e8cSAmir Goldstein * overlay mount boundaries. 137da309e8cSAmir Goldstein * 138da309e8cSAmir Goldstein * If not all layers are on the same fs the pair {real st_ino; 139da309e8cSAmir Goldstein * overlay st_dev} is not unique, so use the non persistent 140da309e8cSAmir Goldstein * overlay st_ino for directories. 141da309e8cSAmir Goldstein */ 142da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 143da309e8cSAmir Goldstein stat->ino = dentry->d_inode->i_ino; 144b7bf9908SAmir Goldstein } else { 145da309e8cSAmir Goldstein /* 146da309e8cSAmir Goldstein * For non-samefs setup, if we cannot map all layers st_ino 147da309e8cSAmir Goldstein * to a unified address space, we need to make sure that st_dev 148b7bf9908SAmir Goldstein * is unique per underlying fs, so we use the unique anonymous 149b7bf9908SAmir Goldstein * bdev assigned to the underlying fs. 150da309e8cSAmir Goldstein */ 15107f1e596SAmir Goldstein stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev; 152da309e8cSAmir Goldstein } 153da309e8cSAmir Goldstein 154da309e8cSAmir Goldstein return 0; 155da309e8cSAmir Goldstein } 156da309e8cSAmir Goldstein 1575b712091SMiklos Szeredi int ovl_getattr(const struct path *path, struct kstat *stat, 158a528d35eSDavid Howells u32 request_mask, unsigned int flags) 159e9be9d5eSMiklos Szeredi { 160a528d35eSDavid Howells struct dentry *dentry = path->dentry; 16172b608f0SAmir Goldstein enum ovl_path_type type; 162e9be9d5eSMiklos Szeredi struct path realpath; 1631175b6b8SVivek Goyal const struct cred *old_cred; 1645b712091SMiklos Szeredi bool is_dir = S_ISDIR(dentry->d_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 17772b608f0SAmir Goldstein /* 178da309e8cSAmir Goldstein * For non-dir or same fs, we use st_ino of the copy up origin. 179da309e8cSAmir Goldstein * This guaranties constant st_dev/st_ino across copy up. 180e487d889SAmir Goldstein * With xino feature and non-samefs, we use st_ino of the copy up 181e487d889SAmir Goldstein * origin masked with high bits that represent the layer id. 18272b608f0SAmir Goldstein * 183da309e8cSAmir Goldstein * If lower filesystem supports NFS file handles, this also guaranties 18472b608f0SAmir Goldstein * persistent st_ino across mount cycle. 18572b608f0SAmir Goldstein */ 1860f831ec8SAmir Goldstein if (!is_dir || ovl_same_dev(dentry->d_sb)) { 187da309e8cSAmir Goldstein if (!OVL_TYPE_UPPER(type)) { 18807f1e596SAmir Goldstein fsid = ovl_layer_lower(dentry)->fsid; 189da309e8cSAmir Goldstein } else if (OVL_TYPE_ORIGIN(type)) { 19072b608f0SAmir Goldstein struct kstat lowerstat; 19167d756c2SVivek Goyal u32 lowermask = STATX_INO | STATX_BLOCKS | 19267d756c2SVivek Goyal (!is_dir ? STATX_NLINK : 0); 19372b608f0SAmir Goldstein 19472b608f0SAmir Goldstein ovl_path_lower(dentry, &realpath); 19572b608f0SAmir Goldstein err = vfs_getattr(&realpath, &lowerstat, 1965b712091SMiklos Szeredi lowermask, flags); 19772b608f0SAmir Goldstein if (err) 19872b608f0SAmir Goldstein goto out; 19972b608f0SAmir Goldstein 20072b608f0SAmir Goldstein /* 201359f392cSAmir Goldstein * Lower hardlinks may be broken on copy up to different 20272b608f0SAmir Goldstein * upper files, so we cannot use the lower origin st_ino 20372b608f0SAmir Goldstein * for those different files, even for the same fs case. 20486eaa130SAmir Goldstein * 20586eaa130SAmir Goldstein * Similarly, several redirected dirs can point to the 20686eaa130SAmir Goldstein * same dir on a lower layer. With the "verify_lower" 20786eaa130SAmir Goldstein * feature, we do not use the lower origin st_ino, if 20886eaa130SAmir Goldstein * we haven't verified that this redirect is unique. 20986eaa130SAmir Goldstein * 210359f392cSAmir Goldstein * With inodes index enabled, it is safe to use st_ino 21186eaa130SAmir Goldstein * of an indexed origin. The index validates that the 21286eaa130SAmir Goldstein * upper hardlink is not broken and that a redirected 21386eaa130SAmir Goldstein * dir is the only redirect to that origin. 21472b608f0SAmir Goldstein */ 21586eaa130SAmir Goldstein if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || 21686eaa130SAmir Goldstein (!ovl_verify_lower(dentry->d_sb) && 2179f99e50dSAmir Goldstein (is_dir || lowerstat.nlink == 1))) { 21807f1e596SAmir Goldstein fsid = ovl_layer_lower(dentry)->fsid; 2199c6d8f13SAmir Goldstein stat->ino = lowerstat.ino; 22072b608f0SAmir Goldstein } 22167d756c2SVivek Goyal 22267d756c2SVivek Goyal /* 22367d756c2SVivek Goyal * If we are querying a metacopy dentry and lower 22467d756c2SVivek Goyal * dentry is data dentry, then use the blocks we 22567d756c2SVivek Goyal * queried just now. We don't have to do additional 22667d756c2SVivek Goyal * vfs_getattr(). If lower itself is metacopy, then 22767d756c2SVivek Goyal * additional vfs_getattr() is unavoidable. 22867d756c2SVivek Goyal */ 22967d756c2SVivek Goyal if (metacopy_blocks && 23067d756c2SVivek Goyal realpath.dentry == ovl_dentry_lowerdata(dentry)) { 23167d756c2SVivek Goyal stat->blocks = lowerstat.blocks; 23267d756c2SVivek Goyal metacopy_blocks = false; 23367d756c2SVivek Goyal } 23467d756c2SVivek Goyal } 23567d756c2SVivek Goyal 23667d756c2SVivek Goyal if (metacopy_blocks) { 23767d756c2SVivek Goyal /* 23867d756c2SVivek Goyal * If lower is not same as lowerdata or if there was 23967d756c2SVivek Goyal * no origin on upper, we can end up here. 24067d756c2SVivek Goyal */ 24167d756c2SVivek Goyal struct kstat lowerdatastat; 24267d756c2SVivek Goyal u32 lowermask = STATX_BLOCKS; 24367d756c2SVivek Goyal 24467d756c2SVivek Goyal ovl_path_lowerdata(dentry, &realpath); 24567d756c2SVivek Goyal err = vfs_getattr(&realpath, &lowerdatastat, 24667d756c2SVivek Goyal lowermask, flags); 24767d756c2SVivek Goyal if (err) 24867d756c2SVivek Goyal goto out; 24967d756c2SVivek Goyal stat->blocks = lowerdatastat.blocks; 2509f99e50dSAmir Goldstein } 251a0c5ad30SAmir Goldstein } 252da309e8cSAmir Goldstein 25307f1e596SAmir Goldstein err = ovl_map_dev_ino(dentry, stat, fsid); 254da309e8cSAmir Goldstein if (err) 255da309e8cSAmir Goldstein goto out; 2565b712091SMiklos Szeredi 2575b712091SMiklos Szeredi /* 2585b712091SMiklos Szeredi * It's probably not worth it to count subdirs to get the 2595b712091SMiklos Szeredi * correct link count. nlink=1 seems to pacify 'find' and 2605b712091SMiklos Szeredi * other utilities. 2615b712091SMiklos Szeredi */ 2625b712091SMiklos Szeredi if (is_dir && OVL_TYPE_MERGE(type)) 2635b712091SMiklos Szeredi stat->nlink = 1; 2645b712091SMiklos Szeredi 2655f8415d6SAmir Goldstein /* 2665f8415d6SAmir Goldstein * Return the overlay inode nlinks for indexed upper inodes. 2675f8415d6SAmir Goldstein * Overlay inode nlink counts the union of the upper hardlinks 2685f8415d6SAmir Goldstein * and non-covered lower hardlinks. It does not include the upper 2695f8415d6SAmir Goldstein * index hardlink. 2705f8415d6SAmir Goldstein */ 2715f8415d6SAmir Goldstein if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) 2725f8415d6SAmir Goldstein stat->nlink = dentry->d_inode->i_nlink; 2735f8415d6SAmir Goldstein 27472b608f0SAmir Goldstein out: 2751175b6b8SVivek Goyal revert_creds(old_cred); 27672b608f0SAmir Goldstein 2771175b6b8SVivek Goyal return err; 278e9be9d5eSMiklos Szeredi } 279e9be9d5eSMiklos Szeredi 280e9be9d5eSMiklos Szeredi int ovl_permission(struct inode *inode, int mask) 281e9be9d5eSMiklos Szeredi { 28209d8b586SMiklos Szeredi struct inode *upperinode = ovl_inode_upper(inode); 28309d8b586SMiklos Szeredi struct inode *realinode = upperinode ?: ovl_inode_lower(inode); 284c0ca3d70SVivek Goyal const struct cred *old_cred; 285e9be9d5eSMiklos Szeredi int err; 286e9be9d5eSMiklos Szeredi 287e9be9d5eSMiklos Szeredi /* Careful in RCU walk mode */ 288e9be9d5eSMiklos Szeredi if (!realinode) { 289e9be9d5eSMiklos Szeredi WARN_ON(!(mask & MAY_NOT_BLOCK)); 290a999d7e1SMiklos Szeredi return -ECHILD; 291e9be9d5eSMiklos Szeredi } 292e9be9d5eSMiklos Szeredi 293c0ca3d70SVivek Goyal /* 294c0ca3d70SVivek Goyal * Check overlay inode with the creds of task and underlying inode 295c0ca3d70SVivek Goyal * with creds of mounter 296c0ca3d70SVivek Goyal */ 297c0ca3d70SVivek Goyal err = generic_permission(inode, mask); 298c0ca3d70SVivek Goyal if (err) 299c0ca3d70SVivek Goyal return err; 300c0ca3d70SVivek Goyal 301ec7ba118SMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 302ec7ba118SMiklos Szeredi if (!upperinode && 303ec7ba118SMiklos Szeredi !special_file(realinode->i_mode) && mask & MAY_WRITE) { 304754f8cb7SVivek Goyal mask &= ~(MAY_WRITE | MAY_APPEND); 305ec7ba118SMiklos Szeredi /* Make sure mounter can read file for copy up later */ 306500cac3cSVivek Goyal mask |= MAY_READ; 307500cac3cSVivek Goyal } 3089c630ebeSMiklos Szeredi err = inode_permission(realinode, mask); 309c0ca3d70SVivek Goyal revert_creds(old_cred); 310c0ca3d70SVivek Goyal 311c0ca3d70SVivek Goyal return err; 312e9be9d5eSMiklos Szeredi } 313e9be9d5eSMiklos Szeredi 3146b255391SAl Viro static const char *ovl_get_link(struct dentry *dentry, 315fceef393SAl Viro struct inode *inode, 316fceef393SAl Viro struct delayed_call *done) 317e9be9d5eSMiklos Szeredi { 3181175b6b8SVivek Goyal const struct cred *old_cred; 3191175b6b8SVivek Goyal const char *p; 320e9be9d5eSMiklos Szeredi 3216b255391SAl Viro if (!dentry) 3226b255391SAl Viro return ERR_PTR(-ECHILD); 3236b255391SAl Viro 3241175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3257764235bSMiklos Szeredi p = vfs_get_link(ovl_dentry_real(dentry), done); 3261175b6b8SVivek Goyal revert_creds(old_cred); 3271175b6b8SVivek Goyal return p; 328e9be9d5eSMiklos Szeredi } 329e9be9d5eSMiklos Szeredi 3300956254aSMiklos Szeredi bool ovl_is_private_xattr(const char *name) 331e9be9d5eSMiklos Szeredi { 332fe2b7595SAndreas Gruenbacher return strncmp(name, OVL_XATTR_PREFIX, 333fe2b7595SAndreas Gruenbacher sizeof(OVL_XATTR_PREFIX) - 1) == 0; 334e9be9d5eSMiklos Szeredi } 335e9be9d5eSMiklos Szeredi 3361d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 3371d88f183SMiklos Szeredi const void *value, size_t size, int flags) 338e9be9d5eSMiklos Szeredi { 339e9be9d5eSMiklos Szeredi int err; 3401d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 3411d88f183SMiklos Szeredi struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); 3421175b6b8SVivek Goyal const struct cred *old_cred; 343e9be9d5eSMiklos Szeredi 344e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 345e9be9d5eSMiklos Szeredi if (err) 346e9be9d5eSMiklos Szeredi goto out; 347e9be9d5eSMiklos Szeredi 3481d88f183SMiklos Szeredi if (!value && !upperdentry) { 3491d88f183SMiklos Szeredi err = vfs_getxattr(realdentry, name, NULL, 0); 3500e585cccSAndreas Gruenbacher if (err < 0) 3510e585cccSAndreas Gruenbacher goto out_drop_write; 3520e585cccSAndreas Gruenbacher } 3530e585cccSAndreas Gruenbacher 3541d88f183SMiklos Szeredi if (!upperdentry) { 355e9be9d5eSMiklos Szeredi err = ovl_copy_up(dentry); 356e9be9d5eSMiklos Szeredi if (err) 357e9be9d5eSMiklos Szeredi goto out_drop_write; 358e9be9d5eSMiklos Szeredi 3591d88f183SMiklos Szeredi realdentry = ovl_dentry_upper(dentry); 3601d88f183SMiklos Szeredi } 3610e585cccSAndreas Gruenbacher 3621175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3630e585cccSAndreas Gruenbacher if (value) 3641d88f183SMiklos Szeredi err = vfs_setxattr(realdentry, name, value, size, flags); 3650e585cccSAndreas Gruenbacher else { 3660e585cccSAndreas Gruenbacher WARN_ON(flags != XATTR_REPLACE); 3671d88f183SMiklos Szeredi err = vfs_removexattr(realdentry, name); 3680e585cccSAndreas Gruenbacher } 3691175b6b8SVivek Goyal revert_creds(old_cred); 370e9be9d5eSMiklos Szeredi 371d9854c87SMiklos Szeredi /* copy c/mtime */ 372d9854c87SMiklos Szeredi ovl_copyattr(d_inode(realdentry), inode); 373d9854c87SMiklos Szeredi 374e9be9d5eSMiklos Szeredi out_drop_write: 375e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 376e9be9d5eSMiklos Szeredi out: 377e9be9d5eSMiklos Szeredi return err; 378e9be9d5eSMiklos Szeredi } 379e9be9d5eSMiklos Szeredi 3801d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 3810eb45fc3SAndreas Gruenbacher void *value, size_t size) 382e9be9d5eSMiklos Szeredi { 3831175b6b8SVivek Goyal ssize_t res; 3841175b6b8SVivek Goyal const struct cred *old_cred; 3851d88f183SMiklos Szeredi struct dentry *realdentry = 3861d88f183SMiklos Szeredi ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry); 38752148463SMiklos Szeredi 3881175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3891175b6b8SVivek Goyal res = vfs_getxattr(realdentry, name, value, size); 3901175b6b8SVivek Goyal revert_creds(old_cred); 3911175b6b8SVivek Goyal return res; 392e9be9d5eSMiklos Szeredi } 393e9be9d5eSMiklos Szeredi 394a082c6f6SMiklos Szeredi static bool ovl_can_list(const char *s) 395a082c6f6SMiklos Szeredi { 396a082c6f6SMiklos Szeredi /* List all non-trusted xatts */ 397a082c6f6SMiklos Szeredi if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) 398a082c6f6SMiklos Szeredi return true; 399a082c6f6SMiklos Szeredi 400a082c6f6SMiklos Szeredi /* Never list trusted.overlay, list other trusted for superuser only */ 4015c2e9f34SMark Salyzyn return !ovl_is_private_xattr(s) && 4025c2e9f34SMark Salyzyn ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN); 403a082c6f6SMiklos Szeredi } 404a082c6f6SMiklos Szeredi 405e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) 406e9be9d5eSMiklos Szeredi { 407b581755bSMiklos Szeredi struct dentry *realdentry = ovl_dentry_real(dentry); 408e9be9d5eSMiklos Szeredi ssize_t res; 4097cb35119SMiklos Szeredi size_t len; 4107cb35119SMiklos Szeredi char *s; 4111175b6b8SVivek Goyal const struct cred *old_cred; 412e9be9d5eSMiklos Szeredi 4131175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 414b581755bSMiklos Szeredi res = vfs_listxattr(realdentry, list, size); 4151175b6b8SVivek Goyal revert_creds(old_cred); 416e9be9d5eSMiklos Szeredi if (res <= 0 || size == 0) 417e9be9d5eSMiklos Szeredi return res; 418e9be9d5eSMiklos Szeredi 419e9be9d5eSMiklos Szeredi /* filter out private xattrs */ 4207cb35119SMiklos Szeredi for (s = list, len = res; len;) { 4217cb35119SMiklos Szeredi size_t slen = strnlen(s, len) + 1; 422e9be9d5eSMiklos Szeredi 4237cb35119SMiklos Szeredi /* underlying fs providing us with an broken xattr list? */ 4247cb35119SMiklos Szeredi if (WARN_ON(slen > len)) 4257cb35119SMiklos Szeredi return -EIO; 426e9be9d5eSMiklos Szeredi 4277cb35119SMiklos Szeredi len -= slen; 428a082c6f6SMiklos Szeredi if (!ovl_can_list(s)) { 429e9be9d5eSMiklos Szeredi res -= slen; 4307cb35119SMiklos Szeredi memmove(s, s + slen, len); 431e9be9d5eSMiklos Szeredi } else { 4327cb35119SMiklos Szeredi s += slen; 433e9be9d5eSMiklos Szeredi } 434e9be9d5eSMiklos Szeredi } 435e9be9d5eSMiklos Szeredi 436e9be9d5eSMiklos Szeredi return res; 437e9be9d5eSMiklos Szeredi } 438e9be9d5eSMiklos Szeredi 43939a25b2bSVivek Goyal struct posix_acl *ovl_get_acl(struct inode *inode, int type) 44039a25b2bSVivek Goyal { 44109d8b586SMiklos Szeredi struct inode *realinode = ovl_inode_real(inode); 4421175b6b8SVivek Goyal const struct cred *old_cred; 4431175b6b8SVivek Goyal struct posix_acl *acl; 44439a25b2bSVivek Goyal 4455201dc44SMiklos Szeredi if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) 44639a25b2bSVivek Goyal return NULL; 44739a25b2bSVivek Goyal 4481175b6b8SVivek Goyal old_cred = ovl_override_creds(inode->i_sb); 4495201dc44SMiklos Szeredi acl = get_acl(realinode, type); 4501175b6b8SVivek Goyal revert_creds(old_cred); 4511175b6b8SVivek Goyal 4521175b6b8SVivek Goyal return acl; 45339a25b2bSVivek Goyal } 45439a25b2bSVivek Goyal 45595582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags) 456d719e8f2SMiklos Szeredi { 4578f35cf51SMiklos Szeredi if (flags & S_ATIME) { 4588f35cf51SMiklos Szeredi struct ovl_fs *ofs = inode->i_sb->s_fs_info; 4598f35cf51SMiklos Szeredi struct path upperpath = { 46008f4c7c8SMiklos Szeredi .mnt = ovl_upper_mnt(ofs), 4618f35cf51SMiklos Szeredi .dentry = ovl_upperdentry_dereference(OVL_I(inode)), 4628f35cf51SMiklos Szeredi }; 463d719e8f2SMiklos Szeredi 464d719e8f2SMiklos Szeredi if (upperpath.dentry) { 465d719e8f2SMiklos Szeredi touch_atime(&upperpath); 466d719e8f2SMiklos Szeredi inode->i_atime = d_inode(upperpath.dentry)->i_atime; 467d719e8f2SMiklos Szeredi } 4688f35cf51SMiklos Szeredi } 469d719e8f2SMiklos Szeredi return 0; 470d719e8f2SMiklos Szeredi } 471d719e8f2SMiklos Szeredi 4729e142c41SMiklos Szeredi static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 4739e142c41SMiklos Szeredi u64 start, u64 len) 4749e142c41SMiklos Szeredi { 4759e142c41SMiklos Szeredi int err; 4769e142c41SMiklos Szeredi struct inode *realinode = ovl_inode_real(inode); 4779e142c41SMiklos Szeredi const struct cred *old_cred; 4789e142c41SMiklos Szeredi 4799e142c41SMiklos Szeredi if (!realinode->i_op->fiemap) 4809e142c41SMiklos Szeredi return -EOPNOTSUPP; 4819e142c41SMiklos Szeredi 4829e142c41SMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 4839e142c41SMiklos Szeredi err = realinode->i_op->fiemap(realinode, fieinfo, start, len); 4849e142c41SMiklos Szeredi revert_creds(old_cred); 4859e142c41SMiklos Szeredi 4869e142c41SMiklos Szeredi return err; 4879e142c41SMiklos Szeredi } 4889e142c41SMiklos Szeredi 489e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_file_inode_operations = { 490e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 491e9be9d5eSMiklos Szeredi .permission = ovl_permission, 492e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 493e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 49439a25b2bSVivek Goyal .get_acl = ovl_get_acl, 495d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 4969e142c41SMiklos Szeredi .fiemap = ovl_fiemap, 497e9be9d5eSMiklos Szeredi }; 498e9be9d5eSMiklos Szeredi 499e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_symlink_inode_operations = { 500e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 5016b255391SAl Viro .get_link = ovl_get_link, 502e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 503e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 504d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 505e9be9d5eSMiklos Szeredi }; 506e9be9d5eSMiklos Szeredi 5079e142c41SMiklos Szeredi static const struct inode_operations ovl_special_inode_operations = { 5089e142c41SMiklos Szeredi .setattr = ovl_setattr, 5099e142c41SMiklos Szeredi .permission = ovl_permission, 5109e142c41SMiklos Szeredi .getattr = ovl_getattr, 5119e142c41SMiklos Szeredi .listxattr = ovl_listxattr, 5129e142c41SMiklos Szeredi .get_acl = ovl_get_acl, 5139e142c41SMiklos Szeredi .update_time = ovl_update_time, 5149e142c41SMiklos Szeredi }; 5159e142c41SMiklos Szeredi 51669383c59SWei Yongjun static const struct address_space_operations ovl_aops = { 5175b910bd6SAmir Goldstein /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */ 5185b910bd6SAmir Goldstein .direct_IO = noop_direct_IO, 5195b910bd6SAmir Goldstein }; 5205b910bd6SAmir Goldstein 521b1eaa950SAmir Goldstein /* 522b1eaa950SAmir Goldstein * It is possible to stack overlayfs instance on top of another 523a5a84682SChengguang Xu * overlayfs instance as lower layer. We need to annotate the 524b1eaa950SAmir Goldstein * stackable i_mutex locks according to stack level of the super 525b1eaa950SAmir Goldstein * block instance. An overlayfs instance can never be in stack 526b1eaa950SAmir Goldstein * depth 0 (there is always a real fs below it). An overlayfs 527b1eaa950SAmir Goldstein * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth]. 528b1eaa950SAmir Goldstein * 529b1eaa950SAmir Goldstein * For example, here is a snip from /proc/lockdep_chains after 530b1eaa950SAmir Goldstein * dir_iterate of nested overlayfs: 531b1eaa950SAmir Goldstein * 532b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) 533b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) 534b1eaa950SAmir Goldstein * [...] &type->i_mutex_dir_key (stack_depth=0) 535b1f9d385SAmir Goldstein * 536b1f9d385SAmir Goldstein * Locking order w.r.t ovl_want_write() is important for nested overlayfs. 537b1f9d385SAmir Goldstein * 538b1f9d385SAmir Goldstein * This chain is valid: 539b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[2]) 540b1f9d385SAmir Goldstein * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 541b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[2]) 542b1f9d385SAmir Goldstein * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 543b1f9d385SAmir Goldstein * 544b1f9d385SAmir Goldstein * And this chain is valid: 545b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[2]) 546b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[2]) 547b1f9d385SAmir Goldstein * - lowerinode->i_rwsem (inode_lock[1]) 548b1f9d385SAmir Goldstein * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) 549b1f9d385SAmir Goldstein * 550b1f9d385SAmir Goldstein * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is 551b1f9d385SAmir Goldstein * held, because it is in reverse order of the non-nested case using the same 552b1f9d385SAmir Goldstein * upper fs: 553b1f9d385SAmir Goldstein * - inode->i_rwsem (inode_lock[1]) 554b1f9d385SAmir Goldstein * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) 555b1f9d385SAmir Goldstein * - OVL_I(inode)->lock (ovl_inode_lock[1]) 556b1eaa950SAmir Goldstein */ 557b1eaa950SAmir Goldstein #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH 558b1eaa950SAmir Goldstein 559b1eaa950SAmir Goldstein static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) 560b1eaa950SAmir Goldstein { 561b1eaa950SAmir Goldstein #ifdef CONFIG_LOCKDEP 562b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; 563b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; 5644eae06deSAmir Goldstein static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; 565b1eaa950SAmir Goldstein 566b1eaa950SAmir Goldstein int depth = inode->i_sb->s_stack_depth - 1; 567b1eaa950SAmir Goldstein 568b1eaa950SAmir Goldstein if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) 569b1eaa950SAmir Goldstein depth = 0; 570b1eaa950SAmir Goldstein 571b1eaa950SAmir Goldstein if (S_ISDIR(inode->i_mode)) 572b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); 573b1eaa950SAmir Goldstein else 574b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); 5754eae06deSAmir Goldstein 5764eae06deSAmir Goldstein lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); 577b1eaa950SAmir Goldstein #endif 578b1eaa950SAmir Goldstein } 579b1eaa950SAmir Goldstein 5804d314f78SAmir Goldstein static void ovl_next_ino(struct inode *inode) 5814d314f78SAmir Goldstein { 5824d314f78SAmir Goldstein struct ovl_fs *ofs = inode->i_sb->s_fs_info; 5834d314f78SAmir Goldstein 5844d314f78SAmir Goldstein inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 5854d314f78SAmir Goldstein if (unlikely(!inode->i_ino)) 5864d314f78SAmir Goldstein inode->i_ino = atomic_long_inc_return(&ofs->last_ino); 5874d314f78SAmir Goldstein } 5884d314f78SAmir Goldstein 58962c832edSAmir Goldstein static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) 590e9be9d5eSMiklos Szeredi { 59112574a9fSAmir Goldstein int xinobits = ovl_xino_bits(inode->i_sb); 592dfe51d47SAmir Goldstein unsigned int xinoshift = 64 - xinobits; 59312574a9fSAmir Goldstein 594695b46e7SAmir Goldstein /* 5956dde1e42SAmir Goldstein * When d_ino is consistent with st_ino (samefs or i_ino has enough 5966dde1e42SAmir Goldstein * bits to encode layer), set the same value used for st_ino to i_ino, 5976dde1e42SAmir Goldstein * so inode number exposed via /proc/locks and a like will be 5986dde1e42SAmir Goldstein * consistent with d_ino and st_ino values. An i_ino value inconsistent 599735c907dSAmir Goldstein * with d_ino also causes nfsd readdirplus to fail. 600695b46e7SAmir Goldstein */ 601695b46e7SAmir Goldstein inode->i_ino = ino; 602dfe51d47SAmir Goldstein if (ovl_same_fs(inode->i_sb)) { 603dfe51d47SAmir Goldstein return; 604dfe51d47SAmir Goldstein } else if (xinobits && likely(!(ino >> xinoshift))) { 605dfe51d47SAmir Goldstein inode->i_ino |= (unsigned long)fsid << (xinoshift + 1); 606dfe51d47SAmir Goldstein return; 607dfe51d47SAmir Goldstein } 608dfe51d47SAmir Goldstein 609dfe51d47SAmir Goldstein /* 610dfe51d47SAmir Goldstein * For directory inodes on non-samefs with xino disabled or xino 611dfe51d47SAmir Goldstein * overflow, we allocate a non-persistent inode number, to be used for 612dfe51d47SAmir Goldstein * resolving st_ino collisions in ovl_map_dev_ino(). 613dfe51d47SAmir Goldstein * 614dfe51d47SAmir Goldstein * To avoid ino collision with legitimate xino values from upper 615dfe51d47SAmir Goldstein * layer (fsid 0), use the lowest xinobit to map the non 616dfe51d47SAmir Goldstein * persistent inode numbers to the unified st_ino address space. 617dfe51d47SAmir Goldstein */ 618dfe51d47SAmir Goldstein if (S_ISDIR(inode->i_mode)) { 6194d314f78SAmir Goldstein ovl_next_ino(inode); 620dfe51d47SAmir Goldstein if (xinobits) { 621dfe51d47SAmir Goldstein inode->i_ino &= ~0UL >> xinobits; 622dfe51d47SAmir Goldstein inode->i_ino |= 1UL << xinoshift; 623dfe51d47SAmir Goldstein } 62412574a9fSAmir Goldstein } 62562c832edSAmir Goldstein } 62662c832edSAmir Goldstein 62762c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 62862c832edSAmir Goldstein unsigned long ino, int fsid) 62962c832edSAmir Goldstein { 63062c832edSAmir Goldstein struct inode *realinode; 63162c832edSAmir Goldstein 63262c832edSAmir Goldstein if (oip->upperdentry) 63362c832edSAmir Goldstein OVL_I(inode)->__upperdentry = oip->upperdentry; 63462c832edSAmir Goldstein if (oip->lowerpath && oip->lowerpath->dentry) 63562c832edSAmir Goldstein OVL_I(inode)->lower = igrab(d_inode(oip->lowerpath->dentry)); 63662c832edSAmir Goldstein if (oip->lowerdata) 63762c832edSAmir Goldstein OVL_I(inode)->lowerdata = igrab(d_inode(oip->lowerdata)); 63862c832edSAmir Goldstein 63962c832edSAmir Goldstein realinode = ovl_inode_real(inode); 64062c832edSAmir Goldstein ovl_copyattr(realinode, inode); 64162c832edSAmir Goldstein ovl_copyflags(realinode, inode); 64262c832edSAmir Goldstein ovl_map_ino(inode, ino, fsid); 64362c832edSAmir Goldstein } 64462c832edSAmir Goldstein 64562c832edSAmir Goldstein static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) 64662c832edSAmir Goldstein { 647e9be9d5eSMiklos Szeredi inode->i_mode = mode; 648d719e8f2SMiklos Szeredi inode->i_flags |= S_NOCMTIME; 6492a3a2a3fSMiklos Szeredi #ifdef CONFIG_FS_POSIX_ACL 6502a3a2a3fSMiklos Szeredi inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; 6512a3a2a3fSMiklos Szeredi #endif 652e9be9d5eSMiklos Szeredi 653b1eaa950SAmir Goldstein ovl_lockdep_annotate_inode_mutex_key(inode); 654b1eaa950SAmir Goldstein 655ca4c8a3aSMiklos Szeredi switch (mode & S_IFMT) { 656ca4c8a3aSMiklos Szeredi case S_IFREG: 657ca4c8a3aSMiklos Szeredi inode->i_op = &ovl_file_inode_operations; 658d1d04ef8SMiklos Szeredi inode->i_fop = &ovl_file_operations; 6595b910bd6SAmir Goldstein inode->i_mapping->a_ops = &ovl_aops; 660ca4c8a3aSMiklos Szeredi break; 661ca4c8a3aSMiklos Szeredi 662e9be9d5eSMiklos Szeredi case S_IFDIR: 663e9be9d5eSMiklos Szeredi inode->i_op = &ovl_dir_inode_operations; 664e9be9d5eSMiklos Szeredi inode->i_fop = &ovl_dir_operations; 665e9be9d5eSMiklos Szeredi break; 666e9be9d5eSMiklos Szeredi 667e9be9d5eSMiklos Szeredi case S_IFLNK: 668e9be9d5eSMiklos Szeredi inode->i_op = &ovl_symlink_inode_operations; 669e9be9d5eSMiklos Szeredi break; 670e9be9d5eSMiklos Szeredi 67151f7e52dSMiklos Szeredi default: 6729e142c41SMiklos Szeredi inode->i_op = &ovl_special_inode_operations; 673ca4c8a3aSMiklos Szeredi init_special_inode(inode, mode, rdev); 674e9be9d5eSMiklos Szeredi break; 67551f7e52dSMiklos Szeredi } 67651f7e52dSMiklos Szeredi } 677e9be9d5eSMiklos Szeredi 6785f8415d6SAmir Goldstein /* 6795f8415d6SAmir Goldstein * With inodes index enabled, an overlay inode nlink counts the union of upper 6805f8415d6SAmir Goldstein * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure 6815f8415d6SAmir Goldstein * upper inode, the following nlink modifying operations can happen: 6825f8415d6SAmir Goldstein * 6835f8415d6SAmir Goldstein * 1. Lower hardlink copy up 6845f8415d6SAmir Goldstein * 2. Upper hardlink created, unlinked or renamed over 6855f8415d6SAmir Goldstein * 3. Lower hardlink whiteout or renamed over 6865f8415d6SAmir Goldstein * 6875f8415d6SAmir Goldstein * For the first, copy up case, the union nlink does not change, whether the 6885f8415d6SAmir Goldstein * operation succeeds or fails, but the upper inode nlink may change. 6895f8415d6SAmir Goldstein * Therefore, before copy up, we store the union nlink value relative to the 6905f8415d6SAmir Goldstein * lower inode nlink in the index inode xattr trusted.overlay.nlink. 6915f8415d6SAmir Goldstein * 6925f8415d6SAmir Goldstein * For the second, upper hardlink case, the union nlink should be incremented 6935f8415d6SAmir Goldstein * or decremented IFF the operation succeeds, aligned with nlink change of the 6945f8415d6SAmir Goldstein * upper inode. Therefore, before link/unlink/rename, we store the union nlink 6955f8415d6SAmir Goldstein * value relative to the upper inode nlink in the index inode. 6965f8415d6SAmir Goldstein * 6975f8415d6SAmir Goldstein * For the last, lower cover up case, we simplify things by preceding the 6985f8415d6SAmir Goldstein * whiteout or cover up with copy up. This makes sure that there is an index 6995f8415d6SAmir Goldstein * upper inode where the nlink xattr can be stored before the copied up upper 7005f8415d6SAmir Goldstein * entry is unlink. 7015f8415d6SAmir Goldstein */ 7025f8415d6SAmir Goldstein #define OVL_NLINK_ADD_UPPER (1 << 0) 7035f8415d6SAmir Goldstein 7045f8415d6SAmir Goldstein /* 7055f8415d6SAmir Goldstein * On-disk format for indexed nlink: 7065f8415d6SAmir Goldstein * 7075f8415d6SAmir Goldstein * nlink relative to the upper inode - "U[+-]NUM" 7085f8415d6SAmir Goldstein * nlink relative to the lower inode - "L[+-]NUM" 7095f8415d6SAmir Goldstein */ 7105f8415d6SAmir Goldstein 7115f8415d6SAmir Goldstein static int ovl_set_nlink_common(struct dentry *dentry, 7125f8415d6SAmir Goldstein struct dentry *realdentry, const char *format) 7135f8415d6SAmir Goldstein { 7145f8415d6SAmir Goldstein struct inode *inode = d_inode(dentry); 7155f8415d6SAmir Goldstein struct inode *realinode = d_inode(realdentry); 7165f8415d6SAmir Goldstein char buf[13]; 7175f8415d6SAmir Goldstein int len; 7185f8415d6SAmir Goldstein 7195f8415d6SAmir Goldstein len = snprintf(buf, sizeof(buf), format, 7205f8415d6SAmir Goldstein (int) (inode->i_nlink - realinode->i_nlink)); 7215f8415d6SAmir Goldstein 7226787341aSMiklos Szeredi if (WARN_ON(len >= sizeof(buf))) 7236787341aSMiklos Szeredi return -EIO; 7246787341aSMiklos Szeredi 7255f8415d6SAmir Goldstein return ovl_do_setxattr(ovl_dentry_upper(dentry), 726*26150ab5SMiklos Szeredi OVL_XATTR_NLINK, buf, len); 7275f8415d6SAmir Goldstein } 7285f8415d6SAmir Goldstein 7295f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry) 7305f8415d6SAmir Goldstein { 7315f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); 7325f8415d6SAmir Goldstein } 7335f8415d6SAmir Goldstein 7345f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry) 7355f8415d6SAmir Goldstein { 7365f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); 7375f8415d6SAmir Goldstein } 7385f8415d6SAmir Goldstein 739caf70cb2SAmir Goldstein unsigned int ovl_get_nlink(struct dentry *lowerdentry, 7405f8415d6SAmir Goldstein struct dentry *upperdentry, 7415f8415d6SAmir Goldstein unsigned int fallback) 7425f8415d6SAmir Goldstein { 7435f8415d6SAmir Goldstein int nlink_diff; 7445f8415d6SAmir Goldstein int nlink; 7455f8415d6SAmir Goldstein char buf[13]; 7465f8415d6SAmir Goldstein int err; 7475f8415d6SAmir Goldstein 7485f8415d6SAmir Goldstein if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) 7495f8415d6SAmir Goldstein return fallback; 7505f8415d6SAmir Goldstein 751d5dc7486SMiklos Szeredi err = ovl_do_getxattr(upperdentry, OVL_XATTR_NLINK, 752d5dc7486SMiklos Szeredi &buf, sizeof(buf) - 1); 7535f8415d6SAmir Goldstein if (err < 0) 7545f8415d6SAmir Goldstein goto fail; 7555f8415d6SAmir Goldstein 7565f8415d6SAmir Goldstein buf[err] = '\0'; 7575f8415d6SAmir Goldstein if ((buf[0] != 'L' && buf[0] != 'U') || 7585f8415d6SAmir Goldstein (buf[1] != '+' && buf[1] != '-')) 7595f8415d6SAmir Goldstein goto fail; 7605f8415d6SAmir Goldstein 7615f8415d6SAmir Goldstein err = kstrtoint(buf + 1, 10, &nlink_diff); 7625f8415d6SAmir Goldstein if (err < 0) 7635f8415d6SAmir Goldstein goto fail; 7645f8415d6SAmir Goldstein 7655f8415d6SAmir Goldstein nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; 7665f8415d6SAmir Goldstein nlink += nlink_diff; 7675f8415d6SAmir Goldstein 7685f8415d6SAmir Goldstein if (nlink <= 0) 7695f8415d6SAmir Goldstein goto fail; 7705f8415d6SAmir Goldstein 7715f8415d6SAmir Goldstein return nlink; 7725f8415d6SAmir Goldstein 7735f8415d6SAmir Goldstein fail: 7741bd0a3aeSlijiazi pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n", 7755f8415d6SAmir Goldstein upperdentry, err); 7765f8415d6SAmir Goldstein return fallback; 7775f8415d6SAmir Goldstein } 7785f8415d6SAmir Goldstein 779ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) 78051f7e52dSMiklos Szeredi { 78151f7e52dSMiklos Szeredi struct inode *inode; 78251f7e52dSMiklos Szeredi 78351f7e52dSMiklos Szeredi inode = new_inode(sb); 78451f7e52dSMiklos Szeredi if (inode) 78562c832edSAmir Goldstein ovl_fill_inode(inode, mode, rdev); 78651f7e52dSMiklos Szeredi 78751f7e52dSMiklos Szeredi return inode; 78851f7e52dSMiklos Szeredi } 78951f7e52dSMiklos Szeredi 79051f7e52dSMiklos Szeredi static int ovl_inode_test(struct inode *inode, void *data) 79151f7e52dSMiklos Szeredi { 79225b7713aSMiklos Szeredi return inode->i_private == data; 79351f7e52dSMiklos Szeredi } 79451f7e52dSMiklos Szeredi 79551f7e52dSMiklos Szeredi static int ovl_inode_set(struct inode *inode, void *data) 79651f7e52dSMiklos Szeredi { 79725b7713aSMiklos Szeredi inode->i_private = data; 79851f7e52dSMiklos Szeredi return 0; 79951f7e52dSMiklos Szeredi } 80051f7e52dSMiklos Szeredi 801b9ac5c27SMiklos Szeredi static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, 8024b91c30aSAmir Goldstein struct dentry *upperdentry, bool strict) 803b9ac5c27SMiklos Szeredi { 8044b91c30aSAmir Goldstein /* 8054b91c30aSAmir Goldstein * For directories, @strict verify from lookup path performs consistency 8064b91c30aSAmir Goldstein * checks, so NULL lower/upper in dentry must match NULL lower/upper in 8074b91c30aSAmir Goldstein * inode. Non @strict verify from NFS handle decode path passes NULL for 8084b91c30aSAmir Goldstein * 'unknown' lower/upper. 8094b91c30aSAmir Goldstein */ 8104b91c30aSAmir Goldstein if (S_ISDIR(inode->i_mode) && strict) { 81131747edaSAmir Goldstein /* Real lower dir moved to upper layer under us? */ 81231747edaSAmir Goldstein if (!lowerdentry && ovl_inode_lower(inode)) 81331747edaSAmir Goldstein return false; 81431747edaSAmir Goldstein 81531747edaSAmir Goldstein /* Lookup of an uncovered redirect origin? */ 81631747edaSAmir Goldstein if (!upperdentry && ovl_inode_upper(inode)) 81731747edaSAmir Goldstein return false; 81831747edaSAmir Goldstein } 81931747edaSAmir Goldstein 820939ae4efSAmir Goldstein /* 821939ae4efSAmir Goldstein * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. 822939ae4efSAmir Goldstein * This happens when finding a copied up overlay inode for a renamed 823939ae4efSAmir Goldstein * or hardlinked overlay dentry and lower dentry cannot be followed 824939ae4efSAmir Goldstein * by origin because lower fs does not support file handles. 825939ae4efSAmir Goldstein */ 826939ae4efSAmir Goldstein if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) 827b9ac5c27SMiklos Szeredi return false; 828b9ac5c27SMiklos Szeredi 829b9ac5c27SMiklos Szeredi /* 830b9ac5c27SMiklos Szeredi * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. 831b9ac5c27SMiklos Szeredi * This happens when finding a lower alias for a copied up hard link. 832b9ac5c27SMiklos Szeredi */ 833b9ac5c27SMiklos Szeredi if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) 834b9ac5c27SMiklos Szeredi return false; 835b9ac5c27SMiklos Szeredi 836b9ac5c27SMiklos Szeredi return true; 837b9ac5c27SMiklos Szeredi } 838b9ac5c27SMiklos Szeredi 8394b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 8404b91c30aSAmir Goldstein bool is_upper) 8419436a1a3SAmir Goldstein { 8424b91c30aSAmir Goldstein struct inode *inode, *key = d_inode(real); 8439436a1a3SAmir Goldstein 8449436a1a3SAmir Goldstein inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 8459436a1a3SAmir Goldstein if (!inode) 8469436a1a3SAmir Goldstein return NULL; 8479436a1a3SAmir Goldstein 8484b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, is_upper ? NULL : real, 8494b91c30aSAmir Goldstein is_upper ? real : NULL, false)) { 8509436a1a3SAmir Goldstein iput(inode); 8519436a1a3SAmir Goldstein return ERR_PTR(-ESTALE); 8529436a1a3SAmir Goldstein } 8539436a1a3SAmir Goldstein 8549436a1a3SAmir Goldstein return inode; 8559436a1a3SAmir Goldstein } 8569436a1a3SAmir Goldstein 857146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir) 858146d62e5SAmir Goldstein { 859146d62e5SAmir Goldstein struct inode *key = d_inode(dir); 860146d62e5SAmir Goldstein struct inode *trap; 861146d62e5SAmir Goldstein bool res; 862146d62e5SAmir Goldstein 863146d62e5SAmir Goldstein trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 864146d62e5SAmir Goldstein if (!trap) 865146d62e5SAmir Goldstein return false; 866146d62e5SAmir Goldstein 867146d62e5SAmir Goldstein res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) && 868146d62e5SAmir Goldstein !ovl_inode_lower(trap); 869146d62e5SAmir Goldstein 870146d62e5SAmir Goldstein iput(trap); 871146d62e5SAmir Goldstein return res; 872146d62e5SAmir Goldstein } 873146d62e5SAmir Goldstein 874146d62e5SAmir Goldstein /* 875146d62e5SAmir Goldstein * Create an inode cache entry for layer root dir, that will intentionally 876146d62e5SAmir Goldstein * fail ovl_verify_inode(), so any lookup that will find some layer root 877146d62e5SAmir Goldstein * will fail. 878146d62e5SAmir Goldstein */ 879146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir) 880146d62e5SAmir Goldstein { 881146d62e5SAmir Goldstein struct inode *key = d_inode(dir); 882146d62e5SAmir Goldstein struct inode *trap; 883146d62e5SAmir Goldstein 884146d62e5SAmir Goldstein if (!d_is_dir(dir)) 885146d62e5SAmir Goldstein return ERR_PTR(-ENOTDIR); 886146d62e5SAmir Goldstein 887146d62e5SAmir Goldstein trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test, 888146d62e5SAmir Goldstein ovl_inode_set, key); 889146d62e5SAmir Goldstein if (!trap) 890146d62e5SAmir Goldstein return ERR_PTR(-ENOMEM); 891146d62e5SAmir Goldstein 892146d62e5SAmir Goldstein if (!(trap->i_state & I_NEW)) { 893146d62e5SAmir Goldstein /* Conflicting layer roots? */ 894146d62e5SAmir Goldstein iput(trap); 895146d62e5SAmir Goldstein return ERR_PTR(-ELOOP); 896146d62e5SAmir Goldstein } 897146d62e5SAmir Goldstein 898146d62e5SAmir Goldstein trap->i_mode = S_IFDIR; 899146d62e5SAmir Goldstein trap->i_flags = S_DEAD; 900146d62e5SAmir Goldstein unlock_new_inode(trap); 901146d62e5SAmir Goldstein 902146d62e5SAmir Goldstein return trap; 903146d62e5SAmir Goldstein } 904146d62e5SAmir Goldstein 905764baba8SAmir Goldstein /* 906764baba8SAmir Goldstein * Does overlay inode need to be hashed by lower inode? 907764baba8SAmir Goldstein */ 908764baba8SAmir Goldstein static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, 90974c6e384SMiklos Szeredi struct dentry *lower, bool index) 910764baba8SAmir Goldstein { 911764baba8SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 912764baba8SAmir Goldstein 913764baba8SAmir Goldstein /* No, if pure upper */ 914764baba8SAmir Goldstein if (!lower) 915764baba8SAmir Goldstein return false; 916764baba8SAmir Goldstein 917764baba8SAmir Goldstein /* Yes, if already indexed */ 918764baba8SAmir Goldstein if (index) 919764baba8SAmir Goldstein return true; 920764baba8SAmir Goldstein 921764baba8SAmir Goldstein /* Yes, if won't be copied up */ 92208f4c7c8SMiklos Szeredi if (!ovl_upper_mnt(ofs)) 923764baba8SAmir Goldstein return true; 924764baba8SAmir Goldstein 925764baba8SAmir Goldstein /* No, if lower hardlink is or will be broken on copy up */ 926764baba8SAmir Goldstein if ((upper || !ovl_indexdir(sb)) && 927764baba8SAmir Goldstein !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 928764baba8SAmir Goldstein return false; 929764baba8SAmir Goldstein 930764baba8SAmir Goldstein /* No, if non-indexed upper with NFS export */ 931764baba8SAmir Goldstein if (sb->s_export_op && upper) 932764baba8SAmir Goldstein return false; 933764baba8SAmir Goldstein 934764baba8SAmir Goldstein /* Otherwise, hash by lower inode for fsnotify */ 935764baba8SAmir Goldstein return true; 936764baba8SAmir Goldstein } 937764baba8SAmir Goldstein 93801b39dccSAmir Goldstein static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode, 93901b39dccSAmir Goldstein struct inode *key) 94001b39dccSAmir Goldstein { 94101b39dccSAmir Goldstein return newinode ? inode_insert5(newinode, (unsigned long) key, 94201b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key) : 94301b39dccSAmir Goldstein iget5_locked(sb, (unsigned long) key, 94401b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key); 94501b39dccSAmir Goldstein } 94601b39dccSAmir Goldstein 947ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb, 948ac6a52ebSVivek Goyal struct ovl_inode_params *oip) 94951f7e52dSMiklos Szeredi { 950ac6a52ebSVivek Goyal struct dentry *upperdentry = oip->upperdentry; 951ac6a52ebSVivek Goyal struct ovl_path *lowerpath = oip->lowerpath; 95209d8b586SMiklos Szeredi struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; 95351f7e52dSMiklos Szeredi struct inode *inode; 95412574a9fSAmir Goldstein struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL; 955ac6a52ebSVivek Goyal bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, 956ac6a52ebSVivek Goyal oip->index); 957300b124fSAmir Goldstein int fsid = bylower ? lowerpath->layer->fsid : 0; 95828166ab3SVivek Goyal bool is_dir; 959695b46e7SAmir Goldstein unsigned long ino = 0; 960acf3062aSAmir Goldstein int err = oip->newinode ? -EEXIST : -ENOMEM; 9616eaf0111SAmir Goldstein 96209d8b586SMiklos Szeredi if (!realinode) 96309d8b586SMiklos Szeredi realinode = d_inode(lowerdentry); 96409d8b586SMiklos Szeredi 9656eaf0111SAmir Goldstein /* 966764baba8SAmir Goldstein * Copy up origin (lower) may exist for non-indexed upper, but we must 967764baba8SAmir Goldstein * not use lower as hash key if this is a broken hardlink. 9686eaf0111SAmir Goldstein */ 96931747edaSAmir Goldstein is_dir = S_ISDIR(realinode->i_mode); 970764baba8SAmir Goldstein if (upperdentry || bylower) { 971764baba8SAmir Goldstein struct inode *key = d_inode(bylower ? lowerdentry : 972764baba8SAmir Goldstein upperdentry); 97331747edaSAmir Goldstein unsigned int nlink = is_dir ? 1 : realinode->i_nlink; 974b9ac5c27SMiklos Szeredi 97501b39dccSAmir Goldstein inode = ovl_iget5(sb, oip->newinode, key); 97609d8b586SMiklos Szeredi if (!inode) 977027065b7SVivek Goyal goto out_err; 97809d8b586SMiklos Szeredi if (!(inode->i_state & I_NEW)) { 979b9ac5c27SMiklos Szeredi /* 980b9ac5c27SMiklos Szeredi * Verify that the underlying files stored in the inode 981b9ac5c27SMiklos Szeredi * match those in the dentry. 982b9ac5c27SMiklos Szeredi */ 9834b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, lowerdentry, upperdentry, 9844b91c30aSAmir Goldstein true)) { 985b9ac5c27SMiklos Szeredi iput(inode); 986027065b7SVivek Goyal err = -ESTALE; 987027065b7SVivek Goyal goto out_err; 988b9ac5c27SMiklos Szeredi } 989b9ac5c27SMiklos Szeredi 99009d8b586SMiklos Szeredi dput(upperdentry); 9919cec54c8SVivek Goyal kfree(oip->redirect); 99209d8b586SMiklos Szeredi goto out; 99309d8b586SMiklos Szeredi } 994e9be9d5eSMiklos Szeredi 99531747edaSAmir Goldstein /* Recalculate nlink for non-dir due to indexing */ 99631747edaSAmir Goldstein if (!is_dir) 99731747edaSAmir Goldstein nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink); 9985f8415d6SAmir Goldstein set_nlink(inode, nlink); 999695b46e7SAmir Goldstein ino = key->i_ino; 1000e6d2ebddSMiklos Szeredi } else { 1001764baba8SAmir Goldstein /* Lower hardlink that will be broken on copy up */ 10020aceb53eSAmir Goldstein inode = new_inode(sb); 1003027065b7SVivek Goyal if (!inode) { 1004027065b7SVivek Goyal err = -ENOMEM; 1005027065b7SVivek Goyal goto out_err; 1006027065b7SVivek Goyal } 1007300b124fSAmir Goldstein ino = realinode->i_ino; 1008300b124fSAmir Goldstein fsid = lowerpath->layer->fsid; 1009e6d2ebddSMiklos Szeredi } 101062c832edSAmir Goldstein ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); 101162c832edSAmir Goldstein ovl_inode_init(inode, oip, ino, fsid); 101213c72075SMiklos Szeredi 101313c72075SMiklos Szeredi if (upperdentry && ovl_is_impuredir(upperdentry)) 101413c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, inode); 101513c72075SMiklos Szeredi 1016ac6a52ebSVivek Goyal if (oip->index) 10170471a9cdSVivek Goyal ovl_set_flag(OVL_INDEX, inode); 10180471a9cdSVivek Goyal 10199cec54c8SVivek Goyal OVL_I(inode)->redirect = oip->redirect; 10209cec54c8SVivek Goyal 1021a00c2d59SVivek Goyal if (bylower) 1022a00c2d59SVivek Goyal ovl_set_flag(OVL_CONST_INO, inode); 1023a00c2d59SVivek Goyal 1024b79e05aaSAmir Goldstein /* Check for non-merge dir that may have whiteouts */ 102531747edaSAmir Goldstein if (is_dir) { 1026ac6a52ebSVivek Goyal if (((upperdentry && lowerdentry) || oip->numlower > 1) || 1027b79e05aaSAmir Goldstein ovl_check_origin_xattr(upperdentry ?: lowerdentry)) { 1028b79e05aaSAmir Goldstein ovl_set_flag(OVL_WHITEOUTS, inode); 1029b79e05aaSAmir Goldstein } 1030b79e05aaSAmir Goldstein } 1031b79e05aaSAmir Goldstein 1032e6d2ebddSMiklos Szeredi if (inode->i_state & I_NEW) 1033e6d2ebddSMiklos Szeredi unlock_new_inode(inode); 1034e6d2ebddSMiklos Szeredi out: 1035e9be9d5eSMiklos Szeredi return inode; 1036b9ac5c27SMiklos Szeredi 1037027065b7SVivek Goyal out_err: 10381bd0a3aeSlijiazi pr_warn_ratelimited("failed to get inode (%i)\n", err); 1039027065b7SVivek Goyal inode = ERR_PTR(err); 1040b9ac5c27SMiklos Szeredi goto out; 1041e9be9d5eSMiklos Szeredi } 1042