xref: /dflybsd-src/sys/vfs/hammer/hammer_vfsops.c (revision 5ca0a96d6c3bf50926197b4bb92af7969ed3528a)
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 
3597fb61c0STomohiro Kusumi #include <sys/mountctl.h>
36b45803e3STomohiro Kusumi 
37427e5fc6SMatthew Dillon #include "hammer.h"
38427e5fc6SMatthew Dillon 
39aac0aabdSMatthew Dillon /*
40aac0aabdSMatthew Dillon  * NOTE!  Global statistics may not be MPSAFE so HAMMER never uses them
41aac0aabdSMatthew Dillon  *	  in conditionals.
42aac0aabdSMatthew Dillon  */
4302428fb6SMatthew Dillon int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT;
442f85fa4dSMatthew Dillon int hammer_debug_io;
45d5ef456eSMatthew Dillon int hammer_debug_general;
46e8599db1SMatthew Dillon int hammer_debug_inode;
477d683b0fSMatthew Dillon int hammer_debug_locks;
48b3deaf57SMatthew Dillon int hammer_debug_btree;
49d113fda1SMatthew Dillon int hammer_debug_tid;
5046fe7ae1SMatthew Dillon int hammer_debug_recover;		/* -1 will disable, +1 will force */
51fc73edd8SMatthew Dillon int hammer_debug_critical;		/* non-zero enter debugger on error */
52e7d75765SMatthew Dillon int hammer_cluster_enable = 2;		/* ena cluster_read, scale x 2 */
53e2a02b72SMatthew Dillon int hammer_tdmux_ticks;
547a61b85dSMatthew Dillon int hammer_count_fsyncs;
55b3deaf57SMatthew Dillon int hammer_count_inodes;
56af209b0fSMatthew Dillon int hammer_count_iqueued;
57e2a02b72SMatthew Dillon int hammer_count_reclaims;
58b3deaf57SMatthew Dillon int hammer_count_records;
59b3deaf57SMatthew Dillon int hammer_count_record_datas;
60b3deaf57SMatthew Dillon int hammer_count_volumes;
61b3deaf57SMatthew Dillon int hammer_count_buffers;
62b3deaf57SMatthew Dillon int hammer_count_nodes;
63cb51be26SMatthew Dillon int64_t hammer_stats_btree_lookups;
64cb51be26SMatthew Dillon int64_t hammer_stats_btree_searches;
65cb51be26SMatthew Dillon int64_t hammer_stats_btree_inserts;
66cb51be26SMatthew Dillon int64_t hammer_stats_btree_deletes;
67cb51be26SMatthew Dillon int64_t hammer_stats_btree_elements;
68cb51be26SMatthew Dillon int64_t hammer_stats_btree_splits;
69cb51be26SMatthew Dillon int64_t hammer_stats_btree_iterations;
7039d8fd63SMatthew Dillon int64_t hammer_stats_btree_root_iterations;
71cb51be26SMatthew Dillon int64_t hammer_stats_record_iterations;
72ce0138a6SMatthew Dillon 
73ce0138a6SMatthew Dillon int64_t hammer_stats_file_read;
74ce0138a6SMatthew Dillon int64_t hammer_stats_file_write;
75ce0138a6SMatthew Dillon int64_t hammer_stats_disk_read;
76ce0138a6SMatthew Dillon int64_t hammer_stats_disk_write;
77ce0138a6SMatthew Dillon int64_t hammer_stats_inode_flushes;
78ce0138a6SMatthew Dillon int64_t hammer_stats_commits;
7989e744ceSMatthew Dillon int64_t hammer_stats_undo;
806048b411SMatthew Dillon int64_t hammer_stats_redo;
81ce0138a6SMatthew Dillon 
823583bbb4SMatthew Dillon long hammer_count_dirtybufspace;	/* global */
83a99b9ea2SMatthew Dillon int hammer_count_refedbufs;		/* global */
840832c9bbSMatthew Dillon int hammer_count_reservations;
853583bbb4SMatthew Dillon long hammer_count_io_running_read;
863583bbb4SMatthew Dillon long hammer_count_io_running_write;
87a99b9ea2SMatthew Dillon int hammer_count_io_locked;
883583bbb4SMatthew Dillon long hammer_limit_dirtybufspace;	/* per-mount */
890832c9bbSMatthew Dillon int hammer_limit_recs;			/* as a whole XXX */
90a117fbebSMatthew Dillon int hammer_limit_inode_recs = 2048;	/* per inode */
91e2a02b72SMatthew Dillon int hammer_limit_reclaims;
929c559b05STomohiro Kusumi int hammer_live_dedup_cache_size = 4096;
936048b411SMatthew Dillon int hammer_limit_redo = 4096 * 1024;	/* per inode */
9437646115SMatthew Dillon int hammer_autoflush = 500;		/* auto flush (typ on reclaim) */
95cb51be26SMatthew Dillon int hammer_verify_zone;
961b0ab2c3SMatthew Dillon int hammer_verify_data = 1;
97b4f86ea3SMatthew Dillon int hammer_double_buffer;
98283a4a38SMatthew Dillon int hammer_btree_full_undo = 1;
993e583440SMatthew Dillon int hammer_yield_check = 16;
1009d0a6205SMatthew Dillon int hammer_fsync_mode = 3;
1017d683b0fSMatthew Dillon int64_t hammer_contention_count;
102b3deaf57SMatthew Dillon 
1034aaf46d1SSepherosa Ziehau int hammer_noatime = 1;
1044aaf46d1SSepherosa Ziehau TUNABLE_INT("vfs.hammer.noatime", &hammer_noatime);
1054aaf46d1SSepherosa Ziehau 
106b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
1075987cc42SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD,
1085987cc42SMatthew Dillon 	   &hammer_supported_version, 0, "");
109d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
110d5ef456eSMatthew Dillon 	   &hammer_debug_general, 0, "");
1112f85fa4dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
1122f85fa4dSMatthew Dillon 	   &hammer_debug_io, 0, "");
113e8599db1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
114e8599db1SMatthew Dillon 	   &hammer_debug_inode, 0, "");
1157d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
1167d683b0fSMatthew Dillon 	   &hammer_debug_locks, 0, "");
117b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
118b3deaf57SMatthew Dillon 	   &hammer_debug_btree, 0, "");
119d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
120d113fda1SMatthew Dillon 	   &hammer_debug_tid, 0, "");
121b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
122b33e2cc0SMatthew Dillon 	   &hammer_debug_recover, 0, "");
123fc73edd8SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW,
124fc73edd8SMatthew Dillon 	   &hammer_debug_critical, 0, "");
1251b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
1261b0ab2c3SMatthew Dillon 	   &hammer_cluster_enable, 0, "");
127e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, tdmux_ticks, CTLFLAG_RW,
128e2a02b72SMatthew Dillon 	   &hammer_tdmux_ticks, 0, "Hammer tdmux ticks");
1299480ff55SMatthew Dillon 
1303583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
131f5a07a7aSMatthew Dillon 	   &hammer_limit_dirtybufspace, 0, "");
13247637bffSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
13347637bffSMatthew Dillon 	   &hammer_limit_recs, 0, "");
134de996e86SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW,
135de996e86SMatthew Dillon 	   &hammer_limit_inode_recs, 0, "");
136e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaims, CTLFLAG_RW,
137e2a02b72SMatthew Dillon 	   &hammer_limit_reclaims, 0, "");
138e2ef7a95SIlya Dryomov SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW,
1392b34db9bSSamuel J. Greear 	   &hammer_live_dedup_cache_size, 0,
1402b34db9bSSamuel J. Greear 	   "Number of cache entries");
1416048b411SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW,
1426048b411SMatthew Dillon 	   &hammer_limit_redo, 0, "");
1439480ff55SMatthew Dillon 
1447a61b85dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
1457a61b85dSMatthew Dillon 	   &hammer_count_fsyncs, 0, "");
146b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
147b3deaf57SMatthew Dillon 	   &hammer_count_inodes, 0, "");
148af209b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
149af209b0fSMatthew Dillon 	   &hammer_count_iqueued, 0, "");
150e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaims, CTLFLAG_RD,
151e2a02b72SMatthew Dillon 	   &hammer_count_reclaims, 0, "");
152b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
153b3deaf57SMatthew Dillon 	   &hammer_count_records, 0, "");
154b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
155b3deaf57SMatthew Dillon 	   &hammer_count_record_datas, 0, "");
156b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
157b3deaf57SMatthew Dillon 	   &hammer_count_volumes, 0, "");
158b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
159b3deaf57SMatthew Dillon 	   &hammer_count_buffers, 0, "");
160b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
161b3deaf57SMatthew Dillon 	   &hammer_count_nodes, 0, "");
162ce0138a6SMatthew Dillon 
163cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
164cb51be26SMatthew Dillon 	   &hammer_stats_btree_searches, 0, "");
165cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
166cb51be26SMatthew Dillon 	   &hammer_stats_btree_lookups, 0, "");
167cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
168cb51be26SMatthew Dillon 	   &hammer_stats_btree_inserts, 0, "");
169cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
170cb51be26SMatthew Dillon 	   &hammer_stats_btree_deletes, 0, "");
171cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
172cb51be26SMatthew Dillon 	   &hammer_stats_btree_elements, 0, "");
173cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
174cb51be26SMatthew Dillon 	   &hammer_stats_btree_splits, 0, "");
175cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
176cb51be26SMatthew Dillon 	   &hammer_stats_btree_iterations, 0, "");
17739d8fd63SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD,
17839d8fd63SMatthew Dillon 	   &hammer_stats_btree_root_iterations, 0, "");
179cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
180cb51be26SMatthew Dillon 	   &hammer_stats_record_iterations, 0, "");
181ce0138a6SMatthew Dillon 
182ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
183ce0138a6SMatthew Dillon 	   &hammer_stats_file_read, 0, "");
184ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
185ce0138a6SMatthew Dillon 	   &hammer_stats_file_write, 0, "");
186ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
187ce0138a6SMatthew Dillon 	   &hammer_stats_disk_read, 0, "");
188ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
189ce0138a6SMatthew Dillon 	   &hammer_stats_disk_write, 0, "");
190ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
191ce0138a6SMatthew Dillon 	   &hammer_stats_inode_flushes, 0, "");
192ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
193ce0138a6SMatthew Dillon 	   &hammer_stats_commits, 0, "");
19489e744ceSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD,
19589e744ceSMatthew Dillon 	   &hammer_stats_undo, 0, "");
1966048b411SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD,
1976048b411SMatthew Dillon 	   &hammer_stats_redo, 0, "");
198ce0138a6SMatthew Dillon 
1993583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
200f5a07a7aSMatthew Dillon 	   &hammer_count_dirtybufspace, 0, "");
201a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
202a99b9ea2SMatthew Dillon 	   &hammer_count_refedbufs, 0, "");
2030832c9bbSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
2040832c9bbSMatthew Dillon 	   &hammer_count_reservations, 0, "");
2053583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
206a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_read, 0, "");
207a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
208a99b9ea2SMatthew Dillon 	   &hammer_count_io_locked, 0, "");
2093583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
210a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_write, 0, "");
2117d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
2127d683b0fSMatthew Dillon 	   &hammer_contention_count, 0, "");
21321fde338SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
21421fde338SMatthew Dillon 	   &hammer_autoflush, 0, "");
215cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
216cb51be26SMatthew Dillon 	   &hammer_verify_zone, 0, "");
2171b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
2181b0ab2c3SMatthew Dillon 	   &hammer_verify_data, 0, "");
219b4f86ea3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, double_buffer, CTLFLAG_RW,
220b4f86ea3SMatthew Dillon 	   &hammer_double_buffer, 0, "");
221283a4a38SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, btree_full_undo, CTLFLAG_RW,
222283a4a38SMatthew Dillon 	   &hammer_btree_full_undo, 0, "");
2233e583440SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW,
2243e583440SMatthew Dillon 	   &hammer_yield_check, 0, "");
2256f3d87c0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW,
2266f3d87c0SMatthew Dillon 	   &hammer_fsync_mode, 0, "");
227b3deaf57SMatthew Dillon 
228879a1b60SSascha Wildner /* KTR_INFO_MASTER(hammer); */
229bcac4bbbSMatthew Dillon 
230427e5fc6SMatthew Dillon /*
231427e5fc6SMatthew Dillon  * VFS ABI
232427e5fc6SMatthew Dillon  */
233427e5fc6SMatthew Dillon static void	hammer_free_hmp(struct mount *mp);
234427e5fc6SMatthew Dillon 
235427e5fc6SMatthew Dillon static int	hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
236427e5fc6SMatthew Dillon 				struct ucred *cred);
237427e5fc6SMatthew Dillon static int	hammer_vfs_unmount(struct mount *mp, int mntflags);
238427e5fc6SMatthew Dillon static int	hammer_vfs_root(struct mount *mp, struct vnode **vpp);
239427e5fc6SMatthew Dillon static int	hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
240427e5fc6SMatthew Dillon 				struct ucred *cred);
2416f97fce3SMatthew Dillon static int	hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
2426f97fce3SMatthew Dillon 				struct ucred *cred);
243427e5fc6SMatthew Dillon static int	hammer_vfs_sync(struct mount *mp, int waitfor);
244b9b0a6d0SMatthew Dillon static int	hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
245b9b0a6d0SMatthew Dillon 				ino_t ino, struct vnode **vpp);
246427e5fc6SMatthew Dillon static int	hammer_vfs_init(struct vfsconf *conf);
24767863d04SMatthew Dillon static int	hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
24867863d04SMatthew Dillon 				struct fid *fhp, struct vnode **vpp);
249513ca7d7SMatthew Dillon static int	hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
250513ca7d7SMatthew Dillon static int	hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
251513ca7d7SMatthew Dillon 				int *exflagsp, struct ucred **credanonp);
252513ca7d7SMatthew Dillon 
253427e5fc6SMatthew Dillon 
254427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = {
255*00369c4aSMatthew Dillon 	.vfs_flags	= 0,
256427e5fc6SMatthew Dillon 	.vfs_mount	= hammer_vfs_mount,
257427e5fc6SMatthew Dillon 	.vfs_unmount	= hammer_vfs_unmount,
258427e5fc6SMatthew Dillon 	.vfs_root	= hammer_vfs_root,
259427e5fc6SMatthew Dillon 	.vfs_statfs	= hammer_vfs_statfs,
2606f97fce3SMatthew Dillon 	.vfs_statvfs	= hammer_vfs_statvfs,
261427e5fc6SMatthew Dillon 	.vfs_sync	= hammer_vfs_sync,
262427e5fc6SMatthew Dillon 	.vfs_vget	= hammer_vfs_vget,
263513ca7d7SMatthew Dillon 	.vfs_init	= hammer_vfs_init,
264513ca7d7SMatthew Dillon 	.vfs_vptofh	= hammer_vfs_vptofh,
265513ca7d7SMatthew Dillon 	.vfs_fhtovp	= hammer_vfs_fhtovp,
266513ca7d7SMatthew Dillon 	.vfs_checkexp	= hammer_vfs_checkexp
267427e5fc6SMatthew Dillon };
268427e5fc6SMatthew Dillon 
269bac808feSMatthew Dillon MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", "");
270427e5fc6SMatthew Dillon 
27187f62b1cSMatthew Dillon VFS_SET(hammer_vfsops, hammer, VFCF_MPSAFE);
272427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1);
273427e5fc6SMatthew Dillon 
274427e5fc6SMatthew Dillon static int
hammer_vfs_init(struct vfsconf * conf)275427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf)
276427e5fc6SMatthew Dillon {
27769906b47SMatthew Dillon 	long n;
2783098dc2fSMatthew Dillon 
279e2a02b72SMatthew Dillon 	/*
280e2a02b72SMatthew Dillon 	 * Wait up to this long for an exclusive deadlock to clear
281e2a02b72SMatthew Dillon 	 * before acquiring a new shared lock on the ip.  The deadlock
282e2a02b72SMatthew Dillon 	 * may have occured on a b-tree node related to the ip.
283e2a02b72SMatthew Dillon 	 */
284e2a02b72SMatthew Dillon 	if (hammer_tdmux_ticks == 0)
285e2a02b72SMatthew Dillon 		hammer_tdmux_ticks = hz / 5;
286e2a02b72SMatthew Dillon 
287e2a02b72SMatthew Dillon 	/*
28869906b47SMatthew Dillon 	 * Autosize, but be careful because a hammer filesystem's
28969906b47SMatthew Dillon 	 * reserve is partially calculated based on dirtybufspace,
29069906b47SMatthew Dillon 	 * so we simply cannot allow it to get too large.
291e2a02b72SMatthew Dillon 	 */
2923098dc2fSMatthew Dillon 	if (hammer_limit_recs == 0) {
29369906b47SMatthew Dillon 		n = nbuf * 25;
29469906b47SMatthew Dillon 		if (n > kmalloc_limit(M_HAMMER) / 512)
2953098dc2fSMatthew Dillon 			n = kmalloc_limit(M_HAMMER) / 512;
29669906b47SMatthew Dillon 		if (n > 2 * 1024 * 1024)
29769906b47SMatthew Dillon 			n = 2 * 1024 * 1024;
29869906b47SMatthew Dillon 		hammer_limit_recs = (int)n;
2993098dc2fSMatthew Dillon 	}
300f5a07a7aSMatthew Dillon 	if (hammer_limit_dirtybufspace == 0) {
301f5a07a7aSMatthew Dillon 		hammer_limit_dirtybufspace = hidirtybufspace / 2;
30269906b47SMatthew Dillon 		if (hammer_limit_dirtybufspace < 1L * 1024 * 1024)
30369906b47SMatthew Dillon 			hammer_limit_dirtybufspace = 1024L * 1024;
30469906b47SMatthew Dillon 		if (hammer_limit_dirtybufspace > 1024L * 1024 * 1024)
30569906b47SMatthew Dillon 			hammer_limit_dirtybufspace = 1024L * 1024 * 1024;
3069f5097dcSMatthew Dillon 	}
307ba298df1SMatthew Dillon 
308ba298df1SMatthew Dillon 	/*
30937646115SMatthew Dillon 	 * The hammer_inode structure detaches from the vnode on reclaim.
31037646115SMatthew Dillon 	 * This limits the number of inodes in this state to prevent a
31137646115SMatthew Dillon 	 * memory pool blowout.
31237646115SMatthew Dillon 	 */
3130fe46dc6SMatthew Dillon 	if (hammer_limit_reclaims == 0) {
3149629eb35SMatthew Dillon 		hammer_limit_reclaims = maxvnodes / 10;
3150fe46dc6SMatthew Dillon 		if (hammer_limit_reclaims > HAMMER_LIMIT_RECLAIMS)
3160fe46dc6SMatthew Dillon 			hammer_limit_reclaims = HAMMER_LIMIT_RECLAIMS;
3170fe46dc6SMatthew Dillon 	}
31837646115SMatthew Dillon 
319427e5fc6SMatthew Dillon 	return(0);
320427e5fc6SMatthew Dillon }
321427e5fc6SMatthew Dillon 
322427e5fc6SMatthew Dillon static int
hammer_vfs_mount(struct mount * mp,char * mntpt,caddr_t data,struct ucred * cred)323427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
324427e5fc6SMatthew Dillon 		 struct ucred *cred)
325427e5fc6SMatthew Dillon {
326427e5fc6SMatthew Dillon 	struct hammer_mount_info info;
327a89aec1bSMatthew Dillon 	hammer_mount_t hmp;
328a89aec1bSMatthew Dillon 	hammer_volume_t rootvol;
32927ea2398SMatthew Dillon 	struct vnode *rootvp;
3307c19b529SMichael Neumann 	struct vnode *devvp = NULL;
331427e5fc6SMatthew Dillon 	const char *upath;	/* volume name in userspace */
332427e5fc6SMatthew Dillon 	char *path;		/* volume name in system space */
333427e5fc6SMatthew Dillon 	int error;
334427e5fc6SMatthew Dillon 	int i;
335732a1697SMatthew Dillon 	int master_id;
336c302e844STomohiro Kusumi 	int nvolumes;
337104cb849SMichael Neumann 	char *next_volume_ptr = NULL;
338ab0bf4a3SMatthew Dillon 
3394aaf46d1SSepherosa Ziehau 	if (hammer_noatime) {
3404aaf46d1SSepherosa Ziehau 		/* Force noatime */
3414aaf46d1SSepherosa Ziehau 		mp->mnt_flag |= MNT_NOATIME;
3424aaf46d1SSepherosa Ziehau 	}
3434aaf46d1SSepherosa Ziehau 
344ab0bf4a3SMatthew Dillon 	/*
345ab0bf4a3SMatthew Dillon 	 * Accept hammer_mount_info.  mntpt is NULL for root mounts at boot.
346ab0bf4a3SMatthew Dillon 	 */
347ab0bf4a3SMatthew Dillon 	if (mntpt == NULL) {
3487c19b529SMichael Neumann 		bzero(&info, sizeof(info));
3497c19b529SMichael Neumann 		info.asof = 0;
3507c19b529SMichael Neumann 		info.hflags = 0;
3517c19b529SMichael Neumann 		info.nvolumes = 1;
352104cb849SMichael Neumann 
353104cb849SMichael Neumann 		next_volume_ptr = mp->mnt_stat.f_mntfromname;
354104cb849SMichael Neumann 
355104cb849SMichael Neumann 		/* Count number of volumes separated by ':' */
356104cb849SMichael Neumann 		for (char *p = next_volume_ptr; *p != '\0'; ++p) {
357104cb849SMichael Neumann 			if (*p == ':') {
358104cb849SMichael Neumann 				++info.nvolumes;
359104cb849SMichael Neumann 			}
360104cb849SMichael Neumann 		}
361104cb849SMichael Neumann 
362104cb849SMichael Neumann 		mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
3637c19b529SMichael Neumann 	} else {
364427e5fc6SMatthew Dillon 		if ((error = copyin(data, &info, sizeof(info))) != 0)
365427e5fc6SMatthew Dillon 			return (error);
3667c19b529SMichael Neumann 	}
367427e5fc6SMatthew Dillon 
368427e5fc6SMatthew Dillon 	/*
369732a1697SMatthew Dillon 	 * updating or new mount
370427e5fc6SMatthew Dillon 	 */
371195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
372195c19a1SMatthew Dillon 		hmp = (void *)mp->mnt_data;
373195c19a1SMatthew Dillon 		KKASSERT(hmp != NULL);
374195c19a1SMatthew Dillon 	} else {
3757529c2acSAntonio Huete Jimenez 		if (info.nvolumes <= 0 || info.nvolumes > HAMMER_MAX_VOLUMES)
376732a1697SMatthew Dillon 			return (EINVAL);
377732a1697SMatthew Dillon 		hmp = NULL;
378732a1697SMatthew Dillon 	}
379732a1697SMatthew Dillon 
380732a1697SMatthew Dillon 	/*
381732a1697SMatthew Dillon 	 * master-id validation.  The master id may not be changed by a
382732a1697SMatthew Dillon 	 * mount update.
383732a1697SMatthew Dillon 	 */
38487b8f936STomohiro Kusumi 	if (info.hflags & HMNT_MASTERID || info.hflags & HMNT_NOMIRROR) {
385732a1697SMatthew Dillon 		if (hmp && hmp->master_id != info.master_id) {
386d053aa8aSTomohiro Kusumi 			hkprintf("cannot change master id with mount update\n");
387732a1697SMatthew Dillon 			return(EINVAL);
388732a1697SMatthew Dillon 		}
389732a1697SMatthew Dillon 		master_id = info.master_id;
390732a1697SMatthew Dillon 		if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
391732a1697SMatthew Dillon 			return (EINVAL);
392732a1697SMatthew Dillon 	} else {
393732a1697SMatthew Dillon 		if (hmp)
394732a1697SMatthew Dillon 			master_id = hmp->master_id;
395732a1697SMatthew Dillon 		else
396732a1697SMatthew Dillon 			master_id = 0;
397732a1697SMatthew Dillon 	}
398732a1697SMatthew Dillon 
399732a1697SMatthew Dillon 	/*
400b0aab9b9SMatthew Dillon 	 * Internal mount data structure
401732a1697SMatthew Dillon 	 */
402732a1697SMatthew Dillon 	if (hmp == NULL) {
403427e5fc6SMatthew Dillon 		hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
404427e5fc6SMatthew Dillon 		mp->mnt_data = (qaddr_t)hmp;
405427e5fc6SMatthew Dillon 		hmp->mp = mp;
40647197d71SMatthew Dillon 
407bac808feSMatthew Dillon 		/*
4087c457ac8SMatthew Dillon 		 * Make sure kmalloc type limits are set appropriately.
4097c457ac8SMatthew Dillon 		 *
4107c457ac8SMatthew Dillon 		 * Our inode kmalloc group is sized based on maxvnodes
4117c457ac8SMatthew Dillon 		 * (controlled by the system, not us).
412bac808feSMatthew Dillon 		 */
413bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_misc, "HAMMER-others");
414bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
415bac808feSMatthew Dillon 
4167c457ac8SMatthew Dillon 		kmalloc_raise_limit(hmp->m_inodes, 0);	/* unlimited */
417bac808feSMatthew Dillon 
4186416eb03STomohiro Kusumi 		hmp->root_btree_beg.localization =
4196416eb03STomohiro Kusumi 			HAMMER_MIN_ONDISK_LOCALIZATION;
4209615af80STomohiro Kusumi 		hmp->root_btree_beg.obj_id = HAMMER_MIN_OBJID;
4219615af80STomohiro Kusumi 		hmp->root_btree_beg.key = HAMMER_MIN_KEY;
42247197d71SMatthew Dillon 		hmp->root_btree_beg.create_tid = 1;
42347197d71SMatthew Dillon 		hmp->root_btree_beg.delete_tid = 1;
4249615af80STomohiro Kusumi 		hmp->root_btree_beg.rec_type = HAMMER_MIN_RECTYPE;
42547197d71SMatthew Dillon 		hmp->root_btree_beg.obj_type = 0;
426bfbd8331STomohiro Kusumi 		hmp->root_btree_beg.btype = HAMMER_BTREE_TYPE_NONE;
42747197d71SMatthew Dillon 
4286416eb03STomohiro Kusumi 		hmp->root_btree_end.localization =
4296416eb03STomohiro Kusumi 			HAMMER_MAX_ONDISK_LOCALIZATION;
4309615af80STomohiro Kusumi 		hmp->root_btree_end.obj_id = HAMMER_MAX_OBJID;
4319615af80STomohiro Kusumi 		hmp->root_btree_end.key = HAMMER_MAX_KEY;
4329615af80STomohiro Kusumi 		hmp->root_btree_end.create_tid = HAMMER_MAX_TID;
43347197d71SMatthew Dillon 		hmp->root_btree_end.delete_tid = 0;   /* special case */
4349615af80STomohiro Kusumi 		hmp->root_btree_end.rec_type = HAMMER_MAX_RECTYPE;
43547197d71SMatthew Dillon 		hmp->root_btree_end.obj_type = 0;
436bfbd8331STomohiro Kusumi 		hmp->root_btree_end.btype = HAMMER_BTREE_TYPE_NONE;
437f03c9cf4SMatthew Dillon 
438cdb6e4e6SMatthew Dillon 		hmp->krate.freq = 1;	/* maximum reporting rate (hz) */
439cdb6e4e6SMatthew Dillon 		hmp->krate.count = -16;	/* initial burst */
440a8d31329SMatthew Dillon 		hmp->kdiag.freq = 1;	/* maximum reporting rate (hz) */
441a8d31329SMatthew Dillon 		hmp->kdiag.count = -16;	/* initial burst */
442cdb6e4e6SMatthew Dillon 
4439480ff55SMatthew Dillon 		hmp->sync_lock.refs = 1;
444d99d6bf5SMatthew Dillon 		hmp->undo_lock.refs = 1;
445d99d6bf5SMatthew Dillon 		hmp->blkmap_lock.refs = 1;
44683f2a3aaSMatthew Dillon 		hmp->snapshot_lock.refs = 1;
44752e547e3SMichael Neumann 		hmp->volume_lock.refs = 1;
4489480ff55SMatthew Dillon 
449cebe9493SMatthew Dillon 		TAILQ_INIT(&hmp->delay_list);
4507a61b85dSMatthew Dillon 		TAILQ_INIT(&hmp->flush_group_list);
4510729c8c8SMatthew Dillon 		TAILQ_INIT(&hmp->objid_cache_list);
452e8599db1SMatthew Dillon 		TAILQ_INIT(&hmp->undo_lru_list);
4537bc5b8c2SMatthew Dillon 		TAILQ_INIT(&hmp->reclaim_list);
454195c19a1SMatthew Dillon 	}
45551c35492SMatthew Dillon 	hmp->hflags &= ~HMNT_USERFLAGS;
45651c35492SMatthew Dillon 	hmp->hflags |= info.hflags & HMNT_USERFLAGS;
457732a1697SMatthew Dillon 
458732a1697SMatthew Dillon 	hmp->master_id = master_id;
459732a1697SMatthew Dillon 
4607f7c1f84SMatthew Dillon 	if (info.asof) {
4617f7c1f84SMatthew Dillon 		mp->mnt_flag |= MNT_RDONLY;
4627f7c1f84SMatthew Dillon 		hmp->asof = info.asof;
4637f7c1f84SMatthew Dillon 	} else {
4647f7c1f84SMatthew Dillon 		hmp->asof = HAMMER_MAX_TID;
4657f7c1f84SMatthew Dillon 	}
466195c19a1SMatthew Dillon 
467865c9609SMichael Neumann 	hmp->volume_to_remove = -1;
468865c9609SMichael Neumann 
469195c19a1SMatthew Dillon 	/*
47051c35492SMatthew Dillon 	 * Re-open read-write if originally read-only, or vise-versa.
47102428fb6SMatthew Dillon 	 *
47202428fb6SMatthew Dillon 	 * When going from read-only to read-write execute the stage2
47302428fb6SMatthew Dillon 	 * recovery if it has not already been run.
474195c19a1SMatthew Dillon 	 */
475195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
476b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->fs_token);
47751c35492SMatthew Dillon 		error = 0;
47851c35492SMatthew Dillon 		if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
479d053aa8aSTomohiro Kusumi 			hkprintf("read-only -> read-write\n");
480195c19a1SMatthew Dillon 			hmp->ronly = 0;
48151c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
48251c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
48351c35492SMatthew Dillon 			rootvol = hammer_get_root_volume(hmp, &error);
48451c35492SMatthew Dillon 			if (rootvol) {
48506ad81ffSMatthew Dillon 				hammer_recover_flush_buffers(hmp, rootvol, 1);
48602428fb6SMatthew Dillon 				error = hammer_recover_stage2(hmp, rootvol);
4879f5097dcSMatthew Dillon 				bcopy(rootvol->ondisk->vol0_blockmap,
4889f5097dcSMatthew Dillon 				      hmp->blockmap,
4899f5097dcSMatthew Dillon 				      sizeof(hmp->blockmap));
49051c35492SMatthew Dillon 				hammer_rel_volume(rootvol, 0);
491195c19a1SMatthew Dillon 			}
49251c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
49351c35492SMatthew Dillon 				hammer_reload_inode, NULL);
49451c35492SMatthew Dillon 			/* kernel clears MNT_RDONLY */
49551c35492SMatthew Dillon 		} else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
496d053aa8aSTomohiro Kusumi 			hkprintf("read-write -> read-only\n");
49751c35492SMatthew Dillon 			hmp->ronly = 1;	/* messy */
49851c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
49951c35492SMatthew Dillon 				hammer_reload_inode, NULL);
50051c35492SMatthew Dillon 			hmp->ronly = 0;
50151c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
50251c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
50351c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
50451c35492SMatthew Dillon 			hmp->ronly = 1;
50551c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
50651c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
50751c35492SMatthew Dillon 		}
508b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
50951c35492SMatthew Dillon 		return(error);
510195c19a1SMatthew Dillon 	}
511195c19a1SMatthew Dillon 
512427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_vols_root);
513427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_inos_root);
51473896937SMatthew Dillon 	RB_INIT(&hmp->rb_redo_root);
51540043e7fSMatthew Dillon 	RB_INIT(&hmp->rb_nods_root);
516e8599db1SMatthew Dillon 	RB_INIT(&hmp->rb_undo_root);
5170832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_resv_root);
5180832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_bufs_root);
5195fa5c92fSMatthew Dillon 	RB_INIT(&hmp->rb_pfsm_root);
5200832c9bbSMatthew Dillon 
521195c19a1SMatthew Dillon 	hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
522427e5fc6SMatthew Dillon 
5231afb73cfSMatthew Dillon 	RB_INIT(&hmp->volu_root);
5241afb73cfSMatthew Dillon 	RB_INIT(&hmp->undo_root);
5251afb73cfSMatthew Dillon 	RB_INIT(&hmp->data_root);
5261afb73cfSMatthew Dillon 	RB_INIT(&hmp->meta_root);
5271afb73cfSMatthew Dillon 	RB_INIT(&hmp->lose_root);
528eddadaeeSMatthew Dillon 	TAILQ_INIT(&hmp->iorun_list);
52910a5d1baSMatthew Dillon 
530a3c18566SMatthew Dillon 	lwkt_token_init(&hmp->fs_token, "hammerfs");
531a3c18566SMatthew Dillon 	lwkt_token_init(&hmp->io_token, "hammerio");
532b0aab9b9SMatthew Dillon 
533b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
534b0aab9b9SMatthew Dillon 
535427e5fc6SMatthew Dillon 	/*
536427e5fc6SMatthew Dillon 	 * Load volumes
537427e5fc6SMatthew Dillon 	 */
538427e5fc6SMatthew Dillon 	path = objcache_get(namei_oc, M_WAITOK);
5391b0ab2c3SMatthew Dillon 	hmp->nvolumes = -1;
540427e5fc6SMatthew Dillon 	for (i = 0; i < info.nvolumes; ++i) {
5417c19b529SMichael Neumann 		if (mntpt == NULL) {
5427c19b529SMichael Neumann 			/*
5437c19b529SMichael Neumann 			 * Root mount.
5447c19b529SMichael Neumann 			 */
545104cb849SMichael Neumann 			KKASSERT(next_volume_ptr != NULL);
546a407819fSMichael Neumann 			strcpy(path, "");
547a407819fSMichael Neumann 			if (*next_volume_ptr != '/') {
548a407819fSMichael Neumann 				/* relative path */
549104cb849SMichael Neumann 				strcpy(path, "/dev/");
550a407819fSMichael Neumann 			}
551104cb849SMichael Neumann 			int k;
552104cb849SMichael Neumann 			for (k = strlen(path); k < MAXPATHLEN-1; ++k) {
553104cb849SMichael Neumann 				if (*next_volume_ptr == '\0') {
554104cb849SMichael Neumann 					break;
555104cb849SMichael Neumann 				} else if (*next_volume_ptr == ':') {
556104cb849SMichael Neumann 					++next_volume_ptr;
557104cb849SMichael Neumann 					break;
558104cb849SMichael Neumann 				} else {
559104cb849SMichael Neumann 					path[k] = *next_volume_ptr;
560104cb849SMichael Neumann 					++next_volume_ptr;
561104cb849SMichael Neumann 				}
562104cb849SMichael Neumann 			}
563104cb849SMichael Neumann 			path[k] = '\0';
564104cb849SMichael Neumann 
5657c19b529SMichael Neumann 			error = 0;
566104cb849SMichael Neumann 			cdev_t dev = kgetdiskbyname(path);
567104cb849SMichael Neumann 			error = bdevvp(dev, &devvp);
568104cb849SMichael Neumann 			if (error) {
56911605a5cSTomohiro Kusumi 				hdkprintf("can't find devvp\n");
570104cb849SMichael Neumann 			}
5717c19b529SMichael Neumann 		} else {
5727c19b529SMichael Neumann 			error = copyin(&info.volumes[i], &upath,
5737c19b529SMichael Neumann 				       sizeof(char *));
574427e5fc6SMatthew Dillon 			if (error == 0)
5757c19b529SMichael Neumann 				error = copyinstr(upath, path,
5767c19b529SMichael Neumann 						  MAXPATHLEN, NULL);
5777c19b529SMichael Neumann 		}
578427e5fc6SMatthew Dillon 		if (error == 0)
579deabdbfbSTomohiro Kusumi 			error = hammer_install_volume(hmp, path, devvp, NULL);
580427e5fc6SMatthew Dillon 		if (error)
581427e5fc6SMatthew Dillon 			break;
582427e5fc6SMatthew Dillon 	}
583427e5fc6SMatthew Dillon 	objcache_put(namei_oc, path);
584427e5fc6SMatthew Dillon 
585427e5fc6SMatthew Dillon 	/*
586427e5fc6SMatthew Dillon 	 * Make sure we found a root volume
587427e5fc6SMatthew Dillon 	 */
588721f3110STomohiro Kusumi 	if (hmp->rootvol == NULL) {
5899d756529STomohiro Kusumi 		if (error == EBUSY) {
59011605a5cSTomohiro Kusumi 			hdkprintf("The volumes are probably mounted\n");
5919d756529STomohiro Kusumi 		} else {
59211605a5cSTomohiro Kusumi 			hdkprintf("No root volume found!\n");
593427e5fc6SMatthew Dillon 			error = EINVAL;
5949d756529STomohiro Kusumi 		}
595721f3110STomohiro Kusumi 		goto failed;
596427e5fc6SMatthew Dillon 	}
5971b0ab2c3SMatthew Dillon 
5981b0ab2c3SMatthew Dillon 	/*
5991b0ab2c3SMatthew Dillon 	 * Check that all required volumes are available
6001b0ab2c3SMatthew Dillon 	 */
6011b0ab2c3SMatthew Dillon 	if (error == 0 && hammer_mountcheck_volumes(hmp)) {
60211605a5cSTomohiro Kusumi 		hdkprintf("Missing volumes, cannot mount!\n");
6031b0ab2c3SMatthew Dillon 		error = EINVAL;
604721f3110STomohiro Kusumi 		goto failed;
6051b0ab2c3SMatthew Dillon 	}
6061b0ab2c3SMatthew Dillon 
607721f3110STomohiro Kusumi 	/*
608721f3110STomohiro Kusumi 	 * Other errors
609721f3110STomohiro Kusumi 	 */
610427e5fc6SMatthew Dillon 	if (error) {
61111605a5cSTomohiro Kusumi 		hdkprintf("Failed to load volumes!\n");
612721f3110STomohiro Kusumi 		goto failed;
613427e5fc6SMatthew Dillon 	}
614427e5fc6SMatthew Dillon 
615c302e844STomohiro Kusumi 	nvolumes = hammer_get_installed_volumes(hmp);
616c302e844STomohiro Kusumi 	if (hmp->nvolumes != nvolumes) {
61711605a5cSTomohiro Kusumi 		hdkprintf("volume header says %d volumes, but %d installed\n",
618c302e844STomohiro Kusumi 			hmp->nvolumes, nvolumes);
619c302e844STomohiro Kusumi 		error = EINVAL;
620c302e844STomohiro Kusumi 		goto failed;
621c302e844STomohiro Kusumi 	}
622c302e844STomohiro Kusumi 
623427e5fc6SMatthew Dillon 	/*
62427ea2398SMatthew Dillon 	 * No errors, setup enough of the mount point so we can lookup the
62527ea2398SMatthew Dillon 	 * root vnode.
626427e5fc6SMatthew Dillon 	 */
627427e5fc6SMatthew Dillon 	mp->mnt_iosize_max = MAXPHYS;
628cf6a53caSMatthew Dillon 	mp->mnt_kern_flag |= MNTK_THR_SYNC;	/* new vsyncscan semantics */
629c0ade690SMatthew Dillon 
630c0ade690SMatthew Dillon 	/*
631aac0aabdSMatthew Dillon 	 * MPSAFE code.  Note that VOPs and VFSops which are not MPSAFE
632aac0aabdSMatthew Dillon 	 * will acquire a per-mount token prior to entry and release it
6336a85c61bSTomohiro Kusumi 	 * on return.
634aac0aabdSMatthew Dillon 	 */
635b0aab9b9SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
636aac0aabdSMatthew Dillon 
637aac0aabdSMatthew Dillon 	/*
638c0ade690SMatthew Dillon 	 * note: f_iosize is used by vnode_pager_haspage() when constructing
639c0ade690SMatthew Dillon 	 * its VOP_BMAP call.
640c0ade690SMatthew Dillon 	 */
641c0ade690SMatthew Dillon 	mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
642fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
6436f97fce3SMatthew Dillon 
6446f97fce3SMatthew Dillon 	mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
6456f97fce3SMatthew Dillon 	mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
6466f97fce3SMatthew Dillon 
647427e5fc6SMatthew Dillon 	mp->mnt_maxsymlinklen = 255;
648427e5fc6SMatthew Dillon 	mp->mnt_flag |= MNT_LOCAL;
649427e5fc6SMatthew Dillon 
650427e5fc6SMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
6517a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
6527a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
65327ea2398SMatthew Dillon 
65427ea2398SMatthew Dillon 	/*
655a89aec1bSMatthew Dillon 	 * The root volume's ondisk pointer is only valid if we hold a
656a89aec1bSMatthew Dillon 	 * reference to it.
657a89aec1bSMatthew Dillon 	 */
658a89aec1bSMatthew Dillon 	rootvol = hammer_get_root_volume(hmp, &error);
659a89aec1bSMatthew Dillon 	if (error)
660f90dde4cSMatthew Dillon 		goto failed;
661f90dde4cSMatthew Dillon 
662f90dde4cSMatthew Dillon 	/*
6639f5097dcSMatthew Dillon 	 * Perform any necessary UNDO operations.  The recovery code does
6640729c8c8SMatthew Dillon 	 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
6650729c8c8SMatthew Dillon 	 * and then re-copy it again after recovery is complete.
666c9b9e29dSMatthew Dillon 	 *
66751c35492SMatthew Dillon 	 * If this is a read-only mount the UNDO information is retained
66851c35492SMatthew Dillon 	 * in memory in the form of dirty buffer cache buffers, and not
66951c35492SMatthew Dillon 	 * written back to the media.
670f90dde4cSMatthew Dillon 	 */
6710729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
6720729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
673c9b9e29dSMatthew Dillon 
6747a61b85dSMatthew Dillon 	/*
67544a83111SMatthew Dillon 	 * Check filesystem version
67644a83111SMatthew Dillon 	 */
67744a83111SMatthew Dillon 	hmp->version = rootvol->ondisk->vol_version;
67844a83111SMatthew Dillon 	if (hmp->version < HAMMER_VOL_VERSION_MIN ||
67944a83111SMatthew Dillon 	    hmp->version > HAMMER_VOL_VERSION_MAX) {
680d053aa8aSTomohiro Kusumi 		hkprintf("mount unsupported fs version %d\n", hmp->version);
68144a83111SMatthew Dillon 		error = ERANGE;
68244a83111SMatthew Dillon 		goto done;
68344a83111SMatthew Dillon 	}
68444a83111SMatthew Dillon 
68544a83111SMatthew Dillon 	/*
6867a61b85dSMatthew Dillon 	 * The undo_rec_limit limits the size of flush groups to avoid
6877a61b85dSMatthew Dillon 	 * blowing out the UNDO FIFO.  This calculation is typically in
6887a61b85dSMatthew Dillon 	 * the tens of thousands and is designed primarily when small
6897a61b85dSMatthew Dillon 	 * HAMMER filesystems are created.
6907a61b85dSMatthew Dillon 	 */
6917a61b85dSMatthew Dillon 	hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
6927a61b85dSMatthew Dillon 	if (hammer_debug_general & 0x0001)
693d053aa8aSTomohiro Kusumi 		hkprintf("undo_rec_limit %d\n", hmp->undo_rec_limit);
6947a61b85dSMatthew Dillon 
69502428fb6SMatthew Dillon 	/*
69602428fb6SMatthew Dillon 	 * NOTE: Recover stage1 not only handles meta-data recovery, it
69702428fb6SMatthew Dillon 	 * 	 also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems.
69802428fb6SMatthew Dillon 	 */
69902428fb6SMatthew Dillon 	error = hammer_recover_stage1(hmp, rootvol);
700f90dde4cSMatthew Dillon 	if (error) {
701f90dde4cSMatthew Dillon 		kprintf("Failed to recover HAMMER filesystem on mount\n");
702a89aec1bSMatthew Dillon 		goto done;
703f90dde4cSMatthew Dillon 	}
704f90dde4cSMatthew Dillon 
705f90dde4cSMatthew Dillon 	/*
706adf01747SMatthew Dillon 	 * Finish setup now that we have a good root volume.
707f90dde4cSMatthew Dillon 	 */
708a89aec1bSMatthew Dillon 	ksnprintf(mp->mnt_stat.f_mntfromname,
709a89aec1bSMatthew Dillon 		  sizeof(mp->mnt_stat.f_mntfromname), "%s",
7106c39d27aSTomohiro Kusumi 		  rootvol->ondisk->vol_label);
711513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[0] =
712513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
713513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] =
714513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
71593775b63STomohiro Kusumi 	mp->mnt_stat.f_fsid.val[1] &= HAMMER_LOCALIZE_MASK;
716b84de5afSMatthew Dillon 
7176f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
7186f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
7196f97fce3SMatthew Dillon 				     sizeof(mp->mnt_vstat.f_fsid_uuid));
7206f97fce3SMatthew Dillon 
7210729c8c8SMatthew Dillon 	/*
7220729c8c8SMatthew Dillon 	 * Certain often-modified fields in the root volume are cached in
7230729c8c8SMatthew Dillon 	 * the hammer_mount structure so we do not have to generate lots
7240729c8c8SMatthew Dillon 	 * of little UNDO structures for them.
725c9b9e29dSMatthew Dillon 	 *
7269f5097dcSMatthew Dillon 	 * Recopy after recovery.  This also has the side effect of
7279f5097dcSMatthew Dillon 	 * setting our cached undo FIFO's first_offset, which serves to
7289f5097dcSMatthew Dillon 	 * placemark the FIFO start for the NEXT flush cycle while the
7299f5097dcSMatthew Dillon 	 * on-disk first_offset represents the LAST flush cycle.
7300729c8c8SMatthew Dillon 	 */
731b84de5afSMatthew Dillon 	hmp->next_tid = rootvol->ondisk->vol0_next_tid;
7324889cbd4SMatthew Dillon 	hmp->flush_tid1 = hmp->next_tid;
7334889cbd4SMatthew Dillon 	hmp->flush_tid2 = hmp->next_tid;
7340729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
7350729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
736e63644f0SMatthew Dillon 	hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
7370729c8c8SMatthew Dillon 
738059819e3SMatthew Dillon 	hammer_flusher_create(hmp);
739059819e3SMatthew Dillon 
740a89aec1bSMatthew Dillon 	/*
7411bcc9299STomohiro Kusumi 	 * Locate the root directory with an obj_id of 1.
74227ea2398SMatthew Dillon 	 */
7436c35a928STomohiro Kusumi 	error = hammer_vfs_root(mp, &rootvp);
744a89aec1bSMatthew Dillon 	if (error)
745a89aec1bSMatthew Dillon 		goto done;
74627ea2398SMatthew Dillon 	vput(rootvp);
74788c39f64SThomas Nikolajsen 	if (hmp->ronly == 0)
74802428fb6SMatthew Dillon 		error = hammer_recover_stage2(hmp, rootvol);
74927ea2398SMatthew Dillon 
7509fa352a2SMatthew Dillon 	/*
7519fa352a2SMatthew Dillon 	 * If the stage2 recovery fails be sure to clean out all cached
7529fa352a2SMatthew Dillon 	 * vnodes before throwing away the mount structure or bad things
7539fa352a2SMatthew Dillon 	 * will happen.
7549fa352a2SMatthew Dillon 	 */
7559fa352a2SMatthew Dillon 	if (error)
7569fa352a2SMatthew Dillon 		vflush(mp, 0, 0);
7579fa352a2SMatthew Dillon 
758a89aec1bSMatthew Dillon done:
75998ad7adeSFrancois Tigeot 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
76098ad7adeSFrancois Tigeot 		/* New mount */
76198ad7adeSFrancois Tigeot 
76298ad7adeSFrancois Tigeot 		/* Populate info for mount point (NULL pad)*/
76398ad7adeSFrancois Tigeot 		bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
76498ad7adeSFrancois Tigeot 		size_t size;
76598ad7adeSFrancois Tigeot 		if (mntpt) {
76698ad7adeSFrancois Tigeot 			copyinstr(mntpt, mp->mnt_stat.f_mntonname,
76798ad7adeSFrancois Tigeot 							MNAMELEN -1, &size);
76898ad7adeSFrancois Tigeot 		} else { /* Root mount */
76998ad7adeSFrancois Tigeot 			mp->mnt_stat.f_mntonname[0] = '/';
77098ad7adeSFrancois Tigeot 		}
77198ad7adeSFrancois Tigeot 	}
77298ad7adeSFrancois Tigeot 	(void)VFS_STATFS(mp, &mp->mnt_stat, cred);
773f90dde4cSMatthew Dillon 	hammer_rel_volume(rootvol, 0);
774f90dde4cSMatthew Dillon failed:
77527ea2398SMatthew Dillon 	/*
77627ea2398SMatthew Dillon 	 * Cleanup and return.
77727ea2398SMatthew Dillon 	 */
778b0aab9b9SMatthew Dillon 	if (error) {
779b0aab9b9SMatthew Dillon 		/* called with fs_token held */
78027ea2398SMatthew Dillon 		hammer_free_hmp(mp);
781b0aab9b9SMatthew Dillon 	} else {
782b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
783b0aab9b9SMatthew Dillon 	}
784427e5fc6SMatthew Dillon 	return (error);
785427e5fc6SMatthew Dillon }
786427e5fc6SMatthew Dillon 
787427e5fc6SMatthew Dillon static int
hammer_vfs_unmount(struct mount * mp,int mntflags)788427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags)
789427e5fc6SMatthew Dillon {
790b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
791427e5fc6SMatthew Dillon 	int flags;
79266325755SMatthew Dillon 	int error;
79327ea2398SMatthew Dillon 
79427ea2398SMatthew Dillon 	/*
795427e5fc6SMatthew Dillon 	 * Clean out the vnodes
796427e5fc6SMatthew Dillon 	 */
797b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
79866325755SMatthew Dillon 	flags = 0;
79966325755SMatthew Dillon 	if (mntflags & MNT_FORCE)
80066325755SMatthew Dillon 		flags |= FORCECLOSE;
801b0aab9b9SMatthew Dillon 	error = vflush(mp, 0, flags);
802427e5fc6SMatthew Dillon 
803427e5fc6SMatthew Dillon 	/*
804427e5fc6SMatthew Dillon 	 * Clean up the internal mount structure and related entities.  This
805427e5fc6SMatthew Dillon 	 * may issue I/O.
806427e5fc6SMatthew Dillon 	 */
807b0aab9b9SMatthew Dillon 	if (error == 0) {
808b0aab9b9SMatthew Dillon 		/* called with fs_token held */
809427e5fc6SMatthew Dillon 		hammer_free_hmp(mp);
810b0aab9b9SMatthew Dillon 	} else {
811b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
812b0aab9b9SMatthew Dillon 	}
813b0aab9b9SMatthew Dillon 	return(error);
814427e5fc6SMatthew Dillon }
815427e5fc6SMatthew Dillon 
816427e5fc6SMatthew Dillon /*
817427e5fc6SMatthew Dillon  * Clean up the internal mount structure and disassociate it from the mount.
818427e5fc6SMatthew Dillon  * This may issue I/O.
819b0aab9b9SMatthew Dillon  *
820b0aab9b9SMatthew Dillon  * Called with fs_token held.
821427e5fc6SMatthew Dillon  */
822427e5fc6SMatthew Dillon static void
hammer_free_hmp(struct mount * mp)823427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp)
824427e5fc6SMatthew Dillon {
825b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
826cdb6e4e6SMatthew Dillon 	hammer_flush_group_t flg;
827427e5fc6SMatthew Dillon 
828427e5fc6SMatthew Dillon 	/*
829cdb6e4e6SMatthew Dillon 	 * Flush anything dirty.  This won't even run if the
830cdb6e4e6SMatthew Dillon 	 * filesystem errored-out.
831427e5fc6SMatthew Dillon 	 */
8328bae937eSTomohiro Kusumi 	hammer_flush_dirty(hmp, 30);
833cdb6e4e6SMatthew Dillon 
834cdb6e4e6SMatthew Dillon 	/*
835cdb6e4e6SMatthew Dillon 	 * If the mount had a critical error we have to destroy any
836cdb6e4e6SMatthew Dillon 	 * remaining inodes before we can finish cleaning up the flusher.
837cdb6e4e6SMatthew Dillon 	 */
838cdb6e4e6SMatthew Dillon 	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
839cdb6e4e6SMatthew Dillon 		RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
840cdb6e4e6SMatthew Dillon 			hammer_destroy_inode_callback, NULL);
841cdb6e4e6SMatthew Dillon 	}
842cdb6e4e6SMatthew Dillon 
843cdb6e4e6SMatthew Dillon 	/*
844cdb6e4e6SMatthew Dillon 	 * There shouldn't be any inodes left now and any left over
845cdb6e4e6SMatthew Dillon 	 * flush groups should now be empty.
846cdb6e4e6SMatthew Dillon 	 */
847cdb6e4e6SMatthew Dillon 	KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
848cdb6e4e6SMatthew Dillon 	while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
849cdb6e4e6SMatthew Dillon 		TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
850ff003b11SMatthew Dillon 		KKASSERT(RB_EMPTY(&flg->flush_tree));
851cdb6e4e6SMatthew Dillon 		if (flg->refs) {
852d053aa8aSTomohiro Kusumi 			hkprintf("Warning, flush_group %p was "
853cdb6e4e6SMatthew Dillon 				"not empty on umount!\n", flg);
854cdb6e4e6SMatthew Dillon 		}
855bac808feSMatthew Dillon 		kfree(flg, hmp->m_misc);
856cdb6e4e6SMatthew Dillon 	}
857cdb6e4e6SMatthew Dillon 
858cdb6e4e6SMatthew Dillon 	/*
859cdb6e4e6SMatthew Dillon 	 * We can finally destroy the flusher
860cdb6e4e6SMatthew Dillon 	 */
861059819e3SMatthew Dillon 	hammer_flusher_destroy(hmp);
862427e5fc6SMatthew Dillon 
863b84de5afSMatthew Dillon 	/*
86400f16fadSMatthew Dillon 	 * We may have held recovered buffers due to a read-only mount.
86500f16fadSMatthew Dillon 	 * These must be discarded.
86600f16fadSMatthew Dillon 	 */
86700f16fadSMatthew Dillon 	if (hmp->ronly)
86800f16fadSMatthew Dillon 		hammer_recover_flush_buffers(hmp, NULL, -1);
86900f16fadSMatthew Dillon 
87000f16fadSMatthew Dillon 	/*
8710832c9bbSMatthew Dillon 	 * Unload buffers and then volumes
872b84de5afSMatthew Dillon 	 */
8730832c9bbSMatthew Dillon         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
8740832c9bbSMatthew Dillon 		hammer_unload_buffer, NULL);
875427e5fc6SMatthew Dillon 	RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
876427e5fc6SMatthew Dillon 		hammer_unload_volume, NULL);
877427e5fc6SMatthew Dillon 
878427e5fc6SMatthew Dillon 	mp->mnt_data = NULL;
87966325755SMatthew Dillon 	mp->mnt_flag &= ~MNT_LOCAL;
880427e5fc6SMatthew Dillon 	hmp->mp = NULL;
8810729c8c8SMatthew Dillon 	hammer_destroy_objid_cache(hmp);
882bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_misc);
883bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_inodes);
884b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
885427e5fc6SMatthew Dillon 	kfree(hmp, M_HAMMER);
886427e5fc6SMatthew Dillon }
887427e5fc6SMatthew Dillon 
888427e5fc6SMatthew Dillon /*
889cdb6e4e6SMatthew Dillon  * Report critical errors.  ip may be NULL.
890cdb6e4e6SMatthew Dillon  */
891cdb6e4e6SMatthew Dillon void
hammer_critical_error(hammer_mount_t hmp,hammer_inode_t ip,int error,const char * msg)892cdb6e4e6SMatthew Dillon hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
893cdb6e4e6SMatthew Dillon 		      int error, const char *msg)
894cdb6e4e6SMatthew Dillon {
895cdb6e4e6SMatthew Dillon 	hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
896c9ce54d6SMatthew Dillon 
897d053aa8aSTomohiro Kusumi 	hmkrateprintf(&hmp->krate, hmp,
898d053aa8aSTomohiro Kusumi 		    "Critical error inode=%jd error=%d %s\n",
899c9ce54d6SMatthew Dillon 		    (intmax_t)(ip ? ip->obj_id : -1),
900c9ce54d6SMatthew Dillon 		    error, msg);
901c9ce54d6SMatthew Dillon 
902cdb6e4e6SMatthew Dillon 	if (hmp->ronly == 0) {
903cdb6e4e6SMatthew Dillon 		hmp->ronly = 2;		/* special errored read-only mode */
904cdb6e4e6SMatthew Dillon 		hmp->mp->mnt_flag |= MNT_RDONLY;
905dfec479fSMatthew Dillon 		RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
906dfec479fSMatthew Dillon 			hammer_adjust_volume_mode, NULL);
907d053aa8aSTomohiro Kusumi 		hmkprintf(hmp, "Forcing read-only mode\n");
908cdb6e4e6SMatthew Dillon 	}
909cdb6e4e6SMatthew Dillon 	hmp->error = error;
910fc73edd8SMatthew Dillon 	if (hammer_debug_critical)
911c9ce54d6SMatthew Dillon 		Debugger("Entering debugger");
912cdb6e4e6SMatthew Dillon }
913cdb6e4e6SMatthew Dillon 
914cdb6e4e6SMatthew Dillon 
915cdb6e4e6SMatthew Dillon /*
916513ca7d7SMatthew Dillon  * Obtain a vnode for the specified inode number.  An exclusively locked
917513ca7d7SMatthew Dillon  * vnode is returned.
918513ca7d7SMatthew Dillon  */
919513ca7d7SMatthew Dillon int
hammer_vfs_vget(struct mount * mp,struct vnode * dvp,ino_t ino,struct vnode ** vpp)920b9b0a6d0SMatthew Dillon hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
921b9b0a6d0SMatthew Dillon 		ino_t ino, struct vnode **vpp)
922513ca7d7SMatthew Dillon {
92336f82b23SMatthew Dillon 	struct hammer_transaction trans;
924ba2be8e9STomohiro Kusumi 	hammer_mount_t hmp = (void *)mp->mnt_data;
925e1067862STomohiro Kusumi 	hammer_inode_t ip;
926513ca7d7SMatthew Dillon 	int error;
92746137e17STomohiro Kusumi 	uint32_t localization;
928513ca7d7SMatthew Dillon 
929b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
93036f82b23SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
93136f82b23SMatthew Dillon 
932513ca7d7SMatthew Dillon 	/*
933b9b0a6d0SMatthew Dillon 	 * If a directory vnode is supplied (mainly NFS) then we can acquire
934b9b0a6d0SMatthew Dillon 	 * the PFS domain from it.  Otherwise we would only be able to vget
935b9b0a6d0SMatthew Dillon 	 * inodes in the root PFS.
936b9b0a6d0SMatthew Dillon 	 */
937b9b0a6d0SMatthew Dillon 	if (dvp) {
9387e52af60STomohiro Kusumi 		localization = HAMMER_DEF_LOCALIZATION |
939b9b0a6d0SMatthew Dillon 				VTOI(dvp)->obj_localization;
940b9b0a6d0SMatthew Dillon 	} else {
941b9b0a6d0SMatthew Dillon 		localization = HAMMER_DEF_LOCALIZATION;
942b9b0a6d0SMatthew Dillon 	}
943b9b0a6d0SMatthew Dillon 
944b9b0a6d0SMatthew Dillon 	/*
94536f82b23SMatthew Dillon 	 * Lookup the requested HAMMER inode.  The structure must be
94636f82b23SMatthew Dillon 	 * left unlocked while we manipulate the related vnode to avoid
94736f82b23SMatthew Dillon 	 * a deadlock.
948513ca7d7SMatthew Dillon 	 */
949ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, ino,
950b9b0a6d0SMatthew Dillon 			      hmp->asof, localization,
951ddfdf542SMatthew Dillon 			      0, &error);
952513ca7d7SMatthew Dillon 	if (ip == NULL) {
953513ca7d7SMatthew Dillon 		*vpp = NULL;
954b0aab9b9SMatthew Dillon 	} else {
955e8599db1SMatthew Dillon 		error = hammer_get_vnode(ip, vpp);
956513ca7d7SMatthew Dillon 		hammer_rel_inode(ip, 0);
957b0aab9b9SMatthew Dillon 	}
958b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
959b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
960513ca7d7SMatthew Dillon 	return (error);
961513ca7d7SMatthew Dillon }
962513ca7d7SMatthew Dillon 
963513ca7d7SMatthew Dillon /*
964427e5fc6SMatthew Dillon  * Return the root vnode for the filesystem.
965427e5fc6SMatthew Dillon  *
966427e5fc6SMatthew Dillon  * HAMMER stores the root vnode in the hammer_mount structure so
967427e5fc6SMatthew Dillon  * getting it is easy.
968427e5fc6SMatthew Dillon  */
969427e5fc6SMatthew Dillon static int
hammer_vfs_root(struct mount * mp,struct vnode ** vpp)970427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp)
971427e5fc6SMatthew Dillon {
97227ea2398SMatthew Dillon 	int error;
973427e5fc6SMatthew Dillon 
974783fb170STomohiro Kusumi 	error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, vpp);
97527ea2398SMatthew Dillon 	return (error);
976427e5fc6SMatthew Dillon }
977427e5fc6SMatthew Dillon 
978427e5fc6SMatthew Dillon static int
hammer_vfs_statfs(struct mount * mp,struct statfs * sbp,struct ucred * cred)979427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
980427e5fc6SMatthew Dillon {
981ba2be8e9STomohiro Kusumi 	hammer_mount_t hmp = (void *)mp->mnt_data;
982fbc6e32aSMatthew Dillon 	hammer_volume_t volume;
983fbc6e32aSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
984fbc6e32aSMatthew Dillon 	int error;
98547197d71SMatthew Dillon 	int64_t bfree;
98631a56ce2SMatthew Dillon 	int64_t breserved;
987fbc6e32aSMatthew Dillon 
988b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
989fbc6e32aSMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
990b0aab9b9SMatthew Dillon 	if (error) {
991b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
992fbc6e32aSMatthew Dillon 		return(error);
993b0aab9b9SMatthew Dillon 	}
994fbc6e32aSMatthew Dillon 	ondisk = volume->ondisk;
995fbc6e32aSMatthew Dillon 
99647197d71SMatthew Dillon 	/*
99747197d71SMatthew Dillon 	 * Basic stats
99847197d71SMatthew Dillon 	 */
99931a56ce2SMatthew Dillon 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
1000fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
1001e04ee2deSTomohiro Kusumi 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
100240043e7fSMatthew Dillon 	hammer_rel_volume(volume, 0);
100347197d71SMatthew Dillon 
100440100248STomohiro Kusumi 	if (breserved > bfree)
100540100248STomohiro Kusumi 		breserved = bfree;
100631a56ce2SMatthew Dillon 	mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
100747197d71SMatthew Dillon 	mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1008fbc6e32aSMatthew Dillon 	if (mp->mnt_stat.f_files < 0)
1009fbc6e32aSMatthew Dillon 		mp->mnt_stat.f_files = 0;
1010fbc6e32aSMatthew Dillon 
101127ea2398SMatthew Dillon 	*sbp = mp->mnt_stat;
1012b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
101327ea2398SMatthew Dillon 	return(0);
1014427e5fc6SMatthew Dillon }
1015427e5fc6SMatthew Dillon 
10166f97fce3SMatthew Dillon static int
hammer_vfs_statvfs(struct mount * mp,struct statvfs * sbp,struct ucred * cred)10176f97fce3SMatthew Dillon hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
10186f97fce3SMatthew Dillon {
1019ba2be8e9STomohiro Kusumi 	hammer_mount_t hmp = (void *)mp->mnt_data;
10206f97fce3SMatthew Dillon 	hammer_volume_t volume;
10216f97fce3SMatthew Dillon 	hammer_volume_ondisk_t ondisk;
10226f97fce3SMatthew Dillon 	int error;
10236f97fce3SMatthew Dillon 	int64_t bfree;
10240f65be10SMatthew Dillon 	int64_t breserved;
10256f97fce3SMatthew Dillon 
1026b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
10276f97fce3SMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
1028b0aab9b9SMatthew Dillon 	if (error) {
1029b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
10306f97fce3SMatthew Dillon 		return(error);
1031b0aab9b9SMatthew Dillon 	}
10326f97fce3SMatthew Dillon 	ondisk = volume->ondisk;
10336f97fce3SMatthew Dillon 
10346f97fce3SMatthew Dillon 	/*
10356f97fce3SMatthew Dillon 	 * Basic stats
10366f97fce3SMatthew Dillon 	 */
10370f65be10SMatthew Dillon 	_hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
10386f97fce3SMatthew Dillon 	mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
1039e04ee2deSTomohiro Kusumi 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
10406f97fce3SMatthew Dillon 	hammer_rel_volume(volume, 0);
10416f97fce3SMatthew Dillon 
104240100248STomohiro Kusumi 	if (breserved > bfree)
104340100248STomohiro Kusumi 		breserved = bfree;
10440f65be10SMatthew Dillon 	mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
1045c0763659SMatthew Dillon 	mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
10466f97fce3SMatthew Dillon 	if (mp->mnt_vstat.f_files < 0)
10476f97fce3SMatthew Dillon 		mp->mnt_vstat.f_files = 0;
10486f97fce3SMatthew Dillon 	*sbp = mp->mnt_vstat;
1049b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
10506f97fce3SMatthew Dillon 	return(0);
10516f97fce3SMatthew Dillon }
10526f97fce3SMatthew Dillon 
10530729c8c8SMatthew Dillon /*
10540729c8c8SMatthew Dillon  * Sync the filesystem.  Currently we have to run it twice, the second
10550729c8c8SMatthew Dillon  * one will advance the undo start index to the end index, so if a crash
10560729c8c8SMatthew Dillon  * occurs no undos will be run on mount.
105777062c8aSMatthew Dillon  *
105877062c8aSMatthew Dillon  * We do not sync the filesystem if we are called from a panic.  If we did
105977062c8aSMatthew Dillon  * we might end up blowing up a sync that was already in progress.
10600729c8c8SMatthew Dillon  */
1061427e5fc6SMatthew Dillon static int
hammer_vfs_sync(struct mount * mp,int waitfor)1062427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor)
1063427e5fc6SMatthew Dillon {
1064ba2be8e9STomohiro Kusumi 	hammer_mount_t hmp = (void *)mp->mnt_data;
10650729c8c8SMatthew Dillon 	int error;
10660729c8c8SMatthew Dillon 
1067b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
106877062c8aSMatthew Dillon 	if (panicstr == NULL) {
10690729c8c8SMatthew Dillon 		error = hammer_sync_hmp(hmp, waitfor);
107077062c8aSMatthew Dillon 	} else {
107177062c8aSMatthew Dillon 		error = EIO;
107277062c8aSMatthew Dillon 	}
1073b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
10740729c8c8SMatthew Dillon 	return (error);
1075427e5fc6SMatthew Dillon }
1076427e5fc6SMatthew Dillon 
1077513ca7d7SMatthew Dillon /*
1078513ca7d7SMatthew Dillon  * Convert a vnode to a file handle.
1079b0aab9b9SMatthew Dillon  *
1080b0aab9b9SMatthew Dillon  * Accesses read-only fields on already-referenced structures so
1081b0aab9b9SMatthew Dillon  * no token is needed.
1082513ca7d7SMatthew Dillon  */
1083513ca7d7SMatthew Dillon static int
hammer_vfs_vptofh(struct vnode * vp,struct fid * fhp)1084513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1085513ca7d7SMatthew Dillon {
1086513ca7d7SMatthew Dillon 	hammer_inode_t ip;
1087513ca7d7SMatthew Dillon 
1088513ca7d7SMatthew Dillon 	KKASSERT(MAXFIDSZ >= 16);
1089513ca7d7SMatthew Dillon 	ip = VTOI(vp);
1090513ca7d7SMatthew Dillon 	fhp->fid_len = offsetof(struct fid, fid_data[16]);
109120cf2291STomohiro Kusumi 	fhp->fid_ext = lo_to_pfs(ip->obj_localization);
1092513ca7d7SMatthew Dillon 	bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
1093513ca7d7SMatthew Dillon 	bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
1094513ca7d7SMatthew Dillon 	return(0);
1095513ca7d7SMatthew Dillon }
1096513ca7d7SMatthew Dillon 
1097513ca7d7SMatthew Dillon 
1098513ca7d7SMatthew Dillon /*
1099513ca7d7SMatthew Dillon  * Convert a file handle back to a vnode.
110067863d04SMatthew Dillon  *
110167863d04SMatthew Dillon  * Use rootvp to enforce PFS isolation when a PFS is exported via a
110267863d04SMatthew Dillon  * null mount.
1103513ca7d7SMatthew Dillon  */
1104513ca7d7SMatthew Dillon static int
hammer_vfs_fhtovp(struct mount * mp,struct vnode * rootvp,struct fid * fhp,struct vnode ** vpp)110567863d04SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
110667863d04SMatthew Dillon 		  struct fid *fhp, struct vnode **vpp)
1107513ca7d7SMatthew Dillon {
1108b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
110936f82b23SMatthew Dillon 	struct hammer_transaction trans;
1110e1067862STomohiro Kusumi 	hammer_inode_t ip;
1111513ca7d7SMatthew Dillon 	struct hammer_inode_info info;
1112513ca7d7SMatthew Dillon 	int error;
111346137e17STomohiro Kusumi 	uint32_t localization;
1114513ca7d7SMatthew Dillon 
1115513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
1116513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
111767863d04SMatthew Dillon 	if (rootvp)
111867863d04SMatthew Dillon 		localization = VTOI(rootvp)->obj_localization;
111967863d04SMatthew Dillon 	else
112020cf2291STomohiro Kusumi 		localization = pfs_to_lo(fhp->fid_ext);
1121513ca7d7SMatthew Dillon 
1122b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1123b0aab9b9SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
112436f82b23SMatthew Dillon 
1125513ca7d7SMatthew Dillon 	/*
1126513ca7d7SMatthew Dillon 	 * Get/allocate the hammer_inode structure.  The structure must be
1127513ca7d7SMatthew Dillon 	 * unlocked while we manipulate the related vnode to avoid a
1128513ca7d7SMatthew Dillon 	 * deadlock.
1129513ca7d7SMatthew Dillon 	 */
1130ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, info.obj_id,
1131adf01747SMatthew Dillon 			      info.obj_asof, localization, 0, &error);
1132d978e7cfSMatthew Dillon 	if (ip) {
1133e8599db1SMatthew Dillon 		error = hammer_get_vnode(ip, vpp);
1134513ca7d7SMatthew Dillon 		hammer_rel_inode(ip, 0);
1135d978e7cfSMatthew Dillon 	} else {
1136d978e7cfSMatthew Dillon 		*vpp = NULL;
1137d978e7cfSMatthew Dillon 	}
1138b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
1139b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1140513ca7d7SMatthew Dillon 	return (error);
1141513ca7d7SMatthew Dillon }
1142513ca7d7SMatthew Dillon 
1143513ca7d7SMatthew Dillon static int
hammer_vfs_checkexp(struct mount * mp,struct sockaddr * nam,int * exflagsp,struct ucred ** credanonp)1144513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1145513ca7d7SMatthew Dillon 		    int *exflagsp, struct ucred **credanonp)
1146513ca7d7SMatthew Dillon {
1147513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
1148513ca7d7SMatthew Dillon 	struct netcred *np;
1149513ca7d7SMatthew Dillon 	int error;
1150513ca7d7SMatthew Dillon 
1151b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1152513ca7d7SMatthew Dillon 	np = vfs_export_lookup(mp, &hmp->export, nam);
1153513ca7d7SMatthew Dillon 	if (np) {
1154513ca7d7SMatthew Dillon 		*exflagsp = np->netc_exflags;
1155513ca7d7SMatthew Dillon 		*credanonp = &np->netc_anon;
1156513ca7d7SMatthew Dillon 		error = 0;
1157513ca7d7SMatthew Dillon 	} else {
1158513ca7d7SMatthew Dillon 		error = EACCES;
1159513ca7d7SMatthew Dillon 	}
1160b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1161513ca7d7SMatthew Dillon 	return (error);
1162513ca7d7SMatthew Dillon 
1163513ca7d7SMatthew Dillon }
1164513ca7d7SMatthew Dillon 
1165513ca7d7SMatthew Dillon int
hammer_vfs_export(struct mount * mp,int op,const struct export_args * export)1166513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
1167513ca7d7SMatthew Dillon {
1168513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
1169513ca7d7SMatthew Dillon 	int error;
1170513ca7d7SMatthew Dillon 
1171b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->fs_token);
1172b0aab9b9SMatthew Dillon 
1173513ca7d7SMatthew Dillon 	switch(op) {
1174513ca7d7SMatthew Dillon 	case MOUNTCTL_SET_EXPORT:
1175513ca7d7SMatthew Dillon 		error = vfs_export(mp, &hmp->export, export);
1176513ca7d7SMatthew Dillon 		break;
1177513ca7d7SMatthew Dillon 	default:
1178513ca7d7SMatthew Dillon 		error = EOPNOTSUPP;
1179513ca7d7SMatthew Dillon 		break;
1180513ca7d7SMatthew Dillon 	}
1181b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->fs_token);
1182b0aab9b9SMatthew Dillon 
1183513ca7d7SMatthew Dillon 	return(error);
1184513ca7d7SMatthew Dillon }
1185513ca7d7SMatthew Dillon 
1186