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

From: Jan Kara

Date: Mon Nov 30 2026 - 08:55:55 EST


On Mon 30-11-26 01:23:52, Sourav Panda wrote:
> 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))

I think you can just add S_ISCHR() check here. If ->fallocate isn't set,
we'll return EOPNOTSUPP a few conditions below. Yes, the error would change
from ENODEV to EOPNOTSUPP but I don't think that really matters and if
we're going to support fallocate() for char devices, then better keep it
consistent with e.g. block devices.

Honza

--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR