xref: /dflybsd-src/sys/vfs/hammer/hammer_vfsops.c (revision 1bcc92995aeb57aa1f0de822a4d584645a2db2b5)
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/nlookup.h>
36427e5fc6SMatthew Dillon #include <sys/fcntl.h>
37b3deaf57SMatthew Dillon #include <sys/sysctl.h>
38b45803e3STomohiro Kusumi 
39427e5fc6SMatthew Dillon #include "hammer.h"
40427e5fc6SMatthew Dillon 
41aac0aabdSMatthew Dillon /*
42aac0aabdSMatthew Dillon  * NOTE!  Global statistics may not be MPSAFE so HAMMER never uses them
43aac0aabdSMatthew Dillon  *	  in conditionals.
44aac0aabdSMatthew Dillon  */
4502428fb6SMatthew Dillon int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT;
462f85fa4dSMatthew Dillon int hammer_debug_io;
47d5ef456eSMatthew Dillon int hammer_debug_general;
481b0ab2c3SMatthew Dillon int hammer_debug_debug = 1;		/* medium-error panics */
49e8599db1SMatthew Dillon int hammer_debug_inode;
507d683b0fSMatthew Dillon int hammer_debug_locks;
51b3deaf57SMatthew Dillon int hammer_debug_btree;
52d113fda1SMatthew Dillon int hammer_debug_tid;
5346fe7ae1SMatthew Dillon int hammer_debug_recover;		/* -1 will disable, +1 will force */
5446fe7ae1SMatthew Dillon int hammer_debug_recover_faults;
55fc73edd8SMatthew Dillon int hammer_debug_critical;		/* non-zero enter debugger on error */
561b0ab2c3SMatthew Dillon int hammer_cluster_enable = 1;		/* enable read clustering by default */
57507df98aSIlya Dryomov int hammer_live_dedup = 0;
58e2a02b72SMatthew Dillon int hammer_tdmux_ticks;
597a61b85dSMatthew Dillon int hammer_count_fsyncs;
60b3deaf57SMatthew Dillon int hammer_count_inodes;
61af209b0fSMatthew Dillon int hammer_count_iqueued;
62e2a02b72SMatthew Dillon int hammer_count_reclaims;
63b3deaf57SMatthew Dillon int hammer_count_records;
64b3deaf57SMatthew Dillon int hammer_count_record_datas;
65b3deaf57SMatthew Dillon int hammer_count_volumes;
66b3deaf57SMatthew Dillon int hammer_count_buffers;
67b3deaf57SMatthew Dillon int hammer_count_nodes;
68a7e9bef1SMatthew Dillon int64_t hammer_count_extra_space_used;
69cb51be26SMatthew Dillon int64_t hammer_stats_btree_lookups;
70cb51be26SMatthew Dillon int64_t hammer_stats_btree_searches;
71cb51be26SMatthew Dillon int64_t hammer_stats_btree_inserts;
72cb51be26SMatthew Dillon int64_t hammer_stats_btree_deletes;
73cb51be26SMatthew Dillon int64_t hammer_stats_btree_elements;
74cb51be26SMatthew Dillon int64_t hammer_stats_btree_splits;
75cb51be26SMatthew Dillon int64_t hammer_stats_btree_iterations;
7639d8fd63SMatthew Dillon int64_t hammer_stats_btree_root_iterations;
77cb51be26SMatthew Dillon int64_t hammer_stats_record_iterations;
78ce0138a6SMatthew Dillon 
79ce0138a6SMatthew Dillon int64_t hammer_stats_file_read;
80ce0138a6SMatthew Dillon int64_t hammer_stats_file_write;
81ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsr;
82ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsw;
83ce0138a6SMatthew Dillon int64_t hammer_stats_disk_read;
84ce0138a6SMatthew Dillon int64_t hammer_stats_disk_write;
85ce0138a6SMatthew Dillon int64_t hammer_stats_inode_flushes;
86ce0138a6SMatthew Dillon int64_t hammer_stats_commits;
8789e744ceSMatthew Dillon int64_t hammer_stats_undo;
886048b411SMatthew Dillon int64_t hammer_stats_redo;
89ce0138a6SMatthew Dillon 
903583bbb4SMatthew Dillon long hammer_count_dirtybufspace;	/* global */
91a99b9ea2SMatthew Dillon int hammer_count_refedbufs;		/* global */
920832c9bbSMatthew Dillon int hammer_count_reservations;
933583bbb4SMatthew Dillon long hammer_count_io_running_read;
943583bbb4SMatthew Dillon long hammer_count_io_running_write;
95a99b9ea2SMatthew Dillon int hammer_count_io_locked;
963583bbb4SMatthew Dillon long hammer_limit_dirtybufspace;	/* per-mount */
970832c9bbSMatthew Dillon int hammer_limit_recs;			/* as a whole XXX */
98a117fbebSMatthew Dillon int hammer_limit_inode_recs = 2048;	/* per inode */
99e2a02b72SMatthew Dillon int hammer_limit_reclaims;
100e2ef7a95SIlya Dryomov int hammer_live_dedup_cache_size = DEDUP_CACHE_SIZE;
1016048b411SMatthew Dillon int hammer_limit_redo = 4096 * 1024;	/* per inode */
10237646115SMatthew Dillon int hammer_autoflush = 500;		/* auto flush (typ on reclaim) */
1031f07f686SMatthew Dillon int hammer_bio_count;
104cb51be26SMatthew Dillon int hammer_verify_zone;
1051b0ab2c3SMatthew Dillon int hammer_verify_data = 1;
106cb51be26SMatthew Dillon int hammer_write_mode;
107b4f86ea3SMatthew Dillon int hammer_double_buffer;
108283a4a38SMatthew Dillon int hammer_btree_full_undo = 1;
1093e583440SMatthew Dillon int hammer_yield_check = 16;
1109d0a6205SMatthew Dillon int hammer_fsync_mode = 3;
1117d683b0fSMatthew Dillon int64_t hammer_contention_count;
112f03c9cf4SMatthew Dillon int64_t hammer_zone_limit;
113b3deaf57SMatthew Dillon 
114507df98aSIlya Dryomov /*
115507df98aSIlya Dryomov  * Live dedup debug counters (sysctls are writable so that counters
116507df98aSIlya Dryomov  * can be reset from userspace).
117507df98aSIlya Dryomov  */
118507df98aSIlya Dryomov int64_t hammer_live_dedup_vnode_bcmps = 0;
119507df98aSIlya Dryomov int64_t hammer_live_dedup_device_bcmps = 0;
120507df98aSIlya Dryomov int64_t hammer_live_dedup_findblk_failures = 0;
121507df98aSIlya Dryomov int64_t hammer_live_dedup_bmap_saves = 0;
122507df98aSIlya Dryomov 
123507df98aSIlya Dryomov 
124b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
125507df98aSIlya Dryomov 
1265987cc42SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD,
1275987cc42SMatthew Dillon 	   &hammer_supported_version, 0, "");
128d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
129d5ef456eSMatthew Dillon 	   &hammer_debug_general, 0, "");
1302f85fa4dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
1312f85fa4dSMatthew Dillon 	   &hammer_debug_io, 0, "");
13277062c8aSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
13377062c8aSMatthew Dillon 	   &hammer_debug_debug, 0, "");
134e8599db1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
135e8599db1SMatthew Dillon 	   &hammer_debug_inode, 0, "");
1367d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
1377d683b0fSMatthew Dillon 	   &hammer_debug_locks, 0, "");
138b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
139b3deaf57SMatthew Dillon 	   &hammer_debug_btree, 0, "");
140d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
141d113fda1SMatthew Dillon 	   &hammer_debug_tid, 0, "");
142b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
143b33e2cc0SMatthew Dillon 	   &hammer_debug_recover, 0, "");
14446fe7ae1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
14546fe7ae1SMatthew Dillon 	   &hammer_debug_recover_faults, 0, "");
146fc73edd8SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW,
147fc73edd8SMatthew Dillon 	   &hammer_debug_critical, 0, "");
1481b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
1491b0ab2c3SMatthew Dillon 	   &hammer_cluster_enable, 0, "");
150507df98aSIlya Dryomov /*
151507df98aSIlya Dryomov  * 0 - live dedup is disabled
152507df98aSIlya Dryomov  * 1 - dedup cache is populated on reads only
153507df98aSIlya Dryomov  * 2 - dedup cache is populated on both reads and writes
154283a4a38SMatthew Dillon  *
155283a4a38SMatthew Dillon  * LIVE_DEDUP IS DISABLED PERMANENTLY!  This feature appears to cause
156283a4a38SMatthew Dillon  * blockmap corruption over time so we've turned it off permanently.
157507df98aSIlya Dryomov  */
1586de803f0SFrançois Tigeot SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup, CTLFLAG_RD,
1596de803f0SFrançois Tigeot 	   &hammer_live_dedup, 0, "Enable live dedup (experimental)");
160e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, tdmux_ticks, CTLFLAG_RW,
161e2a02b72SMatthew Dillon 	   &hammer_tdmux_ticks, 0, "Hammer tdmux ticks");
1629480ff55SMatthew Dillon 
1633583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
164f5a07a7aSMatthew Dillon 	   &hammer_limit_dirtybufspace, 0, "");
16547637bffSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
16647637bffSMatthew Dillon 	   &hammer_limit_recs, 0, "");
167de996e86SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW,
168de996e86SMatthew Dillon 	   &hammer_limit_inode_recs, 0, "");
169e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaims, CTLFLAG_RW,
170e2a02b72SMatthew Dillon 	   &hammer_limit_reclaims, 0, "");
171e2ef7a95SIlya Dryomov SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW,
1722b34db9bSSamuel J. Greear 	   &hammer_live_dedup_cache_size, 0,
1732b34db9bSSamuel J. Greear 	   "Number of cache entries");
1746048b411SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW,
1756048b411SMatthew Dillon 	   &hammer_limit_redo, 0, "");
1769480ff55SMatthew Dillon 
1777a61b85dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
1787a61b85dSMatthew Dillon 	   &hammer_count_fsyncs, 0, "");
179b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
180b3deaf57SMatthew Dillon 	   &hammer_count_inodes, 0, "");
181af209b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
182af209b0fSMatthew Dillon 	   &hammer_count_iqueued, 0, "");
183e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaims, CTLFLAG_RD,
184e2a02b72SMatthew Dillon 	   &hammer_count_reclaims, 0, "");
185b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
186b3deaf57SMatthew Dillon 	   &hammer_count_records, 0, "");
187b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
188b3deaf57SMatthew Dillon 	   &hammer_count_record_datas, 0, "");
189b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
190b3deaf57SMatthew Dillon 	   &hammer_count_volumes, 0, "");
191b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
192b3deaf57SMatthew Dillon 	   &hammer_count_buffers, 0, "");
193b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
194b3deaf57SMatthew Dillon 	   &hammer_count_nodes, 0, "");
195a7e9bef1SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD,
196a7e9bef1SMatthew Dillon 	   &hammer_count_extra_space_used, 0, "");
197ce0138a6SMatthew Dillon 
198cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
199cb51be26SMatthew Dillon 	   &hammer_stats_btree_searches, 0, "");
200cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
201cb51be26SMatthew Dillon 	   &hammer_stats_btree_lookups, 0, "");
202cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
203cb51be26SMatthew Dillon 	   &hammer_stats_btree_inserts, 0, "");
204cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
205cb51be26SMatthew Dillon 	   &hammer_stats_btree_deletes, 0, "");
206cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
207cb51be26SMatthew Dillon 	   &hammer_stats_btree_elements, 0, "");
208cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
209cb51be26SMatthew Dillon 	   &hammer_stats_btree_splits, 0, "");
210cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
211cb51be26SMatthew Dillon 	   &hammer_stats_btree_iterations, 0, "");
21239d8fd63SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD,
21339d8fd63SMatthew Dillon 	   &hammer_stats_btree_root_iterations, 0, "");
214cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
215cb51be26SMatthew Dillon 	   &hammer_stats_record_iterations, 0, "");
216ce0138a6SMatthew Dillon 
217ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
218ce0138a6SMatthew Dillon 	   &hammer_stats_file_read, 0, "");
219ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
220ce0138a6SMatthew Dillon 	   &hammer_stats_file_write, 0, "");
221ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD,
222ce0138a6SMatthew Dillon 	   &hammer_stats_file_iopsr, 0, "");
223ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD,
224ce0138a6SMatthew Dillon 	   &hammer_stats_file_iopsw, 0, "");
225ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
226ce0138a6SMatthew Dillon 	   &hammer_stats_disk_read, 0, "");
227ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
228ce0138a6SMatthew Dillon 	   &hammer_stats_disk_write, 0, "");
229ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
230ce0138a6SMatthew Dillon 	   &hammer_stats_inode_flushes, 0, "");
231ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
232ce0138a6SMatthew Dillon 	   &hammer_stats_commits, 0, "");
23389e744ceSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD,
23489e744ceSMatthew Dillon 	   &hammer_stats_undo, 0, "");
2356048b411SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD,
2366048b411SMatthew Dillon 	   &hammer_stats_redo, 0, "");
237ce0138a6SMatthew Dillon 
238507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_vnode_bcmps, CTLFLAG_RW,
239f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_vnode_bcmps, 0,
240f06b3d6dSSamuel J. Greear 	    "successful vnode buffer comparisons");
241507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_device_bcmps, CTLFLAG_RW,
242f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_device_bcmps, 0,
243f06b3d6dSSamuel J. Greear 	    "successful device buffer comparisons");
244507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_findblk_failures, CTLFLAG_RW,
245f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_findblk_failures, 0,
246f06b3d6dSSamuel J. Greear 	    "block lookup failures for comparison");
247507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_bmap_saves, CTLFLAG_RW,
248f06b3d6dSSamuel J. Greear 	    &hammer_live_dedup_bmap_saves, 0,
249f06b3d6dSSamuel J. Greear 	    "useful physical block lookups");
250507df98aSIlya Dryomov 
2513583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
252f5a07a7aSMatthew Dillon 	   &hammer_count_dirtybufspace, 0, "");
253a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
254a99b9ea2SMatthew Dillon 	   &hammer_count_refedbufs, 0, "");
2550832c9bbSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
2560832c9bbSMatthew Dillon 	   &hammer_count_reservations, 0, "");
2573583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
258a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_read, 0, "");
259a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
260a99b9ea2SMatthew Dillon 	   &hammer_count_io_locked, 0, "");
2613583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
262a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_write, 0, "");
263f03c9cf4SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
264f03c9cf4SMatthew Dillon 	   &hammer_zone_limit, 0, "");
2657d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
2667d683b0fSMatthew Dillon 	   &hammer_contention_count, 0, "");
26721fde338SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
26821fde338SMatthew Dillon 	   &hammer_autoflush, 0, "");
269cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
270cb51be26SMatthew Dillon 	   &hammer_verify_zone, 0, "");
2711b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
2721b0ab2c3SMatthew Dillon 	   &hammer_verify_data, 0, "");
273cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
274cb51be26SMatthew Dillon 	   &hammer_write_mode, 0, "");
275b4f86ea3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, double_buffer, CTLFLAG_RW,
276b4f86ea3SMatthew Dillon 	   &hammer_double_buffer, 0, "");
277283a4a38SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, btree_full_undo, CTLFLAG_RW,
278283a4a38SMatthew Dillon 	   &hammer_btree_full_undo, 0, "");
2793e583440SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW,
2803e583440SMatthew Dillon 	   &hammer_yield_check, 0, "");
2816f3d87c0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW,
2826f3d87c0SMatthew Dillon 	   &hammer_fsync_mode, 0, "");
283b3deaf57SMatthew Dillon 
284879a1b60SSascha Wildner /* KTR_INFO_MASTER(hammer); */
285bcac4bbbSMatthew Dillon 
286427e5fc6SMatthew Dillon /*
287427e5fc6SMatthew Dillon  * VFS ABI
288427e5fc6SMatthew Dillon  */
289427e5fc6SMatthew Dillon static void	hammer_free_hmp(struct mount *mp);
290427e5fc6SMatthew Dillon 
291427e5fc6SMatthew Dillon static int	hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
292427e5fc6SMatthew Dillon 				struct ucred *cred);
293427e5fc6SMatthew Dillon static int	hammer_vfs_unmount(struct mount *mp, int mntflags);
294427e5fc6SMatthew Dillon static int	hammer_vfs_root(struct mount *mp, struct vnode **vpp);
295427e5fc6SMatthew Dillon static int	hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
296427e5fc6SMatthew Dillon 				struct ucred *cred);
2976f97fce3SMatthew Dillon static int	hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
2986f97fce3SMatthew Dillon 				struct ucred *cred);
299427e5fc6SMatthew Dillon static int	hammer_vfs_sync(struct mount *mp, int waitfor);
300b9b0a6d0SMatthew Dillon static int	hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
301b9b0a6d0SMatthew Dillon 				ino_t ino, struct vnode **vpp);
302427e5fc6SMatthew Dillon static int	hammer_vfs_init(struct vfsconf *conf);
30367863d04SMatthew Dillon static int	hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
30467863d04SMatthew Dillon 				struct fid *fhp, struct vnode **vpp);
305513ca7d7SMatthew Dillon static int	hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
306513ca7d7SMatthew Dillon static int	hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
307513ca7d7SMatthew Dillon 				int *exflagsp, struct ucred **credanonp);
308513ca7d7SMatthew Dillon 
309427e5fc6SMatthew Dillon 
310427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = {
311427e5fc6SMatthew Dillon 	.vfs_mount	= hammer_vfs_mount,
312427e5fc6SMatthew Dillon 	.vfs_unmount	= hammer_vfs_unmount,
313427e5fc6SMatthew Dillon 	.vfs_root	= hammer_vfs_root,
314427e5fc6SMatthew Dillon 	.vfs_statfs	= hammer_vfs_statfs,
3156f97fce3SMatthew Dillon 	.vfs_statvfs	= hammer_vfs_statvfs,
316427e5fc6SMatthew Dillon 	.vfs_sync	= hammer_vfs_sync,
317427e5fc6SMatthew Dillon 	.vfs_vget	= hammer_vfs_vget,
318513ca7d7SMatthew Dillon 	.vfs_init	= hammer_vfs_init,
319513ca7d7SMatthew Dillon 	.vfs_vptofh	= hammer_vfs_vptofh,
320513ca7d7SMatthew Dillon 	.vfs_fhtovp	= hammer_vfs_fhtovp,
321513ca7d7SMatthew Dillon 	.vfs_checkexp	= hammer_vfs_checkexp
322427e5fc6SMatthew Dillon };
323427e5fc6SMatthew Dillon 
324bac808feSMatthew Dillon MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", "");
325427e5fc6SMatthew Dillon 
326427e5fc6SMatthew Dillon VFS_SET(hammer_vfsops, hammer, 0);
327427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1);
328427e5fc6SMatthew Dillon 
329427e5fc6SMatthew Dillon static int
330427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf)
331427e5fc6SMatthew Dillon {
33269906b47SMatthew Dillon 	long n;
3333098dc2fSMatthew Dillon 
334e2a02b72SMatthew Dillon 	/*
335e2a02b72SMatthew Dillon 	 * Wait up to this long for an exclusive deadlock to clear
336e2a02b72SMatthew Dillon 	 * before acquiring a new shared lock on the ip.  The deadlock
337e2a02b72SMatthew Dillon 	 * may have occured on a b-tree node related to the ip.
338e2a02b72SMatthew Dillon 	 */
339e2a02b72SMatthew Dillon 	if (hammer_tdmux_ticks == 0)
340e2a02b72SMatthew Dillon 		hammer_tdmux_ticks = hz / 5;
341e2a02b72SMatthew Dillon 
342e2a02b72SMatthew Dillon 	/*
34369906b47SMatthew Dillon 	 * Autosize, but be careful because a hammer filesystem's
34469906b47SMatthew Dillon 	 * reserve is partially calculated based on dirtybufspace,
34569906b47SMatthew Dillon 	 * so we simply cannot allow it to get too large.
346e2a02b72SMatthew Dillon 	 */
3473098dc2fSMatthew Dillon 	if (hammer_limit_recs == 0) {
34869906b47SMatthew Dillon 		n = nbuf * 25;
34969906b47SMatthew Dillon 		if (n > kmalloc_limit(M_HAMMER) / 512)
3503098dc2fSMatthew Dillon 			n = kmalloc_limit(M_HAMMER) / 512;
35169906b47SMatthew Dillon 		if (n > 2 * 1024 * 1024)
35269906b47SMatthew Dillon 			n = 2 * 1024 * 1024;
35369906b47SMatthew Dillon 		hammer_limit_recs = (int)n;
3543098dc2fSMatthew Dillon 	}
355f5a07a7aSMatthew Dillon 	if (hammer_limit_dirtybufspace == 0) {
356f5a07a7aSMatthew Dillon 		hammer_limit_dirtybufspace = hidirtybufspace / 2;
35769906b47SMatthew Dillon 		if (hammer_limit_dirtybufspace < 1L * 1024 * 1024)
35869906b47SMatthew Dillon 			hammer_limit_dirtybufspace = 1024L * 1024;
35969906b47SMatthew Dillon 		if (hammer_limit_dirtybufspace > 1024L * 1024 * 1024)
36069906b47SMatthew Dillon 			hammer_limit_dirtybufspace = 1024L * 1024 * 1024;
3619f5097dcSMatthew Dillon 	}
362ba298df1SMatthew Dillon 
363ba298df1SMatthew Dillon 	/*
36437646115SMatthew Dillon 	 * The hammer_inode structure detaches from the vnode on reclaim.
36537646115SMatthew Dillon 	 * This limits the number of inodes in this state to prevent a
36637646115SMatthew Dillon 	 * memory pool blowout.
36737646115SMatthew Dillon 	 */
368e2a02b72SMatthew Dillon 	if (hammer_limit_reclaims == 0)
369e2a02b72SMatthew Dillon 		hammer_limit_reclaims = desiredvnodes / 10;
37037646115SMatthew Dillon 
371427e5fc6SMatthew Dillon 	return(0);
372427e5fc6SMatthew Dillon }
373427e5fc6SMatthew Dillon 
374427e5fc6SMatthew Dillon static int
375427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
376427e5fc6SMatthew Dillon 		 struct ucred *cred)
377427e5fc6SMatthew Dillon {
378427e5fc6SMatthew Dillon 	struct hammer_mount_info info;
379a89aec1bSMatthew Dillon 	hammer_mount_t hmp;
380a89aec1bSMatthew Dillon 	hammer_volume_t rootvol;
38127ea2398SMatthew Dillon 	struct vnode *rootvp;
3827c19b529SMichael Neumann 	struct vnode *devvp = NULL;
383427e5fc6SMatthew Dillon 	const char *upath;	/* volume name in userspace */
384427e5fc6SMatthew Dillon 	char *path;		/* volume name in system space */
385427e5fc6SMatthew Dillon 	int error;
386427e5fc6SMatthew Dillon 	int i;
387732a1697SMatthew Dillon 	int master_id;
388c302e844STomohiro Kusumi 	int nvolumes;
389104cb849SMichael Neumann 	char *next_volume_ptr = NULL;
390ab0bf4a3SMatthew Dillon 
391ab0bf4a3SMatthew Dillon 	/*
392ab0bf4a3SMatthew Dillon 	 * Accept hammer_mount_info.  mntpt is NULL for root mounts at boot.
393ab0bf4a3SMatthew Dillon 	 */
394ab0bf4a3SMatthew Dillon 	if (mntpt == NULL) {
3957c19b529SMichael Neumann 		bzero(&info, sizeof(info));
3967c19b529SMichael Neumann 		info.asof = 0;
3977c19b529SMichael Neumann 		info.hflags = 0;
3987c19b529SMichael Neumann 		info.nvolumes = 1;
399104cb849SMichael Neumann 
400104cb849SMichael Neumann 		next_volume_ptr = mp->mnt_stat.f_mntfromname;
401104cb849SMichael Neumann 
402104cb849SMichael Neumann 		/* Count number of volumes separated by ':' */
403104cb849SMichael Neumann 		for (char *p = next_volume_ptr; *p != '\0'; ++p) {
404104cb849SMichael Neumann 			if (*p == ':') {
405104cb849SMichael Neumann 				++info.nvolumes;
406104cb849SMichael Neumann 			}
407104cb849SMichael Neumann 		}
408104cb849SMichael Neumann 
409104cb849SMichael Neumann 		mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
4107c19b529SMichael Neumann 	} else {
411427e5fc6SMatthew Dillon 		if ((error = copyin(data, &info, sizeof(info))) != 0)
412427e5fc6SMatthew Dillon 			return (error);
4137c19b529SMichael Neumann 	}
414427e5fc6SMatthew Dillon 
415427e5fc6SMatthew Dillon 	/*
416732a1697SMatthew Dillon 	 * updating or new mount
417427e5fc6SMatthew Dillon 	 */
418195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
419195c19a1SMatthew Dillon 		hmp = (void *)mp->mnt_data;
420195c19a1SMatthew Dillon 		KKASSERT(hmp != NULL);
421195c19a1SMatthew Dillon 	} else {
4227529c2acSAntonio Huete Jimenez 		if (info.nvolumes <= 0 || info.nvolumes > HAMMER_MAX_VOLUMES)
423732a1697SMatthew Dillon 			return (EINVAL);
424732a1697SMatthew Dillon 		hmp = NULL;
425732a1697SMatthew Dillon 	}
426732a1697SMatthew Dillon 
427732a1697SMatthew Dillon 	/*
428732a1697SMatthew Dillon 	 * master-id validation.  The master id may not be changed by a
429732a1697SMatthew Dillon 	 * mount update.
430732a1697SMatthew Dillon 	 */
431732a1697SMatthew Dillon 	if (info.hflags & HMNT_MASTERID) {
432732a1697SMatthew Dillon 		if (hmp && hmp->master_id != info.master_id) {
433c54975d5STomohiro Kusumi 			kprintf("HAMMER: cannot change master id "
434732a1697SMatthew Dillon 				"with mount update\n");
435732a1697SMatthew Dillon 			return(EINVAL);
436732a1697SMatthew Dillon 		}
437732a1697SMatthew Dillon 		master_id = info.master_id;
438732a1697SMatthew Dillon 		if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
439732a1697SMatthew Dillon 			return (EINVAL);
440732a1697SMatthew Dillon 	} else {
441732a1697SMatthew Dillon 		if (hmp)
442732a1697SMatthew Dillon 			master_id = hmp->master_id;
443732a1697SMatthew Dillon 		else
444732a1697SMatthew Dillon 			master_id = 0;
445732a1697SMatthew Dillon 	}
446732a1697SMatthew Dillon 
447732a1697SMatthew Dillon 	/*
448b0aab9b9SMatthew Dillon 	 * Internal mount data structure
449732a1697SMatthew Dillon 	 */
450732a1697SMatthew Dillon 	if (hmp == NULL) {
451427e5fc6SMatthew Dillon 		hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
452427e5fc6SMatthew Dillon 		mp->mnt_data = (qaddr_t)hmp;
453427e5fc6SMatthew Dillon 		hmp->mp = mp;
45447197d71SMatthew Dillon 
455bac808feSMatthew Dillon 		/*
4567c457ac8SMatthew Dillon 		 * Make sure kmalloc type limits are set appropriately.
4577c457ac8SMatthew Dillon 		 *
4587c457ac8SMatthew Dillon 		 * Our inode kmalloc group is sized based on maxvnodes
4597c457ac8SMatthew Dillon 		 * (controlled by the system, not us).
460bac808feSMatthew Dillon 		 */
461bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_misc, "HAMMER-others");
462bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
463bac808feSMatthew Dillon 
4647c457ac8SMatthew Dillon 		kmalloc_raise_limit(hmp->m_inodes, 0);	/* unlimited */
465bac808feSMatthew Dillon 
466dd94f1b1SMatthew Dillon 		hmp->root_btree_beg.localization = 0x00000000U;
46747197d71SMatthew Dillon 		hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
46847197d71SMatthew Dillon 		hmp->root_btree_beg.key = -0x8000000000000000LL;
46947197d71SMatthew Dillon 		hmp->root_btree_beg.create_tid = 1;
47047197d71SMatthew Dillon 		hmp->root_btree_beg.delete_tid = 1;
47147197d71SMatthew Dillon 		hmp->root_btree_beg.rec_type = 0;
47247197d71SMatthew Dillon 		hmp->root_btree_beg.obj_type = 0;
473bfbd8331STomohiro Kusumi 		hmp->root_btree_beg.btype = HAMMER_BTREE_TYPE_NONE;
47447197d71SMatthew Dillon 
475dd94f1b1SMatthew Dillon 		hmp->root_btree_end.localization = 0xFFFFFFFFU;
47647197d71SMatthew Dillon 		hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
47747197d71SMatthew Dillon 		hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
47847197d71SMatthew Dillon 		hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
47947197d71SMatthew Dillon 		hmp->root_btree_end.delete_tid = 0;   /* special case */
48047197d71SMatthew Dillon 		hmp->root_btree_end.rec_type = 0xFFFFU;
48147197d71SMatthew Dillon 		hmp->root_btree_end.obj_type = 0;
482bfbd8331STomohiro Kusumi 		hmp->root_btree_end.btype = HAMMER_BTREE_TYPE_NONE;
483f03c9cf4SMatthew Dillon 
484cdb6e4e6SMatthew Dillon 		hmp->krate.freq = 1;	/* maximum reporting rate (hz) */
485cdb6e4e6SMatthew Dillon 		hmp->krate.count = -16;	/* initial burst */
486a8d31329SMatthew Dillon 		hmp->kdiag.freq = 1;	/* maximum reporting rate (hz) */
487a8d31329SMatthew Dillon 		hmp->kdiag.count = -16;	/* initial burst */
488cdb6e4e6SMatthew Dillon 
4899480ff55SMatthew Dillon 		hmp->sync_lock.refs = 1;
490c9b9e29dSMatthew Dillon 		hmp->free_lock.refs = 1;
491d99d6bf5SMatthew Dillon 		hmp->undo_lock.refs = 1;
492d99d6bf5SMatthew Dillon 		hmp->blkmap_lock.refs = 1;
49383f2a3aaSMatthew Dillon 		hmp->snapshot_lock.refs = 1;
49452e547e3SMichael Neumann 		hmp->volume_lock.refs = 1;
4959480ff55SMatthew Dillon 
496cebe9493SMatthew Dillon 		TAILQ_INIT(&hmp->delay_list);
4977a61b85dSMatthew Dillon 		TAILQ_INIT(&hmp->flush_group_list);
4980729c8c8SMatthew Dillon 		TAILQ_INIT(&hmp->objid_cache_list);
499e8599db1SMatthew Dillon 		TAILQ_INIT(&hmp->undo_lru_list);
5007bc5b8c2SMatthew Dillon 		TAILQ_INIT(&hmp->reclaim_list);
501507df98aSIlya Dryomov 
502507df98aSIlya Dryomov 		RB_INIT(&hmp->rb_dedup_crc_root);
503507df98aSIlya Dryomov 		RB_INIT(&hmp->rb_dedup_off_root);
504507df98aSIlya Dryomov 		TAILQ_INIT(&hmp->dedup_lru_list);
505195c19a1SMatthew Dillon 	}
50651c35492SMatthew Dillon 	hmp->hflags &= ~HMNT_USERFLAGS;
50751c35492SMatthew Dillon 	hmp->hflags |= info.hflags & HMNT_USERFLAGS;
508732a1697SMatthew Dillon 
509732a1697SMatthew Dillon 	hmp->master_id = master_id;
510732a1697SMatthew Dillon 
5117f7c1f84SMatthew Dillon 	if (info.asof) {
5127f7c1f84SMatthew Dillon 		mp->mnt_flag |= MNT_RDONLY;
5137f7c1f84SMatthew Dillon 		hmp->asof = info.asof;
5147f7c1f84SMatthew Dillon 	} else {
5157f7c1f84SMatthew Dillon 		hmp->asof = HAMMER_MAX_TID;
5167f7c1f84SMatthew Dillon 	}
517195c19a1SMatthew Dillon 
518865c9609SMichael Neumann 	hmp->volume_to_remove = -1;
519865c9609SMichael Neumann 
520195c19a1SMatthew Dillon 	/*
52151c35492SMatthew Dillon 	 * Re-open read-write if originally read-only, or vise-versa.
52202428fb6SMatthew Dillon 	 *
52302428fb6SMatthew Dillon 	 * When going from read-only to read-write execute the stage2
52402428fb6SMatthew Dillon 	 * recovery if it has not already been run.
525195c19a1SMatthew Dillon 	 */
526195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
527b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->fs_token);
52851c35492SMatthew Dillon 		error = 0;
52951c35492SMatthew Dillon 		if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
530c54975d5STomohiro Kusumi 			kprintf("HAMMER: read-only -> read-write\n");
531195c19a1SMatthew Dillon 			hmp->ronly = 0;
53251c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
53351c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
53451c35492SMatthew Dillon 			rootvol = hammer_get_root_volume(hmp, &error);
53551c35492SMatthew Dillon 			if (rootvol) {
53606ad81ffSMatthew Dillon 				hammer_recover_flush_buffers(hmp, rootvol, 1);
53702428fb6SMatthew Dillon 				error = hammer_recover_stage2(hmp, rootvol);
5389f5097dcSMatthew Dillon 				bcopy(rootvol->ondisk->vol0_blockmap,
5399f5097dcSMatthew Dillon 				      hmp->blockmap,
5409f5097dcSMatthew Dillon 				      sizeof(hmp->blockmap));
54151c35492SMatthew Dillon 				hammer_rel_volume(rootvol, 0);
542195c19a1SMatthew Dillon 			}
54351c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
54451c35492SMatthew Dillon 				hammer_reload_inode, NULL);
54551c35492SMatthew Dillon 			/* kernel clears MNT_RDONLY */
54651c35492SMatthew Dillon 		} else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
547c54975d5STomohiro Kusumi 			kprintf("HAMMER: read-write -> read-only\n");
54851c35492SMatthew Dillon 			hmp->ronly = 1;	/* messy */
54951c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
55051c35492SMatthew Dillon 				hammer_reload_inode, NULL);
55151c35492SMatthew Dillon 			hmp->ronly = 0;
55251c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
55351c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
55451c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
55551c35492SMatthew Dillon 			hmp->ronly = 1;
55651c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
55751c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
55851c35492SMatthew Dillon 		}
559b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
56051c35492SMatthew Dillon 		return(error);
561195c19a1SMatthew Dillon 	}
562195c19a1SMatthew Dillon 
563427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_vols_root);
564427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_inos_root);
56573896937SMatthew Dillon 	RB_INIT(&hmp->rb_redo_root);
56640043e7fSMatthew Dillon 	RB_INIT(&hmp->rb_nods_root);
567e8599db1SMatthew Dillon 	RB_INIT(&hmp->rb_undo_root);
5680832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_resv_root);
5690832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_bufs_root);
5705fa5c92fSMatthew Dillon 	RB_INIT(&hmp->rb_pfsm_root);
5710832c9bbSMatthew Dillon 
572195c19a1SMatthew Dillon 	hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
573427e5fc6SMatthew Dillon 
5741afb73cfSMatthew Dillon 	RB_INIT(&hmp->volu_root);
5751afb73cfSMatthew Dillon 	RB_INIT(&hmp->undo_root);
5761afb73cfSMatthew Dillon 	RB_INIT(&hmp->data_root);
5771afb73cfSMatthew Dillon 	RB_INIT(&hmp->meta_root);
5781afb73cfSMatthew Dillon 	RB_INIT(&hmp->lose_root);
579eddadaeeSMatthew Dillon 	TAILQ_INIT(&hmp->iorun_list);
58010a5d1baSMatthew Dillon 
581a3c18566SMatthew Dillon 	lwkt_token_init(&hmp->fs_token, "hammerfs");
582a3c18566SMatthew Dillon 	lwkt_token_init(&hmp->io_token, "hammerio");
583b0aab9b9SMatthew Dillon 
584b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
585b0aab9b9SMatthew Dillon 
586427e5fc6SMatthew Dillon 	/*
587427e5fc6SMatthew Dillon 	 * Load volumes
588427e5fc6SMatthew Dillon 	 */
589427e5fc6SMatthew Dillon 	path = objcache_get(namei_oc, M_WAITOK);
5901b0ab2c3SMatthew Dillon 	hmp->nvolumes = -1;
591427e5fc6SMatthew Dillon 	for (i = 0; i < info.nvolumes; ++i) {
5927c19b529SMichael Neumann 		if (mntpt == NULL) {
5937c19b529SMichael Neumann 			/*
5947c19b529SMichael Neumann 			 * Root mount.
5957c19b529SMichael Neumann 			 */
596104cb849SMichael Neumann 			KKASSERT(next_volume_ptr != NULL);
597a407819fSMichael Neumann 			strcpy(path, "");
598a407819fSMichael Neumann 			if (*next_volume_ptr != '/') {
599a407819fSMichael Neumann 				/* relative path */
600104cb849SMichael Neumann 				strcpy(path, "/dev/");
601a407819fSMichael Neumann 			}
602104cb849SMichael Neumann 			int k;
603104cb849SMichael Neumann 			for (k = strlen(path); k < MAXPATHLEN-1; ++k) {
604104cb849SMichael Neumann 				if (*next_volume_ptr == '\0') {
605104cb849SMichael Neumann 					break;
606104cb849SMichael Neumann 				} else if (*next_volume_ptr == ':') {
607104cb849SMichael Neumann 					++next_volume_ptr;
608104cb849SMichael Neumann 					break;
609104cb849SMichael Neumann 				} else {
610104cb849SMichael Neumann 					path[k] = *next_volume_ptr;
611104cb849SMichael Neumann 					++next_volume_ptr;
612104cb849SMichael Neumann 				}
613104cb849SMichael Neumann 			}
614104cb849SMichael Neumann 			path[k] = '\0';
615104cb849SMichael Neumann 
6167c19b529SMichael Neumann 			error = 0;
617104cb849SMichael Neumann 			cdev_t dev = kgetdiskbyname(path);
618104cb849SMichael Neumann 			error = bdevvp(dev, &devvp);
619104cb849SMichael Neumann 			if (error) {
620542b88c2STomohiro Kusumi 				kprintf("hammer_mount: can't find devvp\n");
621104cb849SMichael Neumann 			}
6227c19b529SMichael Neumann 		} else {
6237c19b529SMichael Neumann 			error = copyin(&info.volumes[i], &upath,
6247c19b529SMichael Neumann 				       sizeof(char *));
625427e5fc6SMatthew Dillon 			if (error == 0)
6267c19b529SMichael Neumann 				error = copyinstr(upath, path,
6277c19b529SMichael Neumann 						  MAXPATHLEN, NULL);
6287c19b529SMichael Neumann 		}
629427e5fc6SMatthew Dillon 		if (error == 0)
630deabdbfbSTomohiro Kusumi 			error = hammer_install_volume(hmp, path, devvp, NULL);
631427e5fc6SMatthew Dillon 		if (error)
632427e5fc6SMatthew Dillon 			break;
633427e5fc6SMatthew Dillon 	}
634427e5fc6SMatthew Dillon 	objcache_put(namei_oc, path);
635427e5fc6SMatthew Dillon 
636427e5fc6SMatthew Dillon 	/*
637427e5fc6SMatthew Dillon 	 * Make sure we found a root volume
638427e5fc6SMatthew Dillon 	 */
639721f3110STomohiro Kusumi 	if (hmp->rootvol == NULL) {
640427e5fc6SMatthew Dillon 		kprintf("hammer_mount: No root volume found!\n");
641427e5fc6SMatthew Dillon 		error = EINVAL;
642721f3110STomohiro Kusumi 		goto failed;
643427e5fc6SMatthew Dillon 	}
6441b0ab2c3SMatthew Dillon 
6451b0ab2c3SMatthew Dillon 	/*
6461b0ab2c3SMatthew Dillon 	 * Check that all required volumes are available
6471b0ab2c3SMatthew Dillon 	 */
6481b0ab2c3SMatthew Dillon 	if (error == 0 && hammer_mountcheck_volumes(hmp)) {
6491b0ab2c3SMatthew Dillon 		kprintf("hammer_mount: Missing volumes, cannot mount!\n");
6501b0ab2c3SMatthew Dillon 		error = EINVAL;
651721f3110STomohiro Kusumi 		goto failed;
6521b0ab2c3SMatthew Dillon 	}
6531b0ab2c3SMatthew Dillon 
654721f3110STomohiro Kusumi 	/*
655721f3110STomohiro Kusumi 	 * Other errors
656721f3110STomohiro Kusumi 	 */
657427e5fc6SMatthew Dillon 	if (error) {
658721f3110STomohiro Kusumi 		kprintf("hammer_mount: Failed to load volumes!\n");
659721f3110STomohiro Kusumi 		goto failed;
660427e5fc6SMatthew Dillon 	}
661427e5fc6SMatthew Dillon 
662c302e844STomohiro Kusumi 	nvolumes = hammer_get_installed_volumes(hmp);
663c302e844STomohiro Kusumi 	if (hmp->nvolumes != nvolumes) {
664c302e844STomohiro Kusumi 		kprintf("hammer_mount: volume header says %d volumes, "
665c302e844STomohiro Kusumi 			"but %d installed\n",
666c302e844STomohiro Kusumi 			hmp->nvolumes, nvolumes);
667c302e844STomohiro Kusumi 		error = EINVAL;
668c302e844STomohiro Kusumi 		goto failed;
669c302e844STomohiro Kusumi 	}
670c302e844STomohiro Kusumi 
671427e5fc6SMatthew Dillon 	/*
67227ea2398SMatthew Dillon 	 * No errors, setup enough of the mount point so we can lookup the
67327ea2398SMatthew Dillon 	 * root vnode.
674427e5fc6SMatthew Dillon 	 */
675427e5fc6SMatthew Dillon 	mp->mnt_iosize_max = MAXPHYS;
676427e5fc6SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_FSMID;
677cf6a53caSMatthew Dillon 	mp->mnt_kern_flag |= MNTK_THR_SYNC;	/* new vsyncscan semantics */
678c0ade690SMatthew Dillon 
679c0ade690SMatthew Dillon 	/*
680aac0aabdSMatthew Dillon 	 * MPSAFE code.  Note that VOPs and VFSops which are not MPSAFE
681aac0aabdSMatthew Dillon 	 * will acquire a per-mount token prior to entry and release it
6826a85c61bSTomohiro Kusumi 	 * on return.
683aac0aabdSMatthew Dillon 	 */
684b0aab9b9SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
685aac0aabdSMatthew Dillon 
686aac0aabdSMatthew Dillon 	/*
687c0ade690SMatthew Dillon 	 * note: f_iosize is used by vnode_pager_haspage() when constructing
688c0ade690SMatthew Dillon 	 * its VOP_BMAP call.
689c0ade690SMatthew Dillon 	 */
690c0ade690SMatthew Dillon 	mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
691fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
6926f97fce3SMatthew Dillon 
6936f97fce3SMatthew Dillon 	mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
6946f97fce3SMatthew Dillon 	mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
6956f97fce3SMatthew Dillon 
696427e5fc6SMatthew Dillon 	mp->mnt_maxsymlinklen = 255;
697427e5fc6SMatthew Dillon 	mp->mnt_flag |= MNT_LOCAL;
698427e5fc6SMatthew Dillon 
699427e5fc6SMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
7007a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
7017a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
70227ea2398SMatthew Dillon 
70327ea2398SMatthew Dillon 	/*
704a89aec1bSMatthew Dillon 	 * The root volume's ondisk pointer is only valid if we hold a
705a89aec1bSMatthew Dillon 	 * reference to it.
706a89aec1bSMatthew Dillon 	 */
707a89aec1bSMatthew Dillon 	rootvol = hammer_get_root_volume(hmp, &error);
708a89aec1bSMatthew Dillon 	if (error)
709f90dde4cSMatthew Dillon 		goto failed;
710f90dde4cSMatthew Dillon 
711f90dde4cSMatthew Dillon 	/*
7129f5097dcSMatthew Dillon 	 * Perform any necessary UNDO operations.  The recovery code does
7130729c8c8SMatthew Dillon 	 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
7140729c8c8SMatthew Dillon 	 * and then re-copy it again after recovery is complete.
715c9b9e29dSMatthew Dillon 	 *
71651c35492SMatthew Dillon 	 * If this is a read-only mount the UNDO information is retained
71751c35492SMatthew Dillon 	 * in memory in the form of dirty buffer cache buffers, and not
71851c35492SMatthew Dillon 	 * written back to the media.
719f90dde4cSMatthew Dillon 	 */
7200729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
7210729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
722c9b9e29dSMatthew Dillon 
7237a61b85dSMatthew Dillon 	/*
72444a83111SMatthew Dillon 	 * Check filesystem version
72544a83111SMatthew Dillon 	 */
72644a83111SMatthew Dillon 	hmp->version = rootvol->ondisk->vol_version;
72744a83111SMatthew Dillon 	if (hmp->version < HAMMER_VOL_VERSION_MIN ||
72844a83111SMatthew Dillon 	    hmp->version > HAMMER_VOL_VERSION_MAX) {
72944a83111SMatthew Dillon 		kprintf("HAMMER: mount unsupported fs version %d\n",
73044a83111SMatthew Dillon 			hmp->version);
73144a83111SMatthew Dillon 		error = ERANGE;
73244a83111SMatthew Dillon 		goto done;
73344a83111SMatthew Dillon 	}
73444a83111SMatthew Dillon 
73544a83111SMatthew Dillon 	/*
7367a61b85dSMatthew Dillon 	 * The undo_rec_limit limits the size of flush groups to avoid
7377a61b85dSMatthew Dillon 	 * blowing out the UNDO FIFO.  This calculation is typically in
7387a61b85dSMatthew Dillon 	 * the tens of thousands and is designed primarily when small
7397a61b85dSMatthew Dillon 	 * HAMMER filesystems are created.
7407a61b85dSMatthew Dillon 	 */
7417a61b85dSMatthew Dillon 	hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
7427a61b85dSMatthew Dillon 	if (hammer_debug_general & 0x0001)
7437a61b85dSMatthew Dillon 		kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit);
7447a61b85dSMatthew Dillon 
74502428fb6SMatthew Dillon 	/*
74602428fb6SMatthew Dillon 	 * NOTE: Recover stage1 not only handles meta-data recovery, it
74702428fb6SMatthew Dillon 	 * 	 also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems.
74802428fb6SMatthew Dillon 	 */
74902428fb6SMatthew Dillon 	error = hammer_recover_stage1(hmp, rootvol);
750f90dde4cSMatthew Dillon 	if (error) {
751f90dde4cSMatthew Dillon 		kprintf("Failed to recover HAMMER filesystem on mount\n");
752a89aec1bSMatthew Dillon 		goto done;
753f90dde4cSMatthew Dillon 	}
754f90dde4cSMatthew Dillon 
755f90dde4cSMatthew Dillon 	/*
756adf01747SMatthew Dillon 	 * Finish setup now that we have a good root volume.
757adf01747SMatthew Dillon 	 *
758adf01747SMatthew Dillon 	 * The top 16 bits of fsid.val[1] is a pfs id.
759f90dde4cSMatthew Dillon 	 */
760a89aec1bSMatthew Dillon 	ksnprintf(mp->mnt_stat.f_mntfromname,
761a89aec1bSMatthew Dillon 		  sizeof(mp->mnt_stat.f_mntfromname), "%s",
762a89aec1bSMatthew Dillon 		  rootvol->ondisk->vol_name);
763513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[0] =
764513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
765513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] =
766513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
767adf01747SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF;
768b84de5afSMatthew Dillon 
7696f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
7706f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
7716f97fce3SMatthew Dillon 				     sizeof(mp->mnt_vstat.f_fsid_uuid));
7726f97fce3SMatthew Dillon 
7730729c8c8SMatthew Dillon 	/*
7740729c8c8SMatthew Dillon 	 * Certain often-modified fields in the root volume are cached in
7750729c8c8SMatthew Dillon 	 * the hammer_mount structure so we do not have to generate lots
7760729c8c8SMatthew Dillon 	 * of little UNDO structures for them.
777c9b9e29dSMatthew Dillon 	 *
7789f5097dcSMatthew Dillon 	 * Recopy after recovery.  This also has the side effect of
7799f5097dcSMatthew Dillon 	 * setting our cached undo FIFO's first_offset, which serves to
7809f5097dcSMatthew Dillon 	 * placemark the FIFO start for the NEXT flush cycle while the
7819f5097dcSMatthew Dillon 	 * on-disk first_offset represents the LAST flush cycle.
7820729c8c8SMatthew Dillon 	 */
783b84de5afSMatthew Dillon 	hmp->next_tid = rootvol->ondisk->vol0_next_tid;
7844889cbd4SMatthew Dillon 	hmp->flush_tid1 = hmp->next_tid;
7854889cbd4SMatthew Dillon 	hmp->flush_tid2 = hmp->next_tid;
7860729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
7870729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
788e63644f0SMatthew Dillon 	hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
7890729c8c8SMatthew Dillon 
790059819e3SMatthew Dillon 	hammer_flusher_create(hmp);
791059819e3SMatthew Dillon 
792a89aec1bSMatthew Dillon 	/*
793*1bcc9299STomohiro Kusumi 	 * Locate the root directory with an obj_id of 1.
79427ea2398SMatthew Dillon 	 */
795783fb170STomohiro Kusumi 	error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, &rootvp);
796a89aec1bSMatthew Dillon 	if (error)
797a89aec1bSMatthew Dillon 		goto done;
79827ea2398SMatthew Dillon 	vput(rootvp);
79988c39f64SThomas Nikolajsen 	if (hmp->ronly == 0)
80002428fb6SMatthew Dillon 		error = hammer_recover_stage2(hmp, rootvol);
80127ea2398SMatthew Dillon 
8029fa352a2SMatthew Dillon 	/*
8039fa352a2SMatthew Dillon 	 * If the stage2 recovery fails be sure to clean out all cached
8049fa352a2SMatthew Dillon 	 * vnodes before throwing away the mount structure or bad things
8059fa352a2SMatthew Dillon 	 * will happen.
8069fa352a2SMatthew Dillon 	 */
8079fa352a2SMatthew Dillon 	if (error)
8089fa352a2SMatthew Dillon 		vflush(mp, 0, 0);
8099fa352a2SMatthew Dillon 
810a89aec1bSMatthew Dillon done:
81198ad7adeSFrancois Tigeot 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
81298ad7adeSFrancois Tigeot 		/* New mount */
81398ad7adeSFrancois Tigeot 
81498ad7adeSFrancois Tigeot 		/* Populate info for mount point (NULL pad)*/
81598ad7adeSFrancois Tigeot 		bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
81698ad7adeSFrancois Tigeot 		size_t size;
81798ad7adeSFrancois Tigeot 		if (mntpt) {
81898ad7adeSFrancois Tigeot 			copyinstr(mntpt, mp->mnt_stat.f_mntonname,
81998ad7adeSFrancois Tigeot 							MNAMELEN -1, &size);
82098ad7adeSFrancois Tigeot 		} else { /* Root mount */
82198ad7adeSFrancois Tigeot 			mp->mnt_stat.f_mntonname[0] = '/';
82298ad7adeSFrancois Tigeot 		}
82398ad7adeSFrancois Tigeot 	}
82498ad7adeSFrancois Tigeot 	(void)VFS_STATFS(mp, &mp->mnt_stat, cred);
825f90dde4cSMatthew Dillon 	hammer_rel_volume(rootvol, 0);
826f90dde4cSMatthew Dillon failed:
82727ea2398SMatthew Dillon 	/*
82827ea2398SMatthew Dillon 	 * Cleanup and return.
82927ea2398SMatthew Dillon 	 */
830b0aab9b9SMatthew Dillon 	if (error) {
831b0aab9b9SMatthew Dillon 		/* called with fs_token held */
83227ea2398SMatthew Dillon 		hammer_free_hmp(mp);
833b0aab9b9SMatthew Dillon 	} else {
834b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
835b0aab9b9SMatthew Dillon 	}
836427e5fc6SMatthew Dillon 	return (error);
837427e5fc6SMatthew Dillon }
838427e5fc6SMatthew Dillon 
839427e5fc6SMatthew Dillon static int
840427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags)
841427e5fc6SMatthew Dillon {
842b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
843427e5fc6SMatthew Dillon 	int flags;
84466325755SMatthew Dillon 	int error;
84527ea2398SMatthew Dillon 
84627ea2398SMatthew Dillon 	/*
847427e5fc6SMatthew Dillon 	 * Clean out the vnodes
848427e5fc6SMatthew Dillon 	 */
849b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
85066325755SMatthew Dillon 	flags = 0;
85166325755SMatthew Dillon 	if (mntflags & MNT_FORCE)
85266325755SMatthew Dillon 		flags |= FORCECLOSE;
853b0aab9b9SMatthew Dillon 	error = vflush(mp, 0, flags);
854427e5fc6SMatthew Dillon 
855427e5fc6SMatthew Dillon 	/*
856427e5fc6SMatthew Dillon 	 * Clean up the internal mount structure and related entities.  This
857427e5fc6SMatthew Dillon 	 * may issue I/O.
858427e5fc6SMatthew Dillon 	 */
859b0aab9b9SMatthew Dillon 	if (error == 0) {
860b0aab9b9SMatthew Dillon 		/* called with fs_token held */
861427e5fc6SMatthew Dillon 		hammer_free_hmp(mp);
862b0aab9b9SMatthew Dillon 	} else {
863b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
864b0aab9b9SMatthew Dillon 	}
865b0aab9b9SMatthew Dillon 	return(error);
866427e5fc6SMatthew Dillon }
867427e5fc6SMatthew Dillon 
868427e5fc6SMatthew Dillon /*
869427e5fc6SMatthew Dillon  * Clean up the internal mount structure and disassociate it from the mount.
870427e5fc6SMatthew Dillon  * This may issue I/O.
871b0aab9b9SMatthew Dillon  *
872b0aab9b9SMatthew Dillon  * Called with fs_token held.
873427e5fc6SMatthew Dillon  */
874427e5fc6SMatthew Dillon static void
875427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp)
876427e5fc6SMatthew Dillon {
877b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
878cdb6e4e6SMatthew Dillon 	hammer_flush_group_t flg;
879427e5fc6SMatthew Dillon 
880427e5fc6SMatthew Dillon 	/*
881cdb6e4e6SMatthew Dillon 	 * Flush anything dirty.  This won't even run if the
882cdb6e4e6SMatthew Dillon 	 * filesystem errored-out.
883427e5fc6SMatthew Dillon 	 */
8848bae937eSTomohiro Kusumi 	hammer_flush_dirty(hmp, 30);
885cdb6e4e6SMatthew Dillon 
886cdb6e4e6SMatthew Dillon 	/*
887cdb6e4e6SMatthew Dillon 	 * If the mount had a critical error we have to destroy any
888cdb6e4e6SMatthew Dillon 	 * remaining inodes before we can finish cleaning up the flusher.
889cdb6e4e6SMatthew Dillon 	 */
890cdb6e4e6SMatthew Dillon 	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
891cdb6e4e6SMatthew Dillon 		RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
892cdb6e4e6SMatthew Dillon 			hammer_destroy_inode_callback, NULL);
893cdb6e4e6SMatthew Dillon 	}
894cdb6e4e6SMatthew Dillon 
895cdb6e4e6SMatthew Dillon 	/*
896cdb6e4e6SMatthew Dillon 	 * There shouldn't be any inodes left now and any left over
897cdb6e4e6SMatthew Dillon 	 * flush groups should now be empty.
898cdb6e4e6SMatthew Dillon 	 */
899cdb6e4e6SMatthew Dillon 	KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
900cdb6e4e6SMatthew Dillon 	while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
901cdb6e4e6SMatthew Dillon 		TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
902ff003b11SMatthew Dillon 		KKASSERT(RB_EMPTY(&flg->flush_tree));
903cdb6e4e6SMatthew Dillon 		if (flg->refs) {
904cdb6e4e6SMatthew Dillon 			kprintf("HAMMER: Warning, flush_group %p was "
905cdb6e4e6SMatthew Dillon 				"not empty on umount!\n", flg);
906cdb6e4e6SMatthew Dillon 		}
907bac808feSMatthew Dillon 		kfree(flg, hmp->m_misc);
908cdb6e4e6SMatthew Dillon 	}
909cdb6e4e6SMatthew Dillon 
910cdb6e4e6SMatthew Dillon 	/*
911cdb6e4e6SMatthew Dillon 	 * We can finally destroy the flusher
912cdb6e4e6SMatthew Dillon 	 */
913059819e3SMatthew Dillon 	hammer_flusher_destroy(hmp);
914427e5fc6SMatthew Dillon 
915b84de5afSMatthew Dillon 	/*
91600f16fadSMatthew Dillon 	 * We may have held recovered buffers due to a read-only mount.
91700f16fadSMatthew Dillon 	 * These must be discarded.
91800f16fadSMatthew Dillon 	 */
91900f16fadSMatthew Dillon 	if (hmp->ronly)
92000f16fadSMatthew Dillon 		hammer_recover_flush_buffers(hmp, NULL, -1);
92100f16fadSMatthew Dillon 
92200f16fadSMatthew Dillon 	/*
9230832c9bbSMatthew Dillon 	 * Unload buffers and then volumes
924b84de5afSMatthew Dillon 	 */
9250832c9bbSMatthew Dillon         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
9260832c9bbSMatthew Dillon 		hammer_unload_buffer, NULL);
927427e5fc6SMatthew Dillon 	RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
928427e5fc6SMatthew Dillon 		hammer_unload_volume, NULL);
929427e5fc6SMatthew Dillon 
930427e5fc6SMatthew Dillon 	mp->mnt_data = NULL;
93166325755SMatthew Dillon 	mp->mnt_flag &= ~MNT_LOCAL;
932427e5fc6SMatthew Dillon 	hmp->mp = NULL;
9330729c8c8SMatthew Dillon 	hammer_destroy_objid_cache(hmp);
934507df98aSIlya Dryomov 	hammer_destroy_dedup_cache(hmp);
935507df98aSIlya Dryomov 	if (hmp->dedup_free_cache != NULL) {
936507df98aSIlya Dryomov 		kfree(hmp->dedup_free_cache, hmp->m_misc);
937507df98aSIlya Dryomov 		hmp->dedup_free_cache = NULL;
938507df98aSIlya Dryomov 	}
939bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_misc);
940bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_inodes);
941b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
942427e5fc6SMatthew Dillon 	kfree(hmp, M_HAMMER);
943427e5fc6SMatthew Dillon }
944427e5fc6SMatthew Dillon 
945427e5fc6SMatthew Dillon /*
946cdb6e4e6SMatthew Dillon  * Report critical errors.  ip may be NULL.
947cdb6e4e6SMatthew Dillon  */
948cdb6e4e6SMatthew Dillon void
949cdb6e4e6SMatthew Dillon hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
950cdb6e4e6SMatthew Dillon 		      int error, const char *msg)
951cdb6e4e6SMatthew Dillon {
952cdb6e4e6SMatthew Dillon 	hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
953c9ce54d6SMatthew Dillon 
954cdb6e4e6SMatthew Dillon 	krateprintf(&hmp->krate,
955c9ce54d6SMatthew Dillon 		    "HAMMER(%s): Critical error inode=%jd error=%d %s\n",
956cdb6e4e6SMatthew Dillon 		    hmp->mp->mnt_stat.f_mntfromname,
957c9ce54d6SMatthew Dillon 		    (intmax_t)(ip ? ip->obj_id : -1),
958c9ce54d6SMatthew Dillon 		    error, msg);
959c9ce54d6SMatthew Dillon 
960cdb6e4e6SMatthew Dillon 	if (hmp->ronly == 0) {
961cdb6e4e6SMatthew Dillon 		hmp->ronly = 2;		/* special errored read-only mode */
962cdb6e4e6SMatthew Dillon 		hmp->mp->mnt_flag |= MNT_RDONLY;
963dfec479fSMatthew Dillon 		RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
964dfec479fSMatthew Dillon 			hammer_adjust_volume_mode, NULL);
965cdb6e4e6SMatthew Dillon 		kprintf("HAMMER(%s): Forcing read-only mode\n",
966cdb6e4e6SMatthew Dillon 			hmp->mp->mnt_stat.f_mntfromname);
967cdb6e4e6SMatthew Dillon 	}
968cdb6e4e6SMatthew Dillon 	hmp->error = error;
969fc73edd8SMatthew Dillon 	if (hammer_debug_critical)
970c9ce54d6SMatthew Dillon 		Debugger("Entering debugger");
971cdb6e4e6SMatthew Dillon }
972cdb6e4e6SMatthew Dillon 
973cdb6e4e6SMatthew Dillon 
974cdb6e4e6SMatthew Dillon /*
975513ca7d7SMatthew Dillon  * Obtain a vnode for the specified inode number.  An exclusively locked
976513ca7d7SMatthew Dillon  * vnode is returned.
977513ca7d7SMatthew Dillon  */
978513ca7d7SMatthew Dillon int
979b9b0a6d0SMatthew Dillon hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
980b9b0a6d0SMatthew Dillon 		ino_t ino, struct vnode **vpp)
981513ca7d7SMatthew Dillon {
98236f82b23SMatthew Dillon 	struct hammer_transaction trans;
983513ca7d7SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
984513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
985513ca7d7SMatthew Dillon 	int error;
986b9b0a6d0SMatthew Dillon 	u_int32_t localization;
987513ca7d7SMatthew Dillon 
988b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
98936f82b23SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
99036f82b23SMatthew Dillon 
991513ca7d7SMatthew Dillon 	/*
992b9b0a6d0SMatthew Dillon 	 * If a directory vnode is supplied (mainly NFS) then we can acquire
993b9b0a6d0SMatthew Dillon 	 * the PFS domain from it.  Otherwise we would only be able to vget
994b9b0a6d0SMatthew Dillon 	 * inodes in the root PFS.
995b9b0a6d0SMatthew Dillon 	 */
996b9b0a6d0SMatthew Dillon 	if (dvp) {
997b9b0a6d0SMatthew Dillon 		localization = HAMMER_DEF_LOCALIZATION +
998b9b0a6d0SMatthew Dillon 				VTOI(dvp)->obj_localization;
999b9b0a6d0SMatthew Dillon 	} else {
1000b9b0a6d0SMatthew Dillon 		localization = HAMMER_DEF_LOCALIZATION;
1001b9b0a6d0SMatthew Dillon 	}
1002b9b0a6d0SMatthew Dillon 
1003b9b0a6d0SMatthew Dillon 	/*
100436f82b23SMatthew Dillon 	 * Lookup the requested HAMMER inode.  The structure must be
100536f82b23SMatthew Dillon 	 * left unlocked while we manipulate the related vnode to avoid
100636f82b23SMatthew Dillon 	 * a deadlock.
1007513ca7d7SMatthew Dillon 	 */
1008ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, ino,
1009b9b0a6d0SMatthew Dillon 			      hmp->asof, localization,
1010ddfdf542SMatthew Dillon 			      0, &error);
1011513ca7d7SMatthew Dillon 	if (ip == NULL) {
1012513ca7d7SMatthew Dillon 		*vpp = NULL;
1013b0aab9b9SMatthew Dillon 	} else {
1014e8599db1SMatthew Dillon 		error = hammer_get_vnode(ip, vpp);
1015513ca7d7SMatthew Dillon 		hammer_rel_inode(ip, 0);
1016b0aab9b9SMatthew Dillon 	}
1017b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
1018b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1019513ca7d7SMatthew Dillon 	return (error);
1020513ca7d7SMatthew Dillon }
1021513ca7d7SMatthew Dillon 
1022513ca7d7SMatthew Dillon /*
1023427e5fc6SMatthew Dillon  * Return the root vnode for the filesystem.
1024427e5fc6SMatthew Dillon  *
1025427e5fc6SMatthew Dillon  * HAMMER stores the root vnode in the hammer_mount structure so
1026427e5fc6SMatthew Dillon  * getting it is easy.
1027427e5fc6SMatthew Dillon  */
1028427e5fc6SMatthew Dillon static int
1029427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp)
1030427e5fc6SMatthew Dillon {
103127ea2398SMatthew Dillon 	int error;
1032427e5fc6SMatthew Dillon 
1033783fb170STomohiro Kusumi 	error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, vpp);
103427ea2398SMatthew Dillon 	return (error);
1035427e5fc6SMatthew Dillon }
1036427e5fc6SMatthew Dillon 
1037427e5fc6SMatthew Dillon static int
1038427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1039427e5fc6SMatthew Dillon {
1040fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
1041fbc6e32aSMatthew Dillon 	hammer_volume_t volume;
1042fbc6e32aSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
1043fbc6e32aSMatthew Dillon 	int error;
104447197d71SMatthew Dillon 	int64_t bfree;
104531a56ce2SMatthew Dillon 	int64_t breserved;
1046fbc6e32aSMatthew Dillon 
1047b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1048fbc6e32aSMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
1049b0aab9b9SMatthew Dillon 	if (error) {
1050b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
1051fbc6e32aSMatthew Dillon 		return(error);
1052b0aab9b9SMatthew Dillon 	}
1053fbc6e32aSMatthew Dillon 	ondisk = volume->ondisk;
1054fbc6e32aSMatthew Dillon 
105547197d71SMatthew Dillon 	/*
105647197d71SMatthew Dillon 	 * Basic stats
105747197d71SMatthew Dillon 	 */
105831a56ce2SMatthew Dillon 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
1059fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
1060e04ee2deSTomohiro Kusumi 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
106140043e7fSMatthew Dillon 	hammer_rel_volume(volume, 0);
106247197d71SMatthew Dillon 
106331a56ce2SMatthew Dillon 	mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
106447197d71SMatthew Dillon 	mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1065fbc6e32aSMatthew Dillon 	if (mp->mnt_stat.f_files < 0)
1066fbc6e32aSMatthew Dillon 		mp->mnt_stat.f_files = 0;
1067fbc6e32aSMatthew Dillon 
106827ea2398SMatthew Dillon 	*sbp = mp->mnt_stat;
1069b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
107027ea2398SMatthew Dillon 	return(0);
1071427e5fc6SMatthew Dillon }
1072427e5fc6SMatthew Dillon 
10736f97fce3SMatthew Dillon static int
10746f97fce3SMatthew Dillon hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
10756f97fce3SMatthew Dillon {
10766f97fce3SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
10776f97fce3SMatthew Dillon 	hammer_volume_t volume;
10786f97fce3SMatthew Dillon 	hammer_volume_ondisk_t ondisk;
10796f97fce3SMatthew Dillon 	int error;
10806f97fce3SMatthew Dillon 	int64_t bfree;
10810f65be10SMatthew Dillon 	int64_t breserved;
10826f97fce3SMatthew Dillon 
1083b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
10846f97fce3SMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
1085b0aab9b9SMatthew Dillon 	if (error) {
1086b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
10876f97fce3SMatthew Dillon 		return(error);
1088b0aab9b9SMatthew Dillon 	}
10896f97fce3SMatthew Dillon 	ondisk = volume->ondisk;
10906f97fce3SMatthew Dillon 
10916f97fce3SMatthew Dillon 	/*
10926f97fce3SMatthew Dillon 	 * Basic stats
10936f97fce3SMatthew Dillon 	 */
10940f65be10SMatthew Dillon 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
10956f97fce3SMatthew Dillon 	mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
1096e04ee2deSTomohiro Kusumi 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
10976f97fce3SMatthew Dillon 	hammer_rel_volume(volume, 0);
10986f97fce3SMatthew Dillon 
10990f65be10SMatthew Dillon 	mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
1100c0763659SMatthew Dillon 	mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
11016f97fce3SMatthew Dillon 	if (mp->mnt_vstat.f_files < 0)
11026f97fce3SMatthew Dillon 		mp->mnt_vstat.f_files = 0;
11036f97fce3SMatthew Dillon 	*sbp = mp->mnt_vstat;
1104b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
11056f97fce3SMatthew Dillon 	return(0);
11066f97fce3SMatthew Dillon }
11076f97fce3SMatthew Dillon 
11080729c8c8SMatthew Dillon /*
11090729c8c8SMatthew Dillon  * Sync the filesystem.  Currently we have to run it twice, the second
11100729c8c8SMatthew Dillon  * one will advance the undo start index to the end index, so if a crash
11110729c8c8SMatthew Dillon  * occurs no undos will be run on mount.
111277062c8aSMatthew Dillon  *
111377062c8aSMatthew Dillon  * We do not sync the filesystem if we are called from a panic.  If we did
111477062c8aSMatthew Dillon  * we might end up blowing up a sync that was already in progress.
11150729c8c8SMatthew Dillon  */
1116427e5fc6SMatthew Dillon static int
1117427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor)
1118427e5fc6SMatthew Dillon {
1119fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
11200729c8c8SMatthew Dillon 	int error;
11210729c8c8SMatthew Dillon 
1122b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
112377062c8aSMatthew Dillon 	if (panicstr == NULL) {
11240729c8c8SMatthew Dillon 		error = hammer_sync_hmp(hmp, waitfor);
112577062c8aSMatthew Dillon 	} else {
112677062c8aSMatthew Dillon 		error = EIO;
112777062c8aSMatthew Dillon 	}
1128b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
11290729c8c8SMatthew Dillon 	return (error);
1130427e5fc6SMatthew Dillon }
1131427e5fc6SMatthew Dillon 
1132513ca7d7SMatthew Dillon /*
1133513ca7d7SMatthew Dillon  * Convert a vnode to a file handle.
1134b0aab9b9SMatthew Dillon  *
1135b0aab9b9SMatthew Dillon  * Accesses read-only fields on already-referenced structures so
1136b0aab9b9SMatthew Dillon  * no token is needed.
1137513ca7d7SMatthew Dillon  */
1138513ca7d7SMatthew Dillon static int
1139513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1140513ca7d7SMatthew Dillon {
1141513ca7d7SMatthew Dillon 	hammer_inode_t ip;
1142513ca7d7SMatthew Dillon 
1143513ca7d7SMatthew Dillon 	KKASSERT(MAXFIDSZ >= 16);
1144513ca7d7SMatthew Dillon 	ip = VTOI(vp);
1145513ca7d7SMatthew Dillon 	fhp->fid_len = offsetof(struct fid, fid_data[16]);
1146adf01747SMatthew Dillon 	fhp->fid_ext = ip->obj_localization >> 16;
1147513ca7d7SMatthew Dillon 	bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
1148513ca7d7SMatthew Dillon 	bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
1149513ca7d7SMatthew Dillon 	return(0);
1150513ca7d7SMatthew Dillon }
1151513ca7d7SMatthew Dillon 
1152513ca7d7SMatthew Dillon 
1153513ca7d7SMatthew Dillon /*
1154513ca7d7SMatthew Dillon  * Convert a file handle back to a vnode.
115567863d04SMatthew Dillon  *
115667863d04SMatthew Dillon  * Use rootvp to enforce PFS isolation when a PFS is exported via a
115767863d04SMatthew Dillon  * null mount.
1158513ca7d7SMatthew Dillon  */
1159513ca7d7SMatthew Dillon static int
116067863d04SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
116167863d04SMatthew Dillon 		  struct fid *fhp, struct vnode **vpp)
1162513ca7d7SMatthew Dillon {
1163b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
116436f82b23SMatthew Dillon 	struct hammer_transaction trans;
1165513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
1166513ca7d7SMatthew Dillon 	struct hammer_inode_info info;
1167513ca7d7SMatthew Dillon 	int error;
1168adf01747SMatthew Dillon 	u_int32_t localization;
1169513ca7d7SMatthew Dillon 
1170513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
1171513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
117267863d04SMatthew Dillon 	if (rootvp)
117367863d04SMatthew Dillon 		localization = VTOI(rootvp)->obj_localization;
117467863d04SMatthew Dillon 	else
1175adf01747SMatthew Dillon 		localization = (u_int32_t)fhp->fid_ext << 16;
1176513ca7d7SMatthew Dillon 
1177b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1178b0aab9b9SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
117936f82b23SMatthew Dillon 
1180513ca7d7SMatthew Dillon 	/*
1181513ca7d7SMatthew Dillon 	 * Get/allocate the hammer_inode structure.  The structure must be
1182513ca7d7SMatthew Dillon 	 * unlocked while we manipulate the related vnode to avoid a
1183513ca7d7SMatthew Dillon 	 * deadlock.
1184513ca7d7SMatthew Dillon 	 */
1185ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, info.obj_id,
1186adf01747SMatthew Dillon 			      info.obj_asof, localization, 0, &error);
1187d978e7cfSMatthew Dillon 	if (ip) {
1188e8599db1SMatthew Dillon 		error = hammer_get_vnode(ip, vpp);
1189513ca7d7SMatthew Dillon 		hammer_rel_inode(ip, 0);
1190d978e7cfSMatthew Dillon 	} else {
1191d978e7cfSMatthew Dillon 		*vpp = NULL;
1192d978e7cfSMatthew Dillon 	}
1193b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
1194b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1195513ca7d7SMatthew Dillon 	return (error);
1196513ca7d7SMatthew Dillon }
1197513ca7d7SMatthew Dillon 
1198513ca7d7SMatthew Dillon static int
1199513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1200513ca7d7SMatthew Dillon 		    int *exflagsp, struct ucred **credanonp)
1201513ca7d7SMatthew Dillon {
1202513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
1203513ca7d7SMatthew Dillon 	struct netcred *np;
1204513ca7d7SMatthew Dillon 	int error;
1205513ca7d7SMatthew Dillon 
1206b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1207513ca7d7SMatthew Dillon 	np = vfs_export_lookup(mp, &hmp->export, nam);
1208513ca7d7SMatthew Dillon 	if (np) {
1209513ca7d7SMatthew Dillon 		*exflagsp = np->netc_exflags;
1210513ca7d7SMatthew Dillon 		*credanonp = &np->netc_anon;
1211513ca7d7SMatthew Dillon 		error = 0;
1212513ca7d7SMatthew Dillon 	} else {
1213513ca7d7SMatthew Dillon 		error = EACCES;
1214513ca7d7SMatthew Dillon 	}
1215b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1216513ca7d7SMatthew Dillon 	return (error);
1217513ca7d7SMatthew Dillon 
1218513ca7d7SMatthew Dillon }
1219513ca7d7SMatthew Dillon 
1220513ca7d7SMatthew Dillon int
1221513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
1222513ca7d7SMatthew Dillon {
1223513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
1224513ca7d7SMatthew Dillon 	int error;
1225513ca7d7SMatthew Dillon 
1226b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1227b0aab9b9SMatthew Dillon 
1228513ca7d7SMatthew Dillon 	switch(op) {
1229513ca7d7SMatthew Dillon 	case MOUNTCTL_SET_EXPORT:
1230513ca7d7SMatthew Dillon 		error = vfs_export(mp, &hmp->export, export);
1231513ca7d7SMatthew Dillon 		break;
1232513ca7d7SMatthew Dillon 	default:
1233513ca7d7SMatthew Dillon 		error = EOPNOTSUPP;
1234513ca7d7SMatthew Dillon 		break;
1235513ca7d7SMatthew Dillon 	}
1236b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1237b0aab9b9SMatthew Dillon 
1238513ca7d7SMatthew Dillon 	return(error);
1239513ca7d7SMatthew Dillon }
1240513ca7d7SMatthew Dillon 
1241