debugfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/device.h>
  4. #include <linux/slab.h>
  5. #include <linux/module.h>
  6. #include <linux/ctype.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/ceph/libceph.h>
  10. #include <linux/ceph/mon_client.h>
  11. #include <linux/ceph/auth.h>
  12. #include <linux/ceph/debugfs.h>
  13. #ifdef CONFIG_DEBUG_FS
  14. /*
  15. * Implement /sys/kernel/debug/ceph fun
  16. *
  17. * /sys/kernel/debug/ceph/client* - an instance of the ceph client
  18. * .../osdmap - current osdmap
  19. * .../monmap - current monmap
  20. * .../osdc - active osd requests
  21. * .../monc - mon client state
  22. * .../client_options - libceph-only (i.e. not rbd or cephfs) options
  23. * .../dentry_lru - dump contents of dentry lru
  24. * .../caps - expose cap (reservation) stats
  25. * .../bdi - symlink to ../../bdi/something
  26. */
  27. static struct dentry *ceph_debugfs_dir;
  28. static int monmap_show(struct seq_file *s, void *p)
  29. {
  30. int i;
  31. struct ceph_client *client = s->private;
  32. if (client->monc.monmap == NULL)
  33. return 0;
  34. seq_printf(s, "epoch %d\n", client->monc.monmap->epoch);
  35. for (i = 0; i < client->monc.monmap->num_mon; i++) {
  36. struct ceph_entity_inst *inst =
  37. &client->monc.monmap->mon_inst[i];
  38. seq_printf(s, "\t%s%lld\t%s\n",
  39. ENTITY_NAME(inst->name),
  40. ceph_pr_addr(&inst->addr.in_addr));
  41. }
  42. return 0;
  43. }
  44. static int osdmap_show(struct seq_file *s, void *p)
  45. {
  46. int i;
  47. struct ceph_client *client = s->private;
  48. struct ceph_osd_client *osdc = &client->osdc;
  49. struct ceph_osdmap *map = osdc->osdmap;
  50. struct rb_node *n;
  51. if (map == NULL)
  52. return 0;
  53. down_read(&osdc->lock);
  54. seq_printf(s, "epoch %u barrier %u flags 0x%x\n", map->epoch,
  55. osdc->epoch_barrier, map->flags);
  56. for (n = rb_first(&map->pg_pools); n; n = rb_next(n)) {
  57. struct ceph_pg_pool_info *pi =
  58. rb_entry(n, struct ceph_pg_pool_info, node);
  59. seq_printf(s, "pool %lld '%s' type %d size %d min_size %d pg_num %u pg_num_mask %d flags 0x%llx lfor %u read_tier %lld write_tier %lld\n",
  60. pi->id, pi->name, pi->type, pi->size, pi->min_size,
  61. pi->pg_num, pi->pg_num_mask, pi->flags,
  62. pi->last_force_request_resend, pi->read_tier,
  63. pi->write_tier);
  64. }
  65. for (i = 0; i < map->max_osd; i++) {
  66. struct ceph_entity_addr *addr = &map->osd_addr[i];
  67. u32 state = map->osd_state[i];
  68. char sb[64];
  69. seq_printf(s, "osd%d\t%s\t%3d%%\t(%s)\t%3d%%\n",
  70. i, ceph_pr_addr(&addr->in_addr),
  71. ((map->osd_weight[i]*100) >> 16),
  72. ceph_osdmap_state_str(sb, sizeof(sb), state),
  73. ((ceph_get_primary_affinity(map, i)*100) >> 16));
  74. }
  75. for (n = rb_first(&map->pg_temp); n; n = rb_next(n)) {
  76. struct ceph_pg_mapping *pg =
  77. rb_entry(n, struct ceph_pg_mapping, node);
  78. seq_printf(s, "pg_temp %llu.%x [", pg->pgid.pool,
  79. pg->pgid.seed);
  80. for (i = 0; i < pg->pg_temp.len; i++)
  81. seq_printf(s, "%s%d", (i == 0 ? "" : ","),
  82. pg->pg_temp.osds[i]);
  83. seq_printf(s, "]\n");
  84. }
  85. for (n = rb_first(&map->primary_temp); n; n = rb_next(n)) {
  86. struct ceph_pg_mapping *pg =
  87. rb_entry(n, struct ceph_pg_mapping, node);
  88. seq_printf(s, "primary_temp %llu.%x %d\n", pg->pgid.pool,
  89. pg->pgid.seed, pg->primary_temp.osd);
  90. }
  91. for (n = rb_first(&map->pg_upmap); n; n = rb_next(n)) {
  92. struct ceph_pg_mapping *pg =
  93. rb_entry(n, struct ceph_pg_mapping, node);
  94. seq_printf(s, "pg_upmap %llu.%x [", pg->pgid.pool,
  95. pg->pgid.seed);
  96. for (i = 0; i < pg->pg_upmap.len; i++)
  97. seq_printf(s, "%s%d", (i == 0 ? "" : ","),
  98. pg->pg_upmap.osds[i]);
  99. seq_printf(s, "]\n");
  100. }
  101. for (n = rb_first(&map->pg_upmap_items); n; n = rb_next(n)) {
  102. struct ceph_pg_mapping *pg =
  103. rb_entry(n, struct ceph_pg_mapping, node);
  104. seq_printf(s, "pg_upmap_items %llu.%x [", pg->pgid.pool,
  105. pg->pgid.seed);
  106. for (i = 0; i < pg->pg_upmap_items.len; i++)
  107. seq_printf(s, "%s%d->%d", (i == 0 ? "" : ","),
  108. pg->pg_upmap_items.from_to[i][0],
  109. pg->pg_upmap_items.from_to[i][1]);
  110. seq_printf(s, "]\n");
  111. }
  112. up_read(&osdc->lock);
  113. return 0;
  114. }
  115. static int monc_show(struct seq_file *s, void *p)
  116. {
  117. struct ceph_client *client = s->private;
  118. struct ceph_mon_generic_request *req;
  119. struct ceph_mon_client *monc = &client->monc;
  120. struct rb_node *rp;
  121. int i;
  122. mutex_lock(&monc->mutex);
  123. for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
  124. seq_printf(s, "have %s %u", ceph_sub_str[i],
  125. monc->subs[i].have);
  126. if (monc->subs[i].want)
  127. seq_printf(s, " want %llu%s",
  128. le64_to_cpu(monc->subs[i].item.start),
  129. (monc->subs[i].item.flags &
  130. CEPH_SUBSCRIBE_ONETIME ? "" : "+"));
  131. seq_putc(s, '\n');
  132. }
  133. seq_printf(s, "fs_cluster_id %d\n", monc->fs_cluster_id);
  134. for (rp = rb_first(&monc->generic_request_tree); rp; rp = rb_next(rp)) {
  135. __u16 op;
  136. req = rb_entry(rp, struct ceph_mon_generic_request, node);
  137. op = le16_to_cpu(req->request->hdr.type);
  138. if (op == CEPH_MSG_STATFS)
  139. seq_printf(s, "%llu statfs\n", req->tid);
  140. else if (op == CEPH_MSG_MON_GET_VERSION)
  141. seq_printf(s, "%llu mon_get_version", req->tid);
  142. else
  143. seq_printf(s, "%llu unknown\n", req->tid);
  144. }
  145. mutex_unlock(&monc->mutex);
  146. return 0;
  147. }
  148. static void dump_spgid(struct seq_file *s, const struct ceph_spg *spgid)
  149. {
  150. seq_printf(s, "%llu.%x", spgid->pgid.pool, spgid->pgid.seed);
  151. if (spgid->shard != CEPH_SPG_NOSHARD)
  152. seq_printf(s, "s%d", spgid->shard);
  153. }
  154. static void dump_target(struct seq_file *s, struct ceph_osd_request_target *t)
  155. {
  156. int i;
  157. seq_printf(s, "osd%d\t%llu.%x\t", t->osd, t->pgid.pool, t->pgid.seed);
  158. dump_spgid(s, &t->spgid);
  159. seq_puts(s, "\t[");
  160. for (i = 0; i < t->up.size; i++)
  161. seq_printf(s, "%s%d", (!i ? "" : ","), t->up.osds[i]);
  162. seq_printf(s, "]/%d\t[", t->up.primary);
  163. for (i = 0; i < t->acting.size; i++)
  164. seq_printf(s, "%s%d", (!i ? "" : ","), t->acting.osds[i]);
  165. seq_printf(s, "]/%d\te%u\t", t->acting.primary, t->epoch);
  166. if (t->target_oloc.pool_ns) {
  167. seq_printf(s, "%*pE/%*pE\t0x%x",
  168. (int)t->target_oloc.pool_ns->len,
  169. t->target_oloc.pool_ns->str,
  170. t->target_oid.name_len, t->target_oid.name, t->flags);
  171. } else {
  172. seq_printf(s, "%*pE\t0x%x", t->target_oid.name_len,
  173. t->target_oid.name, t->flags);
  174. }
  175. if (t->paused)
  176. seq_puts(s, "\tP");
  177. }
  178. static void dump_request(struct seq_file *s, struct ceph_osd_request *req)
  179. {
  180. int i;
  181. seq_printf(s, "%llu\t", req->r_tid);
  182. dump_target(s, &req->r_t);
  183. seq_printf(s, "\t%d", req->r_attempts);
  184. for (i = 0; i < req->r_num_ops; i++) {
  185. struct ceph_osd_req_op *op = &req->r_ops[i];
  186. seq_printf(s, "%s%s", (i == 0 ? "\t" : ","),
  187. ceph_osd_op_name(op->op));
  188. if (op->op == CEPH_OSD_OP_WATCH)
  189. seq_printf(s, "-%s",
  190. ceph_osd_watch_op_name(op->watch.op));
  191. }
  192. seq_putc(s, '\n');
  193. }
  194. static void dump_requests(struct seq_file *s, struct ceph_osd *osd)
  195. {
  196. struct rb_node *n;
  197. mutex_lock(&osd->lock);
  198. for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
  199. struct ceph_osd_request *req =
  200. rb_entry(n, struct ceph_osd_request, r_node);
  201. dump_request(s, req);
  202. }
  203. mutex_unlock(&osd->lock);
  204. }
  205. static void dump_linger_request(struct seq_file *s,
  206. struct ceph_osd_linger_request *lreq)
  207. {
  208. seq_printf(s, "%llu\t", lreq->linger_id);
  209. dump_target(s, &lreq->t);
  210. seq_printf(s, "\t%u\t%s%s/%d\n", lreq->register_gen,
  211. lreq->is_watch ? "W" : "N", lreq->committed ? "C" : "",
  212. lreq->last_error);
  213. }
  214. static void dump_linger_requests(struct seq_file *s, struct ceph_osd *osd)
  215. {
  216. struct rb_node *n;
  217. mutex_lock(&osd->lock);
  218. for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
  219. struct ceph_osd_linger_request *lreq =
  220. rb_entry(n, struct ceph_osd_linger_request, node);
  221. dump_linger_request(s, lreq);
  222. }
  223. mutex_unlock(&osd->lock);
  224. }
  225. static void dump_snapid(struct seq_file *s, u64 snapid)
  226. {
  227. if (snapid == CEPH_NOSNAP)
  228. seq_puts(s, "head");
  229. else if (snapid == CEPH_SNAPDIR)
  230. seq_puts(s, "snapdir");
  231. else
  232. seq_printf(s, "%llx", snapid);
  233. }
  234. static void dump_name_escaped(struct seq_file *s, unsigned char *name,
  235. size_t len)
  236. {
  237. size_t i;
  238. for (i = 0; i < len; i++) {
  239. if (name[i] == '%' || name[i] == ':' || name[i] == '/' ||
  240. name[i] < 32 || name[i] >= 127) {
  241. seq_printf(s, "%%%02x", name[i]);
  242. } else {
  243. seq_putc(s, name[i]);
  244. }
  245. }
  246. }
  247. static void dump_hoid(struct seq_file *s, const struct ceph_hobject_id *hoid)
  248. {
  249. if (hoid->snapid == 0 && hoid->hash == 0 && !hoid->is_max &&
  250. hoid->pool == S64_MIN) {
  251. seq_puts(s, "MIN");
  252. return;
  253. }
  254. if (hoid->is_max) {
  255. seq_puts(s, "MAX");
  256. return;
  257. }
  258. seq_printf(s, "%lld:%08x:", hoid->pool, hoid->hash_reverse_bits);
  259. dump_name_escaped(s, hoid->nspace, hoid->nspace_len);
  260. seq_putc(s, ':');
  261. dump_name_escaped(s, hoid->key, hoid->key_len);
  262. seq_putc(s, ':');
  263. dump_name_escaped(s, hoid->oid, hoid->oid_len);
  264. seq_putc(s, ':');
  265. dump_snapid(s, hoid->snapid);
  266. }
  267. static void dump_backoffs(struct seq_file *s, struct ceph_osd *osd)
  268. {
  269. struct rb_node *n;
  270. mutex_lock(&osd->lock);
  271. for (n = rb_first(&osd->o_backoffs_by_id); n; n = rb_next(n)) {
  272. struct ceph_osd_backoff *backoff =
  273. rb_entry(n, struct ceph_osd_backoff, id_node);
  274. seq_printf(s, "osd%d\t", osd->o_osd);
  275. dump_spgid(s, &backoff->spgid);
  276. seq_printf(s, "\t%llu\t", backoff->id);
  277. dump_hoid(s, backoff->begin);
  278. seq_putc(s, '\t');
  279. dump_hoid(s, backoff->end);
  280. seq_putc(s, '\n');
  281. }
  282. mutex_unlock(&osd->lock);
  283. }
  284. static int osdc_show(struct seq_file *s, void *pp)
  285. {
  286. struct ceph_client *client = s->private;
  287. struct ceph_osd_client *osdc = &client->osdc;
  288. struct rb_node *n;
  289. down_read(&osdc->lock);
  290. seq_printf(s, "REQUESTS %d homeless %d\n",
  291. atomic_read(&osdc->num_requests),
  292. atomic_read(&osdc->num_homeless));
  293. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  294. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  295. dump_requests(s, osd);
  296. }
  297. dump_requests(s, &osdc->homeless_osd);
  298. seq_puts(s, "LINGER REQUESTS\n");
  299. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  300. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  301. dump_linger_requests(s, osd);
  302. }
  303. dump_linger_requests(s, &osdc->homeless_osd);
  304. seq_puts(s, "BACKOFFS\n");
  305. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  306. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  307. dump_backoffs(s, osd);
  308. }
  309. up_read(&osdc->lock);
  310. return 0;
  311. }
  312. static int client_options_show(struct seq_file *s, void *p)
  313. {
  314. struct ceph_client *client = s->private;
  315. int ret;
  316. ret = ceph_print_client_options(s, client);
  317. if (ret)
  318. return ret;
  319. seq_putc(s, '\n');
  320. return 0;
  321. }
  322. CEPH_DEFINE_SHOW_FUNC(monmap_show)
  323. CEPH_DEFINE_SHOW_FUNC(osdmap_show)
  324. CEPH_DEFINE_SHOW_FUNC(monc_show)
  325. CEPH_DEFINE_SHOW_FUNC(osdc_show)
  326. CEPH_DEFINE_SHOW_FUNC(client_options_show)
  327. int __init ceph_debugfs_init(void)
  328. {
  329. ceph_debugfs_dir = debugfs_create_dir("ceph", NULL);
  330. if (!ceph_debugfs_dir)
  331. return -ENOMEM;
  332. return 0;
  333. }
  334. void ceph_debugfs_cleanup(void)
  335. {
  336. debugfs_remove(ceph_debugfs_dir);
  337. }
  338. int ceph_debugfs_client_init(struct ceph_client *client)
  339. {
  340. int ret = -ENOMEM;
  341. char name[80];
  342. snprintf(name, sizeof(name), "%pU.client%lld", &client->fsid,
  343. client->monc.auth->global_id);
  344. dout("ceph_debugfs_client_init %p %s\n", client, name);
  345. BUG_ON(client->debugfs_dir);
  346. client->debugfs_dir = debugfs_create_dir(name, ceph_debugfs_dir);
  347. if (!client->debugfs_dir)
  348. goto out;
  349. client->monc.debugfs_file = debugfs_create_file("monc",
  350. 0400,
  351. client->debugfs_dir,
  352. client,
  353. &monc_show_fops);
  354. if (!client->monc.debugfs_file)
  355. goto out;
  356. client->osdc.debugfs_file = debugfs_create_file("osdc",
  357. 0400,
  358. client->debugfs_dir,
  359. client,
  360. &osdc_show_fops);
  361. if (!client->osdc.debugfs_file)
  362. goto out;
  363. client->debugfs_monmap = debugfs_create_file("monmap",
  364. 0400,
  365. client->debugfs_dir,
  366. client,
  367. &monmap_show_fops);
  368. if (!client->debugfs_monmap)
  369. goto out;
  370. client->debugfs_osdmap = debugfs_create_file("osdmap",
  371. 0400,
  372. client->debugfs_dir,
  373. client,
  374. &osdmap_show_fops);
  375. if (!client->debugfs_osdmap)
  376. goto out;
  377. client->debugfs_options = debugfs_create_file("client_options",
  378. 0400,
  379. client->debugfs_dir,
  380. client,
  381. &client_options_show_fops);
  382. if (!client->debugfs_options)
  383. goto out;
  384. return 0;
  385. out:
  386. ceph_debugfs_client_cleanup(client);
  387. return ret;
  388. }
  389. void ceph_debugfs_client_cleanup(struct ceph_client *client)
  390. {
  391. dout("ceph_debugfs_client_cleanup %p\n", client);
  392. debugfs_remove(client->debugfs_options);
  393. debugfs_remove(client->debugfs_osdmap);
  394. debugfs_remove(client->debugfs_monmap);
  395. debugfs_remove(client->osdc.debugfs_file);
  396. debugfs_remove(client->monc.debugfs_file);
  397. debugfs_remove(client->debugfs_dir);
  398. }
  399. #else /* CONFIG_DEBUG_FS */
  400. int __init ceph_debugfs_init(void)
  401. {
  402. return 0;
  403. }
  404. void ceph_debugfs_cleanup(void)
  405. {
  406. }
  407. int ceph_debugfs_client_init(struct ceph_client *client)
  408. {
  409. return 0;
  410. }
  411. void ceph_debugfs_client_cleanup(struct ceph_client *client)
  412. {
  413. }
  414. #endif /* CONFIG_DEBUG_FS */