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