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