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