Skip to content

Commit d9544c1

Browse files
committed
ovl: store persistent uuid/fsid with uuid=on
With uuid=on, store a persistent uuid in xattr on the upper dir to give the overlayfs instance a persistent identifier. This also makes f_fsid persistent and more reliable for reporting fid info in fanotify events. uuid=on is not supported on non-upper overlayfs or with upper fs that does not support xattrs. Signed-off-by: Amir Goldstein <amir73il@gmail.com>
1 parent b0504bf commit d9544c1

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

Documentation/filesystems/overlayfs.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ controlled by the "uuid" mount option, which supports these values:
671671
UUID of underlying layers is ignored.
672672
- "on":
673673
UUID of overlayfs is generated and used to report a unique fsid.
674+
UUID is stored in xattr "trusted.overlay.uuid", making overlayfs fsid
675+
unique and persistent. This option requires an overlayfs with upper
676+
filesystem that supports xattrs.
674677

675678

676679
Volatile mount

fs/overlayfs/overlayfs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum ovl_xattr {
3737
OVL_XATTR_IMPURE,
3838
OVL_XATTR_NLINK,
3939
OVL_XATTR_UPPER,
40+
OVL_XATTR_UUID,
4041
OVL_XATTR_METACOPY,
4142
OVL_XATTR_PROTATTR,
4243
};
@@ -465,6 +466,8 @@ bool ovl_already_copied_up(struct dentry *dentry, int flags);
465466
bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
466467
enum ovl_xattr ox);
467468
bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path);
469+
bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
470+
const struct path *upperpath);
468471

469472
static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs,
470473
struct dentry *upperdentry)

fs/overlayfs/params.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,11 @@ int ovl_fs_params_verify(const struct ovl_fs_context *ctx,
801801
config->ovl_volatile = false;
802802
}
803803

804+
if (!config->upperdir && config->uuid == OVL_UUID_ON) {
805+
pr_info("option \"uuid=on\" requires an upper fs, falling back to uuid=null.\n");
806+
config->uuid = OVL_UUID_NULL;
807+
}
808+
804809
/* Resolve verity -> metacopy dependency */
805810
if (config->verity_mode && !config->metacopy) {
806811
/* Don't allow explicit specified conflicting combinations */

fs/overlayfs/super.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,10 @@ static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
777777
ofs->config.index = false;
778778
pr_warn("...falling back to index=off.\n");
779779
}
780+
if (ovl_has_fsid(ofs)) {
781+
ofs->config.uuid = OVL_UUID_NULL;
782+
pr_warn("...falling back to uuid=null.\n");
783+
}
780784
/*
781785
* xattr support is required for persistent st_ino.
782786
* Without persistent st_ino, xino=auto falls back to xino=off.
@@ -1427,9 +1431,9 @@ int ovl_fill_super(struct super_block *sb, struct fs_context *fc)
14271431
if (!ovl_origin_uuid(ofs) && ofs->numfs > 1) {
14281432
pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=null.\n");
14291433
ofs->config.uuid = OVL_UUID_NULL;
1430-
} else if (ovl_has_fsid(ofs)) {
1431-
/* Use per instance uuid/fsid */
1432-
uuid_gen(&sb->s_uuid);
1434+
} else if (ovl_has_fsid(ofs) && ovl_upper_mnt(ofs)) {
1435+
/* Use per instance persistent uuid/fsid */
1436+
ovl_init_uuid_xattr(sb, ofs, &ctx->upper);
14331437
}
14341438

14351439
if (!ovl_force_readonly(ofs) && ofs->config.index) {

fs/overlayfs/util.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,43 @@ bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
676676
return false;
677677
}
678678

679+
/*
680+
* Load persistent uuid from xattr into s_uuid if found, or store a new
681+
* random generated value in s_uuid and in xattr.
682+
*/
683+
bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
684+
const struct path *upperpath)
685+
{
686+
bool set = false;
687+
int res;
688+
689+
/* Try to load existing persistent uuid */
690+
res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, sb->s_uuid.b,
691+
UUID_SIZE);
692+
if (res == UUID_SIZE)
693+
return true;
694+
695+
if (res != -ENODATA)
696+
goto fail;
697+
698+
/* Generate overlay instance uuid */
699+
uuid_gen(&sb->s_uuid);
700+
701+
/* Try to store persistent uuid */
702+
set = true;
703+
res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, sb->s_uuid.b,
704+
UUID_SIZE);
705+
if (res == 0)
706+
return true;
707+
708+
fail:
709+
memset(sb->s_uuid.b, 0, UUID_SIZE);
710+
ofs->config.uuid = OVL_UUID_NULL;
711+
pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
712+
set ? "set" : "get", upperpath->dentry, res);
713+
return false;
714+
}
715+
679716
bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
680717
enum ovl_xattr ox)
681718
{
@@ -698,6 +735,7 @@ bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
698735
#define OVL_XATTR_IMPURE_POSTFIX "impure"
699736
#define OVL_XATTR_NLINK_POSTFIX "nlink"
700737
#define OVL_XATTR_UPPER_POSTFIX "upper"
738+
#define OVL_XATTR_UUID_POSTFIX "uuid"
701739
#define OVL_XATTR_METACOPY_POSTFIX "metacopy"
702740
#define OVL_XATTR_PROTATTR_POSTFIX "protattr"
703741

@@ -712,6 +750,7 @@ const char *const ovl_xattr_table[][2] = {
712750
OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
713751
OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
714752
OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
753+
OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
715754
OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
716755
OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
717756
};

0 commit comments

Comments
 (0)