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