]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/ceph-client-debug.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / tools / ceph-client-debug.cc
CommitLineData
7c673cae
FG
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
20effc67
TL
29using namespace std;
30
7c673cae
FG
31void usage()
32{
33 std::cout << "Usage: ceph-client-debug [options] <inode number>" << std::endl;
34 generic_client_usage();
35}
36
37
38/**
39 * Given an inode, look up the path from the Client cache: assumes
40 * client cache is fully populated.
41 */
42void traverse_dentries(Inode *ino, std::vector<Dentry*> &parts)
43{
11fdf7f2 44 if (ino->dentries.empty()) {
7c673cae
FG
45 return;
46 }
47
11fdf7f2 48 Dentry* dn = *(ino->dentries.begin());
7c673cae
FG
49 parts.push_back(dn);
50 traverse_dentries(dn->dir->parent_inode, parts);
51}
52
53
54/**
55 * Given an inode, send lookup requests to the MDS for
56 * all its ancestors, such that the full trace will be
57 * populated in client cache.
58 */
59int lookup_trace(ceph_mount_info *client, inodeno_t const ino)
60{
61 Inode *inode;
62 int r = ceph_ll_lookup_inode(client, ino, &inode);
63 if (r != 0) {
64 return r;
65 } else {
11fdf7f2
TL
66 if (!inode->dentries.empty()) {
67 Dentry *dn = *(inode->dentries.begin());
68 ceph_assert(dn->dir);
69 ceph_assert(dn->dir->parent_inode);
7c673cae
FG
70 r = lookup_trace(client, dn->dir->parent_inode->ino);
71 if (r) {
72 return r;
73 }
74 } else {
75 // We reached the root of the tree
11fdf7f2 76 ceph_assert(inode->ino == CEPH_INO_ROOT);
7c673cae
FG
77 }
78 }
79
80 return r;
81}
82
83
84int main(int argc, const char **argv)
85{
86 // Argument handling
20effc67 87 auto args = argv_to_vec(argc, argv);
11fdf7f2
TL
88 if (args.empty()) {
89 cerr << argv[0] << ": -h or --help for usage" << std::endl;
90 exit(1);
91 }
92 if (ceph_argparse_need_usage(args)) {
93 usage();
94 exit(0);
95 }
7c673cae
FG
96
97 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
98 CODE_ENVIRONMENT_UTILITY,
11fdf7f2
TL
99 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS|
100 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
7c673cae
FG
101
102 common_init_finish(g_ceph_context);
103
104 // Expect exactly one positional argument (inode number)
105 if (args.size() != 1) {
11fdf7f2
TL
106 cerr << "missing position argument (inode number)" << std::endl;
107 exit(1);
7c673cae
FG
108 }
109 char const *inode_str = args[0];
110 inodeno_t inode = strtoll(inode_str, NULL, 0);
111 if (inode <= 0) {
112 derr << "Invalid inode: " << inode_str << dendl;
113 return -1;
114 }
115
116 // Initialize filesystem client
117 struct ceph_mount_info *client;
118 int r = ceph_create_with_context(&client, g_ceph_context);
119 if (r) {
120 derr << "Error initializing libcephfs: " << cpp_strerror(r) << dendl;
121 return r;
122 }
123
124 r = ceph_mount(client, "/");
125 if (r) {
126 derr << "Error mounting: " << cpp_strerror(r) << dendl;
127 ceph_shutdown(client);
128 return r;
129 }
130
131
132 // Populate client cache with inode of interest & ancestors
133 r = lookup_trace(client, inode);
134 if (r) {
135 derr << "Error looking up inode " << std::hex << inode << std::dec <<
136 ": " << cpp_strerror(r) << dendl;
137 return -1;
138 }
139
140 // Retrieve inode of interest
141 struct vinodeno_t vinode;
142 vinode.ino = inode;
143 vinode.snapid = CEPH_NOSNAP;
144 Inode *ino = ceph_ll_get_inode(client, vinode);
145
146 // Retrieve dentry trace
147 std::vector<Dentry*> path;
148 traverse_dentries(ino, path);
149
150 // Print inode and path as a JSON object
151 JSONFormatter jf(true);
152 jf.open_object_section("client_debug");
153 {
154 jf.open_object_section("inode");
155 {
156 ino->dump(&jf);
157 }
158 jf.close_section(); // inode
159 jf.open_array_section("path");
160 {
161 for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
162 jf.open_object_section("dentry");
163 {
164 (*p)->dump(&jf);
165 }
166 jf.close_section(); // dentry
167 }
168 }
169 jf.close_section(); // path
170 }
171 jf.close_section(); // client_debug
172 jf.flush(std::cout);
173 std::cout << std::endl;
174
175 // Release Inode references
176 ceph_ll_forget(client, ino, 1);
177 for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
178 ceph_ll_forget(client, (*p)->inode.get(), 1);
179 }
180 ino = NULL;
181 path.clear();
182
183 // Shut down
184 r = ceph_unmount(client);
185 if (r) {
186 derr << "Error mounting: " << cpp_strerror(r) << dendl;
187 }
188 ceph_shutdown(client);
189
190 return r;
191}