1427e5fc6SMatthew Dillon /* 2b84de5afSMatthew Dillon * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. 3427e5fc6SMatthew Dillon * 4427e5fc6SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 5427e5fc6SMatthew Dillon * by Matthew Dillon <dillon@backplane.com> 6427e5fc6SMatthew Dillon * 7427e5fc6SMatthew Dillon * Redistribution and use in source and binary forms, with or without 8427e5fc6SMatthew Dillon * modification, are permitted provided that the following conditions 9427e5fc6SMatthew Dillon * are met: 10427e5fc6SMatthew Dillon * 11427e5fc6SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 12427e5fc6SMatthew Dillon * notice, this list of conditions and the following disclaimer. 13427e5fc6SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 14427e5fc6SMatthew Dillon * notice, this list of conditions and the following disclaimer in 15427e5fc6SMatthew Dillon * the documentation and/or other materials provided with the 16427e5fc6SMatthew Dillon * distribution. 17427e5fc6SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 18427e5fc6SMatthew Dillon * contributors may be used to endorse or promote products derived 19427e5fc6SMatthew Dillon * from this software without specific, prior written permission. 20427e5fc6SMatthew Dillon * 21427e5fc6SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22427e5fc6SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23427e5fc6SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24427e5fc6SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25427e5fc6SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26427e5fc6SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27427e5fc6SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28427e5fc6SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29427e5fc6SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30427e5fc6SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31427e5fc6SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32427e5fc6SMatthew Dillon * SUCH DAMAGE. 33427e5fc6SMatthew Dillon */ 34427e5fc6SMatthew Dillon 3597fb61c0STomohiro Kusumi #include <sys/mountctl.h> 36b45803e3STomohiro Kusumi 37427e5fc6SMatthew Dillon #include "hammer.h" 38427e5fc6SMatthew Dillon 39aac0aabdSMatthew Dillon /* 40aac0aabdSMatthew Dillon * NOTE! Global statistics may not be MPSAFE so HAMMER never uses them 41aac0aabdSMatthew Dillon * in conditionals. 42aac0aabdSMatthew Dillon */ 4302428fb6SMatthew Dillon int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT; 442f85fa4dSMatthew Dillon int hammer_debug_io; 45d5ef456eSMatthew Dillon int hammer_debug_general; 46e8599db1SMatthew Dillon int hammer_debug_inode; 477d683b0fSMatthew Dillon int hammer_debug_locks; 48b3deaf57SMatthew Dillon int hammer_debug_btree; 49d113fda1SMatthew Dillon int hammer_debug_tid; 5046fe7ae1SMatthew Dillon int hammer_debug_recover; /* -1 will disable, +1 will force */ 5146fe7ae1SMatthew Dillon int hammer_debug_recover_faults; 52fc73edd8SMatthew Dillon int hammer_debug_critical; /* non-zero enter debugger on error */ 531b0ab2c3SMatthew Dillon int hammer_cluster_enable = 1; /* enable read clustering by default */ 54507df98aSIlya Dryomov int hammer_live_dedup = 0; 55e2a02b72SMatthew Dillon int hammer_tdmux_ticks; 567a61b85dSMatthew Dillon int hammer_count_fsyncs; 57b3deaf57SMatthew Dillon int hammer_count_inodes; 58af209b0fSMatthew Dillon int hammer_count_iqueued; 59e2a02b72SMatthew Dillon int hammer_count_reclaims; 60b3deaf57SMatthew Dillon int hammer_count_records; 61b3deaf57SMatthew Dillon int hammer_count_record_datas; 62b3deaf57SMatthew Dillon int hammer_count_volumes; 63b3deaf57SMatthew Dillon int hammer_count_buffers; 64b3deaf57SMatthew Dillon int hammer_count_nodes; 65a7e9bef1SMatthew Dillon int64_t hammer_count_extra_space_used; 66cb51be26SMatthew Dillon int64_t hammer_stats_btree_lookups; 67cb51be26SMatthew Dillon int64_t hammer_stats_btree_searches; 68cb51be26SMatthew Dillon int64_t hammer_stats_btree_inserts; 69cb51be26SMatthew Dillon int64_t hammer_stats_btree_deletes; 70cb51be26SMatthew Dillon int64_t hammer_stats_btree_elements; 71cb51be26SMatthew Dillon int64_t hammer_stats_btree_splits; 72cb51be26SMatthew Dillon int64_t hammer_stats_btree_iterations; 7339d8fd63SMatthew Dillon int64_t hammer_stats_btree_root_iterations; 74cb51be26SMatthew Dillon int64_t hammer_stats_record_iterations; 75ce0138a6SMatthew Dillon 76ce0138a6SMatthew Dillon int64_t hammer_stats_file_read; 77ce0138a6SMatthew Dillon int64_t hammer_stats_file_write; 78ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsr; 79ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsw; 80ce0138a6SMatthew Dillon int64_t hammer_stats_disk_read; 81ce0138a6SMatthew Dillon int64_t hammer_stats_disk_write; 82ce0138a6SMatthew Dillon int64_t hammer_stats_inode_flushes; 83ce0138a6SMatthew Dillon int64_t hammer_stats_commits; 8489e744ceSMatthew Dillon int64_t hammer_stats_undo; 856048b411SMatthew Dillon int64_t hammer_stats_redo; 86ce0138a6SMatthew Dillon 873583bbb4SMatthew Dillon long hammer_count_dirtybufspace; /* global */ 88a99b9ea2SMatthew Dillon int hammer_count_refedbufs; /* global */ 890832c9bbSMatthew Dillon int hammer_count_reservations; 903583bbb4SMatthew Dillon long hammer_count_io_running_read; 913583bbb4SMatthew Dillon long hammer_count_io_running_write; 92a99b9ea2SMatthew Dillon int hammer_count_io_locked; 933583bbb4SMatthew Dillon long hammer_limit_dirtybufspace; /* per-mount */ 940832c9bbSMatthew Dillon int hammer_limit_recs; /* as a whole XXX */ 95a117fbebSMatthew Dillon int hammer_limit_inode_recs = 2048; /* per inode */ 96e2a02b72SMatthew Dillon int hammer_limit_reclaims; 97e2ef7a95SIlya Dryomov int hammer_live_dedup_cache_size = DEDUP_CACHE_SIZE; 986048b411SMatthew Dillon int hammer_limit_redo = 4096 * 1024; /* per inode */ 9937646115SMatthew Dillon int hammer_autoflush = 500; /* auto flush (typ on reclaim) */ 1001f07f686SMatthew Dillon int hammer_bio_count; 101cb51be26SMatthew Dillon int hammer_verify_zone; 1021b0ab2c3SMatthew Dillon int hammer_verify_data = 1; 103cb51be26SMatthew Dillon int hammer_write_mode; 104b4f86ea3SMatthew Dillon int hammer_double_buffer; 105283a4a38SMatthew Dillon int hammer_btree_full_undo = 1; 1063e583440SMatthew Dillon int hammer_yield_check = 16; 1079d0a6205SMatthew Dillon int hammer_fsync_mode = 3; 1087d683b0fSMatthew Dillon int64_t hammer_contention_count; 109f03c9cf4SMatthew Dillon int64_t hammer_zone_limit; 110b3deaf57SMatthew Dillon 111507df98aSIlya Dryomov /* 112507df98aSIlya Dryomov * Live dedup debug counters (sysctls are writable so that counters 113507df98aSIlya Dryomov * can be reset from userspace). 114507df98aSIlya Dryomov */ 115507df98aSIlya Dryomov int64_t hammer_live_dedup_vnode_bcmps = 0; 116507df98aSIlya Dryomov int64_t hammer_live_dedup_device_bcmps = 0; 117507df98aSIlya Dryomov int64_t hammer_live_dedup_findblk_failures = 0; 118507df98aSIlya Dryomov int64_t hammer_live_dedup_bmap_saves = 0; 119507df98aSIlya Dryomov 120507df98aSIlya Dryomov 121b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem"); 122507df98aSIlya Dryomov 1235987cc42SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD, 1245987cc42SMatthew Dillon &hammer_supported_version, 0, ""); 125d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW, 126d5ef456eSMatthew Dillon &hammer_debug_general, 0, ""); 1272f85fa4dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW, 1282f85fa4dSMatthew Dillon &hammer_debug_io, 0, ""); 129e8599db1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW, 130e8599db1SMatthew Dillon &hammer_debug_inode, 0, ""); 1317d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW, 1327d683b0fSMatthew Dillon &hammer_debug_locks, 0, ""); 133b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW, 134b3deaf57SMatthew Dillon &hammer_debug_btree, 0, ""); 135d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW, 136d113fda1SMatthew Dillon &hammer_debug_tid, 0, ""); 137b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW, 138b33e2cc0SMatthew Dillon &hammer_debug_recover, 0, ""); 13946fe7ae1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW, 14046fe7ae1SMatthew Dillon &hammer_debug_recover_faults, 0, ""); 141fc73edd8SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW, 142fc73edd8SMatthew Dillon &hammer_debug_critical, 0, ""); 1431b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW, 1441b0ab2c3SMatthew Dillon &hammer_cluster_enable, 0, ""); 145507df98aSIlya Dryomov /* 146507df98aSIlya Dryomov * 0 - live dedup is disabled 147507df98aSIlya Dryomov * 1 - dedup cache is populated on reads only 148507df98aSIlya Dryomov * 2 - dedup cache is populated on both reads and writes 149283a4a38SMatthew Dillon * 150283a4a38SMatthew Dillon * LIVE_DEDUP IS DISABLED PERMANENTLY! This feature appears to cause 151283a4a38SMatthew Dillon * blockmap corruption over time so we've turned it off permanently. 152507df98aSIlya Dryomov */ 1536de803f0SFrançois Tigeot SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup, CTLFLAG_RD, 1546de803f0SFrançois Tigeot &hammer_live_dedup, 0, "Enable live dedup (experimental)"); 155e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, tdmux_ticks, CTLFLAG_RW, 156e2a02b72SMatthew Dillon &hammer_tdmux_ticks, 0, "Hammer tdmux ticks"); 1579480ff55SMatthew Dillon 1583583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW, 159f5a07a7aSMatthew Dillon &hammer_limit_dirtybufspace, 0, ""); 16047637bffSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW, 16147637bffSMatthew Dillon &hammer_limit_recs, 0, ""); 162de996e86SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW, 163de996e86SMatthew Dillon &hammer_limit_inode_recs, 0, ""); 164e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaims, CTLFLAG_RW, 165e2a02b72SMatthew Dillon &hammer_limit_reclaims, 0, ""); 166e2ef7a95SIlya Dryomov SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW, 1672b34db9bSSamuel J. Greear &hammer_live_dedup_cache_size, 0, 1682b34db9bSSamuel J. Greear "Number of cache entries"); 1696048b411SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW, 1706048b411SMatthew Dillon &hammer_limit_redo, 0, ""); 1719480ff55SMatthew Dillon 1727a61b85dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD, 1737a61b85dSMatthew Dillon &hammer_count_fsyncs, 0, ""); 174b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD, 175b3deaf57SMatthew Dillon &hammer_count_inodes, 0, ""); 176af209b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD, 177af209b0fSMatthew Dillon &hammer_count_iqueued, 0, ""); 178e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaims, CTLFLAG_RD, 179e2a02b72SMatthew Dillon &hammer_count_reclaims, 0, ""); 180b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD, 181b3deaf57SMatthew Dillon &hammer_count_records, 0, ""); 182b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD, 183b3deaf57SMatthew Dillon &hammer_count_record_datas, 0, ""); 184b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD, 185b3deaf57SMatthew Dillon &hammer_count_volumes, 0, ""); 186b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD, 187b3deaf57SMatthew Dillon &hammer_count_buffers, 0, ""); 188b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD, 189b3deaf57SMatthew Dillon &hammer_count_nodes, 0, ""); 190a7e9bef1SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD, 191a7e9bef1SMatthew Dillon &hammer_count_extra_space_used, 0, ""); 192ce0138a6SMatthew Dillon 193cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD, 194cb51be26SMatthew Dillon &hammer_stats_btree_searches, 0, ""); 195cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD, 196cb51be26SMatthew Dillon &hammer_stats_btree_lookups, 0, ""); 197cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD, 198cb51be26SMatthew Dillon &hammer_stats_btree_inserts, 0, ""); 199cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD, 200cb51be26SMatthew Dillon &hammer_stats_btree_deletes, 0, ""); 201cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD, 202cb51be26SMatthew Dillon &hammer_stats_btree_elements, 0, ""); 203cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD, 204cb51be26SMatthew Dillon &hammer_stats_btree_splits, 0, ""); 205cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD, 206cb51be26SMatthew Dillon &hammer_stats_btree_iterations, 0, ""); 20739d8fd63SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD, 20839d8fd63SMatthew Dillon &hammer_stats_btree_root_iterations, 0, ""); 209cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD, 210cb51be26SMatthew Dillon &hammer_stats_record_iterations, 0, ""); 211ce0138a6SMatthew Dillon 212ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD, 213ce0138a6SMatthew Dillon &hammer_stats_file_read, 0, ""); 214ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD, 215ce0138a6SMatthew Dillon &hammer_stats_file_write, 0, ""); 216ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD, 217ce0138a6SMatthew Dillon &hammer_stats_file_iopsr, 0, ""); 218ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD, 219ce0138a6SMatthew Dillon &hammer_stats_file_iopsw, 0, ""); 220ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD, 221ce0138a6SMatthew Dillon &hammer_stats_disk_read, 0, ""); 222ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD, 223ce0138a6SMatthew Dillon &hammer_stats_disk_write, 0, ""); 224ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD, 225ce0138a6SMatthew Dillon &hammer_stats_inode_flushes, 0, ""); 226ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD, 227ce0138a6SMatthew Dillon &hammer_stats_commits, 0, ""); 22889e744ceSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD, 22989e744ceSMatthew Dillon &hammer_stats_undo, 0, ""); 2306048b411SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD, 2316048b411SMatthew Dillon &hammer_stats_redo, 0, ""); 232ce0138a6SMatthew Dillon 233507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_vnode_bcmps, CTLFLAG_RW, 234f06b3d6dSSamuel J. Greear &hammer_live_dedup_vnode_bcmps, 0, 235f06b3d6dSSamuel J. Greear "successful vnode buffer comparisons"); 236507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_device_bcmps, CTLFLAG_RW, 237f06b3d6dSSamuel J. Greear &hammer_live_dedup_device_bcmps, 0, 238f06b3d6dSSamuel J. Greear "successful device buffer comparisons"); 239507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_findblk_failures, CTLFLAG_RW, 240f06b3d6dSSamuel J. Greear &hammer_live_dedup_findblk_failures, 0, 241f06b3d6dSSamuel J. Greear "block lookup failures for comparison"); 242507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_bmap_saves, CTLFLAG_RW, 243f06b3d6dSSamuel J. Greear &hammer_live_dedup_bmap_saves, 0, 244f06b3d6dSSamuel J. Greear "useful physical block lookups"); 245507df98aSIlya Dryomov 2463583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD, 247f5a07a7aSMatthew Dillon &hammer_count_dirtybufspace, 0, ""); 248a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD, 249a99b9ea2SMatthew Dillon &hammer_count_refedbufs, 0, ""); 2500832c9bbSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD, 2510832c9bbSMatthew Dillon &hammer_count_reservations, 0, ""); 2523583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD, 253a99b9ea2SMatthew Dillon &hammer_count_io_running_read, 0, ""); 254a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD, 255a99b9ea2SMatthew Dillon &hammer_count_io_locked, 0, ""); 2563583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD, 257a99b9ea2SMatthew Dillon &hammer_count_io_running_write, 0, ""); 258f03c9cf4SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW, 259f03c9cf4SMatthew Dillon &hammer_zone_limit, 0, ""); 2607d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW, 2617d683b0fSMatthew Dillon &hammer_contention_count, 0, ""); 26221fde338SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW, 26321fde338SMatthew Dillon &hammer_autoflush, 0, ""); 264cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW, 265cb51be26SMatthew Dillon &hammer_verify_zone, 0, ""); 2661b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW, 2671b0ab2c3SMatthew Dillon &hammer_verify_data, 0, ""); 268cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW, 269cb51be26SMatthew Dillon &hammer_write_mode, 0, ""); 270b4f86ea3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, double_buffer, CTLFLAG_RW, 271b4f86ea3SMatthew Dillon &hammer_double_buffer, 0, ""); 272283a4a38SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, btree_full_undo, CTLFLAG_RW, 273283a4a38SMatthew Dillon &hammer_btree_full_undo, 0, ""); 2743e583440SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW, 2753e583440SMatthew Dillon &hammer_yield_check, 0, ""); 2766f3d87c0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW, 2776f3d87c0SMatthew Dillon &hammer_fsync_mode, 0, ""); 278b3deaf57SMatthew Dillon 279879a1b60SSascha Wildner /* KTR_INFO_MASTER(hammer); */ 280bcac4bbbSMatthew Dillon 281427e5fc6SMatthew Dillon /* 282427e5fc6SMatthew Dillon * VFS ABI 283427e5fc6SMatthew Dillon */ 284427e5fc6SMatthew Dillon static void hammer_free_hmp(struct mount *mp); 285427e5fc6SMatthew Dillon 286427e5fc6SMatthew Dillon static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data, 287427e5fc6SMatthew Dillon struct ucred *cred); 288427e5fc6SMatthew Dillon static int hammer_vfs_unmount(struct mount *mp, int mntflags); 289427e5fc6SMatthew Dillon static int hammer_vfs_root(struct mount *mp, struct vnode **vpp); 290427e5fc6SMatthew Dillon static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, 291427e5fc6SMatthew Dillon struct ucred *cred); 2926f97fce3SMatthew Dillon static int hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 2936f97fce3SMatthew Dillon struct ucred *cred); 294427e5fc6SMatthew Dillon static int hammer_vfs_sync(struct mount *mp, int waitfor); 295b9b0a6d0SMatthew Dillon static int hammer_vfs_vget(struct mount *mp, struct vnode *dvp, 296b9b0a6d0SMatthew Dillon ino_t ino, struct vnode **vpp); 297427e5fc6SMatthew Dillon static int hammer_vfs_init(struct vfsconf *conf); 29867863d04SMatthew Dillon static int hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 29967863d04SMatthew Dillon struct fid *fhp, struct vnode **vpp); 300513ca7d7SMatthew Dillon static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp); 301513ca7d7SMatthew Dillon static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 302513ca7d7SMatthew Dillon int *exflagsp, struct ucred **credanonp); 303513ca7d7SMatthew Dillon 304427e5fc6SMatthew Dillon 305427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = { 306427e5fc6SMatthew Dillon .vfs_mount = hammer_vfs_mount, 307427e5fc6SMatthew Dillon .vfs_unmount = hammer_vfs_unmount, 308427e5fc6SMatthew Dillon .vfs_root = hammer_vfs_root, 309427e5fc6SMatthew Dillon .vfs_statfs = hammer_vfs_statfs, 3106f97fce3SMatthew Dillon .vfs_statvfs = hammer_vfs_statvfs, 311427e5fc6SMatthew Dillon .vfs_sync = hammer_vfs_sync, 312427e5fc6SMatthew Dillon .vfs_vget = hammer_vfs_vget, 313513ca7d7SMatthew Dillon .vfs_init = hammer_vfs_init, 314513ca7d7SMatthew Dillon .vfs_vptofh = hammer_vfs_vptofh, 315513ca7d7SMatthew Dillon .vfs_fhtovp = hammer_vfs_fhtovp, 316513ca7d7SMatthew Dillon .vfs_checkexp = hammer_vfs_checkexp 317427e5fc6SMatthew Dillon }; 318427e5fc6SMatthew Dillon 319bac808feSMatthew Dillon MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", ""); 320427e5fc6SMatthew Dillon 321427e5fc6SMatthew Dillon VFS_SET(hammer_vfsops, hammer, 0); 322427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1); 323427e5fc6SMatthew Dillon 324427e5fc6SMatthew Dillon static int 325427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf) 326427e5fc6SMatthew Dillon { 32769906b47SMatthew Dillon long n; 3283098dc2fSMatthew Dillon 329e2a02b72SMatthew Dillon /* 330e2a02b72SMatthew Dillon * Wait up to this long for an exclusive deadlock to clear 331e2a02b72SMatthew Dillon * before acquiring a new shared lock on the ip. The deadlock 332e2a02b72SMatthew Dillon * may have occured on a b-tree node related to the ip. 333e2a02b72SMatthew Dillon */ 334e2a02b72SMatthew Dillon if (hammer_tdmux_ticks == 0) 335e2a02b72SMatthew Dillon hammer_tdmux_ticks = hz / 5; 336e2a02b72SMatthew Dillon 337e2a02b72SMatthew Dillon /* 33869906b47SMatthew Dillon * Autosize, but be careful because a hammer filesystem's 33969906b47SMatthew Dillon * reserve is partially calculated based on dirtybufspace, 34069906b47SMatthew Dillon * so we simply cannot allow it to get too large. 341e2a02b72SMatthew Dillon */ 3423098dc2fSMatthew Dillon if (hammer_limit_recs == 0) { 34369906b47SMatthew Dillon n = nbuf * 25; 34469906b47SMatthew Dillon if (n > kmalloc_limit(M_HAMMER) / 512) 3453098dc2fSMatthew Dillon n = kmalloc_limit(M_HAMMER) / 512; 34669906b47SMatthew Dillon if (n > 2 * 1024 * 1024) 34769906b47SMatthew Dillon n = 2 * 1024 * 1024; 34869906b47SMatthew Dillon hammer_limit_recs = (int)n; 3493098dc2fSMatthew Dillon } 350f5a07a7aSMatthew Dillon if (hammer_limit_dirtybufspace == 0) { 351f5a07a7aSMatthew Dillon hammer_limit_dirtybufspace = hidirtybufspace / 2; 35269906b47SMatthew Dillon if (hammer_limit_dirtybufspace < 1L * 1024 * 1024) 35369906b47SMatthew Dillon hammer_limit_dirtybufspace = 1024L * 1024; 35469906b47SMatthew Dillon if (hammer_limit_dirtybufspace > 1024L * 1024 * 1024) 35569906b47SMatthew Dillon hammer_limit_dirtybufspace = 1024L * 1024 * 1024; 3569f5097dcSMatthew Dillon } 357ba298df1SMatthew Dillon 358ba298df1SMatthew Dillon /* 35937646115SMatthew Dillon * The hammer_inode structure detaches from the vnode on reclaim. 36037646115SMatthew Dillon * This limits the number of inodes in this state to prevent a 36137646115SMatthew Dillon * memory pool blowout. 36237646115SMatthew Dillon */ 363e2a02b72SMatthew Dillon if (hammer_limit_reclaims == 0) 364e2a02b72SMatthew Dillon hammer_limit_reclaims = desiredvnodes / 10; 36537646115SMatthew Dillon 366427e5fc6SMatthew Dillon return(0); 367427e5fc6SMatthew Dillon } 368427e5fc6SMatthew Dillon 369427e5fc6SMatthew Dillon static int 370427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data, 371427e5fc6SMatthew Dillon struct ucred *cred) 372427e5fc6SMatthew Dillon { 373427e5fc6SMatthew Dillon struct hammer_mount_info info; 374a89aec1bSMatthew Dillon hammer_mount_t hmp; 375a89aec1bSMatthew Dillon hammer_volume_t rootvol; 37627ea2398SMatthew Dillon struct vnode *rootvp; 3777c19b529SMichael Neumann struct vnode *devvp = NULL; 378427e5fc6SMatthew Dillon const char *upath; /* volume name in userspace */ 379427e5fc6SMatthew Dillon char *path; /* volume name in system space */ 380427e5fc6SMatthew Dillon int error; 381427e5fc6SMatthew Dillon int i; 382732a1697SMatthew Dillon int master_id; 383c302e844STomohiro Kusumi int nvolumes; 384104cb849SMichael Neumann char *next_volume_ptr = NULL; 385ab0bf4a3SMatthew Dillon 386ab0bf4a3SMatthew Dillon /* 387ab0bf4a3SMatthew Dillon * Accept hammer_mount_info. mntpt is NULL for root mounts at boot. 388ab0bf4a3SMatthew Dillon */ 389ab0bf4a3SMatthew Dillon if (mntpt == NULL) { 3907c19b529SMichael Neumann bzero(&info, sizeof(info)); 3917c19b529SMichael Neumann info.asof = 0; 3927c19b529SMichael Neumann info.hflags = 0; 3937c19b529SMichael Neumann info.nvolumes = 1; 394104cb849SMichael Neumann 395104cb849SMichael Neumann next_volume_ptr = mp->mnt_stat.f_mntfromname; 396104cb849SMichael Neumann 397104cb849SMichael Neumann /* Count number of volumes separated by ':' */ 398104cb849SMichael Neumann for (char *p = next_volume_ptr; *p != '\0'; ++p) { 399104cb849SMichael Neumann if (*p == ':') { 400104cb849SMichael Neumann ++info.nvolumes; 401104cb849SMichael Neumann } 402104cb849SMichael Neumann } 403104cb849SMichael Neumann 404104cb849SMichael Neumann mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */ 4057c19b529SMichael Neumann } else { 406427e5fc6SMatthew Dillon if ((error = copyin(data, &info, sizeof(info))) != 0) 407427e5fc6SMatthew Dillon return (error); 4087c19b529SMichael Neumann } 409427e5fc6SMatthew Dillon 410427e5fc6SMatthew Dillon /* 411732a1697SMatthew Dillon * updating or new mount 412427e5fc6SMatthew Dillon */ 413195c19a1SMatthew Dillon if (mp->mnt_flag & MNT_UPDATE) { 414195c19a1SMatthew Dillon hmp = (void *)mp->mnt_data; 415195c19a1SMatthew Dillon KKASSERT(hmp != NULL); 416195c19a1SMatthew Dillon } else { 4177529c2acSAntonio Huete Jimenez if (info.nvolumes <= 0 || info.nvolumes > HAMMER_MAX_VOLUMES) 418732a1697SMatthew Dillon return (EINVAL); 419732a1697SMatthew Dillon hmp = NULL; 420732a1697SMatthew Dillon } 421732a1697SMatthew Dillon 422732a1697SMatthew Dillon /* 423732a1697SMatthew Dillon * master-id validation. The master id may not be changed by a 424732a1697SMatthew Dillon * mount update. 425732a1697SMatthew Dillon */ 42687b8f936STomohiro Kusumi if (info.hflags & HMNT_MASTERID || info.hflags & HMNT_NOMIRROR) { 427732a1697SMatthew Dillon if (hmp && hmp->master_id != info.master_id) { 428d053aa8aSTomohiro Kusumi hkprintf("cannot change master id with mount update\n"); 429732a1697SMatthew Dillon return(EINVAL); 430732a1697SMatthew Dillon } 431732a1697SMatthew Dillon master_id = info.master_id; 432732a1697SMatthew Dillon if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS) 433732a1697SMatthew Dillon return (EINVAL); 434732a1697SMatthew Dillon } else { 435732a1697SMatthew Dillon if (hmp) 436732a1697SMatthew Dillon master_id = hmp->master_id; 437732a1697SMatthew Dillon else 438732a1697SMatthew Dillon master_id = 0; 439732a1697SMatthew Dillon } 440732a1697SMatthew Dillon 441732a1697SMatthew Dillon /* 442b0aab9b9SMatthew Dillon * Internal mount data structure 443732a1697SMatthew Dillon */ 444732a1697SMatthew Dillon if (hmp == NULL) { 445427e5fc6SMatthew Dillon hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO); 446427e5fc6SMatthew Dillon mp->mnt_data = (qaddr_t)hmp; 447427e5fc6SMatthew Dillon hmp->mp = mp; 44847197d71SMatthew Dillon 449bac808feSMatthew Dillon /* 4507c457ac8SMatthew Dillon * Make sure kmalloc type limits are set appropriately. 4517c457ac8SMatthew Dillon * 4527c457ac8SMatthew Dillon * Our inode kmalloc group is sized based on maxvnodes 4537c457ac8SMatthew Dillon * (controlled by the system, not us). 454bac808feSMatthew Dillon */ 455bac808feSMatthew Dillon kmalloc_create(&hmp->m_misc, "HAMMER-others"); 456bac808feSMatthew Dillon kmalloc_create(&hmp->m_inodes, "HAMMER-inodes"); 457bac808feSMatthew Dillon 4587c457ac8SMatthew Dillon kmalloc_raise_limit(hmp->m_inodes, 0); /* unlimited */ 459bac808feSMatthew Dillon 460dd94f1b1SMatthew Dillon hmp->root_btree_beg.localization = 0x00000000U; 46147197d71SMatthew Dillon hmp->root_btree_beg.obj_id = -0x8000000000000000LL; 46247197d71SMatthew Dillon hmp->root_btree_beg.key = -0x8000000000000000LL; 46347197d71SMatthew Dillon hmp->root_btree_beg.create_tid = 1; 46447197d71SMatthew Dillon hmp->root_btree_beg.delete_tid = 1; 46547197d71SMatthew Dillon hmp->root_btree_beg.rec_type = 0; 46647197d71SMatthew Dillon hmp->root_btree_beg.obj_type = 0; 467bfbd8331STomohiro Kusumi hmp->root_btree_beg.btype = HAMMER_BTREE_TYPE_NONE; 46847197d71SMatthew Dillon 469dd94f1b1SMatthew Dillon hmp->root_btree_end.localization = 0xFFFFFFFFU; 47047197d71SMatthew Dillon hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL; 47147197d71SMatthew Dillon hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL; 47247197d71SMatthew Dillon hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL; 47347197d71SMatthew Dillon hmp->root_btree_end.delete_tid = 0; /* special case */ 47447197d71SMatthew Dillon hmp->root_btree_end.rec_type = 0xFFFFU; 47547197d71SMatthew Dillon hmp->root_btree_end.obj_type = 0; 476bfbd8331STomohiro Kusumi hmp->root_btree_end.btype = HAMMER_BTREE_TYPE_NONE; 477f03c9cf4SMatthew Dillon 478cdb6e4e6SMatthew Dillon hmp->krate.freq = 1; /* maximum reporting rate (hz) */ 479cdb6e4e6SMatthew Dillon hmp->krate.count = -16; /* initial burst */ 480a8d31329SMatthew Dillon hmp->kdiag.freq = 1; /* maximum reporting rate (hz) */ 481a8d31329SMatthew Dillon hmp->kdiag.count = -16; /* initial burst */ 482cdb6e4e6SMatthew Dillon 4839480ff55SMatthew Dillon hmp->sync_lock.refs = 1; 484c9b9e29dSMatthew Dillon hmp->free_lock.refs = 1; 485d99d6bf5SMatthew Dillon hmp->undo_lock.refs = 1; 486d99d6bf5SMatthew Dillon hmp->blkmap_lock.refs = 1; 48783f2a3aaSMatthew Dillon hmp->snapshot_lock.refs = 1; 48852e547e3SMichael Neumann hmp->volume_lock.refs = 1; 4899480ff55SMatthew Dillon 490cebe9493SMatthew Dillon TAILQ_INIT(&hmp->delay_list); 4917a61b85dSMatthew Dillon TAILQ_INIT(&hmp->flush_group_list); 4920729c8c8SMatthew Dillon TAILQ_INIT(&hmp->objid_cache_list); 493e8599db1SMatthew Dillon TAILQ_INIT(&hmp->undo_lru_list); 4947bc5b8c2SMatthew Dillon TAILQ_INIT(&hmp->reclaim_list); 495507df98aSIlya Dryomov 496507df98aSIlya Dryomov RB_INIT(&hmp->rb_dedup_crc_root); 497507df98aSIlya Dryomov RB_INIT(&hmp->rb_dedup_off_root); 498507df98aSIlya Dryomov TAILQ_INIT(&hmp->dedup_lru_list); 499195c19a1SMatthew Dillon } 50051c35492SMatthew Dillon hmp->hflags &= ~HMNT_USERFLAGS; 50151c35492SMatthew Dillon hmp->hflags |= info.hflags & HMNT_USERFLAGS; 502732a1697SMatthew Dillon 503732a1697SMatthew Dillon hmp->master_id = master_id; 504732a1697SMatthew Dillon 5057f7c1f84SMatthew Dillon if (info.asof) { 5067f7c1f84SMatthew Dillon mp->mnt_flag |= MNT_RDONLY; 5077f7c1f84SMatthew Dillon hmp->asof = info.asof; 5087f7c1f84SMatthew Dillon } else { 5097f7c1f84SMatthew Dillon hmp->asof = HAMMER_MAX_TID; 5107f7c1f84SMatthew Dillon } 511195c19a1SMatthew Dillon 512865c9609SMichael Neumann hmp->volume_to_remove = -1; 513865c9609SMichael Neumann 514195c19a1SMatthew Dillon /* 51551c35492SMatthew Dillon * Re-open read-write if originally read-only, or vise-versa. 51602428fb6SMatthew Dillon * 51702428fb6SMatthew Dillon * When going from read-only to read-write execute the stage2 51802428fb6SMatthew Dillon * recovery if it has not already been run. 519195c19a1SMatthew Dillon */ 520195c19a1SMatthew Dillon if (mp->mnt_flag & MNT_UPDATE) { 521b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 52251c35492SMatthew Dillon error = 0; 52351c35492SMatthew Dillon if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 524d053aa8aSTomohiro Kusumi hkprintf("read-only -> read-write\n"); 525195c19a1SMatthew Dillon hmp->ronly = 0; 52651c35492SMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 52751c35492SMatthew Dillon hammer_adjust_volume_mode, NULL); 52851c35492SMatthew Dillon rootvol = hammer_get_root_volume(hmp, &error); 52951c35492SMatthew Dillon if (rootvol) { 53006ad81ffSMatthew Dillon hammer_recover_flush_buffers(hmp, rootvol, 1); 53102428fb6SMatthew Dillon error = hammer_recover_stage2(hmp, rootvol); 5329f5097dcSMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, 5339f5097dcSMatthew Dillon hmp->blockmap, 5349f5097dcSMatthew Dillon sizeof(hmp->blockmap)); 53551c35492SMatthew Dillon hammer_rel_volume(rootvol, 0); 536195c19a1SMatthew Dillon } 53751c35492SMatthew Dillon RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 53851c35492SMatthew Dillon hammer_reload_inode, NULL); 53951c35492SMatthew Dillon /* kernel clears MNT_RDONLY */ 54051c35492SMatthew Dillon } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 541d053aa8aSTomohiro Kusumi hkprintf("read-write -> read-only\n"); 54251c35492SMatthew Dillon hmp->ronly = 1; /* messy */ 54351c35492SMatthew Dillon RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 54451c35492SMatthew Dillon hammer_reload_inode, NULL); 54551c35492SMatthew Dillon hmp->ronly = 0; 54651c35492SMatthew Dillon hammer_flusher_sync(hmp); 54751c35492SMatthew Dillon hammer_flusher_sync(hmp); 54851c35492SMatthew Dillon hammer_flusher_sync(hmp); 54951c35492SMatthew Dillon hmp->ronly = 1; 55051c35492SMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 55151c35492SMatthew Dillon hammer_adjust_volume_mode, NULL); 55251c35492SMatthew Dillon } 553b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 55451c35492SMatthew Dillon return(error); 555195c19a1SMatthew Dillon } 556195c19a1SMatthew Dillon 557427e5fc6SMatthew Dillon RB_INIT(&hmp->rb_vols_root); 558427e5fc6SMatthew Dillon RB_INIT(&hmp->rb_inos_root); 55973896937SMatthew Dillon RB_INIT(&hmp->rb_redo_root); 56040043e7fSMatthew Dillon RB_INIT(&hmp->rb_nods_root); 561e8599db1SMatthew Dillon RB_INIT(&hmp->rb_undo_root); 5620832c9bbSMatthew Dillon RB_INIT(&hmp->rb_resv_root); 5630832c9bbSMatthew Dillon RB_INIT(&hmp->rb_bufs_root); 5645fa5c92fSMatthew Dillon RB_INIT(&hmp->rb_pfsm_root); 5650832c9bbSMatthew Dillon 566195c19a1SMatthew Dillon hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 567427e5fc6SMatthew Dillon 5681afb73cfSMatthew Dillon RB_INIT(&hmp->volu_root); 5691afb73cfSMatthew Dillon RB_INIT(&hmp->undo_root); 5701afb73cfSMatthew Dillon RB_INIT(&hmp->data_root); 5711afb73cfSMatthew Dillon RB_INIT(&hmp->meta_root); 5721afb73cfSMatthew Dillon RB_INIT(&hmp->lose_root); 573eddadaeeSMatthew Dillon TAILQ_INIT(&hmp->iorun_list); 57410a5d1baSMatthew Dillon 575a3c18566SMatthew Dillon lwkt_token_init(&hmp->fs_token, "hammerfs"); 576a3c18566SMatthew Dillon lwkt_token_init(&hmp->io_token, "hammerio"); 577b0aab9b9SMatthew Dillon 578b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 579b0aab9b9SMatthew Dillon 580427e5fc6SMatthew Dillon /* 581427e5fc6SMatthew Dillon * Load volumes 582427e5fc6SMatthew Dillon */ 583427e5fc6SMatthew Dillon path = objcache_get(namei_oc, M_WAITOK); 5841b0ab2c3SMatthew Dillon hmp->nvolumes = -1; 585427e5fc6SMatthew Dillon for (i = 0; i < info.nvolumes; ++i) { 5867c19b529SMichael Neumann if (mntpt == NULL) { 5877c19b529SMichael Neumann /* 5887c19b529SMichael Neumann * Root mount. 5897c19b529SMichael Neumann */ 590104cb849SMichael Neumann KKASSERT(next_volume_ptr != NULL); 591a407819fSMichael Neumann strcpy(path, ""); 592a407819fSMichael Neumann if (*next_volume_ptr != '/') { 593a407819fSMichael Neumann /* relative path */ 594104cb849SMichael Neumann strcpy(path, "/dev/"); 595a407819fSMichael Neumann } 596104cb849SMichael Neumann int k; 597104cb849SMichael Neumann for (k = strlen(path); k < MAXPATHLEN-1; ++k) { 598104cb849SMichael Neumann if (*next_volume_ptr == '\0') { 599104cb849SMichael Neumann break; 600104cb849SMichael Neumann } else if (*next_volume_ptr == ':') { 601104cb849SMichael Neumann ++next_volume_ptr; 602104cb849SMichael Neumann break; 603104cb849SMichael Neumann } else { 604104cb849SMichael Neumann path[k] = *next_volume_ptr; 605104cb849SMichael Neumann ++next_volume_ptr; 606104cb849SMichael Neumann } 607104cb849SMichael Neumann } 608104cb849SMichael Neumann path[k] = '\0'; 609104cb849SMichael Neumann 6107c19b529SMichael Neumann error = 0; 611104cb849SMichael Neumann cdev_t dev = kgetdiskbyname(path); 612104cb849SMichael Neumann error = bdevvp(dev, &devvp); 613104cb849SMichael Neumann if (error) { 61411605a5cSTomohiro Kusumi hdkprintf("can't find devvp\n"); 615104cb849SMichael Neumann } 6167c19b529SMichael Neumann } else { 6177c19b529SMichael Neumann error = copyin(&info.volumes[i], &upath, 6187c19b529SMichael Neumann sizeof(char *)); 619427e5fc6SMatthew Dillon if (error == 0) 6207c19b529SMichael Neumann error = copyinstr(upath, path, 6217c19b529SMichael Neumann MAXPATHLEN, NULL); 6227c19b529SMichael Neumann } 623427e5fc6SMatthew Dillon if (error == 0) 624deabdbfbSTomohiro Kusumi error = hammer_install_volume(hmp, path, devvp, NULL); 625427e5fc6SMatthew Dillon if (error) 626427e5fc6SMatthew Dillon break; 627427e5fc6SMatthew Dillon } 628427e5fc6SMatthew Dillon objcache_put(namei_oc, path); 629427e5fc6SMatthew Dillon 630427e5fc6SMatthew Dillon /* 631427e5fc6SMatthew Dillon * Make sure we found a root volume 632427e5fc6SMatthew Dillon */ 633721f3110STomohiro Kusumi if (hmp->rootvol == NULL) { 6349d756529STomohiro Kusumi if (error == EBUSY) { 63511605a5cSTomohiro Kusumi hdkprintf("The volumes are probably mounted\n"); 6369d756529STomohiro Kusumi } else { 63711605a5cSTomohiro Kusumi hdkprintf("No root volume found!\n"); 638427e5fc6SMatthew Dillon error = EINVAL; 6399d756529STomohiro Kusumi } 640721f3110STomohiro Kusumi goto failed; 641427e5fc6SMatthew Dillon } 6421b0ab2c3SMatthew Dillon 6431b0ab2c3SMatthew Dillon /* 6441b0ab2c3SMatthew Dillon * Check that all required volumes are available 6451b0ab2c3SMatthew Dillon */ 6461b0ab2c3SMatthew Dillon if (error == 0 && hammer_mountcheck_volumes(hmp)) { 64711605a5cSTomohiro Kusumi hdkprintf("Missing volumes, cannot mount!\n"); 6481b0ab2c3SMatthew Dillon error = EINVAL; 649721f3110STomohiro Kusumi goto failed; 6501b0ab2c3SMatthew Dillon } 6511b0ab2c3SMatthew Dillon 652721f3110STomohiro Kusumi /* 653721f3110STomohiro Kusumi * Other errors 654721f3110STomohiro Kusumi */ 655427e5fc6SMatthew Dillon if (error) { 65611605a5cSTomohiro Kusumi hdkprintf("Failed to load volumes!\n"); 657721f3110STomohiro Kusumi goto failed; 658427e5fc6SMatthew Dillon } 659427e5fc6SMatthew Dillon 660c302e844STomohiro Kusumi nvolumes = hammer_get_installed_volumes(hmp); 661c302e844STomohiro Kusumi if (hmp->nvolumes != nvolumes) { 66211605a5cSTomohiro Kusumi hdkprintf("volume header says %d volumes, but %d installed\n", 663c302e844STomohiro Kusumi hmp->nvolumes, nvolumes); 664c302e844STomohiro Kusumi error = EINVAL; 665c302e844STomohiro Kusumi goto failed; 666c302e844STomohiro Kusumi } 667c302e844STomohiro Kusumi 668427e5fc6SMatthew Dillon /* 66927ea2398SMatthew Dillon * No errors, setup enough of the mount point so we can lookup the 67027ea2398SMatthew Dillon * root vnode. 671427e5fc6SMatthew Dillon */ 672427e5fc6SMatthew Dillon mp->mnt_iosize_max = MAXPHYS; 673427e5fc6SMatthew Dillon mp->mnt_kern_flag |= MNTK_FSMID; 674cf6a53caSMatthew Dillon mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ 675c0ade690SMatthew Dillon 676c0ade690SMatthew Dillon /* 677aac0aabdSMatthew Dillon * MPSAFE code. Note that VOPs and VFSops which are not MPSAFE 678aac0aabdSMatthew Dillon * will acquire a per-mount token prior to entry and release it 6796a85c61bSTomohiro Kusumi * on return. 680aac0aabdSMatthew Dillon */ 681b0aab9b9SMatthew Dillon mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; 682aac0aabdSMatthew Dillon 683aac0aabdSMatthew Dillon /* 684c0ade690SMatthew Dillon * note: f_iosize is used by vnode_pager_haspage() when constructing 685c0ade690SMatthew Dillon * its VOP_BMAP call. 686c0ade690SMatthew Dillon */ 687c0ade690SMatthew Dillon mp->mnt_stat.f_iosize = HAMMER_BUFSIZE; 688fbc6e32aSMatthew Dillon mp->mnt_stat.f_bsize = HAMMER_BUFSIZE; 6896f97fce3SMatthew Dillon 6906f97fce3SMatthew Dillon mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE; 6916f97fce3SMatthew Dillon mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE; 6926f97fce3SMatthew Dillon 693427e5fc6SMatthew Dillon mp->mnt_maxsymlinklen = 255; 694427e5fc6SMatthew Dillon mp->mnt_flag |= MNT_LOCAL; 695427e5fc6SMatthew Dillon 696427e5fc6SMatthew Dillon vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops); 6977a04d74fSMatthew Dillon vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops); 6987a04d74fSMatthew Dillon vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops); 69927ea2398SMatthew Dillon 70027ea2398SMatthew Dillon /* 701a89aec1bSMatthew Dillon * The root volume's ondisk pointer is only valid if we hold a 702a89aec1bSMatthew Dillon * reference to it. 703a89aec1bSMatthew Dillon */ 704a89aec1bSMatthew Dillon rootvol = hammer_get_root_volume(hmp, &error); 705a89aec1bSMatthew Dillon if (error) 706f90dde4cSMatthew Dillon goto failed; 707f90dde4cSMatthew Dillon 708f90dde4cSMatthew Dillon /* 7099f5097dcSMatthew Dillon * Perform any necessary UNDO operations. The recovery code does 7100729c8c8SMatthew Dillon * call hammer_undo_lookup() so we have to pre-cache the blockmap, 7110729c8c8SMatthew Dillon * and then re-copy it again after recovery is complete. 712c9b9e29dSMatthew Dillon * 71351c35492SMatthew Dillon * If this is a read-only mount the UNDO information is retained 71451c35492SMatthew Dillon * in memory in the form of dirty buffer cache buffers, and not 71551c35492SMatthew Dillon * written back to the media. 716f90dde4cSMatthew Dillon */ 7170729c8c8SMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 7180729c8c8SMatthew Dillon sizeof(hmp->blockmap)); 719c9b9e29dSMatthew Dillon 7207a61b85dSMatthew Dillon /* 72144a83111SMatthew Dillon * Check filesystem version 72244a83111SMatthew Dillon */ 72344a83111SMatthew Dillon hmp->version = rootvol->ondisk->vol_version; 72444a83111SMatthew Dillon if (hmp->version < HAMMER_VOL_VERSION_MIN || 72544a83111SMatthew Dillon hmp->version > HAMMER_VOL_VERSION_MAX) { 726d053aa8aSTomohiro Kusumi hkprintf("mount unsupported fs version %d\n", hmp->version); 72744a83111SMatthew Dillon error = ERANGE; 72844a83111SMatthew Dillon goto done; 72944a83111SMatthew Dillon } 73044a83111SMatthew Dillon 73144a83111SMatthew Dillon /* 7327a61b85dSMatthew Dillon * The undo_rec_limit limits the size of flush groups to avoid 7337a61b85dSMatthew Dillon * blowing out the UNDO FIFO. This calculation is typically in 7347a61b85dSMatthew Dillon * the tens of thousands and is designed primarily when small 7357a61b85dSMatthew Dillon * HAMMER filesystems are created. 7367a61b85dSMatthew Dillon */ 7377a61b85dSMatthew Dillon hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100; 7387a61b85dSMatthew Dillon if (hammer_debug_general & 0x0001) 739d053aa8aSTomohiro Kusumi hkprintf("undo_rec_limit %d\n", hmp->undo_rec_limit); 7407a61b85dSMatthew Dillon 74102428fb6SMatthew Dillon /* 74202428fb6SMatthew Dillon * NOTE: Recover stage1 not only handles meta-data recovery, it 74302428fb6SMatthew Dillon * also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems. 74402428fb6SMatthew Dillon */ 74502428fb6SMatthew Dillon error = hammer_recover_stage1(hmp, rootvol); 746f90dde4cSMatthew Dillon if (error) { 747f90dde4cSMatthew Dillon kprintf("Failed to recover HAMMER filesystem on mount\n"); 748a89aec1bSMatthew Dillon goto done; 749f90dde4cSMatthew Dillon } 750f90dde4cSMatthew Dillon 751f90dde4cSMatthew Dillon /* 752adf01747SMatthew Dillon * Finish setup now that we have a good root volume. 7535b5dd5b4STomohiro Kusumi * vol_name is a filesystem label string. 754f90dde4cSMatthew Dillon */ 755a89aec1bSMatthew Dillon ksnprintf(mp->mnt_stat.f_mntfromname, 756a89aec1bSMatthew Dillon sizeof(mp->mnt_stat.f_mntfromname), "%s", 757a89aec1bSMatthew Dillon rootvol->ondisk->vol_name); 758513ca7d7SMatthew Dillon mp->mnt_stat.f_fsid.val[0] = 759513ca7d7SMatthew Dillon crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8); 760513ca7d7SMatthew Dillon mp->mnt_stat.f_fsid.val[1] = 761513ca7d7SMatthew Dillon crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8); 76293775b63STomohiro Kusumi mp->mnt_stat.f_fsid.val[1] &= HAMMER_LOCALIZE_MASK; 763b84de5afSMatthew Dillon 7646f97fce3SMatthew Dillon mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid; 7656f97fce3SMatthew Dillon mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid, 7666f97fce3SMatthew Dillon sizeof(mp->mnt_vstat.f_fsid_uuid)); 7676f97fce3SMatthew Dillon 7680729c8c8SMatthew Dillon /* 7690729c8c8SMatthew Dillon * Certain often-modified fields in the root volume are cached in 7700729c8c8SMatthew Dillon * the hammer_mount structure so we do not have to generate lots 7710729c8c8SMatthew Dillon * of little UNDO structures for them. 772c9b9e29dSMatthew Dillon * 7739f5097dcSMatthew Dillon * Recopy after recovery. This also has the side effect of 7749f5097dcSMatthew Dillon * setting our cached undo FIFO's first_offset, which serves to 7759f5097dcSMatthew Dillon * placemark the FIFO start for the NEXT flush cycle while the 7769f5097dcSMatthew Dillon * on-disk first_offset represents the LAST flush cycle. 7770729c8c8SMatthew Dillon */ 778b84de5afSMatthew Dillon hmp->next_tid = rootvol->ondisk->vol0_next_tid; 7794889cbd4SMatthew Dillon hmp->flush_tid1 = hmp->next_tid; 7804889cbd4SMatthew Dillon hmp->flush_tid2 = hmp->next_tid; 7810729c8c8SMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 7820729c8c8SMatthew Dillon sizeof(hmp->blockmap)); 783e63644f0SMatthew Dillon hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks; 7840729c8c8SMatthew Dillon 785059819e3SMatthew Dillon hammer_flusher_create(hmp); 786059819e3SMatthew Dillon 787a89aec1bSMatthew Dillon /* 7881bcc9299STomohiro Kusumi * Locate the root directory with an obj_id of 1. 78927ea2398SMatthew Dillon */ 790783fb170STomohiro Kusumi error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, &rootvp); 791a89aec1bSMatthew Dillon if (error) 792a89aec1bSMatthew Dillon goto done; 79327ea2398SMatthew Dillon vput(rootvp); 79488c39f64SThomas Nikolajsen if (hmp->ronly == 0) 79502428fb6SMatthew Dillon error = hammer_recover_stage2(hmp, rootvol); 79627ea2398SMatthew Dillon 7979fa352a2SMatthew Dillon /* 7989fa352a2SMatthew Dillon * If the stage2 recovery fails be sure to clean out all cached 7999fa352a2SMatthew Dillon * vnodes before throwing away the mount structure or bad things 8009fa352a2SMatthew Dillon * will happen. 8019fa352a2SMatthew Dillon */ 8029fa352a2SMatthew Dillon if (error) 8039fa352a2SMatthew Dillon vflush(mp, 0, 0); 8049fa352a2SMatthew Dillon 805a89aec1bSMatthew Dillon done: 80698ad7adeSFrancois Tigeot if ((mp->mnt_flag & MNT_UPDATE) == 0) { 80798ad7adeSFrancois Tigeot /* New mount */ 80898ad7adeSFrancois Tigeot 80998ad7adeSFrancois Tigeot /* Populate info for mount point (NULL pad)*/ 81098ad7adeSFrancois Tigeot bzero(mp->mnt_stat.f_mntonname, MNAMELEN); 81198ad7adeSFrancois Tigeot size_t size; 81298ad7adeSFrancois Tigeot if (mntpt) { 81398ad7adeSFrancois Tigeot copyinstr(mntpt, mp->mnt_stat.f_mntonname, 81498ad7adeSFrancois Tigeot MNAMELEN -1, &size); 81598ad7adeSFrancois Tigeot } else { /* Root mount */ 81698ad7adeSFrancois Tigeot mp->mnt_stat.f_mntonname[0] = '/'; 81798ad7adeSFrancois Tigeot } 81898ad7adeSFrancois Tigeot } 81998ad7adeSFrancois Tigeot (void)VFS_STATFS(mp, &mp->mnt_stat, cred); 820f90dde4cSMatthew Dillon hammer_rel_volume(rootvol, 0); 821f90dde4cSMatthew Dillon failed: 82227ea2398SMatthew Dillon /* 82327ea2398SMatthew Dillon * Cleanup and return. 82427ea2398SMatthew Dillon */ 825b0aab9b9SMatthew Dillon if (error) { 826b0aab9b9SMatthew Dillon /* called with fs_token held */ 82727ea2398SMatthew Dillon hammer_free_hmp(mp); 828b0aab9b9SMatthew Dillon } else { 829b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 830b0aab9b9SMatthew Dillon } 831427e5fc6SMatthew Dillon return (error); 832427e5fc6SMatthew Dillon } 833427e5fc6SMatthew Dillon 834427e5fc6SMatthew Dillon static int 835427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags) 836427e5fc6SMatthew Dillon { 837b0aab9b9SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 838427e5fc6SMatthew Dillon int flags; 83966325755SMatthew Dillon int error; 84027ea2398SMatthew Dillon 84127ea2398SMatthew Dillon /* 842427e5fc6SMatthew Dillon * Clean out the vnodes 843427e5fc6SMatthew Dillon */ 844b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 84566325755SMatthew Dillon flags = 0; 84666325755SMatthew Dillon if (mntflags & MNT_FORCE) 84766325755SMatthew Dillon flags |= FORCECLOSE; 848b0aab9b9SMatthew Dillon error = vflush(mp, 0, flags); 849427e5fc6SMatthew Dillon 850427e5fc6SMatthew Dillon /* 851427e5fc6SMatthew Dillon * Clean up the internal mount structure and related entities. This 852427e5fc6SMatthew Dillon * may issue I/O. 853427e5fc6SMatthew Dillon */ 854b0aab9b9SMatthew Dillon if (error == 0) { 855b0aab9b9SMatthew Dillon /* called with fs_token held */ 856427e5fc6SMatthew Dillon hammer_free_hmp(mp); 857b0aab9b9SMatthew Dillon } else { 858b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 859b0aab9b9SMatthew Dillon } 860b0aab9b9SMatthew Dillon return(error); 861427e5fc6SMatthew Dillon } 862427e5fc6SMatthew Dillon 863427e5fc6SMatthew Dillon /* 864427e5fc6SMatthew Dillon * Clean up the internal mount structure and disassociate it from the mount. 865427e5fc6SMatthew Dillon * This may issue I/O. 866b0aab9b9SMatthew Dillon * 867b0aab9b9SMatthew Dillon * Called with fs_token held. 868427e5fc6SMatthew Dillon */ 869427e5fc6SMatthew Dillon static void 870427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp) 871427e5fc6SMatthew Dillon { 872b0aab9b9SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 873cdb6e4e6SMatthew Dillon hammer_flush_group_t flg; 874427e5fc6SMatthew Dillon 875427e5fc6SMatthew Dillon /* 876cdb6e4e6SMatthew Dillon * Flush anything dirty. This won't even run if the 877cdb6e4e6SMatthew Dillon * filesystem errored-out. 878427e5fc6SMatthew Dillon */ 8798bae937eSTomohiro Kusumi hammer_flush_dirty(hmp, 30); 880cdb6e4e6SMatthew Dillon 881cdb6e4e6SMatthew Dillon /* 882cdb6e4e6SMatthew Dillon * If the mount had a critical error we have to destroy any 883cdb6e4e6SMatthew Dillon * remaining inodes before we can finish cleaning up the flusher. 884cdb6e4e6SMatthew Dillon */ 885cdb6e4e6SMatthew Dillon if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) { 886cdb6e4e6SMatthew Dillon RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 887cdb6e4e6SMatthew Dillon hammer_destroy_inode_callback, NULL); 888cdb6e4e6SMatthew Dillon } 889cdb6e4e6SMatthew Dillon 890cdb6e4e6SMatthew Dillon /* 891cdb6e4e6SMatthew Dillon * There shouldn't be any inodes left now and any left over 892cdb6e4e6SMatthew Dillon * flush groups should now be empty. 893cdb6e4e6SMatthew Dillon */ 894cdb6e4e6SMatthew Dillon KKASSERT(RB_EMPTY(&hmp->rb_inos_root)); 895cdb6e4e6SMatthew Dillon while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) { 896cdb6e4e6SMatthew Dillon TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry); 897ff003b11SMatthew Dillon KKASSERT(RB_EMPTY(&flg->flush_tree)); 898cdb6e4e6SMatthew Dillon if (flg->refs) { 899d053aa8aSTomohiro Kusumi hkprintf("Warning, flush_group %p was " 900cdb6e4e6SMatthew Dillon "not empty on umount!\n", flg); 901cdb6e4e6SMatthew Dillon } 902bac808feSMatthew Dillon kfree(flg, hmp->m_misc); 903cdb6e4e6SMatthew Dillon } 904cdb6e4e6SMatthew Dillon 905cdb6e4e6SMatthew Dillon /* 906cdb6e4e6SMatthew Dillon * We can finally destroy the flusher 907cdb6e4e6SMatthew Dillon */ 908059819e3SMatthew Dillon hammer_flusher_destroy(hmp); 909427e5fc6SMatthew Dillon 910b84de5afSMatthew Dillon /* 91100f16fadSMatthew Dillon * We may have held recovered buffers due to a read-only mount. 91200f16fadSMatthew Dillon * These must be discarded. 91300f16fadSMatthew Dillon */ 91400f16fadSMatthew Dillon if (hmp->ronly) 91500f16fadSMatthew Dillon hammer_recover_flush_buffers(hmp, NULL, -1); 91600f16fadSMatthew Dillon 91700f16fadSMatthew Dillon /* 9180832c9bbSMatthew Dillon * Unload buffers and then volumes 919b84de5afSMatthew Dillon */ 9200832c9bbSMatthew Dillon RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL, 9210832c9bbSMatthew Dillon hammer_unload_buffer, NULL); 922427e5fc6SMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 923427e5fc6SMatthew Dillon hammer_unload_volume, NULL); 924427e5fc6SMatthew Dillon 925427e5fc6SMatthew Dillon mp->mnt_data = NULL; 92666325755SMatthew Dillon mp->mnt_flag &= ~MNT_LOCAL; 927427e5fc6SMatthew Dillon hmp->mp = NULL; 9280729c8c8SMatthew Dillon hammer_destroy_objid_cache(hmp); 929507df98aSIlya Dryomov hammer_destroy_dedup_cache(hmp); 930507df98aSIlya Dryomov if (hmp->dedup_free_cache != NULL) { 931507df98aSIlya Dryomov kfree(hmp->dedup_free_cache, hmp->m_misc); 932507df98aSIlya Dryomov hmp->dedup_free_cache = NULL; 933507df98aSIlya Dryomov } 934bac808feSMatthew Dillon kmalloc_destroy(&hmp->m_misc); 935bac808feSMatthew Dillon kmalloc_destroy(&hmp->m_inodes); 936b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 937427e5fc6SMatthew Dillon kfree(hmp, M_HAMMER); 938427e5fc6SMatthew Dillon } 939427e5fc6SMatthew Dillon 940427e5fc6SMatthew Dillon /* 941cdb6e4e6SMatthew Dillon * Report critical errors. ip may be NULL. 942cdb6e4e6SMatthew Dillon */ 943cdb6e4e6SMatthew Dillon void 944cdb6e4e6SMatthew Dillon hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip, 945cdb6e4e6SMatthew Dillon int error, const char *msg) 946cdb6e4e6SMatthew Dillon { 947cdb6e4e6SMatthew Dillon hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR; 948c9ce54d6SMatthew Dillon 949d053aa8aSTomohiro Kusumi hmkrateprintf(&hmp->krate, hmp, 950d053aa8aSTomohiro Kusumi "Critical error inode=%jd error=%d %s\n", 951c9ce54d6SMatthew Dillon (intmax_t)(ip ? ip->obj_id : -1), 952c9ce54d6SMatthew Dillon error, msg); 953c9ce54d6SMatthew Dillon 954cdb6e4e6SMatthew Dillon if (hmp->ronly == 0) { 955cdb6e4e6SMatthew Dillon hmp->ronly = 2; /* special errored read-only mode */ 956cdb6e4e6SMatthew Dillon hmp->mp->mnt_flag |= MNT_RDONLY; 957dfec479fSMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 958dfec479fSMatthew Dillon hammer_adjust_volume_mode, NULL); 959d053aa8aSTomohiro Kusumi hmkprintf(hmp, "Forcing read-only mode\n"); 960cdb6e4e6SMatthew Dillon } 961cdb6e4e6SMatthew Dillon hmp->error = error; 962fc73edd8SMatthew Dillon if (hammer_debug_critical) 963c9ce54d6SMatthew Dillon Debugger("Entering debugger"); 964cdb6e4e6SMatthew Dillon } 965cdb6e4e6SMatthew Dillon 966cdb6e4e6SMatthew Dillon 967cdb6e4e6SMatthew Dillon /* 968513ca7d7SMatthew Dillon * Obtain a vnode for the specified inode number. An exclusively locked 969513ca7d7SMatthew Dillon * vnode is returned. 970513ca7d7SMatthew Dillon */ 971513ca7d7SMatthew Dillon int 972b9b0a6d0SMatthew Dillon hammer_vfs_vget(struct mount *mp, struct vnode *dvp, 973b9b0a6d0SMatthew Dillon ino_t ino, struct vnode **vpp) 974513ca7d7SMatthew Dillon { 97536f82b23SMatthew Dillon struct hammer_transaction trans; 976513ca7d7SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 977513ca7d7SMatthew Dillon struct hammer_inode *ip; 978513ca7d7SMatthew Dillon int error; 979*46137e17STomohiro Kusumi uint32_t localization; 980513ca7d7SMatthew Dillon 981b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 98236f82b23SMatthew Dillon hammer_simple_transaction(&trans, hmp); 98336f82b23SMatthew Dillon 984513ca7d7SMatthew Dillon /* 985b9b0a6d0SMatthew Dillon * If a directory vnode is supplied (mainly NFS) then we can acquire 986b9b0a6d0SMatthew Dillon * the PFS domain from it. Otherwise we would only be able to vget 987b9b0a6d0SMatthew Dillon * inodes in the root PFS. 988b9b0a6d0SMatthew Dillon */ 989b9b0a6d0SMatthew Dillon if (dvp) { 990b9b0a6d0SMatthew Dillon localization = HAMMER_DEF_LOCALIZATION + 991b9b0a6d0SMatthew Dillon VTOI(dvp)->obj_localization; 992b9b0a6d0SMatthew Dillon } else { 993b9b0a6d0SMatthew Dillon localization = HAMMER_DEF_LOCALIZATION; 994b9b0a6d0SMatthew Dillon } 995b9b0a6d0SMatthew Dillon 996b9b0a6d0SMatthew Dillon /* 99736f82b23SMatthew Dillon * Lookup the requested HAMMER inode. The structure must be 99836f82b23SMatthew Dillon * left unlocked while we manipulate the related vnode to avoid 99936f82b23SMatthew Dillon * a deadlock. 1000513ca7d7SMatthew Dillon */ 1001ddfdf542SMatthew Dillon ip = hammer_get_inode(&trans, NULL, ino, 1002b9b0a6d0SMatthew Dillon hmp->asof, localization, 1003ddfdf542SMatthew Dillon 0, &error); 1004513ca7d7SMatthew Dillon if (ip == NULL) { 1005513ca7d7SMatthew Dillon *vpp = NULL; 1006b0aab9b9SMatthew Dillon } else { 1007e8599db1SMatthew Dillon error = hammer_get_vnode(ip, vpp); 1008513ca7d7SMatthew Dillon hammer_rel_inode(ip, 0); 1009b0aab9b9SMatthew Dillon } 1010b84de5afSMatthew Dillon hammer_done_transaction(&trans); 1011b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1012513ca7d7SMatthew Dillon return (error); 1013513ca7d7SMatthew Dillon } 1014513ca7d7SMatthew Dillon 1015513ca7d7SMatthew Dillon /* 1016427e5fc6SMatthew Dillon * Return the root vnode for the filesystem. 1017427e5fc6SMatthew Dillon * 1018427e5fc6SMatthew Dillon * HAMMER stores the root vnode in the hammer_mount structure so 1019427e5fc6SMatthew Dillon * getting it is easy. 1020427e5fc6SMatthew Dillon */ 1021427e5fc6SMatthew Dillon static int 1022427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp) 1023427e5fc6SMatthew Dillon { 102427ea2398SMatthew Dillon int error; 1025427e5fc6SMatthew Dillon 1026783fb170STomohiro Kusumi error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, vpp); 102727ea2398SMatthew Dillon return (error); 1028427e5fc6SMatthew Dillon } 1029427e5fc6SMatthew Dillon 1030427e5fc6SMatthew Dillon static int 1031427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 1032427e5fc6SMatthew Dillon { 1033fbc6e32aSMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 1034fbc6e32aSMatthew Dillon hammer_volume_t volume; 1035fbc6e32aSMatthew Dillon hammer_volume_ondisk_t ondisk; 1036fbc6e32aSMatthew Dillon int error; 103747197d71SMatthew Dillon int64_t bfree; 103831a56ce2SMatthew Dillon int64_t breserved; 1039fbc6e32aSMatthew Dillon 1040b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1041fbc6e32aSMatthew Dillon volume = hammer_get_root_volume(hmp, &error); 1042b0aab9b9SMatthew Dillon if (error) { 1043b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1044fbc6e32aSMatthew Dillon return(error); 1045b0aab9b9SMatthew Dillon } 1046fbc6e32aSMatthew Dillon ondisk = volume->ondisk; 1047fbc6e32aSMatthew Dillon 104847197d71SMatthew Dillon /* 104947197d71SMatthew Dillon * Basic stats 105047197d71SMatthew Dillon */ 105131a56ce2SMatthew Dillon _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved); 1052fbc6e32aSMatthew Dillon mp->mnt_stat.f_files = ondisk->vol0_stat_inodes; 1053e04ee2deSTomohiro Kusumi bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE; 105440043e7fSMatthew Dillon hammer_rel_volume(volume, 0); 105547197d71SMatthew Dillon 105631a56ce2SMatthew Dillon mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE; 105747197d71SMatthew Dillon mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 1058fbc6e32aSMatthew Dillon if (mp->mnt_stat.f_files < 0) 1059fbc6e32aSMatthew Dillon mp->mnt_stat.f_files = 0; 1060fbc6e32aSMatthew Dillon 106127ea2398SMatthew Dillon *sbp = mp->mnt_stat; 1062b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 106327ea2398SMatthew Dillon return(0); 1064427e5fc6SMatthew Dillon } 1065427e5fc6SMatthew Dillon 10666f97fce3SMatthew Dillon static int 10676f97fce3SMatthew Dillon hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 10686f97fce3SMatthew Dillon { 10696f97fce3SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 10706f97fce3SMatthew Dillon hammer_volume_t volume; 10716f97fce3SMatthew Dillon hammer_volume_ondisk_t ondisk; 10726f97fce3SMatthew Dillon int error; 10736f97fce3SMatthew Dillon int64_t bfree; 10740f65be10SMatthew Dillon int64_t breserved; 10756f97fce3SMatthew Dillon 1076b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 10776f97fce3SMatthew Dillon volume = hammer_get_root_volume(hmp, &error); 1078b0aab9b9SMatthew Dillon if (error) { 1079b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 10806f97fce3SMatthew Dillon return(error); 1081b0aab9b9SMatthew Dillon } 10826f97fce3SMatthew Dillon ondisk = volume->ondisk; 10836f97fce3SMatthew Dillon 10846f97fce3SMatthew Dillon /* 10856f97fce3SMatthew Dillon * Basic stats 10866f97fce3SMatthew Dillon */ 10870f65be10SMatthew Dillon _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved); 10886f97fce3SMatthew Dillon mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes; 1089e04ee2deSTomohiro Kusumi bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE; 10906f97fce3SMatthew Dillon hammer_rel_volume(volume, 0); 10916f97fce3SMatthew Dillon 10920f65be10SMatthew Dillon mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE; 1093c0763659SMatthew Dillon mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree; 10946f97fce3SMatthew Dillon if (mp->mnt_vstat.f_files < 0) 10956f97fce3SMatthew Dillon mp->mnt_vstat.f_files = 0; 10966f97fce3SMatthew Dillon *sbp = mp->mnt_vstat; 1097b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 10986f97fce3SMatthew Dillon return(0); 10996f97fce3SMatthew Dillon } 11006f97fce3SMatthew Dillon 11010729c8c8SMatthew Dillon /* 11020729c8c8SMatthew Dillon * Sync the filesystem. Currently we have to run it twice, the second 11030729c8c8SMatthew Dillon * one will advance the undo start index to the end index, so if a crash 11040729c8c8SMatthew Dillon * occurs no undos will be run on mount. 110577062c8aSMatthew Dillon * 110677062c8aSMatthew Dillon * We do not sync the filesystem if we are called from a panic. If we did 110777062c8aSMatthew Dillon * we might end up blowing up a sync that was already in progress. 11080729c8c8SMatthew Dillon */ 1109427e5fc6SMatthew Dillon static int 1110427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor) 1111427e5fc6SMatthew Dillon { 1112fbc6e32aSMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 11130729c8c8SMatthew Dillon int error; 11140729c8c8SMatthew Dillon 1115b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 111677062c8aSMatthew Dillon if (panicstr == NULL) { 11170729c8c8SMatthew Dillon error = hammer_sync_hmp(hmp, waitfor); 111877062c8aSMatthew Dillon } else { 111977062c8aSMatthew Dillon error = EIO; 112077062c8aSMatthew Dillon } 1121b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 11220729c8c8SMatthew Dillon return (error); 1123427e5fc6SMatthew Dillon } 1124427e5fc6SMatthew Dillon 1125513ca7d7SMatthew Dillon /* 1126513ca7d7SMatthew Dillon * Convert a vnode to a file handle. 1127b0aab9b9SMatthew Dillon * 1128b0aab9b9SMatthew Dillon * Accesses read-only fields on already-referenced structures so 1129b0aab9b9SMatthew Dillon * no token is needed. 1130513ca7d7SMatthew Dillon */ 1131513ca7d7SMatthew Dillon static int 1132513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp) 1133513ca7d7SMatthew Dillon { 1134513ca7d7SMatthew Dillon hammer_inode_t ip; 1135513ca7d7SMatthew Dillon 1136513ca7d7SMatthew Dillon KKASSERT(MAXFIDSZ >= 16); 1137513ca7d7SMatthew Dillon ip = VTOI(vp); 1138513ca7d7SMatthew Dillon fhp->fid_len = offsetof(struct fid, fid_data[16]); 1139adf01747SMatthew Dillon fhp->fid_ext = ip->obj_localization >> 16; 1140513ca7d7SMatthew Dillon bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id)); 1141513ca7d7SMatthew Dillon bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof)); 1142513ca7d7SMatthew Dillon return(0); 1143513ca7d7SMatthew Dillon } 1144513ca7d7SMatthew Dillon 1145513ca7d7SMatthew Dillon 1146513ca7d7SMatthew Dillon /* 1147513ca7d7SMatthew Dillon * Convert a file handle back to a vnode. 114867863d04SMatthew Dillon * 114967863d04SMatthew Dillon * Use rootvp to enforce PFS isolation when a PFS is exported via a 115067863d04SMatthew Dillon * null mount. 1151513ca7d7SMatthew Dillon */ 1152513ca7d7SMatthew Dillon static int 115367863d04SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 115467863d04SMatthew Dillon struct fid *fhp, struct vnode **vpp) 1155513ca7d7SMatthew Dillon { 1156b0aab9b9SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 115736f82b23SMatthew Dillon struct hammer_transaction trans; 1158513ca7d7SMatthew Dillon struct hammer_inode *ip; 1159513ca7d7SMatthew Dillon struct hammer_inode_info info; 1160513ca7d7SMatthew Dillon int error; 1161*46137e17STomohiro Kusumi uint32_t localization; 1162513ca7d7SMatthew Dillon 1163513ca7d7SMatthew Dillon bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id)); 1164513ca7d7SMatthew Dillon bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof)); 116567863d04SMatthew Dillon if (rootvp) 116667863d04SMatthew Dillon localization = VTOI(rootvp)->obj_localization; 116767863d04SMatthew Dillon else 1168*46137e17STomohiro Kusumi localization = (uint32_t)fhp->fid_ext << 16; 1169513ca7d7SMatthew Dillon 1170b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1171b0aab9b9SMatthew Dillon hammer_simple_transaction(&trans, hmp); 117236f82b23SMatthew Dillon 1173513ca7d7SMatthew Dillon /* 1174513ca7d7SMatthew Dillon * Get/allocate the hammer_inode structure. The structure must be 1175513ca7d7SMatthew Dillon * unlocked while we manipulate the related vnode to avoid a 1176513ca7d7SMatthew Dillon * deadlock. 1177513ca7d7SMatthew Dillon */ 1178ddfdf542SMatthew Dillon ip = hammer_get_inode(&trans, NULL, info.obj_id, 1179adf01747SMatthew Dillon info.obj_asof, localization, 0, &error); 1180d978e7cfSMatthew Dillon if (ip) { 1181e8599db1SMatthew Dillon error = hammer_get_vnode(ip, vpp); 1182513ca7d7SMatthew Dillon hammer_rel_inode(ip, 0); 1183d978e7cfSMatthew Dillon } else { 1184d978e7cfSMatthew Dillon *vpp = NULL; 1185d978e7cfSMatthew Dillon } 1186b84de5afSMatthew Dillon hammer_done_transaction(&trans); 1187b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1188513ca7d7SMatthew Dillon return (error); 1189513ca7d7SMatthew Dillon } 1190513ca7d7SMatthew Dillon 1191513ca7d7SMatthew Dillon static int 1192513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 1193513ca7d7SMatthew Dillon int *exflagsp, struct ucred **credanonp) 1194513ca7d7SMatthew Dillon { 1195513ca7d7SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 1196513ca7d7SMatthew Dillon struct netcred *np; 1197513ca7d7SMatthew Dillon int error; 1198513ca7d7SMatthew Dillon 1199b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1200513ca7d7SMatthew Dillon np = vfs_export_lookup(mp, &hmp->export, nam); 1201513ca7d7SMatthew Dillon if (np) { 1202513ca7d7SMatthew Dillon *exflagsp = np->netc_exflags; 1203513ca7d7SMatthew Dillon *credanonp = &np->netc_anon; 1204513ca7d7SMatthew Dillon error = 0; 1205513ca7d7SMatthew Dillon } else { 1206513ca7d7SMatthew Dillon error = EACCES; 1207513ca7d7SMatthew Dillon } 1208b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1209513ca7d7SMatthew Dillon return (error); 1210513ca7d7SMatthew Dillon 1211513ca7d7SMatthew Dillon } 1212513ca7d7SMatthew Dillon 1213513ca7d7SMatthew Dillon int 1214513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export) 1215513ca7d7SMatthew Dillon { 1216513ca7d7SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 1217513ca7d7SMatthew Dillon int error; 1218513ca7d7SMatthew Dillon 1219b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1220b0aab9b9SMatthew Dillon 1221513ca7d7SMatthew Dillon switch(op) { 1222513ca7d7SMatthew Dillon case MOUNTCTL_SET_EXPORT: 1223513ca7d7SMatthew Dillon error = vfs_export(mp, &hmp->export, export); 1224513ca7d7SMatthew Dillon break; 1225513ca7d7SMatthew Dillon default: 1226513ca7d7SMatthew Dillon error = EOPNOTSUPP; 1227513ca7d7SMatthew Dillon break; 1228513ca7d7SMatthew Dillon } 1229b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1230b0aab9b9SMatthew Dillon 1231513ca7d7SMatthew Dillon return(error); 1232513ca7d7SMatthew Dillon } 1233513ca7d7SMatthew Dillon 1234