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