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