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