[PATCH 14/18] fsinfo: Add support to ext4 [ver #21]
From: David Howells
Date: Mon Aug 03 2020 - 09:38:42 EST
Add support to ext4, including the following:
(1) FSINFO_ATTR_SUPPORTS: Information about supported STATX attributes and
support for ioctls like FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR.
(2) FSINFO_ATTR_FEATURES: Information about features supported by an ext4
filesystem, such as whether version counting, birth time and name case
folding are in operation.
(3) FSINFO_ATTR_VOLUME_NAME: The volume name from the superblock.
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Reviewed-by: Darrick J. Wong <darrick.wong@xxxxxxxxxx>
cc: "Theodore Ts'o" <tytso@xxxxxxx>
cc: Andreas Dilger <adilger.kernel@xxxxxxxxx>
cc: Eric Biggers <ebiggers@xxxxxxxxxx>
cc: linux-ext4@xxxxxxxxxxxxxxx
---
fs/ext4/Makefile | 1 +
fs/ext4/ext4.h | 6 +++
fs/ext4/fsinfo.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/ext4/super.c | 3 ++
4 files changed, 107 insertions(+)
create mode 100644 fs/ext4/fsinfo.c
diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
index 2e42f47a7f98..ad67812bf7d0 100644
--- a/fs/ext4/Makefile
+++ b/fs/ext4/Makefile
@@ -17,3 +17,4 @@ ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
ext4-inode-test-objs += inode-test.o
obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-inode-test.o
ext4-$(CONFIG_FS_VERITY) += verity.o
+ext4-$(CONFIG_FSINFO) += fsinfo.o
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 42f5060f3cdf..99a737cf6308 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -43,6 +43,7 @@
#include <linux/fscrypt.h>
#include <linux/fsverity.h>
+#include <linux/fsinfo.h>
#include <linux/compiler.h>
@@ -3233,6 +3234,11 @@ extern const struct inode_operations ext4_file_inode_operations;
extern const struct file_operations ext4_file_operations;
extern loff_t ext4_llseek(struct file *file, loff_t offset, int origin);
+/* fsinfo.c */
+#ifdef CONFIG_FSINFO
+extern int ext4_fsinfo(struct path *path, struct fsinfo_context *ctx);
+#endif
+
/* inline.c */
extern int ext4_get_max_inline_size(struct inode *inode);
extern int ext4_find_inline_data_nolock(struct inode *inode);
diff --git a/fs/ext4/fsinfo.c b/fs/ext4/fsinfo.c
new file mode 100644
index 000000000000..1d4093ef32e7
--- /dev/null
+++ b/fs/ext4/fsinfo.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Filesystem information for ext4
+ *
+ * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@xxxxxxxxxx)
+ */
+
+#include <linux/mount.h>
+#include "ext4.h"
+
+static int ext4_fsinfo_supports(struct path *path, struct fsinfo_context *ctx)
+{
+ struct fsinfo_supports *p = ctx->buffer;
+ struct inode *inode = d_inode(path->dentry);
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ struct ext4_inode *raw_inode;
+ u32 flags;
+
+ fsinfo_generic_supports(path, ctx);
+ p->stx_attributes |= (STATX_ATTR_APPEND |
+ STATX_ATTR_COMPRESSED |
+ STATX_ATTR_ENCRYPTED |
+ STATX_ATTR_IMMUTABLE |
+ STATX_ATTR_NODUMP |
+ STATX_ATTR_VERITY);
+ if (EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime))
+ p->stx_mask |= STATX_BTIME;
+
+ flags = EXT4_FL_USER_VISIBLE;
+ if (S_ISREG(inode->i_mode))
+ flags &= ~EXT4_PROJINHERIT_FL;
+ p->fs_ioc_getflags = flags;
+ flags &= EXT4_FL_USER_MODIFIABLE;
+ p->fs_ioc_setflags_set = flags;
+ p->fs_ioc_setflags_clear = flags;
+
+ p->fs_ioc_fsgetxattr_xflags = EXT4_FL_XFLAG_VISIBLE;
+ p->fs_ioc_fssetxattr_xflags_set = EXT4_FL_XFLAG_VISIBLE;
+ p->fs_ioc_fssetxattr_xflags_clear = EXT4_FL_XFLAG_VISIBLE;
+ return sizeof(*p);
+}
+
+static int ext4_fsinfo_features(struct path *path, struct fsinfo_context *ctx)
+{
+ struct fsinfo_features *p = ctx->buffer;
+ struct super_block *sb = path->dentry->d_sb;
+ struct inode *inode = d_inode(path->dentry);
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ struct ext4_inode *raw_inode;
+
+ fsinfo_generic_features(path, ctx);
+ fsinfo_set_unix_features(p);
+ fsinfo_set_feature(p, FSINFO_FEAT_VOLUME_UUID);
+ fsinfo_set_feature(p, FSINFO_FEAT_VOLUME_NAME);
+ fsinfo_set_feature(p, FSINFO_FEAT_O_SYNC);
+ fsinfo_set_feature(p, FSINFO_FEAT_O_DIRECT);
+ fsinfo_set_feature(p, FSINFO_FEAT_ADV_LOCKS);
+
+ if (test_opt(sb, XATTR_USER))
+ fsinfo_set_feature(p, FSINFO_FEAT_XATTRS);
+ if (ext4_has_feature_journal(sb))
+ fsinfo_set_feature(p, FSINFO_FEAT_JOURNAL);
+ if (ext4_has_feature_casefold(sb))
+ fsinfo_set_feature(p, FSINFO_FEAT_NAME_CASE_INDEP);
+
+ if (sb->s_flags & SB_I_VERSION &&
+ !test_opt2(sb, HURD_COMPAT) &&
+ EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE) {
+ fsinfo_set_feature(p, FSINFO_FEAT_IVER_DATA_CHANGE);
+ fsinfo_set_feature(p, FSINFO_FEAT_IVER_MONO_INCR);
+ }
+
+ if (EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime))
+ fsinfo_set_feature(p, FSINFO_FEAT_HAS_BTIME);
+ return sizeof(*p);
+}
+
+static int ext4_fsinfo_get_volume_name(struct path *path, struct fsinfo_context *ctx)
+{
+ const struct ext4_sb_info *sbi = EXT4_SB(path->mnt->mnt_sb);
+ const struct ext4_super_block *es = sbi->s_es;
+
+ memcpy(ctx->buffer, es->s_volume_name, sizeof(es->s_volume_name));
+ return strlen(ctx->buffer) + 1;
+}
+
+static const struct fsinfo_attribute ext4_fsinfo_attributes[] = {
+ FSINFO_VSTRUCT (FSINFO_ATTR_SUPPORTS, ext4_fsinfo_supports),
+ FSINFO_VSTRUCT (FSINFO_ATTR_FEATURES, ext4_fsinfo_features),
+ FSINFO_STRING (FSINFO_ATTR_VOLUME_NAME, ext4_fsinfo_get_volume_name),
+ {}
+};
+
+int ext4_fsinfo(struct path *path, struct fsinfo_context *ctx)
+{
+ return fsinfo_get_attribute(path, ctx, ext4_fsinfo_attributes);
+}
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 330957ed1f05..47f349620176 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1481,6 +1481,9 @@ static const struct super_operations ext4_sops = {
.freeze_fs = ext4_freeze,
.unfreeze_fs = ext4_unfreeze,
.statfs = ext4_statfs,
+#ifdef CONFIG_FSINFO
+ .fsinfo = ext4_fsinfo,
+#endif
.remount_fs = ext4_remount,
.show_options = ext4_show_options,
#ifdef CONFIG_QUOTA