ovl: add support for unique fsid per instance
authorAmir Goldstein <amir73il@gmail.com>
Mon, 26 Jun 2023 13:34:25 +0000 (16:34 +0300)
committerAmir Goldstein <amir73il@gmail.com>
Sat, 12 Aug 2023 16:02:50 +0000 (19:02 +0300)
The legacy behavior of ovl_statfs() reports the f_fsid filled by
underlying upper fs. This fsid is not unique among overlayfs instances
on the same upper fs.

With mount option uuid=on, generate a non-persistent uuid per overlayfs
instance and use it as the seed for f_fsid, similar to tmpfs.

This is useful for reporting fanotify events with fid info from different
instances of overlayfs over the same upper fs.

The old behavior of null uuid and upper fs fsid is retained with the
mount option uuid=null, which is the default.

The mount option uuid=off that disables uuid checks in underlying layers
also retains the legacy behavior.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Documentation/filesystems/overlayfs.rst
fs/overlayfs/copy_up.c
fs/overlayfs/namei.c
fs/overlayfs/overlayfs.h
fs/overlayfs/ovl_entry.h
fs/overlayfs/params.c
fs/overlayfs/super.c

index b63e0db036311abad3c21355b8e39e6a1c30ee08..d55381d3fa0f483eeb5a268914fb0922fdba0deb 100644 (file)
@@ -657,6 +657,22 @@ can be useful in case the underlying disk is copied and the UUID of this copy
 is changed. This is only applicable if all lower/upper/work directories are on
 the same filesystem, otherwise it will fallback to normal behaviour.
 
+
+UUID and fsid
+-------------
+
+The UUID of overlayfs instance itself and the fsid reported by statfs(2) are
+controlled by the "uuid" mount option, which supports these values:
+
+- "null": (default)
+    UUID of overlayfs is null. fsid is taken from upper most filesystem.
+- "off":
+    UUID of overlayfs is null. fsid is taken from upper most filesystem.
+    UUID of underlying layers is ignored.
+- "on":
+    UUID of overlayfs is generated and used to report a unique fsid.
+
+
 Volatile mount
 --------------
 
index 2ead7c9a77487c6e6b984c39b941e6e260a1ea07..618651b548185fd5b35b9e255cc8b5a7e3bd8d76 100644 (file)
@@ -416,7 +416,7 @@ struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
        if (is_upper)
                fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
        fh->fb.len = sizeof(fh->fb) + buflen;
-       if (ofs->config.uuid)
+       if (ovl_origin_uuid(ofs))
                fh->fb.uuid = *uuid;
 
        return fh;
index d00ec43f2376f0f60ceda8ad20e58eee3a9e1695..84c06512fb71bd2bc8e9cdea463e3a0ff3ff734c 100644 (file)
@@ -171,8 +171,9 @@ struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
         * layer where file handle will be decoded.
         * In case of uuid=off option just make sure that stored uuid is null.
         */
-       if (ofs->config.uuid ? !uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid) :
-                             !uuid_is_null(&fh->fb.uuid))
+       if (ovl_origin_uuid(ofs) ?
+           !uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid) :
+           !uuid_is_null(&fh->fb.uuid))
                return NULL;
 
        bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
index 453610fb9bf960251d96929919610338d1efa1ad..000dd89fe31939167e043788b1da216be00303e5 100644 (file)
@@ -67,6 +67,12 @@ enum {
        OVL_REDIRECT_ON,
 };
 
+enum {
+       OVL_UUID_OFF,
+       OVL_UUID_NULL,
+       OVL_UUID_ON,
+};
+
 enum {
        OVL_XINO_OFF,
        OVL_XINO_AUTO,
@@ -534,6 +540,16 @@ static inline bool ovl_redirect_dir(struct ovl_fs *ofs)
        return ofs->config.redirect_mode == OVL_REDIRECT_ON;
 }
 
