Linux Audio

Check our new training course

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