[RFC PATCH 3/5] vfs: allow fallocate() on character devices implementing ->fallocate()

From: Sourav Panda

Date: Sun Nov 29 2026 - 20:24:29 EST


vfs_fallocate() returns -ENODEV for anything that is not a regular file
or a block device, before it looks at f_op->fallocate. Let a character
device through when its driver implements ->fallocate(). All other
character devices keep returning -ENODEV, and since no character device
in the tree implements ->fallocate() today, nothing changes for existing
drivers.

The first user is device-dax, in a later patch, where punching a hole
releases device memory. An ioctl could do that too, but
madvise(MADV_REMOVE) is implemented by calling vfs_fallocate() with
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE on the mapped file. Going
through ->fallocate() lets an allocator release part of a device-dax
mapping with the madvise() call it already uses for shmem.

The permission and sanity checks in vfs_fallocate() are unchanged and
still run before the driver is called.

Signed-off-by: Sourav Panda <souravpanda@xxxxxxxxxx>
---
fs/open.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/open.c b/fs/open.c
index bb484b07390ac..0f31caf9e7597 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -321,7 +321,13 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
if (S_ISDIR(inode->i_mode))
return -EISDIR;

- if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
+ /*
+ * Character devices that implement ->fallocate() (device-dax uses it
+ * for discard, see drivers/dax/device.c) are allowed through; all other
+ * character devices keep -ENODEV.
+ */
+ if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode) &&
+ !(S_ISCHR(inode->i_mode) && file->f_op->fallocate))
return -ENODEV;

/* Check for wraparound */
--
2.56.1.839.g22a8290db9-goog