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