xref: /netbsd-src/sys/fs/adosfs/advnops.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: advnops.c,v 1.31 2008/12/16 16:18:25 pooka 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.31 2008/12/16 16:18:25 pooka 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	__P((void *));
62 int	adosfs_read	__P((void *));
63 int	adosfs_write	__P((void *));
64 #define	adosfs_fcntl	genfs_fcntl
65 #define	adosfs_ioctl	genfs_enoioctl
66 #define	adosfs_poll	genfs_poll
67 int	adosfs_strategy	__P((void *));
68 int	adosfs_link	__P((void *));
69 int	adosfs_symlink	__P((void *));
70 #define	adosfs_abortop	genfs_abortop
71 int	adosfs_bmap	__P((void *));
72 int	adosfs_print	__P((void *));
73 int	adosfs_readdir	__P((void *));
74 int	adosfs_access	__P((void *));
75 int	adosfs_readlink	__P((void *));
76 int	adosfs_inactive	__P((void *));
77 int	adosfs_reclaim	__P((void *));
78 int	adosfs_pathconf	__P((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(v)
146 	void *v;
147 {
148 	struct vop_getattr_args /* {
149 		struct vnode *a_vp;
150 		struct vattr *a_vap;
151 		kauth_cred_t a_cred;
152 	} */ *sp = v;
153 	struct vattr *vap;
154 	struct adosfsmount *amp;
155 	struct anode *ap;
156 	u_long fblks;
157 
158 #ifdef ADOSFS_DIAGNOSTIC
159 	advopprint(sp);
160 #endif
161 	vap = sp->a_vap;
162 	ap = VTOA(sp->a_vp);
163 	amp = ap->amp;
164 	vattr_null(vap);
165 	vap->va_uid = ap->uid;
166 	vap->va_gid = ap->gid;
167 	vap->va_fsid = sp->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
168 	vap->va_atime.tv_sec = vap->va_mtime.tv_sec = vap->va_ctime.tv_sec =
169 		ap->mtime.days * 24 * 60 * 60 + ap->mtime.mins * 60 +
170 		ap->mtime.ticks / 50 + (8 * 365 + 2) * 24 * 60 * 60;
171 	vap->va_atime.tv_nsec = vap->va_mtime.tv_nsec = vap->va_ctime.tv_nsec = 0;
172 	vap->va_gen = 0;
173 	vap->va_flags = 0;
174 	vap->va_rdev = NODEV;
175 	vap->va_fileid = ap->block;
176 	vap->va_type = sp->a_vp->v_type;
177 	vap->va_mode = adunixprot(ap->adprot) & amp->mask;
178 	if (sp->a_vp->v_type == VDIR) {
179 		vap->va_nlink = 1;	/* XXX bogus, oh well */
180 		vap->va_bytes = amp->bsize;
181 		vap->va_size = amp->bsize;
182 	} else {
183 		/*
184 		 * XXX actually we can track this if we were to walk the list
185 		 * of links if it exists.
186 		 * XXX for now, just set nlink to 2 if this is a hard link
187 		 * to a file, or a file with a hard link.
188 		 */
189 		vap->va_nlink = 1 + (ap->linkto != 0);
190 		/*
191 		 * round up to nearest blocks add number of file list
192 		 * blocks needed and mutiply by number of bytes per block.
193 		 */
194 		fblks = howmany(ap->fsize, amp->dbsize);
195 		fblks += howmany(fblks, ANODENDATBLKENT(ap));
196 		vap->va_bytes = fblks * amp->dbsize;
197 		vap->va_size = ap->fsize;
198 
199 		vap->va_blocksize = amp->dbsize;
200 	}
201 #ifdef ADOSFS_DIAGNOSTIC
202 	printf(" 0)");
203 #endif
204 	return(0);
205 }
206 /*
207  * are things locked??? they need to be to avoid this being
208  * deleted or changed (data block pointer blocks moving about.)
209  */
210 int
211 adosfs_read(v)
212 	void *v;
213 {
214 	struct vop_read_args /* {
215 		struct vnode *a_vp;
216 		struct uio *a_uio;
217 		int a_ioflag;
218 		kauth_cred_t a_cred;
219 	} */ *sp = v;
220 	struct vnode *vp = sp->a_vp;
221 	struct adosfsmount *amp;
222 	struct anode *ap;
223 	struct uio *uio;
224 	struct buf *bp;
225 	daddr_t lbn;
226 	int size, diff, error;
227 	long n, on;
228 
229 #ifdef ADOSFS_DIAGNOSTIC
230 	advopprint(sp);
231 #endif
232 	error = 0;
233 	uio = sp->a_uio;
234 	ap = VTOA(sp->a_vp);
235 	amp = ap->amp;
236 	/*
237 	 * Return EOF for character devices, EIO for others
238 	 */
239 	if (sp->a_vp->v_type != VREG) {
240 		error = EIO;
241 		goto reterr;
242 	}
243 	if (uio->uio_resid == 0)
244 		goto reterr;
245 	if (uio->uio_offset < 0) {
246 		error = EINVAL;
247 		goto reterr;
248 	}
249 
250 	/*
251 	 * to expensive to let general algorithm figure out that
252 	 * we are beyond the file.  Do it now.
253 	 */
254 	if (uio->uio_offset >= ap->fsize)
255 		goto reterr;
256 
257 	/*
258 	 * taken from ufs_read()
259 	 */
260 
261 	if (vp->v_type == VREG && IS_FFS(amp)) {
262 		const int advice = IO_ADV_DECODE(sp->a_ioflag);
263 		error = 0;
264 
265 		while (uio->uio_resid > 0) {
266 			vsize_t bytelen = MIN(ap->fsize - uio->uio_offset,
267 					      uio->uio_resid);
268 
269 			if (bytelen == 0) {
270 				break;
271 			}
272 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
273 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
274 			if (error) {
275 				break;
276 			}
277 		}
278 		goto out;
279 	}
280 
281 	do {
282 		size = amp->dbsize;
283 		lbn = uio->uio_offset / size;
284 		on = uio->uio_offset % size;
285 		n = MIN(size - on, uio->uio_resid);
286 		diff = ap->fsize - uio->uio_offset;
287 		/*
288 		 * check for EOF
289 		 */
290 		if (diff <= 0)
291 			return(0);
292 		if (diff < n)
293 			n = diff;
294 		/*
295 		 * read ahead could possibly be worth something
296 		 * but not much as ados makes little attempt to
297 		 * make things contigous
298 		 */
299 		error = bread(sp->a_vp, lbn, amp->bsize, NOCRED, 0, &bp);
300 		if (error) {
301 			brelse(bp, 0);
302 			goto reterr;
303 		}
304 		if (!IS_FFS(amp)) {
305 			if (bp->b_resid > 0)
306 				error = EIO; /* OFS needs the complete block */
307 			else if (adoswordn(bp, 0) != BPT_DATA) {
308 #ifdef DIAGNOSTIC
309 				printf("adosfs: bad primary type blk %" PRId64 "\n",
310 				    bp->b_blkno / (amp->bsize / DEV_BSIZE));
311 #endif
312 				error = EINVAL;
313 			} else if (adoscksum(bp, ap->nwords)) {
314 #ifdef DIAGNOSTIC
315 				printf("adosfs: blk %" PRId64 " failed cksum.\n",
316 				    bp->b_blkno / (amp->bsize / DEV_BSIZE));
317 #endif
318 				error = EINVAL;
319 			}
320 		}
321 
322 		if (error) {
323 			brelse(bp, 0);
324 			goto reterr;
325 		}
326 #ifdef ADOSFS_DIAGNOSTIC
327 		printf(" %" PRId64 "+%ld-%" PRId64 "+%ld", lbn, on, lbn, n);
328 #endif
329 		n = MIN(n, size - bp->b_resid);
330 		error = uiomove((char *)bp->b_data + on +
331 				amp->bsize - amp->dbsize, (int)n, uio);
332 		brelse(bp, 0);
333 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
334 
335 out:
336 reterr:
337 #ifdef ADOSFS_DIAGNOSTIC
338 	printf(" %d)", error);
339 #endif
340 	return(error);
341 }
342 
343 int
344 adosfs_write(v)
345 	void *v;
346 {
347 #ifdef ADOSFS_DIAGNOSTIC
348 	struct vop_write_args /* {
349 		struct vnode *a_vp;
350 		struct uio *a_uio;
351 		int a_ioflag;
352 		kauth_cred_t a_cred;
353 	} */ *sp = v;
354 	advopprint(sp);
355 	printf(" EOPNOTSUPP)");
356 #endif
357 	return(EOPNOTSUPP);
358 }
359 
360 /*
361  * Just call the device strategy routine
362  */
363 int
364 adosfs_strategy(v)
365 	void *v;
366 {
367 	struct vop_strategy_args /* {
368 		struct vnode *a_vp;
369 		struct buf *a_bp;
370 	} */ *sp = v;
371 	struct buf *bp;
372 	struct anode *ap;
373 	struct vnode *vp;
374 	int error;
375 
376 #ifdef ADOSFS_DIAGNOSTIC
377 	advopprint(sp);
378 #endif
379 	bp = sp->a_bp;
380 	if (bp->b_vp == NULL) {
381 		bp->b_error = EIO;
382 		biodone(bp);
383 		error = EIO;
384 		goto reterr;
385 	}
386 	vp = sp->a_vp;
387 	ap = VTOA(vp);
388 	if (bp->b_blkno == bp->b_lblkno) {
389 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
390 		if (error) {
391 			bp->b_flags = error;
392 			biodone(bp);
393 			goto reterr;
394 		}
395 	}
396 	if ((long)bp->b_blkno == -1) {
397 		biodone(bp);
398 		error = 0;
399 		goto reterr;
400 	}
401 	vp = ap->amp->devvp;
402 	error = VOP_STRATEGY(vp, bp);
403 reterr:
404 #ifdef ADOSFS_DIAGNOSTIC
405 	printf(" %d)", error);
406 #endif
407 	return(error);
408 }
409 
410 int
411 adosfs_link(v)
412 	void *v;
413 {
414 	struct vop_link_args /* {
415 		struct vnode *a_dvp;
416 		struct vnode *a_vp;
417 		struct componentname *a_cnp;
418 	} */ *ap = v;
419 
420 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
421 	vput(ap->a_dvp);
422 	return (EROFS);
423 }
424 
425 int
426 adosfs_symlink(v)
427 	void *v;
428 {
429 	struct vop_symlink_args /* {
430 		struct vnode *a_dvp;
431 		struct vnode **a_vpp;
432 		struct componentname *a_cnp;
433 		struct vattr *a_vap;
434 		char *a_target;
435 	} */ *ap = v;
436 
437 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
438 	vput(ap->a_dvp);
439 	return (EROFS);
440 }
441 
442 /*
443  * Wait until the vnode has finished changing state.
444  */
445 int
446 adosfs_bmap(v)
447 	void *v;
448 {
449 	struct vop_bmap_args /* {
450 		struct vnode *a_vp;
451 		daddr_t  a_bn;
452 		struct vnode **a_vpp;
453 		daddr_t *a_bnp;
454 		int *a_runp;
455 	} */ *sp = v;
456 	struct anode *ap;
457 	struct buf *flbp;
458 	long nb, flblk, flblkoff, fcnt;
459 	daddr_t *bnp;
460 	daddr_t bn;
461 	int error;
462 
463 #ifdef ADOSFS_DIAGNOSTIC
464 	advopprint(sp);
465 #endif
466 	ap = VTOA(sp->a_vp);
467 	bn = sp->a_bn;
468 	bnp = sp->a_bnp;
469 	if (sp->a_runp) {
470 		*sp->a_runp = 0;
471 	}
472 	error = 0;
473 
474 	if (sp->a_vpp != NULL)
475 		*sp->a_vpp = ap->amp->devvp;
476 	if (bnp == NULL)
477 		goto reterr;
478 	if (bn < 0) {
479 		error = EFBIG;
480 		goto reterr;
481 	}
482 	if (sp->a_vp->v_type != VREG) {
483 		error = EINVAL;
484 		goto reterr;
485 	}
486 
487 	/*
488 	 * walk the chain of file list blocks until we find
489 	 * the one that will yield the block pointer we need.
490 	 */
491 	if (ap->type == AFILE)
492 		nb = ap->block;			/* pointer to ourself */
493 	else if (ap->type == ALFILE)
494 		nb = ap->linkto;		/* pointer to real file */
495 	else {
496 		error = EINVAL;
497 		goto reterr;
498 	}
499 
500 	flblk = bn / ANODENDATBLKENT(ap);
501 	flbp = NULL;
502 
503 	/*
504 	 * check last indirect block cache
505 	 */
506 	if (flblk < ap->lastlindblk)
507 		fcnt = 0;
508 	else {
509 		flblk -= ap->lastlindblk;
510 		fcnt = ap->lastlindblk;
511 		nb = ap->lastindblk;
512 	}
513 	while (flblk >= 0) {
514 		if (flbp)
515 			brelse(flbp, 0);
516 		if (nb == 0) {
517 #ifdef DIAGNOSTIC
518 			printf("adosfs: bad file list chain.\n");
519 #endif
520 			error = EINVAL;
521 			goto reterr;
522 		}
523 		error = bread(ap->amp->devvp, nb * ap->amp->bsize / DEV_BSIZE,
524 			      ap->amp->bsize, NOCRED, 0, &flbp);
525 		if (error) {
526 			brelse(flbp, 0);
527 			goto reterr;
528 		}
529 		if (adoscksum(flbp, ap->nwords)) {
530 #ifdef DIAGNOSTIC
531 			printf("adosfs: blk %ld failed cksum.\n", nb);
532 #endif
533 			brelse(flbp, 0);
534 			error = EINVAL;
535 			goto reterr;
536 		}
537 		/*
538 		 * update last indirect block cache
539 		 */
540 		ap->lastlindblk = fcnt++;
541 		ap->lastindblk = nb;
542 
543 		nb = adoswordn(flbp, ap->nwords - 2);
544 		flblk--;
545 	}
546 	/*
547 	 * calculate offset of block number in table.  The table starts
548 	 * at nwords - 51 and goes to offset 6 or less if indicated by the
549 	 * valid table entries stored at offset ADBI_NBLKTABENT.
550 	 */
551 	flblkoff = bn % ANODENDATBLKENT(ap);
552 	if (flblkoff < adoswordn(flbp, 2 /* ADBI_NBLKTABENT */)) {
553 		flblkoff = (ap->nwords - 51) - flblkoff;
554 		*bnp = adoswordn(flbp, flblkoff) * ap->amp->bsize / DEV_BSIZE;
555 	} else {
556 #ifdef DIAGNOSTIC
557 		printf("flblk offset %ld too large in lblk %ld blk %" PRId64 "\n",
558 		    flblkoff, (long)bn, flbp->b_blkno);
559 #endif
560 		error = EINVAL;
561 	}
562 	brelse(flbp, 0);
563 reterr:
564 #ifdef ADOSFS_DIAGNOSTIC
565 	if (error == 0 && bnp)
566 		printf(" %lld => %lld", (long long)bn, (long long)*bnp);
567 	printf(" %d)\n", error);
568 #endif
569 	return(error);
570 }
571 
572 /*
573  * Print out the contents of a adosfs vnode.
574  */
575 /* ARGSUSED */
576 int
577 adosfs_print(v)
578 	void *v;
579 {
580 #if 0
581 	struct vop_print_args /* {
582 		struct vnode *a_vp;
583 	} */ *sp = v;
584 #endif
585 	return(0);
586 }
587 
588 int
589 adosfs_readdir(v)
590 	void *v;
591 {
592 	struct vop_readdir_args /* {
593 		struct vnode *a_vp;
594 		struct uio *a_uio;
595 		kauth_cred_t a_cred;
596 		int *a_eofflag;
597 		off_t **a_cookies;
598 		int *a_ncookies;
599 	} */ *sp = v;
600 	int error, first, useri, chainc, hashi, scanned;
601 	u_long nextbn;
602 	struct dirent ad, *adp;
603 	struct anode *pap, *ap;
604 	struct vnode *vp;
605 	struct uio *uio = sp->a_uio;
606 	off_t uoff = uio->uio_offset;
607 	off_t *cookies = NULL;
608 	int ncookies = 0;
609 
610 #ifdef ADOSFS_DIAGNOSTIC
611 	advopprint(sp);
612 #endif
613 
614 	if (sp->a_vp->v_type != VDIR) {
615 		error = ENOTDIR;
616 		goto reterr;
617 	}
618 
619 	if (uoff < 0) {
620 		error = EINVAL;
621 		goto reterr;
622 	}
623 
624 	pap = VTOA(sp->a_vp);
625 	adp = &ad;
626 	error = nextbn = hashi = chainc = scanned = 0;
627 	first = useri = uoff / sizeof ad;
628 
629 	/*
630 	 * If offset requested is not on a slot boundary
631 	 */
632 	if (uoff % sizeof ad) {
633 		error = EINVAL;
634 		goto reterr;
635 	}
636 
637 	for (;;) {
638 		if (hashi == pap->ntabent) {
639 			*sp->a_eofflag = 1;
640 			break;
641 		}
642 		if (pap->tab[hashi] == 0) {
643 			hashi++;
644 			continue;
645 		}
646 		if (nextbn == 0)
647 			nextbn = pap->tab[hashi];
648 
649 		/*
650 		 * First determine if we can skip this chain
651 		 */
652 		if (chainc == 0) {
653 			int skip;
654 
655 			skip = useri - scanned;
656 			if (pap->tabi[hashi] > 0 && pap->tabi[hashi] <= skip) {
657 				scanned += pap->tabi[hashi];
658 				hashi++;
659 				nextbn = 0;
660 				continue;
661 			}
662 		}
663 
664 		/*
665 		 * Now [continue to] walk the chain
666 		 */
667 		ap = NULL;
668 		do {
669 			error = VFS_VGET(pap->amp->mp, (ino_t)nextbn, &vp);
670 			if (error)
671 				goto reterr;
672 			ap = VTOA(vp);
673 			scanned++;
674 			chainc++;
675 			nextbn = ap->hashf;
676 
677 			/*
678 			 * check for end of chain.
679 			 */
680 			if (nextbn == 0) {
681 				pap->tabi[hashi] = chainc;
682 				hashi++;
683 				chainc = 0;
684 			} else if (pap->tabi[hashi] <= 0 &&
685 			    -chainc < pap->tabi[hashi])
686 				pap->tabi[hashi] = -chainc;
687 
688 			if (useri >= scanned) {
689 				vput(vp);
690 				ap = NULL;
691 			}
692 		} while (ap == NULL && nextbn != 0);
693 
694 		/*
695 		 * We left the loop but without a result so do main over.
696 		 */
697 		if (ap == NULL)
698 			continue;
699 		/*
700 		 * Fill in dirent record
701 		 */
702 		memset(adp, 0, sizeof *adp);
703 		adp->d_fileno = ap->block;
704 		/*
705 		 * This deserves a function in kern/vfs_subr.c
706 		 */
707 		switch (ATOV(ap)->v_type) {
708 		case VREG:
709 			adp->d_type = DT_REG;
710 			break;
711 		case VDIR:
712 			adp->d_type = DT_DIR;
713 			break;
714 		case VLNK:
715 			adp->d_type = DT_LNK;
716 			break;
717 		default:
718 			adp->d_type = DT_UNKNOWN;
719 			break;
720 		}
721 		adp->d_namlen = strlen(ap->name);
722 		memcpy(adp->d_name, ap->name, adp->d_namlen);
723 		adp->d_reclen = _DIRENT_SIZE(adp);
724 		vput(vp);
725 
726 		if (adp->d_reclen > uio->uio_resid) {
727 			if (useri == first)	/* no room for even one entry */
728 				error = EINVAL;
729 			break;
730 		}
731 		error = uiomove(adp, adp->d_reclen, uio);
732 		if (error)
733 			break;
734 		useri++;
735 	}
736 	ncookies = useri - first;
737 	uio->uio_offset = uoff + ncookies * sizeof ad;
738 reterr:
739 #ifdef ADOSFS_DIAGNOSTIC
740 	printf(" %d)", error);
741 #endif
742 	if (sp->a_ncookies != NULL) {
743 		*sp->a_ncookies = ncookies;
744 		if (!error) {
745 			*sp->a_cookies = cookies =
746 			   malloc(ncookies * sizeof *cookies, M_TEMP, M_WAITOK);
747 
748 			while (ncookies--) {
749 				uoff += sizeof ad;
750 				*cookies++ = uoff;
751 			}
752 		} else
753 			*sp->a_cookies = NULL;
754 	}
755 
756 	return(error);
757 }
758 
759 
760 int
761 adosfs_access(v)
762 	void *v;
763 {
764 	struct vop_access_args /* {
765 		struct vnode *a_vp;
766 		int  a_mode;
767 		kauth_cred_t a_cred;
768 	} */ *sp = v;
769 	struct anode *ap;
770 	struct vnode *vp = sp->a_vp;
771 	int error;
772 
773 #ifdef ADOSFS_DIAGNOSTIC
774 	advopprint(sp);
775 #endif
776 
777 	ap = VTOA(vp);
778 #ifdef DIAGNOSTIC
779 	if (!VOP_ISLOCKED(vp)) {
780 		vprint("adosfs_access: not locked", sp->a_vp);
781 		panic("adosfs_access: not locked");
782 	}
783 #endif
784 	/*
785 	 * Disallow write attempts unless the file is a socket,
786 	 * fifo, or a block or character device resident on the
787 	 * file system.
788 	 */
789 	if (sp->a_mode & VWRITE) {
790 		switch (vp->v_type) {
791 		case VDIR:
792 		case VLNK:
793 		case VREG:
794 			return (EROFS);
795 		default:
796 			break;
797 		}
798 	}
799 	error = vaccess(sp->a_vp->v_type, adunixprot(ap->adprot) & ap->amp->mask,
800 	    ap->uid, ap->gid, sp->a_mode, sp->a_cred);
801 #ifdef ADOSFS_DIAGNOSTIC
802 	printf(" %d)", error);
803 #endif
804 	return(error);
805 }
806 
807 int
808 adosfs_readlink(v)
809 	void *v;
810 {
811 	struct vop_readlink_args /* {
812 		struct vnode *a_vp;
813 		struct uio *a_uio;
814 		kauth_cred_t a_cred;
815 	} */ *sp = v;
816 	struct anode *ap;
817 	int error;
818 
819 #ifdef ADOSFS_DIAGNOSTIC
820 	advopprint(sp);
821 #endif
822 	ap = VTOA(sp->a_vp);
823 	error = uiomove(ap->slinkto, strlen(ap->slinkto), sp->a_uio);
824 #ifdef ADOSFS_DIAGNOSTIC
825 	printf(" %d)", error);
826 #endif
827 	return (error);
828 }
829 
830 /*ARGSUSED*/
831 int
832 adosfs_inactive(v)
833 	void *v;
834 {
835 	struct vop_inactive_args /* {
836 		struct vnode *a_vp;
837 		bool *a_recycle;
838 	} */ *sp = v;
839 	struct vnode *vp = sp->a_vp;
840 #ifdef ADOSFS_DIAGNOSTIC
841 	advopprint(sp);
842 #endif
843 	VOP_UNLOCK(vp, 0);
844 	/* XXX this needs to check if file was deleted */
845 	*sp->a_recycle = true;
846 
847 #ifdef ADOSFS_DIAGNOSTIC
848 	printf(" 0)");
849 #endif
850 	return(0);
851 }
852 
853 /*
854  * the kernel wants its vnode back.
855  * no lock needed we are being called from vclean()
856  */
857 int
858 adosfs_reclaim(v)
859 	void *v;
860 {
861 	struct vop_reclaim_args /* {
862 		struct vnode *a_vp;
863 	} */ *sp = v;
864 	struct vnode *vp;
865 	struct anode *ap;
866 
867 #ifdef ADOSFS_DIAGNOSTIC
868 	printf("(reclaim 0)");
869 #endif
870 	vp = sp->a_vp;
871 	ap = VTOA(vp);
872 	LIST_REMOVE(ap, link);
873 	cache_purge(vp);
874 	if (vp->v_type == VDIR && ap->tab)
875 		free(ap->tab, M_ANODE);
876 	else if (vp->v_type == VLNK && ap->slinkto)
877 		free(ap->slinkto, M_ANODE);
878 	genfs_node_destroy(vp);
879 	pool_put(&adosfs_node_pool, ap);
880 	vp->v_data = NULL;
881 	return(0);
882 }
883 
884 /*
885  * POSIX pathconf info, grabbed from kern/u fs, probably need to
886  * investigate exactly what each return type means as they are probably
887  * not valid currently
888  */
889 int
890 adosfs_pathconf(v)
891 	void *v;
892 {
893 	struct vop_pathconf_args /* {
894 		struct vnode *a_vp;
895 		int a_name;
896 		register_t *a_retval;
897 	} */ *ap = v;
898 
899 	switch (ap->a_name) {
900 	case _PC_LINK_MAX:
901 		*ap->a_retval = LINK_MAX;
902 		return (0);
903 	case _PC_NAME_MAX:
904 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
905 		return (0);
906 	case _PC_PATH_MAX:
907 		*ap->a_retval = PATH_MAX;
908 		return (0);
909 	case _PC_PIPE_BUF:
910 		*ap->a_retval = PIPE_BUF;
911 		return (0);
912 	case _PC_CHOWN_RESTRICTED:
913 		*ap->a_retval = 1;
914 		return (0);
915 	case _PC_VDISABLE:
916 		*ap->a_retval = _POSIX_VDISABLE;
917 		return (0);
918 	case _PC_SYNC_IO:
919 		*ap->a_retval = 1;
920 		return (0);
921 	case _PC_FILESIZEBITS:
922 		*ap->a_retval = 32;
923 		return (0);
924 	default:
925 		return (EINVAL);
926 	}
927 	/* NOTREACHED */
928 }
929