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