1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
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"
26 #define dout_context g_ceph_context
27 #define dout_subsys ceph_subsys_client
31 std::cout
<< "Usage: ceph-client-debug [options] <inode number>" << std::endl
;
32 generic_client_usage();
37 * Given an inode, look up the path from the Client cache: assumes
38 * client cache is fully populated.
40 void traverse_dentries(Inode
*ino
, std::vector
<Dentry
*> &parts
)
42 if (ino
->dentries
.empty()) {
46 Dentry
* dn
= *(ino
->dentries
.begin());
48 traverse_dentries(dn
->dir
->parent_inode
, parts
);
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.
57 int lookup_trace(ceph_mount_info
*client
, inodeno_t
const ino
)
60 int r
= ceph_ll_lookup_inode(client
, ino
, &inode
);
64 if (!inode
->dentries
.empty()) {
65 Dentry
*dn
= *(inode
->dentries
.begin());
67 ceph_assert(dn
->dir
->parent_inode
);
68 r
= lookup_trace(client
, dn
->dir
->parent_inode
->ino
);
73 // We reached the root of the tree
74 ceph_assert(inode
->ino
== CEPH_INO_ROOT
);
82 int main(int argc
, const char **argv
)
85 vector
<const char*> args
;
86 argv_to_vec(argc
, argv
, args
);
88 cerr
<< argv
[0] << ": -h or --help for usage" << std::endl
;
91 if (ceph_argparse_need_usage(args
)) {
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
);
101 common_init_finish(g_ceph_context
);
103 // Expect exactly one positional argument (inode number)
104 if (args
.size() != 1) {
105 cerr
<< "missing position argument (inode number)" << std::endl
;
108 char const *inode_str
= args
[0];
109 inodeno_t inode
= strtoll(inode_str
, NULL
, 0);
111 derr
<< "Invalid inode: " << inode_str
<< dendl
;
115 // Initialize filesystem client
116 struct ceph_mount_info
*client
;
117 int r
= ceph_create_with_context(&client
, g_ceph_context
);
119 derr
<< "Error initializing libcephfs: " << cpp_strerror(r
) << dendl
;
123 r
= ceph_mount(client
, "/");
125 derr
<< "Error mounting: " << cpp_strerror(r
) << dendl
;
126 ceph_shutdown(client
);
131 // Populate client cache with inode of interest & ancestors
132 r
= lookup_trace(client
, inode
);
134 derr
<< "Error looking up inode " << std::hex
<< inode
<< std::dec
<<
135 ": " << cpp_strerror(r
) << dendl
;
139 // Retrieve inode of interest
140 struct vinodeno_t vinode
;
142 vinode
.snapid
= CEPH_NOSNAP
;
143 Inode
*ino
= ceph_ll_get_inode(client
, vinode
);
145 // Retrieve dentry trace
146 std::vector
<Dentry
*> path
;
147 traverse_dentries(ino
, path
);
149 // Print inode and path as a JSON object
150 JSONFormatter
jf(true);
151 jf
.open_object_section("client_debug");
153 jf
.open_object_section("inode");
157 jf
.close_section(); // inode
158 jf
.open_array_section("path");
160 for (std::vector
<Dentry
*>::reverse_iterator p
= path
.rbegin(); p
!= path
.rend(); ++p
) {
161 jf
.open_object_section("dentry");
165 jf
.close_section(); // dentry
168 jf
.close_section(); // path
170 jf
.close_section(); // client_debug
172 std::cout
<< std::endl
;
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);
183 r
= ceph_unmount(client
);
185 derr
<< "Error mounting: " << cpp_strerror(r
) << dendl
;
187 ceph_shutdown(client
);