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