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