]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/ceph-client-debug.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / tools / ceph-client-debug.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15
16 #include "common/ceph_argparse.h"
17 #include "global/global_init.h"
18 #include "common/Formatter.h"
19 #include "common/debug.h"
20 #include "common/errno.h"
21 #include "client/Inode.h"
22 #include "client/Dentry.h"
23 #include "client/Dir.h"
24 #include "include/cephfs/libcephfs.h"
25
26 #define dout_context g_ceph_context
27 #define dout_subsys ceph_subsys_client
28
29 void usage()
30 {
31 std::cout << "Usage: ceph-client-debug [options] <inode number>" << std::endl;
32 generic_client_usage();
33 }
34
35
36 /**
37 * Given an inode, look up the path from the Client cache: assumes
38 * client cache is fully populated.
39 */
40 void traverse_dentries(Inode *ino, std::vector<Dentry*> &parts)
41 {
42 if (ino->dn_set.empty()) {
43 return;
44 }
45
46 Dentry* dn = *(ino->dn_set.begin());
47 parts.push_back(dn);
48 traverse_dentries(dn->dir->parent_inode, parts);
49 }
50
51
52 /**
53 * Given an inode, send lookup requests to the MDS for
54 * all its ancestors, such that the full trace will be
55 * populated in client cache.
56 */
57 int lookup_trace(ceph_mount_info *client, inodeno_t const ino)
58 {
59 Inode *inode;
60 int r = ceph_ll_lookup_inode(client, ino, &inode);
61 if (r != 0) {
62 return r;
63 } else {
64 if (!inode->dn_set.empty()) {
65 Dentry *dn = *(inode->dn_set.begin());
66 assert(dn->dir);
67 assert(dn->dir->parent_inode);
68 r = lookup_trace(client, dn->dir->parent_inode->ino);
69 if (r) {
70 return r;
71 }
72 } else {
73 // We reached the root of the tree
74 assert(inode->ino == CEPH_INO_ROOT);
75 }
76 }
77
78 return r;
79 }
80
81
82 int main(int argc, const char **argv)
83 {
84 // Argument handling
85 vector<const char*> args;
86 argv_to_vec(argc, argv, args);
87 env_to_vec(args);
88
89 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
90 CODE_ENVIRONMENT_UTILITY,
91 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS);
92
93 common_init_finish(g_ceph_context);
94
95 // Expect exactly one positional argument (inode number)
96 if (args.size() != 1) {
97 usage();
98 }
99 char const *inode_str = args[0];
100 inodeno_t inode = strtoll(inode_str, NULL, 0);
101 if (inode <= 0) {
102 derr << "Invalid inode: " << inode_str << dendl;
103 return -1;
104 }
105
106 // Initialize filesystem client
107 struct ceph_mount_info *client;
108 int r = ceph_create_with_context(&client, g_ceph_context);
109 if (r) {
110 derr << "Error initializing libcephfs: " << cpp_strerror(r) << dendl;
111 return r;
112 }
113
114 r = ceph_mount(client, "/");
115 if (r) {
116 derr << "Error mounting: " << cpp_strerror(r) << dendl;
117 ceph_shutdown(client);
118 return r;
119 }
120
121
122 // Populate client cache with inode of interest & ancestors
123 r = lookup_trace(client, inode);
124 if (r) {
125 derr << "Error looking up inode " << std::hex << inode << std::dec <<
126 ": " << cpp_strerror(r) << dendl;
127 return -1;
128 }
129
130 // Retrieve inode of interest
131 struct vinodeno_t vinode;
132 vinode.ino = inode;
133 vinode.snapid = CEPH_NOSNAP;
134 Inode *ino = ceph_ll_get_inode(client, vinode);
135
136 // Retrieve dentry trace
137 std::vector<Dentry*> path;
138 traverse_dentries(ino, path);
139
140 // Print inode and path as a JSON object
141 JSONFormatter jf(true);
142 jf.open_object_section("client_debug");
143 {
144 jf.open_object_section("inode");
145 {
146 ino->dump(&jf);
147 }
148 jf.close_section(); // inode
149 jf.open_array_section("path");
150 {
151 for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
152 jf.open_object_section("dentry");
153 {
154 (*p)->dump(&jf);
155 }
156 jf.close_section(); // dentry
157 }
158 }
159 jf.close_section(); // path
160 }
161 jf.close_section(); // client_debug
162 jf.flush(std::cout);
163 std::cout << std::endl;
164
165 // Release Inode references
166 ceph_ll_forget(client, ino, 1);
167 for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
168 ceph_ll_forget(client, (*p)->inode.get(), 1);
169 }
170 ino = NULL;
171 path.clear();
172
173 // Shut down
174 r = ceph_unmount(client);
175 if (r) {
176 derr << "Error mounting: " << cpp_strerror(r) << dendl;
177 }
178 ceph_shutdown(client);
179
180 return r;
181 }