xref: /dflybsd-src/sys/vfs/hammer/hammer_vfsops.c (revision 39d8fd636004057de2f4039d0c527e8a5194fb08)
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  *
3444a83111SMatthew Dillon  * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.74 2008/11/13 02:18:43 dillon Exp $
35427e5fc6SMatthew Dillon  */
36427e5fc6SMatthew Dillon 
37427e5fc6SMatthew Dillon #include <sys/param.h>
38427e5fc6SMatthew Dillon #include <sys/systm.h>
39427e5fc6SMatthew Dillon #include <sys/kernel.h>
40427e5fc6SMatthew Dillon #include <sys/vnode.h>
41427e5fc6SMatthew Dillon #include <sys/mount.h>
42427e5fc6SMatthew Dillon #include <sys/malloc.h>
43427e5fc6SMatthew Dillon #include <sys/nlookup.h>
44427e5fc6SMatthew Dillon #include <sys/fcntl.h>
45b3deaf57SMatthew Dillon #include <sys/sysctl.h>
46427e5fc6SMatthew Dillon #include <sys/buf.h>
47427e5fc6SMatthew Dillon #include <sys/buf2.h>
48427e5fc6SMatthew Dillon #include "hammer.h"
49427e5fc6SMatthew Dillon 
502f85fa4dSMatthew Dillon int hammer_debug_io;
51d5ef456eSMatthew Dillon int hammer_debug_general;
521b0ab2c3SMatthew Dillon int hammer_debug_debug = 1;		/* medium-error panics */
53e8599db1SMatthew Dillon int hammer_debug_inode;
547d683b0fSMatthew Dillon int hammer_debug_locks;
55b3deaf57SMatthew Dillon int hammer_debug_btree;
56d113fda1SMatthew Dillon int hammer_debug_tid;
5746fe7ae1SMatthew Dillon int hammer_debug_recover;		/* -1 will disable, +1 will force */
5846fe7ae1SMatthew Dillon int hammer_debug_recover_faults;
591b0ab2c3SMatthew Dillon int hammer_cluster_enable = 1;		/* enable read clustering by default */
607a61b85dSMatthew Dillon int hammer_count_fsyncs;
61b3deaf57SMatthew Dillon int hammer_count_inodes;
62af209b0fSMatthew Dillon int hammer_count_iqueued;
639f5097dcSMatthew Dillon int hammer_count_reclaiming;
64b3deaf57SMatthew Dillon int hammer_count_records;
65b3deaf57SMatthew Dillon int hammer_count_record_datas;
66b3deaf57SMatthew Dillon int hammer_count_volumes;
67b3deaf57SMatthew Dillon int hammer_count_buffers;
68b3deaf57SMatthew Dillon int hammer_count_nodes;
69a7e9bef1SMatthew Dillon int64_t hammer_count_extra_space_used;
70cb51be26SMatthew Dillon int64_t hammer_stats_btree_lookups;
71cb51be26SMatthew Dillon int64_t hammer_stats_btree_searches;
72cb51be26SMatthew Dillon int64_t hammer_stats_btree_inserts;
73cb51be26SMatthew Dillon int64_t hammer_stats_btree_deletes;
74cb51be26SMatthew Dillon int64_t hammer_stats_btree_elements;
75cb51be26SMatthew Dillon int64_t hammer_stats_btree_splits;
76cb51be26SMatthew Dillon int64_t hammer_stats_btree_iterations;
77*39d8fd63SMatthew Dillon int64_t hammer_stats_btree_root_iterations;
78cb51be26SMatthew Dillon int64_t hammer_stats_record_iterations;
79ce0138a6SMatthew Dillon 
80ce0138a6SMatthew Dillon int64_t hammer_stats_file_read;
81ce0138a6SMatthew Dillon int64_t hammer_stats_file_write;
82ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsr;
83ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsw;
84ce0138a6SMatthew Dillon int64_t hammer_stats_disk_read;
85ce0138a6SMatthew Dillon int64_t hammer_stats_disk_write;
86ce0138a6SMatthew Dillon int64_t hammer_stats_inode_flushes;
87ce0138a6SMatthew Dillon int64_t hammer_stats_commits;
88ce0138a6SMatthew Dillon 
89f5a07a7aSMatthew Dillon int hammer_count_dirtybufspace;		/* global */
90a99b9ea2SMatthew Dillon int hammer_count_refedbufs;		/* global */
910832c9bbSMatthew Dillon int hammer_count_reservations;
92a99b9ea2SMatthew Dillon int hammer_count_io_running_read;
93a99b9ea2SMatthew Dillon int hammer_count_io_running_write;
94a99b9ea2SMatthew Dillon int hammer_count_io_locked;
95f5a07a7aSMatthew Dillon int hammer_limit_dirtybufspace;		/* per-mount */
960832c9bbSMatthew Dillon int hammer_limit_recs;			/* as a whole XXX */
9721fde338SMatthew Dillon int hammer_autoflush = 2000;		/* auto flush */
981f07f686SMatthew Dillon int hammer_bio_count;
99cb51be26SMatthew Dillon int hammer_verify_zone;
1001b0ab2c3SMatthew Dillon int hammer_verify_data = 1;
101cb51be26SMatthew Dillon int hammer_write_mode;
1027d683b0fSMatthew Dillon int64_t hammer_contention_count;
103f03c9cf4SMatthew Dillon int64_t hammer_zone_limit;
104b3deaf57SMatthew Dillon 
105b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
106d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
107d5ef456eSMatthew Dillon 	   &hammer_debug_general, 0, "");
1082f85fa4dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
1092f85fa4dSMatthew Dillon 	   &hammer_debug_io, 0, "");
11077062c8aSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
11177062c8aSMatthew Dillon 	   &hammer_debug_debug, 0, "");
112e8599db1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
113e8599db1SMatthew Dillon 	   &hammer_debug_inode, 0, "");
1147d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
1157d683b0fSMatthew Dillon 	   &hammer_debug_locks, 0, "");
116b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
117b3deaf57SMatthew Dillon 	   &hammer_debug_btree, 0, "");
118d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
119d113fda1SMatthew Dillon 	   &hammer_debug_tid, 0, "");
120b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
121b33e2cc0SMatthew Dillon 	   &hammer_debug_recover, 0, "");
12246fe7ae1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
12346fe7ae1SMatthew Dillon 	   &hammer_debug_recover_faults, 0, "");
1241b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
1251b0ab2c3SMatthew Dillon 	   &hammer_cluster_enable, 0, "");
1269480ff55SMatthew Dillon 
127f5a07a7aSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
128f5a07a7aSMatthew Dillon 	   &hammer_limit_dirtybufspace, 0, "");
12947637bffSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
13047637bffSMatthew Dillon 	   &hammer_limit_recs, 0, "");
1319480ff55SMatthew Dillon 
1327a61b85dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
1337a61b85dSMatthew Dillon 	   &hammer_count_fsyncs, 0, "");
134b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
135b3deaf57SMatthew Dillon 	   &hammer_count_inodes, 0, "");
136af209b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
137af209b0fSMatthew Dillon 	   &hammer_count_iqueued, 0, "");
1389f5097dcSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD,
1399f5097dcSMatthew Dillon 	   &hammer_count_reclaiming, 0, "");
140b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
141b3deaf57SMatthew Dillon 	   &hammer_count_records, 0, "");
142b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
143b3deaf57SMatthew Dillon 	   &hammer_count_record_datas, 0, "");
144b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
145b3deaf57SMatthew Dillon 	   &hammer_count_volumes, 0, "");
146b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
147b3deaf57SMatthew Dillon 	   &hammer_count_buffers, 0, "");
148b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
149b3deaf57SMatthew Dillon 	   &hammer_count_nodes, 0, "");
150a7e9bef1SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD,
151a7e9bef1SMatthew Dillon 	   &hammer_count_extra_space_used, 0, "");
152ce0138a6SMatthew Dillon 
153cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
154cb51be26SMatthew Dillon 	   &hammer_stats_btree_searches, 0, "");
155cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
156cb51be26SMatthew Dillon 	   &hammer_stats_btree_lookups, 0, "");
157cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
158cb51be26SMatthew Dillon 	   &hammer_stats_btree_inserts, 0, "");
159cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
160cb51be26SMatthew Dillon 	   &hammer_stats_btree_deletes, 0, "");
161cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
162cb51be26SMatthew Dillon 	   &hammer_stats_btree_elements, 0, "");
163cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
164cb51be26SMatthew Dillon 	   &hammer_stats_btree_splits, 0, "");
165cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
166cb51be26SMatthew Dillon 	   &hammer_stats_btree_iterations, 0, "");
167*39d8fd63SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD,
168*39d8fd63SMatthew Dillon 	   &hammer_stats_btree_root_iterations, 0, "");
169cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
170cb51be26SMatthew Dillon 	   &hammer_stats_record_iterations, 0, "");
171ce0138a6SMatthew Dillon 
172ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
173ce0138a6SMatthew Dillon 	   &hammer_stats_file_read, 0, "");
174ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
175ce0138a6SMatthew Dillon 	   &hammer_stats_file_write, 0, "");
176ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD,
177ce0138a6SMatthew Dillon 	   &hammer_stats_file_iopsr, 0, "");
178ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD,
179ce0138a6SMatthew Dillon 	   &hammer_stats_file_iopsw, 0, "");
180ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
181ce0138a6SMatthew Dillon 	   &hammer_stats_disk_read, 0, "");
182ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
183ce0138a6SMatthew Dillon 	   &hammer_stats_disk_write, 0, "");
184ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
185ce0138a6SMatthew Dillon 	   &hammer_stats_inode_flushes, 0, "");
186ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
187ce0138a6SMatthew Dillon 	   &hammer_stats_commits, 0, "");
188ce0138a6SMatthew Dillon 
189f5a07a7aSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
190f5a07a7aSMatthew Dillon 	   &hammer_count_dirtybufspace, 0, "");
191a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
192a99b9ea2SMatthew Dillon 	   &hammer_count_refedbufs, 0, "");
1930832c9bbSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
1940832c9bbSMatthew Dillon 	   &hammer_count_reservations, 0, "");
195a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
196a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_read, 0, "");
197a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
198a99b9ea2SMatthew Dillon 	   &hammer_count_io_locked, 0, "");
199a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
200a99b9ea2SMatthew Dillon 	   &hammer_count_io_running_write, 0, "");
201f03c9cf4SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
202f03c9cf4SMatthew Dillon 	   &hammer_zone_limit, 0, "");
2037d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
2047d683b0fSMatthew Dillon 	   &hammer_contention_count, 0, "");
20521fde338SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
20621fde338SMatthew Dillon 	   &hammer_autoflush, 0, "");
207cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
208cb51be26SMatthew Dillon 	   &hammer_verify_zone, 0, "");
2091b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
2101b0ab2c3SMatthew Dillon 	   &hammer_verify_data, 0, "");
211cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
212cb51be26SMatthew Dillon 	   &hammer_write_mode, 0, "");
213b3deaf57SMatthew Dillon 
214bcac4bbbSMatthew Dillon KTR_INFO_MASTER(hammer);
215bcac4bbbSMatthew Dillon 
216427e5fc6SMatthew Dillon /*
217427e5fc6SMatthew Dillon  * VFS ABI
218427e5fc6SMatthew Dillon  */
219427e5fc6SMatthew Dillon static void	hammer_free_hmp(struct mount *mp);
220427e5fc6SMatthew Dillon 
221427e5fc6SMatthew Dillon static int	hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
222427e5fc6SMatthew Dillon 				struct ucred *cred);
223427e5fc6SMatthew Dillon static int	hammer_vfs_unmount(struct mount *mp, int mntflags);
224427e5fc6SMatthew Dillon static int	hammer_vfs_root(struct mount *mp, struct vnode **vpp);
225427e5fc6SMatthew Dillon static int	hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
226427e5fc6SMatthew Dillon 				struct ucred *cred);
2276f97fce3SMatthew Dillon static int	hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
2286f97fce3SMatthew Dillon 				struct ucred *cred);
229427e5fc6SMatthew Dillon static int	hammer_vfs_sync(struct mount *mp, int waitfor);
230513ca7d7SMatthew Dillon static int	hammer_vfs_vget(struct mount *mp, ino_t ino,
231513ca7d7SMatthew Dillon 				struct vnode **vpp);
232427e5fc6SMatthew Dillon static int	hammer_vfs_init(struct vfsconf *conf);
23367863d04SMatthew Dillon static int	hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
23467863d04SMatthew Dillon 				struct fid *fhp, struct vnode **vpp);
235513ca7d7SMatthew Dillon static int	hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
236513ca7d7SMatthew Dillon static int	hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
237513ca7d7SMatthew Dillon 				int *exflagsp, struct ucred **credanonp);
238513ca7d7SMatthew Dillon 
239427e5fc6SMatthew Dillon 
240427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = {
241427e5fc6SMatthew Dillon 	.vfs_mount	= hammer_vfs_mount,
242427e5fc6SMatthew Dillon 	.vfs_unmount	= hammer_vfs_unmount,
243427e5fc6SMatthew Dillon 	.vfs_root 	= hammer_vfs_root,
244427e5fc6SMatthew Dillon 	.vfs_statfs	= hammer_vfs_statfs,
2456f97fce3SMatthew Dillon 	.vfs_statvfs	= hammer_vfs_statvfs,
246427e5fc6SMatthew Dillon 	.vfs_sync	= hammer_vfs_sync,
247427e5fc6SMatthew Dillon 	.vfs_vget	= hammer_vfs_vget,
248513ca7d7SMatthew Dillon 	.vfs_init	= hammer_vfs_init,
249513ca7d7SMatthew Dillon 	.vfs_vptofh	= hammer_vfs_vptofh,
250513ca7d7SMatthew Dillon 	.vfs_fhtovp	= hammer_vfs_fhtovp,
251513ca7d7SMatthew Dillon 	.vfs_checkexp	= hammer_vfs_checkexp
252427e5fc6SMatthew Dillon };
253427e5fc6SMatthew Dillon 
254bac808feSMatthew Dillon MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", "");
255427e5fc6SMatthew Dillon 
256427e5fc6SMatthew Dillon VFS_SET(hammer_vfsops, hammer, 0);
257427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1);
258427e5fc6SMatthew Dillon 
259427e5fc6SMatthew Dillon static int
260427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf)
261427e5fc6SMatthew Dillon {
2623098dc2fSMatthew Dillon 	int n;
2633098dc2fSMatthew Dillon 
2643098dc2fSMatthew Dillon 	if (hammer_limit_recs == 0) {
265a99b9ea2SMatthew Dillon 		hammer_limit_recs = nbuf * 25;
2663098dc2fSMatthew Dillon 		n = kmalloc_limit(M_HAMMER) / 512;
2673098dc2fSMatthew Dillon 		if (hammer_limit_recs > n)
2683098dc2fSMatthew Dillon 			hammer_limit_recs = n;
2693098dc2fSMatthew Dillon 	}
270f5a07a7aSMatthew Dillon 	if (hammer_limit_dirtybufspace == 0) {
271f5a07a7aSMatthew Dillon 		hammer_limit_dirtybufspace = hidirtybufspace / 2;
272f5a07a7aSMatthew Dillon 		if (hammer_limit_dirtybufspace < 100)
273f5a07a7aSMatthew Dillon 			hammer_limit_dirtybufspace = 100;
2749f5097dcSMatthew Dillon 	}
275427e5fc6SMatthew Dillon 	return(0);
276427e5fc6SMatthew Dillon }
277427e5fc6SMatthew Dillon 
278427e5fc6SMatthew Dillon static int
279427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
280427e5fc6SMatthew Dillon 		 struct ucred *cred)
281427e5fc6SMatthew Dillon {
282427e5fc6SMatthew Dillon 	struct hammer_mount_info info;
283a89aec1bSMatthew Dillon 	hammer_mount_t hmp;
284a89aec1bSMatthew Dillon 	hammer_volume_t rootvol;
28527ea2398SMatthew Dillon 	struct vnode *rootvp;
2867c19b529SMichael Neumann 	struct vnode *devvp = NULL;
287427e5fc6SMatthew Dillon 	const char *upath;	/* volume name in userspace */
288427e5fc6SMatthew Dillon 	char *path;		/* volume name in system space */
289427e5fc6SMatthew Dillon 	int error;
290427e5fc6SMatthew Dillon 	int i;
291732a1697SMatthew Dillon 	int master_id;
292ab0bf4a3SMatthew Dillon 	int maxinodes;
293ab0bf4a3SMatthew Dillon 
294ab0bf4a3SMatthew Dillon 	/*
295ab0bf4a3SMatthew Dillon 	 * Accept hammer_mount_info.  mntpt is NULL for root mounts at boot.
296ab0bf4a3SMatthew Dillon 	 */
297ab0bf4a3SMatthew Dillon 	if (mntpt == NULL) {
2987c19b529SMichael Neumann 		if ((error = bdevvp(rootdev, &devvp))) {
2997c19b529SMichael Neumann 			kprintf("hammer_mountroot: can't find devvp\n");
3007c19b529SMichael Neumann 			return (error);
3017c19b529SMichael Neumann 		}
3027c19b529SMichael Neumann 		mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
3037c19b529SMichael Neumann 		bzero(&info, sizeof(info));
3047c19b529SMichael Neumann 		info.asof = 0;
3057c19b529SMichael Neumann 		info.hflags = 0;
3067c19b529SMichael Neumann 		info.nvolumes = 1;
3077c19b529SMichael Neumann 	} else {
308427e5fc6SMatthew Dillon 		if ((error = copyin(data, &info, sizeof(info))) != 0)
309427e5fc6SMatthew Dillon 			return (error);
3107c19b529SMichael Neumann 	}
311427e5fc6SMatthew Dillon 
312427e5fc6SMatthew Dillon 	/*
313732a1697SMatthew Dillon 	 * updating or new mount
314427e5fc6SMatthew Dillon 	 */
315195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
316195c19a1SMatthew Dillon 		hmp = (void *)mp->mnt_data;
317195c19a1SMatthew Dillon 		KKASSERT(hmp != NULL);
318195c19a1SMatthew Dillon 	} else {
319732a1697SMatthew Dillon 		if (info.nvolumes <= 0 || info.nvolumes >= 32768)
320732a1697SMatthew Dillon 			return (EINVAL);
321732a1697SMatthew Dillon 		hmp = NULL;
322732a1697SMatthew Dillon 	}
323732a1697SMatthew Dillon 
324732a1697SMatthew Dillon 	/*
325732a1697SMatthew Dillon 	 * master-id validation.  The master id may not be changed by a
326732a1697SMatthew Dillon 	 * mount update.
327732a1697SMatthew Dillon 	 */
328732a1697SMatthew Dillon 	if (info.hflags & HMNT_MASTERID) {
329732a1697SMatthew Dillon 		if (hmp && hmp->master_id != info.master_id) {
330732a1697SMatthew Dillon 			kprintf("hammer: cannot change master id "
331732a1697SMatthew Dillon 				"with mount update\n");
332732a1697SMatthew Dillon 			return(EINVAL);
333732a1697SMatthew Dillon 		}
334732a1697SMatthew Dillon 		master_id = info.master_id;
335732a1697SMatthew Dillon 		if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
336732a1697SMatthew Dillon 			return (EINVAL);
337732a1697SMatthew Dillon 	} else {
338732a1697SMatthew Dillon 		if (hmp)
339732a1697SMatthew Dillon 			master_id = hmp->master_id;
340732a1697SMatthew Dillon 		else
341732a1697SMatthew Dillon 			master_id = 0;
342732a1697SMatthew Dillon 	}
343732a1697SMatthew Dillon 
344732a1697SMatthew Dillon 	/*
345732a1697SMatthew Dillon 	 * Interal mount data structure
346732a1697SMatthew Dillon 	 */
347732a1697SMatthew Dillon 	if (hmp == NULL) {
348427e5fc6SMatthew Dillon 		hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
349427e5fc6SMatthew Dillon 		mp->mnt_data = (qaddr_t)hmp;
350427e5fc6SMatthew Dillon 		hmp->mp = mp;
35146fe7ae1SMatthew Dillon 		/*TAILQ_INIT(&hmp->recycle_list);*/
35247197d71SMatthew Dillon 
353bac808feSMatthew Dillon 		/*
354bac808feSMatthew Dillon 		 * Make sure kmalloc type limits are set appropriately.  If root
355bac808feSMatthew Dillon 		 * increases the vnode limit you may have to do a dummy remount
356bac808feSMatthew Dillon 		 * to adjust the HAMMER inode limit.
357bac808feSMatthew Dillon 		 */
358bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_misc, "HAMMER-others");
359bac808feSMatthew Dillon 		kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
360bac808feSMatthew Dillon 
361bac808feSMatthew Dillon 		maxinodes = desiredvnodes + desiredvnodes / 5 +
362bac808feSMatthew Dillon 			    HAMMER_RECLAIM_WAIT;
363bac808feSMatthew Dillon 		kmalloc_raise_limit(hmp->m_inodes,
364bac808feSMatthew Dillon 				    maxinodes * sizeof(struct hammer_inode));
365bac808feSMatthew Dillon 
366dd94f1b1SMatthew Dillon 		hmp->root_btree_beg.localization = 0x00000000U;
36747197d71SMatthew Dillon 		hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
36847197d71SMatthew Dillon 		hmp->root_btree_beg.key = -0x8000000000000000LL;
36947197d71SMatthew Dillon 		hmp->root_btree_beg.create_tid = 1;
37047197d71SMatthew Dillon 		hmp->root_btree_beg.delete_tid = 1;
37147197d71SMatthew Dillon 		hmp->root_btree_beg.rec_type = 0;
37247197d71SMatthew Dillon 		hmp->root_btree_beg.obj_type = 0;
37347197d71SMatthew Dillon 
374dd94f1b1SMatthew Dillon 		hmp->root_btree_end.localization = 0xFFFFFFFFU;
37547197d71SMatthew Dillon 		hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
37647197d71SMatthew Dillon 		hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
37747197d71SMatthew Dillon 		hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
37847197d71SMatthew Dillon 		hmp->root_btree_end.delete_tid = 0;   /* special case */
37947197d71SMatthew Dillon 		hmp->root_btree_end.rec_type = 0xFFFFU;
38047197d71SMatthew Dillon 		hmp->root_btree_end.obj_type = 0;
381f03c9cf4SMatthew Dillon 
382cdb6e4e6SMatthew Dillon 		hmp->krate.freq = 1;	/* maximum reporting rate (hz) */
383cdb6e4e6SMatthew Dillon 		hmp->krate.count = -16;	/* initial burst */
384cdb6e4e6SMatthew Dillon 
3859480ff55SMatthew Dillon 		hmp->sync_lock.refs = 1;
386c9b9e29dSMatthew Dillon 		hmp->free_lock.refs = 1;
387d99d6bf5SMatthew Dillon 		hmp->undo_lock.refs = 1;
388d99d6bf5SMatthew Dillon 		hmp->blkmap_lock.refs = 1;
3899480ff55SMatthew Dillon 
390cebe9493SMatthew Dillon 		TAILQ_INIT(&hmp->delay_list);
3917a61b85dSMatthew Dillon 		TAILQ_INIT(&hmp->flush_group_list);
3920729c8c8SMatthew Dillon 		TAILQ_INIT(&hmp->objid_cache_list);
393e8599db1SMatthew Dillon 		TAILQ_INIT(&hmp->undo_lru_list);
3947bc5b8c2SMatthew Dillon 		TAILQ_INIT(&hmp->reclaim_list);
395195c19a1SMatthew Dillon 	}
39651c35492SMatthew Dillon 	hmp->hflags &= ~HMNT_USERFLAGS;
39751c35492SMatthew Dillon 	hmp->hflags |= info.hflags & HMNT_USERFLAGS;
398732a1697SMatthew Dillon 
399732a1697SMatthew Dillon 	hmp->master_id = master_id;
400732a1697SMatthew Dillon 
4017f7c1f84SMatthew Dillon 	if (info.asof) {
4027f7c1f84SMatthew Dillon 		mp->mnt_flag |= MNT_RDONLY;
4037f7c1f84SMatthew Dillon 		hmp->asof = info.asof;
4047f7c1f84SMatthew Dillon 	} else {
4057f7c1f84SMatthew Dillon 		hmp->asof = HAMMER_MAX_TID;
4067f7c1f84SMatthew Dillon 	}
407195c19a1SMatthew Dillon 
408195c19a1SMatthew Dillon 	/*
40951c35492SMatthew Dillon 	 * Re-open read-write if originally read-only, or vise-versa.
410195c19a1SMatthew Dillon 	 */
411195c19a1SMatthew Dillon 	if (mp->mnt_flag & MNT_UPDATE) {
41251c35492SMatthew Dillon 		error = 0;
41351c35492SMatthew Dillon 		if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
41451c35492SMatthew Dillon 			kprintf("HAMMER read-only -> read-write\n");
415195c19a1SMatthew Dillon 			hmp->ronly = 0;
41651c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
41751c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
41851c35492SMatthew Dillon 			rootvol = hammer_get_root_volume(hmp, &error);
41951c35492SMatthew Dillon 			if (rootvol) {
42006ad81ffSMatthew Dillon 				hammer_recover_flush_buffers(hmp, rootvol, 1);
4219f5097dcSMatthew Dillon 				bcopy(rootvol->ondisk->vol0_blockmap,
4229f5097dcSMatthew Dillon 				      hmp->blockmap,
4239f5097dcSMatthew Dillon 				      sizeof(hmp->blockmap));
42451c35492SMatthew Dillon 				hammer_rel_volume(rootvol, 0);
425195c19a1SMatthew Dillon 			}
42651c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
42751c35492SMatthew Dillon 				hammer_reload_inode, NULL);
42851c35492SMatthew Dillon 			/* kernel clears MNT_RDONLY */
42951c35492SMatthew Dillon 		} else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
43051c35492SMatthew Dillon 			kprintf("HAMMER read-write -> read-only\n");
43151c35492SMatthew Dillon 			hmp->ronly = 1;	/* messy */
43251c35492SMatthew Dillon 			RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
43351c35492SMatthew Dillon 				hammer_reload_inode, NULL);
43451c35492SMatthew Dillon 			hmp->ronly = 0;
43551c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
43651c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
43751c35492SMatthew Dillon 			hammer_flusher_sync(hmp);
43851c35492SMatthew Dillon 			hmp->ronly = 1;
43951c35492SMatthew Dillon 			RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
44051c35492SMatthew Dillon 				hammer_adjust_volume_mode, NULL);
44151c35492SMatthew Dillon 		}
44251c35492SMatthew Dillon 		return(error);
443195c19a1SMatthew Dillon 	}
444195c19a1SMatthew Dillon 
445427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_vols_root);
446427e5fc6SMatthew Dillon 	RB_INIT(&hmp->rb_inos_root);
44740043e7fSMatthew Dillon 	RB_INIT(&hmp->rb_nods_root);
448e8599db1SMatthew Dillon 	RB_INIT(&hmp->rb_undo_root);
4490832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_resv_root);
4500832c9bbSMatthew Dillon 	RB_INIT(&hmp->rb_bufs_root);
4515fa5c92fSMatthew Dillon 	RB_INIT(&hmp->rb_pfsm_root);
4520832c9bbSMatthew Dillon 
453195c19a1SMatthew Dillon 	hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
454427e5fc6SMatthew Dillon 
45510a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->volu_list);
45610a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->undo_list);
45710a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->data_list);
45810a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->meta_list);
45910a5d1baSMatthew Dillon 	TAILQ_INIT(&hmp->lose_list);
46010a5d1baSMatthew Dillon 
461427e5fc6SMatthew Dillon 	/*
462427e5fc6SMatthew Dillon 	 * Load volumes
463427e5fc6SMatthew Dillon 	 */
464427e5fc6SMatthew Dillon 	path = objcache_get(namei_oc, M_WAITOK);
4651b0ab2c3SMatthew Dillon 	hmp->nvolumes = -1;
466427e5fc6SMatthew Dillon 	for (i = 0; i < info.nvolumes; ++i) {
4677c19b529SMichael Neumann 		if (mntpt == NULL) {
4687c19b529SMichael Neumann 			/*
4697c19b529SMichael Neumann 			 * Root mount.
4707c19b529SMichael Neumann 			 * Only one volume; and no need for copyin.
4717c19b529SMichael Neumann 			 */
4727c19b529SMichael Neumann 			KKASSERT(info.nvolumes == 1);
4737c19b529SMichael Neumann 			ksnprintf(path, MAXPATHLEN, "/dev/%s",
4747c19b529SMichael Neumann 				  mp->mnt_stat.f_mntfromname);
4757c19b529SMichael Neumann 			error = 0;
4767c19b529SMichael Neumann 		} else {
4777c19b529SMichael Neumann 			error = copyin(&info.volumes[i], &upath,
4787c19b529SMichael Neumann 				       sizeof(char *));
479427e5fc6SMatthew Dillon 			if (error == 0)
4807c19b529SMichael Neumann 				error = copyinstr(upath, path,
4817c19b529SMichael Neumann 						  MAXPATHLEN, NULL);
4827c19b529SMichael Neumann 		}
483427e5fc6SMatthew Dillon 		if (error == 0)
4847c19b529SMichael Neumann 			error = hammer_install_volume(hmp, path, devvp);
485427e5fc6SMatthew Dillon 		if (error)
486427e5fc6SMatthew Dillon 			break;
487427e5fc6SMatthew Dillon 	}
488427e5fc6SMatthew Dillon 	objcache_put(namei_oc, path);
489427e5fc6SMatthew Dillon 
490427e5fc6SMatthew Dillon 	/*
491427e5fc6SMatthew Dillon 	 * Make sure we found a root volume
492427e5fc6SMatthew Dillon 	 */
493427e5fc6SMatthew Dillon 	if (error == 0 && hmp->rootvol == NULL) {
494427e5fc6SMatthew Dillon 		kprintf("hammer_mount: No root volume found!\n");
495427e5fc6SMatthew Dillon 		error = EINVAL;
496427e5fc6SMatthew Dillon 	}
4971b0ab2c3SMatthew Dillon 
4981b0ab2c3SMatthew Dillon 	/*
4991b0ab2c3SMatthew Dillon 	 * Check that all required volumes are available
5001b0ab2c3SMatthew Dillon 	 */
5011b0ab2c3SMatthew Dillon 	if (error == 0 && hammer_mountcheck_volumes(hmp)) {
5021b0ab2c3SMatthew Dillon 		kprintf("hammer_mount: Missing volumes, cannot mount!\n");
5031b0ab2c3SMatthew Dillon 		error = EINVAL;
5041b0ab2c3SMatthew Dillon 	}
5051b0ab2c3SMatthew Dillon 
506427e5fc6SMatthew Dillon 	if (error) {
507427e5fc6SMatthew Dillon 		hammer_free_hmp(mp);
508427e5fc6SMatthew Dillon 		return (error);
509427e5fc6SMatthew Dillon 	}
510427e5fc6SMatthew Dillon 
511427e5fc6SMatthew Dillon 	/*
51227ea2398SMatthew Dillon 	 * No errors, setup enough of the mount point so we can lookup the
51327ea2398SMatthew Dillon 	 * root vnode.
514427e5fc6SMatthew Dillon 	 */
515427e5fc6SMatthew Dillon 	mp->mnt_iosize_max = MAXPHYS;
516427e5fc6SMatthew Dillon 	mp->mnt_kern_flag |= MNTK_FSMID;
517c0ade690SMatthew Dillon 
518c0ade690SMatthew Dillon 	/*
519c0ade690SMatthew Dillon 	 * note: f_iosize is used by vnode_pager_haspage() when constructing
520c0ade690SMatthew Dillon 	 * its VOP_BMAP call.
521c0ade690SMatthew Dillon 	 */
522c0ade690SMatthew Dillon 	mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
523fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
5246f97fce3SMatthew Dillon 
5256f97fce3SMatthew Dillon 	mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
5266f97fce3SMatthew Dillon 	mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
5276f97fce3SMatthew Dillon 
528427e5fc6SMatthew Dillon 	mp->mnt_maxsymlinklen = 255;
529427e5fc6SMatthew Dillon 	mp->mnt_flag |= MNT_LOCAL;
530427e5fc6SMatthew Dillon 
531427e5fc6SMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
5327a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
5337a04d74fSMatthew Dillon 	vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
53427ea2398SMatthew Dillon 
53527ea2398SMatthew Dillon 	/*
536a89aec1bSMatthew Dillon 	 * The root volume's ondisk pointer is only valid if we hold a
537a89aec1bSMatthew Dillon 	 * reference to it.
538a89aec1bSMatthew Dillon 	 */
539a89aec1bSMatthew Dillon 	rootvol = hammer_get_root_volume(hmp, &error);
540a89aec1bSMatthew Dillon 	if (error)
541f90dde4cSMatthew Dillon 		goto failed;
542f90dde4cSMatthew Dillon 
543f90dde4cSMatthew Dillon 	/*
5449f5097dcSMatthew Dillon 	 * Perform any necessary UNDO operations.  The recovery code does
5450729c8c8SMatthew Dillon 	 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
5460729c8c8SMatthew Dillon 	 * and then re-copy it again after recovery is complete.
547c9b9e29dSMatthew Dillon 	 *
54851c35492SMatthew Dillon 	 * If this is a read-only mount the UNDO information is retained
54951c35492SMatthew Dillon 	 * in memory in the form of dirty buffer cache buffers, and not
55051c35492SMatthew Dillon 	 * written back to the media.
551f90dde4cSMatthew Dillon 	 */
5520729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
5530729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
554c9b9e29dSMatthew Dillon 
5557a61b85dSMatthew Dillon 	/*
55644a83111SMatthew Dillon 	 * Check filesystem version
55744a83111SMatthew Dillon 	 */
55844a83111SMatthew Dillon 	hmp->version = rootvol->ondisk->vol_version;
55944a83111SMatthew Dillon 	if (hmp->version < HAMMER_VOL_VERSION_MIN ||
56044a83111SMatthew Dillon 	    hmp->version > HAMMER_VOL_VERSION_MAX) {
56144a83111SMatthew Dillon 		kprintf("HAMMER: mount unsupported fs version %d\n",
56244a83111SMatthew Dillon 			hmp->version);
56344a83111SMatthew Dillon 		error = ERANGE;
56444a83111SMatthew Dillon 		goto done;
56544a83111SMatthew Dillon 	}
56644a83111SMatthew Dillon 
56744a83111SMatthew Dillon 	/*
5687a61b85dSMatthew Dillon 	 * The undo_rec_limit limits the size of flush groups to avoid
5697a61b85dSMatthew Dillon 	 * blowing out the UNDO FIFO.  This calculation is typically in
5707a61b85dSMatthew Dillon 	 * the tens of thousands and is designed primarily when small
5717a61b85dSMatthew Dillon 	 * HAMMER filesystems are created.
5727a61b85dSMatthew Dillon 	 */
5737a61b85dSMatthew Dillon 	hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
5747a61b85dSMatthew Dillon 	if (hammer_debug_general & 0x0001)
5757a61b85dSMatthew Dillon 		kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit);
5767a61b85dSMatthew Dillon 
577f90dde4cSMatthew Dillon 	error = hammer_recover(hmp, rootvol);
578f90dde4cSMatthew Dillon 	if (error) {
579f90dde4cSMatthew Dillon 		kprintf("Failed to recover HAMMER filesystem on mount\n");
580a89aec1bSMatthew Dillon 		goto done;
581f90dde4cSMatthew Dillon 	}
582f90dde4cSMatthew Dillon 
583f90dde4cSMatthew Dillon 	/*
584adf01747SMatthew Dillon 	 * Finish setup now that we have a good root volume.
585adf01747SMatthew Dillon 	 *
586adf01747SMatthew Dillon 	 * The top 16 bits of fsid.val[1] is a pfs id.
587f90dde4cSMatthew Dillon 	 */
588a89aec1bSMatthew Dillon 	ksnprintf(mp->mnt_stat.f_mntfromname,
589a89aec1bSMatthew Dillon 		  sizeof(mp->mnt_stat.f_mntfromname), "%s",
590a89aec1bSMatthew Dillon 		  rootvol->ondisk->vol_name);
591513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[0] =
592513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
593513ca7d7SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] =
594513ca7d7SMatthew Dillon 		crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
595adf01747SMatthew Dillon 	mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF;
596b84de5afSMatthew Dillon 
5976f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
5986f97fce3SMatthew Dillon 	mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
5996f97fce3SMatthew Dillon 				     sizeof(mp->mnt_vstat.f_fsid_uuid));
6006f97fce3SMatthew Dillon 
6010729c8c8SMatthew Dillon 	/*
6020729c8c8SMatthew Dillon 	 * Certain often-modified fields in the root volume are cached in
6030729c8c8SMatthew Dillon 	 * the hammer_mount structure so we do not have to generate lots
6040729c8c8SMatthew Dillon 	 * of little UNDO structures for them.
605c9b9e29dSMatthew Dillon 	 *
6069f5097dcSMatthew Dillon 	 * Recopy after recovery.  This also has the side effect of
6079f5097dcSMatthew Dillon 	 * setting our cached undo FIFO's first_offset, which serves to
6089f5097dcSMatthew Dillon 	 * placemark the FIFO start for the NEXT flush cycle while the
6099f5097dcSMatthew Dillon 	 * on-disk first_offset represents the LAST flush cycle.
6100729c8c8SMatthew Dillon 	 */
611b84de5afSMatthew Dillon 	hmp->next_tid = rootvol->ondisk->vol0_next_tid;
6124889cbd4SMatthew Dillon 	hmp->flush_tid1 = hmp->next_tid;
6134889cbd4SMatthew Dillon 	hmp->flush_tid2 = hmp->next_tid;
6140729c8c8SMatthew Dillon 	bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
6150729c8c8SMatthew Dillon 	      sizeof(hmp->blockmap));
616e63644f0SMatthew Dillon 	hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
6170729c8c8SMatthew Dillon 
618059819e3SMatthew Dillon 	hammer_flusher_create(hmp);
619059819e3SMatthew Dillon 
620a89aec1bSMatthew Dillon 	/*
62127ea2398SMatthew Dillon 	 * Locate the root directory using the root cluster's B-Tree as a
62227ea2398SMatthew Dillon 	 * starting point.  The root directory uses an obj_id of 1.
62327ea2398SMatthew Dillon 	 *
62427ea2398SMatthew Dillon 	 * FUTURE: Leave the root directory cached referenced but unlocked
62527ea2398SMatthew Dillon 	 * in hmp->rootvp (need to flush it on unmount).
62627ea2398SMatthew Dillon 	 */
62727ea2398SMatthew Dillon 	error = hammer_vfs_vget(mp, 1, &rootvp);
628a89aec1bSMatthew Dillon 	if (error)
629a89aec1bSMatthew Dillon 		goto done;
63027ea2398SMatthew Dillon 	vput(rootvp);
63127ea2398SMatthew Dillon 	/*vn_unlock(hmp->rootvp);*/
63227ea2398SMatthew Dillon 
633a89aec1bSMatthew Dillon done:
634f90dde4cSMatthew Dillon 	hammer_rel_volume(rootvol, 0);
635f90dde4cSMatthew Dillon failed:
63627ea2398SMatthew Dillon 	/*
63727ea2398SMatthew Dillon 	 * Cleanup and return.
63827ea2398SMatthew Dillon 	 */
63927ea2398SMatthew Dillon 	if (error)
64027ea2398SMatthew Dillon 		hammer_free_hmp(mp);
641427e5fc6SMatthew Dillon 	return (error);
642427e5fc6SMatthew Dillon }
643427e5fc6SMatthew Dillon 
644427e5fc6SMatthew Dillon static int
645427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags)
646427e5fc6SMatthew Dillon {
647427e5fc6SMatthew Dillon #if 0
648427e5fc6SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
649427e5fc6SMatthew Dillon #endif
650427e5fc6SMatthew Dillon 	int flags;
65166325755SMatthew Dillon 	int error;
65227ea2398SMatthew Dillon 
65327ea2398SMatthew Dillon 	/*
654427e5fc6SMatthew Dillon 	 * Clean out the vnodes
655427e5fc6SMatthew Dillon 	 */
65666325755SMatthew Dillon 	flags = 0;
65766325755SMatthew Dillon 	if (mntflags & MNT_FORCE)
65866325755SMatthew Dillon 		flags |= FORCECLOSE;
65966325755SMatthew Dillon 	if ((error = vflush(mp, 0, flags)) != 0)
66066325755SMatthew Dillon 		return (error);
661427e5fc6SMatthew Dillon 
662427e5fc6SMatthew Dillon 	/*
663427e5fc6SMatthew Dillon 	 * Clean up the internal mount structure and related entities.  This
664427e5fc6SMatthew Dillon 	 * may issue I/O.
665427e5fc6SMatthew Dillon 	 */
666427e5fc6SMatthew Dillon 	hammer_free_hmp(mp);
667427e5fc6SMatthew Dillon 	return(0);
668427e5fc6SMatthew Dillon }
669427e5fc6SMatthew Dillon 
670427e5fc6SMatthew Dillon /*
671427e5fc6SMatthew Dillon  * Clean up the internal mount structure and disassociate it from the mount.
672427e5fc6SMatthew Dillon  * This may issue I/O.
673427e5fc6SMatthew Dillon  */
674427e5fc6SMatthew Dillon static void
675427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp)
676427e5fc6SMatthew Dillon {
677427e5fc6SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
678cdb6e4e6SMatthew Dillon 	hammer_flush_group_t flg;
6791b0ab2c3SMatthew Dillon 	int count;
680d40bfecaSMatthew Dillon 	int dummy;
681427e5fc6SMatthew Dillon 
682427e5fc6SMatthew Dillon 	/*
683cdb6e4e6SMatthew Dillon 	 * Flush anything dirty.  This won't even run if the
684cdb6e4e6SMatthew Dillon 	 * filesystem errored-out.
685427e5fc6SMatthew Dillon 	 */
6861b0ab2c3SMatthew Dillon 	count = 0;
6871b0ab2c3SMatthew Dillon 	while (hammer_flusher_haswork(hmp)) {
688059819e3SMatthew Dillon 		hammer_flusher_sync(hmp);
6891b0ab2c3SMatthew Dillon 		++count;
6901b0ab2c3SMatthew Dillon 		if (count >= 5) {
6911b0ab2c3SMatthew Dillon 			if (count == 5)
6921b0ab2c3SMatthew Dillon 				kprintf("HAMMER: umount flushing.");
6931b0ab2c3SMatthew Dillon 			else
6941b0ab2c3SMatthew Dillon 				kprintf(".");
695d40bfecaSMatthew Dillon 			tsleep(&dummy, 0, "hmrufl", hz);
6961b0ab2c3SMatthew Dillon 		}
6971b0ab2c3SMatthew Dillon 		if (count == 30) {
6981b0ab2c3SMatthew Dillon 			kprintf("giving up\n");
6991b0ab2c3SMatthew Dillon 			break;
7001b0ab2c3SMatthew Dillon 		}
7011b0ab2c3SMatthew Dillon 	}
7021b0ab2c3SMatthew Dillon 	if (count >= 5 && count < 30)
7031b0ab2c3SMatthew Dillon 		kprintf("\n");
704cdb6e4e6SMatthew Dillon 
705cdb6e4e6SMatthew Dillon 	/*
706cdb6e4e6SMatthew Dillon 	 * If the mount had a critical error we have to destroy any
707cdb6e4e6SMatthew Dillon 	 * remaining inodes before we can finish cleaning up the flusher.
708cdb6e4e6SMatthew Dillon 	 */
709cdb6e4e6SMatthew Dillon 	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
710cdb6e4e6SMatthew Dillon 		RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
711cdb6e4e6SMatthew Dillon 			hammer_destroy_inode_callback, NULL);
712cdb6e4e6SMatthew Dillon 	}
713cdb6e4e6SMatthew Dillon 
714cdb6e4e6SMatthew Dillon 	/*
715cdb6e4e6SMatthew Dillon 	 * There shouldn't be any inodes left now and any left over
716cdb6e4e6SMatthew Dillon 	 * flush groups should now be empty.
717cdb6e4e6SMatthew Dillon 	 */
718cdb6e4e6SMatthew Dillon 	KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
719cdb6e4e6SMatthew Dillon 	while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
720cdb6e4e6SMatthew Dillon 		TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
721cdb6e4e6SMatthew Dillon 		KKASSERT(TAILQ_EMPTY(&flg->flush_list));
722cdb6e4e6SMatthew Dillon 		if (flg->refs) {
723cdb6e4e6SMatthew Dillon 			kprintf("HAMMER: Warning, flush_group %p was "
724cdb6e4e6SMatthew Dillon 				"not empty on umount!\n", flg);
725cdb6e4e6SMatthew Dillon 		}
726bac808feSMatthew Dillon 		kfree(flg, hmp->m_misc);
727cdb6e4e6SMatthew Dillon 	}
728cdb6e4e6SMatthew Dillon 
729cdb6e4e6SMatthew Dillon 	/*
730cdb6e4e6SMatthew Dillon 	 * We can finally destroy the flusher
731cdb6e4e6SMatthew Dillon 	 */
732059819e3SMatthew Dillon 	hammer_flusher_destroy(hmp);
733427e5fc6SMatthew Dillon 
734b84de5afSMatthew Dillon 	/*
73500f16fadSMatthew Dillon 	 * We may have held recovered buffers due to a read-only mount.
73600f16fadSMatthew Dillon 	 * These must be discarded.
73700f16fadSMatthew Dillon 	 */
73800f16fadSMatthew Dillon 	if (hmp->ronly)
73900f16fadSMatthew Dillon 		hammer_recover_flush_buffers(hmp, NULL, -1);
74000f16fadSMatthew Dillon 
74100f16fadSMatthew Dillon 	/*
7420832c9bbSMatthew Dillon 	 * Unload buffers and then volumes
743b84de5afSMatthew Dillon 	 */
7440832c9bbSMatthew Dillon         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
7450832c9bbSMatthew Dillon 		hammer_unload_buffer, NULL);
746427e5fc6SMatthew Dillon 	RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
747427e5fc6SMatthew Dillon 		hammer_unload_volume, NULL);
748427e5fc6SMatthew Dillon 
749427e5fc6SMatthew Dillon 	mp->mnt_data = NULL;
75066325755SMatthew Dillon 	mp->mnt_flag &= ~MNT_LOCAL;
751427e5fc6SMatthew Dillon 	hmp->mp = NULL;
7520729c8c8SMatthew Dillon 	hammer_destroy_objid_cache(hmp);
753bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_misc);
754bac808feSMatthew Dillon 	kmalloc_destroy(&hmp->m_inodes);
755427e5fc6SMatthew Dillon 	kfree(hmp, M_HAMMER);
756427e5fc6SMatthew Dillon }
757427e5fc6SMatthew Dillon 
758427e5fc6SMatthew Dillon /*
759cdb6e4e6SMatthew Dillon  * Report critical errors.  ip may be NULL.
760cdb6e4e6SMatthew Dillon  */
761cdb6e4e6SMatthew Dillon void
762cdb6e4e6SMatthew Dillon hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
763cdb6e4e6SMatthew Dillon 		      int error, const char *msg)
764cdb6e4e6SMatthew Dillon {
765cdb6e4e6SMatthew Dillon 	hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
766cdb6e4e6SMatthew Dillon 	krateprintf(&hmp->krate,
767cdb6e4e6SMatthew Dillon 		"HAMMER(%s): Critical error inode=%lld %s\n",
768cdb6e4e6SMatthew Dillon 		hmp->mp->mnt_stat.f_mntfromname,
769cdb6e4e6SMatthew Dillon 		(ip ? ip->obj_id : -1), msg);
770cdb6e4e6SMatthew Dillon 	if (hmp->ronly == 0) {
771cdb6e4e6SMatthew Dillon 		hmp->ronly = 2;		/* special errored read-only mode */
772cdb6e4e6SMatthew Dillon 		hmp->mp->mnt_flag |= MNT_RDONLY;
773cdb6e4e6SMatthew Dillon 		kprintf("HAMMER(%s): Forcing read-only mode\n",
774cdb6e4e6SMatthew Dillon 			hmp->mp->mnt_stat.f_mntfromname);
775cdb6e4e6SMatthew Dillon 	}
776cdb6e4e6SMatthew Dillon 	hmp->error = error;
777cdb6e4e6SMatthew Dillon }
778cdb6e4e6SMatthew Dillon 
779cdb6e4e6SMatthew Dillon 
780cdb6e4e6SMatthew Dillon /*
781513ca7d7SMatthew Dillon  * Obtain a vnode for the specified inode number.  An exclusively locked
782513ca7d7SMatthew Dillon  * vnode is returned.
783513ca7d7SMatthew Dillon  */
784513ca7d7SMatthew Dillon int
785513ca7d7SMatthew Dillon hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
786513ca7d7SMatthew Dillon {
78736f82b23SMatthew Dillon 	struct hammer_transaction trans;
788513ca7d7SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
789513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
790513ca7d7SMatthew Dillon 	int error;
791513ca7d7SMatthew Dillon 
79236f82b23SMatthew Dillon 	hammer_simple_transaction(&trans, hmp);
79336f82b23SMatthew Dillon 
794513ca7d7SMatthew Dillon 	/*
79536f82b23SMatthew Dillon 	 * Lookup the requested HAMMER inode.  The structure must be
79636f82b23SMatthew Dillon 	 * left unlocked while we manipulate the related vnode to avoid
79736f82b23SMatthew Dillon 	 * a deadlock.
798513ca7d7SMatthew Dillon 	 */
799ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, ino,
800ddfdf542SMatthew Dillon 			      hmp->asof, HAMMER_DEF_LOCALIZATION,
801ddfdf542SMatthew Dillon 			      0, &error);
802513ca7d7SMatthew Dillon 	if (ip == NULL) {
803513ca7d7SMatthew Dillon 		*vpp = NULL;
80400f16fadSMatthew Dillon 		hammer_done_transaction(&trans);
805513ca7d7SMatthew Dillon 		return(error);
806513ca7d7SMatthew Dillon 	}
807e8599db1SMatthew Dillon 	error = hammer_get_vnode(ip, vpp);
808513ca7d7SMatthew Dillon 	hammer_rel_inode(ip, 0);
809b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
810513ca7d7SMatthew Dillon 	return (error);
811513ca7d7SMatthew Dillon }
812513ca7d7SMatthew Dillon 
813513ca7d7SMatthew Dillon /*
814427e5fc6SMatthew Dillon  * Return the root vnode for the filesystem.
815427e5fc6SMatthew Dillon  *
816427e5fc6SMatthew Dillon  * HAMMER stores the root vnode in the hammer_mount structure so
817427e5fc6SMatthew Dillon  * getting it is easy.
818427e5fc6SMatthew Dillon  */
819427e5fc6SMatthew Dillon static int
820427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp)
821427e5fc6SMatthew Dillon {
82247197d71SMatthew Dillon #if 0
823427e5fc6SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
82447197d71SMatthew Dillon #endif
82527ea2398SMatthew Dillon 	int error;
826427e5fc6SMatthew Dillon 
82727ea2398SMatthew Dillon 	error = hammer_vfs_vget(mp, 1, vpp);
82827ea2398SMatthew Dillon 	return (error);
829427e5fc6SMatthew Dillon }
830427e5fc6SMatthew Dillon 
831427e5fc6SMatthew Dillon static int
832427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
833427e5fc6SMatthew Dillon {
834fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
835fbc6e32aSMatthew Dillon 	hammer_volume_t volume;
836fbc6e32aSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
837fbc6e32aSMatthew Dillon 	int error;
83847197d71SMatthew Dillon 	int64_t bfree;
839fbc6e32aSMatthew Dillon 
840fbc6e32aSMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
841fbc6e32aSMatthew Dillon 	if (error)
842fbc6e32aSMatthew Dillon 		return(error);
843fbc6e32aSMatthew Dillon 	ondisk = volume->ondisk;
844fbc6e32aSMatthew Dillon 
84547197d71SMatthew Dillon 	/*
84647197d71SMatthew Dillon 	 * Basic stats
84747197d71SMatthew Dillon 	 */
848fbc6e32aSMatthew Dillon 	mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
849c3be93f2SMatthew Dillon 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
85040043e7fSMatthew Dillon 	hammer_rel_volume(volume, 0);
85147197d71SMatthew Dillon 
85247197d71SMatthew Dillon 	mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
85347197d71SMatthew Dillon 	mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
854fbc6e32aSMatthew Dillon 	if (mp->mnt_stat.f_files < 0)
855fbc6e32aSMatthew Dillon 		mp->mnt_stat.f_files = 0;
856fbc6e32aSMatthew Dillon 
85727ea2398SMatthew Dillon 	*sbp = mp->mnt_stat;
85827ea2398SMatthew Dillon 	return(0);
859427e5fc6SMatthew Dillon }
860427e5fc6SMatthew Dillon 
8616f97fce3SMatthew Dillon static int
8626f97fce3SMatthew Dillon hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
8636f97fce3SMatthew Dillon {
8646f97fce3SMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
8656f97fce3SMatthew Dillon 	hammer_volume_t volume;
8666f97fce3SMatthew Dillon 	hammer_volume_ondisk_t ondisk;
8676f97fce3SMatthew Dillon 	int error;
8686f97fce3SMatthew Dillon 	int64_t bfree;
8696f97fce3SMatthew Dillon 
8706f97fce3SMatthew Dillon 	volume = hammer_get_root_volume(hmp, &error);
8716f97fce3SMatthew Dillon 	if (error)
8726f97fce3SMatthew Dillon 		return(error);
8736f97fce3SMatthew Dillon 	ondisk = volume->ondisk;
8746f97fce3SMatthew Dillon 
8756f97fce3SMatthew Dillon 	/*
8766f97fce3SMatthew Dillon 	 * Basic stats
8776f97fce3SMatthew Dillon 	 */
8786f97fce3SMatthew Dillon 	mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
8796f97fce3SMatthew Dillon 	bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
8806f97fce3SMatthew Dillon 	hammer_rel_volume(volume, 0);
8816f97fce3SMatthew Dillon 
8826f97fce3SMatthew Dillon 	mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE;
8836f97fce3SMatthew Dillon 	mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
8846f97fce3SMatthew Dillon 	if (mp->mnt_vstat.f_files < 0)
8856f97fce3SMatthew Dillon 		mp->mnt_vstat.f_files = 0;
8866f97fce3SMatthew Dillon 	*sbp = mp->mnt_vstat;
8876f97fce3SMatthew Dillon 	return(0);
8886f97fce3SMatthew Dillon }
8896f97fce3SMatthew Dillon 
8900729c8c8SMatthew Dillon /*
8910729c8c8SMatthew Dillon  * Sync the filesystem.  Currently we have to run it twice, the second
8920729c8c8SMatthew Dillon  * one will advance the undo start index to the end index, so if a crash
8930729c8c8SMatthew Dillon  * occurs no undos will be run on mount.
89477062c8aSMatthew Dillon  *
89577062c8aSMatthew Dillon  * We do not sync the filesystem if we are called from a panic.  If we did
89677062c8aSMatthew Dillon  * we might end up blowing up a sync that was already in progress.
8970729c8c8SMatthew Dillon  */
898427e5fc6SMatthew Dillon static int
899427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor)
900427e5fc6SMatthew Dillon {
901fbc6e32aSMatthew Dillon 	struct hammer_mount *hmp = (void *)mp->mnt_data;
9020729c8c8SMatthew Dillon 	int error;
9030729c8c8SMatthew Dillon 
90477062c8aSMatthew Dillon 	if (panicstr == NULL) {
9050729c8c8SMatthew Dillon 		error = hammer_sync_hmp(hmp, waitfor);
90677062c8aSMatthew Dillon 	} else {
90777062c8aSMatthew Dillon 		error = EIO;
90877062c8aSMatthew Dillon 	}
9090729c8c8SMatthew Dillon 	return (error);
910427e5fc6SMatthew Dillon }
911427e5fc6SMatthew Dillon 
912513ca7d7SMatthew Dillon /*
913513ca7d7SMatthew Dillon  * Convert a vnode to a file handle.
914513ca7d7SMatthew Dillon  */
915513ca7d7SMatthew Dillon static int
916513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
917513ca7d7SMatthew Dillon {
918513ca7d7SMatthew Dillon 	hammer_inode_t ip;
919513ca7d7SMatthew Dillon 
920513ca7d7SMatthew Dillon 	KKASSERT(MAXFIDSZ >= 16);
921513ca7d7SMatthew Dillon 	ip = VTOI(vp);
922513ca7d7SMatthew Dillon 	fhp->fid_len = offsetof(struct fid, fid_data[16]);
923adf01747SMatthew Dillon 	fhp->fid_ext = ip->obj_localization >> 16;
924513ca7d7SMatthew Dillon 	bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
925513ca7d7SMatthew Dillon 	bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
926513ca7d7SMatthew Dillon 	return(0);
927513ca7d7SMatthew Dillon }
928513ca7d7SMatthew Dillon 
929513ca7d7SMatthew Dillon 
930513ca7d7SMatthew Dillon /*
931513ca7d7SMatthew Dillon  * Convert a file handle back to a vnode.
93267863d04SMatthew Dillon  *
93367863d04SMatthew Dillon  * Use rootvp to enforce PFS isolation when a PFS is exported via a
93467863d04SMatthew Dillon  * null mount.
935513ca7d7SMatthew Dillon  */
936513ca7d7SMatthew Dillon static int
93767863d04SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
93867863d04SMatthew Dillon 		  struct fid *fhp, struct vnode **vpp)
939513ca7d7SMatthew Dillon {
94036f82b23SMatthew Dillon 	struct hammer_transaction trans;
941513ca7d7SMatthew Dillon 	struct hammer_inode *ip;
942513ca7d7SMatthew Dillon 	struct hammer_inode_info info;
943513ca7d7SMatthew Dillon 	int error;
944adf01747SMatthew Dillon 	u_int32_t localization;
945513ca7d7SMatthew Dillon 
946513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
947513ca7d7SMatthew Dillon 	bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
94867863d04SMatthew Dillon 	if (rootvp)
94967863d04SMatthew Dillon 		localization = VTOI(rootvp)->obj_localization;
95067863d04SMatthew Dillon 	else
951adf01747SMatthew Dillon 		localization = (u_int32_t)fhp->fid_ext << 16;
952513ca7d7SMatthew Dillon 
95336f82b23SMatthew Dillon 	hammer_simple_transaction(&trans, (void *)mp->mnt_data);
95436f82b23SMatthew Dillon 
955513ca7d7SMatthew Dillon 	/*
956513ca7d7SMatthew Dillon 	 * Get/allocate the hammer_inode structure.  The structure must be
957513ca7d7SMatthew Dillon 	 * unlocked while we manipulate the related vnode to avoid a
958513ca7d7SMatthew Dillon 	 * deadlock.
959513ca7d7SMatthew Dillon 	 */
960ddfdf542SMatthew Dillon 	ip = hammer_get_inode(&trans, NULL, info.obj_id,
961adf01747SMatthew Dillon 			      info.obj_asof, localization, 0, &error);
962513ca7d7SMatthew Dillon 	if (ip == NULL) {
963513ca7d7SMatthew Dillon 		*vpp = NULL;
964513ca7d7SMatthew Dillon 		return(error);
965513ca7d7SMatthew Dillon 	}
966e8599db1SMatthew Dillon 	error = hammer_get_vnode(ip, vpp);
967513ca7d7SMatthew Dillon 	hammer_rel_inode(ip, 0);
968b84de5afSMatthew Dillon 	hammer_done_transaction(&trans);
969513ca7d7SMatthew Dillon 	return (error);
970513ca7d7SMatthew Dillon }
971513ca7d7SMatthew Dillon 
972513ca7d7SMatthew Dillon static int
973513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
974513ca7d7SMatthew Dillon 		    int *exflagsp, struct ucred **credanonp)
975513ca7d7SMatthew Dillon {
976513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
977513ca7d7SMatthew Dillon 	struct netcred *np;
978513ca7d7SMatthew Dillon 	int error;
979513ca7d7SMatthew Dillon 
980513ca7d7SMatthew Dillon 	np = vfs_export_lookup(mp, &hmp->export, nam);
981513ca7d7SMatthew Dillon 	if (np) {
982513ca7d7SMatthew Dillon 		*exflagsp = np->netc_exflags;
983513ca7d7SMatthew Dillon 		*credanonp = &np->netc_anon;
984513ca7d7SMatthew Dillon 		error = 0;
985513ca7d7SMatthew Dillon 	} else {
986513ca7d7SMatthew Dillon 		error = EACCES;
987513ca7d7SMatthew Dillon 	}
988513ca7d7SMatthew Dillon 	return (error);
989513ca7d7SMatthew Dillon 
990513ca7d7SMatthew Dillon }
991513ca7d7SMatthew Dillon 
992513ca7d7SMatthew Dillon int
993513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
994513ca7d7SMatthew Dillon {
995513ca7d7SMatthew Dillon 	hammer_mount_t hmp = (void *)mp->mnt_data;
996513ca7d7SMatthew Dillon 	int error;
997513ca7d7SMatthew Dillon 
998513ca7d7SMatthew Dillon 	switch(op) {
999513ca7d7SMatthew Dillon 	case MOUNTCTL_SET_EXPORT:
1000513ca7d7SMatthew Dillon 		error = vfs_export(mp, &hmp->export, export);
1001513ca7d7SMatthew Dillon 		break;
1002513ca7d7SMatthew Dillon 	default:
1003513ca7d7SMatthew Dillon 		error = EOPNOTSUPP;
1004513ca7d7SMatthew Dillon 		break;
1005513ca7d7SMatthew Dillon 	}
1006513ca7d7SMatthew Dillon 	return(error);
1007513ca7d7SMatthew Dillon }
1008513ca7d7SMatthew Dillon 
1009