From: Ojaswin Mujoo <ojaswin@linux.ibm.com>
To: linux-ext4@vger.kernel.org, "Theodore Ts'o" <tytso@mit.edu>
Cc: Jan Kara <jack@suse.cz>, Baokun Li <libaokun1@huawei.com>,
Ritesh Harjani <ritesh.list@gmail.com>,
Zhang Yi <yi.zhang@huawei.com>,
linux-kernel@vger.kernel.org,
"Darrick J . Wong" <djwong@kernel.org>,
linux-fsdevel@vger.kernel.org
Subject: [RFC v4 3/7] ext4: support for extsize hint using FS_IOC_FS(GET/SET)XATTR
Date: Mon, 21 Jul 2025 02:27:29 +0530 [thread overview]
Message-ID: <24053b9376e369be0ffffea0d91eb22b7634aed4.1753044253.git.ojaswin@linux.ibm.com> (raw)
In-Reply-To: <cover.1753044253.git.ojaswin@linux.ibm.com>
This patch adds support for getting and setting extsize hint using
FS_IOC_GETXATTR and FS_IOC_SETXATTR interface. The extsize is stored
in xattr of type EXT4_XATTR_INDEX_SYSTEM.
Restrictions on setting extsize:
1. extsize can't be set on files with data
2. extsize can't be set on non regular files
3. extsize hint can't be used with bigalloc (yet)
4. extsize (in blocks) should be power-of-2 for simplicity.
5. extsize must be a multiple of block size
The ioctl behavior has been kept as close to the XFS equivalent
as possible.
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
fs/ext4/ext4.h | 6 +++
fs/ext4/inode.c | 89 +++++++++++++++++++++++++++++++++++
fs/ext4/ioctl.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++
fs/ext4/super.c | 1 +
4 files changed, 218 insertions(+)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 7b353d1af580..d00870cb15f2 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1199,6 +1199,8 @@ struct ext4_inode_info {
__u32 i_csum_seed;
kprojid_t i_projid;
+ /* The extentsize hint for the inode in blocks */
+ ext4_grpblk_t i_extsize;
};
/*
@@ -3081,6 +3083,10 @@ extern void ext4_da_update_reserve_space(struct inode *inode,
int used, int quota_claim);
extern int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk,
ext4_fsblk_t pblk, ext4_lblk_t len);
+int ext4_inode_xattr_get_extsize(struct inode *inode);
+int ext4_inode_xattr_set_extsize(struct inode *inode, ext4_grpblk_t extsize);
+ext4_grpblk_t ext4_inode_get_extsize(struct ext4_inode_info *ei);
+void ext4_inode_set_extsize(struct ext4_inode_info *ei, ext4_grpblk_t extsize);
static inline bool is_special_ino(struct super_block *sb, unsigned long ino)
{
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 8bdf2029ebc7..664218228fd5 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5432,6 +5432,20 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
}
}
+ ret = ext4_inode_xattr_get_extsize(&ei->vfs_inode);
+ if (ret >= 0) {
+ ei->i_extsize = ret;
+ } else if (ret == -ENODATA) {
+ /* extsize is not set */
+ ei->i_extsize = 0;
+ } else {
+ ext4_error_inode(
+ inode, function, line, 0,
+ "iget: error while retrieving extsize from xattr: %ld", ret);
+ ret = -EFSCORRUPTED;
+ goto bad_inode;
+ }
+
EXT4_INODE_GET_CTIME(inode, raw_inode);
EXT4_INODE_GET_ATIME(inode, raw_inode);
EXT4_INODE_GET_MTIME(inode, raw_inode);
@@ -6779,3 +6793,78 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
sb_end_pagefault(inode->i_sb);
return ret;
}
+
+/*
+ * Returns positive extsize if set, 0 if not set else error
+ */
+ext4_grpblk_t ext4_inode_xattr_get_extsize(struct inode *inode)
+{
+ char *buf;
+ int size, ret = 0;
+ ext4_grpblk_t extsize = 0;
+
+ size = ext4_xattr_get(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", NULL, 0);
+
+ if (size == -ENODATA || size == 0) {
+ return 0;
+ } else if (size < 0) {
+ ret = size;
+ goto exit;
+ }
+
+ buf = kmalloc(size + 1, GFP_KERNEL);
+ if (!buf) {
+ ret = -ENOMEM;
+ goto exit;
+ }
+
+ size = ext4_xattr_get(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize", buf,
+ size);
+ if (size == -ENODATA)
+ /* No extsize is set */
+ extsize = 0;
+ else if (size < 0)
+ ret = size;
+ else {
+ buf[size] = '\0';
+ ret = kstrtoint(buf, 10, &extsize);
+ }
+
+ kfree(buf);
+exit:
+ if (ret)
+ return ret;
+ return extsize;
+}
+
+int ext4_inode_xattr_set_extsize(struct inode *inode, ext4_grpblk_t extsize)
+{
+ int err = 0;
+ /* max value of extsize should fit within 11 chars */
+ char extsize_str[11];
+
+ err = snprintf(extsize_str, 10, "%u", extsize);
+ if (err < 0)
+ return err;
+
+ /* Try to replace the xattr if it exists, else try to create it */
+ err = ext4_xattr_set(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize",
+ extsize_str, strlen(extsize_str), XATTR_REPLACE);
+
+ if (err == -ENODATA)
+ err = ext4_xattr_set(inode, EXT4_XATTR_INDEX_SYSTEM, "extsize",
+ extsize_str, strlen(extsize_str),
+ XATTR_CREATE);
+
+ return err;
+}
+
+ext4_grpblk_t ext4_inode_get_extsize(struct ext4_inode_info *ei)
+{
+ return ei->i_extsize;
+}
+
+void ext4_inode_set_extsize(struct ext4_inode_info *ei, ext4_grpblk_t extsize)
+{
+ ei->i_extsize = extsize;
+}
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 5668a17458ae..64a394869317 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -708,6 +708,93 @@ static int ext4_ioctl_setflags(struct inode *inode,
return err;
}
+static u32 ext4_ioctl_getextsize(struct inode *inode)
+{
+ ext4_grpblk_t extsize;
+
+ extsize = ext4_inode_get_extsize(EXT4_I(inode));
+
+ return (u32) extsize << inode->i_blkbits;
+}
+
+
+static int ext4_ioctl_setextsize(struct inode *inode, u32 extsize, u32 xflags)
+{
+ int err;
+ ext4_grpblk_t extsize_blks = extsize >> inode->i_blkbits;
+ struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
+ int blksize = 1 << inode->i_blkbits;
+ char *msg = NULL;
+
+ if (!S_ISREG(inode->i_mode)) {
+ msg = "Cannot set extsize on non regular file";
+ err = -EOPNOTSUPP;
+ goto error;
+ }
+
+ /*
+ * We are okay with a non-zero i_size as long as there is no data.
+ */
+ if (ext4_has_inline_data(inode) ||
+ READ_ONCE(EXT4_I(inode)->i_disksize) ||
+ EXT4_I(inode)->i_reserved_data_blocks) {
+ msg = "Cannot set extsize on file with data";
+ err = -EINVAL;
+ goto error;
+ }
+
+ if (extsize % blksize) {
+ msg = "extsize must be multiple of blocksize";
+ err = -EINVAL;
+ goto error;
+ }
+
+ if (sbi->s_cluster_ratio > 1) {
+ msg = "Can't use extsize hint with bigalloc";
+ err = -EINVAL;
+ goto error;
+ }
+
+ if ((xflags & FS_XFLAG_EXTSIZE) && extsize == 0) {
+ msg = "fsx_extsize can't be 0 if FS_XFLAG_EXTSIZE is passed";
+ err = -EINVAL;
+ goto error;
+ }
+
+ if (extsize_blks > sbi->s_blocks_per_group) {
+ msg = "extsize cannot exceed number of bytes in block group";
+ err = -EINVAL;
+ goto error;
+ }
+
+ if (extsize && !is_power_of_2(extsize_blks)) {
+ msg = "extsize must be either power-of-2 in fs blocks or 0";
+ err = -EINVAL;
+ goto error;
+ }
+
+ if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
+ msg = "extsize can't be set on non-extent based files";
+ err = -EINVAL;
+ goto error;
+ }
+
+ /* update the extsize in inode xattr */
+ err = ext4_inode_xattr_set_extsize(inode, extsize_blks);
+ if (err < 0)
+ return err;
+
+ /* Update the new extsize in the in-core inode */
+ ext4_inode_set_extsize(EXT4_I(inode), extsize_blks);
+ return 0;
+
+error:
+ if (msg)
+ ext4_warning_inode(inode, "%s\n", msg);
+
+ return err;
+}
+
#ifdef CONFIG_QUOTA
static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
{
@@ -985,6 +1072,7 @@ int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa)
struct inode *inode = d_inode(dentry);
struct ext4_inode_info *ei = EXT4_I(inode);
u32 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
+ u32 extsize = 0;
if (S_ISREG(inode->i_mode))
flags &= ~FS_PROJINHERIT_FL;
@@ -993,6 +1081,13 @@ int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa)
if (ext4_has_feature_project(inode->i_sb))
fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid);
+ extsize = ext4_ioctl_getextsize(inode);
+ /* Flag is only set if extsize is non zero */
+ if (extsize > 0) {
+ fa->fsx_extsize = extsize;
+ fa->fsx_xflags |= FS_XFLAG_EXTSIZE;
+ }
+
return 0;
}
@@ -1022,6 +1117,33 @@ int ext4_fileattr_set(struct mnt_idmap *idmap,
if (err)
goto out;
err = ext4_ioctl_setproject(inode, fa->fsx_projid);
+ if (err)
+ goto out;
+
+ if (fa->fsx_xflags & FS_XFLAG_EXTSIZE) {
+ err = ext4_ioctl_setextsize(inode, fa->fsx_extsize,
+ fa->fsx_xflags);
+ if (err)
+ goto out;
+ } else if (fa->fsx_extsize == 0) {
+ /*
+ * Even when user explicitly passes extsize=0 the flag is cleared in
+ * fileattr_set_prepare().
+ */
+ if (ext4_inode_get_extsize(EXT4_I(inode)) != 0) {
+ err = ext4_ioctl_setextsize(inode, fa->fsx_extsize,
+ fa->fsx_xflags);
+ if (err)
+ goto out;
+ }
+
+ } else {
+ /* Unexpected usage, reset extsize to 0 */
+ err = ext4_ioctl_setextsize(inode, 0, fa->fsx_xflags);
+ if (err)
+ goto out;
+ fa->fsx_xflags = 0;
+ }
out:
return err;
}
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index c7d39da7e733..2237cb2240f8 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1409,6 +1409,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
spin_lock_init(&ei->i_completed_io_lock);
ei->i_sync_tid = 0;
ei->i_datasync_tid = 0;
+ ei->i_extsize = 0;
INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
ext4_fc_init_inode(&ei->vfs_inode);
spin_lock_init(&ei->i_fc_lock);
--
2.49.0
next prev parent reply other threads:[~2025-07-20 20:58 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-20 20:57 [RFC v4 0/7] ext4: Add extsize support Ojaswin Mujoo
2025-07-20 20:57 ` [RFC v4 1/7] ext4: add aligned allocation hint in mballoc Ojaswin Mujoo
2025-07-20 20:57 ` [RFC v4 2/7] ext4: allow inode preallocation for aligned alloc Ojaswin Mujoo
2025-07-20 20:57 ` Ojaswin Mujoo [this message]
2025-07-20 20:57 ` [RFC v4 4/7] ext4: pass lblk and len explicitly to ext4_split_extent*() Ojaswin Mujoo
2025-07-20 20:57 ` [RFC v4 5/7] ext4: add extsize hint support Ojaswin Mujoo
2025-07-20 20:57 ` [RFC v4 6/7] ext4: make extsize work with EOF allocations Ojaswin Mujoo
2025-07-20 20:57 ` [RFC v4 7/7] ext4: add ext4_map_blocks_extsize() wrapper to handle overwrites Ojaswin Mujoo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=24053b9376e369be0ffffea0d91eb22b7634aed4.1753044253.git.ojaswin@linux.ibm.com \
--to=ojaswin@linux.ibm.com \
--cc=djwong@kernel.org \
--cc=jack@suse.cz \
--cc=libaokun1@huawei.com \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ritesh.list@gmail.com \
--cc=tytso@mit.edu \
--cc=yi.zhang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®