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