From: Amir Goldstein amir73il@gmail.com
mainline inclusion from mainline-v5.7-rc1 commit 735c907d7b7df501e951ba07134b9f6f989a94e4 category: bugfix bugzilla: NA CVE: NA
-------------------------------------------------
ovl_inode_update() is no longer called from create object code path.
Fixes: 01b39dcc9568 ("ovl: use inode_insert5() to hash a newly...") Signed-off-by: Amir Goldstein amir73il@gmail.com Signed-off-by: Miklos Szeredi mszeredi@redhat.com Signed-off-by: Zheng Liang zhengliang6@huawei.com Reviewed-by: Zhang Yi yi.zhang@huawei.com Signed-off-by: Yang Yingliang yangyingliang@huawei.com --- fs/overlayfs/inode.c | 8 +++++--- fs/overlayfs/util.c | 2 -- 2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c index d1e9d926150b1..b016343a7209b 100644 --- a/fs/overlayfs/inode.c +++ b/fs/overlayfs/inode.c @@ -555,9 +555,11 @@ static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, * bits to encode layer), set the same value used for st_ino to i_ino, * so inode number exposed via /proc/locks and a like will be * consistent with d_ino and st_ino values. An i_ino value inconsistent - * with d_ino also causes nfsd readdirplus to fail. When called from - * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real - * upper inode i_ino on ovl_inode_init() or ovl_inode_update(). + * with d_ino also causes nfsd readdirplus to fail. + * + * When called from ovl_create_object() => ovl_new_inode(), with + * ino = 0, i_ino will be updated to consistent value later on in + * ovl_get_inode() => ovl_fill_inode(). */ if (ovl_same_dev(inode->i_sb)) { inode->i_ino = ino; diff --git a/fs/overlayfs/util.c b/fs/overlayfs/util.c index eb9411461b695..b83955f31ded0 100644 --- a/fs/overlayfs/util.c +++ b/fs/overlayfs/util.c @@ -419,8 +419,6 @@ void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) smp_wmb(); OVL_I(inode)->__upperdentry = upperdentry; if (inode_unhashed(inode)) { - if (!inode->i_ino) - inode->i_ino = upperinode->i_ino; inode->i_private = upperinode; __insert_inode_hash(inode, (unsigned long) upperinode); }
From: Amir Goldstein amir73il@gmail.com
mainline inclusion from mainline-v5.7-rc1 commit 2effc5c25d55b92b89303292128540618f182671 category: bugfix bugzilla: NA CVE: NA
-------------------------------------------------
Allocates and initializes the root dentry and inode.
Signed-off-by: Amir Goldstein amir73il@gmail.com Signed-off-by: Miklos Szeredi mszeredi@redhat.com Signed-off-by: Zheng Liang zhengliang6@huawei.com Reviewed-by: Zhang Yi yi.zhang@huawei.com Signed-off-by: Yang Yingliang yangyingliang@huawei.com --- fs/overlayfs/super.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-)
diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c index 1150bb76ef4d6..e3f75aa915899 100644 --- a/fs/overlayfs/super.c +++ b/fs/overlayfs/super.c @@ -1605,6 +1605,34 @@ static int ovl_check_overlapping_layers(struct super_block *sb, return 0; }
+static struct dentry *ovl_get_root(struct super_block *sb, + struct dentry *upperdentry, + struct ovl_entry *oe) +{ + struct dentry *root; + + root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); + if (!root) + return NULL; + + root->d_fsdata = oe; + + if (upperdentry) { + ovl_dentry_set_upper_alias(root); + if (ovl_is_impuredir(upperdentry)) + ovl_set_flag(OVL_IMPURE, d_inode(root)); + } + + /* Root is always merge -> can have whiteouts */ + ovl_set_flag(OVL_WHITEOUTS, d_inode(root)); + ovl_dentry_set_flag(OVL_E_CONNECTED, root); + ovl_set_upperdata(d_inode(root)); + ovl_inode_init(d_inode(root), upperdentry, ovl_dentry_lower(root), + NULL); + + return root; +} + static int ovl_fill_super(struct super_block *sb, void *data, int silent) { struct path upperpath = { }; @@ -1726,25 +1754,11 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) sb->s_flags |= SB_POSIXACL;
err = -ENOMEM; - root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); + root_dentry = ovl_get_root(sb, upperpath.dentry, oe); if (!root_dentry) goto out_free_oe;
- root_dentry->d_fsdata = oe; - mntput(upperpath.mnt); - if (upperpath.dentry) { - ovl_dentry_set_upper_alias(root_dentry); - if (ovl_is_impuredir(upperpath.dentry)) - ovl_set_flag(OVL_IMPURE, d_inode(root_dentry)); - } - - /* Root is always merge -> can have whiteouts */ - ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry)); - ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry); - ovl_set_upperdata(d_inode(root_dentry)); - ovl_inode_init(d_inode(root_dentry), upperpath.dentry, - ovl_dentry_lower(root_dentry), NULL);
sb->s_root = root_dentry;
From: Amir Goldstein amir73il@gmail.com
mainline inclusion from mainline-v5.7-rc1 commit 62c832ed4e1b67cc65d909005aae5ce7031a2b38 category: bugfix bugzilla: NA CVE: NA
-------------------------------------------------
Move i_ino initialization to ovl_inode_init() to avoid the dance of setting i_ino in ovl_fill_inode() sometimes on the first call and sometimes on the seconds call.
Signed-off-by: Amir Goldstein amir73il@gmail.com Signed-off-by: Miklos Szeredi mszeredi@redhat.com Signed-off-by: Zheng Liang zhengliang6@huawei.com Reviewed-by: Zhang Yi yi.zhang@huawei.com Signed-off-by: Yang Yingliang yangyingliang@huawei.com --- fs/overlayfs/inode.c | 35 ++++++++++++++++++++++++++--------- fs/overlayfs/overlayfs.h | 4 ++-- fs/overlayfs/super.c | 13 +++++++++++-- fs/overlayfs/util.c | 18 ------------------ 4 files changed, 39 insertions(+), 31 deletions(-)
diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c index b016343a7209b..4ff4198a0ad44 100644 --- a/fs/overlayfs/inode.c +++ b/fs/overlayfs/inode.c @@ -545,8 +545,7 @@ static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) #endif }
-static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, - unsigned long ino, int fsid) +static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) { int xinobits = ovl_xino_bits(inode->i_sb);
@@ -556,10 +555,6 @@ static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, * so inode number exposed via /proc/locks and a like will be * consistent with d_ino and st_ino values. An i_ino value inconsistent * with d_ino also causes nfsd readdirplus to fail. - * - * When called from ovl_create_object() => ovl_new_inode(), with - * ino = 0, i_ino will be updated to consistent value later on in - * ovl_get_inode() => ovl_fill_inode(). */ if (ovl_same_dev(inode->i_sb)) { inode->i_ino = ino; @@ -568,6 +563,28 @@ static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, } else { inode->i_ino = get_next_ino(); } +} + +void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, + unsigned long ino, int fsid) +{ + struct inode *realinode; + + if (oip->upperdentry) + OVL_I(inode)->__upperdentry = oip->upperdentry; + if (oip->lowerpath && oip->lowerpath->dentry) + OVL_I(inode)->lower = igrab(d_inode(oip->lowerpath->dentry)); + if (oip->lowerdata) + OVL_I(inode)->lowerdata = igrab(d_inode(oip->lowerdata)); + + realinode = ovl_inode_real(inode); + ovl_copyattr(realinode, inode); + ovl_copyflags(realinode, inode); + ovl_map_ino(inode, ino, fsid); +} + +static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) +{ inode->i_mode = mode; inode->i_flags |= S_NOCMTIME; #ifdef CONFIG_FS_POSIX_ACL @@ -705,7 +722,7 @@ struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
inode = new_inode(sb); if (inode) - ovl_fill_inode(inode, mode, rdev, 0, 0); + ovl_fill_inode(inode, mode, rdev);
return inode; } @@ -930,8 +947,8 @@ struct inode *ovl_get_inode(struct super_block *sb, ino = realinode->i_ino; fsid = lowerpath->layer->fsid; } - ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid); - ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata); + ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); + ovl_inode_init(inode, oip, ino, fsid);
if (upperdentry && ovl_is_impuredir(upperdentry)) ovl_set_flag(OVL_IMPURE, inode); diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h index a342015e0c23c..9199e16653117 100644 --- a/fs/overlayfs/overlayfs.h +++ b/fs/overlayfs/overlayfs.h @@ -248,8 +248,6 @@ void ovl_set_upperdata(struct inode *inode); bool ovl_redirect_dir(struct super_block *sb); const char *ovl_dentry_get_redirect(struct dentry *dentry); void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect); -void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, - struct dentry *lowerdentry, struct dentry *lowerdata); void ovl_inode_update(struct inode *inode, struct dentry *upperdentry); void ovl_dir_modified(struct dentry *dentry, bool impurity); u64 ovl_dentry_version_get(struct dentry *dentry); @@ -373,6 +371,8 @@ struct ovl_inode_params { char *redirect; struct dentry *lowerdata; }; +void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, + unsigned long ino, int fsid); struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev); struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, bool is_upper); diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c index e3f75aa915899..1f9a717fdad55 100644 --- a/fs/overlayfs/super.c +++ b/fs/overlayfs/super.c @@ -1610,6 +1610,13 @@ static struct dentry *ovl_get_root(struct super_block *sb, struct ovl_entry *oe) { struct dentry *root; + struct ovl_path *lowerpath = &oe->lowerstack[0]; + unsigned long ino = d_inode(lowerpath->dentry)->i_ino; + int fsid = lowerpath->layer->fsid; + struct ovl_inode_params oip = { + .upperdentry = upperdentry, + .lowerpath = lowerpath, + };
root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); if (!root) @@ -1618,6 +1625,9 @@ static struct dentry *ovl_get_root(struct super_block *sb, root->d_fsdata = oe;
if (upperdentry) { + /* Root inode uses upper st_ino/i_ino */ + ino = d_inode(upperdentry)->i_ino; + fsid = 0; ovl_dentry_set_upper_alias(root); if (ovl_is_impuredir(upperdentry)) ovl_set_flag(OVL_IMPURE, d_inode(root)); @@ -1627,8 +1637,7 @@ static struct dentry *ovl_get_root(struct super_block *sb, ovl_set_flag(OVL_WHITEOUTS, d_inode(root)); ovl_dentry_set_flag(OVL_E_CONNECTED, root); ovl_set_upperdata(d_inode(root)); - ovl_inode_init(d_inode(root), upperdentry, ovl_dentry_lower(root), - NULL); + ovl_inode_init(d_inode(root), &oip, ino, fsid);
return root; } diff --git a/fs/overlayfs/util.c b/fs/overlayfs/util.c index b83955f31ded0..06ca98f88983a 100644 --- a/fs/overlayfs/util.c +++ b/fs/overlayfs/util.c @@ -389,24 +389,6 @@ void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) oi->redirect = redirect; }
-void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, - struct dentry *lowerdentry, struct dentry *lowerdata) -{ - struct inode *realinode = d_inode(upperdentry ?: lowerdentry); - - if (upperdentry) - OVL_I(inode)->__upperdentry = upperdentry; - if (lowerdentry) - OVL_I(inode)->lower = igrab(d_inode(lowerdentry)); - if (lowerdata) - OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata)); - - ovl_copyattr(realinode, inode); - ovl_copyflags(realinode, inode); - if (!inode->i_ino) - inode->i_ino = realinode->i_ino; -} - void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) { struct inode *upperinode = d_inode(upperdentry);
From: Amir Goldstein amir73il@gmail.com
mainline inclusion from mainline-v5.7-rc1 commit 4d314f7859dc1925ee28b4d4e74f6f3a80e6f34b category: bugfix bugzilla: NA CVE: NA
-------------------------------------------------
There is no reason to deplete the system's global get_next_ino() pool for overlay non-persistent inode numbers and there is no reason at all to allocate non-persistent inode numbers for non-directories.
For non-directories, it is much better to leave i_ino the same as real i_ino, to be consistent with st_ino/d_ino.
Signed-off-by: Amir Goldstein amir73il@gmail.com Signed-off-by: Miklos Szeredi mszeredi@redhat.com Signed-off-by: Zheng Liang zhengliang6@huawei.com Reviewed-by: Zhang Yi yi.zhang@huawei.com Signed-off-by: Yang Yingliang yangyingliang@huawei.com --- fs/overlayfs/inode.c | 15 ++++++++++++--- fs/overlayfs/ovl_entry.h | 2 ++ fs/overlayfs/super.c | 1 + 3 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c index 4ff4198a0ad44..b721387f7f1e1 100644 --- a/fs/overlayfs/inode.c +++ b/fs/overlayfs/inode.c @@ -545,6 +545,15 @@ static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) #endif }
+static void ovl_next_ino(struct inode *inode) +{ + struct ovl_fs *ofs = inode->i_sb->s_fs_info; + + inode->i_ino = atomic_long_inc_return(&ofs->last_ino); + if (unlikely(!inode->i_ino)) + inode->i_ino = atomic_long_inc_return(&ofs->last_ino); +} + static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) { int xinobits = ovl_xino_bits(inode->i_sb); @@ -556,12 +565,12 @@ static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid) * consistent with d_ino and st_ino values. An i_ino value inconsistent * with d_ino also causes nfsd readdirplus to fail. */ + inode->i_ino = ino; if (ovl_same_dev(inode->i_sb)) { - inode->i_ino = ino; if (xinobits && fsid && !(ino >> (64 - xinobits))) inode->i_ino |= (unsigned long)fsid << (64 - xinobits); - } else { - inode->i_ino = get_next_ino(); + } else if (S_ISDIR(inode->i_mode)) { + ovl_next_ino(inode); } }
diff --git a/fs/overlayfs/ovl_entry.h b/fs/overlayfs/ovl_entry.h index ae09310f5a6fe..30523cc7b914b 100644 --- a/fs/overlayfs/ovl_entry.h +++ b/fs/overlayfs/ovl_entry.h @@ -78,6 +78,8 @@ struct ovl_fs { struct inode *indexdir_trap; /* -1: disabled, 0: same fs, 1..32: number of unused ino bits */ int xino_mode; + /* For allocation of non-persistent inode numbers */ + atomic_long_t last_ino; };
static inline struct ovl_fs *OVL_FS(struct super_block *sb) diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c index 1f9a717fdad55..20c20f6210a5f 100644 --- a/fs/overlayfs/super.c +++ b/fs/overlayfs/super.c @@ -1677,6 +1677,7 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
sb->s_stack_depth = 0; sb->s_maxbytes = MAX_LFS_FILESIZE; + atomic_long_set(&ofs->last_ino, 1); /* Assume underlaying fs uses 32bit inodes unless proven otherwise */ if (ofs->config.xino != OVL_XINO_OFF) { ofs->xino_mode = BITS_PER_LONG - 32;