Skip to content

Commit fa33064

Browse files
robnbehlendorf
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 #16496
1 parent 7cdfda3 commit fa33064

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
@@ -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)