Home | History | Annotate | Line # | Download | only in sysvbfs
sysvbfs_vnops.c revision 1.28
      1 /*	$NetBSD: sysvbfs_vnops.c,v 1.28 2010/05/26 21:27:00 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.28 2010/05/26 21:27:00 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 	return err;
    172 }
    173 
    174 int
    175 sysvbfs_open(void *arg)
    176 {
    177 	struct vop_open_args /* {
    178 		struct vnode *a_vp;
    179 		int  a_mode;
    180 		kauth_cred_t a_cred;
    181 	} */ *a = arg;
    182 	struct vnode *v = a->a_vp;
    183 	struct sysvbfs_node *bnode = v->v_data;
    184 	struct bfs_inode *inode = bnode->inode;
    185 	struct bfs *bfs = bnode->bmp->bfs;
    186 	struct bfs_dirent *dirent;
    187 
    188 	DPRINTF("%s:\n", __func__);
    189 	KDASSERT(v->v_type == VREG || v->v_type == VDIR);
    190 
    191 	if (!bfs_dirent_lookup_by_inode(bfs, inode->number, &dirent))
    192 		return ENOENT;
    193 	bnode->update_atime = true;
    194 	if ((a->a_mode & FWRITE) && !(a->a_mode & O_APPEND)) {
    195 		bnode->size = 0;
    196 	} else {
    197 		bnode->size = bfs_file_size(inode);
    198 	}
    199 	bnode->data_block = inode->start_sector;
    200 
    201 	return 0;
    202 }
    203 
    204 int
    205 sysvbfs_close(void *arg)
    206 {
    207 	struct vop_close_args /* {
    208 		struct vnodeop_desc *a_desc;
    209 		struct vnode *a_vp;
    210 		int  a_fflag;
    211 		kauth_cred_t a_cred;
    212 	} */ *a = arg;
    213 	struct vnode *v = a->a_vp;
    214 	struct sysvbfs_node *bnode = v->v_data;
    215 	struct bfs_fileattr attr;
    216 
    217 	DPRINTF("%s:\n", __func__);
    218 	uvm_vnp_setsize(v, bnode->size);
    219 
    220 	memset(&attr, 0xff, sizeof attr);	/* Set VNOVAL all */
    221 	if (bnode->update_atime)
    222 		attr.atime = time_second;
    223 	if (bnode->update_ctime)
    224 		attr.ctime = time_second;
    225 	if (bnode->update_mtime)
    226 		attr.mtime = time_second;
    227 	bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
    228 
    229 	VOP_FSYNC(a->a_vp, a->a_cred, FSYNC_WAIT, 0, 0);
    230 
    231 	return 0;
    232 }
    233 
    234 static int
    235 sysvbfs_check_possible(struct vnode *vp, struct sysvbfs_node *bnode,
    236     mode_t mode)
    237 {
    238 
    239 	if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
    240 		return EROFS;
    241 
    242 	return 0;
    243 }
    244 
    245 static int
    246 sysvbfs_check_permitted(struct vnode *vp, struct sysvbfs_node *bnode,
    247     mode_t mode, kauth_cred_t cred)
    248 {
    249 	struct bfs_fileattr *attr = &bnode->inode->attr;
    250 
    251 	return genfs_can_access(vp->v_type, attr->mode, attr->uid, attr->gid,
    252 	    mode, cred);
    253 }
    254 
    255 int
    256 sysvbfs_access(void *arg)
    257 {
    258 	struct vop_access_args /* {
    259 		struct vnode	*a_vp;
    260 		int		a_mode;
    261 		kauth_cred_t	a_cred;
    262 	} */ *ap = arg;
    263 	struct vnode *vp = ap->a_vp;
    264 	struct sysvbfs_node *bnode = vp->v_data;
    265 	int error;
    266 
    267 	DPRINTF("%s:\n", __func__);
    268 
    269 	error = sysvbfs_check_possible(vp, bnode, ap->a_mode);
    270 	if (error)
    271 		return error;
    272 
    273 	error = sysvbfs_check_permitted(vp, bnode, ap->a_mode, ap->a_cred);
    274 
    275 	return error;
    276 }
    277 
    278 int
    279 sysvbfs_getattr(void *v)
    280 {
    281 	struct vop_getattr_args /* {
    282 		struct vnode *a_vp;
    283 		struct vattr *a_vap;
    284 		kauth_cred_t a_cred;
    285 	} */ *ap = v;
    286 	struct vnode *vp = ap->a_vp;
    287 	struct sysvbfs_node *bnode = vp->v_data;
    288 	struct bfs_inode *inode = bnode->inode;
    289 	struct bfs_fileattr *attr = &inode->attr;
    290 	struct sysvbfs_mount *bmp = bnode->bmp;
    291 	struct vattr *vap = ap->a_vap;
    292 
    293 	DPRINTF("%s:\n", __func__);
    294 
    295 	vap->va_type = vp->v_type;
    296 	vap->va_mode = attr->mode;
    297 	vap->va_nlink = attr->nlink;
    298 	vap->va_uid = attr->uid;
    299 	vap->va_gid = attr->gid;
    300 	vap->va_fsid = bmp->devvp->v_rdev;
    301 	vap->va_fileid = inode->number;
    302 	vap->va_size = bfs_file_size(inode);
    303 	vap->va_blocksize = BFS_BSIZE;
    304 	vap->va_atime.tv_sec = attr->atime;
    305 	vap->va_mtime.tv_sec = attr->mtime;
    306 	vap->va_ctime.tv_sec = attr->ctime;
    307 	vap->va_birthtime.tv_sec = 0;
    308 	vap->va_gen = 1;
    309 	vap->va_flags = 0;
    310 	vap->va_rdev = 0;	/* No device file */
    311 	vap->va_bytes = vap->va_size;
    312 	vap->va_filerev = 0;
    313 	vap->va_vaflags = 0;
    314 
    315 	return 0;
    316 }
    317 
    318 int
    319 sysvbfs_setattr(void *arg)
    320 {
    321 	struct vop_setattr_args /* {
    322 		struct vnode *a_vp;
    323 		struct vattr *a_vap;
    324 		kauth_cred_t a_cred;
    325 		struct proc *p;
    326 	} */ *ap = arg;
    327 	struct vnode *vp = ap->a_vp;
    328 	struct vattr *vap = ap->a_vap;
    329 	struct sysvbfs_node *bnode = vp->v_data;
    330 	struct bfs_inode *inode = bnode->inode;
    331 	struct bfs_fileattr *attr = &inode->attr;
    332 	struct bfs *bfs = bnode->bmp->bfs;
    333 
    334 	DPRINTF("%s:\n", __func__);
    335 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    336 		return EROFS;
    337 
    338 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
    339 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    340 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    341 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL))
    342 		return EINVAL;
    343 
    344 	if (vap->va_uid != (uid_t)VNOVAL)
    345 		attr->uid = vap->va_uid;
    346 	if (vap->va_gid != (uid_t)VNOVAL)
    347 		attr->gid = vap->va_gid;
    348 	if (vap->va_mode != (mode_t)VNOVAL)
    349 		attr->mode = vap->va_mode;
    350 	if (vap->va_atime.tv_sec != VNOVAL)
    351 		attr->atime = vap->va_atime.tv_sec;
    352 	if (vap->va_mtime.tv_sec != VNOVAL)
    353 		attr->mtime = vap->va_mtime.tv_sec;
    354 	if (vap->va_ctime.tv_sec != VNOVAL)
    355 		attr->ctime = vap->va_ctime.tv_sec;
    356 
    357 	bfs_inode_set_attr(bfs, inode, attr);
    358 
    359 	return 0;
    360 }
    361 
    362 int
    363 sysvbfs_read(void *arg)
    364 {
    365 	struct vop_read_args /* {
    366 		struct vnode *a_vp;
    367 		struct uio *a_uio;
    368 		int a_ioflag;
    369 		kauth_cred_t a_cred;
    370 	} */ *a = arg;
    371 	struct vnode *v = a->a_vp;
    372 	struct uio *uio = a->a_uio;
    373 	struct sysvbfs_node *bnode = v->v_data;
    374 	struct bfs_inode *inode = bnode->inode;
    375 	vsize_t sz, filesz = bfs_file_size(inode);
    376 	int err;
    377 	const int advice = IO_ADV_DECODE(a->a_ioflag);
    378 
    379 	DPRINTF("%s: type=%d\n", __func__, v->v_type);
    380 	if (v->v_type != VREG)
    381 		return EINVAL;
    382 
    383 	while (uio->uio_resid > 0) {
    384 		if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0)
    385 			break;
    386 
    387 		err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
    388 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(v));
    389 		if (err)
    390 			break;
    391 		DPRINTF("%s: read %ldbyte\n", __func__, sz);
    392 	}
    393 
    394 	return  sysvbfs_update(v, NULL, NULL, UPDATE_WAIT);
    395 }
    396 
    397 int
    398 sysvbfs_write(void *arg)
    399 {
    400 	struct vop_write_args /* {
    401 		struct vnode *a_vp;
    402 		struct uio *a_uio;
    403 		int  a_ioflag;
    404 		kauth_cred_t a_cred;
    405 	} */ *a = arg;
    406 	struct vnode *v = a->a_vp;
    407 	struct uio *uio = a->a_uio;
    408 	int advice = IO_ADV_DECODE(a->a_ioflag);
    409 	struct sysvbfs_node *bnode = v->v_data;
    410 	struct bfs_inode *inode = bnode->inode;
    411 	bool extended = false;
    412 	vsize_t sz;
    413 	int err = 0;
    414 
    415 	if (a->a_vp->v_type != VREG)
    416 		return EISDIR;
    417 
    418 	if (a->a_ioflag & IO_APPEND)
    419 		uio->uio_offset = bnode->size;
    420 
    421 	if (uio->uio_resid == 0)
    422 		return 0;
    423 
    424 	if (bnode->size < uio->uio_offset + uio->uio_resid) {
    425 		bnode->size = uio->uio_offset + uio->uio_resid;
    426 		uvm_vnp_setsize(v, bnode->size);
    427 		extended = true;
    428 	}
    429 
    430 	while (uio->uio_resid > 0) {
    431 		sz = uio->uio_resid;
    432 		err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
    433 		    UBC_WRITE | UBC_UNMAP_FLAG(v));
    434 		if (err)
    435 			break;
    436 		DPRINTF("%s: write %ldbyte\n", __func__, sz);
    437 	}
    438 	inode->end_sector = bnode->data_block +
    439 	    (ROUND_SECTOR(bnode->size) >> DEV_BSHIFT) - 1;
    440 	inode->eof_offset_byte = bnode->data_block * DEV_BSIZE +
    441 	    bnode->size - 1;
    442 	bnode->update_mtime = true;
    443 
    444 	VN_KNOTE(v, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    445 
    446 	return err;
    447 }
    448 
    449 int
    450 sysvbfs_remove(void *arg)
    451 {
    452 	struct vop_remove_args /* {
    453 		struct vnodeop_desc *a_desc;
    454 		struct vnode * a_dvp;
    455 		struct vnode * a_vp;
    456 		struct componentname * a_cnp;
    457 	} */ *ap = arg;
    458 	struct vnode *vp = ap->a_vp;
    459 	struct vnode *dvp = ap->a_dvp;
    460 	struct sysvbfs_node *bnode = vp->v_data;
    461 	struct sysvbfs_mount *bmp = bnode->bmp;
    462 	struct bfs *bfs = bmp->bfs;
    463 	int err;
    464 
    465 	DPRINTF("%s: delete %s\n", __func__, ap->a_cnp->cn_nameptr);
    466 
    467 	if (vp->v_type == VDIR)
    468 		return EPERM;
    469 
    470 	if ((err = bfs_file_delete(bfs, ap->a_cnp->cn_nameptr)) != 0)
    471 		DPRINTF("%s: bfs_file_delete failed.\n", __func__);
    472 
    473 	VN_KNOTE(ap->a_vp, NOTE_DELETE);
    474 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    475 	if (dvp == vp)
    476 		vrele(vp);
    477 	else
    478 		vput(vp);
    479 	vput(dvp);
    480 
    481 	return err;
    482 }
    483 
    484 int
    485 sysvbfs_rename(void *arg)
    486 {
    487 	struct vop_rename_args /* {
    488 		struct vnode *a_fdvp;	from parent-directory v-node
    489 		struct vnode *a_fvp;	from file v-node
    490 		struct componentname *a_fcnp;
    491 		struct vnode *a_tdvp;	to parent-directory
    492 		struct vnode *a_tvp;	to file v-node
    493 		struct componentname *a_tcnp;
    494 	} */ *ap = arg;
    495 	struct vnode *fvp = ap->a_fvp;
    496 	struct vnode *fdvp = ap->a_fdvp;
    497 	struct vnode *tvp = ap->a_tvp;
    498 	struct vnode *tdvp = ap->a_tdvp;
    499 	struct sysvbfs_node *bnode = fvp->v_data;
    500 	struct bfs *bfs = bnode->bmp->bfs;
    501 	const char *from_name = ap->a_fcnp->cn_nameptr;
    502 	const char *to_name = ap->a_tcnp->cn_nameptr;
    503 	int error;
    504 
    505 	DPRINTF("%s: %s->%s\n", __func__, from_name, to_name);
    506 	if ((fvp->v_mount != tdvp->v_mount) ||
    507 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    508 		error = EXDEV;
    509 		printf("cross-device link\n");
    510 		goto out;
    511 	}
    512 
    513 	KDASSERT(fvp->v_type == VREG);
    514 	KDASSERT(tvp == NULL ? true : tvp->v_type == VREG);
    515 
    516 	error = bfs_file_rename(bfs, from_name, to_name);
    517  out:
    518 	if (tvp)
    519 		vput(tvp);
    520 
    521 	/* tdvp == tvp probably can't happen with this fs, but safety first */
    522 	if (tdvp == tvp)
    523 		vrele(tdvp);
    524 	else
    525 		vput(tdvp);
    526 
    527 	vrele(fdvp);
    528 	vrele(fvp);
    529 
    530 	return 0;
    531 }
    532 
    533 int
    534 sysvbfs_readdir(void *v)
    535 {
    536 	struct vop_readdir_args /* {
    537 		struct vnode *a_vp;
    538 		struct uio *a_uio;
    539 		kauth_cred_t a_cred;
    540 		int *a_eofflag;
    541 		off_t **a_cookies;
    542 		int *a_ncookies;
    543 	} */ *ap = v;
    544 	struct uio *uio = ap->a_uio;
    545 	struct vnode *vp = ap->a_vp;
    546 	struct sysvbfs_node *bnode = vp->v_data;
    547 	struct bfs *bfs = bnode->bmp->bfs;
    548 	struct dirent *dp;
    549 	struct bfs_dirent *file;
    550 	int i, n, error;
    551 
    552 	DPRINTF("%s: offset=%lld residue=%d\n", __func__,
    553 	    uio->uio_offset, uio->uio_resid);
    554 
    555 	KDASSERT(vp->v_type == VDIR);
    556 	KDASSERT(uio->uio_offset >= 0);
    557 
    558 	dp = malloc(sizeof(struct dirent), M_BFS, M_WAITOK | M_ZERO);
    559 
    560 	i = uio->uio_offset / sizeof(struct dirent);
    561 	n = uio->uio_resid / sizeof(struct dirent);
    562 	if ((i + n) > bfs->n_dirent)
    563 		n = bfs->n_dirent - i;
    564 
    565 	for (file = &bfs->dirent[i]; i < n; file++) {
    566 		if (file->inode == 0)
    567 			continue;
    568 		if (i == bfs->max_dirent) {
    569 			DPRINTF("%s: file system inconsistent.\n",
    570 			    __func__);
    571 			break;
    572 		}
    573 		i++;
    574 		memset(dp, 0, sizeof(struct dirent));
    575 		dp->d_fileno = file->inode;
    576 		dp->d_type = file->inode == BFS_ROOT_INODE ? DT_DIR : DT_REG;
    577 		dp->d_namlen = strlen(file->name);
    578 		strncpy(dp->d_name, file->name, BFS_FILENAME_MAXLEN);
    579 		dp->d_reclen = sizeof(struct dirent);
    580 		if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
    581 			DPRINTF("%s: uiomove failed.\n", __func__);
    582 			free(dp, M_BFS);
    583 			return error;
    584 		}
    585 	}
    586 	DPRINTF("%s: %d %d %d\n", __func__, i, n, bfs->n_dirent);
    587 	*ap->a_eofflag = (i == bfs->n_dirent);
    588 
    589 	free(dp, M_BFS);
    590 	return 0;
    591 }
    592 
    593 int
    594 sysvbfs_inactive(void *arg)
    595 {
    596 	struct vop_inactive_args /* {
    597 		struct vnode *a_vp;
    598 		bool *a_recycle;
    599 	} */ *a = arg;
    600 	struct vnode *v = a->a_vp;
    601 
    602 	DPRINTF("%s:\n", __func__);
    603 	*a->a_recycle = true;
    604 	VOP_UNLOCK(v, 0);
    605 
    606 	return 0;
    607 }
    608 
    609 int
    610 sysvbfs_reclaim(void *v)
    611 {
    612 	extern struct pool sysvbfs_node_pool;
    613 	struct vop_reclaim_args /* {
    614 		struct vnode *a_vp;
    615 	} */ *ap = v;
    616 	struct vnode *vp = ap->a_vp;
    617 	struct sysvbfs_node *bnode = vp->v_data;
    618 
    619 	DPRINTF("%s:\n", __func__);
    620 	mutex_enter(&mntvnode_lock);
    621 	LIST_REMOVE(bnode, link);
    622 	mutex_exit(&mntvnode_lock);
    623 	cache_purge(vp);
    624 	genfs_node_destroy(vp);
    625 	pool_put(&sysvbfs_node_pool, bnode);
    626 	vp->v_data = NULL;
    627 
    628 	return 0;
    629 }
    630 
    631 int
    632 sysvbfs_bmap(void *arg)
    633 {
    634 	struct vop_bmap_args /* {
    635 		struct vnode *a_vp;
    636 		daddr_t  a_bn;
    637 		struct vnode **a_vpp;
    638 		daddr_t *a_bnp;
    639 		int *a_runp;
    640 	} */ *a = arg;
    641 	struct vnode *v = a->a_vp;
    642 	struct sysvbfs_node *bnode = v->v_data;
    643 	struct sysvbfs_mount *bmp = bnode->bmp;
    644 	struct bfs_inode *inode = bnode->inode;
    645 	daddr_t blk;
    646 
    647 	DPRINTF("%s:\n", __func__);
    648 	/* BFS algorithm is contiguous allocation */
    649 	blk = inode->start_sector + a->a_bn;
    650 
    651 	if (blk * BFS_BSIZE > bmp->bfs->data_end)
    652 		return ENOSPC;
    653 
    654 	*a->a_vpp = bmp->devvp;
    655 	*a->a_runp = 0;
    656 	DPRINTF("%s: %d + %lld\n", __func__, inode->start_sector, a->a_bn);
    657 
    658 	*a->a_bnp = blk;
    659 
    660 
    661 	return 0;
    662 }
    663 
    664 int
    665 sysvbfs_strategy(void *arg)
    666 {
    667 	struct vop_strategy_args /* {
    668 		struct vnode *a_vp;
    669 		struct buf *a_bp;
    670 	} */ *a = arg;
    671 	struct buf *b = a->a_bp;
    672 	struct vnode *v = a->a_vp;
    673 	struct sysvbfs_node *bnode = v->v_data;
    674 	struct sysvbfs_mount *bmp = bnode->bmp;
    675 	int error;
    676 
    677 	DPRINTF("%s:\n", __func__);
    678 	KDASSERT(v->v_type == VREG);
    679 	if (b->b_blkno == b->b_lblkno) {
    680 		error = VOP_BMAP(v, b->b_lblkno, NULL, &b->b_blkno, NULL);
    681 		if (error) {
    682 			b->b_error = error;
    683 			biodone(b);
    684 			return error;
    685 		}
    686 		if ((long)b->b_blkno == -1)
    687 			clrbuf(b);
    688 	}
    689 	if ((long)b->b_blkno == -1) {
    690 		biodone(b);
    691 		return 0;
    692 	}
    693 
    694 	return VOP_STRATEGY(bmp->devvp, b);
    695 }
    696 
    697 int
    698 sysvbfs_print(void *v)
    699 {
    700 	struct vop_print_args /* {
    701 		struct vnode *a_vp;
    702 	} */ *ap = v;
    703 	struct sysvbfs_node *bnode = ap->a_vp->v_data;
    704 
    705 	DPRINTF("%s:\n", __func__);
    706 	bfs_dump(bnode->bmp->bfs);
    707 
    708 	return 0;
    709 }
    710 
    711 int
    712 sysvbfs_advlock(void *v)
    713 {
    714 	struct vop_advlock_args /* {
    715 		struct vnode *a_vp;
    716 		void *a_id;
    717 		int a_op;
    718 		struct flock *a_fl;
    719 		int a_flags;
    720 	} */ *ap = v;
    721 	struct sysvbfs_node *bnode = ap->a_vp->v_data;
    722 
    723 	DPRINTF("%s: op=%d\n", __func__, ap->a_op);
    724 
    725 	return lf_advlock(ap, &bnode->lockf, bfs_file_size(bnode->inode));
    726 }
    727 
    728 int
    729 sysvbfs_pathconf(void *v)
    730 {
    731 	struct vop_pathconf_args /* {
    732 		struct vnode *a_vp;
    733 		int a_name;
    734 		register_t *a_retval;
    735 	} */ *ap = v;
    736 	int err = 0;
    737 
    738 	DPRINTF("%s:\n", __func__);
    739 
    740 	switch (ap->a_name) {
    741 	case _PC_LINK_MAX:
    742 		*ap->a_retval = 1;
    743 		break;
    744 	case _PC_NAME_MAX:
    745 		*ap->a_retval = BFS_FILENAME_MAXLEN;
    746 		break;
    747 	case _PC_PATH_MAX:
    748 		*ap->a_retval = BFS_FILENAME_MAXLEN;
    749 		break;
    750 	case _PC_CHOWN_RESTRICTED:
    751 		*ap->a_retval = 1;
    752 		break;
    753 	case _PC_NO_TRUNC:
    754 		*ap->a_retval = 0;
    755 		break;
    756 	case _PC_SYNC_IO:
    757 		*ap->a_retval = 1;
    758 		break;
    759 	case _PC_FILESIZEBITS:
    760 		*ap->a_retval = 32;
    761 		break;
    762 	default:
    763 		err = EINVAL;
    764 		break;
    765 	}
    766 
    767 	return err;
    768 }
    769 
    770 int
    771 sysvbfs_fsync(void *v)
    772 {
    773 	struct vop_fsync_args /* {
    774 		struct vnode *a_vp;
    775 		kauth_cred_t a_cred;
    776 		int a_flags;
    777 		off_t offlo;
    778 		off_t offhi;
    779 	} */ *ap = v;
    780 	struct vnode *vp = ap->a_vp;
    781 	int error, wait;
    782 
    783 	if (ap->a_flags & FSYNC_CACHE) {
    784 		return EOPNOTSUPP;
    785 	}
    786 
    787 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
    788 	vflushbuf(vp, wait);
    789 
    790 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
    791 		error = 0;
    792 	else
    793 		error = sysvbfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    794 
    795 	return error;
    796 }
    797 
    798 int
    799 sysvbfs_update(struct vnode *vp, const struct timespec *acc,
    800     const struct timespec *mod, int flags)
    801 {
    802 	struct sysvbfs_node *bnode = vp->v_data;
    803 	struct bfs_fileattr attr;
    804 
    805 	DPRINTF("%s:\n", __func__);
    806 	memset(&attr, 0xff, sizeof attr);	/* Set VNOVAL all */
    807 	if (bnode->update_atime) {
    808 		attr.atime = acc ? acc->tv_sec : time_second;
    809 		bnode->update_atime = false;
    810 	}
    811 	if (bnode->update_ctime) {
    812 		attr.ctime = time_second;
    813 		bnode->update_ctime = false;
    814 	}
    815 	if (bnode->update_mtime) {
    816 		attr.mtime = mod ? mod->tv_sec : time_second;
    817 		bnode->update_mtime = false;
    818 	}
    819 	bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
    820 
    821 	return 0;
    822 }
    823