1e9be9d5eSMiklos Szeredi /* 2e9be9d5eSMiklos Szeredi * 3e9be9d5eSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 4e9be9d5eSMiklos Szeredi * 5e9be9d5eSMiklos Szeredi * This program is free software; you can redistribute it and/or modify it 6e9be9d5eSMiklos Szeredi * under the terms of the GNU General Public License version 2 as published by 7e9be9d5eSMiklos Szeredi * the Free Software Foundation. 8e9be9d5eSMiklos Szeredi */ 9e9be9d5eSMiklos Szeredi 10e9be9d5eSMiklos Szeredi #include <linux/fs.h> 11e9be9d5eSMiklos Szeredi #include <linux/slab.h> 125b825c3aSIngo Molnar #include <linux/cred.h> 13e9be9d5eSMiklos Szeredi #include <linux/xattr.h> 145201dc44SMiklos Szeredi #include <linux/posix_acl.h> 155f8415d6SAmir Goldstein #include <linux/ratelimit.h> 16e9be9d5eSMiklos Szeredi #include "overlayfs.h" 17e9be9d5eSMiklos Szeredi 18ba1e563cSChandan Rajendra 19e9be9d5eSMiklos Szeredi int ovl_setattr(struct dentry *dentry, struct iattr *attr) 20e9be9d5eSMiklos Szeredi { 21e9be9d5eSMiklos Szeredi int err; 22*997336f2SVivek Goyal bool full_copy_up = false; 23e9be9d5eSMiklos Szeredi struct dentry *upperdentry; 241175b6b8SVivek Goyal const struct cred *old_cred; 25e9be9d5eSMiklos Szeredi 2631051c85SJan Kara err = setattr_prepare(dentry, attr); 27cf9a6784SMiklos Szeredi if (err) 28cf9a6784SMiklos Szeredi return err; 29cf9a6784SMiklos Szeredi 30e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 31e9be9d5eSMiklos Szeredi if (err) 32e9be9d5eSMiklos Szeredi goto out; 33e9be9d5eSMiklos Szeredi 345812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 355812160eSMiklos Szeredi struct inode *realinode = d_inode(ovl_dentry_real(dentry)); 365812160eSMiklos Szeredi 375812160eSMiklos Szeredi err = -ETXTBSY; 385812160eSMiklos Szeredi if (atomic_read(&realinode->i_writecount) < 0) 395812160eSMiklos Szeredi goto out_drop_write; 40*997336f2SVivek Goyal 41*997336f2SVivek Goyal /* Truncate should trigger data copy up as well */ 42*997336f2SVivek Goyal full_copy_up = true; 435812160eSMiklos Szeredi } 445812160eSMiklos Szeredi 45*997336f2SVivek Goyal if (!full_copy_up) 46acff81ecSMiklos Szeredi err = ovl_copy_up(dentry); 47*997336f2SVivek Goyal else 48*997336f2SVivek Goyal err = ovl_copy_up_with_data(dentry); 49acff81ecSMiklos Szeredi if (!err) { 505812160eSMiklos Szeredi struct inode *winode = NULL; 515812160eSMiklos Szeredi 52e9be9d5eSMiklos Szeredi upperdentry = ovl_dentry_upper(dentry); 53acff81ecSMiklos Szeredi 545812160eSMiklos Szeredi if (attr->ia_valid & ATTR_SIZE) { 555812160eSMiklos Szeredi winode = d_inode(upperdentry); 565812160eSMiklos Szeredi err = get_write_access(winode); 575812160eSMiklos Szeredi if (err) 585812160eSMiklos Szeredi goto out_drop_write; 595812160eSMiklos Szeredi } 605812160eSMiklos Szeredi 61b99c2d91SMiklos Szeredi if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) 62b99c2d91SMiklos Szeredi attr->ia_valid &= ~ATTR_MODE; 63b99c2d91SMiklos Szeredi 645955102cSAl Viro inode_lock(upperdentry->d_inode); 651175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 66e9be9d5eSMiklos Szeredi err = notify_change(upperdentry, attr, NULL); 671175b6b8SVivek Goyal revert_creds(old_cred); 68b81de061SKonstantin Khlebnikov if (!err) 69b81de061SKonstantin Khlebnikov ovl_copyattr(upperdentry->d_inode, dentry->d_inode); 705955102cSAl Viro inode_unlock(upperdentry->d_inode); 715812160eSMiklos Szeredi 725812160eSMiklos Szeredi if (winode) 735812160eSMiklos Szeredi put_write_access(winode); 74e9be9d5eSMiklos Szeredi } 755812160eSMiklos Szeredi out_drop_write: 76e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 77e9be9d5eSMiklos Szeredi out: 78e9be9d5eSMiklos Szeredi return err; 79e9be9d5eSMiklos Szeredi } 80e9be9d5eSMiklos Szeredi 81da309e8cSAmir Goldstein static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, 82da309e8cSAmir Goldstein struct ovl_layer *lower_layer) 83da309e8cSAmir Goldstein { 84da309e8cSAmir Goldstein bool samefs = ovl_same_sb(dentry->d_sb); 85e487d889SAmir Goldstein unsigned int xinobits = ovl_xino_bits(dentry->d_sb); 86da309e8cSAmir Goldstein 87da309e8cSAmir Goldstein if (samefs) { 88da309e8cSAmir Goldstein /* 89da309e8cSAmir Goldstein * When all layers are on the same fs, all real inode 90da309e8cSAmir Goldstein * number are unique, so we use the overlay st_dev, 91da309e8cSAmir Goldstein * which is friendly to du -x. 92da309e8cSAmir Goldstein */ 93da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 94e487d889SAmir Goldstein return 0; 95e487d889SAmir Goldstein } else if (xinobits) { 96e487d889SAmir Goldstein unsigned int shift = 64 - xinobits; 97e487d889SAmir Goldstein /* 98e487d889SAmir Goldstein * All inode numbers of underlying fs should not be using the 99e487d889SAmir Goldstein * high xinobits, so we use high xinobits to partition the 100e487d889SAmir Goldstein * overlay st_ino address space. The high bits holds the fsid 101e487d889SAmir Goldstein * (upper fsid is 0). This way overlay inode numbers are unique 102e487d889SAmir Goldstein * and all inodes use overlay st_dev. Inode numbers are also 103e487d889SAmir Goldstein * persistent for a given layer configuration. 104e487d889SAmir Goldstein */ 105e487d889SAmir Goldstein if (stat->ino >> shift) { 106e487d889SAmir Goldstein pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n", 107e487d889SAmir Goldstein dentry, stat->ino, xinobits); 108e487d889SAmir Goldstein } else { 109e487d889SAmir Goldstein if (lower_layer) 110e487d889SAmir Goldstein stat->ino |= ((u64)lower_layer->fsid) << shift; 111e487d889SAmir Goldstein 112e487d889SAmir Goldstein stat->dev = dentry->d_sb->s_dev; 113e487d889SAmir Goldstein return 0; 114e487d889SAmir Goldstein } 115e487d889SAmir Goldstein } 116e487d889SAmir Goldstein 117e487d889SAmir Goldstein /* The inode could not be mapped to a unified st_ino address space */ 118e487d889SAmir Goldstein if (S_ISDIR(dentry->d_inode->i_mode)) { 119da309e8cSAmir Goldstein /* 120da309e8cSAmir Goldstein * Always use the overlay st_dev for directories, so 'find 121da309e8cSAmir Goldstein * -xdev' will scan the entire overlay mount and won't cross the 122da309e8cSAmir Goldstein * overlay mount boundaries. 123da309e8cSAmir Goldstein * 124da309e8cSAmir Goldstein * If not all layers are on the same fs the pair {real st_ino; 125da309e8cSAmir Goldstein * overlay st_dev} is not unique, so use the non persistent 126da309e8cSAmir Goldstein * overlay st_ino for directories. 127da309e8cSAmir Goldstein */ 128da309e8cSAmir Goldstein stat->dev = dentry->d_sb->s_dev; 129da309e8cSAmir Goldstein stat->ino = dentry->d_inode->i_ino; 1305148626bSAmir Goldstein } else if (lower_layer && lower_layer->fsid) { 131da309e8cSAmir Goldstein /* 132da309e8cSAmir Goldstein * For non-samefs setup, if we cannot map all layers st_ino 133da309e8cSAmir Goldstein * to a unified address space, we need to make sure that st_dev 1345148626bSAmir Goldstein * is unique per lower fs. Upper layer uses real st_dev and 1355148626bSAmir Goldstein * lower layers use the unique anonymous bdev assigned to the 1365148626bSAmir Goldstein * lower fs. 137da309e8cSAmir Goldstein */ 1385148626bSAmir Goldstein stat->dev = lower_layer->fs->pseudo_dev; 139da309e8cSAmir Goldstein } 140da309e8cSAmir Goldstein 141da309e8cSAmir Goldstein return 0; 142da309e8cSAmir Goldstein } 143da309e8cSAmir Goldstein 1445b712091SMiklos Szeredi int ovl_getattr(const struct path *path, struct kstat *stat, 145a528d35eSDavid Howells u32 request_mask, unsigned int flags) 146e9be9d5eSMiklos Szeredi { 147a528d35eSDavid Howells struct dentry *dentry = path->dentry; 14872b608f0SAmir Goldstein enum ovl_path_type type; 149e9be9d5eSMiklos Szeredi struct path realpath; 1501175b6b8SVivek Goyal const struct cred *old_cred; 1515b712091SMiklos Szeredi bool is_dir = S_ISDIR(dentry->d_inode->i_mode); 152a0c5ad30SAmir Goldstein bool samefs = ovl_same_sb(dentry->d_sb); 153da309e8cSAmir Goldstein struct ovl_layer *lower_layer = NULL; 1541175b6b8SVivek Goyal int err; 15567d756c2SVivek Goyal bool metacopy_blocks = false; 15667d756c2SVivek Goyal 15767d756c2SVivek Goyal metacopy_blocks = ovl_is_metacopy_dentry(dentry); 158e9be9d5eSMiklos Szeredi 15972b608f0SAmir Goldstein type = ovl_path_real(dentry, &realpath); 1601175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 161a528d35eSDavid Howells err = vfs_getattr(&realpath, stat, request_mask, flags); 16272b608f0SAmir Goldstein if (err) 16372b608f0SAmir Goldstein goto out; 16472b608f0SAmir Goldstein 16572b608f0SAmir Goldstein /* 166da309e8cSAmir Goldstein * For non-dir or same fs, we use st_ino of the copy up origin. 167da309e8cSAmir Goldstein * This guaranties constant st_dev/st_ino across copy up. 168e487d889SAmir Goldstein * With xino feature and non-samefs, we use st_ino of the copy up 169e487d889SAmir Goldstein * origin masked with high bits that represent the layer id. 17072b608f0SAmir Goldstein * 171da309e8cSAmir Goldstein * If lower filesystem supports NFS file handles, this also guaranties 17272b608f0SAmir Goldstein * persistent st_ino across mount cycle. 17372b608f0SAmir Goldstein */ 174e487d889SAmir Goldstein if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) { 175da309e8cSAmir Goldstein if (!OVL_TYPE_UPPER(type)) { 176da309e8cSAmir Goldstein lower_layer = ovl_layer_lower(dentry); 177da309e8cSAmir Goldstein } else if (OVL_TYPE_ORIGIN(type)) { 17872b608f0SAmir Goldstein struct kstat lowerstat; 17967d756c2SVivek Goyal u32 lowermask = STATX_INO | STATX_BLOCKS | 18067d756c2SVivek Goyal (!is_dir ? STATX_NLINK : 0); 18172b608f0SAmir Goldstein 18272b608f0SAmir Goldstein ovl_path_lower(dentry, &realpath); 18372b608f0SAmir Goldstein err = vfs_getattr(&realpath, &lowerstat, 1845b712091SMiklos Szeredi lowermask, flags); 18572b608f0SAmir Goldstein if (err) 18672b608f0SAmir Goldstein goto out; 18772b608f0SAmir Goldstein 18872b608f0SAmir Goldstein /* 189359f392cSAmir Goldstein * Lower hardlinks may be broken on copy up to different 19072b608f0SAmir Goldstein * upper files, so we cannot use the lower origin st_ino 19172b608f0SAmir Goldstein * for those different files, even for the same fs case. 19286eaa130SAmir Goldstein * 19386eaa130SAmir Goldstein * Similarly, several redirected dirs can point to the 19486eaa130SAmir Goldstein * same dir on a lower layer. With the "verify_lower" 19586eaa130SAmir Goldstein * feature, we do not use the lower origin st_ino, if 19686eaa130SAmir Goldstein * we haven't verified that this redirect is unique. 19786eaa130SAmir Goldstein * 198359f392cSAmir Goldstein * With inodes index enabled, it is safe to use st_ino 19986eaa130SAmir Goldstein * of an indexed origin. The index validates that the 20086eaa130SAmir Goldstein * upper hardlink is not broken and that a redirected 20186eaa130SAmir Goldstein * dir is the only redirect to that origin. 20272b608f0SAmir Goldstein */ 20386eaa130SAmir Goldstein if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || 20486eaa130SAmir Goldstein (!ovl_verify_lower(dentry->d_sb) && 2059f99e50dSAmir Goldstein (is_dir || lowerstat.nlink == 1))) { 20672b608f0SAmir Goldstein stat->ino = lowerstat.ino; 207da309e8cSAmir Goldstein lower_layer = ovl_layer_lower(dentry); 20872b608f0SAmir Goldstein } 20967d756c2SVivek Goyal 21067d756c2SVivek Goyal /* 21167d756c2SVivek Goyal * If we are querying a metacopy dentry and lower 21267d756c2SVivek Goyal * dentry is data dentry, then use the blocks we 21367d756c2SVivek Goyal * queried just now. We don't have to do additional 21467d756c2SVivek Goyal * vfs_getattr(). If lower itself is metacopy, then 21567d756c2SVivek Goyal * additional vfs_getattr() is unavoidable. 21667d756c2SVivek Goyal */ 21767d756c2SVivek Goyal if (metacopy_blocks && 21867d756c2SVivek Goyal realpath.dentry == ovl_dentry_lowerdata(dentry)) { 21967d756c2SVivek Goyal stat->blocks = lowerstat.blocks; 22067d756c2SVivek Goyal metacopy_blocks = false; 22167d756c2SVivek Goyal } 22267d756c2SVivek Goyal } 22367d756c2SVivek Goyal 22467d756c2SVivek Goyal if (metacopy_blocks) { 22567d756c2SVivek Goyal /* 22667d756c2SVivek Goyal * If lower is not same as lowerdata or if there was 22767d756c2SVivek Goyal * no origin on upper, we can end up here. 22867d756c2SVivek Goyal */ 22967d756c2SVivek Goyal struct kstat lowerdatastat; 23067d756c2SVivek Goyal u32 lowermask = STATX_BLOCKS; 23167d756c2SVivek Goyal 23267d756c2SVivek Goyal ovl_path_lowerdata(dentry, &realpath); 23367d756c2SVivek Goyal err = vfs_getattr(&realpath, &lowerdatastat, 23467d756c2SVivek Goyal lowermask, flags); 23567d756c2SVivek Goyal if (err) 23667d756c2SVivek Goyal goto out; 23767d756c2SVivek Goyal stat->blocks = lowerdatastat.blocks; 2389f99e50dSAmir Goldstein } 239a0c5ad30SAmir Goldstein } 240da309e8cSAmir Goldstein 241da309e8cSAmir Goldstein err = ovl_map_dev_ino(dentry, stat, lower_layer); 242da309e8cSAmir Goldstein if (err) 243da309e8cSAmir Goldstein goto out; 2445b712091SMiklos Szeredi 2455b712091SMiklos Szeredi /* 2465b712091SMiklos Szeredi * It's probably not worth it to count subdirs to get the 2475b712091SMiklos Szeredi * correct link count. nlink=1 seems to pacify 'find' and 2485b712091SMiklos Szeredi * other utilities. 2495b712091SMiklos Szeredi */ 2505b712091SMiklos Szeredi if (is_dir && OVL_TYPE_MERGE(type)) 2515b712091SMiklos Szeredi stat->nlink = 1; 2525b712091SMiklos Szeredi 2535f8415d6SAmir Goldstein /* 2545f8415d6SAmir Goldstein * Return the overlay inode nlinks for indexed upper inodes. 2555f8415d6SAmir Goldstein * Overlay inode nlink counts the union of the upper hardlinks 2565f8415d6SAmir Goldstein * and non-covered lower hardlinks. It does not include the upper 2575f8415d6SAmir Goldstein * index hardlink. 2585f8415d6SAmir Goldstein */ 2595f8415d6SAmir Goldstein if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) 2605f8415d6SAmir Goldstein stat->nlink = dentry->d_inode->i_nlink; 2615f8415d6SAmir Goldstein 26272b608f0SAmir Goldstein out: 2631175b6b8SVivek Goyal revert_creds(old_cred); 26472b608f0SAmir Goldstein 2651175b6b8SVivek Goyal return err; 266e9be9d5eSMiklos Szeredi } 267e9be9d5eSMiklos Szeredi 268e9be9d5eSMiklos Szeredi int ovl_permission(struct inode *inode, int mask) 269e9be9d5eSMiklos Szeredi { 27009d8b586SMiklos Szeredi struct inode *upperinode = ovl_inode_upper(inode); 27109d8b586SMiklos Szeredi struct inode *realinode = upperinode ?: ovl_inode_lower(inode); 272c0ca3d70SVivek Goyal const struct cred *old_cred; 273e9be9d5eSMiklos Szeredi int err; 274e9be9d5eSMiklos Szeredi 275e9be9d5eSMiklos Szeredi /* Careful in RCU walk mode */ 276e9be9d5eSMiklos Szeredi if (!realinode) { 277e9be9d5eSMiklos Szeredi WARN_ON(!(mask & MAY_NOT_BLOCK)); 278a999d7e1SMiklos Szeredi return -ECHILD; 279e9be9d5eSMiklos Szeredi } 280e9be9d5eSMiklos Szeredi 281c0ca3d70SVivek Goyal /* 282c0ca3d70SVivek Goyal * Check overlay inode with the creds of task and underlying inode 283c0ca3d70SVivek Goyal * with creds of mounter 284c0ca3d70SVivek Goyal */ 285c0ca3d70SVivek Goyal err = generic_permission(inode, mask); 286c0ca3d70SVivek Goyal if (err) 287c0ca3d70SVivek Goyal return err; 288c0ca3d70SVivek Goyal 289c0ca3d70SVivek Goyal old_cred = ovl_override_creds(inode->i_sb); 29009d8b586SMiklos Szeredi if (!upperinode && 29109d8b586SMiklos Szeredi !special_file(realinode->i_mode) && mask & MAY_WRITE) { 292754f8cb7SVivek Goyal mask &= ~(MAY_WRITE | MAY_APPEND); 293500cac3cSVivek Goyal /* Make sure mounter can read file for copy up later */ 294500cac3cSVivek Goyal mask |= MAY_READ; 295500cac3cSVivek Goyal } 2969c630ebeSMiklos Szeredi err = inode_permission(realinode, mask); 297c0ca3d70SVivek Goyal revert_creds(old_cred); 298c0ca3d70SVivek Goyal 299c0ca3d70SVivek Goyal return err; 300e9be9d5eSMiklos Szeredi } 301e9be9d5eSMiklos Szeredi 3026b255391SAl Viro static const char *ovl_get_link(struct dentry *dentry, 303fceef393SAl Viro struct inode *inode, 304fceef393SAl Viro struct delayed_call *done) 305e9be9d5eSMiklos Szeredi { 3061175b6b8SVivek Goyal const struct cred *old_cred; 3071175b6b8SVivek Goyal const char *p; 308e9be9d5eSMiklos Szeredi 3096b255391SAl Viro if (!dentry) 3106b255391SAl Viro return ERR_PTR(-ECHILD); 3116b255391SAl Viro 3121175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3137764235bSMiklos Szeredi p = vfs_get_link(ovl_dentry_real(dentry), done); 3141175b6b8SVivek Goyal revert_creds(old_cred); 3151175b6b8SVivek Goyal return p; 316e9be9d5eSMiklos Szeredi } 317e9be9d5eSMiklos Szeredi 3180956254aSMiklos Szeredi bool ovl_is_private_xattr(const char *name) 319e9be9d5eSMiklos Szeredi { 320fe2b7595SAndreas Gruenbacher return strncmp(name, OVL_XATTR_PREFIX, 321fe2b7595SAndreas Gruenbacher sizeof(OVL_XATTR_PREFIX) - 1) == 0; 322e9be9d5eSMiklos Szeredi } 323e9be9d5eSMiklos Szeredi 3241d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 3251d88f183SMiklos Szeredi const void *value, size_t size, int flags) 326e9be9d5eSMiklos Szeredi { 327e9be9d5eSMiklos Szeredi int err; 3281d88f183SMiklos Szeredi struct dentry *upperdentry = ovl_i_dentry_upper(inode); 3291d88f183SMiklos Szeredi struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); 3301175b6b8SVivek Goyal const struct cred *old_cred; 331e9be9d5eSMiklos Szeredi 332e9be9d5eSMiklos Szeredi err = ovl_want_write(dentry); 333e9be9d5eSMiklos Szeredi if (err) 334e9be9d5eSMiklos Szeredi goto out; 335e9be9d5eSMiklos Szeredi 3361d88f183SMiklos Szeredi if (!value && !upperdentry) { 3371d88f183SMiklos Szeredi err = vfs_getxattr(realdentry, name, NULL, 0); 3380e585cccSAndreas Gruenbacher if (err < 0) 3390e585cccSAndreas Gruenbacher goto out_drop_write; 3400e585cccSAndreas Gruenbacher } 3410e585cccSAndreas Gruenbacher 3421d88f183SMiklos Szeredi if (!upperdentry) { 343e9be9d5eSMiklos Szeredi err = ovl_copy_up(dentry); 344e9be9d5eSMiklos Szeredi if (err) 345e9be9d5eSMiklos Szeredi goto out_drop_write; 346e9be9d5eSMiklos Szeredi 3471d88f183SMiklos Szeredi realdentry = ovl_dentry_upper(dentry); 3481d88f183SMiklos Szeredi } 3490e585cccSAndreas Gruenbacher 3501175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3510e585cccSAndreas Gruenbacher if (value) 3521d88f183SMiklos Szeredi err = vfs_setxattr(realdentry, name, value, size, flags); 3530e585cccSAndreas Gruenbacher else { 3540e585cccSAndreas Gruenbacher WARN_ON(flags != XATTR_REPLACE); 3551d88f183SMiklos Szeredi err = vfs_removexattr(realdentry, name); 3560e585cccSAndreas Gruenbacher } 3571175b6b8SVivek Goyal revert_creds(old_cred); 358e9be9d5eSMiklos Szeredi 359d9854c87SMiklos Szeredi /* copy c/mtime */ 360d9854c87SMiklos Szeredi ovl_copyattr(d_inode(realdentry), inode); 361d9854c87SMiklos Szeredi 362e9be9d5eSMiklos Szeredi out_drop_write: 363e9be9d5eSMiklos Szeredi ovl_drop_write(dentry); 364e9be9d5eSMiklos Szeredi out: 365e9be9d5eSMiklos Szeredi return err; 366e9be9d5eSMiklos Szeredi } 367e9be9d5eSMiklos Szeredi 3681d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 3690eb45fc3SAndreas Gruenbacher void *value, size_t size) 370e9be9d5eSMiklos Szeredi { 3711175b6b8SVivek Goyal ssize_t res; 3721175b6b8SVivek Goyal const struct cred *old_cred; 3731d88f183SMiklos Szeredi struct dentry *realdentry = 3741d88f183SMiklos Szeredi ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry); 37552148463SMiklos Szeredi 3761175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 3771175b6b8SVivek Goyal res = vfs_getxattr(realdentry, name, value, size); 3781175b6b8SVivek Goyal revert_creds(old_cred); 3791175b6b8SVivek Goyal return res; 380e9be9d5eSMiklos Szeredi } 381e9be9d5eSMiklos Szeredi 382a082c6f6SMiklos Szeredi static bool ovl_can_list(const char *s) 383a082c6f6SMiklos Szeredi { 384a082c6f6SMiklos Szeredi /* List all non-trusted xatts */ 385a082c6f6SMiklos Szeredi if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) 386a082c6f6SMiklos Szeredi return true; 387a082c6f6SMiklos Szeredi 388a082c6f6SMiklos Szeredi /* Never list trusted.overlay, list other trusted for superuser only */ 389a082c6f6SMiklos Szeredi return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN); 390a082c6f6SMiklos Szeredi } 391a082c6f6SMiklos Szeredi 392e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) 393e9be9d5eSMiklos Szeredi { 394b581755bSMiklos Szeredi struct dentry *realdentry = ovl_dentry_real(dentry); 395e9be9d5eSMiklos Szeredi ssize_t res; 3967cb35119SMiklos Szeredi size_t len; 3977cb35119SMiklos Szeredi char *s; 3981175b6b8SVivek Goyal const struct cred *old_cred; 399e9be9d5eSMiklos Szeredi 4001175b6b8SVivek Goyal old_cred = ovl_override_creds(dentry->d_sb); 401b581755bSMiklos Szeredi res = vfs_listxattr(realdentry, list, size); 4021175b6b8SVivek Goyal revert_creds(old_cred); 403e9be9d5eSMiklos Szeredi if (res <= 0 || size == 0) 404e9be9d5eSMiklos Szeredi return res; 405e9be9d5eSMiklos Szeredi 406e9be9d5eSMiklos Szeredi /* filter out private xattrs */ 4077cb35119SMiklos Szeredi for (s = list, len = res; len;) { 4087cb35119SMiklos Szeredi size_t slen = strnlen(s, len) + 1; 409e9be9d5eSMiklos Szeredi 4107cb35119SMiklos Szeredi /* underlying fs providing us with an broken xattr list? */ 4117cb35119SMiklos Szeredi if (WARN_ON(slen > len)) 4127cb35119SMiklos Szeredi return -EIO; 413e9be9d5eSMiklos Szeredi 4147cb35119SMiklos Szeredi len -= slen; 415a082c6f6SMiklos Szeredi if (!ovl_can_list(s)) { 416e9be9d5eSMiklos Szeredi res -= slen; 4177cb35119SMiklos Szeredi memmove(s, s + slen, len); 418e9be9d5eSMiklos Szeredi } else { 4197cb35119SMiklos Szeredi s += slen; 420e9be9d5eSMiklos Szeredi } 421e9be9d5eSMiklos Szeredi } 422e9be9d5eSMiklos Szeredi 423e9be9d5eSMiklos Szeredi return res; 424e9be9d5eSMiklos Szeredi } 425e9be9d5eSMiklos Szeredi 42639a25b2bSVivek Goyal struct posix_acl *ovl_get_acl(struct inode *inode, int type) 42739a25b2bSVivek Goyal { 42809d8b586SMiklos Szeredi struct inode *realinode = ovl_inode_real(inode); 4291175b6b8SVivek Goyal const struct cred *old_cred; 4301175b6b8SVivek Goyal struct posix_acl *acl; 43139a25b2bSVivek Goyal 4325201dc44SMiklos Szeredi if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) 43339a25b2bSVivek Goyal return NULL; 43439a25b2bSVivek Goyal 4351175b6b8SVivek Goyal old_cred = ovl_override_creds(inode->i_sb); 4365201dc44SMiklos Szeredi acl = get_acl(realinode, type); 4371175b6b8SVivek Goyal revert_creds(old_cred); 4381175b6b8SVivek Goyal 4391175b6b8SVivek Goyal return acl; 44039a25b2bSVivek Goyal } 44139a25b2bSVivek Goyal 44295582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags) 443d719e8f2SMiklos Szeredi { 4448f35cf51SMiklos Szeredi if (flags & S_ATIME) { 4458f35cf51SMiklos Szeredi struct ovl_fs *ofs = inode->i_sb->s_fs_info; 4468f35cf51SMiklos Szeredi struct path upperpath = { 4478f35cf51SMiklos Szeredi .mnt = ofs->upper_mnt, 4488f35cf51SMiklos Szeredi .dentry = ovl_upperdentry_dereference(OVL_I(inode)), 4498f35cf51SMiklos Szeredi }; 450d719e8f2SMiklos Szeredi 451d719e8f2SMiklos Szeredi if (upperpath.dentry) { 452d719e8f2SMiklos Szeredi touch_atime(&upperpath); 453d719e8f2SMiklos Szeredi inode->i_atime = d_inode(upperpath.dentry)->i_atime; 454d719e8f2SMiklos Szeredi } 4558f35cf51SMiklos Szeredi } 456d719e8f2SMiklos Szeredi return 0; 457d719e8f2SMiklos Szeredi } 458d719e8f2SMiklos Szeredi 4599e142c41SMiklos Szeredi static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 4609e142c41SMiklos Szeredi u64 start, u64 len) 4619e142c41SMiklos Szeredi { 4629e142c41SMiklos Szeredi int err; 4639e142c41SMiklos Szeredi struct inode *realinode = ovl_inode_real(inode); 4649e142c41SMiklos Szeredi const struct cred *old_cred; 4659e142c41SMiklos Szeredi 4669e142c41SMiklos Szeredi if (!realinode->i_op->fiemap) 4679e142c41SMiklos Szeredi return -EOPNOTSUPP; 4689e142c41SMiklos Szeredi 4699e142c41SMiklos Szeredi old_cred = ovl_override_creds(inode->i_sb); 4709e142c41SMiklos Szeredi err = realinode->i_op->fiemap(realinode, fieinfo, start, len); 4719e142c41SMiklos Szeredi revert_creds(old_cred); 4729e142c41SMiklos Szeredi 4739e142c41SMiklos Szeredi return err; 4749e142c41SMiklos Szeredi } 4759e142c41SMiklos Szeredi 476e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_file_inode_operations = { 477e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 478e9be9d5eSMiklos Szeredi .permission = ovl_permission, 479e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 480e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 48139a25b2bSVivek Goyal .get_acl = ovl_get_acl, 482d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 4839e142c41SMiklos Szeredi .fiemap = ovl_fiemap, 484e9be9d5eSMiklos Szeredi }; 485e9be9d5eSMiklos Szeredi 486e9be9d5eSMiklos Szeredi static const struct inode_operations ovl_symlink_inode_operations = { 487e9be9d5eSMiklos Szeredi .setattr = ovl_setattr, 4886b255391SAl Viro .get_link = ovl_get_link, 489e9be9d5eSMiklos Szeredi .getattr = ovl_getattr, 490e9be9d5eSMiklos Szeredi .listxattr = ovl_listxattr, 491d719e8f2SMiklos Szeredi .update_time = ovl_update_time, 492e9be9d5eSMiklos Szeredi }; 493e9be9d5eSMiklos Szeredi 4949e142c41SMiklos Szeredi static const struct inode_operations ovl_special_inode_operations = { 4959e142c41SMiklos Szeredi .setattr = ovl_setattr, 4969e142c41SMiklos Szeredi .permission = ovl_permission, 4979e142c41SMiklos Szeredi .getattr = ovl_getattr, 4989e142c41SMiklos Szeredi .listxattr = ovl_listxattr, 4999e142c41SMiklos Szeredi .get_acl = ovl_get_acl, 5009e142c41SMiklos Szeredi .update_time = ovl_update_time, 5019e142c41SMiklos Szeredi }; 5029e142c41SMiklos Szeredi 503b1eaa950SAmir Goldstein /* 504b1eaa950SAmir Goldstein * It is possible to stack overlayfs instance on top of another 505b1eaa950SAmir Goldstein * overlayfs instance as lower layer. We need to annonate the 506b1eaa950SAmir Goldstein * stackable i_mutex locks according to stack level of the super 507b1eaa950SAmir Goldstein * block instance. An overlayfs instance can never be in stack 508b1eaa950SAmir Goldstein * depth 0 (there is always a real fs below it). An overlayfs 509b1eaa950SAmir Goldstein * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth]. 510b1eaa950SAmir Goldstein * 511b1eaa950SAmir Goldstein * For example, here is a snip from /proc/lockdep_chains after 512b1eaa950SAmir Goldstein * dir_iterate of nested overlayfs: 513b1eaa950SAmir Goldstein * 514b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) 515b1eaa950SAmir Goldstein * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) 516b1eaa950SAmir Goldstein * [...] &type->i_mutex_dir_key (stack_depth=0) 517b1eaa950SAmir Goldstein */ 518b1eaa950SAmir Goldstein #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH 519b1eaa950SAmir Goldstein 520b1eaa950SAmir Goldstein static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) 521b1eaa950SAmir Goldstein { 522b1eaa950SAmir Goldstein #ifdef CONFIG_LOCKDEP 523b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; 524b1eaa950SAmir Goldstein static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; 5254eae06deSAmir Goldstein static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; 526b1eaa950SAmir Goldstein 527b1eaa950SAmir Goldstein int depth = inode->i_sb->s_stack_depth - 1; 528b1eaa950SAmir Goldstein 529b1eaa950SAmir Goldstein if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) 530b1eaa950SAmir Goldstein depth = 0; 531b1eaa950SAmir Goldstein 532b1eaa950SAmir Goldstein if (S_ISDIR(inode->i_mode)) 533b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); 534b1eaa950SAmir Goldstein else 535b1eaa950SAmir Goldstein lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); 5364eae06deSAmir Goldstein 5374eae06deSAmir Goldstein lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); 538b1eaa950SAmir Goldstein #endif 539b1eaa950SAmir Goldstein } 540b1eaa950SAmir Goldstein 541695b46e7SAmir Goldstein static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, 54212574a9fSAmir Goldstein unsigned long ino, int fsid) 543e9be9d5eSMiklos Szeredi { 54412574a9fSAmir Goldstein int xinobits = ovl_xino_bits(inode->i_sb); 54512574a9fSAmir Goldstein 546695b46e7SAmir Goldstein /* 547695b46e7SAmir Goldstein * When NFS export is enabled and d_ino is consistent with st_ino 54812574a9fSAmir Goldstein * (samefs or i_ino has enough bits to encode layer), set the same 54912574a9fSAmir Goldstein * value used for d_ino to i_ino, because nfsd readdirplus compares 55012574a9fSAmir Goldstein * d_ino values to i_ino values of child entries. When called from 55112574a9fSAmir Goldstein * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real 552695b46e7SAmir Goldstein * upper inode i_ino on ovl_inode_init() or ovl_inode_update(). 553695b46e7SAmir Goldstein */ 55412574a9fSAmir Goldstein if (inode->i_sb->s_export_op && 55512574a9fSAmir Goldstein (ovl_same_sb(inode->i_sb) || xinobits)) { 556695b46e7SAmir Goldstein inode->i_ino = ino; 55712574a9fSAmir Goldstein if (xinobits && fsid && !(ino >> (64 - xinobits))) 55812574a9fSAmir Goldstein inode->i_ino |= (unsigned long)fsid << (64 - xinobits); 55912574a9fSAmir Goldstein } else { 560e9be9d5eSMiklos Szeredi inode->i_ino = get_next_ino(); 56112574a9fSAmir Goldstein } 562e9be9d5eSMiklos Szeredi inode->i_mode = mode; 563d719e8f2SMiklos Szeredi inode->i_flags |= S_NOCMTIME; 5642a3a2a3fSMiklos Szeredi #ifdef CONFIG_FS_POSIX_ACL 5652a3a2a3fSMiklos Szeredi inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; 5662a3a2a3fSMiklos Szeredi #endif 567e9be9d5eSMiklos Szeredi 568b1eaa950SAmir Goldstein ovl_lockdep_annotate_inode_mutex_key(inode); 569b1eaa950SAmir Goldstein 570ca4c8a3aSMiklos Szeredi switch (mode & S_IFMT) { 571ca4c8a3aSMiklos Szeredi case S_IFREG: 572ca4c8a3aSMiklos Szeredi inode->i_op = &ovl_file_inode_operations; 573d1d04ef8SMiklos Szeredi inode->i_fop = &ovl_file_operations; 574ca4c8a3aSMiklos Szeredi break; 575ca4c8a3aSMiklos Szeredi 576e9be9d5eSMiklos Szeredi case S_IFDIR: 577e9be9d5eSMiklos Szeredi inode->i_op = &ovl_dir_inode_operations; 578e9be9d5eSMiklos Szeredi inode->i_fop = &ovl_dir_operations; 579e9be9d5eSMiklos Szeredi break; 580e9be9d5eSMiklos Szeredi 581e9be9d5eSMiklos Szeredi case S_IFLNK: 582e9be9d5eSMiklos Szeredi inode->i_op = &ovl_symlink_inode_operations; 583e9be9d5eSMiklos Szeredi break; 584e9be9d5eSMiklos Szeredi 58551f7e52dSMiklos Szeredi default: 5869e142c41SMiklos Szeredi inode->i_op = &ovl_special_inode_operations; 587ca4c8a3aSMiklos Szeredi init_special_inode(inode, mode, rdev); 588e9be9d5eSMiklos Szeredi break; 58951f7e52dSMiklos Szeredi } 59051f7e52dSMiklos Szeredi } 591e9be9d5eSMiklos Szeredi 5925f8415d6SAmir Goldstein /* 5935f8415d6SAmir Goldstein * With inodes index enabled, an overlay inode nlink counts the union of upper 5945f8415d6SAmir Goldstein * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure 5955f8415d6SAmir Goldstein * upper inode, the following nlink modifying operations can happen: 5965f8415d6SAmir Goldstein * 5975f8415d6SAmir Goldstein * 1. Lower hardlink copy up 5985f8415d6SAmir Goldstein * 2. Upper hardlink created, unlinked or renamed over 5995f8415d6SAmir Goldstein * 3. Lower hardlink whiteout or renamed over 6005f8415d6SAmir Goldstein * 6015f8415d6SAmir Goldstein * For the first, copy up case, the union nlink does not change, whether the 6025f8415d6SAmir Goldstein * operation succeeds or fails, but the upper inode nlink may change. 6035f8415d6SAmir Goldstein * Therefore, before copy up, we store the union nlink value relative to the 6045f8415d6SAmir Goldstein * lower inode nlink in the index inode xattr trusted.overlay.nlink. 6055f8415d6SAmir Goldstein * 6065f8415d6SAmir Goldstein * For the second, upper hardlink case, the union nlink should be incremented 6075f8415d6SAmir Goldstein * or decremented IFF the operation succeeds, aligned with nlink change of the 6085f8415d6SAmir Goldstein * upper inode. Therefore, before link/unlink/rename, we store the union nlink 6095f8415d6SAmir Goldstein * value relative to the upper inode nlink in the index inode. 6105f8415d6SAmir Goldstein * 6115f8415d6SAmir Goldstein * For the last, lower cover up case, we simplify things by preceding the 6125f8415d6SAmir Goldstein * whiteout or cover up with copy up. This makes sure that there is an index 6135f8415d6SAmir Goldstein * upper inode where the nlink xattr can be stored before the copied up upper 6145f8415d6SAmir Goldstein * entry is unlink. 6155f8415d6SAmir Goldstein */ 6165f8415d6SAmir Goldstein #define OVL_NLINK_ADD_UPPER (1 << 0) 6175f8415d6SAmir Goldstein 6185f8415d6SAmir Goldstein /* 6195f8415d6SAmir Goldstein * On-disk format for indexed nlink: 6205f8415d6SAmir Goldstein * 6215f8415d6SAmir Goldstein * nlink relative to the upper inode - "U[+-]NUM" 6225f8415d6SAmir Goldstein * nlink relative to the lower inode - "L[+-]NUM" 6235f8415d6SAmir Goldstein */ 6245f8415d6SAmir Goldstein 6255f8415d6SAmir Goldstein static int ovl_set_nlink_common(struct dentry *dentry, 6265f8415d6SAmir Goldstein struct dentry *realdentry, const char *format) 6275f8415d6SAmir Goldstein { 6285f8415d6SAmir Goldstein struct inode *inode = d_inode(dentry); 6295f8415d6SAmir Goldstein struct inode *realinode = d_inode(realdentry); 6305f8415d6SAmir Goldstein char buf[13]; 6315f8415d6SAmir Goldstein int len; 6325f8415d6SAmir Goldstein 6335f8415d6SAmir Goldstein len = snprintf(buf, sizeof(buf), format, 6345f8415d6SAmir Goldstein (int) (inode->i_nlink - realinode->i_nlink)); 6355f8415d6SAmir Goldstein 6366787341aSMiklos Szeredi if (WARN_ON(len >= sizeof(buf))) 6376787341aSMiklos Szeredi return -EIO; 6386787341aSMiklos Szeredi 6395f8415d6SAmir Goldstein return ovl_do_setxattr(ovl_dentry_upper(dentry), 6405f8415d6SAmir Goldstein OVL_XATTR_NLINK, buf, len, 0); 6415f8415d6SAmir Goldstein } 6425f8415d6SAmir Goldstein 6435f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry) 6445f8415d6SAmir Goldstein { 6455f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); 6465f8415d6SAmir Goldstein } 6475f8415d6SAmir Goldstein 6485f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry) 6495f8415d6SAmir Goldstein { 6505f8415d6SAmir Goldstein return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); 6515f8415d6SAmir Goldstein } 6525f8415d6SAmir Goldstein 653caf70cb2SAmir Goldstein unsigned int ovl_get_nlink(struct dentry *lowerdentry, 6545f8415d6SAmir Goldstein struct dentry *upperdentry, 6555f8415d6SAmir Goldstein unsigned int fallback) 6565f8415d6SAmir Goldstein { 6575f8415d6SAmir Goldstein int nlink_diff; 6585f8415d6SAmir Goldstein int nlink; 6595f8415d6SAmir Goldstein char buf[13]; 6605f8415d6SAmir Goldstein int err; 6615f8415d6SAmir Goldstein 6625f8415d6SAmir Goldstein if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) 6635f8415d6SAmir Goldstein return fallback; 6645f8415d6SAmir Goldstein 6655f8415d6SAmir Goldstein err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1); 6665f8415d6SAmir Goldstein if (err < 0) 6675f8415d6SAmir Goldstein goto fail; 6685f8415d6SAmir Goldstein 6695f8415d6SAmir Goldstein buf[err] = '\0'; 6705f8415d6SAmir Goldstein if ((buf[0] != 'L' && buf[0] != 'U') || 6715f8415d6SAmir Goldstein (buf[1] != '+' && buf[1] != '-')) 6725f8415d6SAmir Goldstein goto fail; 6735f8415d6SAmir Goldstein 6745f8415d6SAmir Goldstein err = kstrtoint(buf + 1, 10, &nlink_diff); 6755f8415d6SAmir Goldstein if (err < 0) 6765f8415d6SAmir Goldstein goto fail; 6775f8415d6SAmir Goldstein 6785f8415d6SAmir Goldstein nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; 6795f8415d6SAmir Goldstein nlink += nlink_diff; 6805f8415d6SAmir Goldstein 6815f8415d6SAmir Goldstein if (nlink <= 0) 6825f8415d6SAmir Goldstein goto fail; 6835f8415d6SAmir Goldstein 6845f8415d6SAmir Goldstein return nlink; 6855f8415d6SAmir Goldstein 6865f8415d6SAmir Goldstein fail: 6875f8415d6SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n", 6885f8415d6SAmir Goldstein upperdentry, err); 6895f8415d6SAmir Goldstein return fallback; 6905f8415d6SAmir Goldstein } 6915f8415d6SAmir Goldstein 692ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) 69351f7e52dSMiklos Szeredi { 69451f7e52dSMiklos Szeredi struct inode *inode; 69551f7e52dSMiklos Szeredi 69651f7e52dSMiklos Szeredi inode = new_inode(sb); 69751f7e52dSMiklos Szeredi if (inode) 69812574a9fSAmir Goldstein ovl_fill_inode(inode, mode, rdev, 0, 0); 69951f7e52dSMiklos Szeredi 70051f7e52dSMiklos Szeredi return inode; 70151f7e52dSMiklos Szeredi } 70251f7e52dSMiklos Szeredi 70351f7e52dSMiklos Szeredi static int ovl_inode_test(struct inode *inode, void *data) 70451f7e52dSMiklos Szeredi { 70525b7713aSMiklos Szeredi return inode->i_private == data; 70651f7e52dSMiklos Szeredi } 70751f7e52dSMiklos Szeredi 70851f7e52dSMiklos Szeredi static int ovl_inode_set(struct inode *inode, void *data) 70951f7e52dSMiklos Szeredi { 71025b7713aSMiklos Szeredi inode->i_private = data; 71151f7e52dSMiklos Szeredi return 0; 71251f7e52dSMiklos Szeredi } 71351f7e52dSMiklos Szeredi 714b9ac5c27SMiklos Szeredi static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, 7154b91c30aSAmir Goldstein struct dentry *upperdentry, bool strict) 716b9ac5c27SMiklos Szeredi { 7174b91c30aSAmir Goldstein /* 7184b91c30aSAmir Goldstein * For directories, @strict verify from lookup path performs consistency 7194b91c30aSAmir Goldstein * checks, so NULL lower/upper in dentry must match NULL lower/upper in 7204b91c30aSAmir Goldstein * inode. Non @strict verify from NFS handle decode path passes NULL for 7214b91c30aSAmir Goldstein * 'unknown' lower/upper. 7224b91c30aSAmir Goldstein */ 7234b91c30aSAmir Goldstein if (S_ISDIR(inode->i_mode) && strict) { 72431747edaSAmir Goldstein /* Real lower dir moved to upper layer under us? */ 72531747edaSAmir Goldstein if (!lowerdentry && ovl_inode_lower(inode)) 72631747edaSAmir Goldstein return false; 72731747edaSAmir Goldstein 72831747edaSAmir Goldstein /* Lookup of an uncovered redirect origin? */ 72931747edaSAmir Goldstein if (!upperdentry && ovl_inode_upper(inode)) 73031747edaSAmir Goldstein return false; 73131747edaSAmir Goldstein } 73231747edaSAmir Goldstein 733939ae4efSAmir Goldstein /* 734939ae4efSAmir Goldstein * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. 735939ae4efSAmir Goldstein * This happens when finding a copied up overlay inode for a renamed 736939ae4efSAmir Goldstein * or hardlinked overlay dentry and lower dentry cannot be followed 737939ae4efSAmir Goldstein * by origin because lower fs does not support file handles. 738939ae4efSAmir Goldstein */ 739939ae4efSAmir Goldstein if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) 740b9ac5c27SMiklos Szeredi return false; 741b9ac5c27SMiklos Szeredi 742b9ac5c27SMiklos Szeredi /* 743b9ac5c27SMiklos Szeredi * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. 744b9ac5c27SMiklos Szeredi * This happens when finding a lower alias for a copied up hard link. 745b9ac5c27SMiklos Szeredi */ 746b9ac5c27SMiklos Szeredi if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) 747b9ac5c27SMiklos Szeredi return false; 748b9ac5c27SMiklos Szeredi 749b9ac5c27SMiklos Szeredi return true; 750b9ac5c27SMiklos Szeredi } 751b9ac5c27SMiklos Szeredi 7524b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 7534b91c30aSAmir Goldstein bool is_upper) 7549436a1a3SAmir Goldstein { 7554b91c30aSAmir Goldstein struct inode *inode, *key = d_inode(real); 7569436a1a3SAmir Goldstein 7579436a1a3SAmir Goldstein inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); 7589436a1a3SAmir Goldstein if (!inode) 7599436a1a3SAmir Goldstein return NULL; 7609436a1a3SAmir Goldstein 7614b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, is_upper ? NULL : real, 7624b91c30aSAmir Goldstein is_upper ? real : NULL, false)) { 7639436a1a3SAmir Goldstein iput(inode); 7649436a1a3SAmir Goldstein return ERR_PTR(-ESTALE); 7659436a1a3SAmir Goldstein } 7669436a1a3SAmir Goldstein 7679436a1a3SAmir Goldstein return inode; 7689436a1a3SAmir Goldstein } 7699436a1a3SAmir Goldstein 770764baba8SAmir Goldstein /* 771764baba8SAmir Goldstein * Does overlay inode need to be hashed by lower inode? 772764baba8SAmir Goldstein */ 773764baba8SAmir Goldstein static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, 774764baba8SAmir Goldstein struct dentry *lower, struct dentry *index) 775764baba8SAmir Goldstein { 776764baba8SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 777764baba8SAmir Goldstein 778764baba8SAmir Goldstein /* No, if pure upper */ 779764baba8SAmir Goldstein if (!lower) 780764baba8SAmir Goldstein return false; 781764baba8SAmir Goldstein 782764baba8SAmir Goldstein /* Yes, if already indexed */ 783764baba8SAmir Goldstein if (index) 784764baba8SAmir Goldstein return true; 785764baba8SAmir Goldstein 786764baba8SAmir Goldstein /* Yes, if won't be copied up */ 787764baba8SAmir Goldstein if (!ofs->upper_mnt) 788764baba8SAmir Goldstein return true; 789764baba8SAmir Goldstein 790764baba8SAmir Goldstein /* No, if lower hardlink is or will be broken on copy up */ 791764baba8SAmir Goldstein if ((upper || !ovl_indexdir(sb)) && 792764baba8SAmir Goldstein !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 793764baba8SAmir Goldstein return false; 794764baba8SAmir Goldstein 795764baba8SAmir Goldstein /* No, if non-indexed upper with NFS export */ 796764baba8SAmir Goldstein if (sb->s_export_op && upper) 797764baba8SAmir Goldstein return false; 798764baba8SAmir Goldstein 799764baba8SAmir Goldstein /* Otherwise, hash by lower inode for fsnotify */ 800764baba8SAmir Goldstein return true; 801764baba8SAmir Goldstein } 802764baba8SAmir Goldstein 80301b39dccSAmir Goldstein static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode, 80401b39dccSAmir Goldstein struct inode *key) 80501b39dccSAmir Goldstein { 80601b39dccSAmir Goldstein return newinode ? inode_insert5(newinode, (unsigned long) key, 80701b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key) : 80801b39dccSAmir Goldstein iget5_locked(sb, (unsigned long) key, 80901b39dccSAmir Goldstein ovl_inode_test, ovl_inode_set, key); 81001b39dccSAmir Goldstein } 81101b39dccSAmir Goldstein 812ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb, 813ac6a52ebSVivek Goyal struct ovl_inode_params *oip) 81451f7e52dSMiklos Szeredi { 815ac6a52ebSVivek Goyal struct dentry *upperdentry = oip->upperdentry; 816ac6a52ebSVivek Goyal struct ovl_path *lowerpath = oip->lowerpath; 81709d8b586SMiklos Szeredi struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; 81851f7e52dSMiklos Szeredi struct inode *inode; 81912574a9fSAmir Goldstein struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL; 820ac6a52ebSVivek Goyal bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, 821ac6a52ebSVivek Goyal oip->index); 822ac6a52ebSVivek Goyal int fsid = bylower ? oip->lowerpath->layer->fsid : 0; 8239d3dfea3SVivek Goyal bool is_dir, metacopy = false; 824695b46e7SAmir Goldstein unsigned long ino = 0; 825027065b7SVivek Goyal int err = -ENOMEM; 8266eaf0111SAmir Goldstein 82709d8b586SMiklos Szeredi if (!realinode) 82809d8b586SMiklos Szeredi realinode = d_inode(lowerdentry); 82909d8b586SMiklos Szeredi 8306eaf0111SAmir Goldstein /* 831764baba8SAmir Goldstein * Copy up origin (lower) may exist for non-indexed upper, but we must 832764baba8SAmir Goldstein * not use lower as hash key if this is a broken hardlink. 8336eaf0111SAmir Goldstein */ 83431747edaSAmir Goldstein is_dir = S_ISDIR(realinode->i_mode); 835764baba8SAmir Goldstein if (upperdentry || bylower) { 836764baba8SAmir Goldstein struct inode *key = d_inode(bylower ? lowerdentry : 837764baba8SAmir Goldstein upperdentry); 83831747edaSAmir Goldstein unsigned int nlink = is_dir ? 1 : realinode->i_nlink; 839b9ac5c27SMiklos Szeredi 84001b39dccSAmir Goldstein inode = ovl_iget5(sb, oip->newinode, key); 84109d8b586SMiklos Szeredi if (!inode) 842027065b7SVivek Goyal goto out_err; 84309d8b586SMiklos Szeredi if (!(inode->i_state & I_NEW)) { 844b9ac5c27SMiklos Szeredi /* 845b9ac5c27SMiklos Szeredi * Verify that the underlying files stored in the inode 846b9ac5c27SMiklos Szeredi * match those in the dentry. 847b9ac5c27SMiklos Szeredi */ 8484b91c30aSAmir Goldstein if (!ovl_verify_inode(inode, lowerdentry, upperdentry, 8494b91c30aSAmir Goldstein true)) { 850b9ac5c27SMiklos Szeredi iput(inode); 851027065b7SVivek Goyal err = -ESTALE; 852027065b7SVivek Goyal goto out_err; 853b9ac5c27SMiklos Szeredi } 854b9ac5c27SMiklos Szeredi 85509d8b586SMiklos Szeredi dput(upperdentry); 8569cec54c8SVivek Goyal kfree(oip->redirect); 85709d8b586SMiklos Szeredi goto out; 85809d8b586SMiklos Szeredi } 859e9be9d5eSMiklos Szeredi 86031747edaSAmir Goldstein /* Recalculate nlink for non-dir due to indexing */ 86131747edaSAmir Goldstein if (!is_dir) 86231747edaSAmir Goldstein nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink); 8635f8415d6SAmir Goldstein set_nlink(inode, nlink); 864695b46e7SAmir Goldstein ino = key->i_ino; 865e6d2ebddSMiklos Szeredi } else { 866764baba8SAmir Goldstein /* Lower hardlink that will be broken on copy up */ 8670aceb53eSAmir Goldstein inode = new_inode(sb); 868027065b7SVivek Goyal if (!inode) { 869027065b7SVivek Goyal err = -ENOMEM; 870027065b7SVivek Goyal goto out_err; 871027065b7SVivek Goyal } 872e6d2ebddSMiklos Szeredi } 87312574a9fSAmir Goldstein ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid); 8742664bd08SVivek Goyal ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata); 87513c72075SMiklos Szeredi 87613c72075SMiklos Szeredi if (upperdentry && ovl_is_impuredir(upperdentry)) 87713c72075SMiklos Szeredi ovl_set_flag(OVL_IMPURE, inode); 87813c72075SMiklos Szeredi 879ac6a52ebSVivek Goyal if (oip->index) 8800471a9cdSVivek Goyal ovl_set_flag(OVL_INDEX, inode); 8810471a9cdSVivek Goyal 8829d3dfea3SVivek Goyal if (upperdentry) { 8839d3dfea3SVivek Goyal err = ovl_check_metacopy_xattr(upperdentry); 8849d3dfea3SVivek Goyal if (err < 0) 8859d3dfea3SVivek Goyal goto out_err; 8869d3dfea3SVivek Goyal metacopy = err; 8879d3dfea3SVivek Goyal if (!metacopy) 8889d3dfea3SVivek Goyal ovl_set_flag(OVL_UPPERDATA, inode); 8899d3dfea3SVivek Goyal } 8909d3dfea3SVivek Goyal 8919cec54c8SVivek Goyal OVL_I(inode)->redirect = oip->redirect; 8929cec54c8SVivek Goyal 893a00c2d59SVivek Goyal if (bylower) 894a00c2d59SVivek Goyal ovl_set_flag(OVL_CONST_INO, inode); 895a00c2d59SVivek Goyal 896b79e05aaSAmir Goldstein /* Check for non-merge dir that may have whiteouts */ 89731747edaSAmir Goldstein if (is_dir) { 898ac6a52ebSVivek Goyal if (((upperdentry && lowerdentry) || oip->numlower > 1) || 899b79e05aaSAmir Goldstein ovl_check_origin_xattr(upperdentry ?: lowerdentry)) { 900b79e05aaSAmir Goldstein ovl_set_flag(OVL_WHITEOUTS, inode); 901b79e05aaSAmir Goldstein } 902b79e05aaSAmir Goldstein } 903b79e05aaSAmir Goldstein 904e6d2ebddSMiklos Szeredi if (inode->i_state & I_NEW) 905e6d2ebddSMiklos Szeredi unlock_new_inode(inode); 906e6d2ebddSMiklos Szeredi out: 907e9be9d5eSMiklos Szeredi return inode; 908b9ac5c27SMiklos Szeredi 909027065b7SVivek Goyal out_err: 910027065b7SVivek Goyal inode = ERR_PTR(err); 911b9ac5c27SMiklos Szeredi goto out; 912e9be9d5eSMiklos Szeredi } 913