xref: /dflybsd-src/sys/vfs/hammer/hammer_vfsops.c (revision 6a85c61bc2794e37b6c15b7d670d5dae35ebe1af)
1427e5fc6SMatthew Dillon /*
2b84de5afSMatthew Dillon  * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
3427e5fc6SMatthew Dillon  *
4427e5fc6SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5427e5fc6SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6427e5fc6SMatthew Dillon  *
7427e5fc6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8427e5fc6SMatthew Dillon  * modification, are permitted provided that the following conditions
9427e5fc6SMatthew Dillon  * are met:
10427e5fc6SMatthew Dillon  *
11427e5fc6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12427e5fc6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13427e5fc6SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14427e5fc6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15427e5fc6SMatthew Dillon  *    the documentation and/or other materials provided with the
16427e5fc6SMatthew Dillon  *    distribution.
17427e5fc6SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18427e5fc6SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19427e5fc6SMatthew Dillon  *    from this software without specific, prior written permission.
20427e5fc6SMatthew Dillon  *
21427e5fc6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22427e5fc6SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23427e5fc6SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24427e5fc6SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25427e5fc6SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26427e5fc6SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27427e5fc6SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28427e5fc6SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29427e5fc6SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30427e5fc6SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31427e5fc6SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32427e5fc6SMatthew Dillon  * SUCH DAMAGE.
33427e5fc6SMatthew Dillon  */
34427e5fc6SMatthew Dillon 
35427e5fc6SMatthew Dillon #include <sys/param.h>
36427e5fc6SMatthew Dillon #include <sys/systm.h>
37427e5fc6SMatthew Dillon #include <sys/kernel.h>
38427e5fc6SMatthew Dillon #include <sys/vnode.h>
39427e5fc6SMatthew Dillon #include <sys/mount.h>
40427e5fc6SMatthew Dillon #include <sys/malloc.h>
41427e5fc6SMatthew Dillon #include <sys/nlookup.h>
42427e5fc6SMatthew Dillon #include <sys/fcntl.h>
43b3deaf57SMatthew Dillon #include <sys/sysctl.h>
44427e5fc6SMatthew Dillon #include <sys/buf.h>
45427e5fc6SMatthew Dillon #include <sys/buf2.h>
46427e5fc6SMatthew Dillon #include "hammer.h"
47427e5fc6SMatthew Dillon 
48aac0aabdSMatthew Dillon /*
49aac0aabdSMatthew Dillon  * NOTE!  Global statistics may not be MPSAFE so HAMMER never uses them
50aac0aabdSMatthew Dillon  *	  in conditionals.
51aac0aabdSMatthew Dillon  */
5202428fb6SMatthew Dillon int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT;
532f85fa4dSMatthew Dillon int hammer_debug_io;
54d5ef456eSMatthew Dillon int hammer_debug_general;
551b0ab2c3SMatthew Dillon int hammer_debug_debug = 1;		/* medium-error panics */
56e8599db1SMatthew Dillon int hammer_debug_inode;
577d683b0fSMatthew Dillon int hammer_debug_locks;
58b3deaf57SMatthew Dillon int hammer_debug_btree;
59d113fda1SMatthew Dillon int hammer_debug_tid;
6046fe7ae1SMatthew Dillon int hammer_debug_recover;		/* -1 will disable, +1 will force */
6146fe7ae1SMatthew Dillon int hammer_debug_recover_faults;
62fc73edd8SMatthew Dillon int hammer_debug_critical;		/* non-zero enter debugger on error */
631b0ab2c3SMatthew Dillon int hammer_cluster_enable = 1;		/* enable read clustering by default */
64507df98aSIlya Dryomov int hammer_live_dedup = 0;
65e2a02b72SMatthew Dillon int hammer_tdmux_ticks;
667a61b85dSMatthew Dillon int hammer_count_fsyncs;
67b3deaf57SMatthew Dillon int hammer_count_inodes;
68af209b0fSMatthew Dillon int hammer_count_iqueued;
69e2a02b72SMatthew Dillon int hammer_count_reclaims;
70b3deaf57SMatthew Dillon int hammer_count_records;
71b3deaf57SMatthew Dillon int hammer_count_record_datas;
72b3deaf57SMatthew Dillon int hammer_count_volumes;
73b3deaf57SMatthew Dillon int hammer_count_buffers;
74b3deaf57SMatthew Dillon int hammer_count_nodes;
75a7e9bef1SMatthew Dillon int64_t hammer_count_extra_space_used;
76cb51be26SMatthew Dillon int64_t hammer_stats_btree_lookups;
77cb51be26SMatthew Dillon int64_t hammer_stats_btree_searches;
78cb51be26SMatthew Dillon int64_t hammer_stats_btree_inserts;
79cb51be26SMatthew Dillon int64_t hammer_stats_btree_deletes;
80cb51be26SMatthew Dillon int64_t hammer_stats_btree_elements;
81cb51be26SMatthew Dillon int64_t hammer_stats_btree_splits;
82cb51be26SMatthew Dillon int64_t hammer_stats_btree_iterations;
8339d8fd63SMatthew Dillon int64_t hammer_stats_btree_root_iterations;
84cb51be26SMatthew Dillon int64_t hammer_stats_record_iterations;
85ce0138a6SMatthew Dillon 
86ce0138a6SMatthew Dillon int64_t hammer_stats_file_read;
87ce0138a6SMatthew Dillon int64_t hammer_stats_file_write;
88ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsr;
89ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsw;
90ce0138a6SMatthew Dillon int64_t hammer_stats_disk_read;
91ce0138a6SMatthew Dillon int64_t hammer_stats_disk_write;
92ce0138a6SMatthew Dillon int64_t hammer_stats_inode_flushes;
93ce0138a6SMatthew Dillon int64_t hammer_stats_commits;
9489e744ceSMatthew Dillon int64_t hammer_stats_undo;
956048b411SMatthew Dillon int64_t hammer_stats_redo;
96ce0138a6SMatthew Dillon 
973583bbb4SMatthew Dillon long hammer_count_dirtybufspace;	/* global */
98a99b9ea2SMatthew Dillon int hammer_count_refedbufs;		/* global */
990832c9bbSMatthew Dillon int hammer_count_reservations;
1003583bbb4SMatthew Dillon long hammer_count_io_running_read;
1013583bbb4SMatthew Dillon long hammer_count_io_running_write;
102a99b9ea2SMatthew Dillon int hammer_count_io_locked;
1033583bbb4SMatthew Dillon long hammer_limit_dirtybufspace;	/* per-mount */
1040832c9bbSMatthew Dillon int hammer_limit_recs;			/* as a whole XXX */
105a117fbebSMatthew Dillon int hammer_limit_inode_recs = 2048;	/* per inode */
106e2a02b72SMatthew Dillon int hammer_limit_reclaims;
107e2ef7a95SIlya Dryomov int hammer_live_dedup_cache_size = DEDUP_CACHE_SIZE;
1086048b411SMatthew Dillon int hammer_limit_redo = 4096 * 1024;	/* per inode */
10937646115SMatthew Dillon int hammer_autoflush = 500;		/* auto flush (typ on reclaim) */
1101f07f686SMatthew Dillon int hammer_bio_count;
111cb51be26SMatthew Dillon int hammer_verify_zone;
1121b0ab2c3SMatthew Dillon int hammer_verify_data = 1;
113cb51be26SMatthew Dillon int hammer_write_mode;
114b4f86ea3SMatthew Dillon int hammer_double_buffer;
115283a4a38SMatthew Dillon int hammer_btree_full_undo = 1;
1163e583440SMatthew Dillon int hammer_yield_check = 16;
1179d0a6205SMatthew Dillon int hammer_fsync_mode = 3;
1187d683b0fSMatthew Dillon int64_t hammer_contention_count;
119f03c9cf4SMatthew Dillon int64_t hammer_zone_limit;
120b3deaf57SMatthew Dillon 
121507df98aSIlya Dryomov /*
122507df98aSIlya Dryomov  * Live dedup debug counters (sysctls are writable so that counters
123507df98aSIlya Dryomov  * can be reset from userspace).
124507df98aSIlya Dryomov  */
125507df98aSIlya Dryomov int64_t hammer_live_dedup_vnode_bcmps = 0;
126507df98aSIlya Dryomov int64_t hammer_live_dedup_device_bcmps = 0;
127507df98aSIlya Dryomov int64_t hammer_live_dedup_findblk_failures = 0;
128507df98aSIlya Dryomov int64_t hammer_live_dedup_bmap_saves = 0;
129507df98aSIlya Dryomov 
130507df98aSIlya Dryomov 
131b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
132507df98aSIlya Dryomov 
1335987cc42SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD,
1345987cc42SMatthew Dillon 	   &hammer_supported_version, 0, "");
135d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
136d5ef456eSMatthew Dillon 	   &hammer_debug_general, 0, "");
1372f85fa4dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
1382f85fa4dSMatthew Dillon 	   &hammer_debug_io, 0, "");
13977062c8aSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
14077062c8aSMatthew Dillon 	   &hammer_debug_debug, 0, "");
141e8599db1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
142e8599db1SMatthew Dillon 	   &hammer_debug_inode, 0, "");
1437d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
1447d683b0fSMatthew Dillon 	   &hammer_debug_locks, 0, "");
145b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
146b3deaf57SMatthew Dillon 	   &hammer_debug_btree, 0, "");
147d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
148d113fda1SMatthew Dillon 	   &hammer_debug_tid, 0, "");
149b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
150b33e2cc0SMatthew Dillon 	   &hammer_debug_recover, 0, "");
15146fe7ae1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
15246fe7ae1SMatthew Dillon 	   &hammer_debug_recover_faults, 0, "");
153fc73edd8SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW,
154fc73edd8SMatthew Dillon 	   &hammer_debug_critical, 0, "");
1551b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
1561b0ab2c3SMatthew Dillon 	   &hammer_cluster_enable, 0, "");
157507df98aSIlya Dryomov /*
158507df98aSIlya Dryomov  * 0 - live dedup is disabled
159507df98aSIlya Dryomov  * 1 - dedup cache is populated on reads only
160507df98aSIlya Dryomov  * 2 - dedup cache is populated on both reads and writes
161283a4a38SMatthew Dillon  *
162283a4a38SMatthew Dillon  * LIVE_DEDUP IS DISABLED PERMANENTLY!  This feature appears to cause
163283a4a38SMatthew Dillon  * blockmap corruption over time so we've turned it off permanently.
164507df98aSIlya Dryomov  */
1656de803f0SFrançois Tigeot SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup, CTLFLAG_RD,
1666de803f0SFrançois Tigeot 	   &hammer_live_dedup, 0, "Enable live dedup (experimental)");
167e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, tdmux_ticks, CTLFLAG_RW,
168e2a02b72SMatthew Dillon 	   &hammer_tdmux_ticks, 0, "Hammer tdmux ticks");
1699480ff55SMatthew Dillon 
1703583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
171f5a07a7aSMatthew Dillon 	   &hammer_limit_dirtybufspace, 0, "");
17247637bffSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
17347637bffSMatthew Dillon 	   &hammer_limit_recs, 0, "");
174de996e86SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW,
175de996e86SMatthew Dillon 	   &hammer_limit_inode_recs, 0, "");
176e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaims, CTLFLAG_RW,
177e2a02b72SMatthew Dillon 	   &hammer_limit_reclaims, 0, "");
178e2ef7a95SIlya Dryomov SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW,
1792b34db9bSSamuel J. Greear 	   &hammer_live_dedup_cache_size, 0,
1802b34db9bSSamuel J. Greear 	   "Number of cache entries");
1816048b411SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW,
1826048b411SMatthew Dillon 	   &hammer_limit_redo, 0, "");
1839480ff55SMatthew Dillon 
1847a61b85dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
1857a61b85dSMatthew Dillon 	   &hammer_count_fsyncs, 0, "");
186b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
187b3deaf57SMatthew Dillon 	   &hammer_count_inodes, 0, "");
188af209b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
189af209b0fSMatthew Dillon 	   &hammer_count_iqueued, 0, "");
190e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaims, CTLFLAG_RD,
191e2a02b72SMatthew Dillon 	   &hammer_count_reclaims, 0, "");
192b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
193b3deaf57SMatthew Dillon 	   &hammer_count_records, 0, "");
194b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
195b3deaf57SMatthew Dillon 	   &hammer_count_record_datas, 0, "");
196b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
197b3deaf57SMatthew Dillon 	   &hammer_count_volumes, 0, "");
198b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
199b3deaf57SMatthew Dillon 	   &hammer_count_buffers, 0, "");
200b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
201b3deaf57SMatthew Dillon 	   &hammer_count_nodes, 0, "");
202a7e9bef1SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD,
203a7e9bef1SMatthew Dillon 	   &hammer_count_extra_space_used, 0, "");
204ce0138a6SMatthew Dillon 
205cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
206cb51be26SMatthew Dillon 	   &hammer_stats_btree_searches, 0, "");
207cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
208cb51be26SMatthew Dillon 	   &hammer_stats_btree_lookups, 0, "");
209cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
210cb51be26SMatthew Dillon 	   &hammer_stats_btree_inserts, 0, "");
211cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
212cb51be26SMatthew Dillon 	   &hammer_stats_btree_deletes, 0, "");
213cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
214cb51be26SMatthew Dillon 	   &hammer_stats_btree_elements, 0, "");
215cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
216cb51be26SMatthew Dillon 	   &hammer_stats_btree_splits, 0, "");
217cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
218cb51be26SMatthew Dillon 	   &hammer_stats_btree_iterations, 0, "");
21939d8fd63SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD,
22039d8fd63SMatthew Dillon 	   &hammer_stats_btree_root_iterations, 0, "");
221cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
222cb51be26SMatthew Dillon 	   &hammer_stats_record_iterations, 0, "");
223ce0138a6SMatthew Dillon 
224ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
225ce0138a6SMatthew Dillon 	   &hammer_stats_file_read, 0, "");
226ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
227ce0138a6SMatthew Dillon 	   &hammer_stats_file_write, 0, "");
228ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD,
229ce0138a6SMatthew Dillon 	   &hammer_stats_file_iopsr, 0, "");
230ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD,
231ce0138a6SMatthew Dillon 	   &hammer_stats_file_iopsw, 0, "");
232ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
233ce0138a6SMatthew Dillon 	   &hammer_stats_disk_read, 0, "");
234ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
235ce0138a6SMatthew Dillon 	   &hammer_stats_disk_write, 0, "");
236ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
237ce0138a6SMatthew Dillon 	   &hammer_stats_inode_flushes, 0, "");
238ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
239ce0138a6SMatthew Dillon 	   &hammer_stats_commits, 0, "");
24089e744ceSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD,
24189e744ceSMatthew Dillon 	   &hammer_stats_undo, 0, "");
2426048b411SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD,
2436048b411SMatthew Dillon 	   &hammer_stats_redo, 0, "");
244ce0138a6SMatthew Dillon 
245507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_vnode_bcmps, CTLFLAG_RW,
246f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_vnode_bcmps, 0,
247f06b3d6dSSamuel J. Greear 	    "successful vnode buffer comparisons");
248507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_device_bcmps, CTLFLAG_RW,
249f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_device_bcmps, 0,
250f06b3d6dSSamuel J. Greear 	    "successful device buffer comparisons");
251507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_findblk_failures, CTLFLAG_RW,
252f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_findblk_failures, 0,
253f06b3d6dSSamuel J. Greear 	    "block lookup failures for comparison");
254507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_bmap_saves, CTLFLAG_RW,
255f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_bmap_saves, 0,
256f06b3d6dSSamuel J. Greear 	    "useful physical block lookups");
257507df98aSIlya Dryomov 
2583583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
259f5a07a7aSMatthew Dillon 	   &hammer_count_dirtybufspace, 0, "");
260a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
261a99b9ea2SMatthew Dillon 	   &hammer_count_refedbufs, 0, "");
2620832c9bbSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
2630832c9bbSMatthew Dillon 	   &hammer_count_reservations, 0, "");
2643583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
265a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_read, 0, "");
266a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
267a99b9ea2SMatthew Dillon 	   &hammer_count_io_locked, 0, "");
2683583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
269a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_write, 0, "");
270f03c9cf4SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
271f03c9cf4SMatthew Dillon 	   &hammer_zone_limit, 0, "");
2727d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
2737d683b0fSMatthew Dillon 	   &hammer_contention_count, 0, "");
27421fde338SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
27521fde338SMatthew Dillon 	   &hammer_autoflush, 0, "");
276cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
277cb51be26SMatthew Dillon 	   &hammer_verify_zone, 0, "");
2781b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
2791b0ab2c3SMatthew Dillon 	   &hammer_verify_data, 0, "");
280cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
281cb51be26SMatthew Dillon 	   &hammer_write_mode, 0, "");
282b4f86ea3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, double_buffer, CTLFLAG_RW,
283b4f86ea3SMatthew Dillon 	   &hammer_double_buffer, 0, "");
284283a4a38SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, btree_full_undo, CTLFLAG_RW,
285283a4a38SMatthew Dillon 	   &hammer_btree_full_undo, 0, "");
2863e583440SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW,
2873e583440SMatthew Dillon 	   &hammer_yield_check, 0, "");
2886f3d87c0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW,
2896f3d87c0SMatthew Dillon 	   &hammer_fsync_mode, 0, "");
290b3deaf57SMatthew Dillon 
291879a1b60SSascha Wildner /* KTR_INFO_MASTER(hammer); */
292bcac4bbbSMatthew Dillon 
293427e5fc6SMatthew Dillon /*
294427e5fc6SMatthew Dillon  * VFS ABI
295427e5fc6SMatthew Dillon  */
296427e5fc6SMatthew Dillon static void	hammer_free_hmp(struct mount *mp);
297427e5fc6SMatthew Dillon 
298427e5fc6SMatthew Dillon static int	hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
299427e5fc6SMatthew Dillon 				struct ucred *cred);
300427e5fc6SMatthew Dillon static int	hammer_vfs_unmount(struct mount *mp, int mntflags);
301427e5fc6SMatthew Dillon static int	hammer_vfs_root(struct mount *mp, struct vnode **vpp);
302427e5fc6SMatthew Dillon static int	hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
303427e5fc6SMatthew Dillon 				struct ucred *cred);
3046f97fce3SMatthew Dillon static int	hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
3056f97fce3SMatthew Dillon 				struct ucred *cred);
306427e5fc6SMatthew Dillon static int	hammer_vfs_sync(struct mount *mp, int waitfor);
307b9b0a6d0SMatthew Dillon static int	hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
308b9b0a6d0SMatthew Dillon 				ino_t ino, struct vnode **vpp);
309427e5fc6SMatthew Dillon static int	hammer_vfs_init(struct vfsconf *conf);
31067863d04SMatthew Dillon static int	hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
31167863d04SMatthew Dillon 				struct fid *fhp, struct vnode **vpp);
312513ca7d7SMatthew Dillon static int	hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
313513ca7d7SMatthew Dillon static int	hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
314513ca7d7SMatthew Dillon 				int *exflagsp, struct ucred **credanonp);
315513ca7d7SMatthew Dillon 
316427e5fc6SMatthew Dillon 
317427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = {
318427e5fc6SMatthew Dillon 	.vfs_mount	= hammer_vfs_mount,
319427e5fc6SMatthew Dillon 	.vfs_unmount	= hammer_vfs_unmount,
320427e5fc6SMatthew Dillon 	.vfs_root 	= hammer_vfs_root,
321427e5fc6SMatthew Dillon 	.vfs_statfs	= hammer_vfs_statfs,
3226f97fce3SMatthew Dillon 	.vfs_statvfs	= hammer_vfs_statvfs,
323427e5fc6SMatthew Dillon 	.vfs_sync	= hammer_vfs_sync,
324427e5fc6SMatthew Dillon 	.vfs_vget	= hammer_vfs_vget,
325513ca7d7SMatthew Dillon 	.vfs_init	= hammer_vfs_init,
326513ca7d7SMatthew Dillon 	.vfs_vptofh	= hammer_vfs_vptofh,
327513ca7d7SMatthew Dillon 	.vfs_fhtovp	= hammer_vfs_fhtovp,
328513ca7d7SMatthew Dillon 	.vfs_checkexp	= hammer_vfs_checkexp
329427e5fc6SMatthew Dillon };
330427e5fc6SMatthew Dillon 
331bac808feSMatthew Dillon MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", "");
332427e5fc6SMatthew Dillon 
333427e5fc6SMatthew Dillon VFS_SET(hammer_vfsops, hammer, 0);
334427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1);
335427e5fc6SMatthew Dillon 
336427e5fc6SMatthew Dillon static int
337427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf)
338427e5fc6SMatthew Dillon {
33969906b47SMatthew Dillon 	long n;
3403098dc2fSMatthew Dillon 
341e2a02b72SMatthew Dillon 	/*
342e2a02b72SMatthew Dillon 	 * Wait up to this long for an exclusive deadlock to clear
343e2a02b72SMatthew Dillon 	 * before acquiring a new shared lock on the ip.  The deadlock
344e2a02b72SMatthew Dillon 	 * may have occured on a b-tree node related to the ip.
345e2a02b72SMatthew Dillon 	 */
346e2a02b72SMatthew Dillon 	if (hammer_tdmux_ticks == 0)
347e2a02b72SMatthew Dillon 		hammer_tdmux_ticks = hz / 5;
348e2a02b72SMatthew Dillon 
349e2a02b72SMatthew Dillon 	/*
35069906b47SMatthew Dillon 	 * Autosize, but be careful because a hammer filesystem's
35169906b47SMatthew Dillon 	 * reserve is partially calculated based on dirtybufspace,
35269906b47SMatthew Dillon 	 * so we simply cannot allow it to get too large.
353e2a02b72SMatthew Dillon 	 */
3543098dc2fSMatthew Dillon 	if (hammer_limit_recs == 0) {
35569906b47SMatthew Dillon 		n = nbuf * 25;
35669906b47SMatthew Dillon 		if (n > kmalloc_limit(M_HAMMER) / 512)
3573098dc2fSMatthew Dillon 			n = kmalloc_limit(M_HAMMER) / 512;
35869906b47SMatthew Dillon 		if (n > 2 * 1024 * 1024)
35969906b47SMatthew Dillon 			n = 2 * 1024 * 1024;
36069906b47SMatthew Dillon 		hammer_limit_recs = (int)n;
3613098dc2fSMatthew Dillon 	}
362f5a07a7aSMatthew Dillon 	if (hammer_limit_dirtybufspace == 0) {
363f5a07a7aSMatthew Dillon 		hammer_limit_dirtybufspace = hidirtybufspace / 2;
36469906b47SMatthew Dillon 		if (hammer_limit_dirtybufspace < 1L * 1024 * 1024)
36569906b47SMatthew Dillon 			hammer_limit_dirtybufspace = 1024L * 1024;
36669906b47SMatthew Dillon 		if (hammer_limit_dirtybufspace > 1024L * 1024 * 1024)
36769906b47SMatthew Dillon 			hammer_limit_dirtybufspace = 1024L * 1024 * 1024;
3689f5097dcSMatthew Dillon 	}
369ba298df1SMatthew Dillon 
370ba298df1SMatthew Dillon 	/*
37137646115SMatthew Dillon 	 * The hammer_inode structure detaches from the vnode on reclaim.
37237646115SMatthew Dillon 	 * This limits the number of inodes in this state to prevent a
37337646115SMatthew Dillon 	 * memory pool blowout.
37437646115SMatthew Dillon 	 */
375e2a02b72SMatthew Dillon 	if (hammer_limit_reclaims == 0)
376e2a02b72SMatthew Dillon 		hammer_limit_reclaims = desiredvnodes / 10;
37737646115SMatthew Dillon 
378427e5fc6SMatthew Dillon 	return(0);
379427e5fc6SMatthew Dillon }
380427e5fc6SMatthew Dillon 
381427e5fc6SMatthew Dillon static int
382427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
383427e5fc6SMatthew Dillon 		 struct ucred *cred)
384427e5fc6SMatthew Dillon {
385427e5fc6SMatthew Dillon 	struct hammer_mount_info info;
386a89aec1bSMatthew Dillon 	hammer_mount_t hmp;
387a89aec1bSMatthew Dillon 	hammer_volume_t rootvol;
38827ea2398SMatthew Dillon 	struct vnode *rootvp;
3897c19b529SMichael Neumann 	struct vnode *devvp = NULL;
390427e5fc6SMatthew Dillon 	const char *upath;	/* volume name in userspace */
391427e5fc6SMatthew Dillon 	char *path;		/* volume name in system space */
392427e5fc6SMatthew Dillon 	int error;
393427e5fc6SMatthew Dillon 	int i;
394732a1697SMatthew Dillon 	int master_id;
395104cb849SMichael Neumann 	char *next_volume_ptr = NULL;
396ab0bf4a3SMatthew Dillon 
397ab0bf4a3SMatthew Dillon 	/*
398ab0bf4a3SMatthew Dillon 	 * Accept hammer_mount_info.  mntpt is NULL for root mounts at boot.
399ab0bf4a3SMatthew Dillon 	 */
400ab0bf4a3SMatthew Dillon 	if (mntpt == NULL) {
4017c19b529SMichael Neumann 		bzero(&info, sizeof(info));
4027c19b529SMichael Neumann 		info.asof = 0;
4037c19b529SMichael Neumann 		info.hflags = 0;
4047c19b529SMichael Neumann 		info.nvolumes = 1;
405104cb849SMichael Neumann 
406104cb849SMichael Neumann 		next_volume_ptr = mp->mnt_stat.f_mntfromname;
407104cb849SMichael Neumann 
408104cb849SMichael Neumann 		/* Count number of volumes separated by ':' */
409104cb849SMichael Neumann 		for (char *p = next_volume_ptr; *p != '\0'; ++p) {
410104cb849SMichael Neumann 			if (*p == ':') {
411104cb849SMichael Neumann 				++info.nvolumes;
412104cb849SMichael Neumann 			}
413104cb849SMichael Neumann 		}
414104cb849SMichael Neumann 
415104cb849SMichael Neumann 		mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
4167c19b529SMichael Neumann 	} else {
417427e5fc6SMatthew Dillon 		if ((error = copyin(data, &info, sizeof(info))) != 0)
418427e5fc6SMatthew Dillon 			return (error);
4197c19b529SMichael Neumann 	}
420427e5fc6SMatthew Dillon 
421427e5fc6SMatthew Dillon 	/*
422732a1697SMatthew Dillon 	 * updating or new mount
423427e5fc6SMatthew Dillon 	 */
424195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
425195c19a1SMatthew Dillon 		hmp = (void *)mp->mnt_data;
426195c19a1SMatthew Dillon 		KKASSERT(hmp != NULL);
427195c19a1SMatthew Dillon 	} else {
4287529c2acSAntonio Huete Jimenez 		if (info.nvolumes <= 0 || info.nvolumes > HAMMER_MAX_VOLUMES)
429732a1697SMatthew Dillon 			return (EINVAL);
430732a1697SMatthew Dillon 		hmp = NULL;
431732a1697SMatthew Dillon 	}
432732a1697SMatthew Dillon 
433732a1697SMatthew Dillon 	/*
434732a1697SMatthew Dillon 	 * master-id validation.  The master id may not be changed by a
435732a1697SMatthew Dillon 	 * mount update.
436732a1697SMatthew Dillon 	 */
437732a1697SMatthew Dillon 	if (info.hflags & HMNT_MASTERID) {
438732a1697SMatthew Dillon 		if (hmp && hmp->master_id != info.master_id) {
439732a1697SMatthew Dillon 			kprintf("hammer: cannot change master id "
440732a1697SMatthew Dillon 				"with mount update\n");
441732a1697SMatthew Dillon 			return(EINVAL);
442732a1697SMatthew Dillon 		}
443732a1697SMatthew Dillon 		master_id = info.master_id;
444732a1697SMatthew Dillon 		if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
445732a1697SMatthew Dillon 			return (EINVAL);
446732a1697SMatthew Dillon 	} else {
447732a1697SMatthew Dillon 		if (hmp)
448732a1697SMatthew Dillon 			master_id = hmp->master_id;
449732a1697SMatthew Dillon 		else
450732a1697SMatthew Dillon 			master_id = 0;
451732a1697SMatthew Dillon 	}
452732a1697SMatthew Dillon 
453732a1697SMatthew Dillon 	/*
454b0aab9b9SMatthew Dillon 	 * Internal mount data structure
455732a1697SMatthew Dillon 	 */
456732a1697SMatthew Dillon 	if (hmp == NULL) {
457427e5fc6SMatthew Dillon 		hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
458427e5fc6SMatthew Dillon 		mp->mnt_data = (qaddr_t)hmp;
459427e5fc6SMatthew Dillon 		hmp->mp = mp;
46046fe7ae1SMatthew Dillon 		/*TAILQ_INIT(&hmp->recycle_list);*/
46147197d71SMatthew Dillon 
462bac808feSMatthew Dillon 		/*
4637c457ac8SMatthew Dillon 		 * Make sure kmalloc type limits are set appropriately.
4647c457ac8SMatthew Dillon 		 *
4657c457ac8SMatthew Dillon 		 * Our inode kmalloc group is sized based on maxvnodes
4667c457ac8SMatthew Dillon 		 * (controlled by the system, not us).
467bac808feSMatthew Dillon 		 */
468bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_misc, "HAMMER-others");
469bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
470bac808feSMatthew Dillon 
4717c457ac8SMatthew Dillon 		kmalloc_raise_limit(hmp->m_inodes, 0);	/* unlimited */
472bac808feSMatthew Dillon 
473dd94f1b1SMatthew Dillon 		hmp->root_btree_beg.localization = 0x00000000U;
47447197d71SMatthew Dillon 		hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
47547197d71SMatthew Dillon 		hmp->root_btree_beg.key = -0x8000000000000000LL;
47647197d71SMatthew Dillon 		hmp->root_btree_beg.create_tid = 1;
47747197d71SMatthew Dillon 		hmp->root_btree_beg.delete_tid = 1;
47847197d71SMatthew Dillon 		hmp->root_btree_beg.rec_type = 0;
47947197d71SMatthew Dillon 		hmp->root_btree_beg.obj_type = 0;
48047197d71SMatthew Dillon 
481dd94f1b1SMatthew Dillon 		hmp->root_btree_end.localization = 0xFFFFFFFFU;
48247197d71SMatthew Dillon 		hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
48347197d71SMatthew Dillon 		hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
48447197d71SMatthew Dillon 		hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
48547197d71SMatthew Dillon 		hmp->root_btree_end.delete_tid = 0;   /* special case */
48647197d71SMatthew Dillon 		hmp->root_btree_end.rec_type = 0xFFFFU;
48747197d71SMatthew Dillon 		hmp->root_btree_end.obj_type = 0;
488f03c9cf4SMatthew Dillon 
489cdb6e4e6SMatthew Dillon 		hmp->krate.freq = 1;	/* maximum reporting rate (hz) */
490cdb6e4e6SMatthew Dillon 		hmp->krate.count = -16;	/* initial burst */
491cdb6e4e6SMatthew Dillon 
4929480ff55SMatthew Dillon 		hmp->sync_lock.refs = 1;
493c9b9e29dSMatthew Dillon 		hmp->free_lock.refs = 1;
494d99d6bf5SMatthew Dillon 		hmp->undo_lock.refs = 1;
495d99d6bf5SMatthew Dillon 		hmp->blkmap_lock.refs = 1;
49683f2a3aaSMatthew Dillon 		hmp->snapshot_lock.refs = 1;
49752e547e3SMichael Neumann 		hmp->volume_lock.refs = 1;
4989480ff55SMatthew Dillon 
499cebe9493SMatthew Dillon 		TAILQ_INIT(&hmp->delay_list);
5007a61b85dSMatthew Dillon 		TAILQ_INIT(&hmp->flush_group_list);
5010729c8c8SMatthew Dillon 		TAILQ_INIT(&hmp->objid_cache_list);
502e8599db1SMatthew Dillon 		TAILQ_INIT(&hmp->undo_lru_list);
5037bc5b8c2SMatthew Dillon 		TAILQ_INIT(&hmp->reclaim_list);
504507df98aSIlya Dryomov 
505507df98aSIlya Dryomov 		RB_INIT(&hmp->rb_dedup_crc_root);
506507df98aSIlya Dryomov 		RB_INIT(&hmp->rb_dedup_off_root);
507507df98aSIlya Dryomov 		TAILQ_INIT(&hmp->dedup_lru_list);
508195c19a1SMatthew Dillon 	}
50951c35492SMatthew Dillon 	hmp->hflags &= ~HMNT_USERFLAGS;
51051c35492SMatthew Dillon 	hmp->hflags |= info.hflags & HMNT_USERFLAGS;
511732a1697SMatthew Dillon 
512732a1697SMatthew Dillon 	hmp->master_id = master_id;
513732a1697SMatthew Dillon 
5147f7c1f84SMatthew Dillon 	if (info.asof) {
5157f7c1f84SMatthew Dillon 		mp->mnt_flag |= MNT_RDONLY;
5167f7c1f84SMatthew Dillon 		hmp->asof = info.asof;
5177f7c1f84SMatthew Dillon 	} else {
5187f7c1f84SMatthew Dillon 		hmp->asof = HAMMER_MAX_TID;
5197f7c1f84SMatthew Dillon 	}
520195c19a1SMatthew Dillon 
521865c9609SMichael Neumann 	hmp->volume_to_remove = -1;
522865c9609SMichael Neumann 
523195c19a1SMatthew Dillon 	/*
52451c35492SMatthew Dillon 	 * Re-open read-write if originally read-only, or vise-versa.
52502428fb6SMatthew Dillon 	 *
52602428fb6SMatthew Dillon 	 * When going from read-only to read-write execute the stage2
52702428fb6SMatthew Dillon 	 * recovery if it has not already been run.
528195c19a1SMatthew Dillon 	 */
529195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
530b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->fs_token);
53151c35492SMatthew Dillon 		error = 0;
53251c35492SMatthew Dillon 		if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
53351c35492SMatthew Dillon 			kprintf("HAMMER read-only -> read-write\n");
534195c19a1SMatthew Dillon 			hmp->ronly = 0;
53551c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
53651c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
53751c35492SMatthew Dillon 			rootvol = hammer_get_root_volume(hmp, &error);
53851c35492SMatthew Dillon 			if (rootvol) {
53906ad81ffSMatthew Dillon 				hammer_recover_flush_buffers(hmp, rootvol, 1);
54002428fb6SMatthew Dillon 				error = hammer_recover_stage2(hmp, rootvol);
5419f5097dcSMatthew Dillon 				bcopy(rootvol->ondisk->vol0_blockmap,
5429f5097dcSMatthew Dillon 				      hmp->blockmap,
5439f5097dcSMatthew Dillon 				      sizeof(hmp->blockmap));
54451c35492SMatthew Dillon 				hammer_rel_volume(rootvol, 0);
545195c19a1SMatthew Dillon 			}
54651c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
54751c35492SMatthew Dillon 				hammer_reload_inode, NULL);
54851c35492SMatthew Dillon 			/* kernel clears MNT_RDONLY */
54951c35492SMatthew Dillon 		} else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
55051c35492SMatthew Dillon 			kprintf("HAMMER read-write -> read-only\n");
55151c35492SMatthew Dillon 			hmp->ronly = 1;	/* messy */
55251c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
55351c35492SMatthew Dillon 				hammer_reload_inode, NULL);
55451c35492SMatthew Dillon 			hmp->ronly = 0;
55551c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
55651c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
55751c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
55851c35492SMatthew Dillon 			hmp->ronly = 1;
55951c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
56051c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
56151c35492SMatthew Dillon 		}
562b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
56351c35492SMatthew Dillon 		return(error);
564195c19a1SMatthew Dillon 	}
565195c19a1SMatthew Dillon 
566427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_vols_root);
567427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_inos_root);
56873896937SMatthew Dillon 	RB_INIT(&hmp->rb_redo_root);
56940043e7fSMatthew Dillon 	RB_INIT(&hmp->rb_nods_root);
570e8599db1SMatthew Dillon 	RB_INIT(&hmp->rb_undo_root);
5710832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_resv_root);
5720832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_bufs_root);
5735fa5c92fSMatthew Dillon 	RB_INIT(&hmp->rb_pfsm_root);
5740832c9bbSMatthew Dillon 
575195c19a1SMatthew Dillon 	hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
576427e5fc6SMatthew Dillon 
5771afb73cfSMatthew Dillon 	RB_INIT(&hmp->volu_root);
5781afb73cfSMatthew Dillon 	RB_INIT(&hmp->undo_root);
5791afb73cfSMatthew Dillon 	RB_INIT(&hmp->data_root);
5801afb73cfSMatthew Dillon 	RB_INIT(&hmp->meta_root);
5811afb73cfSMatthew Dillon 	RB_INIT(&hmp->lose_root);
582eddadaeeSMatthew Dillon 	TAILQ_INIT(&hmp->iorun_list);
58310a5d1baSMatthew Dillon 
584a3c18566SMatthew Dillon 	lwkt_token_init(&hmp->fs_token, "hammerfs");
585a3c18566SMatthew Dillon 	lwkt_token_init(&hmp->io_token, "hammerio");
586b0aab9b9SMatthew Dillon 
587b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
588b0aab9b9SMatthew Dillon 
589427e5fc6SMatthew Dillon 	/*
590427e5fc6SMatthew Dillon 	 * Load volumes
591427e5fc6SMatthew Dillon 	 */
592427e5fc6SMatthew Dillon 	path = objcache_get(namei_oc, M_WAITOK);
5931b0ab2c3SMatthew Dillon 	hmp->nvolumes = -1;
594427e5fc6SMatthew Dillon 	for (i = 0; i < info.nvolumes; ++i) {
5957c19b529SMichael Neumann 		if (mntpt == NULL) {
5967c19b529SMichael Neumann 			/*
5977c19b529SMichael Neumann 			 * Root mount.
5987c19b529SMichael Neumann 			 */
599104cb849SMichael Neumann 			KKASSERT(next_volume_ptr != NULL);
600a407819fSMichael Neumann 			strcpy(path, "");
601a407819fSMichael Neumann 			if (*next_volume_ptr != '/') {
602a407819fSMichael Neumann 				/* relative path */
603104cb849SMichael Neumann 				strcpy(path, "/dev/");
604a407819fSMichael Neumann 			}
605104cb849SMichael Neumann 			int k;
606104cb849SMichael Neumann 			for (k = strlen(path); k < MAXPATHLEN-1; ++k) {
607104cb849SMichael Neumann 				if (*next_volume_ptr == '\0') {
608104cb849SMichael Neumann 					break;
609104cb849SMichael Neumann 				} else if (*next_volume_ptr == ':') {
610104cb849SMichael Neumann 					++next_volume_ptr;
611104cb849SMichael Neumann 					break;
612104cb849SMichael Neumann 				} else {
613104cb849SMichael Neumann 					path[k] = *next_volume_ptr;
614104cb849SMichael Neumann 					++next_volume_ptr;
615104cb849SMichael Neumann 				}
616104cb849SMichael Neumann 			}
617104cb849SMichael Neumann 			path[k] = '\0';
618104cb849SMichael Neumann 
6197c19b529SMichael Neumann 			error = 0;
620104cb849SMichael Neumann 			cdev_t dev = kgetdiskbyname(path);
621104cb849SMichael Neumann 			error = bdevvp(dev, &devvp);
622104cb849SMichael Neumann 			if (error) {
623104cb849SMichael Neumann 				kprintf("hammer_mountroot: can't find devvp\n");
624104cb849SMichael Neumann 			}
6257c19b529SMichael Neumann 		} else {
6267c19b529SMichael Neumann 			error = copyin(&info.volumes[i], &upath,
6277c19b529SMichael Neumann 				       sizeof(char *));
628427e5fc6SMatthew Dillon 			if (error == 0)
6297c19b529SMichael Neumann 				error = copyinstr(upath, path,
6307c19b529SMichael Neumann 						  MAXPATHLEN, NULL);
6317c19b529SMichael Neumann 		}
632427e5fc6SMatthew Dillon 		if (error == 0)
6337c19b529SMichael Neumann 			error = hammer_install_volume(hmp, path, devvp);
634427e5fc6SMatthew Dillon 		if (error)
635427e5fc6SMatthew Dillon 			break;
636427e5fc6SMatthew Dillon 	}
637427e5fc6SMatthew Dillon 	objcache_put(namei_oc, path);
638427e5fc6SMatthew Dillon 
639427e5fc6SMatthew Dillon 	/*
640427e5fc6SMatthew Dillon 	 * Make sure we found a root volume
641427e5fc6SMatthew Dillon 	 */
642427e5fc6SMatthew Dillon 	if (error == 0 && hmp->rootvol == NULL) {
643427e5fc6SMatthew Dillon 		kprintf("hammer_mount: No root volume found!\n");
644427e5fc6SMatthew Dillon 		error = EINVAL;
645427e5fc6SMatthew Dillon 	}
6461b0ab2c3SMatthew Dillon 
6471b0ab2c3SMatthew Dillon 	/*
6481b0ab2c3SMatthew Dillon 	 * Check that all required volumes are available
6491b0ab2c3SMatthew Dillon 	 */
6501b0ab2c3SMatthew Dillon 	if (error == 0 && hammer_mountcheck_volumes(hmp)) {
6511b0ab2c3SMatthew Dillon 		kprintf("hammer_mount: Missing volumes, cannot mount!\n");
6521b0ab2c3SMatthew Dillon 		error = EINVAL;
6531b0ab2c3SMatthew Dillon 	}
6541b0ab2c3SMatthew Dillon 
655427e5fc6SMatthew Dillon 	if (error) {
656b0aab9b9SMatthew Dillon 		/* called with fs_token held */
657427e5fc6SMatthew Dillon 		hammer_free_hmp(mp);
658427e5fc6SMatthew Dillon 		return (error);
659427e5fc6SMatthew Dillon 	}
660427e5fc6SMatthew Dillon 
661427e5fc6SMatthew Dillon 	/*
66227ea2398SMatthew Dillon 	 * No errors, setup enough of the mount point so we can lookup the
66327ea2398SMatthew Dillon 	 * root vnode.
664427e5fc6SMatthew Dillon 	 */
665427e5fc6SMatthew Dillon 	mp->mnt_iosize_max = MAXPHYS;
666427e5fc6SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_FSMID;
667cf6a53caSMatthew Dillon 	mp->mnt_kern_flag |= MNTK_THR_SYNC;	/* new vsyncscan semantics */
668c0ade690SMatthew Dillon 
669c0ade690SMatthew Dillon 	/*
670aac0aabdSMatthew Dillon 	 * MPSAFE code.  Note that VOPs and VFSops which are not MPSAFE
671aac0aabdSMatthew Dillon 	 * will acquire a per-mount token prior to entry and release it
672*6a85c61bSTomohiro Kusumi 	 * on return.
673aac0aabdSMatthew Dillon 	 */
674b0aab9b9SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
675b0aab9b9SMatthew Dillon 	/*MNTK_RD_MPSAFE | MNTK_GA_MPSAFE | MNTK_IN_MPSAFE;*/
676aac0aabdSMatthew Dillon 
677aac0aabdSMatthew Dillon 	/*
678c0ade690SMatthew Dillon 	 * note: f_iosize is used by vnode_pager_haspage() when constructing
679c0ade690SMatthew Dillon 	 * its VOP_BMAP call.
680c0ade690SMatthew Dillon 	 */
681c0ade690SMatthew Dillon 	mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
682fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
6836f97fce3SMatthew Dillon 
6846f97fce3SMatthew Dillon 	mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
6856f97fce3SMatthew Dillon 	mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
6866f97fce3SMatthew Dillon 
687427e5fc6SMatthew Dillon 	mp->mnt_maxsymlinklen = 255;
688427e5fc6SMatthew Dillon 	mp->mnt_flag |= MNT_LOCAL;
689427e5fc6SMatthew Dillon 
690427e5fc6SMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
6917a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
6927a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
69327ea2398SMatthew Dillon 
69427ea2398SMatthew Dillon 	/*
695a89aec1bSMatthew Dillon 	 * The root volume's ondisk pointer is only valid if we hold a
696a89aec1bSMatthew Dillon 	 * reference to it.
697a89aec1bSMatthew Dillon 	 */
698a89aec1bSMatthew Dillon 	rootvol = hammer_get_root_volume(hmp, &error);
699a89aec1bSMatthew Dillon 	if (error)
700f90dde4cSMatthew Dillon 		goto failed;
701f90dde4cSMatthew Dillon 
702f90dde4cSMatthew Dillon 	/*
7039f5097dcSMatthew Dillon 	 * Perform any necessary UNDO operations.  The recovery code does
7040729c8c8SMatthew Dillon 	 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
7050729c8c8SMatthew Dillon 	 * and then re-copy it again after recovery is complete.
706c9b9e29dSMatthew Dillon 	 *
70751c35492SMatthew Dillon 	 * If this is a read-only mount the UNDO information is retained
70851c35492SMatthew Dillon 	 * in memory in the form of dirty buffer cache buffers, and not
70951c35492SMatthew Dillon 	 * written back to the media.
710f90dde4cSMatthew Dillon 	 */
7110729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
7120729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
713c9b9e29dSMatthew Dillon 
7147a61b85dSMatthew Dillon 	/*
71544a83111SMatthew Dillon 	 * Check filesystem version
71644a83111SMatthew Dillon 	 */
71744a83111SMatthew Dillon 	hmp->version = rootvol->ondisk->vol_version;
71844a83111SMatthew Dillon 	if (hmp->version < HAMMER_VOL_VERSION_MIN ||
71944a83111SMatthew Dillon 	    hmp->version > HAMMER_VOL_VERSION_MAX) {
72044a83111SMatthew Dillon 		kprintf("HAMMER: mount unsupported fs version %d\n",
72144a83111SMatthew Dillon 			hmp->version);
72244a83111SMatthew Dillon 		error = ERANGE;
72344a83111SMatthew Dillon 		goto done;
72444a83111SMatthew Dillon 	}
72544a83111SMatthew Dillon 
72644a83111SMatthew Dillon 	/*
7277a61b85dSMatthew Dillon 	 * The undo_rec_limit limits the size of flush groups to avoid
7287a61b85dSMatthew Dillon 	 * blowing out the UNDO FIFO.  This calculation is typically in
7297a61b85dSMatthew Dillon 	 * the tens of thousands and is designed primarily when small
7307a61b85dSMatthew Dillon 	 * HAMMER filesystems are created.
7317a61b85dSMatthew Dillon 	 */
7327a61b85dSMatthew Dillon 	hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
7337a61b85dSMatthew Dillon 	if (hammer_debug_general & 0x0001)
7347a61b85dSMatthew Dillon 		kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit);
7357a61b85dSMatthew Dillon 
73602428fb6SMatthew Dillon 	/*
73702428fb6SMatthew Dillon 	 * NOTE: Recover stage1 not only handles meta-data recovery, it
73802428fb6SMatthew Dillon 	 * 	 also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems.
73902428fb6SMatthew Dillon 	 */
74002428fb6SMatthew Dillon 	error = hammer_recover_stage1(hmp, rootvol);
741f90dde4cSMatthew Dillon 	if (error) {
742f90dde4cSMatthew Dillon 		kprintf("Failed to recover HAMMER filesystem on mount\n");
743a89aec1bSMatthew Dillon 		goto done;
744f90dde4cSMatthew Dillon 	}
745f90dde4cSMatthew Dillon 
746f90dde4cSMatthew Dillon 	/*
747adf01747SMatthew Dillon 	 * Finish setup now that we have a good root volume.
748adf01747SMatthew Dillon 	 *
749adf01747SMatthew Dillon 	 * The top 16 bits of fsid.val[1] is a pfs id.
750f90dde4cSMatthew Dillon 	 */
751a89aec1bSMatthew Dillon 	ksnprintf(mp->mnt_stat.f_mntfromname,
752a89aec1bSMatthew Dillon 		  sizeof(mp->mnt_stat.f_mntfromname), "%s",
753a89aec1bSMatthew Dillon 		  rootvol->ondisk->vol_name);
754513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[0] =
755513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
756513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] =
757513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
758adf01747SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF;
759b84de5afSMatthew Dillon 
7606f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
7616f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
7626f97fce3SMatthew Dillon 				     sizeof(mp->mnt_vstat.f_fsid_uuid));
7636f97fce3SMatthew Dillon 
7640729c8c8SMatthew Dillon 	/*
7650729c8c8SMatthew Dillon 	 * Certain often-modified fields in the root volume are cached in
7660729c8c8SMatthew Dillon 	 * the hammer_mount structure so we do not have to generate lots
7670729c8c8SMatthew Dillon 	 * of little UNDO structures for them.
768c9b9e29dSMatthew Dillon 	 *
7699f5097dcSMatthew Dillon 	 * Recopy after recovery.  This also has the side effect of
7709f5097dcSMatthew Dillon 	 * setting our cached undo FIFO's first_offset, which serves to
7719f5097dcSMatthew Dillon 	 * placemark the FIFO start for the NEXT flush cycle while the
7729f5097dcSMatthew Dillon 	 * on-disk first_offset represents the LAST flush cycle.
7730729c8c8SMatthew Dillon 	 */
774b84de5afSMatthew Dillon 	hmp->next_tid = rootvol->ondisk->vol0_next_tid;
7754889cbd4SMatthew Dillon 	hmp->flush_tid1 = hmp->next_tid;
7764889cbd4SMatthew Dillon 	hmp->flush_tid2 = hmp->next_tid;
7770729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
7780729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
779e63644f0SMatthew Dillon 	hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
7800729c8c8SMatthew Dillon 
781059819e3SMatthew Dillon 	hammer_flusher_create(hmp);
782059819e3SMatthew Dillon 
783a89aec1bSMatthew Dillon 	/*
78427ea2398SMatthew Dillon 	 * Locate the root directory using the root cluster's B-Tree as a
78527ea2398SMatthew Dillon 	 * starting point.  The root directory uses an obj_id of 1.
78627ea2398SMatthew Dillon 	 *
78727ea2398SMatthew Dillon 	 * FUTURE: Leave the root directory cached referenced but unlocked
78827ea2398SMatthew Dillon 	 * in hmp->rootvp (need to flush it on unmount).
78927ea2398SMatthew Dillon 	 */
790b9b0a6d0SMatthew Dillon 	error = hammer_vfs_vget(mp, NULL, 1, &rootvp);
791a89aec1bSMatthew Dillon 	if (error)
792a89aec1bSMatthew Dillon 		goto done;
79327ea2398SMatthew Dillon 	vput(rootvp);
79427ea2398SMatthew Dillon 	/*vn_unlock(hmp->rootvp);*/
79588c39f64SThomas Nikolajsen 	if (hmp->ronly == 0)
79602428fb6SMatthew Dillon 		error = hammer_recover_stage2(hmp, rootvol);
79727ea2398SMatthew Dillon 
7989fa352a2SMatthew Dillon 	/*
7999fa352a2SMatthew Dillon 	 * If the stage2 recovery fails be sure to clean out all cached
8009fa352a2SMatthew Dillon 	 * vnodes before throwing away the mount structure or bad things
8019fa352a2SMatthew Dillon 	 * will happen.
8029fa352a2SMatthew Dillon 	 */
8039fa352a2SMatthew Dillon 	if (error)
8049fa352a2SMatthew Dillon 		vflush(mp, 0, 0);
8059fa352a2SMatthew Dillon 
806a89aec1bSMatthew Dillon done:
80798ad7adeSFrancois Tigeot 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
80898ad7adeSFrancois Tigeot 		/* New mount */
80998ad7adeSFrancois Tigeot 
81098ad7adeSFrancois Tigeot 		/* Populate info for mount point (NULL pad)*/
81198ad7adeSFrancois Tigeot 		bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
81298ad7adeSFrancois Tigeot 		size_t size;
81398ad7adeSFrancois Tigeot 		if (mntpt) {
81498ad7adeSFrancois Tigeot 			copyinstr(mntpt, mp->mnt_stat.f_mntonname,
81598ad7adeSFrancois Tigeot 							MNAMELEN -1, &size);
81698ad7adeSFrancois Tigeot 		} else { /* Root mount */
81798ad7adeSFrancois Tigeot 			mp->mnt_stat.f_mntonname[0] = '/';
81898ad7adeSFrancois Tigeot 		}
81998ad7adeSFrancois Tigeot 	}
82098ad7adeSFrancois Tigeot 	(void)VFS_STATFS(mp, &mp->mnt_stat, cred);
821f90dde4cSMatthew Dillon 	hammer_rel_volume(rootvol, 0);
822f90dde4cSMatthew Dillon failed:
82327ea2398SMatthew Dillon 	/*
82427ea2398SMatthew Dillon 	 * Cleanup and return.
82527ea2398SMatthew Dillon 	 */
826b0aab9b9SMatthew Dillon 	if (error) {
827b0aab9b9SMatthew Dillon 		/* called with fs_token held */
82827ea2398SMatthew Dillon 		hammer_free_hmp(mp);
829b0aab9b9SMatthew Dillon 	} else {
830b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
831b0aab9b9SMatthew Dillon 	}
832427e5fc6SMatthew Dillon 	return (error);
833427e5fc6SMatthew Dillon }
834427e5fc6SMatthew Dillon 
835427e5fc6SMatthew Dillon static int
836427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags)
837427e5fc6SMatthew Dillon {
838b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
839427e5fc6SMatthew Dillon 	int flags;
84066325755SMatthew Dillon 	int error;
84127ea2398SMatthew Dillon 
84227ea2398SMatthew Dillon 	/*
843427e5fc6SMatthew Dillon 	 * Clean out the vnodes
844427e5fc6SMatthew Dillon 	 */
845b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
84666325755SMatthew Dillon 	flags = 0;
84766325755SMatthew Dillon 	if (mntflags & MNT_FORCE)
84866325755SMatthew Dillon 		flags |= FORCECLOSE;
849b0aab9b9SMatthew Dillon 	error = vflush(mp, 0, flags);
850427e5fc6SMatthew Dillon 
851427e5fc6SMatthew Dillon 	/*
852427e5fc6SMatthew Dillon 	 * Clean up the internal mount structure and related entities.  This
853427e5fc6SMatthew Dillon 	 * may issue I/O.
854427e5fc6SMatthew Dillon 	 */
855b0aab9b9SMatthew Dillon 	if (error == 0) {
856b0aab9b9SMatthew Dillon 		/* called with fs_token held */
857427e5fc6SMatthew Dillon 		hammer_free_hmp(mp);
858b0aab9b9SMatthew Dillon 	} else {
859b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
860b0aab9b9SMatthew Dillon 	}
861b0aab9b9SMatthew Dillon 	return(error);
862427e5fc6SMatthew Dillon }
863427e5fc6SMatthew Dillon 
864427e5fc6SMatthew Dillon /*
865427e5fc6SMatthew Dillon  * Clean up the internal mount structure and disassociate it from the mount.
866427e5fc6SMatthew Dillon  * This may issue I/O.
867b0aab9b9SMatthew Dillon  *
868b0aab9b9SMatthew Dillon  * Called with fs_token held.
869427e5fc6SMatthew Dillon  */
870427e5fc6SMatthew Dillon static void
871427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp)
872427e5fc6SMatthew Dillon {
873b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
874cdb6e4e6SMatthew Dillon 	hammer_flush_group_t flg;
8751b0ab2c3SMatthew Dillon 	int count;
876d40bfecaSMatthew Dillon 	int dummy;
877427e5fc6SMatthew Dillon 
878427e5fc6SMatthew Dillon 	/*
879cdb6e4e6SMatthew Dillon 	 * Flush anything dirty.  This won't even run if the
880cdb6e4e6SMatthew Dillon 	 * filesystem errored-out.
881427e5fc6SMatthew Dillon 	 */
8821b0ab2c3SMatthew Dillon 	count = 0;
8831b0ab2c3SMatthew Dillon 	while (hammer_flusher_haswork(hmp)) {
884059819e3SMatthew Dillon 		hammer_flusher_sync(hmp);
8851b0ab2c3SMatthew Dillon 		++count;
8861b0ab2c3SMatthew Dillon 		if (count >= 5) {
8871b0ab2c3SMatthew Dillon 			if (count == 5)
8881b0ab2c3SMatthew Dillon 				kprintf("HAMMER: umount flushing.");
8891b0ab2c3SMatthew Dillon 			else
8901b0ab2c3SMatthew Dillon 				kprintf(".");
891d40bfecaSMatthew Dillon 			tsleep(&dummy, 0, "hmrufl", hz);
8921b0ab2c3SMatthew Dillon 		}
8931b0ab2c3SMatthew Dillon 		if (count == 30) {
8941b0ab2c3SMatthew Dillon 			kprintf("giving up\n");
8951b0ab2c3SMatthew Dillon 			break;
8961b0ab2c3SMatthew Dillon 		}
8971b0ab2c3SMatthew Dillon 	}
8981b0ab2c3SMatthew Dillon 	if (count >= 5 && count < 30)
8991b0ab2c3SMatthew Dillon 		kprintf("\n");
900cdb6e4e6SMatthew Dillon 
901cdb6e4e6SMatthew Dillon 	/*
902cdb6e4e6SMatthew Dillon 	 * If the mount had a critical error we have to destroy any
903cdb6e4e6SMatthew Dillon 	 * remaining inodes before we can finish cleaning up the flusher.
904cdb6e4e6SMatthew Dillon 	 */
905cdb6e4e6SMatthew Dillon 	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
906cdb6e4e6SMatthew Dillon 		RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
907cdb6e4e6SMatthew Dillon 			hammer_destroy_inode_callback, NULL);
908cdb6e4e6SMatthew Dillon 	}
909cdb6e4e6SMatthew Dillon 
910cdb6e4e6SMatthew Dillon 	/*
911cdb6e4e6SMatthew Dillon 	 * There shouldn't be any inodes left now and any left over
912cdb6e4e6SMatthew Dillon 	 * flush groups should now be empty.
913cdb6e4e6SMatthew Dillon 	 */
914cdb6e4e6SMatthew Dillon 	KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
915cdb6e4e6SMatthew Dillon 	while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
916cdb6e4e6SMatthew Dillon 		TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
917ff003b11SMatthew Dillon 		KKASSERT(RB_EMPTY(&flg->flush_tree));
918cdb6e4e6SMatthew Dillon 		if (flg->refs) {
919cdb6e4e6SMatthew Dillon 			kprintf("HAMMER: Warning, flush_group %p was "
920cdb6e4e6SMatthew Dillon 				"not empty on umount!\n", flg);
921cdb6e4e6SMatthew Dillon 		}
922bac808feSMatthew Dillon 		kfree(flg, hmp->m_misc);
923cdb6e4e6SMatthew Dillon 	}
924cdb6e4e6SMatthew Dillon 
925cdb6e4e6SMatthew Dillon 	/*
926cdb6e4e6SMatthew Dillon 	 * We can finally destroy the flusher
927cdb6e4e6SMatthew Dillon 	 */
928059819e3SMatthew Dillon 	hammer_flusher_destroy(hmp);
929427e5fc6SMatthew Dillon 
930b84de5afSMatthew Dillon 	/*
93100f16fadSMatthew Dillon 	 * We may have held recovered buffers due to a read-only mount.
93200f16fadSMatthew Dillon 	 * These must be discarded.
93300f16fadSMatthew Dillon 	 */
93400f16fadSMatthew Dillon 	if (hmp->ronly)
93500f16fadSMatthew Dillon 		hammer_recover_flush_buffers(hmp, NULL, -1);
93600f16fadSMatthew Dillon 
93700f16fadSMatthew Dillon 	/*
9380832c9bbSMatthew Dillon 	 * Unload buffers and then volumes
939b84de5afSMatthew Dillon 	 */
9400832c9bbSMatthew Dillon         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
9410832c9bbSMatthew Dillon 		hammer_unload_buffer, NULL);
942427e5fc6SMatthew Dillon 	RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
943427e5fc6SMatthew Dillon 		hammer_unload_volume, NULL);
944427e5fc6SMatthew Dillon 
945427e5fc6SMatthew Dillon 	mp->mnt_data = NULL;
94666325755SMatthew Dillon 	mp->mnt_flag &= ~MNT_LOCAL;
947427e5fc6SMatthew Dillon 	hmp->mp = NULL;
9480729c8c8SMatthew Dillon 	hammer_destroy_objid_cache(hmp);
949507df98aSIlya Dryomov 	hammer_destroy_dedup_cache(hmp);
950507df98aSIlya Dryomov 	if (hmp->dedup_free_cache != NULL) {
951507df98aSIlya Dryomov 		kfree(hmp->dedup_free_cache, hmp->m_misc);
952507df98aSIlya Dryomov 		hmp->dedup_free_cache = NULL;
953507df98aSIlya Dryomov 	}
954bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_misc);
955bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_inodes);
956b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
957427e5fc6SMatthew Dillon 	kfree(hmp, M_HAMMER);
958427e5fc6SMatthew Dillon }
959427e5fc6SMatthew Dillon 
960427e5fc6SMatthew Dillon /*
961cdb6e4e6SMatthew Dillon  * Report critical errors.  ip may be NULL.
962cdb6e4e6SMatthew Dillon  */
963cdb6e4e6SMatthew Dillon void
964cdb6e4e6SMatthew Dillon hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
965cdb6e4e6SMatthew Dillon 		      int error, const char *msg)
966cdb6e4e6SMatthew Dillon {
967cdb6e4e6SMatthew Dillon 	hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
968c9ce54d6SMatthew Dillon 
969cdb6e4e6SMatthew Dillon 	krateprintf(&hmp->krate,
970c9ce54d6SMatthew Dillon 		    "HAMMER(%s): Critical error inode=%jd error=%d %s\n",
971cdb6e4e6SMatthew Dillon 		    hmp->mp->mnt_stat.f_mntfromname,
972c9ce54d6SMatthew Dillon 		    (intmax_t)(ip ? ip->obj_id : -1),
973c9ce54d6SMatthew Dillon 		    error, msg);
974c9ce54d6SMatthew Dillon 
975cdb6e4e6SMatthew Dillon 	if (hmp->ronly == 0) {
976cdb6e4e6SMatthew Dillon 		hmp->ronly = 2;		/* special errored read-only mode */
977cdb6e4e6SMatthew Dillon 		hmp->mp->mnt_flag |= MNT_RDONLY;
978dfec479fSMatthew Dillon 		RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
979dfec479fSMatthew Dillon 			hammer_adjust_volume_mode, NULL);
980cdb6e4e6SMatthew Dillon 		kprintf("HAMMER(%s): Forcing read-only mode\n",
981cdb6e4e6SMatthew Dillon 			hmp->mp->mnt_stat.f_mntfromname);
982cdb6e4e6SMatthew Dillon 	}
983cdb6e4e6SMatthew Dillon 	hmp->error = error;
984fc73edd8SMatthew Dillon 	if (hammer_debug_critical)
985c9ce54d6SMatthew Dillon 		Debugger("Entering debugger");
986cdb6e4e6SMatthew Dillon }
987cdb6e4e6SMatthew Dillon 
988cdb6e4e6SMatthew Dillon 
989cdb6e4e6SMatthew Dillon /*
990513ca7d7SMatthew Dillon  * Obtain a vnode for the specified inode number.  An exclusively locked
991513ca7d7SMatthew Dillon  * vnode is returned.
992513ca7d7SMatthew Dillon  */
993513ca7d7SMatthew Dillon int
994b9b0a6d0SMatthew Dillon hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
995b9b0a6d0SMatthew Dillon 		ino_t ino, struct vnode **vpp)
996513ca7d7SMatthew Dillon {
99736f82b23SMatthew Dillon 	struct hammer_transaction trans;
998513ca7d7SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
999513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
1000513ca7d7SMatthew Dillon 	int error;
1001b9b0a6d0SMatthew Dillon 	u_int32_t localization;
1002513ca7d7SMatthew Dillon 
1003b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
100436f82b23SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
100536f82b23SMatthew Dillon 
1006513ca7d7SMatthew Dillon 	/*
1007b9b0a6d0SMatthew Dillon 	 * If a directory vnode is supplied (mainly NFS) then we can acquire
1008b9b0a6d0SMatthew Dillon 	 * the PFS domain from it.  Otherwise we would only be able to vget
1009b9b0a6d0SMatthew Dillon 	 * inodes in the root PFS.
1010b9b0a6d0SMatthew Dillon 	 */
1011b9b0a6d0SMatthew Dillon 	if (dvp) {
1012b9b0a6d0SMatthew Dillon 		localization = HAMMER_DEF_LOCALIZATION +
1013b9b0a6d0SMatthew Dillon 				VTOI(dvp)->obj_localization;
1014b9b0a6d0SMatthew Dillon 	} else {
1015b9b0a6d0SMatthew Dillon 		localization = HAMMER_DEF_LOCALIZATION;
1016b9b0a6d0SMatthew Dillon 	}
1017b9b0a6d0SMatthew Dillon 
1018b9b0a6d0SMatthew Dillon 	/*
101936f82b23SMatthew Dillon 	 * Lookup the requested HAMMER inode.  The structure must be
102036f82b23SMatthew Dillon 	 * left unlocked while we manipulate the related vnode to avoid
102136f82b23SMatthew Dillon 	 * a deadlock.
1022513ca7d7SMatthew Dillon 	 */
1023ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, ino,
1024b9b0a6d0SMatthew Dillon 			      hmp->asof, localization,
1025ddfdf542SMatthew Dillon 			      0, &error);
1026513ca7d7SMatthew Dillon 	if (ip == NULL) {
1027513ca7d7SMatthew Dillon 		*vpp = NULL;
1028b0aab9b9SMatthew Dillon 	} else {
1029e8599db1SMatthew Dillon 		error = hammer_get_vnode(ip, vpp);
1030513ca7d7SMatthew Dillon 		hammer_rel_inode(ip, 0);
1031b0aab9b9SMatthew Dillon 	}
1032b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
1033b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1034513ca7d7SMatthew Dillon 	return (error);
1035513ca7d7SMatthew Dillon }
1036513ca7d7SMatthew Dillon 
1037513ca7d7SMatthew Dillon /*
1038427e5fc6SMatthew Dillon  * Return the root vnode for the filesystem.
1039427e5fc6SMatthew Dillon  *
1040427e5fc6SMatthew Dillon  * HAMMER stores the root vnode in the hammer_mount structure so
1041427e5fc6SMatthew Dillon  * getting it is easy.
1042427e5fc6SMatthew Dillon  */
1043427e5fc6SMatthew Dillon static int
1044427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp)
1045427e5fc6SMatthew Dillon {
104627ea2398SMatthew Dillon 	int error;
1047427e5fc6SMatthew Dillon 
1048b9b0a6d0SMatthew Dillon 	error = hammer_vfs_vget(mp, NULL, 1, vpp);
104927ea2398SMatthew Dillon 	return (error);
1050427e5fc6SMatthew Dillon }
1051427e5fc6SMatthew Dillon 
1052427e5fc6SMatthew Dillon static int
1053427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1054427e5fc6SMatthew Dillon {
1055fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
1056fbc6e32aSMatthew Dillon 	hammer_volume_t volume;
1057fbc6e32aSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
1058fbc6e32aSMatthew Dillon 	int error;
105947197d71SMatthew Dillon 	int64_t bfree;
106031a56ce2SMatthew Dillon 	int64_t breserved;
1061fbc6e32aSMatthew Dillon 
1062b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1063fbc6e32aSMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
1064b0aab9b9SMatthew Dillon 	if (error) {
1065b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
1066fbc6e32aSMatthew Dillon 		return(error);
1067b0aab9b9SMatthew Dillon 	}
1068fbc6e32aSMatthew Dillon 	ondisk = volume->ondisk;
1069fbc6e32aSMatthew Dillon 
107047197d71SMatthew Dillon 	/*
107147197d71SMatthew Dillon 	 * Basic stats
107247197d71SMatthew Dillon 	 */
107331a56ce2SMatthew Dillon 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
1074fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
1075e04ee2deSTomohiro Kusumi 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
107640043e7fSMatthew Dillon 	hammer_rel_volume(volume, 0);
107747197d71SMatthew Dillon 
107831a56ce2SMatthew Dillon 	mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
107947197d71SMatthew Dillon 	mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1080fbc6e32aSMatthew Dillon 	if (mp->mnt_stat.f_files < 0)
1081fbc6e32aSMatthew Dillon 		mp->mnt_stat.f_files = 0;
1082fbc6e32aSMatthew Dillon 
108327ea2398SMatthew Dillon 	*sbp = mp->mnt_stat;
1084b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
108527ea2398SMatthew Dillon 	return(0);
1086427e5fc6SMatthew Dillon }
1087427e5fc6SMatthew Dillon 
10886f97fce3SMatthew Dillon static int
10896f97fce3SMatthew Dillon hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
10906f97fce3SMatthew Dillon {
10916f97fce3SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
10926f97fce3SMatthew Dillon 	hammer_volume_t volume;
10936f97fce3SMatthew Dillon 	hammer_volume_ondisk_t ondisk;
10946f97fce3SMatthew Dillon 	int error;
10956f97fce3SMatthew Dillon 	int64_t bfree;
10960f65be10SMatthew Dillon 	int64_t breserved;
10976f97fce3SMatthew Dillon 
1098b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
10996f97fce3SMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
1100b0aab9b9SMatthew Dillon 	if (error) {
1101b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
11026f97fce3SMatthew Dillon 		return(error);
1103b0aab9b9SMatthew Dillon 	}
11046f97fce3SMatthew Dillon 	ondisk = volume->ondisk;
11056f97fce3SMatthew Dillon 
11066f97fce3SMatthew Dillon 	/*
11076f97fce3SMatthew Dillon 	 * Basic stats
11086f97fce3SMatthew Dillon 	 */
11090f65be10SMatthew Dillon 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
11106f97fce3SMatthew Dillon 	mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
1111e04ee2deSTomohiro Kusumi 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
11126f97fce3SMatthew Dillon 	hammer_rel_volume(volume, 0);
11136f97fce3SMatthew Dillon 
11140f65be10SMatthew Dillon 	mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
1115c0763659SMatthew Dillon 	mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
11166f97fce3SMatthew Dillon 	if (mp->mnt_vstat.f_files < 0)
11176f97fce3SMatthew Dillon 		mp->mnt_vstat.f_files = 0;
11186f97fce3SMatthew Dillon 	*sbp = mp->mnt_vstat;
1119b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
11206f97fce3SMatthew Dillon 	return(0);
11216f97fce3SMatthew Dillon }
11226f97fce3SMatthew Dillon 
11230729c8c8SMatthew Dillon /*
11240729c8c8SMatthew Dillon  * Sync the filesystem.  Currently we have to run it twice, the second
11250729c8c8SMatthew Dillon  * one will advance the undo start index to the end index, so if a crash
11260729c8c8SMatthew Dillon  * occurs no undos will be run on mount.
112777062c8aSMatthew Dillon  *
112877062c8aSMatthew Dillon  * We do not sync the filesystem if we are called from a panic.  If we did
112977062c8aSMatthew Dillon  * we might end up blowing up a sync that was already in progress.
11300729c8c8SMatthew Dillon  */
1131427e5fc6SMatthew Dillon static int
1132427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor)
1133427e5fc6SMatthew Dillon {
1134fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
11350729c8c8SMatthew Dillon 	int error;
11360729c8c8SMatthew Dillon 
1137b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
113877062c8aSMatthew Dillon 	if (panicstr == NULL) {
11390729c8c8SMatthew Dillon 		error = hammer_sync_hmp(hmp, waitfor);
114077062c8aSMatthew Dillon 	} else {
114177062c8aSMatthew Dillon 		error = EIO;
114277062c8aSMatthew Dillon 	}
1143b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
11440729c8c8SMatthew Dillon 	return (error);
1145427e5fc6SMatthew Dillon }
1146427e5fc6SMatthew Dillon 
1147513ca7d7SMatthew Dillon /*
1148513ca7d7SMatthew Dillon  * Convert a vnode to a file handle.
1149b0aab9b9SMatthew Dillon  *
1150b0aab9b9SMatthew Dillon  * Accesses read-only fields on already-referenced structures so
1151b0aab9b9SMatthew Dillon  * no token is needed.
1152513ca7d7SMatthew Dillon  */
1153513ca7d7SMatthew Dillon static int
1154513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1155513ca7d7SMatthew Dillon {
1156513ca7d7SMatthew Dillon 	hammer_inode_t ip;
1157513ca7d7SMatthew Dillon 
1158513ca7d7SMatthew Dillon 	KKASSERT(MAXFIDSZ >= 16);
1159513ca7d7SMatthew Dillon 	ip = VTOI(vp);
1160513ca7d7SMatthew Dillon 	fhp->fid_len = offsetof(struct fid, fid_data[16]);
1161adf01747SMatthew Dillon 	fhp->fid_ext = ip->obj_localization >> 16;
1162513ca7d7SMatthew Dillon 	bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
1163513ca7d7SMatthew Dillon 	bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
1164513ca7d7SMatthew Dillon 	return(0);
1165513ca7d7SMatthew Dillon }
1166513ca7d7SMatthew Dillon 
1167513ca7d7SMatthew Dillon 
1168513ca7d7SMatthew Dillon /*
1169513ca7d7SMatthew Dillon  * Convert a file handle back to a vnode.
117067863d04SMatthew Dillon  *
117167863d04SMatthew Dillon  * Use rootvp to enforce PFS isolation when a PFS is exported via a
117267863d04SMatthew Dillon  * null mount.
1173513ca7d7SMatthew Dillon  */
1174513ca7d7SMatthew Dillon static int
117567863d04SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
117667863d04SMatthew Dillon 		  struct fid *fhp, struct vnode **vpp)
1177513ca7d7SMatthew Dillon {
1178b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
117936f82b23SMatthew Dillon 	struct hammer_transaction trans;
1180513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
1181513ca7d7SMatthew Dillon 	struct hammer_inode_info info;
1182513ca7d7SMatthew Dillon 	int error;
1183adf01747SMatthew Dillon 	u_int32_t localization;
1184513ca7d7SMatthew Dillon 
1185513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
1186513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
118767863d04SMatthew Dillon 	if (rootvp)
118867863d04SMatthew Dillon 		localization = VTOI(rootvp)->obj_localization;
118967863d04SMatthew Dillon 	else
1190adf01747SMatthew Dillon 		localization = (u_int32_t)fhp->fid_ext << 16;
1191513ca7d7SMatthew Dillon 
1192b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1193b0aab9b9SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
119436f82b23SMatthew Dillon 
1195513ca7d7SMatthew Dillon 	/*
1196513ca7d7SMatthew Dillon 	 * Get/allocate the hammer_inode structure.  The structure must be
1197513ca7d7SMatthew Dillon 	 * unlocked while we manipulate the related vnode to avoid a
1198513ca7d7SMatthew Dillon 	 * deadlock.
1199513ca7d7SMatthew Dillon 	 */
1200ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, info.obj_id,
1201adf01747SMatthew Dillon 			      info.obj_asof, localization, 0, &error);
1202d978e7cfSMatthew Dillon 	if (ip) {
1203e8599db1SMatthew Dillon 		error = hammer_get_vnode(ip, vpp);
1204513ca7d7SMatthew Dillon 		hammer_rel_inode(ip, 0);
1205d978e7cfSMatthew Dillon 	} else {
1206d978e7cfSMatthew Dillon 		*vpp = NULL;
1207d978e7cfSMatthew Dillon 	}
1208b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
1209b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1210513ca7d7SMatthew Dillon 	return (error);
1211513ca7d7SMatthew Dillon }
1212513ca7d7SMatthew Dillon 
1213513ca7d7SMatthew Dillon static int
1214513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1215513ca7d7SMatthew Dillon 		    int *exflagsp, struct ucred **credanonp)
1216513ca7d7SMatthew Dillon {
1217513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
1218513ca7d7SMatthew Dillon 	struct netcred *np;
1219513ca7d7SMatthew Dillon 	int error;
1220513ca7d7SMatthew Dillon 
1221b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1222513ca7d7SMatthew Dillon 	np = vfs_export_lookup(mp, &hmp->export, nam);
1223513ca7d7SMatthew Dillon 	if (np) {
1224513ca7d7SMatthew Dillon 		*exflagsp = np->netc_exflags;
1225513ca7d7SMatthew Dillon 		*credanonp = &np->netc_anon;
1226513ca7d7SMatthew Dillon 		error = 0;
1227513ca7d7SMatthew Dillon 	} else {
1228513ca7d7SMatthew Dillon 		error = EACCES;
1229513ca7d7SMatthew Dillon 	}
1230b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1231513ca7d7SMatthew Dillon 	return (error);
1232513ca7d7SMatthew Dillon 
1233513ca7d7SMatthew Dillon }
1234513ca7d7SMatthew Dillon 
1235513ca7d7SMatthew Dillon int
1236513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
1237513ca7d7SMatthew Dillon {
1238513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
1239513ca7d7SMatthew Dillon 	int error;
1240513ca7d7SMatthew Dillon 
1241b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1242b0aab9b9SMatthew Dillon 
1243513ca7d7SMatthew Dillon 	switch(op) {
1244513ca7d7SMatthew Dillon 	case MOUNTCTL_SET_EXPORT:
1245513ca7d7SMatthew Dillon 		error = vfs_export(mp, &hmp->export, export);
1246513ca7d7SMatthew Dillon 		break;
1247513ca7d7SMatthew Dillon 	default:
1248513ca7d7SMatthew Dillon 		error = EOPNOTSUPP;
1249513ca7d7SMatthew Dillon 		break;
1250513ca7d7SMatthew Dillon 	}
1251b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1252b0aab9b9SMatthew Dillon 
1253513ca7d7SMatthew Dillon 	return(error);
1254513ca7d7SMatthew Dillon }
1255513ca7d7SMatthew Dillon 
1256