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