Skip to contentSkip to conversation

vfs: fs{set,get}attr iops

RFC PATCH In review Miklos Szeredi 3 participants 5 messages Nov 23, 2020 – Nov 24, 2020

Subsystems (10)
lore
Share & tools
lore

Miklos Szeredi

author maintainer
#0 lore raw

RFC PATCH 1/1 vfs: fs{set,get}attr iops

Dmitry found an issue with overlayfs's FS_IOC_SETFLAGS/FS_IOC_FSSETXATTR implementation:

https://lore.kernel.org/linux-unionfs/CACT4Y+bUfavwMVv2SEMve5pabE_AwsDO0YsRBGZtYqX59a77vA@mail.gmail.com/

I think the only proper soltuion is to move these into inode operations, which should be a good cleanup as well.

This is a first cut, the FS_IOC_SETFLAGS conversion is not complete, and only lightly tested on ext4 and xfs.

There are minor changes in behavior, like the exact errno value in case of multiple error conditions.

Thanks,
Miklos

---
 fs/btrfs/ctree.h              |    2 
 fs/btrfs/inode.c              |    4 
 fs/btrfs/ioctl.c              |  246 ++++++------------------------------
 fs/ext2/ext2.h                |    6 
 fs/ext2/file.c                |    2 
 fs/ext2/ioctl.c               |   85 ++++--------
 fs/ext2/namei.c               |    2 
 fs/ext4/ext4.h                |   11 -
 fs/ext4/file.c                |    2 
 fs/ext4/ioctl.c               |  201 ++++++------------------------
 fs/ext4/namei.c               |    2 
 fs/f2fs/f2fs.h                |    2 
 fs/f2fs/file.c                |  210 ++++---------------------------
 fs/f2fs/namei.c               |    2 
 fs/gfs2/file.c                |   56 +-------
 fs/gfs2/inode.c               |    4 
 fs/gfs2/inode.h               |    2 
 fs/inode.c                    |   87 -------------
 fs/ioctl.c                    |  280 ++++++++++++++++++++++++++++++++++++++++++
 fs/orangefs/file.c            |   79 -----------
 fs/orangefs/inode.c           |   50 +++++++
 fs/overlayfs/dir.c            |    2 
 fs/overlayfs/file.c           |  181 ++++-----------------------
 fs/overlayfs/inode.c          |    2 
 fs/overlayfs/overlayfs.h      |    4 
 fs/overlayfs/readdir.c        |    4 
 fs/xfs/libxfs/xfs_fs.h        |    2 
 fs/xfs/xfs_ioctl.c            |  226 +++++++--------------------------
 fs/xfs/xfs_ioctl.h            |   10 +
 fs/xfs/xfs_ioctl32.c          |    2 
 fs/xfs/xfs_ioctl32.h          |    2 
 fs/xfs/xfs_iops.c             |    7 +
 include/linux/fs.h            |   15 --
 include/linux/ioctl_helpers.h |   46 ++++++
 34 files changed, 668 insertions(+), 1170 deletions(-)
fs/btrfs/ctree.h +2 -0
@@ -3065,6 +3065,8 @@ ssize_t btrfs_direct_IO(struct kiocb *io
 /* ioctl.c */
 long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
+int btrfs_fsgetxattr(struct dentry *dentry, struct kfsxattr *fa);
+int btrfs_fssetxattr(struct dentry *dentry, struct kfsxattr *fa);
 int btrfs_ioctl_get_supported_features(void __user *arg);
 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);
 int __pure btrfs_is_empty_uuid(u8 *uuid);
fs/btrfs/inode.c +4 -0
@@ -10226,6 +10226,8 @@ static const struct inode_operations btr
 	.set_acl	= btrfs_set_acl,
 	.update_time	= btrfs_update_time,
 	.tmpfile        = btrfs_tmpfile,
