xref: /netbsd/sys/ufs/chfs/chfs_vfsops.c (revision 01d8bbd5)
1*01d8bbd5Shannken /*	$NetBSD: chfs_vfsops.c,v 1.23 2022/03/19 13:53:32 hannken Exp $	*/
2060d2ed9Sahoka 
3060d2ed9Sahoka /*-
4060d2ed9Sahoka  * Copyright (c) 2010 Department of Software Engineering,
5060d2ed9Sahoka  *		      University of Szeged, Hungary
6060d2ed9Sahoka  * Copyright (C) 2010 Tamas Toth <ttoth@inf.u-szeged.hu>
7060d2ed9Sahoka  * Copyright (C) 2010 Adam Hoka <ahoka@NetBSD.org>
8060d2ed9Sahoka  * All rights reserved.
9060d2ed9Sahoka  *
10060d2ed9Sahoka  * This code is derived from software contributed to The NetBSD Foundation
11060d2ed9Sahoka  * by the Department of Software Engineering, University of Szeged, Hungary
12060d2ed9Sahoka  *
13060d2ed9Sahoka  * Redistribution and use in source and binary forms, with or without
14060d2ed9Sahoka  * modification, are permitted provided that the following conditions
15060d2ed9Sahoka  * are met:
16060d2ed9Sahoka  * 1. Redistributions of source code must retain the above copyright
17060d2ed9Sahoka  *    notice, this list of conditions and the following disclaimer.
18060d2ed9Sahoka  * 2. Redistributions in binary form must reproduce the above copyright
19060d2ed9Sahoka  *    notice, this list of conditions and the following disclaimer in the
20060d2ed9Sahoka  *    documentation and/or other materials provided with the distribution.
21060d2ed9Sahoka  *
22060d2ed9Sahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23060d2ed9Sahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24060d2ed9Sahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25060d2ed9Sahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26060d2ed9Sahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27060d2ed9Sahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28060d2ed9Sahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29060d2ed9Sahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30060d2ed9Sahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31060d2ed9Sahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32060d2ed9Sahoka  * SUCH DAMAGE.
33060d2ed9Sahoka  */
34060d2ed9Sahoka 
35060d2ed9Sahoka #include <sys/cdefs.h>
36060d2ed9Sahoka 
37060d2ed9Sahoka #include <sys/param.h>
38060d2ed9Sahoka #include <sys/types.h>
39060d2ed9Sahoka #include <sys/kmem.h>
40060d2ed9Sahoka #include <sys/mount.h>
41060d2ed9Sahoka #include <sys/stat.h>
42060d2ed9Sahoka #include <sys/systm.h>
43060d2ed9Sahoka #include <sys/proc.h>
44060d2ed9Sahoka #include <sys/module.h>
45060d2ed9Sahoka #include <sys/namei.h>
46060d2ed9Sahoka #include <sys/fcntl.h>
47060d2ed9Sahoka #include <sys/conf.h>
48060d2ed9Sahoka #include <sys/buf.h>
49060d2ed9Sahoka //XXX needed just for debugging
50060d2ed9Sahoka #include <sys/fstrans.h>
51060d2ed9Sahoka #include <sys/sleepq.h>
52060d2ed9Sahoka #include <sys/lockdebug.h>
53060d2ed9Sahoka #include <sys/ktrace.h>
54060d2ed9Sahoka 
55ea5c7e85Sriastradh #include <uvm/uvm_extern.h>
56ea5c7e85Sriastradh 
57060d2ed9Sahoka #include <ufs/ufs/dir.h>
58060d2ed9Sahoka #include <ufs/ufs/ufs_extern.h>
59060d2ed9Sahoka #include <miscfs/genfs/genfs.h>
60060d2ed9Sahoka #include <miscfs/genfs/genfs_node.h>
61060d2ed9Sahoka #include <miscfs/specfs/specdev.h>
62060d2ed9Sahoka #include "chfs.h"
63060d2ed9Sahoka #include "chfs_args.h"
64060d2ed9Sahoka 
65060d2ed9Sahoka /* --------------------------------------------------------------------- */
66060d2ed9Sahoka /* functions */
67060d2ed9Sahoka 
68060d2ed9Sahoka static int chfs_mount(struct mount *, const char *, void *, size_t *);
69060d2ed9Sahoka static int chfs_unmount(struct mount *, int);
7002443708Sad static int chfs_root(struct mount *, int, struct vnode **);
7166996266Shannken static int chfs_loadvnode(struct mount *, struct vnode *,
7266996266Shannken     const void *, size_t, const void **);
7302443708Sad static int chfs_vget(struct mount *, ino_t, int, struct vnode **);
7402443708Sad static int chfs_fhtovp(struct mount *, struct fid *, int, struct vnode **);
75060d2ed9Sahoka static int chfs_vptofh(struct vnode *, struct fid *, size_t *);
76060d2ed9Sahoka static int chfs_start(struct mount *, int);
77060d2ed9Sahoka static int chfs_statvfs(struct mount *, struct statvfs *);
78060d2ed9Sahoka static int chfs_sync(struct mount *, int, kauth_cred_t);
79060d2ed9Sahoka static void chfs_init(void);
80060d2ed9Sahoka static void chfs_reinit(void);
81060d2ed9Sahoka static void chfs_done(void);
82060d2ed9Sahoka static int chfs_snapshot(struct mount *, struct vnode *,
83060d2ed9Sahoka     struct timespec *);
84060d2ed9Sahoka 
85060d2ed9Sahoka /* --------------------------------------------------------------------- */
86060d2ed9Sahoka /* structures */
87060d2ed9Sahoka 
88060d2ed9Sahoka int
chfs_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)89060d2ed9Sahoka chfs_gop_alloc(struct vnode *vp, off_t off, off_t len,  int flags,
90060d2ed9Sahoka     kauth_cred_t cred)
91060d2ed9Sahoka {
92060d2ed9Sahoka 	return (0);
93060d2ed9Sahoka }
94060d2ed9Sahoka 
95060d2ed9Sahoka const struct genfs_ops chfs_genfsops = {
96060d2ed9Sahoka 	.gop_size = genfs_size,
97060d2ed9Sahoka 	.gop_alloc = chfs_gop_alloc,
98060d2ed9Sahoka 	.gop_write = genfs_gop_write,
99060d2ed9Sahoka 	.gop_markupdate = ufs_gop_markupdate,
1000dc745edSchs 	.gop_putrange = genfs_gop_putrange,
101060d2ed9Sahoka };
102060d2ed9Sahoka 
103060d2ed9Sahoka struct pool chfs_inode_pool;
104060d2ed9Sahoka 
105060d2ed9Sahoka /* for looking up the major for flash */
106060d2ed9Sahoka extern const struct cdevsw flash_cdevsw;
107060d2ed9Sahoka 
108060d2ed9Sahoka /* --------------------------------------------------------------------- */
109060d2ed9Sahoka 
110060d2ed9Sahoka static int
chfs_mount(struct mount * mp,const char * path,void * data,size_t * data_len)111060d2ed9Sahoka chfs_mount(struct mount *mp,
112060d2ed9Sahoka     const char *path, void *data, size_t *data_len)
113060d2ed9Sahoka {
114060d2ed9Sahoka 	struct lwp *l = curlwp;
115060d2ed9Sahoka 	struct nameidata nd;
116060d2ed9Sahoka 	struct pathbuf *pb;
117060d2ed9Sahoka 	struct vnode *devvp = NULL;
118060d2ed9Sahoka 	struct ufs_args *args = data;
119060d2ed9Sahoka 	struct ufsmount *ump = NULL;
120060d2ed9Sahoka 	struct chfs_mount *chmp;
121060d2ed9Sahoka 	int err = 0;
122060d2ed9Sahoka 	int xflags;
123060d2ed9Sahoka 
124060d2ed9Sahoka 	dbg("mount()\n");
125060d2ed9Sahoka 
126f4f7f4f3Smaxv 	if (args == NULL)
127f4f7f4f3Smaxv 		return EINVAL;
128060d2ed9Sahoka 	if (*data_len < sizeof *args)
129060d2ed9Sahoka 		return EINVAL;
130060d2ed9Sahoka 
131060d2ed9Sahoka 	if (mp->mnt_flag & MNT_GETARGS) {
132060d2ed9Sahoka 		ump = VFSTOUFS(mp);
133060d2ed9Sahoka 		if (ump == NULL)
134060d2ed9Sahoka 			return EIO;
135060d2ed9Sahoka 		memset(args, 0, sizeof *args);
136060d2ed9Sahoka 		args->fspec = NULL;
137060d2ed9Sahoka 		*data_len = sizeof *args;
138060d2ed9Sahoka 		return 0;
139060d2ed9Sahoka 	}
140060d2ed9Sahoka 
141060d2ed9Sahoka 	if (mp->mnt_flag & MNT_UPDATE) {
142060d2ed9Sahoka 		/* XXX: There is no support yet to update file system
143060d2ed9Sahoka 		 * settings.  Should be added. */
144060d2ed9Sahoka 
145060d2ed9Sahoka 		return ENODEV;
146060d2ed9Sahoka 	}
147060d2ed9Sahoka 
148060d2ed9Sahoka 	if (args->fspec != NULL) {
149060d2ed9Sahoka 		err = pathbuf_copyin(args->fspec, &pb);
150060d2ed9Sahoka 		if (err) {
151060d2ed9Sahoka 			return err;
152060d2ed9Sahoka 		}
153aa98cbbaSttoth 		/* Look up the name and verify that it's sane. */
154060d2ed9Sahoka 		NDINIT(&nd, LOOKUP, FOLLOW, pb);
15554778a05Schristos 		err = namei(&nd);
156858a9750Smaxv 		pathbuf_destroy(pb);
15754778a05Schristos 		if (err)
15854778a05Schristos 			return err;
159060d2ed9Sahoka 		devvp = nd.ni_vp;
160060d2ed9Sahoka 
161aa98cbbaSttoth 		/* Be sure this is a valid block device */
162060d2ed9Sahoka 		if (devvp->v_type != VBLK)
163060d2ed9Sahoka 			err = ENOTBLK;
164060d2ed9Sahoka 		else if (bdevsw_lookup(devvp->v_rdev) == NULL)
165060d2ed9Sahoka 			err = ENXIO;
166060d2ed9Sahoka 	}
167060d2ed9Sahoka 
168060d2ed9Sahoka 	if (err) {
169060d2ed9Sahoka 		vrele(devvp);
170060d2ed9Sahoka 		return (err);
171060d2ed9Sahoka 	}
172060d2ed9Sahoka 
173060d2ed9Sahoka 	if (mp->mnt_flag & MNT_RDONLY)
174060d2ed9Sahoka 		xflags = FREAD;
175060d2ed9Sahoka 	else
176060d2ed9Sahoka 		xflags = FREAD|FWRITE;
177060d2ed9Sahoka 
178060d2ed9Sahoka 	err = VOP_OPEN(devvp, xflags, FSCRED);
179060d2ed9Sahoka 	if (err)
180060d2ed9Sahoka 		goto fail;
181060d2ed9Sahoka 
182aa98cbbaSttoth 	/* call CHFS mount function */
183060d2ed9Sahoka 	err = chfs_mountfs(devvp, mp);
184060d2ed9Sahoka 	if (err) {
185060d2ed9Sahoka 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
186060d2ed9Sahoka 		(void)VOP_CLOSE(devvp, xflags, NOCRED);
187060d2ed9Sahoka 		VOP_UNLOCK(devvp);
188060d2ed9Sahoka 		goto fail;
189060d2ed9Sahoka 	}
190aa98cbbaSttoth 
191060d2ed9Sahoka 	ump = VFSTOUFS(mp);
192060d2ed9Sahoka 	chmp = ump->um_chfs;
193060d2ed9Sahoka 
194060d2ed9Sahoka 	vfs_getnewfsid(mp);
195060d2ed9Sahoka 	chmp->chm_fsmp = mp;
196060d2ed9Sahoka 
197060d2ed9Sahoka 	return set_statvfs_info(path,
198060d2ed9Sahoka 	    UIO_USERSPACE, args->fspec,
199060d2ed9Sahoka 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
200060d2ed9Sahoka 
201060d2ed9Sahoka fail:
202060d2ed9Sahoka 	vrele(devvp);
203060d2ed9Sahoka 	return (err);
204060d2ed9Sahoka }
205060d2ed9Sahoka 
206aa98cbbaSttoth /* chfs_mountfs - init CHFS */
207060d2ed9Sahoka int
chfs_mountfs(struct vnode * devvp,struct mount * mp)208060d2ed9Sahoka chfs_mountfs(struct vnode *devvp, struct mount *mp)
209060d2ed9Sahoka {
210060d2ed9Sahoka 	struct lwp *l = curlwp;
211060d2ed9Sahoka 	kauth_cred_t cred;
212060d2ed9Sahoka 	devmajor_t flash_major;
213060d2ed9Sahoka 	dev_t dev;
214060d2ed9Sahoka 	struct ufsmount* ump = NULL;
215060d2ed9Sahoka 	struct chfs_mount* chmp;
216060d2ed9Sahoka 	struct vnode *vp;
217060d2ed9Sahoka 	int err = 0;
218060d2ed9Sahoka 
219060d2ed9Sahoka 	dbg("mountfs()\n");
220060d2ed9Sahoka 
221060d2ed9Sahoka 	dev = devvp->v_rdev;
222060d2ed9Sahoka 	cred = l ? l->l_cred : NOCRED;
223060d2ed9Sahoka 
224060d2ed9Sahoka 	/* Flush out any old buffers remaining from a previous use. */
225060d2ed9Sahoka 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
226060d2ed9Sahoka 	err = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
227060d2ed9Sahoka 	VOP_UNLOCK(devvp);
228060d2ed9Sahoka 	if (err)
2296a9242deSriastradh 		goto fail0;
230060d2ed9Sahoka 
231aa98cbbaSttoth 	/* Setup device. */
232060d2ed9Sahoka 	flash_major = cdevsw_lookup_major(&flash_cdevsw);
233060d2ed9Sahoka 
234060d2ed9Sahoka 	if (devvp->v_type != VBLK)
235060d2ed9Sahoka 		err = ENOTBLK;
236060d2ed9Sahoka 	else if (bdevsw_lookup(dev) == NULL)
237060d2ed9Sahoka 		err = ENXIO;
238060d2ed9Sahoka 	else if (major(dev) != flash_major) {
239060d2ed9Sahoka 		dbg("major(dev): %d, flash_major: %d\n",
240060d2ed9Sahoka 		    major(dev), flash_major);
241060d2ed9Sahoka 		err = ENODEV;
242060d2ed9Sahoka 	}
2436a9242deSriastradh 	if (err)
2446a9242deSriastradh 		goto fail0;
245060d2ed9Sahoka 
246aa98cbbaSttoth 	/* Connect CHFS to UFS. */
247a6388638Srmind 	ump = kmem_zalloc(sizeof(struct ufsmount), KM_SLEEP);
248a6388638Srmind 
249060d2ed9Sahoka 	ump->um_fstype = UFS1;
250a6388638Srmind 	ump->um_chfs = kmem_zalloc(sizeof(struct chfs_mount), KM_SLEEP);
251060d2ed9Sahoka 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
252060d2ed9Sahoka 
253060d2ed9Sahoka 	chmp = ump->um_chfs;
254060d2ed9Sahoka 
255aa98cbbaSttoth 	/* Initialize erase block handler. */
256060d2ed9Sahoka 	chmp->chm_ebh = kmem_alloc(sizeof(struct chfs_ebh), KM_SLEEP);
257060d2ed9Sahoka 
258060d2ed9Sahoka 	dbg("[]opening flash: %u\n", (unsigned int)devvp->v_rdev);
259060d2ed9Sahoka 	err = ebh_open(chmp->chm_ebh, devvp->v_rdev);
260060d2ed9Sahoka 	if (err) {
261060d2ed9Sahoka 		dbg("error while opening flash\n");
2626a9242deSriastradh 		goto fail1;
263060d2ed9Sahoka 	}
264060d2ed9Sahoka 
265060d2ed9Sahoka 	//TODO check flash sizes
266060d2ed9Sahoka 
267aa98cbbaSttoth 	/* Initialize vnode cache's hashtable and eraseblock array. */
268060d2ed9Sahoka 	chmp->chm_gbl_version = 0;
269060d2ed9Sahoka 	chmp->chm_vnocache_hash = chfs_vnocache_hash_init();
270060d2ed9Sahoka 
271060d2ed9Sahoka 	chmp->chm_blocks = kmem_zalloc(chmp->chm_ebh->peb_nr *
272060d2ed9Sahoka 	    sizeof(struct chfs_eraseblock), KM_SLEEP);
273060d2ed9Sahoka 
274aa98cbbaSttoth 	/* Initialize mutexes. */
275060d2ed9Sahoka 	mutex_init(&chmp->chm_lock_mountfields, MUTEX_DEFAULT, IPL_NONE);
276060d2ed9Sahoka 	mutex_init(&chmp->chm_lock_sizes, MUTEX_DEFAULT, IPL_NONE);
277060d2ed9Sahoka 	mutex_init(&chmp->chm_lock_vnocache, MUTEX_DEFAULT, IPL_NONE);
278060d2ed9Sahoka 
279aa98cbbaSttoth 	/* Initialize read/write contants. (from UFS) */
280060d2ed9Sahoka 	chmp->chm_fs_bmask = -4096;
281060d2ed9Sahoka 	chmp->chm_fs_bsize = 4096;
282060d2ed9Sahoka 	chmp->chm_fs_qbmask = 4095;
283060d2ed9Sahoka 	chmp->chm_fs_bshift = 12;
284060d2ed9Sahoka 	chmp->chm_fs_fmask = -2048;
285060d2ed9Sahoka 	chmp->chm_fs_qfmask = 2047;
286060d2ed9Sahoka 
287aa98cbbaSttoth 	/* Initialize writebuffer. */
288060d2ed9Sahoka 	chmp->chm_wbuf_pagesize = chmp->chm_ebh->flash_if->page_size;
289060d2ed9Sahoka 	dbg("wbuf size: %zu\n", chmp->chm_wbuf_pagesize);
290060d2ed9Sahoka 	chmp->chm_wbuf = kmem_alloc(chmp->chm_wbuf_pagesize, KM_SLEEP);
291060d2ed9Sahoka 	rw_init(&chmp->chm_lock_wbuf);
292060d2ed9Sahoka 
293aa98cbbaSttoth 	/* Initialize queues. */
294060d2ed9Sahoka 	TAILQ_INIT(&chmp->chm_free_queue);
295060d2ed9Sahoka 	TAILQ_INIT(&chmp->chm_clean_queue);
296060d2ed9Sahoka 	TAILQ_INIT(&chmp->chm_dirty_queue);
297060d2ed9Sahoka 	TAILQ_INIT(&chmp->chm_very_dirty_queue);
298060d2ed9Sahoka 	TAILQ_INIT(&chmp->chm_erasable_pending_wbuf_queue);
299060d2ed9Sahoka 	TAILQ_INIT(&chmp->chm_erase_pending_queue);
300060d2ed9Sahoka 
301aa98cbbaSttoth 	/* Initialize flash-specific constants. */
302060d2ed9Sahoka 	chfs_calc_trigger_levels(chmp);
303060d2ed9Sahoka 
304aa98cbbaSttoth 	/* Initialize sizes. */
305060d2ed9Sahoka 	chmp->chm_nr_free_blocks = 0;
306060d2ed9Sahoka 	chmp->chm_nr_erasable_blocks = 0;
307060d2ed9Sahoka 	chmp->chm_max_vno = 2;
308060d2ed9Sahoka 	chmp->chm_checked_vno = 2;
309060d2ed9Sahoka 	chmp->chm_unchecked_size = 0;
310060d2ed9Sahoka 	chmp->chm_used_size = 0;
311060d2ed9Sahoka 	chmp->chm_dirty_size = 0;
312060d2ed9Sahoka 	chmp->chm_wasted_size = 0;
313060d2ed9Sahoka 	chmp->chm_free_size = chmp->chm_ebh->eb_size * chmp->chm_ebh->peb_nr;
314aa98cbbaSttoth 
315aa98cbbaSttoth 	/* Build filesystem. */
316060d2ed9Sahoka 	err = chfs_build_filesystem(chmp);
317060d2ed9Sahoka 
318060d2ed9Sahoka 	if (err) {
319aa98cbbaSttoth 		/* Armageddon and return. */
320a6388638Srmind 		err = EIO;
3216a9242deSriastradh 		goto fail2;
322060d2ed9Sahoka 	}
323060d2ed9Sahoka 
324aa98cbbaSttoth 	/* Initialize UFS. */
325060d2ed9Sahoka 	mp->mnt_data = ump;
326060d2ed9Sahoka 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
327060d2ed9Sahoka 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CHFS);
328060d2ed9Sahoka 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
329060d2ed9Sahoka 	mp->mnt_stat.f_namemax = MAXNAMLEN;
330060d2ed9Sahoka 	mp->mnt_flag |= MNT_LOCAL;
331060d2ed9Sahoka 	mp->mnt_fs_bshift = PAGE_SHIFT;
332060d2ed9Sahoka 	mp->mnt_dev_bshift = DEV_BSHIFT;
333060d2ed9Sahoka 	mp->mnt_iflag |= IMNT_MPSAFE;
334060d2ed9Sahoka 	ump->um_flags = 0;
335060d2ed9Sahoka 	ump->um_mountp = mp;
336060d2ed9Sahoka 	ump->um_dev = dev;
337060d2ed9Sahoka 	ump->um_devvp = devvp;
338060d2ed9Sahoka 	ump->um_maxfilesize = 1048512 * 1024;
339060d2ed9Sahoka 
340aa98cbbaSttoth 	/* Allocate the root vnode. */
34102443708Sad 	err = VFS_VGET(mp, CHFS_ROOTINO, LK_EXCLUSIVE, &vp);
342060d2ed9Sahoka 	if (err) {
343060d2ed9Sahoka 		dbg("error: %d while allocating root node\n", err);
344060d2ed9Sahoka 		return err;
345060d2ed9Sahoka 	}
346060d2ed9Sahoka 	vput(vp);
347060d2ed9Sahoka 
348aa98cbbaSttoth 	/* Start GC. */
349060d2ed9Sahoka 	chfs_gc_thread_start(chmp);
350060d2ed9Sahoka 	mutex_enter(&chmp->chm_lock_mountfields);
351060d2ed9Sahoka 	chfs_gc_trigger(chmp);
352060d2ed9Sahoka 	mutex_exit(&chmp->chm_lock_mountfields);
353060d2ed9Sahoka 
354ce2a78d3Shannken 	spec_node_setmountedfs(devvp, mp);
355060d2ed9Sahoka 	return 0;
356aa98cbbaSttoth 
3576a9242deSriastradh fail2:
3586a9242deSriastradh 	KASSERT(TAILQ_EMPTY(&chmp->chm_erase_pending_queue));
3596a9242deSriastradh 	KASSERT(TAILQ_EMPTY(&chmp->chm_erasable_pending_wbuf_queue));
3606a9242deSriastradh 	KASSERT(TAILQ_EMPTY(&chmp->chm_very_dirty_queue));
3616a9242deSriastradh 	KASSERT(TAILQ_EMPTY(&chmp->chm_dirty_queue));
3626a9242deSriastradh 	KASSERT(TAILQ_EMPTY(&chmp->chm_clean_queue));
3636a9242deSriastradh 	KASSERT(TAILQ_EMPTY(&chmp->chm_free_queue));
3646a9242deSriastradh 	rw_destroy(&chmp->chm_lock_wbuf);
3656a9242deSriastradh 	kmem_free(chmp->chm_wbuf, chmp->chm_wbuf_pagesize);
3666a9242deSriastradh 	mutex_destroy(&chmp->chm_lock_vnocache);
3676a9242deSriastradh 	mutex_destroy(&chmp->chm_lock_sizes);
3686a9242deSriastradh 	mutex_destroy(&chmp->chm_lock_mountfields);
3696a9242deSriastradh 	kmem_free(chmp->chm_blocks, chmp->chm_ebh->peb_nr *
3706a9242deSriastradh 	    sizeof(struct chfs_eraseblock));
3716a9242deSriastradh 	chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
3726a9242deSriastradh 	ebh_close(chmp->chm_ebh);
3736a9242deSriastradh 
3746a9242deSriastradh fail1:
375a6388638Srmind 	kmem_free(chmp->chm_ebh, sizeof(struct chfs_ebh));
3766a9242deSriastradh 	mutex_destroy(&ump->um_lock);
377a6388638Srmind 	kmem_free(chmp, sizeof(struct chfs_mount));
378a6388638Srmind 	kmem_free(ump, sizeof(struct ufsmount));
3796a9242deSriastradh 
3806a9242deSriastradh fail0:
3816a9242deSriastradh 	KASSERT(err);
382a6388638Srmind 	return err;
383060d2ed9Sahoka }
384060d2ed9Sahoka 
385060d2ed9Sahoka /* --------------------------------------------------------------------- */
386060d2ed9Sahoka 
387060d2ed9Sahoka static int
chfs_unmount(struct mount * mp,int mntflags)388060d2ed9Sahoka chfs_unmount(struct mount *mp, int mntflags)
389060d2ed9Sahoka {
390060d2ed9Sahoka 	int flags = 0, i = 0;
391060d2ed9Sahoka 	struct ufsmount *ump;
392060d2ed9Sahoka 	struct chfs_mount *chmp;
393060d2ed9Sahoka 
394060d2ed9Sahoka 	if (mntflags & MNT_FORCE)
395060d2ed9Sahoka 		flags |= FORCECLOSE;
396060d2ed9Sahoka 
397060d2ed9Sahoka 	dbg("[START]\n");
398060d2ed9Sahoka 
399060d2ed9Sahoka 	ump = VFSTOUFS(mp);
400060d2ed9Sahoka 	chmp = ump->um_chfs;
401060d2ed9Sahoka 
402aa98cbbaSttoth 	/* Stop GC. */
403060d2ed9Sahoka 	chfs_gc_thread_stop(chmp);
404060d2ed9Sahoka 
405aa98cbbaSttoth 	/* Flush everyt buffer. */
406060d2ed9Sahoka 	(void)vflush(mp, NULLVP, flags);
407060d2ed9Sahoka 
408060d2ed9Sahoka 	if (chmp->chm_wbuf_len) {
409060d2ed9Sahoka 		mutex_enter(&chmp->chm_lock_mountfields);
410060d2ed9Sahoka 		chfs_flush_pending_wbuf(chmp);
411060d2ed9Sahoka 		mutex_exit(&chmp->chm_lock_mountfields);
412060d2ed9Sahoka 	}
413060d2ed9Sahoka 
414aa98cbbaSttoth 	/* Free node references. */
415060d2ed9Sahoka 	for (i = 0; i < chmp->chm_ebh->peb_nr; i++) {
416060d2ed9Sahoka 		chfs_free_node_refs(&chmp->chm_blocks[i]);
417060d2ed9Sahoka 	}
418060d2ed9Sahoka 
419aa98cbbaSttoth 	/* Destroy vnode cache hashtable. */
420060d2ed9Sahoka 	chfs_vnocache_hash_destroy(chmp->chm_vnocache_hash);
421060d2ed9Sahoka 
422aa98cbbaSttoth 	/* Close eraseblock handler. */
423060d2ed9Sahoka 	ebh_close(chmp->chm_ebh);
424060d2ed9Sahoka 
425aa98cbbaSttoth 	/* Destroy mutexes. */
426060d2ed9Sahoka 	rw_destroy(&chmp->chm_lock_wbuf);
427060d2ed9Sahoka 	mutex_destroy(&chmp->chm_lock_vnocache);
428060d2ed9Sahoka 	mutex_destroy(&chmp->chm_lock_sizes);
429060d2ed9Sahoka 	mutex_destroy(&chmp->chm_lock_mountfields);
430060d2ed9Sahoka 
431aa98cbbaSttoth 	/* Unmount UFS. */
432060d2ed9Sahoka 	if (ump->um_devvp->v_type != VBAD) {
433ce2a78d3Shannken 		spec_node_setmountedfs(ump->um_devvp, NULL);
434060d2ed9Sahoka 	}
435060d2ed9Sahoka 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
436060d2ed9Sahoka 	(void)VOP_CLOSE(ump->um_devvp, FREAD|FWRITE, NOCRED);
437060d2ed9Sahoka 	vput(ump->um_devvp);
438060d2ed9Sahoka 
439060d2ed9Sahoka 	mutex_destroy(&ump->um_lock);
440060d2ed9Sahoka 
441aa98cbbaSttoth 	/* Everything done. */
442a6388638Srmind 	kmem_free(ump, sizeof(struct ufsmount));
443060d2ed9Sahoka 	mp->mnt_data = NULL;
444060d2ed9Sahoka 	mp->mnt_flag &= ~MNT_LOCAL;
445060d2ed9Sahoka 	dbg("[END]\n");
446060d2ed9Sahoka 	return (0);
447060d2ed9Sahoka }
448060d2ed9Sahoka 
449060d2ed9Sahoka /* --------------------------------------------------------------------- */
450060d2ed9Sahoka 
451060d2ed9Sahoka static int
chfs_root(struct mount * mp,int lktype,struct vnode ** vpp)45202443708Sad chfs_root(struct mount *mp, int lktype, struct vnode **vpp)
453060d2ed9Sahoka {
454060d2ed9Sahoka 	struct vnode *vp;
455060d2ed9Sahoka 	int error;
456060d2ed9Sahoka 
45702443708Sad 	if ((error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, lktype, &vp)) != 0)
458060d2ed9Sahoka 		return error;
459060d2ed9Sahoka 	*vpp = vp;
460060d2ed9Sahoka 	return 0;
461060d2ed9Sahoka }
462060d2ed9Sahoka 
463060d2ed9Sahoka /* --------------------------------------------------------------------- */
464060d2ed9Sahoka 
465060d2ed9Sahoka extern rb_tree_ops_t frag_rbtree_ops;
466060d2ed9Sahoka 
467060d2ed9Sahoka static int
chfs_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)46866996266Shannken chfs_loadvnode(struct mount *mp, struct vnode *vp,
46966996266Shannken     const void *key, size_t key_len, const void **new_key)
470060d2ed9Sahoka {
471060d2ed9Sahoka 	struct chfs_mount *chmp;
472060d2ed9Sahoka 	struct chfs_inode *ip;
473060d2ed9Sahoka 	struct ufsmount *ump;
474060d2ed9Sahoka 	dev_t dev;
475060d2ed9Sahoka 	int error;
476060d2ed9Sahoka 	struct chfs_vnode_cache* chvc = NULL;
477060d2ed9Sahoka 	struct chfs_node_ref* nref = NULL;
478060d2ed9Sahoka 	struct buf *bp;
47966996266Shannken 	ino_t ino;
48066996266Shannken 
48166996266Shannken 	KASSERT(key_len == sizeof(ino));
48266996266Shannken 	memcpy(&ino, key, key_len);
483060d2ed9Sahoka 
48491acea70Sagc 	dbg("vget() | ino: %llu\n", (unsigned long long)ino);
485060d2ed9Sahoka 
486060d2ed9Sahoka 	ump = VFSTOUFS(mp);
487060d2ed9Sahoka 	dev = ump->um_dev;
488060d2ed9Sahoka 
489060d2ed9Sahoka 	ip = pool_get(&chfs_inode_pool, PR_WAITOK);
490060d2ed9Sahoka 
491aa98cbbaSttoth 	/* Initialize vnode/inode. */
492060d2ed9Sahoka 	memset(ip, 0, sizeof(*ip));
493060d2ed9Sahoka 	ip->vp = vp;
494060d2ed9Sahoka 	ip->ump = ump;
495060d2ed9Sahoka 	ip->chmp = chmp = ump->um_chfs;
496060d2ed9Sahoka 	ip->dev = dev;
497060d2ed9Sahoka 	ip->ino = ino;
498060d2ed9Sahoka 
499060d2ed9Sahoka 	rb_tree_init(&ip->fragtree, &frag_rbtree_ops);
500060d2ed9Sahoka 
50166996266Shannken 	vp->v_tag = VT_CHFS;
50266996266Shannken 	vp->v_op = chfs_vnodeop_p;
50366996266Shannken 	if (ino == CHFS_ROOTINO)
50466996266Shannken 		vp->v_vflag |= VV_ROOT;
50566996266Shannken 	vp->v_data = ip;
506060d2ed9Sahoka 
507aa98cbbaSttoth 	/* Set root inode. */
508060d2ed9Sahoka 	if (ino == CHFS_ROOTINO) {
509060d2ed9Sahoka 		dbg("SETROOT\n");
510060d2ed9Sahoka 		vp->v_type = VDIR;
511e6a23d44Sttoth 		ip->ch_type = CHT_DIR;
512060d2ed9Sahoka 		ip->mode = IFMT | IEXEC | IWRITE | IREAD;
513060d2ed9Sahoka 		ip->iflag |= (IN_ACCESS | IN_CHANGE | IN_UPDATE);
514060d2ed9Sahoka 		chfs_update(vp, NULL, NULL, UPDATE_WAIT);
515060d2ed9Sahoka 		TAILQ_INIT(&ip->dents);
516060d2ed9Sahoka 		chfs_set_vnode_size(vp, 512);
517060d2ed9Sahoka 	}
518060d2ed9Sahoka 
519060d2ed9Sahoka 	mutex_enter(&chmp->chm_lock_vnocache);
520060d2ed9Sahoka 	chvc = chfs_vnode_cache_get(chmp, ino);
521060d2ed9Sahoka 	mutex_exit(&chmp->chm_lock_vnocache);
522060d2ed9Sahoka 	if (!chvc) {
523060d2ed9Sahoka 		dbg("!chvc\n");
524aa98cbbaSttoth 		/* Initialize the corresponding vnode cache. */
525060d2ed9Sahoka 		/* XXX, we cant alloc under a lock, refactor this! */
526060d2ed9Sahoka 		chvc = chfs_vnode_cache_alloc(ino);
527060d2ed9Sahoka 		mutex_enter(&chmp->chm_lock_vnocache);
528060d2ed9Sahoka 		if (ino == CHFS_ROOTINO) {
529060d2ed9Sahoka 			chvc->nlink = 2;
530060d2ed9Sahoka 			chvc->pvno = CHFS_ROOTINO;
5317982b4ffSttoth 			chvc->state = VNO_STATE_CHECKEDABSENT;
532060d2ed9Sahoka 		}
533060d2ed9Sahoka 		chfs_vnode_cache_add(chmp, chvc);
534060d2ed9Sahoka 		mutex_exit(&chmp->chm_lock_vnocache);
535060d2ed9Sahoka 
536060d2ed9Sahoka 		ip->chvc = chvc;
537060d2ed9Sahoka 		TAILQ_INIT(&ip->dents);
538060d2ed9Sahoka 	} else {
539060d2ed9Sahoka 		dbg("chvc\n");
540060d2ed9Sahoka 		ip->chvc = chvc;
541aa98cbbaSttoth 		/* We had a vnode cache, the node is already on flash, so read it */
542060d2ed9Sahoka 		if (ino == CHFS_ROOTINO) {
543060d2ed9Sahoka 			chvc->pvno = CHFS_ROOTINO;
544060d2ed9Sahoka 			TAILQ_INIT(&chvc->scan_dirents);
545060d2ed9Sahoka 		} else {
546060d2ed9Sahoka 			chfs_readvnode(mp, ino, &vp);
547060d2ed9Sahoka 		}
548060d2ed9Sahoka 
549060d2ed9Sahoka 		mutex_enter(&chmp->chm_lock_mountfields);
550aa98cbbaSttoth 		/* Initialize type specific things. */
55166996266Shannken 		error = 0;
552e6a23d44Sttoth 		switch (ip->ch_type) {
553e6a23d44Sttoth 		case CHT_DIR:
554aa98cbbaSttoth 			/* Read every dirent. */
555060d2ed9Sahoka 			nref = chvc->dirents;
556060d2ed9Sahoka 			while (nref &&
557060d2ed9Sahoka 			    (struct chfs_vnode_cache *)nref != chvc) {
558060d2ed9Sahoka 				chfs_readdirent(mp, nref, ip);
559060d2ed9Sahoka 				nref = nref->nref_next;
560060d2ed9Sahoka 			}
561060d2ed9Sahoka 			chfs_set_vnode_size(vp, 512);
562060d2ed9Sahoka 			break;
563e6a23d44Sttoth 		case CHT_REG:
564aa98cbbaSttoth 			/* FALLTHROUGH */
565e6a23d44Sttoth 		case CHT_SOCK:
566aa98cbbaSttoth 			/* Collect data. */
56791acea70Sagc 			dbg("read_inode_internal | ino: %llu\n",
56891acea70Sagc 				(unsigned long long)ip->ino);
569060d2ed9Sahoka 			error = chfs_read_inode(chmp, ip);
570060d2ed9Sahoka 			break;
571e6a23d44Sttoth 		case CHT_LNK:
572aa98cbbaSttoth 			/* Collect data. */
57391acea70Sagc 			dbg("read_inode_internal | ino: %llu\n",
57491acea70Sagc 				(unsigned long long)ip->ino);
575060d2ed9Sahoka 			error = chfs_read_inode_internal(chmp, ip);
57666996266Shannken 			if (error)
57766996266Shannken 				break;
578060d2ed9Sahoka 
579aa98cbbaSttoth 			/* Set link. */
58091acea70Sagc 			dbg("size: %llu\n", (unsigned long long)ip->size);
581060d2ed9Sahoka 			bp = getiobuf(vp, true);
582060d2ed9Sahoka 			bp->b_blkno = 0;
583060d2ed9Sahoka 			bp->b_bufsize = bp->b_resid =
584060d2ed9Sahoka 			    bp->b_bcount = ip->size;
585060d2ed9Sahoka 			bp->b_data = kmem_alloc(ip->size, KM_SLEEP);
586060d2ed9Sahoka 			chfs_read_data(chmp, vp, bp);
587060d2ed9Sahoka 			if (!ip->target)
588060d2ed9Sahoka 				ip->target = kmem_alloc(ip->size,
589060d2ed9Sahoka 				    KM_SLEEP);
590060d2ed9Sahoka 			memcpy(ip->target, bp->b_data, ip->size);
591060d2ed9Sahoka 			kmem_free(bp->b_data, ip->size);
592060d2ed9Sahoka 			putiobuf(bp);
593060d2ed9Sahoka 
594060d2ed9Sahoka 			break;
595e6a23d44Sttoth 		case CHT_CHR:
596aa98cbbaSttoth 			/* FALLTHROUGH */
597e6a23d44Sttoth 		case CHT_BLK:
598aa98cbbaSttoth 			/* FALLTHROUGH */
599e6a23d44Sttoth 		case CHT_FIFO:
600aa98cbbaSttoth 			/* Collect data. */
60191acea70Sagc 			dbg("read_inode_internal | ino: %llu\n",
60291acea70Sagc 				(unsigned long long)ip->ino);
603060d2ed9Sahoka 			error = chfs_read_inode_internal(chmp, ip);
60466996266Shannken 			if (error)
60566996266Shannken 				break;
606060d2ed9Sahoka 
607aa98cbbaSttoth 			/* Set device. */
608060d2ed9Sahoka 			bp = getiobuf(vp, true);
609060d2ed9Sahoka 			bp->b_blkno = 0;
610060d2ed9Sahoka 			bp->b_bufsize = bp->b_resid =
611060d2ed9Sahoka 			    bp->b_bcount = sizeof(dev_t);
612060d2ed9Sahoka 			bp->b_data = kmem_alloc(sizeof(dev_t), KM_SLEEP);
613060d2ed9Sahoka 			chfs_read_data(chmp, vp, bp);
614060d2ed9Sahoka 			memcpy(&ip->rdev,
615060d2ed9Sahoka 			    bp->b_data, sizeof(dev_t));
616060d2ed9Sahoka 			kmem_free(bp->b_data, sizeof(dev_t));
617060d2ed9Sahoka 			putiobuf(bp);
618aa98cbbaSttoth 			/* Set specific operations. */
619e6a23d44Sttoth 			if (ip->ch_type == CHT_FIFO) {
620060d2ed9Sahoka 				vp->v_op = chfs_fifoop_p;
621e6a23d44Sttoth 			} else {
622060d2ed9Sahoka 				vp->v_op = chfs_specop_p;
623060d2ed9Sahoka 				spec_node_init(vp, ip->rdev);
624060d2ed9Sahoka 			}
625060d2ed9Sahoka 
626060d2ed9Sahoka 		    break;
627e6a23d44Sttoth 		case CHT_BLANK:
628aa98cbbaSttoth 			/* FALLTHROUGH */
629e6a23d44Sttoth 		case CHT_BAD:
630060d2ed9Sahoka 			break;
631060d2ed9Sahoka 		}
632060d2ed9Sahoka 		mutex_exit(&chmp->chm_lock_mountfields);
63366996266Shannken 		if (error) {
63466996266Shannken 			vp->v_data = NULL;
63566996266Shannken 			KASSERT(TAILQ_FIRST(&ip->dents) == NULL);
63666996266Shannken 			pool_put(&chfs_inode_pool, ip);
63766996266Shannken 			return error;
63866996266Shannken 		}
639060d2ed9Sahoka 
640060d2ed9Sahoka 	}
641060d2ed9Sahoka 
64231b7cf32Smsaitoh 	/* Finish inode initialization. */
64366996266Shannken 	ip->ch_type = VTTOCHT(vp->v_type);
644060d2ed9Sahoka 	ip->devvp = ump->um_devvp;
645060d2ed9Sahoka 	vref(ip->devvp);
646060d2ed9Sahoka 
64766996266Shannken 	genfs_node_init(vp, &chfs_genfsops);
648060d2ed9Sahoka 	uvm_vnp_setsize(vp, ip->size);
64966996266Shannken 
65066996266Shannken 	*new_key = &ip->ino;
651060d2ed9Sahoka 
652060d2ed9Sahoka 	return 0;
653060d2ed9Sahoka }
654060d2ed9Sahoka 
655060d2ed9Sahoka /* --------------------------------------------------------------------- */
656060d2ed9Sahoka 
657060d2ed9Sahoka static int
chfs_vget(struct mount * mp,ino_t ino,int lktype,struct vnode ** vpp)65802443708Sad chfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
65966996266Shannken {
66066996266Shannken 	int error;
66166996266Shannken 
66266996266Shannken 	error = vcache_get(mp, &ino, sizeof(ino), vpp);
66366996266Shannken 	if (error)
66466996266Shannken 		return error;
66566996266Shannken 
66602443708Sad 	error = vn_lock(*vpp, lktype);
66766996266Shannken 	if (error) {
66866996266Shannken 		vrele(*vpp);
66966996266Shannken 		*vpp = NULL;
67066996266Shannken 		return error;
67166996266Shannken 	}
67266996266Shannken 
67366996266Shannken 	return 0;
67466996266Shannken }
67566996266Shannken 
67666996266Shannken /* --------------------------------------------------------------------- */
67766996266Shannken 
67866996266Shannken 
67966996266Shannken static int
chfs_fhtovp(struct mount * mp,struct fid * fhp,int lktype,struct vnode ** vpp)68002443708Sad chfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
681060d2ed9Sahoka {
682060d2ed9Sahoka 	return ENODEV;
683060d2ed9Sahoka }
684060d2ed9Sahoka 
685060d2ed9Sahoka /* --------------------------------------------------------------------- */
686060d2ed9Sahoka 
687060d2ed9Sahoka static int
chfs_vptofh(struct vnode * vp,struct fid * fhp,size_t * fh_size)688060d2ed9Sahoka chfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
689060d2ed9Sahoka {
690060d2ed9Sahoka 	return ENODEV;
691060d2ed9Sahoka }
692060d2ed9Sahoka 
693060d2ed9Sahoka /* --------------------------------------------------------------------- */
694060d2ed9Sahoka 
695060d2ed9Sahoka static int
chfs_start(struct mount * mp,int flags)696060d2ed9Sahoka chfs_start(struct mount *mp, int flags)
697060d2ed9Sahoka {
698060d2ed9Sahoka 	return 0;
699060d2ed9Sahoka }
700060d2ed9Sahoka 
701060d2ed9Sahoka /* --------------------------------------------------------------------- */
702060d2ed9Sahoka 
703060d2ed9Sahoka static int
chfs_statvfs(struct mount * mp,struct statvfs * sbp)704060d2ed9Sahoka chfs_statvfs(struct mount *mp, struct statvfs *sbp)
705060d2ed9Sahoka {
706060d2ed9Sahoka  	struct chfs_mount *chmp;
707060d2ed9Sahoka 	struct ufsmount *ump;
708060d2ed9Sahoka 	dbg("statvfs\n");
709060d2ed9Sahoka 
710060d2ed9Sahoka 	ump = VFSTOUFS(mp);
711060d2ed9Sahoka 	chmp = ump->um_chfs;
712060d2ed9Sahoka 
713060d2ed9Sahoka 	sbp->f_flag   = mp->mnt_flag;
714060d2ed9Sahoka 	sbp->f_bsize  = chmp->chm_ebh->eb_size;
715060d2ed9Sahoka 	sbp->f_frsize = chmp->chm_ebh->eb_size;
716060d2ed9Sahoka 	sbp->f_iosize = chmp->chm_ebh->eb_size;
717060d2ed9Sahoka 
718060d2ed9Sahoka 	sbp->f_blocks = chmp->chm_ebh->peb_nr;
719060d2ed9Sahoka 	sbp->f_files  = 0;
720060d2ed9Sahoka 	sbp->f_bavail = chmp->chm_nr_free_blocks - chmp->chm_resv_blocks_write;
721060d2ed9Sahoka 
722060d2ed9Sahoka 	sbp->f_bfree = chmp->chm_nr_free_blocks;
723060d2ed9Sahoka 	sbp->f_bresvd = chmp->chm_resv_blocks_write;
724060d2ed9Sahoka 
725060d2ed9Sahoka 	/* FFS specific */
726060d2ed9Sahoka 	sbp->f_ffree  = 0;
727060d2ed9Sahoka 	sbp->f_favail = 0;
728060d2ed9Sahoka 	sbp->f_fresvd = 0;
729060d2ed9Sahoka 
730060d2ed9Sahoka 	copy_statvfs_info(sbp, mp);
731060d2ed9Sahoka 
732060d2ed9Sahoka 	return 0;
733060d2ed9Sahoka }
734060d2ed9Sahoka 
735060d2ed9Sahoka /* --------------------------------------------------------------------- */
736060d2ed9Sahoka 
737060d2ed9Sahoka static int
chfs_sync(struct mount * mp,int waitfor,kauth_cred_t uc)738060d2ed9Sahoka chfs_sync(struct mount *mp, int waitfor,
739060d2ed9Sahoka     kauth_cred_t uc)
740060d2ed9Sahoka {
741060d2ed9Sahoka 	return 0;
742060d2ed9Sahoka }
743060d2ed9Sahoka 
744060d2ed9Sahoka /* --------------------------------------------------------------------- */
745060d2ed9Sahoka 
746060d2ed9Sahoka static void
chfs_init(void)747060d2ed9Sahoka chfs_init(void)
748060d2ed9Sahoka {
749aa98cbbaSttoth 	/* Initialize pools and inode hash. */
750060d2ed9Sahoka 	chfs_alloc_pool_caches();
751060d2ed9Sahoka 	pool_init(&chfs_inode_pool, sizeof(struct chfs_inode), 0, 0, 0,
752060d2ed9Sahoka 	    "chfsinopl", &pool_allocator_nointr, IPL_NONE);
753060d2ed9Sahoka 	ufs_init();
754060d2ed9Sahoka }
755060d2ed9Sahoka 
756060d2ed9Sahoka /* --------------------------------------------------------------------- */
757060d2ed9Sahoka 
758060d2ed9Sahoka static void
chfs_reinit(void)759060d2ed9Sahoka chfs_reinit(void)
760060d2ed9Sahoka {
761060d2ed9Sahoka 	ufs_reinit();
762060d2ed9Sahoka }
763060d2ed9Sahoka 
764060d2ed9Sahoka /* --------------------------------------------------------------------- */
765060d2ed9Sahoka 
766060d2ed9Sahoka static void
chfs_done(void)767060d2ed9Sahoka chfs_done(void)
768060d2ed9Sahoka {
769060d2ed9Sahoka 	ufs_done();
770060d2ed9Sahoka 	pool_destroy(&chfs_inode_pool);
771060d2ed9Sahoka 	chfs_destroy_pool_caches();
772060d2ed9Sahoka }
773060d2ed9Sahoka 
774060d2ed9Sahoka /* --------------------------------------------------------------------- */
775060d2ed9Sahoka 
776060d2ed9Sahoka static int
chfs_snapshot(struct mount * mp,struct vnode * vp,struct timespec * ctime)777060d2ed9Sahoka chfs_snapshot(struct mount *mp, struct vnode *vp,
778060d2ed9Sahoka     struct timespec *ctime)
779060d2ed9Sahoka {
780060d2ed9Sahoka 	return ENODEV;
781060d2ed9Sahoka }
782060d2ed9Sahoka 
783060d2ed9Sahoka /* --------------------------------------------------------------------- */
784060d2ed9Sahoka 
785060d2ed9Sahoka /*
786060d2ed9Sahoka  * chfs vfs operations.
787060d2ed9Sahoka  */
788060d2ed9Sahoka 
789060d2ed9Sahoka extern const struct vnodeopv_desc chfs_fifoop_opv_desc;
790060d2ed9Sahoka extern const struct vnodeopv_desc chfs_specop_opv_desc;
791060d2ed9Sahoka extern const struct vnodeopv_desc chfs_vnodeop_opv_desc;
792060d2ed9Sahoka 
793060d2ed9Sahoka const struct vnodeopv_desc * const chfs_vnodeopv_descs[] = {
794060d2ed9Sahoka 	&chfs_fifoop_opv_desc,
795060d2ed9Sahoka 	&chfs_specop_opv_desc,
796060d2ed9Sahoka 	&chfs_vnodeop_opv_desc,
797060d2ed9Sahoka 	NULL,
798060d2ed9Sahoka };
799060d2ed9Sahoka 
800060d2ed9Sahoka struct vfsops chfs_vfsops = {
80197116d3bShannken 	.vfs_name = MOUNT_CHFS,
80297116d3bShannken 	.vfs_min_mount_data = sizeof (struct chfs_args),
80397116d3bShannken 	.vfs_mount = chfs_mount,
80497116d3bShannken 	.vfs_start = chfs_start,
80597116d3bShannken 	.vfs_unmount = chfs_unmount,
80697116d3bShannken 	.vfs_root = chfs_root,
80797116d3bShannken 	.vfs_quotactl = ufs_quotactl,
80897116d3bShannken 	.vfs_statvfs = chfs_statvfs,
80997116d3bShannken 	.vfs_sync = chfs_sync,
81097116d3bShannken 	.vfs_vget = chfs_vget,
81166996266Shannken 	.vfs_loadvnode = chfs_loadvnode,
81297116d3bShannken 	.vfs_fhtovp = chfs_fhtovp,
81397116d3bShannken 	.vfs_vptofh = chfs_vptofh,
81497116d3bShannken 	.vfs_init = chfs_init,
81597116d3bShannken 	.vfs_reinit = chfs_reinit,
81697116d3bShannken 	.vfs_done = chfs_done,
81797116d3bShannken 	.vfs_snapshot = chfs_snapshot,
81897116d3bShannken 	.vfs_extattrctl = vfs_stdextattrctl,
819c0f141edShannken 	.vfs_suspendctl = genfs_suspendctl,
82097116d3bShannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
82197116d3bShannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
82297116d3bShannken 	.vfs_fsync = (void *)eopnotsupp,
82397116d3bShannken 	.vfs_opv_descs = chfs_vnodeopv_descs
824060d2ed9Sahoka };
825060d2ed9Sahoka 
826aa98cbbaSttoth /* For using CHFS as a module. */
82728dab56dSpgoyette 
82828dab56dSpgoyette MODULE(MODULE_CLASS_VFS, chfs, "ufs,flash");
82928dab56dSpgoyette 
830060d2ed9Sahoka static int
chfs_modcmd(modcmd_t cmd,void * arg)831060d2ed9Sahoka chfs_modcmd(modcmd_t cmd, void *arg)
832060d2ed9Sahoka {
833060d2ed9Sahoka 	switch (cmd) {
834060d2ed9Sahoka 	case MODULE_CMD_INIT:
835060d2ed9Sahoka 		return vfs_attach(&chfs_vfsops);
836060d2ed9Sahoka 	case MODULE_CMD_FINI:
837060d2ed9Sahoka 		return vfs_detach(&chfs_vfsops);
838060d2ed9Sahoka 	default:
839060d2ed9Sahoka 		return ENOTTY;
840060d2ed9Sahoka 	}
841060d2ed9Sahoka }
842