debugfs.c 13 KB

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