xref: /netbsd-src/sys/fs/adosfs/advfsops.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: advfsops.c,v 1.4 2003/04/16 21:44:18 christos 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: advfsops.c,v 1.4 2003/04/16 21:44:18 christos Exp $");
36 
37 #if defined(_KERNEL_OPT)
38 #include "opt_compat_netbsd.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/proc.h>
46 #include <sys/time.h>
47 #include <sys/malloc.h>
48 #include <sys/pool.h>
49 #include <sys/disklabel.h>
50 #include <miscfs/specfs/specdev.h> /* XXX */
51 #include <sys/fcntl.h>
52 #include <sys/namei.h>
53 #include <sys/ioctl.h>
54 #include <sys/queue.h>
55 #include <sys/buf.h>
56 #include <sys/conf.h>
57 #include <fs/adosfs/adosfs.h>
58 
59 void adosfs_init __P((void));
60 void adosfs_reinit __P((void));
61 void adosfs_done __P((void));
62 int adosfs_mount __P((struct mount *, const char *, void *, struct nameidata *,
63 		      struct proc *));
64 int adosfs_start __P((struct mount *, int, struct proc *));
65 int adosfs_unmount __P((struct mount *, int, struct proc *));
66 int adosfs_root __P((struct mount *, struct vnode **));
67 int adosfs_quotactl __P((struct mount *, int, uid_t, caddr_t, struct proc *));
68 int adosfs_statfs __P((struct mount *, struct statfs *, struct proc *));
69 int adosfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
70 int adosfs_vget __P((struct mount *, ino_t, struct vnode **));
71 int adosfs_fhtovp __P((struct mount *, struct fid *, struct vnode **));
72 int adosfs_checkexp __P((struct mount *, struct mbuf *, int *,
73 		       struct ucred **));
74 int adosfs_vptofh __P((struct vnode *, struct fid *));
75 
76 int adosfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
77 int adosfs_loadbitmap __P((struct adosfsmount *));
78 int adosfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
79 			struct proc *));
80 
81 struct simplelock adosfs_hashlock;
82 
83 struct pool adosfs_node_pool;
84 
85 MALLOC_DEFINE(M_ADOSFSMNT, "adosfs mount", "adosfs mount structures");
86 MALLOC_DEFINE(M_ANODE, "adosfs anode", "adosfs anode structures and tables");
87 MALLOC_DEFINE(M_ADOSFSBITMAP, "adosfs bitmap", "adosfs bitmap");
88 
89 struct genfs_ops adosfs_genfsops = {
90 	genfs_size,
91 };
92 
93 int (**adosfs_vnodeop_p) __P((void *));
94 
95 int
96 adosfs_mount(mp, path, data, ndp, p)
97 	struct mount *mp;
98 	const char *path;
99 	void *data;
100 	struct nameidata *ndp;
101 	struct proc *p;
102 {
103 	struct vnode *devvp;
104 	struct adosfs_args args;
105 	struct adosfsmount *amp;
106 	int error;
107 	mode_t accessmode;
108 
109 	if (mp->mnt_flag & MNT_GETARGS) {
110 		amp = VFSTOADOSFS(mp);
111 		if (amp == NULL)
112 			return EIO;
113 		args.uid = amp->uid;
114 		args.gid = amp->gid;
115 		args.mask = amp->mask;
116 		args.fspec = NULL;
117 		vfs_showexport(mp, &args.export, &amp->export);
118 		return copyout(&args, data, sizeof(args));
119 	}
120 	error = copyin(data, &args, sizeof(struct adosfs_args));
121 	if (error)
122 		return(error);
123 
124 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
125 		return (EROFS);
126 	/*
127 	 * If updating, check whether changing from read-only to
128 	 * read/write; if there is no device name, that's all we do.
129 	 */
130 	if (mp->mnt_flag & MNT_UPDATE) {
131 		amp = VFSTOADOSFS(mp);
132 		if (args.fspec == 0)
133 			return (vfs_export(mp, &amp->export, &args.export));
134 	}
135 	/*
136 	 * Not an update, or updating the name: look up the name
137 	 * and verify that it refers to a sensible block device.
138 	 */
139 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
140 	if ((error = namei(ndp)) != 0)
141 		return (error);
142 	devvp = ndp->ni_vp;
143 
144 	if (devvp->v_type != VBLK) {
145 		vrele(devvp);
146 		return (ENOTBLK);
147 	}
148 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
149 		vrele(devvp);
150 		return (ENXIO);
151 	}
152 	/*
153 	 * If mount by non-root, then verify that user has necessary
154 	 * permissions on the device.
155 	 */
156 	if (p->p_ucred->cr_uid != 0) {
157 		accessmode = VREAD;
158 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
159 			accessmode |= VWRITE;
160 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
161 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
162 		if (error) {
163 			vput(devvp);
164 			return (error);
165 		}
166 		VOP_UNLOCK(devvp, 0);
167 	}
168 /* MNT_UPDATE? */
169 	if ((error = adosfs_mountfs(devvp, mp, p)) != 0) {
170 		vrele(devvp);
171 		return (error);
172 	}
173 	amp = VFSTOADOSFS(mp);
174 	amp->uid = args.uid;
175 	amp->gid = args.gid;
176 	amp->mask = args.mask;
177 	return set_statfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
178 	    mp, p);
179 }
180 
181 int
182 adosfs_mountfs(devvp, mp, p)
183 	struct vnode *devvp;
184 	struct mount *mp;
185 	struct proc *p;
186 {
187 	struct disklabel dl;
188 	struct partition *parp;
189 	struct adosfsmount *amp;
190 	struct buf *bp;
191 	struct vnode *rvp;
192 	int error, part, i;
193 
194 	part = DISKPART(devvp->v_rdev);
195 	amp = NULL;
196 
197 	/*
198 	 * Disallow multiple mounts of the same device.
199 	 * Disallow mounting of a device that is currently in use
200 	 * (except for root, which might share swap device for miniroot).
201 	 * Flush out any old buffers remaining from a previous use.
202 	 */
203 	if ((error = vfs_mountedon(devvp)) != 0)
204 		return (error);
205 	if (vcount(devvp) > 1 && devvp != rootvp)
206 		return (EBUSY);
207 	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
208 		return (error);
209 
210 	/*
211 	 * open blkdev and read root block
212 	 */
213 	if ((error = VOP_OPEN(devvp, FREAD, NOCRED, p)) != 0)
214 		return (error);
215 	error = VOP_IOCTL(devvp, DIOCGDINFO, &dl, FREAD, NOCRED, p);
216 	if (error)
217 		goto fail;
218 
219 	parp = &dl.d_partitions[part];
220 	amp = malloc(sizeof(struct adosfsmount), M_ADOSFSMNT, M_WAITOK);
221 	memset((char *)amp, 0, (u_long)sizeof(struct adosfsmount));
222 	amp->mp = mp;
223 	if (dl.d_type == DTYPE_FLOPPY) {
224 		amp->bsize = dl.d_secsize;
225 		amp->secsperblk = 1;
226 	}
227 	else {
228 		amp->bsize = parp->p_fsize * parp->p_frag;
229 		amp->secsperblk = parp->p_frag;
230 	}
231 
232 	/* invalid fs ? */
233 	if (amp->secsperblk == 0) {
234 		error = EINVAL;
235 		goto fail;
236 	}
237 
238 	bp = NULL;
239 	if ((error = bread(devvp, (daddr_t)BBOFF,
240 			   amp->bsize, NOCRED, &bp)) != 0) {
241 		brelse(bp);
242 		goto fail;
243 	}
244 	amp->dostype = adoswordn(bp, 0);
245 	brelse(bp);
246 
247 	/* basic sanity checks */
248 	if (amp->dostype < 0x444f5300 || amp->dostype > 0x444f5305) {
249 		error = EINVAL;
250 		goto fail;
251 	}
252 
253 	amp->rootb = (parp->p_size / amp->secsperblk - 1 + parp->p_cpg) >> 1;
254 	amp->numblks = parp->p_size / amp->secsperblk - parp->p_cpg;
255 
256 	amp->nwords = amp->bsize >> 2;
257 	amp->dbsize = amp->bsize - (IS_FFS(amp) ? 0 : OFS_DATA_OFFSET);
258 	amp->devvp = devvp;
259 
260 	mp->mnt_data = amp;
261         mp->mnt_stat.f_fsid.val[0] = (long)devvp->v_rdev;
262         mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_ADOSFS);
263 	mp->mnt_fs_bshift = ffs(amp->bsize) - 1;
264 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
265 	mp->mnt_flag |= MNT_LOCAL;
266 
267 	/*
268 	 * init anode table.
269 	 */
270 	for (i = 0; i < ANODEHASHSZ; i++)
271 		LIST_INIT(&amp->anodetab[i]);
272 
273 	/*
274 	 * get the root anode, if not a valid fs this will fail.
275 	 */
276 	if ((error = VFS_ROOT(mp, &rvp)) != 0)
277 		goto fail;
278 	/* allocate and load bitmap, set free space */
279 	amp->bitmap = malloc(((amp->numblks + 31) / 32) * sizeof(*amp->bitmap),
280 	    M_ADOSFSBITMAP, M_WAITOK);
281 	if (amp->bitmap)
282 		adosfs_loadbitmap(amp);
283 	if (mp->mnt_flag & MNT_RDONLY && amp->bitmap) {
284 		/*
285 		 * Don't need the bitmap any more if it's read-only.
286 		 */
287 		free(amp->bitmap, M_ADOSFSBITMAP);
288 		amp->bitmap = NULL;
289 	}
290 	vput(rvp);
291 
292 	return(0);
293 
294 fail:
295 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
296 	(void) VOP_CLOSE(devvp, FREAD, NOCRED, p);
297 	VOP_UNLOCK(devvp, 0);
298 	if (amp && amp->bitmap)
299 		free(amp->bitmap, M_ADOSFSBITMAP);
300 	if (amp)
301 		free(amp, M_ADOSFSMNT);
302 	return (error);
303 }
304 
305 int
306 adosfs_start(mp, flags, p)
307 	struct mount *mp;
308 	int flags;
309 	struct proc *p;
310 {
311 
312 	return (0);
313 }
314 
315 int
316 adosfs_unmount(mp, mntflags, p)
317 	struct mount *mp;
318 	int mntflags;
319 	struct proc *p;
320 {
321 	struct adosfsmount *amp;
322 	int error, flags;
323 
324 	flags = 0;
325 	if (mntflags & MNT_FORCE)
326 		flags |= FORCECLOSE;
327 	if ((error = vflush(mp, NULLVP, flags)) != 0)
328 		return (error);
329 	amp = VFSTOADOSFS(mp);
330 	if (amp->devvp->v_type != VBAD)
331 		amp->devvp->v_specmountpoint = NULL;
332 	vn_lock(amp->devvp, LK_EXCLUSIVE | LK_RETRY);
333 	error = VOP_CLOSE(amp->devvp, FREAD, NOCRED, p);
334 	vput(amp->devvp);
335 	if (amp->bitmap)
336 		free(amp->bitmap, M_ADOSFSBITMAP);
337 	free(amp, M_ADOSFSMNT);
338 	mp->mnt_data = NULL;
339 	mp->mnt_flag &= ~MNT_LOCAL;
340 	return (error);
341 }
342 
343 int
344 adosfs_root(mp, vpp)
345 	struct mount *mp;
346 	struct vnode **vpp;
347 {
348 	struct vnode *nvp;
349 	int error;
350 
351 	if ((error = VFS_VGET(mp, (ino_t)VFSTOADOSFS(mp)->rootb, &nvp)) != 0)
352 		return (error);
353 	/* XXX verify it's a root block? */
354 	*vpp = nvp;
355 	return (0);
356 }
357 
358 int
359 adosfs_statfs(mp, sbp, p)
360 	struct mount *mp;
361 	struct statfs *sbp;
362 	struct proc *p;
363 {
364 	struct adosfsmount *amp;
365 
366 	amp = VFSTOADOSFS(mp);
367 #ifdef COMPAT_09
368 	sbp->f_type = 16;
369 #else
370 	sbp->f_type = 0;
371 #endif
372 	sbp->f_bsize = amp->bsize;
373 	sbp->f_iosize = amp->dbsize;
374 	sbp->f_blocks = amp->numblks;
375 	sbp->f_bfree = amp->freeblks;
376 	sbp->f_bavail = amp->freeblks;
377 	sbp->f_files = 0;		/* who knows */
378 	sbp->f_ffree = 0;		/* " " */
379 	copy_statfs_info(sbp, mp);
380 	return (0);
381 }
382 
383 /*
384  * lookup an anode, check mount's hash table if not found, create
385  * return locked and referenced al la vget(vp, 1);
386  */
387 int
388 adosfs_vget(mp, an, vpp)
389 	struct mount *mp;
390 	ino_t an;
391 	struct vnode **vpp;
392 {
393 	struct adosfsmount *amp;
394 	struct vnode *vp;
395 	struct anode *ap;
396 	struct buf *bp;
397 	char *nam, *tmp;
398 	int namlen, error;
399 
400 	error = 0;
401 	amp = VFSTOADOSFS(mp);
402 	bp = NULL;
403 
404 	/*
405 	 * check hash table. we are done if found
406 	 */
407 	if ((*vpp = adosfs_ahashget(mp, an)) != NULL)
408 		return (0);
409 
410 	error = getnewvnode(VT_ADOSFS, mp, adosfs_vnodeop_p, &vp);
411 	if (error)
412 		return (error);
413 
414 	/*
415 	 * setup, insert in hash, and lock before io.
416 	 */
417 	vp->v_data = ap = pool_get(&adosfs_node_pool, PR_WAITOK);
418 	memset(ap, 0, sizeof(struct anode));
419 	ap->vp = vp;
420 	ap->amp = amp;
421 	ap->block = an;
422 	ap->nwords = amp->nwords;
423 	adosfs_ainshash(amp, ap);
424 
425 	if ((error = bread(amp->devvp, an * amp->bsize / DEV_BSIZE,
426 			   amp->bsize, NOCRED, &bp)) != 0) {
427 		brelse(bp);
428 		vput(vp);
429 		return (error);
430 	}
431 
432 	/*
433 	 * get type and fill rest in based on that.
434 	 */
435 	switch (ap->type = adosfs_getblktype(amp, bp)) {
436 	case AROOT:
437 		vp->v_type = VDIR;
438 		vp->v_flag |= VROOT;
439 		ap->mtimev.days = adoswordn(bp, ap->nwords - 10);
440 		ap->mtimev.mins = adoswordn(bp, ap->nwords - 9);
441 		ap->mtimev.ticks = adoswordn(bp, ap->nwords - 8);
442 		ap->created.days = adoswordn(bp, ap->nwords - 7);
443 		ap->created.mins = adoswordn(bp, ap->nwords - 6);
444 		ap->created.ticks = adoswordn(bp, ap->nwords - 5);
445 		break;
446 	case ALDIR:
447 	case ADIR:
448 		vp->v_type = VDIR;
449 		break;
450 	case ALFILE:
451 	case AFILE:
452 		vp->v_type = VREG;
453 		ap->fsize = adoswordn(bp, ap->nwords - 47);
454 		break;
455 	case ASLINK:		/* XXX soft link */
456 		vp->v_type = VLNK;
457 		/*
458 		 * convert from BCPL string and
459 		 * from: "part:dir/file" to: "/part/dir/file"
460 		 */
461 		nam = bp->b_data + (6 * sizeof(long));
462 		namlen = strlen(nam);
463 		tmp = nam;
464 		while (*tmp && *tmp != ':')
465 			tmp++;
466 		if (*tmp == 0) {
467 			ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK);
468 			memcpy(ap->slinkto, nam, namlen);
469 		} else if (*nam == ':') {
470 			ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK);
471 			memcpy(ap->slinkto, nam, namlen);
472 			ap->slinkto[0] = '/';
473 		} else {
474 			ap->slinkto = malloc(namlen + 2, M_ANODE, M_WAITOK);
475 			ap->slinkto[0] = '/';
476 			memcpy(&ap->slinkto[1], nam, namlen);
477 			ap->slinkto[tmp - nam + 1] = '/';
478 			namlen++;
479 		}
480 		ap->slinkto[namlen] = 0;
481 		ap->fsize = namlen;
482 		break;
483 	default:
484 		brelse(bp);
485 		vput(vp);
486 		return (EINVAL);
487 	}
488 
489 	/*
490 	 * Get appropriate data from this block;  hard link needs
491 	 * to get other data from the "real" block.
492 	 */
493 
494 	/*
495 	 * copy in name (from original block)
496 	 */
497 	nam = bp->b_data + (ap->nwords - 20) * sizeof(u_int32_t);
498 	namlen = *(u_char *)nam++;
499 	if (namlen > 30) {
500 #ifdef DIAGNOSTIC
501 		printf("adosfs: aget: name length too long blk %d\n", an);
502 #endif
503 		brelse(bp);
504 		vput(vp);
505 		return (EINVAL);
506 	}
507 	memcpy(ap->name, nam, namlen);
508 	ap->name[namlen] = 0;
509 
510 	/*
511 	 * if dir alloc hash table and copy it in
512 	 */
513 	if (vp->v_type == VDIR) {
514 		int i;
515 
516 		ap->tab = malloc(ANODETABSZ(ap) * 2, M_ANODE, M_WAITOK);
517 		ap->ntabent = ANODETABENT(ap);
518 		ap->tabi = (int *)&ap->tab[ap->ntabent];
519 		memset(ap->tabi, 0, ANODETABSZ(ap));
520 		for (i = 0; i < ap->ntabent; i++)
521 			ap->tab[i] = adoswordn(bp, i + 6);
522 	}
523 
524 	/*
525 	 * misc.
526 	 */
527 	ap->pblock = adoswordn(bp, ap->nwords - 3);
528 	ap->hashf = adoswordn(bp, ap->nwords - 4);
529 	ap->linknext = adoswordn(bp, ap->nwords - 10);
530 	ap->linkto = adoswordn(bp, ap->nwords - 11);
531 
532 	/*
533 	 * setup last indirect block cache.
534 	 */
535 	ap->lastlindblk = 0;
536 	if (ap->type == AFILE)  {
537 		ap->lastindblk = ap->block;
538 		if (adoswordn(bp, ap->nwords - 10))
539 			ap->linkto = ap->block;
540 	} else if (ap->type == ALFILE) {
541 		ap->lastindblk = ap->linkto;
542 		brelse(bp);
543 		bp = NULL;
544 		error = bread(amp->devvp, ap->linkto * amp->bsize / DEV_BSIZE,
545 		    amp->bsize, NOCRED, &bp);
546 		if (error) {
547 			brelse(bp);
548 			vput(vp);
549 			return (error);
550 		}
551 		ap->fsize = adoswordn(bp, ap->nwords - 47);
552 		/*
553 		 * Should ap->block be set to the real file header block?
554 		 */
555 		ap->block = ap->linkto;
556 	}
557 
558 	if (ap->type == AROOT) {
559 		ap->adprot = 15;
560 		ap->uid = amp->uid;
561 		ap->gid = amp->gid;
562 	} else {
563 		ap->adprot = adoswordn(bp, ap->nwords - 48) ^ 15;
564 		/*
565 		 * ADOS directories do not have a `x' protection bit as
566 		 * it is known in VFS; this functionality is fulfilled
567 		 * by the ADOS `r' bit.
568 		 *
569 		 * To retain the ADOS behaviour, fake execute permissions
570 		 * in that case.
571 		 */
572 		if ((ap->type == ADIR || ap->type == ALDIR) &&
573 		    (ap->adprot & 0x00000008) == 0)
574 			ap->adprot &= ~0x00000002;
575 
576 		/*
577 		 * Get uid/gid from extensions in file header
578 		 * (really need to know if this is a muFS partition)
579 		 */
580 		ap->uid = (adoswordn(bp, ap->nwords - 49) >> 16) & 0xffff;
581 		ap->gid = adoswordn(bp, ap->nwords - 49) & 0xffff;
582 		if (ap->uid || ap->gid) {
583 			if (ap->uid == 0xffff)
584 				ap->uid = 0;
585 			if (ap->gid == 0xffff)
586 				ap->gid = 0;
587 			ap->adprot |= 0x40000000;	/* Kludge */
588 		}
589 		else {
590 			/*
591 			 * uid & gid extension don't exist,
592 			 * so use the mount-point uid/gid
593 			 */
594 			ap->uid = amp->uid;
595 			ap->gid = amp->gid;
596 		}
597 	}
598 	ap->mtime.days = adoswordn(bp, ap->nwords - 23);
599 	ap->mtime.mins = adoswordn(bp, ap->nwords - 22);
600 	ap->mtime.ticks = adoswordn(bp, ap->nwords - 21);
601 
602 	genfs_node_init(vp, &adosfs_genfsops);
603 	*vpp = vp;
604 	brelse(bp);
605 	vp->v_size = ap->fsize;
606 	return (0);
607 }
608 
609 /*
610  * Load the bitmap into memory, and count the number of available
611  * blocks.
612  * The bitmap will be released if the filesystem is read-only;  it's
613  * only needed to find the free space.
614  */
615 int
616 adosfs_loadbitmap(amp)
617 	struct adosfsmount *amp;
618 {
619 	struct buf *bp, *mapbp;
620 	u_long bn;
621 	int blkix, endix, mapix;
622 	int bmsize;
623 	int error;
624 
625 	bp = mapbp = NULL;
626 	bn = amp->rootb;
627 	if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE, amp->bsize,
628 	    NOCRED, &bp)) != 0) {
629 		brelse(bp);
630 		return (error);
631 	}
632 	blkix = amp->nwords - 49;
633 	endix = amp->nwords - 24;
634 	mapix = 0;
635 	bmsize = (amp->numblks + 31) / 32;
636 	while (mapix < bmsize) {
637 		int n;
638 		u_long bits;
639 
640 		if (adoswordn(bp, blkix) == 0)
641 			break;
642 		if (mapbp != NULL)
643 			brelse(mapbp);
644 		if ((error = bread(amp->devvp,
645 		    adoswordn(bp, blkix) * amp->bsize / DEV_BSIZE, amp->bsize,
646 		     NOCRED, &mapbp)) != 0)
647 			break;
648 		if (adoscksum(mapbp, amp->nwords)) {
649 #ifdef DIAGNOSTIC
650 			printf("adosfs: loadbitmap - cksum of blk %d failed\n",
651 			    adoswordn(bp, blkix));
652 #endif
653 			/* XXX Force read-only?  Set free space 0? */
654 			break;
655 		}
656 		n = 1;
657 		while (n < amp->nwords && mapix < bmsize) {
658 			amp->bitmap[mapix++] = bits = adoswordn(mapbp, n);
659 			++n;
660 			if (mapix == bmsize && amp->numblks & 31)
661 				bits &= ~(0xffffffff << (amp->numblks & 31));
662 			while (bits) {
663 				if (bits & 1)
664 					++amp->freeblks;
665 				bits >>= 1;
666 			}
667 		}
668 		++blkix;
669 		if (mapix < bmsize && blkix == endix) {
670 			bn = adoswordn(bp, blkix);
671 			brelse(bp);
672 			if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE,
673 			    amp->bsize, NOCRED, &bp)) != 0)
674 				break;
675 			/*
676 			 * Why is there no checksum on these blocks?
677 			 */
678 			blkix = 0;
679 			endix = amp->nwords - 1;
680 		}
681 	}
682 	if (bp)
683 		brelse(bp);
684 	if (mapbp)
685 		brelse(mapbp);
686 	return (error);
687 }
688 
689 
690 /*
691  * File handle to vnode
692  *
693  * Have to be really careful about stale file handles:
694  * - check that the inode number is in range
695  * - call iget() to get the locked inode
696  * - check for an unallocated inode (i_mode == 0)
697  * - check that the generation number matches
698  */
699 
700 struct ifid {
701 	ushort	ifid_len;
702 	ushort	ifid_pad;
703 	int	ifid_ino;
704 	long	ifid_start;
705 };
706 
707 int
708 adosfs_fhtovp(mp, fhp, vpp)
709 	struct mount *mp;
710 	struct fid *fhp;
711 	struct vnode **vpp;
712 {
713 	struct ifid *ifhp = (struct ifid *)fhp;
714 #if 0
715 	struct anode *ap;
716 #endif
717 	struct vnode *nvp;
718 	int error;
719 
720 #ifdef ADOSFS_DIAGNOSTIC
721 	printf("adfhtovp(%x, %x, %x)\n", mp, fhp, vpp);
722 #endif
723 
724 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
725 		*vpp = NULLVP;
726 		return (error);
727 	}
728 #if 0
729 	ap = VTOA(nvp);
730 	if (ap->inode.iso_mode == 0) {
731 		vput(nvp);
732 		*vpp = NULLVP;
733 		return (ESTALE);
734 	}
735 #endif
736 	*vpp = nvp;
737 	return(0);
738 }
739 
740 int
741 adosfs_checkexp(mp, nam, exflagsp, credanonp)
742 	struct mount *mp;
743 	struct mbuf *nam;
744 	int *exflagsp;
745 	struct ucred **credanonp;
746 {
747 	struct adosfsmount *amp = VFSTOADOSFS(mp);
748 #if 0
749 	struct anode *ap;
750 #endif
751 	struct netcred *np;
752 
753 #ifdef ADOSFS_DIAGNOSTIC
754 	printf("adcheckexp(%x, %x, %x)\n", mp, nam, exflagsp);
755 #endif
756 
757 	/*
758 	 * Get the export permission structure for this <mp, client> tuple.
759 	 */
760 	np = vfs_export_lookup(mp, &amp->export, nam);
761 	if (np == NULL)
762 		return (EACCES);
763 
764 	*exflagsp = np->netc_exflags;
765 	*credanonp = &np->netc_anon;
766 	return(0);
767 }
768 
769 int
770 adosfs_vptofh(vp, fhp)
771 	struct vnode *vp;
772 	struct fid *fhp;
773 {
774 	struct anode *ap = VTOA(vp);
775 	struct ifid *ifhp;
776 
777 	ifhp = (struct ifid *)fhp;
778 	ifhp->ifid_len = sizeof(struct ifid);
779 
780 	ifhp->ifid_ino = ap->block;
781 	ifhp->ifid_start = ap->block;
782 
783 #ifdef ADOSFS_DIAGNOSTIC
784 	printf("advptofh(%x, %x)\n", vp, fhp);
785 #endif
786 	return(0);
787 }
788 
789 int
790 adosfs_quotactl(mp, cmds, uid, arg, p)
791 	struct mount *mp;
792 	int cmds;
793 	uid_t uid;
794 	caddr_t arg;
795 	struct proc *p;
796 {
797 	return(EOPNOTSUPP);
798 }
799 
800 int
801 adosfs_sync(mp, waitfor, uc, p)
802 	struct mount *mp;
803 	int waitfor;
804 	struct ucred *uc;
805 	struct proc *p;
806 {
807 #ifdef ADOSFS_DIAGNOSTIC
808 	printf("ad_sync(%x, %x)\n", mp, waitfor);
809 #endif
810 	return(0);
811 }
812 
813 void
814 adosfs_init()
815 {
816 	simple_lock_init(&adosfs_hashlock);
817 
818 	pool_init(&adosfs_node_pool, sizeof(struct anode), 0, 0, 0,
819 	    "adosndpl", &pool_allocator_nointr);
820 }
821 
822 void
823 adosfs_done()
824 {
825 	pool_destroy(&adosfs_node_pool);
826 }
827 
828 int
829 adosfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
830 	int *name;
831 	u_int namelen;
832 	void *oldp;
833 	size_t *oldlenp;
834 	void *newp;
835 	size_t newlen;
836 	struct proc *p;
837 {
838 	return (EOPNOTSUPP);
839 }
840 
841 /*
842  * vfs generic function call table
843  */
844 
845 extern const struct vnodeopv_desc adosfs_vnodeop_opv_desc;
846 
847 const struct vnodeopv_desc *adosfs_vnodeopv_descs[] = {
848 	&adosfs_vnodeop_opv_desc,
849 	NULL,
850 };
851 
852 struct vfsops adosfs_vfsops = {
853 	MOUNT_ADOSFS,
854 	adosfs_mount,
855 	adosfs_start,
856 	adosfs_unmount,
857 	adosfs_root,
858 	adosfs_quotactl,
859 	adosfs_statfs,
860 	adosfs_sync,
861 	adosfs_vget,
862 	adosfs_fhtovp,
863 	adosfs_vptofh,
864 	adosfs_init,
865 	NULL,
866 	adosfs_done,
867 	adosfs_sysctl,
868 	NULL,				/* vfs_mountroot */
869 	adosfs_checkexp,
870 	adosfs_vnodeopv_descs,
871 };
872