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