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