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