sysfs.c 24 KB

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