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