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