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