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->dn_set.empty()) {
46 Dentry* dn = *(ino->dn_set.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->dn_set.empty()) {
65 Dentry *dn = *(inode->dn_set.begin());
67 assert(dn->dir->parent_inode);
68 r = lookup_trace(client, dn->dir->parent_inode->ino);
73 // We reached the root of the tree
74 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);
89 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
90 CODE_ENVIRONMENT_UTILITY,
91 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS);
93 common_init_finish(g_ceph_context);
95 // Expect exactly one positional argument (inode number)
96 if (args.size() != 1) {
99 char const *inode_str = args[0];
100 inodeno_t inode = strtoll(inode_str, NULL, 0);
102 derr << "Invalid inode: " << inode_str << dendl;
106 // Initialize filesystem client
107 struct ceph_mount_info *client;
108 int r = ceph_create_with_context(&client, g_ceph_context);
110 derr << "Error initializing libcephfs: " << cpp_strerror(r) << dendl;
114 r = ceph_mount(client, "/");
116 derr << "Error mounting: " << cpp_strerror(r) << dendl;
117 ceph_shutdown(client);
122 // Populate client cache with inode of interest & ancestors
123 r = lookup_trace(client, inode);
125 derr << "Error looking up inode " << std::hex << inode << std::dec <<
126 ": " << cpp_strerror(r) << dendl;
130 // Retrieve inode of interest
131 struct vinodeno_t vinode;
133 vinode.snapid = CEPH_NOSNAP;
134 Inode *ino = ceph_ll_get_inode(client, vinode);
136 // Retrieve dentry trace
137 std::vector<Dentry*> path;
138 traverse_dentries(ino, path);
140 // Print inode and path as a JSON object
141 JSONFormatter jf(true);
142 jf.open_object_section("client_debug");
144 jf.open_object_section("inode");
148 jf.close_section(); // inode
149 jf.open_array_section("path");
151 for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
152 jf.open_object_section("dentry");
156 jf.close_section(); // dentry
159 jf.close_section(); // path
161 jf.close_section(); // client_debug
163 std::cout << std::endl;
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);
174 r = ceph_unmount(client);
176 derr << "Error mounting: " << cpp_strerror(r) << dendl;
178 ceph_shutdown(client);