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