xref: /openbmc/linux/drivers/md/bcache/sysfs.c (revision fe7498ef)
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 #include "features.h"
15 
16 #include <linux/blkdev.h>
17 #include <linux/sort.h>
18 #include <linux/sched/clock.h>
19 
20 extern bool bcache_is_reboot;
21 
22 /* Default is 0 ("writethrough") */
23 static const char * const bch_cache_modes[] = {
24 	"writethrough",
25 	"writeback",
26 	"writearound",
27 	"none",
28 	NULL
29 };
30 
31 static const char * const bch_reada_cache_policies[] = {
32 	"all",
33 	"meta-only",
34 	NULL
35 };
36 
37 /* Default is 0 ("auto") */
38 static const char * const bch_stop_on_failure_modes[] = {
39 	"auto",
40 	"always",
41 	NULL
42 };
43 
44 static const char * const cache_replacement_policies[] = {
45 	"lru",
46 	"fifo",
47 	"random",
48 	NULL
49 };
50 
51 static const char * const error_actions[] = {
52 	"unregister",
53 	"panic",
54 	NULL
55 };
56 
57 write_attribute(attach);
58 write_attribute(detach);
59 write_attribute(unregister);
60 write_attribute(stop);
61 write_attribute(clear_stats);
62 write_attribute(trigger_gc);
63 write_attribute(prune_cache);
64 write_attribute(flash_vol_create);
65 
66 read_attribute(bucket_size);
67 read_attribute(block_size);
68 read_attribute(nbuckets);
69 read_attribute(tree_depth);
70 read_attribute(root_usage_percent);
71 read_attribute(priority_stats);
72 read_attribute(btree_cache_size);
73 read_attribute(btree_cache_max_chain);
74 read_attribute(cache_available_percent);
75 read_attribute(written);
76 read_attribute(btree_written);
77 read_attribute(metadata_written);
78 read_attribute(active_journal_entries);
79 read_attribute(backing_dev_name);
80 read_attribute(backing_dev_uuid);
81 
82 sysfs_time_stats_attribute(btree_gc,	sec, ms);
83 sysfs_time_stats_attribute(btree_split, sec, us);
84 sysfs_time_stats_attribute(btree_sort,	ms,  us);
85 sysfs_time_stats_attribute(btree_read,	ms,  us);
86 
87 read_attribute(btree_nodes);
88 read_attribute(btree_used_percent);
89 read_attribute(average_key_size);
90 read_attribute(dirty_data);
91 read_attribute(bset_tree_stats);
92 read_attribute(feature_compat);
93 read_attribute(feature_ro_compat);
94 read_attribute(feature_incompat);
95 
96 read_attribute(state);
97 read_attribute(cache_read_races);
98 read_attribute(reclaim);
99 read_attribute(reclaimed_journal_buckets);
100 read_attribute(flush_write);
101 read_attribute(writeback_keys_done);
102 read_attribute(writeback_keys_failed);
103 read_attribute(io_errors);
104 read_attribute(congested);
105 read_attribute(cutoff_writeback);
106 read_attribute(cutoff_writeback_sync);
107 rw_attribute(congested_read_threshold_us);
108 rw_attribute(congested_write_threshold_us);
109 
110 rw_attribute(sequential_cutoff);
111 rw_attribute(data_csum);
112 rw_attribute(cache_mode);
113 rw_attribute(readahead_cache_policy);
114 rw_attribute(stop_when_cache_set_failed);
115 rw_attribute(writeback_metadata);
116 rw_attribute(writeback_running);
117 rw_attribute(writeback_percent);
118 rw_attribute(writeback_delay);
119 rw_attribute(writeback_rate);
120 rw_attribute(writeback_consider_fragment);
121 
122 rw_attribute(writeback_rate_update_seconds);
123 rw_attribute(writeback_rate_i_term_inverse);
124 rw_attribute(writeback_rate_p_term_inverse);
125 rw_attribute(writeback_rate_fp_term_low);
126 rw_attribute(writeback_rate_fp_term_mid);
127 rw_attribute(writeback_rate_fp_term_high);
128 rw_attribute(writeback_rate_minimum);
129 read_attribute(writeback_rate_debug);
130 
131 read_attribute(stripe_size);
132 read_attribute(partial_stripes_expensive);
133 
134 rw_attribute(synchronous);
135 rw_attribute(journal_delay_ms);
136 rw_attribute(io_disable);
137 rw_attribute(discard);
138 rw_attribute(running);
139 rw_attribute(label);
140 rw_attribute(errors);
141 rw_attribute(io_error_limit);
142 rw_attribute(io_error_halflife);
143 rw_attribute(verify);
144 rw_attribute(bypass_torture_test);
145 rw_attribute(key_merging_disabled);
146 rw_attribute(gc_always_rewrite);
147 rw_attribute(expensive_debug_checks);
148 rw_attribute(cache_replacement_policy);
149 rw_attribute(btree_shrinker_disabled);
150 rw_attribute(copy_gc_enabled);
151 rw_attribute(idle_max_writeback_rate);
152 rw_attribute(gc_after_writeback);
153 rw_attribute(size);
154 
155 static ssize_t bch_snprint_string_list(char *buf,
156 				       size_t size,
157 				       const char * const list[],
158 				       size_t selected)
159 {
160 	char *out = buf;
161 	size_t i;
162 
163 	for (i = 0; list[i]; i++)
164 		out += scnprintf(out, buf + size - out,
165 				i == selected ? "[%s] " : "%s ", list[i]);
166 
167 	out[-1] = '\n';
168 	return out - buf;
169 }
170 
171 SHOW(__bch_cached_dev)
172 {
173 	struct cached_dev *dc = container_of(kobj, struct cached_dev,
174 					     disk.kobj);
175 	char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
176 	int wb = dc->writeback_running;
177 
178 #define var(stat)		(dc->stat)
179 
180 	if (attr == &sysfs_cache_mode)
181 		return bch_snprint_string_list(buf, PAGE_SIZE,
182 					       bch_cache_modes,
183 					       BDEV_CACHE_MODE(&dc->sb));
184 
185 	if (attr == &sysfs_readahead_cache_policy)
186 		return bch_snprint_string_list(buf, PAGE_SIZE,
187 					      bch_reada_cache_policies,
188 					      dc->cache_readahead_policy);
189 
190 	if (attr == &sysfs_stop_when_cache_set_failed)
191 		return bch_snprint_string_list(buf, PAGE_SIZE,
192 					       bch_stop_on_failure_modes,
193 					       dc->stop_when_cache_set_failed);
194 
195 
196 	sysfs_printf(data_csum,		"%i", dc->disk.data_csum);
197 	var_printf(verify,		"%i");
198 	var_printf(bypass_torture_test,	"%i");
199 	var_printf(writeback_metadata,	"%i");
200 	var_printf(writeback_running,	"%i");
201 	var_printf(writeback_consider_fragment,	"%i");
202 	var_print(writeback_delay);
203 	var_print(writeback_percent);
204 	sysfs_hprint(writeback_rate,
205 		     wb ? atomic_long_read(&dc->writeback_rate.rate) << 9 : 0);
206 	sysfs_printf(io_errors,		"%i", atomic_read(&dc->io_errors));
207 	sysfs_printf(io_error_limit,	"%i", dc->error_limit);
208 	sysfs_printf(io_disable,	"%i", dc->io_disable);
209 	var_print(writeback_rate_update_seconds);
210 	var_print(writeback_rate_i_term_inverse);
211 	var_print(writeback_rate_p_term_inverse);
212 	var_print(writeback_rate_fp_term_low);
213 	var_print(writeback_rate_fp_term_mid);
214 	var_print(writeback_rate_fp_term_high);
215 	var_print(writeback_rate_minimum);
216 
217 	if (attr == &sysfs_writeback_rate_debug) {
218 		char rate[20];
219 		char dirty[20];
220 		char target[20];
221 		char proportional[20];
222 		char integral[20];
223 		char change[20];
224 		s64 next_io;
225 
226 		/*
227 		 * Except for dirty and target, other values should
228 		 * be 0 if writeback is not running.
229 		 */
230 		bch_hprint(rate,
231 			   wb ? atomic_long_read(&dc->writeback_rate.rate) << 9
232 			      : 0);
233 		bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
234 		bch_hprint(target, dc->writeback_rate_target << 9);
235 		bch_hprint(proportional,
236 			   wb ? dc->writeback_rate_proportional << 9 : 0);
237 		bch_hprint(integral,
238 			   wb ? dc->writeback_rate_integral_scaled << 9 : 0);
239 		bch_hprint(change, wb ? dc->writeback_rate_change << 9 : 0);
240 		next_io = wb ? div64_s64(dc->writeback_rate.next-local_clock(),
241 					 NSEC_PER_MSEC) : 0;
242 
243 		return sprintf(buf,
244 			       "rate:\t\t%s/sec\n"
245 			       "dirty:\t\t%s\n"
246 			       "target:\t\t%s\n"
247 			       "proportional:\t%s\n"
248 			       "integral:\t%s\n"
249 			       "change:\t\t%s/sec\n"
250 			       "next io:\t%llims\n",
251 			       rate, dirty, target, proportional,
252 			       integral, change, next_io);
253 	}
254 
255 	sysfs_hprint(dirty_data,
256 		     bcache_dev_sectors_dirty(&dc->disk) << 9);
257 
258 	sysfs_hprint(stripe_size,	 ((uint64_t)dc->disk.stripe_size) << 9);
259 	var_printf(partial_stripes_expensive,	"%u");
260 
261 	var_hprint(sequential_cutoff);
262 
263 	sysfs_print(running,		atomic_read(&dc->running));
264 	sysfs_print(state,		states[BDEV_STATE(&dc->sb)]);
265 
266 	if (attr == &sysfs_label) {
267 		memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
268 		buf[SB_LABEL_SIZE + 1] = '\0';
269 		strcat(buf, "\n");
270 		return strlen(buf);
271 	}
272 
273 	if (attr == &sysfs_backing_dev_name) {
274 		snprintf(buf, BDEVNAME_SIZE + 1, "%pg", dc->bdev);
275 		strcat(buf, "\n");
276 		return strlen(buf);
277 	}
278 
279 	if (attr == &sysfs_backing_dev_uuid) {
280 		/* convert binary uuid into 36-byte string plus '\0' */
281 		snprintf(buf, 36+1, "%pU", dc->sb.uuid);
282 		strcat(buf, "\n");
283 		return strlen(buf);
284 	}
285 
286 #undef var
287 	return 0;
288 }
289 SHOW_LOCKED(bch_cached_dev)
290 
291 STORE(__cached_dev)
292 {
293 	struct cached_dev *dc = container_of(kobj, struct cached_dev,
294 					     disk.kobj);
295 	ssize_t v;
296 	struct cache_set *c;
297 	struct kobj_uevent_env *env;
298 
299 	/* no user space access if system is rebooting */
300 	if (bcache_is_reboot)
301 		return -EBUSY;
302 
303 #define d_strtoul(var)		sysfs_strtoul(var, dc->var)
304 #define d_strtoul_nonzero(var)	sysfs_strtoul_clamp(var, dc->var, 1, INT_MAX)
305 #define d_strtoi_h(var)		sysfs_hatoi(var, dc->var)
306 
307 	sysfs_strtoul(data_csum,	dc->disk.data_csum);
308 	d_strtoul(verify);
309 	sysfs_strtoul_bool(bypass_torture_test, dc->bypass_torture_test);
310 	sysfs_strtoul_bool(writeback_metadata, dc->writeback_metadata);
311 	sysfs_strtoul_bool(writeback_running, dc->writeback_running);
312 	sysfs_strtoul_bool(writeback_consider_fragment, dc->writeback_consider_fragment);
313 	sysfs_strtoul_clamp(writeback_delay, dc->writeback_delay, 0, UINT_MAX);
314 
315 	sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent,
316 			    0, bch_cutoff_writeback);
317 
318 	if (attr == &sysfs_writeback_rate) {
319 		ssize_t ret;
320 		long int v = atomic_long_read(&dc->writeback_rate.rate);
321 
322 		ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
323 
324 		if (!ret) {
325 			atomic_long_set(&dc->writeback_rate.rate, v);
326 			ret = size;
327 		}
328 
329 		return ret;
330 	}
331 
332 	sysfs_strtoul_clamp(writeback_rate_update_seconds,
333 			    dc->writeback_rate_update_seconds,
334 			    1, WRITEBACK_RATE_UPDATE_SECS_MAX);
335 	sysfs_strtoul_clamp(writeback_rate_i_term_inverse,
336 			    dc->writeback_rate_i_term_inverse,
337 			    1, UINT_MAX);
338 	sysfs_strtoul_clamp(writeback_rate_p_term_inverse,
339 			    dc->writeback_rate_p_term_inverse,
340 			    1, UINT_MAX);
341 	sysfs_strtoul_clamp(writeback_rate_fp_term_low,
342 			    dc->writeback_rate_fp_term_low,
343 			    1, dc->writeback_rate_fp_term_mid - 1);
344 	sysfs_strtoul_clamp(writeback_rate_fp_term_mid,
345 			    dc->writeback_rate_fp_term_mid,
346 			    dc->writeback_rate_fp_term_low + 1,
347 			    dc->writeback_rate_fp_term_high - 1);
348 	sysfs_strtoul_clamp(writeback_rate_fp_term_high,
349 			    dc->writeback_rate_fp_term_high,
350 			    dc->writeback_rate_fp_term_mid + 1, UINT_MAX);
351 	sysfs_strtoul_clamp(writeback_rate_minimum,
352 			    dc->writeback_rate_minimum,
353 			    1, UINT_MAX);
354 
355 	sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
356 
357 	if (attr == &sysfs_io_disable) {
358 		int v = strtoul_or_return(buf);
359 
360 		dc->io_disable = v ? 1 : 0;
361 	}
362 
363 	sysfs_strtoul_clamp(sequential_cutoff,
364 			    dc->sequential_cutoff,
365 			    0, UINT_MAX);
366 
367 	if (attr == &sysfs_clear_stats)
368 		bch_cache_accounting_clear(&dc->accounting);
369 
370 	if (attr == &sysfs_running &&
371 	    strtoul_or_return(buf)) {
372 		v = bch_cached_dev_run(dc);
373 		if (v)
374 			return v;
375 	}
376 
377 	if (attr == &sysfs_cache_mode) {
378 		v = __sysfs_match_string(bch_cache_modes, -1, buf);
379 		if (v < 0)
380 			return v;
381 
382 		if ((unsigned int) v != BDEV_CACHE_MODE(&dc->sb)) {
383 			SET_BDEV_CACHE_MODE(&dc->sb, v);
384 			bch_write_bdev_super(dc, NULL);
385 		}
386 	}
387 
388 	if (attr == &sysfs_readahead_cache_policy) {
389 		v = __sysfs_match_string(bch_reada_cache_policies, -1, buf);
390 		if (v < 0)
391 			return v;
392 
393 		if ((unsigned int) v != dc->cache_readahead_policy)
394 			dc->cache_readahead_policy = v;
395 	}
396 
397 	if (attr == &sysfs_stop_when_cache_set_failed) {
398 		v = __sysfs_match_string(bch_stop_on_failure_modes, -1, buf);
399 		if (v < 0)
400 			return v;
401 
402 		dc->stop_when_cache_set_failed = v;
403 	}
404 
405 	if (attr == &sysfs_label) {
406 		if (size > SB_LABEL_SIZE)
407 			return -EINVAL;
408 		memcpy(dc->sb.label, buf, size);
409 		if (size < SB_LABEL_SIZE)
410 			dc->sb.label[size] = '\0';
411 		if (size && dc->sb.label[size - 1] == '\n')
412 			dc->sb.label[size - 1] = '\0';
413 		bch_write_bdev_super(dc, NULL);
414 		if (dc->disk.c) {
415 			memcpy(dc->disk.c->uuids[dc->disk.id].label,
416 			       buf, SB_LABEL_SIZE);
417 			bch_uuid_write(dc->disk.c);
418 		}
419 		env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
420 		if (!env)
421 			return -ENOMEM;
422 		add_uevent_var(env, "DRIVER=bcache");
423 		add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid);
424 		add_uevent_var(env, "CACHED_LABEL=%s", buf);
425 		kobject_uevent_env(&disk_to_dev(dc->disk.disk)->kobj,
426 				   KOBJ_CHANGE,
427 				   env->envp);
428 		kfree(env);
429 	}
430 
431 	if (attr == &sysfs_attach) {
432 		uint8_t		set_uuid[16];
433 
434 		if (bch_parse_uuid(buf, set_uuid) < 16)
435 			return -EINVAL;
436 
437 		v = -ENOENT;
438 		list_for_each_entry(c, &bch_cache_sets, list) {
439 			v = bch_cached_dev_attach(dc, c, set_uuid);
440 			if (!v)
441 				return size;
442 		}
443 		if (v == -ENOENT)
444 			pr_err("Can't attach %s: cache set not found\n", buf);
445 		return v;
446 	}
447 
448 	if (attr == &sysfs_detach && dc->disk.c)
449 		bch_cached_dev_detach(dc);
450 
451 	if (attr == &sysfs_stop)
452 		bcache_device_stop(&dc->disk);
453 
454 	return size;
455 }
456 
457 STORE(bch_cached_dev)
458 {
459 	struct cached_dev *dc = container_of(kobj, struct cached_dev,
460 					     disk.kobj);
461 
462 	/* no user space access if system is rebooting */
463 	if (bcache_is_reboot)
464 		return -EBUSY;
465 
466 	mutex_lock(&bch_register_lock);
467 	size = __cached_dev_store(kobj, attr, buf, size);
468 
469 	if (attr == &sysfs_writeback_running) {
470 		/* dc->writeback_running changed in __cached_dev_store() */
471 		if (IS_ERR_OR_NULL(dc->writeback_thread)) {
472 			/*
473 			 * reject setting it to 1 via sysfs if writeback
474 			 * kthread is not created yet.
475 			 */
476 			if (dc->writeback_running) {
477 				dc->writeback_running = false;
478 				pr_err("%s: failed to run non-existent writeback thread\n",
479 						dc->disk.disk->disk_name);
480 			}
481 		} else
482 			/*
483 			 * writeback kthread will check if dc->writeback_running
484 			 * is true or false.
485 			 */
486 			bch_writeback_queue(dc);
487 	}
488 
489 	/*
490 	 * Only set BCACHE_DEV_WB_RUNNING when cached device attached to
491 	 * a cache set, otherwise it doesn't make sense.
492 	 */
493 	if (attr == &sysfs_writeback_percent)
494 		if ((dc->disk.c != NULL) &&
495 		    (!test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)))
496 			schedule_delayed_work(&dc->writeback_rate_update,
497 				      dc->writeback_rate_update_seconds * HZ);
498 
499 	mutex_unlock(&bch_register_lock);
500 	return size;
501 }
502 
503 static struct attribute *bch_cached_dev_files[] = {
504 	&sysfs_attach,
505 	&sysfs_detach,
506 	&sysfs_stop,
507 #if 0
508 	&sysfs_data_csum,
509 #endif
510 	&sysfs_cache_mode,
511 	&sysfs_readahead_cache_policy,
512 	&sysfs_stop_when_cache_set_failed,
513 	&sysfs_writeback_metadata,
514 	&sysfs_writeback_running,
515 	&sysfs_writeback_delay,
516 	&sysfs_writeback_percent,
517 	&sysfs_writeback_rate,
518 	&sysfs_writeback_consider_fragment,
519 	&sysfs_writeback_rate_update_seconds,
520 	&sysfs_writeback_rate_i_term_inverse,
521 	&sysfs_writeback_rate_p_term_inverse,
522 	&sysfs_writeback_rate_fp_term_low,
523 	&sysfs_writeback_rate_fp_term_mid,
524 	&sysfs_writeback_rate_fp_term_high,
525 	&sysfs_writeback_rate_minimum,
526 	&sysfs_writeback_rate_debug,
527 	&sysfs_io_errors,
528 	&sysfs_io_error_limit,
529 	&sysfs_io_disable,
530 	&sysfs_dirty_data,
531 	&sysfs_stripe_size,
532 	&sysfs_partial_stripes_expensive,
533 	&sysfs_sequential_cutoff,
534 	&sysfs_clear_stats,
535 	&sysfs_running,
536 	&sysfs_state,
537 	&sysfs_label,
538 #ifdef CONFIG_BCACHE_DEBUG
539 	&sysfs_verify,
540 	&sysfs_bypass_torture_test,
541 #endif
542 	&sysfs_backing_dev_name,
543 	&sysfs_backing_dev_uuid,
544 	NULL
545 };
546 KTYPE(bch_cached_dev);
547 
548 SHOW(bch_flash_dev)
549 {
550 	struct bcache_device *d = container_of(kobj, struct bcache_device,
551 					       kobj);
552 	struct uuid_entry *u = &d->c->uuids[d->id];
553 
554 	sysfs_printf(data_csum,	"%i", d->data_csum);
555 	sysfs_hprint(size,	u->sectors << 9);
556 
557 	if (attr == &sysfs_label) {
558 		memcpy(buf, u->label, SB_LABEL_SIZE);
559 		buf[SB_LABEL_SIZE + 1] = '\0';
560 		strcat(buf, "\n");
561 		return strlen(buf);
562 	}
563 
564 	return 0;
565 }
566 
567 STORE(__bch_flash_dev)
568 {
569 	struct bcache_device *d = container_of(kobj, struct bcache_device,
570 					       kobj);
571 	struct uuid_entry *u = &d->c->uuids[d->id];
572 
573 	/* no user space access if system is rebooting */
574 	if (bcache_is_reboot)
575 		return -EBUSY;
576 
577 	sysfs_strtoul(data_csum,	d->data_csum);
578 
579 	if (attr == &sysfs_size) {
580 		uint64_t v;
581 
582 		strtoi_h_or_return(buf, v);
583 
584 		u->sectors = v >> 9;
585 		bch_uuid_write(d->c);
586 		set_capacity(d->disk, u->sectors);
587 	}
588 
589 	if (attr == &sysfs_label) {
590 		memcpy(u->label, buf, SB_LABEL_SIZE);
591 		bch_uuid_write(d->c);
592 	}
593 
594 	if (attr == &sysfs_unregister) {
595 		set_bit(BCACHE_DEV_DETACHING, &d->flags);
596 		bcache_device_stop(d);
597 	}
598 
599 	return size;
600 }
601 STORE_LOCKED(bch_flash_dev)
602 
603 static struct attribute *bch_flash_dev_files[] = {
604 	&sysfs_unregister,
605 #if 0
606 	&sysfs_data_csum,
607 #endif
608 	&sysfs_label,
609 	&sysfs_size,
610 	NULL
611 };
612 KTYPE(bch_flash_dev);
613 
614 struct bset_stats_op {
615 	struct btree_op op;
616 	size_t nodes;
617 	struct bset_stats stats;
618 };
619 
620 static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
621 {
622 	struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
623 
624 	op->nodes++;
625 	bch_btree_keys_stats(&b->keys, &op->stats);
626 
627 	return MAP_CONTINUE;
628 }
629 
630 static int bch_bset_print_stats(struct cache_set *c, char *buf)
631 {
632 	struct bset_stats_op op;
633 	int ret;
634 
635 	memset(&op, 0, sizeof(op));
636 	bch_btree_op_init(&op.op, -1);
637 
638 	ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
639 	if (ret < 0)
640 		return ret;
641 
642 	return snprintf(buf, PAGE_SIZE,
643 			"btree nodes:		%zu\n"
644 			"written sets:		%zu\n"
645 			"unwritten sets:		%zu\n"
646 			"written key bytes:	%zu\n"
647 			"unwritten key bytes:	%zu\n"
648 			"floats:			%zu\n"
649 			"failed:			%zu\n",
650 			op.nodes,
651 			op.stats.sets_written, op.stats.sets_unwritten,
652 			op.stats.bytes_written, op.stats.bytes_unwritten,
653 			op.stats.floats, op.stats.failed);
654 }
655 
656 static unsigned int bch_root_usage(struct cache_set *c)
657 {
658 	unsigned int bytes = 0;
659 	struct bkey *k;
660 	struct btree *b;
661 	struct btree_iter iter;
662 
663 	goto lock_root;
664 
665 	do {
666 		rw_unlock(false, b);
667 lock_root:
668 		b = c->root;
669 		rw_lock(false, b, b->level);
670 	} while (b != c->root);
671 
672 	for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
673 		bytes += bkey_bytes(k);
674 
675 	rw_unlock(false, b);
676 
677 	return (bytes * 100) / btree_bytes(c);
678 }
679 
680 static size_t bch_cache_size(struct cache_set *c)
681 {
682 	size_t ret = 0;
683 	struct btree *b;
684 
685 	mutex_lock(&c->bucket_lock);
686 	list_for_each_entry(b, &c->btree_cache, list)
687 		ret += 1 << (b->keys.page_order + PAGE_SHIFT);
688 
689 	mutex_unlock(&c->bucket_lock);
690 	return ret;
691 }
692 
693 static unsigned int bch_cache_max_chain(struct cache_set *c)
694 {
695 	unsigned int ret = 0;
696 	struct hlist_head *h;
697 
698 	mutex_lock(&c->bucket_lock);
699 
700 	for (h = c->bucket_hash;
701 	     h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
702 	     h++) {
703 		unsigned int i = 0;
704 		struct hlist_node *p;
705 
706 		hlist_for_each(p, h)
707 			i++;
708 
709 		ret = max(ret, i);
710 	}
711 
712 	mutex_unlock(&c->bucket_lock);
713 	return ret;
714 }
715 
716 static unsigned int bch_btree_used(struct cache_set *c)
717 {
718 	return div64_u64(c->gc_stats.key_bytes * 100,
719 			 (c->gc_stats.nodes ?: 1) * btree_bytes(c));
720 }
721 
722 static unsigned int bch_average_key_size(struct cache_set *c)
723 {
724 	return c->gc_stats.nkeys
725 		? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
726 		: 0;
727 }
728 
729 SHOW(__bch_cache_set)
730 {
731 	struct cache_set *c = container_of(kobj, struct cache_set, kobj);
732 
733 	sysfs_print(synchronous,		CACHE_SYNC(&c->cache->sb));
734 	sysfs_print(journal_delay_ms,		c->journal_delay_ms);
735 	sysfs_hprint(bucket_size,		bucket_bytes(c->cache));
736 	sysfs_hprint(block_size,		block_bytes(c->cache));
737 	sysfs_print(tree_depth,			c->root->level);
738 	sysfs_print(root_usage_percent,		bch_root_usage(c));
739 
740 	sysfs_hprint(btree_cache_size,		bch_cache_size(c));
741 	sysfs_print(btree_cache_max_chain,	bch_cache_max_chain(c));
742 	sysfs_print(cache_available_percent,	100 - c->gc_stats.in_use);
743 
744 	sysfs_print_time_stats(&c->btree_gc_time,	btree_gc, sec, ms);
745 	sysfs_print_time_stats(&c->btree_split_time,	btree_split, sec, us);
746 	sysfs_print_time_stats(&c->sort.time,		btree_sort, ms, us);
747 	sysfs_print_time_stats(&c->btree_read_time,	btree_read, ms, us);
748 
749 	sysfs_print(btree_used_percent,	bch_btree_used(c));
750 	sysfs_print(btree_nodes,	c->gc_stats.nodes);
751 	sysfs_hprint(average_key_size,	bch_average_key_size(c));
752 
753 	sysfs_print(cache_read_races,
754 		    atomic_long_read(&c->cache_read_races));
755 
756 	sysfs_print(reclaim,
757 		    atomic_long_read(&c->reclaim));
758 
759 	sysfs_print(reclaimed_journal_buckets,
760 		    atomic_long_read(&c->reclaimed_journal_buckets));
761 
762 	sysfs_print(flush_write,
763 		    atomic_long_read(&c->flush_write));
764 
765 	sysfs_print(writeback_keys_done,
766 		    atomic_long_read(&c->writeback_keys_done));
767 	sysfs_print(writeback_keys_failed,
768 		    atomic_long_read(&c->writeback_keys_failed));
769 
770 	if (attr == &sysfs_errors)
771 		return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
772 					       c->on_error);
773 
774 	/* See count_io_errors for why 88 */
775 	sysfs_print(io_error_halflife,	c->error_decay * 88);
776 	sysfs_print(io_error_limit,	c->error_limit);
777 
778 	sysfs_hprint(congested,
779 		     ((uint64_t) bch_get_congested(c)) << 9);
780 	sysfs_print(congested_read_threshold_us,
781 		    c->congested_read_threshold_us);
782 	sysfs_print(congested_write_threshold_us,
783 		    c->congested_write_threshold_us);
784 
785 	sysfs_print(cutoff_writeback, bch_cutoff_writeback);
786 	sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync);
787 
788 	sysfs_print(active_journal_entries,	fifo_used(&c->journal.pin));
789 	sysfs_printf(verify,			"%i", c->verify);
790 	sysfs_printf(key_merging_disabled,	"%i", c->key_merging_disabled);
791 	sysfs_printf(expensive_debug_checks,
792 		     "%i", c->expensive_debug_checks);
793 	sysfs_printf(gc_always_rewrite,		"%i", c->gc_always_rewrite);
794 	sysfs_printf(btree_shrinker_disabled,	"%i", c->shrinker_disabled);
795 	sysfs_printf(copy_gc_enabled,		"%i", c->copy_gc_enabled);
796 	sysfs_printf(idle_max_writeback_rate,	"%i",
797 		     c->idle_max_writeback_rate_enabled);
798 	sysfs_printf(gc_after_writeback,	"%i", c->gc_after_writeback);
799 	sysfs_printf(io_disable,		"%i",
800 		     test_bit(CACHE_SET_IO_DISABLE, &c->flags));
801 
802 	if (attr == &sysfs_bset_tree_stats)
803 		return bch_bset_print_stats(c, buf);
804 
805 	if (attr == &sysfs_feature_compat)
806 		return bch_print_cache_set_feature_compat(c, buf, PAGE_SIZE);
807 	if (attr == &sysfs_feature_ro_compat)
808 		return bch_print_cache_set_feature_ro_compat(c, buf, PAGE_SIZE);
809 	if (attr == &sysfs_feature_incompat)
810 		return bch_print_cache_set_feature_incompat(c, buf, PAGE_SIZE);
811 
812 	return 0;
813 }
814 SHOW_LOCKED(bch_cache_set)
815 
816 STORE(__bch_cache_set)
817 {
818 	struct cache_set *c = container_of(kobj, struct cache_set, kobj);
819 	ssize_t v;
820 
821 	/* no user space access if system is rebooting */
822 	if (bcache_is_reboot)
823 		return -EBUSY;
824 
825 	if (attr == &sysfs_unregister)
826 		bch_cache_set_unregister(c);
827 
828 	if (attr == &sysfs_stop)
829 		bch_cache_set_stop(c);
830 
831 	if (attr == &sysfs_synchronous) {
832 		bool sync = strtoul_or_return(buf);
833 
834 		if (sync != CACHE_SYNC(&c->cache->sb)) {
835 			SET_CACHE_SYNC(&c->cache->sb, sync);
836 			bcache_write_super(c);
837 		}
838 	}
839 
840 	if (attr == &sysfs_flash_vol_create) {
841 		int r;
842 		uint64_t v;
843 
844 		strtoi_h_or_return(buf, v);
845 
846 		r = bch_flash_dev_create(c, v);
847 		if (r)
848 			return r;
849 	}
850 
851 	if (attr == &sysfs_clear_stats) {
852 		atomic_long_set(&c->writeback_keys_done,	0);
853 		atomic_long_set(&c->writeback_keys_failed,	0);
854 
855 		memset(&c->gc_stats, 0, sizeof(struct gc_stat));
856 		bch_cache_accounting_clear(&c->accounting);
857 	}
858 
859 	if (attr == &sysfs_trigger_gc)
860 		force_wake_up_gc(c);
861 
862 	if (attr == &sysfs_prune_cache) {
863 		struct shrink_control sc;
864 
865 		sc.gfp_mask = GFP_KERNEL;
866 		sc.nr_to_scan = strtoul_or_return(buf);
867 		c->shrink.scan_objects(&c->shrink, &sc);
868 	}
869 
870 	sysfs_strtoul_clamp(congested_read_threshold_us,
871 			    c->congested_read_threshold_us,
872 			    0, UINT_MAX);
873 	sysfs_strtoul_clamp(congested_write_threshold_us,
874 			    c->congested_write_threshold_us,
875 			    0, UINT_MAX);
876 
877 	if (attr == &sysfs_errors) {
878 		v = __sysfs_match_string(error_actions, -1, buf);
879 		if (v < 0)
880 			return v;
881 
882 		c->on_error = v;
883 	}
884 
885 	sysfs_strtoul_clamp(io_error_limit, c->error_limit, 0, UINT_MAX);
886 
887 	/* See count_io_errors() for why 88 */
888 	if (attr == &sysfs_io_error_halflife) {
889 		unsigned long v = 0;
890 		ssize_t ret;
891 
892 		ret = strtoul_safe_clamp(buf, v, 0, UINT_MAX);
893 		if (!ret) {
894 			c->error_decay = v / 88;
895 			return size;
896 		}
897 		return ret;
898 	}
899 
900 	if (attr == &sysfs_io_disable) {
901 		v = strtoul_or_return(buf);
902 		if (v) {
903 			if (test_and_set_bit(CACHE_SET_IO_DISABLE,
904 					     &c->flags))
905 				pr_warn("CACHE_SET_IO_DISABLE already set\n");
906 		} else {
907 			if (!test_and_clear_bit(CACHE_SET_IO_DISABLE,
908 						&c->flags))
909 				pr_warn("CACHE_SET_IO_DISABLE already cleared\n");
910 		}
911 	}
912 
913 	sysfs_strtoul_clamp(journal_delay_ms,
914 			    c->journal_delay_ms,
915 			    0, USHRT_MAX);
916 	sysfs_strtoul_bool(verify,		c->verify);
917 	sysfs_strtoul_bool(key_merging_disabled, c->key_merging_disabled);
918 	sysfs_strtoul(expensive_debug_checks,	c->expensive_debug_checks);
919 	sysfs_strtoul_bool(gc_always_rewrite,	c->gc_always_rewrite);
920 	sysfs_strtoul_bool(btree_shrinker_disabled, c->shrinker_disabled);
921 	sysfs_strtoul_bool(copy_gc_enabled,	c->copy_gc_enabled);
922 	sysfs_strtoul_bool(idle_max_writeback_rate,
923 			   c->idle_max_writeback_rate_enabled);
924 
925 	/*
926 	 * write gc_after_writeback here may overwrite an already set
927 	 * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
928 	 * set in next chance.
929 	 */
930 	sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
931 
932 	return size;
933 }
934 STORE_LOCKED(bch_cache_set)
935 
936 SHOW(bch_cache_set_internal)
937 {
938 	struct cache_set *c = container_of(kobj, struct cache_set, internal);
939 
940 	return bch_cache_set_show(&c->kobj, attr, buf);
941 }
942 
943 STORE(bch_cache_set_internal)
944 {
945 	struct cache_set *c = container_of(kobj, struct cache_set, internal);
946 
947 	/* no user space access if system is rebooting */
948 	if (bcache_is_reboot)
949 		return -EBUSY;
950 
951 	return bch_cache_set_store(&c->kobj, attr, buf, size);
952 }
953 
954 static void bch_cache_set_internal_release(struct kobject *k)
955 {
956 }
957 
958 static struct attribute *bch_cache_set_files[] = {
959 	&sysfs_unregister,
960 	&sysfs_stop,
961 	&sysfs_synchronous,
962 	&sysfs_journal_delay_ms,
963 	&sysfs_flash_vol_create,
964 
965 	&sysfs_bucket_size,
966 	&sysfs_block_size,
967 	&sysfs_tree_depth,
968 	&sysfs_root_usage_percent,
969 	&sysfs_btree_cache_size,
970 	&sysfs_cache_available_percent,
971 
972 	&sysfs_average_key_size,
973 
974 	&sysfs_errors,
975 	&sysfs_io_error_limit,
976 	&sysfs_io_error_halflife,
977 	&sysfs_congested,
978 	&sysfs_congested_read_threshold_us,
979 	&sysfs_congested_write_threshold_us,
980 	&sysfs_clear_stats,
981 	NULL
982 };
983 KTYPE(bch_cache_set);
984 
985 static struct attribute *bch_cache_set_internal_files[] = {
986 	&sysfs_active_journal_entries,
987 
988 	sysfs_time_stats_attribute_list(btree_gc, sec, ms)
989 	sysfs_time_stats_attribute_list(btree_split, sec, us)
990 	sysfs_time_stats_attribute_list(btree_sort, ms, us)
991 	sysfs_time_stats_attribute_list(btree_read, ms, us)
992 
993 	&sysfs_btree_nodes,
994 	&sysfs_btree_used_percent,
995 	&sysfs_btree_cache_max_chain,
996 
997 	&sysfs_bset_tree_stats,
998 	&sysfs_cache_read_races,
999 	&sysfs_reclaim,
1000 	&sysfs_reclaimed_journal_buckets,
1001 	&sysfs_flush_write,
1002 	&sysfs_writeback_keys_done,
1003 	&sysfs_writeback_keys_failed,
1004 
1005 	&sysfs_trigger_gc,
1006 	&sysfs_prune_cache,
1007 #ifdef CONFIG_BCACHE_DEBUG
1008 	&sysfs_verify,
1009 	&sysfs_key_merging_disabled,
1010 	&sysfs_expensive_debug_checks,
1011 #endif
1012 	&sysfs_gc_always_rewrite,
1013 	&sysfs_btree_shrinker_disabled,
1014 	&sysfs_copy_gc_enabled,
1015 	&sysfs_idle_max_writeback_rate,
1016 	&sysfs_gc_after_writeback,
1017 	&sysfs_io_disable,
1018 	&sysfs_cutoff_writeback,
1019 	&sysfs_cutoff_writeback_sync,
1020 	&sysfs_feature_compat,
1021 	&sysfs_feature_ro_compat,
1022 	&sysfs_feature_incompat,
1023 	NULL
1024 };
1025 KTYPE(bch_cache_set_internal);
1026 
1027 static int __bch_cache_cmp(const void *l, const void *r)
1028 {
1029 	cond_resched();
1030 	return *((uint16_t *)r) - *((uint16_t *)l);
1031 }
1032 
1033 SHOW(__bch_cache)
1034 {
1035 	struct cache *ca = container_of(kobj, struct cache, kobj);
1036 
1037 	sysfs_hprint(bucket_size,	bucket_bytes(ca));
1038 	sysfs_hprint(block_size,	block_bytes(ca));
1039 	sysfs_print(nbuckets,		ca->sb.nbuckets);
1040 	sysfs_print(discard,		ca->discard);
1041 	sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
1042 	sysfs_hprint(btree_written,
1043 		     atomic_long_read(&ca->btree_sectors_written) << 9);
1044 	sysfs_hprint(metadata_written,
1045 		     (atomic_long_read(&ca->meta_sectors_written) +
1046 		      atomic_long_read(&ca->btree_sectors_written)) << 9);
1047 
1048 	sysfs_print(io_errors,
1049 		    atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
1050 
1051 	if (attr == &sysfs_cache_replacement_policy)
1052 		return bch_snprint_string_list(buf, PAGE_SIZE,
1053 					       cache_replacement_policies,
1054 					       CACHE_REPLACEMENT(&ca->sb));
1055 
1056 	if (attr == &sysfs_priority_stats) {
1057 		struct bucket *b;
1058 		size_t n = ca->sb.nbuckets, i;
1059 		size_t unused = 0, available = 0, dirty = 0, meta = 0;
1060 		uint64_t sum = 0;
1061 		/* Compute 31 quantiles */
1062 		uint16_t q[31], *p, *cached;
1063 		ssize_t ret;
1064 
1065 		cached = p = vmalloc(array_size(sizeof(uint16_t),
1066 						ca->sb.nbuckets));
1067 		if (!p)
1068 			return -ENOMEM;
1069 
1070 		mutex_lock(&ca->set->bucket_lock);
1071 		for_each_bucket(b, ca) {
1072 			if (!GC_SECTORS_USED(b))
1073 				unused++;
1074 			if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
1075 				available++;
1076 			if (GC_MARK(b) == GC_MARK_DIRTY)
1077 				dirty++;
1078 			if (GC_MARK(b) == GC_MARK_METADATA)
1079 				meta++;
1080 		}
1081 
1082 		for (i = ca->sb.first_bucket; i < n; i++)
1083 			p[i] = ca->buckets[i].prio;
1084 		mutex_unlock(&ca->set->bucket_lock);
1085 
1086 		sort(p, n, sizeof(uint16_t), __bch_cache_cmp, NULL);
1087 
1088 		while (n &&
1089 		       !cached[n - 1])
1090 			--n;
1091 
1092 		while (cached < p + n &&
1093 		       *cached == BTREE_PRIO) {
1094 			cached++;
1095 			n--;
1096 		}
1097 
1098 		for (i = 0; i < n; i++)
1099 			sum += INITIAL_PRIO - cached[i];
1100 
1101 		if (n)
1102 			do_div(sum, n);
1103 
1104 		for (i = 0; i < ARRAY_SIZE(q); i++)
1105 			q[i] = INITIAL_PRIO - cached[n * (i + 1) /
1106 				(ARRAY_SIZE(q) + 1)];
1107 
1108 		vfree(p);
1109 
1110 		ret = scnprintf(buf, PAGE_SIZE,
1111 				"Unused:		%zu%%\n"
1112 				"Clean:		%zu%%\n"
1113 				"Dirty:		%zu%%\n"
1114 				"Metadata:	%zu%%\n"
1115 				"Average:	%llu\n"
1116 				"Sectors per Q:	%zu\n"
1117 				"Quantiles:	[",
1118 				unused * 100 / (size_t) ca->sb.nbuckets,
1119 				available * 100 / (size_t) ca->sb.nbuckets,
1120 				dirty * 100 / (size_t) ca->sb.nbuckets,
1121 				meta * 100 / (size_t) ca->sb.nbuckets, sum,
1122 				n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
1123 
1124 		for (i = 0; i < ARRAY_SIZE(q); i++)
1125 			ret += scnprintf(buf + ret, PAGE_SIZE - ret,
1126 					 "%u ", q[i]);
1127 		ret--;
1128 
1129 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
1130 
1131 		return ret;
1132 	}
1133 
1134 	return 0;
1135 }
1136 SHOW_LOCKED(bch_cache)
1137 
1138 STORE(__bch_cache)
1139 {
1140 	struct cache *ca = container_of(kobj, struct cache, kobj);
1141 	ssize_t v;
1142 
1143 	/* no user space access if system is rebooting */
1144 	if (bcache_is_reboot)
1145 		return -EBUSY;
1146 
1147 	if (attr == &sysfs_discard) {
1148 		bool v = strtoul_or_return(buf);
1149 
1150 		if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1151 			ca->discard = v;
1152 
1153 		if (v != CACHE_DISCARD(&ca->sb)) {
1154 			SET_CACHE_DISCARD(&ca->sb, v);
1155 			bcache_write_super(ca->set);
1156 		}
1157 	}
1158 
1159 	if (attr == &sysfs_cache_replacement_policy) {
1160 		v = __sysfs_match_string(cache_replacement_policies, -1, buf);
1161 		if (v < 0)
1162 			return v;
1163 
1164 		if ((unsigned int) v != CACHE_REPLACEMENT(&ca->sb)) {
1165 			mutex_lock(&ca->set->bucket_lock);
1166 			SET_CACHE_REPLACEMENT(&ca->sb, v);
1167 			mutex_unlock(&ca->set->bucket_lock);
1168 
1169 			bcache_write_super(ca->set);
1170 		}
1171 	}
1172 
1173 	if (attr == &sysfs_clear_stats) {
1174 		atomic_long_set(&ca->sectors_written, 0);
1175 		atomic_long_set(&ca->btree_sectors_written, 0);
1176 		atomic_long_set(&ca->meta_sectors_written, 0);
1177 		atomic_set(&ca->io_count, 0);
1178 		atomic_set(&ca->io_errors, 0);
1179 	}
1180 
1181 	return size;
1182 }
1183 STORE_LOCKED(bch_cache)
1184 
1185 static struct attribute *bch_cache_files[] = {
1186 	&sysfs_bucket_size,
1187 	&sysfs_block_size,
1188 	&sysfs_nbuckets,
1189 	&sysfs_priority_stats,
1190 	&sysfs_discard,
1191 	&sysfs_written,
1192 	&sysfs_btree_written,
1193 	&sysfs_metadata_written,
1194 	&sysfs_io_errors,
1195 	&sysfs_clear_stats,
1196 	&sysfs_cache_replacement_policy,
1197 	NULL
1198 };
1199 KTYPE(bch_cache);
1200