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