2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 #include <sys/mountctl.h>
40 * NOTE! Global statistics may not be MPSAFE so HAMMER never uses them
43 int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT;
45 int hammer_debug_general;
46 int hammer_debug_inode;
47 int hammer_debug_locks;
48 int hammer_debug_btree;
50 int hammer_debug_recover; /* -1 will disable, +1 will force */
51 int hammer_debug_critical; /* non-zero enter debugger on error */
52 int hammer_cluster_enable = 2; /* ena cluster_read, scale x 2 */
53 int hammer_live_dedup = 0;
54 int hammer_tdmux_ticks;
55 int hammer_count_fsyncs;
56 int hammer_count_inodes;
57 int hammer_count_iqueued;
58 int hammer_count_reclaims;
59 int hammer_count_records;
60 int hammer_count_record_datas;
61 int hammer_count_volumes;
62 int hammer_count_buffers;
63 int hammer_count_nodes;
64 int64_t hammer_stats_btree_lookups;
65 int64_t hammer_stats_btree_searches;
66 int64_t hammer_stats_btree_inserts;
67 int64_t hammer_stats_btree_deletes;
68 int64_t hammer_stats_btree_elements;
69 int64_t hammer_stats_btree_splits;
70 int64_t hammer_stats_btree_iterations;
71 int64_t hammer_stats_btree_root_iterations;
72 int64_t hammer_stats_record_iterations;
74 int64_t hammer_stats_file_read;
75 int64_t hammer_stats_file_write;
76 int64_t hammer_stats_disk_read;
77 int64_t hammer_stats_disk_write;
78 int64_t hammer_stats_inode_flushes;
79 int64_t hammer_stats_commits;
80 int64_t hammer_stats_undo;
81 int64_t hammer_stats_redo;
83 long hammer_count_dirtybufspace; /* global */
84 int hammer_count_refedbufs; /* global */
85 int hammer_count_reservations;
86 long hammer_count_io_running_read;
87 long hammer_count_io_running_write;
88 int hammer_count_io_locked;
89 long hammer_limit_dirtybufspace; /* per-mount */
90 int hammer_limit_recs; /* as a whole XXX */
91 int hammer_limit_inode_recs = 2048; /* per inode */
92 int hammer_limit_reclaims;
93 int hammer_live_dedup_cache_size = 4096;
94 int hammer_limit_redo = 4096 * 1024; /* per inode */
95 int hammer_autoflush = 500; /* auto flush (typ on reclaim) */
96 int hammer_verify_zone;
97 int hammer_verify_data = 1;
98 int hammer_double_buffer;
99 int hammer_btree_full_undo = 1;
100 int hammer_yield_check = 16;
101 int hammer_fsync_mode = 3;
102 int64_t hammer_contention_count;
104 int hammer_noatime = 1;
105 TUNABLE_INT("vfs.hammer.noatime", &hammer_noatime);
108 * Live dedup debug counters (sysctls are writable so that counters
109 * can be reset from userspace).
111 int64_t hammer_live_dedup_vnode_bcmps = 0;
112 int64_t hammer_live_dedup_device_bcmps = 0;
113 int64_t hammer_live_dedup_findblk_failures = 0;
114 int64_t hammer_live_dedup_bmap_saves = 0;
117 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
119 SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD,
120 &hammer_supported_version, 0, "");
121 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
122 &hammer_debug_general, 0, "");
123 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
124 &hammer_debug_io, 0, "");
125 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
126 &hammer_debug_inode, 0, "");
127 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
128 &hammer_debug_locks, 0, "");
129 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
130 &hammer_debug_btree, 0, "");
131 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
132 &hammer_debug_tid, 0, "");
133 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
134 &hammer_debug_recover, 0, "");
135 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW,
136 &hammer_debug_critical, 0, "");
137 SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
138 &hammer_cluster_enable, 0, "");
140 * 0 - live dedup is disabled
141 * 1 - dedup cache is populated on reads only
142 * 2 - dedup cache is populated on both reads and writes
144 * LIVE_DEDUP IS DISABLED PERMANENTLY! This feature appears to cause
145 * blockmap corruption over time so we've turned it off permanently.
147 SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup, CTLFLAG_RD,
148 &hammer_live_dedup, 0, "Enable live dedup (experimental)");
149 SYSCTL_INT(_vfs_hammer, OID_AUTO, tdmux_ticks, CTLFLAG_RW,
150 &hammer_tdmux_ticks, 0, "Hammer tdmux ticks");
152 SYSCTL_LONG(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
153 &hammer_limit_dirtybufspace, 0, "");
154 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
155 &hammer_limit_recs, 0, "");
156 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW,
157 &hammer_limit_inode_recs, 0, "");
158 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaims, CTLFLAG_RW,
159 &hammer_limit_reclaims, 0, "");
160 SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW,
161 &hammer_live_dedup_cache_size, 0,
162 "Number of cache entries");
163 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW,
164 &hammer_limit_redo, 0, "");
166 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
167 &hammer_count_fsyncs, 0, "");
168 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
169 &hammer_count_inodes, 0, "");
170 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
171 &hammer_count_iqueued, 0, "");
172 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaims, CTLFLAG_RD,
173 &hammer_count_reclaims, 0, "");
174 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
175 &hammer_count_records, 0, "");
176 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
177 &hammer_count_record_datas, 0, "");
178 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
179 &hammer_count_volumes, 0, "");
180 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
181 &hammer_count_buffers, 0, "");
182 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
183 &hammer_count_nodes, 0, "");
185 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
186 &hammer_stats_btree_searches, 0, "");
187 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
188 &hammer_stats_btree_lookups, 0, "");
189 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
190 &hammer_stats_btree_inserts, 0, "");
191 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
192 &hammer_stats_btree_deletes, 0, "");
193 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
194 &hammer_stats_btree_elements, 0, "");
195 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
196 &hammer_stats_btree_splits, 0, "");
197 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
198 &hammer_stats_btree_iterations, 0, "");
199 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD,
200 &hammer_stats_btree_root_iterations, 0, "");
201 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
202 &hammer_stats_record_iterations, 0, "");
204 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
205 &hammer_stats_file_read, 0, "");
206 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
207 &hammer_stats_file_write, 0, "");
208 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
209 &hammer_stats_disk_read, 0, "");
210 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
211 &hammer_stats_disk_write, 0, "");
212 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
213 &hammer_stats_inode_flushes, 0, "");
214 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
215 &hammer_stats_commits, 0, "");
216 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD,
217 &hammer_stats_undo, 0, "");
218 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD,
219 &hammer_stats_redo, 0, "");
221 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_vnode_bcmps, CTLFLAG_RW,
222 &hammer_live_dedup_vnode_bcmps, 0,
223 "successful vnode buffer comparisons");
224 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_device_bcmps, CTLFLAG_RW,
225 &hammer_live_dedup_device_bcmps, 0,
226 "successful device buffer comparisons");
227 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_findblk_failures, CTLFLAG_RW,
228 &hammer_live_dedup_findblk_failures, 0,
229 "block lookup failures for comparison");
230 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_bmap_saves, CTLFLAG_RW,
231 &hammer_live_dedup_bmap_saves, 0,
232 "useful physical block lookups");
234 SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
235 &hammer_count_dirtybufspace, 0, "");
236 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
237 &hammer_count_refedbufs, 0, "");
238 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
239 &hammer_count_reservations, 0, "");
240 SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
241 &hammer_count_io_running_read, 0, "");
242 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
243 &hammer_count_io_locked, 0, "");
244 SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
245 &hammer_count_io_running_write, 0, "");
246 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
247 &hammer_contention_count, 0, "");
248 SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
249 &hammer_autoflush, 0, "");
250 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
251 &hammer_verify_zone, 0, "");
252 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
253 &hammer_verify_data, 0, "");
254 SYSCTL_INT(_vfs_hammer, OID_AUTO, double_buffer, CTLFLAG_RW,
255 &hammer_double_buffer, 0, "");
256 SYSCTL_INT(_vfs_hammer, OID_AUTO, btree_full_undo, CTLFLAG_RW,
257 &hammer_btree_full_undo, 0, "");
258 SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW,
259 &hammer_yield_check, 0, "");
260 SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW,
261 &hammer_fsync_mode, 0, "");
263 /* KTR_INFO_MASTER(hammer); */
268 static void hammer_free_hmp(struct mount *mp);
270 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
272 static int hammer_vfs_unmount(struct mount *mp, int mntflags);
273 static int hammer_vfs_root(struct mount *mp, struct vnode **vpp);
274 static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
276 static int hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
278 static int hammer_vfs_sync(struct mount *mp, int waitfor);
279 static int hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
280 ino_t ino, struct vnode **vpp);
281 static int hammer_vfs_init(struct vfsconf *conf);
282 static int hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
283 struct fid *fhp, struct vnode **vpp);
284 static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
285 static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
286 int *exflagsp, struct ucred **credanonp);
289 static struct vfsops hammer_vfsops = {
290 .vfs_mount = hammer_vfs_mount,
291 .vfs_unmount = hammer_vfs_unmount,
292 .vfs_root = hammer_vfs_root,
293 .vfs_statfs = hammer_vfs_statfs,
294 .vfs_statvfs = hammer_vfs_statvfs,
295 .vfs_sync = hammer_vfs_sync,
296 .vfs_vget = hammer_vfs_vget,
297 .vfs_init = hammer_vfs_init,
298 .vfs_vptofh = hammer_vfs_vptofh,
299 .vfs_fhtovp = hammer_vfs_fhtovp,
300 .vfs_checkexp = hammer_vfs_checkexp
303 MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", "");
305 VFS_SET(hammer_vfsops, hammer, 0);
306 MODULE_VERSION(hammer, 1);
309 hammer_vfs_init(struct vfsconf *conf)
314 * Wait up to this long for an exclusive deadlock to clear
315 * before acquiring a new shared lock on the ip. The deadlock
316 * may have occured on a b-tree node related to the ip.
318 if (hammer_tdmux_ticks == 0)
319 hammer_tdmux_ticks = hz / 5;
322 * Autosize, but be careful because a hammer filesystem's
323 * reserve is partially calculated based on dirtybufspace,
324 * so we simply cannot allow it to get too large.
326 if (hammer_limit_recs == 0) {
328 if (n > kmalloc_limit(M_HAMMER) / 512)
329 n = kmalloc_limit(M_HAMMER) / 512;
330 if (n > 2 * 1024 * 1024)
332 hammer_limit_recs = (int)n;
334 if (hammer_limit_dirtybufspace == 0) {
335 hammer_limit_dirtybufspace = hidirtybufspace / 2;
336 if (hammer_limit_dirtybufspace < 1L * 1024 * 1024)
337 hammer_limit_dirtybufspace = 1024L * 1024;
338 if (hammer_limit_dirtybufspace > 1024L * 1024 * 1024)
339 hammer_limit_dirtybufspace = 1024L * 1024 * 1024;
343 * The hammer_inode structure detaches from the vnode on reclaim.
344 * This limits the number of inodes in this state to prevent a
345 * memory pool blowout.
347 if (hammer_limit_reclaims == 0) {
348 hammer_limit_reclaims = maxvnodes / 10;
349 if (hammer_limit_reclaims > HAMMER_LIMIT_RECLAIMS)
350 hammer_limit_reclaims = HAMMER_LIMIT_RECLAIMS;
357 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
360 struct hammer_mount_info info;
362 hammer_volume_t rootvol;
363 struct vnode *rootvp;
364 struct vnode *devvp = NULL;
365 const char *upath; /* volume name in userspace */
366 char *path; /* volume name in system space */
371 char *next_volume_ptr = NULL;
373 if (hammer_noatime) {
375 mp->mnt_flag |= MNT_NOATIME;
379 * Accept hammer_mount_info. mntpt is NULL for root mounts at boot.
382 bzero(&info, sizeof(info));
387 next_volume_ptr = mp->mnt_stat.f_mntfromname;
389 /* Count number of volumes separated by ':' */
390 for (char *p = next_volume_ptr; *p != '\0'; ++p) {
396 mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
398 if ((error = copyin(data, &info, sizeof(info))) != 0)
403 * updating or new mount
405 if (mp->mnt_flag & MNT_UPDATE) {
406 hmp = (void *)mp->mnt_data;
407 KKASSERT(hmp != NULL);
409 if (info.nvolumes <= 0 || info.nvolumes > HAMMER_MAX_VOLUMES)
415 * master-id validation. The master id may not be changed by a
418 if (info.hflags & HMNT_MASTERID || info.hflags & HMNT_NOMIRROR) {
419 if (hmp && hmp->master_id != info.master_id) {
420 hkprintf("cannot change master id with mount update\n");
423 master_id = info.master_id;
424 if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
428 master_id = hmp->master_id;
434 * Internal mount data structure
437 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
438 mp->mnt_data = (qaddr_t)hmp;
442 * Make sure kmalloc type limits are set appropriately.
444 * Our inode kmalloc group is sized based on maxvnodes
445 * (controlled by the system, not us).
447 kmalloc_create(&hmp->m_misc, "HAMMER-others");
448 kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
450 kmalloc_raise_limit(hmp->m_inodes, 0); /* unlimited */
452 hmp->root_btree_beg.localization =
453 HAMMER_MIN_ONDISK_LOCALIZATION;
454 hmp->root_btree_beg.obj_id = HAMMER_MIN_OBJID;
455 hmp->root_btree_beg.key = HAMMER_MIN_KEY;
456 hmp->root_btree_beg.create_tid = 1;
457 hmp->root_btree_beg.delete_tid = 1;
458 hmp->root_btree_beg.rec_type = HAMMER_MIN_RECTYPE;
459 hmp->root_btree_beg.obj_type = 0;
460 hmp->root_btree_beg.btype = HAMMER_BTREE_TYPE_NONE;
462 hmp->root_btree_end.localization =
463 HAMMER_MAX_ONDISK_LOCALIZATION;
464 hmp->root_btree_end.obj_id = HAMMER_MAX_OBJID;
465 hmp->root_btree_end.key = HAMMER_MAX_KEY;
466 hmp->root_btree_end.create_tid = HAMMER_MAX_TID;
467 hmp->root_btree_end.delete_tid = 0; /* special case */
468 hmp->root_btree_end.rec_type = HAMMER_MAX_RECTYPE;
469 hmp->root_btree_end.obj_type = 0;
470 hmp->root_btree_end.btype = HAMMER_BTREE_TYPE_NONE;
472 hmp->krate.freq = 1; /* maximum reporting rate (hz) */
473 hmp->krate.count = -16; /* initial burst */
474 hmp->kdiag.freq = 1; /* maximum reporting rate (hz) */
475 hmp->kdiag.count = -16; /* initial burst */
477 hmp->sync_lock.refs = 1;
478 hmp->undo_lock.refs = 1;
479 hmp->blkmap_lock.refs = 1;
480 hmp->snapshot_lock.refs = 1;
481 hmp->volume_lock.refs = 1;
483 TAILQ_INIT(&hmp->delay_list);
484 TAILQ_INIT(&hmp->flush_group_list);
485 TAILQ_INIT(&hmp->objid_cache_list);
486 TAILQ_INIT(&hmp->undo_lru_list);
487 TAILQ_INIT(&hmp->reclaim_list);
489 RB_INIT(&hmp->rb_dedup_crc_root);
490 RB_INIT(&hmp->rb_dedup_off_root);
491 TAILQ_INIT(&hmp->dedup_lru_list);
493 hmp->hflags &= ~HMNT_USERFLAGS;
494 hmp->hflags |= info.hflags & HMNT_USERFLAGS;
496 hmp->master_id = master_id;
499 mp->mnt_flag |= MNT_RDONLY;
500 hmp->asof = info.asof;
502 hmp->asof = HAMMER_MAX_TID;
505 hmp->volume_to_remove = -1;
508 * Re-open read-write if originally read-only, or vise-versa.
510 * When going from read-only to read-write execute the stage2
511 * recovery if it has not already been run.
513 if (mp->mnt_flag & MNT_UPDATE) {
514 lwkt_gettoken(&hmp->fs_token);
516 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
517 hkprintf("read-only -> read-write\n");
519 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
520 hammer_adjust_volume_mode, NULL);
521 rootvol = hammer_get_root_volume(hmp, &error);
523 hammer_recover_flush_buffers(hmp, rootvol, 1);
524 error = hammer_recover_stage2(hmp, rootvol);
525 bcopy(rootvol->ondisk->vol0_blockmap,
527 sizeof(hmp->blockmap));
528 hammer_rel_volume(rootvol, 0);
530 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
531 hammer_reload_inode, NULL);
532 /* kernel clears MNT_RDONLY */
533 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
534 hkprintf("read-write -> read-only\n");
535 hmp->ronly = 1; /* messy */
536 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
537 hammer_reload_inode, NULL);
539 hammer_flusher_sync(hmp);
540 hammer_flusher_sync(hmp);
541 hammer_flusher_sync(hmp);
543 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
544 hammer_adjust_volume_mode, NULL);
546 lwkt_reltoken(&hmp->fs_token);
550 RB_INIT(&hmp->rb_vols_root);
551 RB_INIT(&hmp->rb_inos_root);
552 RB_INIT(&hmp->rb_redo_root);
553 RB_INIT(&hmp->rb_nods_root);
554 RB_INIT(&hmp->rb_undo_root);
555 RB_INIT(&hmp->rb_resv_root);
556 RB_INIT(&hmp->rb_bufs_root);
557 RB_INIT(&hmp->rb_pfsm_root);
559 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
561 RB_INIT(&hmp->volu_root);
562 RB_INIT(&hmp->undo_root);
563 RB_INIT(&hmp->data_root);
564 RB_INIT(&hmp->meta_root);
565 RB_INIT(&hmp->lose_root);
566 TAILQ_INIT(&hmp->iorun_list);
568 lwkt_token_init(&hmp->fs_token, "hammerfs");
569 lwkt_token_init(&hmp->io_token, "hammerio");
571 lwkt_gettoken(&hmp->fs_token);
576 path = objcache_get(namei_oc, M_WAITOK);
578 for (i = 0; i < info.nvolumes; ++i) {
583 KKASSERT(next_volume_ptr != NULL);
585 if (*next_volume_ptr != '/') {
587 strcpy(path, "/dev/");
590 for (k = strlen(path); k < MAXPATHLEN-1; ++k) {
591 if (*next_volume_ptr == '\0') {
593 } else if (*next_volume_ptr == ':') {
597 path[k] = *next_volume_ptr;
604 cdev_t dev = kgetdiskbyname(path);
605 error = bdevvp(dev, &devvp);
607 hdkprintf("can't find devvp\n");
610 error = copyin(&info.volumes[i], &upath,
613 error = copyinstr(upath, path,
617 error = hammer_install_volume(hmp, path, devvp, NULL);
621 objcache_put(namei_oc, path);
624 * Make sure we found a root volume
626 if (hmp->rootvol == NULL) {
627 if (error == EBUSY) {
628 hdkprintf("The volumes are probably mounted\n");
630 hdkprintf("No root volume found!\n");
637 * Check that all required volumes are available
639 if (error == 0 && hammer_mountcheck_volumes(hmp)) {
640 hdkprintf("Missing volumes, cannot mount!\n");
649 hdkprintf("Failed to load volumes!\n");
653 nvolumes = hammer_get_installed_volumes(hmp);
654 if (hmp->nvolumes != nvolumes) {
655 hdkprintf("volume header says %d volumes, but %d installed\n",
656 hmp->nvolumes, nvolumes);
662 * No errors, setup enough of the mount point so we can lookup the
665 mp->mnt_iosize_max = MAXPHYS;
666 mp->mnt_kern_flag |= MNTK_FSMID;
667 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
670 * MPSAFE code. Note that VOPs and VFSops which are not MPSAFE
671 * will acquire a per-mount token prior to entry and release it
674 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
677 * note: f_iosize is used by vnode_pager_haspage() when constructing
680 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
681 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
683 mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
684 mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
686 mp->mnt_maxsymlinklen = 255;
687 mp->mnt_flag |= MNT_LOCAL;
689 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
690 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
691 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
694 * The root volume's ondisk pointer is only valid if we hold a
697 rootvol = hammer_get_root_volume(hmp, &error);
702 * Perform any necessary UNDO operations. The recovery code does
703 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
704 * and then re-copy it again after recovery is complete.
706 * If this is a read-only mount the UNDO information is retained
707 * in memory in the form of dirty buffer cache buffers, and not
708 * written back to the media.
710 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
711 sizeof(hmp->blockmap));
714 * Check filesystem version
716 hmp->version = rootvol->ondisk->vol_version;
717 if (hmp->version < HAMMER_VOL_VERSION_MIN ||
718 hmp->version > HAMMER_VOL_VERSION_MAX) {
719 hkprintf("mount unsupported fs version %d\n", hmp->version);
725 * The undo_rec_limit limits the size of flush groups to avoid
726 * blowing out the UNDO FIFO. This calculation is typically in
727 * the tens of thousands and is designed primarily when small
728 * HAMMER filesystems are created.
730 hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
731 if (hammer_debug_general & 0x0001)
732 hkprintf("undo_rec_limit %d\n", hmp->undo_rec_limit);
735 * NOTE: Recover stage1 not only handles meta-data recovery, it
736 * also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems.
738 error = hammer_recover_stage1(hmp, rootvol);
740 kprintf("Failed to recover HAMMER filesystem on mount\n");
745 * Finish setup now that we have a good root volume.
747 ksnprintf(mp->mnt_stat.f_mntfromname,
748 sizeof(mp->mnt_stat.f_mntfromname), "%s",
749 rootvol->ondisk->vol_label);
750 mp->mnt_stat.f_fsid.val[0] =
751 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
752 mp->mnt_stat.f_fsid.val[1] =
753 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
754 mp->mnt_stat.f_fsid.val[1] &= HAMMER_LOCALIZE_MASK;
756 mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
757 mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
758 sizeof(mp->mnt_vstat.f_fsid_uuid));
761 * Certain often-modified fields in the root volume are cached in
762 * the hammer_mount structure so we do not have to generate lots
763 * of little UNDO structures for them.
765 * Recopy after recovery. This also has the side effect of
766 * setting our cached undo FIFO's first_offset, which serves to
767 * placemark the FIFO start for the NEXT flush cycle while the
768 * on-disk first_offset represents the LAST flush cycle.
770 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
771 hmp->flush_tid1 = hmp->next_tid;
772 hmp->flush_tid2 = hmp->next_tid;
773 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
774 sizeof(hmp->blockmap));
775 hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
777 hammer_flusher_create(hmp);
780 * Locate the root directory with an obj_id of 1.
782 error = hammer_vfs_root(mp, &rootvp);
787 error = hammer_recover_stage2(hmp, rootvol);
790 * If the stage2 recovery fails be sure to clean out all cached
791 * vnodes before throwing away the mount structure or bad things
798 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
801 /* Populate info for mount point (NULL pad)*/
802 bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
805 copyinstr(mntpt, mp->mnt_stat.f_mntonname,
807 } else { /* Root mount */
808 mp->mnt_stat.f_mntonname[0] = '/';
811 (void)VFS_STATFS(mp, &mp->mnt_stat, cred);
812 hammer_rel_volume(rootvol, 0);
815 * Cleanup and return.
818 /* called with fs_token held */
821 lwkt_reltoken(&hmp->fs_token);
827 hammer_vfs_unmount(struct mount *mp, int mntflags)
829 hammer_mount_t hmp = (void *)mp->mnt_data;
834 * Clean out the vnodes
836 lwkt_gettoken(&hmp->fs_token);
838 if (mntflags & MNT_FORCE)
840 error = vflush(mp, 0, flags);
843 * Clean up the internal mount structure and related entities. This
847 /* called with fs_token held */
850 lwkt_reltoken(&hmp->fs_token);
856 * Clean up the internal mount structure and disassociate it from the mount.
857 * This may issue I/O.
859 * Called with fs_token held.
862 hammer_free_hmp(struct mount *mp)
864 hammer_mount_t hmp = (void *)mp->mnt_data;
865 hammer_flush_group_t flg;
868 * Flush anything dirty. This won't even run if the
869 * filesystem errored-out.
871 hammer_flush_dirty(hmp, 30);
874 * If the mount had a critical error we have to destroy any
875 * remaining inodes before we can finish cleaning up the flusher.
877 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
878 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
879 hammer_destroy_inode_callback, NULL);
883 * There shouldn't be any inodes left now and any left over
884 * flush groups should now be empty.
886 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
887 while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
888 TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
889 KKASSERT(RB_EMPTY(&flg->flush_tree));
891 hkprintf("Warning, flush_group %p was "
892 "not empty on umount!\n", flg);
894 kfree(flg, hmp->m_misc);
898 * We can finally destroy the flusher
900 hammer_flusher_destroy(hmp);
903 * We may have held recovered buffers due to a read-only mount.
904 * These must be discarded.
907 hammer_recover_flush_buffers(hmp, NULL, -1);
910 * Unload buffers and then volumes
912 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
913 hammer_unload_buffer, NULL);
914 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
915 hammer_unload_volume, NULL);
918 mp->mnt_flag &= ~MNT_LOCAL;
920 hammer_destroy_objid_cache(hmp);
921 hammer_destroy_dedup_cache(hmp);
922 if (hmp->dedup_free_cache != NULL) {
923 kfree(hmp->dedup_free_cache, hmp->m_misc);
924 hmp->dedup_free_cache = NULL;
926 kmalloc_destroy(&hmp->m_misc);
927 kmalloc_destroy(&hmp->m_inodes);
928 lwkt_reltoken(&hmp->fs_token);
929 kfree(hmp, M_HAMMER);
933 * Report critical errors. ip may be NULL.
936 hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
937 int error, const char *msg)
939 hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
941 hmkrateprintf(&hmp->krate, hmp,
942 "Critical error inode=%jd error=%d %s\n",
943 (intmax_t)(ip ? ip->obj_id : -1),
946 if (hmp->ronly == 0) {
947 hmp->ronly = 2; /* special errored read-only mode */
948 hmp->mp->mnt_flag |= MNT_RDONLY;
949 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
950 hammer_adjust_volume_mode, NULL);
951 hmkprintf(hmp, "Forcing read-only mode\n");
954 if (hammer_debug_critical)
955 Debugger("Entering debugger");
960 * Obtain a vnode for the specified inode number. An exclusively locked
964 hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
965 ino_t ino, struct vnode **vpp)
967 struct hammer_transaction trans;
968 hammer_mount_t hmp = (void *)mp->mnt_data;
971 uint32_t localization;
973 lwkt_gettoken(&hmp->fs_token);
974 hammer_simple_transaction(&trans, hmp);
977 * If a directory vnode is supplied (mainly NFS) then we can acquire
978 * the PFS domain from it. Otherwise we would only be able to vget
979 * inodes in the root PFS.
982 localization = HAMMER_DEF_LOCALIZATION |
983 VTOI(dvp)->obj_localization;
985 localization = HAMMER_DEF_LOCALIZATION;
989 * Lookup the requested HAMMER inode. The structure must be
990 * left unlocked while we manipulate the related vnode to avoid
993 ip = hammer_get_inode(&trans, NULL, ino,
994 hmp->asof, localization,
999 error = hammer_get_vnode(ip, vpp);
1000 hammer_rel_inode(ip, 0);
1002 hammer_done_transaction(&trans);
1003 lwkt_reltoken(&hmp->fs_token);
1008 * Return the root vnode for the filesystem.
1010 * HAMMER stores the root vnode in the hammer_mount structure so
1011 * getting it is easy.
1014 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
1018 error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, vpp);
1023 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1025 hammer_mount_t hmp = (void *)mp->mnt_data;
1026 hammer_volume_t volume;
1027 hammer_volume_ondisk_t ondisk;
1032 lwkt_gettoken(&hmp->fs_token);
1033 volume = hammer_get_root_volume(hmp, &error);
1035 lwkt_reltoken(&hmp->fs_token);
1038 ondisk = volume->ondisk;
1043 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
1044 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
1045 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
1046 hammer_rel_volume(volume, 0);
1048 if (breserved > bfree)
1050 mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
1051 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1052 if (mp->mnt_stat.f_files < 0)
1053 mp->mnt_stat.f_files = 0;
1055 *sbp = mp->mnt_stat;
1056 lwkt_reltoken(&hmp->fs_token);
1061 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1063 hammer_mount_t hmp = (void *)mp->mnt_data;
1064 hammer_volume_t volume;
1065 hammer_volume_ondisk_t ondisk;
1070 lwkt_gettoken(&hmp->fs_token);
1071 volume = hammer_get_root_volume(hmp, &error);
1073 lwkt_reltoken(&hmp->fs_token);
1076 ondisk = volume->ondisk;
1081 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
1082 mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
1083 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE;
1084 hammer_rel_volume(volume, 0);
1086 if (breserved > bfree)
1088 mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
1089 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1090 if (mp->mnt_vstat.f_files < 0)
1091 mp->mnt_vstat.f_files = 0;
1092 *sbp = mp->mnt_vstat;
1093 lwkt_reltoken(&hmp->fs_token);
1098 * Sync the filesystem. Currently we have to run it twice, the second
1099 * one will advance the undo start index to the end index, so if a crash
1100 * occurs no undos will be run on mount.
1102 * We do not sync the filesystem if we are called from a panic. If we did
1103 * we might end up blowing up a sync that was already in progress.
1106 hammer_vfs_sync(struct mount *mp, int waitfor)
1108 hammer_mount_t hmp = (void *)mp->mnt_data;
1111 lwkt_gettoken(&hmp->fs_token);
1112 if (panicstr == NULL) {
1113 error = hammer_sync_hmp(hmp, waitfor);
1117 lwkt_reltoken(&hmp->fs_token);
1122 * Convert a vnode to a file handle.
1124 * Accesses read-only fields on already-referenced structures so
1125 * no token is needed.
1128 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1132 KKASSERT(MAXFIDSZ >= 16);
1134 fhp->fid_len = offsetof(struct fid, fid_data[16]);
1135 fhp->fid_ext = lo_to_pfs(ip->obj_localization);
1136 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
1137 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
1143 * Convert a file handle back to a vnode.
1145 * Use rootvp to enforce PFS isolation when a PFS is exported via a
1149 hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
1150 struct fid *fhp, struct vnode **vpp)
1152 hammer_mount_t hmp = (void *)mp->mnt_data;
1153 struct hammer_transaction trans;
1155 struct hammer_inode_info info;
1157 uint32_t localization;
1159 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
1160 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
1162 localization = VTOI(rootvp)->obj_localization;
1164 localization = pfs_to_lo(fhp->fid_ext);
1166 lwkt_gettoken(&hmp->fs_token);
1167 hammer_simple_transaction(&trans, hmp);
1170 * Get/allocate the hammer_inode structure. The structure must be
1171 * unlocked while we manipulate the related vnode to avoid a
1174 ip = hammer_get_inode(&trans, NULL, info.obj_id,
1175 info.obj_asof, localization, 0, &error);
1177 error = hammer_get_vnode(ip, vpp);
1178 hammer_rel_inode(ip, 0);
1182 hammer_done_transaction(&trans);
1183 lwkt_reltoken(&hmp->fs_token);
1188 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1189 int *exflagsp, struct ucred **credanonp)
1191 hammer_mount_t hmp = (void *)mp->mnt_data;
1195 lwkt_gettoken(&hmp->fs_token);
1196 np = vfs_export_lookup(mp, &hmp->export, nam);
1198 *exflagsp = np->netc_exflags;
1199 *credanonp = &np->netc_anon;
1204 lwkt_reltoken(&hmp->fs_token);
1210 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
1212 hammer_mount_t hmp = (void *)mp->mnt_data;
1215 lwkt_gettoken(&hmp->fs_token);
1218 case MOUNTCTL_SET_EXPORT:
1219 error = vfs_export(mp, &hmp->export, export);
1225 lwkt_reltoken(&hmp->fs_token);