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