sysfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. d_strtoi_h(sequential_cutoff);
  184. d_strtoi_h(readahead);
  185. if (attr == &sysfs_clear_stats)
  186. bch_cache_accounting_clear(&dc->accounting);
  187. if (attr == &sysfs_running &&
  188. strtoul_or_return(buf))
  189. bch_cached_dev_run(dc);
  190. if (attr == &sysfs_cache_mode) {
  191. v = bch_read_string_list(buf, bch_cache_modes + 1);
  192. if (v < 0)
  193. return v;
  194. if ((unsigned) v != BDEV_CACHE_MODE(&dc->sb)) {
  195. SET_BDEV_CACHE_MODE(&dc->sb, v);
  196. bch_write_bdev_super(dc, NULL);
  197. }
  198. }
  199. if (attr == &sysfs_label) {
  200. if (size > SB_LABEL_SIZE)
  201. return -EINVAL;
  202. memcpy(dc->sb.label, buf, size);
  203. if (size < SB_LABEL_SIZE)
  204. dc->sb.label[size] = '\0';
  205. if (size && dc->sb.label[size - 1] == '\n')
  206. dc->sb.label[size - 1] = '\0';
  207. bch_write_bdev_super(dc, NULL);
  208. if (dc->disk.c) {
  209. memcpy(dc->disk.c->uuids[dc->disk.id].label,
  210. buf, SB_LABEL_SIZE);
  211. bch_uuid_write(dc->disk.c);
  212. }
  213. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  214. if (!env)
  215. return -ENOMEM;
  216. add_uevent_var(env, "DRIVER=bcache");
  217. add_uevent_var(env, "CACHED_UUID=%pU", dc->sb.uuid),
  218. add_uevent_var(env, "CACHED_LABEL=%s", buf);
  219. kobject_uevent_env(
  220. &disk_to_dev(dc->disk.disk)->kobj, KOBJ_CHANGE, env->envp);
  221. kfree(env);
  222. }
  223. if (attr == &sysfs_attach) {
  224. uint8_t set_uuid[16];
  225. if (bch_parse_uuid(buf, set_uuid) < 16)
  226. return -EINVAL;
  227. v = -ENOENT;
  228. list_for_each_entry(c, &bch_cache_sets, list) {
  229. v = bch_cached_dev_attach(dc, c, set_uuid);
  230. if (!v)
  231. return size;
  232. }
  233. pr_err("Can't attach %s: cache set not found", buf);
  234. return v;
  235. }
  236. if (attr == &sysfs_detach && dc->disk.c)
  237. bch_cached_dev_detach(dc);
  238. if (attr == &sysfs_stop)
  239. bcache_device_stop(&dc->disk);
  240. return size;
  241. }
  242. STORE(bch_cached_dev)
  243. {
  244. struct cached_dev *dc = container_of(kobj, struct cached_dev,
  245. disk.kobj);
  246. mutex_lock(&bch_register_lock);
  247. size = __cached_dev_store(kobj, attr, buf, size);
  248. if (attr == &sysfs_writeback_running)
  249. bch_writeback_queue(dc);
  250. if (attr == &sysfs_writeback_percent)
  251. schedule_delayed_work(&dc->writeback_rate_update,
  252. dc->writeback_rate_update_seconds * HZ);
  253. mutex_unlock(&bch_register_lock);
  254. return size;
  255. }
  256. static struct attribute *bch_cached_dev_files[] = {
  257. &sysfs_attach,
  258. &sysfs_detach,
  259. &sysfs_stop,
  260. #if 0
  261. &sysfs_data_csum,
  262. #endif
  263. &sysfs_cache_mode,
  264. &sysfs_writeback_metadata,
  265. &sysfs_writeback_running,
  266. &sysfs_writeback_delay,
  267. &sysfs_writeback_percent,
  268. &sysfs_writeback_rate,
  269. &sysfs_writeback_rate_update_seconds,
  270. &sysfs_writeback_rate_d_term,
  271. &sysfs_writeback_rate_p_term_inverse,
  272. &sysfs_writeback_rate_debug,
  273. &sysfs_dirty_data,
  274. &sysfs_stripe_size,
  275. &sysfs_partial_stripes_expensive,
  276. &sysfs_sequential_cutoff,
  277. &sysfs_clear_stats,
  278. &sysfs_running,
  279. &sysfs_state,
  280. &sysfs_label,
  281. &sysfs_readahead,
  282. #ifdef CONFIG_BCACHE_DEBUG
  283. &sysfs_verify,
  284. &sysfs_bypass_torture_test,
  285. #endif
  286. NULL
  287. };
  288. KTYPE(bch_cached_dev);
  289. SHOW(bch_flash_dev)
  290. {
  291. struct bcache_device *d = container_of(kobj, struct bcache_device,
  292. kobj);
  293. struct uuid_entry *u = &d->c->uuids[d->id];
  294. sysfs_printf(data_csum, "%i", d->data_csum);
  295. sysfs_hprint(size, u->sectors << 9);
  296. if (attr == &sysfs_label) {
  297. memcpy(buf, u->label, SB_LABEL_SIZE);
  298. buf[SB_LABEL_SIZE + 1] = '\0';
  299. strcat(buf, "\n");
  300. return strlen(buf);
  301. }
  302. return 0;
  303. }
  304. STORE(__bch_flash_dev)
  305. {
  306. struct bcache_device *d = container_of(kobj, struct bcache_device,
  307. kobj);
  308. struct uuid_entry *u = &d->c->uuids[d->id];
  309. sysfs_strtoul(data_csum, d->data_csum);
  310. if (attr == &sysfs_size) {
  311. uint64_t v;
  312. strtoi_h_or_return(buf, v);
  313. u->sectors = v >> 9;
  314. bch_uuid_write(d->c);
  315. set_capacity(d->disk, u->sectors);
  316. }
  317. if (attr == &sysfs_label) {
  318. memcpy(u->label, buf, SB_LABEL_SIZE);
  319. bch_uuid_write(d->c);
  320. }
  321. if (attr == &sysfs_unregister) {
  322. set_bit(BCACHE_DEV_DETACHING, &d->flags);
  323. bcache_device_stop(d);
  324. }
  325. return size;
  326. }
  327. STORE_LOCKED(bch_flash_dev)
  328. static struct attribute *bch_flash_dev_files[] = {
  329. &sysfs_unregister,
  330. #if 0
  331. &sysfs_data_csum,
  332. #endif
  333. &sysfs_label,
  334. &sysfs_size,
  335. NULL
  336. };
  337. KTYPE(bch_flash_dev);
  338. struct bset_stats_op {
  339. struct btree_op op;
  340. size_t nodes;
  341. struct bset_stats stats;
  342. };
  343. static int bch_btree_bset_stats(struct btree_op *b_op, struct btree *b)
  344. {
  345. struct bset_stats_op *op = container_of(b_op, struct bset_stats_op, op);
  346. op->nodes++;
  347. bch_btree_keys_stats(&b->keys, &op->stats);
  348. return MAP_CONTINUE;
  349. }
  350. static int bch_bset_print_stats(struct cache_set *c, char *buf)
  351. {
  352. struct bset_stats_op op;
  353. int ret;
  354. memset(&op, 0, sizeof(op));
  355. bch_btree_op_init(&op.op, -1);
  356. ret = bch_btree_map_nodes(&op.op, c, &ZERO_KEY, bch_btree_bset_stats);
  357. if (ret < 0)
  358. return ret;
  359. return snprintf(buf, PAGE_SIZE,
  360. "btree nodes: %zu\n"
  361. "written sets: %zu\n"
  362. "unwritten sets: %zu\n"
  363. "written key bytes: %zu\n"
  364. "unwritten key bytes: %zu\n"
  365. "floats: %zu\n"
  366. "failed: %zu\n",
  367. op.nodes,
  368. op.stats.sets_written, op.stats.sets_unwritten,
  369. op.stats.bytes_written, op.stats.bytes_unwritten,
  370. op.stats.floats, op.stats.failed);
  371. }
  372. static unsigned bch_root_usage(struct cache_set *c)
  373. {
  374. unsigned bytes = 0;
  375. struct bkey *k;
  376. struct btree *b;
  377. struct btree_iter iter;
  378. goto lock_root;
  379. do {
  380. rw_unlock(false, b);
  381. lock_root:
  382. b = c->root;
  383. rw_lock(false, b, b->level);
  384. } while (b != c->root);
  385. for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad)
  386. bytes += bkey_bytes(k);
  387. rw_unlock(false, b);
  388. return (bytes * 100) / btree_bytes(c);
  389. }
  390. static size_t bch_cache_size(struct cache_set *c)
  391. {
  392. size_t ret = 0;
  393. struct btree *b;
  394. mutex_lock(&c->bucket_lock);
  395. list_for_each_entry(b, &c->btree_cache, list)
  396. ret += 1 << (b->keys.page_order + PAGE_SHIFT);
  397. mutex_unlock(&c->bucket_lock);
  398. return ret;
  399. }
  400. static unsigned bch_cache_max_chain(struct cache_set *c)
  401. {
  402. unsigned ret = 0;
  403. struct hlist_head *h;
  404. mutex_lock(&c->bucket_lock);
  405. for (h = c->bucket_hash;
  406. h < c->bucket_hash + (1 << BUCKET_HASH_BITS);
  407. h++) {
  408. unsigned i = 0;
  409. struct hlist_node *p;
  410. hlist_for_each(p, h)
  411. i++;
  412. ret = max(ret, i);
  413. }
  414. mutex_unlock(&c->bucket_lock);
  415. return ret;
  416. }
  417. static unsigned bch_btree_used(struct cache_set *c)
  418. {
  419. return div64_u64(c->gc_stats.key_bytes * 100,
  420. (c->gc_stats.nodes ?: 1) * btree_bytes(c));
  421. }
  422. static unsigned bch_average_key_size(struct cache_set *c)
  423. {
  424. return c->gc_stats.nkeys
  425. ? div64_u64(c->gc_stats.data, c->gc_stats.nkeys)
  426. : 0;
  427. }
  428. SHOW(__bch_cache_set)
  429. {
  430. struct cache_set *c = container_of(kobj, struct cache_set, kobj);
  431. sysfs_print(synchronous, CACHE_SYNC(&c->sb));
  432. sysfs_print(journal_delay_ms, c->journal_delay_ms);
  433. sysfs_hprint(bucket_size, bucket_bytes(c));
  434. sysfs_hprint(block_size, block_bytes(c));
  435. sysfs_print(tree_depth, c->root->level);
  436. sysfs_print(root_usage_percent, bch_root_usage(c));
  437. sysfs_hprint(btree_cache_size, bch_cache_size(c));
  438. sysfs_print(btree_cache_max_chain, bch_cache_max_chain(c));
  439. sysfs_print(cache_available_percent, 100 - c->gc_stats.in_use);
  440. sysfs_print_time_stats(&c->btree_gc_time, btree_gc, sec, ms);
  441. sysfs_print_time_stats(&c->btree_split_time, btree_split, sec, us);
  442. sysfs_print_time_stats(&c->sort.time, btree_sort, ms, us);
  443. sysfs_print_time_stats(&c->btree_read_time, btree_read, ms, us);
  444. sysfs_print(btree_used_percent, bch_btree_used(c));
  445. sysfs_print(btree_nodes, c->gc_stats.nodes);
  446. sysfs_hprint(average_key_size, bch_average_key_size(c));
  447. sysfs_print(cache_read_races,
  448. atomic_long_read(&c->cache_read_races));
  449. sysfs_print(writeback_keys_done,
  450. atomic_long_read(&c->writeback_keys_done));
  451. sysfs_print(writeback_keys_failed,
  452. atomic_long_read(&c->writeback_keys_failed));
  453. if (attr == &sysfs_errors)
  454. return bch_snprint_string_list(buf, PAGE_SIZE, error_actions,
  455. c->on_error);
  456. /* See count_io_errors for why 88 */
  457. sysfs_print(io_error_halflife, c->error_decay * 88);
  458. sysfs_print(io_error_limit, c->error_limit >> IO_ERROR_SHIFT);
  459. sysfs_hprint(congested,
  460. ((uint64_t) bch_get_congested(c)) << 9);
  461. sysfs_print(congested_read_threshold_us,
  462. c->congested_read_threshold_us);
  463. sysfs_print(congested_write_threshold_us,
  464. c->congested_write_threshold_us);
  465. sysfs_print(active_journal_entries, fifo_used(&c->journal.pin));
  466. sysfs_printf(verify, "%i", c->verify);
  467. sysfs_printf(key_merging_disabled, "%i", c->key_merging_disabled);
  468. sysfs_printf(expensive_debug_checks,
  469. "%i", c->expensive_debug_checks);
  470. sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
  471. sysfs_printf(btree_shrinker_disabled, "%i", c->shrinker_disabled);
  472. sysfs_printf(copy_gc_enabled, "%i", c->copy_gc_enabled);
  473. if (attr == &sysfs_bset_tree_stats)
  474. return bch_bset_print_stats(c, buf);
  475. return 0;
  476. }
  477. SHOW_LOCKED(bch_cache_set)
  478. STORE(__bch_cache_set)
  479. {
  480. struct cache_set *c = container_of(kobj, struct cache_set, kobj);
  481. if (attr == &sysfs_unregister)
  482. bch_cache_set_unregister(c);
  483. if (attr == &sysfs_stop)
  484. bch_cache_set_stop(c);
  485. if (attr == &sysfs_synchronous) {
  486. bool sync = strtoul_or_return(buf);
  487. if (sync != CACHE_SYNC(&c->sb)) {
  488. SET_CACHE_SYNC(&c->sb, sync);
  489. bcache_write_super(c);
  490. }
  491. }
  492. if (attr == &sysfs_flash_vol_create) {
  493. int r;
  494. uint64_t v;
  495. strtoi_h_or_return(buf, v);
  496. r = bch_flash_dev_create(c, v);
  497. if (r)
  498. return r;
  499. }
  500. if (attr == &sysfs_clear_stats) {
  501. atomic_long_set(&c->writeback_keys_done, 0);
  502. atomic_long_set(&c->writeback_keys_failed, 0);
  503. memset(&c->gc_stats, 0, sizeof(struct gc_stat));
  504. bch_cache_accounting_clear(&c->accounting);
  505. }
  506. if (attr == &sysfs_trigger_gc)
  507. wake_up_gc(c);
  508. if (attr == &sysfs_prune_cache) {
  509. struct shrink_control sc;
  510. sc.gfp_mask = GFP_KERNEL;
  511. sc.nr_to_scan = strtoul_or_return(buf);
  512. c->shrink.scan_objects(&c->shrink, &sc);
  513. }
  514. sysfs_strtoul(congested_read_threshold_us,
  515. c->congested_read_threshold_us);
  516. sysfs_strtoul(congested_write_threshold_us,
  517. c->congested_write_threshold_us);
  518. if (attr == &sysfs_errors) {
  519. ssize_t v = bch_read_string_list(buf, error_actions);
  520. if (v < 0)
  521. return v;
  522. c->on_error = v;
  523. }
  524. if (attr == &sysfs_io_error_limit)
  525. c->error_limit = strtoul_or_return(buf) << IO_ERROR_SHIFT;
  526. /* See count_io_errors() for why 88 */
  527. if (attr == &sysfs_io_error_halflife)
  528. c->error_decay = strtoul_or_return(buf) / 88;
  529. sysfs_strtoul(journal_delay_ms, c->journal_delay_ms);
  530. sysfs_strtoul(verify, c->verify);
  531. sysfs_strtoul(key_merging_disabled, c->key_merging_disabled);
  532. sysfs_strtoul(expensive_debug_checks, c->expensive_debug_checks);
  533. sysfs_strtoul(gc_always_rewrite, c->gc_always_rewrite);
  534. sysfs_strtoul(btree_shrinker_disabled, c->shrinker_disabled);
  535. sysfs_strtoul(copy_gc_enabled, c->copy_gc_enabled);
  536. return size;
  537. }
  538. STORE_LOCKED(bch_cache_set)
  539. SHOW(bch_cache_set_internal)
  540. {
  541. struct cache_set *c = container_of(kobj, struct cache_set, internal);
  542. return bch_cache_set_show(&c->kobj, attr, buf);
  543. }
  544. STORE(bch_cache_set_internal)
  545. {
  546. struct cache_set *c = container_of(kobj, struct cache_set, internal);
  547. return bch_cache_set_store(&c->kobj, attr, buf, size);
  548. }
  549. static void bch_cache_set_internal_release(struct kobject *k)
  550. {
  551. }
  552. static struct attribute *bch_cache_set_files[] = {
  553. &sysfs_unregister,
  554. &sysfs_stop,
  555. &sysfs_synchronous,
  556. &sysfs_journal_delay_ms,
  557. &sysfs_flash_vol_create,
  558. &sysfs_bucket_size,
  559. &sysfs_block_size,
  560. &sysfs_tree_depth,
  561. &sysfs_root_usage_percent,
  562. &sysfs_btree_cache_size,
  563. &sysfs_cache_available_percent,
  564. &sysfs_average_key_size,
  565. &sysfs_errors,
  566. &sysfs_io_error_limit,
  567. &sysfs_io_error_halflife,
  568. &sysfs_congested,
  569. &sysfs_congested_read_threshold_us,
  570. &sysfs_congested_write_threshold_us,
  571. &sysfs_clear_stats,
  572. NULL
  573. };
  574. KTYPE(bch_cache_set);
  575. static struct attribute *bch_cache_set_internal_files[] = {
  576. &sysfs_active_journal_entries,
  577. sysfs_time_stats_attribute_list(btree_gc, sec, ms)
  578. sysfs_time_stats_attribute_list(btree_split, sec, us)
  579. sysfs_time_stats_attribute_list(btree_sort, ms, us)
  580. sysfs_time_stats_attribute_list(btree_read, ms, us)
  581. &sysfs_btree_nodes,
  582. &sysfs_btree_used_percent,
  583. &sysfs_btree_cache_max_chain,
  584. &sysfs_bset_tree_stats,
  585. &sysfs_cache_read_races,
  586. &sysfs_writeback_keys_done,
  587. &sysfs_writeback_keys_failed,
  588. &sysfs_trigger_gc,
  589. &sysfs_prune_cache,
  590. #ifdef CONFIG_BCACHE_DEBUG
  591. &sysfs_verify,
  592. &sysfs_key_merging_disabled,
  593. &sysfs_expensive_debug_checks,
  594. #endif
  595. &sysfs_gc_always_rewrite,
  596. &sysfs_btree_shrinker_disabled,
  597. &sysfs_copy_gc_enabled,
  598. NULL
  599. };
  600. KTYPE(bch_cache_set_internal);
  601. SHOW(__bch_cache)
  602. {
  603. struct cache *ca = container_of(kobj, struct cache, kobj);
  604. sysfs_hprint(bucket_size, bucket_bytes(ca));
  605. sysfs_hprint(block_size, block_bytes(ca));
  606. sysfs_print(nbuckets, ca->sb.nbuckets);
  607. sysfs_print(discard, ca->discard);
  608. sysfs_hprint(written, atomic_long_read(&ca->sectors_written) << 9);
  609. sysfs_hprint(btree_written,
  610. atomic_long_read(&ca->btree_sectors_written) << 9);
  611. sysfs_hprint(metadata_written,
  612. (atomic_long_read(&ca->meta_sectors_written) +
  613. atomic_long_read(&ca->btree_sectors_written)) << 9);
  614. sysfs_print(io_errors,
  615. atomic_read(&ca->io_errors) >> IO_ERROR_SHIFT);
  616. if (attr == &sysfs_cache_replacement_policy)
  617. return bch_snprint_string_list(buf, PAGE_SIZE,
  618. cache_replacement_policies,
  619. CACHE_REPLACEMENT(&ca->sb));
  620. if (attr == &sysfs_priority_stats) {
  621. int cmp(const void *l, const void *r)
  622. { return *((uint16_t *) r) - *((uint16_t *) l); }
  623. struct bucket *b;
  624. size_t n = ca->sb.nbuckets, i;
  625. size_t unused = 0, available = 0, dirty = 0, meta = 0;
  626. uint64_t sum = 0;
  627. /* Compute 31 quantiles */
  628. uint16_t q[31], *p, *cached;
  629. ssize_t ret;
  630. cached = p = vmalloc(ca->sb.nbuckets * sizeof(uint16_t));
  631. if (!p)
  632. return -ENOMEM;
  633. mutex_lock(&ca->set->bucket_lock);
  634. for_each_bucket(b, ca) {
  635. if (!GC_SECTORS_USED(b))
  636. unused++;
  637. if (GC_MARK(b) == GC_MARK_RECLAIMABLE)
  638. available++;
  639. if (GC_MARK(b) == GC_MARK_DIRTY)
  640. dirty++;
  641. if (GC_MARK(b) == GC_MARK_METADATA)
  642. meta++;
  643. }
  644. for (i = ca->sb.first_bucket; i < n; i++)
  645. p[i] = ca->buckets[i].prio;
  646. mutex_unlock(&ca->set->bucket_lock);
  647. sort(p, n, sizeof(uint16_t), cmp, NULL);
  648. while (n &&
  649. !cached[n - 1])
  650. --n;
  651. unused = ca->sb.nbuckets - n;
  652. while (cached < p + n &&
  653. *cached == BTREE_PRIO)
  654. cached++, n--;
  655. for (i = 0; i < n; i++)
  656. sum += INITIAL_PRIO - cached[i];
  657. if (n)
  658. do_div(sum, n);
  659. for (i = 0; i < ARRAY_SIZE(q); i++)
  660. q[i] = INITIAL_PRIO - cached[n * (i + 1) /
  661. (ARRAY_SIZE(q) + 1)];
  662. vfree(p);
  663. ret = scnprintf(buf, PAGE_SIZE,
  664. "Unused: %zu%%\n"
  665. "Clean: %zu%%\n"
  666. "Dirty: %zu%%\n"
  667. "Metadata: %zu%%\n"
  668. "Average: %llu\n"
  669. "Sectors per Q: %zu\n"
  670. "Quantiles: [",
  671. unused * 100 / (size_t) ca->sb.nbuckets,
  672. available * 100 / (size_t) ca->sb.nbuckets,
  673. dirty * 100 / (size_t) ca->sb.nbuckets,
  674. meta * 100 / (size_t) ca->sb.nbuckets, sum,
  675. n * ca->sb.bucket_size / (ARRAY_SIZE(q) + 1));
  676. for (i = 0; i < ARRAY_SIZE(q); i++)
  677. ret += scnprintf(buf + ret, PAGE_SIZE - ret,
  678. "%u ", q[i]);
  679. ret--;
  680. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "]\n");
  681. return ret;
  682. }
  683. return 0;
  684. }
  685. SHOW_LOCKED(bch_cache)
  686. STORE(__bch_cache)
  687. {
  688. struct cache *ca = container_of(kobj, struct cache, kobj);
  689. if (attr == &sysfs_discard) {
  690. bool v = strtoul_or_return(buf);
  691. if (blk_queue_discard(bdev_get_queue(ca->bdev)))
  692. ca->discard = v;
  693. if (v != CACHE_DISCARD(&ca->sb)) {
  694. SET_CACHE_DISCARD(&ca->sb, v);
  695. bcache_write_super(ca->set);
  696. }
  697. }
  698. if (attr == &sysfs_cache_replacement_policy) {
  699. ssize_t v = bch_read_string_list(buf, cache_replacement_policies);
  700. if (v < 0)
  701. return v;
  702. if ((unsigned) v != CACHE_REPLACEMENT(&ca->sb)) {
  703. mutex_lock(&ca->set->bucket_lock);
  704. SET_CACHE_REPLACEMENT(&ca->sb, v);
  705. mutex_unlock(&ca->set->bucket_lock);
  706. bcache_write_super(ca->set);
  707. }
  708. }
  709. if (attr == &sysfs_clear_stats) {
  710. atomic_long_set(&ca->sectors_written, 0);
  711. atomic_long_set(&ca->btree_sectors_written, 0);
  712. atomic_long_set(&ca->meta_sectors_written, 0);
  713. atomic_set(&ca->io_count, 0);
  714. atomic_set(&ca->io_errors, 0);
  715. }
  716. return size;
  717. }
  718. STORE_LOCKED(bch_cache)
  719. static struct attribute *bch_cache_files[] = {
  720. &sysfs_bucket_size,
  721. &sysfs_block_size,
  722. &sysfs_nbuckets,
  723. &sysfs_priority_stats,
  724. &sysfs_discard,
  725. &sysfs_written,
  726. &sysfs_btree_written,
  727. &sysfs_metadata_written,
  728. &sysfs_io_errors,
  729. &sysfs_clear_stats,
  730. &sysfs_cache_replacement_policy,
  731. NULL
  732. };
  733. KTYPE(bch_cache);