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