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