Skip to content

Commit f169449

Browse files
robntonyhutter
authored andcommitted
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/ Reviewed by: Brian Behlendorf <[email protected]> Reviewed-by: Alexander Motin <[email protected]> Reviewed-by: Tino Reichardt <[email protected]> Signed-off-by: Rob Norris <[email protected]> Closes openzfs#16496
1 parent 2e646b5 commit f169449

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
@@ -1364,24 +1364,26 @@ zfs_file_fsync(zfs_file_t *fp, int flags)
13641364
}
13651365

13661366
/*
1367-
* fallocate - allocate or free space on disk
1367+
* deallocate - zero and/or deallocate file storage
13681368
*
13691369
* fp - file pointer
1370-
* mode (non-standard options for hole punching etc)
1371-
* offset - offset to start allocating or freeing from
1372-
* len - length to free / allocate
1373-
*
1374-
* OPTIONAL
1370+
* offset - offset to start zeroing or deallocating
1371+
* len - length to zero or deallocate
13751372
*/
13761373
int
1377-
zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
1374+
zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
13781375
{
1379-
#ifdef __linux__
1380-
return (fallocate(fp->f_fd, mode, offset, len));
1376+
int rc;
1377+
#if defined(__linux__)
1378+
rc = fallocate(fp->f_fd,
1379+
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, len);
13811380
#else
1382-
(void) fp, (void) mode, (void) offset, (void) len;
1383-
return (EOPNOTSUPP);
1381+
(void) fp, (void) offset, (void) len;
1382+
rc = EOPNOTSUPP;
13841383
#endif
1384+
if (rc)
1385+
return (SET_ERROR(rc));
1386+
return (0);
13851387
}
13861388

13871389
/*

module/os/freebsd/zfs/vdev_file.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,9 @@ vdev_file_io_start(zio_t *zio)
267267
zio_execute(zio);
268268
return;
269269
} else if (zio->io_type == ZIO_TYPE_TRIM) {
270-
#ifdef notyet
271-
int mode = 0;
272-
273270
ASSERT3U(zio->io_size, !=, 0);
274-
275-
/* XXX FreeBSD has no fallocate routine in file ops */
276-
zio->io_error = zfs_file_fallocate(vf->vf_file,
277-
mode, zio->io_offset, zio->io_size);
278-
#endif
279-
zio->io_error = SET_ERROR(ENOTSUP);
271+
zio->io_error = zfs_file_deallocate(vf->vf_file,
272+
zio->io_offset, zio->io_size);
280273
zio_execute(zio);
281274
return;
282275
}

module/os/freebsd/zfs/zfs_file_os.c

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

291+
/*
292+
* deallocate - zero and/or deallocate file storage
293+
*
294+
* fp - file pointer
295+
* offset - offset to start zeroing or deallocating
296+
* len - length to zero or deallocate
297+
*/
298+
int
299+
zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
300+
{
301+
(void) fp, (void) offset, (void) len;
302+
return (SET_ERROR(EOPNOTSUPP));
303+
}
304+
291305
zfs_file_t *
292306
zfs_file_get(int fd)
293307
{

module/os/linux/zfs/vdev_file.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,9 @@ vdev_file_io_start(zio_t *zio)
280280
zio_execute(zio);
281281
return;
282282
} else if (zio->io_type == ZIO_TYPE_TRIM) {
283-
int mode = 0;
284-
285283
ASSERT3U(zio->io_size, !=, 0);
286-
#ifdef __linux__
287-
mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
288-
#endif
289-
zio->io_error = zfs_file_fallocate(vf->vf_file,
290-
mode, zio->io_offset, zio->io_size);
284+
zio->io_error = zfs_file_deallocate(vf->vf_file,
285+
zio->io_offset, zio->io_size);
291286
zio_execute(zio);
292287
return;
293288
}

module/os/linux/zfs/zfs_file_os.c

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

283283
/*
284-
* fallocate - allocate or free space on disk
284+
* deallocate - zero and/or deallocate file storage
285285
*
286286
* fp - file pointer
287-
* mode (non-standard options for hole punching etc)
288-
* offset - offset to start allocating or freeing from
289-
* len - length to free / allocate
290-
*
291-
* OPTIONAL
287+
* offset - offset to start zeroing or deallocating
288+
* len - length to zero or deallocate
292289
*/
293290
int
294-
zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
291+
zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
295292
{
296293
/*
297294
* May enter XFS which generates a warning when PF_FSTRANS is set.
@@ -307,12 +304,16 @@ zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
307304
*/
308305
int error = EOPNOTSUPP;
309306
if (fp->f_op->fallocate)
310-
error = fp->f_op->fallocate(fp, mode, offset, len);
307+
error = -fp->f_op->fallocate(fp,
308+
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, len);
311309

312310
if (fstrans)
313311
current->flags |= __SPL_PF_FSTRANS;
314312

315-
return (error);
313+
if (error)
314+
return (SET_ERROR(error));
315+
316+
return (0);
316317
}
317318

318319
/*

0 commit comments

Comments
 (0)