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