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 35427e5fc6SMatthew Dillon #include <sys/nlookup.h> 36427e5fc6SMatthew Dillon #include <sys/fcntl.h> 37b3deaf57SMatthew Dillon #include <sys/sysctl.h> 38b45803e3STomohiro Kusumi 39427e5fc6SMatthew Dillon #include "hammer.h" 40427e5fc6SMatthew Dillon 41aac0aabdSMatthew Dillon /* 42aac0aabdSMatthew Dillon * NOTE! Global statistics may not be MPSAFE so HAMMER never uses them 43aac0aabdSMatthew Dillon * in conditionals. 44aac0aabdSMatthew Dillon */ 4502428fb6SMatthew Dillon int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT; 462f85fa4dSMatthew Dillon int hammer_debug_io; 47d5ef456eSMatthew Dillon int hammer_debug_general; 481b0ab2c3SMatthew Dillon int hammer_debug_debug = 1; /* medium-error panics */ 49e8599db1SMatthew Dillon int hammer_debug_inode; 507d683b0fSMatthew Dillon int hammer_debug_locks; 51b3deaf57SMatthew Dillon int hammer_debug_btree; 52d113fda1SMatthew Dillon int hammer_debug_tid; 5346fe7ae1SMatthew Dillon int hammer_debug_recover; /* -1 will disable, +1 will force */ 5446fe7ae1SMatthew Dillon int hammer_debug_recover_faults; 55fc73edd8SMatthew Dillon int hammer_debug_critical; /* non-zero enter debugger on error */ 561b0ab2c3SMatthew Dillon int hammer_cluster_enable = 1; /* enable read clustering by default */ 57507df98aSIlya Dryomov int hammer_live_dedup = 0; 58e2a02b72SMatthew Dillon int hammer_tdmux_ticks; 597a61b85dSMatthew Dillon int hammer_count_fsyncs; 60b3deaf57SMatthew Dillon int hammer_count_inodes; 61af209b0fSMatthew Dillon int hammer_count_iqueued; 62e2a02b72SMatthew Dillon int hammer_count_reclaims; 63b3deaf57SMatthew Dillon int hammer_count_records; 64b3deaf57SMatthew Dillon int hammer_count_record_datas; 65b3deaf57SMatthew Dillon int hammer_count_volumes; 66b3deaf57SMatthew Dillon int hammer_count_buffers; 67b3deaf57SMatthew Dillon int hammer_count_nodes; 68a7e9bef1SMatthew Dillon int64_t hammer_count_extra_space_used; 69cb51be26SMatthew Dillon int64_t hammer_stats_btree_lookups; 70cb51be26SMatthew Dillon int64_t hammer_stats_btree_searches; 71cb51be26SMatthew Dillon int64_t hammer_stats_btree_inserts; 72cb51be26SMatthew Dillon int64_t hammer_stats_btree_deletes; 73cb51be26SMatthew Dillon int64_t hammer_stats_btree_elements; 74cb51be26SMatthew Dillon int64_t hammer_stats_btree_splits; 75cb51be26SMatthew Dillon int64_t hammer_stats_btree_iterations; 7639d8fd63SMatthew Dillon int64_t hammer_stats_btree_root_iterations; 77cb51be26SMatthew Dillon int64_t hammer_stats_record_iterations; 78ce0138a6SMatthew Dillon 79ce0138a6SMatthew Dillon int64_t hammer_stats_file_read; 80ce0138a6SMatthew Dillon int64_t hammer_stats_file_write; 81ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsr; 82ce0138a6SMatthew Dillon int64_t hammer_stats_file_iopsw; 83ce0138a6SMatthew Dillon int64_t hammer_stats_disk_read; 84ce0138a6SMatthew Dillon int64_t hammer_stats_disk_write; 85ce0138a6SMatthew Dillon int64_t hammer_stats_inode_flushes; 86ce0138a6SMatthew Dillon int64_t hammer_stats_commits; 8789e744ceSMatthew Dillon int64_t hammer_stats_undo; 886048b411SMatthew Dillon int64_t hammer_stats_redo; 89ce0138a6SMatthew Dillon 903583bbb4SMatthew Dillon long hammer_count_dirtybufspace; /* global */ 91a99b9ea2SMatthew Dillon int hammer_count_refedbufs; /* global */ 920832c9bbSMatthew Dillon int hammer_count_reservations; 933583bbb4SMatthew Dillon long hammer_count_io_running_read; 943583bbb4SMatthew Dillon long hammer_count_io_running_write; 95a99b9ea2SMatthew Dillon int hammer_count_io_locked; 963583bbb4SMatthew Dillon long hammer_limit_dirtybufspace; /* per-mount */ 970832c9bbSMatthew Dillon int hammer_limit_recs; /* as a whole XXX */ 98a117fbebSMatthew Dillon int hammer_limit_inode_recs = 2048; /* per inode */ 99e2a02b72SMatthew Dillon int hammer_limit_reclaims; 100e2ef7a95SIlya Dryomov int hammer_live_dedup_cache_size = DEDUP_CACHE_SIZE; 1016048b411SMatthew Dillon int hammer_limit_redo = 4096 * 1024; /* per inode */ 10237646115SMatthew Dillon int hammer_autoflush = 500; /* auto flush (typ on reclaim) */ 1031f07f686SMatthew Dillon int hammer_bio_count; 104cb51be26SMatthew Dillon int hammer_verify_zone; 1051b0ab2c3SMatthew Dillon int hammer_verify_data = 1; 106cb51be26SMatthew Dillon int hammer_write_mode; 107b4f86ea3SMatthew Dillon int hammer_double_buffer; 108283a4a38SMatthew Dillon int hammer_btree_full_undo = 1; 1093e583440SMatthew Dillon int hammer_yield_check = 16; 1109d0a6205SMatthew Dillon int hammer_fsync_mode = 3; 1117d683b0fSMatthew Dillon int64_t hammer_contention_count; 112f03c9cf4SMatthew Dillon int64_t hammer_zone_limit; 113b3deaf57SMatthew Dillon 114507df98aSIlya Dryomov /* 115507df98aSIlya Dryomov * Live dedup debug counters (sysctls are writable so that counters 116507df98aSIlya Dryomov * can be reset from userspace). 117507df98aSIlya Dryomov */ 118507df98aSIlya Dryomov int64_t hammer_live_dedup_vnode_bcmps = 0; 119507df98aSIlya Dryomov int64_t hammer_live_dedup_device_bcmps = 0; 120507df98aSIlya Dryomov int64_t hammer_live_dedup_findblk_failures = 0; 121507df98aSIlya Dryomov int64_t hammer_live_dedup_bmap_saves = 0; 122507df98aSIlya Dryomov 123507df98aSIlya Dryomov 124b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem"); 125507df98aSIlya Dryomov 1265987cc42SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD, 1275987cc42SMatthew Dillon &hammer_supported_version, 0, ""); 128d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW, 129d5ef456eSMatthew Dillon &hammer_debug_general, 0, ""); 1302f85fa4dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW, 1312f85fa4dSMatthew Dillon &hammer_debug_io, 0, ""); 13277062c8aSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW, 13377062c8aSMatthew Dillon &hammer_debug_debug, 0, ""); 134e8599db1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW, 135e8599db1SMatthew Dillon &hammer_debug_inode, 0, ""); 1367d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW, 1377d683b0fSMatthew Dillon &hammer_debug_locks, 0, ""); 138b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW, 139b3deaf57SMatthew Dillon &hammer_debug_btree, 0, ""); 140d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW, 141d113fda1SMatthew Dillon &hammer_debug_tid, 0, ""); 142b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW, 143b33e2cc0SMatthew Dillon &hammer_debug_recover, 0, ""); 14446fe7ae1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW, 14546fe7ae1SMatthew Dillon &hammer_debug_recover_faults, 0, ""); 146fc73edd8SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW, 147fc73edd8SMatthew Dillon &hammer_debug_critical, 0, ""); 1481b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW, 1491b0ab2c3SMatthew Dillon &hammer_cluster_enable, 0, ""); 150507df98aSIlya Dryomov /* 151507df98aSIlya Dryomov * 0 - live dedup is disabled 152507df98aSIlya Dryomov * 1 - dedup cache is populated on reads only 153507df98aSIlya Dryomov * 2 - dedup cache is populated on both reads and writes 154283a4a38SMatthew Dillon * 155283a4a38SMatthew Dillon * LIVE_DEDUP IS DISABLED PERMANENTLY! This feature appears to cause 156283a4a38SMatthew Dillon * blockmap corruption over time so we've turned it off permanently. 157507df98aSIlya Dryomov */ 1586de803f0SFrançois Tigeot SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup, CTLFLAG_RD, 1596de803f0SFrançois Tigeot &hammer_live_dedup, 0, "Enable live dedup (experimental)"); 160e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, tdmux_ticks, CTLFLAG_RW, 161e2a02b72SMatthew Dillon &hammer_tdmux_ticks, 0, "Hammer tdmux ticks"); 1629480ff55SMatthew Dillon 1633583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW, 164f5a07a7aSMatthew Dillon &hammer_limit_dirtybufspace, 0, ""); 16547637bffSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW, 16647637bffSMatthew Dillon &hammer_limit_recs, 0, ""); 167de996e86SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW, 168de996e86SMatthew Dillon &hammer_limit_inode_recs, 0, ""); 169e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaims, CTLFLAG_RW, 170e2a02b72SMatthew Dillon &hammer_limit_reclaims, 0, ""); 171e2ef7a95SIlya Dryomov SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW, 1722b34db9bSSamuel J. Greear &hammer_live_dedup_cache_size, 0, 1732b34db9bSSamuel J. Greear "Number of cache entries"); 1746048b411SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW, 1756048b411SMatthew Dillon &hammer_limit_redo, 0, ""); 1769480ff55SMatthew Dillon 1777a61b85dSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD, 1787a61b85dSMatthew Dillon &hammer_count_fsyncs, 0, ""); 179b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD, 180b3deaf57SMatthew Dillon &hammer_count_inodes, 0, ""); 181af209b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD, 182af209b0fSMatthew Dillon &hammer_count_iqueued, 0, ""); 183e2a02b72SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaims, CTLFLAG_RD, 184e2a02b72SMatthew Dillon &hammer_count_reclaims, 0, ""); 185b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD, 186b3deaf57SMatthew Dillon &hammer_count_records, 0, ""); 187b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD, 188b3deaf57SMatthew Dillon &hammer_count_record_datas, 0, ""); 189b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD, 190b3deaf57SMatthew Dillon &hammer_count_volumes, 0, ""); 191b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD, 192b3deaf57SMatthew Dillon &hammer_count_buffers, 0, ""); 193b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD, 194b3deaf57SMatthew Dillon &hammer_count_nodes, 0, ""); 195a7e9bef1SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD, 196a7e9bef1SMatthew Dillon &hammer_count_extra_space_used, 0, ""); 197ce0138a6SMatthew Dillon 198cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD, 199cb51be26SMatthew Dillon &hammer_stats_btree_searches, 0, ""); 200cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD, 201cb51be26SMatthew Dillon &hammer_stats_btree_lookups, 0, ""); 202cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD, 203cb51be26SMatthew Dillon &hammer_stats_btree_inserts, 0, ""); 204cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD, 205cb51be26SMatthew Dillon &hammer_stats_btree_deletes, 0, ""); 206cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD, 207cb51be26SMatthew Dillon &hammer_stats_btree_elements, 0, ""); 208cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD, 209cb51be26SMatthew Dillon &hammer_stats_btree_splits, 0, ""); 210cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD, 211cb51be26SMatthew Dillon &hammer_stats_btree_iterations, 0, ""); 21239d8fd63SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD, 21339d8fd63SMatthew Dillon &hammer_stats_btree_root_iterations, 0, ""); 214cb51be26SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD, 215cb51be26SMatthew Dillon &hammer_stats_record_iterations, 0, ""); 216ce0138a6SMatthew Dillon 217ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD, 218ce0138a6SMatthew Dillon &hammer_stats_file_read, 0, ""); 219ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD, 220ce0138a6SMatthew Dillon &hammer_stats_file_write, 0, ""); 221ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD, 222ce0138a6SMatthew Dillon &hammer_stats_file_iopsr, 0, ""); 223ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD, 224ce0138a6SMatthew Dillon &hammer_stats_file_iopsw, 0, ""); 225ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD, 226ce0138a6SMatthew Dillon &hammer_stats_disk_read, 0, ""); 227ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD, 228ce0138a6SMatthew Dillon &hammer_stats_disk_write, 0, ""); 229ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD, 230ce0138a6SMatthew Dillon &hammer_stats_inode_flushes, 0, ""); 231ce0138a6SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD, 232ce0138a6SMatthew Dillon &hammer_stats_commits, 0, ""); 23389e744ceSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD, 23489e744ceSMatthew Dillon &hammer_stats_undo, 0, ""); 2356048b411SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD, 2366048b411SMatthew Dillon &hammer_stats_redo, 0, ""); 237ce0138a6SMatthew Dillon 238507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_vnode_bcmps, CTLFLAG_RW, 239f06b3d6dSSamuel J. Greear &hammer_live_dedup_vnode_bcmps, 0, 240f06b3d6dSSamuel J. Greear "successful vnode buffer comparisons"); 241507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_device_bcmps, CTLFLAG_RW, 242f06b3d6dSSamuel J. Greear &hammer_live_dedup_device_bcmps, 0, 243f06b3d6dSSamuel J. Greear "successful device buffer comparisons"); 244507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_findblk_failures, CTLFLAG_RW, 245f06b3d6dSSamuel J. Greear &hammer_live_dedup_findblk_failures, 0, 246f06b3d6dSSamuel J. Greear "block lookup failures for comparison"); 247507df98aSIlya Dryomov SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_bmap_saves, CTLFLAG_RW, 248f06b3d6dSSamuel J. Greear &hammer_live_dedup_bmap_saves, 0, 249f06b3d6dSSamuel J. Greear "useful physical block lookups"); 250507df98aSIlya Dryomov 2513583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD, 252f5a07a7aSMatthew Dillon &hammer_count_dirtybufspace, 0, ""); 253a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD, 254a99b9ea2SMatthew Dillon &hammer_count_refedbufs, 0, ""); 2550832c9bbSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD, 2560832c9bbSMatthew Dillon &hammer_count_reservations, 0, ""); 2573583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD, 258a99b9ea2SMatthew Dillon &hammer_count_io_running_read, 0, ""); 259a99b9ea2SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD, 260a99b9ea2SMatthew Dillon &hammer_count_io_locked, 0, ""); 2613583bbb4SMatthew Dillon SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD, 262a99b9ea2SMatthew Dillon &hammer_count_io_running_write, 0, ""); 263f03c9cf4SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW, 264f03c9cf4SMatthew Dillon &hammer_zone_limit, 0, ""); 2657d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW, 2667d683b0fSMatthew Dillon &hammer_contention_count, 0, ""); 26721fde338SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW, 26821fde338SMatthew Dillon &hammer_autoflush, 0, ""); 269cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW, 270cb51be26SMatthew Dillon &hammer_verify_zone, 0, ""); 2711b0ab2c3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW, 2721b0ab2c3SMatthew Dillon &hammer_verify_data, 0, ""); 273cb51be26SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW, 274cb51be26SMatthew Dillon &hammer_write_mode, 0, ""); 275b4f86ea3SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, double_buffer, CTLFLAG_RW, 276b4f86ea3SMatthew Dillon &hammer_double_buffer, 0, ""); 277283a4a38SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, btree_full_undo, CTLFLAG_RW, 278283a4a38SMatthew Dillon &hammer_btree_full_undo, 0, ""); 2793e583440SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW, 2803e583440SMatthew Dillon &hammer_yield_check, 0, ""); 2816f3d87c0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW, 2826f3d87c0SMatthew Dillon &hammer_fsync_mode, 0, ""); 283b3deaf57SMatthew Dillon 284879a1b60SSascha Wildner /* KTR_INFO_MASTER(hammer); */ 285bcac4bbbSMatthew Dillon 286427e5fc6SMatthew Dillon /* 287427e5fc6SMatthew Dillon * VFS ABI 288427e5fc6SMatthew Dillon */ 289427e5fc6SMatthew Dillon static void hammer_free_hmp(struct mount *mp); 290427e5fc6SMatthew Dillon 291427e5fc6SMatthew Dillon static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data, 292427e5fc6SMatthew Dillon struct ucred *cred); 293427e5fc6SMatthew Dillon static int hammer_vfs_unmount(struct mount *mp, int mntflags); 294427e5fc6SMatthew Dillon static int hammer_vfs_root(struct mount *mp, struct vnode **vpp); 295427e5fc6SMatthew Dillon static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, 296427e5fc6SMatthew Dillon struct ucred *cred); 2976f97fce3SMatthew Dillon static int hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 2986f97fce3SMatthew Dillon struct ucred *cred); 299427e5fc6SMatthew Dillon static int hammer_vfs_sync(struct mount *mp, int waitfor); 300b9b0a6d0SMatthew Dillon static int hammer_vfs_vget(struct mount *mp, struct vnode *dvp, 301b9b0a6d0SMatthew Dillon ino_t ino, struct vnode **vpp); 302427e5fc6SMatthew Dillon static int hammer_vfs_init(struct vfsconf *conf); 30367863d04SMatthew Dillon static int hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 30467863d04SMatthew Dillon struct fid *fhp, struct vnode **vpp); 305513ca7d7SMatthew Dillon static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp); 306513ca7d7SMatthew Dillon static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 307513ca7d7SMatthew Dillon int *exflagsp, struct ucred **credanonp); 308513ca7d7SMatthew Dillon 309427e5fc6SMatthew Dillon 310427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = { 311427e5fc6SMatthew Dillon .vfs_mount = hammer_vfs_mount, 312427e5fc6SMatthew Dillon .vfs_unmount = hammer_vfs_unmount, 313427e5fc6SMatthew Dillon .vfs_root = hammer_vfs_root, 314427e5fc6SMatthew Dillon .vfs_statfs = hammer_vfs_statfs, 3156f97fce3SMatthew Dillon .vfs_statvfs = hammer_vfs_statvfs, 316427e5fc6SMatthew Dillon .vfs_sync = hammer_vfs_sync, 317427e5fc6SMatthew Dillon .vfs_vget = hammer_vfs_vget, 318513ca7d7SMatthew Dillon .vfs_init = hammer_vfs_init, 319513ca7d7SMatthew Dillon .vfs_vptofh = hammer_vfs_vptofh, 320513ca7d7SMatthew Dillon .vfs_fhtovp = hammer_vfs_fhtovp, 321513ca7d7SMatthew Dillon .vfs_checkexp = hammer_vfs_checkexp 322427e5fc6SMatthew Dillon }; 323427e5fc6SMatthew Dillon 324bac808feSMatthew Dillon MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", ""); 325427e5fc6SMatthew Dillon 326427e5fc6SMatthew Dillon VFS_SET(hammer_vfsops, hammer, 0); 327427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1); 328427e5fc6SMatthew Dillon 329427e5fc6SMatthew Dillon static int 330427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf) 331427e5fc6SMatthew Dillon { 33269906b47SMatthew Dillon long n; 3333098dc2fSMatthew Dillon 334e2a02b72SMatthew Dillon /* 335e2a02b72SMatthew Dillon * Wait up to this long for an exclusive deadlock to clear 336e2a02b72SMatthew Dillon * before acquiring a new shared lock on the ip. The deadlock 337e2a02b72SMatthew Dillon * may have occured on a b-tree node related to the ip. 338e2a02b72SMatthew Dillon */ 339e2a02b72SMatthew Dillon if (hammer_tdmux_ticks == 0) 340e2a02b72SMatthew Dillon hammer_tdmux_ticks = hz / 5; 341e2a02b72SMatthew Dillon 342e2a02b72SMatthew Dillon /* 34369906b47SMatthew Dillon * Autosize, but be careful because a hammer filesystem's 34469906b47SMatthew Dillon * reserve is partially calculated based on dirtybufspace, 34569906b47SMatthew Dillon * so we simply cannot allow it to get too large. 346e2a02b72SMatthew Dillon */ 3473098dc2fSMatthew Dillon if (hammer_limit_recs == 0) { 34869906b47SMatthew Dillon n = nbuf * 25; 34969906b47SMatthew Dillon if (n > kmalloc_limit(M_HAMMER) / 512) 3503098dc2fSMatthew Dillon n = kmalloc_limit(M_HAMMER) / 512; 35169906b47SMatthew Dillon if (n > 2 * 1024 * 1024) 35269906b47SMatthew Dillon n = 2 * 1024 * 1024; 35369906b47SMatthew Dillon hammer_limit_recs = (int)n; 3543098dc2fSMatthew Dillon } 355f5a07a7aSMatthew Dillon if (hammer_limit_dirtybufspace == 0) { 356f5a07a7aSMatthew Dillon hammer_limit_dirtybufspace = hidirtybufspace / 2; 35769906b47SMatthew Dillon if (hammer_limit_dirtybufspace < 1L * 1024 * 1024) 35869906b47SMatthew Dillon hammer_limit_dirtybufspace = 1024L * 1024; 35969906b47SMatthew Dillon if (hammer_limit_dirtybufspace > 1024L * 1024 * 1024) 36069906b47SMatthew Dillon hammer_limit_dirtybufspace = 1024L * 1024 * 1024; 3619f5097dcSMatthew Dillon } 362ba298df1SMatthew Dillon 363ba298df1SMatthew Dillon /* 36437646115SMatthew Dillon * The hammer_inode structure detaches from the vnode on reclaim. 36537646115SMatthew Dillon * This limits the number of inodes in this state to prevent a 36637646115SMatthew Dillon * memory pool blowout. 36737646115SMatthew Dillon */ 368e2a02b72SMatthew Dillon if (hammer_limit_reclaims == 0) 369e2a02b72SMatthew Dillon hammer_limit_reclaims = desiredvnodes / 10; 37037646115SMatthew Dillon 371427e5fc6SMatthew Dillon return(0); 372427e5fc6SMatthew Dillon } 373427e5fc6SMatthew Dillon 374427e5fc6SMatthew Dillon static int 375427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data, 376427e5fc6SMatthew Dillon struct ucred *cred) 377427e5fc6SMatthew Dillon { 378427e5fc6SMatthew Dillon struct hammer_mount_info info; 379a89aec1bSMatthew Dillon hammer_mount_t hmp; 380a89aec1bSMatthew Dillon hammer_volume_t rootvol; 38127ea2398SMatthew Dillon struct vnode *rootvp; 3827c19b529SMichael Neumann struct vnode *devvp = NULL; 383427e5fc6SMatthew Dillon const char *upath; /* volume name in userspace */ 384427e5fc6SMatthew Dillon char *path; /* volume name in system space */ 385427e5fc6SMatthew Dillon int error; 386427e5fc6SMatthew Dillon int i; 387732a1697SMatthew Dillon int master_id; 388c302e844STomohiro Kusumi int nvolumes; 389104cb849SMichael Neumann char *next_volume_ptr = NULL; 390ab0bf4a3SMatthew Dillon 391ab0bf4a3SMatthew Dillon /* 392ab0bf4a3SMatthew Dillon * Accept hammer_mount_info. mntpt is NULL for root mounts at boot. 393ab0bf4a3SMatthew Dillon */ 394ab0bf4a3SMatthew Dillon if (mntpt == NULL) { 3957c19b529SMichael Neumann bzero(&info, sizeof(info)); 3967c19b529SMichael Neumann info.asof = 0; 3977c19b529SMichael Neumann info.hflags = 0; 3987c19b529SMichael Neumann info.nvolumes = 1; 399104cb849SMichael Neumann 400104cb849SMichael Neumann next_volume_ptr = mp->mnt_stat.f_mntfromname; 401104cb849SMichael Neumann 402104cb849SMichael Neumann /* Count number of volumes separated by ':' */ 403104cb849SMichael Neumann for (char *p = next_volume_ptr; *p != '\0'; ++p) { 404104cb849SMichael Neumann if (*p == ':') { 405104cb849SMichael Neumann ++info.nvolumes; 406104cb849SMichael Neumann } 407104cb849SMichael Neumann } 408104cb849SMichael Neumann 409104cb849SMichael Neumann mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */ 4107c19b529SMichael Neumann } else { 411427e5fc6SMatthew Dillon if ((error = copyin(data, &info, sizeof(info))) != 0) 412427e5fc6SMatthew Dillon return (error); 4137c19b529SMichael Neumann } 414427e5fc6SMatthew Dillon 415427e5fc6SMatthew Dillon /* 416732a1697SMatthew Dillon * updating or new mount 417427e5fc6SMatthew Dillon */ 418195c19a1SMatthew Dillon if (mp->mnt_flag & MNT_UPDATE) { 419195c19a1SMatthew Dillon hmp = (void *)mp->mnt_data; 420195c19a1SMatthew Dillon KKASSERT(hmp != NULL); 421195c19a1SMatthew Dillon } else { 4227529c2acSAntonio Huete Jimenez if (info.nvolumes <= 0 || info.nvolumes > HAMMER_MAX_VOLUMES) 423732a1697SMatthew Dillon return (EINVAL); 424732a1697SMatthew Dillon hmp = NULL; 425732a1697SMatthew Dillon } 426732a1697SMatthew Dillon 427732a1697SMatthew Dillon /* 428732a1697SMatthew Dillon * master-id validation. The master id may not be changed by a 429732a1697SMatthew Dillon * mount update. 430732a1697SMatthew Dillon */ 431732a1697SMatthew Dillon if (info.hflags & HMNT_MASTERID) { 432732a1697SMatthew Dillon if (hmp && hmp->master_id != info.master_id) { 433c54975d5STomohiro Kusumi kprintf("HAMMER: cannot change master id " 434732a1697SMatthew Dillon "with mount update\n"); 435732a1697SMatthew Dillon return(EINVAL); 436732a1697SMatthew Dillon } 437732a1697SMatthew Dillon master_id = info.master_id; 438732a1697SMatthew Dillon if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS) 439732a1697SMatthew Dillon return (EINVAL); 440732a1697SMatthew Dillon } else { 441732a1697SMatthew Dillon if (hmp) 442732a1697SMatthew Dillon master_id = hmp->master_id; 443732a1697SMatthew Dillon else 444732a1697SMatthew Dillon master_id = 0; 445732a1697SMatthew Dillon } 446732a1697SMatthew Dillon 447732a1697SMatthew Dillon /* 448b0aab9b9SMatthew Dillon * Internal mount data structure 449732a1697SMatthew Dillon */ 450732a1697SMatthew Dillon if (hmp == NULL) { 451427e5fc6SMatthew Dillon hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO); 452427e5fc6SMatthew Dillon mp->mnt_data = (qaddr_t)hmp; 453427e5fc6SMatthew Dillon hmp->mp = mp; 45446fe7ae1SMatthew Dillon /*TAILQ_INIT(&hmp->recycle_list);*/ 45547197d71SMatthew Dillon 456bac808feSMatthew Dillon /* 4577c457ac8SMatthew Dillon * Make sure kmalloc type limits are set appropriately. 4587c457ac8SMatthew Dillon * 4597c457ac8SMatthew Dillon * Our inode kmalloc group is sized based on maxvnodes 4607c457ac8SMatthew Dillon * (controlled by the system, not us). 461bac808feSMatthew Dillon */ 462bac808feSMatthew Dillon kmalloc_create(&hmp->m_misc, "HAMMER-others"); 463bac808feSMatthew Dillon kmalloc_create(&hmp->m_inodes, "HAMMER-inodes"); 464bac808feSMatthew Dillon 4657c457ac8SMatthew Dillon kmalloc_raise_limit(hmp->m_inodes, 0); /* unlimited */ 466bac808feSMatthew Dillon 467dd94f1b1SMatthew Dillon hmp->root_btree_beg.localization = 0x00000000U; 46847197d71SMatthew Dillon hmp->root_btree_beg.obj_id = -0x8000000000000000LL; 46947197d71SMatthew Dillon hmp->root_btree_beg.key = -0x8000000000000000LL; 47047197d71SMatthew Dillon hmp->root_btree_beg.create_tid = 1; 47147197d71SMatthew Dillon hmp->root_btree_beg.delete_tid = 1; 47247197d71SMatthew Dillon hmp->root_btree_beg.rec_type = 0; 47347197d71SMatthew Dillon hmp->root_btree_beg.obj_type = 0; 474bfbd8331STomohiro Kusumi hmp->root_btree_beg.btype = HAMMER_BTREE_TYPE_NONE; 47547197d71SMatthew Dillon 476dd94f1b1SMatthew Dillon hmp->root_btree_end.localization = 0xFFFFFFFFU; 47747197d71SMatthew Dillon hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL; 47847197d71SMatthew Dillon hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL; 47947197d71SMatthew Dillon hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL; 48047197d71SMatthew Dillon hmp->root_btree_end.delete_tid = 0; /* special case */ 48147197d71SMatthew Dillon hmp->root_btree_end.rec_type = 0xFFFFU; 48247197d71SMatthew Dillon hmp->root_btree_end.obj_type = 0; 483bfbd8331STomohiro Kusumi hmp->root_btree_end.btype = HAMMER_BTREE_TYPE_NONE; 484f03c9cf4SMatthew Dillon 485cdb6e4e6SMatthew Dillon hmp->krate.freq = 1; /* maximum reporting rate (hz) */ 486cdb6e4e6SMatthew Dillon hmp->krate.count = -16; /* initial burst */ 487a8d31329SMatthew Dillon hmp->kdiag.freq = 1; /* maximum reporting rate (hz) */ 488a8d31329SMatthew Dillon hmp->kdiag.count = -16; /* initial burst */ 489cdb6e4e6SMatthew Dillon 4909480ff55SMatthew Dillon hmp->sync_lock.refs = 1; 491c9b9e29dSMatthew Dillon hmp->free_lock.refs = 1; 492d99d6bf5SMatthew Dillon hmp->undo_lock.refs = 1; 493d99d6bf5SMatthew Dillon hmp->blkmap_lock.refs = 1; 49483f2a3aaSMatthew Dillon hmp->snapshot_lock.refs = 1; 49552e547e3SMichael Neumann hmp->volume_lock.refs = 1; 4969480ff55SMatthew Dillon 497cebe9493SMatthew Dillon TAILQ_INIT(&hmp->delay_list); 4987a61b85dSMatthew Dillon TAILQ_INIT(&hmp->flush_group_list); 4990729c8c8SMatthew Dillon TAILQ_INIT(&hmp->objid_cache_list); 500e8599db1SMatthew Dillon TAILQ_INIT(&hmp->undo_lru_list); 5017bc5b8c2SMatthew Dillon TAILQ_INIT(&hmp->reclaim_list); 502507df98aSIlya Dryomov 503507df98aSIlya Dryomov RB_INIT(&hmp->rb_dedup_crc_root); 504507df98aSIlya Dryomov RB_INIT(&hmp->rb_dedup_off_root); 505507df98aSIlya Dryomov TAILQ_INIT(&hmp->dedup_lru_list); 506195c19a1SMatthew Dillon } 50751c35492SMatthew Dillon hmp->hflags &= ~HMNT_USERFLAGS; 50851c35492SMatthew Dillon hmp->hflags |= info.hflags & HMNT_USERFLAGS; 509732a1697SMatthew Dillon 510732a1697SMatthew Dillon hmp->master_id = master_id; 511732a1697SMatthew Dillon 5127f7c1f84SMatthew Dillon if (info.asof) { 5137f7c1f84SMatthew Dillon mp->mnt_flag |= MNT_RDONLY; 5147f7c1f84SMatthew Dillon hmp->asof = info.asof; 5157f7c1f84SMatthew Dillon } else { 5167f7c1f84SMatthew Dillon hmp->asof = HAMMER_MAX_TID; 5177f7c1f84SMatthew Dillon } 518195c19a1SMatthew Dillon 519865c9609SMichael Neumann hmp->volume_to_remove = -1; 520865c9609SMichael Neumann 521195c19a1SMatthew Dillon /* 52251c35492SMatthew Dillon * Re-open read-write if originally read-only, or vise-versa. 52302428fb6SMatthew Dillon * 52402428fb6SMatthew Dillon * When going from read-only to read-write execute the stage2 52502428fb6SMatthew Dillon * recovery if it has not already been run. 526195c19a1SMatthew Dillon */ 527195c19a1SMatthew Dillon if (mp->mnt_flag & MNT_UPDATE) { 528b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 52951c35492SMatthew Dillon error = 0; 53051c35492SMatthew Dillon if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 531c54975d5STomohiro Kusumi kprintf("HAMMER: read-only -> read-write\n"); 532195c19a1SMatthew Dillon hmp->ronly = 0; 53351c35492SMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 53451c35492SMatthew Dillon hammer_adjust_volume_mode, NULL); 53551c35492SMatthew Dillon rootvol = hammer_get_root_volume(hmp, &error); 53651c35492SMatthew Dillon if (rootvol) { 53706ad81ffSMatthew Dillon hammer_recover_flush_buffers(hmp, rootvol, 1); 53802428fb6SMatthew Dillon error = hammer_recover_stage2(hmp, rootvol); 5399f5097dcSMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, 5409f5097dcSMatthew Dillon hmp->blockmap, 5419f5097dcSMatthew Dillon sizeof(hmp->blockmap)); 54251c35492SMatthew Dillon hammer_rel_volume(rootvol, 0); 543195c19a1SMatthew Dillon } 54451c35492SMatthew Dillon RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 54551c35492SMatthew Dillon hammer_reload_inode, NULL); 54651c35492SMatthew Dillon /* kernel clears MNT_RDONLY */ 54751c35492SMatthew Dillon } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 548c54975d5STomohiro Kusumi kprintf("HAMMER: read-write -> read-only\n"); 54951c35492SMatthew Dillon hmp->ronly = 1; /* messy */ 55051c35492SMatthew Dillon RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 55151c35492SMatthew Dillon hammer_reload_inode, NULL); 55251c35492SMatthew Dillon hmp->ronly = 0; 55351c35492SMatthew Dillon hammer_flusher_sync(hmp); 55451c35492SMatthew Dillon hammer_flusher_sync(hmp); 55551c35492SMatthew Dillon hammer_flusher_sync(hmp); 55651c35492SMatthew Dillon hmp->ronly = 1; 55751c35492SMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 55851c35492SMatthew Dillon hammer_adjust_volume_mode, NULL); 55951c35492SMatthew Dillon } 560b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 56151c35492SMatthew Dillon return(error); 562195c19a1SMatthew Dillon } 563195c19a1SMatthew Dillon 564427e5fc6SMatthew Dillon RB_INIT(&hmp->rb_vols_root); 565427e5fc6SMatthew Dillon RB_INIT(&hmp->rb_inos_root); 56673896937SMatthew Dillon RB_INIT(&hmp->rb_redo_root); 56740043e7fSMatthew Dillon RB_INIT(&hmp->rb_nods_root); 568e8599db1SMatthew Dillon RB_INIT(&hmp->rb_undo_root); 5690832c9bbSMatthew Dillon RB_INIT(&hmp->rb_resv_root); 5700832c9bbSMatthew Dillon RB_INIT(&hmp->rb_bufs_root); 5715fa5c92fSMatthew Dillon RB_INIT(&hmp->rb_pfsm_root); 5720832c9bbSMatthew Dillon 573195c19a1SMatthew Dillon hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 574427e5fc6SMatthew Dillon 5751afb73cfSMatthew Dillon RB_INIT(&hmp->volu_root); 5761afb73cfSMatthew Dillon RB_INIT(&hmp->undo_root); 5771afb73cfSMatthew Dillon RB_INIT(&hmp->data_root); 5781afb73cfSMatthew Dillon RB_INIT(&hmp->meta_root); 5791afb73cfSMatthew Dillon RB_INIT(&hmp->lose_root); 580eddadaeeSMatthew Dillon TAILQ_INIT(&hmp->iorun_list); 58110a5d1baSMatthew Dillon 582a3c18566SMatthew Dillon lwkt_token_init(&hmp->fs_token, "hammerfs"); 583a3c18566SMatthew Dillon lwkt_token_init(&hmp->io_token, "hammerio"); 584b0aab9b9SMatthew Dillon 585b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 586b0aab9b9SMatthew Dillon 587427e5fc6SMatthew Dillon /* 588427e5fc6SMatthew Dillon * Load volumes 589427e5fc6SMatthew Dillon */ 590427e5fc6SMatthew Dillon path = objcache_get(namei_oc, M_WAITOK); 5911b0ab2c3SMatthew Dillon hmp->nvolumes = -1; 592427e5fc6SMatthew Dillon for (i = 0; i < info.nvolumes; ++i) { 5937c19b529SMichael Neumann if (mntpt == NULL) { 5947c19b529SMichael Neumann /* 5957c19b529SMichael Neumann * Root mount. 5967c19b529SMichael Neumann */ 597104cb849SMichael Neumann KKASSERT(next_volume_ptr != NULL); 598a407819fSMichael Neumann strcpy(path, ""); 599a407819fSMichael Neumann if (*next_volume_ptr != '/') { 600a407819fSMichael Neumann /* relative path */ 601104cb849SMichael Neumann strcpy(path, "/dev/"); 602a407819fSMichael Neumann } 603104cb849SMichael Neumann int k; 604104cb849SMichael Neumann for (k = strlen(path); k < MAXPATHLEN-1; ++k) { 605104cb849SMichael Neumann if (*next_volume_ptr == '\0') { 606104cb849SMichael Neumann break; 607104cb849SMichael Neumann } else if (*next_volume_ptr == ':') { 608104cb849SMichael Neumann ++next_volume_ptr; 609104cb849SMichael Neumann break; 610104cb849SMichael Neumann } else { 611104cb849SMichael Neumann path[k] = *next_volume_ptr; 612104cb849SMichael Neumann ++next_volume_ptr; 613104cb849SMichael Neumann } 614104cb849SMichael Neumann } 615104cb849SMichael Neumann path[k] = '\0'; 616104cb849SMichael Neumann 6177c19b529SMichael Neumann error = 0; 618104cb849SMichael Neumann cdev_t dev = kgetdiskbyname(path); 619104cb849SMichael Neumann error = bdevvp(dev, &devvp); 620104cb849SMichael Neumann if (error) { 621542b88c2STomohiro Kusumi kprintf("hammer_mount: can't find devvp\n"); 622104cb849SMichael Neumann } 6237c19b529SMichael Neumann } else { 6247c19b529SMichael Neumann error = copyin(&info.volumes[i], &upath, 6257c19b529SMichael Neumann sizeof(char *)); 626427e5fc6SMatthew Dillon if (error == 0) 6277c19b529SMichael Neumann error = copyinstr(upath, path, 6287c19b529SMichael Neumann MAXPATHLEN, NULL); 6297c19b529SMichael Neumann } 630427e5fc6SMatthew Dillon if (error == 0) 631deabdbfbSTomohiro Kusumi error = hammer_install_volume(hmp, path, devvp, NULL); 632427e5fc6SMatthew Dillon if (error) 633427e5fc6SMatthew Dillon break; 634427e5fc6SMatthew Dillon } 635427e5fc6SMatthew Dillon objcache_put(namei_oc, path); 636427e5fc6SMatthew Dillon 637427e5fc6SMatthew Dillon /* 638427e5fc6SMatthew Dillon * Make sure we found a root volume 639427e5fc6SMatthew Dillon */ 640721f3110STomohiro Kusumi if (hmp->rootvol == NULL) { 641427e5fc6SMatthew Dillon kprintf("hammer_mount: No root volume found!\n"); 642427e5fc6SMatthew Dillon error = EINVAL; 643721f3110STomohiro Kusumi goto failed; 644427e5fc6SMatthew Dillon } 6451b0ab2c3SMatthew Dillon 6461b0ab2c3SMatthew Dillon /* 6471b0ab2c3SMatthew Dillon * Check that all required volumes are available 6481b0ab2c3SMatthew Dillon */ 6491b0ab2c3SMatthew Dillon if (error == 0 && hammer_mountcheck_volumes(hmp)) { 6501b0ab2c3SMatthew Dillon kprintf("hammer_mount: Missing volumes, cannot mount!\n"); 6511b0ab2c3SMatthew Dillon error = EINVAL; 652721f3110STomohiro Kusumi goto failed; 6531b0ab2c3SMatthew Dillon } 6541b0ab2c3SMatthew Dillon 655721f3110STomohiro Kusumi /* 656721f3110STomohiro Kusumi * Other errors 657721f3110STomohiro Kusumi */ 658427e5fc6SMatthew Dillon if (error) { 659721f3110STomohiro Kusumi kprintf("hammer_mount: Failed to load volumes!\n"); 660721f3110STomohiro Kusumi goto failed; 661427e5fc6SMatthew Dillon } 662427e5fc6SMatthew Dillon 663c302e844STomohiro Kusumi nvolumes = hammer_get_installed_volumes(hmp); 664c302e844STomohiro Kusumi if (hmp->nvolumes != nvolumes) { 665c302e844STomohiro Kusumi kprintf("hammer_mount: volume header says %d volumes, " 666c302e844STomohiro Kusumi "but %d installed\n", 667c302e844STomohiro Kusumi hmp->nvolumes, nvolumes); 668c302e844STomohiro Kusumi error = EINVAL; 669c302e844STomohiro Kusumi goto failed; 670c302e844STomohiro Kusumi } 671c302e844STomohiro Kusumi 672427e5fc6SMatthew Dillon /* 67327ea2398SMatthew Dillon * No errors, setup enough of the mount point so we can lookup the 67427ea2398SMatthew Dillon * root vnode. 675427e5fc6SMatthew Dillon */ 676427e5fc6SMatthew Dillon mp->mnt_iosize_max = MAXPHYS; 677427e5fc6SMatthew Dillon mp->mnt_kern_flag |= MNTK_FSMID; 678cf6a53caSMatthew Dillon mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ 679c0ade690SMatthew Dillon 680c0ade690SMatthew Dillon /* 681aac0aabdSMatthew Dillon * MPSAFE code. Note that VOPs and VFSops which are not MPSAFE 682aac0aabdSMatthew Dillon * will acquire a per-mount token prior to entry and release it 6836a85c61bSTomohiro Kusumi * on return. 684aac0aabdSMatthew Dillon */ 685b0aab9b9SMatthew Dillon mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; 686b0aab9b9SMatthew Dillon /*MNTK_RD_MPSAFE | MNTK_GA_MPSAFE | MNTK_IN_MPSAFE;*/ 687aac0aabdSMatthew Dillon 688aac0aabdSMatthew Dillon /* 689c0ade690SMatthew Dillon * note: f_iosize is used by vnode_pager_haspage() when constructing 690c0ade690SMatthew Dillon * its VOP_BMAP call. 691c0ade690SMatthew Dillon */ 692c0ade690SMatthew Dillon mp->mnt_stat.f_iosize = HAMMER_BUFSIZE; 693fbc6e32aSMatthew Dillon mp->mnt_stat.f_bsize = HAMMER_BUFSIZE; 6946f97fce3SMatthew Dillon 6956f97fce3SMatthew Dillon mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE; 6966f97fce3SMatthew Dillon mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE; 6976f97fce3SMatthew Dillon 698427e5fc6SMatthew Dillon mp->mnt_maxsymlinklen = 255; 699427e5fc6SMatthew Dillon mp->mnt_flag |= MNT_LOCAL; 700427e5fc6SMatthew Dillon 701427e5fc6SMatthew Dillon vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops); 7027a04d74fSMatthew Dillon vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops); 7037a04d74fSMatthew Dillon vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops); 70427ea2398SMatthew Dillon 70527ea2398SMatthew Dillon /* 706a89aec1bSMatthew Dillon * The root volume's ondisk pointer is only valid if we hold a 707a89aec1bSMatthew Dillon * reference to it. 708a89aec1bSMatthew Dillon */ 709a89aec1bSMatthew Dillon rootvol = hammer_get_root_volume(hmp, &error); 710a89aec1bSMatthew Dillon if (error) 711f90dde4cSMatthew Dillon goto failed; 712f90dde4cSMatthew Dillon 713f90dde4cSMatthew Dillon /* 7149f5097dcSMatthew Dillon * Perform any necessary UNDO operations. The recovery code does 7150729c8c8SMatthew Dillon * call hammer_undo_lookup() so we have to pre-cache the blockmap, 7160729c8c8SMatthew Dillon * and then re-copy it again after recovery is complete. 717c9b9e29dSMatthew Dillon * 71851c35492SMatthew Dillon * If this is a read-only mount the UNDO information is retained 71951c35492SMatthew Dillon * in memory in the form of dirty buffer cache buffers, and not 72051c35492SMatthew Dillon * written back to the media. 721f90dde4cSMatthew Dillon */ 7220729c8c8SMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 7230729c8c8SMatthew Dillon sizeof(hmp->blockmap)); 724c9b9e29dSMatthew Dillon 7257a61b85dSMatthew Dillon /* 72644a83111SMatthew Dillon * Check filesystem version 72744a83111SMatthew Dillon */ 72844a83111SMatthew Dillon hmp->version = rootvol->ondisk->vol_version; 72944a83111SMatthew Dillon if (hmp->version < HAMMER_VOL_VERSION_MIN || 73044a83111SMatthew Dillon hmp->version > HAMMER_VOL_VERSION_MAX) { 73144a83111SMatthew Dillon kprintf("HAMMER: mount unsupported fs version %d\n", 73244a83111SMatthew Dillon hmp->version); 73344a83111SMatthew Dillon error = ERANGE; 73444a83111SMatthew Dillon goto done; 73544a83111SMatthew Dillon } 73644a83111SMatthew Dillon 73744a83111SMatthew Dillon /* 7387a61b85dSMatthew Dillon * The undo_rec_limit limits the size of flush groups to avoid 7397a61b85dSMatthew Dillon * blowing out the UNDO FIFO. This calculation is typically in 7407a61b85dSMatthew Dillon * the tens of thousands and is designed primarily when small 7417a61b85dSMatthew Dillon * HAMMER filesystems are created. 7427a61b85dSMatthew Dillon */ 7437a61b85dSMatthew Dillon hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100; 7447a61b85dSMatthew Dillon if (hammer_debug_general & 0x0001) 7457a61b85dSMatthew Dillon kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit); 7467a61b85dSMatthew Dillon 74702428fb6SMatthew Dillon /* 74802428fb6SMatthew Dillon * NOTE: Recover stage1 not only handles meta-data recovery, it 74902428fb6SMatthew Dillon * also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems. 75002428fb6SMatthew Dillon */ 75102428fb6SMatthew Dillon error = hammer_recover_stage1(hmp, rootvol); 752f90dde4cSMatthew Dillon if (error) { 753f90dde4cSMatthew Dillon kprintf("Failed to recover HAMMER filesystem on mount\n"); 754a89aec1bSMatthew Dillon goto done; 755f90dde4cSMatthew Dillon } 756f90dde4cSMatthew Dillon 757f90dde4cSMatthew Dillon /* 758adf01747SMatthew Dillon * Finish setup now that we have a good root volume. 759adf01747SMatthew Dillon * 760adf01747SMatthew Dillon * The top 16 bits of fsid.val[1] is a pfs id. 761f90dde4cSMatthew Dillon */ 762a89aec1bSMatthew Dillon ksnprintf(mp->mnt_stat.f_mntfromname, 763a89aec1bSMatthew Dillon sizeof(mp->mnt_stat.f_mntfromname), "%s", 764a89aec1bSMatthew Dillon rootvol->ondisk->vol_name); 765513ca7d7SMatthew Dillon mp->mnt_stat.f_fsid.val[0] = 766513ca7d7SMatthew Dillon crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8); 767513ca7d7SMatthew Dillon mp->mnt_stat.f_fsid.val[1] = 768513ca7d7SMatthew Dillon crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8); 769adf01747SMatthew Dillon mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF; 770b84de5afSMatthew Dillon 7716f97fce3SMatthew Dillon mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid; 7726f97fce3SMatthew Dillon mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid, 7736f97fce3SMatthew Dillon sizeof(mp->mnt_vstat.f_fsid_uuid)); 7746f97fce3SMatthew Dillon 7750729c8c8SMatthew Dillon /* 7760729c8c8SMatthew Dillon * Certain often-modified fields in the root volume are cached in 7770729c8c8SMatthew Dillon * the hammer_mount structure so we do not have to generate lots 7780729c8c8SMatthew Dillon * of little UNDO structures for them. 779c9b9e29dSMatthew Dillon * 7809f5097dcSMatthew Dillon * Recopy after recovery. This also has the side effect of 7819f5097dcSMatthew Dillon * setting our cached undo FIFO's first_offset, which serves to 7829f5097dcSMatthew Dillon * placemark the FIFO start for the NEXT flush cycle while the 7839f5097dcSMatthew Dillon * on-disk first_offset represents the LAST flush cycle. 7840729c8c8SMatthew Dillon */ 785b84de5afSMatthew Dillon hmp->next_tid = rootvol->ondisk->vol0_next_tid; 7864889cbd4SMatthew Dillon hmp->flush_tid1 = hmp->next_tid; 7874889cbd4SMatthew Dillon hmp->flush_tid2 = hmp->next_tid; 7880729c8c8SMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 7890729c8c8SMatthew Dillon sizeof(hmp->blockmap)); 790e63644f0SMatthew Dillon hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks; 7910729c8c8SMatthew Dillon 792059819e3SMatthew Dillon hammer_flusher_create(hmp); 793059819e3SMatthew Dillon 794a89aec1bSMatthew Dillon /* 79527ea2398SMatthew Dillon * Locate the root directory using the root cluster's B-Tree as a 79627ea2398SMatthew Dillon * starting point. The root directory uses an obj_id of 1. 79727ea2398SMatthew Dillon * 79827ea2398SMatthew Dillon * FUTURE: Leave the root directory cached referenced but unlocked 79927ea2398SMatthew Dillon * in hmp->rootvp (need to flush it on unmount). 80027ea2398SMatthew Dillon */ 801783fb170STomohiro Kusumi error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, &rootvp); 802a89aec1bSMatthew Dillon if (error) 803a89aec1bSMatthew Dillon goto done; 80427ea2398SMatthew Dillon vput(rootvp); 80527ea2398SMatthew Dillon /*vn_unlock(hmp->rootvp);*/ 80688c39f64SThomas Nikolajsen if (hmp->ronly == 0) 80702428fb6SMatthew Dillon error = hammer_recover_stage2(hmp, rootvol); 80827ea2398SMatthew Dillon 8099fa352a2SMatthew Dillon /* 8109fa352a2SMatthew Dillon * If the stage2 recovery fails be sure to clean out all cached 8119fa352a2SMatthew Dillon * vnodes before throwing away the mount structure or bad things 8129fa352a2SMatthew Dillon * will happen. 8139fa352a2SMatthew Dillon */ 8149fa352a2SMatthew Dillon if (error) 8159fa352a2SMatthew Dillon vflush(mp, 0, 0); 8169fa352a2SMatthew Dillon 817a89aec1bSMatthew Dillon done: 81898ad7adeSFrancois Tigeot if ((mp->mnt_flag & MNT_UPDATE) == 0) { 81998ad7adeSFrancois Tigeot /* New mount */ 82098ad7adeSFrancois Tigeot 82198ad7adeSFrancois Tigeot /* Populate info for mount point (NULL pad)*/ 82298ad7adeSFrancois Tigeot bzero(mp->mnt_stat.f_mntonname, MNAMELEN); 82398ad7adeSFrancois Tigeot size_t size; 82498ad7adeSFrancois Tigeot if (mntpt) { 82598ad7adeSFrancois Tigeot copyinstr(mntpt, mp->mnt_stat.f_mntonname, 82698ad7adeSFrancois Tigeot MNAMELEN -1, &size); 82798ad7adeSFrancois Tigeot } else { /* Root mount */ 82898ad7adeSFrancois Tigeot mp->mnt_stat.f_mntonname[0] = '/'; 82998ad7adeSFrancois Tigeot } 83098ad7adeSFrancois Tigeot } 83198ad7adeSFrancois Tigeot (void)VFS_STATFS(mp, &mp->mnt_stat, cred); 832f90dde4cSMatthew Dillon hammer_rel_volume(rootvol, 0); 833f90dde4cSMatthew Dillon failed: 83427ea2398SMatthew Dillon /* 83527ea2398SMatthew Dillon * Cleanup and return. 83627ea2398SMatthew Dillon */ 837b0aab9b9SMatthew Dillon if (error) { 838b0aab9b9SMatthew Dillon /* called with fs_token held */ 83927ea2398SMatthew Dillon hammer_free_hmp(mp); 840b0aab9b9SMatthew Dillon } else { 841b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 842b0aab9b9SMatthew Dillon } 843427e5fc6SMatthew Dillon return (error); 844427e5fc6SMatthew Dillon } 845427e5fc6SMatthew Dillon 846427e5fc6SMatthew Dillon static int 847427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags) 848427e5fc6SMatthew Dillon { 849b0aab9b9SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 850427e5fc6SMatthew Dillon int flags; 85166325755SMatthew Dillon int error; 85227ea2398SMatthew Dillon 85327ea2398SMatthew Dillon /* 854427e5fc6SMatthew Dillon * Clean out the vnodes 855427e5fc6SMatthew Dillon */ 856b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 85766325755SMatthew Dillon flags = 0; 85866325755SMatthew Dillon if (mntflags & MNT_FORCE) 85966325755SMatthew Dillon flags |= FORCECLOSE; 860b0aab9b9SMatthew Dillon error = vflush(mp, 0, flags); 861427e5fc6SMatthew Dillon 862427e5fc6SMatthew Dillon /* 863427e5fc6SMatthew Dillon * Clean up the internal mount structure and related entities. This 864427e5fc6SMatthew Dillon * may issue I/O. 865427e5fc6SMatthew Dillon */ 866b0aab9b9SMatthew Dillon if (error == 0) { 867b0aab9b9SMatthew Dillon /* called with fs_token held */ 868427e5fc6SMatthew Dillon hammer_free_hmp(mp); 869b0aab9b9SMatthew Dillon } else { 870b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 871b0aab9b9SMatthew Dillon } 872b0aab9b9SMatthew Dillon return(error); 873427e5fc6SMatthew Dillon } 874427e5fc6SMatthew Dillon 875427e5fc6SMatthew Dillon /* 876427e5fc6SMatthew Dillon * Clean up the internal mount structure and disassociate it from the mount. 877427e5fc6SMatthew Dillon * This may issue I/O. 878b0aab9b9SMatthew Dillon * 879b0aab9b9SMatthew Dillon * Called with fs_token held. 880427e5fc6SMatthew Dillon */ 881427e5fc6SMatthew Dillon static void 882427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp) 883427e5fc6SMatthew Dillon { 884b0aab9b9SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 885cdb6e4e6SMatthew Dillon hammer_flush_group_t flg; 886427e5fc6SMatthew Dillon 887427e5fc6SMatthew Dillon /* 888cdb6e4e6SMatthew Dillon * Flush anything dirty. This won't even run if the 889cdb6e4e6SMatthew Dillon * filesystem errored-out. 890427e5fc6SMatthew Dillon */ 891*8bae937eSTomohiro Kusumi hammer_flush_dirty(hmp, 30); 892cdb6e4e6SMatthew Dillon 893cdb6e4e6SMatthew Dillon /* 894cdb6e4e6SMatthew Dillon * If the mount had a critical error we have to destroy any 895cdb6e4e6SMatthew Dillon * remaining inodes before we can finish cleaning up the flusher. 896cdb6e4e6SMatthew Dillon */ 897cdb6e4e6SMatthew Dillon if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) { 898cdb6e4e6SMatthew Dillon RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 899cdb6e4e6SMatthew Dillon hammer_destroy_inode_callback, NULL); 900cdb6e4e6SMatthew Dillon } 901cdb6e4e6SMatthew Dillon 902cdb6e4e6SMatthew Dillon /* 903cdb6e4e6SMatthew Dillon * There shouldn't be any inodes left now and any left over 904cdb6e4e6SMatthew Dillon * flush groups should now be empty. 905cdb6e4e6SMatthew Dillon */ 906cdb6e4e6SMatthew Dillon KKASSERT(RB_EMPTY(&hmp->rb_inos_root)); 907cdb6e4e6SMatthew Dillon while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) { 908cdb6e4e6SMatthew Dillon TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry); 909ff003b11SMatthew Dillon KKASSERT(RB_EMPTY(&flg->flush_tree)); 910cdb6e4e6SMatthew Dillon if (flg->refs) { 911cdb6e4e6SMatthew Dillon kprintf("HAMMER: Warning, flush_group %p was " 912cdb6e4e6SMatthew Dillon "not empty on umount!\n", flg); 913cdb6e4e6SMatthew Dillon } 914bac808feSMatthew Dillon kfree(flg, hmp->m_misc); 915cdb6e4e6SMatthew Dillon } 916cdb6e4e6SMatthew Dillon 917cdb6e4e6SMatthew Dillon /* 918cdb6e4e6SMatthew Dillon * We can finally destroy the flusher 919cdb6e4e6SMatthew Dillon */ 920059819e3SMatthew Dillon hammer_flusher_destroy(hmp); 921427e5fc6SMatthew Dillon 922b84de5afSMatthew Dillon /* 92300f16fadSMatthew Dillon * We may have held recovered buffers due to a read-only mount. 92400f16fadSMatthew Dillon * These must be discarded. 92500f16fadSMatthew Dillon */ 92600f16fadSMatthew Dillon if (hmp->ronly) 92700f16fadSMatthew Dillon hammer_recover_flush_buffers(hmp, NULL, -1); 92800f16fadSMatthew Dillon 92900f16fadSMatthew Dillon /* 9300832c9bbSMatthew Dillon * Unload buffers and then volumes 931b84de5afSMatthew Dillon */ 9320832c9bbSMatthew Dillon RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL, 9330832c9bbSMatthew Dillon hammer_unload_buffer, NULL); 934427e5fc6SMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 935427e5fc6SMatthew Dillon hammer_unload_volume, NULL); 936427e5fc6SMatthew Dillon 937427e5fc6SMatthew Dillon mp->mnt_data = NULL; 93866325755SMatthew Dillon mp->mnt_flag &= ~MNT_LOCAL; 939427e5fc6SMatthew Dillon hmp->mp = NULL; 9400729c8c8SMatthew Dillon hammer_destroy_objid_cache(hmp); 941507df98aSIlya Dryomov hammer_destroy_dedup_cache(hmp); 942507df98aSIlya Dryomov if (hmp->dedup_free_cache != NULL) { 943507df98aSIlya Dryomov kfree(hmp->dedup_free_cache, hmp->m_misc); 944507df98aSIlya Dryomov hmp->dedup_free_cache = NULL; 945507df98aSIlya Dryomov } 946bac808feSMatthew Dillon kmalloc_destroy(&hmp->m_misc); 947bac808feSMatthew Dillon kmalloc_destroy(&hmp->m_inodes); 948b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 949427e5fc6SMatthew Dillon kfree(hmp, M_HAMMER); 950427e5fc6SMatthew Dillon } 951427e5fc6SMatthew Dillon 952427e5fc6SMatthew Dillon /* 953cdb6e4e6SMatthew Dillon * Report critical errors. ip may be NULL. 954cdb6e4e6SMatthew Dillon */ 955cdb6e4e6SMatthew Dillon void 956cdb6e4e6SMatthew Dillon hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip, 957cdb6e4e6SMatthew Dillon int error, const char *msg) 958cdb6e4e6SMatthew Dillon { 959cdb6e4e6SMatthew Dillon hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR; 960c9ce54d6SMatthew Dillon 961cdb6e4e6SMatthew Dillon krateprintf(&hmp->krate, 962c9ce54d6SMatthew Dillon "HAMMER(%s): Critical error inode=%jd error=%d %s\n", 963cdb6e4e6SMatthew Dillon hmp->mp->mnt_stat.f_mntfromname, 964c9ce54d6SMatthew Dillon (intmax_t)(ip ? ip->obj_id : -1), 965c9ce54d6SMatthew Dillon error, msg); 966c9ce54d6SMatthew Dillon 967cdb6e4e6SMatthew Dillon if (hmp->ronly == 0) { 968cdb6e4e6SMatthew Dillon hmp->ronly = 2; /* special errored read-only mode */ 969cdb6e4e6SMatthew Dillon hmp->mp->mnt_flag |= MNT_RDONLY; 970dfec479fSMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 971dfec479fSMatthew Dillon hammer_adjust_volume_mode, NULL); 972cdb6e4e6SMatthew Dillon kprintf("HAMMER(%s): Forcing read-only mode\n", 973cdb6e4e6SMatthew Dillon hmp->mp->mnt_stat.f_mntfromname); 974cdb6e4e6SMatthew Dillon } 975cdb6e4e6SMatthew Dillon hmp->error = error; 976fc73edd8SMatthew Dillon if (hammer_debug_critical) 977c9ce54d6SMatthew Dillon Debugger("Entering debugger"); 978cdb6e4e6SMatthew Dillon } 979cdb6e4e6SMatthew Dillon 980cdb6e4e6SMatthew Dillon 981cdb6e4e6SMatthew Dillon /* 982513ca7d7SMatthew Dillon * Obtain a vnode for the specified inode number. An exclusively locked 983513ca7d7SMatthew Dillon * vnode is returned. 984513ca7d7SMatthew Dillon */ 985513ca7d7SMatthew Dillon int 986b9b0a6d0SMatthew Dillon hammer_vfs_vget(struct mount *mp, struct vnode *dvp, 987b9b0a6d0SMatthew Dillon ino_t ino, struct vnode **vpp) 988513ca7d7SMatthew Dillon { 98936f82b23SMatthew Dillon struct hammer_transaction trans; 990513ca7d7SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 991513ca7d7SMatthew Dillon struct hammer_inode *ip; 992513ca7d7SMatthew Dillon int error; 993b9b0a6d0SMatthew Dillon u_int32_t localization; 994513ca7d7SMatthew Dillon 995b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 99636f82b23SMatthew Dillon hammer_simple_transaction(&trans, hmp); 99736f82b23SMatthew Dillon 998513ca7d7SMatthew Dillon /* 999b9b0a6d0SMatthew Dillon * If a directory vnode is supplied (mainly NFS) then we can acquire 1000b9b0a6d0SMatthew Dillon * the PFS domain from it. Otherwise we would only be able to vget 1001b9b0a6d0SMatthew Dillon * inodes in the root PFS. 1002b9b0a6d0SMatthew Dillon */ 1003b9b0a6d0SMatthew Dillon if (dvp) { 1004b9b0a6d0SMatthew Dillon localization = HAMMER_DEF_LOCALIZATION + 1005b9b0a6d0SMatthew Dillon VTOI(dvp)->obj_localization; 1006b9b0a6d0SMatthew Dillon } else { 1007b9b0a6d0SMatthew Dillon localization = HAMMER_DEF_LOCALIZATION; 1008b9b0a6d0SMatthew Dillon } 1009b9b0a6d0SMatthew Dillon 1010b9b0a6d0SMatthew Dillon /* 101136f82b23SMatthew Dillon * Lookup the requested HAMMER inode. The structure must be 101236f82b23SMatthew Dillon * left unlocked while we manipulate the related vnode to avoid 101336f82b23SMatthew Dillon * a deadlock. 1014513ca7d7SMatthew Dillon */ 1015ddfdf542SMatthew Dillon ip = hammer_get_inode(&trans, NULL, ino, 1016b9b0a6d0SMatthew Dillon hmp->asof, localization, 1017ddfdf542SMatthew Dillon 0, &error); 1018513ca7d7SMatthew Dillon if (ip == NULL) { 1019513ca7d7SMatthew Dillon *vpp = NULL; 1020b0aab9b9SMatthew Dillon } else { 1021e8599db1SMatthew Dillon error = hammer_get_vnode(ip, vpp); 1022513ca7d7SMatthew Dillon hammer_rel_inode(ip, 0); 1023b0aab9b9SMatthew Dillon } 1024b84de5afSMatthew Dillon hammer_done_transaction(&trans); 1025b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1026513ca7d7SMatthew Dillon return (error); 1027513ca7d7SMatthew Dillon } 1028513ca7d7SMatthew Dillon 1029513ca7d7SMatthew Dillon /* 1030427e5fc6SMatthew Dillon * Return the root vnode for the filesystem. 1031427e5fc6SMatthew Dillon * 1032427e5fc6SMatthew Dillon * HAMMER stores the root vnode in the hammer_mount structure so 1033427e5fc6SMatthew Dillon * getting it is easy. 1034427e5fc6SMatthew Dillon */ 1035427e5fc6SMatthew Dillon static int 1036427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp) 1037427e5fc6SMatthew Dillon { 103827ea2398SMatthew Dillon int error; 1039427e5fc6SMatthew Dillon 1040783fb170STomohiro Kusumi error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, vpp); 104127ea2398SMatthew Dillon return (error); 1042427e5fc6SMatthew Dillon } 1043427e5fc6SMatthew Dillon 1044427e5fc6SMatthew Dillon static int 1045427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 1046427e5fc6SMatthew Dillon { 1047fbc6e32aSMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 1048fbc6e32aSMatthew Dillon hammer_volume_t volume; 1049fbc6e32aSMatthew Dillon hammer_volume_ondisk_t ondisk; 1050fbc6e32aSMatthew Dillon int error; 105147197d71SMatthew Dillon int64_t bfree; 105231a56ce2SMatthew Dillon int64_t breserved; 1053fbc6e32aSMatthew Dillon 1054b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1055fbc6e32aSMatthew Dillon volume = hammer_get_root_volume(hmp, &error); 1056b0aab9b9SMatthew Dillon if (error) { 1057b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1058fbc6e32aSMatthew Dillon return(error); 1059b0aab9b9SMatthew Dillon } 1060fbc6e32aSMatthew Dillon ondisk = volume->ondisk; 1061fbc6e32aSMatthew Dillon 106247197d71SMatthew Dillon /* 106347197d71SMatthew Dillon * Basic stats 106447197d71SMatthew Dillon */ 106531a56ce2SMatthew Dillon _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved); 1066fbc6e32aSMatthew Dillon mp->mnt_stat.f_files = ondisk->vol0_stat_inodes; 1067e04ee2deSTomohiro Kusumi bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE; 106840043e7fSMatthew Dillon hammer_rel_volume(volume, 0); 106947197d71SMatthew Dillon 107031a56ce2SMatthew Dillon mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE; 107147197d71SMatthew Dillon mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 1072fbc6e32aSMatthew Dillon if (mp->mnt_stat.f_files < 0) 1073fbc6e32aSMatthew Dillon mp->mnt_stat.f_files = 0; 1074fbc6e32aSMatthew Dillon 107527ea2398SMatthew Dillon *sbp = mp->mnt_stat; 1076b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 107727ea2398SMatthew Dillon return(0); 1078427e5fc6SMatthew Dillon } 1079427e5fc6SMatthew Dillon 10806f97fce3SMatthew Dillon static int 10816f97fce3SMatthew Dillon hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 10826f97fce3SMatthew Dillon { 10836f97fce3SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 10846f97fce3SMatthew Dillon hammer_volume_t volume; 10856f97fce3SMatthew Dillon hammer_volume_ondisk_t ondisk; 10866f97fce3SMatthew Dillon int error; 10876f97fce3SMatthew Dillon int64_t bfree; 10880f65be10SMatthew Dillon int64_t breserved; 10896f97fce3SMatthew Dillon 1090b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 10916f97fce3SMatthew Dillon volume = hammer_get_root_volume(hmp, &error); 1092b0aab9b9SMatthew Dillon if (error) { 1093b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 10946f97fce3SMatthew Dillon return(error); 1095b0aab9b9SMatthew Dillon } 10966f97fce3SMatthew Dillon ondisk = volume->ondisk; 10976f97fce3SMatthew Dillon 10986f97fce3SMatthew Dillon /* 10996f97fce3SMatthew Dillon * Basic stats 11006f97fce3SMatthew Dillon */ 11010f65be10SMatthew Dillon _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved); 11026f97fce3SMatthew Dillon mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes; 1103e04ee2deSTomohiro Kusumi bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE; 11046f97fce3SMatthew Dillon hammer_rel_volume(volume, 0); 11056f97fce3SMatthew Dillon 11060f65be10SMatthew Dillon mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE; 1107c0763659SMatthew Dillon mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree; 11086f97fce3SMatthew Dillon if (mp->mnt_vstat.f_files < 0) 11096f97fce3SMatthew Dillon mp->mnt_vstat.f_files = 0; 11106f97fce3SMatthew Dillon *sbp = mp->mnt_vstat; 1111b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 11126f97fce3SMatthew Dillon return(0); 11136f97fce3SMatthew Dillon } 11146f97fce3SMatthew Dillon 11150729c8c8SMatthew Dillon /* 11160729c8c8SMatthew Dillon * Sync the filesystem. Currently we have to run it twice, the second 11170729c8c8SMatthew Dillon * one will advance the undo start index to the end index, so if a crash 11180729c8c8SMatthew Dillon * occurs no undos will be run on mount. 111977062c8aSMatthew Dillon * 112077062c8aSMatthew Dillon * We do not sync the filesystem if we are called from a panic. If we did 112177062c8aSMatthew Dillon * we might end up blowing up a sync that was already in progress. 11220729c8c8SMatthew Dillon */ 1123427e5fc6SMatthew Dillon static int 1124427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor) 1125427e5fc6SMatthew Dillon { 1126fbc6e32aSMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 11270729c8c8SMatthew Dillon int error; 11280729c8c8SMatthew Dillon 1129b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 113077062c8aSMatthew Dillon if (panicstr == NULL) { 11310729c8c8SMatthew Dillon error = hammer_sync_hmp(hmp, waitfor); 113277062c8aSMatthew Dillon } else { 113377062c8aSMatthew Dillon error = EIO; 113477062c8aSMatthew Dillon } 1135b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 11360729c8c8SMatthew Dillon return (error); 1137427e5fc6SMatthew Dillon } 1138427e5fc6SMatthew Dillon 1139513ca7d7SMatthew Dillon /* 1140513ca7d7SMatthew Dillon * Convert a vnode to a file handle. 1141b0aab9b9SMatthew Dillon * 1142b0aab9b9SMatthew Dillon * Accesses read-only fields on already-referenced structures so 1143b0aab9b9SMatthew Dillon * no token is needed. 1144513ca7d7SMatthew Dillon */ 1145513ca7d7SMatthew Dillon static int 1146513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp) 1147513ca7d7SMatthew Dillon { 1148513ca7d7SMatthew Dillon hammer_inode_t ip; 1149513ca7d7SMatthew Dillon 1150513ca7d7SMatthew Dillon KKASSERT(MAXFIDSZ >= 16); 1151513ca7d7SMatthew Dillon ip = VTOI(vp); 1152513ca7d7SMatthew Dillon fhp->fid_len = offsetof(struct fid, fid_data[16]); 1153adf01747SMatthew Dillon fhp->fid_ext = ip->obj_localization >> 16; 1154513ca7d7SMatthew Dillon bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id)); 1155513ca7d7SMatthew Dillon bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof)); 1156513ca7d7SMatthew Dillon return(0); 1157513ca7d7SMatthew Dillon } 1158513ca7d7SMatthew Dillon 1159513ca7d7SMatthew Dillon 1160513ca7d7SMatthew Dillon /* 1161513ca7d7SMatthew Dillon * Convert a file handle back to a vnode. 116267863d04SMatthew Dillon * 116367863d04SMatthew Dillon * Use rootvp to enforce PFS isolation when a PFS is exported via a 116467863d04SMatthew Dillon * null mount. 1165513ca7d7SMatthew Dillon */ 1166513ca7d7SMatthew Dillon static int 116767863d04SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 116867863d04SMatthew Dillon struct fid *fhp, struct vnode **vpp) 1169513ca7d7SMatthew Dillon { 1170b0aab9b9SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 117136f82b23SMatthew Dillon struct hammer_transaction trans; 1172513ca7d7SMatthew Dillon struct hammer_inode *ip; 1173513ca7d7SMatthew Dillon struct hammer_inode_info info; 1174513ca7d7SMatthew Dillon int error; 1175adf01747SMatthew Dillon u_int32_t localization; 1176513ca7d7SMatthew Dillon 1177513ca7d7SMatthew Dillon bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id)); 1178513ca7d7SMatthew Dillon bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof)); 117967863d04SMatthew Dillon if (rootvp) 118067863d04SMatthew Dillon localization = VTOI(rootvp)->obj_localization; 118167863d04SMatthew Dillon else 1182adf01747SMatthew Dillon localization = (u_int32_t)fhp->fid_ext << 16; 1183513ca7d7SMatthew Dillon 1184b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1185b0aab9b9SMatthew Dillon hammer_simple_transaction(&trans, hmp); 118636f82b23SMatthew Dillon 1187513ca7d7SMatthew Dillon /* 1188513ca7d7SMatthew Dillon * Get/allocate the hammer_inode structure. The structure must be 1189513ca7d7SMatthew Dillon * unlocked while we manipulate the related vnode to avoid a 1190513ca7d7SMatthew Dillon * deadlock. 1191513ca7d7SMatthew Dillon */ 1192ddfdf542SMatthew Dillon ip = hammer_get_inode(&trans, NULL, info.obj_id, 1193adf01747SMatthew Dillon info.obj_asof, localization, 0, &error); 1194d978e7cfSMatthew Dillon if (ip) { 1195e8599db1SMatthew Dillon error = hammer_get_vnode(ip, vpp); 1196513ca7d7SMatthew Dillon hammer_rel_inode(ip, 0); 1197d978e7cfSMatthew Dillon } else { 1198d978e7cfSMatthew Dillon *vpp = NULL; 1199d978e7cfSMatthew Dillon } 1200b84de5afSMatthew Dillon hammer_done_transaction(&trans); 1201b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1202513ca7d7SMatthew Dillon return (error); 1203513ca7d7SMatthew Dillon } 1204513ca7d7SMatthew Dillon 1205513ca7d7SMatthew Dillon static int 1206513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 1207513ca7d7SMatthew Dillon int *exflagsp, struct ucred **credanonp) 1208513ca7d7SMatthew Dillon { 1209513ca7d7SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 1210513ca7d7SMatthew Dillon struct netcred *np; 1211513ca7d7SMatthew Dillon int error; 1212513ca7d7SMatthew Dillon 1213b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1214513ca7d7SMatthew Dillon np = vfs_export_lookup(mp, &hmp->export, nam); 1215513ca7d7SMatthew Dillon if (np) { 1216513ca7d7SMatthew Dillon *exflagsp = np->netc_exflags; 1217513ca7d7SMatthew Dillon *credanonp = &np->netc_anon; 1218513ca7d7SMatthew Dillon error = 0; 1219513ca7d7SMatthew Dillon } else { 1220513ca7d7SMatthew Dillon error = EACCES; 1221513ca7d7SMatthew Dillon } 1222b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1223513ca7d7SMatthew Dillon return (error); 1224513ca7d7SMatthew Dillon 1225513ca7d7SMatthew Dillon } 1226513ca7d7SMatthew Dillon 1227513ca7d7SMatthew Dillon int 1228513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export) 1229513ca7d7SMatthew Dillon { 1230513ca7d7SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 1231513ca7d7SMatthew Dillon int error; 1232513ca7d7SMatthew Dillon 1233b0aab9b9SMatthew Dillon lwkt_gettoken(&hmp->fs_token); 1234b0aab9b9SMatthew Dillon 1235513ca7d7SMatthew Dillon switch(op) { 1236513ca7d7SMatthew Dillon case MOUNTCTL_SET_EXPORT: 1237513ca7d7SMatthew Dillon error = vfs_export(mp, &hmp->export, export); 1238513ca7d7SMatthew Dillon break; 1239513ca7d7SMatthew Dillon default: 1240513ca7d7SMatthew Dillon error = EOPNOTSUPP; 1241513ca7d7SMatthew Dillon break; 1242513ca7d7SMatthew Dillon } 1243b0aab9b9SMatthew Dillon lwkt_reltoken(&hmp->fs_token); 1244b0aab9b9SMatthew Dillon 1245513ca7d7SMatthew Dillon return(error); 1246513ca7d7SMatthew Dillon } 1247513ca7d7SMatthew Dillon 1248