xref: /dflybsd-src/sys/vfs/hammer/hammer_vfsops.c (revision 10a5d1ba1fb5faaa67ccb08651ff39f1208a4a82)
1427e5fc6SMatthew Dillon /*
2b84de5afSMatthew Dillon  * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
3427e5fc6SMatthew Dillon  *
4427e5fc6SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5427e5fc6SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6427e5fc6SMatthew Dillon  *
7427e5fc6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8427e5fc6SMatthew Dillon  * modification, are permitted provided that the following conditions
9427e5fc6SMatthew Dillon  * are met:
10427e5fc6SMatthew Dillon  *
11427e5fc6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12427e5fc6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13427e5fc6SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14427e5fc6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15427e5fc6SMatthew Dillon  *    the documentation and/or other materials provided with the
16427e5fc6SMatthew Dillon  *    distribution.
17427e5fc6SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18427e5fc6SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19427e5fc6SMatthew Dillon  *    from this software without specific, prior written permission.
20427e5fc6SMatthew Dillon  *
21427e5fc6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22427e5fc6SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23427e5fc6SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24427e5fc6SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25427e5fc6SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26427e5fc6SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27427e5fc6SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28427e5fc6SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29427e5fc6SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30427e5fc6SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31427e5fc6SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32427e5fc6SMatthew Dillon  * SUCH DAMAGE.
33427e5fc6SMatthew Dillon  *
34*10a5d1baSMatthew Dillon  * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.27 2008/04/25 21:49:49 dillon Exp $
35427e5fc6SMatthew Dillon  */
36427e5fc6SMatthew Dillon 
37427e5fc6SMatthew Dillon #include <sys/param.h>
38427e5fc6SMatthew Dillon #include <sys/systm.h>
39427e5fc6SMatthew Dillon #include <sys/kernel.h>
40427e5fc6SMatthew Dillon #include <sys/vnode.h>
41427e5fc6SMatthew Dillon #include <sys/mount.h>
42427e5fc6SMatthew Dillon #include <sys/malloc.h>
43427e5fc6SMatthew Dillon #include <sys/nlookup.h>
44427e5fc6SMatthew Dillon #include <sys/fcntl.h>
45b3deaf57SMatthew Dillon #include <sys/sysctl.h>
46427e5fc6SMatthew Dillon #include <sys/buf.h>
47427e5fc6SMatthew Dillon #include <sys/buf2.h>
48427e5fc6SMatthew Dillon #include "hammer.h"
49427e5fc6SMatthew Dillon 
50d5ef456eSMatthew Dillon int hammer_debug_general;
517d683b0fSMatthew Dillon int hammer_debug_locks;
52b3deaf57SMatthew Dillon int hammer_debug_btree;
53d113fda1SMatthew Dillon int hammer_debug_tid;
5446fe7ae1SMatthew Dillon int hammer_debug_recover;	/* -1 will disable, +1 will force */
5546fe7ae1SMatthew Dillon int hammer_debug_recover_faults;
56b3deaf57SMatthew Dillon int hammer_count_inodes;
57b3deaf57SMatthew Dillon int hammer_count_records;
58b3deaf57SMatthew Dillon int hammer_count_record_datas;
59b3deaf57SMatthew Dillon int hammer_count_volumes;
60b3deaf57SMatthew Dillon int hammer_count_buffers;
61b3deaf57SMatthew Dillon int hammer_count_nodes;
627d683b0fSMatthew Dillon int64_t hammer_contention_count;
63f03c9cf4SMatthew Dillon int64_t hammer_zone_limit;
64b3deaf57SMatthew Dillon 
65b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
66d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
67d5ef456eSMatthew Dillon 	   &hammer_debug_general, 0, "");
687d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
697d683b0fSMatthew Dillon 	   &hammer_debug_locks, 0, "");
70b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
71b3deaf57SMatthew Dillon 	   &hammer_debug_btree, 0, "");
72d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
73d113fda1SMatthew Dillon 	   &hammer_debug_tid, 0, "");
74b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
75b33e2cc0SMatthew Dillon 	   &hammer_debug_recover, 0, "");
7646fe7ae1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
7746fe7ae1SMatthew Dillon 	   &hammer_debug_recover_faults, 0, "");
78b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
79b3deaf57SMatthew Dillon 	   &hammer_count_inodes, 0, "");
80b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
81b3deaf57SMatthew Dillon 	   &hammer_count_records, 0, "");
82b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
83b3deaf57SMatthew Dillon 	   &hammer_count_record_datas, 0, "");
84b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
85b3deaf57SMatthew Dillon 	   &hammer_count_volumes, 0, "");
86b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
87b3deaf57SMatthew Dillon 	   &hammer_count_buffers, 0, "");
88b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
89b3deaf57SMatthew Dillon 	   &hammer_count_nodes, 0, "");
90f03c9cf4SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
91f03c9cf4SMatthew Dillon 	   &hammer_zone_limit, 0, "");
927d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
937d683b0fSMatthew Dillon 	   &hammer_contention_count, 0, "");
94b3deaf57SMatthew Dillon 
95427e5fc6SMatthew Dillon /*
96427e5fc6SMatthew Dillon  * VFS ABI
97427e5fc6SMatthew Dillon  */
98427e5fc6SMatthew Dillon static void	hammer_free_hmp(struct mount *mp);
99427e5fc6SMatthew Dillon 
100427e5fc6SMatthew Dillon static int	hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
101427e5fc6SMatthew Dillon 				struct ucred *cred);
102427e5fc6SMatthew Dillon static int	hammer_vfs_unmount(struct mount *mp, int mntflags);
103427e5fc6SMatthew Dillon static int	hammer_vfs_root(struct mount *mp, struct vnode **vpp);
104427e5fc6SMatthew Dillon static int	hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
105427e5fc6SMatthew Dillon 				struct ucred *cred);
106427e5fc6SMatthew Dillon static int	hammer_vfs_sync(struct mount *mp, int waitfor);
107513ca7d7SMatthew Dillon static int	hammer_vfs_vget(struct mount *mp, ino_t ino,
108513ca7d7SMatthew Dillon 				struct vnode **vpp);
109427e5fc6SMatthew Dillon static int	hammer_vfs_init(struct vfsconf *conf);
110513ca7d7SMatthew Dillon static int	hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
111513ca7d7SMatthew Dillon 				struct vnode **vpp);
112513ca7d7SMatthew Dillon static int	hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
113513ca7d7SMatthew Dillon static int	hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
114513ca7d7SMatthew Dillon 				int *exflagsp, struct ucred **credanonp);
115513ca7d7SMatthew Dillon 
116427e5fc6SMatthew Dillon 
117427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = {
118427e5fc6SMatthew Dillon 	.vfs_mount	= hammer_vfs_mount,
119427e5fc6SMatthew Dillon 	.vfs_unmount	= hammer_vfs_unmount,
120427e5fc6SMatthew Dillon 	.vfs_root 	= hammer_vfs_root,
121427e5fc6SMatthew Dillon 	.vfs_statfs	= hammer_vfs_statfs,
122427e5fc6SMatthew Dillon 	.vfs_sync	= hammer_vfs_sync,
123427e5fc6SMatthew Dillon 	.vfs_vget	= hammer_vfs_vget,
124513ca7d7SMatthew Dillon 	.vfs_init	= hammer_vfs_init,
125513ca7d7SMatthew Dillon 	.vfs_vptofh	= hammer_vfs_vptofh,
126513ca7d7SMatthew Dillon 	.vfs_fhtovp	= hammer_vfs_fhtovp,
127513ca7d7SMatthew Dillon 	.vfs_checkexp	= hammer_vfs_checkexp
128427e5fc6SMatthew Dillon };
129427e5fc6SMatthew Dillon 
130427e5fc6SMatthew Dillon MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
131427e5fc6SMatthew Dillon 
132427e5fc6SMatthew Dillon VFS_SET(hammer_vfsops, hammer, 0);
133427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1);
134427e5fc6SMatthew Dillon 
135427e5fc6SMatthew Dillon static int
136427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf)
137427e5fc6SMatthew Dillon {
13847197d71SMatthew Dillon 	/*hammer_init_alist_config();*/
139427e5fc6SMatthew Dillon 	return(0);
140427e5fc6SMatthew Dillon }
141427e5fc6SMatthew Dillon 
142427e5fc6SMatthew Dillon static int
143427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
144427e5fc6SMatthew Dillon 		 struct ucred *cred)
145427e5fc6SMatthew Dillon {
146427e5fc6SMatthew Dillon 	struct hammer_mount_info info;
147a89aec1bSMatthew Dillon 	hammer_mount_t hmp;
148a89aec1bSMatthew Dillon 	hammer_volume_t rootvol;
14927ea2398SMatthew Dillon 	struct vnode *rootvp;
150427e5fc6SMatthew Dillon 	const char *upath;	/* volume name in userspace */
151427e5fc6SMatthew Dillon 	char *path;		/* volume name in system space */
152427e5fc6SMatthew Dillon 	int error;
153427e5fc6SMatthew Dillon 	int i;
154427e5fc6SMatthew Dillon 
155427e5fc6SMatthew Dillon 	if ((error = copyin(data, &info, sizeof(info))) != 0)
156427e5fc6SMatthew Dillon 		return (error);
157427e5fc6SMatthew Dillon 	if (info.nvolumes <= 0 || info.nvolumes >= 32768)
158427e5fc6SMatthew Dillon 		return (EINVAL);
159427e5fc6SMatthew Dillon 
160427e5fc6SMatthew Dillon 	/*
161427e5fc6SMatthew Dillon 	 * Interal mount data structure
162427e5fc6SMatthew Dillon 	 */
163195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
164195c19a1SMatthew Dillon 		hmp = (void *)mp->mnt_data;
165195c19a1SMatthew Dillon 		KKASSERT(hmp != NULL);
166195c19a1SMatthew Dillon 	} else {
167427e5fc6SMatthew Dillon 		hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
168427e5fc6SMatthew Dillon 		mp->mnt_data = (qaddr_t)hmp;
169427e5fc6SMatthew Dillon 		hmp->mp = mp;
170195c19a1SMatthew Dillon 		hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
171195c19a1SMatthew Dillon 		hmp->namekey_iterator = mycpu->gd_time_seconds;
17246fe7ae1SMatthew Dillon 		/*TAILQ_INIT(&hmp->recycle_list);*/
17347197d71SMatthew Dillon 
17447197d71SMatthew Dillon 		hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
17547197d71SMatthew Dillon 		hmp->root_btree_beg.key = -0x8000000000000000LL;
17647197d71SMatthew Dillon 		hmp->root_btree_beg.create_tid = 1;
17747197d71SMatthew Dillon 		hmp->root_btree_beg.delete_tid = 1;
17847197d71SMatthew Dillon 		hmp->root_btree_beg.rec_type = 0;
17947197d71SMatthew Dillon 		hmp->root_btree_beg.obj_type = 0;
18047197d71SMatthew Dillon 
18147197d71SMatthew Dillon 		hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
18247197d71SMatthew Dillon 		hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
18347197d71SMatthew Dillon 		hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
18447197d71SMatthew Dillon 		hmp->root_btree_end.delete_tid = 0;   /* special case */
18547197d71SMatthew Dillon 		hmp->root_btree_end.rec_type = 0xFFFFU;
18647197d71SMatthew Dillon 		hmp->root_btree_end.obj_type = 0;
18740043e7fSMatthew Dillon 		lockinit(&hmp->blockmap_lock, "blkmap", 0, 0);
188f03c9cf4SMatthew Dillon 
189059819e3SMatthew Dillon 		TAILQ_INIT(&hmp->flush_list);
190059819e3SMatthew Dillon 
191f03c9cf4SMatthew Dillon 		for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
192f03c9cf4SMatthew Dillon 			hmp->zone_limits[i] =
193f03c9cf4SMatthew Dillon 				HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT);
194f03c9cf4SMatthew Dillon 			/*
195f03c9cf4SMatthew Dillon 			 * Sysctl override for debugging (force the zone
196f03c9cf4SMatthew Dillon 			 * the cycle more quickly then every 2^60 bytes).
197f03c9cf4SMatthew Dillon 			 */
198f03c9cf4SMatthew Dillon 			if (hammer_zone_limit) {
199f03c9cf4SMatthew Dillon 				hmp->zone_limits[i] =
200f03c9cf4SMatthew Dillon 				    HAMMER_ZONE_ENCODE(i, hammer_zone_limit);
201f03c9cf4SMatthew Dillon 			}
202bf686dbeSMatthew Dillon 			hammer_init_holes(hmp, &hmp->holes[i]);
203f03c9cf4SMatthew Dillon 		}
204195c19a1SMatthew Dillon 	}
205195c19a1SMatthew Dillon 	hmp->hflags = info.hflags;
2067f7c1f84SMatthew Dillon 	if (info.asof) {
2077f7c1f84SMatthew Dillon 		mp->mnt_flag |= MNT_RDONLY;
2087f7c1f84SMatthew Dillon 		hmp->asof = info.asof;
2097f7c1f84SMatthew Dillon 	} else {
2107f7c1f84SMatthew Dillon 		hmp->asof = HAMMER_MAX_TID;
2117f7c1f84SMatthew Dillon 	}
212195c19a1SMatthew Dillon 
213195c19a1SMatthew Dillon 	/*
214195c19a1SMatthew Dillon 	 * Re-open read-write if originally read-only, or vise-versa XXX
215195c19a1SMatthew Dillon 	 */
216195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
217195c19a1SMatthew Dillon 		if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
218195c19a1SMatthew Dillon 			kprintf("HAMMER read-write -> read-only XXX\n");
219195c19a1SMatthew Dillon 			hmp->ronly = 1;
220195c19a1SMatthew Dillon 		} else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
221195c19a1SMatthew Dillon 			kprintf("HAMMER read-only -> read-write XXX\n");
222195c19a1SMatthew Dillon 			hmp->ronly = 0;
223195c19a1SMatthew Dillon 		}
224195c19a1SMatthew Dillon 		return(0);
225195c19a1SMatthew Dillon 	}
226195c19a1SMatthew Dillon 
227427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_vols_root);
228427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_inos_root);
22940043e7fSMatthew Dillon 	RB_INIT(&hmp->rb_nods_root);
230195c19a1SMatthew Dillon 	hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
231427e5fc6SMatthew Dillon 
232*10a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->volu_list);
233*10a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->undo_list);
234*10a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->data_list);
235*10a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->meta_list);
236*10a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->lose_list);
237*10a5d1baSMatthew Dillon 
238427e5fc6SMatthew Dillon 	/*
239427e5fc6SMatthew Dillon 	 * Load volumes
240427e5fc6SMatthew Dillon 	 */
241427e5fc6SMatthew Dillon 	path = objcache_get(namei_oc, M_WAITOK);
242d26d0ae9SMatthew Dillon 	hmp->nvolumes = info.nvolumes;
243427e5fc6SMatthew Dillon 	for (i = 0; i < info.nvolumes; ++i) {
244427e5fc6SMatthew Dillon 		error = copyin(&info.volumes[i], &upath, sizeof(char *));
245427e5fc6SMatthew Dillon 		if (error == 0)
246427e5fc6SMatthew Dillon 			error = copyinstr(upath, path, MAXPATHLEN, NULL);
247427e5fc6SMatthew Dillon 		if (error == 0)
2488cd0a023SMatthew Dillon 			error = hammer_install_volume(hmp, path);
249427e5fc6SMatthew Dillon 		if (error)
250427e5fc6SMatthew Dillon 			break;
251427e5fc6SMatthew Dillon 	}
252427e5fc6SMatthew Dillon 	objcache_put(namei_oc, path);
253427e5fc6SMatthew Dillon 
254427e5fc6SMatthew Dillon 	/*
255427e5fc6SMatthew Dillon 	 * Make sure we found a root volume
256427e5fc6SMatthew Dillon 	 */
257427e5fc6SMatthew Dillon 	if (error == 0 && hmp->rootvol == NULL) {
258427e5fc6SMatthew Dillon 		kprintf("hammer_mount: No root volume found!\n");
259427e5fc6SMatthew Dillon 		error = EINVAL;
260427e5fc6SMatthew Dillon 	}
261427e5fc6SMatthew Dillon 	if (error) {
262427e5fc6SMatthew Dillon 		hammer_free_hmp(mp);
263427e5fc6SMatthew Dillon 		return (error);
264427e5fc6SMatthew Dillon 	}
265427e5fc6SMatthew Dillon 
266427e5fc6SMatthew Dillon 	/*
26727ea2398SMatthew Dillon 	 * No errors, setup enough of the mount point so we can lookup the
26827ea2398SMatthew Dillon 	 * root vnode.
269427e5fc6SMatthew Dillon 	 */
270427e5fc6SMatthew Dillon 	mp->mnt_iosize_max = MAXPHYS;
271427e5fc6SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_FSMID;
272c0ade690SMatthew Dillon 
273c0ade690SMatthew Dillon 	/*
274c0ade690SMatthew Dillon 	 * note: f_iosize is used by vnode_pager_haspage() when constructing
275c0ade690SMatthew Dillon 	 * its VOP_BMAP call.
276c0ade690SMatthew Dillon 	 */
277c0ade690SMatthew Dillon 	mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
278fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
279427e5fc6SMatthew Dillon 	mp->mnt_maxsymlinklen = 255;
280427e5fc6SMatthew Dillon 	mp->mnt_flag |= MNT_LOCAL;
281427e5fc6SMatthew Dillon 
282427e5fc6SMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
2837a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
2847a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
28527ea2398SMatthew Dillon 
28627ea2398SMatthew Dillon 	/*
287a89aec1bSMatthew Dillon 	 * The root volume's ondisk pointer is only valid if we hold a
288a89aec1bSMatthew Dillon 	 * reference to it.
289a89aec1bSMatthew Dillon 	 */
290a89aec1bSMatthew Dillon 	rootvol = hammer_get_root_volume(hmp, &error);
291a89aec1bSMatthew Dillon 	if (error)
292a89aec1bSMatthew Dillon 		goto done;
293a89aec1bSMatthew Dillon 	ksnprintf(mp->mnt_stat.f_mntfromname,
294a89aec1bSMatthew Dillon 		  sizeof(mp->mnt_stat.f_mntfromname), "%s",
295a89aec1bSMatthew Dillon 		  rootvol->ondisk->vol_name);
296513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[0] =
297513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
298513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] =
299513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
300b84de5afSMatthew Dillon 
301b84de5afSMatthew Dillon 	hmp->next_tid = rootvol->ondisk->vol0_next_tid;
302b84de5afSMatthew Dillon 	kprintf("on-disk next_tid %016llx\n", hmp->next_tid);
303b84de5afSMatthew Dillon 
304a89aec1bSMatthew Dillon 	hammer_rel_volume(rootvol, 0);
305a89aec1bSMatthew Dillon 
306059819e3SMatthew Dillon 	hammer_flusher_create(hmp);
307059819e3SMatthew Dillon 
308a89aec1bSMatthew Dillon 	/*
30927ea2398SMatthew Dillon 	 * Locate the root directory using the root cluster's B-Tree as a
31027ea2398SMatthew Dillon 	 * starting point.  The root directory uses an obj_id of 1.
31127ea2398SMatthew Dillon 	 *
31227ea2398SMatthew Dillon 	 * FUTURE: Leave the root directory cached referenced but unlocked
31327ea2398SMatthew Dillon 	 * in hmp->rootvp (need to flush it on unmount).
31427ea2398SMatthew Dillon 	 */
31527ea2398SMatthew Dillon 	error = hammer_vfs_vget(mp, 1, &rootvp);
316a89aec1bSMatthew Dillon 	if (error)
317a89aec1bSMatthew Dillon 		goto done;
31827ea2398SMatthew Dillon 	vput(rootvp);
31927ea2398SMatthew Dillon 	/*vn_unlock(hmp->rootvp);*/
32027ea2398SMatthew Dillon 
321a89aec1bSMatthew Dillon done:
32227ea2398SMatthew Dillon 	/*
32327ea2398SMatthew Dillon 	 * Cleanup and return.
32427ea2398SMatthew Dillon 	 */
32527ea2398SMatthew Dillon 	if (error)
32627ea2398SMatthew Dillon 		hammer_free_hmp(mp);
327427e5fc6SMatthew Dillon 	return (error);
328427e5fc6SMatthew Dillon }
329427e5fc6SMatthew Dillon 
330427e5fc6SMatthew Dillon static int
331427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags)
332427e5fc6SMatthew Dillon {
333427e5fc6SMatthew Dillon #if 0
334427e5fc6SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
335427e5fc6SMatthew Dillon #endif
336427e5fc6SMatthew Dillon 	int flags;
33766325755SMatthew Dillon 	int error;
33827ea2398SMatthew Dillon 
33927ea2398SMatthew Dillon 	/*
340427e5fc6SMatthew Dillon 	 * Clean out the vnodes
341427e5fc6SMatthew Dillon 	 */
34266325755SMatthew Dillon 	flags = 0;
34366325755SMatthew Dillon 	if (mntflags & MNT_FORCE)
34466325755SMatthew Dillon 		flags |= FORCECLOSE;
34566325755SMatthew Dillon 	if ((error = vflush(mp, 0, flags)) != 0)
34666325755SMatthew Dillon 		return (error);
347427e5fc6SMatthew Dillon 
348427e5fc6SMatthew Dillon 	/*
349427e5fc6SMatthew Dillon 	 * Clean up the internal mount structure and related entities.  This
350427e5fc6SMatthew Dillon 	 * may issue I/O.
351427e5fc6SMatthew Dillon 	 */
352427e5fc6SMatthew Dillon 	hammer_free_hmp(mp);
353427e5fc6SMatthew Dillon 	return(0);
354427e5fc6SMatthew Dillon }
355427e5fc6SMatthew Dillon 
356427e5fc6SMatthew Dillon /*
357427e5fc6SMatthew Dillon  * Clean up the internal mount structure and disassociate it from the mount.
358427e5fc6SMatthew Dillon  * This may issue I/O.
359427e5fc6SMatthew Dillon  */
360427e5fc6SMatthew Dillon static void
361427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp)
362427e5fc6SMatthew Dillon {
363427e5fc6SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
364bf686dbeSMatthew Dillon 	int i;
365427e5fc6SMatthew Dillon 
36627ea2398SMatthew Dillon #if 0
367427e5fc6SMatthew Dillon 	/*
368427e5fc6SMatthew Dillon 	 * Clean up the root vnode
369427e5fc6SMatthew Dillon 	 */
370427e5fc6SMatthew Dillon 	if (hmp->rootvp) {
371427e5fc6SMatthew Dillon 		vrele(hmp->rootvp);
372427e5fc6SMatthew Dillon 		hmp->rootvp = NULL;
373427e5fc6SMatthew Dillon 	}
37427ea2398SMatthew Dillon #endif
375059819e3SMatthew Dillon 	hammer_flusher_sync(hmp);
376b84de5afSMatthew Dillon 	hammer_flusher_sync(hmp);
377059819e3SMatthew Dillon 	hammer_flusher_destroy(hmp);
378427e5fc6SMatthew Dillon 
379b84de5afSMatthew Dillon 	KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
380b84de5afSMatthew Dillon 
381b84de5afSMatthew Dillon #if 0
382427e5fc6SMatthew Dillon 	/*
383427e5fc6SMatthew Dillon 	 * Unload & flush inodes
384b84de5afSMatthew Dillon 	 *
385b84de5afSMatthew Dillon 	 * XXX illegal to call this from here, it can only be done from
386b84de5afSMatthew Dillon 	 * the flusher.
387427e5fc6SMatthew Dillon 	 */
388427e5fc6SMatthew Dillon 	RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
389d113fda1SMatthew Dillon 		hammer_unload_inode, (void *)MNT_WAIT);
390427e5fc6SMatthew Dillon 
391427e5fc6SMatthew Dillon 	/*
392427e5fc6SMatthew Dillon 	 * Unload & flush volumes
393427e5fc6SMatthew Dillon 	 */
394b84de5afSMatthew Dillon #endif
395b84de5afSMatthew Dillon 	/*
396b84de5afSMatthew Dillon 	 * Unload the volumes
397b84de5afSMatthew Dillon 	 */
398427e5fc6SMatthew Dillon 	RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
399427e5fc6SMatthew Dillon 		hammer_unload_volume, NULL);
400427e5fc6SMatthew Dillon 
401427e5fc6SMatthew Dillon 	mp->mnt_data = NULL;
40266325755SMatthew Dillon 	mp->mnt_flag &= ~MNT_LOCAL;
403427e5fc6SMatthew Dillon 	hmp->mp = NULL;
40466325755SMatthew Dillon 	kfree(hmp->zbuf, M_HAMMER);
40540043e7fSMatthew Dillon 	lockuninit(&hmp->blockmap_lock);
406bf686dbeSMatthew Dillon 
407bf686dbeSMatthew Dillon 	for (i = 0; i < HAMMER_MAX_ZONES; ++i)
408bf686dbeSMatthew Dillon 		hammer_free_holes(hmp, &hmp->holes[i]);
409bf686dbeSMatthew Dillon 
410427e5fc6SMatthew Dillon 	kfree(hmp, M_HAMMER);
411427e5fc6SMatthew Dillon }
412427e5fc6SMatthew Dillon 
413427e5fc6SMatthew Dillon /*
414513ca7d7SMatthew Dillon  * Obtain a vnode for the specified inode number.  An exclusively locked
415513ca7d7SMatthew Dillon  * vnode is returned.
416513ca7d7SMatthew Dillon  */
417513ca7d7SMatthew Dillon int
418513ca7d7SMatthew Dillon hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
419513ca7d7SMatthew Dillon {
42036f82b23SMatthew Dillon 	struct hammer_transaction trans;
421513ca7d7SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
422513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
423513ca7d7SMatthew Dillon 	int error;
424513ca7d7SMatthew Dillon 
42536f82b23SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
42636f82b23SMatthew Dillon 
427513ca7d7SMatthew Dillon 	/*
42836f82b23SMatthew Dillon 	 * Lookup the requested HAMMER inode.  The structure must be
42936f82b23SMatthew Dillon 	 * left unlocked while we manipulate the related vnode to avoid
43036f82b23SMatthew Dillon 	 * a deadlock.
431513ca7d7SMatthew Dillon 	 */
43236f82b23SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
433513ca7d7SMatthew Dillon 	if (ip == NULL) {
434513ca7d7SMatthew Dillon 		*vpp = NULL;
435513ca7d7SMatthew Dillon 		return(error);
436513ca7d7SMatthew Dillon 	}
437513ca7d7SMatthew Dillon 	error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
438513ca7d7SMatthew Dillon 	hammer_rel_inode(ip, 0);
439b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
440513ca7d7SMatthew Dillon 	return (error);
441513ca7d7SMatthew Dillon }
442513ca7d7SMatthew Dillon 
443513ca7d7SMatthew Dillon /*
444427e5fc6SMatthew Dillon  * Return the root vnode for the filesystem.
445427e5fc6SMatthew Dillon  *
446427e5fc6SMatthew Dillon  * HAMMER stores the root vnode in the hammer_mount structure so
447427e5fc6SMatthew Dillon  * getting it is easy.
448427e5fc6SMatthew Dillon  */
449427e5fc6SMatthew Dillon static int
450427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp)
451427e5fc6SMatthew Dillon {
45247197d71SMatthew Dillon #if 0
453427e5fc6SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
45447197d71SMatthew Dillon #endif
45527ea2398SMatthew Dillon 	int error;
456427e5fc6SMatthew Dillon 
45727ea2398SMatthew Dillon 	error = hammer_vfs_vget(mp, 1, vpp);
45827ea2398SMatthew Dillon 	return (error);
459427e5fc6SMatthew Dillon }
460427e5fc6SMatthew Dillon 
461427e5fc6SMatthew Dillon static int
462427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
463427e5fc6SMatthew Dillon {
464fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
465fbc6e32aSMatthew Dillon 	hammer_volume_t volume;
466fbc6e32aSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
467fbc6e32aSMatthew Dillon 	int error;
46847197d71SMatthew Dillon 	int64_t bfree;
469fbc6e32aSMatthew Dillon 
470fbc6e32aSMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
471fbc6e32aSMatthew Dillon 	if (error)
472fbc6e32aSMatthew Dillon 		return(error);
473fbc6e32aSMatthew Dillon 	ondisk = volume->ondisk;
474fbc6e32aSMatthew Dillon 
47547197d71SMatthew Dillon 	/*
47647197d71SMatthew Dillon 	 * Basic stats
47747197d71SMatthew Dillon 	 */
478fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
479c3be93f2SMatthew Dillon 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
48040043e7fSMatthew Dillon 	hammer_rel_volume(volume, 0);
48147197d71SMatthew Dillon 
48247197d71SMatthew Dillon 	mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
48347197d71SMatthew Dillon 	mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
484fbc6e32aSMatthew Dillon 	if (mp->mnt_stat.f_files < 0)
485fbc6e32aSMatthew Dillon 		mp->mnt_stat.f_files = 0;
486fbc6e32aSMatthew Dillon 
48727ea2398SMatthew Dillon 	*sbp = mp->mnt_stat;
48827ea2398SMatthew Dillon 	return(0);
489427e5fc6SMatthew Dillon }
490427e5fc6SMatthew Dillon 
491427e5fc6SMatthew Dillon static int
492427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor)
493427e5fc6SMatthew Dillon {
494fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
495*10a5d1baSMatthew Dillon 	return(hammer_sync_hmp(hmp, waitfor));
496427e5fc6SMatthew Dillon }
497427e5fc6SMatthew Dillon 
498513ca7d7SMatthew Dillon /*
499513ca7d7SMatthew Dillon  * Convert a vnode to a file handle.
500513ca7d7SMatthew Dillon  */
501513ca7d7SMatthew Dillon static int
502513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
503513ca7d7SMatthew Dillon {
504513ca7d7SMatthew Dillon 	hammer_inode_t ip;
505513ca7d7SMatthew Dillon 
506513ca7d7SMatthew Dillon 	KKASSERT(MAXFIDSZ >= 16);
507513ca7d7SMatthew Dillon 	ip = VTOI(vp);
508513ca7d7SMatthew Dillon 	fhp->fid_len = offsetof(struct fid, fid_data[16]);
509513ca7d7SMatthew Dillon 	fhp->fid_reserved = 0;
510513ca7d7SMatthew Dillon 	bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
511513ca7d7SMatthew Dillon 	bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
512513ca7d7SMatthew Dillon 	return(0);
513513ca7d7SMatthew Dillon }
514513ca7d7SMatthew Dillon 
515513ca7d7SMatthew Dillon 
516513ca7d7SMatthew Dillon /*
517513ca7d7SMatthew Dillon  * Convert a file handle back to a vnode.
518513ca7d7SMatthew Dillon  */
519513ca7d7SMatthew Dillon static int
520513ca7d7SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
521513ca7d7SMatthew Dillon {
52236f82b23SMatthew Dillon 	struct hammer_transaction trans;
523513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
524513ca7d7SMatthew Dillon 	struct hammer_inode_info info;
525513ca7d7SMatthew Dillon 	int error;
526513ca7d7SMatthew Dillon 
527513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
528513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
529513ca7d7SMatthew Dillon 
53036f82b23SMatthew Dillon 	hammer_simple_transaction(&trans, (void *)mp->mnt_data);
53136f82b23SMatthew Dillon 
532513ca7d7SMatthew Dillon 	/*
533513ca7d7SMatthew Dillon 	 * Get/allocate the hammer_inode structure.  The structure must be
534513ca7d7SMatthew Dillon 	 * unlocked while we manipulate the related vnode to avoid a
535513ca7d7SMatthew Dillon 	 * deadlock.
536513ca7d7SMatthew Dillon 	 */
53736f82b23SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
53836f82b23SMatthew Dillon 			      0, &error);
539513ca7d7SMatthew Dillon 	if (ip == NULL) {
540513ca7d7SMatthew Dillon 		*vpp = NULL;
541513ca7d7SMatthew Dillon 		return(error);
542513ca7d7SMatthew Dillon 	}
543513ca7d7SMatthew Dillon 	error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
544513ca7d7SMatthew Dillon 	hammer_rel_inode(ip, 0);
545b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
546513ca7d7SMatthew Dillon 	return (error);
547513ca7d7SMatthew Dillon }
548513ca7d7SMatthew Dillon 
549513ca7d7SMatthew Dillon static int
550513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
551513ca7d7SMatthew Dillon 		    int *exflagsp, struct ucred **credanonp)
552513ca7d7SMatthew Dillon {
553513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
554513ca7d7SMatthew Dillon 	struct netcred *np;
555513ca7d7SMatthew Dillon 	int error;
556513ca7d7SMatthew Dillon 
557513ca7d7SMatthew Dillon 	np = vfs_export_lookup(mp, &hmp->export, nam);
558513ca7d7SMatthew Dillon 	if (np) {
559513ca7d7SMatthew Dillon 		*exflagsp = np->netc_exflags;
560513ca7d7SMatthew Dillon 		*credanonp = &np->netc_anon;
561513ca7d7SMatthew Dillon 		error = 0;
562513ca7d7SMatthew Dillon 	} else {
563513ca7d7SMatthew Dillon 		error = EACCES;
564513ca7d7SMatthew Dillon 	}
565513ca7d7SMatthew Dillon 	return (error);
566513ca7d7SMatthew Dillon 
567513ca7d7SMatthew Dillon }
568513ca7d7SMatthew Dillon 
569513ca7d7SMatthew Dillon int
570513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
571513ca7d7SMatthew Dillon {
572513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
573513ca7d7SMatthew Dillon 	int error;
574513ca7d7SMatthew Dillon 
575513ca7d7SMatthew Dillon 	switch(op) {
576513ca7d7SMatthew Dillon 	case MOUNTCTL_SET_EXPORT:
577513ca7d7SMatthew Dillon 		error = vfs_export(mp, &hmp->export, export);
578513ca7d7SMatthew Dillon 		break;
579513ca7d7SMatthew Dillon 	default:
580513ca7d7SMatthew Dillon 		error = EOPNOTSUPP;
581513ca7d7SMatthew Dillon 		break;
582513ca7d7SMatthew Dillon 	}
583513ca7d7SMatthew Dillon 	return(error);
584513ca7d7SMatthew Dillon }
585513ca7d7SMatthew Dillon 
586