hammer - Describe live_dedup_cache_size sysctl
[dragonfly.git] / sys / vfs / hammer / hammer_vfsops.c
CommitLineData
427e5fc6 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
427e5fc6
MD
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
44a83111 34 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.74 2008/11/13 02:18:43 dillon Exp $
427e5fc6
MD
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/vnode.h>
41#include <sys/mount.h>
42#include <sys/malloc.h>
43#include <sys/nlookup.h>
44#include <sys/fcntl.h>
b3deaf57 45#include <sys/sysctl.h>
427e5fc6
MD
46#include <sys/buf.h>
47#include <sys/buf2.h>
48#include "hammer.h"
49
aac0aabd
MD
50/*
51 * NOTE! Global statistics may not be MPSAFE so HAMMER never uses them
52 * in conditionals.
53 */
02428fb6 54int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT;
2f85fa4d 55int hammer_debug_io;
d5ef456e 56int hammer_debug_general;
1b0ab2c3 57int hammer_debug_debug = 1; /* medium-error panics */
e8599db1 58int hammer_debug_inode;
7d683b0f 59int hammer_debug_locks;
b3deaf57 60int hammer_debug_btree;
d113fda1 61int hammer_debug_tid;
cebe9493 62int hammer_debug_recover; /* -1 will disable, +1 will force */
46fe7ae1 63int hammer_debug_recover_faults;
fc73edd8 64int hammer_debug_critical; /* non-zero enter debugger on error */
1b0ab2c3 65int hammer_cluster_enable = 1; /* enable read clustering by default */
507df98a 66int hammer_live_dedup = 0;
7a61b85d 67int hammer_count_fsyncs;
b3deaf57 68int hammer_count_inodes;
af209b0f 69int hammer_count_iqueued;
9f5097dc 70int hammer_count_reclaiming;
b3deaf57
MD
71int hammer_count_records;
72int hammer_count_record_datas;
73int hammer_count_volumes;
b3deaf57
MD
74int hammer_count_buffers;
75int hammer_count_nodes;
a7e9bef1 76int64_t hammer_count_extra_space_used;
cb51be26
MD
77int64_t hammer_stats_btree_lookups;
78int64_t hammer_stats_btree_searches;
79int64_t hammer_stats_btree_inserts;
80int64_t hammer_stats_btree_deletes;
81int64_t hammer_stats_btree_elements;
82int64_t hammer_stats_btree_splits;
83int64_t hammer_stats_btree_iterations;
39d8fd63 84int64_t hammer_stats_btree_root_iterations;
cb51be26 85int64_t hammer_stats_record_iterations;
ce0138a6
MD
86
87int64_t hammer_stats_file_read;
88int64_t hammer_stats_file_write;
89int64_t hammer_stats_file_iopsr;
90int64_t hammer_stats_file_iopsw;
91int64_t hammer_stats_disk_read;
92int64_t hammer_stats_disk_write;
93int64_t hammer_stats_inode_flushes;
94int64_t hammer_stats_commits;
89e744ce 95int64_t hammer_stats_undo;
6048b411 96int64_t hammer_stats_redo;
ce0138a6 97
f5a07a7a 98int hammer_count_dirtybufspace; /* global */
a99b9ea2 99int hammer_count_refedbufs; /* global */
0832c9bb 100int hammer_count_reservations;
a99b9ea2
MD
101int hammer_count_io_running_read;
102int hammer_count_io_running_write;
103int hammer_count_io_locked;
f5a07a7a 104int hammer_limit_dirtybufspace; /* per-mount */
ba298df1 105int hammer_limit_running_io; /* per-mount */
0832c9bb 106int hammer_limit_recs; /* as a whole XXX */
de996e86 107int hammer_limit_inode_recs = 1024; /* per inode */
ff003b11 108int hammer_limit_reclaim = HAMMER_RECLAIM_WAIT;
e2ef7a95 109int hammer_live_dedup_cache_size = DEDUP_CACHE_SIZE;
6048b411 110int hammer_limit_redo = 4096 * 1024; /* per inode */
21fde338 111int hammer_autoflush = 2000; /* auto flush */
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;
427e5fc6
MD
358 return(0);
359}
360
361static int
362hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
363 struct ucred *cred)
364{
365 struct hammer_mount_info info;
a89aec1b
MD
366 hammer_mount_t hmp;
367 hammer_volume_t rootvol;
27ea2398 368 struct vnode *rootvp;
7c19b529 369 struct vnode *devvp = NULL;
427e5fc6
MD
370 const char *upath; /* volume name in userspace */
371 char *path; /* volume name in system space */
372 int error;
373 int i;
732a1697 374 int master_id;
104cb849 375 char *next_volume_ptr = NULL;
ab0bf4a3
MD
376
377 /*
378 * Accept hammer_mount_info. mntpt is NULL for root mounts at boot.
379 */
7c19b529 380 if (mntpt == NULL) {
7c19b529
MN
381 bzero(&info, sizeof(info));
382 info.asof = 0;
383 info.hflags = 0;
384 info.nvolumes = 1;
104cb849
MN
385
386 next_volume_ptr = mp->mnt_stat.f_mntfromname;
387
388 /* Count number of volumes separated by ':' */
389 for (char *p = next_volume_ptr; *p != '\0'; ++p) {
390 if (*p == ':') {
391 ++info.nvolumes;
392 }
393 }
394
395 mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
7c19b529
MN
396 } else {
397 if ((error = copyin(data, &info, sizeof(info))) != 0)
398 return (error);
399 }
732a1697
MD
400
401 /*
402 * updating or new mount
403 */
404 if (mp->mnt_flag & MNT_UPDATE) {
405 hmp = (void *)mp->mnt_data;
406 KKASSERT(hmp != NULL);
407 } else {
51c35492
MD
408 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
409 return (EINVAL);
732a1697 410 hmp = NULL;
51c35492 411 }
732a1697
MD
412
413 /*
414 * master-id validation. The master id may not be changed by a
415 * mount update.
416 */
417 if (info.hflags & HMNT_MASTERID) {
418 if (hmp && hmp->master_id != info.master_id) {
419 kprintf("hammer: cannot change master id "
420 "with mount update\n");
421 return(EINVAL);
422 }
423 master_id = info.master_id;
424 if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
5de0c0e5 425 return (EINVAL);
732a1697
MD
426 } else {
427 if (hmp)
428 master_id = hmp->master_id;
429 else
430 master_id = 0;
5de0c0e5 431 }
427e5fc6
MD
432
433 /*
b0aab9b9 434 * Internal mount data structure
427e5fc6 435 */
732a1697 436 if (hmp == NULL) {
195c19a1
MD
437 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
438 mp->mnt_data = (qaddr_t)hmp;
439 hmp->mp = mp;
46fe7ae1 440 /*TAILQ_INIT(&hmp->recycle_list);*/
47197d71 441
bac808fe 442 /*
7c457ac8
MD
443 * Make sure kmalloc type limits are set appropriately.
444 *
445 * Our inode kmalloc group is sized based on maxvnodes
446 * (controlled by the system, not us).
bac808fe
MD
447 */
448 kmalloc_create(&hmp->m_misc, "HAMMER-others");
449 kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
450
7c457ac8 451 kmalloc_raise_limit(hmp->m_inodes, 0); /* unlimited */
bac808fe 452
dd94f1b1 453 hmp->root_btree_beg.localization = 0x00000000U;
47197d71
MD
454 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
455 hmp->root_btree_beg.key = -0x8000000000000000LL;
456 hmp->root_btree_beg.create_tid = 1;
457 hmp->root_btree_beg.delete_tid = 1;
458 hmp->root_btree_beg.rec_type = 0;
459 hmp->root_btree_beg.obj_type = 0;
460
dd94f1b1 461 hmp->root_btree_end.localization = 0xFFFFFFFFU;
47197d71
MD
462 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
463 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
464 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
465 hmp->root_btree_end.delete_tid = 0; /* special case */
466 hmp->root_btree_end.rec_type = 0xFFFFU;
467 hmp->root_btree_end.obj_type = 0;
f03c9cf4 468
cdb6e4e6
MD
469 hmp->krate.freq = 1; /* maximum reporting rate (hz) */
470 hmp->krate.count = -16; /* initial burst */
471
9480ff55 472 hmp->sync_lock.refs = 1;
c9b9e29d 473 hmp->free_lock.refs = 1;
d99d6bf5
MD
474 hmp->undo_lock.refs = 1;
475 hmp->blkmap_lock.refs = 1;
83f2a3aa 476 hmp->snapshot_lock.refs = 1;
52e547e3 477 hmp->volume_lock.refs = 1;
9480ff55 478
cebe9493 479 TAILQ_INIT(&hmp->delay_list);
7a61b85d 480 TAILQ_INIT(&hmp->flush_group_list);
0729c8c8 481 TAILQ_INIT(&hmp->objid_cache_list);
e8599db1 482 TAILQ_INIT(&hmp->undo_lru_list);
7bc5b8c2 483 TAILQ_INIT(&hmp->reclaim_list);
507df98a
ID
484
485 RB_INIT(&hmp->rb_dedup_crc_root);
486 RB_INIT(&hmp->rb_dedup_off_root);
487 TAILQ_INIT(&hmp->dedup_lru_list);
195c19a1 488 }
51c35492
MD
489 hmp->hflags &= ~HMNT_USERFLAGS;
490 hmp->hflags |= info.hflags & HMNT_USERFLAGS;
732a1697
MD
491
492 hmp->master_id = master_id;
493
7f7c1f84
MD
494 if (info.asof) {
495 mp->mnt_flag |= MNT_RDONLY;
496 hmp->asof = info.asof;
497 } else {
498 hmp->asof = HAMMER_MAX_TID;
499 }
195c19a1 500
865c9609
MN
501 hmp->volume_to_remove = -1;
502
195c19a1 503 /*
51c35492 504 * Re-open read-write if originally read-only, or vise-versa.
02428fb6
MD
505 *
506 * When going from read-only to read-write execute the stage2
507 * recovery if it has not already been run.
195c19a1
MD
508 */
509 if (mp->mnt_flag & MNT_UPDATE) {
b0aab9b9 510 lwkt_gettoken(&hmp->fs_token);
51c35492
MD
511 error = 0;
512 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
513 kprintf("HAMMER read-only -> read-write\n");
514 hmp->ronly = 0;
515 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
516 hammer_adjust_volume_mode, NULL);
517 rootvol = hammer_get_root_volume(hmp, &error);
518 if (rootvol) {
06ad81ff 519 hammer_recover_flush_buffers(hmp, rootvol, 1);
02428fb6 520 error = hammer_recover_stage2(hmp, rootvol);
9f5097dc
MD
521 bcopy(rootvol->ondisk->vol0_blockmap,
522 hmp->blockmap,
523 sizeof(hmp->blockmap));
51c35492
MD
524 hammer_rel_volume(rootvol, 0);
525 }
526 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
527 hammer_reload_inode, NULL);
528 /* kernel clears MNT_RDONLY */
529 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
530 kprintf("HAMMER read-write -> read-only\n");
531 hmp->ronly = 1; /* messy */
532 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
533 hammer_reload_inode, NULL);
195c19a1 534 hmp->ronly = 0;
51c35492
MD
535 hammer_flusher_sync(hmp);
536 hammer_flusher_sync(hmp);
537 hammer_flusher_sync(hmp);
538 hmp->ronly = 1;
539 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
540 hammer_adjust_volume_mode, NULL);
195c19a1 541 }
b0aab9b9 542 lwkt_reltoken(&hmp->fs_token);
51c35492 543 return(error);
195c19a1
MD
544 }
545
427e5fc6
MD
546 RB_INIT(&hmp->rb_vols_root);
547 RB_INIT(&hmp->rb_inos_root);
73896937 548 RB_INIT(&hmp->rb_redo_root);
40043e7f 549 RB_INIT(&hmp->rb_nods_root);
e8599db1 550 RB_INIT(&hmp->rb_undo_root);
0832c9bb
MD
551 RB_INIT(&hmp->rb_resv_root);
552 RB_INIT(&hmp->rb_bufs_root);
5fa5c92f 553 RB_INIT(&hmp->rb_pfsm_root);
0832c9bb 554
195c19a1 555 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
427e5fc6 556
10a5d1ba
MD
557 TAILQ_INIT(&hmp->volu_list);
558 TAILQ_INIT(&hmp->undo_list);
559 TAILQ_INIT(&hmp->data_list);
560 TAILQ_INIT(&hmp->meta_list);
561 TAILQ_INIT(&hmp->lose_list);
eddadaee 562 TAILQ_INIT(&hmp->iorun_list);
10a5d1ba 563
b0aab9b9
MD
564 lwkt_token_init(&hmp->fs_token, 1, "hammerfs");
565 lwkt_token_init(&hmp->io_token, 1, "hammerio");
566
567 lwkt_gettoken(&hmp->fs_token);
568
427e5fc6
MD
569 /*
570 * Load volumes
571 */
572 path = objcache_get(namei_oc, M_WAITOK);
1b0ab2c3 573 hmp->nvolumes = -1;
427e5fc6 574 for (i = 0; i < info.nvolumes; ++i) {
7c19b529
MN
575 if (mntpt == NULL) {
576 /*
577 * Root mount.
7c19b529 578 */
104cb849 579 KKASSERT(next_volume_ptr != NULL);
a407819f
MN
580 strcpy(path, "");
581 if (*next_volume_ptr != '/') {
582 /* relative path */
583 strcpy(path, "/dev/");
584 }
104cb849
MN
585 int k;
586 for (k = strlen(path); k < MAXPATHLEN-1; ++k) {
587 if (*next_volume_ptr == '\0') {
588 break;
589 } else if (*next_volume_ptr == ':') {
590 ++next_volume_ptr;
591 break;
592 } else {
593 path[k] = *next_volume_ptr;
594 ++next_volume_ptr;
595 }
596 }
597 path[k] = '\0';
598
7c19b529 599 error = 0;
104cb849
MN
600 cdev_t dev = kgetdiskbyname(path);
601 error = bdevvp(dev, &devvp);
602 if (error) {
603 kprintf("hammer_mountroot: can't find devvp\n");
604 }
7c19b529
MN
605 } else {
606 error = copyin(&info.volumes[i], &upath,
607 sizeof(char *));
608 if (error == 0)
609 error = copyinstr(upath, path,
610 MAXPATHLEN, NULL);
611 }
427e5fc6 612 if (error == 0)
7c19b529 613 error = hammer_install_volume(hmp, path, devvp);
427e5fc6
MD
614 if (error)
615 break;
616 }
617 objcache_put(namei_oc, path);
618
619 /*
620 * Make sure we found a root volume
621 */
622 if (error == 0 && hmp->rootvol == NULL) {
623 kprintf("hammer_mount: No root volume found!\n");
624 error = EINVAL;
625 }
1b0ab2c3
MD
626
627 /*
628 * Check that all required volumes are available
629 */
630 if (error == 0 && hammer_mountcheck_volumes(hmp)) {
631 kprintf("hammer_mount: Missing volumes, cannot mount!\n");
632 error = EINVAL;
633 }
634
427e5fc6 635 if (error) {
b0aab9b9 636 /* called with fs_token held */
427e5fc6
MD
637 hammer_free_hmp(mp);
638 return (error);
639 }
640
641 /*
27ea2398
MD
642 * No errors, setup enough of the mount point so we can lookup the
643 * root vnode.
427e5fc6 644 */
27ea2398
MD
645 mp->mnt_iosize_max = MAXPHYS;
646 mp->mnt_kern_flag |= MNTK_FSMID;
c0ade690 647
aac0aabd
MD
648 /*
649 * MPSAFE code. Note that VOPs and VFSops which are not MPSAFE
650 * will acquire a per-mount token prior to entry and release it
651 * on return, so even if we do not specify it we no longer get
652 * the BGL regardlless of how we are flagged.
653 */
b0aab9b9
MD
654 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
655 /*MNTK_RD_MPSAFE | MNTK_GA_MPSAFE | MNTK_IN_MPSAFE;*/
aac0aabd 656
c0ade690
MD
657 /*
658 * note: f_iosize is used by vnode_pager_haspage() when constructing
659 * its VOP_BMAP call.
660 */
661 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
fbc6e32a 662 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
6f97fce3
MD
663
664 mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
665 mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
666
27ea2398
MD
667 mp->mnt_maxsymlinklen = 255;
668 mp->mnt_flag |= MNT_LOCAL;
669
670 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
7a04d74f
MD
671 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
672 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
27ea2398
MD
673
674 /*
a89aec1b
MD
675 * The root volume's ondisk pointer is only valid if we hold a
676 * reference to it.
677 */
678 rootvol = hammer_get_root_volume(hmp, &error);
679 if (error)
f90dde4c
MD
680 goto failed;
681
682 /*
9f5097dc 683 * Perform any necessary UNDO operations. The recovery code does
0729c8c8
MD
684 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
685 * and then re-copy it again after recovery is complete.
c9b9e29d 686 *
51c35492
MD
687 * If this is a read-only mount the UNDO information is retained
688 * in memory in the form of dirty buffer cache buffers, and not
689 * written back to the media.
f90dde4c 690 */
0729c8c8
MD
691 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
692 sizeof(hmp->blockmap));
c9b9e29d 693
7a61b85d 694 /*
44a83111
MD
695 * Check filesystem version
696 */
697 hmp->version = rootvol->ondisk->vol_version;
698 if (hmp->version < HAMMER_VOL_VERSION_MIN ||
699 hmp->version > HAMMER_VOL_VERSION_MAX) {
700 kprintf("HAMMER: mount unsupported fs version %d\n",
701 hmp->version);
702 error = ERANGE;
703 goto done;
704 }
705
706 /*
7a61b85d
MD
707 * The undo_rec_limit limits the size of flush groups to avoid
708 * blowing out the UNDO FIFO. This calculation is typically in
709 * the tens of thousands and is designed primarily when small
710 * HAMMER filesystems are created.
711 */
712 hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
713 if (hammer_debug_general & 0x0001)
714 kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit);
715
02428fb6
MD
716 /*
717 * NOTE: Recover stage1 not only handles meta-data recovery, it
718 * also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems.
719 */
720 error = hammer_recover_stage1(hmp, rootvol);
f90dde4c
MD
721 if (error) {
722 kprintf("Failed to recover HAMMER filesystem on mount\n");
a89aec1b 723 goto done;
f90dde4c
MD
724 }
725
726 /*
adf01747
MD
727 * Finish setup now that we have a good root volume.
728 *
729 * The top 16 bits of fsid.val[1] is a pfs id.
f90dde4c 730 */
a89aec1b
MD
731 ksnprintf(mp->mnt_stat.f_mntfromname,
732 sizeof(mp->mnt_stat.f_mntfromname), "%s",
733 rootvol->ondisk->vol_name);
513ca7d7
MD
734 mp->mnt_stat.f_fsid.val[0] =
735 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
736 mp->mnt_stat.f_fsid.val[1] =
737 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
adf01747 738 mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF;
b84de5af 739
6f97fce3
MD
740 mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
741 mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
742 sizeof(mp->mnt_vstat.f_fsid_uuid));
743
0729c8c8
MD
744 /*
745 * Certain often-modified fields in the root volume are cached in
746 * the hammer_mount structure so we do not have to generate lots
747 * of little UNDO structures for them.
c9b9e29d 748 *
9f5097dc
MD
749 * Recopy after recovery. This also has the side effect of
750 * setting our cached undo FIFO's first_offset, which serves to
751 * placemark the FIFO start for the NEXT flush cycle while the
752 * on-disk first_offset represents the LAST flush cycle.
0729c8c8 753 */
b84de5af 754 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
4889cbd4
MD
755 hmp->flush_tid1 = hmp->next_tid;
756 hmp->flush_tid2 = hmp->next_tid;
0729c8c8
MD
757 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
758 sizeof(hmp->blockmap));
e63644f0 759 hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
0729c8c8 760
059819e3
MD
761 hammer_flusher_create(hmp);
762
a89aec1b 763 /*
27ea2398
MD
764 * Locate the root directory using the root cluster's B-Tree as a
765 * starting point. The root directory uses an obj_id of 1.
766 *
767 * FUTURE: Leave the root directory cached referenced but unlocked
768 * in hmp->rootvp (need to flush it on unmount).
769 */
b9b0a6d0 770 error = hammer_vfs_vget(mp, NULL, 1, &rootvp);
a89aec1b
MD
771 if (error)
772 goto done;
773 vput(rootvp);
27ea2398 774 /*vn_unlock(hmp->rootvp);*/
88c39f64
TN
775 if (hmp->ronly == 0)
776 error = hammer_recover_stage2(hmp, rootvol);
427e5fc6 777
9fa352a2
MD
778 /*
779 * If the stage2 recovery fails be sure to clean out all cached
780 * vnodes before throwing away the mount structure or bad things
781 * will happen.
782 */
783 if (error)
784 vflush(mp, 0, 0);
785
a89aec1b 786done:
f90dde4c
MD
787 hammer_rel_volume(rootvol, 0);
788failed:
427e5fc6 789 /*
27ea2398 790 * Cleanup and return.
427e5fc6 791 */
b0aab9b9
MD
792 if (error) {
793 /* called with fs_token held */
427e5fc6 794 hammer_free_hmp(mp);
b0aab9b9
MD
795 } else {
796 lwkt_reltoken(&hmp->fs_token);
797 }
427e5fc6
MD
798 return (error);
799}
800
801static int
802hammer_vfs_unmount(struct mount *mp, int mntflags)
803{
b0aab9b9 804 hammer_mount_t hmp = (void *)mp->mnt_data;
427e5fc6 805 int flags;
66325755 806 int error;
27ea2398
MD
807
808 /*
427e5fc6
MD
809 * Clean out the vnodes
810 */
b0aab9b9 811 lwkt_gettoken(&hmp->fs_token);
66325755
MD
812 flags = 0;
813 if (mntflags & MNT_FORCE)
814 flags |= FORCECLOSE;
b0aab9b9 815 error = vflush(mp, 0, flags);
427e5fc6
MD
816
817 /*
818 * Clean up the internal mount structure and related entities. This
819 * may issue I/O.
820 */
b0aab9b9
MD
821 if (error == 0) {
822 /* called with fs_token held */
823 hammer_free_hmp(mp);
824 } else {
825 lwkt_reltoken(&hmp->fs_token);
826 }
827 return(error);
427e5fc6
MD
828}
829
830/*
831 * Clean up the internal mount structure and disassociate it from the mount.
832 * This may issue I/O.
b0aab9b9
MD
833 *
834 * Called with fs_token held.
427e5fc6
MD
835 */
836static void
837hammer_free_hmp(struct mount *mp)
838{
b0aab9b9 839 hammer_mount_t hmp = (void *)mp->mnt_data;
cdb6e4e6 840 hammer_flush_group_t flg;
1b0ab2c3 841 int count;
d40bfeca 842 int dummy;
427e5fc6
MD
843
844 /*
cdb6e4e6
MD
845 * Flush anything dirty. This won't even run if the
846 * filesystem errored-out.
427e5fc6 847 */
1b0ab2c3
MD
848 count = 0;
849 while (hammer_flusher_haswork(hmp)) {
850 hammer_flusher_sync(hmp);
851 ++count;
852 if (count >= 5) {
853 if (count == 5)
854 kprintf("HAMMER: umount flushing.");
855 else
856 kprintf(".");
d40bfeca 857 tsleep(&dummy, 0, "hmrufl", hz);
1b0ab2c3
MD
858 }
859 if (count == 30) {
860 kprintf("giving up\n");
861 break;
862 }
863 }
864 if (count >= 5 && count < 30)
865 kprintf("\n");
427e5fc6 866
cdb6e4e6
MD
867 /*
868 * If the mount had a critical error we have to destroy any
869 * remaining inodes before we can finish cleaning up the flusher.
870 */
871 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
872 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
873 hammer_destroy_inode_callback, NULL);
874 }
b84de5af 875
427e5fc6 876 /*
cdb6e4e6
MD
877 * There shouldn't be any inodes left now and any left over
878 * flush groups should now be empty.
427e5fc6 879 */
cdb6e4e6
MD
880 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
881 while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
882 TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
ff003b11 883 KKASSERT(RB_EMPTY(&flg->flush_tree));
cdb6e4e6
MD
884 if (flg->refs) {
885 kprintf("HAMMER: Warning, flush_group %p was "
886 "not empty on umount!\n", flg);
887 }
bac808fe 888 kfree(flg, hmp->m_misc);
cdb6e4e6 889 }
427e5fc6
MD
890
891 /*
cdb6e4e6 892 * We can finally destroy the flusher
427e5fc6 893 */
cdb6e4e6
MD
894 hammer_flusher_destroy(hmp);
895
b84de5af 896 /*
00f16fad
MD
897 * We may have held recovered buffers due to a read-only mount.
898 * These must be discarded.
899 */
900 if (hmp->ronly)
901 hammer_recover_flush_buffers(hmp, NULL, -1);
902
903 /*
0832c9bb 904 * Unload buffers and then volumes
b84de5af 905 */
0832c9bb
MD
906 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
907 hammer_unload_buffer, NULL);
427e5fc6
MD
908 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
909 hammer_unload_volume, NULL);
910
911 mp->mnt_data = NULL;
66325755 912 mp->mnt_flag &= ~MNT_LOCAL;
427e5fc6 913 hmp->mp = NULL;
0729c8c8 914 hammer_destroy_objid_cache(hmp);
507df98a
ID
915 hammer_destroy_dedup_cache(hmp);
916 if (hmp->dedup_free_cache != NULL) {
917 kfree(hmp->dedup_free_cache, hmp->m_misc);
918 hmp->dedup_free_cache = NULL;
919 }
bac808fe
MD
920 kmalloc_destroy(&hmp->m_misc);
921 kmalloc_destroy(&hmp->m_inodes);
b0aab9b9 922 lwkt_reltoken(&hmp->fs_token);
427e5fc6
MD
923 kfree(hmp, M_HAMMER);
924}
925
926/*
cdb6e4e6
MD
927 * Report critical errors. ip may be NULL.
928 */
929void
930hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
931 int error, const char *msg)
932{
933 hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
c9ce54d6 934
cdb6e4e6 935 krateprintf(&hmp->krate,
c9ce54d6
MD
936 "HAMMER(%s): Critical error inode=%jd error=%d %s\n",
937 hmp->mp->mnt_stat.f_mntfromname,
938 (intmax_t)(ip ? ip->obj_id : -1),
939 error, msg);
940
cdb6e4e6
MD
941 if (hmp->ronly == 0) {
942 hmp->ronly = 2; /* special errored read-only mode */
943 hmp->mp->mnt_flag |= MNT_RDONLY;
944 kprintf("HAMMER(%s): Forcing read-only mode\n",
945 hmp->mp->mnt_stat.f_mntfromname);
946 }
947 hmp->error = error;
fc73edd8 948 if (hammer_debug_critical)
c9ce54d6 949 Debugger("Entering debugger");
cdb6e4e6
MD
950}
951
952
953/*
513ca7d7
MD
954 * Obtain a vnode for the specified inode number. An exclusively locked
955 * vnode is returned.
956 */
957int
b9b0a6d0
MD
958hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
959 ino_t ino, struct vnode **vpp)
513ca7d7 960{
36f82b23 961 struct hammer_transaction trans;
513ca7d7
MD
962 struct hammer_mount *hmp = (void *)mp->mnt_data;
963 struct hammer_inode *ip;
964 int error;
b9b0a6d0 965 u_int32_t localization;
513ca7d7 966
b0aab9b9 967 lwkt_gettoken(&hmp->fs_token);
36f82b23
MD
968 hammer_simple_transaction(&trans, hmp);
969
513ca7d7 970 /*
b9b0a6d0
MD
971 * If a directory vnode is supplied (mainly NFS) then we can acquire
972 * the PFS domain from it. Otherwise we would only be able to vget
973 * inodes in the root PFS.
974 */
975 if (dvp) {
976 localization = HAMMER_DEF_LOCALIZATION +
977 VTOI(dvp)->obj_localization;
978 } else {
979 localization = HAMMER_DEF_LOCALIZATION;
980 }
981
982 /*
36f82b23
MD
983 * Lookup the requested HAMMER inode. The structure must be
984 * left unlocked while we manipulate the related vnode to avoid
985 * a deadlock.
513ca7d7 986 */
ddfdf542 987 ip = hammer_get_inode(&trans, NULL, ino,
b9b0a6d0 988 hmp->asof, localization,
ddfdf542 989 0, &error);
513ca7d7
MD
990 if (ip == NULL) {
991 *vpp = NULL;
b0aab9b9
MD
992 } else {
993 error = hammer_get_vnode(ip, vpp);
994 hammer_rel_inode(ip, 0);
513ca7d7 995 }
b84de5af 996 hammer_done_transaction(&trans);
b0aab9b9 997 lwkt_reltoken(&hmp->fs_token);
513ca7d7
MD
998 return (error);
999}
1000
1001/*
427e5fc6
MD
1002 * Return the root vnode for the filesystem.
1003 *
1004 * HAMMER stores the root vnode in the hammer_mount structure so
1005 * getting it is easy.
1006 */
1007static int
1008hammer_vfs_root(struct mount *mp, struct vnode **vpp)
1009{
27ea2398 1010 int error;
427e5fc6 1011
b9b0a6d0 1012 error = hammer_vfs_vget(mp, NULL, 1, vpp);
27ea2398 1013 return (error);
427e5fc6
MD
1014}
1015
1016static int
1017hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1018{
fbc6e32a
MD
1019 struct hammer_mount *hmp = (void *)mp->mnt_data;
1020 hammer_volume_t volume;
1021 hammer_volume_ondisk_t ondisk;
1022 int error;
47197d71 1023 int64_t bfree;
31a56ce2 1024 int64_t breserved;
fbc6e32a 1025
b0aab9b9 1026 lwkt_gettoken(&hmp->fs_token);
fbc6e32a 1027 volume = hammer_get_root_volume(hmp, &error);
b0aab9b9
MD
1028 if (error) {
1029 lwkt_reltoken(&hmp->fs_token);
fbc6e32a 1030 return(error);
b0aab9b9 1031 }
fbc6e32a
MD
1032 ondisk = volume->ondisk;
1033
47197d71
MD
1034 /*
1035 * Basic stats
1036 */
31a56ce2 1037 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
fbc6e32a 1038 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
c3be93f2 1039 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
40043e7f 1040 hammer_rel_volume(volume, 0);
47197d71 1041
31a56ce2 1042 mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
47197d71 1043 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
fbc6e32a
MD
1044 if (mp->mnt_stat.f_files < 0)
1045 mp->mnt_stat.f_files = 0;
1046
27ea2398 1047 *sbp = mp->mnt_stat;
b0aab9b9 1048 lwkt_reltoken(&hmp->fs_token);
27ea2398 1049 return(0);
427e5fc6
MD
1050}
1051
6f97fce3
MD
1052static int
1053hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1054{
1055 struct hammer_mount *hmp = (void *)mp->mnt_data;
1056 hammer_volume_t volume;
1057 hammer_volume_ondisk_t ondisk;
1058 int error;
1059 int64_t bfree;
0f65be10 1060 int64_t breserved;
6f97fce3 1061
b0aab9b9 1062 lwkt_gettoken(&hmp->fs_token);
6f97fce3 1063 volume = hammer_get_root_volume(hmp, &error);
b0aab9b9
MD
1064 if (error) {
1065 lwkt_reltoken(&hmp->fs_token);
6f97fce3 1066 return(error);
b0aab9b9 1067 }
6f97fce3
MD
1068 ondisk = volume->ondisk;
1069
1070 /*
1071 * Basic stats
1072 */
0f65be10 1073 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
6f97fce3
MD
1074 mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
1075 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
1076 hammer_rel_volume(volume, 0);
1077
0f65be10 1078 mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
c0763659 1079 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
6f97fce3
MD
1080 if (mp->mnt_vstat.f_files < 0)
1081 mp->mnt_vstat.f_files = 0;
1082 *sbp = mp->mnt_vstat;
b0aab9b9 1083 lwkt_reltoken(&hmp->fs_token);
6f97fce3
MD
1084 return(0);
1085}
1086
0729c8c8
MD
1087/*
1088 * Sync the filesystem. Currently we have to run it twice, the second
1089 * one will advance the undo start index to the end index, so if a crash
1090 * occurs no undos will be run on mount.
77062c8a
MD
1091 *
1092 * We do not sync the filesystem if we are called from a panic. If we did
1093 * we might end up blowing up a sync that was already in progress.
0729c8c8 1094 */
427e5fc6
MD
1095static int
1096hammer_vfs_sync(struct mount *mp, int waitfor)
1097{
fbc6e32a 1098 struct hammer_mount *hmp = (void *)mp->mnt_data;
0729c8c8
MD
1099 int error;
1100
b0aab9b9 1101 lwkt_gettoken(&hmp->fs_token);
77062c8a 1102 if (panicstr == NULL) {
0729c8c8 1103 error = hammer_sync_hmp(hmp, waitfor);
77062c8a
MD
1104 } else {
1105 error = EIO;
77062c8a 1106 }
b0aab9b9 1107 lwkt_reltoken(&hmp->fs_token);
0729c8c8 1108 return (error);
427e5fc6
MD
1109}
1110
513ca7d7
MD
1111/*
1112 * Convert a vnode to a file handle.
b0aab9b9
MD
1113 *
1114 * Accesses read-only fields on already-referenced structures so
1115 * no token is needed.
513ca7d7
MD
1116 */
1117static int
1118hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1119{
1120 hammer_inode_t ip;
1121
1122 KKASSERT(MAXFIDSZ >= 16);
1123 ip = VTOI(vp);
1124 fhp->fid_len = offsetof(struct fid, fid_data[16]);
adf01747 1125 fhp->fid_ext = ip->obj_localization >> 16;
513ca7d7
MD
1126 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
1127 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
1128 return(0);
1129}
1130
1131
1132/*
1133 * Convert a file handle back to a vnode.
67863d04
MD
1134 *
1135 * Use rootvp to enforce PFS isolation when a PFS is exported via a
1136 * null mount.
513ca7d7
MD
1137 */
1138static int
67863d04
MD
1139hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
1140 struct fid *fhp, struct vnode **vpp)
513ca7d7 1141{
b0aab9b9 1142 hammer_mount_t hmp = (void *)mp->mnt_data;
36f82b23 1143 struct hammer_transaction trans;
513ca7d7
MD
1144 struct hammer_inode *ip;
1145 struct hammer_inode_info info;
1146 int error;
adf01747 1147 u_int32_t localization;
513ca7d7
MD
1148
1149 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
1150 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
67863d04
MD
1151 if (rootvp)
1152 localization = VTOI(rootvp)->obj_localization;
1153 else
1154 localization = (u_int32_t)fhp->fid_ext << 16;
513ca7d7 1155
b0aab9b9
MD
1156 lwkt_gettoken(&hmp->fs_token);
1157 hammer_simple_transaction(&trans, hmp);
36f82b23 1158
513ca7d7
MD
1159 /*
1160 * Get/allocate the hammer_inode structure. The structure must be
1161 * unlocked while we manipulate the related vnode to avoid a
1162 * deadlock.
1163 */
ddfdf542 1164 ip = hammer_get_inode(&trans, NULL, info.obj_id,
adf01747 1165 info.obj_asof, localization, 0, &error);
d978e7cf
MD
1166 if (ip) {
1167 error = hammer_get_vnode(ip, vpp);
1168 hammer_rel_inode(ip, 0);
1169 } else {
513ca7d7 1170 *vpp = NULL;
513ca7d7 1171 }
b84de5af 1172 hammer_done_transaction(&trans);
b0aab9b9 1173 lwkt_reltoken(&hmp->fs_token);
513ca7d7
MD
1174 return (error);
1175}
1176
1177static int
1178hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1179 int *exflagsp, struct ucred **credanonp)
1180{
1181 hammer_mount_t hmp = (void *)mp->mnt_data;
1182 struct netcred *np;
1183 int error;
1184
b0aab9b9 1185 lwkt_gettoken(&hmp->fs_token);
513ca7d7
MD
1186 np = vfs_export_lookup(mp, &hmp->export, nam);
1187 if (np) {
1188 *exflagsp = np->netc_exflags;
1189 *credanonp = &np->netc_anon;
1190 error = 0;
1191 } else {
1192 error = EACCES;
1193 }
b0aab9b9 1194 lwkt_reltoken(&hmp->fs_token);
513ca7d7
MD
1195 return (error);
1196
1197}
1198
1199int
1200hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
1201{
1202 hammer_mount_t hmp = (void *)mp->mnt_data;
1203 int error;
1204
b0aab9b9
MD
1205 lwkt_gettoken(&hmp->fs_token);
1206
513ca7d7
MD
1207 switch(op) {
1208 case MOUNTCTL_SET_EXPORT:
1209 error = vfs_export(mp, &hmp->export, export);
1210 break;
1211 default:
1212 error = EOPNOTSUPP;
1213 break;
1214 }
b0aab9b9
MD
1215 lwkt_reltoken(&hmp->fs_token);
1216
513ca7d7
MD
1217 return(error);
1218}
1219