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