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