sysfs.c 27 KB

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