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 | |
| 19 | static const char * const cache_replacement_policies[] = { |
| 20 | "lru", |
| 21 | "fifo", |
| 22 | "random", |
| 23 | NULL |
| 24 | }; |
| 25 | |
Kent Overstreet | 77c320e | 2013-07-11 19:42:51 -0700 | [diff] [blame] | 26 | static const char * const error_actions[] = { |
| 27 | "unregister", |
| 28 | "panic", |
| 29 | NULL |
| 30 | }; |
| 31 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 32 | write_attribute(attach); |
| 33 | write_attribute(detach); |
| 34 | write_attribute(unregister); |
| 35 | write_attribute(stop); |
| 36 | write_attribute(clear_stats); |
| 37 | write_attribute(trigger_gc); |
| 38 | write_attribute(prune_cache); |
| 39 | write_attribute(flash_vol_create); |
| 40 | |
| 41 | read_attribute(bucket_size); |
| 42 | read_attribute(block_size); |
| 43 | read_attribute(nbuckets); |
| 44 | read_attribute(tree_depth); |
| 45 | read_attribute(root_usage_percent); |
| 46 | read_attribute(priority_stats); |
| 47 | read_attribute(btree_cache_size); |
| 48 | read_attribute(btree_cache_max_chain); |
| 49 | read_attribute(cache_available_percent); |
| 50 | read_attribute(written); |
| 51 | read_attribute(btree_written); |
| 52 | read_attribute(metadata_written); |
| 53 | read_attribute(active_journal_entries); |
| 54 | |
| 55 | sysfs_time_stats_attribute(btree_gc, sec, ms); |
| 56 | sysfs_time_stats_attribute(btree_split, sec, us); |
| 57 | sysfs_time_stats_attribute(btree_sort, ms, us); |
| 58 | sysfs_time_stats_attribute(btree_read, ms, us); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 59 | |
| 60 | read_attribute(btree_nodes); |
| 61 | read_attribute(btree_used_percent); |
| 62 | read_attribute(average_key_size); |
| 63 | read_attribute(dirty_data); |
| 64 | read_attribute(bset_tree_stats); |
| 65 | |
| 66 | read_attribute(state); |
| 67 | read_attribute(cache_read_races); |
Tang Junhui | a728eac | 2018-02-07 11:41:39 -0800 | [diff] [blame] | 68 | read_attribute(reclaim); |
| 69 | read_attribute(flush_write); |
| 70 | read_attribute(retry_flush_write); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 71 | read_attribute(writeback_keys_done); |
| 72 | read_attribute(writeback_keys_failed); |
| 73 | read_attribute(io_errors); |
| 74 | read_attribute(congested); |
| 75 | rw_attribute(congested_read_threshold_us); |
| 76 | rw_attribute(congested_write_threshold_us); |
| 77 | |
| 78 | rw_attribute(sequential_cutoff); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 79 | rw_attribute(data_csum); |
| 80 | rw_attribute(cache_mode); |
Coly Li | 7e027ca | 2018-03-18 17:36:18 -0700 | [diff] [blame^] | 81 | rw_attribute(stop_when_cache_set_failed); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 82 | rw_attribute(writeback_metadata); |
| 83 | rw_attribute(writeback_running); |
| 84 | rw_attribute(writeback_percent); |
| 85 | rw_attribute(writeback_delay); |
| 86 | rw_attribute(writeback_rate); |
| 87 | |
| 88 | rw_attribute(writeback_rate_update_seconds); |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 89 | rw_attribute(writeback_rate_i_term_inverse); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 90 | rw_attribute(writeback_rate_p_term_inverse); |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 91 | rw_attribute(writeback_rate_minimum); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 92 | read_attribute(writeback_rate_debug); |
| 93 | |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 94 | read_attribute(stripe_size); |
| 95 | read_attribute(partial_stripes_expensive); |
| 96 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 97 | rw_attribute(synchronous); |
| 98 | rw_attribute(journal_delay_ms); |
Coly Li | 771f393 | 2018-03-18 17:36:17 -0700 | [diff] [blame] | 99 | rw_attribute(io_disable); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 100 | rw_attribute(discard); |
| 101 | rw_attribute(running); |
| 102 | rw_attribute(label); |
| 103 | rw_attribute(readahead); |
Kent Overstreet | 77c320e | 2013-07-11 19:42:51 -0700 | [diff] [blame] | 104 | rw_attribute(errors); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 105 | rw_attribute(io_error_limit); |
| 106 | rw_attribute(io_error_halflife); |
| 107 | rw_attribute(verify); |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 108 | rw_attribute(bypass_torture_test); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 109 | rw_attribute(key_merging_disabled); |
| 110 | rw_attribute(gc_always_rewrite); |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 111 | rw_attribute(expensive_debug_checks); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 112 | rw_attribute(cache_replacement_policy); |
| 113 | rw_attribute(btree_shrinker_disabled); |
| 114 | rw_attribute(copy_gc_enabled); |
| 115 | rw_attribute(size); |
| 116 | |
| 117 | SHOW(__bch_cached_dev) |
| 118 | { |
| 119 | struct cached_dev *dc = container_of(kobj, struct cached_dev, |
| 120 | disk.kobj); |
| 121 | const char *states[] = { "no cache", "clean", "dirty", "inconsistent" }; |
| 122 | |
| 123 | #define var(stat) (dc->stat) |
| 124 | |
| 125 | if (attr == &sysfs_cache_mode) |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 126 | return bch_snprint_string_list(buf, PAGE_SIZE, |
| 127 | bch_cache_modes + 1, |
| 128 | BDEV_CACHE_MODE(&dc->sb)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 129 | |
Coly Li | 7e027ca | 2018-03-18 17:36:18 -0700 | [diff] [blame^] | 130 | if (attr == &sysfs_stop_when_cache_set_failed) |
| 131 | return bch_snprint_string_list(buf, PAGE_SIZE, |
| 132 | bch_stop_on_failure_modes + 1, |
| 133 | dc->stop_when_cache_set_failed); |
| 134 | |
| 135 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 136 | sysfs_printf(data_csum, "%i", dc->disk.data_csum); |
| 137 | var_printf(verify, "%i"); |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 138 | var_printf(bypass_torture_test, "%i"); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 139 | var_printf(writeback_metadata, "%i"); |
| 140 | var_printf(writeback_running, "%i"); |
| 141 | var_print(writeback_delay); |
| 142 | var_print(writeback_percent); |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 143 | sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 144 | |
| 145 | var_print(writeback_rate_update_seconds); |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 146 | var_print(writeback_rate_i_term_inverse); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 147 | var_print(writeback_rate_p_term_inverse); |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 148 | var_print(writeback_rate_minimum); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 149 | |
| 150 | if (attr == &sysfs_writeback_rate_debug) { |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 151 | char rate[20]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 152 | char dirty[20]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 153 | char target[20]; |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 154 | char proportional[20]; |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 155 | char integral[20]; |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 156 | char change[20]; |
| 157 | s64 next_io; |
| 158 | |
| 159 | bch_hprint(rate, dc->writeback_rate.rate << 9); |
| 160 | bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9); |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 161 | bch_hprint(target, dc->writeback_rate_target << 9); |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 162 | bch_hprint(proportional,dc->writeback_rate_proportional << 9); |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 163 | bch_hprint(integral, dc->writeback_rate_integral_scaled << 9); |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 164 | bch_hprint(change, dc->writeback_rate_change << 9); |
| 165 | |
| 166 | next_io = div64_s64(dc->writeback_rate.next - local_clock(), |
| 167 | NSEC_PER_MSEC); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 168 | |
| 169 | return sprintf(buf, |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 170 | "rate:\t\t%s/sec\n" |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 171 | "dirty:\t\t%s\n" |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 172 | "target:\t\t%s\n" |
| 173 | "proportional:\t%s\n" |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 174 | "integral:\t%s\n" |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 175 | "change:\t\t%s/sec\n" |
| 176 | "next io:\t%llims\n", |
| 177 | rate, dirty, target, proportional, |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 178 | integral, change, next_io); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | sysfs_hprint(dirty_data, |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 182 | bcache_dev_sectors_dirty(&dc->disk) << 9); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 183 | |
Kent Overstreet | 2d679fc | 2013-08-17 02:13:15 -0700 | [diff] [blame] | 184 | sysfs_hprint(stripe_size, dc->disk.stripe_size << 9); |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 185 | var_printf(partial_stripes_expensive, "%u"); |
| 186 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 187 | var_hprint(sequential_cutoff); |
| 188 | var_hprint(readahead); |
| 189 | |
| 190 | sysfs_print(running, atomic_read(&dc->running)); |
| 191 | sysfs_print(state, states[BDEV_STATE(&dc->sb)]); |
| 192 | |
| 193 | if (attr == &sysfs_label) { |
| 194 | memcpy(buf, dc->sb.label, SB_LABEL_SIZE); |
| 195 | buf[SB_LABEL_SIZE + 1] = '\0'; |
| 196 | strcat(buf, "\n"); |
| 197 | return strlen(buf); |
| 198 | } |
| 199 | |
| 200 | #undef var |
| 201 | return 0; |
| 202 | } |
| 203 | SHOW_LOCKED(bch_cached_dev) |
| 204 | |
| 205 | STORE(__cached_dev) |
| 206 | { |
| 207 | struct cached_dev *dc = container_of(kobj, struct cached_dev, |
| 208 | disk.kobj); |
Tang Junhui | 7f4fc93 | 2018-02-07 11:41:45 -0800 | [diff] [blame] | 209 | ssize_t v; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 210 | struct cache_set *c; |
Gabriel de Perthuis | ab9e140 | 2013-06-09 00:54:48 +0200 | [diff] [blame] | 211 | struct kobj_uevent_env *env; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 212 | |
| 213 | #define d_strtoul(var) sysfs_strtoul(var, dc->var) |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 214 | #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] | 215 | #define d_strtoi_h(var) sysfs_hatoi(var, dc->var) |
| 216 | |
| 217 | sysfs_strtoul(data_csum, dc->disk.data_csum); |
| 218 | d_strtoul(verify); |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 219 | d_strtoul(bypass_torture_test); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 220 | d_strtoul(writeback_metadata); |
| 221 | d_strtoul(writeback_running); |
| 222 | d_strtoul(writeback_delay); |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 223 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 224 | sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40); |
| 225 | |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 226 | sysfs_strtoul_clamp(writeback_rate, |
| 227 | dc->writeback_rate.rate, 1, INT_MAX); |
| 228 | |
Coly Li | 7a5e3ec | 2018-02-07 11:41:44 -0800 | [diff] [blame] | 229 | sysfs_strtoul_clamp(writeback_rate_update_seconds, |
| 230 | dc->writeback_rate_update_seconds, |
| 231 | 1, WRITEBACK_RATE_UPDATE_SECS_MAX); |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 232 | d_strtoul(writeback_rate_i_term_inverse); |
Kent Overstreet | 16749c2 | 2013-11-11 13:58:34 -0800 | [diff] [blame] | 233 | d_strtoul_nonzero(writeback_rate_p_term_inverse); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 234 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 235 | d_strtoi_h(sequential_cutoff); |
| 236 | d_strtoi_h(readahead); |
| 237 | |
| 238 | if (attr == &sysfs_clear_stats) |
| 239 | bch_cache_accounting_clear(&dc->accounting); |
| 240 | |
| 241 | if (attr == &sysfs_running && |
| 242 | strtoul_or_return(buf)) |
| 243 | bch_cached_dev_run(dc); |
| 244 | |
| 245 | if (attr == &sysfs_cache_mode) { |
Tony Asleson | 77fa100 | 2017-09-06 14:25:57 +0800 | [diff] [blame] | 246 | v = bch_read_string_list(buf, bch_cache_modes + 1); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 247 | |
| 248 | if (v < 0) |
| 249 | return v; |
| 250 | |
| 251 | if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) { |
| 252 | SET_BDEV_CACHE_MODE(&dc->sb, v); |
| 253 | bch_write_bdev_super(dc, NULL); |
| 254 | } |
| 255 | } |
| 256 | |
Coly Li | 7e027ca | 2018-03-18 17:36:18 -0700 | [diff] [blame^] | 257 | if (attr == &sysfs_stop_when_cache_set_failed) { |
| 258 | v = bch_read_string_list(buf, bch_stop_on_failure_modes + 1); |
| 259 | |
| 260 | if (v < 0) |
| 261 | return v; |
| 262 | |
| 263 | dc->stop_when_cache_set_failed = v; |
| 264 | } |
| 265 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 266 | if (attr == &sysfs_label) { |
Gabriel de Perthuis | aee6f1c | 2013-09-23 23:17:28 -0700 | [diff] [blame] | 267 | if (size > SB_LABEL_SIZE) |
| 268 | return -EINVAL; |
| 269 | memcpy(dc->sb.label, buf, size); |
| 270 | if (size < SB_LABEL_SIZE) |
| 271 | dc->sb.label[size] = '\0'; |
| 272 | if (size && dc->sb.label[size - 1] == '\n') |
| 273 | dc->sb.label[size - 1] = '\0'; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 274 | bch_write_bdev_super(dc, NULL); |
| 275 | if (dc->disk.c) { |
| 276 | memcpy(dc->disk.c->uuids[dc->disk.id].label, |
| 277 | buf, SB_LABEL_SIZE); |
| 278 | bch_uuid_write(dc->disk.c); |
| 279 | } |
Gabriel de Perthuis | ab9e140 | 2013-06-09 00:54:48 +0200 | [diff] [blame] | 280 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
Dan Carpenter | d2a65ce | 2013-07-05 09:05:46 +0300 | [diff] [blame] | 281 | if (!env) |
| 282 | return -ENOMEM; |
Gabriel de Perthuis | ab9e140 | 2013-06-09 00:54:48 +0200 | [diff] [blame] | 283 | add_uevent_var(env, "DRIVER=bcache"); |
| 284 | add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid), |
| 285 | add_uevent_var(env, "CACHED_LABEL=%s", buf); |
| 286 | kobject_uevent_env( |
| 287 | &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp); |
| 288 | kfree(env); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (attr == &sysfs_attach) { |
Tang Junhui | 73ac105 | 2018-02-07 11:41:46 -0800 | [diff] [blame] | 292 | uint8_t set_uuid[16]; |
| 293 | |
| 294 | if (bch_parse_uuid(buf, set_uuid) < 16) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 295 | return -EINVAL; |
| 296 | |
Tang Junhui | 7f4fc93 | 2018-02-07 11:41:45 -0800 | [diff] [blame] | 297 | v = -ENOENT; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 298 | list_for_each_entry(c, &bch_cache_sets, list) { |
Tang Junhui | 73ac105 | 2018-02-07 11:41:46 -0800 | [diff] [blame] | 299 | v = bch_cached_dev_attach(dc, c, set_uuid); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 300 | if (!v) |
| 301 | return size; |
| 302 | } |
| 303 | |
| 304 | pr_err("Can't attach %s: cache set not found", buf); |
Tang Junhui | 7f4fc93 | 2018-02-07 11:41:45 -0800 | [diff] [blame] | 305 | return v; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (attr == &sysfs_detach && dc->disk.c) |
| 309 | bch_cached_dev_detach(dc); |
| 310 | |
| 311 | if (attr == &sysfs_stop) |
| 312 | bcache_device_stop(&dc->disk); |
| 313 | |
| 314 | return size; |
| 315 | } |
| 316 | |
| 317 | STORE(bch_cached_dev) |
| 318 | { |
| 319 | struct cached_dev *dc = container_of(kobj, struct cached_dev, |
| 320 | disk.kobj); |
| 321 | |
| 322 | mutex_lock(&bch_register_lock); |
| 323 | size = __cached_dev_store(kobj, attr, buf, size); |
| 324 | |
| 325 | if (attr == &sysfs_writeback_running) |
| 326 | bch_writeback_queue(dc); |
| 327 | |
| 328 | if (attr == &sysfs_writeback_percent) |
Coly Li | 3fd47bf | 2018-03-18 17:36:16 -0700 | [diff] [blame] | 329 | if (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)) |
| 330 | schedule_delayed_work(&dc->writeback_rate_update, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 331 | dc->writeback_rate_update_seconds * HZ); |
| 332 | |
| 333 | mutex_unlock(&bch_register_lock); |
| 334 | return size; |
| 335 | } |
| 336 | |
| 337 | static struct attribute *bch_cached_dev_files[] = { |
| 338 | &sysfs_attach, |
| 339 | &sysfs_detach, |
| 340 | &sysfs_stop, |
| 341 | #if 0 |
| 342 | &sysfs_data_csum, |
| 343 | #endif |
| 344 | &sysfs_cache_mode, |
Coly Li | 7e027ca | 2018-03-18 17:36:18 -0700 | [diff] [blame^] | 345 | &sysfs_stop_when_cache_set_failed, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 346 | &sysfs_writeback_metadata, |
| 347 | &sysfs_writeback_running, |
| 348 | &sysfs_writeback_delay, |
| 349 | &sysfs_writeback_percent, |
| 350 | &sysfs_writeback_rate, |
| 351 | &sysfs_writeback_rate_update_seconds, |
Michael Lyle | 1d316e6 | 2017-10-13 16:35:36 -0700 | [diff] [blame] | 352 | &sysfs_writeback_rate_i_term_inverse, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 353 | &sysfs_writeback_rate_p_term_inverse, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 354 | &sysfs_writeback_rate_debug, |
| 355 | &sysfs_dirty_data, |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 356 | &sysfs_stripe_size, |
| 357 | &sysfs_partial_stripes_expensive, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 358 | &sysfs_sequential_cutoff, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 359 | &sysfs_clear_stats, |
| 360 | &sysfs_running, |
| 361 | &sysfs_state, |
| 362 | &sysfs_label, |
| 363 | &sysfs_readahead, |
| 364 | #ifdef CONFIG_BCACHE_DEBUG |
| 365 | &sysfs_verify, |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 366 | &sysfs_bypass_torture_test, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 367 | #endif |
| 368 | NULL |
| 369 | }; |
| 370 | KTYPE(bch_cached_dev); |
| 371 | |
| 372 | SHOW(bch_flash_dev) |
| 373 | { |
| 374 | struct bcache_device *d = container_of(kobj, struct bcache_device, |
| 375 | kobj); |
| 376 | struct uuid_entry *u = &d->c->uuids[d->id]; |
| 377 | |
| 378 | sysfs_printf(data_csum, "%i", d->data_csum); |
| 379 | sysfs_hprint(size, u->sectors << 9); |
| 380 | |
| 381 | if (attr == &sysfs_label) { |
| 382 | memcpy(buf, u->label, SB_LABEL_SIZE); |
| 383 | buf[SB_LABEL_SIZE + 1] = '\0'; |
| 384 | strcat(buf, "\n"); |
| 385 | return strlen(buf); |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | STORE(__bch_flash_dev) |
| 392 | { |
| 393 | struct bcache_device *d = container_of(kobj, struct bcache_device, |
| 394 | kobj); |
| 395 | struct uuid_entry *u = &d->c->uuids[d->id]; |
| 396 | |
| 397 | sysfs_strtoul(data_csum, d->data_csum); |
| 398 | |
| 399 | if (attr == &sysfs_size) { |
| 400 | uint64_t v; |
| 401 | strtoi_h_or_return(buf, v); |
| 402 | |
| 403 | u->sectors = v >> 9; |
| 404 | bch_uuid_write(d->c); |
| 405 | set_capacity(d->disk, u->sectors); |
| 406 | } |
| 407 | |
| 408 | if (attr == &sysfs_label) { |
| 409 | memcpy(u->label, buf, SB_LABEL_SIZE); |
| 410 | bch_uuid_write(d->c); |
| 411 | } |
| 412 | |
| 413 | if (attr == &sysfs_unregister) { |
Kent Overstreet | c4d951d | 2013-08-21 17:49:09 -0700 | [diff] [blame] | 414 | set_bit(BCACHE_DEV_DETACHING, &d->flags); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 415 | bcache_device_stop(d); |
| 416 | } |
| 417 | |
| 418 | return size; |
| 419 | } |
| 420 | STORE_LOCKED(bch_flash_dev) |
| 421 | |
| 422 | static struct attribute *bch_flash_dev_files[] = { |
| 423 | &sysfs_unregister, |
| 424 | #if 0 |
| 425 | &sysfs_data_csum, |
| 426 | #endif |
| 427 | &sysfs_label, |
| 428 | &sysfs_size, |
| 429 | NULL |
| 430 | }; |
| 431 | KTYPE(bch_flash_dev); |
| 432 | |
Kent Overstreet | f67342d | 2013-11-11 19:25:55 -0800 | [diff] [blame] | 433 | struct bset_stats_op { |
| 434 | struct btree_op op; |
| 435 | size_t nodes; |
| 436 | struct bset_stats stats; |
| 437 | }; |
| 438 | |
John Sheu | cb85114 | 2014-03-17 23:13:56 -0700 | [diff] [blame] | 439 | 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] | 440 | { |
| 441 | struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op); |
| 442 | |
| 443 | op->nodes++; |
| 444 | bch_btree_keys_stats(&b->keys, &op->stats); |
| 445 | |
| 446 | return MAP_CONTINUE; |
| 447 | } |
| 448 | |
Kent Overstreet | 3572324 | 2014-01-10 18:53:02 -0800 | [diff] [blame] | 449 | static int bch_bset_print_stats(struct cache_set *c, char *buf) |
Kent Overstreet | f67342d | 2013-11-11 19:25:55 -0800 | [diff] [blame] | 450 | { |
| 451 | struct bset_stats_op op; |
| 452 | int ret; |
| 453 | |
| 454 | memset(&op, 0, sizeof(op)); |
| 455 | bch_btree_op_init(&op.op, -1); |
| 456 | |
John Sheu | cb85114 | 2014-03-17 23:13:56 -0700 | [diff] [blame] | 457 | 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] | 458 | if (ret < 0) |
| 459 | return ret; |
| 460 | |
| 461 | return snprintf(buf, PAGE_SIZE, |
| 462 | "btree nodes: %zu\n" |
| 463 | "written sets: %zu\n" |
| 464 | "unwritten sets: %zu\n" |
| 465 | "written key bytes: %zu\n" |
| 466 | "unwritten key bytes: %zu\n" |
| 467 | "floats: %zu\n" |
| 468 | "failed: %zu\n", |
| 469 | op.nodes, |
| 470 | op.stats.sets_written, op.stats.sets_unwritten, |
| 471 | op.stats.bytes_written, op.stats.bytes_unwritten, |
| 472 | op.stats.floats, op.stats.failed); |
| 473 | } |
| 474 | |
John Sheu | cb85114 | 2014-03-17 23:13:56 -0700 | [diff] [blame] | 475 | static unsigned bch_root_usage(struct cache_set *c) |
| 476 | { |
| 477 | unsigned bytes = 0; |
| 478 | struct bkey *k; |
| 479 | struct btree *b; |
| 480 | struct btree_iter iter; |
| 481 | |
| 482 | goto lock_root; |
| 483 | |
| 484 | do { |
| 485 | rw_unlock(false, b); |
| 486 | lock_root: |
| 487 | b = c->root; |
| 488 | rw_lock(false, b, b->level); |
| 489 | } while (b != c->root); |
| 490 | |
| 491 | for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) |
| 492 | bytes += bkey_bytes(k); |
| 493 | |
| 494 | rw_unlock(false, b); |
| 495 | |
| 496 | return (bytes * 100) / btree_bytes(c); |
| 497 | } |
| 498 | |
| 499 | static size_t bch_cache_size(struct cache_set *c) |
| 500 | { |
| 501 | size_t ret = 0; |
| 502 | struct btree *b; |
| 503 | |
| 504 | mutex_lock(&c->bucket_lock); |
| 505 | list_for_each_entry(b, &c->btree_cache, list) |
| 506 | ret += 1 << (b->keys.page_order + PAGE_SHIFT); |
| 507 | |
| 508 | mutex_unlock(&c->bucket_lock); |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static unsigned bch_cache_max_chain(struct cache_set *c) |
| 513 | { |
| 514 | unsigned ret = 0; |
| 515 | struct hlist_head *h; |
| 516 | |
| 517 | mutex_lock(&c->bucket_lock); |
| 518 | |
| 519 | for (h = c->bucket_hash; |
| 520 | h < c->bucket_hash + (1 << BUCKET_HASH_BITS); |
| 521 | h++) { |
| 522 | unsigned i = 0; |
| 523 | struct hlist_node *p; |
| 524 | |
| 525 | hlist_for_each(p, h) |
| 526 | i++; |
| 527 | |
| 528 | ret = max(ret, i); |
| 529 | } |
| 530 | |
| 531 | mutex_unlock(&c->bucket_lock); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | static unsigned bch_btree_used(struct cache_set *c) |
| 536 | { |
| 537 | return div64_u64(c->gc_stats.key_bytes * 100, |
| 538 | (c->gc_stats.nodes ?: 1) * btree_bytes(c)); |
| 539 | } |
| 540 | |
| 541 | static unsigned bch_average_key_size(struct cache_set *c) |
| 542 | { |
| 543 | return c->gc_stats.nkeys |
| 544 | ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys) |
| 545 | : 0; |
| 546 | } |
| 547 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 548 | SHOW(__bch_cache_set) |
| 549 | { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 550 | struct cache_set *c = container_of(kobj, struct cache_set, kobj); |
| 551 | |
| 552 | sysfs_print(synchronous, CACHE_SYNC(&c->sb)); |
| 553 | sysfs_print(journal_delay_ms, c->journal_delay_ms); |
| 554 | sysfs_hprint(bucket_size, bucket_bytes(c)); |
| 555 | sysfs_hprint(block_size, block_bytes(c)); |
| 556 | sysfs_print(tree_depth, c->root->level); |
John Sheu | cb85114 | 2014-03-17 23:13:56 -0700 | [diff] [blame] | 557 | sysfs_print(root_usage_percent, bch_root_usage(c)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 558 | |
John Sheu | cb85114 | 2014-03-17 23:13:56 -0700 | [diff] [blame] | 559 | sysfs_hprint(btree_cache_size, bch_cache_size(c)); |
| 560 | sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 561 | sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use); |
| 562 | |
| 563 | sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms); |
| 564 | sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us); |
Kent Overstreet | 67539e8 | 2013-09-10 22:53:34 -0700 | [diff] [blame] | 565 | sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 566 | sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 567 | |
John Sheu | cb85114 | 2014-03-17 23:13:56 -0700 | [diff] [blame] | 568 | sysfs_print(btree_used_percent, bch_btree_used(c)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 569 | sysfs_print(btree_nodes, c->gc_stats.nodes); |
John Sheu | cb85114 | 2014-03-17 23:13:56 -0700 | [diff] [blame] | 570 | sysfs_hprint(average_key_size, bch_average_key_size(c)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 571 | |
| 572 | sysfs_print(cache_read_races, |
| 573 | atomic_long_read(&c->cache_read_races)); |
| 574 | |
Tang Junhui | a728eac | 2018-02-07 11:41:39 -0800 | [diff] [blame] | 575 | sysfs_print(reclaim, |
| 576 | atomic_long_read(&c->reclaim)); |
| 577 | |
| 578 | sysfs_print(flush_write, |
| 579 | atomic_long_read(&c->flush_write)); |
| 580 | |
| 581 | sysfs_print(retry_flush_write, |
| 582 | atomic_long_read(&c->retry_flush_write)); |
| 583 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 584 | sysfs_print(writeback_keys_done, |
| 585 | atomic_long_read(&c->writeback_keys_done)); |
| 586 | sysfs_print(writeback_keys_failed, |
| 587 | atomic_long_read(&c->writeback_keys_failed)); |
| 588 | |
Kent Overstreet | 77c320e | 2013-07-11 19:42:51 -0700 | [diff] [blame] | 589 | if (attr == &sysfs_errors) |
| 590 | return bch_snprint_string_list(buf, PAGE_SIZE, error_actions, |
| 591 | c->on_error); |
| 592 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 593 | /* See count_io_errors for why 88 */ |
| 594 | sysfs_print(io_error_halflife, c->error_decay * 88); |
Coly Li | 7ba0d83 | 2018-02-07 11:41:42 -0800 | [diff] [blame] | 595 | sysfs_print(io_error_limit, c->error_limit); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 596 | |
| 597 | sysfs_hprint(congested, |
| 598 | ((uint64_t) bch_get_congested(c)) << 9); |
| 599 | sysfs_print(congested_read_threshold_us, |
| 600 | c->congested_read_threshold_us); |
| 601 | sysfs_print(congested_write_threshold_us, |
| 602 | c->congested_write_threshold_us); |
| 603 | |
| 604 | sysfs_print(active_journal_entries, fifo_used(&c->journal.pin)); |
| 605 | sysfs_printf(verify, "%i", c->verify); |
| 606 | sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled); |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 607 | sysfs_printf(expensive_debug_checks, |
| 608 | "%i", c->expensive_debug_checks); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 609 | sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite); |
| 610 | sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled); |
| 611 | sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled); |
Coly Li | 771f393 | 2018-03-18 17:36:17 -0700 | [diff] [blame] | 612 | sysfs_printf(io_disable, "%i", |
| 613 | test_bit(CACHE_SET_IO_DISABLE, &c->flags)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 614 | |
| 615 | if (attr == &sysfs_bset_tree_stats) |
| 616 | return bch_bset_print_stats(c, buf); |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | SHOW_LOCKED(bch_cache_set) |
| 621 | |
| 622 | STORE(__bch_cache_set) |
| 623 | { |
| 624 | struct cache_set *c = container_of(kobj, struct cache_set, kobj); |
| 625 | |
| 626 | if (attr == &sysfs_unregister) |
| 627 | bch_cache_set_unregister(c); |
| 628 | |
| 629 | if (attr == &sysfs_stop) |
| 630 | bch_cache_set_stop(c); |
| 631 | |
| 632 | if (attr == &sysfs_synchronous) { |
| 633 | bool sync = strtoul_or_return(buf); |
| 634 | |
| 635 | if (sync != CACHE_SYNC(&c->sb)) { |
| 636 | SET_CACHE_SYNC(&c->sb, sync); |
| 637 | bcache_write_super(c); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (attr == &sysfs_flash_vol_create) { |
| 642 | int r; |
| 643 | uint64_t v; |
| 644 | strtoi_h_or_return(buf, v); |
| 645 | |
| 646 | r = bch_flash_dev_create(c, v); |
| 647 | if (r) |
| 648 | return r; |
| 649 | } |
| 650 | |
| 651 | if (attr == &sysfs_clear_stats) { |
| 652 | atomic_long_set(&c->writeback_keys_done, 0); |
| 653 | atomic_long_set(&c->writeback_keys_failed, 0); |
| 654 | |
| 655 | memset(&c->gc_stats, 0, sizeof(struct gc_stat)); |
| 656 | bch_cache_accounting_clear(&c->accounting); |
| 657 | } |
| 658 | |
Tang Junhui | 0b43f49 | 2017-09-06 14:25:55 +0800 | [diff] [blame] | 659 | if (attr == &sysfs_trigger_gc) { |
| 660 | /* |
| 661 | * Garbage collection thread only works when sectors_to_gc < 0, |
| 662 | * when users write to sysfs entry trigger_gc, most of time |
| 663 | * they want to forcibly triger gargage collection. Here -1 is |
| 664 | * set to c->sectors_to_gc, to make gc_should_run() give a |
| 665 | * chance to permit gc thread to run. "give a chance" means |
| 666 | * before going into gc_should_run(), there is still chance |
| 667 | * that c->sectors_to_gc being set to other positive value. So |
| 668 | * writing sysfs entry trigger_gc won't always make sure gc |
| 669 | * thread takes effect. |
| 670 | */ |
| 671 | atomic_set(&c->sectors_to_gc, -1); |
Kent Overstreet | 72a4451 | 2013-10-24 17:19:26 -0700 | [diff] [blame] | 672 | wake_up_gc(c); |
Tang Junhui | 0b43f49 | 2017-09-06 14:25:55 +0800 | [diff] [blame] | 673 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 674 | |
| 675 | if (attr == &sysfs_prune_cache) { |
| 676 | struct shrink_control sc; |
| 677 | sc.gfp_mask = GFP_KERNEL; |
| 678 | sc.nr_to_scan = strtoul_or_return(buf); |
Dave Chinner | 7dc19d5 | 2013-08-28 10:18:11 +1000 | [diff] [blame] | 679 | c->shrink.scan_objects(&c->shrink, &sc); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | sysfs_strtoul(congested_read_threshold_us, |
| 683 | c->congested_read_threshold_us); |
| 684 | sysfs_strtoul(congested_write_threshold_us, |
| 685 | c->congested_write_threshold_us); |
| 686 | |
Kent Overstreet | 77c320e | 2013-07-11 19:42:51 -0700 | [diff] [blame] | 687 | if (attr == &sysfs_errors) { |
| 688 | ssize_t v = bch_read_string_list(buf, error_actions); |
| 689 | |
| 690 | if (v < 0) |
| 691 | return v; |
| 692 | |
| 693 | c->on_error = v; |
| 694 | } |
| 695 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 696 | if (attr == &sysfs_io_error_limit) |
Coly Li | 7ba0d83 | 2018-02-07 11:41:42 -0800 | [diff] [blame] | 697 | c->error_limit = strtoul_or_return(buf); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 698 | |
| 699 | /* See count_io_errors() for why 88 */ |
| 700 | if (attr == &sysfs_io_error_halflife) |
| 701 | c->error_decay = strtoul_or_return(buf) / 88; |
| 702 | |
Coly Li | 771f393 | 2018-03-18 17:36:17 -0700 | [diff] [blame] | 703 | if (attr == &sysfs_io_disable) { |
| 704 | int v = strtoul_or_return(buf); |
| 705 | |
| 706 | if (v) { |
| 707 | if (test_and_set_bit(CACHE_SET_IO_DISABLE, |
| 708 | &c->flags)) |
| 709 | pr_warn("CACHE_SET_IO_DISABLE already set"); |
| 710 | } else { |
| 711 | if (!test_and_clear_bit(CACHE_SET_IO_DISABLE, |
| 712 | &c->flags)) |
| 713 | pr_warn("CACHE_SET_IO_DISABLE already cleared"); |
| 714 | } |
| 715 | } |
| 716 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 717 | sysfs_strtoul(journal_delay_ms, c->journal_delay_ms); |
| 718 | sysfs_strtoul(verify, c->verify); |
| 719 | sysfs_strtoul(key_merging_disabled, c->key_merging_disabled); |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 720 | sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 721 | sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite); |
| 722 | sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled); |
| 723 | sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled); |
| 724 | |
| 725 | return size; |
| 726 | } |
| 727 | STORE_LOCKED(bch_cache_set) |
| 728 | |
| 729 | SHOW(bch_cache_set_internal) |
| 730 | { |
| 731 | struct cache_set *c = container_of(kobj, struct cache_set, internal); |
| 732 | return bch_cache_set_show(&c->kobj, attr, buf); |
| 733 | } |
| 734 | |
| 735 | STORE(bch_cache_set_internal) |
| 736 | { |
| 737 | struct cache_set *c = container_of(kobj, struct cache_set, internal); |
| 738 | return bch_cache_set_store(&c->kobj, attr, buf, size); |
| 739 | } |
| 740 | |
| 741 | static void bch_cache_set_internal_release(struct kobject *k) |
| 742 | { |
| 743 | } |
| 744 | |
| 745 | static struct attribute *bch_cache_set_files[] = { |
| 746 | &sysfs_unregister, |
| 747 | &sysfs_stop, |
| 748 | &sysfs_synchronous, |
| 749 | &sysfs_journal_delay_ms, |
| 750 | &sysfs_flash_vol_create, |
| 751 | |
| 752 | &sysfs_bucket_size, |
| 753 | &sysfs_block_size, |
| 754 | &sysfs_tree_depth, |
| 755 | &sysfs_root_usage_percent, |
| 756 | &sysfs_btree_cache_size, |
| 757 | &sysfs_cache_available_percent, |
| 758 | |
| 759 | &sysfs_average_key_size, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 760 | |
Kent Overstreet | 77c320e | 2013-07-11 19:42:51 -0700 | [diff] [blame] | 761 | &sysfs_errors, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 762 | &sysfs_io_error_limit, |
| 763 | &sysfs_io_error_halflife, |
| 764 | &sysfs_congested, |
| 765 | &sysfs_congested_read_threshold_us, |
| 766 | &sysfs_congested_write_threshold_us, |
| 767 | &sysfs_clear_stats, |
| 768 | NULL |
| 769 | }; |
| 770 | KTYPE(bch_cache_set); |
| 771 | |
| 772 | static struct attribute *bch_cache_set_internal_files[] = { |
| 773 | &sysfs_active_journal_entries, |
| 774 | |
| 775 | sysfs_time_stats_attribute_list(btree_gc, sec, ms) |
| 776 | sysfs_time_stats_attribute_list(btree_split, sec, us) |
| 777 | sysfs_time_stats_attribute_list(btree_sort, ms, us) |
| 778 | sysfs_time_stats_attribute_list(btree_read, ms, us) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 779 | |
| 780 | &sysfs_btree_nodes, |
| 781 | &sysfs_btree_used_percent, |
| 782 | &sysfs_btree_cache_max_chain, |
| 783 | |
| 784 | &sysfs_bset_tree_stats, |
| 785 | &sysfs_cache_read_races, |
Tang Junhui | a728eac | 2018-02-07 11:41:39 -0800 | [diff] [blame] | 786 | &sysfs_reclaim, |
| 787 | &sysfs_flush_write, |
| 788 | &sysfs_retry_flush_write, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 789 | &sysfs_writeback_keys_done, |
| 790 | &sysfs_writeback_keys_failed, |
| 791 | |
| 792 | &sysfs_trigger_gc, |
| 793 | &sysfs_prune_cache, |
| 794 | #ifdef CONFIG_BCACHE_DEBUG |
| 795 | &sysfs_verify, |
| 796 | &sysfs_key_merging_disabled, |
Kent Overstreet | 280481d | 2013-10-24 16:36:03 -0700 | [diff] [blame] | 797 | &sysfs_expensive_debug_checks, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 798 | #endif |
| 799 | &sysfs_gc_always_rewrite, |
| 800 | &sysfs_btree_shrinker_disabled, |
| 801 | &sysfs_copy_gc_enabled, |
Coly Li | 771f393 | 2018-03-18 17:36:17 -0700 | [diff] [blame] | 802 | &sysfs_io_disable, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 803 | NULL |
| 804 | }; |
| 805 | KTYPE(bch_cache_set_internal); |
| 806 | |
Peter Foley | 58f913d | 2017-10-13 16:35:28 -0700 | [diff] [blame] | 807 | static int __bch_cache_cmp(const void *l, const void *r) |
| 808 | { |
| 809 | return *((uint16_t *)r) - *((uint16_t *)l); |
| 810 | } |
| 811 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 812 | SHOW(__bch_cache) |
| 813 | { |
| 814 | struct cache *ca = container_of(kobj, struct cache, kobj); |
| 815 | |
| 816 | sysfs_hprint(bucket_size, bucket_bytes(ca)); |
| 817 | sysfs_hprint(block_size, block_bytes(ca)); |
| 818 | sysfs_print(nbuckets, ca->sb.nbuckets); |
| 819 | sysfs_print(discard, ca->discard); |
| 820 | sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9); |
| 821 | sysfs_hprint(btree_written, |
| 822 | atomic_long_read(&ca->btree_sectors_written) << 9); |
| 823 | sysfs_hprint(metadata_written, |
| 824 | (atomic_long_read(&ca->meta_sectors_written) + |
| 825 | atomic_long_read(&ca->btree_sectors_written)) << 9); |
| 826 | |
| 827 | sysfs_print(io_errors, |
| 828 | atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT); |
| 829 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 830 | if (attr == &sysfs_cache_replacement_policy) |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 831 | return bch_snprint_string_list(buf, PAGE_SIZE, |
| 832 | cache_replacement_policies, |
| 833 | CACHE_REPLACEMENT(&ca->sb)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 834 | |
| 835 | if (attr == &sysfs_priority_stats) { |
Kent Overstreet | 1575402 | 2014-02-25 17:34:21 -0800 | [diff] [blame] | 836 | struct bucket *b; |
| 837 | size_t n = ca->sb.nbuckets, i; |
| 838 | size_t unused = 0, available = 0, dirty = 0, meta = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 839 | uint64_t sum = 0; |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame] | 840 | /* Compute 31 quantiles */ |
| 841 | uint16_t q[31], *p, *cached; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 842 | ssize_t ret; |
| 843 | |
| 844 | cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t)); |
| 845 | if (!p) |
| 846 | return -ENOMEM; |
| 847 | |
| 848 | mutex_lock(&ca->set->bucket_lock); |
Kent Overstreet | 1575402 | 2014-02-25 17:34:21 -0800 | [diff] [blame] | 849 | for_each_bucket(b, ca) { |
| 850 | if (!GC_SECTORS_USED(b)) |
| 851 | unused++; |
| 852 | if (GC_MARK(b) == GC_MARK_RECLAIMABLE) |
| 853 | available++; |
| 854 | if (GC_MARK(b) == GC_MARK_DIRTY) |
| 855 | dirty++; |
| 856 | if (GC_MARK(b) == GC_MARK_METADATA) |
| 857 | meta++; |
| 858 | } |
| 859 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 860 | for (i = ca->sb.first_bucket; i < n; i++) |
| 861 | p[i] = ca->buckets[i].prio; |
| 862 | mutex_unlock(&ca->set->bucket_lock); |
| 863 | |
Peter Foley | 58f913d | 2017-10-13 16:35:28 -0700 | [diff] [blame] | 864 | sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 865 | |
| 866 | while (n && |
| 867 | !cached[n - 1]) |
| 868 | --n; |
| 869 | |
| 870 | unused = ca->sb.nbuckets - n; |
| 871 | |
| 872 | while (cached < p + n && |
| 873 | *cached == BTREE_PRIO) |
Kent Overstreet | 1575402 | 2014-02-25 17:34:21 -0800 | [diff] [blame] | 874 | cached++, n--; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 875 | |
| 876 | for (i = 0; i < n; i++) |
| 877 | sum += INITIAL_PRIO - cached[i]; |
| 878 | |
| 879 | if (n) |
| 880 | do_div(sum, n); |
| 881 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame] | 882 | for (i = 0; i < ARRAY_SIZE(q); i++) |
| 883 | q[i] = INITIAL_PRIO - cached[n * (i + 1) / |
| 884 | (ARRAY_SIZE(q) + 1)]; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 885 | |
| 886 | vfree(p); |
| 887 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame] | 888 | ret = scnprintf(buf, PAGE_SIZE, |
| 889 | "Unused: %zu%%\n" |
Kent Overstreet | 1575402 | 2014-02-25 17:34:21 -0800 | [diff] [blame] | 890 | "Clean: %zu%%\n" |
| 891 | "Dirty: %zu%%\n" |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame] | 892 | "Metadata: %zu%%\n" |
| 893 | "Average: %llu\n" |
| 894 | "Sectors per Q: %zu\n" |
| 895 | "Quantiles: [", |
| 896 | unused * 100 / (size_t) ca->sb.nbuckets, |
Kent Overstreet | 1575402 | 2014-02-25 17:34:21 -0800 | [diff] [blame] | 897 | available * 100 / (size_t) ca->sb.nbuckets, |
| 898 | dirty * 100 / (size_t) ca->sb.nbuckets, |
| 899 | meta * 100 / (size_t) ca->sb.nbuckets, sum, |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame] | 900 | n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 901 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame] | 902 | for (i = 0; i < ARRAY_SIZE(q); i++) |
| 903 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, |
| 904 | "%u ", q[i]); |
| 905 | ret--; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 906 | |
Kent Overstreet | bbc77aa | 2013-05-28 21:53:19 -0700 | [diff] [blame] | 907 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n"); |
| 908 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | SHOW_LOCKED(bch_cache) |
| 915 | |
| 916 | STORE(__bch_cache) |
| 917 | { |
| 918 | struct cache *ca = container_of(kobj, struct cache, kobj); |
| 919 | |
| 920 | if (attr == &sysfs_discard) { |
| 921 | bool v = strtoul_or_return(buf); |
| 922 | |
| 923 | if (blk_queue_discard(bdev_get_queue(ca->bdev))) |
| 924 | ca->discard = v; |
| 925 | |
| 926 | if (v != CACHE_DISCARD(&ca->sb)) { |
| 927 | SET_CACHE_DISCARD(&ca->sb, v); |
| 928 | bcache_write_super(ca->set); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | if (attr == &sysfs_cache_replacement_policy) { |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 933 | ssize_t v = bch_read_string_list(buf, cache_replacement_policies); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 934 | |
| 935 | if (v < 0) |
| 936 | return v; |
| 937 | |
| 938 | if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) { |
| 939 | mutex_lock(&ca->set->bucket_lock); |
| 940 | SET_CACHE_REPLACEMENT(&ca->sb, v); |
| 941 | mutex_unlock(&ca->set->bucket_lock); |
| 942 | |
| 943 | bcache_write_super(ca->set); |
| 944 | } |
| 945 | } |
| 946 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 947 | if (attr == &sysfs_clear_stats) { |
| 948 | atomic_long_set(&ca->sectors_written, 0); |
| 949 | atomic_long_set(&ca->btree_sectors_written, 0); |
| 950 | atomic_long_set(&ca->meta_sectors_written, 0); |
| 951 | atomic_set(&ca->io_count, 0); |
| 952 | atomic_set(&ca->io_errors, 0); |
| 953 | } |
| 954 | |
| 955 | return size; |
| 956 | } |
| 957 | STORE_LOCKED(bch_cache) |
| 958 | |
| 959 | static struct attribute *bch_cache_files[] = { |
| 960 | &sysfs_bucket_size, |
| 961 | &sysfs_block_size, |
| 962 | &sysfs_nbuckets, |
| 963 | &sysfs_priority_stats, |
| 964 | &sysfs_discard, |
| 965 | &sysfs_written, |
| 966 | &sysfs_btree_written, |
| 967 | &sysfs_metadata_written, |
| 968 | &sysfs_io_errors, |
| 969 | &sysfs_clear_stats, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 970 | &sysfs_cache_replacement_policy, |
| 971 | NULL |
| 972 | }; |
| 973 | KTYPE(bch_cache); |