]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_bucket.cc
update sources to v12.2.3
[ceph.git] / ceph / src / rgw / rgw_bucket.cc
CommitLineData
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"
181888fb 15#include "common/backport14.h"
7c673cae
FG
16#include "rgw_rados.h"
17#include "rgw_acl.h"
18#include "rgw_acl_s3.h"
19
20#include "include/types.h"
21#include "rgw_bucket.h"
22#include "rgw_user.h"
23#include "rgw_string.h"
224ce89b 24#include "rgw_multi.h"
7c673cae
FG
25
26#include "include/rados/librados.hpp"
27// until everything is moved from rgw_common
28#include "rgw_common.h"
29
30#include "cls/user/cls_user_types.h"
31
32#define dout_context g_ceph_context
33#define dout_subsys ceph_subsys_rgw
34
35#define BUCKET_TAG_TIMEOUT 30
36
37using namespace std;
38
39static RGWMetadataHandler *bucket_meta_handler = NULL;
40static RGWMetadataHandler *bucket_instance_meta_handler = NULL;
41
42// define as static when RGWBucket implementation compete
43void rgw_get_buckets_obj(const rgw_user& user_id, string& buckets_obj_id)
44{
45 buckets_obj_id = user_id.to_str();
46 buckets_obj_id += RGW_BUCKETS_OBJ_SUFFIX;
47}
48
49/*
50 * Note that this is not a reversal of parse_bucket(). That one deals
51 * with the syntax we need in metadata and such. This one deals with
52 * the representation in RADOS pools. We chose '/' because it's not
53 * acceptable in bucket names and thus qualified buckets cannot conflict
54 * with the legacy or S3 buckets.
55 */
56std::string rgw_make_bucket_entry_name(const std::string& tenant_name,
57 const std::string& bucket_name) {
58 std::string bucket_entry;
59
60 if (bucket_name.empty()) {
61 bucket_entry.clear();
62 } else if (tenant_name.empty()) {
63 bucket_entry = bucket_name;
64 } else {
65 bucket_entry = tenant_name + "/" + bucket_name;
66 }
67
68 return bucket_entry;
69}
70
71/*
72 * Tenants are separated from buckets in URLs by a colon in S3.
73 * This function is not to be used on Swift URLs, not even for COPY arguments.
74 */
75void rgw_parse_url_bucket(const string &bucket, const string& auth_tenant,
76 string &tenant_name, string &bucket_name) {
77
78 int pos = bucket.find(':');
79 if (pos >= 0) {
80 /*
81 * N.B.: We allow ":bucket" syntax with explicit empty tenant in order
82 * to refer to the legacy tenant, in case users in new named tenants
83 * want to access old global buckets.
84 */
85 tenant_name = bucket.substr(0, pos);
86 bucket_name = bucket.substr(pos + 1);
87 } else {
88 tenant_name = auth_tenant;
89 bucket_name = bucket;
90 }
91}
92
93/**
94 * Get all the buckets owned by a user and fill up an RGWUserBuckets with them.
95 * Returns: 0 on success, -ERR# on failure.
96 */
97int rgw_read_user_buckets(RGWRados * store,
98 const rgw_user& user_id,
99 RGWUserBuckets& buckets,
100 const string& marker,
101 const string& end_marker,
102 uint64_t max,
103 bool need_stats,
104 bool *is_truncated,
105 uint64_t default_amount)
106{
107 int ret;
108 buckets.clear();
3efd9988 109 std::string buckets_obj_id;
7c673cae
FG
110 rgw_get_buckets_obj(user_id, buckets_obj_id);
111 rgw_raw_obj obj(store->get_zone_params().user_uid_pool, buckets_obj_id);
7c673cae
FG
112
113 bool truncated = false;
114 string m = marker;
115
116 uint64_t total = 0;
117
118 if (!max) {
119 max = default_amount;
120 }
121
122 do {
3efd9988 123 std::list<cls_user_bucket_entry> entries;
7c673cae 124 ret = store->cls_user_list_buckets(obj, m, end_marker, max - total, entries, &m, &truncated);
3efd9988 125 if (ret == -ENOENT) {
7c673cae 126 ret = 0;
3efd9988 127 }
7c673cae 128
3efd9988 129 if (ret < 0) {
7c673cae 130 return ret;
3efd9988 131 }
7c673cae 132
3efd9988
FG
133 for (auto& entry : entries) {
134 buckets.add(RGWBucketEnt(user_id, std::move(entry)));
7c673cae
FG
135 total++;
136 }
137
138 } while (truncated && total < max);
139
140 if (is_truncated != nullptr) {
141 *is_truncated = truncated;
142 }
143
144 if (need_stats) {
145 map<string, RGWBucketEnt>& m = buckets.get_buckets();
146 ret = store->update_containers_stats(m);
147 if (ret < 0 && ret != -ENOENT) {
148 ldout(store->ctx(), 0) << "ERROR: could not get stats for buckets" << dendl;
149 return ret;
150 }
151 }
152 return 0;
153}
154
155int rgw_bucket_sync_user_stats(RGWRados *store, const rgw_user& user_id, const RGWBucketInfo& bucket_info)
156{
157 string buckets_obj_id;
158 rgw_get_buckets_obj(user_id, buckets_obj_id);
159 rgw_raw_obj obj(store->get_zone_params().user_uid_pool, buckets_obj_id);
160
161 return store->cls_user_sync_bucket_stats(obj, bucket_info);
162}
163
164int rgw_bucket_sync_user_stats(RGWRados *store, const string& tenant_name, const string& bucket_name)
165{
166 RGWBucketInfo bucket_info;
167 RGWObjectCtx obj_ctx(store);
168 int ret = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, NULL);
169 if (ret < 0) {
170 ldout(store->ctx(), 0) << "ERROR: could not fetch bucket info: ret=" << ret << dendl;
171 return ret;
172 }
173
174 ret = rgw_bucket_sync_user_stats(store, bucket_info.owner, bucket_info);
175 if (ret < 0) {
176 ldout(store->ctx(), 0) << "ERROR: could not sync user stats for bucket " << bucket_name << ": ret=" << ret << dendl;
177 return ret;
178 }
179
180 return 0;
181}
182
3efd9988
FG
183int rgw_link_bucket(RGWRados* const store,
184 const rgw_user& user_id,
185 rgw_bucket& bucket,
186 ceph::real_time creation_time,
187 bool update_entrypoint)
7c673cae
FG
188{
189 int ret;
190 string& tenant_name = bucket.tenant;
191 string& bucket_name = bucket.name;
192
193 cls_user_bucket_entry new_bucket;
194
195 RGWBucketEntryPoint ep;
196 RGWObjVersionTracker ot;
197
198 bucket.convert(&new_bucket.bucket);
199 new_bucket.size = 0;
200 if (real_clock::is_zero(creation_time))
201 new_bucket.creation_time = real_clock::now();
202 else
203 new_bucket.creation_time = creation_time;
204
205 map<string, bufferlist> attrs;
206 RGWObjectCtx obj_ctx(store);
207
208 if (update_entrypoint) {
209 ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, ep, &ot, NULL, &attrs);
210 if (ret < 0 && ret != -ENOENT) {
211 ldout(store->ctx(), 0) << "ERROR: store->get_bucket_entrypoint_info() returned: "
212 << cpp_strerror(-ret) << dendl;
213 }
214 }
215
216 string buckets_obj_id;
217 rgw_get_buckets_obj(user_id, buckets_obj_id);
218
219 rgw_raw_obj obj(store->get_zone_params().user_uid_pool, buckets_obj_id);
220 ret = store->cls_user_add_bucket(obj, new_bucket);
221 if (ret < 0) {
222 ldout(store->ctx(), 0) << "ERROR: error adding bucket to directory: "
223 << cpp_strerror(-ret) << dendl;
224 goto done_err;
225 }
226
227 if (!update_entrypoint)
228 return 0;
229
230 ep.linked = true;
231 ep.owner = user_id;
232 ep.bucket = bucket;
233 ret = store->put_bucket_entrypoint_info(tenant_name, bucket_name, ep, false, ot, real_time(), &attrs);
234 if (ret < 0)
235 goto done_err;
236
237 return 0;
238done_err:
239 int r = rgw_unlink_bucket(store, user_id, bucket.tenant, bucket.name);
240 if (r < 0) {
241 ldout(store->ctx(), 0) << "ERROR: failed unlinking bucket on error cleanup: "
242 << cpp_strerror(-r) << dendl;
243 }
244 return ret;
245}
246
247int rgw_unlink_bucket(RGWRados *store, const rgw_user& user_id, const string& tenant_name, const string& bucket_name, bool update_entrypoint)
248{
249 int ret;
250
251 string buckets_obj_id;
252 rgw_get_buckets_obj(user_id, buckets_obj_id);
253
254 cls_user_bucket bucket;
255 bucket.name = bucket_name;
256 rgw_raw_obj obj(store->get_zone_params().user_uid_pool, buckets_obj_id);
257 ret = store->cls_user_remove_bucket(obj, bucket);
258 if (ret < 0) {
259 ldout(store->ctx(), 0) << "ERROR: error removing bucket from directory: "
260 << cpp_strerror(-ret)<< dendl;
261 }
262
263 if (!update_entrypoint)
264 return 0;
265
266 RGWBucketEntryPoint ep;
267 RGWObjVersionTracker ot;
268 map<string, bufferlist> attrs;
269 RGWObjectCtx obj_ctx(store);
270 ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, ep, &ot, NULL, &attrs);
271 if (ret == -ENOENT)
272 return 0;
273 if (ret < 0)
274 return ret;
275
276 if (!ep.linked)
277 return 0;
278
279 if (ep.owner != user_id) {
280 ldout(store->ctx(), 0) << "bucket entry point user mismatch, can't unlink bucket: " << ep.owner << " != " << user_id << dendl;
281 return -EINVAL;
282 }
283
284 ep.linked = false;
285 return store->put_bucket_entrypoint_info(tenant_name, bucket_name, ep, false, ot, real_time(), &attrs);
286}
287
288int rgw_bucket_store_info(RGWRados *store, const string& bucket_name, bufferlist& bl, bool exclusive,
289 map<string, bufferlist> *pattrs, RGWObjVersionTracker *objv_tracker,
290 real_time mtime) {
291 return store->meta_mgr->put_entry(bucket_meta_handler, bucket_name, bl, exclusive, objv_tracker, mtime, pattrs);
292}
293
294int rgw_bucket_instance_store_info(RGWRados *store, string& entry, bufferlist& bl, bool exclusive,
295 map<string, bufferlist> *pattrs, RGWObjVersionTracker *objv_tracker,
296 real_time mtime) {
297 return store->meta_mgr->put_entry(bucket_instance_meta_handler, entry, bl, exclusive, objv_tracker, mtime, pattrs);
298}
299
300int rgw_bucket_instance_remove_entry(RGWRados *store, string& entry, RGWObjVersionTracker *objv_tracker) {
301 return store->meta_mgr->remove_entry(bucket_instance_meta_handler, entry, objv_tracker);
302}
303
304// 'tenant/' is used in bucket instance keys for sync to avoid parsing ambiguity
305// with the existing instance[:shard] format. once we parse the shard, the / is
306// replaced with a : to match the [tenant:]instance format
307void rgw_bucket_instance_key_to_oid(string& key)
308{
309 // replace tenant/ with tenant:
310 auto c = key.find('/');
311 if (c != string::npos) {
312 key[c] = ':';
313 }
314}
315
316// convert bucket instance oids back to the tenant/ format for metadata keys.
317// it's safe to parse 'tenant:' only for oids, because they won't contain the
318// optional :shard at the end
319void rgw_bucket_instance_oid_to_key(string& oid)
320{
321 // find first : (could be tenant:bucket or bucket:instance)
322 auto c = oid.find(':');
323 if (c != string::npos) {
324 // if we find another :, the first one was for tenant
325 if (oid.find(':', c + 1) != string::npos) {
326 oid[c] = '/';
327 }
328 }
329}
330
331int rgw_bucket_parse_bucket_instance(const string& bucket_instance, string *target_bucket_instance, int *shard_id)
332{
333 ssize_t pos = bucket_instance.rfind(':');
334 if (pos < 0) {
335 return -EINVAL;
336 }
337
338 string first = bucket_instance.substr(0, pos);
339 string second = bucket_instance.substr(pos + 1);
340
341 if (first.find(':') == string::npos) {
342 *shard_id = -1;
343 *target_bucket_instance = bucket_instance;
344 return 0;
345 }
346
347 *target_bucket_instance = first;
348 string err;
349 *shard_id = strict_strtol(second.c_str(), 10, &err);
350 if (!err.empty()) {
351 return -EINVAL;
352 }
353
354 return 0;
355}
356
357// parse key in format: [tenant/]name:instance[:shard_id]
358int rgw_bucket_parse_bucket_key(CephContext *cct, const string& key,
359 rgw_bucket *bucket, int *shard_id)
360{
361 boost::string_ref name{key};
362 boost::string_ref instance;
363
364 // split tenant/name
365 auto pos = name.find('/');
366 if (pos != boost::string_ref::npos) {
367 auto tenant = name.substr(0, pos);
368 bucket->tenant.assign(tenant.begin(), tenant.end());
369 name = name.substr(pos + 1);
370 }
371
372 // split name:instance
373 pos = name.find(':');
374 if (pos != boost::string_ref::npos) {
375 instance = name.substr(pos + 1);
376 name = name.substr(0, pos);
377 }
378 bucket->name.assign(name.begin(), name.end());
379
380 // split instance:shard
381 pos = instance.find(':');
382 if (pos == boost::string_ref::npos) {
383 bucket->bucket_id.assign(instance.begin(), instance.end());
384 *shard_id = -1;
385 return 0;
386 }
387
388 // parse shard id
389 auto shard = instance.substr(pos + 1);
390 string err;
391 auto id = strict_strtol(shard.data(), 10, &err);
392 if (!err.empty()) {
393 ldout(cct, 0) << "ERROR: failed to parse bucket shard '"
394 << instance.data() << "': " << err << dendl;
395 return -EINVAL;
396 }
397
398 *shard_id = id;
399 instance = instance.substr(0, pos);
400 bucket->bucket_id.assign(instance.begin(), instance.end());
401 return 0;
402}
403
404int rgw_bucket_set_attrs(RGWRados *store, RGWBucketInfo& bucket_info,
405 map<string, bufferlist>& attrs,
406 RGWObjVersionTracker *objv_tracker)
407{
408 rgw_bucket& bucket = bucket_info.bucket;
409
410 if (!bucket_info.has_instance_obj) {
411 /* an old bucket object, need to convert it */
412 RGWObjectCtx obj_ctx(store);
413 int ret = store->convert_old_bucket_info(obj_ctx, bucket.tenant, bucket.name);
414 if (ret < 0) {
415 ldout(store->ctx(), 0) << "ERROR: failed converting old bucket info: " << ret << dendl;
416 return ret;
417 }
418 }
419
420 /* we want the bucket instance name without the oid prefix cruft */
421 string key = bucket.get_key();
422 bufferlist bl;
423
424 ::encode(bucket_info, bl);
425
426 return rgw_bucket_instance_store_info(store, key, bl, false, &attrs, objv_tracker, real_time());
427}
428
429static void dump_mulipart_index_results(list<rgw_obj_index_key>& objs_to_unlink,
430 Formatter *f)
431{
d2e6a577
FG
432 for (const auto& o : objs_to_unlink) {
433 f->dump_string("object", o.name);
7c673cae 434 }
7c673cae
FG
435}
436
437void check_bad_user_bucket_mapping(RGWRados *store, const rgw_user& user_id,
438 bool fix)
439{
440 RGWUserBuckets user_buckets;
441 bool is_truncated = false;
442 string marker;
443
444 CephContext *cct = store->ctx();
445
446 size_t max_entries = cct->_conf->rgw_list_buckets_max_chunk;
447
448 do {
449 int ret = rgw_read_user_buckets(store, user_id, user_buckets, marker,
450 string(), max_entries, false,
451 &is_truncated);
452 if (ret < 0) {
453 ldout(store->ctx(), 0) << "failed to read user buckets: "
454 << cpp_strerror(-ret) << dendl;
455 return;
456 }
457
458 map<string, RGWBucketEnt>& buckets = user_buckets.get_buckets();
459 for (map<string, RGWBucketEnt>::iterator i = buckets.begin();
460 i != buckets.end();
461 ++i) {
462 marker = i->first;
463
464 RGWBucketEnt& bucket_ent = i->second;
465 rgw_bucket& bucket = bucket_ent.bucket;
466
467 RGWBucketInfo bucket_info;
468 real_time mtime;
469 RGWObjectCtx obj_ctx(store);
470 int r = store->get_bucket_info(obj_ctx, user_id.tenant, bucket.name, bucket_info, &mtime);
471 if (r < 0) {
472 ldout(store->ctx(), 0) << "could not get bucket info for bucket=" << bucket << dendl;
473 continue;
474 }
475
476 rgw_bucket& actual_bucket = bucket_info.bucket;
477
478 if (actual_bucket.name.compare(bucket.name) != 0 ||
479 actual_bucket.tenant.compare(bucket.tenant) != 0 ||
480 actual_bucket.marker.compare(bucket.marker) != 0 ||
481 actual_bucket.bucket_id.compare(bucket.bucket_id) != 0) {
482 cout << "bucket info mismatch: expected " << actual_bucket << " got " << bucket << std::endl;
483 if (fix) {
484 cout << "fixing" << std::endl;
3efd9988
FG
485 r = rgw_link_bucket(store, user_id, actual_bucket,
486 bucket_info.creation_time);
7c673cae
FG
487 if (r < 0) {
488 cerr << "failed to fix bucket: " << cpp_strerror(-r) << std::endl;
489 }
490 }
491 }
492 }
493 } while (is_truncated);
494}
495
496static bool bucket_object_check_filter(const string& oid)
497{
498 rgw_obj_key key;
499 string ns;
500 return rgw_obj_key::oid_to_key_in_ns(oid, &key, ns);
501}
502
503int rgw_remove_object(RGWRados *store, RGWBucketInfo& bucket_info, rgw_bucket& bucket, rgw_obj_key& key)
504{
505 RGWObjectCtx rctx(store);
506
507 if (key.instance.empty()) {
508 key.instance = "null";
509 }
510
511 rgw_obj obj(bucket, key);
512
513 return store->delete_obj(rctx, bucket_info, obj, bucket_info.versioning_status());
514}
515
516int rgw_remove_bucket(RGWRados *store, rgw_bucket& bucket, bool delete_children)
517{
518 int ret;
519 map<RGWObjCategory, RGWStorageStats> stats;
520 std::vector<rgw_bucket_dir_entry> objs;
521 map<string, bool> common_prefixes;
522 RGWBucketInfo info;
523 RGWObjectCtx obj_ctx(store);
524
525 string bucket_ver, master_ver;
526
527 ret = store->get_bucket_info(obj_ctx, bucket.tenant, bucket.name, info, NULL);
528 if (ret < 0)
529 return ret;
530
531 ret = store->get_bucket_stats(info, RGW_NO_SHARD, &bucket_ver, &master_ver, stats, NULL);
532 if (ret < 0)
533 return ret;
534
7c673cae
FG
535 RGWRados::Bucket target(store, info);
536 RGWRados::Bucket::List list_op(&target);
224ce89b
WB
537 CephContext *cct = store->ctx();
538 int max = 1000;
7c673cae
FG
539
540 list_op.params.list_versions = true;
541
224ce89b
WB
542 do {
543 objs.clear();
7c673cae 544
224ce89b
WB
545 ret = list_op.list_objects(max, &objs, &common_prefixes, NULL);
546 if (ret < 0)
547 return ret;
7c673cae 548
224ce89b
WB
549 if (!objs.empty() && !delete_children) {
550 lderr(store->ctx()) << "ERROR: could not remove non-empty bucket " << bucket.name << dendl;
551 return -ENOTEMPTY;
552 }
553
554 for (const auto& obj : objs) {
555 rgw_obj_key key(obj.key);
556 ret = rgw_remove_object(store, info, bucket, key);
7c673cae
FG
557 if (ret < 0)
558 return ret;
224ce89b 559 }
7c673cae 560
224ce89b 561 } while (!objs.empty());
7c673cae 562
224ce89b 563 string prefix, delimiter;
7c673cae 564
224ce89b
WB
565 ret = abort_bucket_multiparts(store, cct, info, prefix, delimiter);
566 if (ret < 0) {
567 return ret;
7c673cae
FG
568 }
569
b32b8144 570 ret = rgw_bucket_sync_user_stats(store, info.owner, info);
7c673cae
FG
571 if ( ret < 0) {
572 dout(1) << "WARNING: failed sync user stats before bucket delete. ret=" << ret << dendl;
573 }
574
575 RGWObjVersionTracker objv_tracker;
576
577 ret = store->delete_bucket(info, objv_tracker);
578 if (ret < 0) {
579 lderr(store->ctx()) << "ERROR: could not remove bucket " << bucket.name << dendl;
580 return ret;
581 }
582
583 ret = rgw_unlink_bucket(store, info.owner, bucket.tenant, bucket.name, false);
584 if (ret < 0) {
585 lderr(store->ctx()) << "ERROR: unable to remove user bucket information" << dendl;
586 }
587
588 return ret;
589}
590
591static int aio_wait(librados::AioCompletion *handle)
592{
593 librados::AioCompletion *c = (librados::AioCompletion *)handle;
594 c->wait_for_safe();
595 int ret = c->get_return_value();
596 c->release();
597 return ret;
598}
599
600static int drain_handles(list<librados::AioCompletion *>& pending)
601{
602 int ret = 0;
603 while (!pending.empty()) {
604 librados::AioCompletion *handle = pending.front();
605 pending.pop_front();
606 int r = aio_wait(handle);
607 if (r < 0) {
608 ret = r;
609 }
610 }
611 return ret;
612}
613
614int rgw_remove_bucket_bypass_gc(RGWRados *store, rgw_bucket& bucket,
615 int concurrent_max, bool keep_index_consistent)
616{
617 int ret;
618 map<RGWObjCategory, RGWStorageStats> stats;
619 std::vector<rgw_bucket_dir_entry> objs;
620 map<string, bool> common_prefixes;
621 RGWBucketInfo info;
622 RGWObjectCtx obj_ctx(store);
224ce89b 623 CephContext *cct = store->ctx();
7c673cae
FG
624
625 string bucket_ver, master_ver;
626
627 ret = store->get_bucket_info(obj_ctx, bucket.tenant, bucket.name, info, NULL);
628 if (ret < 0)
629 return ret;
630
631 ret = store->get_bucket_stats(info, RGW_NO_SHARD, &bucket_ver, &master_ver, stats, NULL);
632 if (ret < 0)
633 return ret;
634
224ce89b
WB
635 string prefix, delimiter;
636
637 ret = abort_bucket_multiparts(store, cct, info, prefix, delimiter);
638 if (ret < 0) {
639 return ret;
640 }
7c673cae
FG
641
642 RGWRados::Bucket target(store, info);
643 RGWRados::Bucket::List list_op(&target);
644
645 list_op.params.list_versions = true;
646
647 std::list<librados::AioCompletion*> handles;
648
649 int max = 1000;
650 int max_aio = concurrent_max;
651 ret = list_op.list_objects(max, &objs, &common_prefixes, NULL);
652 if (ret < 0)
653 return ret;
654
655 while (!objs.empty()) {
656 std::vector<rgw_bucket_dir_entry>::iterator it = objs.begin();
657 for (; it != objs.end(); ++it) {
658 RGWObjState *astate = NULL;
659 rgw_obj obj(bucket, (*it).key);
660
661 ret = store->get_obj_state(&obj_ctx, info, obj, &astate, false);
662 if (ret == -ENOENT) {
663 dout(1) << "WARNING: cannot find obj state for obj " << obj.get_oid() << dendl;
664 continue;
665 }
666 if (ret < 0) {
667 lderr(store->ctx()) << "ERROR: get obj state returned with error " << ret << dendl;
668 return ret;
669 }
670
671 if (astate->has_manifest) {
672 RGWObjManifest& manifest = astate->manifest;
673 RGWObjManifest::obj_iterator miter = manifest.obj_begin();
674 rgw_obj head_obj = manifest.get_obj();
675 rgw_raw_obj raw_head_obj;
676 store->obj_to_raw(info.placement_rule, head_obj, &raw_head_obj);
677
678
679 for (; miter != manifest.obj_end() && max_aio--; ++miter) {
680 if (!max_aio) {
681 ret = drain_handles(handles);
682 if (ret < 0) {
683 lderr(store->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl;
684 return ret;
685 }
686 max_aio = concurrent_max;
687 }
688
689 rgw_raw_obj last_obj = miter.get_location().get_raw_obj(store);
690 if (last_obj == raw_head_obj) {
691 // have the head obj deleted at the end
692 continue;
693 }
694
695 ret = store->delete_raw_obj_aio(last_obj, handles);
696 if (ret < 0) {
697 lderr(store->ctx()) << "ERROR: delete obj aio failed with " << ret << dendl;
698 return ret;
699 }
700 } // for all shadow objs
701
702 ret = store->delete_obj_aio(head_obj, info, astate, handles, keep_index_consistent);
703 if (ret < 0) {
704 lderr(store->ctx()) << "ERROR: delete obj aio failed with " << ret << dendl;
705 return ret;
706 }
707 }
708
709 if (!max_aio) {
710 ret = drain_handles(handles);
711 if (ret < 0) {
712 lderr(store->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret << dendl;
713 return ret;
714 }
715 max_aio = concurrent_max;
716 }
717 } // for all RGW objects
718 objs.clear();
719
720 ret = list_op.list_objects(max, &objs, &common_prefixes, NULL);
721 if (ret < 0)
722 return ret;
723 }
724
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
b32b8144 731 ret = rgw_bucket_sync_user_stats(store, info.owner, info);
7c673cae
FG
732 if (ret < 0) {
733 dout(1) << "WARNING: failed sync user stats before bucket delete. ret=" << ret << dendl;
734 }
735
736 RGWObjVersionTracker objv_tracker;
737
b32b8144 738 ret = store->delete_bucket(info, objv_tracker);
7c673cae 739 if (ret < 0) {
b32b8144 740 lderr(store->ctx()) << "ERROR: could not remove bucket " << bucket.name << dendl;
7c673cae
FG
741 return ret;
742 }
743
7c673cae
FG
744 ret = rgw_unlink_bucket(store, info.owner, bucket.tenant, bucket.name, false);
745 if (ret < 0) {
746 lderr(store->ctx()) << "ERROR: unable to remove user bucket information" << dendl;
747 }
748
749 return ret;
750}
751
752int rgw_bucket_delete_bucket_obj(RGWRados *store,
753 const string& tenant_name,
754 const string& bucket_name,
755 RGWObjVersionTracker& objv_tracker)
756{
757 string key;
758
759 rgw_make_bucket_entry_name(tenant_name, bucket_name, key);
760 return store->meta_mgr->remove_entry(bucket_meta_handler, key, &objv_tracker);
761}
762
763static void set_err_msg(std::string *sink, std::string msg)
764{
765 if (sink && !msg.empty())
766 *sink = msg;
767}
768
769int RGWBucket::init(RGWRados *storage, RGWBucketAdminOpState& op_state)
770{
771 if (!storage)
772 return -EINVAL;
773
774 store = storage;
775
776 rgw_user user_id = op_state.get_user_id();
777 tenant = user_id.tenant;
778 bucket_name = op_state.get_bucket_name();
779 RGWUserBuckets user_buckets;
780 RGWObjectCtx obj_ctx(store);
781
782 if (bucket_name.empty() && user_id.empty())
783 return -EINVAL;
784
785 if (!bucket_name.empty()) {
786 int r = store->get_bucket_info(obj_ctx, tenant, bucket_name, bucket_info, NULL);
787 if (r < 0) {
788 ldout(store->ctx(), 0) << "could not get bucket info for bucket=" << bucket_name << dendl;
789 return r;
790 }
791
792 op_state.set_bucket(bucket_info.bucket);
793 }
794
795 if (!user_id.empty()) {
796 int r = rgw_get_user_info_by_uid(store, user_id, user_info);
797 if (r < 0)
798 return r;
799
800 op_state.display_name = user_info.display_name;
801 }
802
803 clear_failure();
804 return 0;
805}
806
807int RGWBucket::link(RGWBucketAdminOpState& op_state, std::string *err_msg)
808{
809 if (!op_state.is_user_op()) {
810 set_err_msg(err_msg, "empty user id");
811 return -EINVAL;
812 }
813
814 string bucket_id = op_state.get_bucket_id();
815 if (bucket_id.empty()) {
816 set_err_msg(err_msg, "empty bucket instance id");
817 return -EINVAL;
818 }
819
820 std::string display_name = op_state.get_user_display_name();
821 rgw_bucket bucket = op_state.get_bucket();
822
823 const rgw_pool& root_pool = store->get_zone_params().domain_root;
824 rgw_raw_obj obj(root_pool, bucket.name);
825 RGWObjVersionTracker objv_tracker;
826
827 map<string, bufferlist> attrs;
828 RGWBucketInfo bucket_info;
829
830 string key = bucket.name + ":" + bucket_id;
831 RGWObjectCtx obj_ctx(store);
832 int r = store->get_bucket_instance_info(obj_ctx, key, bucket_info, NULL, &attrs);
833 if (r < 0) {
834 return r;
835 }
836
837 rgw_user user_id = op_state.get_user_id();
838
839 map<string, bufferlist>::iterator aiter = attrs.find(RGW_ATTR_ACL);
840 if (aiter != attrs.end()) {
841 bufferlist aclbl = aiter->second;
842 RGWAccessControlPolicy policy;
843 ACLOwner owner;
844 try {
845 bufferlist::iterator iter = aclbl.begin();
846 ::decode(policy, iter);
847 owner = policy.get_owner();
848 } catch (buffer::error& err) {
849 set_err_msg(err_msg, "couldn't decode policy");
850 return -EIO;
851 }
852
853 r = rgw_unlink_bucket(store, owner.get_id(), bucket.tenant, bucket.name, false);
854 if (r < 0) {
855 set_err_msg(err_msg, "could not unlink policy from user " + owner.get_id().to_str());
856 return r;
857 }
858
859 // now update the user for the bucket...
860 if (display_name.empty()) {
861 ldout(store->ctx(), 0) << "WARNING: user " << user_info.user_id << " has no display name set" << dendl;
862 }
863 policy.create_default(user_info.user_id, display_name);
864
865 owner = policy.get_owner();
31f18b77 866 r = store->set_bucket_owner(bucket_info.bucket, owner);
7c673cae
FG
867 if (r < 0) {
868 set_err_msg(err_msg, "failed to set bucket owner: " + cpp_strerror(-r));
869 return r;
870 }
871
872 // ...and encode the acl
873 aclbl.clear();
874 policy.encode(aclbl);
875
876 r = store->system_obj_set_attr(NULL, obj, RGW_ATTR_ACL, aclbl, &objv_tracker);
224ce89b 877 if (r < 0) {
7c673cae 878 return r;
224ce89b 879 }
7c673cae
FG
880
881 RGWAccessControlPolicy policy_instance;
882 policy_instance.create_default(user_info.user_id, display_name);
883 aclbl.clear();
884 policy_instance.encode(aclbl);
885
886 string oid_bucket_instance = RGW_BUCKET_INSTANCE_MD_PREFIX + key;
887 rgw_raw_obj obj_bucket_instance(root_pool, oid_bucket_instance);
888 r = store->system_obj_set_attr(NULL, obj_bucket_instance, RGW_ATTR_ACL, aclbl, &objv_tracker);
224ce89b
WB
889 if (r < 0) {
890 return r;
891 }
7c673cae 892
3efd9988
FG
893 r = rgw_link_bucket(store, user_info.user_id, bucket_info.bucket,
894 ceph::real_time());
224ce89b 895 if (r < 0) {
7c673cae 896 return r;
224ce89b 897 }
7c673cae
FG
898 }
899
900 return 0;
901}
902
903int RGWBucket::unlink(RGWBucketAdminOpState& op_state, std::string *err_msg)
904{
905 rgw_bucket bucket = op_state.get_bucket();
906
907 if (!op_state.is_user_op()) {
908 set_err_msg(err_msg, "could not fetch user or user bucket info");
909 return -EINVAL;
910 }
911
912 int r = rgw_unlink_bucket(store, user_info.user_id, bucket.tenant, bucket.name);
913 if (r < 0) {
914 set_err_msg(err_msg, "error unlinking bucket" + cpp_strerror(-r));
915 }
916
917 return r;
918}
919
920int RGWBucket::remove(RGWBucketAdminOpState& op_state, bool bypass_gc,
921 bool keep_index_consistent, std::string *err_msg)
922{
923 bool delete_children = op_state.will_delete_children();
924 rgw_bucket bucket = op_state.get_bucket();
925 int ret;
926
927 if (bypass_gc) {
928 if (delete_children) {
929 ret = rgw_remove_bucket_bypass_gc(store, bucket, op_state.get_max_aio(), keep_index_consistent);
930 } else {
931 set_err_msg(err_msg, "purge objects should be set for gc to be bypassed");
932 return -EINVAL;
933 }
934 } else {
935 ret = rgw_remove_bucket(store, bucket, delete_children);
936 }
937
938 if (ret < 0) {
939 set_err_msg(err_msg, "unable to remove bucket" + cpp_strerror(-ret));
940 return ret;
941 }
942
943 return 0;
944}
945
946int RGWBucket::remove_object(RGWBucketAdminOpState& op_state, std::string *err_msg)
947{
948 rgw_bucket bucket = op_state.get_bucket();
949 std::string object_name = op_state.get_object_name();
950
951 rgw_obj_key key(object_name);
952
953 int ret = rgw_remove_object(store, bucket_info, bucket, key);
954 if (ret < 0) {
955 set_err_msg(err_msg, "unable to remove object" + cpp_strerror(-ret));
956 return ret;
957 }
958
959 return 0;
960}
961
962static void dump_bucket_index(map<string, rgw_bucket_dir_entry> result, Formatter *f)
963{
964 map<string, rgw_bucket_dir_entry>::iterator iter;
965 for (iter = result.begin(); iter != result.end(); ++iter) {
966 f->dump_string("object", iter->first);
967 }
968}
969
970static void dump_bucket_usage(map<RGWObjCategory, RGWStorageStats>& stats, Formatter *formatter)
971{
972 map<RGWObjCategory, RGWStorageStats>::iterator iter;
973
974 formatter->open_object_section("usage");
975 for (iter = stats.begin(); iter != stats.end(); ++iter) {
976 RGWStorageStats& s = iter->second;
977 const char *cat_name = rgw_obj_category_name(iter->first);
978 formatter->open_object_section(cat_name);
979 s.dump(formatter);
980 formatter->close_section();
981 }
982 formatter->close_section();
983}
984
985static void dump_index_check(map<RGWObjCategory, RGWStorageStats> existing_stats,
986 map<RGWObjCategory, RGWStorageStats> calculated_stats,
987 Formatter *formatter)
988{
989 formatter->open_object_section("check_result");
990 formatter->open_object_section("existing_header");
991 dump_bucket_usage(existing_stats, formatter);
992 formatter->close_section();
993 formatter->open_object_section("calculated_header");
994 dump_bucket_usage(calculated_stats, formatter);
995 formatter->close_section();
996 formatter->close_section();
997}
998
999int RGWBucket::check_bad_index_multipart(RGWBucketAdminOpState& op_state,
d2e6a577 1000 RGWFormatterFlusher& flusher ,std::string *err_msg)
7c673cae
FG
1001{
1002 bool fix_index = op_state.will_fix_index();
1003 rgw_bucket bucket = op_state.get_bucket();
1004
d2e6a577 1005 size_t max = 1000;
7c673cae
FG
1006
1007 map<string, bool> common_prefixes;
1008
1009 bool is_truncated;
1010 map<string, bool> meta_objs;
1011 map<rgw_obj_index_key, string> all_objs;
1012
1013 RGWBucketInfo bucket_info;
1014 RGWObjectCtx obj_ctx(store);
1015 int r = store->get_bucket_instance_info(obj_ctx, bucket, bucket_info, nullptr, nullptr);
1016 if (r < 0) {
1017 ldout(store->ctx(), 0) << "ERROR: " << __func__ << "(): get_bucket_instance_info(bucket=" << bucket << ") returned r=" << r << dendl;
1018 return r;
1019 }
1020
1021 RGWRados::Bucket target(store, bucket_info);
1022 RGWRados::Bucket::List list_op(&target);
1023
1024 list_op.params.list_versions = true;
224ce89b 1025 list_op.params.ns = RGW_OBJ_NS_MULTIPART;
7c673cae
FG
1026
1027 do {
1028 vector<rgw_bucket_dir_entry> result;
1029 int r = list_op.list_objects(max, &result, &common_prefixes, &is_truncated);
1030 if (r < 0) {
1031 set_err_msg(err_msg, "failed to list objects in bucket=" + bucket.name +
1032 " err=" + cpp_strerror(-r));
1033
1034 return r;
1035 }
1036
1037 vector<rgw_bucket_dir_entry>::iterator iter;
1038 for (iter = result.begin(); iter != result.end(); ++iter) {
31f18b77
FG
1039 rgw_obj_index_key key = iter->key;
1040 rgw_obj obj(bucket, key);
1041 string oid = obj.get_oid();
7c673cae
FG
1042
1043 int pos = oid.find_last_of('.');
1044 if (pos < 0) {
1045 /* obj has no suffix */
1046 all_objs[key] = oid;
1047 } else {
1048 /* obj has suffix */
1049 string name = oid.substr(0, pos);
1050 string suffix = oid.substr(pos + 1);
1051
1052 if (suffix.compare("meta") == 0) {
1053 meta_objs[name] = true;
1054 } else {
1055 all_objs[key] = name;
1056 }
1057 }
1058 }
1059
1060 } while (is_truncated);
1061
d2e6a577
FG
1062 list<rgw_obj_index_key> objs_to_unlink;
1063 Formatter *f = flusher.get_formatter();
1064
1065 f->open_array_section("invalid_multipart_entries");
1066
7c673cae
FG
1067 for (auto aiter = all_objs.begin(); aiter != all_objs.end(); ++aiter) {
1068 string& name = aiter->second;
1069
7c673cae
FG
1070 if (meta_objs.find(name) == meta_objs.end()) {
1071 objs_to_unlink.push_back(aiter->first);
1072 }
7c673cae 1073
d2e6a577
FG
1074 if (objs_to_unlink.size() > max) {
1075 if (fix_index) {
1076 int r = store->remove_objs_from_index(bucket_info, objs_to_unlink);
1077 if (r < 0) {
1078 set_err_msg(err_msg, "ERROR: remove_obj_from_index() returned error: " +
1079 cpp_strerror(-r));
1080 return r;
1081 }
1082 }
1083
1084 dump_mulipart_index_results(objs_to_unlink, flusher.get_formatter());
1085 flusher.flush();
1086 objs_to_unlink.clear();
1087 }
1088 }
7c673cae
FG
1089
1090 if (fix_index) {
1091 int r = store->remove_objs_from_index(bucket_info, objs_to_unlink);
1092 if (r < 0) {
1093 set_err_msg(err_msg, "ERROR: remove_obj_from_index() returned error: " +
1094 cpp_strerror(-r));
1095
1096 return r;
1097 }
1098 }
1099
d2e6a577
FG
1100 dump_mulipart_index_results(objs_to_unlink, f);
1101 f->close_section();
1102 flusher.flush();
1103
7c673cae
FG
1104 return 0;
1105}
1106
1107int RGWBucket::check_object_index(RGWBucketAdminOpState& op_state,
1108 RGWFormatterFlusher& flusher,
1109 std::string *err_msg)
1110{
1111
1112 bool fix_index = op_state.will_fix_index();
1113
1114 rgw_bucket bucket = op_state.get_bucket();
1115
1116 if (!fix_index) {
1117 set_err_msg(err_msg, "check-objects flag requires fix index enabled");
1118 return -EINVAL;
1119 }
1120
1121 store->cls_obj_set_bucket_tag_timeout(bucket_info, BUCKET_TAG_TIMEOUT);
1122
1123 string prefix;
1124 rgw_obj_index_key marker;
1125 bool is_truncated = true;
1126
1127 Formatter *formatter = flusher.get_formatter();
1128 formatter->open_object_section("objects");
1129 while (is_truncated) {
1130 map<string, rgw_bucket_dir_entry> result;
1131
1132 int r = store->cls_bucket_list(bucket_info, RGW_NO_SHARD, marker, prefix, 1000, true,
1133 result, &is_truncated, &marker,
1134 bucket_object_check_filter);
1135 if (r == -ENOENT) {
1136 break;
1137 } else if (r < 0 && r != -ENOENT) {
1138 set_err_msg(err_msg, "ERROR: failed operation r=" + cpp_strerror(-r));
1139 }
1140
1141
1142 dump_bucket_index(result, formatter);
1143 flusher.flush();
1144
1145 }
1146
1147 formatter->close_section();
1148
1149 store->cls_obj_set_bucket_tag_timeout(bucket_info, 0);
1150
1151 return 0;
1152}
1153
1154
1155int RGWBucket::check_index(RGWBucketAdminOpState& op_state,
1156 map<RGWObjCategory, RGWStorageStats>& existing_stats,
1157 map<RGWObjCategory, RGWStorageStats>& calculated_stats,
1158 std::string *err_msg)
1159{
1160 rgw_bucket bucket = op_state.get_bucket();
1161 bool fix_index = op_state.will_fix_index();
1162
1163 int r = store->bucket_check_index(bucket_info, &existing_stats, &calculated_stats);
1164 if (r < 0) {
1165 set_err_msg(err_msg, "failed to check index error=" + cpp_strerror(-r));
1166 return r;
1167 }
1168
1169 if (fix_index) {
1170 r = store->bucket_rebuild_index(bucket_info);
1171 if (r < 0) {
1172 set_err_msg(err_msg, "failed to rebuild index err=" + cpp_strerror(-r));
1173 return r;
1174 }
1175 }
1176
1177 return 0;
1178}
1179
1180
1181int RGWBucket::policy_bl_to_stream(bufferlist& bl, ostream& o)
1182{
1183 RGWAccessControlPolicy_S3 policy(g_ceph_context);
1184 bufferlist::iterator iter = bl.begin();
1185 try {
1186 policy.decode(iter);
1187 } catch (buffer::error& err) {
1188 dout(0) << "ERROR: caught buffer::error, could not decode policy" << dendl;
1189 return -EIO;
1190 }
1191 policy.to_xml(o);
1192 return 0;
1193}
1194
1195static int policy_decode(RGWRados *store, bufferlist& bl, RGWAccessControlPolicy& policy)
1196{
1197 bufferlist::iterator iter = bl.begin();
1198 try {
1199 policy.decode(iter);
1200 } catch (buffer::error& err) {
1201 ldout(store->ctx(), 0) << "ERROR: caught buffer::error, could not decode policy" << dendl;
1202 return -EIO;
1203 }
1204 return 0;
1205}
1206
1207int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, RGWAccessControlPolicy& policy)
1208{
1209 std::string object_name = op_state.get_object_name();
1210 rgw_bucket bucket = op_state.get_bucket();
1211 RGWObjectCtx obj_ctx(store);
1212
1213 RGWBucketInfo bucket_info;
1214 map<string, bufferlist> attrs;
1215 int ret = store->get_bucket_info(obj_ctx, bucket.tenant, bucket.name, bucket_info, NULL, &attrs);
1216 if (ret < 0) {
1217 return ret;
1218 }
1219
1220 if (!object_name.empty()) {
1221 bufferlist bl;
1222 rgw_obj obj(bucket, object_name);
1223
1224 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
1225 RGWRados::Object::Read rop(&op_target);
1226
1227 int ret = rop.get_attr(RGW_ATTR_ACL, bl);
1228 if (ret < 0)
1229 return ret;
1230
1231 return policy_decode(store, bl, policy);
1232 }
1233
1234 map<string, bufferlist>::iterator aiter = attrs.find(RGW_ATTR_ACL);
1235 if (aiter == attrs.end()) {
1236 return -ENOENT;
1237 }
1238
1239 return policy_decode(store, aiter->second, policy);
1240}
1241
1242
1243int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state,
1244 RGWAccessControlPolicy& policy)
1245{
1246 RGWBucket bucket;
1247
1248 int ret = bucket.init(store, op_state);
1249 if (ret < 0)
1250 return ret;
1251
1252 ret = bucket.get_policy(op_state, policy);
1253 if (ret < 0)
1254 return ret;
1255
1256 return 0;
1257}
1258
1259/* Wrappers to facilitate RESTful interface */
1260
1261
1262int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state,
1263 RGWFormatterFlusher& flusher)
1264{
1265 RGWAccessControlPolicy policy(store->ctx());
1266
1267 int ret = get_policy(store, op_state, policy);
1268 if (ret < 0)
1269 return ret;
1270
1271 Formatter *formatter = flusher.get_formatter();
1272
1273 flusher.start(0);
1274
1275 formatter->open_object_section("policy");
1276 policy.dump(formatter);
1277 formatter->close_section();
1278
1279 flusher.flush();
1280
1281 return 0;
1282}
1283
1284int RGWBucketAdminOp::dump_s3_policy(RGWRados *store, RGWBucketAdminOpState& op_state,
1285 ostream& os)
1286{
1287 RGWAccessControlPolicy_S3 policy(store->ctx());
1288
1289 int ret = get_policy(store, op_state, policy);
1290 if (ret < 0)
1291 return ret;
1292
1293 policy.to_xml(os);
1294
1295 return 0;
1296}
1297
1298int RGWBucketAdminOp::unlink(RGWRados *store, RGWBucketAdminOpState& op_state)
1299{
1300 RGWBucket bucket;
1301
1302 int ret = bucket.init(store, op_state);
1303 if (ret < 0)
1304 return ret;
1305
1306 return bucket.unlink(op_state);
1307}
1308
1309int RGWBucketAdminOp::link(RGWRados *store, RGWBucketAdminOpState& op_state, string *err)
1310{
1311 RGWBucket bucket;
1312
1313 int ret = bucket.init(store, op_state);
1314 if (ret < 0)
1315 return ret;
1316
1317 return bucket.link(op_state, err);
1318
1319}
1320
1321int RGWBucketAdminOp::check_index(RGWRados *store, RGWBucketAdminOpState& op_state,
1322 RGWFormatterFlusher& flusher)
1323{
1324 int ret;
7c673cae
FG
1325 map<RGWObjCategory, RGWStorageStats> existing_stats;
1326 map<RGWObjCategory, RGWStorageStats> calculated_stats;
d2e6a577 1327
7c673cae
FG
1328
1329 RGWBucket bucket;
1330
1331 ret = bucket.init(store, op_state);
1332 if (ret < 0)
1333 return ret;
1334
1335 Formatter *formatter = flusher.get_formatter();
1336 flusher.start(0);
1337
d2e6a577 1338 ret = bucket.check_bad_index_multipart(op_state, flusher);
7c673cae
FG
1339 if (ret < 0)
1340 return ret;
1341
7c673cae
FG
1342 ret = bucket.check_object_index(op_state, flusher);
1343 if (ret < 0)
1344 return ret;
1345
1346 ret = bucket.check_index(op_state, existing_stats, calculated_stats);
1347 if (ret < 0)
1348 return ret;
1349
1350 dump_index_check(existing_stats, calculated_stats, formatter);
1351 flusher.flush();
1352
1353 return 0;
1354}
1355
1356int RGWBucketAdminOp::remove_bucket(RGWRados *store, RGWBucketAdminOpState& op_state,
1357 bool bypass_gc, bool keep_index_consistent)
1358{
1359 RGWBucket bucket;
1360
1361 int ret = bucket.init(store, op_state);
1362 if (ret < 0)
1363 return ret;
1364
c07f9fc5
FG
1365 std::string err_msg;
1366 ret = bucket.remove(op_state, bypass_gc, keep_index_consistent, &err_msg);
1367 if (!err_msg.empty()) {
1368 lderr(store->ctx()) << "ERROR: " << err_msg << dendl;
1369 }
1370 return ret;
7c673cae
FG
1371}
1372
1373int RGWBucketAdminOp::remove_object(RGWRados *store, RGWBucketAdminOpState& op_state)
1374{
1375 RGWBucket bucket;
1376
1377 int ret = bucket.init(store, op_state);
1378 if (ret < 0)
1379 return ret;
1380
1381 return bucket.remove_object(op_state);
1382}
1383
1384static int bucket_stats(RGWRados *store, const std::string& tenant_name, std::string& bucket_name, Formatter *formatter)
1385{
1386 RGWBucketInfo bucket_info;
1387 map<RGWObjCategory, RGWStorageStats> stats;
1388
1389 real_time mtime;
1390 RGWObjectCtx obj_ctx(store);
1391 int r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, &mtime);
1392 if (r < 0)
1393 return r;
1394
1395 rgw_bucket& bucket = bucket_info.bucket;
1396
1397 string bucket_ver, master_ver;
1398 string max_marker;
1399 int ret = store->get_bucket_stats(bucket_info, RGW_NO_SHARD, &bucket_ver, &master_ver, stats, &max_marker);
1400 if (ret < 0) {
1401 cerr << "error getting bucket stats ret=" << ret << std::endl;
1402 return ret;
1403 }
1404
1405 utime_t ut(mtime);
1406
1407 formatter->open_object_section("stats");
1408 formatter->dump_string("bucket", bucket.name);
31f18b77
FG
1409 formatter->dump_string("zonegroup", bucket_info.zonegroup);
1410 formatter->dump_string("placement_rule", bucket_info.placement_rule);
1411 ::encode_json("explicit_placement", bucket.explicit_placement, formatter);
7c673cae
FG
1412 formatter->dump_string("id", bucket.bucket_id);
1413 formatter->dump_string("marker", bucket.marker);
1414 formatter->dump_stream("index_type") << bucket_info.index_type;
1415 ::encode_json("owner", bucket_info.owner, formatter);
1416 formatter->dump_string("ver", bucket_ver);
1417 formatter->dump_string("master_ver", master_ver);
1418 formatter->dump_stream("mtime") << ut;
1419 formatter->dump_string("max_marker", max_marker);
1420 dump_bucket_usage(stats, formatter);
1421 encode_json("bucket_quota", bucket_info.quota, formatter);
1422 formatter->close_section();
1423
1424 return 0;
1425}
1426
1427int RGWBucketAdminOp::limit_check(RGWRados *store,
1428 RGWBucketAdminOpState& op_state,
1429 const std::list<std::string>& user_ids,
1430 RGWFormatterFlusher& flusher,
1431 bool warnings_only)
1432{
1433 int ret = 0;
1434 const size_t max_entries =
1435 store->ctx()->_conf->rgw_list_buckets_max_chunk;
1436
1437 const size_t safe_max_objs_per_shard =
1438 store->ctx()->_conf->rgw_safe_max_objects_per_shard;
1439
1440 uint16_t shard_warn_pct =
1441 store->ctx()->_conf->rgw_shard_warning_threshold;
1442 if (shard_warn_pct > 100)
1443 shard_warn_pct = 90;
1444
1445 Formatter *formatter = flusher.get_formatter();
1446 flusher.start(0);
1447
1448 formatter->open_array_section("users");
1449
1450 for (const auto& user_id : user_ids) {
1451 formatter->open_object_section("user");
1452 formatter->dump_string("user_id", user_id);
1453 bool done;
1454 formatter->open_array_section("buckets");
1455 do {
1456 RGWUserBuckets buckets;
1457 string marker;
1458 bool is_truncated;
1459
1460 ret = rgw_read_user_buckets(store, user_id, buckets,
1461 marker, string(), max_entries, false,
1462 &is_truncated);
1463 if (ret < 0)
1464 return ret;
1465
1466 map<string, RGWBucketEnt>& m_buckets = buckets.get_buckets();
1467
1468 for (const auto& iter : m_buckets) {
1469 auto& bucket = iter.second.bucket;
1470 uint32_t num_shards = 1;
1471 uint64_t num_objects = 0;
1472
1473 /* need info for num_shards */
1474 RGWBucketInfo info;
1475 RGWObjectCtx obj_ctx(store);
1476
1477 marker = bucket.name; /* Casey's location for marker update,
1478 * as we may now not reach the end of
1479 * the loop body */
1480
1481 ret = store->get_bucket_info(obj_ctx, bucket.tenant, bucket.name,
1482 info, nullptr);
1483 if (ret < 0)
1484 continue;
1485
1486 /* need stats for num_entries */
1487 string bucket_ver, master_ver;
1488 std::map<RGWObjCategory, RGWStorageStats> stats;
1489 ret = store->get_bucket_stats(info, RGW_NO_SHARD, &bucket_ver,
1490 &master_ver, stats, nullptr);
1491
1492 if (ret < 0)
1493 continue;
1494
1495 for (const auto& s : stats) {
1496 num_objects += s.second.num_objects;
1497 }
1498
1499 num_shards = info.num_shards;
31f18b77
FG
1500 uint64_t objs_per_shard =
1501 (num_shards) ? num_objects/num_shards : num_objects;
7c673cae
FG
1502 {
1503 bool warn = false;
1504 stringstream ss;
1505 if (objs_per_shard > safe_max_objs_per_shard) {
1506 double over =
1507 100 - (safe_max_objs_per_shard/objs_per_shard * 100);
1508 ss << boost::format("OVER %4f%%") % over;
1509 warn = true;
1510 } else {
1511 double fill_pct =
1512 objs_per_shard / safe_max_objs_per_shard * 100;
1513 if (fill_pct >= shard_warn_pct) {
1514 ss << boost::format("WARN %4f%%") % fill_pct;
1515 warn = true;
1516 } else {
1517 ss << "OK";
1518 }
1519 }
1520
1521 if (warn || (! warnings_only)) {
1522 formatter->open_object_section("bucket");
1523 formatter->dump_string("bucket", bucket.name);
1524 formatter->dump_string("tenant", bucket.tenant);
1525 formatter->dump_int("num_objects", num_objects);
1526 formatter->dump_int("num_shards", num_shards);
1527 formatter->dump_int("objects_per_shard", objs_per_shard);
1528 formatter->dump_string("fill_status", ss.str());
1529 formatter->close_section();
1530 }
1531 }
1532 }
1533
1534 done = (m_buckets.size() < max_entries);
1535 } while (!done); /* foreach: bucket */
1536
1537 formatter->close_section();
1538 formatter->close_section();
1539 formatter->flush(cout);
1540
1541 } /* foreach: user_id */
1542
1543 formatter->close_section();
1544 formatter->flush(cout);
1545
1546 return ret;
1547} /* RGWBucketAdminOp::limit_check */
1548
1549int RGWBucketAdminOp::info(RGWRados *store, RGWBucketAdminOpState& op_state,
1550 RGWFormatterFlusher& flusher)
1551{
1552 RGWBucket bucket;
1553 int ret;
1554
1555 string bucket_name = op_state.get_bucket_name();
1556
1557 if (!bucket_name.empty()) {
1558 ret = bucket.init(store, op_state);
1559 if (ret < 0)
1560 return ret;
1561 }
1562
1563 Formatter *formatter = flusher.get_formatter();
1564 flusher.start(0);
1565
1566 CephContext *cct = store->ctx();
1567
1568 const size_t max_entries = cct->_conf->rgw_list_buckets_max_chunk;
1569
1570 bool show_stats = op_state.will_fetch_stats();
1571 rgw_user user_id = op_state.get_user_id();
1572 if (op_state.is_user_op()) {
1573 formatter->open_array_section("buckets");
1574
1575 RGWUserBuckets buckets;
1576 string marker;
1577 bool is_truncated = false;
1578
1579 do {
1580 ret = rgw_read_user_buckets(store, op_state.get_user_id(), buckets,
1581 marker, string(), max_entries, false,
1582 &is_truncated);
1583 if (ret < 0)
1584 return ret;
1585
1586 map<string, RGWBucketEnt>& m = buckets.get_buckets();
1587 map<string, RGWBucketEnt>::iterator iter;
1588
1589 for (iter = m.begin(); iter != m.end(); ++iter) {
1590 std::string obj_name = iter->first;
1591 if (show_stats)
1592 bucket_stats(store, user_id.tenant, obj_name, formatter);
1593 else
1594 formatter->dump_string("bucket", obj_name);
1595
1596 marker = obj_name;
1597 }
1598
1599 flusher.flush();
1600 } while (is_truncated);
1601
1602 formatter->close_section();
1603 } else if (!bucket_name.empty()) {
1604 bucket_stats(store, user_id.tenant, bucket_name, formatter);
1605 } else {
1606 RGWAccessHandle handle;
1607
1608 formatter->open_array_section("buckets");
1609 if (store->list_buckets_init(&handle) >= 0) {
1610 rgw_bucket_dir_entry obj;
1611 while (store->list_buckets_next(obj, &handle) >= 0) {
1612 if (show_stats)
1613 bucket_stats(store, user_id.tenant, obj.key.name, formatter);
1614 else
1615 formatter->dump_string("bucket", obj.key.name);
1616 }
1617 }
1618
1619 formatter->close_section();
1620 }
1621
1622 flusher.flush();
1623
1624 return 0;
1625}
1626
1627
1628void rgw_data_change::dump(Formatter *f) const
1629{
1630 string type;
1631 switch (entity_type) {
1632 case ENTITY_TYPE_BUCKET:
1633 type = "bucket";
1634 break;
1635 default:
1636 type = "unknown";
1637 }
1638 encode_json("entity_type", type, f);
1639 encode_json("key", key, f);
1640 utime_t ut(timestamp);
1641 encode_json("timestamp", ut, f);
1642}
1643
1644void rgw_data_change::decode_json(JSONObj *obj) {
1645 string s;
1646 JSONDecoder::decode_json("entity_type", s, obj);
1647 if (s == "bucket") {
1648 entity_type = ENTITY_TYPE_BUCKET;
1649 } else {
1650 entity_type = ENTITY_TYPE_UNKNOWN;
1651 }
1652 JSONDecoder::decode_json("key", key, obj);
1653 utime_t ut;
1654 JSONDecoder::decode_json("timestamp", ut, obj);
1655 timestamp = ut.to_real_time();
1656}
1657
1658void rgw_data_change_log_entry::dump(Formatter *f) const
1659{
1660 encode_json("log_id", log_id, f);
1661 utime_t ut(log_timestamp);
1662 encode_json("log_timestamp", ut, f);
1663 encode_json("entry", entry, f);
1664}
1665
1666void rgw_data_change_log_entry::decode_json(JSONObj *obj) {
1667 JSONDecoder::decode_json("log_id", log_id, obj);
1668 utime_t ut;
1669 JSONDecoder::decode_json("log_timestamp", ut, obj);
1670 log_timestamp = ut.to_real_time();
1671 JSONDecoder::decode_json("entry", entry, obj);
1672}
1673
1674int RGWDataChangesLog::choose_oid(const rgw_bucket_shard& bs) {
1675 const string& name = bs.bucket.name;
1676 int shard_shift = (bs.shard_id > 0 ? bs.shard_id : 0);
1677 uint32_t r = (ceph_str_hash_linux(name.c_str(), name.size()) + shard_shift) % num_shards;
1678
1679 return (int)r;
1680}
1681
1682int RGWDataChangesLog::renew_entries()
1683{
1684 if (!store->need_to_log_data())
1685 return 0;
1686
1687 /* we can't keep the bucket name as part of the cls_log_entry, and we need
1688 * it later, so we keep two lists under the map */
1689 map<int, pair<list<rgw_bucket_shard>, list<cls_log_entry> > > m;
1690
1691 lock.Lock();
1692 map<rgw_bucket_shard, bool> entries;
1693 entries.swap(cur_cycle);
1694 lock.Unlock();
1695
1696 map<rgw_bucket_shard, bool>::iterator iter;
1697 string section;
1698 real_time ut = real_clock::now();
1699 for (iter = entries.begin(); iter != entries.end(); ++iter) {
1700 const rgw_bucket_shard& bs = iter->first;
1701
1702 int index = choose_oid(bs);
1703
1704 cls_log_entry entry;
1705
1706 rgw_data_change change;
1707 bufferlist bl;
1708 change.entity_type = ENTITY_TYPE_BUCKET;
1709 change.key = bs.get_key();
1710 change.timestamp = ut;
1711 ::encode(change, bl);
1712
1713 store->time_log_prepare_entry(entry, ut, section, change.key, bl);
1714
1715 m[index].first.push_back(bs);
1716 m[index].second.emplace_back(std::move(entry));
1717 }
1718
1719 map<int, pair<list<rgw_bucket_shard>, list<cls_log_entry> > >::iterator miter;
1720 for (miter = m.begin(); miter != m.end(); ++miter) {
1721 list<cls_log_entry>& entries = miter->second.second;
1722
1723 real_time now = real_clock::now();
1724
1725 int ret = store->time_log_add(oids[miter->first], entries, NULL);
1726 if (ret < 0) {
1727 /* we don't really need to have a special handling for failed cases here,
1728 * as this is just an optimization. */
1729 lderr(cct) << "ERROR: store->time_log_add() returned " << ret << dendl;
1730 return ret;
1731 }
1732
1733 real_time expiration = now;
1734 expiration += make_timespan(cct->_conf->rgw_data_log_window);
1735
1736 list<rgw_bucket_shard>& buckets = miter->second.first;
1737 list<rgw_bucket_shard>::iterator liter;
1738 for (liter = buckets.begin(); liter != buckets.end(); ++liter) {
1739 update_renewed(*liter, expiration);
1740 }
1741 }
1742
1743 return 0;
1744}
1745
1746void RGWDataChangesLog::_get_change(const rgw_bucket_shard& bs, ChangeStatusPtr& status)
1747{
1748 assert(lock.is_locked());
1749 if (!changes.find(bs, status)) {
1750 status = ChangeStatusPtr(new ChangeStatus);
1751 changes.add(bs, status);
1752 }
1753}
1754
1755void RGWDataChangesLog::register_renew(rgw_bucket_shard& bs)
1756{
1757 Mutex::Locker l(lock);
1758 cur_cycle[bs] = true;
1759}
1760
1761void RGWDataChangesLog::update_renewed(rgw_bucket_shard& bs, real_time& expiration)
1762{
1763 Mutex::Locker l(lock);
1764 ChangeStatusPtr status;
1765 _get_change(bs, status);
1766
1767 ldout(cct, 20) << "RGWDataChangesLog::update_renewd() bucket_name=" << bs.bucket.name << " shard_id=" << bs.shard_id << " expiration=" << expiration << dendl;
1768 status->cur_expiration = expiration;
1769}
1770
1771int RGWDataChangesLog::get_log_shard_id(rgw_bucket& bucket, int shard_id) {
1772 rgw_bucket_shard bs(bucket, shard_id);
1773
1774 return choose_oid(bs);
1775}
1776
1777int RGWDataChangesLog::add_entry(rgw_bucket& bucket, int shard_id) {
1778 if (!store->need_to_log_data())
1779 return 0;
1780
1781 rgw_bucket_shard bs(bucket, shard_id);
1782
1783 int index = choose_oid(bs);
1784 mark_modified(index, bs);
1785
1786 lock.Lock();
1787
1788 ChangeStatusPtr status;
1789 _get_change(bs, status);
1790
1791 lock.Unlock();
1792
1793 real_time now = real_clock::now();
1794
1795 status->lock->Lock();
1796
1797 ldout(cct, 20) << "RGWDataChangesLog::add_entry() bucket.name=" << bucket.name << " shard_id=" << shard_id << " now=" << now << " cur_expiration=" << status->cur_expiration << dendl;
1798
1799 if (now < status->cur_expiration) {
1800 /* no need to send, recently completed */
1801 status->lock->Unlock();
1802
1803 register_renew(bs);
1804 return 0;
1805 }
1806
1807 RefCountedCond *cond;
1808
1809 if (status->pending) {
1810 cond = status->cond;
1811
1812 assert(cond);
1813
1814 status->cond->get();
1815 status->lock->Unlock();
1816
1817 int ret = cond->wait();
1818 cond->put();
1819 if (!ret) {
1820 register_renew(bs);
1821 }
1822 return ret;
1823 }
1824
1825 status->cond = new RefCountedCond;
1826 status->pending = true;
1827
1828 string& oid = oids[index];
1829 real_time expiration;
1830
1831 int ret;
1832
1833 do {
1834 status->cur_sent = now;
1835
1836 expiration = now;
1837 expiration += ceph::make_timespan(cct->_conf->rgw_data_log_window);
1838
1839 status->lock->Unlock();
1840
1841 bufferlist bl;
1842 rgw_data_change change;
1843 change.entity_type = ENTITY_TYPE_BUCKET;
1844 change.key = bs.get_key();
1845 change.timestamp = now;
1846 ::encode(change, bl);
1847 string section;
1848
1849 ldout(cct, 20) << "RGWDataChangesLog::add_entry() sending update with now=" << now << " cur_expiration=" << expiration << dendl;
1850
1851 ret = store->time_log_add(oid, now, section, change.key, bl);
1852
1853 now = real_clock::now();
1854
1855 status->lock->Lock();
1856
1857 } while (!ret && real_clock::now() > expiration);
1858
1859 cond = status->cond;
1860
1861 status->pending = false;
1862 status->cur_expiration = status->cur_sent; /* time of when operation started, not completed */
1863 status->cur_expiration += make_timespan(cct->_conf->rgw_data_log_window);
1864 status->cond = NULL;
1865 status->lock->Unlock();
1866
1867 cond->done(ret);
1868 cond->put();
1869
1870 return ret;
1871}
1872
1873int RGWDataChangesLog::list_entries(int shard, const real_time& start_time, const real_time& end_time, int max_entries,
1874 list<rgw_data_change_log_entry>& entries,
1875 const string& marker,
1876 string *out_marker,
1877 bool *truncated) {
31f18b77
FG
1878 if (shard >= num_shards)
1879 return -EINVAL;
7c673cae
FG
1880
1881 list<cls_log_entry> log_entries;
1882
1883 int ret = store->time_log_list(oids[shard], start_time, end_time,
1884 max_entries, log_entries, marker,
1885 out_marker, truncated);
1886 if (ret < 0)
1887 return ret;
1888
1889 list<cls_log_entry>::iterator iter;
1890 for (iter = log_entries.begin(); iter != log_entries.end(); ++iter) {
1891 rgw_data_change_log_entry log_entry;
1892 log_entry.log_id = iter->id;
1893 real_time rt = iter->timestamp.to_real_time();
1894 log_entry.log_timestamp = rt;
1895 bufferlist::iterator liter = iter->data.begin();
1896 try {
1897 ::decode(log_entry.entry, liter);
1898 } catch (buffer::error& err) {
1899 lderr(cct) << "ERROR: failed to decode data changes log entry" << dendl;
1900 return -EIO;
1901 }
1902 entries.push_back(log_entry);
1903 }
1904
1905 return 0;
1906}
1907
1908int RGWDataChangesLog::list_entries(const real_time& start_time, const real_time& end_time, int max_entries,
1909 list<rgw_data_change_log_entry>& entries, LogMarker& marker, bool *ptruncated) {
1910 bool truncated;
1911 entries.clear();
1912
1913 for (; marker.shard < num_shards && (int)entries.size() < max_entries;
1914 marker.shard++, marker.marker.clear()) {
1915 int ret = list_entries(marker.shard, start_time, end_time, max_entries - entries.size(), entries,
1916 marker.marker, NULL, &truncated);
1917 if (ret == -ENOENT) {
1918 continue;
1919 }
1920 if (ret < 0) {
1921 return ret;
1922 }
1923 if (truncated) {
1924 *ptruncated = true;
1925 return 0;
1926 }
1927 }
1928
1929 *ptruncated = (marker.shard < num_shards);
1930
1931 return 0;
1932}
1933
1934int RGWDataChangesLog::get_info(int shard_id, RGWDataChangesLogInfo *info)
1935{
1936 if (shard_id >= num_shards)
1937 return -EINVAL;
1938
1939 string oid = oids[shard_id];
1940
1941 cls_log_header header;
1942
1943 int ret = store->time_log_info(oid, &header);
1944 if ((ret < 0) && (ret != -ENOENT))
1945 return ret;
1946
1947 info->marker = header.max_marker;
1948 info->last_update = header.max_time.to_real_time();
1949
1950 return 0;
1951}
1952
1953int RGWDataChangesLog::trim_entries(int shard_id, const real_time& start_time, const real_time& end_time,
1954 const string& start_marker, const string& end_marker)
1955{
1956 int ret;
1957
1958 if (shard_id > num_shards)
1959 return -EINVAL;
1960
1961 ret = store->time_log_trim(oids[shard_id], start_time, end_time, start_marker, end_marker);
1962
31f18b77 1963 if (ret == -ENOENT || ret == -ENODATA)
7c673cae
FG
1964 ret = 0;
1965
1966 return ret;
1967}
1968
1969int RGWDataChangesLog::trim_entries(const real_time& start_time, const real_time& end_time,
1970 const string& start_marker, const string& end_marker)
1971{
1972 for (int shard = 0; shard < num_shards; shard++) {
1973 int ret = store->time_log_trim(oids[shard], start_time, end_time, start_marker, end_marker);
31f18b77 1974 if (ret == -ENOENT || ret == -ENODATA) {
7c673cae
FG
1975 continue;
1976 }
1977 if (ret < 0)
1978 return ret;
1979 }
1980
1981 return 0;
1982}
1983
1984bool RGWDataChangesLog::going_down()
1985{
1986 return down_flag;
1987}
1988
1989RGWDataChangesLog::~RGWDataChangesLog() {
1990 down_flag = true;
1991 renew_thread->stop();
1992 renew_thread->join();
1993 delete renew_thread;
1994 delete[] oids;
1995}
1996
1997void *RGWDataChangesLog::ChangesRenewThread::entry() {
1998 do {
1999 dout(2) << "RGWDataChangesLog::ChangesRenewThread: start" << dendl;
2000 int r = log->renew_entries();
2001 if (r < 0) {
2002 dout(0) << "ERROR: RGWDataChangesLog::renew_entries returned error r=" << r << dendl;
2003 }
2004
2005 if (log->going_down())
2006 break;
2007
2008 int interval = cct->_conf->rgw_data_log_window * 3 / 4;
2009 lock.Lock();
2010 cond.WaitInterval(lock, utime_t(interval, 0));
2011 lock.Unlock();
2012 } while (!log->going_down());
2013
2014 return NULL;
2015}
2016
2017void RGWDataChangesLog::ChangesRenewThread::stop()
2018{
2019 Mutex::Locker l(lock);
2020 cond.Signal();
2021}
2022
2023void RGWDataChangesLog::mark_modified(int shard_id, const rgw_bucket_shard& bs)
2024{
2025 auto key = bs.get_key();
2026 modified_lock.get_read();
2027 map<int, set<string> >::iterator iter = modified_shards.find(shard_id);
2028 if (iter != modified_shards.end()) {
2029 set<string>& keys = iter->second;
2030 if (keys.find(key) != keys.end()) {
2031 modified_lock.unlock();
2032 return;
2033 }
2034 }
2035 modified_lock.unlock();
2036
2037 RWLock::WLocker wl(modified_lock);
2038 modified_shards[shard_id].insert(key);
2039}
2040
2041void RGWDataChangesLog::read_clear_modified(map<int, set<string> > &modified)
2042{
2043 RWLock::WLocker wl(modified_lock);
2044 modified.swap(modified_shards);
2045 modified_shards.clear();
2046}
2047
2048void RGWBucketCompleteInfo::dump(Formatter *f) const {
2049 encode_json("bucket_info", info, f);
2050 encode_json("attrs", attrs, f);
2051}
2052
2053void RGWBucketCompleteInfo::decode_json(JSONObj *obj) {
2054 JSONDecoder::decode_json("bucket_info", info, obj);
2055 JSONDecoder::decode_json("attrs", attrs, obj);
2056}
2057
2058class RGWBucketMetadataHandler : public RGWMetadataHandler {
2059
2060public:
2061 string get_type() override { return "bucket"; }
2062
2063 int get(RGWRados *store, string& entry, RGWMetadataObject **obj) override {
2064 RGWObjVersionTracker ot;
2065 RGWBucketEntryPoint be;
2066
2067 real_time mtime;
2068 map<string, bufferlist> attrs;
2069 RGWObjectCtx obj_ctx(store);
2070
2071 string tenant_name, bucket_name;
2072 parse_bucket(entry, &tenant_name, &bucket_name);
2073 int ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, be, &ot, &mtime, &attrs);
2074 if (ret < 0)
2075 return ret;
2076
2077 RGWBucketEntryMetadataObject *mdo = new RGWBucketEntryMetadataObject(be, ot.read_version, mtime);
2078
2079 *obj = mdo;
2080
2081 return 0;
2082 }
2083
2084 int put(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker,
2085 real_time mtime, JSONObj *obj, sync_type_t sync_type) override {
2086 RGWBucketEntryPoint be, old_be;
2087 try {
2088 decode_json_obj(be, obj);
2089 } catch (JSONDecoder::err& e) {
2090 return -EINVAL;
2091 }
2092
2093 real_time orig_mtime;
2094 map<string, bufferlist> attrs;
2095
2096 RGWObjVersionTracker old_ot;
2097 RGWObjectCtx obj_ctx(store);
2098
2099 string tenant_name, bucket_name;
2100 parse_bucket(entry, &tenant_name, &bucket_name);
2101 int ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, old_be, &old_ot, &orig_mtime, &attrs);
2102 if (ret < 0 && ret != -ENOENT)
2103 return ret;
2104
2105 // are we actually going to perform this put, or is it too old?
2106 if (ret != -ENOENT &&
2107 !check_versions(old_ot.read_version, orig_mtime,
2108 objv_tracker.write_version, mtime, sync_type)) {
2109 return STATUS_NO_APPLY;
2110 }
2111
2112 objv_tracker.read_version = old_ot.read_version; /* maintain the obj version we just read */
2113
2114 ret = store->put_bucket_entrypoint_info(tenant_name, bucket_name, be, false, objv_tracker, mtime, &attrs);
2115 if (ret < 0)
2116 return ret;
2117
2118 /* link bucket */
2119 if (be.linked) {
2120 ret = rgw_link_bucket(store, be.owner, be.bucket, be.creation_time, false);
2121 } else {
3efd9988
FG
2122 ret = rgw_unlink_bucket(store, be.owner, be.bucket.tenant,
2123 be.bucket.name, false);
7c673cae
FG
2124 }
2125
2126 return ret;
2127 }
2128
2129 struct list_keys_info {
2130 RGWRados *store;
2131 RGWListRawObjsCtx ctx;
2132 };
2133
2134 int remove(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker) override {
2135 RGWBucketEntryPoint be;
2136 RGWObjectCtx obj_ctx(store);
2137
2138 string tenant_name, bucket_name;
2139 parse_bucket(entry, &tenant_name, &bucket_name);
2140 int ret = store->get_bucket_entrypoint_info(obj_ctx, tenant_name, bucket_name, be, &objv_tracker, NULL, NULL);
2141 if (ret < 0)
2142 return ret;
2143
2144 /*
31f18b77 2145 * We're unlinking the bucket but we don't want to update the entrypoint here - we're removing
7c673cae
FG
2146 * it immediately and don't want to invalidate our cached objv_version or the bucket obj removal
2147 * will incorrectly fail.
2148 */
2149 ret = rgw_unlink_bucket(store, be.owner, tenant_name, bucket_name, false);
2150 if (ret < 0) {
2151 lderr(store->ctx()) << "could not unlink bucket=" << entry << " owner=" << be.owner << dendl;
2152 }
2153
2154 ret = rgw_bucket_delete_bucket_obj(store, tenant_name, bucket_name, objv_tracker);
2155 if (ret < 0) {
2156 lderr(store->ctx()) << "could not delete bucket=" << entry << dendl;
2157 }
2158 /* idempotent */
2159 return 0;
2160 }
2161
2162 void get_pool_and_oid(RGWRados *store, const string& key, rgw_pool& pool, string& oid) override {
2163 oid = key;
2164 pool = store->get_zone_params().domain_root;
2165 }
2166
181888fb
FG
2167 int list_keys_init(RGWRados *store, const string& marker, void **phandle) override {
2168 auto info = ceph::make_unique<list_keys_info>();
7c673cae
FG
2169
2170 info->store = store;
2171
181888fb
FG
2172 int ret = store->list_raw_objects_init(store->get_zone_params().domain_root, marker,
2173 &info->ctx);
2174 if (ret < 0) {
2175 return ret;
2176 }
2177 *phandle = (void *)info.release();
7c673cae
FG
2178
2179 return 0;
2180 }
2181
2182 int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated) override {
2183 list_keys_info *info = static_cast<list_keys_info *>(handle);
2184
2185 string no_filter;
2186
2187 keys.clear();
2188
2189 RGWRados *store = info->store;
2190
2191 list<string> unfiltered_keys;
2192
181888fb
FG
2193 int ret = store->list_raw_objects_next(no_filter, max, info->ctx,
2194 unfiltered_keys, truncated);
7c673cae
FG
2195 if (ret < 0 && ret != -ENOENT)
2196 return ret;
2197 if (ret == -ENOENT) {
2198 if (truncated)
2199 *truncated = false;
2200 return 0;
2201 }
2202
2203 // now filter out the system entries
2204 list<string>::iterator iter;
2205 for (iter = unfiltered_keys.begin(); iter != unfiltered_keys.end(); ++iter) {
2206 string& k = *iter;
2207
2208 if (k[0] != '.') {
2209 keys.push_back(k);
2210 }
2211 }
2212
2213 return 0;
2214 }
2215
2216 void list_keys_complete(void *handle) override {
2217 list_keys_info *info = static_cast<list_keys_info *>(handle);
2218 delete info;
2219 }
181888fb
FG
2220
2221 string get_marker(void *handle) {
2222 list_keys_info *info = static_cast<list_keys_info *>(handle);
2223 return info->store->list_raw_objs_get_cursor(info->ctx);
2224 }
7c673cae
FG
2225};
2226
2227class RGWBucketInstanceMetadataHandler : public RGWMetadataHandler {
2228
2229public:
2230 string get_type() override { return "bucket.instance"; }
2231
2232 int get(RGWRados *store, string& oid, RGWMetadataObject **obj) override {
2233 RGWBucketCompleteInfo bci;
2234
2235 real_time mtime;
2236 RGWObjectCtx obj_ctx(store);
2237
2238 int ret = store->get_bucket_instance_info(obj_ctx, oid, bci.info, &mtime, &bci.attrs);
2239 if (ret < 0)
2240 return ret;
2241
2242 RGWBucketInstanceMetadataObject *mdo = new RGWBucketInstanceMetadataObject(bci, bci.info.objv_tracker.read_version, mtime);
2243
2244 *obj = mdo;
2245
2246 return 0;
2247 }
2248
2249 int put(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker,
2250 real_time mtime, JSONObj *obj, sync_type_t sync_type) override {
2251 RGWBucketCompleteInfo bci, old_bci;
2252 try {
2253 decode_json_obj(bci, obj);
2254 } catch (JSONDecoder::err& e) {
2255 return -EINVAL;
2256 }
2257
2258 real_time orig_mtime;
2259 RGWObjectCtx obj_ctx(store);
2260
2261 int ret = store->get_bucket_instance_info(obj_ctx, entry, old_bci.info,
2262 &orig_mtime, &old_bci.attrs);
2263 bool exists = (ret != -ENOENT);
2264 if (ret < 0 && exists)
2265 return ret;
2266
2267 if (!exists || old_bci.info.bucket.bucket_id != bci.info.bucket.bucket_id) {
2268 /* a new bucket, we need to select a new bucket placement for it */
2269 auto key(entry);
2270 rgw_bucket_instance_oid_to_key(key);
2271 string tenant_name;
2272 string bucket_name;
2273 string bucket_instance;
2274 parse_bucket(key, &tenant_name, &bucket_name, &bucket_instance);
2275
2276 RGWZonePlacementInfo rule_info;
2277 bci.info.bucket.name = bucket_name;
2278 bci.info.bucket.bucket_id = bucket_instance;
2279 bci.info.bucket.tenant = tenant_name;
2280 ret = store->select_bucket_location_by_rule(bci.info.placement_rule, &rule_info);
2281 if (ret < 0) {
2282 ldout(store->ctx(), 0) << "ERROR: select_bucket_placement() returned " << ret << dendl;
2283 return ret;
2284 }
2285 bci.info.index_type = rule_info.index_type;
2286 } else {
2287 /* existing bucket, keep its placement */
2288 bci.info.bucket.explicit_placement = old_bci.info.bucket.explicit_placement;
2289 bci.info.placement_rule = old_bci.info.placement_rule;
2290 }
2291
c07f9fc5
FG
2292 if (exists && old_bci.info.datasync_flag_enabled() != bci.info.datasync_flag_enabled()) {
2293 int shards_num = bci.info.num_shards? bci.info.num_shards : 1;
2294 int shard_id = bci.info.num_shards? 0 : -1;
2295
2296 if (!bci.info.datasync_flag_enabled()) {
2297 ret = store->stop_bi_log_entries(bci.info, -1);
2298 if (ret < 0) {
2299 lderr(store->ctx()) << "ERROR: failed writing bilog" << dendl;
2300 return ret;
2301 }
2302 } else {
2303 ret = store->resync_bi_log_entries(bci.info, -1);
2304 if (ret < 0) {
2305 lderr(store->ctx()) << "ERROR: failed writing bilog" << dendl;
2306 return ret;
2307 }
2308 }
2309
2310 for (int i = 0; i < shards_num; ++i, ++shard_id) {
2311 ret = store->data_log->add_entry(bci.info.bucket, shard_id);
2312 if (ret < 0) {
2313 lderr(store->ctx()) << "ERROR: failed writing data log" << dendl;
2314 return ret;
2315 }
2316 }
2317 }
2318
7c673cae
FG
2319 // are we actually going to perform this put, or is it too old?
2320 if (exists &&
2321 !check_versions(old_bci.info.objv_tracker.read_version, orig_mtime,
2322 objv_tracker.write_version, mtime, sync_type)) {
2323 objv_tracker.read_version = old_bci.info.objv_tracker.read_version;
2324 return STATUS_NO_APPLY;
2325 }
2326
2327 /* record the read version (if any), store the new version */
2328 bci.info.objv_tracker.read_version = old_bci.info.objv_tracker.read_version;
2329 bci.info.objv_tracker.write_version = objv_tracker.write_version;
2330
2331 ret = store->put_bucket_instance_info(bci.info, false, mtime, &bci.attrs);
2332 if (ret < 0)
2333 return ret;
2334
2335 objv_tracker = bci.info.objv_tracker;
2336
2337 ret = store->init_bucket_index(bci.info, bci.info.num_shards);
2338 if (ret < 0)
2339 return ret;
2340
2341 return STATUS_APPLIED;
2342 }
2343
2344 struct list_keys_info {
2345 RGWRados *store;
2346 RGWListRawObjsCtx ctx;
2347 };
2348
2349 int remove(RGWRados *store, string& entry, RGWObjVersionTracker& objv_tracker) override {
2350 RGWBucketInfo info;
2351 RGWObjectCtx obj_ctx(store);
2352
2353 int ret = store->get_bucket_instance_info(obj_ctx, entry, info, NULL, NULL);
2354 if (ret < 0 && ret != -ENOENT)
2355 return ret;
2356
2357 return rgw_bucket_instance_remove_entry(store, entry, &info.objv_tracker);
2358 }
2359
2360 void get_pool_and_oid(RGWRados *store, const string& key, rgw_pool& pool, string& oid) override {
2361 oid = RGW_BUCKET_INSTANCE_MD_PREFIX + key;
2362 rgw_bucket_instance_key_to_oid(oid);
2363 pool = store->get_zone_params().domain_root;
2364 }
2365
181888fb
FG
2366 int list_keys_init(RGWRados *store, const string& marker, void **phandle) override {
2367 auto info = ceph::make_unique<list_keys_info>();
7c673cae
FG
2368
2369 info->store = store;
2370
181888fb
FG
2371 int ret = store->list_raw_objects_init(store->get_zone_params().domain_root, marker,
2372 &info->ctx);
2373 if (ret < 0) {
2374 return ret;
2375 }
2376 *phandle = (void *)info.release();
7c673cae
FG
2377
2378 return 0;
2379 }
2380
2381 int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated) override {
2382 list_keys_info *info = static_cast<list_keys_info *>(handle);
2383
2384 string no_filter;
2385
2386 keys.clear();
2387
2388 RGWRados *store = info->store;
2389
2390 list<string> unfiltered_keys;
2391
181888fb
FG
2392 int ret = store->list_raw_objects_next(no_filter, max, info->ctx,
2393 unfiltered_keys, truncated);
7c673cae
FG
2394 if (ret < 0 && ret != -ENOENT)
2395 return ret;
2396 if (ret == -ENOENT) {
2397 if (truncated)
2398 *truncated = false;
2399 return 0;
2400 }
2401
2402 constexpr int prefix_size = sizeof(RGW_BUCKET_INSTANCE_MD_PREFIX) - 1;
2403 // now filter in the relevant entries
2404 list<string>::iterator iter;
2405 for (iter = unfiltered_keys.begin(); iter != unfiltered_keys.end(); ++iter) {
2406 string& k = *iter;
2407
2408 if (k.compare(0, prefix_size, RGW_BUCKET_INSTANCE_MD_PREFIX) == 0) {
2409 auto oid = k.substr(prefix_size);
2410 rgw_bucket_instance_oid_to_key(oid);
2411 keys.emplace_back(std::move(oid));
2412 }
2413 }
2414
2415 return 0;
2416 }
2417
2418 void list_keys_complete(void *handle) override {
2419 list_keys_info *info = static_cast<list_keys_info *>(handle);
2420 delete info;
2421 }
2422
181888fb
FG
2423 string get_marker(void *handle) {
2424 list_keys_info *info = static_cast<list_keys_info *>(handle);
2425 return info->store->list_raw_objs_get_cursor(info->ctx);
2426 }
2427
7c673cae
FG
2428 /*
2429 * hash entry for mdlog placement. Use the same hash key we'd have for the bucket entry
2430 * point, so that the log entries end up at the same log shard, so that we process them
2431 * in order
2432 */
2433 void get_hash_key(const string& section, const string& key, string& hash_key) override {
2434 string k;
2435 int pos = key.find(':');
2436 if (pos < 0)
2437 k = key;
2438 else
2439 k = key.substr(0, pos);
2440 hash_key = "bucket:" + k;
2441 }
2442};
2443
2444void rgw_bucket_init(RGWMetadataManager *mm)
2445{
2446 bucket_meta_handler = new RGWBucketMetadataHandler;
2447 mm->register_handler(bucket_meta_handler);
2448 bucket_instance_meta_handler = new RGWBucketInstanceMetadataHandler;
2449 mm->register_handler(bucket_instance_meta_handler);
2450}