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