blob: 8ccbc8f3b3af7ff805398fe3ea7ff719f46ec20f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kent Overstreetcafe5632013-03-23 16:11:31 -07002/*
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 Overstreet279afba2013-06-05 06:21:07 -070013#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
Kent Overstreetc37511b2013-04-26 15:39:55 -070015#include <linux/blkdev.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070016#include <linux/sort.h>
Ingo Molnare6017572017-02-01 16:36:40 +010017#include <linux/sched/clock.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070018
Andy Shevchenko04cbc212018-05-28 15:37:42 +080019/* Default is -1; we skip past it for struct cached_dev's cache mode */
20static const char * const bch_cache_modes[] = {
Andy Shevchenko04cbc212018-05-28 15:37:42 +080021 "writethrough",
22 "writeback",
23 "writearound",
24 "none",
25 NULL
26};
27
28/* Default is -1; we skip past it for stop_when_cache_set_failed */
29static const char * const bch_stop_on_failure_modes[] = {
Andy Shevchenko04cbc212018-05-28 15:37:42 +080030 "auto",
31 "always",
32 NULL
33};
34
Kent Overstreetcafe5632013-03-23 16:11:31 -070035static const char * const cache_replacement_policies[] = {
36 "lru",
37 "fifo",
38 "random",
39 NULL
40};
41
Kent Overstreet77c320e2013-07-11 19:42:51 -070042static const char * const error_actions[] = {
43 "unregister",
44 "panic",
45 NULL
46};
47
Kent Overstreetcafe5632013-03-23 16:11:31 -070048write_attribute(attach);
49write_attribute(detach);
50write_attribute(unregister);
51write_attribute(stop);
52write_attribute(clear_stats);
53write_attribute(trigger_gc);
54write_attribute(prune_cache);
55write_attribute(flash_vol_create);
56
57read_attribute(bucket_size);
58read_attribute(block_size);
59read_attribute(nbuckets);
60read_attribute(tree_depth);
61read_attribute(root_usage_percent);
62read_attribute(priority_stats);
63read_attribute(btree_cache_size);
64read_attribute(btree_cache_max_chain);
65read_attribute(cache_available_percent);
66read_attribute(written);
67read_attribute(btree_written);
68read_attribute(metadata_written);
69read_attribute(active_journal_entries);
70
71sysfs_time_stats_attribute(btree_gc, sec, ms);
72sysfs_time_stats_attribute(btree_split, sec, us);
73sysfs_time_stats_attribute(btree_sort, ms, us);
74sysfs_time_stats_attribute(btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -070075
76read_attribute(btree_nodes);
77read_attribute(btree_used_percent);
78read_attribute(average_key_size);
79read_attribute(dirty_data);
80read_attribute(bset_tree_stats);
81
82read_attribute(state);
83read_attribute(cache_read_races);
Tang Junhuia728eac2018-02-07 11:41:39 -080084read_attribute(reclaim);
85read_attribute(flush_write);
86read_attribute(retry_flush_write);
Kent Overstreetcafe5632013-03-23 16:11:31 -070087read_attribute(writeback_keys_done);
88read_attribute(writeback_keys_failed);
89read_attribute(io_errors);
90read_attribute(congested);
91rw_attribute(congested_read_threshold_us);
92rw_attribute(congested_write_threshold_us);
93
94rw_attribute(sequential_cutoff);
Kent Overstreetcafe5632013-03-23 16:11:31 -070095rw_attribute(data_csum);
96rw_attribute(cache_mode);
Coly Li7e027ca2018-03-18 17:36:18 -070097rw_attribute(stop_when_cache_set_failed);
Kent Overstreetcafe5632013-03-23 16:11:31 -070098rw_attribute(writeback_metadata);
99rw_attribute(writeback_running);
100rw_attribute(writeback_percent);
101rw_attribute(writeback_delay);
102rw_attribute(writeback_rate);
103
104rw_attribute(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700105rw_attribute(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700106rw_attribute(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -0700107rw_attribute(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700108read_attribute(writeback_rate_debug);
109
Kent Overstreet72c27062013-06-05 06:24:39 -0700110read_attribute(stripe_size);
111read_attribute(partial_stripes_expensive);
112
Kent Overstreetcafe5632013-03-23 16:11:31 -0700113rw_attribute(synchronous);
114rw_attribute(journal_delay_ms);
Coly Li771f3932018-03-18 17:36:17 -0700115rw_attribute(io_disable);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700116rw_attribute(discard);
117rw_attribute(running);
118rw_attribute(label);
119rw_attribute(readahead);
Kent Overstreet77c320e2013-07-11 19:42:51 -0700120rw_attribute(errors);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121rw_attribute(io_error_limit);
122rw_attribute(io_error_halflife);
123rw_attribute(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700124rw_attribute(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125rw_attribute(key_merging_disabled);
126rw_attribute(gc_always_rewrite);
Kent Overstreet280481d2013-10-24 16:36:03 -0700127rw_attribute(expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700128rw_attribute(cache_replacement_policy);
129rw_attribute(btree_shrinker_disabled);
130rw_attribute(copy_gc_enabled);
131rw_attribute(size);
132
Andy Shevchenkoecb37ce2018-05-28 15:37:43 +0800133static ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
134 size_t selected)
135{
136 char *out = buf;
137 size_t i;
138
139 for (i = 0; list[i]; i++)
140 out += snprintf(out, buf + size - out,
141 i == selected ? "[%s] " : "%s ", list[i]);
142
143 out[-1] = '\n';
144 return out - buf;
145}
146
Kent Overstreetcafe5632013-03-23 16:11:31 -0700147SHOW(__bch_cached_dev)
148{
149 struct cached_dev *dc = container_of(kobj, struct cached_dev,
150 disk.kobj);
151 const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
152
153#define var(stat) (dc->stat)
154
155 if (attr == &sysfs_cache_mode)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600156 return bch_snprint_string_list(buf, PAGE_SIZE,
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800157 bch_cache_modes,
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600158 BDEV_CACHE_MODE(&dc->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700159
Coly Li7e027ca2018-03-18 17:36:18 -0700160 if (attr == &sysfs_stop_when_cache_set_failed)
161 return bch_snprint_string_list(buf, PAGE_SIZE,
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800162 bch_stop_on_failure_modes,
Coly Li7e027ca2018-03-18 17:36:18 -0700163 dc->stop_when_cache_set_failed);
164
165
Kent Overstreetcafe5632013-03-23 16:11:31 -0700166 sysfs_printf(data_csum, "%i", dc->disk.data_csum);
167 var_printf(verify, "%i");
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700168 var_printf(bypass_torture_test, "%i");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700169 var_printf(writeback_metadata, "%i");
170 var_printf(writeback_running, "%i");
171 var_print(writeback_delay);
172 var_print(writeback_percent);
Kent Overstreet16749c22013-11-11 13:58:34 -0800173 sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
Coly Lic7b7bd02018-03-18 17:36:25 -0700174 sysfs_hprint(io_errors, atomic_read(&dc->io_errors));
175 sysfs_printf(io_error_limit, "%i", dc->error_limit);
176 sysfs_printf(io_disable, "%i", dc->io_disable);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700177 var_print(writeback_rate_update_seconds);
Michael Lyle1d316e62017-10-13 16:35:36 -0700178 var_print(writeback_rate_i_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700179 var_print(writeback_rate_p_term_inverse);
Michael Lyle1d316e62017-10-13 16:35:36 -0700180 var_print(writeback_rate_minimum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181
182 if (attr == &sysfs_writeback_rate_debug) {
Kent Overstreet16749c22013-11-11 13:58:34 -0800183 char rate[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700184 char dirty[20];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700185 char target[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800186 char proportional[20];
Michael Lyle1d316e62017-10-13 16:35:36 -0700187 char integral[20];
Kent Overstreet16749c22013-11-11 13:58:34 -0800188 char change[20];
189 s64 next_io;
190
191 bch_hprint(rate, dc->writeback_rate.rate << 9);
192 bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600193 bch_hprint(target, dc->writeback_rate_target << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800194 bch_hprint(proportional,dc->writeback_rate_proportional << 9);
Michael Lyle1d316e62017-10-13 16:35:36 -0700195 bch_hprint(integral, dc->writeback_rate_integral_scaled << 9);
Kent Overstreet16749c22013-11-11 13:58:34 -0800196 bch_hprint(change, dc->writeback_rate_change << 9);
197
198 next_io = div64_s64(dc->writeback_rate.next - local_clock(),
199 NSEC_PER_MSEC);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700200
201 return sprintf(buf,
Kent Overstreet16749c22013-11-11 13:58:34 -0800202 "rate:\t\t%s/sec\n"
Kent Overstreetcafe5632013-03-23 16:11:31 -0700203 "dirty:\t\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800204 "target:\t\t%s\n"
205 "proportional:\t%s\n"
Michael Lyle1d316e62017-10-13 16:35:36 -0700206 "integral:\t%s\n"
Kent Overstreet16749c22013-11-11 13:58:34 -0800207 "change:\t\t%s/sec\n"
208 "next io:\t%llims\n",
209 rate, dirty, target, proportional,
Michael Lyle1d316e62017-10-13 16:35:36 -0700210 integral, change, next_io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700211 }
212
213 sysfs_hprint(dirty_data,
Kent Overstreet279afba2013-06-05 06:21:07 -0700214 bcache_dev_sectors_dirty(&dc->disk) << 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700215
Tang Junhui688892b2018-03-18 17:36:20 -0700216 sysfs_hprint(stripe_size, ((uint64_t)dc->disk.stripe_size) << 9);
Kent Overstreet72c27062013-06-05 06:24:39 -0700217 var_printf(partial_stripes_expensive, "%u");
218
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219 var_hprint(sequential_cutoff);
220 var_hprint(readahead);
221
222 sysfs_print(running, atomic_read(&dc->running));
223 sysfs_print(state, states[BDEV_STATE(&dc->sb)]);
224
225 if (attr == &sysfs_label) {
226 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
227 buf[SB_LABEL_SIZE + 1] = '\0';
228 strcat(buf, "\n");
229 return strlen(buf);
230 }
231
232#undef var
233 return 0;
234}
235SHOW_LOCKED(bch_cached_dev)
236
237STORE(__cached_dev)
238{
239 struct cached_dev *dc = container_of(kobj, struct cached_dev,
240 disk.kobj);
Tang Junhui7f4fc932018-02-07 11:41:45 -0800241 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700242 struct cache_set *c;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200243 struct kobj_uevent_env *env;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700244
245#define d_strtoul(var) sysfs_strtoul(var, dc->var)
Kent Overstreet16749c22013-11-11 13:58:34 -0800246#define d_strtoul_nonzero(var) sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700247#define d_strtoi_h(var) sysfs_hatoi(var, dc->var)
248
249 sysfs_strtoul(data_csum, dc->disk.data_csum);
250 d_strtoul(verify);
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700251 d_strtoul(bypass_torture_test);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700252 d_strtoul(writeback_metadata);
253 d_strtoul(writeback_running);
254 d_strtoul(writeback_delay);
Kent Overstreet16749c22013-11-11 13:58:34 -0800255
Kent Overstreetcafe5632013-03-23 16:11:31 -0700256 sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
257
Kent Overstreet16749c22013-11-11 13:58:34 -0800258 sysfs_strtoul_clamp(writeback_rate,
259 dc->writeback_rate.rate, 1, INT_MAX);
260
Coly Li7a5e3ec2018-02-07 11:41:44 -0800261 sysfs_strtoul_clamp(writeback_rate_update_seconds,
262 dc->writeback_rate_update_seconds,
263 1, WRITEBACK_RATE_UPDATE_SECS_MAX);
Michael Lyle1d316e62017-10-13 16:35:36 -0700264 d_strtoul(writeback_rate_i_term_inverse);
Kent Overstreet16749c22013-11-11 13:58:34 -0800265 d_strtoul_nonzero(writeback_rate_p_term_inverse);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700266
Coly Lic7b7bd02018-03-18 17:36:25 -0700267 sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
268
269 if (attr == &sysfs_io_disable) {
270 int v = strtoul_or_return(buf);
271
272 dc->io_disable = v ? 1 : 0;
273 }
274
Kent Overstreetcafe5632013-03-23 16:11:31 -0700275 d_strtoi_h(sequential_cutoff);
276 d_strtoi_h(readahead);
277
278 if (attr == &sysfs_clear_stats)
279 bch_cache_accounting_clear(&dc->accounting);
280
281 if (attr == &sysfs_running &&
282 strtoul_or_return(buf))
283 bch_cached_dev_run(dc);
284
285 if (attr == &sysfs_cache_mode) {
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800286 v = __sysfs_match_string(bch_cache_modes, -1, buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700287 if (v < 0)
288 return v;
289
290 if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
291 SET_BDEV_CACHE_MODE(&dc->sb, v);
292 bch_write_bdev_super(dc, NULL);
293 }
294 }
295
Coly Li7e027ca2018-03-18 17:36:18 -0700296 if (attr == &sysfs_stop_when_cache_set_failed) {
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800297 v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
Coly Li7e027ca2018-03-18 17:36:18 -0700298 if (v < 0)
299 return v;
300
301 dc->stop_when_cache_set_failed = v;
302 }
303
Kent Overstreetcafe5632013-03-23 16:11:31 -0700304 if (attr == &sysfs_label) {
Gabriel de Perthuisaee6f1c2013-09-23 23:17:28 -0700305 if (size > SB_LABEL_SIZE)
306 return -EINVAL;
307 memcpy(dc->sb.label, buf, size);
308 if (size < SB_LABEL_SIZE)
309 dc->sb.label[size] = '\0';
310 if (size && dc->sb.label[size - 1] == '\n')
311 dc->sb.label[size - 1] = '\0';
Kent Overstreetcafe5632013-03-23 16:11:31 -0700312 bch_write_bdev_super(dc, NULL);
313 if (dc->disk.c) {
314 memcpy(dc->disk.c->uuids[dc->disk.id].label,
315 buf, SB_LABEL_SIZE);
316 bch_uuid_write(dc->disk.c);
317 }
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200318 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
Dan Carpenterd2a65ce2013-07-05 09:05:46 +0300319 if (!env)
320 return -ENOMEM;
Gabriel de Perthuisab9e1402013-06-09 00:54:48 +0200321 add_uevent_var(env, "DRIVER=bcache");
322 add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
323 add_uevent_var(env, "CACHED_LABEL=%s", buf);
324 kobject_uevent_env(
325 &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
326 kfree(env);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700327 }
328
329 if (attr == &sysfs_attach) {
Tang Junhui73ac1052018-02-07 11:41:46 -0800330 uint8_t set_uuid[16];
331
332 if (bch_parse_uuid(buf, set_uuid) < 16)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700333 return -EINVAL;
334
Tang Junhui7f4fc932018-02-07 11:41:45 -0800335 v = -ENOENT;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700336 list_for_each_entry(c, &bch_cache_sets, list) {
Tang Junhui73ac1052018-02-07 11:41:46 -0800337 v = bch_cached_dev_attach(dc, c, set_uuid);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700338 if (!v)
339 return size;
340 }
341
342 pr_err("Can't attach %s: cache set not found", buf);
Tang Junhui7f4fc932018-02-07 11:41:45 -0800343 return v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700344 }
345
346 if (attr == &sysfs_detach && dc->disk.c)
347 bch_cached_dev_detach(dc);
348
349 if (attr == &sysfs_stop)
350 bcache_device_stop(&dc->disk);
351
352 return size;
353}
354
355STORE(bch_cached_dev)
356{
357 struct cached_dev *dc = container_of(kobj, struct cached_dev,
358 disk.kobj);
359
360 mutex_lock(&bch_register_lock);
361 size = __cached_dev_store(kobj, attr, buf, size);
362
363 if (attr == &sysfs_writeback_running)
364 bch_writeback_queue(dc);
365
366 if (attr == &sysfs_writeback_percent)
Coly Li3fd47bf2018-03-18 17:36:16 -0700367 if (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
368 schedule_delayed_work(&dc->writeback_rate_update,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700369 dc->writeback_rate_update_seconds * HZ);
370
371 mutex_unlock(&bch_register_lock);
372 return size;
373}
374
375static struct attribute *bch_cached_dev_files[] = {
376 &sysfs_attach,
377 &sysfs_detach,
378 &sysfs_stop,
379#if 0
380 &sysfs_data_csum,
381#endif
382 &sysfs_cache_mode,
Coly Li7e027ca2018-03-18 17:36:18 -0700383 &sysfs_stop_when_cache_set_failed,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700384 &sysfs_writeback_metadata,
385 &sysfs_writeback_running,
386 &sysfs_writeback_delay,
387 &sysfs_writeback_percent,
388 &sysfs_writeback_rate,
389 &sysfs_writeback_rate_update_seconds,
Michael Lyle1d316e62017-10-13 16:35:36 -0700390 &sysfs_writeback_rate_i_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700391 &sysfs_writeback_rate_p_term_inverse,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700392 &sysfs_writeback_rate_debug,
Coly Lic7b7bd02018-03-18 17:36:25 -0700393 &sysfs_errors,
394 &sysfs_io_error_limit,
395 &sysfs_io_disable,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700396 &sysfs_dirty_data,
Kent Overstreet72c27062013-06-05 06:24:39 -0700397 &sysfs_stripe_size,
398 &sysfs_partial_stripes_expensive,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700399 &sysfs_sequential_cutoff,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700400 &sysfs_clear_stats,
401 &sysfs_running,
402 &sysfs_state,
403 &sysfs_label,
404 &sysfs_readahead,
405#ifdef CONFIG_BCACHE_DEBUG
406 &sysfs_verify,
Kent Overstreet5ceaaad2013-09-10 14:27:42 -0700407 &sysfs_bypass_torture_test,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700408#endif
409 NULL
410};
411KTYPE(bch_cached_dev);
412
413SHOW(bch_flash_dev)
414{
415 struct bcache_device *d = container_of(kobj, struct bcache_device,
416 kobj);
417 struct uuid_entry *u = &d->c->uuids[d->id];
418
419 sysfs_printf(data_csum, "%i", d->data_csum);
420 sysfs_hprint(size, u->sectors << 9);
421
422 if (attr == &sysfs_label) {
423 memcpy(buf, u->label, SB_LABEL_SIZE);
424 buf[SB_LABEL_SIZE + 1] = '\0';
425 strcat(buf, "\n");
426 return strlen(buf);
427 }
428
429 return 0;
430}
431
432STORE(__bch_flash_dev)
433{
434 struct bcache_device *d = container_of(kobj, struct bcache_device,
435 kobj);
436 struct uuid_entry *u = &d->c->uuids[d->id];
437
438 sysfs_strtoul(data_csum, d->data_csum);
439
440 if (attr == &sysfs_size) {
441 uint64_t v;
442 strtoi_h_or_return(buf, v);
443
444 u->sectors = v >> 9;
445 bch_uuid_write(d->c);
446 set_capacity(d->disk, u->sectors);
447 }
448
449 if (attr == &sysfs_label) {
450 memcpy(u->label, buf, SB_LABEL_SIZE);
451 bch_uuid_write(d->c);
452 }
453
454 if (attr == &sysfs_unregister) {
Kent Overstreetc4d951d2013-08-21 17:49:09 -0700455 set_bit(BCACHE_DEV_DETACHING, &d->flags);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700456 bcache_device_stop(d);
457 }
458
459 return size;
460}
461STORE_LOCKED(bch_flash_dev)
462
463static struct attribute *bch_flash_dev_files[] = {
464 &sysfs_unregister,
465#if 0
466 &sysfs_data_csum,
467#endif
468 &sysfs_label,
469 &sysfs_size,
470 NULL
471};
472KTYPE(bch_flash_dev);
473
Kent Overstreetf67342d2013-11-11 19:25:55 -0800474struct bset_stats_op {
475 struct btree_op op;
476 size_t nodes;
477 struct bset_stats stats;
478};
479
John Sheucb851142014-03-17 23:13:56 -0700480static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800481{
482 struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
483
484 op->nodes++;
485 bch_btree_keys_stats(&b->keys, &op->stats);
486
487 return MAP_CONTINUE;
488}
489
Kent Overstreet35723242014-01-10 18:53:02 -0800490static int bch_bset_print_stats(struct cache_set *c, char *buf)
Kent Overstreetf67342d2013-11-11 19:25:55 -0800491{
492 struct bset_stats_op op;
493 int ret;
494
495 memset(&op, 0, sizeof(op));
496 bch_btree_op_init(&op.op, -1);
497
John Sheucb851142014-03-17 23:13:56 -0700498 ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
Kent Overstreetf67342d2013-11-11 19:25:55 -0800499 if (ret < 0)
500 return ret;
501
502 return snprintf(buf, PAGE_SIZE,
503 "btree nodes: %zu\n"
504 "written sets: %zu\n"
505 "unwritten sets: %zu\n"
506 "written key bytes: %zu\n"
507 "unwritten key bytes: %zu\n"
508 "floats: %zu\n"
509 "failed: %zu\n",
510 op.nodes,
511 op.stats.sets_written, op.stats.sets_unwritten,
512 op.stats.bytes_written, op.stats.bytes_unwritten,
513 op.stats.floats, op.stats.failed);
514}
515
John Sheucb851142014-03-17 23:13:56 -0700516static unsigned bch_root_usage(struct cache_set *c)
517{
518 unsigned bytes = 0;
519 struct bkey *k;
520 struct btree *b;
521 struct btree_iter iter;
522
523 goto lock_root;
524
525 do {
526 rw_unlock(false, b);
527lock_root:
528 b = c->root;
529 rw_lock(false, b, b->level);
530 } while (b != c->root);
531
532 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
533 bytes += bkey_bytes(k);
534
535 rw_unlock(false, b);
536
537 return (bytes * 100) / btree_bytes(c);
538}
539
540static size_t bch_cache_size(struct cache_set *c)
541{
542 size_t ret = 0;
543 struct btree *b;
544
545 mutex_lock(&c->bucket_lock);
546 list_for_each_entry(b, &c->btree_cache, list)
547 ret += 1 << (b->keys.page_order + PAGE_SHIFT);
548
549 mutex_unlock(&c->bucket_lock);
550 return ret;
551}
552
553static unsigned bch_cache_max_chain(struct cache_set *c)
554{
555 unsigned ret = 0;
556 struct hlist_head *h;
557
558 mutex_lock(&c->bucket_lock);
559
560 for (h = c->bucket_hash;
561 h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
562 h++) {
563 unsigned i = 0;
564 struct hlist_node *p;
565
566 hlist_for_each(p, h)
567 i++;
568
569 ret = max(ret, i);
570 }
571
572 mutex_unlock(&c->bucket_lock);
573 return ret;
574}
575
576static unsigned bch_btree_used(struct cache_set *c)
577{
578 return div64_u64(c->gc_stats.key_bytes * 100,
579 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
580}
581
582static unsigned bch_average_key_size(struct cache_set *c)
583{
584 return c->gc_stats.nkeys
585 ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
586 : 0;
587}
588
Kent Overstreetcafe5632013-03-23 16:11:31 -0700589SHOW(__bch_cache_set)
590{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700591 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
592
593 sysfs_print(synchronous, CACHE_SYNC(&c->sb));
594 sysfs_print(journal_delay_ms, c->journal_delay_ms);
595 sysfs_hprint(bucket_size, bucket_bytes(c));
596 sysfs_hprint(block_size, block_bytes(c));
597 sysfs_print(tree_depth, c->root->level);
John Sheucb851142014-03-17 23:13:56 -0700598 sysfs_print(root_usage_percent, bch_root_usage(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700599
John Sheucb851142014-03-17 23:13:56 -0700600 sysfs_hprint(btree_cache_size, bch_cache_size(c));
601 sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700602 sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
603
604 sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
605 sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
Kent Overstreet67539e82013-09-10 22:53:34 -0700606 sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700607 sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700608
John Sheucb851142014-03-17 23:13:56 -0700609 sysfs_print(btree_used_percent, bch_btree_used(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700610 sysfs_print(btree_nodes, c->gc_stats.nodes);
John Sheucb851142014-03-17 23:13:56 -0700611 sysfs_hprint(average_key_size, bch_average_key_size(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700612
613 sysfs_print(cache_read_races,
614 atomic_long_read(&c->cache_read_races));
615
Tang Junhuia728eac2018-02-07 11:41:39 -0800616 sysfs_print(reclaim,
617 atomic_long_read(&c->reclaim));
618
619 sysfs_print(flush_write,
620 atomic_long_read(&c->flush_write));
621
622 sysfs_print(retry_flush_write,
623 atomic_long_read(&c->retry_flush_write));
624
Kent Overstreetcafe5632013-03-23 16:11:31 -0700625 sysfs_print(writeback_keys_done,
626 atomic_long_read(&c->writeback_keys_done));
627 sysfs_print(writeback_keys_failed,
628 atomic_long_read(&c->writeback_keys_failed));
629
Kent Overstreet77c320e2013-07-11 19:42:51 -0700630 if (attr == &sysfs_errors)
631 return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
632 c->on_error);
633
Kent Overstreetcafe5632013-03-23 16:11:31 -0700634 /* See count_io_errors for why 88 */
635 sysfs_print(io_error_halflife, c->error_decay * 88);
Coly Li7ba0d832018-02-07 11:41:42 -0800636 sysfs_print(io_error_limit, c->error_limit);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700637
638 sysfs_hprint(congested,
639 ((uint64_t) bch_get_congested(c)) << 9);
640 sysfs_print(congested_read_threshold_us,
641 c->congested_read_threshold_us);
642 sysfs_print(congested_write_threshold_us,
643 c->congested_write_threshold_us);
644
645 sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
646 sysfs_printf(verify, "%i", c->verify);
647 sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700648 sysfs_printf(expensive_debug_checks,
649 "%i", c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700650 sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
651 sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
652 sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
Coly Li771f3932018-03-18 17:36:17 -0700653 sysfs_printf(io_disable, "%i",
654 test_bit(CACHE_SET_IO_DISABLE, &c->flags));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700655
656 if (attr == &sysfs_bset_tree_stats)
657 return bch_bset_print_stats(c, buf);
658
659 return 0;
660}
661SHOW_LOCKED(bch_cache_set)
662
663STORE(__bch_cache_set)
664{
665 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800666 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700667
668 if (attr == &sysfs_unregister)
669 bch_cache_set_unregister(c);
670
671 if (attr == &sysfs_stop)
672 bch_cache_set_stop(c);
673
674 if (attr == &sysfs_synchronous) {
675 bool sync = strtoul_or_return(buf);
676
677 if (sync != CACHE_SYNC(&c->sb)) {
678 SET_CACHE_SYNC(&c->sb, sync);
679 bcache_write_super(c);
680 }
681 }
682
683 if (attr == &sysfs_flash_vol_create) {
684 int r;
685 uint64_t v;
686 strtoi_h_or_return(buf, v);
687
688 r = bch_flash_dev_create(c, v);
689 if (r)
690 return r;
691 }
692
693 if (attr == &sysfs_clear_stats) {
694 atomic_long_set(&c->writeback_keys_done, 0);
695 atomic_long_set(&c->writeback_keys_failed, 0);
696
697 memset(&c->gc_stats, 0, sizeof(struct gc_stat));
698 bch_cache_accounting_clear(&c->accounting);
699 }
700
Tang Junhui0b43f492017-09-06 14:25:55 +0800701 if (attr == &sysfs_trigger_gc) {
702 /*
703 * Garbage collection thread only works when sectors_to_gc < 0,
704 * when users write to sysfs entry trigger_gc, most of time
705 * they want to forcibly triger gargage collection. Here -1 is
706 * set to c->sectors_to_gc, to make gc_should_run() give a
707 * chance to permit gc thread to run. "give a chance" means
708 * before going into gc_should_run(), there is still chance
709 * that c->sectors_to_gc being set to other positive value. So
710 * writing sysfs entry trigger_gc won't always make sure gc
711 * thread takes effect.
712 */
713 atomic_set(&c->sectors_to_gc, -1);
Kent Overstreet72a44512013-10-24 17:19:26 -0700714 wake_up_gc(c);
Tang Junhui0b43f492017-09-06 14:25:55 +0800715 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700716
717 if (attr == &sysfs_prune_cache) {
718 struct shrink_control sc;
719 sc.gfp_mask = GFP_KERNEL;
720 sc.nr_to_scan = strtoul_or_return(buf);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000721 c->shrink.scan_objects(&c->shrink, &sc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700722 }
723
724 sysfs_strtoul(congested_read_threshold_us,
725 c->congested_read_threshold_us);
726 sysfs_strtoul(congested_write_threshold_us,
727 c->congested_write_threshold_us);
728
Kent Overstreet77c320e2013-07-11 19:42:51 -0700729 if (attr == &sysfs_errors) {
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800730 v = __sysfs_match_string(error_actions, -1, buf);
Kent Overstreet77c320e2013-07-11 19:42:51 -0700731 if (v < 0)
732 return v;
733
734 c->on_error = v;
735 }
736
Kent Overstreetcafe5632013-03-23 16:11:31 -0700737 if (attr == &sysfs_io_error_limit)
Coly Li7ba0d832018-02-07 11:41:42 -0800738 c->error_limit = strtoul_or_return(buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700739
740 /* See count_io_errors() for why 88 */
741 if (attr == &sysfs_io_error_halflife)
742 c->error_decay = strtoul_or_return(buf) / 88;
743
Coly Li771f3932018-03-18 17:36:17 -0700744 if (attr == &sysfs_io_disable) {
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800745 v = strtoul_or_return(buf);
Coly Li771f3932018-03-18 17:36:17 -0700746 if (v) {
747 if (test_and_set_bit(CACHE_SET_IO_DISABLE,
748 &c->flags))
749 pr_warn("CACHE_SET_IO_DISABLE already set");
750 } else {
751 if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
752 &c->flags))
753 pr_warn("CACHE_SET_IO_DISABLE already cleared");
754 }
755 }
756
Kent Overstreetcafe5632013-03-23 16:11:31 -0700757 sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
758 sysfs_strtoul(verify, c->verify);
759 sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
Kent Overstreet280481d2013-10-24 16:36:03 -0700760 sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700761 sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
762 sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
763 sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
764
765 return size;
766}
767STORE_LOCKED(bch_cache_set)
768
769SHOW(bch_cache_set_internal)
770{
771 struct cache_set *c = container_of(kobj, struct cache_set, internal);
772 return bch_cache_set_show(&c->kobj, attr, buf);
773}
774
775STORE(bch_cache_set_internal)
776{
777 struct cache_set *c = container_of(kobj, struct cache_set, internal);
778 return bch_cache_set_store(&c->kobj, attr, buf, size);
779}
780
781static void bch_cache_set_internal_release(struct kobject *k)
782{
783}
784
785static struct attribute *bch_cache_set_files[] = {
786 &sysfs_unregister,
787 &sysfs_stop,
788 &sysfs_synchronous,
789 &sysfs_journal_delay_ms,
790 &sysfs_flash_vol_create,
791
792 &sysfs_bucket_size,
793 &sysfs_block_size,
794 &sysfs_tree_depth,
795 &sysfs_root_usage_percent,
796 &sysfs_btree_cache_size,
797 &sysfs_cache_available_percent,
798
799 &sysfs_average_key_size,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700800
Kent Overstreet77c320e2013-07-11 19:42:51 -0700801 &sysfs_errors,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700802 &sysfs_io_error_limit,
803 &sysfs_io_error_halflife,
804 &sysfs_congested,
805 &sysfs_congested_read_threshold_us,
806 &sysfs_congested_write_threshold_us,
807 &sysfs_clear_stats,
808 NULL
809};
810KTYPE(bch_cache_set);
811
812static struct attribute *bch_cache_set_internal_files[] = {
813 &sysfs_active_journal_entries,
814
815 sysfs_time_stats_attribute_list(btree_gc, sec, ms)
816 sysfs_time_stats_attribute_list(btree_split, sec, us)
817 sysfs_time_stats_attribute_list(btree_sort, ms, us)
818 sysfs_time_stats_attribute_list(btree_read, ms, us)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700819
820 &sysfs_btree_nodes,
821 &sysfs_btree_used_percent,
822 &sysfs_btree_cache_max_chain,
823
824 &sysfs_bset_tree_stats,
825 &sysfs_cache_read_races,
Tang Junhuia728eac2018-02-07 11:41:39 -0800826 &sysfs_reclaim,
827 &sysfs_flush_write,
828 &sysfs_retry_flush_write,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700829 &sysfs_writeback_keys_done,
830 &sysfs_writeback_keys_failed,
831
832 &sysfs_trigger_gc,
833 &sysfs_prune_cache,
834#ifdef CONFIG_BCACHE_DEBUG
835 &sysfs_verify,
836 &sysfs_key_merging_disabled,
Kent Overstreet280481d2013-10-24 16:36:03 -0700837 &sysfs_expensive_debug_checks,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700838#endif
839 &sysfs_gc_always_rewrite,
840 &sysfs_btree_shrinker_disabled,
841 &sysfs_copy_gc_enabled,
Coly Li771f3932018-03-18 17:36:17 -0700842 &sysfs_io_disable,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700843 NULL
844};
845KTYPE(bch_cache_set_internal);
846
Peter Foley58f913d2017-10-13 16:35:28 -0700847static int __bch_cache_cmp(const void *l, const void *r)
848{
849 return *((uint16_t *)r) - *((uint16_t *)l);
850}
851
Kent Overstreetcafe5632013-03-23 16:11:31 -0700852SHOW(__bch_cache)
853{
854 struct cache *ca = container_of(kobj, struct cache, kobj);
855
856 sysfs_hprint(bucket_size, bucket_bytes(ca));
857 sysfs_hprint(block_size, block_bytes(ca));
858 sysfs_print(nbuckets, ca->sb.nbuckets);
859 sysfs_print(discard, ca->discard);
860 sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
861 sysfs_hprint(btree_written,
862 atomic_long_read(&ca->btree_sectors_written) << 9);
863 sysfs_hprint(metadata_written,
864 (atomic_long_read(&ca->meta_sectors_written) +
865 atomic_long_read(&ca->btree_sectors_written)) << 9);
866
867 sysfs_print(io_errors,
868 atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
869
Kent Overstreetcafe5632013-03-23 16:11:31 -0700870 if (attr == &sysfs_cache_replacement_policy)
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600871 return bch_snprint_string_list(buf, PAGE_SIZE,
872 cache_replacement_policies,
873 CACHE_REPLACEMENT(&ca->sb));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700874
875 if (attr == &sysfs_priority_stats) {
Kent Overstreet15754022014-02-25 17:34:21 -0800876 struct bucket *b;
877 size_t n = ca->sb.nbuckets, i;
878 size_t unused = 0, available = 0, dirty = 0, meta = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700879 uint64_t sum = 0;
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700880 /* Compute 31 quantiles */
881 uint16_t q[31], *p, *cached;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700882 ssize_t ret;
883
884 cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
885 if (!p)
886 return -ENOMEM;
887
888 mutex_lock(&ca->set->bucket_lock);
Kent Overstreet15754022014-02-25 17:34:21 -0800889 for_each_bucket(b, ca) {
890 if (!GC_SECTORS_USED(b))
891 unused++;
892 if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
893 available++;
894 if (GC_MARK(b) == GC_MARK_DIRTY)
895 dirty++;
896 if (GC_MARK(b) == GC_MARK_METADATA)
897 meta++;
898 }
899
Kent Overstreetcafe5632013-03-23 16:11:31 -0700900 for (i = ca->sb.first_bucket; i < n; i++)
901 p[i] = ca->buckets[i].prio;
902 mutex_unlock(&ca->set->bucket_lock);
903
Peter Foley58f913d2017-10-13 16:35:28 -0700904 sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700905
906 while (n &&
907 !cached[n - 1])
908 --n;
909
910 unused = ca->sb.nbuckets - n;
911
912 while (cached < p + n &&
913 *cached == BTREE_PRIO)
Kent Overstreet15754022014-02-25 17:34:21 -0800914 cached++, n--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700915
916 for (i = 0; i < n; i++)
917 sum += INITIAL_PRIO - cached[i];
918
919 if (n)
920 do_div(sum, n);
921
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700922 for (i = 0; i < ARRAY_SIZE(q); i++)
923 q[i] = INITIAL_PRIO - cached[n * (i + 1) /
924 (ARRAY_SIZE(q) + 1)];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700925
926 vfree(p);
927
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700928 ret = scnprintf(buf, PAGE_SIZE,
929 "Unused: %zu%%\n"
Kent Overstreet15754022014-02-25 17:34:21 -0800930 "Clean: %zu%%\n"
931 "Dirty: %zu%%\n"
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700932 "Metadata: %zu%%\n"
933 "Average: %llu\n"
934 "Sectors per Q: %zu\n"
935 "Quantiles: [",
936 unused * 100 / (size_t) ca->sb.nbuckets,
Kent Overstreet15754022014-02-25 17:34:21 -0800937 available * 100 / (size_t) ca->sb.nbuckets,
938 dirty * 100 / (size_t) ca->sb.nbuckets,
939 meta * 100 / (size_t) ca->sb.nbuckets, sum,
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700940 n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700941
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700942 for (i = 0; i < ARRAY_SIZE(q); i++)
943 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
944 "%u ", q[i]);
945 ret--;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700946
Kent Overstreetbbc77aa2013-05-28 21:53:19 -0700947 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
948
Kent Overstreetcafe5632013-03-23 16:11:31 -0700949 return ret;
950 }
951
952 return 0;
953}
954SHOW_LOCKED(bch_cache)
955
956STORE(__bch_cache)
957{
958 struct cache *ca = container_of(kobj, struct cache, kobj);
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800959 ssize_t v;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700960
961 if (attr == &sysfs_discard) {
962 bool v = strtoul_or_return(buf);
963
964 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
965 ca->discard = v;
966
967 if (v != CACHE_DISCARD(&ca->sb)) {
968 SET_CACHE_DISCARD(&ca->sb, v);
969 bcache_write_super(ca->set);
970 }
971 }
972
973 if (attr == &sysfs_cache_replacement_policy) {
Andy Shevchenkoce4c3e12018-05-28 15:37:44 +0800974 v = __sysfs_match_string(cache_replacement_policies, -1, buf);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700975 if (v < 0)
976 return v;
977
978 if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
979 mutex_lock(&ca->set->bucket_lock);
980 SET_CACHE_REPLACEMENT(&ca->sb, v);
981 mutex_unlock(&ca->set->bucket_lock);
982
983 bcache_write_super(ca->set);
984 }
985 }
986
Kent Overstreetcafe5632013-03-23 16:11:31 -0700987 if (attr == &sysfs_clear_stats) {
988 atomic_long_set(&ca->sectors_written, 0);
989 atomic_long_set(&ca->btree_sectors_written, 0);
990 atomic_long_set(&ca->meta_sectors_written, 0);
991 atomic_set(&ca->io_count, 0);
992 atomic_set(&ca->io_errors, 0);
993 }
994
995 return size;
996}
997STORE_LOCKED(bch_cache)
998
999static struct attribute *bch_cache_files[] = {
1000 &sysfs_bucket_size,
1001 &sysfs_block_size,
1002 &sysfs_nbuckets,
1003 &sysfs_priority_stats,
1004 &sysfs_discard,
1005 &sysfs_written,
1006 &sysfs_btree_written,
1007 &sysfs_metadata_written,
1008 &sysfs_io_errors,
1009 &sysfs_clear_stats,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001010 &sysfs_cache_replacement_policy,
1011 NULL
1012};
1013KTYPE(bch_cache);