summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/action/List.cc
blob: e70835102b6c79da1a929b79ab7137ade2664548 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Shell.h"
#include "tools/rbd/Utils.h"
#include "include/Context.h"
#include "include/stringify.h"
#include "include/types.h"
#include "common/errno.h"
#include "common/Formatter.h"
#include "common/TextTable.h"
#include <iostream>
#include <boost/bind/bind.hpp>
#include <boost/program_options.hpp>
#include "global/global_context.h"

namespace rbd {

namespace action {
namespace list {

namespace at = argument_types;
namespace po = boost::program_options;
using namespace boost::placeholders;

enum WorkerState {
  STATE_IDLE = 0,
  STATE_OPENED,
  STATE_DONE
} ;

struct WorkerEntry {
  librbd::Image img;
  librbd::RBD::AioCompletion* completion;
  WorkerState state;
  string name;
  string id;

  WorkerEntry() {
    state = STATE_IDLE;
    completion = nullptr;
  }
};


int list_process_image(librados::Rados* rados, WorkerEntry* w, bool lflag, Formatter *f, TextTable &tbl)
{
  int r = 0;
  librbd::image_info_t info;
  std::string parent;

  // handle second-nth trips through loop
  librbd::linked_image_spec_t parent_image_spec;
  librbd::snap_spec_t parent_snap_spec;
  r = w->img.get_parent(&parent_image_spec, &parent_snap_spec);
  if (r < 0 && r != -ENOENT) {
    return r;
  }

  bool has_parent = false;
  if (r != -ENOENT) {
    parent = parent_image_spec.pool_name + "/";
    if (!parent_image_spec.pool_namespace.empty()) {
      parent += parent_image_spec.pool_namespace + "/";
    }
    parent +=  parent_image_spec.image_name + "@" + parent_snap_spec.name;
    has_parent = true;
  }

  if (w->img.stat(info, sizeof(info)) < 0) {
    return -EINVAL;
  }

  uint8_t old_format;
  w->img.old_format(&old_format);

  std::list<librbd::locker_t> lockers;
  bool exclusive;
  r = w->img.list_lockers(&lockers, &exclusive, NULL);
  if (r < 0)
    return r;
  std::string lockstr;
  if (!lockers.empty()) {
    lockstr = (exclusive) ? "excl" : "shr";
  }

  if (f) {
    f->open_object_section("image");
    f->dump_string("image", w->name);
    f->dump_string("id", w->id);
    f->dump_unsigned("size", info.size);
    if (has_parent) {
      f->open_object_section("parent");
      f->dump_string("pool", parent_image_spec.pool_name);
      f->dump_string("pool_namespace", parent_image_spec.pool_namespace);
      f->dump_string("image", parent_image_spec.image_name);
      f->dump_string("snapshot", parent_snap_spec.name);
      f->close_section();
    }
    f->dump_int("format", old_format ? 1 : 2);
    if (!lockers.empty())
      f->dump_string("lock_type", exclusive ? "exclusive" : "shared");
    f->close_section();
  } else {
    tbl << w->name
        << stringify(byte_u_t(info.size))
        << parent
        << ((old_format) ? '1' : '2')
        << ""                         // protect doesn't apply to images
        << lockstr
        << TextTable::endrow;
  }

  std::vector<librbd::snap_info_t> snaplist;
  if (w->img.snap_list(snaplist) >= 0 && !snaplist.empty()) {
    snaplist.erase(remove_if(snaplist.begin(),
                             snaplist.end(),
                             boost::bind(utils::is_not_user_snap_namespace, &w->img, _1)),
                   snaplist.end());
    for (std::vector<librbd::snap_info_t>::iterator s = snaplist.begin();
         s != snaplist.end(); ++s) {
      bool is_protected;
      bool has_parent = false;
      parent.clear();
      w->img.snap_set(s->name.c_str());
      r = w->img.snap_is_protected(s->name.c_str(), &is_protected);
      if (r < 0)
        return r;
      if (w->img.get_parent(&parent_image_spec, &parent_snap_spec) >= 0) {
        parent = parent_image_spec.pool_name + "/";
        if (!parent_image_spec.pool_namespace.empty()) {
          parent += parent_image_spec.pool_namespace + "/";
        }
        parent +=  parent_image_spec.image_name + "@" + parent_snap_spec.name;
        has_parent = true;
      }
      if (f) {
        f->open_object_section("snapshot");
        f->dump_string("image", w->name);
        f->dump_string("id", w->id);
        f->dump_string("snapshot", s->name);
        f->dump_unsigned("snapshot_id", s->id);
        f->dump_unsigned("size", s->size);
        if (has_parent) {
          f->open_object_section("parent");
          f->dump_string("pool", parent_image_spec.pool_name);
          f->dump_string("pool_namespace", parent_image_spec.pool_namespace);
          f->dump_string("image", parent_image_spec.image_name);
          f->dump_string("snapshot", parent_snap_spec.name);
          f->close_section();
        }
        f->dump_int("format", old_format ? 1 : 2);
        f->dump_string("protected", is_protected ? "true" : "false");
        f->close_section();
      } else {
        tbl << w->name + "@" + s->name
            << stringify(byte_u_t(s->size))
            << parent
            << ((old_format) ? '1' : '2')
            << (is_protected ? "yes" : "")
            << ""                     // locks don't apply to snaps
            << TextTable::endrow;
      }
    }
  }

  return 0;
}

int do_list(const std::string &pool_name, const std::string& namespace_name,
            bool lflag, Formatter *f) {
  std::vector<WorkerEntry*> workers;
  std::vector<librbd::image_spec_t> images;
  librados::Rados rados;
  librbd::RBD rbd;
  librados::IoCtx ioctx;

  int r = utils::init(pool_name, namespace_name, &rados, &ioctx);
  if (r < 0) {
    return r;
  }

  int threads = g_conf().get_val<uint64_t>("rbd_concurrent_management_ops");
  if (threads < 1) {
    threads = 1;
  }
  if (threads > 32) {
    threads = 32;
  }

  utils::disable_cache();

  r = rbd.list2(ioctx, &images);
  if (r < 0)
    return r;

  if (!lflag) {
    if (f)
      f->open_array_section("images");
    for (auto& image : images) {
       if (f)
	 f->dump_string("name", image.name);
       else
	 std::cout << image.name << std::endl;
    }
    if (f) {
      f->close_section();
      f->flush(std::cout);
    }
    return 0;
  }

  TextTable tbl;

  if (f) {
    f->open_array_section("images");
  } else {
    tbl.define_column("NAME", TextTable::LEFT, TextTable::LEFT);
    tbl.define_column("SIZE", TextTable::LEFT, TextTable::RIGHT);
    tbl.define_column("PARENT", TextTable::LEFT, TextTable::LEFT);
    tbl.define_column("FMT", TextTable::LEFT, TextTable::RIGHT);
    tbl.define_column("PROT", TextTable::LEFT, TextTable::LEFT);
    tbl.define_column("LOCK", TextTable::LEFT, TextTable::LEFT);
  }

  for (size_t left = 0; left < std::min<size_t>(threads, images.size());
       left++) {
    workers.push_back(new WorkerEntry());
  }

  auto i = images.begin();
  while (true) {
    size_t workers_idle = 0;
    for (auto comp : workers) {
      switch (comp->state) {
	case STATE_DONE:
	  comp->completion->wait_for_complete();
	  comp->state = STATE_IDLE;
	  comp->completion->release();
	  comp->completion = nullptr;
	  // we want it to fall through in this case
	case STATE_IDLE:
	  if (i == images.end()) {
	    workers_idle++;
	    continue;
	  }
	  comp->name = i->name;
          comp->id = i->id;
	  comp->completion = new librbd::RBD::AioCompletion(nullptr, nullptr);
	  r = rbd.aio_open_read_only(ioctx, comp->img, i->name.c_str(), nullptr,
                                     comp->completion);
	  i++;
	  comp->state = STATE_OPENED;
	  break;
	case STATE_OPENED:
	  comp->completion->wait_for_complete();
	  // image might disappear between rbd.list() and rbd.open(); ignore
	  // that, warn about other possible errors (EPERM, say, for opening
	  // an old-format image, because you need execute permission for the
	  // class method)
	  r = comp->completion->get_return_value();
	  comp->completion->release();
	  if (r < 0) {
	    std::cerr << "rbd: error opening " << comp->name << ": "
                      << cpp_strerror(r) << std::endl;

	    // in any event, continue to next image
	    comp->state = STATE_IDLE;
	    continue;
	  }
	  r = list_process_image(&rados, comp, lflag, f, tbl);
	  if (r < 0) {
	      std::cerr << "rbd: error processing image " << comp->name << ": "
                        << cpp_strerror(r) << std::endl;
	  }
	  comp->completion = new librbd::RBD::AioCompletion(nullptr, nullptr);
	  r = comp->img.aio_close(comp->completion);
	  comp->state = STATE_DONE;
	  break;
      }
    }
    if (workers_idle == workers.size()) {
	break;
    }
  }

  if (f) {
    f->close_section();
    f->flush(std::cout);
  } else if (!images.empty()) {
    std::cout << tbl;
  }

  rados.shutdown();

  for (auto comp : workers) {
    delete comp;
  }

  return r < 0 ? r : 0;
}

void get_arguments(po::options_description *positional,
                   po::options_description *options) {
  options->add_options()
    ("long,l", po::bool_switch(), "long listing format");
  at::add_pool_options(positional, options, true);
  at::add_format_options(options);
}

int execute(const po::variables_map &vm,
            const std::vector<std::string> &ceph_global_init_args) {
  std::string pool_name;
  std::string namespace_name;
  size_t arg_index = 0;
  int r = utils::get_pool_and_namespace_names(vm, false, &pool_name,
                                              &namespace_name, &arg_index);
  if (r < 0) {
    return r;
  }

  at::Format::Formatter formatter;
  r = utils::get_formatter(vm, &formatter);
  if (r < 0) {
    return r;
  }

  r = do_list(pool_name, namespace_name, vm["long"].as<bool>(),
              formatter.get());
  if (r < 0) {
    std::cerr << "rbd: listing images failed: " << cpp_strerror(r)
              << std::endl;
    return r;
  }

  return 0;
}

Shell::SwitchArguments switched_arguments({"long", "l"});
Shell::Action action(
  {"list"}, {"ls"}, "List rbd images.", "", &get_arguments, &execute);

} // namespace list
} // namespace action
} // namespace rbd