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