]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/ceph-client-debug.cc
update sources to ceph Nautilus 14.2.1
[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->dentries.empty()) {
43 return;
44 }
45
46 Dentry* dn = *(ino->dentries.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->dentries.empty()) {
65 Dentry *dn = *(inode->dentries.begin());
66 ceph_assert(dn->dir);
67 ceph_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 ceph_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 if (args.empty()) {
88 cerr << argv[0] << ": -h or --help for usage" << std::endl;
89 exit(1);
90 }
91 if (ceph_argparse_need_usage(args)) {
92 usage();
93 exit(0);
94 }
95
96 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
97 CODE_ENVIRONMENT_UTILITY,
98 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS|
99 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
100
101 common_init_finish(g_ceph_context);
102
103 // Expect exactly one positional argument (inode number)
104 if (args.size() != 1) {
105 cerr << "missing position argument (inode number)" << std::endl;
106 exit(1);
107 }
108 char const *inode_str = args[0];
109 inodeno_t inode = strtoll(inode_str, NULL, 0);
110 if (inode <= 0) {
111 derr << "Invalid inode: " << inode_str << dendl;
112 return -1;
113 }
114
115 // Initialize filesystem client
116 struct ceph_mount_info *client;
117 int r = ceph_create_with_context(&client, g_ceph_context);
118 if (r) {
119 derr << "Error initializing libcephfs: " << cpp_strerror(r) << dendl;
120 return r;
121 }
122
123 r = ceph_mount(client, "/");
124 if (r) {
125 derr << "Error mounting: " << cpp_strerror(r) << dendl;
126 ceph_shutdown(client);
127 return r;
128 }
129
130
131 // Populate client cache with inode of interest & ancestors
132 r = lookup_trace(client, inode);
133 if (r) {
134 derr << "Error looking up inode " << std::hex << inode << std::dec <<
135 ": " << cpp_strerror(r) << dendl;
136 return -1;
137 }
138
139 // Retrieve inode of interest
140 struct vinodeno_t vinode;
141 vinode.ino = inode;
142 vinode.snapid = CEPH_NOSNAP;
143 Inode *ino = ceph_ll_get_inode(client, vinode);
144
145 // Retrieve dentry trace
146 std::vector<Dentry*> path;
147 traverse_dentries(ino, path);
148
149 // Print inode and path as a JSON object
150 JSONFormatter jf(true);
151 jf.open_object_section("client_debug");
152 {
153 jf.open_object_section("inode");
154 {
155 ino->dump(&jf);
156 }
157 jf.close_section(); // inode
158 jf.open_array_section("path");
159 {
160 for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
161 jf.open_object_section("dentry");
162 {
163 (*p)->dump(&jf);
164 }
165 jf.close_section(); // dentry
166 }
167 }
168 jf.close_section(); // path
169 }
170 jf.close_section(); // client_debug
171 jf.flush(std::cout);
172 std::cout << std::endl;
173
174 // Release Inode references
175 ceph_ll_forget(client, ino, 1);
176 for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
177 ceph_ll_forget(client, (*p)->inode.get(), 1);
178 }
179 ino = NULL;
180 path.clear();
181
182 // Shut down
183 r = ceph_unmount(client);
184 if (r) {
185 derr << "Error mounting: " << cpp_strerror(r) << dendl;
186 }
187 ceph_shutdown(client);
188
189 return r;
190 }