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