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