xref: /netbsd-src/sys/fs/adosfs/advfsops.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: advfsops.c,v 1.77 2017/06/01 02:45:12 chs 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.77 2017/06/01 02:45:12 chs 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 	adosfs_loadbitmap(amp);
273 	if (mp->mnt_flag & MNT_RDONLY) {
274 		/*
275 		 * Don't need the bitmap any more if it's read-only.
276 		 */
277 		kmem_free(amp->bitmap, bitmap_sz);
278 		amp->bitmap = NULL;
279 	}
280 	vput(rvp);
281 
282 	return(0);
283 
284 fail:
285 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
286 	(void) VOP_CLOSE(devvp, FREAD, NOCRED);
287 	VOP_UNLOCK(devvp);
288 	if (amp && amp->bitmap)
289 		kmem_free(amp->bitmap, bitmap_sz);
290 	if (amp)
291 		kmem_free(amp, sizeof(*amp));
292 	return (error);
293 }
294 
295 int
296 adosfs_start(struct mount *mp, int flags)
297 {
298 
299 	return (0);
300 }
301 
302 int
303 adosfs_unmount(struct mount *mp, int mntflags)
304 {
305 	struct adosfsmount *amp;
306 	int error, flags;
307 
308 	flags = 0;
309 	if (mntflags & MNT_FORCE)
310 		flags |= FORCECLOSE;
311 	if ((error = vflush(mp, NULLVP, flags)) != 0)
312 		return (error);
313 	amp = VFSTOADOSFS(mp);
314 	if (amp->devvp->v_type != VBAD)
315 		spec_node_setmountedfs(amp->devvp, NULL);
316 	vn_lock(amp->devvp, LK_EXCLUSIVE | LK_RETRY);
317 	error = VOP_CLOSE(amp->devvp, FREAD, NOCRED);
318 	vput(amp->devvp);
319 	if (amp->bitmap) {
320 		size_t bitmap_sz = ((amp->numblks + 31) / 32) *
321 		    sizeof(*amp->bitmap);
322 		kmem_free(amp->bitmap, bitmap_sz);
323 	}
324 	kmem_free(amp, sizeof(*amp));
325 	mp->mnt_data = NULL;
326 	mp->mnt_flag &= ~MNT_LOCAL;
327 	return (error);
328 }
329 
330 int
331 adosfs_root(struct mount *mp, struct vnode **vpp)
332 {
333 	struct vnode *nvp;
334 	int error;
335 
336 	if ((error = VFS_VGET(mp, (ino_t)VFSTOADOSFS(mp)->rootb, &nvp)) != 0)
337 		return (error);
338 	/* XXX verify it's a root block? */
339 	*vpp = nvp;
340 	return (0);
341 }
342 
343 int
344 adosfs_statvfs(struct mount *mp, struct statvfs *sbp)
345 {
346 	struct adosfsmount *amp;
347 
348 	amp = VFSTOADOSFS(mp);
349 	sbp->f_bsize = amp->bsize;
350 	sbp->f_frsize = amp->bsize;
351 	sbp->f_iosize = amp->dbsize;
352 	sbp->f_blocks = amp->numblks;
353 	sbp->f_bfree = amp->freeblks;
354 	sbp->f_bavail = amp->freeblks;
355 	sbp->f_bresvd = 0;
356 	sbp->f_files = 0;		/* who knows */
357 	sbp->f_ffree = 0;		/* " " */
358 	sbp->f_favail = 0;		/* " " */
359 	sbp->f_fresvd = 0;
360 	copy_statvfs_info(sbp, mp);
361 	return (0);
362 }
363 
364 /*
365  * lookup an anode, if not found, create
366  * return locked and referenced
367  */
368 int
369 adosfs_vget(struct mount *mp, ino_t an, struct vnode **vpp)
370 {
371 	u_long block;
372 	int error;
373 
374 	block = an;
375 	KASSERT(block == an);
376 	error = vcache_get(mp, &block, sizeof(block), vpp);
377 	if (error)
378 		return error;
379 	error = vn_lock(*vpp, LK_EXCLUSIVE);
380 	if (error) {
381 		vrele(*vpp);
382 		*vpp = NULL;
383 		return error;
384 	}
385 	return 0;
386 }
387 
388 /*
389  * Initialize this vnode / anode pair.
390  * Caller assures no other thread will try to load this inode.
391  */
392 int
393 adosfs_loadvnode(struct mount *mp, struct vnode *vp,
394     const void *key, size_t key_len, const void **new_key)
395 {
396 	struct adosfsmount *amp;
397 	struct anode *ap;
398 	struct buf *bp;
399 	u_long an;
400 	char *nam, *tmp;
401 	int namlen, error;
402 
403 	KASSERT(key_len == sizeof(an));
404 	memcpy(&an, key, key_len);
405 	amp = VFSTOADOSFS(mp);
406 
407 	if ((error = bread(amp->devvp, an * amp->bsize / DEV_BSIZE,
408 			   amp->bsize, 0, &bp)) != 0)
409 		return error;
410 
411 	ap = pool_get(&adosfs_node_pool, PR_WAITOK);
412 	memset(ap, 0, sizeof(struct anode));
413 	ap->vp = vp;
414 	ap->amp = amp;
415 	ap->block = an;
416 	ap->nwords = amp->nwords;
417 
418 	/*
419 	 * get type and fill rest in based on that.
420 	 */
421 	switch (ap->type = adosfs_getblktype(amp, bp)) {
422 	case AROOT:
423 		vp->v_type = VDIR;
424 		vp->v_vflag |= VV_ROOT;
425 		ap->mtimev.days = adoswordn(bp, ap->nwords - 10);
426 		ap->mtimev.mins = adoswordn(bp, ap->nwords - 9);
427 		ap->mtimev.ticks = adoswordn(bp, ap->nwords - 8);
428 		ap->created.days = adoswordn(bp, ap->nwords - 7);
429 		ap->created.mins = adoswordn(bp, ap->nwords - 6);
430 		ap->created.ticks = adoswordn(bp, ap->nwords - 5);
431 		break;
432 	case ALDIR:
433 	case ADIR:
434 		vp->v_type = VDIR;
435 		break;
436 	case ALFILE:
437 	case AFILE:
438 		vp->v_type = VREG;
439 		ap->fsize = adoswordn(bp, ap->nwords - 47);
440 		break;
441 	case ASLINK:		/* XXX soft link */
442 		vp->v_type = VLNK;
443 		/*
444 		 * convert from BCPL string and
445 		 * from: "part:dir/file" to: "/part/dir/file"
446 		 */
447 		nam = (char *)bp->b_data + (6 * sizeof(long));
448 		namlen = strlen(nam);
449 		tmp = nam;
450 		while (*tmp && *tmp != ':')
451 			tmp++;
452 		if (*tmp == 0) {
453 			ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK);
454 			memcpy(ap->slinkto, nam, namlen);
455 		} else if (*nam == ':') {
456 			ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK);
457 			memcpy(ap->slinkto, nam, namlen);
458 			ap->slinkto[0] = '/';
459 		} else {
460 			ap->slinkto = malloc(namlen + 2, M_ANODE, M_WAITOK);
461 			ap->slinkto[0] = '/';
462 			memcpy(&ap->slinkto[1], nam, namlen);
463 			ap->slinkto[tmp - nam + 1] = '/';
464 			namlen++;
465 		}
466 		ap->slinkto[namlen] = 0;
467 		ap->fsize = namlen;
468 		break;
469 	default:
470 		error = EINVAL;
471 		goto bad;
472 	}
473 
474 	/*
475 	 * Get appropriate data from this block;  hard link needs
476 	 * to get other data from the "real" block.
477 	 */
478 
479 	/*
480 	 * copy in name (from original block)
481 	 */
482 	nam = (char *)bp->b_data + (ap->nwords - 20) * sizeof(u_int32_t);
483 	namlen = *(u_char *)nam++;
484 	if (namlen > 30) {
485 #ifdef DIAGNOSTIC
486 		printf("adosfs: aget: name length too long blk %llu\n",
487 		    (unsigned long long)an);
488 #endif
489 		error = EINVAL;
490 		goto bad;
491 	}
492 	memcpy(ap->name, nam, namlen);
493 	ap->name[namlen] = 0;
494 
495 	/*
496 	 * if dir alloc hash table and copy it in
497 	 */
498 	if (vp->v_type == VDIR) {
499 		int i;
500 
501 		ap->tab = malloc(ANODETABSZ(ap) * 2, M_ANODE, M_WAITOK);
502 		ap->ntabent = ANODETABENT(ap);
503 		ap->tabi = (int *)&ap->tab[ap->ntabent];
504 		memset(ap->tabi, 0, ANODETABSZ(ap));
505 		for (i = 0; i < ap->ntabent; i++)
506 			ap->tab[i] = adoswordn(bp, i + 6);
507 	}
508 
509 	/*
510 	 * misc.
511 	 */
512 	ap->pblock = adoswordn(bp, ap->nwords - 3);
513 	ap->hashf = adoswordn(bp, ap->nwords - 4);
514 	ap->linknext = adoswordn(bp, ap->nwords - 10);
515 	ap->linkto = adoswordn(bp, ap->nwords - 11);
516 
517 	/*
518 	 * setup last indirect block cache.
519 	 */
520 	ap->lastlindblk = 0;
521 	if (ap->type == AFILE)  {
522 		ap->lastindblk = ap->block;
523 		if (adoswordn(bp, ap->nwords - 10))
524 			ap->linkto = ap->block;
525 	} else if (ap->type == ALFILE) {
526 		ap->lastindblk = ap->linkto;
527 		brelse(bp, 0);
528 		bp = NULL;
529 		error = bread(amp->devvp, ap->linkto * amp->bsize / DEV_BSIZE,
530 		    amp->bsize, 0, &bp);
531 		if (error)
532 			goto bad;
533 		ap->fsize = adoswordn(bp, ap->nwords - 47);
534 	}
535 
536 	if (ap->type == AROOT) {
537 		ap->adprot = 15;
538 		ap->uid = amp->uid;
539 		ap->gid = amp->gid;
540 	} else {
541 		ap->adprot = adoswordn(bp, ap->nwords - 48) ^ 15;
542 		/*
543 		 * ADOS directories do not have a `x' protection bit as
544 		 * it is known in VFS; this functionality is fulfilled
545 		 * by the ADOS `r' bit.
546 		 *
547 		 * To retain the ADOS behaviour, fake execute permissions
548 		 * in that case.
549 		 */
550 		if ((ap->type == ADIR || ap->type == ALDIR) &&
551 		    (ap->adprot & 0x00000008) == 0)
552 			ap->adprot &= ~0x00000002;
553 
554 		/*
555 		 * Get uid/gid from extensions in file header
556 		 * (really need to know if this is a muFS partition)
557 		 */
558 		ap->uid = (adoswordn(bp, ap->nwords - 49) >> 16) & 0xffff;
559 		ap->gid = adoswordn(bp, ap->nwords - 49) & 0xffff;
560 		if (ap->uid || ap->gid) {
561 			if (ap->uid == 0xffff)
562 				ap->uid = 0;
563 			if (ap->gid == 0xffff)
564 				ap->gid = 0;
565 			ap->adprot |= 0x40000000;	/* Kludge */
566 		}
567 		else {
568 			/*
569 			 * uid & gid extension don't exist,
570 			 * so use the mount-point uid/gid
571 			 */
572 			ap->uid = amp->uid;
573 			ap->gid = amp->gid;
574 		}
575 	}
576 	ap->mtime.days = adoswordn(bp, ap->nwords - 23);
577 	ap->mtime.mins = adoswordn(bp, ap->nwords - 22);
578 	ap->mtime.ticks = adoswordn(bp, ap->nwords - 21);
579 
580 	brelse(bp, 0);
581 	vp->v_tag = VT_ADOSFS;
582 	vp->v_op = adosfs_vnodeop_p;
583 	vp->v_data = ap;
584 	genfs_node_init(vp, &adosfs_genfsops);
585 	uvm_vnp_setsize(vp, ap->fsize);
586 	*new_key = &ap->block;
587 	return 0;
588 
589 bad:
590 	if (bp)
591 		brelse(bp, 0);
592 	pool_put(&adosfs_node_pool, ap);
593 	return error;
594 }
595 
596 /*
597  * Load the bitmap into memory, and count the number of available
598  * blocks.
599  * The bitmap will be released if the filesystem is read-only;  it's
600  * only needed to find the free space.
601  */
602 int
603 adosfs_loadbitmap(struct adosfsmount *amp)
604 {
605 	struct buf *bp, *mapbp;
606 	u_long bn;
607 	int blkix, endix, mapix;
608 	int bmsize;
609 	int error;
610 
611 	bp = mapbp = NULL;
612 	bn = amp->rootb;
613 	if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE, amp->bsize,
614 	    0, &bp)) != 0) {
615 		return (error);
616 	}
617 	blkix = amp->nwords - 49;
618 	endix = amp->nwords - 24;
619 	mapix = 0;
620 	bmsize = (amp->numblks + 31) / 32;
621 	while (mapix < bmsize) {
622 		int n;
623 		u_long bits;
624 
625 		if (adoswordn(bp, blkix) == 0)
626 			break;
627 		if (mapbp != NULL)
628 			brelse(mapbp, 0);
629 		if ((error = bread(amp->devvp,
630 		    adoswordn(bp, blkix) * amp->bsize / DEV_BSIZE, amp->bsize,
631 		     0, &mapbp)) != 0)
632 			break;
633 		if (adoscksum(mapbp, amp->nwords)) {
634 #ifdef DIAGNOSTIC
635 			printf("adosfs: loadbitmap - cksum of blk %d failed\n",
636 			    adoswordn(bp, blkix));
637 #endif
638 			/* XXX Force read-only?  Set free space 0? */
639 			break;
640 		}
641 		n = 1;
642 		while (n < amp->nwords && mapix < bmsize) {
643 			amp->bitmap[mapix++] = bits = adoswordn(mapbp, n);
644 			++n;
645 			if (mapix == bmsize && amp->numblks & 31)
646 				bits &= ~(0xffffffff << (amp->numblks & 31));
647 			while (bits) {
648 				if (bits & 1)
649 					++amp->freeblks;
650 				bits >>= 1;
651 			}
652 		}
653 		++blkix;
654 		if (mapix < bmsize && blkix == endix) {
655 			bn = adoswordn(bp, blkix);
656 			brelse(bp, 0);
657 			if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE,
658 			    amp->bsize, 0, &bp)) != 0)
659 				break;
660 			/*
661 			 * Why is there no checksum on these blocks?
662 			 */
663 			blkix = 0;
664 			endix = amp->nwords - 1;
665 		}
666 	}
667 	if (bp)
668 		brelse(bp, 0);
669 	if (mapbp)
670 		brelse(mapbp, 0);
671 	return (error);
672 }
673 
674 
675 /*
676  * File handle to vnode
677  *
678  * Have to be really careful about stale file handles:
679  * - check that the inode number is in range
680  * - call iget() to get the locked inode
681  * - check for an unallocated inode (i_mode == 0)
682  * - check that the generation number matches
683  */
684 
685 struct ifid {
686 	ushort	ifid_len;
687 	ushort	ifid_pad;
688 	int	ifid_ino;
689 	long	ifid_start;
690 };
691 
692 int
693 adosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
694 {
695 	struct ifid ifh;
696 #if 0
697 	struct anode *ap;
698 #endif
699 	struct vnode *nvp;
700 	int error;
701 
702 	if (fhp->fid_len != sizeof(struct ifid))
703 		return EINVAL;
704 
705 #ifdef ADOSFS_DIAGNOSTIC
706 	printf("adfhtovp(%p, %p, %p)\n", mp, fhp, vpp);
707 #endif
708 
709 	memcpy(&ifh, fhp, sizeof(ifh));
710 
711 	if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
712 		*vpp = NULLVP;
713 		return (error);
714 	}
715 #if 0
716 	ap = VTOA(nvp);
717 	if (ap->inode.iso_mode == 0) {
718 		vput(nvp);
719 		*vpp = NULLVP;
720 		return (ESTALE);
721 	}
722 #endif
723 	*vpp = nvp;
724 	return(0);
725 }
726 
727 int
728 adosfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
729 {
730 	struct anode *ap = VTOA(vp);
731 	struct ifid ifh;
732 
733 	if (*fh_size < sizeof(struct ifid)) {
734 		*fh_size = sizeof(struct ifid);
735 		return E2BIG;
736 	}
737 	*fh_size = sizeof(struct ifid);
738 
739 	memset(&ifh, 0, sizeof(ifh));
740 	ifh.ifid_len = sizeof(struct ifid);
741 	ifh.ifid_ino = ap->block;
742 	ifh.ifid_start = ap->block;
743 	memcpy(fhp, &ifh, sizeof(ifh));
744 
745 #ifdef ADOSFS_DIAGNOSTIC
746 	printf("advptofh(%p, %p)\n", vp, fhp);
747 #endif
748 	return(0);
749 }
750 
751 int
752 adosfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
753 {
754 #ifdef ADOSFS_DIAGNOSTIC
755 	printf("ad_sync(%p, %d)\n", mp, waitfor);
756 #endif
757 	return(0);
758 }
759 
760 void
761 adosfs_init(void)
762 {
763 
764 	malloc_type_attach(M_ANODE);
765 	pool_init(&adosfs_node_pool, sizeof(struct anode), 0, 0, 0, "adosndpl",
766 	    &pool_allocator_nointr, IPL_NONE);
767 }
768 
769 void
770 adosfs_done(void)
771 {
772 
773 	pool_destroy(&adosfs_node_pool);
774 	malloc_type_detach(M_ANODE);
775 }
776 
777 /*
778  * vfs generic function call table
779  */
780 
781 extern const struct vnodeopv_desc adosfs_vnodeop_opv_desc;
782 
783 const struct vnodeopv_desc *adosfs_vnodeopv_descs[] = {
784 	&adosfs_vnodeop_opv_desc,
785 	NULL,
786 };
787 
788 struct vfsops adosfs_vfsops = {
789 	.vfs_name = MOUNT_ADOSFS,
790 	.vfs_min_mount_data = sizeof (struct adosfs_args),
791 	.vfs_mount = adosfs_mount,
792 	.vfs_start = adosfs_start,
793 	.vfs_unmount = adosfs_unmount,
794 	.vfs_root = adosfs_root,
795 	.vfs_quotactl = (void *)eopnotsupp,
796 	.vfs_statvfs = adosfs_statvfs,
797 	.vfs_sync = adosfs_sync,
798 	.vfs_vget = adosfs_vget,
799 	.vfs_loadvnode = adosfs_loadvnode,
800 	.vfs_fhtovp = adosfs_fhtovp,
801 	.vfs_vptofh = adosfs_vptofh,
802 	.vfs_init = adosfs_init,
803 	.vfs_done = adosfs_done,
804 	.vfs_snapshot = (void *)eopnotsupp,
805 	.vfs_extattrctl = vfs_stdextattrctl,
806 	.vfs_suspendctl = genfs_suspendctl,
807 	.vfs_renamelock_enter = genfs_renamelock_enter,
808 	.vfs_renamelock_exit = genfs_renamelock_exit,
809 	.vfs_fsync = (void *)eopnotsupp,
810 	.vfs_opv_descs = adosfs_vnodeopv_descs
811 };
812 
813 static int
814 adosfs_modcmd(modcmd_t cmd, void *arg)
815 {
816 	int error;
817 
818 	switch (cmd) {
819 	case MODULE_CMD_INIT:
820 		error = vfs_attach(&adosfs_vfsops);
821 		if (error != 0)
822 			break;
823 		sysctl_createv(&adosfs_sysctl_log, 0, NULL, NULL,
824 			       CTLFLAG_PERMANENT,
825 			       CTLTYPE_NODE, "adosfs",
826 			       SYSCTL_DESCR("AmigaDOS file system"),
827 			       NULL, 0, NULL, 0,
828 			       CTL_VFS, 16, CTL_EOL);
829 		/*
830 		 * XXX the "16" above could be dynamic, thereby eliminating
831 		 * one more instance of the "number to vfs" mapping problem,
832 		 * but "16" is the order as taken from sys/mount.h
833 		 */
834 		break;
835 	case MODULE_CMD_FINI:
836 		error = vfs_detach(&adosfs_vfsops);
837 		if (error != 0)
838 			break;
839 		sysctl_teardown(&adosfs_sysctl_log);
840 		break;
841 	default:
842 		error = ENOTTY;
843 		break;
844 	}
845 
846 	return (error);
847 }
848