Home | History | Annotate | Line # | Download | only in sysvbfs
sysvbfs_vnops.c revision 1.29
      1 /*	$NetBSD: sysvbfs_vnops.c,v 1.29 2010/05/27 13:22:02 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: sysvbfs_vnops.c,v 1.29 2010/05/27 13:22:02 pooka Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/kernel.h>
     37 #include <sys/resource.h>
     38 #include <sys/vnode.h>
     39 #include <sys/namei.h>
     40 #include <sys/dirent.h>
     41 #include <sys/malloc.h>
     42 #include <sys/lockf.h>
     43 #include <sys/unistd.h>
     44 #include <sys/fcntl.h>
     45 #include <sys/kauth.h>
     46 #include <sys/buf.h>
     47 
     48 #include <miscfs/genfs/genfs.h>
     49 
     50 #include <fs/sysvbfs/sysvbfs.h>
     51 #include <fs/sysvbfs/bfs.h>
     52 
     53 #ifdef SYSVBFS_VNOPS_DEBUG
     54 #define	DPRINTF(fmt, args...)	printf(fmt, ##args)
     55 #else
     56 #define	DPRINTF(arg...)		((void)0)
     57 #endif
     58 #define	ROUND_SECTOR(x)		(((x) + 511) & ~511)
     59 
     60 MALLOC_JUSTDEFINE(M_SYSVBFS_VNODE, "sysvbfs vnode", "sysvbfs vnode structures");
     61 MALLOC_DECLARE(M_BFS);
     62 
     63 int
     64 sysvbfs_lookup(void *arg)
     65 {
     66 	struct vop_lookup_args /* {
     67 		struct vnode *a_dvp;
     68 		struct vnode **a_vpp;
     69 		struct componentname *a_cnp;
     70 	} */ *a = arg;
     71 	struct vnode *v = a->a_dvp;
     72 	struct sysvbfs_node *bnode = v->v_data;
     73 	struct bfs *bfs = bnode->bmp->bfs;	/* my filesystem */
     74 	struct vnode *vpp = NULL;
     75 	struct bfs_dirent *dirent = NULL;
     76 	struct componentname *cnp = a->a_cnp;
     77 	int nameiop = cnp->cn_nameiop;
     78 	const char *name = cnp->cn_nameptr;
     79 	int namelen = cnp->cn_namelen;
     80 	int error;
     81 	bool islastcn = cnp->cn_flags & ISLASTCN;
     82 
     83 	DPRINTF("%s: %s op=%d %d\n", __func__, name, nameiop,
     84 	    cnp->cn_flags);
     85 
     86 	*a->a_vpp = NULL;
     87 
     88 	KASSERT((cnp->cn_flags & ISDOTDOT) == 0);
     89 	if ((error = VOP_ACCESS(a->a_dvp, VEXEC, cnp->cn_cred)) != 0) {
     90 		return error;	/* directory permittion. */
     91 	}
     92 
     93 	if (namelen == 1 && name[0] == '.') {	/* "." */
     94 		vref(v);
     95 		*a->a_vpp = v;
     96 	} else {				/* Regular file */
     97 		if (!bfs_dirent_lookup_by_name(bfs, cnp->cn_nameptr,
     98 		    &dirent)) {
     99 			if (nameiop != CREATE && nameiop != RENAME) {
    100 				DPRINTF("%s: no such a file. (1)\n",
    101 				    __func__);
    102 				return ENOENT;
    103 			}
    104 			if ((error = VOP_ACCESS(v, VWRITE, cnp->cn_cred)) != 0)
    105 				return error;
    106 			cnp->cn_flags |= SAVENAME;
    107 			return EJUSTRETURN;
    108 		}
    109 
    110 		/* Allocate v-node */
    111 		if ((error = sysvbfs_vget(v->v_mount, dirent->inode, &vpp)) != 0) {
    112 			DPRINTF("%s: can't get vnode.\n", __func__);
    113 			return error;
    114 		}
    115 		*a->a_vpp = vpp;
    116 	}
    117 
    118 	if (cnp->cn_nameiop != LOOKUP && islastcn)
    119 		cnp->cn_flags |= SAVENAME;
    120 
    121 	return 0;
    122 }
    123 
    124 int
    125 sysvbfs_create(void *arg)
    126 {
    127 	struct vop_create_args /* {
    128 		struct vnode *a_dvp;
    129 		struct vnode **a_vpp;
    130 		struct componentname *a_cnp;
    131 		struct vattr *a_vap;
    132 	} */ *a = arg;
    133 	struct sysvbfs_node *bnode = a->a_dvp->v_data;
    134 	struct sysvbfs_mount *bmp = bnode->bmp;
    135 	struct bfs *bfs = bmp->bfs;
    136 	struct mount *mp = bmp->mountp;
    137 	struct bfs_dirent *dirent;
    138 	struct bfs_fileattr attr;
    139 	struct vattr *va = a->a_vap;
    140 	kauth_cred_t cr = a->a_cnp->cn_cred;
    141 	int err = 0;
    142 
    143 	DPRINTF("%s: %s\n", __func__, a->a_cnp->cn_nameptr);
    144 	KDASSERT(a->a_vap->va_type == VREG);
    145 	attr.uid = kauth_cred_geteuid(cr);
    146 	attr.gid = kauth_cred_getegid(cr);
    147 	attr.mode = va->va_mode;
    148 
    149 	if ((err = bfs_file_create(bfs, a->a_cnp->cn_nameptr, 0, 0, &attr))
    150 	    != 0) {
    151 		DPRINTF("%s: bfs_file_create failed.\n", __func__);
    152 		goto unlock_exit;
    153 	}
    154 
    155 	if (!bfs_dirent_lookup_by_name(bfs, a->a_cnp->cn_nameptr, &dirent))
    156 		panic("no dirent for created file.");
    157 
    158 	if ((err = sysvbfs_vget(mp, dirent->inode, a->a_vpp)) != 0) {
    159 		DPRINTF("%s: sysvbfs_vget failed.\n", __func__);
    160 		goto unlock_exit;
    161 	}
    162 	bnode = (*a->a_vpp)->v_data;
    163 	bnode->update_ctime = true;
    164 	bnode->update_mtime = true;
    165 	bnode->update_atime = true;
    166 
    167  unlock_exit:
    168 	/* unlock parent directory */
    169 	vput(a->a_dvp);	/* locked at sysvbfs_lookup(); */
    170 
    171 	if (err || (a->a_cnp->cn_flags & SAVESTART) == 0)
    172 		PNBUF_PUT(a->a_cnp->cn_pnbuf);
    173 
    174 	return err;
    175 }
    176 
    177 int
    178 sysvbfs_open(void *arg)
    179 {
    180 	struct vop_open_args /* {
    181 		struct vnode *a_vp;
    182 		int  a_mode;
    183 		kauth_cred_t a_cred;
    184 	} */ *a = arg;
    185 	struct vnode *v = a->a_vp;
    186 	struct sysvbfs_node *bnode = v->v_data;
    187 	struct bfs_inode *inode = bnode->inode;
    188 	struct bfs *bfs = bnode->bmp->bfs;
    189 	struct bfs_dirent *dirent;
    190 
    191 	DPRINTF("%s:\n", __func__);
    192 	KDASSERT(v->v_type == VREG || v->v_type == VDIR);
    193 
    194 	if (!bfs_dirent_lookup_by_inode(bfs, inode->number, &dirent))
    195 		return ENOENT;
    196 	bnode->update_atime = true;
    197 	if ((a->a_mode & FWRITE) && !(a->a_mode & O_APPEND)) {
    198 		bnode->size = 0;
    199 	} else {
    200 		bnode->size = bfs_file_size(inode);
    201 	}
    202 	bnode->data_block = inode->start_sector;
    203 
    204 	return 0;
    205 }
    206 
    207 int
    208 sysvbfs_close(void *arg)
    209 {
    210 	struct vop_close_args /* {
    211 		struct vnodeop_desc *a_desc;
    212 		struct vnode *a_vp;
    213 		int  a_fflag;
    214 		kauth_cred_t a_cred;
    215 	} */ *a = arg;
    216 	struct vnode *v = a->a_vp;
    217 	struct sysvbfs_node *bnode = v->v_data;
    218 	struct bfs_fileattr attr;
    219 
    220 	DPRINTF("%s:\n", __func__);
    221 	uvm_vnp_setsize(v, bnode->size);
    222 
    223 	memset(&attr, 0xff, sizeof attr);	/* Set VNOVAL all */
    224 	if (bnode->update_atime)
    225 		attr.atime = time_second;
    226 	if (bnode->update_ctime)
    227 		attr.ctime = time_second;
    228 	if (bnode->update_mtime)
    229 		attr.mtime = time_second;
    230 	bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
    231 
    232 	VOP_FSYNC(a->a_vp, a->a_cred, FSYNC_WAIT, 0, 0);
    233 
    234 	return 0;
    235 }
    236 
    237 static int
    238 sysvbfs_check_possible(struct vnode *vp, struct sysvbfs_node *bnode,
    239     mode_t mode)
    240 {
    241 
    242 	if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
    243 		return EROFS;
    244 
    245 	return 0;
    246 }
    247 
    248 static int
    249 sysvbfs_check_permitted(struct vnode *vp, struct sysvbfs_node *bnode,
    250     mode_t mode, kauth_cred_t cred)
    251 {
    252 	struct bfs_fileattr *attr = &bnode->inode->attr;
    253 
    254 	return genfs_can_access(vp->v_type, attr->mode, attr->uid, attr->gid,
    255 	    mode, cred);
    256 }
    257 
    258 int
    259 sysvbfs_access(void *arg)
    260 {
    261 	struct vop_access_args /* {
    262 		struct vnode	*a_vp;
    263 		int		a_mode;
    264 		kauth_cred_t	a_cred;
    265 	} */ *ap = arg;
    266 	struct vnode *vp = ap->a_vp;
    267 	struct sysvbfs_node *bnode = vp->v_data;
    268 	int error;
    269 
    270 	DPRINTF("%s:\n", __func__);
    271 
    272 	error = sysvbfs_check_possible(vp, bnode, ap->a_mode);
    273 	if (error)
    274 		return error;
    275 
    276 	error = sysvbfs_check_permitted(vp, bnode, ap->a_mode, ap->a_cred);
    277 
    278 	return error;
    279 }
    280 
    281 int
    282 sysvbfs_getattr(void *v)
    283 {
    284 	struct vop_getattr_args /* {
    285 		struct vnode *a_vp;
    286 		struct vattr *a_vap;
    287 		kauth_cred_t a_cred;
    288 	} */ *ap = v;
    289 	struct vnode *vp = ap->a_vp;
    290 	struct sysvbfs_node *bnode = vp->v_data;
    291 	struct bfs_inode *inode = bnode->inode;
    292 	struct bfs_fileattr *attr = &inode->attr;
    293 	struct sysvbfs_mount *bmp = bnode->bmp;
    294 	struct vattr *vap = ap->a_vap;
    295 
    296 	DPRINTF("%s:\n", __func__);
    297 
    298 	vap->va_type = vp->v_type;
    299 	vap->va_mode = attr->mode;
    300 	vap->va_nlink = attr->nlink;
    301 	vap->va_uid = attr->uid;
    302 	vap->va_gid = attr->gid;
    303 	vap->va_fsid = bmp->devvp->v_rdev;
    304 	vap->va_fileid = inode->number;
    305 	vap->va_size = bfs_file_size(inode);
    306 	vap->va_blocksize = BFS_BSIZE;
    307 	vap->va_atime.tv_sec = attr->atime;
    308 	vap->va_mtime.tv_sec = attr->mtime;
    309 	vap->va_ctime.tv_sec = attr->ctime;
    310 	vap->va_birthtime.tv_sec = 0;
    311 	vap->va_gen = 1;
    312 	vap->va_flags = 0;
    313 	vap->va_rdev = 0;	/* No device file */
    314 	vap->va_bytes = vap->va_size;
    315 	vap->va_filerev = 0;
    316 	vap->va_vaflags = 0;
    317 
    318 	return 0;
    319 }
    320 
    321 int
    322 sysvbfs_setattr(void *arg)
    323 {
    324 	struct vop_setattr_args /* {
    325 		struct vnode *a_vp;
    326 		struct vattr *a_vap;
    327 		kauth_cred_t a_cred;
    328 		struct proc *p;
    329 	} */ *ap = arg;
    330 	struct vnode *vp = ap->a_vp;
    331 	struct vattr *vap = ap->a_vap;
    332 	struct sysvbfs_node *bnode = vp->v_data;
    333 	struct bfs_inode *inode = bnode->inode;
    334 	struct bfs_fileattr *attr = &inode->attr;
    335 	struct bfs *bfs = bnode->bmp->bfs;
    336 
    337 	DPRINTF("%s:\n", __func__);
    338 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    339 		return EROFS;
    340 
    341 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
    342 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    343 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    344 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL))
    345 		return EINVAL;
    346 
    347 	if (vap->va_uid != (uid_t)VNOVAL)
    348 		attr->uid = vap->va_uid;
    349 	if (vap->va_gid != (uid_t)VNOVAL)
    350 		attr->gid = vap->va_gid;
    351 	if (vap->va_mode != (mode_t)VNOVAL)
    352 		attr->mode = vap->va_mode;
    353 	if (vap->va_atime.tv_sec != VNOVAL)
    354 		attr->atime = vap->va_atime.tv_sec;
    355 	if (vap->va_mtime.tv_sec != VNOVAL)
    356 		attr->mtime = vap->va_mtime.tv_sec;
    357 	if (vap->va_ctime.tv_sec != VNOVAL)
    358 		attr->ctime = vap->va_ctime.tv_sec;
    359 
    360 	bfs_inode_set_attr(bfs, inode, attr);
    361 
    362 	return 0;
    363 }
    364 
    365 int
    366 sysvbfs_read(void *arg)
    367 {
    368 	struct vop_read_args /* {
    369 		struct vnode *a_vp;
    370 		struct uio *a_uio;
    371 		int a_ioflag;
    372 		kauth_cred_t a_cred;
    373 	} */ *a = arg;
    374 	struct vnode *v = a->a_vp;
    375 	struct uio *uio = a->a_uio;
    376 	struct sysvbfs_node *bnode = v->v_data;
    377 	struct bfs_inode *inode = bnode->inode;
    378 	vsize_t sz, filesz = bfs_file_size(inode);
    379 	int err;
    380 	const int advice = IO_ADV_DECODE(a->a_ioflag);
    381 
    382 	DPRINTF("%s: type=%d\n", __func__, v->v_type);
    383 	if (v->v_type != VREG)
    384 		return EINVAL;
    385 
    386 	while (uio->uio_resid > 0) {
    387 		if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0)
    388 			break;
    389 
    390 		err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
    391 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(v));
    392 		if (err)
    393 			break;
    394 		DPRINTF("%s: read %ldbyte\n", __func__, sz);
    395 	}
    396 
    397 	return  sysvbfs_update(v, NULL, NULL, UPDATE_WAIT);
    398 }
    399 
    400 int
    401 sysvbfs_write(void *arg)
    402 {
    403 	struct vop_write_args /* {
    404 		struct vnode *a_vp;
    405 		struct uio *a_uio;
    406 		int  a_ioflag;
    407 		kauth_cred_t a_cred;
    408 	} */ *a = arg;
    409 	struct vnode *v = a->a_vp;
    410 	struct uio *uio = a->a_uio;
    411 	int advice = IO_ADV_DECODE(a->a_ioflag);
    412 	struct sysvbfs_node *bnode = v->v_data;
    413 	struct bfs_inode *inode = bnode->inode;
    414 	bool extended = false;
    415 	vsize_t sz;
    416 	int err = 0;
    417 
    418 	if (a->a_vp->v_type != VREG)
    419 		return EISDIR;
    420 
    421 	if (a->a_ioflag & IO_APPEND)
    422 		uio->uio_offset = bnode->size;
    423 
    424 	if (uio->uio_resid == 0)
    425 		return 0;
    426 
    427 	if (bnode->size < uio->uio_offset + uio->uio_resid) {
    428 		bnode->size = uio->uio_offset + uio->uio_resid;
    429 		uvm_vnp_setsize(v, bnode->size);
    430 		extended = true;
    431 	}
    432 
    433 	while (uio->uio_resid > 0) {
    434 		sz = uio->uio_resid;
    435 		err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
    436 		    UBC_WRITE | UBC_UNMAP_FLAG(v));
    437 		if (err)
    438 			break;
    439 		DPRINTF("%s: write %ldbyte\n", __func__, sz);
    440 	}
    441 	inode->end_sector = bnode->data_block +
    442 	    (ROUND_SECTOR(bnode->size) >> DEV_BSHIFT) - 1;
    443 	inode->eof_offset_byte = bnode->data_block * DEV_BSIZE +
    444 	    bnode->size - 1;
    445 	bnode->update_mtime = true;
    446 
    447 	VN_KNOTE(v, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    448 
    449 	return err;
    450 }
    451 
    452 int
    453 sysvbfs_remove(void *arg)
    454 {
    455 	struct vop_remove_args /* {
    456 		struct vnodeop_desc *a_desc;
    457 		struct vnode * a_dvp;
    458 		struct vnode * a_vp;
    459 		struct componentname * a_cnp;
    460 	} */ *ap = arg;
    461 	struct vnode *vp = ap->a_vp;
    462 	struct vnode *dvp = ap->a_dvp;
    463 	struct sysvbfs_node *bnode = vp->v_data;
    464 	struct sysvbfs_mount *bmp = bnode->bmp;
    465 	struct bfs *bfs = bmp->bfs;
    466 	int err;
    467 
    468 	DPRINTF("%s: delete %s\n", __func__, ap->a_cnp->cn_nameptr);
    469 
    470 	if (vp->v_type == VDIR)
    471 		return EPERM;
    472 
    473 	if ((err = bfs_file_delete(bfs, ap->a_cnp->cn_nameptr)) != 0)
    474 		DPRINTF("%s: bfs_file_delete failed.\n", __func__);
    475 
    476 	VN_KNOTE(ap->a_vp, NOTE_DELETE);
    477 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    478 	if (dvp == vp)
    479 		vrele(vp);
    480 	else
    481 		vput(vp);
    482 	vput(dvp);
    483 
    484 	if (err || (ap->a_cnp->cn_flags & SAVESTART) == 0)
    485 		PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    486 
    487 	return err;
    488 }
    489 
    490 int
    491 sysvbfs_rename(void *arg)
    492 {
    493 	struct vop_rename_args /* {
    494 		struct vnode *a_fdvp;	from parent-directory v-node
    495 		struct vnode *a_fvp;	from file v-node
    496 		struct componentname *a_fcnp;
    497 		struct vnode *a_tdvp;	to parent-directory
    498 		struct vnode *a_tvp;	to file v-node
    499 		struct componentname *a_tcnp;
    500 	} */ *ap = arg;
    501 	struct vnode *fvp = ap->a_fvp;
    502 	struct vnode *fdvp = ap->a_fdvp;
    503 	struct vnode *tvp = ap->a_tvp;
    504 	struct vnode *tdvp = ap->a_tdvp;
    505 	struct sysvbfs_node *bnode = fvp->v_data;
    506 	struct bfs *bfs = bnode->bmp->bfs;
    507 	const char *from_name = ap->a_fcnp->cn_nameptr;
    508 	const char *to_name = ap->a_tcnp->cn_nameptr;
    509 	int error;
    510 
    511 	DPRINTF("%s: %s->%s\n", __func__, from_name, to_name);
    512 	if ((fvp->v_mount != tdvp->v_mount) ||
    513 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    514 		error = EXDEV;
    515 		printf("cross-device link\n");
    516 		goto out;
    517 	}
    518 
    519 	KDASSERT(fvp->v_type == VREG);
    520 	KDASSERT(tvp == NULL ? true : tvp->v_type == VREG);
    521 
    522 	error = bfs_file_rename(bfs, from_name, to_name);
    523  out:
    524 	if (tvp)
    525 		vput(tvp);
    526 
    527 	/* tdvp == tvp probably can't happen with this fs, but safety first */
    528 	if (tdvp == tvp)
    529 		vrele(tdvp);
    530 	else
    531 		vput(tdvp);
    532 
    533 	vrele(fdvp);
    534 	vrele(fvp);
    535 
    536 	return 0;
    537 }
    538 
    539 int
    540 sysvbfs_readdir(void *v)
    541 {
    542 	struct vop_readdir_args /* {
    543 		struct vnode *a_vp;
    544 		struct uio *a_uio;
    545 		kauth_cred_t a_cred;
    546 		int *a_eofflag;
    547 		off_t **a_cookies;
    548 		int *a_ncookies;
    549 	} */ *ap = v;
    550 	struct uio *uio = ap->a_uio;
    551 	struct vnode *vp = ap->a_vp;
    552 	struct sysvbfs_node *bnode = vp->v_data;
    553 	struct bfs *bfs = bnode->bmp->bfs;
    554 	struct dirent *dp;
    555 	struct bfs_dirent *file;
    556 	int i, n, error;
    557 
    558 	DPRINTF("%s: offset=%lld residue=%d\n", __func__,
    559 	    uio->uio_offset, uio->uio_resid);
    560 
    561 	KDASSERT(vp->v_type == VDIR);
    562 	KDASSERT(uio->uio_offset >= 0);
    563 
    564 	dp = malloc(sizeof(struct dirent), M_BFS, M_WAITOK | M_ZERO);
    565 
    566 	i = uio->uio_offset / sizeof(struct dirent);
    567 	n = uio->uio_resid / sizeof(struct dirent);
    568 	if ((i + n) > bfs->n_dirent)
    569 		n = bfs->n_dirent - i;
    570 
    571 	for (file = &bfs->dirent[i]; i < n; file++) {
    572 		if (file->inode == 0)
    573 			continue;
    574 		if (i == bfs->max_dirent) {
    575 			DPRINTF("%s: file system inconsistent.\n",
    576 			    __func__);
    577 			break;
    578 		}
    579 		i++;
    580 		memset(dp, 0, sizeof(struct dirent));
    581 		dp->d_fileno = file->inode;
    582 		dp->d_type = file->inode == BFS_ROOT_INODE ? DT_DIR : DT_REG;
    583 		dp->d_namlen = strlen(file->name);
    584 		strncpy(dp->d_name, file->name, BFS_FILENAME_MAXLEN);
    585 		dp->d_reclen = sizeof(struct dirent);
    586 		if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
    587 			DPRINTF("%s: uiomove failed.\n", __func__);
    588 			free(dp, M_BFS);
    589 			return error;
    590 		}
    591 	}
    592 	DPRINTF("%s: %d %d %d\n", __func__, i, n, bfs->n_dirent);
    593 	*ap->a_eofflag = (i == bfs->n_dirent);
    594 
    595 	free(dp, M_BFS);
    596 	return 0;
    597 }
    598 
    599 int
    600 sysvbfs_inactive(void *arg)
    601 {
    602 	struct vop_inactive_args /* {
    603 		struct vnode *a_vp;
    604 		bool *a_recycle;
    605 	} */ *a = arg;
    606 	struct vnode *v = a->a_vp;
    607 
    608 	DPRINTF("%s:\n", __func__);
    609 	*a->a_recycle = true;
    610 	VOP_UNLOCK(v, 0);
    611 
    612 	return 0;
    613 }
    614 
    615 int
    616 sysvbfs_reclaim(void *v)
    617 {
    618 	extern struct pool sysvbfs_node_pool;
    619 	struct vop_reclaim_args /* {
    620 		struct vnode *a_vp;
    621 	} */ *ap = v;
    622 	struct vnode *vp = ap->a_vp;
    623 	struct sysvbfs_node *bnode = vp->v_data;
    624 
    625 	DPRINTF("%s:\n", __func__);
    626 	mutex_enter(&mntvnode_lock);
    627 	LIST_REMOVE(bnode, link);
    628 	mutex_exit(&mntvnode_lock);
    629 	cache_purge(vp);
    630 	genfs_node_destroy(vp);
    631 	pool_put(&sysvbfs_node_pool, bnode);
    632 	vp->v_data = NULL;
    633 
    634 	return 0;
    635 }
    636 
    637 int
    638 sysvbfs_bmap(void *arg)
    639 {
    640 	struct vop_bmap_args /* {
    641 		struct vnode *a_vp;
    642 		daddr_t  a_bn;
    643 		struct vnode **a_vpp;
    644 		daddr_t *a_bnp;
    645 		int *a_runp;
    646 	} */ *a = arg;
    647 	struct vnode *v = a->a_vp;
    648 	struct sysvbfs_node *bnode = v->v_data;
    649 	struct sysvbfs_mount *bmp = bnode->bmp;
    650 	struct bfs_inode *inode = bnode->inode;
    651 	daddr_t blk;
    652 
    653 	DPRINTF("%s:\n", __func__);
    654 	/* BFS algorithm is contiguous allocation */
    655 	blk = inode->start_sector + a->a_bn;
    656 
    657 	if (blk * BFS_BSIZE > bmp->bfs->data_end)
    658 		return ENOSPC;
    659 
    660 	*a->a_vpp = bmp->devvp;
    661 	*a->a_runp = 0;
    662 	DPRINTF("%s: %d + %lld\n", __func__, inode->start_sector, a->a_bn);
    663 
    664 	*a->a_bnp = blk;
    665 
    666 
    667 	return 0;
    668 }
    669 
    670 int
    671 sysvbfs_strategy(void *arg)
    672 {
    673 	struct vop_strategy_args /* {
    674 		struct vnode *a_vp;
    675 		struct buf *a_bp;
    676 	} */ *a = arg;
    677 	struct buf *b = a->a_bp;
    678 	struct vnode *v = a->a_vp;
    679 	struct sysvbfs_node *bnode = v->v_data;
    680 	struct sysvbfs_mount *bmp = bnode->bmp;
    681 	int error;
    682 
    683 	DPRINTF("%s:\n", __func__);
    684 	KDASSERT(v->v_type == VREG);
    685 	if (b->b_blkno == b->b_lblkno) {
    686 		error = VOP_BMAP(v, b->b_lblkno, NULL, &b->b_blkno, NULL);
    687 		if (error) {
    688 			b->b_error = error;
    689 			biodone(b);
    690 			return error;
    691 		}
    692 		if ((long)b->b_blkno == -1)
    693 			clrbuf(b);
    694 	}
    695 	if ((long)b->b_blkno == -1) {
    696 		biodone(b);
    697 		return 0;
    698 	}
    699 
    700 	return VOP_STRATEGY(bmp->devvp, b);
    701 }
    702 
    703 int
    704 sysvbfs_print(void *v)
    705 {
    706 	struct vop_print_args /* {
    707 		struct vnode *a_vp;
    708 	} */ *ap = v;
    709 	struct sysvbfs_node *bnode = ap->a_vp->v_data;
    710 
    711 	DPRINTF("%s:\n", __func__);
    712 	bfs_dump(bnode->bmp->bfs);
    713 
    714 	return 0;
    715 }
    716 
    717 int
    718 sysvbfs_advlock(void *v)
    719 {
    720 	struct vop_advlock_args /* {
    721 		struct vnode *a_vp;
    722 		void *a_id;
    723 		int a_op;
    724 		struct flock *a_fl;
    725 		int a_flags;
    726 	} */ *ap = v;
    727 	struct sysvbfs_node *bnode = ap->a_vp->v_data;
    728 
    729 	DPRINTF("%s: op=%d\n", __func__, ap->a_op);
    730 
    731 	return lf_advlock(ap, &bnode->lockf, bfs_file_size(bnode->inode));
    732 }
    733 
    734 int
    735 sysvbfs_pathconf(void *v)
    736 {
    737 	struct vop_pathconf_args /* {
    738 		struct vnode *a_vp;
    739 		int a_name;
    740 		register_t *a_retval;
    741 	} */ *ap = v;
    742 	int err = 0;
    743 
    744 	DPRINTF("%s:\n", __func__);
    745 
    746 	switch (ap->a_name) {
    747 	case _PC_LINK_MAX:
    748 		*ap->a_retval = 1;
    749 		break;
    750 	case _PC_NAME_MAX:
    751 		*ap->a_retval = BFS_FILENAME_MAXLEN;
    752 		break;
    753 	case _PC_PATH_MAX:
    754 		*ap->a_retval = BFS_FILENAME_MAXLEN;
    755 		break;
    756 	case _PC_CHOWN_RESTRICTED:
    757 		*ap->a_retval = 1;
    758 		break;
    759 	case _PC_NO_TRUNC:
    760 		*ap->a_retval = 0;
    761 		break;
    762 	case _PC_SYNC_IO:
    763 		*ap->a_retval = 1;
    764 		break;
    765 	case _PC_FILESIZEBITS:
    766 		*ap->a_retval = 32;
    767 		break;
    768 	default:
    769 		err = EINVAL;
    770 		break;
    771 	}
    772 
    773 	return err;
    774 }
    775 
    776 int
    777 sysvbfs_fsync(void *v)
    778 {
    779 	struct vop_fsync_args /* {
    780 		struct vnode *a_vp;
    781 		kauth_cred_t a_cred;
    782 		int a_flags;
    783 		off_t offlo;
    784 		off_t offhi;
    785 	} */ *ap = v;
    786 	struct vnode *vp = ap->a_vp;
    787 	int error, wait;
    788 
    789 	if (ap->a_flags & FSYNC_CACHE) {
    790 		return EOPNOTSUPP;
    791 	}
    792 
    793 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
    794 	vflushbuf(vp, wait);
    795 
    796 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
    797 		error = 0;
    798 	else
    799 		error = sysvbfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    800 
    801 	return error;
    802 }
    803 
    804 int
    805 sysvbfs_update(struct vnode *vp, const struct timespec *acc,
    806     const struct timespec *mod, int flags)
    807 {
    808 	struct sysvbfs_node *bnode = vp->v_data;
    809 	struct bfs_fileattr attr;
    810 
    811 	DPRINTF("%s:\n", __func__);
    812 	memset(&attr, 0xff, sizeof attr);	/* Set VNOVAL all */
    813 	if (bnode->update_atime) {
    814 		attr.atime = acc ? acc->tv_sec : time_second;
    815 		bnode->update_atime = false;
    816 	}
    817 	if (bnode->update_ctime) {
    818 		attr.ctime = time_second;
    819 		bnode->update_ctime = false;
    820 	}
    821 	if (bnode->update_mtime) {
    822 		attr.mtime = mod ? mod->tv_sec : time_second;
    823 		bnode->update_mtime = false;
    824 	}
    825 	bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
    826 
    827 	return 0;
    828 }
    829