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