Skip to content

Commit

Permalink
zfs_file: implement zfs_file_deallocate for FreeBSD 14
Browse files Browse the repository at this point in the history
FreeBSD 14 gained a `VOP_DEALLOCATE` VFS operation and a `fspacectl`
syscall to use it. At minimum, these zero the given region, and if the
underlying filesystem supports it, can make the region sparse. We can
use this to get TRIM-like behaviour for file vdevs.

Sponsored-by: https://despairlabs.com/sponsor/
Signed-off-by: Rob Norris <robn@despairlabs.com>
  • Loading branch information
robn committed Sep 1, 2024
1 parent b9bc5db commit 7f0c1a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/libzpool/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,12 @@ zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
#if defined(__linux__)
return (fallocate(fp->f_fd,
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, len));
#elif defined(__FreeBSD__) && (__FreeBSD_version >= 1400028)
struct spacectl_range rqsr = {
.r_offset = offset,
.r_len = len,
};
return (fspacectl(fp->f_fd, SPACECTL_DEALLOC, &rqsr, 0, &rqsr));
#else
(void) fp, (void) offset, (void) len;
return (EOPNOTSUPP);
Expand Down
12 changes: 12 additions & 0 deletions module/os/freebsd/zfs/zfs_file_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,20 @@ zfs_file_fsync(zfs_file_t *fp, int flags)
int
zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
{
#if __FreeBSD_version >= 1400028
struct thread *td;
int rc;

td = curthread;
rc = fo_fspacectl(fp, SPACECTL_DEALLOC, &offset, &len, 0,
td->td_ucred, td);
if (rc)
return (SET_ERROR(rc));
return (0);
#else
(void) fp, (void) offset, (void) len;
return (EOPNOTSUPP);
#endif
}

zfs_file_t *
Expand Down

0 comments on commit 7f0c1a3

Please sign in to comment.