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