Support nested overlayfs mounts with xattrs and whiteous
Subsystems (2)
v4 cover Support nested overlayfs mounts with xattrs and whiteous
There are cases where you want to use an overlayfs mount as a lowerdir for another overlayfs mount. For example, if the system rootfs is on overlayfs due to composefs, or to make it volatile (via tmpfs), then you cannot currently store a lowerdir on the rootfs, because the inner overlayfs will eat all the whiteouts and overlay xattrs. This means you can't e.g. store on the rootfs a prepared container image for use with overlayfs.
This patch series adds support for nesting of overlayfs mounts by escaping the problematic features and unescaping them when exposing to the overlayfs user.
This series is also available here:
https://github.com/alexlarsson/linux/tree/ovl-nesting
And xfstest to test it is available here:
https://github.com/alexlarsson/xfstests/tree/overlayfs-nesting
The overlay/083 test checks both xattr escaping, the new whiteouts as well as actual nesting of overlayfs.
Note that this series breaks the overlay/026 test which validates that writing overlay.* xattrs is not supported, but it now is. I'm not sure if we should fix this test to not fail, or if we should make this an opt-in mount feature.
Changes since v3:
* Dropped the handling of whiteout xattrs across layers.
* Copy-up escaped overlayfs xattrs.
* Minor code cleanups.
Changes since v2:
* Uses a new approach for escaping whiteouts with a regular file with an overlay.whiteout xattr in a lower directory with an overlay.whiteouts xattr.
Changes since v1:
* Moved all xattr handling to xattr.c
* Made creation of escaped whiteouts atomic
Alexander Larsson (5):
ovl: Move xattr support to new xattrs.c file
ovl: Add OVL_XATTR_TRUSTED/USER_PREFIX_LEN macros
ovl: Support escaped overlay.* xattrs
ovl: Add an alternative type of whiteout
ovl: Add documentation on nesting of overlayfs mounts
Documentation/filesystems/overlayfs.rst | 23 ++ fs/overlayfs/Makefile | 2 +- fs/overlayfs/dir.c | 4 +- fs/overlayfs/inode.c | 124 ----------- fs/overlayfs/namei.c | 15 +- fs/overlayfs/overlayfs.h | 42 +++- fs/overlayfs/readdir.c | 27 ++- fs/overlayfs/super.c | 67 +----- fs/overlayfs/util.c | 40 ++++ fs/overlayfs/xattrs.c | 273 ++++++++++++++++++++++++ 10 files changed, 404 insertions(+), 213 deletions(-) create mode 100644 fs/overlayfs/xattrs.c
Signature · 1 line
2.41.0
PATCH v4 1/5 ovl: Move xattr support to new xattrs.c file
This moves the code from super.c and inode.c, and makes ovl_xattr_get/set() static.
This is in preparation for doing more work on xattrs support.
--- fs/overlayfs/Makefile | 2 +- fs/overlayfs/inode.c | 124 ------------------------ fs/overlayfs/overlayfs.h | 18 ++-- fs/overlayfs/super.c | 65 +------------ fs/overlayfs/xattrs.c | 198 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 209 insertions(+), 198 deletions(-) create mode 100644 fs/overlayfs/xattrs.c
fs/overlayfs/Makefile +1 -1
@@ -6,4 +6,4 @@ obj-$(CONFIG_OVERLAY_FS) += overlay.o overlay-objs := super.o namei.o util.o inode.o file.o dir.o readdir.o \ - copy_up.o export.o params.o + copy_up.o export.o params.o xattrs.o
fs/overlayfs/inode.c +0 -124
130 lines not on this page · raw message
fs/overlayfs/overlayfs.h +9 -9
@@ -684,17 +684,8 @@ int ovl_set_nlink_lower(struct dentry *dentry);unsigned int ovl_get_nlink (struct ovl_fs *ofs,struct dentry *lowerdentry,struct dentry *upperdentry,unsigned int fallback); -int ovl_setattr (struct mnt_idmap *idmap,struct dentry *dentry, -struct iattr *attr); -int ovl_getattr (struct mnt_idmap *idmap,const struct path *path, -struct kstat *stat,u32 request_mask,unsigned int flags);int ovl_permission (struct mnt_idmap *idmap,struct inode *inode,int mask); -int ovl_xattr_set (struct dentry *dentry,struct inode *inode,const char *name, -const void *value,size_t size,int flags); -int ovl_xattr_get (struct dentry *dentry,struct inode *inode,const char *name, -void *value,size_t size); -ssize_t ovl_listxattr (struct dentry *dentry,char *list,size_t size);#ifdef CONFIG_FS_POSIX_ACL struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap,
@@ -830,3 +821,12 @@ static inline bool ovl_force_readonly(struct ovl_fs *ofs) {return (!ovl_upper_mnt (ofs) || !ofs->workdir); } + +/* xattr.c */ + +const struct xattr_handler **ovl_xattr_handlers (struct ovl_fs *ofs); +int ovl_setattr (struct mnt_idmap *idmap,struct dentry *dentry, +struct iattr *attr); +int ovl_getattr (struct mnt_idmap *idmap,const struct path *path, +struct kstat *stat,u32 request_mask,unsigned int flags); +ssize_t ovl_listxattr (struct dentry *dentry,char *list,size_t size);
fs/overlayfs/super.c +1 -64
77 lines not on this page · raw message
fs/overlayfs/xattrs.c new file +198 -0
198 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH v4 2/5 ovl: Add OVL_XATTR_TRUSTED/USER_PREFIX_LEN macros
In reply to Alexander LarssonThese match the ones for e.g. XATTR_TRUSTED_PREFIX_LEN.
--- fs/overlayfs/overlayfs.h | 2 ++ fs/overlayfs/xattrs.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-)
fs/overlayfs/overlayfs.h +2 -0
@@ -28,7 +28,9 @@ enum ovl_path_type {#define OVL_XATTR_NAMESPACE "overlay."#define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE +#define OVL_XATTR_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1)#define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE +#define OVL_XATTR_USER_PREFIX_LEN (sizeof(OVL_XATTR_USER_PREFIX) - 1)enum ovl_xattr {OVL_XATTR_OPAQUE ,
fs/overlayfs/xattrs.c +2 -2
@@ -10,10 +10,10 @@ bool ovl_is_private_xattr(struct super_block *sb, const char *name)if (ofs->config.userxattr)return strncmp (name,OVL_XATTR_USER_PREFIX , -sizeof (OVL_XATTR_USER_PREFIX ) -1 ) ==0 ; +OVL_XATTR_USER_PREFIX_LEN ) ==0 ;else return strncmp (name,OVL_XATTR_TRUSTED_PREFIX , -sizeof (OVL_XATTR_TRUSTED_PREFIX ) -1 ) ==0 ; +OVL_XATTR_TRUSTED_PREFIX_LEN ) ==0 ; } static int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
Signature · 1 line
2.41.0
PATCH v4 3/5 ovl: Support escaped overlay.* xattrs
In reply to Alexander LarssonThere are cases where you want to use an overlayfs mount as a lowerdir for another overlayfs mount. For example, if the system rootfs is on overlayfs due to composefs, or to make it volatile (via tmps), then you cannot currently store a lowerdir on the rootfs. This means you can't e.g. store on the rootfs a prepared container image for use using overlayfs.
To work around this, we introduce an escapment mechanism for overlayfs xattrs. Whenever the lower/upper dir has a xattr named "overlay.overlay.XYZ", we list it as "overlay.XYZ" in listxattrs, and when the user calls getxattr or setxattr on "overlay.XYZ", we apply to "overlay.overlay.XYZ" in the backing directories.
This allows storing any kind of overlay xattrs in a overlayfs mount that can be used as a lowerdir in another mount. It is possible to stack this mechanism multiple times, such that "overlay.overlay.overlay.XYZ" will survive two levels of overlay mounts, however this is not all that useful in practice because of stack depth limitations of overlayfs mounts.
Note: These escaped xattrs are copied to upper during copy-up.
--- fs/overlayfs/overlayfs.h | 7 ++++ fs/overlayfs/xattrs.c | 81 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 3 deletions(-)
fs/overlayfs/overlayfs.h +7 -0
@@ -32,6 +32,13 @@ enum ovl_path_type {#define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE#define OVL_XATTR_USER_PREFIX_LEN (sizeof(OVL_XATTR_USER_PREFIX) - 1) +#define OVL_XATTR_ESCAPE_PREFIX OVL_XATTR_NAMESPACE +#define OVL_XATTR_ESCAPE_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_PREFIX) - 1) +#define OVL_XATTR_ESCAPE_TRUSTED_PREFIX OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_ESCAPE_PREFIX +#define OVL_XATTR_ESCAPE_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_TRUSTED_PREFIX) - 1) +#define OVL_XATTR_ESCAPE_USER_PREFIX OVL_XATTR_USER_PREFIX OVL_XATTR_ESCAPE_PREFIX +#define OVL_XATTR_ESCAPE_USER_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_USER_PREFIX) - 1) +enum ovl_xattr {OVL_XATTR_OPAQUE ,OVL_XATTR_REDIRECT ,
fs/overlayfs/xattrs.c +78 -3
131 lines not on this page · raw message
Signature · 1 line
2.41.0
Amir Goldstein
maintainerRe: [PATCH v4 3/5] ovl: Support escaped overlay.* xattrs
Quoted 21 lines · Alexander Larsson
There are cases where you want to use an overlayfs mount as a lowerdir for another overlayfs mount. For example, if the system rootfs is on overlayfs due to composefs, or to make it volatile (via tmps), then you cannot currently store a lowerdir on the rootfs. This means you can't e.g. store on the rootfs a prepared container image for use using overlayfs.
To work around this, we introduce an escapment mechanism for overlayfs xattrs. Whenever the lower/upper dir has a xattr named "overlay.overlay.XYZ", we list it as "overlay.XYZ" in listxattrs, and when the user calls getxattr or setxattr on "overlay.XYZ", we apply to "overlay.overlay.XYZ" in the backing directories.
This allows storing any kind of overlay xattrs in a overlayfs mount that can be used as a lowerdir in another mount. It is possible to stack this mechanism multiple times, such that "overlay.overlay.overlay.XYZ" will survive two levels of overlay mounts, however this is not all that useful in practice because of stack depth limitations of overlayfs mounts.
Quoted 4 lines
Note: These escaped xattrs are copied to upper during copy-up.
Signed-off-by Alexander Larsson <[email protected]>Reviewed-by Amir Goldstein <[email protected]>
Now I really reviewed ;-)
I like this patch - it is much smaller.
I'd rename the helper s/ovl_is_prefixed_xattr/ovl_is_own_xattr to match ovl_own_xattr_{get,set}, but don't worry about it, I can rename on commit.
Thanks,
Amir.
Quoted 168 lines
--- fs/overlayfs/overlayfs.h | 7 ++++ fs/overlayfs/xattrs.c | 81 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 3 deletions(-)fs/overlayfs/overlayfs.h +7
-0@@ -32,6 +32,13 @@ enum ovl_path_type {#define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE#define OVL_XATTR_USER_PREFIX_LEN (sizeof(OVL_XATTR_USER_PREFIX) - 1) +#define OVL_XATTR_ESCAPE_PREFIX OVL_XATTR_NAMESPACE +#define OVL_XATTR_ESCAPE_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_PREFIX) - 1) +#define OVL_XATTR_ESCAPE_TRUSTED_PREFIX OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_ESCAPE_PREFIX +#define OVL_XATTR_ESCAPE_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_TRUSTED_PREFIX) - 1) +#define OVL_XATTR_ESCAPE_USER_PREFIX OVL_XATTR_USER_PREFIX OVL_XATTR_ESCAPE_PREFIX +#define OVL_XATTR_ESCAPE_USER_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_USER_PREFIX) - 1) +enum ovl_xattr {OVL_XATTR_OPAQUE ,OVL_XATTR_REDIRECT ,fs/overlayfs/xattrs.c +78
-4@@ -4,7 +4,19 @@#include <linux/xattr.h> #include "overlayfs.h" -bool ovl_is_private_xattr (struct super_block *sb,const char *name) +static bool ovl_is_escaped_xattr (struct super_block *sb,const char *name) +{ +struct ovl_fs *ofs = sb->s_fs_info; + +if (ofs->config.userxattr) +return strncmp (name,OVL_XATTR_ESCAPE_USER_PREFIX , +OVL_XATTR_ESCAPE_USER_PREFIX_LEN ) ==0 ; +else +return strncmp (name,OVL_XATTR_ESCAPE_TRUSTED_PREFIX , +OVL_XATTR_ESCAPE_TRUSTED_PREFIX_LEN -1 ) ==0 ; +} + +static bool ovl_is_prefixed_xattr (struct super_block *sb,const char *name) {struct ovl_fs *ofs =OVL_FS (sb);@@ -16,6 +28,11 @@ bool ovl_is_private_xattr(struct super_block *sb, const char *name) OVL_XATTR_TRUSTED_PREFIX_LEN) == 0; } +bool ovl_is_private_xattr (struct super_block *sb,const char *name) +{ +return ovl_is_prefixed_xattr (sb, name) && !ovl_is_escaped_xattr (sb, name); +} +static int ovl_xattr_set (struct dentry *dentry,struct inode *inode,const char *name,const void *value,size_t size,int flags) {@@ -97,10 +114,12 @@ static bool ovl_can_list(struct super_block *sb, const char *s)ssize_t ovl_listxattr (struct dentry *dentry,char *list,size_t size) {struct dentry *realdentry =ovl_dentry_real (dentry); +struct ovl_fs *ofs =OVL_FS (dentry->d_sb);ssize_t res;size_t len;char *s;const struct cred *old_cred; +size_t prefix_len, name_len; old_cred =ovl_override_creds (dentry->d_sb); res =vfs_listxattr (realdentry, list, size);@@ -108,6 +127,9 @@ ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)if (res <=0 || size ==0 )return res; + prefix_len = ofs->config.userxattr ? +OVL_XATTR_USER_PREFIX_LEN :OVL_XATTR_TRUSTED_PREFIX_LEN ; +/* filter out private xattrs */ for (s = list, len = res; len;) {size_t slen =strnlen (s, len) +1 ;@@ -120,6 +142,12 @@ ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)if (!ovl_can_list (dentry->d_sb, s)) { res -= slen;memmove (s, s + slen, len); + }else if (ovl_is_escaped_xattr (dentry->d_sb, s)) { + res -=OVL_XATTR_ESCAPE_PREFIX_LEN ; + name_len = slen - prefix_len -OVL_XATTR_ESCAPE_PREFIX_LEN ; + s += prefix_len; +memmove (s, s +OVL_XATTR_ESCAPE_PREFIX_LEN , name_len + len); + s += name_len; }else { s += slen; }@@ -128,11 +156,47 @@ ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)return res; } +static char *ovl_xattr_escape_name (const char *prefix,const char *name) +{ +size_t prefix_len =strlen (prefix); +size_t name_len =strlen (name); +size_t escaped_len; +char *escaped, *s; + + escaped_len = prefix_len +OVL_XATTR_ESCAPE_PREFIX_LEN + name_len; +if (escaped_len >XATTR_NAME_MAX ) +return ERR_PTR (-EOPNOTSUPP ); + + escaped =kmalloc (escaped_len +1 ,GFP_KERNEL ); +if (escaped ==NULL ) +return ERR_PTR (-ENOMEM ); + + s = escaped; +memcpy (s, prefix, prefix_len); + s += prefix_len; +memcpy (s,OVL_XATTR_ESCAPE_PREFIX ,OVL_XATTR_ESCAPE_PREFIX_LEN ); + s +=OVL_XATTR_ESCAPE_PREFIX_LEN ; +memcpy (s, name, name_len +1 ); + +return escaped; +} +static int ovl_own_xattr_get (const struct xattr_handler *handler,struct dentry *dentry,struct inode *inode,const char *name,void *buffer,size_t size) { -return -EOPNOTSUPP ; +char *escaped; +int r; + + escaped =ovl_xattr_escape_name (handler->prefix, name); +if (IS_ERR (escaped)) +return PTR_ERR (escaped); + + r =ovl_xattr_get (dentry, inode, escaped, buffer, size); + +kfree (escaped); + +return r; } static int ovl_own_xattr_set(const struct xattr_handler *handler,@@ -141,7 +205,18 @@ static int ovl_own_xattr_set(const struct xattr_handler *handler, const char *name, const void *value, size_t size, int flags) { - return -EOPNOTSUPP; + char *escaped; +int r; + + escaped =ovl_xattr_escape_name (handler->prefix, name); +if (IS_ERR (escaped)) +return PTR_ERR (escaped); + + r =ovl_xattr_set (dentry, inode, escaped, value, size, flags); + +kfree (escaped); + +return r; } static int ovl_other_xattr_get(const struct xattr_handler *handler, --2.41.0
PATCH v4 4/5 ovl: Add an alternative type of whiteout
In reply to Alexander LarssonAn xattr whiteout (called "xwhiteout" in the code) is a reguar file of zero size with the "overlay.whiteout" xattr set. A file like this in a directory with the "overlay.whiteouts" xattrs set will be treated the same way as a regular whiteout.
The "overlay.whiteouts" directory xattr is used in order to efficiently handle overlay checks in readdir(), as we only need to checks xattrs in affected directories.
The advantage of this kind of whiteout is that they can be escaped using the standard overlay xattr escaping mechanism. So, a file with a "overlay.overlay.whiteout" xattr would be unescaped to "overlay.whiteout", which could then be consumed by another overlayfs as a whiteout.
Overlayfs itself doesn't create whiteouts like this, but a userspace mechanism could use this alternative mechanism to convert images that may contain whiteouts to be used with overlayfs.
To work as a whiteout for both regular overlayfs mounts as well as userxattr mounts both the "user.overlay.whiteout*" and the "trusted.overlay.whiteout*" xattrs will need to be created.
--- fs/overlayfs/dir.c | 4 ++-- fs/overlayfs/namei.c | 15 ++++++++++----- fs/overlayfs/overlayfs.h | 15 +++++++++++++++ fs/overlayfs/readdir.c | 27 ++++++++++++++++++++------- fs/overlayfs/super.c | 2 +- fs/overlayfs/util.c | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 15 deletions(-)
fs/overlayfs/dir.c +2 -2
@@ -477,7 +477,7 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode, goto out_unlock; err = -ESTALE ; -if (d_is_negative (upper) || !IS_WHITEOUT (d_inode (upper))) +if (d_is_negative (upper) || !ovl_upper_is_whiteout (ofs, upper)) goto out_dput; newdentry =ovl_create_temp (ofs, workdir, cattr);
@@ -1219,7 +1219,7 @@ static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir, } }else {if (!d_is_negative (newdentry)) { -if (!new_opaque || !ovl_is_whiteout (newdentry)) +if (!new_opaque || !ovl_upper_is_whiteout (ofs, newdentry)) goto out_dput; }else {if (flags &RENAME_EXCHANGE )
fs/overlayfs/namei.c +10 -5
@@ -251,7 +251,10 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, err = -EREMOTE ; goto out_err; } -if (ovl_is_whiteout (this)) { + + path.dentry = this; + path.mnt = d->mnt; +if (ovl_path_is_whiteout (OVL_FS (d->sb), &path)) { d->stop = d->opaque = true; goto put_and_out; }
@@ -264,8 +267,6 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, goto put_and_out; } - path.dentry = this; - path.mnt = d->mnt;if (!d_can_lookup (this)) {if (d->is_dir || !last_element) { d->stop = true;
@@ -438,7 +439,7 @@ int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,else if (IS_ERR (origin))return PTR_ERR (origin); -if (upperdentry && !ovl_is_whiteout (upperdentry) && +if (upperdentry && !ovl_upper_is_whiteout (ofs, upperdentry) &&inode_wrong_type (d_inode (upperdentry),d_inode (origin)->i_mode)) goto invalid;
@@ -1383,7 +1384,11 @@ bool ovl_lower_positive(struct dentry *dentry)break ; } }else { - positive = !ovl_is_whiteout (this); +struct path path = { + .dentry = this, + .mnt = parentpath->layer->mnt, + }; + positive = !ovl_path_is_whiteout (OVL_FS (dentry->d_sb), &path); done = true;dput (this); }
fs/overlayfs/overlayfs.h +15 -0
@@ -49,6 +49,8 @@ enum ovl_xattr {OVL_XATTR_UUID ,OVL_XATTR_METACOPY ,OVL_XATTR_PROTATTR , +OVL_XATTR_XWHITEOUT , + OVL_XATTR_XWHITEOUTS, }; enum ovl_inode_flag {
@@ -469,6 +471,7 @@ void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);void ovl_dir_modified (struct dentry *dentry,bool impurity);u64 ovl_inode_version_get (struct inode *inode);bool ovl_is_whiteout (struct dentry *dentry); +bool ovl_path_is_whiteout (struct ovl_fs *ofs,const struct path *path);struct file *ovl_path_open (const struct path *path,int flags);int ovl_copy_up_start (struct dentry *dentry,int flags);void ovl_copy_up_end (struct dentry *dentry);
@@ -476,9 +479,21 @@ bool ovl_already_copied_up(struct dentry *dentry, int flags);bool ovl_path_check_dir_xattr (struct ovl_fs *ofs,const struct path *path,enum ovl_xattr ox);bool ovl_path_check_origin_xattr (struct ovl_fs *ofs,const struct path *path); +bool ovl_path_check_xwhiteout_xattr (struct ovl_fs *ofs,const struct path *path); +bool ovl_path_check_xwhiteouts_xattr (struct ovl_fs *ofs,const struct path *path);bool ovl_init_uuid_xattr (struct super_block *sb,struct ovl_fs *ofs,const struct path *upperpath); +static inline bool ovl_upper_is_whiteout (struct ovl_fs *ofs, +struct dentry *upperdentry) +{ +struct path upperpath = { + .dentry = upperdentry, + .mnt =ovl_upper_mnt (ofs), + }; +return ovl_path_is_whiteout (ofs, &upperpath); +} +static inline bool ovl_check_origin_xattr (struct ovl_fs *ofs,struct dentry *upperdentry) {
fs/overlayfs/readdir.c +20 -7
@@ -25,6 +25,7 @@ struct ovl_cache_entry {struct ovl_cache_entry *next_maybe_whiteout;bool is_upper;bool is_whiteout; +bool check_xwhiteout;char name[]; };
@@ -47,6 +48,7 @@ struct ovl_readdir_data {int err;bool is_upper;bool d_type_supported; +bool in_xwhiteouts_dir; };struct ovl_dir_file {
@@ -162,6 +164,8 @@ static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd, p->ino =0 ; p->is_upper = rdd->is_upper; p->is_whiteout = false; +/* Defer check for overlay.whiteout to ovl_iterate() */ + p->check_xwhiteout = rdd->in_xwhiteouts_dir && d_type ==DT_REG ;if (d_type ==DT_CHR ) { p->next_maybe_whiteout = rdd->first_maybe_whiteout;
@@ -301,6 +305,8 @@ static inline int ovl_dir_read(const struct path *realpath,if (IS_ERR (realfile))return PTR_ERR (realfile); + rdd->in_xwhiteouts_dir = rdd->dentry && +ovl_path_check_xwhiteouts_xattr (OVL_FS (rdd->dentry->d_sb), realpath); rdd->first_maybe_whiteout =NULL ; rdd->ctx.pos =0 ; do {
@@ -447,7 +453,7 @@ static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid, }/* -* Set d_ino for upper entries. Non-upper entries should always report +* Set d_ino for upper entries if needed. Non-upper entries should always report * the uppermost real inode ino and should not call this function. * * When not all layer are on same fs, report real ino also for upper.
@@ -455,8 +461,11 @@ static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,* When all layers are on the same fs, and upper has a reference to * copy up origin, call vfs_getattr() on the overlay entry to make * sure that d_ino will be consistent with st_ino from stat(2). +* +* Also checks the overlay.whiteout xattr by doing a full lookup which will return +* negative in this case. */ -static int ovl_cache_update_ino (const struct path *path,struct ovl_cache_entry *p) +static int ovl_cache_update (const struct path *path,struct ovl_cache_entry *p,bool update_ino) {struct dentry *dir = path->dentry;
@@ -467,7 +476,7 @@ static int ovl_cache_update_ino(const struct path *path, struct ovl_cache_entryint xinobits =ovl_xino_bits (ofs);int err =0 ; -if (!ovl_same_dev (ofs)) +if (!ovl_same_dev (ofs) && !p->check_xwhiteout) goto out;if (p->name[0 ] =='.' ) {
@@ -481,6 +490,7 @@ static int ovl_cache_update_ino(const struct path *path, struct ovl_cache_entry goto get; } } +/* This checks also for xwhiteouts */ this =lookup_one (mnt_idmap (path->mnt), p->name, dir, p->len);if (IS_ERR_OR_NULL (this) || !this->d_inode) {/* Mark a stale entry */
@@ -494,6 +504,9 @@ static int ovl_cache_update_ino(const struct path *path, struct ovl_cache_entry } get: +if (!ovl_same_dev (ofs) || !update_ino) + goto out; + type =ovl_path_type (this);if (OVL_TYPE_ORIGIN (type)) {struct kstat stat;
@@ -572,7 +585,7 @@ static int ovl_dir_read_impure(const struct path *path, struct list_head *list,list_for_each_entry_safe (p, n, list, l_node) {if (strcmp (p->name,"." ) !=0 &&strcmp (p->name,".." ) !=0 ) { - err =ovl_cache_update_ino (path, p); + err =ovl_cache_update (path, p, true);if (err)return err; }
@@ -778,13 +791,13 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)while (od->cursor != &od->cache->entries) { p = list_entry(od->cursor, struct ovl_cache_entry, l_node);if (!p->is_whiteout) { -if (!p->ino) { - err =ovl_cache_update_ino (&file->f_path, p); +if (!p->ino || p->check_xwhiteout) { + err =ovl_cache_update (&file->f_path, p, !p->ino);if (err) goto out; } } -/* ovl_cache_update_ino() sets is_whiteout on stale entry */ +/* ovl_cache_update() sets is_whiteout on stale entry */ if (!p->is_whiteout) {if (!dir_emit (ctx, p->name, p->len, p->ino, p->type))break ;
fs/overlayfs/super.c +1 -1
@@ -579,7 +579,7 @@ static int ovl_check_rename_whiteout(struct ovl_fs *ofs)if (IS_ERR (whiteout)) goto cleanup_temp; - err =ovl_is_whiteout (whiteout); + err =ovl_upper_is_whiteout (ofs, whiteout);/* Best effort cleanup of whiteout and temp file */ if (err)
fs/overlayfs/util.c +40 -0
@@ -575,6 +575,16 @@ bool ovl_is_whiteout(struct dentry *dentry)return inode &&IS_WHITEOUT (inode); } +/* +* Use this over ovl_is_whiteout for upper and lower files, as it also +* handles overlay.whiteout xattr whiteout files. +*/ +bool ovl_path_is_whiteout (struct ovl_fs *ofs,const struct path *path) +{ +return ovl_is_whiteout (path->dentry) || +ovl_path_check_xwhiteout_xattr (ofs, path); +} +struct file *ovl_path_open (const struct path *path,int flags) {struct inode *inode =d_inode (path->dentry);
@@ -676,6 +686,32 @@ bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)return false; } +bool ovl_path_check_xwhiteout_xattr (struct ovl_fs *ofs,const struct path *path) +{ +struct dentry *dentry = path->dentry; +int res; + +/* xattr.whiteout must be a zero size regular file */ +if (!d_is_reg (dentry) ||i_size_read (d_inode (dentry)) !=0 ) +return false; + + res =ovl_path_getxattr (ofs, path,OVL_XATTR_XWHITEOUT ,NULL ,0 ); +return res >=0 ; +} + +bool ovl_path_check_xwhiteouts_xattr (struct ovl_fs *ofs,const struct path *path) +{ +struct dentry *dentry = path->dentry; +int res; + +/* xattr.whiteouts must be a directory */ +if (!d_is_dir (dentry)) +return false; + + res =ovl_path_getxattr (ofs, path,OVL_XATTR_XWHITEOUTS ,NULL ,0 ); +return res >=0 ; +} +/* * Load persistent uuid from xattr into s_uuid if found, or store a new * random generated value in s_uuid and in xattr.
@@ -760,6 +796,8 @@ bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,#define OVL_XATTR_UUID_POSTFIX "uuid"#define OVL_XATTR_METACOPY_POSTFIX "metacopy"#define OVL_XATTR_PROTATTR_POSTFIX "protattr" +#define OVL_XATTR_XWHITEOUT_POSTFIX "whiteout" +#define OVL_XATTR_XWHITEOUTS_POSTFIX "whiteouts"#define OVL_XATTR_TAB_ENTRY (x) \ [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
@@ -775,6 +813,8 @@ const char *const ovl_xattr_table[][2] = {OVL_XATTR_TAB_ENTRY (OVL_XATTR_UUID ),OVL_XATTR_TAB_ENTRY (OVL_XATTR_METACOPY ),OVL_XATTR_TAB_ENTRY (OVL_XATTR_PROTATTR ), +OVL_XATTR_TAB_ENTRY (OVL_XATTR_XWHITEOUT ), + OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUTS), }; int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
Signature · 1 line
2.41.0
PATCH v4 5/5 ovl: Add documentation on nesting of overlayfs mounts
In reply to Alexander Larsson--- Documentation/filesystems/overlayfs.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
Documentation/filesystems/overlayfs.rst +23 -0
@@ -492,6 +492,29 @@ directory tree on the same or different underlying filesystem, and even to a different machine. With the "inodes index" feature, trying to mount the copied layers will fail the verification of the lower root file handle. +Nesting overlayfs mounts +------------------------ + +It is possible to use a lower directory that is stored on an overlayfs +mount. For regular files this does not need any special care. However, files +that have overlayfs attributes, such as whiteouts or "overlay.*" xattrs will be +interpreted by the underlying overlayfs mount and stripped out. In order to +allow the second overlayfs mount to see the attributes they must be escaped. + +Overlayfs specific xattrs are escaped by using a special prefix of +"overlay.overlay.". So, a file with a "trusted.overlay.overlay.metacopy" xattr +in the lower dir will be exposed as a regular file with a +"trusted.overlay.metacopy" xattr in the overlayfs mount. This can be nested by +repeating the prefix multiple time, as each instance only removes one prefix. + +A lower dir with a regular whiteout will always be handled by the overlayfs +mount, so to support storing an effective whiteout file in an overlayfs mount an +alternative form of whiteout is supported. This form is a regular, zero-size +file with the "overlay.whiteout" xattr set, inside a directory with the +"overlay.whiteouts" xattr set. Such whiteouts are never created by overlayfs, +but can be used by userspace tools (like containers) that generate lower layers. +These alternative whiteouts can be escaped using the standard xattr escape +mechanism in order to properly nest to any depth. Non-standard behavior ---------------------
Signature · 1 line
2.41.0
Amir Goldstein
maintainerQuoted 17 lines · Alexander Larsson
There are cases where you want to use an overlayfs mount as a lowerdir for another overlayfs mount. For example, if the system rootfs is on overlayfs due to composefs, or to make it volatile (via tmpfs), then you cannot currently store a lowerdir on the rootfs, because the inner overlayfs will eat all the whiteouts and overlay xattrs. This means you can't e.g. store on the rootfs a prepared container image for use with overlayfs.
This patch series adds support for nesting of overlayfs mounts by escaping the problematic features and unescaping them when exposing to the overlayfs user.
This series is also available here:
https://github.com/alexlarsson/linux/tree/ovl-nestingAnd xfstest to test it is available here:
https://github.com/alexlarsson/xfstests/tree/overlayfs-nesting
Quoted 7 lines
The overlay/083 test checks both xattr escaping, the new whiteouts as well as actual nesting of overlayfs.
Note that this series breaks the overlay/026 test which validates that writing overlay.* xattrs is not supported, but it now is. I'm not sure if we should fix this test to not fail, or if we should make this an opt-in mount feature.
Please fix the test as we discussed:
1. relax the requirement that getxattr trusted.overlay should fail
2. set expectations for setxattr depending on result of getxattr
This way we will not lose much test coverage for old kernels. This test fix can be posted to fstests ahead of time as preparation for your patch set.
Given that this version has dropped the controversial bits and that the whiteout implementation is the one that was proposed by Miklos, I don't see any problem with it now. I can queue it up for 6.7 and if Miklos has any objections or comments, we have enough time to address them.
Thanks,
Amir.
Quoted 39 lines
Changes since v3:
* Dropped the handling of whiteout xattrs across layers.
* Copy-up escaped overlayfs xattrs.
* Minor code cleanups.Changes since v2:
* Uses a new approach for escaping whiteouts with a regular file with an overlay.whiteout xattr in a lower directory with an overlay.whiteouts xattr.Changes since v1:
* Moved all xattr handling to
xattr.c
* Made creation of escaped whiteouts atomicAlexander Larsson (5):
ovl: Move xattr support to newxattrs.cfile
ovl: Add OVL_XATTR_TRUSTED/USER_PREFIX_LEN macros
ovl: Support escaped overlay.* xattrs
ovl: Add an alternative type of whiteout
ovl: Add documentation on nesting of overlayfs mountsDocumentation/filesystems/overlayfs.rst | 23 ++ fs/overlayfs/Makefile | 2 +- fs/overlayfs/dir.c | 4 +- fs/overlayfs/inode.c | 124 ----------- fs/overlayfs/namei.c | 15 +- fs/overlayfs/overlayfs.h | 42 +++- fs/overlayfs/readdir.c | 27 ++- fs/overlayfs/super.c | 67 +----- fs/overlayfs/util.c | 40 ++++ fs/overlayfs/xattrs.c | 273 ++++++++++++++++++++++++ 10 files changed, 404 insertions(+), 213 deletions(-) create mode 100644 fs/overlayfs/xattrs.c--
2.41.0
A message that is not in the archive (sent off-list or not yet received): CAL7ro1F9cuyXqQEgQHm1Fwo0vFJTUFVXuBXR-v9U1JUFpcW11Q@mail.gmail.com
Amir Goldstein
maintainerQuoted 35 lines · Alexander Larsson
Quoted 31 lines · Amir Goldstein
Quoted 17 lines · Alexander Larsson
There are cases where you want to use an overlayfs mount as a lowerdir for another overlayfs mount. For example, if the system rootfs is on overlayfs due to composefs, or to make it volatile (via tmpfs), then you cannot currently store a lowerdir on the rootfs, because the inner overlayfs will eat all the whiteouts and overlay xattrs. This means you can't e.g. store on the rootfs a prepared container image for use with overlayfs.
This patch series adds support for nesting of overlayfs mounts by escaping the problematic features and unescaping them when exposing to the overlayfs user.
This series is also available here:
https://github.com/alexlarsson/linux/tree/ovl-nestingAnd xfstest to test it is available here:
https://github.com/alexlarsson/xfstests/tree/overlayfs-nestingQuoted 7 lines
The overlay/083 test checks both xattr escaping, the new whiteouts as well as actual nesting of overlayfs.
Note that this series breaks the overlay/026 test which validates that writing overlay.* xattrs is not supported, but it now is. I'm not sure if we should fix this test to not fail, or if we should make this an opt-in mount feature.
Please fix the test as we discussed:
1. relax the requirement that getxattr trusted.overlay should fail
2. set expectations for setxattr depending on result of getxattr
Quoted 7 lines
Quoted 3 lines
This way we will not lose much test coverage for old kernels. This test fix can be posted to fstests ahead of time as preparation for your patch set.
Added to https://github.com/alexlarsson/xfstests/commits/overlayfs-nesting
Nice.
Not sure that you need "getattr ok" in the golden output.
Also, if you test "legit failure" with [[ "$res" =~ "Operation not" ]] there is no need to sed the output of [gs]etfattr not to _filter_scratch. Those are only done to canonicalize the golder output.
However, test 083 failed all the xwhiteout test cases when I ran it with your ovl-nesting-4 tag:
== Check xwhiteout trusted ==
+hidden
regular
-stat: cannot statx 'SCRATCH_MNT/hidden': No such file or directory
+ File: SCRATCH_MNT/hidden
...
Quoted 59 lines
Quoted 6 lines
Given that this version has dropped the controversial bits and that the whiteout implementation is the one that was proposed by Miklos, I don't see any problem with it now. I can queue it up for 6.7 and if Miklos has any objections or comments, we have enough time to address them.
That sounds good to me.
Quoted 44 lines
Thanks,
Amir.Quoted 39 lines
Changes since v3:
* Dropped the handling of whiteout xattrs across layers.
* Copy-up escaped overlayfs xattrs.
* Minor code cleanups.Changes since v2:
* Uses a new approach for escaping whiteouts with a regular file with an overlay.whiteout xattr in a lower directory with an overlay.whiteouts xattr.Changes since v1:
* Moved all xattr handling to
xattr.c
* Made creation of escaped whiteouts atomicAlexander Larsson (5):
ovl: Move xattr support to newxattrs.cfile
ovl: Add OVL_XATTR_TRUSTED/USER_PREFIX_LEN macros
ovl: Support escaped overlay.* xattrs
ovl: Add an alternative type of whiteout
ovl: Add documentation on nesting of overlayfs mountsDocumentation/filesystems/overlayfs.rst | 23 ++ fs/overlayfs/Makefile | 2 +- fs/overlayfs/dir.c | 4 +- fs/overlayfs/inode.c | 124 ----------- fs/overlayfs/namei.c | 15 +- fs/overlayfs/overlayfs.h | 42 +++- fs/overlayfs/readdir.c | 27 ++- fs/overlayfs/super.c | 67 +----- fs/overlayfs/util.c | 40 ++++ fs/overlayfs/xattrs.c | 273 ++++++++++++++++++++++++ 10 files changed, 404 insertions(+), 213 deletions(-) create mode 100644 fs/overlayfs/xattrs.c--
2.41.0--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Alexander Larsson Red Hat, Inc
[email protected] [email protected]