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