Home | History | Annotate | Line # | Download | only in sysvbfs
sysvbfs_vnops.c revision 1.42
      1 /*	$NetBSD: sysvbfs_vnops.c,v 1.42 2012/03/18 02:40:55 christos 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.42 2012/03/18 02:40:55 christos 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 
     82 	DPRINTF("%s: %s op=%d %d\n", __func__, name, nameiop,
     83 	    cnp->cn_flags);
     84 
     85 	*a->a_vpp = NULL;
     86 
     87 	KASSERT((cnp->cn_flags & ISDOTDOT) == 0);
     88 
     89 	if ((error = VOP_ACCESS(a->a_dvp, VEXEC, cnp->cn_cred)) != 0) {
     90 		return error;	/* directory permission. */
     91 	}
     92 
     93 	/* Deny last component write operation on a read-only mount */
     94 	if ((cnp->cn_flags & ISLASTCN) && (v->v_mount->mnt_flag & MNT_RDONLY) &&
     95 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
     96 		return EROFS;
     97 
     98 	if (namelen == 1 && name[0] == '.') {	/* "." */
     99 		vref(v);
    100 		*a->a_vpp = v;
    101 	} else {				/* Regular file */
    102 		if (!bfs_dirent_lookup_by_name(bfs, cnp->cn_nameptr,
    103 		    &dirent)) {
    104 			if (nameiop != CREATE && nameiop != RENAME) {
    105 				DPRINTF("%s: no such a file. (1)\n",
    106 				    __func__);
    107 				return ENOENT;
    108 			}
    109 			if ((error = VOP_ACCESS(v, VWRITE, cnp->cn_cred)) != 0)
    110 				return error;
    111 			return EJUSTRETURN;
    112 		}
    113 
    114 		/* Allocate v-node */
    115 		if ((error = sysvbfs_vget(v->v_mount, dirent->inode, &vpp)) != 0) {
    116 			DPRINTF("%s: can't get vnode.\n", __func__);
    117 			return error;
    118 		}
    119 		*a->a_vpp = vpp;
    120 	}
    121 
    122 	return 0;
    123 }
    124 
    125 int
    126 sysvbfs_create(void *arg)
    127 {
    128 	struct vop_create_args /* {
    129 		struct vnode *a_dvp;
    130 		struct vnode **a_vpp;
    131 		struct componentname *a_cnp;
    132 		struct vattr *a_vap;
    133 	} */ *a = arg;
    134 	struct sysvbfs_node *bnode = a->a_dvp->v_data;
    135 	struct sysvbfs_mount *bmp = bnode->bmp;
    136 	struct bfs *bfs = bmp->bfs;
    137 	struct mount *mp = bmp->mountp;
    138 	struct bfs_dirent *dirent;
    139 	struct bfs_fileattr attr;
    140 	struct vattr *va = a->a_vap;
    141 	kauth_cred_t cr = a->a_cnp->cn_cred;
    142 	int err = 0;
    143 
    144 	DPRINTF("%s: %s\n", __func__, a->a_cnp->cn_nameptr);
    145 	KDASSERT(a->a_vap->va_type == VREG);
    146 	attr.uid = kauth_cred_geteuid(cr);
    147 	attr.gid = kauth_cred_getegid(cr);
    148 	attr.mode = va->va_mode;
    149 
    150 	if ((err = bfs_file_create(bfs, a->a_cnp->cn_nameptr, 0, 0, &attr))
    151 	    != 0) {
    152 		DPRINTF("%s: bfs_file_create failed.\n", __func__);
    153 		goto unlock_exit;
    154 	}
    155 
    156 	if (!bfs_dirent_lookup_by_name(bfs, a->a_cnp->cn_nameptr, &dirent))
    157 		panic("no dirent for created file.");
    158 
    159 	if ((err = sysvbfs_vget(mp, dirent->inode, a->a_vpp)) != 0) {
    160 		DPRINTF("%s: sysvbfs_vget failed.\n", __func__);
    161 		goto unlock_exit;
    162 	}
    163 	bnode = (*a->a_vpp)->v_data;
    164 	bnode->update_ctime = true;
    165 	bnode->update_mtime = true;
    166 	bnode->update_atime = true;
    167 
    168  unlock_exit:
    169 	/* unlock parent directory */
    170 	vput(a->a_dvp);	/* locked at sysvbfs_lookup(); */
    171 
    172 	return err;
    173 }
    174 
    175 int
    176 sysvbfs_open(void *arg)
    177 {
    178 	struct vop_open_args /* {
    179 		struct vnode *a_vp;
    180 		int  a_mode;
    181 		kauth_cred_t a_cred;
    182 	} */ *a = arg;
    183 	struct vnode *v = a->a_vp;
    184 	struct sysvbfs_node *bnode = v->v_data;
    185 	struct bfs_inode *inode = bnode->inode;
    186 	struct bfs *bfs = bnode->bmp->bfs;
    187 	struct bfs_dirent *dirent;
    188 
    189 	DPRINTF("%s:\n", __func__);
    190 	KDASSERT(v->v_type == VREG || v->v_type == VDIR);
    191 
    192 	if (!bfs_dirent_lookup_by_inode(bfs, inode->number, &dirent))
    193 		return ENOENT;
    194 	bnode->update_atime = true;
    195 	if ((a->a_mode & FWRITE) && !(a->a_mode & O_APPEND)) {
    196 		bnode->size = 0;
    197 	} else {
    198 		bnode->size = bfs_file_size(inode);
    199 	}
    200 	bnode->data_block = inode->start_sector;
    201 
    202 	return 0;
    203 }
    204 
    205 int
    206 sysvbfs_close(void *arg)
    207 {
    208 	struct vop_close_args /* {
    209 		struct vnodeop_desc *a_desc;
    210 		struct vnode *a_vp;
    211 		int  a_fflag;
    212 		kauth_cred_t a_cred;
    213 	} */ *a = arg;
    214 	struct vnode *v = a->a_vp;
    215 	struct sysvbfs_node *bnode = v->v_data;
    216 	struct bfs_fileattr attr;
    217 
    218 	DPRINTF("%s:\n", __func__);
    219 	uvm_vnp_setsize(v, bnode->size);
    220 
    221 	memset(&attr, 0xff, sizeof attr);	/* Set VNOVAL all */
    222 	if (bnode->update_atime)
    223 		attr.atime = time_second;
    224 	if (bnode->update_ctime)
    225 		attr.ctime = time_second;
    226 	if (bnode->update_mtime)
    227 		attr.mtime = time_second;
    228 	bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
    229 
    230 	VOP_FSYNC(a->a_vp, a->a_cred, FSYNC_WAIT, 0, 0);
    231 
    232 	return 0;
    233 }
    234 
    235 static int
    236 sysvbfs_check_possible(struct vnode *vp, struct sysvbfs_node *bnode,
    237     mode_t mode)
    238 {
    239 
    240 	if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
    241 		return EROFS;
    242 
    243 	return 0;
    244 }
    245 
    246 static int
    247 sysvbfs_check_permitted(struct vnode *vp, struct sysvbfs_node *bnode,
    248     mode_t mode, kauth_cred_t cred)
    249 {
    250 	struct bfs_fileattr *attr = &bnode->inode->attr;
    251 
    252 	return kauth_authorize_vnode(cred, kauth_access_action(mode,
    253 	    vp->v_type, attr->mode), vp, NULL, genfs_can_access(vp->v_type,
    254 	    attr->mode, attr->uid, attr->gid, mode, cred));
    255 }
    256 
    257 int
    258 sysvbfs_access(void *arg)
    259 {
    260 	struct vop_access_args /* {
    261 		struct vnode	*a_vp;
    262 		int		a_mode;
    263 		kauth_cred_t	a_cred;
    264 	} */ *ap = arg;
    265 	struct vnode *vp = ap->a_vp;
    266 	struct sysvbfs_node *bnode = vp->v_data;
    267 	int error;
    268 
    269 	DPRINTF("%s:\n", __func__);
    270 
    271 	error = sysvbfs_check_possible(vp, bnode, ap->a_mode);
    272 	if (error)
    273 		return error;
    274 
    275 	error = sysvbfs_check_permitted(vp, bnode, ap->a_mode, ap->a_cred);
    276 
    277 	return error;
    278 }
    279 
    280 int
    281 sysvbfs_getattr(void *v)
    282 {
    283 	struct vop_getattr_args /* {
    284 		struct vnode *a_vp;
    285 		struct vattr *a_vap;
    286 		kauth_cred_t a_cred;
    287 	} */ *ap = v;
    288 	struct vnode *vp = ap->a_vp;
    289 	struct sysvbfs_node *bnode = vp->v_data;
    290 	struct bfs_inode *inode = bnode->inode;
    291 	struct bfs_fileattr *attr = &inode->attr;
    292 	struct sysvbfs_mount *bmp = bnode->bmp;
    293 	struct vattr *vap = ap->a_vap;
    294 
    295 	DPRINTF("%s:\n", __func__);
    296 
    297 	vap->va_type = vp->v_type;
    298 	vap->va_mode = attr->mode;
    299 	vap->va_nlink = attr->nlink;
    300 	vap->va_uid = attr->uid;
    301 	vap->va_gid = attr->gid;
    302 	vap->va_fsid = bmp->devvp->v_rdev;
    303 	vap->va_fileid = inode->number;
    304 	vap->va_size = bfs_file_size(inode);
    305 	vap->va_blocksize = BFS_BSIZE;
    306 	vap->va_atime.tv_sec = attr->atime;
    307 	vap->va_mtime.tv_sec = attr->mtime;
    308 	vap->va_ctime.tv_sec = attr->ctime;
    309 	vap->va_birthtime.tv_sec = 0;
    310 	vap->va_gen = 1;
    311 	vap->va_flags = 0;
    312 	vap->va_rdev = 0;	/* No device file */
    313 	vap->va_bytes = vap->va_size;
    314 	vap->va_filerev = 0;
    315 	vap->va_vaflags = 0;
    316 
    317 	return 0;
    318 }
    319 
    320 int
    321 sysvbfs_setattr(void *arg)
    322 {
    323 	struct vop_setattr_args /* {
    324 		struct vnode *a_vp;
    325 		struct vattr *a_vap;
    326 		kauth_cred_t a_cred;
    327 		struct proc *p;
    328 	} */ *ap = arg;
    329 	struct vnode *vp = ap->a_vp;
    330 	struct vattr *vap = ap->a_vap;
    331 	struct sysvbfs_node *bnode = vp->v_data;
    332 	struct bfs_inode *inode = bnode->inode;
    333 	struct bfs_fileattr *attr = &inode->attr;
    334 	struct bfs *bfs = bnode->bmp->bfs;
    335 	kauth_cred_t cred = ap->a_cred;
    336 	int error;
    337 
    338 	DPRINTF("%s:\n", __func__);
    339 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    340 		return EROFS;
    341 
    342 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
    343 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    344 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    345 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL))
    346 		return EINVAL;
    347 
    348 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (uid_t)VNOVAL) {
    349 		uid_t uid =
    350 		    (vap->va_uid != (uid_t)VNOVAL) ? vap->va_uid : attr->uid;
    351 		gid_t gid =
    352 		    (vap->va_gid != (gid_t)VNOVAL) ? vap->va_gid : attr->gid;
    353 		error = kauth_authorize_vnode(cred,
    354 		    KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL,
    355 		    genfs_can_chown(cred, attr->uid, attr->gid, uid, gid));
    356 		if (error)
    357 			return error;
    358 		attr->uid = uid;
    359 		attr->gid = gid;
    360 	}
    361 
    362 	if (vap->va_size != VNOVAL)
    363 		switch (vp->v_type) {
    364 		case VDIR:
    365 			return EISDIR;
    366 		case VCHR:
    367 		case VBLK:
    368 		case VFIFO:
    369 			break;
    370 		case VREG:
    371 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    372 				return EROFS;
    373 			bfs_file_setsize(vp, vap->va_size);
    374 			break;
    375 		default:
    376 			return EOPNOTSUPP;
    377 		}
    378 
    379 	if (vap->va_mode != (mode_t)VNOVAL) {
    380 		mode_t mode = vap->va_mode;
    381 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY,
    382 		    vp, NULL, genfs_can_chmod(vp->v_type, cred, attr->uid,
    383 		    attr->gid, mode));
    384 		if (error)
    385 			return error;
    386 		attr->mode = mode;
    387 	}
    388 
    389 	if ((vap->va_atime.tv_sec != VNOVAL) ||
    390 	    (vap->va_mtime.tv_sec != VNOVAL) ||
    391 	    (vap->va_ctime.tv_sec != VNOVAL)) {
    392 		error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
    393 		    NULL, genfs_can_chtimes(vp, vap->va_vaflags, attr->uid,
    394 		    cred));
    395 		if (error)
    396 			return error;
    397 
    398 		if (vap->va_atime.tv_sec != VNOVAL)
    399 			attr->atime = vap->va_atime.tv_sec;
    400 		if (vap->va_mtime.tv_sec != VNOVAL)
    401 			attr->mtime = vap->va_mtime.tv_sec;
    402 		if (vap->va_ctime.tv_sec != VNOVAL)
    403 			attr->ctime = vap->va_ctime.tv_sec;
    404 	}
    405 
    406 	bfs_inode_set_attr(bfs, inode, attr);
    407 
    408 	return 0;
    409 }
    410 
    411 int
    412 sysvbfs_read(void *arg)
    413 {
    414 	struct vop_read_args /* {
    415 		struct vnode *a_vp;
    416 		struct uio *a_uio;
    417 		int a_ioflag;
    418 		kauth_cred_t a_cred;
    419 	} */ *a = arg;
    420 	struct vnode *v = a->a_vp;
    421 	struct uio *uio = a->a_uio;
    422 	struct sysvbfs_node *bnode = v->v_data;
    423 	struct bfs_inode *inode = bnode->inode;
    424 	vsize_t sz, filesz = bfs_file_size(inode);
    425 	int err;
    426 	const int advice = IO_ADV_DECODE(a->a_ioflag);
    427 
    428 	DPRINTF("%s: type=%d\n", __func__, v->v_type);
    429 	switch (v->v_type) {
    430 	case VREG:
    431 		break;
    432 	case VDIR:
    433 		return EISDIR;
    434 	default:
    435 		return EINVAL;
    436 	}
    437 
    438 	while (uio->uio_resid > 0) {
    439 		if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0)
    440 			break;
    441 
    442 		err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
    443 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(v));
    444 		if (err)
    445 			break;
    446 		DPRINTF("%s: read %ldbyte\n", __func__, sz);
    447 	}
    448 
    449 	return  sysvbfs_update(v, NULL, NULL, UPDATE_WAIT);
    450 }
    451 
    452 int
    453 sysvbfs_write(void *arg)
    454 {
    455 	struct vop_write_args /* {
    456 		struct vnode *a_vp;
    457 		struct uio *a_uio;
    458 		int  a_ioflag;
    459 		kauth_cred_t a_cred;
    460 	} */ *a = arg;
    461 	struct vnode *v = a->a_vp;
    462 	struct uio *uio = a->a_uio;
    463 	int advice = IO_ADV_DECODE(a->a_ioflag);
    464 	struct sysvbfs_node *bnode = v->v_data;
    465 	bool extended = false;
    466 	vsize_t sz;
    467 	int err = 0;
    468 
    469 	if (a->a_vp->v_type != VREG)
    470 		return EISDIR;
    471 
    472 	if (a->a_ioflag & IO_APPEND)
    473 		uio->uio_offset = bnode->size;
    474 
    475 	if (uio->uio_resid == 0)
    476 		return 0;
    477 
    478 	if (bnode->size < uio->uio_offset + uio->uio_resid) {
    479 		bfs_file_setsize(v, uio->uio_offset + uio->uio_resid);
    480 		extended = true;
    481 	}
    482 
    483 	while (uio->uio_resid > 0) {
    484 		sz = uio->uio_resid;
    485 		err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
    486 		    UBC_WRITE | UBC_UNMAP_FLAG(v));
    487 		if (err)
    488 			break;
    489 		DPRINTF("%s: write %ldbyte\n", __func__, sz);
    490 	}
    491 	if (err)
    492 		bfs_file_setsize(v, bnode->size - uio->uio_resid);
    493 
    494 	VN_KNOTE(v, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    495 
    496 	return err;
    497 }
    498 
    499 int
    500 sysvbfs_remove(void *arg)
    501 {
    502 	struct vop_remove_args /* {
    503 		struct vnodeop_desc *a_desc;
    504 		struct vnode * a_dvp;
    505 		struct vnode * a_vp;
    506 		struct componentname * a_cnp;
    507 	} */ *ap = arg;
    508 	struct vnode *vp = ap->a_vp;
    509 	struct vnode *dvp = ap->a_dvp;
    510 	struct sysvbfs_node *bnode = vp->v_data;
    511 	struct sysvbfs_mount *bmp = bnode->bmp;
    512 	struct bfs *bfs = bmp->bfs;
    513 	int err;
    514 
    515 	DPRINTF("%s: delete %s\n", __func__, ap->a_cnp->cn_nameptr);
    516 
    517 	if (vp->v_type == VDIR)
    518 		return EPERM;
    519 
    520 	if ((err = bfs_file_delete(bfs, ap->a_cnp->cn_nameptr)) != 0)
    521 		DPRINTF("%s: bfs_file_delete failed.\n", __func__);
    522 
    523 	VN_KNOTE(ap->a_vp, NOTE_DELETE);
    524 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    525 	if (dvp == vp)
    526 		vrele(vp);
    527 	else
    528 		vput(vp);
    529 	vput(dvp);
    530 
    531 	if (err == 0) {
    532 		bnode->removed = 1;
    533 	}
    534 
    535 	return err;
    536 }
    537 
    538 int
    539 sysvbfs_rename(void *arg)
    540 {
    541 	struct vop_rename_args /* {
    542 		struct vnode *a_fdvp;	from parent-directory v-node
    543 		struct vnode *a_fvp;	from file v-node
    544 		struct componentname *a_fcnp;
    545 		struct vnode *a_tdvp;	to parent-directory
    546 		struct vnode *a_tvp;	to file v-node
    547 		struct componentname *a_tcnp;
    548 	} */ *ap = arg;
    549 	struct vnode *fvp = ap->a_fvp;
    550 	struct vnode *fdvp = ap->a_fdvp;
    551 	struct vnode *tvp = ap->a_tvp;
    552 	struct vnode *tdvp = ap->a_tdvp;
    553 	struct sysvbfs_node *bnode = fvp->v_data;
    554 	struct bfs *bfs = bnode->bmp->bfs;
    555 	const char *from_name = ap->a_fcnp->cn_nameptr;
    556 	const char *to_name = ap->a_tcnp->cn_nameptr;
    557 	int error;
    558 
    559 	DPRINTF("%s: %s->%s\n", __func__, from_name, to_name);
    560 	if ((fvp->v_mount != tdvp->v_mount) ||
    561 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    562 		error = EXDEV;
    563 		printf("cross-device link\n");
    564 		goto out;
    565 	}
    566 
    567 	KDASSERT(fvp->v_type == VREG);
    568 	KDASSERT(tvp == NULL ? true : tvp->v_type == VREG);
    569 	KASSERT(tdvp == fdvp);
    570 
    571 	/*
    572 	 * Make sure the source hasn't been removed between lookup
    573 	 * and target directory lock.
    574 	 */
    575 	if (bnode->removed) {
    576 		error = ENOENT;
    577 		goto out;
    578 	}
    579 
    580 	error = bfs_file_rename(bfs, from_name, to_name);
    581  out:
    582 	if (tvp) {
    583 		if (error == 0) {
    584 			struct sysvbfs_node *tbnode = tvp->v_data;
    585 			tbnode->removed = 1;
    586 		}
    587 		vput(tvp);
    588 	}
    589 
    590 	/* tdvp == tvp probably can't happen with this fs, but safety first */
    591 	if (tdvp == tvp)
    592 		vrele(tdvp);
    593 	else
    594 		vput(tdvp);
    595 
    596 	vrele(fdvp);
    597 	vrele(fvp);
    598 
    599 	return 0;
    600 }
    601 
    602 int
    603 sysvbfs_readdir(void *v)
    604 {
    605 	struct vop_readdir_args /* {
    606 		struct vnode *a_vp;
    607 		struct uio *a_uio;
    608 		kauth_cred_t a_cred;
    609 		int *a_eofflag;
    610 		off_t **a_cookies;
    611 		int *a_ncookies;
    612 	} */ *ap = v;
    613 	struct uio *uio = ap->a_uio;
    614 	struct vnode *vp = ap->a_vp;
    615 	struct sysvbfs_node *bnode = vp->v_data;
    616 	struct bfs *bfs = bnode->bmp->bfs;
    617 	struct dirent *dp;
    618 	struct bfs_dirent *file;
    619 	int i, n, error;
    620 
    621 	DPRINTF("%s: offset=%" PRId64 " residue=%zu\n", __func__,
    622 	    uio->uio_offset, uio->uio_resid);
    623 
    624 	KDASSERT(vp->v_type == VDIR);
    625 	KDASSERT(uio->uio_offset >= 0);
    626 
    627 	dp = malloc(sizeof(struct dirent), M_BFS, M_WAITOK | M_ZERO);
    628 
    629 	i = uio->uio_offset / sizeof(struct dirent);
    630 	n = uio->uio_resid / sizeof(struct dirent);
    631 	if ((i + n) > bfs->n_dirent)
    632 		n = bfs->n_dirent - i;
    633 
    634 	for (file = &bfs->dirent[i]; i < n; file++) {
    635 		if (file->inode == 0)
    636 			continue;
    637 		if (i == bfs->max_dirent) {
    638 			DPRINTF("%s: file system inconsistent.\n",
    639 			    __func__);
    640 			break;
    641 		}
    642 		i++;
    643 		memset(dp, 0, sizeof(struct dirent));
    644 		dp->d_fileno = file->inode;
    645 		dp->d_type = file->inode == BFS_ROOT_INODE ? DT_DIR : DT_REG;
    646 		dp->d_namlen = strlen(file->name);
    647 		strncpy(dp->d_name, file->name, BFS_FILENAME_MAXLEN);
    648 		dp->d_reclen = sizeof(struct dirent);
    649 		if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
    650 			DPRINTF("%s: uiomove failed.\n", __func__);
    651 			free(dp, M_BFS);
    652 			return error;
    653 		}
    654 	}
    655 	DPRINTF("%s: %d %d %d\n", __func__, i, n, bfs->n_dirent);
    656 	*ap->a_eofflag = (i == bfs->n_dirent);
    657 
    658 	free(dp, M_BFS);
    659 	return 0;
    660 }
    661 
    662 int
    663 sysvbfs_inactive(void *arg)
    664 {
    665 	struct vop_inactive_args /* {
    666 		struct vnode *a_vp;
    667 		bool *a_recycle;
    668 	} */ *a = arg;
    669 	struct vnode *v = a->a_vp;
    670 	struct sysvbfs_node *bnode = v->v_data;
    671 
    672 	DPRINTF("%s:\n", __func__);
    673 	if (bnode->removed)
    674 		*a->a_recycle = true;
    675 	else
    676 		*a->a_recycle = false;
    677 	VOP_UNLOCK(v);
    678 
    679 	return 0;
    680 }
    681 
    682 int
    683 sysvbfs_reclaim(void *v)
    684 {
    685 	extern struct pool sysvbfs_node_pool;
    686 	struct vop_reclaim_args /* {
    687 		struct vnode *a_vp;
    688 	} */ *ap = v;
    689 	struct vnode *vp = ap->a_vp;
    690 	struct sysvbfs_node *bnode = vp->v_data;
    691 
    692 	DPRINTF("%s:\n", __func__);
    693 	mutex_enter(&mntvnode_lock);
    694 	LIST_REMOVE(bnode, link);
    695 	mutex_exit(&mntvnode_lock);
    696 	genfs_node_destroy(vp);
    697 	pool_put(&sysvbfs_node_pool, bnode);
    698 	vp->v_data = NULL;
    699 
    700 	return 0;
    701 }
    702 
    703 int
    704 sysvbfs_bmap(void *arg)
    705 {
    706 	struct vop_bmap_args /* {
    707 		struct vnode *a_vp;
    708 		daddr_t  a_bn;
    709 		struct vnode **a_vpp;
    710 		daddr_t *a_bnp;
    711 		int *a_runp;
    712 	} */ *a = arg;
    713 	struct vnode *v = a->a_vp;
    714 	struct sysvbfs_node *bnode = v->v_data;
    715 	struct sysvbfs_mount *bmp = bnode->bmp;
    716 	struct bfs_inode *inode = bnode->inode;
    717 	daddr_t blk;
    718 
    719 	DPRINTF("%s:\n", __func__);
    720 	/* BFS algorithm is contiguous allocation */
    721 	blk = inode->start_sector + a->a_bn;
    722 
    723 	if (blk * BFS_BSIZE > bmp->bfs->data_end)
    724 		return ENOSPC;
    725 
    726 	*a->a_vpp = bmp->devvp;
    727 	*a->a_runp = 0;
    728 	DPRINTF("%s: %d + %" PRId64 "\n", __func__, inode->start_sector,
    729 	    a->a_bn);
    730 
    731 	*a->a_bnp = blk;
    732 
    733 
    734 	return 0;
    735 }
    736 
    737 int
    738 sysvbfs_strategy(void *arg)
    739 {
    740 	struct vop_strategy_args /* {
    741 		struct vnode *a_vp;
    742 		struct buf *a_bp;
    743 	} */ *a = arg;
    744 	struct buf *b = a->a_bp;
    745 	struct vnode *v = a->a_vp;
    746 	struct sysvbfs_node *bnode = v->v_data;
    747 	struct sysvbfs_mount *bmp = bnode->bmp;
    748 	int error;
    749 
    750 	DPRINTF("%s:\n", __func__);
    751 	KDASSERT(v->v_type == VREG);
    752 	if (b->b_blkno == b->b_lblkno) {
    753 		error = VOP_BMAP(v, b->b_lblkno, NULL, &b->b_blkno, NULL);
    754 		if (error) {
    755 			b->b_error = error;
    756 			biodone(b);
    757 			return error;
    758 		}
    759 		if ((long)b->b_blkno == -1)
    760 			clrbuf(b);
    761 	}
    762 	if ((long)b->b_blkno == -1) {
    763 		biodone(b);
    764 		return 0;
    765 	}
    766 
    767 	return VOP_STRATEGY(bmp->devvp, b);
    768 }
    769 
    770 int
    771 sysvbfs_print(void *v)
    772 {
    773 	struct vop_print_args /* {
    774 		struct vnode *a_vp;
    775 	} */ *ap = v;
    776 	struct sysvbfs_node *bnode = ap->a_vp->v_data;
    777 
    778 	DPRINTF("%s:\n", __func__);
    779 	bfs_dump(bnode->bmp->bfs);
    780 
    781 	return 0;
    782 }
    783 
    784 int
    785 sysvbfs_advlock(void *v)
    786 {
    787 	struct vop_advlock_args /* {
    788 		struct vnode *a_vp;
    789 		void *a_id;
    790 		int a_op;
    791 		struct flock *a_fl;
    792 		int a_flags;
    793 	} */ *ap = v;
    794 	struct sysvbfs_node *bnode = ap->a_vp->v_data;
    795 
    796 	DPRINTF("%s: op=%d\n", __func__, ap->a_op);
    797 
    798 	return lf_advlock(ap, &bnode->lockf, bfs_file_size(bnode->inode));
    799 }
    800 
    801 int
    802 sysvbfs_pathconf(void *v)
    803 {
    804 	struct vop_pathconf_args /* {
    805 		struct vnode *a_vp;
    806 		int a_name;
    807 		register_t *a_retval;
    808 	} */ *ap = v;
    809 	int err = 0;
    810 
    811 	DPRINTF("%s:\n", __func__);
    812 
    813 	switch (ap->a_name) {
    814 	case _PC_LINK_MAX:
    815 		*ap->a_retval = 1;
    816 		break;
    817 	case _PC_NAME_MAX:
    818 		*ap->a_retval = BFS_FILENAME_MAXLEN;
    819 		break;
    820 	case _PC_PATH_MAX:
    821 		*ap->a_retval = BFS_FILENAME_MAXLEN;
    822 		break;
    823 	case _PC_CHOWN_RESTRICTED:
    824 		*ap->a_retval = 1;
    825 		break;
    826 	case _PC_NO_TRUNC:
    827 		*ap->a_retval = 0;
    828 		break;
    829 	case _PC_SYNC_IO:
    830 		*ap->a_retval = 1;
    831 		break;
    832 	case _PC_FILESIZEBITS:
    833 		*ap->a_retval = 32;
    834 		break;
    835 	default:
    836 		err = EINVAL;
    837 		break;
    838 	}
    839 
    840 	return err;
    841 }
    842 
    843 int
    844 sysvbfs_fsync(void *v)
    845 {
    846 	struct vop_fsync_args /* {
    847 		struct vnode *a_vp;
    848 		kauth_cred_t a_cred;
    849 		int a_flags;
    850 		off_t offlo;
    851 		off_t offhi;
    852 	} */ *ap = v;
    853 	struct vnode *vp = ap->a_vp;
    854 	int error, wait;
    855 
    856 	if (ap->a_flags & FSYNC_CACHE) {
    857 		return EOPNOTSUPP;
    858 	}
    859 
    860 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
    861 	error = vflushbuf(vp, wait);
    862 	if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
    863 		error = sysvbfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    864 
    865 	return error;
    866 }
    867 
    868 int
    869 sysvbfs_update(struct vnode *vp, const struct timespec *acc,
    870     const struct timespec *mod, int flags)
    871 {
    872 	struct sysvbfs_node *bnode = vp->v_data;
    873 	struct bfs_fileattr attr;
    874 
    875 	DPRINTF("%s:\n", __func__);
    876 	memset(&attr, 0xff, sizeof attr);	/* Set VNOVAL all */
    877 	if (bnode->update_atime) {
    878 		attr.atime = acc ? acc->tv_sec : time_second;
    879 		bnode->update_atime = false;
    880 	}
    881 	if (bnode->update_ctime) {
    882 		attr.ctime = time_second;
    883 		bnode->update_ctime = false;
    884 	}
    885 	if (bnode->update_mtime) {
    886 		attr.mtime = mod ? mod->tv_sec : time_second;
    887 		bnode->update_mtime = false;
    888 	}
    889 	bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
    890 
    891 	return 0;
    892 }
    893