xref: /netbsd-src/sys/fs/adosfs/advnops.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: advnops.c,v 1.43 2014/01/23 10:13:56 hannken Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Christian E. Hopps
5  * Copyright (c) 1996 Matthias Scheler
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christian E. Hopps.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: advnops.c,v 1.43 2014/01/23 10:13:56 hannken Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/time.h>
42 #include <sys/queue.h>
43 #include <sys/namei.h>
44 #include <sys/buf.h>
45 #include <sys/dirent.h>
46 #include <sys/inttypes.h>
47 #include <sys/malloc.h>
48 #include <sys/pool.h>
49 #include <sys/stat.h>
50 #include <sys/unistd.h>
51 #include <sys/proc.h>
52 #include <sys/kauth.h>
53 
54 #include <miscfs/genfs/genfs.h>
55 #include <miscfs/specfs/specdev.h>
56 #include <fs/adosfs/adosfs.h>
57 
58 extern struct vnodeops adosfs_vnodeops;
59 
60 #define	adosfs_open	genfs_nullop
61 int	adosfs_getattr(void *);
62 int	adosfs_read(void *);
63 int	adosfs_write(void *);
64 #define	adosfs_fcntl	genfs_fcntl
65 #define	adosfs_ioctl	genfs_enoioctl
66 #define	adosfs_poll	genfs_poll
67 int	adosfs_strategy(void *);
68 int	adosfs_link(void *);
69 int	adosfs_symlink(void *);
70 #define	adosfs_abortop	genfs_abortop
71 int	adosfs_bmap(void *);
72 int	adosfs_print(void *);
73 int	adosfs_readdir(void *);
74 int	adosfs_access(void *);
75 int	adosfs_readlink(void *);
76 int	adosfs_inactive(void *);
77 int	adosfs_reclaim(void *);
78 int	adosfs_pathconf(void *);
79 
80 #define adosfs_close 	genfs_nullop
81 #define adosfs_fsync 	genfs_nullop
82 #define adosfs_seek 	genfs_seek
83 
84 #define adosfs_advlock 	genfs_einval
85 #define adosfs_bwrite 	genfs_eopnotsupp
86 #define adosfs_create 	genfs_eopnotsupp
87 #define adosfs_mkdir 	genfs_eopnotsupp
88 #define adosfs_mknod 	genfs_eopnotsupp
89 #define adosfs_revoke	genfs_revoke
90 #define adosfs_mmap 	genfs_mmap
91 #define adosfs_remove 	genfs_eopnotsupp
92 #define adosfs_rename 	genfs_eopnotsupp
93 #define adosfs_rmdir 	genfs_eopnotsupp
94 #define adosfs_setattr 	genfs_eopnotsupp
95 
96 const struct vnodeopv_entry_desc adosfs_vnodeop_entries[] = {
97 	{ &vop_default_desc, vn_default_error },
98 	{ &vop_lookup_desc, adosfs_lookup },		/* lookup */
99 	{ &vop_create_desc, adosfs_create },		/* create */
100 	{ &vop_mknod_desc, adosfs_mknod },		/* mknod */
101 	{ &vop_open_desc, adosfs_open },		/* open */
102 	{ &vop_close_desc, adosfs_close },		/* close */
103 	{ &vop_access_desc, adosfs_access },		/* access */
104 	{ &vop_getattr_desc, adosfs_getattr },		/* getattr */
105 	{ &vop_setattr_desc, adosfs_setattr },		/* setattr */
106 	{ &vop_read_desc, adosfs_read },		/* read */
107 	{ &vop_write_desc, adosfs_write },		/* write */
108 	{ &vop_fcntl_desc, adosfs_fcntl },		/* fcntl */
109 	{ &vop_ioctl_desc, adosfs_ioctl },		/* ioctl */
110 	{ &vop_poll_desc, adosfs_poll },		/* poll */
111 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
112 	{ &vop_revoke_desc, adosfs_revoke },		/* revoke */
113 	{ &vop_mmap_desc, adosfs_mmap },		/* mmap */
114 	{ &vop_fsync_desc, adosfs_fsync },		/* fsync */
115 	{ &vop_seek_desc, adosfs_seek },		/* seek */
116 	{ &vop_remove_desc, adosfs_remove },		/* remove */
117 	{ &vop_link_desc, adosfs_link },		/* link */
118 	{ &vop_rename_desc, adosfs_rename },		/* rename */
119 	{ &vop_mkdir_desc, adosfs_mkdir },		/* mkdir */
120 	{ &vop_rmdir_desc, adosfs_rmdir },		/* rmdir */
121 	{ &vop_symlink_desc, adosfs_symlink },		/* symlink */
122 	{ &vop_readdir_desc, adosfs_readdir },		/* readdir */
123 	{ &vop_readlink_desc, adosfs_readlink },	/* readlink */
124 	{ &vop_abortop_desc, adosfs_abortop },		/* abortop */
125 	{ &vop_inactive_desc, adosfs_inactive },	/* inactive */
126 	{ &vop_reclaim_desc, adosfs_reclaim },		/* reclaim */
127 	{ &vop_lock_desc, genfs_lock },			/* lock */
128 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
129 	{ &vop_bmap_desc, adosfs_bmap },		/* bmap */
130 	{ &vop_strategy_desc, adosfs_strategy },	/* strategy */
131 	{ &vop_print_desc, adosfs_print },		/* print */
132 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
133 	{ &vop_pathconf_desc, adosfs_pathconf },	/* pathconf */
134 	{ &vop_advlock_desc, adosfs_advlock },		/* advlock */
135 	{ &vop_bwrite_desc, adosfs_bwrite },		/* bwrite */
136 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
137 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
138 	{ NULL, NULL }
139 };
140 
141 const struct vnodeopv_desc adosfs_vnodeop_opv_desc =
142 	{ &adosfs_vnodeop_p, adosfs_vnodeop_entries };
143 
144 int
145 adosfs_getattr(void *v)
146 {
147 	struct vop_getattr_args /* {
148 		struct vnode *a_vp;
149 		struct vattr *a_vap;
150 		kauth_cred_t a_cred;
151 	} */ *sp = v;
152 	struct vattr *vap;
153 	struct adosfsmount *amp;
154 	struct anode *ap;
155 	u_long fblks;
156 
157 #ifdef ADOSFS_DIAGNOSTIC
158 	advopprint(sp);
159 #endif
160 	vap = sp->a_vap;
161 	ap = VTOA(sp->a_vp);
162 	amp = ap->amp;
163 	vattr_null(vap);
164 	vap->va_uid = ap->uid;
165 	vap->va_gid = ap->gid;
166 	vap->va_fsid = sp->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
167 	vap->va_atime.tv_sec = vap->va_mtime.tv_sec = vap->va_ctime.tv_sec =
168 		ap->mtime.days * 24 * 60 * 60 + ap->mtime.mins * 60 +
169 		ap->mtime.ticks / 50 + (8 * 365 + 2) * 24 * 60 * 60;
170 	vap->va_atime.tv_nsec = vap->va_mtime.tv_nsec = vap->va_ctime.tv_nsec = 0;
171 	vap->va_gen = 0;
172 	vap->va_flags = 0;
173 	vap->va_rdev = NODEV;
174 	vap->va_fileid = ap->block;
175 	vap->va_type = sp->a_vp->v_type;
176 	vap->va_mode = adunixprot(ap->adprot) & amp->mask;
177 	if (sp->a_vp->v_type == VDIR) {
178 		vap->va_nlink = 1;	/* XXX bogus, oh well */
179 		vap->va_bytes = amp->bsize;
180 		vap->va_size = amp->bsize;
181 	} else {
182 		/*
183 		 * XXX actually we can track this if we were to walk the list
184 		 * of links if it exists.
185 		 * XXX for now, just set nlink to 2 if this is a hard link
186 		 * to a file, or a file with a hard link.
187 		 */
188 		vap->va_nlink = 1 + (ap->linkto != 0);
189 		/*
190 		 * round up to nearest blocks add number of file list
191 		 * blocks needed and mutiply by number of bytes per block.
192 		 */
193 		fblks = howmany(ap->fsize, amp->dbsize);
194 		fblks += howmany(fblks, ANODENDATBLKENT(ap));
195 		vap->va_bytes = fblks * amp->dbsize;
196 		vap->va_size = ap->fsize;
197 
198 		vap->va_blocksize = amp->dbsize;
199 	}
200 #ifdef ADOSFS_DIAGNOSTIC
201 	printf(" 0)");
202 #endif
203 	return(0);
204 }
205 /*
206  * are things locked??? they need to be to avoid this being
207  * deleted or changed (data block pointer blocks moving about.)
208  */
209 int
210 adosfs_read(void *v)
211 {
212 	struct vop_read_args /* {
213 		struct vnode *a_vp;
214 		struct uio *a_uio;
215 		int a_ioflag;
216 		kauth_cred_t a_cred;
217 	} */ *sp = v;
218 	struct vnode *vp = sp->a_vp;
219 	struct adosfsmount *amp;
220 	struct anode *ap;
221 	struct uio *uio;
222 	struct buf *bp;
223 	daddr_t lbn;
224 	int size, diff, error;
225 	long n, on;
226 
227 #ifdef ADOSFS_DIAGNOSTIC
228 	advopprint(sp);
229 #endif
230 	error = 0;
231 	uio = sp->a_uio;
232 	ap = VTOA(sp->a_vp);
233 	amp = ap->amp;
234 	/*
235 	 * Return EOF for character devices, EIO for others
236 	 */
237 	if (sp->a_vp->v_type != VREG) {
238 		error = EIO;
239 		goto reterr;
240 	}
241 	if (uio->uio_resid == 0)
242 		goto reterr;
243 	if (uio->uio_offset < 0) {
244 		error = EINVAL;
245 		goto reterr;
246 	}
247 
248 	/*
249 	 * to expensive to let general algorithm figure out that
250 	 * we are beyond the file.  Do it now.
251 	 */
252 	if (uio->uio_offset >= ap->fsize)
253 		goto reterr;
254 
255 	/*
256 	 * taken from ufs_read()
257 	 */
258 
259 	if (vp->v_type == VREG && IS_FFS(amp)) {
260 		const int advice = IO_ADV_DECODE(sp->a_ioflag);
261 		error = 0;
262 
263 		while (uio->uio_resid > 0) {
264 			vsize_t bytelen = MIN(ap->fsize - uio->uio_offset,
265 					      uio->uio_resid);
266 
267 			if (bytelen == 0) {
268 				break;
269 			}
270 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
271 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
272 			if (error) {
273 				break;
274 			}
275 		}
276 		goto out;
277 	}
278 
279 	do {
280 		size = amp->dbsize;
281 		lbn = uio->uio_offset / size;
282 		on = uio->uio_offset % size;
283 		n = MIN(size - on, uio->uio_resid);
284 		diff = ap->fsize - uio->uio_offset;
285 		/*
286 		 * check for EOF
287 		 */
288 		if (diff <= 0)
289 			return(0);
290 		if (diff < n)
291 			n = diff;
292 		/*
293 		 * read ahead could possibly be worth something
294 		 * but not much as ados makes little attempt to
295 		 * make things contigous
296 		 */
297 		error = bread(sp->a_vp, lbn, amp->bsize, NOCRED, 0, &bp);
298 		if (error) {
299 			goto reterr;
300 		}
301 		if (!IS_FFS(amp)) {
302 			if (bp->b_resid > 0)
303 				error = EIO; /* OFS needs the complete block */
304 			else if (adoswordn(bp, 0) != BPT_DATA) {
305 #ifdef DIAGNOSTIC
306 				printf("adosfs: bad primary type blk %" PRId64 "\n",
307 				    bp->b_blkno / (amp->bsize / DEV_BSIZE));
308 #endif
309 				error = EINVAL;
310 			} else if (adoscksum(bp, ap->nwords)) {
311 #ifdef DIAGNOSTIC
312 				printf("adosfs: blk %" PRId64 " failed cksum.\n",
313 				    bp->b_blkno / (amp->bsize / DEV_BSIZE));
314 #endif
315 				error = EINVAL;
316 			}
317 		}
318 
319 		if (error) {
320 			brelse(bp, 0);
321 			goto reterr;
322 		}
323 #ifdef ADOSFS_DIAGNOSTIC
324 		printf(" %" PRId64 "+%ld-%" PRId64 "+%ld", lbn, on, lbn, n);
325 #endif
326 		n = MIN(n, size - bp->b_resid);
327 		error = uiomove((char *)bp->b_data + on +
328 				amp->bsize - amp->dbsize, (int)n, uio);
329 		brelse(bp, 0);
330 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
331 
332 out:
333 reterr:
334 #ifdef ADOSFS_DIAGNOSTIC
335 	printf(" %d)", error);
336 #endif
337 	return(error);
338 }
339 
340 int
341 adosfs_write(void *v)
342 {
343 #ifdef ADOSFS_DIAGNOSTIC
344 #if 0
345 	struct vop_write_args /* {
346 		struct vnode *a_vp;
347 		struct uio *a_uio;
348 		int a_ioflag;
349 		kauth_cred_t a_cred;
350 	} */ *sp = v;
351 	advopprint(sp);
352 #endif
353 	printf(" EOPNOTSUPP)");
354 #endif
355 	return(EOPNOTSUPP);
356 }
357 
358 /*
359  * Just call the device strategy routine
360  */
361 int
362 adosfs_strategy(void *v)
363 {
364 	struct vop_strategy_args /* {
365 		struct vnode *a_vp;
366 		struct buf *a_bp;
367 	} */ *sp = v;
368 	struct buf *bp;
369 	struct anode *ap;
370 	struct vnode *vp;
371 	int error;
372 
373 #ifdef ADOSFS_DIAGNOSTIC
374 	advopprint(sp);
375 #endif
376 	bp = sp->a_bp;
377 	if (bp->b_vp == NULL) {
378 		bp->b_error = EIO;
379 		biodone(bp);
380 		error = EIO;
381 		goto reterr;
382 	}
383 	vp = sp->a_vp;
384 	ap = VTOA(vp);
385 	if (bp->b_blkno == bp->b_lblkno) {
386 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
387 		if (error) {
388 			bp->b_flags = error;
389 			biodone(bp);
390 			goto reterr;
391 		}
392 	}
393 	if ((long)bp->b_blkno == -1) {
394 		biodone(bp);
395 		error = 0;
396 		goto reterr;
397 	}
398 	vp = ap->amp->devvp;
399 	error = VOP_STRATEGY(vp, bp);
400 reterr:
401 #ifdef ADOSFS_DIAGNOSTIC
402 	printf(" %d)", error);
403 #endif
404 	return(error);
405 }
406 
407 int
408 adosfs_link(void *v)
409 {
410 	struct vop_link_args /* {
411 		struct vnode *a_dvp;
412 		struct vnode *a_vp;
413 		struct componentname *a_cnp;
414 	} */ *ap = v;
415 
416 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
417 	vput(ap->a_dvp);
418 	return (EROFS);
419 }
420 
421 int
422 adosfs_symlink(void *v)
423 {
424 	struct vop_symlink_v3_args /* {
425 		struct vnode *a_dvp;
426 		struct vnode **a_vpp;
427 		struct componentname *a_cnp;
428 		struct vattr *a_vap;
429 		char *a_target;
430 	} */ *ap = v;
431 
432 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
433 	return (EROFS);
434 }
435 
436 /*
437  * Wait until the vnode has finished changing state.
438  */
439 int
440 adosfs_bmap(void *v)
441 {
442 	struct vop_bmap_args /* {
443 		struct vnode *a_vp;
444 		daddr_t  a_bn;
445 		struct vnode **a_vpp;
446 		daddr_t *a_bnp;
447 		int *a_runp;
448 	} */ *sp = v;
449 	struct anode *ap;
450 	struct buf *flbp;
451 	long nb, flblk, flblkoff, fcnt;
452 	daddr_t *bnp;
453 	daddr_t bn;
454 	int error;
455 
456 #ifdef ADOSFS_DIAGNOSTIC
457 	advopprint(sp);
458 #endif
459 	ap = VTOA(sp->a_vp);
460 	bn = sp->a_bn;
461 	bnp = sp->a_bnp;
462 	if (sp->a_runp) {
463 		*sp->a_runp = 0;
464 	}
465 	error = 0;
466 
467 	if (sp->a_vpp != NULL)
468 		*sp->a_vpp = ap->amp->devvp;
469 	if (bnp == NULL)
470 		goto reterr;
471 	if (bn < 0) {
472 		error = EFBIG;
473 		goto reterr;
474 	}
475 	if (sp->a_vp->v_type != VREG) {
476 		error = EINVAL;
477 		goto reterr;
478 	}
479 
480 	/*
481 	 * walk the chain of file list blocks until we find
482 	 * the one that will yield the block pointer we need.
483 	 */
484 	if (ap->type == AFILE)
485 		nb = ap->block;			/* pointer to ourself */
486 	else if (ap->type == ALFILE)
487 		nb = ap->linkto;		/* pointer to real file */
488 	else {
489 		error = EINVAL;
490 		goto reterr;
491 	}
492 
493 	flblk = bn / ANODENDATBLKENT(ap);
494 	flbp = NULL;
495 
496 	/*
497 	 * check last indirect block cache
498 	 */
499 	if (flblk < ap->lastlindblk)
500 		fcnt = 0;
501 	else {
502 		flblk -= ap->lastlindblk;
503 		fcnt = ap->lastlindblk;
504 		nb = ap->lastindblk;
505 	}
506 	while (flblk >= 0) {
507 		if (flbp)
508 			brelse(flbp, 0);
509 		if (nb == 0) {
510 #ifdef DIAGNOSTIC
511 			printf("adosfs: bad file list chain.\n");
512 #endif
513 			error = EINVAL;
514 			goto reterr;
515 		}
516 		error = bread(ap->amp->devvp, nb * ap->amp->bsize / DEV_BSIZE,
517 			      ap->amp->bsize, NOCRED, 0, &flbp);
518 		if (error) {
519 			goto reterr;
520 		}
521 		if (adoscksum(flbp, ap->nwords)) {
522 #ifdef DIAGNOSTIC
523 			printf("adosfs: blk %ld failed cksum.\n", nb);
524 #endif
525 			brelse(flbp, 0);
526 			error = EINVAL;
527 			goto reterr;
528 		}
529 		/*
530 		 * update last indirect block cache
531 		 */
532 		ap->lastlindblk = fcnt++;
533 		ap->lastindblk = nb;
534 
535 		nb = adoswordn(flbp, ap->nwords - 2);
536 		flblk--;
537 	}
538 	/*
539 	 * calculate offset of block number in table.  The table starts
540 	 * at nwords - 51 and goes to offset 6 or less if indicated by the
541 	 * valid table entries stored at offset ADBI_NBLKTABENT.
542 	 */
543 	flblkoff = bn % ANODENDATBLKENT(ap);
544 	if (flblkoff < adoswordn(flbp, 2 /* ADBI_NBLKTABENT */)) {
545 		flblkoff = (ap->nwords - 51) - flblkoff;
546 		*bnp = adoswordn(flbp, flblkoff) * ap->amp->bsize / DEV_BSIZE;
547 	} else {
548 #ifdef DIAGNOSTIC
549 		printf("flblk offset %ld too large in lblk %ld blk %" PRId64 "\n",
550 		    flblkoff, (long)bn, flbp->b_blkno);
551 #endif
552 		error = EINVAL;
553 	}
554 	brelse(flbp, 0);
555 reterr:
556 #ifdef ADOSFS_DIAGNOSTIC
557 	if (error == 0 && bnp)
558 		printf(" %lld => %lld", (long long)bn, (long long)*bnp);
559 	printf(" %d)\n", error);
560 #endif
561 	return(error);
562 }
563 
564 /*
565  * Print out the contents of a adosfs vnode.
566  */
567 /* ARGSUSED */
568 int
569 adosfs_print(void *v)
570 {
571 #if 0
572 	struct vop_print_args /* {
573 		struct vnode *a_vp;
574 	} */ *sp = v;
575 #endif
576 	return(0);
577 }
578 
579 int
580 adosfs_readdir(void *v)
581 {
582 	struct vop_readdir_args /* {
583 		struct vnode *a_vp;
584 		struct uio *a_uio;
585 		kauth_cred_t a_cred;
586 		int *a_eofflag;
587 		off_t **a_cookies;
588 		int *a_ncookies;
589 	} */ *sp = v;
590 	int error, first, useri, chainc, hashi, scanned;
591 	u_long nextbn;
592 	struct dirent ad, *adp;
593 	struct anode *pap, *ap;
594 	struct vnode *vp;
595 	struct uio *uio = sp->a_uio;
596 	off_t uoff = uio->uio_offset;
597 	off_t *cookies = NULL;
598 	int ncookies = 0;
599 
600 #ifdef ADOSFS_DIAGNOSTIC
601 	advopprint(sp);
602 #endif
603 
604 	if (sp->a_vp->v_type != VDIR) {
605 		error = ENOTDIR;
606 		goto reterr;
607 	}
608 
609 	if (uoff < 0) {
610 		error = EINVAL;
611 		goto reterr;
612 	}
613 
614 	pap = VTOA(sp->a_vp);
615 	adp = &ad;
616 	error = nextbn = hashi = chainc = scanned = 0;
617 	first = useri = uoff / sizeof ad;
618 
619 	/*
620 	 * If offset requested is not on a slot boundary
621 	 */
622 	if (uoff % sizeof ad) {
623 		error = EINVAL;
624 		goto reterr;
625 	}
626 
627 	for (;;) {
628 		if (hashi == pap->ntabent) {
629 			*sp->a_eofflag = 1;
630 			break;
631 		}
632 		if (pap->tab[hashi] == 0) {
633 			hashi++;
634 			continue;
635 		}
636 		if (nextbn == 0)
637 			nextbn = pap->tab[hashi];
638 
639 		/*
640 		 * First determine if we can skip this chain
641 		 */
642 		if (chainc == 0) {
643 			int skip;
644 
645 			skip = useri - scanned;
646 			if (pap->tabi[hashi] > 0 && pap->tabi[hashi] <= skip) {
647 				scanned += pap->tabi[hashi];
648 				hashi++;
649 				nextbn = 0;
650 				continue;
651 			}
652 		}
653 
654 		/*
655 		 * Now [continue to] walk the chain
656 		 */
657 		ap = NULL;
658 		do {
659 			error = VFS_VGET(pap->amp->mp, (ino_t)nextbn, &vp);
660 			if (error)
661 				goto reterr;
662 			ap = VTOA(vp);
663 			scanned++;
664 			chainc++;
665 			nextbn = ap->hashf;
666 
667 			/*
668 			 * check for end of chain.
669 			 */
670 			if (nextbn == 0) {
671 				pap->tabi[hashi] = chainc;
672 				hashi++;
673 				chainc = 0;
674 			} else if (pap->tabi[hashi] <= 0 &&
675 			    -chainc < pap->tabi[hashi])
676 				pap->tabi[hashi] = -chainc;
677 
678 			if (useri >= scanned) {
679 				vput(vp);
680 				ap = NULL;
681 			}
682 		} while (ap == NULL && nextbn != 0);
683 
684 		/*
685 		 * We left the loop but without a result so do main over.
686 		 */
687 		if (ap == NULL)
688 			continue;
689 		/*
690 		 * Fill in dirent record
691 		 */
692 		memset(adp, 0, sizeof *adp);
693 		adp->d_fileno = ap->block;
694 		/*
695 		 * This deserves a function in kern/vfs_subr.c
696 		 */
697 		switch (ATOV(ap)->v_type) {
698 		case VREG:
699 			adp->d_type = DT_REG;
700 			break;
701 		case VDIR:
702 			adp->d_type = DT_DIR;
703 			break;
704 		case VLNK:
705 			adp->d_type = DT_LNK;
706 			break;
707 		default:
708 			adp->d_type = DT_UNKNOWN;
709 			break;
710 		}
711 		adp->d_namlen = strlen(ap->name);
712 		memcpy(adp->d_name, ap->name, adp->d_namlen);
713 		adp->d_reclen = _DIRENT_SIZE(adp);
714 		vput(vp);
715 
716 		if (adp->d_reclen > uio->uio_resid) {
717 			if (useri == first)	/* no room for even one entry */
718 				error = EINVAL;
719 			break;
720 		}
721 		error = uiomove(adp, adp->d_reclen, uio);
722 		if (error)
723 			break;
724 		useri++;
725 	}
726 	ncookies = useri - first;
727 	uio->uio_offset = uoff + ncookies * sizeof ad;
728 reterr:
729 #ifdef ADOSFS_DIAGNOSTIC
730 	printf(" %d)", error);
731 #endif
732 	if (sp->a_ncookies != NULL) {
733 		*sp->a_ncookies = ncookies;
734 		if (!error) {
735 			*sp->a_cookies = cookies =
736 			   malloc(ncookies * sizeof *cookies, M_TEMP, M_WAITOK);
737 
738 			while (ncookies--) {
739 				uoff += sizeof ad;
740 				*cookies++ = uoff;
741 			}
742 		} else
743 			*sp->a_cookies = NULL;
744 	}
745 
746 	return(error);
747 }
748 
749 static int
750 adosfs_check_possible(struct vnode *vp, struct anode *ap, mode_t mode)
751 {
752 
753 	/*
754 	 * Disallow write attempts unless the file is a socket,
755 	 * fifo, or a block or character device resident on the
756 	 * file system.
757 	 */
758 	if (mode & VWRITE) {
759 		switch (vp->v_type) {
760 		case VDIR:
761 		case VLNK:
762 		case VREG:
763 			return (EROFS);
764 		default:
765 			break;
766 		}
767 	}
768 
769 	return 0;
770 }
771 
772 static int
773 adosfs_check_permitted(struct vnode *vp, struct anode *ap, mode_t mode,
774     kauth_cred_t cred)
775 {
776 	mode_t file_mode = adunixprot(ap->adprot) & ap->amp->mask;
777 
778 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
779 	    vp->v_type, file_mode), vp, NULL, genfs_can_access(vp->v_type,
780 	    file_mode, ap->uid, ap->gid, mode, cred));
781 }
782 
783 int
784 adosfs_access(void *v)
785 {
786 	struct vop_access_args /* {
787 		struct vnode *a_vp;
788 		int  a_mode;
789 		kauth_cred_t a_cred;
790 	} */ *sp = v;
791 	struct anode *ap;
792 	struct vnode *vp = sp->a_vp;
793 	int error;
794 
795 #ifdef ADOSFS_DIAGNOSTIC
796 	advopprint(sp);
797 #endif
798 
799 	ap = VTOA(vp);
800 #ifdef DIAGNOSTIC
801 	if (!VOP_ISLOCKED(vp)) {
802 		vprint("adosfs_access: not locked", sp->a_vp);
803 		panic("adosfs_access: not locked");
804 	}
805 #endif
806 
807 	error = adosfs_check_possible(vp, ap, sp->a_mode);
808 	if (error)
809 		return error;
810 
811 	error = adosfs_check_permitted(vp, ap, sp->a_mode, sp->a_cred);
812 
813 #ifdef ADOSFS_DIAGNOSTIC
814 	printf(" %d)", error);
815 #endif
816 	return(error);
817 }
818 
819 int
820 adosfs_readlink(void *v)
821 {
822 	struct vop_readlink_args /* {
823 		struct vnode *a_vp;
824 		struct uio *a_uio;
825 		kauth_cred_t a_cred;
826 	} */ *sp = v;
827 	struct anode *ap;
828 	int error;
829 
830 #ifdef ADOSFS_DIAGNOSTIC
831 	advopprint(sp);
832 #endif
833 	ap = VTOA(sp->a_vp);
834 	error = uiomove(ap->slinkto, strlen(ap->slinkto), sp->a_uio);
835 #ifdef ADOSFS_DIAGNOSTIC
836 	printf(" %d)", error);
837 #endif
838 	return (error);
839 }
840 
841 /*ARGSUSED*/
842 int
843 adosfs_inactive(void *v)
844 {
845 	struct vop_inactive_args /* {
846 		struct vnode *a_vp;
847 		bool *a_recycle;
848 	} */ *sp = v;
849 	struct vnode *vp = sp->a_vp;
850 #ifdef ADOSFS_DIAGNOSTIC
851 	advopprint(sp);
852 #endif
853 	VOP_UNLOCK(vp);
854 	/* XXX this needs to check if file was deleted */
855 	*sp->a_recycle = true;
856 
857 #ifdef ADOSFS_DIAGNOSTIC
858 	printf(" 0)");
859 #endif
860 	return(0);
861 }
862 
863 /*
864  * the kernel wants its vnode back.
865  * no lock needed we are being called from vclean()
866  */
867 int
868 adosfs_reclaim(void *v)
869 {
870 	struct vop_reclaim_args /* {
871 		struct vnode *a_vp;
872 	} */ *sp = v;
873 	struct vnode *vp;
874 	struct anode *ap;
875 
876 #ifdef ADOSFS_DIAGNOSTIC
877 	printf("(reclaim 0)");
878 #endif
879 	vp = sp->a_vp;
880 	ap = VTOA(vp);
881 	LIST_REMOVE(ap, link);
882 	if (vp->v_type == VDIR && ap->tab)
883 		free(ap->tab, M_ANODE);
884 	else if (vp->v_type == VLNK && ap->slinkto)
885 		free(ap->slinkto, M_ANODE);
886 	genfs_node_destroy(vp);
887 	pool_put(&adosfs_node_pool, ap);
888 	vp->v_data = NULL;
889 	return(0);
890 }
891 
892 /*
893  * POSIX pathconf info, grabbed from kern/u fs, probably need to
894  * investigate exactly what each return type means as they are probably
895  * not valid currently
896  */
897 int
898 adosfs_pathconf(void *v)
899 {
900 	struct vop_pathconf_args /* {
901 		struct vnode *a_vp;
902 		int a_name;
903 		register_t *a_retval;
904 	} */ *ap = v;
905 
906 	switch (ap->a_name) {
907 	case _PC_LINK_MAX:
908 		*ap->a_retval = LINK_MAX;
909 		return (0);
910 	case _PC_NAME_MAX:
911 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
912 		return (0);
913 	case _PC_PATH_MAX:
914 		*ap->a_retval = PATH_MAX;
915 		return (0);
916 	case _PC_PIPE_BUF:
917 		*ap->a_retval = PIPE_BUF;
918 		return (0);
919 	case _PC_CHOWN_RESTRICTED:
920 		*ap->a_retval = 1;
921 		return (0);
922 	case _PC_VDISABLE:
923 		*ap->a_retval = _POSIX_VDISABLE;
924 		return (0);
925 	case _PC_SYNC_IO:
926 		*ap->a_retval = 1;
927 		return (0);
928 	case _PC_FILESIZEBITS:
929 		*ap->a_retval = 32;
930 		return (0);
931 	default:
932 		return (EINVAL);
933 	}
934 	/* NOTREACHED */
935 }
936