ovl: fix inconsistent d_ino for legacy merge dir
Amir Goldstein
maintainerPATCH 1/1 ovl: fix inconsistent d_ino for legacy merge dir
For a merge dir that was copied up before v4.12 or that was hand crafted offline (e.g. mkdir {upper/lower}/dir), upper dir does not contain the 'trusted.overlay.origin' xattr. In that case, stat(2) on the merge dir returns the lower dir st_ino, but getdents(2) returns the upper dir d_ino.
After this change, on merge dir lookup, missing origin xattr on upper dir will be fixed and 'impure' xattr will be fixed on parent of the legacy merge dir.
---
Zhangyi,
Please review/test this patch.
I ended up applying it at the bottom of verify_dir [1] series, so it could be merged to v4.15 and/or easily backported to v4.14 (where consistent d_ino was merged) if Miklos thinks this is an important use case to fix.
Thanks,
Amir.
[1] https://github.com/amir73il/linux/commits/ovl-verify-dir
fs/overlayfs/namei.c | 36 +++++++++++++++++++++++++++++++----- fs/overlayfs/overlayfs.h | 2 +- fs/overlayfs/super.c | 5 +++-- 3 files changed, 35 insertions(+), 8 deletions(-)
fs/overlayfs/namei.c +31 -5
@@ -343,13 +343,13 @@ static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)/* * Verify that an inode matches the origin file handle stored in upper inode. * -* If @set is true and there is no stored file handle, encode and store origin -* file handle in OVL_XATTR_ORIGIN. +* If @set is non-null and there is no stored file handle, set *@set to true +* and try to encode and store origin file handle in OVL_XATTR_ORIGIN. * -* Return 0 on match, -ESTALE on mismatch, < 0 on error. +* Return 0 on match or successful set, -ESTALE on mismatch, < 0 on error. */ int ovl_verify_origin (struct dentry *dentry,struct dentry *origin, -bool is_upper,bool set) +bool is_upper,bool *set) {struct inode *inode;struct ovl_fh *fh;
@@ -361,8 +361,13 @@ int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, goto fail; err =ovl_verify_origin_fh (dentry, fh); -if (set && err == -ENODATA ) +if (set && err == -ENODATA ) { + *set = true; err =ovl_do_setxattr (dentry,OVL_XATTR_ORIGIN , fh, fh->len,0 ); +if (err) + goto fail; + } +if (err) goto fail;
@@ -674,6 +679,27 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,if (!this)continue ; +/* +* If no origin fh is stored in upper of a merge dir, store fh +* of upper most lower dir and set upper parent "impure". +*/ +if (upperdentry && !ctr) { +bool set = false; + + err =ovl_verify_origin (upperdentry, this, false, &set); +if (set && !err) { + err =ovl_set_impure (dentry->d_parent, + upperdentry->d_parent); + } +if (set && err) + goto out_put; + +/* +* Ignore merge dir origin mismatch, which may have +* been caused by copying layers +*/ + } + stack[ctr].dentry = this; stack[ctr].layer = lower.layer; ctr++;
fs/overlayfs/overlayfs.h +1 -1
@@ -250,7 +250,7 @@ static inline bool ovl_is_impuredir(struct dentry *dentry)/* namei.c */ int ovl_verify_origin (struct dentry *dentry,struct dentry *origin, -bool is_upper,bool set); +bool is_upper,bool *set);int ovl_verify_index (struct dentry *index,struct ovl_path *lower,unsigned int numlower);int ovl_get_index_name (struct dentry *origin,struct qstr *name);
fs/overlayfs/super.c +3 -2
@@ -1026,11 +1026,12 @@ static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)static int ovl_get_indexdir (struct ovl_fs *ofs,struct ovl_entry *oe,struct path *upperpath) { +bool set;int err;/* Verify lower root is upper root origin */ err =ovl_verify_origin (upperpath->dentry, oe->lowerstack[0 ].dentry, - false, true); + false, &set);if (err) {pr_err ("overlayfs: failed to verify upper root origin\n" ); goto out;
@@ -1040,7 +1041,7 @@ static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,if (ofs->indexdir) {/* Verify upper root is index dir origin */ err =ovl_verify_origin (ofs->indexdir, upperpath->dentry, - true, true); + true, &set);if (err)pr_err ("overlayfs: failed to verify index dir origin\n" );
Signature · 1 line
2.7.4
On 2017/12/31 17:49, Amir Goldstein Wrote:
Quoted 13 lines
For a merge dir that was copied up before v4.12 or that was hand crafted offline (e.g. mkdir {upper/lower}/dir), upper dir does not contain the 'trusted.overlay.origin' xattr. In that case, stat(2) on the merge dir returns the lower dir
st_ino, but getdents(2) returns the upper dird_ino.After this change, on merge dir lookup, missing origin xattr on upper dir will be fixed and 'impure' xattr will be fixed on parent of the legacy merge dir.
Suggested-by zhangyi (F) <[email protected]>Signed-off-by Amir Goldstein <[email protected]>---
Quoted 8 lines
Zhangyi,
Please review/test this patch.
I ended up applying it at the bottom ofverify_dir[1] series, so it could be merged to v4.15 and/or easily backported to v4.14 (where consistentd_inowas merged) if Miklos thinks this is an important use case to fix.
Hi Amir,
This patch looks good (one nit below) but still have a case which will lead to problem we missed in last discussion:
Call getdent(2) before invoke ovl_lookup() after first mount.
eg:
1) mkdir {upper/lower}/dir merge work
2) mount to merge
3) getdent merge
2253114 directory 24 2 dir
4) ls merge
5) getdent merge
2253113 directory 24 2 dir
Strictly speaking, it is a problem ,but not sure it's worth to fix now.
Thanks,
zhangyi.
Quoted 33 lines
Thanks,
Amir.[1] https://github.com/amir73il/linux/commits/ovl-verify-dir
fs/overlayfs/namei.c | 36 +++++++++++++++++++++++++++++++----- fs/overlayfs/overlayfs.h | 2 +- fs/overlayfs/super.c | 5 +++-- 3 files changed, 35 insertions(+), 8 deletions(-)fs/overlayfs/namei.c +4
-4@@ -343,13 +343,13 @@ static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)/* * Verify that an inode matches the origin file handle stored in upper inode. * -* If @set is true and there is no stored file handle, encode and store origin -* file handle in OVL_XATTR_ORIGIN. +* If @set is non-null and there is no stored file handle, set *@set to true +* and try to encode and store origin file handle in OVL_XATTR_ORIGIN. * -* Return 0 on match, -ESTALE on mismatch, < 0 on error. +* Return 0 on match or successful set, -ESTALE on mismatch, < 0 on error. */ int ovl_verify_origin (struct dentry *dentry,struct dentry *origin, -bool is_upper,bool set) +bool is_upper,bool *set) {struct inode *inode;struct ovl_fh *fh;
fs/overlayfs/namei.c int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, · line 368
Quoted 9 lines
fs/overlayfs/namei.c +4
-1@@ -361,8 +361,13 @@ int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, goto fail; err =ovl_verify_origin_fh (dentry, fh); -if (set && err == -ENODATA ) +if (set && err == -ENODATA ) { + *set = true; err =ovl_do_setxattr (dentry,OVL_XATTR_ORIGIN , fh, fh->len,0 ); +if (err) + goto fail;
Remove this repeated error handle ?
Quoted 74 lines
fs/overlayfs/namei.c +23
-0+ } +if (err) goto fail;@@ -674,6 +679,27 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,if (!this)continue ; +/* +* If no origin fh is stored in upper of a merge dir, store fh +* of upper most lower dir and set upper parent "impure". +*/ +if (upperdentry && !ctr) { +bool set = false; + + err =ovl_verify_origin (upperdentry, this, false, &set); +if (set && !err) { + err =ovl_set_impure (dentry->d_parent, + upperdentry->d_parent); + } +if (set && err) + goto out_put; + +/* +* Ignore merge dir origin mismatch, which may have +* been caused by copying layers +*/ + } + stack[ctr].dentry = this; stack[ctr].layer = lower.layer; ctr++;fs/overlayfs/overlayfs.h +1
-1@@ -250,7 +250,7 @@ static inline bool ovl_is_impuredir(struct dentry *dentry)/* namei.c */ int ovl_verify_origin (struct dentry *dentry,struct dentry *origin, -bool is_upper,bool set); +bool is_upper,bool *set);int ovl_verify_index (struct dentry *index,struct ovl_path *lower,unsigned int numlower);int ovl_get_index_name (struct dentry *origin,struct qstr *name);fs/overlayfs/super.c +3
-2@@ -1026,11 +1026,12 @@ static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)static int ovl_get_indexdir (struct ovl_fs *ofs,struct ovl_entry *oe,struct path *upperpath) { +bool set;int err;/* Verify lower root is upper root origin */ err =ovl_verify_origin (upperpath->dentry, oe->lowerstack[0 ].dentry, - false, true); + false, &set);if (err) {pr_err ("overlayfs: failed to verify upper root origin\n" ); goto out;@@ -1040,7 +1041,7 @@ static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,if (ofs->indexdir) {/* Verify upper root is index dir origin */ err =ovl_verify_origin (ofs->indexdir, upperpath->dentry, - true, true); + true, &set);if (err)pr_err ("overlayfs: failed to verify index dir origin\n" );
Amir Goldstein
maintainerQuoted 37 lines · zhangyi (F)
On 2017/12/31 17:49, Amir Goldstein Wrote:
Quoted 13 lines
For a merge dir that was copied up before v4.12 or that was hand crafted offline (e.g. mkdir {upper/lower}/dir), upper dir does not contain the 'trusted.overlay.origin' xattr. In that case, stat(2) on the merge dir returns the lower dir
st_ino, but getdents(2) returns the upper dird_ino.After this change, on merge dir lookup, missing origin xattr on upper dir will be fixed and 'impure' xattr will be fixed on parent of the legacy merge dir.
Suggested-by zhangyi (F) <[email protected]>Signed-off-by Amir Goldstein <[email protected]>---
Quoted 8 lines
Zhangyi,
Please review/test this patch.
I ended up applying it at the bottom ofverify_dir[1] series, so it could be merged to v4.15 and/or easily backported to v4.14 (where consistentd_inowas merged) if Miklos thinks this is an important use case to fix.Hi Amir,
This patch looks good (one nit below) but still have a case which will lead to problem we missed in last discussion:
Call getdent(2) before invokeovl_lookup()after first mount.eg:
1) mkdir {upper/lower}/dir merge work
2) mount to merge
3) getdent merge
2253114 directory 24 2 dir
4) ls merge
5) getdent merge
2253113 directory 24 2 dir
Quoted 1 line
Strictly speaking, it is a problem ,but not sure it's worth to fix now.
Yeh, it's true.
Please note, that the sole purpose of "impure" xattr in the world, is to optimize away the need to lookup directory entries in getdents for fixing d_ino.
So yes, I think this specific corner case is something that users will have to be educated about.
Also please note that I have not found any program that relies of d_ino of directories (not that I would know where to look). I did check that 'find -inum <ino>'
does NOT reply of values of d_ino for subdirs. 'find' always call stat(2) for subdirs.
So that is one less program to worry about.
Quoted 39 lines
Thanks,
zhangyi.Quoted 35 lines
Thanks,
Amir.[1] https://github.com/amir73il/linux/commits/ovl-verify-dir
fs/overlayfs/namei.c | 36 +++++++++++++++++++++++++++++++----- fs/overlayfs/overlayfs.h | 2 +- fs/overlayfs/super.c | 5 +++-- 3 files changed, 35 insertions(+), 8 deletions(-)fs/overlayfs/namei.c +4
-4@@ -343,13 +343,13 @@ static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)/* * Verify that an inode matches the origin file handle stored in upper inode. * -* If @set is true and there is no stored file handle, encode and store origin -* file handle in OVL_XATTR_ORIGIN. +* If @set is non-null and there is no stored file handle, set *@set to true +* and try to encode and store origin file handle in OVL_XATTR_ORIGIN. * -* Return 0 on match, -ESTALE on mismatch, < 0 on error. +* Return 0 on match or successful set, -ESTALE on mismatch, < 0 on error. */ int ovl_verify_origin (struct dentry *dentry,struct dentry *origin, -bool is_upper,bool set) +bool is_upper,bool *set) {struct inode *inode;struct ovl_fh *fh;@@ -361,8 +361,13 @@ int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, goto fail;
Quoted 10 lines
fs/overlayfs/namei.c int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, · line 368
Quoted 7 lines
fs/overlayfs/namei.c +4
-1err =ovl_verify_origin_fh (dentry, fh); -if (set && err == -ENODATA ) +if (set && err == -ENODATA ) { + *set = true; err =ovl_do_setxattr (dentry,OVL_XATTR_ORIGIN , fh, fh->len,0 ); +if (err) + goto fail;Remove this repeated error handle ?
Right. Thanks,
Amir.
zhangyi (F)
Reviewed-byOn 2018/1/2 16:14, Amir Goldstein Wrote:
Quoted 48 lines · zhangyi (F)
Quoted 37 lines
On 2017/12/31 17:49, Amir Goldstein Wrote:
Quoted 13 lines
For a merge dir that was copied up before v4.12 or that was hand crafted offline (e.g. mkdir {upper/lower}/dir), upper dir does not contain the 'trusted.overlay.origin' xattr. In that case, stat(2) on the merge dir returns the lower dir
st_ino, but getdents(2) returns the upper dird_ino.After this change, on merge dir lookup, missing origin xattr on upper dir will be fixed and 'impure' xattr will be fixed on parent of the legacy merge dir.
Suggested-by zhangyi (F) <[email protected]>Signed-off-by Amir Goldstein <[email protected]>---
Quoted 8 lines
Zhangyi,
Please review/test this patch.
I ended up applying it at the bottom ofverify_dir[1] series, so it could be merged to v4.15 and/or easily backported to v4.14 (where consistentd_inowas merged) if Miklos thinks this is an important use case to fix.Hi Amir,
This patch looks good (one nit below) but still have a case which will lead to problem we missed in last discussion:
Call getdent(2) before invokeovl_lookup()after first mount.eg:
1) mkdir {upper/lower}/dir merge work
2) mount to merge
3) getdent merge
2253114 directory 24 2 dir
4) ls merge
5) getdent merge
2253113 directory 24 2 dirQuoted 1 line
Strictly speaking, it is a problem ,but not sure it's worth to fix now.
Yeh, it's true.
Please note, that the sole purpose of "impure" xattr in the world, is to optimize away the need to lookup directory entries in getdents for fixing
d_ino.
So yes, I think this specific corner case is something that users will have to be educated about.
Quoted 7 lines
Also please note that I have not found any program that relies of
d_inoof directories (not that I would know where to look). I did check that 'find -inum <ino>'
does NOT reply of values ofd_inofor subdirs. 'find' always call stat(2) for subdirs.
So that is one less program to worry about.
I see, you can add:
Thanks,
Yi.
Amir Goldstein
maintainerQuoted 74 lines · Amir Goldstein
For a merge dir that was copied up before v4.12 or that was hand crafted offline (e.g. mkdir {upper/lower}/dir), upper dir does not contain the 'trusted.overlay.origin' xattr. In that case, stat(2) on the merge dir returns the lower dir
st_ino, but getdents(2) returns the upper dird_ino.After this change, on merge dir lookup, missing origin xattr on upper dir will be fixed and 'impure' xattr will be fixed on parent of the legacy merge dir.
Suggested-by zhangyi (F) <[email protected]>Signed-off-by Amir Goldstein <[email protected]>---
Zhangyi,
Please review/test this patch.
I ended up applying it at the bottom ofverify_dir[1] series, so it could be merged to v4.15 and/or easily backported to v4.14 (where consistentd_inowas merged) if Miklos thinks this is an important use case to fix.Thanks,
Amir.[1] https://github.com/amir73il/linux/commits/ovl-verify-dir
fs/overlayfs/namei.c | 36 +++++++++++++++++++++++++++++++----- fs/overlayfs/overlayfs.h | 2 +- fs/overlayfs/super.c | 5 +++-- 3 files changed, 35 insertions(+), 8 deletions(-)fs/overlayfs/namei.c +12
-5@@ -343,13 +343,13 @@ static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)/* * Verify that an inode matches the origin file handle stored in upper inode. * -* If @set is true and there is no stored file handle, encode and store origin -* file handle in OVL_XATTR_ORIGIN. +* If @set is non-null and there is no stored file handle, set *@set to true +* and try to encode and store origin file handle in OVL_XATTR_ORIGIN. * -* Return 0 on match, -ESTALE on mismatch, < 0 on error. +* Return 0 on match or successful set, -ESTALE on mismatch, < 0 on error. */ int ovl_verify_origin (struct dentry *dentry,struct dentry *origin, -bool is_upper,bool set) +bool is_upper,bool *set) {struct inode *inode;struct ovl_fh *fh;@@ -361,8 +361,13 @@ int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, goto fail; err =ovl_verify_origin_fh (dentry, fh); -if (set && err == -ENODATA ) +if (set && err == -ENODATA ) { + *set = true; err =ovl_do_setxattr (dentry,OVL_XATTR_ORIGIN , fh, fh->len,0 ); +if (err) + goto fail; + } +if (err) goto fail;@@ -674,6 +679,27 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,if (!this)continue ; +/* +* If no origin fh is stored in upper of a merge dir, store fh
fs/overlayfs/namei.c struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, · line 693
Quoted 10 lines
fs/overlayfs/namei.c +10
-0+* of upper most lower dir and set upper parent "impure". +*/ +if (upperdentry && !ctr) { +bool set = false; + + err =ovl_verify_origin (upperdentry, this, false, &set); +if (set && !err) { + err =ovl_set_impure (dentry->d_parent, + upperdentry->d_parent); + }
Oh oh! this ovl_set_impure() is missing mnt_want_write(), but so do:
- set origin xattr in ovl_verify_origin()
- set opaque in ovl_make_workdir()
Will have to fix those too.
Amir.