From: Youling Tang <youling.tang@linux.dev>
To: Kent Overstreet <kent.overstreet@linux.dev>
Cc: linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org,
youling.tang@linux.dev, Youling Tang <tangyouling@kylinos.cn>
Subject: [PATCH 2/3] bcachefs: Refactor the handling logic of fallocate mode in bch2_fallocate_dispatch()
Date: Fri, 27 Jun 2025 09:21:03 +0800 [thread overview]
Message-ID: <20250627012104.222703-2-youling.tang@linux.dev> (raw)
In-Reply-To: <20250627012104.222703-1-youling.tang@linux.dev>
From: Youling Tang <tangyouling@kylinos.cn>
The mode parameter of the fallocate system call is divided into exclusive
mode (FALLOC_FL_MODE_MASK) and optional flag (FALLOC_FL_KEEP_SIZE).
Use FALLOC_FL_MODE_MASK and FALLOC_FL_ALLOCATE_RANGE makes the code more
readable.
Some of the logic has been handled in vfs_fallocate:
- FALLOC_FL_PUNCH_HOLE must exist simultaneously with FALLOC_FL_KEEP_SIZE.
- FALLOC_FL_COLLAPSE_RANGE and FALLOC_FL_INSERT_RANGE cannot exist
simultaneously with FALLOC_FL_KEEP_SIZE.
Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
fs/bcachefs/fs-io.c | 27 ++++++++++++++++++++++-----
1 file changed, 22 insertions(+), 5 deletions(-)
diff --git a/fs/bcachefs/fs-io.c b/fs/bcachefs/fs-io.c
index 74841b1dc8ca..69d3ddd4c6a1 100644
--- a/fs/bcachefs/fs-io.c
+++ b/fs/bcachefs/fs-io.c
@@ -814,6 +814,12 @@ static noinline long bchfs_fallocate(struct bch_inode_info *inode, int mode,
return ret ?: ret2;
}
+#define BCH2_FALLOC_FL_SUPPORTED \
+ (FALLOC_FL_KEEP_SIZE | \
+ FALLOC_FL_ALLOCATE_RANGE | FALLOC_FL_PUNCH_HOLE | \
+ FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
+ FALLOC_FL_INSERT_RANGE)
+
long bch2_fallocate_dispatch(struct file *file, int mode,
loff_t offset, loff_t len)
{
@@ -824,6 +830,9 @@ long bch2_fallocate_dispatch(struct file *file, int mode,
if (!enumerated_ref_tryget(&c->writes, BCH_WRITE_REF_fallocate))
return -EROFS;
+ if (mode & ~BCH2_FALLOC_FL_SUPPORTED)
+ return bch2_err_class(bch_err_throw(c, unsupported_fallocate_mode));
+
inode_lock(&inode->v);
inode_dio_wait(&inode->v);
bch2_pagecache_block_get(inode);
@@ -832,16 +841,24 @@ long bch2_fallocate_dispatch(struct file *file, int mode,
if (ret)
goto err;
- if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE)))
+ switch (mode & FALLOC_FL_MODE_MASK) {
+ case FALLOC_FL_ALLOCATE_RANGE:
+ case FALLOC_FL_ZERO_RANGE:
ret = bchfs_fallocate(inode, mode, offset, len);
- else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE))
+ break;
+ case FALLOC_FL_PUNCH_HOLE:
ret = bchfs_fpunch(inode, offset, len);
- else if (mode == FALLOC_FL_INSERT_RANGE)
+ break;
+ case FALLOC_FL_INSERT_RANGE:
ret = bchfs_fcollapse_finsert(inode, offset, len, true);
- else if (mode == FALLOC_FL_COLLAPSE_RANGE)
+ break;
+ case FALLOC_FL_COLLAPSE_RANGE:
ret = bchfs_fcollapse_finsert(inode, offset, len, false);
- else
+ break;
+ default:
ret = bch_err_throw(c, unsupported_fallocate_mode);
+ break;
+ }
err:
bch2_pagecache_block_put(inode);
inode_unlock(&inode->v);
--
2.34.1
next prev parent reply other threads:[~2025-06-27 1:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-27 1:21 [PATCH 1/3] bcachefs: Removes NULL pointer checks for filemap_lock_folio() return values Youling Tang
2025-06-27 1:21 ` Youling Tang [this message]
2025-06-27 1:21 ` [PATCH 3/3] bcachefs: Spilt bchfs_fallocate() into two functions Youling Tang
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=20250627012104.222703-2-youling.tang@linux.dev \
--to=youling.tang@linux.dev \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcachefs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tangyouling@kylinos.cn \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.