summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/action/Kernel.cc
blob: dc0938eb0bab74f71f0472521bf9af3a2a24cf78 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "acconfig.h"
#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Shell.h"
#include "tools/rbd/Utils.h"
#include "include/krbd.h"
#include "include/stringify.h"
#include "include/uuid.h"
#include "common/config_proxy.h"
#include "common/errno.h"
#include "common/safe_io.h"
#include "common/strtol.h"
#include "common/Formatter.h"
#include "msg/msg_types.h"
#include "global/global_context.h"
#include <iostream>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/scope_exit.hpp>
#include <boost/program_options.hpp>

namespace rbd {
namespace action {
namespace kernel {

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

namespace {

std::map<std::string, std::string> map_options; // used for both map and unmap

} // anonymous namespace

static std::string map_option_uuid_cb(const char *value_char)
{
  uuid_d u;
  if (!u.parse(value_char))
    return "";

  return stringify(u);
}

static std::string map_option_ip_cb(const char *value_char)
{
  entity_addr_t a;
  const char *endptr;
  if (!a.parse(value_char, &endptr) ||
      endptr != value_char + strlen(value_char)) {
    return "";
  }

  return stringify(a.get_sockaddr());
}

static std::string map_option_int_cb(const char *value_char)
{
  std::string err;
  int d = strict_strtol(value_char, 10, &err);
  if (!err.empty() || d < 0)
    return "";

  return stringify(d);
}

static std::string map_option_ms_mode_cb(const char *value_char)
{
  if (!strcmp(value_char, "legacy") || !strcmp(value_char, "crc") ||
      !strcmp(value_char, "secure") || !strcmp(value_char, "prefer-crc") ||
      !strcmp(value_char, "prefer-secure")) {
    return value_char;
  }
  return "";
}

static void put_map_option(const std::string &key, const std::string &val)
{
  map_options[key] = val;
}

static int put_map_option_value(const std::string &opt, const char *value_char,
                                std::string (*parse_cb)(const char *))
{
  if (!value_char || *value_char == '\0') {
    std::cerr << "rbd: " << opt << " option requires a value" << std::endl;
    return -EINVAL;
  }

  std::string value = parse_cb(value_char);
  if (value.empty()) {
    std::cerr << "rbd: invalid " << opt << " value '" << value_char << "'"
              << std::endl;
    return -EINVAL;
  }

  put_map_option(opt, opt + "=" + value);
  return 0;
}

static int parse_map_options(const std::string &options_string)
{
  char *options = strdup(options_string.c_str());
  BOOST_SCOPE_EXIT(options) {
    free(options);
  } BOOST_SCOPE_EXIT_END;

  for (char *this_char = strtok(options, ", ");
       this_char != NULL;
       this_char = strtok(NULL, ",")) {
    char *value_char;

    if ((value_char = strchr(this_char, '=')) != NULL)
      *value_char++ = '\0';

    if (!strcmp(this_char, "fsid")) {
      if (put_map_option_value("fsid", value_char, map_option_uuid_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "ip")) {
      if (put_map_option_value("ip", value_char, map_option_ip_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "share") || !strcmp(this_char, "noshare")) {
      put_map_option("share", this_char);
    } else if (!strcmp(this_char, "crc") || !strcmp(this_char, "nocrc")) {
      put_map_option("crc", this_char);
    } else if (!strcmp(this_char, "cephx_require_signatures") ||
               !strcmp(this_char, "nocephx_require_signatures")) {
      put_map_option("cephx_require_signatures", this_char);
    } else if (!strcmp(this_char, "tcp_nodelay") ||
               !strcmp(this_char, "notcp_nodelay")) {
      put_map_option("tcp_nodelay", this_char);
    } else if (!strcmp(this_char, "cephx_sign_messages") ||
               !strcmp(this_char, "nocephx_sign_messages")) {
      put_map_option("cephx_sign_messages", this_char);
    } else if (!strcmp(this_char, "mount_timeout")) {
      if (put_map_option_value("mount_timeout", value_char, map_option_int_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "osd_request_timeout")) {
      if (put_map_option_value("osd_request_timeout", value_char, map_option_int_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "lock_timeout")) {
      if (put_map_option_value("lock_timeout", value_char, map_option_int_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "osdkeepalive")) {
      if (put_map_option_value("osdkeepalive", value_char, map_option_int_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "osd_idle_ttl")) {
      if (put_map_option_value("osd_idle_ttl", value_char, map_option_int_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "rw") || !strcmp(this_char, "ro")) {
      put_map_option("rw", this_char);
    } else if (!strcmp(this_char, "queue_depth")) {
      if (put_map_option_value("queue_depth", value_char, map_option_int_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "lock_on_read")) {
      put_map_option("lock_on_read", this_char);
    } else if (!strcmp(this_char, "exclusive")) {
      put_map_option("exclusive", this_char);
    } else if (!strcmp(this_char, "notrim")) {
      put_map_option("notrim", this_char);
    } else if (!strcmp(this_char, "abort_on_full")) {
      put_map_option("abort_on_full", this_char);
    } else if (!strcmp(this_char, "alloc_size")) {
      if (put_map_option_value("alloc_size", value_char, map_option_int_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "ms_mode")) {
      if (put_map_option_value("ms_mode", value_char, map_option_ms_mode_cb))
        return -EINVAL;
    } else if (!strcmp(this_char, "udev") || !strcmp(this_char, "noudev")) {
      put_map_option("udev", this_char);
    } else {
      std::cerr << "rbd: unknown map option '" << this_char << "'" << std::endl;
      return -EINVAL;
    }
  }

  return 0;
}

static int parse_unmap_options(const std::string &options_string)
{
  char *options = strdup(options_string.c_str());
  BOOST_SCOPE_EXIT(options) {
    free(options);
  } BOOST_SCOPE_EXIT_END;

  for (char *this_char = strtok(options, ", ");
       this_char != NULL;
       this_char = strtok(NULL, ",")) {
    char *value_char;

    if ((value_char = strchr(this_char, '=')) != NULL)
      *value_char++ = '\0';

    if (!strcmp(this_char, "force")) {
      put_map_option("force", this_char);
    } else if (!strcmp(this_char, "udev") || !strcmp(this_char, "noudev")) {
      put_map_option("udev", this_char);
    } else {
      std::cerr << "rbd: unknown unmap option '" << this_char << "'" << std::endl;
      return -EINVAL;
    }
  }

  return 0;
}

static int do_kernel_list(Formatter *f) {
#if defined(WITH_KRBD)
  struct krbd_ctx *krbd;
  int r;

  r = krbd_create_from_context(g_ceph_context, 0, &krbd);
  if (r < 0)
    return r;

  r = krbd_showmapped(krbd, f);

  krbd_destroy(krbd);
  return r;
#else
  std::cerr << "rbd: kernel device is not supported" << std::endl;
  return -EOPNOTSUPP;
#endif
}

static int get_unsupported_features(librbd::Image &image,
                                    uint64_t *unsupported_features)
{
  char buf[20];
  uint64_t features, supported_features;
  int r;

  r = safe_read_file("/sys/bus/rbd/", "supported_features", buf,
                     sizeof(buf) - 1);
  if (r < 0)
    return r;

  buf[r] = '\0';
  try {
    supported_features = std::stoull(buf, nullptr, 16);
  } catch (...) {
    return -EINVAL;
  }

  r = image.features(&features);
  if (r < 0)
    return r;

  *unsupported_features = features & ~supported_features;
  return 0;
}

/*
 * hint user to check syslog for krbd related messages and provide suggestions
 * based on errno return by krbd_map(). also note that even if some librbd calls
 * fail, we at least dump the "try dmesg..." message to aid debugging.
 */
static void print_error_description(const char *poolname,
                                    const char *nspace_name,
                                    const char *imgname,
                                    const char *snapname,
                                    int maperrno)
{
  int r;
  uint8_t oldformat;
  librados::Rados rados;
  librados::IoCtx ioctx;
  librbd::Image image;

  if (maperrno == -ENOENT)
    goto done;

  r = utils::init_and_open_image(poolname, nspace_name, imgname, "", snapname,
				 true, &rados, &ioctx, &image);
  if (r < 0)
    goto done;

  r = image.old_format(&oldformat);
  if (r < 0)
    goto done;

  /*
   * kernel returns -ENXIO when mapping a V2 image due to unsupported feature
   * set - so, hint about that too...
   */
  if (!oldformat && (maperrno == -ENXIO)) {
    uint64_t unsupported_features;
    bool need_terminate = true;

    std::cout << "RBD image feature set mismatch. ";
    r = get_unsupported_features(image, &unsupported_features);
    if (r == 0 && (unsupported_features & ~RBD_FEATURES_ALL) == 0) {
      uint64_t immutable = RBD_FEATURES_ALL & ~(RBD_FEATURES_MUTABLE |
                                                RBD_FEATURES_DISABLE_ONLY);
      if (unsupported_features & immutable) {
        std::cout << "This image cannot be mapped because the following "
                  << "immutable features are unsupported by the kernel:";
        unsupported_features &= immutable;
        need_terminate = false;
      } else {
        std::cout << "You can disable features unsupported by the kernel "
                  << "with \"rbd feature disable ";
        if (poolname != utils::get_default_pool_name() || *nspace_name) {
          std::cout << poolname << "/";
        }
        if (*nspace_name) {
          std::cout << nspace_name << "/";
        }
        std::cout << imgname;
      }
    } else {
      std::cout << "Try disabling features unsupported by the kernel "
                << "with \"rbd feature disable";
      unsupported_features = 0;
    }
    for (auto it : at::ImageFeatures::FEATURE_MAPPING) {
      if (it.first & unsupported_features) {
        std::cout << " " << it.second;
      }
    }
    if (need_terminate)
      std::cout << "\"";
    std::cout << "." << std::endl;
  }

 done:
  std::cout << "In some cases useful info is found in syslog - try \"dmesg | tail\"." << std::endl;
}

static int do_kernel_map(const char *poolname, const char *nspace_name,
                         const char *imgname, const char *snapname)
{
#if defined(WITH_KRBD)
  struct krbd_ctx *krbd;
  std::ostringstream oss;
  uint32_t flags = 0;
  char *devnode;
  int r;

  for (auto it = map_options.begin(); it != map_options.end(); ) {
    // for compatibility with < 3.7 kernels, assume that rw is on by
    // default and omit it even if it was specified by the user
    // (see ceph.git commit fb0f1986449b)
    if (it->first == "rw" && it->second == "rw") {
      it = map_options.erase(it);
    } else if (it->first == "udev") {
      if (it->second == "noudev") {
        flags |= KRBD_CTX_F_NOUDEV;
      }
      it = map_options.erase(it);
    } else {
      if (it != map_options.begin())
        oss << ",";
      oss << it->second;
      ++it;
    }
  }

  r = krbd_create_from_context(g_ceph_context, flags, &krbd);
  if (r < 0)
    return r;

  r = krbd_is_mapped(krbd, poolname, nspace_name, imgname, snapname, &devnode);
  if (r < 0) {
    std::cerr << "rbd: warning: can't get image map information: "
	      << cpp_strerror(r) << std::endl;
  } else if (r > 0) {
    std::cerr << "rbd: warning: image already mapped as " << devnode
              << std::endl;
    free(devnode);
  }

  r = krbd_map(krbd, poolname, nspace_name, imgname, snapname,
               oss.str().c_str(), &devnode);
  if (r < 0) {
    print_error_description(poolname, nspace_name, imgname, snapname, r);
    goto out;
  }

  std::cout << devnode << std::endl;

  free(devnode);
out:
  krbd_destroy(krbd);
  return r;
#else
  std::cerr << "rbd: kernel device is not supported" << std::endl;
  return -EOPNOTSUPP;
#endif
}

static int do_kernel_unmap(const char *dev, const char *poolname,
                           const char *nspace_name, const char *imgname,
                           const char *snapname)
{
#if defined(WITH_KRBD)
  struct krbd_ctx *krbd;
  std::ostringstream oss;
  uint32_t flags = 0;
  int r;

  for (auto it = map_options.begin(); it != map_options.end(); ) {
    if (it->first == "udev") {
      if (it->second == "noudev") {
        flags |= KRBD_CTX_F_NOUDEV;
      }
      it = map_options.erase(it);
    } else {
      if (it != map_options.begin())
        oss << ",";
      oss << it->second;
      ++it;
    }
  }

  r = krbd_create_from_context(g_ceph_context, flags, &krbd);
  if (r < 0)
    return r;

  if (dev)
    r = krbd_unmap(krbd, dev, oss.str().c_str());
  else
    r = krbd_unmap_by_spec(krbd, poolname, nspace_name, imgname, snapname,
                           oss.str().c_str());

  krbd_destroy(krbd);
  return r;
#else
  std::cerr << "rbd: kernel device is not supported" << std::endl;
  return -EOPNOTSUPP;
#endif
}

int execute_list(const po::variables_map &vm,
                 const std::vector<std::string> &ceph_global_init_args) {
  at::Format::Formatter formatter;
  int r = utils::get_formatter(vm, &formatter);
  if (r < 0) {
    return r;
  }

  utils::init_context();

  r = do_kernel_list(formatter.get());
  if (r < 0) {
    std::cerr << "rbd: device list failed: " << cpp_strerror(r) << std::endl;
    return r;
  }
  return 0;
}

int execute_map(const po::variables_map &vm,
                const std::vector<std::string> &ceph_global_init_args) {
  size_t arg_index = 0;
  std::string pool_name;
  std::string nspace_name;
  std::string image_name;
  std::string snap_name;
  int r = utils::get_pool_image_snapshot_names(
    vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &nspace_name,
    &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_PERMITTED,
    utils::SPEC_VALIDATION_NONE);
  if (r < 0) {
    return r;
  }

  // parse default options first so they can be overwritten by cli options
  r = parse_map_options(
      g_conf().get_val<std::string>("rbd_default_map_options"));
  if (r < 0) {
    std::cerr << "rbd: couldn't parse default map options" << std::endl;
    return r;
  }

  if (vm.count("options")) {
    for (auto &options : vm["options"].as<std::vector<std::string>>()) {
      r = parse_map_options(options);
      if (r < 0) {
        std::cerr << "rbd: couldn't parse map options" << std::endl;
        return r;
      }
    }
  }

  // parse options common to all device types after parsing krbd-specific
  // options so that common options win (in particular "-o rw --read-only"
  // should result in read-only mapping)
  if (vm["read-only"].as<bool>()) {
    put_map_option("rw", "ro");
  }
  if (vm["exclusive"].as<bool>()) {
    put_map_option("exclusive", "exclusive");
  }

  utils::init_context();

  r = do_kernel_map(pool_name.c_str(), nspace_name.c_str(), image_name.c_str(),
                    snap_name.c_str());
  if (r < 0) {
    std::cerr << "rbd: map failed: " << cpp_strerror(r) << std::endl;
    return r;
  }

  return 0;
}

int execute_unmap(const po::variables_map &vm,
                  const std::vector<std::string> &ceph_global_init_args) {
  std::string device_name = utils::get_positional_argument(vm, 0);
  if (!boost::starts_with(device_name, "/dev/")) {
    device_name.clear();
  }

  size_t arg_index = 0;
  std::string pool_name;
  std::string nspace_name;
  std::string image_name;
  std::string snap_name;
  int r;
  if (device_name.empty()) {
    r = utils::get_pool_image_snapshot_names(
      vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &nspace_name,
      &image_name, &snap_name, false, utils::SNAPSHOT_PRESENCE_PERMITTED,
      utils::SPEC_VALIDATION_NONE);
    if (r < 0) {
      return r;
    }
  }

  if (device_name.empty() && image_name.empty()) {
    std::cerr << "rbd: unmap requires either image name or device path"
              << std::endl;
    return -EINVAL;
  }

  if (vm.count("options")) {
    for (auto &options : vm["options"].as<std::vector<std::string>>()) {
      r = parse_unmap_options(options);
      if (r < 0) {
        std::cerr << "rbd: couldn't parse unmap options" << std::endl;
        return r;
      }
    }
  }

  utils::init_context();

  r = do_kernel_unmap(device_name.empty() ? nullptr : device_name.c_str(),
                      pool_name.c_str(), nspace_name.c_str(),
                      image_name.c_str(), snap_name.c_str());
  if (r < 0) {
    std::cerr << "rbd: unmap failed: " << cpp_strerror(r) << std::endl;
    return r;
  }
  return 0;
}

} // namespace kernel
} // namespace action
} // namespace rbd