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