#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: lfs_syscalls.c,v 1.180 2026/01/05 05:02:47 perseant Exp $");
#ifndef LFS
# define LFS
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/kernel.h>
#include <sys/kauth.h>
#include <sys/syscallargs.h>
#include <ufs/lfs/ulfs_inode.h>
#include <ufs/lfs/ulfsmount.h>
#include <ufs/lfs/ulfs_extern.h>
#include <ufs/lfs/lfs.h>
#include <ufs/lfs/lfs_accessors.h>
#include <ufs/lfs/lfs_kernel.h>
#include <ufs/lfs/lfs_extern.h>
static int lfs_fastvget(struct mount *, ino_t, BLOCK_INFO *, int,
struct vnode **);
static struct buf *lfs_fakebuf(struct lfs *, struct vnode *, daddr_t,
size_t, void *);
#ifdef USE_64BIT_SYSCALLS
int
sys_lfs_markv(struct lwp *l, const struct sys_lfs_markv_args *uap, register_t *retval)
{
BLOCK_INFO *blkiov;
int blkcnt, error;
fsid_t fsid;
struct lfs *fs;
struct mount *mntp;
if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
return (error);
if ((mntp = vfs_getvfs(&fsid)) == NULL)
return (ENOENT);
fs = VFSTOULFS(mntp)->um_lfs;
blkcnt = SCARG(uap, blkcnt);
if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
return (EINVAL);
KERNEL_LOCK(1, NULL);
blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
if ((error = copyin(SCARG(uap, blkiov), blkiov,
blkcnt * sizeof(BLOCK_INFO))) != 0)
goto out;
if ((error = lfs_markv(l, &fsid, blkiov, blkcnt)) == 0)
copyout(blkiov, SCARG(uap, blkiov),
blkcnt * sizeof(BLOCK_INFO));
out:
lfs_free(fs, blkiov, LFS_NB_BLKIOV);
KERNEL_UNLOCK_ONE(NULL);
return error;
}
#else
int
sys_lfs_markv(struct lwp *l, const struct sys_lfs_markv_args *uap, register_t *retval)
{
BLOCK_INFO *blkiov;
BLOCK_INFO_15 *blkiov15;
int i, blkcnt, error;
fsid_t fsid;
struct lfs *fs;
struct mount *mntp;
if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
return (error);
if ((mntp = vfs_getvfs(&fsid)) == NULL)
return (ENOENT);
fs = VFSTOULFS(mntp)->um_lfs;
blkcnt = SCARG(uap, blkcnt);
if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
return (EINVAL);
KERNEL_LOCK(1, NULL);
blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
blkiov15 = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO_15), LFS_NB_BLKIOV);
if ((error = copyin(SCARG(uap, blkiov), blkiov15,
blkcnt * sizeof(BLOCK_INFO_15))) != 0)
goto out;
for (i = 0; i < blkcnt; i++) {
blkiov[i].bi_inode = blkiov15[i].bi_inode;
blkiov[i].bi_lbn = blkiov15[i].bi_lbn;
blkiov[i].bi_daddr = blkiov15[i].bi_daddr;
blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
blkiov[i].bi_version = blkiov15[i].bi_version;
blkiov[i].bi_bp = blkiov15[i].bi_bp;
blkiov[i].bi_size = blkiov15[i].bi_size;
}
if ((error = lfs_markv(l, &fsid, blkiov, blkcnt)) == 0) {
for (i = 0; i < blkcnt; i++) {
blkiov15[i].bi_inode = blkiov[i].bi_inode;
blkiov15[i].bi_lbn = blkiov[i].bi_lbn;
blkiov15[i].bi_daddr = blkiov[i].bi_daddr;
blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
blkiov15[i].bi_version = blkiov[i].bi_version;
blkiov15[i].bi_bp = blkiov[i].bi_bp;
blkiov15[i].bi_size = blkiov[i].bi_size;
}
copyout(blkiov15, SCARG(uap, blkiov),
blkcnt * sizeof(BLOCK_INFO_15));
}
out:
lfs_free(fs, blkiov, LFS_NB_BLKIOV);
lfs_free(fs, blkiov15, LFS_NB_BLKIOV);
KERNEL_UNLOCK_ONE(NULL);
return error;
}
#endif
#define LFS_MARKV_MAX_BLOCKS (LFS_MAX_BUFS)
int
lfs_markv(struct lwp *l, fsid_t *fsidp, BLOCK_INFO *blkiov,
int blkcnt)
{
BLOCK_INFO *blkp;
IFILE *ifp;
struct buf *bp;
struct inode *ip = NULL;
struct lfs *fs;
struct mount *mntp;
struct ulfsmount *ump;
struct vnode *vp;
ino_t lastino;
daddr_t b_daddr;
int cnt, error;
int do_again = 0;
int numrefed = 0;
ino_t maxino;
size_t obsize;
int nblkwritten, ninowritten;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
KAUTH_REQ_SYSTEM_LFS_MARKV, NULL, NULL, NULL);
if (error)
return (error);
if ((mntp = vfs_getvfs(fsidp)) == NULL)
return (ENOENT);
ump = VFSTOULFS(mntp);
fs = ump->um_lfs;
if (fs->lfs_ronly)
return EROFS;
maxino = (lfs_fragstoblks(fs, lfs_dino_getblocks(fs, VTOI(fs->lfs_ivnode)->i_din)) -
lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
if ((error = vfs_busy(mntp)) != 0)
return (error);
lfs_cleanerlock(fs);
lastino = LFS_UNUSED_INUM;
for (cnt = blkcnt, blkp = blkiov; cnt--; ++blkp) {
if (blkp->bi_inode <= 0 || blkp->bi_inode >= maxino) {
error = EINVAL;
goto err4;
}
if (lastino != blkp->bi_inode) {
error = lfs_fastvget(mntp, blkp->bi_inode, blkp,
LK_EXCLUSIVE | LK_NOWAIT, &vp);
lfs_setclean(fs, vp);
vput(vp);
vp = NULL;
lastino = blkp->bi_inode;
}
}
lfs_seglock(fs, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
error = 0;
vp = NULL;
lastino = LFS_UNUSED_INUM;
nblkwritten = ninowritten = 0;
cnt = blkcnt;
for (blkp = blkiov; cnt--; ++blkp)
{
if (blkp->bi_inode <= 0 || blkp->bi_inode >= maxino) {
error = EINVAL;
goto err3;
}
if (lastino != blkp->bi_inode) {
if (vp != NULL) {
vput(vp);
vp = NULL;
numrefed--;
}
lastino = blkp->bi_inode;
error = lfs_fastvget(mntp, blkp->bi_inode, blkp,
LK_EXCLUSIVE | LK_NOWAIT, &vp);
if (error) {
DLOG((DLOG_CLEAN, "lfs_markv: lfs_fastvget"
" failed with %d (ino %d, segment %d)\n",
error, blkp->bi_inode,
lfs_dtosn(fs, blkp->bi_daddr)));
if (error == EAGAIN) {
error = 0;
do_again++;
} else
KASSERT(error == ENOENT);
KASSERT(vp == NULL);
ip = NULL;
continue;
}
ip = VTOI(vp);
numrefed++;
ninowritten++;
} else if (vp == NULL) {
continue;
}
if (vp->v_type == VDIR && (vp->v_uflag & VU_DIROP)) {
do_again++;
continue;
}
if (blkp->bi_lbn == LFS_UNUSED_LBN) {
if (blkp->bi_inode != LFS_IFILE_INUM) {
LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
if (lfs_if_getdaddr(fs, ifp) == blkp->bi_daddr) {
lfs_setclean(fs, vp);
}
brelse(bp, 0);
}
continue;
}
b_daddr = 0;
if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
LFS_DBTOFSB(fs, b_daddr) != blkp->bi_daddr)
{
if (lfs_dtosn(fs, LFS_DBTOFSB(fs, b_daddr)) ==
lfs_dtosn(fs, blkp->bi_daddr))
{
DLOG((DLOG_CLEAN, "lfs_markv: wrong da same seg: %jx vs %jx\n",
(intmax_t)blkp->bi_daddr, (intmax_t)LFS_DBTOFSB(fs, b_daddr)));
}
do_again++;
continue;
}
if (blkp->bi_lbn >= 0)
obsize = lfs_blksize(fs, ip, blkp->bi_lbn);
else
obsize = lfs_sb_getbsize(fs);
if (blkp->bi_lbn >= 0 && blkp->bi_lbn < ULFS_NDADDR) {
obsize = ip->i_lfs_fragsize[blkp->bi_lbn];
}
if (obsize != blkp->bi_size) {
DLOG((DLOG_CLEAN, "lfs_markv: ino %d lbn %jd wrong"
" size (%ld != %d), try again\n",
blkp->bi_inode, (intmax_t)blkp->bi_lbn,
(long) obsize, blkp->bi_size));
do_again++;
continue;
}
if (ip->i_number != LFS_IFILE_INUM && blkp->bi_lbn >= 0) {
bp = lfs_fakebuf(fs, vp, blkp->bi_lbn,
blkp->bi_size, blkp->bi_bp);
bp->b_blkno = LFS_FSBTODB(fs, blkp->bi_daddr);
} else {
if (blkp->bi_size != lfs_sb_getbsize(fs) &&
ip->i_number != LFS_IFILE_INUM)
panic("lfs_markv: partial indirect block?"
" size=%d\n", blkp->bi_size);
bp = getblk(vp, blkp->bi_lbn, blkp->bi_size, 0, 0);
if (!(bp->b_oflags & (BO_DONE|BO_DELWRI))) {
error = copyin(blkp->bi_bp, bp->b_data, blkp->bi_size);
if (error)
goto err2;
}
}
if ((error = lfs_bwrite_ext(bp, BW_CLEAN)) != 0)
goto err2;
nblkwritten++;
if (nblkwritten + lfs_lblkno(fs, ninowritten * DINOSIZE(fs))
> LFS_MARKV_MAX_BLOCKS) {
DLOG((DLOG_CLEAN, "lfs_markv: writing %d blks %d inos\n",
nblkwritten, ninowritten));
lfs_segwrite(mntp, SEGM_CLEAN);
nblkwritten = ninowritten = 0;
}
}
if (vp != NULL) {
vput(vp);
vp = NULL;
numrefed--;
}
KASSERTMSG((numrefed == 0), "lfs_markv: numrefed=%d", numrefed);
DLOG((DLOG_CLEAN, "lfs_markv: writing %d blks %d inos (check point)\n",
nblkwritten, ninowritten));
lfs_segwrite(mntp, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
lfs_segunlock(fs);
vfs_unbusy(mntp);
if (error)
return (error);
else if (do_again)
return EAGAIN;
return 0;
err2:
DLOG((DLOG_CLEAN, "lfs_markv err2\n"));
err3:
if (vp != NULL) {
vput(vp);
vp = NULL;
--numrefed;
}
lfs_segunlock(fs);
err4:
vfs_unbusy(mntp);
KASSERTMSG((numrefed == 0), "lfs_markv: numrefed=%d", numrefed);
return (error);
}
#ifdef USE_64BIT_SYSCALLS
int
sys_lfs_bmapv(struct lwp *l, const struct sys_lfs_bmapv_args *uap, register_t *retval)
{
BLOCK_INFO *blkiov;
int blkcnt, error;
fsid_t fsid;
struct lfs *fs;
struct mount *mntp;
if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
return (error);
if ((mntp = vfs_getvfs(&fsid)) == NULL)
return (ENOENT);
fs = VFSTOULFS(mntp)->um_lfs;
blkcnt = SCARG(uap, blkcnt);
#if SIZE_T_MAX <= UINT_MAX
if ((u_int) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
return (EINVAL);
#endif
KERNEL_LOCK(1, NULL);
blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
if ((error = copyin(SCARG(uap, blkiov), blkiov,
blkcnt * sizeof(BLOCK_INFO))) != 0)
goto out;
if ((error = lfs_bmapv(l, &fsid, blkiov, blkcnt)) == 0)
copyout(blkiov, SCARG(uap, blkiov),
blkcnt * sizeof(BLOCK_INFO));
out:
lfs_free(fs, blkiov, LFS_NB_BLKIOV);
KERNEL_UNLOCK_ONE(NULL);
return error;
}
#else
int
sys_lfs_bmapv(struct lwp *l, const struct sys_lfs_bmapv_args *uap, register_t *retval)
{
BLOCK_INFO *blkiov;
BLOCK_INFO_15 *blkiov15;
int i, blkcnt, error;
fsid_t fsid;
struct lfs *fs;
struct mount *mntp;
if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
return (error);
if ((mntp = vfs_getvfs(&fsid)) == NULL)
return (ENOENT);
fs = VFSTOULFS(mntp)->um_lfs;
blkcnt = SCARG(uap, blkcnt);
if ((size_t) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
return (EINVAL);
KERNEL_LOCK(1, NULL);
blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
blkiov15 = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO_15), LFS_NB_BLKIOV);
if ((error = copyin(SCARG(uap, blkiov), blkiov15,
blkcnt * sizeof(BLOCK_INFO_15))) != 0)
goto out;
for (i = 0; i < blkcnt; i++) {
blkiov[i].bi_inode = blkiov15[i].bi_inode;
blkiov[i].bi_lbn = blkiov15[i].bi_lbn;
blkiov[i].bi_daddr = blkiov15[i].bi_daddr;
blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
blkiov[i].bi_version = blkiov15[i].bi_version;
blkiov[i].bi_bp = blkiov15[i].bi_bp;
blkiov[i].bi_size = blkiov15[i].bi_size;
}
if ((error = lfs_bmapv(l, &fsid, blkiov, blkcnt)) == 0) {
for (i = 0; i < blkcnt; i++) {
blkiov15[i].bi_inode = blkiov[i].bi_inode;
blkiov15[i].bi_lbn = blkiov[i].bi_lbn;
blkiov15[i].bi_daddr = blkiov[i].bi_daddr;
blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
blkiov15[i].bi_version = blkiov[i].bi_version;
blkiov15[i].bi_bp = blkiov[i].bi_bp;
blkiov15[i].bi_size = blkiov[i].bi_size;
}
copyout(blkiov15, SCARG(uap, blkiov),
blkcnt * sizeof(BLOCK_INFO_15));
}
out:
lfs_free(fs, blkiov, LFS_NB_BLKIOV);
lfs_free(fs, blkiov15, LFS_NB_BLKIOV);
KERNEL_UNLOCK_ONE(NULL);
return error;
}
#endif
int
lfs_bmapv(struct lwp *l, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
{
BLOCK_INFO *blkp;
IFILE *ifp;
struct buf *bp;
struct inode *ip = NULL;
struct lfs *fs;
struct mount *mntp;
struct ulfsmount *ump;
struct vnode *vp;
ino_t lastino;
daddr_t v_daddr;
int cnt, error;
int numrefed = 0;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
KAUTH_REQ_SYSTEM_LFS_BMAPV, NULL, NULL, NULL);
if (error)
return (error);
if ((mntp = vfs_getvfs(fsidp)) == NULL)
return (ENOENT);
if ((error = vfs_busy(mntp)) != 0)
return (error);
ump = VFSTOULFS(mntp);
fs = ump->um_lfs;
if (fs->lfs_cleaner_thread == NULL)
fs->lfs_cleaner_thread = curlwp;
KASSERT(fs->lfs_cleaner_thread == curlwp);
cnt = blkcnt;
error = 0;
vp = NULL;
v_daddr = LFS_UNUSED_DADDR;
lastino = LFS_UNUSED_INUM;
for (blkp = blkiov; cnt--; ++blkp)
{
if (lastino != blkp->bi_inode) {
if (vp != NULL) {
vput(vp);
vp = NULL;
numrefed--;
}
lastino = blkp->bi_inode;
if (blkp->bi_inode == LFS_IFILE_INUM)
v_daddr = lfs_sb_getidaddr(fs);
else {
LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
v_daddr = lfs_if_getdaddr(fs, ifp);
brelse(bp, 0);
}
if (v_daddr == LFS_UNUSED_DADDR) {
blkp->bi_daddr = LFS_UNUSED_DADDR;
continue;
}
error = lfs_fastvget(mntp, blkp->bi_inode, NULL,
LK_SHARED, &vp);
if (error) {
DLOG((DLOG_CLEAN, "lfs_bmapv: lfs_fastvget ino"
"%d failed with %d",
blkp->bi_inode,error));
KASSERT(vp == NULL);
continue;
} else {
KASSERT(VOP_ISLOCKED(vp));
numrefed++;
}
ip = VTOI(vp);
} else if (vp == NULL) {
continue;
}
if (blkp->bi_lbn == LFS_UNUSED_LBN) {
blkp->bi_daddr = v_daddr;
} else {
daddr_t bi_daddr;
error = VOP_BMAP(vp, blkp->bi_lbn, NULL,
&bi_daddr, NULL);
if (error)
{
blkp->bi_daddr = LFS_UNUSED_DADDR;
continue;
}
blkp->bi_daddr = LFS_DBTOFSB(fs, bi_daddr);
if (blkp->bi_lbn >= 0)
blkp->bi_size = lfs_blksize(fs, ip, blkp->bi_lbn);
else
blkp->bi_size = lfs_sb_getbsize(fs);
}
}
if (vp != NULL) {
vput(vp);
vp = NULL;
numrefed--;
}
KASSERTMSG((numrefed == 0), "lfs_bmapv: numrefed=%d", numrefed);
vfs_unbusy(mntp);
return 0;
}
int
sys_lfs_segclean(struct lwp *l, const struct sys_lfs_segclean_args *uap, register_t *retval)
{
struct lfs *fs;
struct mount *mntp;
fsid_t fsid;
int error;
unsigned long segnum;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
KAUTH_REQ_SYSTEM_LFS_SEGCLEAN, NULL, NULL, NULL);
if (error)
return (error);
if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
return (error);
if ((mntp = vfs_getvfs(&fsid)) == NULL)
return (ENOENT);
fs = VFSTOULFS(mntp)->um_lfs;
segnum = SCARG(uap, segment);
if ((error = vfs_busy(mntp)) != 0)
return (error);
KERNEL_LOCK(1, NULL);
lfs_prelock(fs, 0);
error = lfs_do_segclean(fs, segnum, l->l_cred, l);
lfs_preunlock(fs);
KERNEL_UNLOCK_ONE(NULL);
vfs_unbusy(mntp);
return error;
}
int
lfs_do_segclean(struct lfs *fs, unsigned long segnum, kauth_cred_t cred, struct lwp *l)
{
struct buf *bp;
SEGUSE *sup;
ASSERT_SEGLOCK(fs);
if (lfs_dtosn(fs, lfs_sb_getcurseg(fs)) == segnum) {
return (EBUSY);
}
LFS_SEGENTRY(sup, fs, segnum, bp);
if (sup->su_nbytes) {
DLOG((DLOG_CLEAN, "lfs_segclean: not cleaning segment %lu:"
" %d live bytes\n", segnum, sup->su_nbytes));
brelse(bp, 0);
return (EBUSY);
}
if (sup->su_flags & SEGUSE_ACTIVE) {
DLOG((DLOG_CLEAN, "lfs_segclean: not cleaning segment %lu:"
" segment is active\n", segnum));
brelse(bp, 0);
return (EBUSY);
}
if (!(sup->su_flags & SEGUSE_DIRTY)) {
DLOG((DLOG_CLEAN, "lfs_segclean: not cleaning segment %lu:"
" segment is already clean\n", segnum));
brelse(bp, 0);
return (EALREADY);
}
lfs_markclean(fs, segnum, sup, cred, l);
LFS_WRITESEGENTRY(sup, fs, segnum, bp);
return 0;
}
int
lfs_markclean(struct lfs *fs, unsigned long segnum, SEGUSE *sup,
kauth_cred_t cred, struct lwp *l)
{
extern int lfs_dostats;
struct buf *bp;
CLEANERINFO *cip;
ASSERT_SEGLOCK(fs);
#ifdef DEBUG
if (lfs_checkempty(fs, segnum, cred, l) == EEXIST)
panic("Live data in cleaned segment %jd\n", (intmax_t)segnum);
#endif
lfs_sb_addavail(fs, lfs_segtod(fs, 1));
if (sup->su_flags & SEGUSE_SUPERBLOCK)
lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_SBPAD));
if (lfs_sb_getversion(fs) > 1 && segnum == 0 &&
lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_LABELPAD) - lfs_sb_gets0addr(fs));
mutex_enter(&lfs_lock);
lfs_sb_addbfree(fs, sup->su_nsums * lfs_btofsb(fs, lfs_sb_getsumsize(fs)) +
lfs_btofsb(fs, sup->su_ninos * lfs_sb_getibsize(fs)));
lfs_sb_subdmeta(fs, sup->su_nsums * lfs_btofsb(fs, lfs_sb_getsumsize(fs)) +
lfs_btofsb(fs, sup->su_ninos * lfs_sb_getibsize(fs)));
if (lfs_sb_getdmeta(fs) < 0)
lfs_sb_setdmeta(fs, 0);
mutex_exit(&lfs_lock);
sup->su_flags &= ~(SEGUSE_ACTIVE | SEGUSE_DIRTY
| SEGUSE_EMPTY | SEGUSE_READY
| SEGUSE_ERROR);
LFS_CLEANERINFO(cip, fs, bp);
lfs_ci_shiftdirtytoclean(fs, cip, 1);
lfs_sb_setnclean(fs, lfs_ci_getclean(fs, cip));
mutex_enter(&lfs_lock);
lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));
lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs)
- fs->lfs_ravail - fs->lfs_favail);
wakeup(&fs->lfs_availsleep);
mutex_exit(&lfs_lock);
(void) LFS_BWRITE_LOG(bp);
if (lfs_dostats)
++lfs_stats.segs_reclaimed;
return (0);
}
int
lfs_segwait(fsid_t *fsidp, struct timeval *tv)
{
struct mount *mntp;
void *addr;
u_long timeout;
int error;
mutex_enter(&lfs_lock);
if (fsidp == NULL || (mntp = vfs_getvfs(fsidp)) == NULL)
addr = &lfs_allclean_wakeup;
else
addr = &VFSTOULFS(mntp)->um_lfs->lfs_nextsegsleep;
timeout = tvtohz(tv);
error = cv_timedwait_sig(addr, &lfs_lock, timeout);
mutex_exit(&lfs_lock);
return (error == ERESTART ? EINTR : 0);
}
int
sys___lfs_segwait50(struct lwp *l, const struct sys___lfs_segwait50_args *uap,
register_t *retval)
{
struct timeval atv;
fsid_t fsid;
int error;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
KAUTH_REQ_SYSTEM_LFS_SEGWAIT, NULL, NULL, NULL);
if (error)
return (error);
if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
return (error);
if (SCARG(uap, tv)) {
error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
if (error)
return (error);
if (itimerfix(&atv))
return (EINVAL);
} else
atv.tv_sec = atv.tv_usec = 0;
return lfs_segwait(&fsid, &atv);
}
static int
lfs_fastvget(struct mount *mp, ino_t ino, BLOCK_INFO *blkp, int lk_flags,
struct vnode **vpp)
{
struct ulfsmount *ump;
struct lfs *fs;
int error;
ump = VFSTOULFS(mp);
fs = ump->um_lfs;
fs->lfs_cleaner_hint = blkp;
error = vcache_get(mp, &ino, sizeof(ino), vpp);
fs->lfs_cleaner_hint = NULL;
if (error)
return error;
error = vn_lock(*vpp, lk_flags);
if (error) {
if (error == EBUSY)
error = EAGAIN;
vrele(*vpp);
*vpp = NULL;
return error;
}
return 0;
}
static struct buf *
lfs_fakebuf(struct lfs *fs, struct vnode *vp, daddr_t lbn, size_t size, void *uaddr)
{
struct buf *bp;
int error;
KASSERT(VTOI(vp)->i_number != LFS_IFILE_INUM);
bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size, LFS_NB_CLEAN);
error = copyin(uaddr, bp->b_data, size);
if (error) {
lfs_freebuf(fs, bp);
return NULL;
}
KDASSERT(bp->b_iodone == lfs_free_aiodone);
#if 0
mutex_enter(&lfs_lock);
++fs->lfs_iocount;
mutex_exit(&lfs_lock);
#endif
bp->b_bufsize = size;
bp->b_bcount = size;
return (bp);
}