summaryrefslogtreecommitdiffstats
path: root/src/tools/ceph-client-debug.cc
blob: 7a43c9c2a6d26f91aeea3605170a135b1e7d5723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */


#include "common/ceph_argparse.h"
#include "global/global_init.h"
#include "common/Formatter.h"
#include "common/debug.h"
#include "common/errno.h"
#include "client/Inode.h"
#include "client/Dentry.h"
#include "client/Dir.h"
#include "include/cephfs/libcephfs.h"

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_client

void usage()
{
  std::cout << "Usage: ceph-client-debug [options] <inode number>" << std::endl;
  generic_client_usage();
}


/**
 * Given an inode, look up the path from the Client cache: assumes
 * client cache is fully populated.
 */
void traverse_dentries(Inode *ino, std::vector<Dentry*> &parts)
{
  if (ino->dentries.empty()) {
    return;
  }
  
  Dentry* dn = *(ino->dentries.begin());
  parts.push_back(dn);
  traverse_dentries(dn->dir->parent_inode, parts);
}


/**
 * Given an inode, send lookup requests to the MDS for
 * all its ancestors, such that the full trace will be
 * populated in client cache.
 */
int lookup_trace(ceph_mount_info *client, inodeno_t const ino)
{
  Inode *inode;
  int r = ceph_ll_lookup_inode(client, ino, &inode);
  if (r != 0) {
    return r;
  } else {
    if (!inode->dentries.empty()) {
      Dentry *dn = *(inode->dentries.begin());
      ceph_assert(dn->dir);
      ceph_assert(dn->dir->parent_inode);
      r = lookup_trace(client, dn->dir->parent_inode->ino);
      if (r) {
        return r;
      }
    } else {
      // We reached the root of the tree
      ceph_assert(inode->ino == CEPH_INO_ROOT);
    }
  }

  return r;
}


int main(int argc, const char **argv)
{
  // Argument handling
  vector<const char*> args;
  argv_to_vec(argc, argv, args);
  if (args.empty()) {
    cerr << argv[0] << ": -h or --help for usage" << std::endl;
    exit(1);
  }
  if (ceph_argparse_need_usage(args)) {
    usage();
    exit(0);
  }

  auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
			 CODE_ENVIRONMENT_UTILITY,
			 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS|
			 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
  
  common_init_finish(g_ceph_context);

  // Expect exactly one positional argument (inode number)
  if (args.size() != 1) {
    cerr << "missing position argument (inode number)" << std::endl;
    exit(1);
  }
  char const *inode_str = args[0];
  inodeno_t inode = strtoll(inode_str, NULL, 0);
  if (inode <= 0) {
    derr << "Invalid inode: " << inode_str << dendl;
    return -1;
  }

  // Initialize filesystem client
  struct ceph_mount_info *client;
  int r = ceph_create_with_context(&client, g_ceph_context);
  if (r) {
    derr << "Error initializing libcephfs: " << cpp_strerror(r) << dendl;
    return r;
  }

  r = ceph_mount(client, "/");
  if (r) {
    derr << "Error mounting: " << cpp_strerror(r) << dendl;
    ceph_shutdown(client);
    return r;
  }


  // Populate client cache with inode of interest & ancestors
  r = lookup_trace(client, inode);
  if (r) {
    derr << "Error looking up inode " << std::hex << inode << std::dec <<
      ": " << cpp_strerror(r) << dendl;
    return -1;
  }

  // Retrieve inode of interest
  struct vinodeno_t vinode;
  vinode.ino = inode;
  vinode.snapid = CEPH_NOSNAP;
  Inode *ino = ceph_ll_get_inode(client, vinode);

  // Retrieve dentry trace
  std::vector<Dentry*> path;
  traverse_dentries(ino, path);
  
  // Print inode and path as a JSON object
  JSONFormatter jf(true);
  jf.open_object_section("client_debug");
  {
    jf.open_object_section("inode");
    {
      ino->dump(&jf);
    }
    jf.close_section(); // inode
    jf.open_array_section("path");
    {
      for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
        jf.open_object_section("dentry");
        {
          (*p)->dump(&jf);
        }
        jf.close_section(); // dentry
      }
    }
    jf.close_section(); // path
  }
  jf.close_section(); // client_debug
  jf.flush(std::cout);
  std::cout << std::endl;

  // Release Inode references
  ceph_ll_forget(client, ino, 1);
  for (std::vector<Dentry*>::reverse_iterator p = path.rbegin(); p != path.rend(); ++p) {
    ceph_ll_forget(client, (*p)->inode.get(), 1);
  }
  ino = NULL;
  path.clear();  

  // Shut down
  r = ceph_unmount(client);
  if (r) {
    derr << "Error mounting: " << cpp_strerror(r) << dendl;
  }
  ceph_shutdown(client);
  
  return r;
}