+static inline bool ovl_origin_uuid(struct ovl_fs *ofs)
+{
+       return ofs->config.uuid != OVL_UUID_OFF;
+}
+
+static inline bool ovl_has_fsid(struct ovl_fs *ofs)
+{
+       return ofs->config.uuid == OVL_UUID_ON;
+}
+
 /*
  * With xino=auto, we do best effort to keep all inodes on same st_dev and
  * d_ino consistent with st_ino.
index 7a5196c94d7527c2827e0456e07f840e23ce473b..5d03f449adb1f0fab214b2316e32c607b77c9894 100644 (file)
@@ -12,7 +12,7 @@ struct ovl_config {
        int redirect_mode;
        int verity_mode;
        bool index;
-       bool uuid;
+       int uuid;
        bool nfs_export;
        int xino;
        bool metacopy;
index 575a60b76a6c39183710c80b1a96e383429c2a5d..1ff93467e79312abee65e4e4a74c0a295047d437 100644 (file)
@@ -65,6 +65,23 @@ static const struct constant_table ovl_parameter_bool[] = {
        {}
 };
 
+static const struct constant_table ovl_parameter_uuid[] = {
+       { "off",        OVL_UUID_OFF  },
+       { "null",       OVL_UUID_NULL },
+       { "on",         OVL_UUID_ON   },
+       {}
+};
+
+static const char *ovl_uuid_mode(struct ovl_config *config)
+{
+       return ovl_parameter_uuid[config->uuid].name;
+}
+
+static int ovl_uuid_def(void)
+{
+       return OVL_UUID_NULL;
+}
+
 static const struct constant_table ovl_parameter_xino[] = {
        { "off",        OVL_XINO_OFF  },
        { "auto",       OVL_XINO_AUTO },
@@ -129,7 +146,7 @@ const struct fs_parameter_spec ovl_parameter_spec[] = {
        fsparam_flag("default_permissions", Opt_default_permissions),
        fsparam_enum("redirect_dir",        Opt_redirect_dir, ovl_parameter_redirect_dir),
        fsparam_enum("index",               Opt_index, ovl_parameter_bool),
-       fsparam_enum("uuid",                Opt_uuid, ovl_parameter_bool),
+       fsparam_enum("uuid",                Opt_uuid, ovl_parameter_uuid),
        fsparam_enum("nfs_export",          Opt_nfs_export, ovl_parameter_bool),
        fsparam_flag("userxattr",           Opt_userxattr),
        fsparam_enum("xino",                Opt_xino, ovl_parameter_xino),
@@ -701,7 +718,7 @@ int ovl_init_fs_context(struct fs_context *fc)
 
        ofs->config.redirect_mode       = ovl_redirect_mode_def();
        ofs->config.index               = ovl_index_def;
-       ofs->config.uuid                = true;
+       ofs->config.uuid                = ovl_uuid_def();
        ofs->config.nfs_export          = ovl_nfs_export_def;
        ofs->config.xino                = ovl_xino_def();
        ofs->config.metacopy            = ovl_metacopy_def;
@@ -947,8 +964,8 @@ int ovl_show_options(struct seq_file *m, struct dentry *dentry)
                           ovl_redirect_mode(&ofs->config));
        if (ofs->config.index != ovl_index_def)
                seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
-       if (!ofs->config.uuid)
-               seq_puts(m, ",uuid=off");
+       if (ofs->config.uuid != ovl_uuid_def())
+               seq_printf(m, ",uuid=%s", ovl_uuid_mode(&ofs->config));
        if (ofs->config.nfs_export != ovl_nfs_export_def)
                seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
                                                "on" : "off");
index e56108ffe8aa5cebb5b99e5d99bdf5549d133156..c2bab6106e98830a3a6a123ea0dcbafd184ae75e 100644 (file)
@@ -242,8 +242,9 @@ static int ovl_sync_fs(struct super_block *sb, int wait)
  */
 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
-       struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
-       struct dentry *root_dentry = dentry->d_sb->s_root;
+       struct super_block *sb = dentry->d_sb;
+       struct ovl_fs *ofs = OVL_FS(sb);
+       struct dentry *root_dentry = sb->s_root;
        struct path path;
        int err;
 
@@ -253,6 +254,8 @@ static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
        if (!err) {
                buf->f_namelen = ofs->namelen;
                buf->f_type = OVERLAYFS_SUPER_MAGIC;
+               if (ovl_has_fsid(ofs))
+                       buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
        }
 
        return err;
@@ -1421,9 +1424,12 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc)
        if (!ovl_upper_mnt(ofs))
                sb->s_flags |= SB_RDONLY;
 
-       if (!ofs->config.uuid && ofs->numfs > 1) {
-               pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n");
-               ofs->config.uuid = true;
+       if (!ovl_origin_uuid(ofs) && ofs->numfs > 1) {
+               pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=null.\n");
+               ofs->config.uuid = OVL_UUID_NULL;
+       } else if (ovl_has_fsid(ofs)) {
+               /* Use per instance uuid/fsid */
+               uuid_gen(&sb->s_uuid);
        }
 
        if (!ovl_force_readonly(ofs) && ofs->config.index) {