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