^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * bcache sysfs interfaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2012 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "bcache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "request.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "writeback.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "features.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sched/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) extern bool bcache_is_reboot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* Default is 0 ("writethrough") */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const char * const bch_cache_modes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) "writethrough",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) "writeback",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) "writearound",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) "none",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static const char * const bch_reada_cache_policies[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) "all",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) "meta-only",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Default is 0 ("auto") */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const char * const bch_stop_on_failure_modes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) "auto",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) "always",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static const char * const cache_replacement_policies[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) "lru",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) "fifo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) "random",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static const char * const error_actions[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) "unregister",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "panic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) write_attribute(attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) write_attribute(detach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) write_attribute(unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) write_attribute(stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) write_attribute(clear_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) write_attribute(trigger_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) write_attribute(prune_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) write_attribute(flash_vol_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) read_attribute(bucket_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) read_attribute(block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) read_attribute(nbuckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) read_attribute(tree_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) read_attribute(root_usage_percent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) read_attribute(priority_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) read_attribute(btree_cache_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) read_attribute(btree_cache_max_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) read_attribute(cache_available_percent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) read_attribute(written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) read_attribute(btree_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) read_attribute(metadata_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) read_attribute(active_journal_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) read_attribute(backing_dev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) read_attribute(backing_dev_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) sysfs_time_stats_attribute(btree_gc, sec, ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) sysfs_time_stats_attribute(btree_split, sec, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) sysfs_time_stats_attribute(btree_sort, ms, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) sysfs_time_stats_attribute(btree_read, ms, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) read_attribute(btree_nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) read_attribute(btree_used_percent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) read_attribute(average_key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) read_attribute(dirty_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) read_attribute(bset_tree_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) read_attribute(feature_compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) read_attribute(feature_ro_compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) read_attribute(feature_incompat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) read_attribute(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) read_attribute(cache_read_races);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) read_attribute(reclaim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) read_attribute(reclaimed_journal_buckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) read_attribute(flush_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) read_attribute(writeback_keys_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) read_attribute(writeback_keys_failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) read_attribute(io_errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) read_attribute(congested);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) read_attribute(cutoff_writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) read_attribute(cutoff_writeback_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) rw_attribute(congested_read_threshold_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) rw_attribute(congested_write_threshold_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) rw_attribute(sequential_cutoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) rw_attribute(data_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) rw_attribute(cache_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rw_attribute(readahead_cache_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) rw_attribute(stop_when_cache_set_failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) rw_attribute(writeback_metadata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) rw_attribute(writeback_running);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) rw_attribute(writeback_percent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) rw_attribute(writeback_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) rw_attribute(writeback_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) rw_attribute(writeback_rate_update_seconds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) rw_attribute(writeback_rate_i_term_inverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) rw_attribute(writeback_rate_p_term_inverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) rw_attribute(writeback_rate_minimum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) read_attribute(writeback_rate_debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) read_attribute(stripe_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) read_attribute(partial_stripes_expensive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) rw_attribute(synchronous);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) rw_attribute(journal_delay_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) rw_attribute(io_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) rw_attribute(discard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) rw_attribute(running);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) rw_attribute(label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) rw_attribute(readahead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) rw_attribute(errors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) rw_attribute(io_error_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) rw_attribute(io_error_halflife);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) rw_attribute(verify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) rw_attribute(bypass_torture_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) rw_attribute(key_merging_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) rw_attribute(gc_always_rewrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) rw_attribute(expensive_debug_checks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) rw_attribute(cache_replacement_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) rw_attribute(btree_shrinker_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) rw_attribute(copy_gc_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) rw_attribute(idle_max_writeback_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) rw_attribute(gc_after_writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) rw_attribute(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static ssize_t bch_snprint_string_list(char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) const char * const list[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) size_t selected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) char *out = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) for (i = 0; list[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) out += scnprintf(out, buf + size - out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) i == selected ? "[%s] " : "%s ", list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) out[-1] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) SHOW(__bch_cached_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct cached_dev *dc = container_of(kobj, struct cached_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) disk.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int wb = dc->writeback_running;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) #define var(stat) (dc->stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (attr == &sysfs_cache_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return bch_snprint_string_list(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) bch_cache_modes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) BDEV_CACHE_MODE(&dc->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (attr == &sysfs_readahead_cache_policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return bch_snprint_string_list(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) bch_reada_cache_policies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) dc->cache_readahead_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (attr == &sysfs_stop_when_cache_set_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return bch_snprint_string_list(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) bch_stop_on_failure_modes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) dc->stop_when_cache_set_failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) sysfs_printf(data_csum, "%i", dc->disk.data_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) var_printf(verify, "%i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) var_printf(bypass_torture_test, "%i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) var_printf(writeback_metadata, "%i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) var_printf(writeback_running, "%i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) var_print(writeback_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) var_print(writeback_percent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) sysfs_hprint(writeback_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) sysfs_printf(io_errors, "%i", atomic_read(&dc->io_errors));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) sysfs_printf(io_error_limit, "%i", dc->error_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) sysfs_printf(io_disable, "%i", dc->io_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) var_print(writeback_rate_update_seconds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) var_print(writeback_rate_i_term_inverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) var_print(writeback_rate_p_term_inverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) var_print(writeback_rate_minimum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (attr == &sysfs_writeback_rate_debug) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) char rate[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) char dirty[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) char target[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) char proportional[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) char integral[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) char change[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) s64 next_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * Except for dirty and target, other values should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * be 0 if writeback is not running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) bch_hprint(rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) wb ? atomic_long_read(&dc->writeback_rate.rate) << 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) bch_hprint(target, dc->writeback_rate_target << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) bch_hprint(proportional,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) wb ? dc->writeback_rate_proportional << 9 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) bch_hprint(integral,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) wb ? dc->writeback_rate_integral_scaled << 9 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) NSEC_PER_MSEC) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return sprintf(buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) "rate:\t\t%s/sec\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) "dirty:\t\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) "target:\t\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) "proportional:\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) "integral:\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) "change:\t\t%s/sec\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) "next io:\t%llims\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) rate, dirty, target, proportional,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) integral, change, next_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) sysfs_hprint(dirty_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) bcache_dev_sectors_dirty(&dc->disk) << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) var_printf(partial_stripes_expensive, "%u");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) var_hprint(sequential_cutoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) var_hprint(readahead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) sysfs_print(running, atomic_read(&dc->running));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (attr == &sysfs_label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) buf[SB_LABEL_SIZE + 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) strcat(buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (attr == &sysfs_backing_dev_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) snprintf(buf, BDEVNAME_SIZE + 1, "%s", dc->backing_dev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) strcat(buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (attr == &sysfs_backing_dev_uuid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* convert binary uuid into 36-byte string plus '\0' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) snprintf(buf, 36+1, "%pU", dc->sb.uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) strcat(buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #undef var
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) SHOW_LOCKED(bch_cached_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) STORE(__cached_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct cached_dev *dc = container_of(kobj, struct cached_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) disk.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) ssize_t v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct cache_set *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct kobj_uevent_env *env;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* no user space access if system is rebooting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (bcache_is_reboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #define d_strtoul(var) sysfs_strtoul(var, dc->var)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) #define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) #define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) sysfs_strtoul(data_csum, dc->disk.data_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) d_strtoul(verify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) sysfs_strtoul_bool(bypass_torture_test, dc->bypass_torture_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) sysfs_strtoul_bool(writeback_metadata, dc->writeback_metadata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) sysfs_strtoul_bool(writeback_running, dc->writeback_running);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) sysfs_strtoul_clamp(writeback_delay, dc->writeback_delay, 0, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 0, bch_cutoff_writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (attr == &sysfs_writeback_rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) long int v = atomic_long_read(&dc->writeback_rate.rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) atomic_long_set(&dc->writeback_rate.rate, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) sysfs_strtoul_clamp(writeback_rate_update_seconds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) dc->writeback_rate_update_seconds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) sysfs_strtoul_clamp(writeback_rate_i_term_inverse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) dc->writeback_rate_i_term_inverse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 1, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) sysfs_strtoul_clamp(writeback_rate_p_term_inverse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) dc->writeback_rate_p_term_inverse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 1, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) sysfs_strtoul_clamp(writeback_rate_minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) dc->writeback_rate_minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 1, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (attr == &sysfs_io_disable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int v = strtoul_or_return(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) dc->io_disable = v ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) sysfs_strtoul_clamp(sequential_cutoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) dc->sequential_cutoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 0, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) d_strtoi_h(readahead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (attr == &sysfs_clear_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) bch_cache_accounting_clear(&dc->accounting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (attr == &sysfs_running &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) strtoul_or_return(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) v = bch_cached_dev_run(dc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (attr == &sysfs_cache_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) v = __sysfs_match_string(bch_cache_modes, -1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (v < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) SET_BDEV_CACHE_MODE(&dc->sb, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) bch_write_bdev_super(dc, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (attr == &sysfs_readahead_cache_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) v = __sysfs_match_string(bch_reada_cache_policies, -1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (v < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if ((unsigned int) v != dc->cache_readahead_policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) dc->cache_readahead_policy = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (attr == &sysfs_stop_when_cache_set_failed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (v < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) dc->stop_when_cache_set_failed = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (attr == &sysfs_label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (size > SB_LABEL_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) memcpy(dc->sb.label, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (size < SB_LABEL_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) dc->sb.label[size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (size && dc->sb.label[size - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dc->sb.label[size - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) bch_write_bdev_super(dc, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (dc->disk.c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) memcpy(dc->disk.c->uuids[dc->disk.id].label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) buf, SB_LABEL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) bch_uuid_write(dc->disk.c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (!env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) add_uevent_var(env, "DRIVER=bcache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) add_uevent_var(env, "CACHED_LABEL=%s", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) KOBJ_CHANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) env->envp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) kfree(env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (attr == &sysfs_attach) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) uint8_t set_uuid[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (bch_parse_uuid(buf, set_uuid) < 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) v = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) list_for_each_entry(c, &bch_cache_sets, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) v = bch_cached_dev_attach(dc, c, set_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (v == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) pr_err("Can't attach %s: cache set not found\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (attr == &sysfs_detach && dc->disk.c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) bch_cached_dev_detach(dc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (attr == &sysfs_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) bcache_device_stop(&dc->disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) STORE(bch_cached_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) struct cached_dev *dc = container_of(kobj, struct cached_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) disk.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /* no user space access if system is rebooting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (bcache_is_reboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) mutex_lock(&bch_register_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) size = __cached_dev_store(kobj, attr, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (attr == &sysfs_writeback_running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /* dc->writeback_running changed in __cached_dev_store() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (IS_ERR_OR_NULL(dc->writeback_thread)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * reject setting it to 1 via sysfs if writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * kthread is not created yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (dc->writeback_running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) dc->writeback_running = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) pr_err("%s: failed to run non-existent writeback thread\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) dc->disk.disk->disk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * writeback kthread will check if dc->writeback_running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * is true or false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) bch_writeback_queue(dc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * Only set BCACHE_DEV_WB_RUNNING when cached device attached to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * a cache set, otherwise it doesn't make sense.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (attr == &sysfs_writeback_percent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if ((dc->disk.c != NULL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) schedule_delayed_work(&dc->writeback_rate_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) dc->writeback_rate_update_seconds * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) mutex_unlock(&bch_register_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static struct attribute *bch_cached_dev_files[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) &sysfs_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) &sysfs_detach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) &sysfs_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) &sysfs_data_csum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) &sysfs_cache_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) &sysfs_readahead_cache_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) &sysfs_stop_when_cache_set_failed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) &sysfs_writeback_metadata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) &sysfs_writeback_running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) &sysfs_writeback_delay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) &sysfs_writeback_percent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) &sysfs_writeback_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) &sysfs_writeback_rate_update_seconds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) &sysfs_writeback_rate_i_term_inverse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) &sysfs_writeback_rate_p_term_inverse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) &sysfs_writeback_rate_minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) &sysfs_writeback_rate_debug,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) &sysfs_io_errors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) &sysfs_io_error_limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) &sysfs_io_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) &sysfs_dirty_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) &sysfs_stripe_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) &sysfs_partial_stripes_expensive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) &sysfs_sequential_cutoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) &sysfs_clear_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) &sysfs_running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) &sysfs_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) &sysfs_label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) &sysfs_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) #ifdef CONFIG_BCACHE_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) &sysfs_verify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) &sysfs_bypass_torture_test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) &sysfs_backing_dev_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) &sysfs_backing_dev_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) KTYPE(bch_cached_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) SHOW(bch_flash_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct bcache_device *d = container_of(kobj, struct bcache_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct uuid_entry *u = &d->c->uuids[d->id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) sysfs_printf(data_csum, "%i", d->data_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) sysfs_hprint(size, u->sectors << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (attr == &sysfs_label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) memcpy(buf, u->label, SB_LABEL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) buf[SB_LABEL_SIZE + 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) strcat(buf, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) STORE(__bch_flash_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct bcache_device *d = container_of(kobj, struct bcache_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct uuid_entry *u = &d->c->uuids[d->id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /* no user space access if system is rebooting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (bcache_is_reboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sysfs_strtoul(data_csum, d->data_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (attr == &sysfs_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) uint64_t v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) strtoi_h_or_return(buf, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) u->sectors = v >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) bch_uuid_write(d->c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) set_capacity(d->disk, u->sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (attr == &sysfs_label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) memcpy(u->label, buf, SB_LABEL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) bch_uuid_write(d->c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (attr == &sysfs_unregister) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) set_bit(BCACHE_DEV_DETACHING, &d->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) bcache_device_stop(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) STORE_LOCKED(bch_flash_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static struct attribute *bch_flash_dev_files[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) &sysfs_unregister,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) &sysfs_data_csum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) &sysfs_label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) &sysfs_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) KTYPE(bch_flash_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct bset_stats_op {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) struct btree_op op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) size_t nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct bset_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) op->nodes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) bch_btree_keys_stats(&b->keys, &op->stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return MAP_CONTINUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static int bch_bset_print_stats(struct cache_set *c, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct bset_stats_op op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) memset(&op, 0, sizeof(op));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) bch_btree_op_init(&op.op, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return snprintf(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) "btree nodes: %zu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) "written sets: %zu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) "unwritten sets: %zu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) "written key bytes: %zu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) "unwritten key bytes: %zu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) "floats: %zu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) "failed: %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) op.nodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) op.stats.sets_written, op.stats.sets_unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) op.stats.bytes_written, op.stats.bytes_unwritten,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) op.stats.floats, op.stats.failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) static unsigned int bch_root_usage(struct cache_set *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) unsigned int bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct bkey *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) struct btree *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) struct btree_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) goto lock_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) rw_unlock(false, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) lock_root:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) b = c->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) rw_lock(false, b, b->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) } while (b != c->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) bytes += bkey_bytes(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) rw_unlock(false, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return (bytes * 100) / btree_bytes(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static size_t bch_cache_size(struct cache_set *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) size_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct btree *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) mutex_lock(&c->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) list_for_each_entry(b, &c->btree_cache, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) ret += 1 << (b->keys.page_order + PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) mutex_unlock(&c->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) static unsigned int bch_cache_max_chain(struct cache_set *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) unsigned int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) struct hlist_head *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) mutex_lock(&c->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) for (h = c->bucket_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) h++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) unsigned int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) struct hlist_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) hlist_for_each(p, h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ret = max(ret, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) mutex_unlock(&c->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) static unsigned int bch_btree_used(struct cache_set *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return div64_u64(c->gc_stats.key_bytes * 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) (c->gc_stats.nodes ?: 1) * btree_bytes(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) static unsigned int bch_average_key_size(struct cache_set *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return c->gc_stats.nkeys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) SHOW(__bch_cache_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) struct cache_set *c = container_of(kobj, struct cache_set, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) sysfs_print(synchronous, CACHE_SYNC(&c->cache->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) sysfs_print(journal_delay_ms, c->journal_delay_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) sysfs_hprint(bucket_size, bucket_bytes(c->cache));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) sysfs_hprint(block_size, block_bytes(c->cache));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) sysfs_print(tree_depth, c->root->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) sysfs_print(root_usage_percent, bch_root_usage(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) sysfs_hprint(btree_cache_size, bch_cache_size(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) sysfs_print(btree_used_percent, bch_btree_used(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) sysfs_print(btree_nodes, c->gc_stats.nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) sysfs_hprint(average_key_size, bch_average_key_size(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) sysfs_print(cache_read_races,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) atomic_long_read(&c->cache_read_races));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) sysfs_print(reclaim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) atomic_long_read(&c->reclaim));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) sysfs_print(reclaimed_journal_buckets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) atomic_long_read(&c->reclaimed_journal_buckets));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) sysfs_print(flush_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) atomic_long_read(&c->flush_write));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) sysfs_print(writeback_keys_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) atomic_long_read(&c->writeback_keys_done));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) sysfs_print(writeback_keys_failed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) atomic_long_read(&c->writeback_keys_failed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (attr == &sysfs_errors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) c->on_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /* See count_io_errors for why 88 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) sysfs_print(io_error_halflife, c->error_decay * 88);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) sysfs_print(io_error_limit, c->error_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) sysfs_hprint(congested,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) ((uint64_t) bch_get_congested(c)) << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) sysfs_print(congested_read_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) c->congested_read_threshold_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) sysfs_print(congested_write_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) c->congested_write_threshold_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) sysfs_print(cutoff_writeback, bch_cutoff_writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) sysfs_printf(verify, "%i", c->verify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) sysfs_printf(expensive_debug_checks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) "%i", c->expensive_debug_checks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) sysfs_printf(idle_max_writeback_rate, "%i",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) c->idle_max_writeback_rate_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) sysfs_printf(gc_after_writeback, "%i", c->gc_after_writeback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) sysfs_printf(io_disable, "%i",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) test_bit(CACHE_SET_IO_DISABLE, &c->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (attr == &sysfs_bset_tree_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return bch_bset_print_stats(c, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (attr == &sysfs_feature_compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) return bch_print_cache_set_feature_compat(c, buf, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (attr == &sysfs_feature_ro_compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return bch_print_cache_set_feature_ro_compat(c, buf, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (attr == &sysfs_feature_incompat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) return bch_print_cache_set_feature_incompat(c, buf, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) SHOW_LOCKED(bch_cache_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) STORE(__bch_cache_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct cache_set *c = container_of(kobj, struct cache_set, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) ssize_t v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) /* no user space access if system is rebooting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (bcache_is_reboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (attr == &sysfs_unregister)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) bch_cache_set_unregister(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (attr == &sysfs_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) bch_cache_set_stop(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (attr == &sysfs_synchronous) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) bool sync = strtoul_or_return(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (sync != CACHE_SYNC(&c->cache->sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) SET_CACHE_SYNC(&c->cache->sb, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) bcache_write_super(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) if (attr == &sysfs_flash_vol_create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) uint64_t v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) strtoi_h_or_return(buf, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) r = bch_flash_dev_create(c, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (attr == &sysfs_clear_stats) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) atomic_long_set(&c->writeback_keys_done, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) atomic_long_set(&c->writeback_keys_failed, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) memset(&c->gc_stats, 0, sizeof(struct gc_stat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) bch_cache_accounting_clear(&c->accounting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (attr == &sysfs_trigger_gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) force_wake_up_gc(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (attr == &sysfs_prune_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) struct shrink_control sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) sc.gfp_mask = GFP_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) sc.nr_to_scan = strtoul_or_return(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) c->shrink.scan_objects(&c->shrink, &sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) sysfs_strtoul_clamp(congested_read_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) c->congested_read_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 0, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) sysfs_strtoul_clamp(congested_write_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) c->congested_write_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 0, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (attr == &sysfs_errors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) v = __sysfs_match_string(error_actions, -1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (v < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) c->on_error = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) sysfs_strtoul_clamp(io_error_limit, c->error_limit, 0, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /* See count_io_errors() for why 88 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (attr == &sysfs_io_error_halflife) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) unsigned long v = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) c->error_decay = v / 88;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (attr == &sysfs_io_disable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) v = strtoul_or_return(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (test_and_set_bit(CACHE_SET_IO_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) &c->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) pr_warn("CACHE_SET_IO_DISABLE already set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) &c->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) pr_warn("CACHE_SET_IO_DISABLE already cleared\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) sysfs_strtoul_clamp(journal_delay_ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) c->journal_delay_ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 0, USHRT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) sysfs_strtoul_bool(verify, c->verify);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) sysfs_strtoul_bool(key_merging_disabled, c->key_merging_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) sysfs_strtoul_bool(gc_always_rewrite, c->gc_always_rewrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) sysfs_strtoul_bool(btree_shrinker_disabled, c->shrinker_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) sysfs_strtoul_bool(copy_gc_enabled, c->copy_gc_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) sysfs_strtoul_bool(idle_max_writeback_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) c->idle_max_writeback_rate_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) * write gc_after_writeback here may overwrite an already set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * set in next chance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) STORE_LOCKED(bch_cache_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) SHOW(bch_cache_set_internal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct cache_set *c = container_of(kobj, struct cache_set, internal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) return bch_cache_set_show(&c->kobj, attr, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) STORE(bch_cache_set_internal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) struct cache_set *c = container_of(kobj, struct cache_set, internal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) /* no user space access if system is rebooting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) if (bcache_is_reboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) return bch_cache_set_store(&c->kobj, attr, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) static void bch_cache_set_internal_release(struct kobject *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) static struct attribute *bch_cache_set_files[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) &sysfs_unregister,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) &sysfs_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) &sysfs_synchronous,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) &sysfs_journal_delay_ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) &sysfs_flash_vol_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) &sysfs_bucket_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) &sysfs_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) &sysfs_tree_depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) &sysfs_root_usage_percent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) &sysfs_btree_cache_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) &sysfs_cache_available_percent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) &sysfs_average_key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) &sysfs_errors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) &sysfs_io_error_limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) &sysfs_io_error_halflife,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) &sysfs_congested,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) &sysfs_congested_read_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) &sysfs_congested_write_threshold_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) &sysfs_clear_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) KTYPE(bch_cache_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) static struct attribute *bch_cache_set_internal_files[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) &sysfs_active_journal_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) sysfs_time_stats_attribute_list(btree_gc, sec, ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) sysfs_time_stats_attribute_list(btree_split, sec, us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) sysfs_time_stats_attribute_list(btree_sort, ms, us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) sysfs_time_stats_attribute_list(btree_read, ms, us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) &sysfs_btree_nodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) &sysfs_btree_used_percent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) &sysfs_btree_cache_max_chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) &sysfs_bset_tree_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) &sysfs_cache_read_races,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) &sysfs_reclaim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) &sysfs_reclaimed_journal_buckets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) &sysfs_flush_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) &sysfs_writeback_keys_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) &sysfs_writeback_keys_failed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) &sysfs_trigger_gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) &sysfs_prune_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) #ifdef CONFIG_BCACHE_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) &sysfs_verify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) &sysfs_key_merging_disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) &sysfs_expensive_debug_checks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) &sysfs_gc_always_rewrite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) &sysfs_btree_shrinker_disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) &sysfs_copy_gc_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) &sysfs_idle_max_writeback_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) &sysfs_gc_after_writeback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) &sysfs_io_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) &sysfs_cutoff_writeback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) &sysfs_cutoff_writeback_sync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) &sysfs_feature_compat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) &sysfs_feature_ro_compat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) &sysfs_feature_incompat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) KTYPE(bch_cache_set_internal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) static int __bch_cache_cmp(const void *l, const void *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return *((uint16_t *)r) - *((uint16_t *)l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) SHOW(__bch_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) struct cache *ca = container_of(kobj, struct cache, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) sysfs_hprint(bucket_size, bucket_bytes(ca));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) sysfs_hprint(block_size, block_bytes(ca));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) sysfs_print(nbuckets, ca->sb.nbuckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) sysfs_print(discard, ca->discard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) sysfs_hprint(btree_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) atomic_long_read(&ca->btree_sectors_written) << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) sysfs_hprint(metadata_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) (atomic_long_read(&ca->meta_sectors_written) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) atomic_long_read(&ca->btree_sectors_written)) << 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) sysfs_print(io_errors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (attr == &sysfs_cache_replacement_policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return bch_snprint_string_list(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) cache_replacement_policies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) CACHE_REPLACEMENT(&ca->sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (attr == &sysfs_priority_stats) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) struct bucket *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) size_t n = ca->sb.nbuckets, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) size_t unused = 0, available = 0, dirty = 0, meta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) uint64_t sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) /* Compute 31 quantiles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) uint16_t q[31], *p, *cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) cached = p = vmalloc(array_size(sizeof(uint16_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) ca->sb.nbuckets));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) mutex_lock(&ca->set->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) for_each_bucket(b, ca) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) if (!GC_SECTORS_USED(b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) unused++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) available++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) if (GC_MARK(b) == GC_MARK_DIRTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) dirty++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) if (GC_MARK(b) == GC_MARK_METADATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) meta++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) for (i = ca->sb.first_bucket; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) p[i] = ca->buckets[i].prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) mutex_unlock(&ca->set->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) while (n &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) !cached[n - 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) --n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) while (cached < p + n &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) *cached == BTREE_PRIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) cached++, n--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) sum += INITIAL_PRIO - cached[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) if (n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) do_div(sum, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) for (i = 0; i < ARRAY_SIZE(q); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) q[i] = INITIAL_PRIO - cached[n * (i + 1) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) (ARRAY_SIZE(q) + 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) vfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) ret = scnprintf(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) "Unused: %zu%%\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) "Clean: %zu%%\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) "Dirty: %zu%%\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) "Metadata: %zu%%\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) "Average: %llu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) "Sectors per Q: %zu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) "Quantiles: [",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) unused * 100 / (size_t) ca->sb.nbuckets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) available * 100 / (size_t) ca->sb.nbuckets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) dirty * 100 / (size_t) ca->sb.nbuckets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) meta * 100 / (size_t) ca->sb.nbuckets, sum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) for (i = 0; i < ARRAY_SIZE(q); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) ret += scnprintf(buf + ret, PAGE_SIZE - ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) "%u ", q[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) ret--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) SHOW_LOCKED(bch_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) STORE(__bch_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) struct cache *ca = container_of(kobj, struct cache, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) ssize_t v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) /* no user space access if system is rebooting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) if (bcache_is_reboot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) if (attr == &sysfs_discard) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) bool v = strtoul_or_return(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (blk_queue_discard(bdev_get_queue(ca->bdev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) ca->discard = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) if (v != CACHE_DISCARD(&ca->sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) SET_CACHE_DISCARD(&ca->sb, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) bcache_write_super(ca->set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) if (attr == &sysfs_cache_replacement_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) v = __sysfs_match_string(cache_replacement_policies, -1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) if (v < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) mutex_lock(&ca->set->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) SET_CACHE_REPLACEMENT(&ca->sb, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) mutex_unlock(&ca->set->bucket_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) bcache_write_super(ca->set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) if (attr == &sysfs_clear_stats) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) atomic_long_set(&ca->sectors_written, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) atomic_long_set(&ca->btree_sectors_written, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) atomic_long_set(&ca->meta_sectors_written, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) atomic_set(&ca->io_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) atomic_set(&ca->io_errors, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) STORE_LOCKED(bch_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static struct attribute *bch_cache_files[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) &sysfs_bucket_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) &sysfs_block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) &sysfs_nbuckets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) &sysfs_priority_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) &sysfs_discard,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) &sysfs_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) &sysfs_btree_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) &sysfs_metadata_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) &sysfs_io_errors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) &sysfs_clear_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) &sysfs_cache_replacement_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) KTYPE(bch_cache);