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