summaryrefslogtreecommitdiffstats
path: root/src/librbd/api/Namespace.cc
blob: 86ed70c06acec84caa80b9330a118897dd500163 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "common/errno.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/api/Mirror.h"
#include "librbd/api/Namespace.h"
#include "librbd/ImageCtx.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::api::Namespace: " << __func__ << ": "

namespace librbd {
namespace api {

namespace {

const std::list<std::string> POOL_OBJECTS {
  RBD_CHILDREN,
  RBD_GROUP_DIRECTORY,
  RBD_INFO,
  RBD_MIRRORING,
  RBD_TASK,
  RBD_TRASH,
  RBD_DIRECTORY
};

} // anonymous namespace

template <typename I>
int Namespace<I>::create(librados::IoCtx& io_ctx, const std::string& name)
{
  CephContext *cct = (CephContext *)io_ctx.cct();
  ldout(cct, 5) << "name=" << name << dendl;

  if (name.empty()) {
    return -EINVAL;
  }

  librados::Rados rados(io_ctx);
  int8_t require_osd_release;
  int r = rados.get_min_compatible_osd(&require_osd_release);
  if (r < 0) {
    lderr(cct) << "failed to retrieve min OSD release: " << cpp_strerror(r)
               << dendl;
    return r;
  }

  if (require_osd_release < CEPH_RELEASE_NAUTILUS) {
    ldout(cct, 1) << "namespace support requires nautilus or later OSD"
                  << dendl;
    return -ENOSYS;
  }


  librados::IoCtx default_ns_ctx;
  default_ns_ctx.dup(io_ctx);
  default_ns_ctx.set_namespace("");

  r = cls_client::namespace_add(&default_ns_ctx, name);
  if (r < 0) {
    lderr(cct) << "failed to add namespace: " << cpp_strerror(r) << dendl;
    return r;
  }

  librados::IoCtx ns_ctx;
  ns_ctx.dup(io_ctx);
  ns_ctx.set_namespace(name);

  r = cls_client::dir_state_set(&ns_ctx, RBD_DIRECTORY,
                                cls::rbd::DIRECTORY_STATE_READY);
  if (r < 0) {
    lderr(cct) << "failed to initialize image directory: " << cpp_strerror(r)
               << dendl;
    goto rollback;
  }

  return 0;

rollback:
  int ret_val = cls_client::namespace_remove(&default_ns_ctx, name);
  if (ret_val < 0) {
    lderr(cct) << "failed to remove namespace: " << cpp_strerror(ret_val) << dendl;
  }

  return r;
}

template <typename I>
int Namespace<I>::remove(librados::IoCtx& io_ctx, const std::string& name)
{
  CephContext *cct = (CephContext *)io_ctx.cct();
  ldout(cct, 5) << "name=" << name << dendl;

  if (name.empty()) {
    return -EINVAL;
  }

  librados::IoCtx default_ns_ctx;
  default_ns_ctx.dup(io_ctx);
  default_ns_ctx.set_namespace("");

  librados::IoCtx ns_ctx;
  ns_ctx.dup(io_ctx);
  ns_ctx.set_namespace(name);

  std::map<std::string, cls::rbd::TrashImageSpec> trash_entries;

  librados::ObjectWriteOperation dir_op;
  librbd::cls_client::dir_state_set(
    &dir_op, cls::rbd::DIRECTORY_STATE_ADD_DISABLED);
  dir_op.remove();

  int r = ns_ctx.operate(RBD_DIRECTORY, &dir_op);
  if (r == -EBUSY) {
    ldout(cct, 5) << "image directory not empty" << dendl;
    goto rollback;
  } else if (r < 0 && r != -ENOENT) {
    lderr(cct) << "failed to disable the namespace: " << cpp_strerror(r)
               << dendl;
    return r;
  }

  r = cls_client::trash_list(&ns_ctx, "", 1, &trash_entries);
  if (r < 0 && r != -ENOENT) {
    lderr(cct) << "failed to list trash directory: " << cpp_strerror(r)
               << dendl;
    return r;
  } else if (!trash_entries.empty()) {
    ldout(cct, 5) << "image trash not empty" << dendl;
    goto rollback;
  }

  r = Mirror<I>::mode_set(ns_ctx, RBD_MIRROR_MODE_DISABLED);
  if (r < 0) {
    lderr(cct) << "failed to disable mirroring: " << cpp_strerror(r)
               << dendl;
    return r;
  }

  for (auto& oid : POOL_OBJECTS) {
    r = ns_ctx.remove(oid);
    if (r < 0 && r != -ENOENT) {
      lderr(cct) << "failed to remove object '" << oid << "': "
                 << cpp_strerror(r) << dendl;
      return r;
    }
  }

  r = cls_client::namespace_remove(&default_ns_ctx, name);
  if (r < 0) {
    lderr(cct) << "failed to remove namespace: " << cpp_strerror(r) << dendl;
    return r;
  }

  return 0;

rollback:

  r = librbd::cls_client::dir_state_set(
    &ns_ctx, RBD_DIRECTORY, cls::rbd::DIRECTORY_STATE_READY);
  if (r < 0) {
    lderr(cct) << "failed to restore directory state: " << cpp_strerror(r)
               << dendl;
  }

  return -EBUSY;
}

template <typename I>
int Namespace<I>::list(IoCtx& io_ctx, std::vector<std::string> *names)
{
  CephContext *cct = (CephContext *)io_ctx.cct();
  ldout(cct, 5) << dendl;

  librados::IoCtx default_ns_ctx;
  default_ns_ctx.dup(io_ctx);
  default_ns_ctx.set_namespace("");

  int r;
  int max_read = 1024;
  std::string last_read = "";
  do {
    std::list<std::string> name_list;
    r = cls_client::namespace_list(&default_ns_ctx, last_read, max_read,
                                   &name_list);
    if (r == -ENOENT) {
      return 0;
    } else if (r < 0) {
      lderr(cct) << "error listing namespaces: " << cpp_strerror(r) << dendl;
      return r;
    }

    names->insert(names->end(), name_list.begin(), name_list.end());
    if (!name_list.empty()) {
      last_read = name_list.back();
    }
    r = name_list.size();
  } while (r == max_read);

  return 0;
}

template <typename I>
int Namespace<I>::exists(librados::IoCtx& io_ctx, const std::string& name, bool *exists)
{
  CephContext *cct = (CephContext *)io_ctx.cct();
  ldout(cct, 5) << "name=" << name << dendl;

  *exists = false;
  if (name.empty()) {
    return -EINVAL;
  }

  librados::IoCtx ns_ctx;
  ns_ctx.dup(io_ctx);
  ns_ctx.set_namespace(name);

  int r = librbd::cls_client::dir_state_assert(&ns_ctx, RBD_DIRECTORY,
                                               cls::rbd::DIRECTORY_STATE_READY);
  if (r == 0) {
    *exists = true;
  } else if (r != -ENOENT) {
    lderr(cct) << "error asserting namespace: " << cpp_strerror(r) << dendl;
    return r;
  }

  return 0;
}

} // namespace api
} // namespace librbd

template class librbd::api::Namespace<librbd::ImageCtx>;