xref: /dflybsd-src/sys/vfs/hammer/hammer_vfsops.c (revision 72d6a027d96b30c52d242ce162021efcecfe2bb9)
1 /*
2  * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.74 2008/11/13 02:18:43 dillon Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/vnode.h>
41 #include <sys/mount.h>
42 #include <sys/malloc.h>
43 #include <sys/nlookup.h>
44 #include <sys/fcntl.h>
45 #include <sys/sysctl.h>
46 #include <sys/buf.h>
47 #include <sys/buf2.h>
48 #include "hammer.h"
49 
50 /*
51  * NOTE!  Global statistics may not be MPSAFE so HAMMER never uses them
52  *	  in conditionals.
53  */
54 int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT;
55 int hammer_debug_io;
56 int hammer_debug_general;
57 int hammer_debug_debug = 1;		/* medium-error panics */
58 int hammer_debug_inode;
59 int hammer_debug_locks;
60 int hammer_debug_btree;
61 int hammer_debug_tid;
62 int hammer_debug_recover;		/* -1 will disable, +1 will force */
63 int hammer_debug_recover_faults;
64 int hammer_debug_critical;		/* non-zero enter debugger on error */
65 int hammer_cluster_enable = 1;		/* enable read clustering by default */
66 int hammer_live_dedup = 0;
67 int hammer_count_fsyncs;
68 int hammer_count_inodes;
69 int hammer_count_iqueued;
70 int hammer_count_reclaiming;
71 int hammer_count_records;
72 int hammer_count_record_datas;
73 int hammer_count_volumes;
74 int hammer_count_buffers;
75 int hammer_count_nodes;
76 int64_t hammer_count_extra_space_used;
77 int64_t hammer_stats_btree_lookups;
78 int64_t hammer_stats_btree_searches;
79 int64_t hammer_stats_btree_inserts;
80 int64_t hammer_stats_btree_deletes;
81 int64_t hammer_stats_btree_elements;
82 int64_t hammer_stats_btree_splits;
83 int64_t hammer_stats_btree_iterations;
84 int64_t hammer_stats_btree_root_iterations;
85 int64_t hammer_stats_record_iterations;
86 
87 int64_t hammer_stats_file_read;
88 int64_t hammer_stats_file_write;
89 int64_t hammer_stats_file_iopsr;
90 int64_t hammer_stats_file_iopsw;
91 int64_t hammer_stats_disk_read;
92 int64_t hammer_stats_disk_write;
93 int64_t hammer_stats_inode_flushes;
94 int64_t hammer_stats_commits;
95 int64_t hammer_stats_undo;
96 int64_t hammer_stats_redo;
97 
98 int hammer_count_dirtybufspace;		/* global */
99 int hammer_count_refedbufs;		/* global */
100 int hammer_count_reservations;
101 int hammer_count_io_running_read;
102 int hammer_count_io_running_write;
103 int hammer_count_io_locked;
104 int hammer_limit_dirtybufspace;		/* per-mount */
105 int hammer_limit_running_io;		/* per-mount */
106 int hammer_limit_recs;			/* as a whole XXX */
107 int hammer_limit_inode_recs = 1024;	/* per inode */
108 int hammer_limit_reclaim;
109 int hammer_live_dedup_cache_size = DEDUP_CACHE_SIZE;
110 int hammer_limit_redo = 4096 * 1024;	/* per inode */
111 int hammer_autoflush = 500;		/* auto flush (typ on reclaim) */
112 int hammer_bio_count;
113 int hammer_verify_zone;
114 int hammer_verify_data = 1;
115 int hammer_write_mode;
116 int hammer_yield_check = 16;
117 int hammer_fsync_mode = 3;
118 int64_t hammer_contention_count;
119 int64_t hammer_zone_limit;
120 
121 /*
122  * Live dedup debug counters (sysctls are writable so that counters
123  * can be reset from userspace).
124  */
125 int64_t hammer_live_dedup_vnode_bcmps = 0;
126 int64_t hammer_live_dedup_device_bcmps = 0;
127 int64_t hammer_live_dedup_findblk_failures = 0;
128 int64_t hammer_live_dedup_bmap_saves = 0;
129 
130 
131 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
132 
133 SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD,
134 	   &hammer_supported_version, 0, "");
135 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
136 	   &hammer_debug_general, 0, "");
137 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
138 	   &hammer_debug_io, 0, "");
139 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
140 	   &hammer_debug_debug, 0, "");
141 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
142 	   &hammer_debug_inode, 0, "");
143 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
144 	   &hammer_debug_locks, 0, "");
145 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
146 	   &hammer_debug_btree, 0, "");
147 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
148 	   &hammer_debug_tid, 0, "");
149 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
150 	   &hammer_debug_recover, 0, "");
151 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
152 	   &hammer_debug_recover_faults, 0, "");
153 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW,
154 	   &hammer_debug_critical, 0, "");
155 SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
156 	   &hammer_cluster_enable, 0, "");
157 /*
158  * 0 - live dedup is disabled
159  * 1 - dedup cache is populated on reads only
160  * 2 - dedup cache is populated on both reads and writes
161  */
162 SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup, CTLFLAG_RW,
163 	   &hammer_live_dedup, 0, "Enable live dedup");
164 
165 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
166 	   &hammer_limit_dirtybufspace, 0, "");
167 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_running_io, CTLFLAG_RW,
168 	   &hammer_limit_running_io, 0, "");
169 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
170 	   &hammer_limit_recs, 0, "");
171 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW,
172 	   &hammer_limit_inode_recs, 0, "");
173 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaim, CTLFLAG_RW,
174 	   &hammer_limit_reclaim, 0, "");
175 SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW,
176 	   &hammer_live_dedup_cache_size, 0,
177 	   "Number of cache entries");
178 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW,
179 	   &hammer_limit_redo, 0, "");
180 
181 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
182 	   &hammer_count_fsyncs, 0, "");
183 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
184 	   &hammer_count_inodes, 0, "");
185 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
186 	   &hammer_count_iqueued, 0, "");
187 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD,
188 	   &hammer_count_reclaiming, 0, "");
189 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
190 	   &hammer_count_records, 0, "");
191 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
192 	   &hammer_count_record_datas, 0, "");
193 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
194 	   &hammer_count_volumes, 0, "");
195 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
196 	   &hammer_count_buffers, 0, "");
197 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
198 	   &hammer_count_nodes, 0, "");
199 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD,
200 	   &hammer_count_extra_space_used, 0, "");
201 
202 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
203 	   &hammer_stats_btree_searches, 0, "");
204 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
205 	   &hammer_stats_btree_lookups, 0, "");
206 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
207 	   &hammer_stats_btree_inserts, 0, "");
208 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
209 	   &hammer_stats_btree_deletes, 0, "");
210 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
211 	   &hammer_stats_btree_elements, 0, "");
212 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
213 	   &hammer_stats_btree_splits, 0, "");
214 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
215 	   &hammer_stats_btree_iterations, 0, "");
216 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD,
217 	   &hammer_stats_btree_root_iterations, 0, "");
218 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
219 	   &hammer_stats_record_iterations, 0, "");
220 
221 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
222 	   &hammer_stats_file_read, 0, "");
223 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
224 	   &hammer_stats_file_write, 0, "");
225 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD,
226 	   &hammer_stats_file_iopsr, 0, "");
227 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD,
228 	   &hammer_stats_file_iopsw, 0, "");
229 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
230 	   &hammer_stats_disk_read, 0, "");
231 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
232 	   &hammer_stats_disk_write, 0, "");
233 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
234 	   &hammer_stats_inode_flushes, 0, "");
235 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
236 	   &hammer_stats_commits, 0, "");
237 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD,
238 	   &hammer_stats_undo, 0, "");
239 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD,
240 	   &hammer_stats_redo, 0, "");
241 
242 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_vnode_bcmps, CTLFLAG_RW,
243 	    &hammer_live_dedup_vnode_bcmps, 0,
244 	    "successful vnode buffer comparisons");
245 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_device_bcmps, CTLFLAG_RW,
246 	    &hammer_live_dedup_device_bcmps, 0,
247 	    "successful device buffer comparisons");
248 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_findblk_failures, CTLFLAG_RW,
249 	    &hammer_live_dedup_findblk_failures, 0,
250 	    "block lookup failures for comparison");
251 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_bmap_saves, CTLFLAG_RW,
252 	    &hammer_live_dedup_bmap_saves, 0,
253 	    "useful physical block lookups");
254 
255 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
256 	   &hammer_count_dirtybufspace, 0, "");
257 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
258 	   &hammer_count_refedbufs, 0, "");
259 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
260 	   &hammer_count_reservations, 0, "");
261 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
262 	   &hammer_count_io_running_read, 0, "");
263 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
264 	   &hammer_count_io_locked, 0, "");
265 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
266 	   &hammer_count_io_running_write, 0, "");
267 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
268 	   &hammer_zone_limit, 0, "");
269 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
270 	   &hammer_contention_count, 0, "");
271 SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
272 	   &hammer_autoflush, 0, "");
273 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
274 	   &hammer_verify_zone, 0, "");
275 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
276 	   &hammer_verify_data, 0, "");
277 SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
278 	   &hammer_write_mode, 0, "");
279 SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW,
280 	   &hammer_yield_check, 0, "");
281 SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW,
282 	   &hammer_fsync_mode, 0, "");
283 
284 KTR_INFO_MASTER(hammer);
285 
286 /*
287  * VFS ABI
288  */
289 static void	hammer_free_hmp(struct mount *mp);
290 
291 static int	hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
292 				struct ucred *cred);
293 static int	hammer_vfs_unmount(struct mount *mp, int mntflags);
294 static int	hammer_vfs_root(struct mount *mp, struct vnode **vpp);
295 static int	hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
296 				struct ucred *cred);
297 static int	hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
298 				struct ucred *cred);
299 static int	hammer_vfs_sync(struct mount *mp, int waitfor);
300 static int	hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
301 				ino_t ino, struct vnode **vpp);
302 static int	hammer_vfs_init(struct vfsconf *conf);
303 static int	hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
304 				struct fid *fhp, struct vnode **vpp);
305 static int	hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
306 static int	hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
307 				int *exflagsp, struct ucred **credanonp);
308 
309 
310 static struct vfsops hammer_vfsops = {
311 	.vfs_mount	= hammer_vfs_mount,
312 	.vfs_unmount	= hammer_vfs_unmount,
313 	.vfs_root 	= hammer_vfs_root,
314 	.vfs_statfs	= hammer_vfs_statfs,
315 	.vfs_statvfs	= hammer_vfs_statvfs,
316 	.vfs_sync	= hammer_vfs_sync,
317 	.vfs_vget	= hammer_vfs_vget,
318 	.vfs_init	= hammer_vfs_init,
319 	.vfs_vptofh	= hammer_vfs_vptofh,
320 	.vfs_fhtovp	= hammer_vfs_fhtovp,
321 	.vfs_checkexp	= hammer_vfs_checkexp
322 };
323 
324 MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", "");
325 
326 VFS_SET(hammer_vfsops, hammer, 0);
327 MODULE_VERSION(hammer, 1);
328 
329 static int
330 hammer_vfs_init(struct vfsconf *conf)
331 {
332 	int n;
333 
334 	if (hammer_limit_recs == 0) {
335 		hammer_limit_recs = nbuf * 25;
336 		n = kmalloc_limit(M_HAMMER) / 512;
337 		if (hammer_limit_recs > n)
338 			hammer_limit_recs = n;
339 	}
340 	if (hammer_limit_dirtybufspace == 0) {
341 		hammer_limit_dirtybufspace = hidirtybufspace / 2;
342 		if (hammer_limit_dirtybufspace < 100)
343 			hammer_limit_dirtybufspace = 100;
344 	}
345 
346 	/*
347 	 * Set reasonable limits to maintain an I/O pipeline.  This is
348 	 * used by the flush code which explicitly initiates I/O, and
349 	 * is per-mount.
350 	 *
351 	 * The system-driven buffer cache uses vfs.lorunningspace and
352 	 * vfs.hirunningspace globally.
353 	 */
354 	if (hammer_limit_running_io == 0)
355 		hammer_limit_running_io = hammer_limit_dirtybufspace;
356 	if (hammer_limit_running_io > 10 * 1024 * 1024)
357 		hammer_limit_running_io = 10 * 1024 * 1024;
358 
359 	/*
360 	 * The hammer_inode structure detaches from the vnode on reclaim.
361 	 * This limits the number of inodes in this state to prevent a
362 	 * memory pool blowout.
363 	 */
364 	if (hammer_limit_reclaim == 0)
365 		hammer_limit_reclaim = desiredvnodes / 10;
366 
367 	return(0);
368 }
369 
370 static int
371 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
372 		 struct ucred *cred)
373 {
374 	struct hammer_mount_info info;
375 	hammer_mount_t hmp;
376 	hammer_volume_t rootvol;
377 	struct vnode *rootvp;
378 	struct vnode *devvp = NULL;
379 	const char *upath;	/* volume name in userspace */
380 	char *path;		/* volume name in system space */
381 	int error;
382 	int i;
383 	int master_id;
384 	char *next_volume_ptr = NULL;
385 
386 	/*
387 	 * Accept hammer_mount_info.  mntpt is NULL for root mounts at boot.
388 	 */
389 	if (mntpt == NULL) {
390 		bzero(&info, sizeof(info));
391 		info.asof = 0;
392 		info.hflags = 0;
393 		info.nvolumes = 1;
394 
395 		next_volume_ptr = mp->mnt_stat.f_mntfromname;
396 
397 		/* Count number of volumes separated by ':' */
398 		for (char *p = next_volume_ptr; *p != '\0'; ++p) {
399 			if (*p == ':') {
400 				++info.nvolumes;
401 			}
402 		}
403 
404 		mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
405 	} else {
406 		if ((error = copyin(data, &info, sizeof(info))) != 0)
407 			return (error);
408 	}
409 
410 	/*
411 	 * updating or new mount
412 	 */
413 	if (mp->mnt_flag & MNT_UPDATE) {
414 		hmp = (void *)mp->mnt_data;
415 		KKASSERT(hmp != NULL);
416 	} else {
417 		if (info.nvolumes <= 0 || info.nvolumes >= 32768)
418 			return (EINVAL);
419 		hmp = NULL;
420 	}
421 
422 	/*
423 	 * master-id validation.  The master id may not be changed by a
424 	 * mount update.
425 	 */
426 	if (info.hflags & HMNT_MASTERID) {
427 		if (hmp && hmp->master_id != info.master_id) {
428 			kprintf("hammer: cannot change master id "
429 				"with mount update\n");
430 			return(EINVAL);
431 		}
432 		master_id = info.master_id;
433 		if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
434 			return (EINVAL);
435 	} else {
436 		if (hmp)
437 			master_id = hmp->master_id;
438 		else
439 			master_id = 0;
440 	}
441 
442 	/*
443 	 * Internal mount data structure
444 	 */
445 	if (hmp == NULL) {
446 		hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
447 		mp->mnt_data = (qaddr_t)hmp;
448 		hmp->mp = mp;
449 		/*TAILQ_INIT(&hmp->recycle_list);*/
450 
451 		/*
452 		 * Make sure kmalloc type limits are set appropriately.
453 		 *
454 		 * Our inode kmalloc group is sized based on maxvnodes
455 		 * (controlled by the system, not us).
456 		 */
457 		kmalloc_create(&hmp->m_misc, "HAMMER-others");
458 		kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
459 
460 		kmalloc_raise_limit(hmp->m_inodes, 0);	/* unlimited */
461 
462 		hmp->root_btree_beg.localization = 0x00000000U;
463 		hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
464 		hmp->root_btree_beg.key = -0x8000000000000000LL;
465 		hmp->root_btree_beg.create_tid = 1;
466 		hmp->root_btree_beg.delete_tid = 1;
467 		hmp->root_btree_beg.rec_type = 0;
468 		hmp->root_btree_beg.obj_type = 0;
469 
470 		hmp->root_btree_end.localization = 0xFFFFFFFFU;
471 		hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
472 		hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
473 		hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
474 		hmp->root_btree_end.delete_tid = 0;   /* special case */
475 		hmp->root_btree_end.rec_type = 0xFFFFU;
476 		hmp->root_btree_end.obj_type = 0;
477 
478 		hmp->krate.freq = 1;	/* maximum reporting rate (hz) */
479 		hmp->krate.count = -16;	/* initial burst */
480 
481 		hmp->sync_lock.refs = 1;
482 		hmp->free_lock.refs = 1;
483 		hmp->undo_lock.refs = 1;
484 		hmp->blkmap_lock.refs = 1;
485 		hmp->snapshot_lock.refs = 1;
486 		hmp->volume_lock.refs = 1;
487 
488 		TAILQ_INIT(&hmp->delay_list);
489 		TAILQ_INIT(&hmp->flush_group_list);
490 		TAILQ_INIT(&hmp->objid_cache_list);
491 		TAILQ_INIT(&hmp->undo_lru_list);
492 		TAILQ_INIT(&hmp->reclaim_list);
493 
494 		RB_INIT(&hmp->rb_dedup_crc_root);
495 		RB_INIT(&hmp->rb_dedup_off_root);
496 		TAILQ_INIT(&hmp->dedup_lru_list);
497 	}
498 	hmp->hflags &= ~HMNT_USERFLAGS;
499 	hmp->hflags |= info.hflags & HMNT_USERFLAGS;
500 
501 	hmp->master_id = master_id;
502 
503 	if (info.asof) {
504 		mp->mnt_flag |= MNT_RDONLY;
505 		hmp->asof = info.asof;
506 	} else {
507 		hmp->asof = HAMMER_MAX_TID;
508 	}
509 
510 	hmp->volume_to_remove = -1;
511 
512 	/*
513 	 * Re-open read-write if originally read-only, or vise-versa.
514 	 *
515 	 * When going from read-only to read-write execute the stage2
516 	 * recovery if it has not already been run.
517 	 */
518 	if (mp->mnt_flag & MNT_UPDATE) {
519 		lwkt_gettoken(&hmp->fs_token);
520 		error = 0;
521 		if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
522 			kprintf("HAMMER read-only -> read-write\n");
523 			hmp->ronly = 0;
524 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
525 				hammer_adjust_volume_mode, NULL);
526 			rootvol = hammer_get_root_volume(hmp, &error);
527 			if (rootvol) {
528 				hammer_recover_flush_buffers(hmp, rootvol, 1);
529 				error = hammer_recover_stage2(hmp, rootvol);
530 				bcopy(rootvol->ondisk->vol0_blockmap,
531 				      hmp->blockmap,
532 				      sizeof(hmp->blockmap));
533 				hammer_rel_volume(rootvol, 0);
534 			}
535 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
536 				hammer_reload_inode, NULL);
537 			/* kernel clears MNT_RDONLY */
538 		} else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
539 			kprintf("HAMMER read-write -> read-only\n");
540 			hmp->ronly = 1;	/* messy */
541 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
542 				hammer_reload_inode, NULL);
543 			hmp->ronly = 0;
544 			hammer_flusher_sync(hmp);
545 			hammer_flusher_sync(hmp);
546 			hammer_flusher_sync(hmp);
547 			hmp->ronly = 1;
548 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
549 				hammer_adjust_volume_mode, NULL);
550 		}
551 		lwkt_reltoken(&hmp->fs_token);
552 		return(error);
553 	}
554 
555 	RB_INIT(&hmp->rb_vols_root);
556 	RB_INIT(&hmp->rb_inos_root);
557 	RB_INIT(&hmp->rb_redo_root);
558 	RB_INIT(&hmp->rb_nods_root);
559 	RB_INIT(&hmp->rb_undo_root);
560 	RB_INIT(&hmp->rb_resv_root);
561 	RB_INIT(&hmp->rb_bufs_root);
562 	RB_INIT(&hmp->rb_pfsm_root);
563 
564 	hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
565 
566 	RB_INIT(&hmp->volu_root);
567 	RB_INIT(&hmp->undo_root);
568 	RB_INIT(&hmp->data_root);
569 	RB_INIT(&hmp->meta_root);
570 	RB_INIT(&hmp->lose_root);
571 	TAILQ_INIT(&hmp->iorun_list);
572 
573 	lwkt_token_init(&hmp->fs_token, 1, "hammerfs");
574 	lwkt_token_init(&hmp->io_token, 1, "hammerio");
575 
576 	lwkt_gettoken(&hmp->fs_token);
577 
578 	/*
579 	 * Load volumes
580 	 */
581 	path = objcache_get(namei_oc, M_WAITOK);
582 	hmp->nvolumes = -1;
583 	for (i = 0; i < info.nvolumes; ++i) {
584 		if (mntpt == NULL) {
585 			/*
586 			 * Root mount.
587 			 */
588 			KKASSERT(next_volume_ptr != NULL);
589 			strcpy(path, "");
590 			if (*next_volume_ptr != '/') {
591 				/* relative path */
592 				strcpy(path, "/dev/");
593 			}
594 			int k;
595 			for (k = strlen(path); k < MAXPATHLEN-1; ++k) {
596 				if (*next_volume_ptr == '\0') {
597 					break;
598 				} else if (*next_volume_ptr == ':') {
599 					++next_volume_ptr;
600 					break;
601 				} else {
602 					path[k] = *next_volume_ptr;
603 					++next_volume_ptr;
604 				}
605 			}
606 			path[k] = '\0';
607 
608 			error = 0;
609 			cdev_t dev = kgetdiskbyname(path);
610 			error = bdevvp(dev, &devvp);
611 			if (error) {
612 				kprintf("hammer_mountroot: can't find devvp\n");
613 			}
614 		} else {
615 			error = copyin(&info.volumes[i], &upath,
616 				       sizeof(char *));
617 			if (error == 0)
618 				error = copyinstr(upath, path,
619 						  MAXPATHLEN, NULL);
620 		}
621 		if (error == 0)
622 			error = hammer_install_volume(hmp, path, devvp);
623 		if (error)
624 			break;
625 	}
626 	objcache_put(namei_oc, path);
627 
628 	/*
629 	 * Make sure we found a root volume
630 	 */
631 	if (error == 0 && hmp->rootvol == NULL) {
632 		kprintf("hammer_mount: No root volume found!\n");
633 		error = EINVAL;
634 	}
635 
636 	/*
637 	 * Check that all required volumes are available
638 	 */
639 	if (error == 0 && hammer_mountcheck_volumes(hmp)) {
640 		kprintf("hammer_mount: Missing volumes, cannot mount!\n");
641 		error = EINVAL;
642 	}
643 
644 	if (error) {
645 		/* called with fs_token held */
646 		hammer_free_hmp(mp);
647 		return (error);
648 	}
649 
650 	/*
651 	 * No errors, setup enough of the mount point so we can lookup the
652 	 * root vnode.
653 	 */
654 	mp->mnt_iosize_max = MAXPHYS;
655 	mp->mnt_kern_flag |= MNTK_FSMID;
656 
657 	/*
658 	 * MPSAFE code.  Note that VOPs and VFSops which are not MPSAFE
659 	 * will acquire a per-mount token prior to entry and release it
660 	 * on return, so even if we do not specify it we no longer get
661 	 * the BGL regardlless of how we are flagged.
662 	 */
663 	mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
664 	/*MNTK_RD_MPSAFE | MNTK_GA_MPSAFE | MNTK_IN_MPSAFE;*/
665 
666 	/*
667 	 * note: f_iosize is used by vnode_pager_haspage() when constructing
668 	 * its VOP_BMAP call.
669 	 */
670 	mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
671 	mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
672 
673 	mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
674 	mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
675 
676 	mp->mnt_maxsymlinklen = 255;
677 	mp->mnt_flag |= MNT_LOCAL;
678 
679 	vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
680 	vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
681 	vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
682 
683 	/*
684 	 * The root volume's ondisk pointer is only valid if we hold a
685 	 * reference to it.
686 	 */
687 	rootvol = hammer_get_root_volume(hmp, &error);
688 	if (error)
689 		goto failed;
690 
691 	/*
692 	 * Perform any necessary UNDO operations.  The recovery code does
693 	 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
694 	 * and then re-copy it again after recovery is complete.
695 	 *
696 	 * If this is a read-only mount the UNDO information is retained
697 	 * in memory in the form of dirty buffer cache buffers, and not
698 	 * written back to the media.
699 	 */
700 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
701 	      sizeof(hmp->blockmap));
702 
703 	/*
704 	 * Check filesystem version
705 	 */
706 	hmp->version = rootvol->ondisk->vol_version;
707 	if (hmp->version < HAMMER_VOL_VERSION_MIN ||
708 	    hmp->version > HAMMER_VOL_VERSION_MAX) {
709 		kprintf("HAMMER: mount unsupported fs version %d\n",
710 			hmp->version);
711 		error = ERANGE;
712 		goto done;
713 	}
714 
715 	/*
716 	 * The undo_rec_limit limits the size of flush groups to avoid
717 	 * blowing out the UNDO FIFO.  This calculation is typically in
718 	 * the tens of thousands and is designed primarily when small
719 	 * HAMMER filesystems are created.
720 	 */
721 	hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
722 	if (hammer_debug_general & 0x0001)
723 		kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit);
724 
725 	/*
726 	 * NOTE: Recover stage1 not only handles meta-data recovery, it
727 	 * 	 also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems.
728 	 */
729 	error = hammer_recover_stage1(hmp, rootvol);
730 	if (error) {
731 		kprintf("Failed to recover HAMMER filesystem on mount\n");
732 		goto done;
733 	}
734 
735 	/*
736 	 * Finish setup now that we have a good root volume.
737 	 *
738 	 * The top 16 bits of fsid.val[1] is a pfs id.
739 	 */
740 	ksnprintf(mp->mnt_stat.f_mntfromname,
741 		  sizeof(mp->mnt_stat.f_mntfromname), "%s",
742 		  rootvol->ondisk->vol_name);
743 	mp->mnt_stat.f_fsid.val[0] =
744 		crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
745 	mp->mnt_stat.f_fsid.val[1] =
746 		crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
747 	mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF;
748 
749 	mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
750 	mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
751 				     sizeof(mp->mnt_vstat.f_fsid_uuid));
752 
753 	/*
754 	 * Certain often-modified fields in the root volume are cached in
755 	 * the hammer_mount structure so we do not have to generate lots
756 	 * of little UNDO structures for them.
757 	 *
758 	 * Recopy after recovery.  This also has the side effect of
759 	 * setting our cached undo FIFO's first_offset, which serves to
760 	 * placemark the FIFO start for the NEXT flush cycle while the
761 	 * on-disk first_offset represents the LAST flush cycle.
762 	 */
763 	hmp->next_tid = rootvol->ondisk->vol0_next_tid;
764 	hmp->flush_tid1 = hmp->next_tid;
765 	hmp->flush_tid2 = hmp->next_tid;
766 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
767 	      sizeof(hmp->blockmap));
768 	hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
769 
770 	hammer_flusher_create(hmp);
771 
772 	/*
773 	 * Locate the root directory using the root cluster's B-Tree as a
774 	 * starting point.  The root directory uses an obj_id of 1.
775 	 *
776 	 * FUTURE: Leave the root directory cached referenced but unlocked
777 	 * in hmp->rootvp (need to flush it on unmount).
778 	 */
779 	error = hammer_vfs_vget(mp, NULL, 1, &rootvp);
780 	if (error)
781 		goto done;
782 	vput(rootvp);
783 	/*vn_unlock(hmp->rootvp);*/
784 	if (hmp->ronly == 0)
785 		error = hammer_recover_stage2(hmp, rootvol);
786 
787 	/*
788 	 * If the stage2 recovery fails be sure to clean out all cached
789 	 * vnodes before throwing away the mount structure or bad things
790 	 * will happen.
791 	 */
792 	if (error)
793 		vflush(mp, 0, 0);
794 
795 done:
796 	hammer_rel_volume(rootvol, 0);
797 failed:
798 	/*
799 	 * Cleanup and return.
800 	 */
801 	if (error) {
802 		/* called with fs_token held */
803 		hammer_free_hmp(mp);
804 	} else {
805 		lwkt_reltoken(&hmp->fs_token);
806 	}
807 	return (error);
808 }
809 
810 static int
811 hammer_vfs_unmount(struct mount *mp, int mntflags)
812 {
813 	hammer_mount_t hmp = (void *)mp->mnt_data;
814 	int flags;
815 	int error;
816 
817 	/*
818 	 * Clean out the vnodes
819 	 */
820 	lwkt_gettoken(&hmp->fs_token);
821 	flags = 0;
822 	if (mntflags & MNT_FORCE)
823 		flags |= FORCECLOSE;
824 	error = vflush(mp, 0, flags);
825 
826 	/*
827 	 * Clean up the internal mount structure and related entities.  This
828 	 * may issue I/O.
829 	 */
830 	if (error == 0) {
831 		/* called with fs_token held */
832 		hammer_free_hmp(mp);
833 	} else {
834 		lwkt_reltoken(&hmp->fs_token);
835 	}
836 	return(error);
837 }
838 
839 /*
840  * Clean up the internal mount structure and disassociate it from the mount.
841  * This may issue I/O.
842  *
843  * Called with fs_token held.
844  */
845 static void
846 hammer_free_hmp(struct mount *mp)
847 {
848 	hammer_mount_t hmp = (void *)mp->mnt_data;
849 	hammer_flush_group_t flg;
850 	int count;
851 	int dummy;
852 
853 	/*
854 	 * Flush anything dirty.  This won't even run if the
855 	 * filesystem errored-out.
856 	 */
857 	count = 0;
858 	while (hammer_flusher_haswork(hmp)) {
859 		hammer_flusher_sync(hmp);
860 		++count;
861 		if (count >= 5) {
862 			if (count == 5)
863 				kprintf("HAMMER: umount flushing.");
864 			else
865 				kprintf(".");
866 			tsleep(&dummy, 0, "hmrufl", hz);
867 		}
868 		if (count == 30) {
869 			kprintf("giving up\n");
870 			break;
871 		}
872 	}
873 	if (count >= 5 && count < 30)
874 		kprintf("\n");
875 
876 	/*
877 	 * If the mount had a critical error we have to destroy any
878 	 * remaining inodes before we can finish cleaning up the flusher.
879 	 */
880 	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
881 		RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
882 			hammer_destroy_inode_callback, NULL);
883 	}
884 
885 	/*
886 	 * There shouldn't be any inodes left now and any left over
887 	 * flush groups should now be empty.
888 	 */
889 	KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
890 	while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
891 		TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
892 		KKASSERT(RB_EMPTY(&flg->flush_tree));
893 		if (flg->refs) {
894 			kprintf("HAMMER: Warning, flush_group %p was "
895 				"not empty on umount!\n", flg);
896 		}
897 		kfree(flg, hmp->m_misc);
898 	}
899 
900 	/*
901 	 * We can finally destroy the flusher
902 	 */
903 	hammer_flusher_destroy(hmp);
904 
905 	/*
906 	 * We may have held recovered buffers due to a read-only mount.
907 	 * These must be discarded.
908 	 */
909 	if (hmp->ronly)
910 		hammer_recover_flush_buffers(hmp, NULL, -1);
911 
912 	/*
913 	 * Unload buffers and then volumes
914 	 */
915         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
916 		hammer_unload_buffer, NULL);
917 	RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
918 		hammer_unload_volume, NULL);
919 
920 	mp->mnt_data = NULL;
921 	mp->mnt_flag &= ~MNT_LOCAL;
922 	hmp->mp = NULL;
923 	hammer_destroy_objid_cache(hmp);
924 	hammer_destroy_dedup_cache(hmp);
925 	if (hmp->dedup_free_cache != NULL) {
926 		kfree(hmp->dedup_free_cache, hmp->m_misc);
927 		hmp->dedup_free_cache = NULL;
928 	}
929 	kmalloc_destroy(&hmp->m_misc);
930 	kmalloc_destroy(&hmp->m_inodes);
931 	lwkt_reltoken(&hmp->fs_token);
932 	kfree(hmp, M_HAMMER);
933 }
934 
935 /*
936  * Report critical errors.  ip may be NULL.
937  */
938 void
939 hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
940 		      int error, const char *msg)
941 {
942 	hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
943 
944 	krateprintf(&hmp->krate,
945 		    "HAMMER(%s): Critical error inode=%jd error=%d %s\n",
946 		    hmp->mp->mnt_stat.f_mntfromname,
947 		    (intmax_t)(ip ? ip->obj_id : -1),
948 		    error, msg);
949 
950 	if (hmp->ronly == 0) {
951 		hmp->ronly = 2;		/* special errored read-only mode */
952 		hmp->mp->mnt_flag |= MNT_RDONLY;
953 		kprintf("HAMMER(%s): Forcing read-only mode\n",
954 			hmp->mp->mnt_stat.f_mntfromname);
955 	}
956 	hmp->error = error;
957 	if (hammer_debug_critical)
958 		Debugger("Entering debugger");
959 }
960 
961 
962 /*
963  * Obtain a vnode for the specified inode number.  An exclusively locked
964  * vnode is returned.
965  */
966 int
967 hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
968 		ino_t ino, struct vnode **vpp)
969 {
970 	struct hammer_transaction trans;
971 	struct hammer_mount *hmp = (void *)mp->mnt_data;
972 	struct hammer_inode *ip;
973 	int error;
974 	u_int32_t localization;
975 
976 	lwkt_gettoken(&hmp->fs_token);
977 	hammer_simple_transaction(&trans, hmp);
978 
979 	/*
980 	 * If a directory vnode is supplied (mainly NFS) then we can acquire
981 	 * the PFS domain from it.  Otherwise we would only be able to vget
982 	 * inodes in the root PFS.
983 	 */
984 	if (dvp) {
985 		localization = HAMMER_DEF_LOCALIZATION +
986 				VTOI(dvp)->obj_localization;
987 	} else {
988 		localization = HAMMER_DEF_LOCALIZATION;
989 	}
990 
991 	/*
992 	 * Lookup the requested HAMMER inode.  The structure must be
993 	 * left unlocked while we manipulate the related vnode to avoid
994 	 * a deadlock.
995 	 */
996 	ip = hammer_get_inode(&trans, NULL, ino,
997 			      hmp->asof, localization,
998 			      0, &error);
999 	if (ip == NULL) {
1000 		*vpp = NULL;
1001 	} else {
1002 		error = hammer_get_vnode(ip, vpp);
1003 		hammer_rel_inode(ip, 0);
1004 	}
1005 	hammer_done_transaction(&trans);
1006 	lwkt_reltoken(&hmp->fs_token);
1007 	return (error);
1008 }
1009 
1010 /*
1011  * Return the root vnode for the filesystem.
1012  *
1013  * HAMMER stores the root vnode in the hammer_mount structure so
1014  * getting it is easy.
1015  */
1016 static int
1017 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
1018 {
1019 	int error;
1020 
1021 	error = hammer_vfs_vget(mp, NULL, 1, vpp);
1022 	return (error);
1023 }
1024 
1025 static int
1026 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1027 {
1028 	struct hammer_mount *hmp = (void *)mp->mnt_data;
1029 	hammer_volume_t volume;
1030 	hammer_volume_ondisk_t ondisk;
1031 	int error;
1032 	int64_t bfree;
1033 	int64_t breserved;
1034 
1035 	lwkt_gettoken(&hmp->fs_token);
1036 	volume = hammer_get_root_volume(hmp, &error);
1037 	if (error) {
1038 		lwkt_reltoken(&hmp->fs_token);
1039 		return(error);
1040 	}
1041 	ondisk = volume->ondisk;
1042 
1043 	/*
1044 	 * Basic stats
1045 	 */
1046 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
1047 	mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
1048 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
1049 	hammer_rel_volume(volume, 0);
1050 
1051 	mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
1052 	mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1053 	if (mp->mnt_stat.f_files < 0)
1054 		mp->mnt_stat.f_files = 0;
1055 
1056 	*sbp = mp->mnt_stat;
1057 	lwkt_reltoken(&hmp->fs_token);
1058 	return(0);
1059 }
1060 
1061 static int
1062 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1063 {
1064 	struct hammer_mount *hmp = (void *)mp->mnt_data;
1065 	hammer_volume_t volume;
1066 	hammer_volume_ondisk_t ondisk;
1067 	int error;
1068 	int64_t bfree;
1069 	int64_t breserved;
1070 
1071 	lwkt_gettoken(&hmp->fs_token);
1072 	volume = hammer_get_root_volume(hmp, &error);
1073 	if (error) {
1074 		lwkt_reltoken(&hmp->fs_token);
1075 		return(error);
1076 	}
1077 	ondisk = volume->ondisk;
1078 
1079 	/*
1080 	 * Basic stats
1081 	 */
1082 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
1083 	mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
1084 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
1085 	hammer_rel_volume(volume, 0);
1086 
1087 	mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
1088 	mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1089 	if (mp->mnt_vstat.f_files < 0)
1090 		mp->mnt_vstat.f_files = 0;
1091 	*sbp = mp->mnt_vstat;
1092 	lwkt_reltoken(&hmp->fs_token);
1093 	return(0);
1094 }
1095 
1096 /*
1097  * Sync the filesystem.  Currently we have to run it twice, the second
1098  * one will advance the undo start index to the end index, so if a crash
1099  * occurs no undos will be run on mount.
1100  *
1101  * We do not sync the filesystem if we are called from a panic.  If we did
1102  * we might end up blowing up a sync that was already in progress.
1103  */
1104 static int
1105 hammer_vfs_sync(struct mount *mp, int waitfor)
1106 {
1107 	struct hammer_mount *hmp = (void *)mp->mnt_data;
1108 	int error;
1109 
1110 	lwkt_gettoken(&hmp->fs_token);
1111 	if (panicstr == NULL) {
1112 		error = hammer_sync_hmp(hmp, waitfor);
1113 	} else {
1114 		error = EIO;
1115 	}
1116 	lwkt_reltoken(&hmp->fs_token);
1117 	return (error);
1118 }
1119 
1120 /*
1121  * Convert a vnode to a file handle.
1122  *
1123  * Accesses read-only fields on already-referenced structures so
1124  * no token is needed.
1125  */
1126 static int
1127 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1128 {
1129 	hammer_inode_t ip;
1130 
1131 	KKASSERT(MAXFIDSZ >= 16);
1132 	ip = VTOI(vp);
1133 	fhp->fid_len = offsetof(struct fid, fid_data[16]);
1134 	fhp->fid_ext = ip->obj_localization >> 16;
1135 	bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
1136 	bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
1137 	return(0);
1138 }
1139 
1140 
1141 /*
1142  * Convert a file handle back to a vnode.
1143  *
1144  * Use rootvp to enforce PFS isolation when a PFS is exported via a
1145  * null mount.
1146  */
1147 static int
1148 hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
1149 		  struct fid *fhp, struct vnode **vpp)
1150 {
1151 	hammer_mount_t hmp = (void *)mp->mnt_data;
1152 	struct hammer_transaction trans;
1153 	struct hammer_inode *ip;
1154 	struct hammer_inode_info info;
1155 	int error;
1156 	u_int32_t localization;
1157 
1158 	bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
1159 	bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
1160 	if (rootvp)
1161 		localization = VTOI(rootvp)->obj_localization;
1162 	else
1163 		localization = (u_int32_t)fhp->fid_ext << 16;
1164 
1165 	lwkt_gettoken(&hmp->fs_token);
1166 	hammer_simple_transaction(&trans, hmp);
1167 
1168 	/*
1169 	 * Get/allocate the hammer_inode structure.  The structure must be
1170 	 * unlocked while we manipulate the related vnode to avoid a
1171 	 * deadlock.
1172 	 */
1173 	ip = hammer_get_inode(&trans, NULL, info.obj_id,
1174 			      info.obj_asof, localization, 0, &error);
1175 	if (ip) {
1176 		error = hammer_get_vnode(ip, vpp);
1177 		hammer_rel_inode(ip, 0);
1178 	} else {
1179 		*vpp = NULL;
1180 	}
1181 	hammer_done_transaction(&trans);
1182 	lwkt_reltoken(&hmp->fs_token);
1183 	return (error);
1184 }
1185 
1186 static int
1187 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1188 		    int *exflagsp, struct ucred **credanonp)
1189 {
1190 	hammer_mount_t hmp = (void *)mp->mnt_data;
1191 	struct netcred *np;
1192 	int error;
1193 
1194 	lwkt_gettoken(&hmp->fs_token);
1195 	np = vfs_export_lookup(mp, &hmp->export, nam);
1196 	if (np) {
1197 		*exflagsp = np->netc_exflags;
1198 		*credanonp = &np->netc_anon;
1199 		error = 0;
1200 	} else {
1201 		error = EACCES;
1202 	}
1203 	lwkt_reltoken(&hmp->fs_token);
1204 	return (error);
1205 
1206 }
1207 
1208 int
1209 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
1210 {
1211 	hammer_mount_t hmp = (void *)mp->mnt_data;
1212 	int error;
1213 
1214 	lwkt_gettoken(&hmp->fs_token);
1215 
1216 	switch(op) {
1217 	case MOUNTCTL_SET_EXPORT:
1218 		error = vfs_export(mp, &hmp->export, export);
1219 		break;
1220 	default:
1221 		error = EOPNOTSUPP;
1222 		break;
1223 	}
1224 	lwkt_reltoken(&hmp->fs_token);
1225 
1226 	return(error);
1227 }
1228 
1229