+	.fsgetxattr	= btrfs_fsgetxattr,
+	.fssetxattr	= btrfs_fssetxattr,
 };
 
 static const struct file_operations btrfs_dir_file_operations = {
@@ -10279,6 +10281,8 @@ static const struct inode_operations btr
 	.get_acl	= btrfs_get_acl,
 	.set_acl	= btrfs_set_acl,
 	.update_time	= btrfs_update_time,
+	.fsgetxattr	= btrfs_fsgetxattr,
+	.fssetxattr	= btrfs_fssetxattr,
 };
 static const struct inode_operations btrfs_special_inode_operations = {
 	.getattr	= btrfs_getattr,
fs/btrfs/ioctl.c +43 -203

326 lines not on this page · raw message

fs/ext2/ext2.h +2 -4
@@ -281,8 +281,6 @@ static inline __u32 ext2_mask_flags(umod
 /*
  * ioctl commands
  */
-#define	EXT2_IOC_GETFLAGS		FS_IOC_GETFLAGS
-#define	EXT2_IOC_SETFLAGS		FS_IOC_SETFLAGS
 #define	EXT2_IOC_GETVERSION		FS_IOC_GETVERSION
 #define	EXT2_IOC_SETVERSION		FS_IOC_SETVERSION
 #define	EXT2_IOC_GETRSVSZ		_IOR('f', 5, long)
@@ -291,8 +289,6 @@ static inline __u32 ext2_mask_flags(umod
 /*
  * ioctl commands in 32 bit emulation
  */
-#define EXT2_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
-#define EXT2_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
 #define EXT2_IOC32_GETVERSION		FS_IOC32_GETVERSION
 #define EXT2_IOC32_SETVERSION		FS_IOC32_SETVERSION
 
@@ -764,6 +760,8 @@ extern int ext2_fiemap(struct inode *ino
 		       u64 start, u64 len);
 
 /* ioctl.c */
+extern int ext2_fsgetxattr(struct dentry *dentry, struct kfsxattr *fa);
+extern int ext2_fssetxattr(struct dentry *dentry, struct kfsxattr *fa);
 extern long ext2_ioctl(struct file *, unsigned int, unsigned long);
 extern long ext2_compat_ioctl(struct file *, unsigned int, unsigned long);
 
fs/ext2/file.c +2 -0
@@ -204,4 +204,6 @@ const struct inode_operations ext2_file_
 	.get_acl	= ext2_get_acl,
 	.set_acl	= ext2_set_acl,
 	.fiemap		= ext2_fiemap,
+	.fsgetxattr	= ext2_fsgetxattr,
+	.fssetxattr	= ext2_fssetxattr,
 };
fs/ext2/ioctl.c +28 -57

109 lines not on this page · raw message

fs/ext2/namei.c +2 -0
@@ -426,6 +426,8 @@ const struct inode_operations ext2_dir_i
 	.get_acl	= ext2_get_acl,
 	.set_acl	= ext2_set_acl,
 	.tmpfile	= ext2_tmpfile,
+	.fsgetxattr	= ext2_fsgetxattr,
+	.fssetxattr	= ext2_fssetxattr,
 };
 
 const struct inode_operations ext2_special_inode_operations = {
fs/ext4/ext4.h +2 -9
@@ -462,15 +462,6 @@ struct flex_groups {
 					 EXT4_VERITY_FL | \
 					 EXT4_INLINE_DATA_FL)
 
-/* Flags we can manipulate with through FS_IOC_FSSETXATTR */
-#define EXT4_FL_XFLAG_VISIBLE		(EXT4_SYNC_FL | \
-					 EXT4_IMMUTABLE_FL | \
-					 EXT4_APPEND_FL | \
-					 EXT4_NODUMP_FL | \
-					 EXT4_NOATIME_FL | \
-					 EXT4_PROJINHERIT_FL | \
-					 EXT4_DAX_FL)
-
 /* Flags that should be inherited by new inodes from their parent. */
 #define EXT4_FL_INHERITED (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL |\
 			   EXT4_SYNC_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL |\
@@ -2876,6 +2867,8 @@ extern int ext4_ind_remove_space(handle_
 /* ioctl.c */
 extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
 extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
+int ext4_fssetxattr(struct dentry *dentry, struct kfsxattr *fa);
+int ext4_fsgetxattr(struct dentry *dentry, struct kfsxattr *fa);
 extern void ext4_reset_inode_seed(struct inode *inode);
 
 /* migrate.c */
fs/ext4/file.c +2 -0
@@ -919,5 +919,7 @@ const struct inode_operations ext4_file_
 	.get_acl	= ext4_get_acl,
 	.set_acl	= ext4_set_acl,
 	.fiemap		= ext4_fiemap,
+	.fsgetxattr	= ext4_fsgetxattr,
+	.fssetxattr	= ext4_fssetxattr,
 };
 
fs/ext4/ioctl.c +42 -159

268 lines not on this page · raw message

fs/ext4/namei.c +2 -0
@@ -4138,6 +4138,8 @@ const struct inode_operations ext4_dir_i
 	.get_acl	= ext4_get_acl,
 	.set_acl	= ext4_set_acl,
 	.fiemap         = ext4_fiemap,
+	.fsgetxattr	= ext4_fsgetxattr,
+	.fssetxattr	= ext4_fssetxattr,
 };
 
 const struct inode_operations ext4_special_inode_operations = {
fs/f2fs/f2fs.h +2 -0
@@ -3173,6 +3173,8 @@ int f2fs_setattr(struct dentry *dentry,
 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
 int f2fs_precache_extents(struct inode *inode);
+int f2fs_fsgetxattr(struct dentry *dentry, struct kfsxattr *fa);
+int f2fs_fssetxattr(struct dentry *dentry, struct kfsxattr *fa);
 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
fs/f2fs/file.c +33 -177

291 lines not on this page · raw message

fs/f2fs/namei.c +2 -0
@@ -1315,6 +1315,8 @@ const struct inode_operations f2fs_dir_i
 	.set_acl	= f2fs_set_acl,
 	.listxattr	= f2fs_listxattr,
 	.fiemap		= f2fs_fiemap,
+	.fsgetxattr	= f2fs_fsgetxattr,
+	.fssetxattr	= f2fs_fssetxattr,
 };
 
 const struct inode_operations f2fs_symlink_inode_operations = {
fs/gfs2/file.c +13 -43

128 lines not on this page · raw message

fs/gfs2/inode.c +4 -0
@@ -2123,6 +2123,8 @@ const struct inode_operations gfs2_file_
 	.fiemap = gfs2_fiemap,
 	.get_acl = gfs2_get_acl,
 	.set_acl = gfs2_set_acl,
+	.fsgetxattr = gfs2_fsgetxattr,
+	.fssetxattr = gfs2_fssetxattr,
 };
 
 const struct inode_operations gfs2_dir_iops = {
@@ -2143,6 +2145,8 @@ const struct inode_operations gfs2_dir_i
 	.get_acl = gfs2_get_acl,
 	.set_acl = gfs2_set_acl,
 	.atomic_open = gfs2_atomic_open,
+	.fsgetxattr = gfs2_fsgetxattr,
+	.fssetxattr = gfs2_fssetxattr,
 };
 
 const struct inode_operations gfs2_symlink_iops = {
fs/gfs2/inode.h +2 -0
@@ -113,6 +113,8 @@ extern const struct inode_operations gfs
 extern const struct file_operations gfs2_file_fops_nolock;
 extern const struct file_operations gfs2_dir_fops_nolock;
 
+extern int gfs2_fsgetxattr(struct dentry *dentry, struct kfsxattr *fa);
+extern int gfs2_fssetxattr(struct dentry *dentry, struct kfsxattr *fa);
 extern void gfs2_set_inode_flags(struct inode *inode);
  
 #ifdef CONFIG_GFS2_FS_LOCKING_DLM
fs/inode.c +0 -87

96 lines not on this page · raw message

fs/ioctl.c +280 -0

310 lines not on this page · raw message

fs/orangefs/file.c +0 -79

91 lines not on this page · raw message

fs/orangefs/inode.c +50 -0

68 lines not on this page · raw message

fs/overlayfs/dir.c +2 -0
@@ -1300,4 +1300,6 @@ const struct inode_operations ovl_dir_in
 	.listxattr	= ovl_listxattr,
 	.get_acl	= ovl_get_acl,
 	.update_time	= ovl_update_time,
+	.fsgetxattr	= ovl_fsgetxattr,
+	.fssetxattr	= ovl_fssetxattr,
 };
fs/overlayfs/file.c +29 -152

211 lines not on this page · raw message

fs/overlayfs/inode.c +2 -0
@@ -497,6 +497,8 @@ static const struct inode_operations ovl
 	.get_acl	= ovl_get_acl,
 	.update_time	= ovl_update_time,
 	.fiemap		= ovl_fiemap,
+	.fsgetxattr	= ovl_fsgetxattr,
+	.fssetxattr	= ovl_fssetxattr,
 };
 
 static const struct inode_operations ovl_symlink_inode_operations = {
fs/overlayfs/overlayfs.h +2 -2
@@ -505,8 +505,8 @@ struct dentry *ovl_create_temp(struct de
 extern const struct file_operations ovl_file_operations;
 int __init ovl_aio_request_cache_init(void);
 void ovl_aio_request_cache_destroy(void);
-long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
-long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
+int ovl_fsgetxattr(struct dentry *dentry, struct kfsxattr *fa);
+int ovl_fssetxattr(struct dentry *dentry, struct kfsxattr *fa);
 
 /* copy_up.c */
 int ovl_copy_up(struct dentry *dentry);
fs/overlayfs/readdir.c +0 -4
@@ -971,10 +971,6 @@ const struct file_operations ovl_dir_ope
 	.llseek		= ovl_dir_llseek,
 	.fsync		= ovl_dir_fsync,
 	.release	= ovl_dir_release,
-	.unlocked_ioctl	= ovl_ioctl,
-#ifdef CONFIG_COMPAT
-	.compat_ioctl	= ovl_compat_ioctl,
-#endif
 };
 
 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
fs/xfs/libxfs/xfs_fs.h +0 -2
@@ -781,8 +781,6 @@ struct xfs_scrub_metadata {
 #define XFS_IOC_ALLOCSP		_IOW ('X', 10, struct xfs_flock64)
 #define XFS_IOC_FREESP		_IOW ('X', 11, struct xfs_flock64)
 #define XFS_IOC_DIOINFO		_IOR ('X', 30, struct dioattr)
-#define XFS_IOC_FSGETXATTR	FS_IOC_FSGETXATTR
-#define XFS_IOC_FSSETXATTR	FS_IOC_FSSETXATTR
 #define XFS_IOC_ALLOCSP64	_IOW ('X', 36, struct xfs_flock64)
 #define XFS_IOC_FREESP64	_IOW ('X', 37, struct xfs_flock64)
 #define XFS_IOC_GETBMAP		_IOWR('X', 38, struct getbmap)
fs/xfs/xfs_ioctl.c +50 -176

348 lines not on this page · raw message

fs/xfs/xfs_ioctl.h +10 -0
@@ -47,6 +47,16 @@ xfs_handle_to_dentry(
 	void __user		*uhandle,
 	u32			hlen);
 
+extern int
+xfs_fsgetxattr(
+	struct dentry		*dentry,
+	struct kfsxattr		*fa);
+
+extern int
+xfs_fssetxattr(
+	struct dentry		*dentry,
+	struct kfsxattr		*fa);
+
 extern long
 xfs_file_ioctl(
 	struct file		*filp,
fs/xfs/xfs_ioctl32.c +0 -2
@@ -483,8 +483,6 @@ xfs_file_compat_ioctl(
 	}
 #endif
 	/* long changes size, but xfs only copiese out 32 bits */
-	case XFS_IOC_GETXFLAGS_32:
-	case XFS_IOC_SETXFLAGS_32:
 	case XFS_IOC_GETVERSION_32:
 		cmd = _NATIVE_IOC(cmd, long);
 		return xfs_file_ioctl(filp, cmd, p);
fs/xfs/xfs_ioctl32.h +0 -2
@@ -17,8 +17,6 @@
  */
 
 /* stock kernel-level ioctls we support */
-#define XFS_IOC_GETXFLAGS_32	FS_IOC32_GETFLAGS
-#define XFS_IOC_SETXFLAGS_32	FS_IOC32_SETFLAGS
 #define XFS_IOC_GETVERSION_32	FS_IOC32_GETVERSION
 
 /*
fs/xfs/xfs_iops.c +7 -0
@@ -21,6 +21,7 @@
 #include "xfs_dir2.h"
 #include "xfs_iomap.h"
 #include "xfs_error.h"
+#include "xfs_ioctl.h"
 
 #include <linux/posix_acl.h>
 #include <linux/security.h>
@@ -1164,6 +1165,8 @@ static const struct inode_operations xfs
 	.listxattr		= xfs_vn_listxattr,
 	.fiemap			= xfs_vn_fiemap,
 	.update_time		= xfs_vn_update_time,
+	.fsgetxattr		= xfs_fsgetxattr,
+	.fssetxattr		= xfs_fssetxattr,
 };
 
 static const struct inode_operations xfs_dir_inode_operations = {
@@ -1189,6 +1192,8 @@ static const struct inode_operations xfs
 	.listxattr		= xfs_vn_listxattr,
 	.update_time		= xfs_vn_update_time,
 	.tmpfile		= xfs_vn_tmpfile,
+	.fsgetxattr		= xfs_fsgetxattr,
+	.fssetxattr		= xfs_fssetxattr,
 };
 
 static const struct inode_operations xfs_dir_ci_inode_operations = {
@@ -1214,6 +1219,8 @@ static const struct inode_operations xfs
 	.listxattr		= xfs_vn_listxattr,
 	.update_time		= xfs_vn_update_time,
 	.tmpfile		= xfs_vn_tmpfile,
+	.fsgetxattr		= xfs_fsgetxattr,
+	.fssetxattr		= xfs_fssetxattr,
 };
 
 static const struct inode_operations xfs_symlink_inode_operations = {
include/linux/fs.h +3 -12
@@ -68,6 +68,7 @@ struct fsverity_info;
 struct fsverity_operations;
 struct fs_context;
 struct fs_parameter_spec;
+struct kfsxattr;
 
 extern void __init inode_init(void);
 extern void __init inode_init_early(void);
@@ -1873,6 +1874,8 @@ struct inode_operations {
 			   umode_t create_mode);
 	int (*tmpfile) (struct inode *, struct dentry *, umode_t);
 	int (*set_acl)(struct inode *, struct posix_acl *, int);
+	int (*fssetxattr)(struct dentry *, struct kfsxattr *);
+	int (*fsgetxattr)(struct dentry *, struct kfsxattr *);
 } ____cacheline_aligned;
 
 static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio,
@@ -3437,18 +3440,6 @@ extern int vfs_fadvise(struct file *file
 extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
 			   int advice);
 
-int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
-			     unsigned int flags);
-
-int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa,
-			     struct fsxattr *fa);
-
-static inline void simple_fill_fsxattr(struct fsxattr *fa, __u32 xflags)
-{
-	memset(fa, 0, sizeof(*fa));
-	fa->fsx_xflags = xflags;
-}
-
 /*
  * Flush file data before changing attributes.  Caller must hold any locks
  * required to prevent further writes to this file until we're done setting
include/linux/ioctl_helpers.h new file +46 -0

46 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Miklos Szeredi

Dmitry found an issue with overlayfs's FS_IOC_SETFLAGS/FS_IOC_FSSETXATTR implementation:

https://lore.kernel.org/linux-unionfs/CACT4Y+bUfavwMVv2SEMve5pabE_AwsDO0YsRBGZtYqX59a77vA@mail.gmail.com/

I think the only proper soltuion is to move these into inode operations, which should be a good cleanup as well.

Quoted 7 lines

This is a first cut, the FS_IOC_SETFLAGS conversion is not complete, and only lightly tested on ext4 and xfs.

There are minor changes in behavior, like the exact errno value in case of multiple error conditions.

34 files changed, 668 insertions(+), 1170 deletions(-)

Hi Miklos,
this looks like a good reduction in code duplication (-500 LOC is nice).

One issues I have with this patch is that it spreads the use of "fsxattr" asthe name for these inode flags further into the VFS. That was inherited from XFS because of the ioctl name, but is very confusing with "real" extended attributes, also using get/setxattr names but totally differently.

It would be better to use function/variable names with "xflags" and "iflags" that are already used in several filesystems to separate this from xattrs.

It wouldn't be terrible to also rename the ioctl to FS_IOC_FSSETXFLAGS and keep a #define for FS_IOC_FSSETXATTR for compatibility, but that is less critical if that is now only used in one place in the code.

Some more comments inline...

Quoted 13 lines
fs/ioctl.c +10 -0
+/*
+ * Generic function to check FS_IOC_FSSETXATTR/FS_IOC_SETFLAGS values and reject
+ * any invalid configurations.
+ *
+ * Note: must be called with inode lock held.
+ */
+static int fssetxattr_prepare(struct inode *inode,
+			      const struct kfsxattr *old_fa,
+			      struct kfsxattr *fa)
 
+{

fs/ioctl.c static int ioctl_file_dedupe_range(struc · line 775

Quoted 10 lines
fs/ioctl.c +10 -0
+       /*
+        * The IMMUTABLE and APPEND_ONLY flags can only be changed by
+        * the relevant capability.
+	*/
+	if ((fa->fsx_flags ^ old_fa->fsx_flags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) &&
+	    !capable(CAP_LINUX_IMMUTABLE))
+		return -EPERM;
+
+	if (fa->flags_valid)
+		return fscrypt_prepare_setflags(inode, old_fa->fsx_flags, fa->fsx_flags);

This doesn't seem right? It means if iflags are set, the rest of the checks are skipped *even if* there are no problems with the fscrypt flags? That bypasses the DAX and PROJINHERIT checks later in this function, but it is also possible to set/clear those flags via FS_IOC_SETFLAGS, and is not very obvious for the code flow. I'd think this should be something more like:

	if (IS_ENCRYPTED(inode) && fa->flags_valid) {
		rc = fscrypt_prepare_setflags(...);
		if (rc)
			return rc;
	}

and continue on to the rest of the checks, and maybe skip the xflags-specific checks (EXTSIZE, EXTSZINHERIT, COWEXTSIZE) if xflags_valid is not set, though they would just be no-ops in that case since the iflags interface will not set those flags.

Alternately, move the DAX and PROJINHERIT checks above "flags_valid", but add a comment that the remaining checks are only for xflags-specific values.

Quoted 41 lines
fs/ioctl.c +41 -0
+	/*
+	 * Project Quota ID state is only allowed to change from within the init
+	 * namespace. Enforce that restriction only if we are trying to change
+	 * the quota ID state. Everything else is allowed in user namespaces.
+	 */
+	if (current_user_ns() != &init_user_ns) {
+		if (old_fa->fsx_projid != fa->fsx_projid)
+			return -EINVAL;
+		if ((old_fa->fsx_xflags ^ fa->fsx_xflags) &
+				FS_XFLAG_PROJINHERIT)
+			return -EINVAL;
+	}
+
+	/* Check extent size hints. */
+	if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(inode->i_mode))
+		return -EINVAL;
+
+	if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
+			!S_ISDIR(inode->i_mode))
+		return -EINVAL;
+
+	if ((fa->fsx_xflags & FS_XFLAG_COWEXTSIZE) &&
+	    !S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
+		return -EINVAL;
+
+	/*
+	 * It is only valid to set the DAX flag on regular files and
+	 * directories on filesystems.
+	 */
+	if ((fa->fsx_xflags & FS_XFLAG_DAX) &&
+	    !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
+		return -EINVAL;
+
+	/* Extent size hints of zero turn off the flags. */
+	if (fa->fsx_extsize == 0)
+		fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
+	if (fa->fsx_cowextsize == 0)
+		fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
+
+	return 0;
+}

[snip]

Quoted 18 lines
fs/ioctl.c +18 -0
+int vfs_fssetxattr(struct dentry *dentry, struct kfsxattr *fa)
+{
+	struct inode *inode = d_inode(dentry);
+	struct kfsxattr old_fa;
+	int err;
+
+	if (d_is_special(dentry))
+		return -ENOTTY;
+
+	if (!inode->i_op->fssetxattr)
+		return -ENOIOCTLCMD;
+
+	if (!inode_owner_or_capable(inode))
+		return -EPERM;
+
+	inode_lock(inode);
+	err = vfs_fsgetxattr(dentry, &old_fa);
+	if (!err) {

fs/ioctl.c static int ioctl_file_dedupe_range(struc · line 846

Quoted 10 lines
fs/ioctl.c +10 -0
+		/* initialize missing bits from old_fa */
+		if (fa->flags_valid) {
+			fa->fsx_xflags |= old_fa.fsx_xflags & ~FS_XFLAG_COMMON;
+			fa->fsx_extsize = old_fa.fsx_extsize;
+			fa->fsx_nextents = old_fa.fsx_nextents;
+			fa->fsx_projid = old_fa.fsx_projid;
+			fa->fsx_cowextsize = old_fa.fsx_cowextsize;
+		} else {
+			fa->fsx_flags |= old_fa.fsx_flags & ~FS_COMMON_FL;
+		}

This extra call to vfs_fsgetxattr() is adding pure overhead for the case of FS_IOC_GETFLAGS and is totally unnecessary. If iflags_valid is set, then none of these other fields should be accessed in the ->fssetxattr() method, and they can check for iflags_valid vs. xflags_valid themselves to see which ioctl is being called and only access fields which are valid.

fs/ioctl.c static int ioctl_file_dedupe_range(struc · line 855

Quoted 9 lines
fs/ioctl.c +9 -0
+		err = fssetxattr_prepare(inode, &old_fa, fa);
+		if (!err)
+			err = inode->i_op->fssetxattr(dentry, fa);
+	}
+	inode_unlock(inode);
+
+	return err;
+}
+EXPORT_SYMBOL(vfs_fssetxattr);

Cheers, Andreas

author maintainer
#2 lore raw
Quoted 23 lines · Andreas Dilger
Quoted 9 lines · Miklos Szeredi

Dmitry found an issue with overlayfs's FS_IOC_SETFLAGS/FS_IOC_FSSETXATTR implementation:

https://lore.kernel.org/linux-unionfs/CACT4Y+bUfavwMVv2SEMve5pabE_AwsDO0YsRBGZtYqX59a77vA@mail.gmail.com/

I think the only proper soltuion is to move these into inode operations, which should be a good cleanup as well.

Quoted 7 lines

This is a first cut, the FS_IOC_SETFLAGS conversion is not complete, and only lightly tested on ext4 and xfs.

There are minor changes in behavior, like the exact errno value in case of multiple error conditions.

34 files changed, 668 insertions(+), 1170 deletions(-)

Hi Miklos,
this looks like a good reduction in code duplication (-500 LOC is nice).

Quoted 7 lines

One issues I have with this patch is that it spreads the use of "fsxattr" asthe name for these inode flags further into the VFS. That was inherited from XFS because of the ioctl name, but is very confusing with "real" extended attributes, also using get/setxattr names but totally differently.

It would be better to use function/variable names with "xflags" and "iflags" that are already used in several filesystems to separate this from xattrs.

Makes sense.

Quoted 44 lines

It wouldn't be terrible to also rename the ioctl to FS_IOC_FSSETXFLAGS and keep a #define for FS_IOC_FSSETXATTR for compatibility, but that is less critical if that is now only used in one place in the code.

Some more comments inline...

Quoted 13 lines
fs/ioctl.c +10 -0
+/*
+ * Generic function to check FS_IOC_FSSETXATTR/FS_IOC_SETFLAGS values and reject
+ * any invalid configurations.
+ *
+ * Note: must be called with inode lock held.
+ */
+static int fssetxattr_prepare(struct inode *inode,
+                           const struct kfsxattr *old_fa,
+                           struct kfsxattr *fa)
 
+{

fs/ioctl.c static int ioctl_file_dedupe_range(struc · line 775

Quoted 10 lines
fs/ioctl.c +10 -0
+       /*
+        * The IMMUTABLE and APPEND_ONLY flags can only be changed by
+        * the relevant capability.
+     */
+     if ((fa->fsx_flags ^ old_fa->fsx_flags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) &&
+         !capable(CAP_LINUX_IMMUTABLE))
+             return -EPERM;
+
+     if (fa->flags_valid)
+             return fscrypt_prepare_setflags(inode, old_fa->fsx_flags, fa->fsx_flags);

This doesn't seem right? It means if iflags are set, the rest of the checks are skipped *even if* there are no problems with the fscrypt flags? That bypasses the DAX and PROJINHERIT checks later in this function, but it is also possible to set/clear those flags via FS_IOC_SETFLAGS, and is not very obvious for the code flow. I'd think this should be something more like:

        if (IS_ENCRYPTED(inode) && fa->flags_valid) {
                rc = fscrypt_prepare_setflags(...);
                if (rc)
                        return rc;
        }
Quoted 7 lines

and continue on to the rest of the checks, and maybe skip the xflags-specific checks (EXTSIZE, EXTSZINHERIT, COWEXTSIZE) if xflags_valid is not set, though they would just be no-ops in that case since the iflags interface will not set those flags.

Alternately, move the DAX and PROJINHERIT checks above "flags_valid", but add a comment that the remaining checks are only for xflags-specific values.

Good point. I wasn't actually looking at the code, just converting
the old one to the new, and apparently the DAX and PROJINHERIT checks were missing for the FS_IOC_SETFLAGS case (see vfs_ioc_setflags_prepare).

Quoted 26 lines
Quoted 26 lines
fs/ioctl.c +26 -0
+int vfs_fssetxattr(struct dentry *dentry, struct kfsxattr *fa)
+{
+     struct inode *inode = d_inode(dentry);
+     struct kfsxattr old_fa;
+     int err;
+
+     if (d_is_special(dentry))
+             return -ENOTTY;
+
+     if (!inode->i_op->fssetxattr)
+             return -ENOIOCTLCMD;
+
+     if (!inode_owner_or_capable(inode))
+             return -EPERM;
+
+     inode_lock(inode);
+     err = vfs_fsgetxattr(dentry, &old_fa);
+     if (!err) {
+             /* initialize missing bits from old_fa */
+             if (fa->flags_valid) {
+                     fa->fsx_xflags |= old_fa.fsx_xflags & ~FS_XFLAG_COMMON;
+                     fa->fsx_extsize = old_fa.fsx_extsize;
+                     fa->fsx_nextents = old_fa.fsx_nextents;
+                     fa->fsx_projid = old_fa.fsx_projid;
+                     fa->fsx_cowextsize = old_fa.fsx_cowextsize;
+             } else {
Quoted 8 lines

fs/ioctl.c

Quoted 2 lines
fs/ioctl.c +2 -0
+                     fa->fsx_flags |= old_fa.fsx_flags & ~FS_COMMON_FL;
+             }

This extra call to vfs_fsgetxattr() is adding pure overhead for the case of FS_IOC_GETFLAGS and is totally unnecessary. If iflags_valid is set, then none of these other fields should be accessed in the ->fssetxattr() method, and they can check for iflags_valid vs. xflags_valid themselves to see which ioctl is being called and only access fields which are valid.

The extra call is needed for fssetxattr_prepare() for the the checks
anyway. Filling in the other fields is a bonus that makes fs code
simpler in some cases (e.g. xfs which mainly just looks at xflags).

Also ->fsgetxattr() is cheap for all filesystems (and this syscall shouldn't be performance sensitive anyway) so I don't see why we'd need to optimize this at all.

Thanks,
Miklos

#3 lore raw
In reply to Miklos Szeredi

Looks pretty neat from a quick look, but you probably want to split it into a patch or two adding the infrastructure and then one patch per file system to ease review and bisection.

author maintainer
#4 lore raw
Quoted 4 lines · Christoph Hellwig

Looks pretty neat from a quick look, but you probably want to split it into a patch or two adding the infrastructure and then one patch per file system to ease review and bisection.

Definitely. Just wanted to get this out to get early feedback.

Thanks,
Miklos