Skip to content

Commit 817704a

Browse files
committed
zfs_file: rename zfs_file_fallocate to zfs_file_deallocate
We only use it on a specific way: to punch a hole in (make sparse) a region of a file, in order to implement TRIM-like behaviour. So, call the op "deallocate", and move the Linux-style mode flags down into the Linux implementation, since they're an implementation detail. FreeBSD gets a no-op stub (for the moment). Sponsored-by: https://despairlabs.com/sponsor/ Signed-off-by: Rob Norris <[email protected]>
1 parent 17dd66d commit 817704a

File tree

6 files changed

+42
-37
lines changed

6 files changed

+42
-37
lines changed

include/sys/zfs_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int zfs_file_pread(zfs_file_t *fp, void *buf, size_t len, loff_t off,
5353
int zfs_file_seek(zfs_file_t *fp, loff_t *offp, int whence);
5454
int zfs_file_getattr(zfs_file_t *fp, zfs_file_attr_t *zfattr);
5555
int zfs_file_fsync(zfs_file_t *fp, int flags);
56-
int zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len);
56+
int zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len);
5757
loff_t zfs_file_off(zfs_file_t *fp);
5858
int zfs_file_unlink(const char *);
5959

lib/libzpool/kernel.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,24 +1367,26 @@ zfs_file_fsync(zfs_file_t *fp, int flags)
13671367
}
13681368

13691369
/*
1370-
* fallocate - allocate or free space on disk
1370+
* deallocate - zero and/or deallocate file storage
13711371
*
13721372
* fp - file pointer
1373-
* mode (non-standard options for hole punching etc)
1374-
* offset - offset to start allocating or freeing from
1375-
* len - length to free / allocate
1376-
*
1377-
* OPTIONAL
1373+
* offset - offset to start zeroing or deallocating
1374+
* len - length to zero or deallocate
13781375
*/
13791376
int
1380-
zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
1377+
zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
13811378
{
1382-
#ifdef __linux__
1383-
return (fallocate(fp->f_fd, mode, offset, len));
1379+
int rc;
1380+
#if defined(__linux__)
1381+
rc = fallocate(fp->f_fd,
1382+
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, len);
13841383
#else
1385-
(void) fp, (void) mode, (void) offset, (void) len;
1386-
return (EOPNOTSUPP);
1384+
(void) fp, (void) offset, (void) len;
1385+
rc = EOPNOTSUPP;
13871386
#endif
1387+
if (rc)
1388+
return (SET_ERROR(rc));
1389+
return (0);
13881390
}
13891391

13901392
/*

module/os/freebsd/zfs/vdev_file.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,9 @@ vdev_file_io_start(zio_t *zio)
260260
zio_execute(zio);
261261
return;
262262
} else if (zio->io_type == ZIO_TYPE_TRIM) {
263-
#ifdef notyet
264-
int mode = 0;
265-
266263
ASSERT3U(zio->io_size, !=, 0);
267-
268-
/* XXX FreeBSD has no fallocate routine in file ops */
269-
zio->io_error = zfs_file_fallocate(vf->vf_file,
270-
mode, zio->io_offset, zio->io_size);
271-
#endif
272-
zio->io_error = SET_ERROR(ENOTSUP);
264+
zio->io_error = zfs_file_deallocate(vf->vf_file,
265+
zio->io_offset, zio->io_size);
273266
zio_execute(zio);
274267
return;
275268
}

module/os/freebsd/zfs/zfs_file_os.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ zfs_file_fsync(zfs_file_t *fp, int flags)
285285
return (zfs_vop_fsync(fp->f_vnode));
286286
}
287287

288+
/*
289+
* deallocate - zero and/or deallocate file storage
290+
*
291+
* fp - file pointer
292+
* offset - offset to start zeroing or deallocating
293+
* len - length to zero or deallocate
294+
*/
295+
int
296+
zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
297+
{
298+
(void) fp, (void) offset, (void) len;
299+
return (SET_ERROR(EOPNOTSUPP));
300+
}
301+
288302
zfs_file_t *
289303
zfs_file_get(int fd)
290304
{

module/os/linux/zfs/vdev_file.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,9 @@ vdev_file_io_start(zio_t *zio)
274274
zio_execute(zio);
275275
return;
276276
} else if (zio->io_type == ZIO_TYPE_TRIM) {
277-
int mode = 0;
278-
279277
ASSERT3U(zio->io_size, !=, 0);
280-
#ifdef __linux__
281-
mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
282-
#endif
283-
zio->io_error = zfs_file_fallocate(vf->vf_file,
284-
mode, zio->io_offset, zio->io_size);
278+
zio->io_error = zfs_file_deallocate(vf->vf_file,
279+
zio->io_offset, zio->io_size);
285280
zio_execute(zio);
286281
return;
287282
}

module/os/linux/zfs/zfs_file_os.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,14 @@ zfs_file_fsync(zfs_file_t *filp, int flags)
328328
}
329329

330330
/*
331-
* fallocate - allocate or free space on disk
331+
* deallocate - zero and/or deallocate file storage
332332
*
333333
* fp - file pointer
334-
* mode (non-standard options for hole punching etc)
335-
* offset - offset to start allocating or freeing from
336-
* len - length to free / allocate
337-
*
338-
* OPTIONAL
334+
* offset - offset to start zeroing or deallocating
335+
* len - length to zero or deallocate
339336
*/
340337
int
341-
zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
338+
zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
342339
{
343340
/*
344341
* May enter XFS which generates a warning when PF_FSTRANS is set.
@@ -354,12 +351,16 @@ zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
354351
*/
355352
int error = EOPNOTSUPP;
356353
if (fp->f_op->fallocate)
357-
error = fp->f_op->fallocate(fp, mode, offset, len);
354+
error = -fp->f_op->fallocate(fp,
355+
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, len);
358356

359357
if (fstrans)
360358
current->flags |= __SPL_PF_FSTRANS;
361359

362-
return (error);
360+
if (error)
361+
return (SET_ERROR(error));
362+
363+
return (0);
363364
}
364365

365366
/*

0 commit comments

Comments
 (0)