sysfs.c 22 KB

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