]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include <errno.h> | |
5 | ||
6 | #include <string> | |
7 | #include <map> | |
8 | #include <sstream> | |
9 | ||
10 | #include <boost/utility/string_ref.hpp> | |
11 | #include <boost/format.hpp> | |
12 | ||
13 | #include "common/errno.h" | |
14 | #include "common/ceph_json.h" | |
f64942e4 | 15 | #include "include/scope_guard.h" |
7c673cae | 16 | #include "rgw_rados.h" |
11fdf7f2 | 17 | #include "rgw_zone.h" |
7c673cae FG |
18 | #include "rgw_acl.h" |
19 | #include "rgw_acl_s3.h" | |
20 | ||
21 | #include "include/types.h" | |
22 | #include "rgw_bucket.h" | |
23 | #include "rgw_user.h" | |
24 | #include "rgw_string.h" | |
224ce89b | 25 | #include "rgw_multi.h" |
7c673cae | 26 | |
11fdf7f2 TL |
27 | #include "services/svc_zone.h" |
28 | #include "services/svc_sys_obj.h" | |
29 | ||
7c673cae FG |
30 | #include "include/rados/librados.hpp" |
31 | // until everything is moved from rgw_common | |
32 | #include "rgw_common.h" | |
f64942e4 | 33 | #include "rgw_reshard.h" |
11fdf7f2 | 34 | #include "rgw_lc.h" |
7c673cae FG |
35 | #include "cls/user/cls_user_types.h" |
36 | ||
37 | #define dout_context g_ceph_context | |
38 | #define dout_subsys ceph_subsys_rgw | |
39 | ||
40 | #define BUCKET_TAG_TIMEOUT 30 | |
41 | ||
7c673cae FG |
42 | |
43 | static RGWMetadataHandler *bucket_meta_handler = NULL; | |
44 | static RGWMetadataHandler *bucket_instance_meta_handler = NULL; | |
45 | ||
11fdf7f2 | 46 | // define as static when RGWBucket implementation completes |
7c673cae FG |
47 | void rgw_get_buckets_obj(const rgw_user& user_id, string& buckets_obj_id) |
48 | { | |
49 | buckets_obj_id = user_id.to_str(); | |
50 | buckets_obj_id += RGW_BUCKETS_OBJ_SUFFIX; | |
51 | } | |
52 | ||
53 | /* | |
54 | * Note that this is not a reversal of parse_bucket(). That one deals | |
55 | * with the syntax we need in metadata and such. This one deals with | |
56 | * the representation in RADOS pools. We chose '/' because it's not | |
57 | * acceptable in bucket names and thus qualified buckets cannot conflict | |
58 | * with the legacy or S3 buckets. | |
59 | */ | |
60 | std::string rgw_make_bucket_entry_name(const std::string& tenant_name, | |
61 | const std::string& bucket_name) { | |
62 | std::string bucket_entry; | |
63 | ||
64 | if (bucket_name.empty()) { | |
65 | bucket_entry.clear(); | |
66 | } else if (tenant_name.empty()) { | |
67 | bucket_entry = bucket_name; | |
68 | } else { | |
69 | bucket_entry = tenant_name + "/" + bucket_name; | |
70 | } | |
71 | ||
72 | return bucket_entry; | |
73 | } | |
74 | ||
75 | /* | |
76 | * Tenants are separated from buckets in URLs by a colon in S3. | |
77 | * This function is not to be used on Swift URLs, not even for COPY arguments. | |
78 | */ | |
79 | void rgw_parse_url_bucket(const string &bucket, const string& auth_tenant, | |
80 | string &tenant_name, string &bucket_name) { | |
81 | ||
82 | int pos = bucket.find(':'); | |
83 | if (pos >= 0) { | |
84 | /* | |
85 | * N.B.: We allow ":bucket" syntax with explicit empty tenant in order | |
86 | * to refer to the legacy tenant, in case users in new named tenants | |
87 | * want to access old global buckets. | |
88 | */ | |
89 | tenant_name = bucket.substr(0, pos); | |
90 | bucket_name = bucket.substr(pos + 1); | |
91 | } else { | |
92 | tenant_name = auth_tenant; | |
93 | bucket_name = bucket; | |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
98 | * Get all the buckets owned by a user and fill up an RGWUserBuckets with them. | |
99 | * Returns: 0 on success, -ERR# on failure. | |
100 | */ | |
101 | int rgw_read_user_buckets(RGWRados * store, | |
102 | const rgw_user& user_id, | |
103 | RGWUserBuckets& buckets, | |
104 | const string& marker, | |
105 | const string& end_marker, | |
106 | uint64_t max, | |
107 | bool need_stats, | |
108 | bool *is_truncated, | |
109 | uint64_t default_amount) | |
110 | { | |
111 | int ret; | |
112 | buckets.clear(); | |
3efd9988 | 113 | std::string buckets_obj_id; |
7c673cae | 114 | rgw_get_buckets_obj(user_id, buckets_obj_id); |
11fdf7f2 | 115 | rgw_raw_obj obj(store->svc.zone->get_zone_params().user_uid_pool, buckets_obj_id); |
7c673cae FG |
116 | |
117 | bool truncated = false; | |
118 | string m = marker; | |
119 | ||
120 | uint64_t total = 0; | |
121 | ||
122 | if (!max) { | |
123 | max = default_amount; | |
124 | } | |
125 | ||
126 | do { | |
3efd9988 | 127 | std::list<cls_user_bucket_entry> entries; |
7c673cae | 128 | ret = store->cls_user_list_buckets(obj, m, end_marker, max - total, entries, &m, &truncated); |
3efd9988 | 129 | if (ret == -ENOENT) { |
7c673cae | 130 | ret = 0; |
3efd9988 | 131 | } |
7c673cae | 132 | |
3efd9988 | 133 | if (ret < 0) { |
7c673cae | 134 | return ret; |
3efd9988 | 135 | } |
7c673cae | 136 | |
3efd9988 FG |
137 | for (auto& entry : entries) { |
138 | buckets.add(RGWBucketEnt(user_id, std::move(entry))); | |
7c673cae FG |
139 | total++; |
140 | } | |
141 | ||
142 | } while (truncated && total < max); | |
143 | ||
144 | if (is_truncated != nullptr) { | |
145 | *is_truncated = truncated; | |
146 | } | |
147 | ||
148 | if (need_stats) { | |
149 | map<string, RGWBucketEnt>& m = buckets.get_buckets(); | |
150 | ret = store->update_containers_stats(m); | |
151 | if (ret < 0 && ret != -ENOENT) { | |
152 | ldout(store->ctx(), 0) << "ERROR: could not get stats for buckets" << dendl; | |
153 | return ret; | |
154 | } | |
155 | } | |
156 | return 0; | |
157 | } | |
158 | ||
159 | int rgw_bucket_sync_user_stats(RGWRados *store, const rgw_user& user_id, const RGWBucketInfo& bucket_info) | |
160 | { | |
161 | string buckets_obj_id; | |
162 | rgw_get_buckets_obj(user_id, buckets_obj_id); | |
11fdf7f2 | 163 | rgw_raw_obj obj(store->svc.zone->get_zone_params().user_uid_pool, buckets_obj_id); |
7c673cae FG |
164 | |
165 | return store->cls_user_sync_bucket_stats(obj, bucket_info); | |
166 | } | |
167 | ||
168 | int rgw_bucket_sync_user_stats(RGWRados *store, const string& tenant_name, const string& bucket_name) | |
169 | { | |
170 | RGWBucketInfo bucket_info; | |
11fdf7f2 | 171 | RGWSysObjectCtx obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
172 | int ret = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, NULL); |
173 | if (ret < 0) { | |
174 | ldout(store->ctx(), 0) << "ERROR: could not fetch bucket info: ret=" << ret << dendl; | |
175 | return ret; | |
176 | } | |
177 | ||
178 | ret = rgw_bucket_sync_user_stats(store, bucket_info.owner, bucket_info); | |
179 | if (ret < 0) { | |
180 | ldout(store->ctx(), 0) << "ERROR: could not sync user stats for bucket " << bucket_name << ": ret=" << ret << dendl; | |
181 | return ret; | |
182 | } | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
3efd9988 FG |
187 | int rgw_link_bucket(RGWRados* const store, |
188 | const rgw_user& user_id, | |
189 | rgw_bucket& bucket, | |
190 | ceph::real_time creation_time, | |
191 | bool update_entrypoint) | |
7c673cae FG |
192 | { |
193 | int ret; | |
194 | string& tenant_name = bucket.tenant; | |
195 | string& bucket_name = bucket.name; | |
196 | ||
197 | cls_user_bucket_entry new_bucket; | |
198 | ||
199 | RGWBucketEntryPoint ep; | |
200 | RGWObjVersionTracker ot; | |
201 | ||
202 | bucket.convert(&new_bucket.bucket); | |
203 | new_bucket.size = 0; | |
204 | if (real_clock::is_zero(creation_time)) | |
205 | new_bucket.creation_time = real_clock::now(); | |
206 | else | |
207 | new_bucket.creation_time = creation_time; | |
208 | ||
209 | map<string, bufferlist> attrs; | |
11fdf7f2 | 210 | RGWSysObjectCtx obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
211 | |
212 | if (update_entrypoint) { | |
213 | ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, ep, &ot, NULL, &attrs); | |
214 | if (ret < 0 && ret != -ENOENT) { | |
215 | ldout(store->ctx(), 0) << "ERROR: store->get_bucket_entrypoint_info() returned: " | |
216 | << cpp_strerror(-ret) << dendl; | |
217 | } | |
218 | } | |
219 | ||
220 | string buckets_obj_id; | |
221 | rgw_get_buckets_obj(user_id, buckets_obj_id); | |
222 | ||
11fdf7f2 | 223 | rgw_raw_obj obj(store->svc.zone->get_zone_params().user_uid_pool, buckets_obj_id); |
7c673cae FG |
224 | ret = store->cls_user_add_bucket(obj, new_bucket); |
225 | if (ret < 0) { | |
226 | ldout(store->ctx(), 0) << "ERROR: error adding bucket to directory: " | |
227 | << cpp_strerror(-ret) << dendl; | |
228 | goto done_err; | |
229 | } | |
230 | ||
231 | if (!update_entrypoint) | |
232 | return 0; | |
233 | ||
234 | ep.linked = true; | |
235 | ep.owner = user_id; | |
236 | ep.bucket = bucket; | |
237 | ret = store->put_bucket_entrypoint_info(tenant_name, bucket_name, ep, false, ot, real_time(), &attrs); | |
238 | if (ret < 0) | |
239 | goto done_err; | |
240 | ||
241 | return 0; | |
242 | done_err: | |
243 | int r = rgw_unlink_bucket(store, user_id, bucket.tenant, bucket.name); | |
244 | if (r < 0) { | |
245 | ldout(store->ctx(), 0) << "ERROR: failed unlinking bucket on error cleanup: " | |
246 | << cpp_strerror(-r) << dendl; | |
247 | } | |
248 | return ret; | |
249 | } | |
250 | ||
251 | int rgw_unlink_bucket(RGWRados *store, const rgw_user& user_id, const string& tenant_name, const string& bucket_name, bool update_entrypoint) | |
252 | { | |
253 | int ret; | |
254 | ||
255 | string buckets_obj_id; | |
256 | rgw_get_buckets_obj(user_id, buckets_obj_id); | |
257 | ||
258 | cls_user_bucket bucket; | |
259 | bucket.name = bucket_name; | |
11fdf7f2 | 260 | rgw_raw_obj obj(store->svc.zone->get_zone_params().user_uid_pool, buckets_obj_id); |
7c673cae FG |
261 | ret = store->cls_user_remove_bucket(obj, bucket); |
262 | if (ret < 0) { | |
263 | ldout(store->ctx(), 0) << "ERROR: error removing bucket from directory: " | |
264 | << cpp_strerror(-ret)<< dendl; | |
265 | } | |
266 | ||
267 | if (!update_entrypoint) | |
268 | return 0; | |
269 | ||
270 | RGWBucketEntryPoint ep; | |
271 | RGWObjVersionTracker ot; | |
272 | map<string, bufferlist> attrs; | |
11fdf7f2 | 273 | RGWSysObjectCtx obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
274 | ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, ep, &ot, NULL, &attrs); |
275 | if (ret == -ENOENT) | |
276 | return 0; | |
277 | if (ret < 0) | |
278 | return ret; | |
279 | ||
280 | if (!ep.linked) | |
281 | return 0; | |
282 | ||
283 | if (ep.owner != user_id) { | |
284 | ldout(store->ctx(), 0) << "bucket entry point user mismatch, can't unlink bucket: " << ep.owner << " != " << user_id << dendl; | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | ep.linked = false; | |
289 | return store->put_bucket_entrypoint_info(tenant_name, bucket_name, ep, false, ot, real_time(), &attrs); | |
290 | } | |
291 | ||
292 | int rgw_bucket_store_info(RGWRados *store, const string& bucket_name, bufferlist& bl, bool exclusive, | |
293 | map<string, bufferlist> *pattrs, RGWObjVersionTracker *objv_tracker, | |
294 | real_time mtime) { | |
295 | return store->meta_mgr->put_entry(bucket_meta_handler, bucket_name, bl, exclusive, objv_tracker, mtime, pattrs); | |
296 | } | |
297 | ||
298 | int rgw_bucket_instance_store_info(RGWRados *store, string& entry, bufferlist& bl, bool exclusive, | |
299 | map<string, bufferlist> *pattrs, RGWObjVersionTracker *objv_tracker, | |
300 | real_time mtime) { | |
301 | return store->meta_mgr->put_entry(bucket_instance_meta_handler, entry, bl, exclusive, objv_tracker, mtime, pattrs); | |
302 | } | |
303 | ||
f64942e4 AA |
304 | int rgw_bucket_instance_remove_entry(RGWRados *store, const string& entry, |
305 | RGWObjVersionTracker *objv_tracker) { | |
7c673cae FG |
306 | return store->meta_mgr->remove_entry(bucket_instance_meta_handler, entry, objv_tracker); |
307 | } | |
308 | ||
309 | // 'tenant/' is used in bucket instance keys for sync to avoid parsing ambiguity | |
310 | // with the existing instance[:shard] format. once we parse the shard, the / is | |
311 | // replaced with a : to match the [tenant:]instance format | |
312 | void rgw_bucket_instance_key_to_oid(string& key) | |
313 | { | |
314 | // replace tenant/ with tenant: | |
315 | auto c = key.find('/'); | |
316 | if (c != string::npos) { | |
317 | key[c] = ':'; | |
318 | } | |
319 | } | |
320 | ||
321 | // convert bucket instance oids back to the tenant/ format for metadata keys. | |
322 | // it's safe to parse 'tenant:' only for oids, because they won't contain the | |
323 | // optional :shard at the end | |
324 | void rgw_bucket_instance_oid_to_key(string& oid) | |
325 | { | |
326 | // find first : (could be tenant:bucket or bucket:instance) | |
327 | auto c = oid.find(':'); | |
328 | if (c != string::npos) { | |
329 | // if we find another :, the first one was for tenant | |
330 | if (oid.find(':', c + 1) != string::npos) { | |
331 | oid[c] = '/'; | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
336 | int rgw_bucket_parse_bucket_instance(const string& bucket_instance, string *target_bucket_instance, int *shard_id) | |
337 | { | |
338 | ssize_t pos = bucket_instance.rfind(':'); | |
339 | if (pos < 0) { | |
340 | return -EINVAL; | |
341 | } | |
342 | ||
343 | string first = bucket_instance.substr(0, pos); | |
344 | string second = bucket_instance.substr(pos + 1); | |
345 | ||
346 | if (first.find(':') == string::npos) { | |
347 | *shard_id = -1; | |
348 | *target_bucket_instance = bucket_instance; | |
349 | return 0; | |
350 | } | |
351 | ||
352 | *target_bucket_instance = first; | |
353 | string err; | |
354 | *shard_id = strict_strtol(second.c_str(), 10, &err); | |
355 | if (!err.empty()) { | |
356 | return -EINVAL; | |
357 | } | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
362 | // parse key in format: [tenant/]name:instance[:shard_id] | |
363 | int rgw_bucket_parse_bucket_key(CephContext *cct, const string& key, | |
364 | rgw_bucket *bucket, int *shard_id) | |
365 | { | |
366 | boost::string_ref name{key}; | |
367 | boost::string_ref instance; | |
368 | ||
369 | // split tenant/name | |
370 | auto pos = name.find('/'); | |
371 | if (pos != boost::string_ref::npos) { | |
372 | auto tenant = name.substr(0, pos); | |
373 | bucket->tenant.assign(tenant.begin(), tenant.end()); | |
374 | name = name.substr(pos + 1); | |
375 | } | |
376 | ||
377 | // split name:instance | |
378 | pos = name.find(':'); | |
379 | if (pos != boost::string_ref::npos) { | |
380 | instance = name.substr(pos + 1); | |
381 | name = name.substr(0, pos); | |
382 | } | |
383 | bucket->name.assign(name.begin(), name.end()); | |
384 | ||
385 | // split instance:shard | |
386 | pos = instance.find(':'); | |
387 | if (pos == boost::string_ref::npos) { | |
388 | bucket->bucket_id.assign(instance.begin(), instance.end()); | |
389 | *shard_id = -1; | |
390 | return 0; | |
391 | } | |
392 | ||
393 | // parse shard id | |
394 | auto shard = instance.substr(pos + 1); | |
395 | string err; | |
396 | auto id = strict_strtol(shard.data(), 10, &err); | |
397 | if (!err.empty()) { | |
398 | ldout(cct, 0) << "ERROR: failed to parse bucket shard '" | |
399 | << instance.data() << "': " << err << dendl; | |
400 | return -EINVAL; | |
401 | } | |
402 | ||
403 | *shard_id = id; | |
404 | instance = instance.substr(0, pos); | |
405 | bucket->bucket_id.assign(instance.begin(), instance.end()); | |
406 | return 0; | |
407 | } | |
408 | ||
409 | int rgw_bucket_set_attrs(RGWRados *store, RGWBucketInfo& bucket_info, | |
410 | map<string, bufferlist>& attrs, | |
411 | RGWObjVersionTracker *objv_tracker) | |
412 | { | |
413 | rgw_bucket& bucket = bucket_info.bucket; | |
414 | ||
415 | if (!bucket_info.has_instance_obj) { | |
416 | /* an old bucket object, need to convert it */ | |
11fdf7f2 | 417 | RGWSysObjectCtx obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
418 | int ret = store->convert_old_bucket_info(obj_ctx, bucket.tenant, bucket.name); |
419 | if (ret < 0) { | |
420 | ldout(store->ctx(), 0) << "ERROR: failed converting old bucket info: " << ret << dendl; | |
421 | return ret; | |
422 | } | |
423 | } | |
424 | ||
425 | /* we want the bucket instance name without the oid prefix cruft */ | |
426 | string key = bucket.get_key(); | |
427 | bufferlist bl; | |
428 | ||
11fdf7f2 | 429 | encode(bucket_info, bl); |
7c673cae FG |
430 | |
431 | return rgw_bucket_instance_store_info(store, key, bl, false, &attrs, objv_tracker, real_time()); | |
432 | } | |
433 | ||
434 | static void dump_mulipart_index_results(list<rgw_obj_index_key>& objs_to_unlink, | |
435 | Formatter *f) | |
436 | { | |
d2e6a577 FG |
437 | for (const auto& o : objs_to_unlink) { |
438 | f->dump_string("object", o.name); | |
7c673cae | 439 | } |
7c673cae FG |
440 | } |
441 | ||
442 | void check_bad_user_bucket_mapping(RGWRados *store, const rgw_user& user_id, | |
443 | bool fix) | |
444 | { | |
445 | RGWUserBuckets user_buckets; | |
446 | bool is_truncated = false; | |
447 | string marker; | |
448 | ||
449 | CephContext *cct = store->ctx(); | |
450 | ||
451 | size_t max_entries = cct->_conf->rgw_list_buckets_max_chunk; | |
452 | ||
453 | do { | |
454 | int ret = rgw_read_user_buckets(store, user_id, user_buckets, marker, | |
455 | string(), max_entries, false, | |
456 | &is_truncated); | |
457 | if (ret < 0) { | |
458 | ldout(store->ctx(), 0) << "failed to read user buckets: " | |
459 | << cpp_strerror(-ret) << dendl; | |
460 | return; | |
461 | } | |
462 | ||
463 | map<string, RGWBucketEnt>& buckets = user_buckets.get_buckets(); | |
464 | for (map<string, RGWBucketEnt>::iterator i = buckets.begin(); | |
465 | i != buckets.end(); | |
466 | ++i) { | |
467 | marker = i->first; | |
468 | ||
469 | RGWBucketEnt& bucket_ent = i->second; | |
470 | rgw_bucket& bucket = bucket_ent.bucket; | |
471 | ||
472 | RGWBucketInfo bucket_info; | |
473 | real_time mtime; | |
11fdf7f2 | 474 | RGWSysObjectCtx obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
475 | int r = store->get_bucket_info(obj_ctx, user_id.tenant, bucket.name, bucket_info, &mtime); |
476 | if (r < 0) { | |
477 | ldout(store->ctx(), 0) << "could not get bucket info for bucket=" << bucket << dendl; | |
478 | continue; | |
479 | } | |
480 | ||
481 | rgw_bucket& actual_bucket = bucket_info.bucket; | |
482 | ||
483 | if (actual_bucket.name.compare(bucket.name) != 0 || | |
484 | actual_bucket.tenant.compare(bucket.tenant) != 0 || | |
485 | actual_bucket.marker.compare(bucket.marker) != 0 || | |
486 | actual_bucket.bucket_id.compare(bucket.bucket_id) != 0) { | |
487 | cout << "bucket info mismatch: expected " << actual_bucket << " got " << bucket << std::endl; | |
488 | if (fix) { | |
489 | cout << "fixing" << std::endl; | |
3efd9988 FG |
490 | r = rgw_link_bucket(store, user_id, actual_bucket, |
491 | bucket_info.creation_time); | |
7c673cae FG |
492 | if (r < 0) { |
493 | cerr << "failed to fix bucket: " << cpp_strerror(-r) << std::endl; | |
494 | } | |
495 | } | |
496 | } | |
497 | } | |
498 | } while (is_truncated); | |
499 | } | |
500 | ||
501 | static bool bucket_object_check_filter(const string& oid) | |
502 | { | |
503 | rgw_obj_key key; | |
504 | string ns; | |
505 | return rgw_obj_key::oid_to_key_in_ns(oid, &key, ns); | |
506 | } | |
507 | ||
508 | int rgw_remove_object(RGWRados *store, RGWBucketInfo& bucket_info, rgw_bucket& bucket, rgw_obj_key& key) | |
509 | { | |
510 | RGWObjectCtx rctx(store); | |
511 | ||
512 | if (key.instance.empty()) { | |
513 | key.instance = "null"; | |
514 | } | |
515 | ||
516 | rgw_obj obj(bucket, key); | |
517 | ||
518 | return store->delete_obj(rctx, bucket_info, obj, bucket_info.versioning_status()); | |
519 | } | |
520 | ||
521 | int rgw_remove_bucket(RGWRados *store, rgw_bucket& bucket, bool delete_children) | |
522 | { | |
523 | int ret; | |
524 | map<RGWObjCategory, RGWStorageStats> stats; | |
525 | std::vector<rgw_bucket_dir_entry> objs; | |
526 | map<string, bool> common_prefixes; | |
527 | RGWBucketInfo info; | |
11fdf7f2 | 528 | RGWSysObjectCtx obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
529 | |
530 | string bucket_ver, master_ver; | |
531 | ||
532 | ret = store->get_bucket_info(obj_ctx, bucket.tenant, bucket.name, info, NULL); | |
533 | if (ret < 0) | |
534 | return ret; | |
535 | ||
536 | ret = store->get_bucket_stats(info, RGW_NO_SHARD, &bucket_ver, &master_ver, stats, NULL); | |
537 | if (ret < 0) | |
538 | return ret; | |
539 | ||
7c673cae FG |
540 | RGWRados::Bucket target(store, info); |
541 | RGWRados::Bucket::List list_op(&target); | |
224ce89b WB |
542 | CephContext *cct = store->ctx(); |
543 | int max = 1000; | |
7c673cae FG |
544 | |
545 | list_op.params.list_versions = true; | |
a8e16298 | 546 | list_op.params.allow_unordered = true; |
7c673cae | 547 | |
a8e16298 | 548 | bool is_truncated = false; |
224ce89b WB |
549 | do { |
550 | objs.clear(); | |
7c673cae | 551 | |
a8e16298 | 552 | ret = list_op.list_objects(max, &objs, &common_prefixes, &is_truncated); |
224ce89b WB |
553 | if (ret < 0) |
554 | return ret; | |
7c673cae | 555 | |
224ce89b WB |
556 | if (!objs.empty() && !delete_children) { |
557 | lderr(store->ctx()) << "ERROR: could not remove non-empty bucket " << bucket.name << dendl; | |
558 | return -ENOTEMPTY; | |
559 | } | |
560 | ||
561 | for (const auto& obj : objs) { | |
562 | rgw_obj_key key(obj.key); | |
563 | ret = rgw_remove_object(store, info, bucket, key); | |
a8e16298 | 564 | if (ret < 0 && ret != -ENOENT) { |
7c673cae | 565 | return ret; |
a8e16298 | 566 | } |
224ce89b | 567 | } |
a8e16298 | 568 | } while(is_truncated); |
7c673cae | 569 | |
224ce89b | 570 | string prefix, delimiter; |
7c673cae | 571 | |
224ce89b WB |
572 | ret = abort_bucket_multiparts(store, cct, info, prefix, delimiter); |
573 | if (ret < 0) { | |
574 | return ret; | |
7c673cae FG |
575 | } |
576 | ||
b32b8144 | 577 | ret = rgw_bucket_sync_user_stats(store, info.owner, info); |
7c673cae FG |
578 | if ( ret < 0) { |
579 | dout(1) << "WARNING: failed sync user stats before bucket delete. ret=" << ret << dendl; | |
580 | } | |
581 | ||
582 | RGWObjVersionTracker objv_tracker; | |
583 | ||
a8e16298 TL |
584 | // if we deleted children above we will force delete, as any that |
585 | // remain is detrius from a prior bug | |
586 | ret = store->delete_bucket(info, objv_tracker, !delete_children); | |
7c673cae | 587 | if (ret < 0) { |
a8e16298 TL |
588 | lderr(store->ctx()) << "ERROR: could not remove bucket " << |
589 | bucket.name << dendl; | |
7c673cae FG |
590 | return ret; |
591 | } | |
592 | ||
593 | ret = rgw_unlink_bucket(store, info.owner, bucket.tenant, bucket.name, false); | |
594 | if (ret < 0) { | |
595 | lderr(store->ctx()) << "ERROR: unable to remove user bucket information" << dendl; | |
596 | } | |
597 | ||
598 | return ret; | |
599 | } | |
600 | ||
601 | static int aio_wait(librados::AioCompletion *handle) | |
602 | { | |
603 | librados::AioCompletion *c = (librados::AioCompletion *)handle; | |
604 | c->wait_for_safe(); | |
605 | int ret = c->get_return_value(); | |
606 | c->release(); | |
607 | return ret; | |
608 | } | |
609 | ||
610 | static int drain_handles(list<librados::AioCompletion *>& pending) | |
611 | { | |
612 | int ret = 0; | |
613 | while (!pending.empty()) { | |
614 | librados::AioCompletion *handle = pending.front(); | |
615 | pending.pop_front(); | |
616 | int r = aio_wait(handle); | |
617 | if (r < 0) { | |
618 | ret = r; | |
619 | } | |
620 | } | |
621 | return ret; | |
622 | } | |
623 | ||
624 | int rgw_remove_bucket_bypass_gc(RGWRados *store, rgw_bucket& bucket, | |
625 | int concurrent_max, bool keep_index_consistent) | |
626 | { | |
627 | int ret; | |
628 | map<RGWObjCategory, RGWStorageStats> stats; | |
629 | std::vector<rgw_bucket_dir_entry> objs; | |
630 | map<string, bool> common_prefixes; | |
631 | RGWBucketInfo info; | |
632 | RGWObjectCtx obj_ctx(store); | |
11fdf7f2 | 633 | RGWSysObjectCtx sysobj_ctx = store->svc.sysobj->init_obj_ctx(); |
224ce89b | 634 | CephContext *cct = store->ctx(); |
7c673cae FG |
635 | |
636 | string bucket_ver, master_ver; | |
637 | ||
11fdf7f2 | 638 | ret = store->get_bucket_info(sysobj_ctx, bucket.tenant, bucket.name, info, NULL); |
7c673cae FG |
639 | if (ret < 0) |
640 | return ret; | |
641 | ||
642 | ret = store->get_bucket_stats(info, RGW_NO_SHARD, &bucket_ver, &master_ver, stats, NULL); | |
643 | if (ret < 0) | |
644 | return ret; | |
645 | ||
224ce89b WB |
646 | string prefix, delimiter; |
647 | ||
648 | ret = abort_bucket_multiparts(store, cct, info, prefix, delimiter); | |
649 | if (ret < 0) { | |
650 | return ret; | |
651 | } | |
7c673cae FG |
652 | |
653 | RGWRados::Bucket target(store, info); | |
654 | RGWRados::Bucket::List list_op(&target); | |
655 | ||
656 | list_op.params.list_versions = true; | |
a8e16298 | 657 | list_op.params.allow_unordered = true; |
7c673cae FG |
658 | |
659 | std::list<librados::AioCompletion*> handles; | |
660 | ||
661 | int max = 1000; | |
662 | int max_aio = concurrent_max; | |
a8e16298 TL |
663 | bool is_truncated = true; |
664 | ||
665 | while (is_truncated) { | |
666 | objs.clear(); | |
667 | ret = list_op.list_objects(max, &objs, &common_prefixes, &is_truncated); | |
668 | if (ret < 0) | |
669 | return ret; | |
7c673cae | 670 | |
7c673cae FG |
671 | std::vector<rgw_bucket_dir_entry>::iterator it = objs.begin(); |
672 | for (; it != objs.end(); ++it) { | |
673 | RGWObjState *astate = NULL; | |
674 | rgw_obj obj(bucket, (*it).key); | |
675 | ||
676 | ret = store->get_obj_state(&obj_ctx, info, obj, &astate, false); | |
677 | if (ret == -ENOENT) { | |
678 | dout(1) << "WARNING: cannot find obj state for obj " << obj.get_oid() << dendl; | |
679 | continue; | |
680 | } | |
681 | if (ret < 0) { | |
682 | lderr(store->ctx()) << "ERROR: get obj state returned with error " << ret << dendl; | |
683 | return ret; | |
684 | } | |
685 | ||
686 | if (astate->has_manifest) { | |
687 | RGWObjManifest& manifest = astate->manifest; | |
688 | RGWObjManifest::obj_iterator miter = manifest.obj_begin(); | |
689 | rgw_obj head_obj = manifest.get_obj(); | |
690 | rgw_raw_obj raw_head_obj; | |
691 | store->obj_to_raw(info.placement_rule, head_obj, &raw_head_obj); | |
692 | ||
693 | ||
694 | for (; miter != manifest.obj_end() && max_aio--; ++miter) { | |
695 | if (!max_aio) { | |
696 | ret = drain_handles(handles); | |
697 | if (ret < 0) { | |
698 | lderr(store->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl; | |
699 | return ret; | |
700 | } | |
701 | max_aio = concurrent_max; | |
702 | } | |
703 | ||
704 | rgw_raw_obj last_obj = miter.get_location().get_raw_obj(store); | |
705 | if (last_obj == raw_head_obj) { | |
706 | // have the head obj deleted at the end | |
707 | continue; | |
708 | } | |
709 | ||
710 | ret = store->delete_raw_obj_aio(last_obj, handles); | |
711 | if (ret < 0) { | |
712 | lderr(store->ctx()) << "ERROR: delete obj aio failed with " << ret << dendl; | |
713 | return ret; | |
714 | } | |
715 | } // for all shadow objs | |
716 | ||
717 | ret = store->delete_obj_aio(head_obj, info, astate, handles, keep_index_consistent); | |
718 | if (ret < 0) { | |
719 | lderr(store->ctx()) << "ERROR: delete obj aio failed with " << ret << dendl; | |
720 | return ret; | |
721 | } | |
722 | } | |
723 | ||
724 | if (!max_aio) { | |
725 | ret = drain_handles(handles); | |
726 | if (ret < 0) { | |
727 | lderr(store->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl; | |
728 | return ret; | |
729 | } | |
730 | max_aio = concurrent_max; | |
731 | } | |
732 | } // for all RGW objects | |
7c673cae FG |
733 | } |
734 | ||
735 | ret = drain_handles(handles); | |
736 | if (ret < 0) { | |
737 | lderr(store->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl; | |
738 | return ret; | |
739 | } | |
740 | ||
b32b8144 | 741 | ret = rgw_bucket_sync_user_stats(store, info.owner, info); |
7c673cae FG |
742 | if (ret < 0) { |
743 | dout(1) << "WARNING: failed sync user stats before bucket delete. ret=" << ret << dendl; | |
744 | } | |
745 | ||
746 | RGWObjVersionTracker objv_tracker; | |
747 | ||
a8e16298 TL |
748 | // this function can only be run if caller wanted children to be |
749 | // deleted, so we can ignore the check for children as any that | |
750 | // remain are detritus from a prior bug | |
751 | ret = store->delete_bucket(info, objv_tracker, false); | |
7c673cae | 752 | if (ret < 0) { |
b32b8144 | 753 | lderr(store->ctx()) << "ERROR: could not remove bucket " << bucket.name << dendl; |
7c673cae FG |
754 | return ret; |
755 | } | |
756 | ||
7c673cae FG |
757 | ret = rgw_unlink_bucket(store, info.owner, bucket.tenant, bucket.name, false); |
758 | if (ret < 0) { | |
759 | lderr(store->ctx()) << "ERROR: unable to remove user bucket information" << dendl; | |
760 | } | |
761 | ||
762 | return ret; | |
763 | } | |
764 | ||
765 | int rgw_bucket_delete_bucket_obj(RGWRados *store, | |
766 | const string& tenant_name, | |
767 | const string& bucket_name, | |
768 | RGWObjVersionTracker& objv_tracker) | |
769 | { | |
770 | string key; | |
771 | ||
772 | rgw_make_bucket_entry_name(tenant_name, bucket_name, key); | |
773 | return store->meta_mgr->remove_entry(bucket_meta_handler, key, &objv_tracker); | |
774 | } | |
775 | ||
776 | static void set_err_msg(std::string *sink, std::string msg) | |
777 | { | |
778 | if (sink && !msg.empty()) | |
779 | *sink = msg; | |
780 | } | |
781 | ||
782 | int RGWBucket::init(RGWRados *storage, RGWBucketAdminOpState& op_state) | |
783 | { | |
784 | if (!storage) | |
785 | return -EINVAL; | |
786 | ||
787 | store = storage; | |
788 | ||
789 | rgw_user user_id = op_state.get_user_id(); | |
790 | tenant = user_id.tenant; | |
791 | bucket_name = op_state.get_bucket_name(); | |
792 | RGWUserBuckets user_buckets; | |
11fdf7f2 | 793 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
794 | |
795 | if (bucket_name.empty() && user_id.empty()) | |
796 | return -EINVAL; | |
797 | ||
798 | if (!bucket_name.empty()) { | |
799 | int r = store->get_bucket_info(obj_ctx, tenant, bucket_name, bucket_info, NULL); | |
800 | if (r < 0) { | |
801 | ldout(store->ctx(), 0) << "could not get bucket info for bucket=" << bucket_name << dendl; | |
802 | return r; | |
803 | } | |
804 | ||
805 | op_state.set_bucket(bucket_info.bucket); | |
806 | } | |
807 | ||
808 | if (!user_id.empty()) { | |
809 | int r = rgw_get_user_info_by_uid(store, user_id, user_info); | |
810 | if (r < 0) | |
811 | return r; | |
812 | ||
813 | op_state.display_name = user_info.display_name; | |
814 | } | |
815 | ||
816 | clear_failure(); | |
817 | return 0; | |
818 | } | |
819 | ||
820 | int RGWBucket::link(RGWBucketAdminOpState& op_state, std::string *err_msg) | |
821 | { | |
822 | if (!op_state.is_user_op()) { | |
823 | set_err_msg(err_msg, "empty user id"); | |
824 | return -EINVAL; | |
825 | } | |
826 | ||
827 | string bucket_id = op_state.get_bucket_id(); | |
828 | if (bucket_id.empty()) { | |
829 | set_err_msg(err_msg, "empty bucket instance id"); | |
830 | return -EINVAL; | |
831 | } | |
832 | ||
833 | std::string display_name = op_state.get_user_display_name(); | |
834 | rgw_bucket bucket = op_state.get_bucket(); | |
835 | ||
11fdf7f2 TL |
836 | const rgw_pool& root_pool = store->svc.zone->get_zone_params().domain_root; |
837 | std::string bucket_entry; | |
838 | rgw_make_bucket_entry_name(tenant, bucket_name, bucket_entry); | |
839 | rgw_raw_obj obj(root_pool, bucket_entry); | |
7c673cae FG |
840 | RGWObjVersionTracker objv_tracker; |
841 | ||
842 | map<string, bufferlist> attrs; | |
843 | RGWBucketInfo bucket_info; | |
844 | ||
11fdf7f2 TL |
845 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
846 | int r = store->get_bucket_instance_info(obj_ctx, bucket, bucket_info, NULL, &attrs); | |
7c673cae FG |
847 | if (r < 0) { |
848 | return r; | |
849 | } | |
850 | ||
7c673cae FG |
851 | map<string, bufferlist>::iterator aiter = attrs.find(RGW_ATTR_ACL); |
852 | if (aiter != attrs.end()) { | |
853 | bufferlist aclbl = aiter->second; | |
854 | RGWAccessControlPolicy policy; | |
855 | ACLOwner owner; | |
856 | try { | |
11fdf7f2 TL |
857 | auto iter = aclbl.cbegin(); |
858 | decode(policy, iter); | |
7c673cae FG |
859 | owner = policy.get_owner(); |
860 | } catch (buffer::error& err) { | |
861 | set_err_msg(err_msg, "couldn't decode policy"); | |
862 | return -EIO; | |
863 | } | |
864 | ||
865 | r = rgw_unlink_bucket(store, owner.get_id(), bucket.tenant, bucket.name, false); | |
866 | if (r < 0) { | |
867 | set_err_msg(err_msg, "could not unlink policy from user " + owner.get_id().to_str()); | |
868 | return r; | |
869 | } | |
870 | ||
871 | // now update the user for the bucket... | |
872 | if (display_name.empty()) { | |
873 | ldout(store->ctx(), 0) << "WARNING: user " << user_info.user_id << " has no display name set" << dendl; | |
874 | } | |
875 | policy.create_default(user_info.user_id, display_name); | |
876 | ||
877 | owner = policy.get_owner(); | |
31f18b77 | 878 | r = store->set_bucket_owner(bucket_info.bucket, owner); |
7c673cae FG |
879 | if (r < 0) { |
880 | set_err_msg(err_msg, "failed to set bucket owner: " + cpp_strerror(-r)); | |
881 | return r; | |
882 | } | |
883 | ||
884 | // ...and encode the acl | |
885 | aclbl.clear(); | |
886 | policy.encode(aclbl); | |
887 | ||
11fdf7f2 TL |
888 | auto sysobj = obj_ctx.get_obj(obj); |
889 | r = sysobj.wop() | |
890 | .set_objv_tracker(&objv_tracker) | |
891 | .write_attr(RGW_ATTR_ACL, aclbl); | |
224ce89b | 892 | if (r < 0) { |
7c673cae | 893 | return r; |
224ce89b | 894 | } |
7c673cae FG |
895 | |
896 | RGWAccessControlPolicy policy_instance; | |
897 | policy_instance.create_default(user_info.user_id, display_name); | |
898 | aclbl.clear(); | |
899 | policy_instance.encode(aclbl); | |
900 | ||
11fdf7f2 TL |
901 | rgw_raw_obj obj_bucket_instance; |
902 | store->get_bucket_instance_obj(bucket, obj_bucket_instance); | |
903 | auto inst_sysobj = obj_ctx.get_obj(obj_bucket_instance); | |
904 | r = inst_sysobj.wop() | |
905 | .set_objv_tracker(&objv_tracker) | |
906 | .write_attr(RGW_ATTR_ACL, aclbl); | |
224ce89b WB |
907 | if (r < 0) { |
908 | return r; | |
909 | } | |
7c673cae | 910 | |
3efd9988 FG |
911 | r = rgw_link_bucket(store, user_info.user_id, bucket_info.bucket, |
912 | ceph::real_time()); | |
224ce89b | 913 | if (r < 0) { |
7c673cae | 914 | return r; |
224ce89b | 915 | } |
7c673cae FG |
916 | } |
917 | ||
918 | return 0; | |
919 | } | |
920 | ||
921 | int RGWBucket::unlink(RGWBucketAdminOpState& op_state, std::string *err_msg) | |
922 | { | |
923 | rgw_bucket bucket = op_state.get_bucket(); | |
924 | ||
925 | if (!op_state.is_user_op()) { | |
926 | set_err_msg(err_msg, "could not fetch user or user bucket info"); | |
927 | return -EINVAL; | |
928 | } | |
929 | ||
930 | int r = rgw_unlink_bucket(store, user_info.user_id, bucket.tenant, bucket.name); | |
931 | if (r < 0) { | |
932 | set_err_msg(err_msg, "error unlinking bucket" + cpp_strerror(-r)); | |
933 | } | |
934 | ||
935 | return r; | |
936 | } | |
937 | ||
94b18763 FG |
938 | int RGWBucket::set_quota(RGWBucketAdminOpState& op_state, std::string *err_msg) |
939 | { | |
940 | rgw_bucket bucket = op_state.get_bucket(); | |
941 | RGWBucketInfo bucket_info; | |
942 | map<string, bufferlist> attrs; | |
11fdf7f2 | 943 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
94b18763 FG |
944 | int r = store->get_bucket_info(obj_ctx, bucket.tenant, bucket.name, bucket_info, NULL, &attrs); |
945 | if (r < 0) { | |
946 | set_err_msg(err_msg, "could not get bucket info for bucket=" + bucket.name + ": " + cpp_strerror(-r)); | |
947 | return r; | |
948 | } | |
949 | ||
950 | bucket_info.quota = op_state.quota; | |
951 | r = store->put_bucket_instance_info(bucket_info, false, real_time(), &attrs); | |
952 | if (r < 0) { | |
953 | set_err_msg(err_msg, "ERROR: failed writing bucket instance info: " + cpp_strerror(-r)); | |
954 | return r; | |
955 | } | |
956 | return r; | |
957 | } | |
958 | ||
7c673cae FG |
959 | int RGWBucket::remove(RGWBucketAdminOpState& op_state, bool bypass_gc, |
960 | bool keep_index_consistent, std::string *err_msg) | |
961 | { | |
962 | bool delete_children = op_state.will_delete_children(); | |
963 | rgw_bucket bucket = op_state.get_bucket(); | |
964 | int ret; | |
965 | ||
966 | if (bypass_gc) { | |
967 | if (delete_children) { | |
968 | ret = rgw_remove_bucket_bypass_gc(store, bucket, op_state.get_max_aio(), keep_index_consistent); | |
969 | } else { | |
970 | set_err_msg(err_msg, "purge objects should be set for gc to be bypassed"); | |
971 | return -EINVAL; | |
972 | } | |
973 | } else { | |
974 | ret = rgw_remove_bucket(store, bucket, delete_children); | |
975 | } | |
976 | ||
977 | if (ret < 0) { | |
978 | set_err_msg(err_msg, "unable to remove bucket" + cpp_strerror(-ret)); | |
979 | return ret; | |
980 | } | |
981 | ||
982 | return 0; | |
983 | } | |
984 | ||
985 | int RGWBucket::remove_object(RGWBucketAdminOpState& op_state, std::string *err_msg) | |
986 | { | |
987 | rgw_bucket bucket = op_state.get_bucket(); | |
988 | std::string object_name = op_state.get_object_name(); | |
989 | ||
990 | rgw_obj_key key(object_name); | |
991 | ||
992 | int ret = rgw_remove_object(store, bucket_info, bucket, key); | |
993 | if (ret < 0) { | |
994 | set_err_msg(err_msg, "unable to remove object" + cpp_strerror(-ret)); | |
995 | return ret; | |
996 | } | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
1001 | static void dump_bucket_index(map<string, rgw_bucket_dir_entry> result, Formatter *f) | |
1002 | { | |
1003 | map<string, rgw_bucket_dir_entry>::iterator iter; | |
1004 | for (iter = result.begin(); iter != result.end(); ++iter) { | |
1005 | f->dump_string("object", iter->first); | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | static void dump_bucket_usage(map<RGWObjCategory, RGWStorageStats>& stats, Formatter *formatter) | |
1010 | { | |
1011 | map<RGWObjCategory, RGWStorageStats>::iterator iter; | |
1012 | ||
1013 | formatter->open_object_section("usage"); | |
1014 | for (iter = stats.begin(); iter != stats.end(); ++iter) { | |
1015 | RGWStorageStats& s = iter->second; | |
1016 | const char *cat_name = rgw_obj_category_name(iter->first); | |
1017 | formatter->open_object_section(cat_name); | |
1018 | s.dump(formatter); | |
1019 | formatter->close_section(); | |
1020 | } | |
1021 | formatter->close_section(); | |
1022 | } | |
1023 | ||
1024 | static void dump_index_check(map<RGWObjCategory, RGWStorageStats> existing_stats, | |
1025 | map<RGWObjCategory, RGWStorageStats> calculated_stats, | |
1026 | Formatter *formatter) | |
1027 | { | |
1028 | formatter->open_object_section("check_result"); | |
1029 | formatter->open_object_section("existing_header"); | |
1030 | dump_bucket_usage(existing_stats, formatter); | |
1031 | formatter->close_section(); | |
1032 | formatter->open_object_section("calculated_header"); | |
1033 | dump_bucket_usage(calculated_stats, formatter); | |
1034 | formatter->close_section(); | |
1035 | formatter->close_section(); | |
1036 | } | |
1037 | ||
1038 | int RGWBucket::check_bad_index_multipart(RGWBucketAdminOpState& op_state, | |
d2e6a577 | 1039 | RGWFormatterFlusher& flusher ,std::string *err_msg) |
7c673cae FG |
1040 | { |
1041 | bool fix_index = op_state.will_fix_index(); | |
1042 | rgw_bucket bucket = op_state.get_bucket(); | |
1043 | ||
d2e6a577 | 1044 | size_t max = 1000; |
7c673cae FG |
1045 | |
1046 | map<string, bool> common_prefixes; | |
1047 | ||
1048 | bool is_truncated; | |
1049 | map<string, bool> meta_objs; | |
1050 | map<rgw_obj_index_key, string> all_objs; | |
1051 | ||
1052 | RGWBucketInfo bucket_info; | |
11fdf7f2 | 1053 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
1054 | int r = store->get_bucket_instance_info(obj_ctx, bucket, bucket_info, nullptr, nullptr); |
1055 | if (r < 0) { | |
1056 | ldout(store->ctx(), 0) << "ERROR: " << __func__ << "(): get_bucket_instance_info(bucket=" << bucket << ") returned r=" << r << dendl; | |
1057 | return r; | |
1058 | } | |
1059 | ||
1060 | RGWRados::Bucket target(store, bucket_info); | |
1061 | RGWRados::Bucket::List list_op(&target); | |
1062 | ||
1063 | list_op.params.list_versions = true; | |
224ce89b | 1064 | list_op.params.ns = RGW_OBJ_NS_MULTIPART; |
7c673cae FG |
1065 | |
1066 | do { | |
1067 | vector<rgw_bucket_dir_entry> result; | |
1068 | int r = list_op.list_objects(max, &result, &common_prefixes, &is_truncated); | |
1069 | if (r < 0) { | |
1070 | set_err_msg(err_msg, "failed to list objects in bucket=" + bucket.name + | |
1071 | " err=" + cpp_strerror(-r)); | |
1072 | ||
1073 | return r; | |
1074 | } | |
1075 | ||
1076 | vector<rgw_bucket_dir_entry>::iterator iter; | |
1077 | for (iter = result.begin(); iter != result.end(); ++iter) { | |
31f18b77 FG |
1078 | rgw_obj_index_key key = iter->key; |
1079 | rgw_obj obj(bucket, key); | |
1080 | string oid = obj.get_oid(); | |
7c673cae FG |
1081 | |
1082 | int pos = oid.find_last_of('.'); | |
1083 | if (pos < 0) { | |
1084 | /* obj has no suffix */ | |
1085 | all_objs[key] = oid; | |
1086 | } else { | |
1087 | /* obj has suffix */ | |
1088 | string name = oid.substr(0, pos); | |
1089 | string suffix = oid.substr(pos + 1); | |
1090 | ||
1091 | if (suffix.compare("meta") == 0) { | |
1092 | meta_objs[name] = true; | |
1093 | } else { | |
1094 | all_objs[key] = name; | |
1095 | } | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | } while (is_truncated); | |
1100 | ||
d2e6a577 FG |
1101 | list<rgw_obj_index_key> objs_to_unlink; |
1102 | Formatter *f = flusher.get_formatter(); | |
1103 | ||
1104 | f->open_array_section("invalid_multipart_entries"); | |
1105 | ||
7c673cae FG |
1106 | for (auto aiter = all_objs.begin(); aiter != all_objs.end(); ++aiter) { |
1107 | string& name = aiter->second; | |
1108 | ||
7c673cae FG |
1109 | if (meta_objs.find(name) == meta_objs.end()) { |
1110 | objs_to_unlink.push_back(aiter->first); | |
1111 | } | |
7c673cae | 1112 | |
d2e6a577 FG |
1113 | if (objs_to_unlink.size() > max) { |
1114 | if (fix_index) { | |
1115 | int r = store->remove_objs_from_index(bucket_info, objs_to_unlink); | |
1116 | if (r < 0) { | |
1117 | set_err_msg(err_msg, "ERROR: remove_obj_from_index() returned error: " + | |
1118 | cpp_strerror(-r)); | |
1119 | return r; | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | dump_mulipart_index_results(objs_to_unlink, flusher.get_formatter()); | |
1124 | flusher.flush(); | |
1125 | objs_to_unlink.clear(); | |
1126 | } | |
1127 | } | |
7c673cae FG |
1128 | |
1129 | if (fix_index) { | |
1130 | int r = store->remove_objs_from_index(bucket_info, objs_to_unlink); | |
1131 | if (r < 0) { | |
1132 | set_err_msg(err_msg, "ERROR: remove_obj_from_index() returned error: " + | |
1133 | cpp_strerror(-r)); | |
1134 | ||
1135 | return r; | |
1136 | } | |
1137 | } | |
1138 | ||
d2e6a577 FG |
1139 | dump_mulipart_index_results(objs_to_unlink, f); |
1140 | f->close_section(); | |
1141 | flusher.flush(); | |
1142 | ||
7c673cae FG |
1143 | return 0; |
1144 | } | |
1145 | ||
1146 | int RGWBucket::check_object_index(RGWBucketAdminOpState& op_state, | |
1147 | RGWFormatterFlusher& flusher, | |
1148 | std::string *err_msg) | |
1149 | { | |
1150 | ||
1151 | bool fix_index = op_state.will_fix_index(); | |
1152 | ||
7c673cae FG |
1153 | if (!fix_index) { |
1154 | set_err_msg(err_msg, "check-objects flag requires fix index enabled"); | |
1155 | return -EINVAL; | |
1156 | } | |
1157 | ||
1158 | store->cls_obj_set_bucket_tag_timeout(bucket_info, BUCKET_TAG_TIMEOUT); | |
1159 | ||
1160 | string prefix; | |
1161 | rgw_obj_index_key marker; | |
1162 | bool is_truncated = true; | |
1163 | ||
1164 | Formatter *formatter = flusher.get_formatter(); | |
1165 | formatter->open_object_section("objects"); | |
1166 | while (is_truncated) { | |
1167 | map<string, rgw_bucket_dir_entry> result; | |
1168 | ||
1adf2230 AA |
1169 | int r = store->cls_bucket_list_ordered(bucket_info, RGW_NO_SHARD, |
1170 | marker, prefix, 1000, true, | |
1171 | result, &is_truncated, &marker, | |
1172 | bucket_object_check_filter); | |
7c673cae FG |
1173 | if (r == -ENOENT) { |
1174 | break; | |
1175 | } else if (r < 0 && r != -ENOENT) { | |
1176 | set_err_msg(err_msg, "ERROR: failed operation r=" + cpp_strerror(-r)); | |
1177 | } | |
1178 | ||
7c673cae FG |
1179 | dump_bucket_index(result, formatter); |
1180 | flusher.flush(); | |
7c673cae FG |
1181 | } |
1182 | ||
1183 | formatter->close_section(); | |
1184 | ||
1185 | store->cls_obj_set_bucket_tag_timeout(bucket_info, 0); | |
1186 | ||
1187 | return 0; | |
1188 | } | |
1189 | ||
1190 | ||
1191 | int RGWBucket::check_index(RGWBucketAdminOpState& op_state, | |
1192 | map<RGWObjCategory, RGWStorageStats>& existing_stats, | |
1193 | map<RGWObjCategory, RGWStorageStats>& calculated_stats, | |
1194 | std::string *err_msg) | |
1195 | { | |
7c673cae FG |
1196 | bool fix_index = op_state.will_fix_index(); |
1197 | ||
1198 | int r = store->bucket_check_index(bucket_info, &existing_stats, &calculated_stats); | |
1199 | if (r < 0) { | |
1200 | set_err_msg(err_msg, "failed to check index error=" + cpp_strerror(-r)); | |
1201 | return r; | |
1202 | } | |
1203 | ||
1204 | if (fix_index) { | |
1205 | r = store->bucket_rebuild_index(bucket_info); | |
1206 | if (r < 0) { | |
1207 | set_err_msg(err_msg, "failed to rebuild index err=" + cpp_strerror(-r)); | |
1208 | return r; | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | return 0; | |
1213 | } | |
1214 | ||
1215 | ||
1216 | int RGWBucket::policy_bl_to_stream(bufferlist& bl, ostream& o) | |
1217 | { | |
1218 | RGWAccessControlPolicy_S3 policy(g_ceph_context); | |
11fdf7f2 | 1219 | auto iter = bl.cbegin(); |
7c673cae FG |
1220 | try { |
1221 | policy.decode(iter); | |
1222 | } catch (buffer::error& err) { | |
1223 | dout(0) << "ERROR: caught buffer::error, could not decode policy" << dendl; | |
1224 | return -EIO; | |
1225 | } | |
1226 | policy.to_xml(o); | |
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | static int policy_decode(RGWRados *store, bufferlist& bl, RGWAccessControlPolicy& policy) | |
1231 | { | |
11fdf7f2 | 1232 | auto iter = bl.cbegin(); |
7c673cae FG |
1233 | try { |
1234 | policy.decode(iter); | |
1235 | } catch (buffer::error& err) { | |
1236 | ldout(store->ctx(), 0) << "ERROR: caught buffer::error, could not decode policy" << dendl; | |
1237 | return -EIO; | |
1238 | } | |
1239 | return 0; | |
1240 | } | |
1241 | ||
1242 | int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, RGWAccessControlPolicy& policy) | |
1243 | { | |
1244 | std::string object_name = op_state.get_object_name(); | |
1245 | rgw_bucket bucket = op_state.get_bucket(); | |
11fdf7f2 | 1246 | auto sysobj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
1247 | RGWObjectCtx obj_ctx(store); |
1248 | ||
1249 | RGWBucketInfo bucket_info; | |
1250 | map<string, bufferlist> attrs; | |
11fdf7f2 | 1251 | int ret = store->get_bucket_info(sysobj_ctx, bucket.tenant, bucket.name, bucket_info, NULL, &attrs); |
7c673cae FG |
1252 | if (ret < 0) { |
1253 | return ret; | |
1254 | } | |
1255 | ||
1256 | if (!object_name.empty()) { | |
1257 | bufferlist bl; | |
1258 | rgw_obj obj(bucket, object_name); | |
1259 | ||
1260 | RGWRados::Object op_target(store, bucket_info, obj_ctx, obj); | |
1261 | RGWRados::Object::Read rop(&op_target); | |
1262 | ||
1263 | int ret = rop.get_attr(RGW_ATTR_ACL, bl); | |
1264 | if (ret < 0) | |
1265 | return ret; | |
1266 | ||
1267 | return policy_decode(store, bl, policy); | |
1268 | } | |
1269 | ||
1270 | map<string, bufferlist>::iterator aiter = attrs.find(RGW_ATTR_ACL); | |
1271 | if (aiter == attrs.end()) { | |
1272 | return -ENOENT; | |
1273 | } | |
1274 | ||
1275 | return policy_decode(store, aiter->second, policy); | |
1276 | } | |
1277 | ||
1278 | ||
1279 | int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state, | |
1280 | RGWAccessControlPolicy& policy) | |
1281 | { | |
1282 | RGWBucket bucket; | |
1283 | ||
1284 | int ret = bucket.init(store, op_state); | |
1285 | if (ret < 0) | |
1286 | return ret; | |
1287 | ||
1288 | ret = bucket.get_policy(op_state, policy); | |
1289 | if (ret < 0) | |
1290 | return ret; | |
1291 | ||
1292 | return 0; | |
1293 | } | |
1294 | ||
1295 | /* Wrappers to facilitate RESTful interface */ | |
1296 | ||
1297 | ||
1298 | int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state, | |
1299 | RGWFormatterFlusher& flusher) | |
1300 | { | |
1301 | RGWAccessControlPolicy policy(store->ctx()); | |
1302 | ||
1303 | int ret = get_policy(store, op_state, policy); | |
1304 | if (ret < 0) | |
1305 | return ret; | |
1306 | ||
1307 | Formatter *formatter = flusher.get_formatter(); | |
1308 | ||
1309 | flusher.start(0); | |
1310 | ||
1311 | formatter->open_object_section("policy"); | |
1312 | policy.dump(formatter); | |
1313 | formatter->close_section(); | |
1314 | ||
1315 | flusher.flush(); | |
1316 | ||
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | int RGWBucketAdminOp::dump_s3_policy(RGWRados *store, RGWBucketAdminOpState& op_state, | |
1321 | ostream& os) | |
1322 | { | |
1323 | RGWAccessControlPolicy_S3 policy(store->ctx()); | |
1324 | ||
1325 | int ret = get_policy(store, op_state, policy); | |
1326 | if (ret < 0) | |
1327 | return ret; | |
1328 | ||
1329 | policy.to_xml(os); | |
1330 | ||
1331 | return 0; | |
1332 | } | |
1333 | ||
1334 | int RGWBucketAdminOp::unlink(RGWRados *store, RGWBucketAdminOpState& op_state) | |
1335 | { | |
1336 | RGWBucket bucket; | |
1337 | ||
1338 | int ret = bucket.init(store, op_state); | |
1339 | if (ret < 0) | |
1340 | return ret; | |
1341 | ||
1342 | return bucket.unlink(op_state); | |
1343 | } | |
1344 | ||
1345 | int RGWBucketAdminOp::link(RGWRados *store, RGWBucketAdminOpState& op_state, string *err) | |
1346 | { | |
1347 | RGWBucket bucket; | |
1348 | ||
1349 | int ret = bucket.init(store, op_state); | |
1350 | if (ret < 0) | |
1351 | return ret; | |
1352 | ||
1353 | return bucket.link(op_state, err); | |
1354 | ||
1355 | } | |
1356 | ||
1357 | int RGWBucketAdminOp::check_index(RGWRados *store, RGWBucketAdminOpState& op_state, | |
1358 | RGWFormatterFlusher& flusher) | |
1359 | { | |
1360 | int ret; | |
7c673cae FG |
1361 | map<RGWObjCategory, RGWStorageStats> existing_stats; |
1362 | map<RGWObjCategory, RGWStorageStats> calculated_stats; | |
d2e6a577 | 1363 | |
7c673cae FG |
1364 | |
1365 | RGWBucket bucket; | |
1366 | ||
1367 | ret = bucket.init(store, op_state); | |
1368 | if (ret < 0) | |
1369 | return ret; | |
1370 | ||
1371 | Formatter *formatter = flusher.get_formatter(); | |
1372 | flusher.start(0); | |
1373 | ||
d2e6a577 | 1374 | ret = bucket.check_bad_index_multipart(op_state, flusher); |
7c673cae FG |
1375 | if (ret < 0) |
1376 | return ret; | |
1377 | ||
7c673cae FG |
1378 | ret = bucket.check_object_index(op_state, flusher); |
1379 | if (ret < 0) | |
1380 | return ret; | |
1381 | ||
1382 | ret = bucket.check_index(op_state, existing_stats, calculated_stats); | |
1383 | if (ret < 0) | |
1384 | return ret; | |
1385 | ||
1386 | dump_index_check(existing_stats, calculated_stats, formatter); | |
1387 | flusher.flush(); | |
1388 | ||
1389 | return 0; | |
1390 | } | |
1391 | ||
1392 | int RGWBucketAdminOp::remove_bucket(RGWRados *store, RGWBucketAdminOpState& op_state, | |
1393 | bool bypass_gc, bool keep_index_consistent) | |
1394 | { | |
1395 | RGWBucket bucket; | |
1396 | ||
1397 | int ret = bucket.init(store, op_state); | |
1398 | if (ret < 0) | |
1399 | return ret; | |
1400 | ||
c07f9fc5 FG |
1401 | std::string err_msg; |
1402 | ret = bucket.remove(op_state, bypass_gc, keep_index_consistent, &err_msg); | |
1403 | if (!err_msg.empty()) { | |
1404 | lderr(store->ctx()) << "ERROR: " << err_msg << dendl; | |
1405 | } | |
1406 | return ret; | |
7c673cae FG |
1407 | } |
1408 | ||
1409 | int RGWBucketAdminOp::remove_object(RGWRados *store, RGWBucketAdminOpState& op_state) | |
1410 | { | |
1411 | RGWBucket bucket; | |
1412 | ||
1413 | int ret = bucket.init(store, op_state); | |
1414 | if (ret < 0) | |
1415 | return ret; | |
1416 | ||
1417 | return bucket.remove_object(op_state); | |
1418 | } | |
1419 | ||
1420 | static int bucket_stats(RGWRados *store, const std::string& tenant_name, std::string& bucket_name, Formatter *formatter) | |
1421 | { | |
1422 | RGWBucketInfo bucket_info; | |
1423 | map<RGWObjCategory, RGWStorageStats> stats; | |
1424 | ||
1425 | real_time mtime; | |
11fdf7f2 | 1426 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
1427 | int r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, &mtime); |
1428 | if (r < 0) | |
1429 | return r; | |
1430 | ||
1431 | rgw_bucket& bucket = bucket_info.bucket; | |
1432 | ||
1433 | string bucket_ver, master_ver; | |
1434 | string max_marker; | |
1435 | int ret = store->get_bucket_stats(bucket_info, RGW_NO_SHARD, &bucket_ver, &master_ver, stats, &max_marker); | |
1436 | if (ret < 0) { | |
1437 | cerr << "error getting bucket stats ret=" << ret << std::endl; | |
1438 | return ret; | |
1439 | } | |
1440 | ||
1441 | utime_t ut(mtime); | |
1442 | ||
1443 | formatter->open_object_section("stats"); | |
1444 | formatter->dump_string("bucket", bucket.name); | |
11fdf7f2 | 1445 | formatter->dump_string("tenant", bucket.tenant); |
31f18b77 | 1446 | formatter->dump_string("zonegroup", bucket_info.zonegroup); |
11fdf7f2 | 1447 | formatter->dump_string("placement_rule", bucket_info.placement_rule.to_str()); |
31f18b77 | 1448 | ::encode_json("explicit_placement", bucket.explicit_placement, formatter); |
7c673cae FG |
1449 | formatter->dump_string("id", bucket.bucket_id); |
1450 | formatter->dump_string("marker", bucket.marker); | |
1451 | formatter->dump_stream("index_type") << bucket_info.index_type; | |
1452 | ::encode_json("owner", bucket_info.owner, formatter); | |
1453 | formatter->dump_string("ver", bucket_ver); | |
1454 | formatter->dump_string("master_ver", master_ver); | |
1455 | formatter->dump_stream("mtime") << ut; | |
1456 | formatter->dump_string("max_marker", max_marker); | |
1457 | dump_bucket_usage(stats, formatter); | |
1458 | encode_json("bucket_quota", bucket_info.quota, formatter); | |
1459 | formatter->close_section(); | |
1460 | ||
1461 | return 0; | |
1462 | } | |
1463 | ||
1464 | int RGWBucketAdminOp::limit_check(RGWRados *store, | |
1465 | RGWBucketAdminOpState& op_state, | |
1466 | const std::list<std::string>& user_ids, | |
1467 | RGWFormatterFlusher& flusher, | |
1468 | bool warnings_only) | |
1469 | { | |
1470 | int ret = 0; | |
1471 | const size_t max_entries = | |
1472 | store->ctx()->_conf->rgw_list_buckets_max_chunk; | |
1473 | ||
1474 | const size_t safe_max_objs_per_shard = | |
1475 | store->ctx()->_conf->rgw_safe_max_objects_per_shard; | |
1476 | ||
1477 | uint16_t shard_warn_pct = | |
1478 | store->ctx()->_conf->rgw_shard_warning_threshold; | |
1479 | if (shard_warn_pct > 100) | |
1480 | shard_warn_pct = 90; | |
1481 | ||
1482 | Formatter *formatter = flusher.get_formatter(); | |
1483 | flusher.start(0); | |
1484 | ||
1485 | formatter->open_array_section("users"); | |
1486 | ||
1487 | for (const auto& user_id : user_ids) { | |
a8e16298 | 1488 | |
7c673cae FG |
1489 | formatter->open_object_section("user"); |
1490 | formatter->dump_string("user_id", user_id); | |
7c673cae | 1491 | formatter->open_array_section("buckets"); |
a8e16298 TL |
1492 | |
1493 | string marker; | |
1494 | bool is_truncated{false}; | |
7c673cae FG |
1495 | do { |
1496 | RGWUserBuckets buckets; | |
7c673cae FG |
1497 | |
1498 | ret = rgw_read_user_buckets(store, user_id, buckets, | |
1499 | marker, string(), max_entries, false, | |
1500 | &is_truncated); | |
1501 | if (ret < 0) | |
1502 | return ret; | |
1503 | ||
1504 | map<string, RGWBucketEnt>& m_buckets = buckets.get_buckets(); | |
1505 | ||
1506 | for (const auto& iter : m_buckets) { | |
1507 | auto& bucket = iter.second.bucket; | |
1508 | uint32_t num_shards = 1; | |
1509 | uint64_t num_objects = 0; | |
1510 | ||
1511 | /* need info for num_shards */ | |
1512 | RGWBucketInfo info; | |
11fdf7f2 | 1513 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
1514 | |
1515 | marker = bucket.name; /* Casey's location for marker update, | |
1516 | * as we may now not reach the end of | |
1517 | * the loop body */ | |
1518 | ||
1519 | ret = store->get_bucket_info(obj_ctx, bucket.tenant, bucket.name, | |
1520 | info, nullptr); | |
1521 | if (ret < 0) | |
1522 | continue; | |
1523 | ||
1524 | /* need stats for num_entries */ | |
1525 | string bucket_ver, master_ver; | |
1526 | std::map<RGWObjCategory, RGWStorageStats> stats; | |
1527 | ret = store->get_bucket_stats(info, RGW_NO_SHARD, &bucket_ver, | |
1528 | &master_ver, stats, nullptr); | |
1529 | ||
1530 | if (ret < 0) | |
1531 | continue; | |
1532 | ||
1533 | for (const auto& s : stats) { | |
1534 | num_objects += s.second.num_objects; | |
1535 | } | |
1536 | ||
1537 | num_shards = info.num_shards; | |
31f18b77 FG |
1538 | uint64_t objs_per_shard = |
1539 | (num_shards) ? num_objects/num_shards : num_objects; | |
7c673cae FG |
1540 | { |
1541 | bool warn = false; | |
1542 | stringstream ss; | |
1543 | if (objs_per_shard > safe_max_objs_per_shard) { | |
1544 | double over = | |
1545 | 100 - (safe_max_objs_per_shard/objs_per_shard * 100); | |
1546 | ss << boost::format("OVER %4f%%") % over; | |
1547 | warn = true; | |
1548 | } else { | |
1549 | double fill_pct = | |
1550 | objs_per_shard / safe_max_objs_per_shard * 100; | |
1551 | if (fill_pct >= shard_warn_pct) { | |
1552 | ss << boost::format("WARN %4f%%") % fill_pct; | |
1553 | warn = true; | |
1554 | } else { | |
1555 | ss << "OK"; | |
1556 | } | |
1557 | } | |
1558 | ||
1559 | if (warn || (! warnings_only)) { | |
1560 | formatter->open_object_section("bucket"); | |
1561 | formatter->dump_string("bucket", bucket.name); | |
1562 | formatter->dump_string("tenant", bucket.tenant); | |
1563 | formatter->dump_int("num_objects", num_objects); | |
1564 | formatter->dump_int("num_shards", num_shards); | |
1565 | formatter->dump_int("objects_per_shard", objs_per_shard); | |
1566 | formatter->dump_string("fill_status", ss.str()); | |
1567 | formatter->close_section(); | |
1568 | } | |
1569 | } | |
1570 | } | |
a8e16298 TL |
1571 | formatter->flush(cout); |
1572 | } while (is_truncated); /* foreach: bucket */ | |
7c673cae FG |
1573 | |
1574 | formatter->close_section(); | |
1575 | formatter->close_section(); | |
1576 | formatter->flush(cout); | |
1577 | ||
1578 | } /* foreach: user_id */ | |
1579 | ||
1580 | formatter->close_section(); | |
1581 | formatter->flush(cout); | |
1582 | ||
1583 | return ret; | |
1584 | } /* RGWBucketAdminOp::limit_check */ | |
1585 | ||
1586 | int RGWBucketAdminOp::info(RGWRados *store, RGWBucketAdminOpState& op_state, | |
1587 | RGWFormatterFlusher& flusher) | |
1588 | { | |
11fdf7f2 | 1589 | int ret = 0; |
7c673cae | 1590 | string bucket_name = op_state.get_bucket_name(); |
7c673cae FG |
1591 | Formatter *formatter = flusher.get_formatter(); |
1592 | flusher.start(0); | |
1593 | ||
1594 | CephContext *cct = store->ctx(); | |
1595 | ||
1596 | const size_t max_entries = cct->_conf->rgw_list_buckets_max_chunk; | |
1597 | ||
1598 | bool show_stats = op_state.will_fetch_stats(); | |
1599 | rgw_user user_id = op_state.get_user_id(); | |
1600 | if (op_state.is_user_op()) { | |
1601 | formatter->open_array_section("buckets"); | |
1602 | ||
1603 | RGWUserBuckets buckets; | |
1604 | string marker; | |
1605 | bool is_truncated = false; | |
1606 | ||
1607 | do { | |
1608 | ret = rgw_read_user_buckets(store, op_state.get_user_id(), buckets, | |
1609 | marker, string(), max_entries, false, | |
1610 | &is_truncated); | |
1611 | if (ret < 0) | |
1612 | return ret; | |
1613 | ||
1614 | map<string, RGWBucketEnt>& m = buckets.get_buckets(); | |
1615 | map<string, RGWBucketEnt>::iterator iter; | |
1616 | ||
1617 | for (iter = m.begin(); iter != m.end(); ++iter) { | |
11fdf7f2 TL |
1618 | std::string obj_name = iter->first; |
1619 | if (!bucket_name.empty() && bucket_name != obj_name) { | |
1620 | continue; | |
1621 | } | |
1622 | ||
7c673cae FG |
1623 | if (show_stats) |
1624 | bucket_stats(store, user_id.tenant, obj_name, formatter); | |
1625 | else | |
1626 | formatter->dump_string("bucket", obj_name); | |
1627 | ||
1628 | marker = obj_name; | |
1629 | } | |
1630 | ||
1631 | flusher.flush(); | |
1632 | } while (is_truncated); | |
1633 | ||
1634 | formatter->close_section(); | |
1635 | } else if (!bucket_name.empty()) { | |
11fdf7f2 TL |
1636 | ret = bucket_stats(store, user_id.tenant, bucket_name, formatter); |
1637 | if (ret < 0) { | |
1638 | return ret; | |
1639 | } | |
7c673cae | 1640 | } else { |
11fdf7f2 TL |
1641 | void *handle = nullptr; |
1642 | bool truncated = true; | |
7c673cae FG |
1643 | |
1644 | formatter->open_array_section("buckets"); | |
11fdf7f2 TL |
1645 | ret = store->meta_mgr->list_keys_init("bucket", &handle); |
1646 | while (ret == 0 && truncated) { | |
1647 | std::list<std::string> buckets; | |
1648 | const int max_keys = 1000; | |
1649 | ret = store->meta_mgr->list_keys_next(handle, max_keys, buckets, | |
1650 | &truncated); | |
1651 | for (auto& bucket_name : buckets) { | |
7c673cae | 1652 | if (show_stats) |
11fdf7f2 | 1653 | bucket_stats(store, user_id.tenant, bucket_name, formatter); |
7c673cae | 1654 | else |
11fdf7f2 | 1655 | formatter->dump_string("bucket", bucket_name); |
7c673cae FG |
1656 | } |
1657 | } | |
1658 | ||
1659 | formatter->close_section(); | |
1660 | } | |
1661 | ||
1662 | flusher.flush(); | |
1663 | ||
1664 | return 0; | |
1665 | } | |
1666 | ||
94b18763 FG |
1667 | int RGWBucketAdminOp::set_quota(RGWRados *store, RGWBucketAdminOpState& op_state) |
1668 | { | |
1669 | RGWBucket bucket; | |
1670 | ||
1671 | int ret = bucket.init(store, op_state); | |
1672 | if (ret < 0) | |
1673 | return ret; | |
1674 | return bucket.set_quota(op_state); | |
1675 | } | |
7c673cae | 1676 | |
f64942e4 AA |
1677 | static int purge_bucket_instance(RGWRados *store, const RGWBucketInfo& bucket_info) |
1678 | { | |
1679 | int max_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1); | |
1680 | for (int i = 0; i < max_shards; i++) { | |
1681 | RGWRados::BucketShard bs(store); | |
1682 | int shard_id = (bucket_info.num_shards > 0 ? i : -1); | |
1683 | int ret = bs.init(bucket_info.bucket, shard_id, nullptr); | |
1684 | if (ret < 0) { | |
1685 | cerr << "ERROR: bs.init(bucket=" << bucket_info.bucket << ", shard=" << shard_id | |
1686 | << "): " << cpp_strerror(-ret) << std::endl; | |
1687 | return ret; | |
1688 | } | |
1689 | ret = store->bi_remove(bs); | |
1690 | if (ret < 0) { | |
1691 | cerr << "ERROR: failed to remove bucket index object: " | |
1692 | << cpp_strerror(-ret) << std::endl; | |
1693 | return ret; | |
1694 | } | |
1695 | } | |
1696 | return 0; | |
1697 | } | |
1698 | ||
11fdf7f2 | 1699 | inline auto split_tenant(const std::string& bucket_name){ |
f64942e4 AA |
1700 | auto p = bucket_name.find('/'); |
1701 | if(p != std::string::npos) { | |
1702 | return std::make_pair(bucket_name.substr(0,p), bucket_name.substr(p+1)); | |
1703 | } | |
1704 | return std::make_pair(std::string(), bucket_name); | |
1705 | } | |
1706 | ||
1707 | using bucket_instance_ls = std::vector<RGWBucketInfo>; | |
1708 | void get_stale_instances(RGWRados *store, const std::string& bucket_name, | |
1709 | const vector<std::string>& lst, | |
1710 | bucket_instance_ls& stale_instances) | |
1711 | { | |
1712 | ||
11fdf7f2 | 1713 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
f64942e4 AA |
1714 | |
1715 | bucket_instance_ls other_instances; | |
1716 | // first iterate over the entries, and pick up the done buckets; these | |
1717 | // are guaranteed to be stale | |
1718 | for (const auto& bucket_instance : lst){ | |
1719 | RGWBucketInfo binfo; | |
1720 | int r = store->get_bucket_instance_info(obj_ctx, bucket_instance, | |
1721 | binfo, nullptr,nullptr); | |
1722 | if (r < 0){ | |
1723 | // this can only happen if someone deletes us right when we're processing | |
1724 | lderr(store->ctx()) << "Bucket instance is invalid: " << bucket_instance | |
1725 | << cpp_strerror(-r) << dendl; | |
1726 | continue; | |
1727 | } | |
1728 | if (binfo.reshard_status == CLS_RGW_RESHARD_DONE) | |
1729 | stale_instances.emplace_back(std::move(binfo)); | |
1730 | else { | |
1731 | other_instances.emplace_back(std::move(binfo)); | |
1732 | } | |
1733 | } | |
1734 | ||
1735 | // Read the cur bucket info, if the bucket doesn't exist we can simply return | |
1736 | // all the instances | |
11fdf7f2 | 1737 | auto [tenant, bucket] = split_tenant(bucket_name); |
f64942e4 AA |
1738 | RGWBucketInfo cur_bucket_info; |
1739 | int r = store->get_bucket_info(obj_ctx, tenant, bucket, cur_bucket_info, nullptr); | |
1740 | if (r < 0) { | |
1741 | if (r == -ENOENT) { | |
1742 | // bucket doesn't exist, everything is stale then | |
1743 | stale_instances.insert(std::end(stale_instances), | |
1744 | std::make_move_iterator(other_instances.begin()), | |
1745 | std::make_move_iterator(other_instances.end())); | |
1746 | } else { | |
1747 | // all bets are off if we can't read the bucket, just return the sureshot stale instances | |
1748 | lderr(store->ctx()) << "error: reading bucket info for bucket: " | |
1749 | << bucket << cpp_strerror(-r) << dendl; | |
1750 | } | |
1751 | return; | |
1752 | } | |
1753 | ||
1754 | // Don't process further in this round if bucket is resharding | |
1755 | if (cur_bucket_info.reshard_status == CLS_RGW_RESHARD_IN_PROGRESS) | |
1756 | return; | |
1757 | ||
1758 | other_instances.erase(std::remove_if(other_instances.begin(), other_instances.end(), | |
1759 | [&cur_bucket_info](const RGWBucketInfo& b){ | |
1760 | return (b.bucket.bucket_id == cur_bucket_info.bucket.bucket_id || | |
1761 | b.bucket.bucket_id == cur_bucket_info.new_bucket_instance_id); | |
1762 | }), | |
1763 | other_instances.end()); | |
1764 | ||
1765 | // check if there are still instances left | |
1766 | if (other_instances.empty()) { | |
1767 | return; | |
1768 | } | |
1769 | ||
1770 | // Now we have a bucket with instances where the reshard status is none, this | |
1771 | // usually happens when the reshard process couldn't complete, lockdown the | |
1772 | // bucket and walk through these instances to make sure no one else interferes | |
1773 | // with these | |
1774 | { | |
1775 | RGWBucketReshardLock reshard_lock(store, cur_bucket_info, true); | |
1776 | r = reshard_lock.lock(); | |
1777 | if (r < 0) { | |
1778 | // most likely bucket is under reshard, return the sureshot stale instances | |
1779 | ldout(store->ctx(), 5) << __func__ | |
1780 | << "failed to take reshard lock; reshard underway likey" << dendl; | |
1781 | return; | |
1782 | } | |
1783 | auto sg = make_scope_guard([&reshard_lock](){ reshard_lock.unlock();} ); | |
1784 | // this should be fast enough that we may not need to renew locks and check | |
1785 | // exit status?, should we read the values of the instances again? | |
1786 | stale_instances.insert(std::end(stale_instances), | |
1787 | std::make_move_iterator(other_instances.begin()), | |
1788 | std::make_move_iterator(other_instances.end())); | |
1789 | } | |
1790 | ||
1791 | return; | |
1792 | } | |
1793 | ||
1794 | static int process_stale_instances(RGWRados *store, RGWBucketAdminOpState& op_state, | |
1795 | RGWFormatterFlusher& flusher, | |
1796 | std::function<void(const bucket_instance_ls&, | |
1797 | Formatter *, | |
1798 | RGWRados*)> process_f) | |
1799 | { | |
1800 | std::string marker; | |
1801 | void *handle; | |
1802 | Formatter *formatter = flusher.get_formatter(); | |
1803 | static constexpr auto default_max_keys = 1000; | |
1804 | ||
1805 | int ret = store->meta_mgr->list_keys_init("bucket.instance", marker, &handle); | |
1806 | if (ret < 0) { | |
1807 | cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl; | |
1808 | return ret; | |
1809 | } | |
1810 | ||
1811 | bool truncated; | |
1812 | ||
1813 | formatter->open_array_section("keys"); | |
1814 | ||
1815 | do { | |
1816 | list<std::string> keys; | |
1817 | ||
1818 | ret = store->meta_mgr->list_keys_next(handle, default_max_keys, keys, &truncated); | |
1819 | if (ret < 0 && ret != -ENOENT) { | |
1820 | cerr << "ERROR: lists_keys_next(): " << cpp_strerror(-ret) << std::endl; | |
1821 | return ret; | |
1822 | } if (ret != -ENOENT) { | |
1823 | // partition the list of buckets by buckets as the listing is un sorted, | |
1824 | // since it would minimize the reads to bucket_info | |
1825 | std::unordered_map<std::string, std::vector<std::string>> bucket_instance_map; | |
1826 | for (auto &key: keys) { | |
1827 | auto pos = key.find(':'); | |
1828 | if(pos != std::string::npos) | |
1829 | bucket_instance_map[key.substr(0,pos)].emplace_back(std::move(key)); | |
1830 | } | |
1831 | for (const auto& kv: bucket_instance_map) { | |
1832 | bucket_instance_ls stale_lst; | |
1833 | get_stale_instances(store, kv.first, kv.second, stale_lst); | |
1834 | process_f(stale_lst, formatter, store); | |
1835 | } | |
1836 | } | |
1837 | } while (truncated); | |
1838 | ||
1839 | formatter->close_section(); // keys | |
1840 | formatter->flush(cout); | |
1841 | return 0; | |
1842 | } | |
1843 | ||
1844 | int RGWBucketAdminOp::list_stale_instances(RGWRados *store, | |
1845 | RGWBucketAdminOpState& op_state, | |
1846 | RGWFormatterFlusher& flusher) | |
1847 | { | |
1848 | auto process_f = [](const bucket_instance_ls& lst, | |
1849 | Formatter *formatter, | |
1850 | RGWRados*){ | |
1851 | for (const auto& binfo: lst) | |
1852 | formatter->dump_string("key", binfo.bucket.get_key()); | |
1853 | }; | |
1854 | return process_stale_instances(store, op_state, flusher, process_f); | |
1855 | } | |
1856 | ||
1857 | ||
1858 | int RGWBucketAdminOp::clear_stale_instances(RGWRados *store, | |
1859 | RGWBucketAdminOpState& op_state, | |
1860 | RGWFormatterFlusher& flusher) | |
1861 | { | |
1862 | auto process_f = [](const bucket_instance_ls& lst, | |
1863 | Formatter *formatter, | |
1864 | RGWRados *store){ | |
1865 | for (const auto &binfo: lst) { | |
1866 | int ret = purge_bucket_instance(store, binfo); | |
1867 | if (ret == 0){ | |
1868 | auto md_key = "bucket.instance:" + binfo.bucket.get_key(); | |
1869 | ret = store->meta_mgr->remove(md_key); | |
1870 | } | |
1871 | formatter->open_object_section("delete_status"); | |
1872 | formatter->dump_string("bucket_instance", binfo.bucket.get_key()); | |
1873 | formatter->dump_int("status", -ret); | |
1874 | formatter->close_section(); | |
1875 | } | |
1876 | }; | |
1877 | ||
1878 | return process_stale_instances(store, op_state, flusher, process_f); | |
1879 | } | |
1880 | ||
11fdf7f2 TL |
1881 | static int fix_single_bucket_lc(RGWRados *store, |
1882 | const std::string& tenant_name, | |
1883 | const std::string& bucket_name) | |
1884 | { | |
1885 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); | |
1886 | RGWBucketInfo bucket_info; | |
1887 | map <std::string, bufferlist> bucket_attrs; | |
1888 | int ret = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, | |
1889 | bucket_info, nullptr, &bucket_attrs); | |
1890 | if (ret < 0) { | |
1891 | // TODO: Should we handle the case where the bucket could've been removed between | |
1892 | // listing and fetching? | |
1893 | return ret; | |
1894 | } | |
1895 | ||
1896 | return rgw::lc::fix_lc_shard_entry(store, bucket_info, bucket_attrs); | |
1897 | } | |
1898 | ||
1899 | static void format_lc_status(Formatter* formatter, | |
1900 | const std::string& tenant_name, | |
1901 | const std::string& bucket_name, | |
1902 | int status) | |
1903 | { | |
1904 | formatter->open_object_section("bucket_entry"); | |
1905 | std::string entry = tenant_name.empty() ? bucket_name : tenant_name + "/" + bucket_name; | |
1906 | formatter->dump_string("bucket", entry); | |
1907 | formatter->dump_int("status", status); | |
1908 | formatter->close_section(); // bucket_entry | |
1909 | } | |
1910 | ||
1911 | static void process_single_lc_entry(RGWRados *store, Formatter *formatter, | |
1912 | const std::string& tenant_name, | |
1913 | const std::string& bucket_name) | |
1914 | { | |
1915 | int ret = fix_single_bucket_lc(store, tenant_name, bucket_name); | |
1916 | format_lc_status(formatter, tenant_name, bucket_name, -ret); | |
1917 | } | |
1918 | ||
1919 | int RGWBucketAdminOp::fix_lc_shards(RGWRados *store, | |
1920 | RGWBucketAdminOpState& op_state, | |
1921 | RGWFormatterFlusher& flusher) | |
1922 | { | |
1923 | std::string marker; | |
1924 | void *handle; | |
1925 | Formatter *formatter = flusher.get_formatter(); | |
1926 | static constexpr auto default_max_keys = 1000; | |
1927 | ||
1928 | bool truncated; | |
1929 | if (const std::string& bucket_name = op_state.get_bucket_name(); | |
1930 | ! bucket_name.empty()) { | |
1931 | const rgw_user user_id = op_state.get_user_id(); | |
1932 | process_single_lc_entry(store, formatter, user_id.tenant, bucket_name); | |
1933 | formatter->flush(cout); | |
1934 | } else { | |
1935 | int ret = store->meta_mgr->list_keys_init("bucket", marker, &handle); | |
1936 | if (ret < 0) { | |
1937 | std::cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl; | |
1938 | return ret; | |
1939 | } | |
1940 | ||
1941 | { | |
1942 | formatter->open_array_section("lc_fix_status"); | |
1943 | auto sg = make_scope_guard([&store, &handle, &formatter](){ | |
1944 | store->meta_mgr->list_keys_complete(handle); | |
1945 | formatter->close_section(); // lc_fix_status | |
1946 | formatter->flush(cout); | |
1947 | }); | |
1948 | do { | |
1949 | list<std::string> keys; | |
1950 | ret = store->meta_mgr->list_keys_next(handle, default_max_keys, keys, &truncated); | |
1951 | if (ret < 0 && ret != -ENOENT) { | |
1952 | std::cerr << "ERROR: lists_keys_next(): " << cpp_strerror(-ret) << std::endl; | |
1953 | return ret; | |
1954 | } if (ret != -ENOENT) { | |
1955 | for (const auto &key:keys) { | |
1956 | auto [tenant_name, bucket_name] = split_tenant(key); | |
1957 | process_single_lc_entry(store, formatter, tenant_name, bucket_name); | |
1958 | } | |
1959 | } | |
1960 | formatter->flush(cout); // regularly flush every 1k entries | |
1961 | } while (truncated); | |
1962 | } | |
1963 | ||
1964 | } | |
1965 | return 0; | |
1966 | ||
1967 | } | |
1968 | ||
7c673cae FG |
1969 | void rgw_data_change::dump(Formatter *f) const |
1970 | { | |
1971 | string type; | |
1972 | switch (entity_type) { | |
1973 | case ENTITY_TYPE_BUCKET: | |
1974 | type = "bucket"; | |
1975 | break; | |
1976 | default: | |
1977 | type = "unknown"; | |
1978 | } | |
1979 | encode_json("entity_type", type, f); | |
1980 | encode_json("key", key, f); | |
1981 | utime_t ut(timestamp); | |
1982 | encode_json("timestamp", ut, f); | |
1983 | } | |
1984 | ||
1985 | void rgw_data_change::decode_json(JSONObj *obj) { | |
1986 | string s; | |
1987 | JSONDecoder::decode_json("entity_type", s, obj); | |
1988 | if (s == "bucket") { | |
1989 | entity_type = ENTITY_TYPE_BUCKET; | |
1990 | } else { | |
1991 | entity_type = ENTITY_TYPE_UNKNOWN; | |
1992 | } | |
1993 | JSONDecoder::decode_json("key", key, obj); | |
1994 | utime_t ut; | |
1995 | JSONDecoder::decode_json("timestamp", ut, obj); | |
1996 | timestamp = ut.to_real_time(); | |
1997 | } | |
1998 | ||
1999 | void rgw_data_change_log_entry::dump(Formatter *f) const | |
2000 | { | |
2001 | encode_json("log_id", log_id, f); | |
2002 | utime_t ut(log_timestamp); | |
2003 | encode_json("log_timestamp", ut, f); | |
2004 | encode_json("entry", entry, f); | |
2005 | } | |
2006 | ||
2007 | void rgw_data_change_log_entry::decode_json(JSONObj *obj) { | |
2008 | JSONDecoder::decode_json("log_id", log_id, obj); | |
2009 | utime_t ut; | |
2010 | JSONDecoder::decode_json("log_timestamp", ut, obj); | |
2011 | log_timestamp = ut.to_real_time(); | |
2012 | JSONDecoder::decode_json("entry", entry, obj); | |
2013 | } | |
2014 | ||
2015 | int RGWDataChangesLog::choose_oid(const rgw_bucket_shard& bs) { | |
2016 | const string& name = bs.bucket.name; | |
2017 | int shard_shift = (bs.shard_id > 0 ? bs.shard_id : 0); | |
2018 | uint32_t r = (ceph_str_hash_linux(name.c_str(), name.size()) + shard_shift) % num_shards; | |
2019 | ||
2020 | return (int)r; | |
2021 | } | |
2022 | ||
2023 | int RGWDataChangesLog::renew_entries() | |
2024 | { | |
11fdf7f2 | 2025 | if (!store->svc.zone->need_to_log_data()) |
7c673cae FG |
2026 | return 0; |
2027 | ||
2028 | /* we can't keep the bucket name as part of the cls_log_entry, and we need | |
2029 | * it later, so we keep two lists under the map */ | |
2030 | map<int, pair<list<rgw_bucket_shard>, list<cls_log_entry> > > m; | |
2031 | ||
2032 | lock.Lock(); | |
2033 | map<rgw_bucket_shard, bool> entries; | |
2034 | entries.swap(cur_cycle); | |
2035 | lock.Unlock(); | |
2036 | ||
2037 | map<rgw_bucket_shard, bool>::iterator iter; | |
2038 | string section; | |
2039 | real_time ut = real_clock::now(); | |
2040 | for (iter = entries.begin(); iter != entries.end(); ++iter) { | |
2041 | const rgw_bucket_shard& bs = iter->first; | |
2042 | ||
2043 | int index = choose_oid(bs); | |
2044 | ||
2045 | cls_log_entry entry; | |
2046 | ||
2047 | rgw_data_change change; | |
2048 | bufferlist bl; | |
2049 | change.entity_type = ENTITY_TYPE_BUCKET; | |
2050 | change.key = bs.get_key(); | |
2051 | change.timestamp = ut; | |
11fdf7f2 | 2052 | encode(change, bl); |
7c673cae FG |
2053 | |
2054 | store->time_log_prepare_entry(entry, ut, section, change.key, bl); | |
2055 | ||
2056 | m[index].first.push_back(bs); | |
2057 | m[index].second.emplace_back(std::move(entry)); | |
2058 | } | |
2059 | ||
2060 | map<int, pair<list<rgw_bucket_shard>, list<cls_log_entry> > >::iterator miter; | |
2061 | for (miter = m.begin(); miter != m.end(); ++miter) { | |
2062 | list<cls_log_entry>& entries = miter->second.second; | |
2063 | ||
2064 | real_time now = real_clock::now(); | |
2065 | ||
2066 | int ret = store->time_log_add(oids[miter->first], entries, NULL); | |
2067 | if (ret < 0) { | |
2068 | /* we don't really need to have a special handling for failed cases here, | |
2069 | * as this is just an optimization. */ | |
2070 | lderr(cct) << "ERROR: store->time_log_add() returned " << ret << dendl; | |
2071 | return ret; | |
2072 | } | |
2073 | ||
2074 | real_time expiration = now; | |
2075 | expiration += make_timespan(cct->_conf->rgw_data_log_window); | |
2076 | ||
2077 | list<rgw_bucket_shard>& buckets = miter->second.first; | |
2078 | list<rgw_bucket_shard>::iterator liter; | |
2079 | for (liter = buckets.begin(); liter != buckets.end(); ++liter) { | |
2080 | update_renewed(*liter, expiration); | |
2081 | } | |
2082 | } | |
2083 | ||
2084 | return 0; | |
2085 | } | |
2086 | ||
2087 | void RGWDataChangesLog::_get_change(const rgw_bucket_shard& bs, ChangeStatusPtr& status) | |
2088 | { | |
11fdf7f2 | 2089 | ceph_assert(lock.is_locked()); |
7c673cae FG |
2090 | if (!changes.find(bs, status)) { |
2091 | status = ChangeStatusPtr(new ChangeStatus); | |
2092 | changes.add(bs, status); | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | void RGWDataChangesLog::register_renew(rgw_bucket_shard& bs) | |
2097 | { | |
2098 | Mutex::Locker l(lock); | |
2099 | cur_cycle[bs] = true; | |
2100 | } | |
2101 | ||
2102 | void RGWDataChangesLog::update_renewed(rgw_bucket_shard& bs, real_time& expiration) | |
2103 | { | |
2104 | Mutex::Locker l(lock); | |
2105 | ChangeStatusPtr status; | |
2106 | _get_change(bs, status); | |
2107 | ||
2108 | ldout(cct, 20) << "RGWDataChangesLog::update_renewd() bucket_name=" << bs.bucket.name << " shard_id=" << bs.shard_id << " expiration=" << expiration << dendl; | |
2109 | status->cur_expiration = expiration; | |
2110 | } | |
2111 | ||
2112 | int RGWDataChangesLog::get_log_shard_id(rgw_bucket& bucket, int shard_id) { | |
2113 | rgw_bucket_shard bs(bucket, shard_id); | |
2114 | ||
2115 | return choose_oid(bs); | |
2116 | } | |
2117 | ||
2118 | int RGWDataChangesLog::add_entry(rgw_bucket& bucket, int shard_id) { | |
11fdf7f2 | 2119 | if (!store->svc.zone->need_to_log_data()) |
7c673cae FG |
2120 | return 0; |
2121 | ||
91327a77 AA |
2122 | if (observer) { |
2123 | observer->on_bucket_changed(bucket.get_key()); | |
2124 | } | |
2125 | ||
7c673cae FG |
2126 | rgw_bucket_shard bs(bucket, shard_id); |
2127 | ||
2128 | int index = choose_oid(bs); | |
2129 | mark_modified(index, bs); | |
2130 | ||
2131 | lock.Lock(); | |
2132 | ||
2133 | ChangeStatusPtr status; | |
2134 | _get_change(bs, status); | |
2135 | ||
2136 | lock.Unlock(); | |
2137 | ||
2138 | real_time now = real_clock::now(); | |
2139 | ||
2140 | status->lock->Lock(); | |
2141 | ||
2142 | ldout(cct, 20) << "RGWDataChangesLog::add_entry() bucket.name=" << bucket.name << " shard_id=" << shard_id << " now=" << now << " cur_expiration=" << status->cur_expiration << dendl; | |
2143 | ||
2144 | if (now < status->cur_expiration) { | |
2145 | /* no need to send, recently completed */ | |
2146 | status->lock->Unlock(); | |
2147 | ||
2148 | register_renew(bs); | |
2149 | return 0; | |
2150 | } | |
2151 | ||
2152 | RefCountedCond *cond; | |
2153 | ||
2154 | if (status->pending) { | |
2155 | cond = status->cond; | |
2156 | ||
11fdf7f2 | 2157 | ceph_assert(cond); |
7c673cae FG |
2158 | |
2159 | status->cond->get(); | |
2160 | status->lock->Unlock(); | |
2161 | ||
2162 | int ret = cond->wait(); | |
2163 | cond->put(); | |
2164 | if (!ret) { | |
2165 | register_renew(bs); | |
2166 | } | |
2167 | return ret; | |
2168 | } | |
2169 | ||
2170 | status->cond = new RefCountedCond; | |
2171 | status->pending = true; | |
2172 | ||
2173 | string& oid = oids[index]; | |
2174 | real_time expiration; | |
2175 | ||
2176 | int ret; | |
2177 | ||
2178 | do { | |
2179 | status->cur_sent = now; | |
2180 | ||
2181 | expiration = now; | |
2182 | expiration += ceph::make_timespan(cct->_conf->rgw_data_log_window); | |
2183 | ||
2184 | status->lock->Unlock(); | |
2185 | ||
2186 | bufferlist bl; | |
2187 | rgw_data_change change; | |
2188 | change.entity_type = ENTITY_TYPE_BUCKET; | |
2189 | change.key = bs.get_key(); | |
2190 | change.timestamp = now; | |
11fdf7f2 | 2191 | encode(change, bl); |
7c673cae FG |
2192 | string section; |
2193 | ||
2194 | ldout(cct, 20) << "RGWDataChangesLog::add_entry() sending update with now=" << now << " cur_expiration=" << expiration << dendl; | |
2195 | ||
2196 | ret = store->time_log_add(oid, now, section, change.key, bl); | |
2197 | ||
2198 | now = real_clock::now(); | |
2199 | ||
2200 | status->lock->Lock(); | |
2201 | ||
2202 | } while (!ret && real_clock::now() > expiration); | |
2203 | ||
2204 | cond = status->cond; | |
2205 | ||
2206 | status->pending = false; | |
2207 | status->cur_expiration = status->cur_sent; /* time of when operation started, not completed */ | |
2208 | status->cur_expiration += make_timespan(cct->_conf->rgw_data_log_window); | |
2209 | status->cond = NULL; | |
2210 | status->lock->Unlock(); | |
2211 | ||
2212 | cond->done(ret); | |
2213 | cond->put(); | |
2214 | ||
2215 | return ret; | |
2216 | } | |
2217 | ||
2218 | int RGWDataChangesLog::list_entries(int shard, const real_time& start_time, const real_time& end_time, int max_entries, | |
2219 | list<rgw_data_change_log_entry>& entries, | |
2220 | const string& marker, | |
2221 | string *out_marker, | |
2222 | bool *truncated) { | |
31f18b77 FG |
2223 | if (shard >= num_shards) |
2224 | return -EINVAL; | |
7c673cae FG |
2225 | |
2226 | list<cls_log_entry> log_entries; | |
2227 | ||
2228 | int ret = store->time_log_list(oids[shard], start_time, end_time, | |
2229 | max_entries, log_entries, marker, | |
2230 | out_marker, truncated); | |
2231 | if (ret < 0) | |
2232 | return ret; | |
2233 | ||
2234 | list<cls_log_entry>::iterator iter; | |
2235 | for (iter = log_entries.begin(); iter != log_entries.end(); ++iter) { | |
2236 | rgw_data_change_log_entry log_entry; | |
2237 | log_entry.log_id = iter->id; | |
2238 | real_time rt = iter->timestamp.to_real_time(); | |
2239 | log_entry.log_timestamp = rt; | |
11fdf7f2 | 2240 | auto liter = iter->data.cbegin(); |
7c673cae | 2241 | try { |
11fdf7f2 | 2242 | decode(log_entry.entry, liter); |
7c673cae FG |
2243 | } catch (buffer::error& err) { |
2244 | lderr(cct) << "ERROR: failed to decode data changes log entry" << dendl; | |
2245 | return -EIO; | |
2246 | } | |
2247 | entries.push_back(log_entry); | |
2248 | } | |
2249 | ||
2250 | return 0; | |
2251 | } | |
2252 | ||
2253 | int RGWDataChangesLog::list_entries(const real_time& start_time, const real_time& end_time, int max_entries, | |
2254 | list<rgw_data_change_log_entry>& entries, LogMarker& marker, bool *ptruncated) { | |
2255 | bool truncated; | |
2256 | entries.clear(); | |
2257 | ||
2258 | for (; marker.shard < num_shards && (int)entries.size() < max_entries; | |
2259 | marker.shard++, marker.marker.clear()) { | |
2260 | int ret = list_entries(marker.shard, start_time, end_time, max_entries - entries.size(), entries, | |
2261 | marker.marker, NULL, &truncated); | |
2262 | if (ret == -ENOENT) { | |
2263 | continue; | |
2264 | } | |
2265 | if (ret < 0) { | |
2266 | return ret; | |
2267 | } | |
2268 | if (truncated) { | |
2269 | *ptruncated = true; | |
2270 | return 0; | |
2271 | } | |
2272 | } | |
2273 | ||
2274 | *ptruncated = (marker.shard < num_shards); | |
2275 | ||
2276 | return 0; | |
2277 | } | |
2278 | ||
2279 | int RGWDataChangesLog::get_info(int shard_id, RGWDataChangesLogInfo *info) | |
2280 | { | |
2281 | if (shard_id >= num_shards) | |
2282 | return -EINVAL; | |
2283 | ||
2284 | string oid = oids[shard_id]; | |
2285 | ||
2286 | cls_log_header header; | |
2287 | ||
2288 | int ret = store->time_log_info(oid, &header); | |
2289 | if ((ret < 0) && (ret != -ENOENT)) | |
2290 | return ret; | |
2291 | ||
2292 | info->marker = header.max_marker; | |
2293 | info->last_update = header.max_time.to_real_time(); | |
2294 | ||
2295 | return 0; | |
2296 | } | |
2297 | ||
2298 | int RGWDataChangesLog::trim_entries(int shard_id, const real_time& start_time, const real_time& end_time, | |
2299 | const string& start_marker, const string& end_marker) | |
2300 | { | |
2301 | int ret; | |
2302 | ||
2303 | if (shard_id > num_shards) | |
2304 | return -EINVAL; | |
2305 | ||
2306 | ret = store->time_log_trim(oids[shard_id], start_time, end_time, start_marker, end_marker); | |
2307 | ||
31f18b77 | 2308 | if (ret == -ENOENT || ret == -ENODATA) |
7c673cae FG |
2309 | ret = 0; |
2310 | ||
2311 | return ret; | |
2312 | } | |
2313 | ||
2314 | int RGWDataChangesLog::trim_entries(const real_time& start_time, const real_time& end_time, | |
2315 | const string& start_marker, const string& end_marker) | |
2316 | { | |
2317 | for (int shard = 0; shard < num_shards; shard++) { | |
2318 | int ret = store->time_log_trim(oids[shard], start_time, end_time, start_marker, end_marker); | |
31f18b77 | 2319 | if (ret == -ENOENT || ret == -ENODATA) { |
7c673cae FG |
2320 | continue; |
2321 | } | |
2322 | if (ret < 0) | |
2323 | return ret; | |
2324 | } | |
2325 | ||
2326 | return 0; | |
2327 | } | |
2328 | ||
11fdf7f2 TL |
2329 | int RGWDataChangesLog::lock_exclusive(int shard_id, timespan duration, string& zone_id, string& owner_id) { |
2330 | return store->lock_exclusive(store->svc.zone->get_zone_params().log_pool, oids[shard_id], duration, zone_id, owner_id); | |
2331 | } | |
2332 | ||
2333 | int RGWDataChangesLog::unlock(int shard_id, string& zone_id, string& owner_id) { | |
2334 | return store->unlock(store->svc.zone->get_zone_params().log_pool, oids[shard_id], zone_id, owner_id); | |
2335 | } | |
2336 | ||
7c673cae FG |
2337 | bool RGWDataChangesLog::going_down() |
2338 | { | |
2339 | return down_flag; | |
2340 | } | |
2341 | ||
2342 | RGWDataChangesLog::~RGWDataChangesLog() { | |
2343 | down_flag = true; | |
2344 | renew_thread->stop(); | |
2345 | renew_thread->join(); | |
2346 | delete renew_thread; | |
2347 | delete[] oids; | |
2348 | } | |
2349 | ||
2350 | void *RGWDataChangesLog::ChangesRenewThread::entry() { | |
2351 | do { | |
2352 | dout(2) << "RGWDataChangesLog::ChangesRenewThread: start" << dendl; | |
2353 | int r = log->renew_entries(); | |
2354 | if (r < 0) { | |
2355 | dout(0) << "ERROR: RGWDataChangesLog::renew_entries returned error r=" << r << dendl; | |
2356 | } | |
2357 | ||
2358 | if (log->going_down()) | |
2359 | break; | |
2360 | ||
2361 | int interval = cct->_conf->rgw_data_log_window * 3 / 4; | |
2362 | lock.Lock(); | |
2363 | cond.WaitInterval(lock, utime_t(interval, 0)); | |
2364 | lock.Unlock(); | |
2365 | } while (!log->going_down()); | |
2366 | ||
2367 | return NULL; | |
2368 | } | |
2369 | ||
2370 | void RGWDataChangesLog::ChangesRenewThread::stop() | |
2371 | { | |
2372 | Mutex::Locker l(lock); | |
2373 | cond.Signal(); | |
2374 | } | |
2375 | ||
2376 | void RGWDataChangesLog::mark_modified(int shard_id, const rgw_bucket_shard& bs) | |
2377 | { | |
2378 | auto key = bs.get_key(); | |
2379 | modified_lock.get_read(); | |
2380 | map<int, set<string> >::iterator iter = modified_shards.find(shard_id); | |
2381 | if (iter != modified_shards.end()) { | |
2382 | set<string>& keys = iter->second; | |
2383 | if (keys.find(key) != keys.end()) { | |
2384 | modified_lock.unlock(); | |
2385 | return; | |
2386 | } | |
2387 | } | |
2388 | modified_lock.unlock(); | |
2389 | ||
2390 | RWLock::WLocker wl(modified_lock); | |
2391 | modified_shards[shard_id].insert(key); | |
2392 | } | |
2393 | ||
2394 | void RGWDataChangesLog::read_clear_modified(map<int, set<string> > &modified) | |
2395 | { | |
2396 | RWLock::WLocker wl(modified_lock); | |
2397 | modified.swap(modified_shards); | |
2398 | modified_shards.clear(); | |
2399 | } | |
2400 | ||
2401 | void RGWBucketCompleteInfo::dump(Formatter *f) const { | |
2402 | encode_json("bucket_info", info, f); | |
2403 | encode_json("attrs", attrs, f); | |
2404 | } | |
2405 | ||
2406 | void RGWBucketCompleteInfo::decode_json(JSONObj *obj) { | |
2407 | JSONDecoder::decode_json("bucket_info", info, obj); | |
2408 | JSONDecoder::decode_json("attrs", attrs, obj); | |
2409 | } | |
2410 | ||
2411 | class RGWBucketMetadataHandler : public RGWMetadataHandler { | |
2412 | ||
2413 | public: | |
2414 | string get_type() override { return "bucket"; } | |
2415 | ||
2416 | int get(RGWRados *store, string& entry, RGWMetadataObject **obj) override { | |
2417 | RGWObjVersionTracker ot; | |
2418 | RGWBucketEntryPoint be; | |
2419 | ||
2420 | real_time mtime; | |
2421 | map<string, bufferlist> attrs; | |
11fdf7f2 | 2422 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
2423 | |
2424 | string tenant_name, bucket_name; | |
2425 | parse_bucket(entry, &tenant_name, &bucket_name); | |
2426 | int ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, be, &ot, &mtime, &attrs); | |
2427 | if (ret < 0) | |
2428 | return ret; | |
2429 | ||
2430 | RGWBucketEntryMetadataObject *mdo = new RGWBucketEntryMetadataObject(be, ot.read_version, mtime); | |
2431 | ||
2432 | *obj = mdo; | |
2433 | ||
2434 | return 0; | |
2435 | } | |
2436 | ||
2437 | int put(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker, | |
2438 | real_time mtime, JSONObj *obj, sync_type_t sync_type) override { | |
2439 | RGWBucketEntryPoint be, old_be; | |
2440 | try { | |
2441 | decode_json_obj(be, obj); | |
2442 | } catch (JSONDecoder::err& e) { | |
2443 | return -EINVAL; | |
2444 | } | |
2445 | ||
2446 | real_time orig_mtime; | |
2447 | map<string, bufferlist> attrs; | |
2448 | ||
2449 | RGWObjVersionTracker old_ot; | |
11fdf7f2 | 2450 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
2451 | |
2452 | string tenant_name, bucket_name; | |
2453 | parse_bucket(entry, &tenant_name, &bucket_name); | |
2454 | int ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, old_be, &old_ot, &orig_mtime, &attrs); | |
2455 | if (ret < 0 && ret != -ENOENT) | |
2456 | return ret; | |
2457 | ||
2458 | // are we actually going to perform this put, or is it too old? | |
2459 | if (ret != -ENOENT && | |
2460 | !check_versions(old_ot.read_version, orig_mtime, | |
2461 | objv_tracker.write_version, mtime, sync_type)) { | |
2462 | return STATUS_NO_APPLY; | |
2463 | } | |
2464 | ||
2465 | objv_tracker.read_version = old_ot.read_version; /* maintain the obj version we just read */ | |
2466 | ||
2467 | ret = store->put_bucket_entrypoint_info(tenant_name, bucket_name, be, false, objv_tracker, mtime, &attrs); | |
2468 | if (ret < 0) | |
2469 | return ret; | |
2470 | ||
2471 | /* link bucket */ | |
2472 | if (be.linked) { | |
2473 | ret = rgw_link_bucket(store, be.owner, be.bucket, be.creation_time, false); | |
2474 | } else { | |
3efd9988 FG |
2475 | ret = rgw_unlink_bucket(store, be.owner, be.bucket.tenant, |
2476 | be.bucket.name, false); | |
7c673cae FG |
2477 | } |
2478 | ||
2479 | return ret; | |
2480 | } | |
2481 | ||
2482 | struct list_keys_info { | |
2483 | RGWRados *store; | |
2484 | RGWListRawObjsCtx ctx; | |
2485 | }; | |
2486 | ||
2487 | int remove(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker) override { | |
2488 | RGWBucketEntryPoint be; | |
11fdf7f2 | 2489 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
2490 | |
2491 | string tenant_name, bucket_name; | |
2492 | parse_bucket(entry, &tenant_name, &bucket_name); | |
2493 | int ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, be, &objv_tracker, NULL, NULL); | |
2494 | if (ret < 0) | |
2495 | return ret; | |
2496 | ||
2497 | /* | |
31f18b77 | 2498 | * We're unlinking the bucket but we don't want to update the entrypoint here - we're removing |
7c673cae FG |
2499 | * it immediately and don't want to invalidate our cached objv_version or the bucket obj removal |
2500 | * will incorrectly fail. | |
2501 | */ | |
2502 | ret = rgw_unlink_bucket(store, be.owner, tenant_name, bucket_name, false); | |
2503 | if (ret < 0) { | |
2504 | lderr(store->ctx()) << "could not unlink bucket=" << entry << " owner=" << be.owner << dendl; | |
2505 | } | |
2506 | ||
2507 | ret = rgw_bucket_delete_bucket_obj(store, tenant_name, bucket_name, objv_tracker); | |
2508 | if (ret < 0) { | |
2509 | lderr(store->ctx()) << "could not delete bucket=" << entry << dendl; | |
2510 | } | |
2511 | /* idempotent */ | |
2512 | return 0; | |
2513 | } | |
2514 | ||
2515 | void get_pool_and_oid(RGWRados *store, const string& key, rgw_pool& pool, string& oid) override { | |
2516 | oid = key; | |
11fdf7f2 | 2517 | pool = store->svc.zone->get_zone_params().domain_root; |
7c673cae FG |
2518 | } |
2519 | ||
181888fb | 2520 | int list_keys_init(RGWRados *store, const string& marker, void **phandle) override { |
11fdf7f2 | 2521 | auto info = std::make_unique<list_keys_info>(); |
7c673cae FG |
2522 | |
2523 | info->store = store; | |
2524 | ||
11fdf7f2 | 2525 | int ret = store->list_raw_objects_init(store->svc.zone->get_zone_params().domain_root, marker, |
181888fb FG |
2526 | &info->ctx); |
2527 | if (ret < 0) { | |
2528 | return ret; | |
2529 | } | |
2530 | *phandle = (void *)info.release(); | |
7c673cae FG |
2531 | |
2532 | return 0; | |
2533 | } | |
2534 | ||
2535 | int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated) override { | |
2536 | list_keys_info *info = static_cast<list_keys_info *>(handle); | |
2537 | ||
2538 | string no_filter; | |
2539 | ||
2540 | keys.clear(); | |
2541 | ||
2542 | RGWRados *store = info->store; | |
2543 | ||
2544 | list<string> unfiltered_keys; | |
2545 | ||
181888fb FG |
2546 | int ret = store->list_raw_objects_next(no_filter, max, info->ctx, |
2547 | unfiltered_keys, truncated); | |
7c673cae FG |
2548 | if (ret < 0 && ret != -ENOENT) |
2549 | return ret; | |
2550 | if (ret == -ENOENT) { | |
2551 | if (truncated) | |
2552 | *truncated = false; | |
2553 | return 0; | |
2554 | } | |
2555 | ||
2556 | // now filter out the system entries | |
2557 | list<string>::iterator iter; | |
2558 | for (iter = unfiltered_keys.begin(); iter != unfiltered_keys.end(); ++iter) { | |
2559 | string& k = *iter; | |
2560 | ||
2561 | if (k[0] != '.') { | |
2562 | keys.push_back(k); | |
2563 | } | |
2564 | } | |
2565 | ||
2566 | return 0; | |
2567 | } | |
2568 | ||
2569 | void list_keys_complete(void *handle) override { | |
2570 | list_keys_info *info = static_cast<list_keys_info *>(handle); | |
2571 | delete info; | |
2572 | } | |
181888fb | 2573 | |
11fdf7f2 | 2574 | string get_marker(void *handle) override { |
181888fb FG |
2575 | list_keys_info *info = static_cast<list_keys_info *>(handle); |
2576 | return info->store->list_raw_objs_get_cursor(info->ctx); | |
2577 | } | |
7c673cae FG |
2578 | }; |
2579 | ||
11fdf7f2 TL |
2580 | void get_md5_digest(const RGWBucketEntryPoint *be, string& md5_digest) { |
2581 | ||
2582 | char md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1]; | |
2583 | unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE]; | |
2584 | bufferlist bl; | |
2585 | ||
2586 | Formatter *f = new JSONFormatter(false); | |
2587 | be->dump(f); | |
2588 | f->flush(bl); | |
2589 | ||
2590 | MD5 hash; | |
2591 | hash.Update((const unsigned char *)bl.c_str(), bl.length()); | |
2592 | hash.Final(m); | |
2593 | ||
2594 | buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, md5); | |
2595 | ||
2596 | delete f; | |
2597 | ||
2598 | md5_digest = md5; | |
2599 | } | |
2600 | ||
2601 | #define ARCHIVE_META_ATTR RGW_ATTR_PREFIX "zone.archive.info" | |
2602 | ||
2603 | struct archive_meta_info { | |
2604 | rgw_bucket orig_bucket; | |
2605 | ||
2606 | bool from_attrs(CephContext *cct, map<string, bufferlist>& attrs) { | |
2607 | auto iter = attrs.find(ARCHIVE_META_ATTR); | |
2608 | if (iter == attrs.end()) { | |
2609 | return false; | |
2610 | } | |
2611 | ||
2612 | auto bliter = iter->second.cbegin(); | |
2613 | try { | |
2614 | decode(bliter); | |
2615 | } catch (buffer::error& err) { | |
2616 | ldout(cct, 0) << "ERROR: failed to decode archive meta info" << dendl; | |
2617 | return false; | |
2618 | } | |
2619 | ||
2620 | return true; | |
2621 | } | |
2622 | ||
2623 | void store_in_attrs(map<string, bufferlist>& attrs) const { | |
2624 | encode(attrs[ARCHIVE_META_ATTR]); | |
2625 | } | |
2626 | ||
2627 | void encode(bufferlist& bl) const { | |
2628 | ENCODE_START(1, 1, bl); | |
2629 | encode(orig_bucket, bl); | |
2630 | ENCODE_FINISH(bl); | |
2631 | } | |
2632 | ||
2633 | void decode(bufferlist::const_iterator& bl) { | |
2634 | DECODE_START(1, bl); | |
2635 | decode(orig_bucket, bl); | |
2636 | DECODE_FINISH(bl); | |
2637 | } | |
2638 | }; | |
2639 | WRITE_CLASS_ENCODER(archive_meta_info) | |
2640 | ||
2641 | class RGWArchiveBucketMetadataHandler : public RGWBucketMetadataHandler { | |
2642 | public: | |
2643 | int remove(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker) override { | |
2644 | ldout(store->ctx(), 5) << "SKIP: bucket removal is not allowed on archive zone: bucket:" << entry << " ... proceeding to rename" << dendl; | |
2645 | ||
2646 | string tenant_name, bucket_name; | |
2647 | parse_bucket(entry, &tenant_name, &bucket_name); | |
2648 | ||
2649 | real_time mtime; | |
2650 | ||
2651 | /* read original entrypoint */ | |
2652 | ||
2653 | RGWBucketEntryPoint be; | |
2654 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); | |
2655 | map<string, bufferlist> attrs; | |
2656 | int ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, be, &objv_tracker, &mtime, &attrs); | |
2657 | if (ret < 0) { | |
2658 | return ret; | |
2659 | } | |
2660 | ||
2661 | string meta_name = bucket_name + ":" + be.bucket.bucket_id; | |
2662 | ||
2663 | /* read original bucket instance info */ | |
2664 | ||
2665 | map<string, bufferlist> attrs_m; | |
2666 | ceph::real_time orig_mtime; | |
2667 | RGWBucketInfo old_bi; | |
2668 | ||
2669 | ret = store->get_bucket_instance_info(obj_ctx, be.bucket, old_bi, &orig_mtime, &attrs_m); | |
2670 | if (ret < 0) { | |
2671 | return ret; | |
2672 | } | |
2673 | ||
2674 | archive_meta_info ami; | |
2675 | ||
2676 | if (!ami.from_attrs(store->ctx(), attrs_m)) { | |
2677 | ami.orig_bucket = old_bi.bucket; | |
2678 | ami.store_in_attrs(attrs_m); | |
2679 | } | |
2680 | ||
2681 | /* generate a new bucket instance. We could have avoided this if we could just point a new | |
2682 | * bucket entry point to the old bucket instance, however, due to limitation in the way | |
2683 | * we index buckets under the user, bucket entrypoint and bucket instance of the same | |
2684 | * bucket need to have the same name, so we need to copy the old bucket instance into | |
2685 | * to a new entry with the new name | |
2686 | */ | |
2687 | ||
2688 | string new_bucket_name; | |
2689 | ||
2690 | RGWBucketInfo new_bi = old_bi; | |
2691 | RGWBucketEntryPoint new_be = be; | |
2692 | ||
2693 | string md5_digest; | |
2694 | ||
2695 | get_md5_digest(&new_be, md5_digest); | |
2696 | new_bucket_name = ami.orig_bucket.name + "-deleted-" + md5_digest; | |
2697 | ||
2698 | new_bi.bucket.name = new_bucket_name; | |
2699 | new_bi.objv_tracker.clear(); | |
2700 | ||
2701 | new_be.bucket.name = new_bucket_name; | |
2702 | ||
2703 | ret = store->put_bucket_instance_info(new_bi, false, orig_mtime, &attrs_m); | |
2704 | if (ret < 0) { | |
2705 | ldout(store->ctx(), 0) << "ERROR: failed to put new bucket instance info for bucket=" << new_bi.bucket << " ret=" << ret << dendl; | |
2706 | return ret; | |
2707 | } | |
2708 | ||
2709 | /* store a new entrypoint */ | |
2710 | ||
2711 | RGWObjVersionTracker ot; | |
2712 | ot.generate_new_write_ver(store->ctx()); | |
2713 | ||
2714 | ret = store->put_bucket_entrypoint_info(tenant_name, new_bucket_name, new_be, true, ot, mtime, &attrs); | |
2715 | if (ret < 0) { | |
2716 | ldout(store->ctx(), 0) << "ERROR: failed to put new bucket entrypoint for bucket=" << new_be.bucket << " ret=" << ret << dendl; | |
2717 | return ret; | |
2718 | } | |
2719 | ||
2720 | /* link new bucket */ | |
2721 | ||
2722 | ret = rgw_link_bucket(store, new_be.owner, new_be.bucket, new_be.creation_time, false); | |
2723 | if (ret < 0) { | |
2724 | ldout(store->ctx(), 0) << "ERROR: failed to link new bucket for bucket=" << new_be.bucket << " ret=" << ret << dendl; | |
2725 | return ret; | |
2726 | } | |
2727 | ||
2728 | /* clean up old stuff */ | |
2729 | ||
2730 | ret = rgw_unlink_bucket(store, be.owner, tenant_name, bucket_name, false); | |
2731 | if (ret < 0) { | |
2732 | lderr(store->ctx()) << "could not unlink bucket=" << entry << " owner=" << be.owner << dendl; | |
2733 | } | |
2734 | ||
2735 | // if (ret == -ECANCELED) it means that there was a race here, and someone | |
2736 | // wrote to the bucket entrypoint just before we removed it. The question is | |
2737 | // whether it was a newly created bucket entrypoint ... in which case we | |
2738 | // should ignore the error and move forward, or whether it is a higher version | |
2739 | // of the same bucket instance ... in which we should retry | |
2740 | ret = rgw_bucket_delete_bucket_obj(store, tenant_name, bucket_name, objv_tracker); | |
2741 | if (ret < 0) { | |
2742 | lderr(store->ctx()) << "could not delete bucket=" << entry << dendl; | |
2743 | } | |
2744 | ||
2745 | ret = rgw_delete_system_obj(store, store->svc.zone->get_zone_params().domain_root, RGW_BUCKET_INSTANCE_MD_PREFIX + meta_name, NULL); | |
2746 | ||
2747 | /* idempotent */ | |
2748 | ||
2749 | return 0; | |
2750 | } | |
2751 | ||
2752 | int put(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker, | |
2753 | real_time mtime, JSONObj *obj, sync_type_t sync_type) override { | |
2754 | if (entry.find("-deleted-") != string::npos) { | |
2755 | RGWObjVersionTracker ot; | |
2756 | RGWMetadataObject *robj; | |
2757 | int ret = get(store, entry, &robj); | |
2758 | if (ret != -ENOENT) { | |
2759 | if (ret < 0) { | |
2760 | return ret; | |
2761 | } | |
2762 | ot.read_version = robj->get_version(); | |
2763 | delete robj; | |
2764 | ||
2765 | ret = remove(store, entry, ot); | |
2766 | if (ret < 0) { | |
2767 | return ret; | |
2768 | } | |
2769 | } | |
2770 | } | |
2771 | ||
2772 | return RGWBucketMetadataHandler::put(store, entry, objv_tracker, | |
2773 | mtime, obj, sync_type); | |
2774 | } | |
2775 | ||
2776 | }; | |
2777 | ||
7c673cae FG |
2778 | class RGWBucketInstanceMetadataHandler : public RGWMetadataHandler { |
2779 | ||
2780 | public: | |
2781 | string get_type() override { return "bucket.instance"; } | |
2782 | ||
2783 | int get(RGWRados *store, string& oid, RGWMetadataObject **obj) override { | |
2784 | RGWBucketCompleteInfo bci; | |
2785 | ||
2786 | real_time mtime; | |
11fdf7f2 | 2787 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
2788 | |
2789 | int ret = store->get_bucket_instance_info(obj_ctx, oid, bci.info, &mtime, &bci.attrs); | |
2790 | if (ret < 0) | |
2791 | return ret; | |
2792 | ||
2793 | RGWBucketInstanceMetadataObject *mdo = new RGWBucketInstanceMetadataObject(bci, bci.info.objv_tracker.read_version, mtime); | |
2794 | ||
2795 | *obj = mdo; | |
2796 | ||
2797 | return 0; | |
2798 | } | |
2799 | ||
2800 | int put(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker, | |
2801 | real_time mtime, JSONObj *obj, sync_type_t sync_type) override { | |
2802 | RGWBucketCompleteInfo bci, old_bci; | |
2803 | try { | |
2804 | decode_json_obj(bci, obj); | |
2805 | } catch (JSONDecoder::err& e) { | |
2806 | return -EINVAL; | |
2807 | } | |
2808 | ||
2809 | real_time orig_mtime; | |
11fdf7f2 | 2810 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
2811 | |
2812 | int ret = store->get_bucket_instance_info(obj_ctx, entry, old_bci.info, | |
2813 | &orig_mtime, &old_bci.attrs); | |
2814 | bool exists = (ret != -ENOENT); | |
2815 | if (ret < 0 && exists) | |
2816 | return ret; | |
2817 | ||
2818 | if (!exists || old_bci.info.bucket.bucket_id != bci.info.bucket.bucket_id) { | |
2819 | /* a new bucket, we need to select a new bucket placement for it */ | |
2820 | auto key(entry); | |
2821 | rgw_bucket_instance_oid_to_key(key); | |
2822 | string tenant_name; | |
2823 | string bucket_name; | |
2824 | string bucket_instance; | |
2825 | parse_bucket(key, &tenant_name, &bucket_name, &bucket_instance); | |
2826 | ||
2827 | RGWZonePlacementInfo rule_info; | |
2828 | bci.info.bucket.name = bucket_name; | |
2829 | bci.info.bucket.bucket_id = bucket_instance; | |
2830 | bci.info.bucket.tenant = tenant_name; | |
11fdf7f2 | 2831 | ret = store->svc.zone->select_bucket_location_by_rule(bci.info.placement_rule, &rule_info); |
7c673cae FG |
2832 | if (ret < 0) { |
2833 | ldout(store->ctx(), 0) << "ERROR: select_bucket_placement() returned " << ret << dendl; | |
2834 | return ret; | |
2835 | } | |
2836 | bci.info.index_type = rule_info.index_type; | |
2837 | } else { | |
2838 | /* existing bucket, keep its placement */ | |
2839 | bci.info.bucket.explicit_placement = old_bci.info.bucket.explicit_placement; | |
2840 | bci.info.placement_rule = old_bci.info.placement_rule; | |
2841 | } | |
2842 | ||
c07f9fc5 FG |
2843 | if (exists && old_bci.info.datasync_flag_enabled() != bci.info.datasync_flag_enabled()) { |
2844 | int shards_num = bci.info.num_shards? bci.info.num_shards : 1; | |
2845 | int shard_id = bci.info.num_shards? 0 : -1; | |
2846 | ||
2847 | if (!bci.info.datasync_flag_enabled()) { | |
2848 | ret = store->stop_bi_log_entries(bci.info, -1); | |
2849 | if (ret < 0) { | |
2850 | lderr(store->ctx()) << "ERROR: failed writing bilog" << dendl; | |
2851 | return ret; | |
2852 | } | |
2853 | } else { | |
2854 | ret = store->resync_bi_log_entries(bci.info, -1); | |
2855 | if (ret < 0) { | |
2856 | lderr(store->ctx()) << "ERROR: failed writing bilog" << dendl; | |
2857 | return ret; | |
2858 | } | |
2859 | } | |
2860 | ||
2861 | for (int i = 0; i < shards_num; ++i, ++shard_id) { | |
2862 | ret = store->data_log->add_entry(bci.info.bucket, shard_id); | |
2863 | if (ret < 0) { | |
2864 | lderr(store->ctx()) << "ERROR: failed writing data log" << dendl; | |
2865 | return ret; | |
2866 | } | |
2867 | } | |
2868 | } | |
2869 | ||
7c673cae FG |
2870 | // are we actually going to perform this put, or is it too old? |
2871 | if (exists && | |
2872 | !check_versions(old_bci.info.objv_tracker.read_version, orig_mtime, | |
2873 | objv_tracker.write_version, mtime, sync_type)) { | |
2874 | objv_tracker.read_version = old_bci.info.objv_tracker.read_version; | |
2875 | return STATUS_NO_APPLY; | |
2876 | } | |
2877 | ||
2878 | /* record the read version (if any), store the new version */ | |
2879 | bci.info.objv_tracker.read_version = old_bci.info.objv_tracker.read_version; | |
2880 | bci.info.objv_tracker.write_version = objv_tracker.write_version; | |
2881 | ||
2882 | ret = store->put_bucket_instance_info(bci.info, false, mtime, &bci.attrs); | |
2883 | if (ret < 0) | |
2884 | return ret; | |
2885 | ||
2886 | objv_tracker = bci.info.objv_tracker; | |
2887 | ||
2888 | ret = store->init_bucket_index(bci.info, bci.info.num_shards); | |
2889 | if (ret < 0) | |
2890 | return ret; | |
2891 | ||
2892 | return STATUS_APPLIED; | |
2893 | } | |
2894 | ||
2895 | struct list_keys_info { | |
2896 | RGWRados *store; | |
2897 | RGWListRawObjsCtx ctx; | |
2898 | }; | |
2899 | ||
f64942e4 AA |
2900 | int remove(RGWRados *store, string& entry, |
2901 | RGWObjVersionTracker& objv_tracker) override { | |
7c673cae | 2902 | RGWBucketInfo info; |
11fdf7f2 | 2903 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
2904 | |
2905 | int ret = store->get_bucket_instance_info(obj_ctx, entry, info, NULL, NULL); | |
2906 | if (ret < 0 && ret != -ENOENT) | |
2907 | return ret; | |
2908 | ||
f64942e4 AA |
2909 | return rgw_bucket_instance_remove_entry(store, entry, |
2910 | &info.objv_tracker); | |
7c673cae FG |
2911 | } |
2912 | ||
2913 | void get_pool_and_oid(RGWRados *store, const string& key, rgw_pool& pool, string& oid) override { | |
2914 | oid = RGW_BUCKET_INSTANCE_MD_PREFIX + key; | |
2915 | rgw_bucket_instance_key_to_oid(oid); | |
11fdf7f2 | 2916 | pool = store->svc.zone->get_zone_params().domain_root; |
7c673cae FG |
2917 | } |
2918 | ||
181888fb | 2919 | int list_keys_init(RGWRados *store, const string& marker, void **phandle) override { |
11fdf7f2 | 2920 | auto info = std::make_unique<list_keys_info>(); |
7c673cae FG |
2921 | |
2922 | info->store = store; | |
2923 | ||
11fdf7f2 | 2924 | int ret = store->list_raw_objects_init(store->svc.zone->get_zone_params().domain_root, marker, |
181888fb FG |
2925 | &info->ctx); |
2926 | if (ret < 0) { | |
2927 | return ret; | |
2928 | } | |
2929 | *phandle = (void *)info.release(); | |
7c673cae FG |
2930 | |
2931 | return 0; | |
2932 | } | |
2933 | ||
2934 | int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated) override { | |
2935 | list_keys_info *info = static_cast<list_keys_info *>(handle); | |
2936 | ||
2937 | string no_filter; | |
2938 | ||
2939 | keys.clear(); | |
2940 | ||
2941 | RGWRados *store = info->store; | |
2942 | ||
2943 | list<string> unfiltered_keys; | |
2944 | ||
181888fb FG |
2945 | int ret = store->list_raw_objects_next(no_filter, max, info->ctx, |
2946 | unfiltered_keys, truncated); | |
7c673cae FG |
2947 | if (ret < 0 && ret != -ENOENT) |
2948 | return ret; | |
2949 | if (ret == -ENOENT) { | |
2950 | if (truncated) | |
2951 | *truncated = false; | |
2952 | return 0; | |
2953 | } | |
2954 | ||
2955 | constexpr int prefix_size = sizeof(RGW_BUCKET_INSTANCE_MD_PREFIX) - 1; | |
2956 | // now filter in the relevant entries | |
2957 | list<string>::iterator iter; | |
2958 | for (iter = unfiltered_keys.begin(); iter != unfiltered_keys.end(); ++iter) { | |
2959 | string& k = *iter; | |
2960 | ||
2961 | if (k.compare(0, prefix_size, RGW_BUCKET_INSTANCE_MD_PREFIX) == 0) { | |
2962 | auto oid = k.substr(prefix_size); | |
2963 | rgw_bucket_instance_oid_to_key(oid); | |
2964 | keys.emplace_back(std::move(oid)); | |
2965 | } | |
2966 | } | |
2967 | ||
2968 | return 0; | |
2969 | } | |
2970 | ||
2971 | void list_keys_complete(void *handle) override { | |
2972 | list_keys_info *info = static_cast<list_keys_info *>(handle); | |
2973 | delete info; | |
2974 | } | |
2975 | ||
11fdf7f2 | 2976 | string get_marker(void *handle) override { |
181888fb FG |
2977 | list_keys_info *info = static_cast<list_keys_info *>(handle); |
2978 | return info->store->list_raw_objs_get_cursor(info->ctx); | |
2979 | } | |
2980 | ||
7c673cae FG |
2981 | /* |
2982 | * hash entry for mdlog placement. Use the same hash key we'd have for the bucket entry | |
2983 | * point, so that the log entries end up at the same log shard, so that we process them | |
2984 | * in order | |
2985 | */ | |
2986 | void get_hash_key(const string& section, const string& key, string& hash_key) override { | |
2987 | string k; | |
2988 | int pos = key.find(':'); | |
2989 | if (pos < 0) | |
2990 | k = key; | |
2991 | else | |
2992 | k = key.substr(0, pos); | |
2993 | hash_key = "bucket:" + k; | |
2994 | } | |
2995 | }; | |
2996 | ||
11fdf7f2 TL |
2997 | class RGWArchiveBucketInstanceMetadataHandler : public RGWBucketInstanceMetadataHandler { |
2998 | public: | |
2999 | ||
3000 | int remove(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker) override { | |
3001 | ldout(store->ctx(), 0) << "SKIP: bucket instance removal is not allowed on archive zone: bucket.instance:" << entry << dendl; | |
3002 | return 0; | |
3003 | } | |
3004 | }; | |
3005 | ||
3006 | RGWMetadataHandler *RGWBucketMetaHandlerAllocator::alloc() { | |
3007 | return new RGWBucketMetadataHandler; | |
3008 | } | |
3009 | ||
3010 | RGWMetadataHandler *RGWBucketInstanceMetaHandlerAllocator::alloc() { | |
3011 | return new RGWBucketInstanceMetadataHandler; | |
3012 | } | |
3013 | ||
3014 | RGWMetadataHandler *RGWArchiveBucketMetaHandlerAllocator::alloc() { | |
3015 | return new RGWArchiveBucketMetadataHandler; | |
3016 | } | |
3017 | ||
3018 | RGWMetadataHandler *RGWArchiveBucketInstanceMetaHandlerAllocator::alloc() { | |
3019 | return new RGWArchiveBucketInstanceMetadataHandler; | |
3020 | } | |
3021 | ||
7c673cae FG |
3022 | void rgw_bucket_init(RGWMetadataManager *mm) |
3023 | { | |
11fdf7f2 TL |
3024 | auto sync_module = mm->get_store()->get_sync_module(); |
3025 | if (sync_module) { | |
3026 | bucket_meta_handler = sync_module->alloc_bucket_meta_handler(); | |
3027 | bucket_instance_meta_handler = sync_module->alloc_bucket_instance_meta_handler(); | |
3028 | } else { | |
3029 | bucket_meta_handler = RGWBucketMetaHandlerAllocator::alloc(); | |
3030 | bucket_instance_meta_handler = RGWBucketInstanceMetaHandlerAllocator::alloc(); | |
3031 | } | |
7c673cae | 3032 | mm->register_handler(bucket_meta_handler); |
7c673cae FG |
3033 | mm->register_handler(bucket_instance_meta_handler); |
3034 | } |