Skip to content

Commit 8342c2f

Browse files
macdiceamotin
authored andcommitted
Add FreeBSD posix_fadvise support.
As commit 320f0c6 did for Linux, connect POSIX_FADV_WILLNEED up to dmu_prefetch() on FreeBSD. Reviewed-by: Mateusz Guzik <[email protected]> Reviewed-by: Alexander Motin <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Thomas Munro <[email protected]>
1 parent 358605b commit 8342c2f

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

module/os/freebsd/zfs/zfs_vnops_os.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6055,6 +6055,80 @@ zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
60556055
return (EOPNOTSUPP);
60566056
}
60576057

6058+
#ifndef _SYS_SYSPROTO_H_
6059+
struct vop_advise_args {
6060+
struct vnode *a_vp;
6061+
off_t a_start;
6062+
off_t a_end;
6063+
int a_advice;
6064+
};
6065+
#endif
6066+
6067+
static int
6068+
zfs_freebsd_advise(struct vop_advise_args *ap)
6069+
{
6070+
vnode_t *vp = ap->a_vp;
6071+
off_t start = ap->a_start;
6072+
off_t end = ap->a_end;
6073+
int advice = ap->a_advice;
6074+
off_t len;
6075+
znode_t *zp;
6076+
zfsvfs_t *zfsvfs;
6077+
objset_t *os;
6078+
int error = 0;
6079+
6080+
if (end < start)
6081+
return (EINVAL);
6082+
6083+
VOP_LOCK(vp, LK_SHARED);
6084+
if (VN_IS_DOOMED(vp)) {
6085+
error = EBADF;
6086+
goto out_unlock;
6087+
}
6088+
6089+
zp = VTOZ(vp);
6090+
zfsvfs = zp->z_zfsvfs;
6091+
os = zp->z_zfsvfs->z_os;
6092+
6093+
if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6094+
goto out_unlock;
6095+
6096+
/* kern_posix_fadvise points to the last byte, we want one past */
6097+
if (end != OFF_MAX)
6098+
end += 1;
6099+
len = end - start;
6100+
6101+
switch (advice) {
6102+
case POSIX_FADV_WILLNEED:
6103+
/*
6104+
* Pass on the caller's size directly, but note that
6105+
* dmu_prefetch_max will effectively cap it. If there really
6106+
* is a larger sequential access pattern, perhaps dmu_zfetch
6107+
* will detect it.
6108+
*/
6109+
dmu_prefetch(os, zp->z_id, 0, start, len,
6110+
ZIO_PRIORITY_ASYNC_READ);
6111+
break;
6112+
case POSIX_FADV_NORMAL:
6113+
case POSIX_FADV_RANDOM:
6114+
case POSIX_FADV_SEQUENTIAL:
6115+
case POSIX_FADV_DONTNEED:
6116+
case POSIX_FADV_NOREUSE:
6117+
/* ignored for now */
6118+
break;
6119+
default:
6120+
error = EINVAL;
6121+
break;
6122+
}
6123+
6124+
zfs_exit(zfsvfs, FTAG);
6125+
6126+
out_unlock:
6127+
VOP_UNLOCK(vp);
6128+
6129+
return (error);
6130+
}
6131+
60586132
static int
60596133
zfs_vptocnp(struct vop_vptocnp_args *ap)
60606134
{
@@ -6293,6 +6367,7 @@ struct vop_vector zfs_vnodeops = {
62936367
.vop_link = zfs_freebsd_link,
62946368
.vop_symlink = zfs_freebsd_symlink,
62956369
.vop_readlink = zfs_freebsd_readlink,
6370+
.vop_advise = zfs_freebsd_advise,
62966371
.vop_read = zfs_freebsd_read,
62976372
.vop_write = zfs_freebsd_write,
62986373
.vop_remove = zfs_freebsd_remove,

module/zfs/dmu.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,18 @@ dmu_prefetch_by_dnode(dnode_t *dn, int64_t level, uint64_t offset,
730730
*/
731731
rw_enter(&dn->dn_struct_rwlock, RW_READER);
732732
if (dn->dn_datablkshift != 0) {
733+
734+
/*
735+
* Limit prefetch to present blocks.
736+
*/
737+
uint64_t size = (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
738+
if (offset >= size) {
739+
rw_exit(&dn->dn_struct_rwlock);
740+
return;
741+
}
742+
if (offset + len < offset || offset + len > size)
743+
len = size - offset;
744+
733745
/*
734746
* The object has multiple blocks. Calculate the full range
735747
* of blocks [start, end2) and then split it into two parts,

tests/runfiles/common.run

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ tags = ['functional', 'direct']
717717
tests = ['exec_001_pos', 'exec_002_neg']
718718
tags = ['functional', 'exec']
719719

720+
[tests/functional/fadvise]
721+
tests = ['fadvise_willneed']
722+
tags = ['functional', 'fadvise']
723+
720724
[tests/functional/fallocate]
721725
tests = ['fallocate_punch-hole']
722726
tags = ['functional', 'fallocate']

tests/runfiles/linux.run

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ tests = ['events_001_pos', 'events_002_pos', 'zed_rc_filter', 'zed_fd_spill',
112112
'zed_slow_io', 'zed_slow_io_many_vdevs', 'zed_diagnose_multiple']
113113
tags = ['functional', 'events']
114114

115-
[tests/functional/fadvise:Linux]
116-
tests = ['fadvise_willneed']
117-
tags = ['functional', 'fadvise']
118-
119115
[tests/functional/fallocate:Linux]
120116
tests = ['fallocate_prealloc', 'fallocate_zero-range']
121117
tags = ['functional', 'fallocate']

tests/zfs-tests/cmd/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ scripts_zfs_tests_bin_PROGRAMS += %D%/read_dos_attributes %D%/write_dos_attribu
140140

141141
scripts_zfs_tests_bin_PROGRAMS += %D%/randfree_file
142142
%C%_randfree_file_SOURCES = %D%/file/randfree_file.c
143+
endif
143144

144145
scripts_zfs_tests_bin_PROGRAMS += %D%/file_fadvise
145146
%C%_file_fadvise_SOURCES = %D%/file/file_fadvise.c
146-
endif

0 commit comments

Comments
 (0)