]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_admin.cc
update sources to 12.2.8
[ceph.git] / ceph / src / rgw / rgw_admin.cc
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 #include <iostream>
6 #include <sstream>
7 #include <string>
8
9 #include <boost/optional.hpp>
10
11 #include "auth/Crypto.h"
12 #include "compressor/Compressor.h"
13
14 #include "common/armor.h"
15 #include "common/ceph_json.h"
16 #include "common/config.h"
17 #include "common/ceph_argparse.h"
18 #include "common/Formatter.h"
19 #include "common/errno.h"
20 #include "common/safe_io.h"
21
22 #include "cls/rgw/cls_rgw_client.h"
23
24 #include "global/global_init.h"
25
26 #include "include/utime.h"
27 #include "include/str_list.h"
28
29 #include "rgw_user.h"
30 #include "rgw_bucket.h"
31 #include "rgw_rados.h"
32 #include "rgw_acl.h"
33 #include "rgw_acl_s3.h"
34 #include "rgw_lc.h"
35 #include "rgw_log.h"
36 #include "rgw_formats.h"
37 #include "rgw_usage.h"
38 #include "rgw_replica_log.h"
39 #include "rgw_orphan.h"
40 #include "rgw_sync.h"
41 #include "rgw_sync_log_trim.h"
42 #include "rgw_data_sync.h"
43 #include "rgw_rest_conn.h"
44 #include "rgw_realm_watcher.h"
45 #include "rgw_role.h"
46 #include "rgw_reshard.h"
47 #include "rgw_http_client_curl.h"
48
49 using namespace std;
50
51 #define dout_context g_ceph_context
52 #define dout_subsys ceph_subsys_rgw
53
54 #define SECRET_KEY_LEN 40
55 #define PUBLIC_ID_LEN 20
56
57 static RGWRados *store = NULL;
58
59 void usage()
60 {
61 cout << "usage: radosgw-admin <cmd> [options...]" << std::endl;
62 cout << "commands:\n";
63 cout << " user create create a new user\n" ;
64 cout << " user modify modify user\n";
65 cout << " user info get user info\n";
66 cout << " user rm remove user\n";
67 cout << " user suspend suspend a user\n";
68 cout << " user enable re-enable user after suspension\n";
69 cout << " user check check user info\n";
70 cout << " user stats show user stats as accounted by quota subsystem\n";
71 cout << " user list list users\n";
72 cout << " caps add add user capabilities\n";
73 cout << " caps rm remove user capabilities\n";
74 cout << " subuser create create a new subuser\n" ;
75 cout << " subuser modify modify subuser\n";
76 cout << " subuser rm remove subuser\n";
77 cout << " key create create access key\n";
78 cout << " key rm remove access key\n";
79 cout << " bucket list list buckets\n";
80 cout << " bucket limit check show bucket sharding stats\n";
81 cout << " bucket link link bucket to specified user\n";
82 cout << " bucket unlink unlink bucket from specified user\n";
83 cout << " bucket stats returns bucket statistics\n";
84 cout << " bucket rm remove bucket\n";
85 cout << " bucket check check bucket index\n";
86 cout << " bucket reshard reshard bucket\n";
87 cout << " bucket sync disable disable bucket sync\n";
88 cout << " bucket sync enable enable bucket sync\n";
89 cout << " bi get retrieve bucket index object entries\n";
90 cout << " bi put store bucket index object entries\n";
91 cout << " bi list list raw bucket index entries\n";
92 cout << " object rm remove object\n";
93 cout << " object stat stat an object for its metadata\n";
94 cout << " object unlink unlink object from bucket index\n";
95 cout << " objects expire run expired objects cleanup\n";
96 cout << " period delete delete a period\n";
97 cout << " period get get period info\n";
98 cout << " period get-current get current period info\n";
99 cout << " period pull pull a period\n";
100 cout << " period push push a period\n";
101 cout << " period list list all periods\n";
102 cout << " period update update the staging period\n";
103 cout << " period commit commit the staging period\n";
104 cout << " quota set set quota params\n";
105 cout << " quota enable enable quota\n";
106 cout << " quota disable disable quota\n";
107 cout << " global quota get view global quota params\n";
108 cout << " global quota set set global quota params\n";
109 cout << " global quota enable enable a global quota\n";
110 cout << " global quota disable disable a global quota\n";
111 cout << " realm create create a new realm\n";
112 cout << " realm delete delete a realm\n";
113 cout << " realm get show realm info\n";
114 cout << " realm get-default get default realm name\n";
115 cout << " realm list list realms\n";
116 cout << " realm list-periods list all realm periods\n";
117 cout << " realm remove remove a zonegroup from the realm\n";
118 cout << " realm rename rename a realm\n";
119 cout << " realm set set realm info (requires infile)\n";
120 cout << " realm default set realm as default\n";
121 cout << " realm pull pull a realm and its current period\n";
122 cout << " zonegroup add add a zone to a zonegroup\n";
123 cout << " zonegroup create create a new zone group info\n";
124 cout << " zonegroup default set default zone group\n";
125 cout << " zonegroup delete delete a zone group info\n";
126 cout << " zonegroup get show zone group info\n";
127 cout << " zonegroup modify modify an existing zonegroup\n";
128 cout << " zonegroup set set zone group info (requires infile)\n";
129 cout << " zonegroup remove remove a zone from a zonegroup\n";
130 cout << " zonegroup rename rename a zone group\n";
131 cout << " zonegroup list list all zone groups set on this cluster\n";
132 cout << " zonegroup placement list list zonegroup's placement targets\n";
133 cout << " zonegroup placement add add a placement target id to a zonegroup\n";
134 cout << " zonegroup placement modify modify a placement target of a specific zonegroup\n";
135 cout << " zonegroup placement rm remove a placement target from a zonegroup\n";
136 cout << " zonegroup placement default set a zonegroup's default placement target\n";
137 cout << " zone create create a new zone\n";
138 cout << " zone delete delete a zone\n";
139 cout << " zone get show zone cluster params\n";
140 cout << " zone modify modify an existing zone\n";
141 cout << " zone set set zone cluster params (requires infile)\n";
142 cout << " zone list list all zones set on this cluster\n";
143 cout << " zone rename rename a zone\n";
144 cout << " zone placement list list zone's placement targets\n";
145 cout << " zone placement add add a zone placement target\n";
146 cout << " zone placement modify modify a zone placement target\n";
147 cout << " zone placement rm remove a zone placement target\n";
148 cout << " pool add add an existing pool for data placement\n";
149 cout << " pool rm remove an existing pool from data placement set\n";
150 cout << " pools list list placement active set\n";
151 cout << " policy read bucket/object policy\n";
152 cout << " log list list log objects\n";
153 cout << " log show dump a log from specific object or (bucket + date\n";
154 cout << " + bucket-id)\n";
155 cout << " (NOTE: required to specify formatting of date\n";
156 cout << " to \"YYYY-MM-DD-hh\")\n";
157 cout << " log rm remove log object\n";
158 cout << " usage show show usage (by user, date range)\n";
159 cout << " usage trim trim usage (by user, date range)\n";
160 cout << " gc list dump expired garbage collection objects (specify\n";
161 cout << " --include-all to list all entries, including unexpired)\n";
162 cout << " gc process manually process garbage\n";
163 cout << " lc list list all bucket lifecycle progress\n";
164 cout << " lc process manually process lifecycle\n";
165 cout << " metadata get get metadata info\n";
166 cout << " metadata put put metadata info\n";
167 cout << " metadata rm remove metadata info\n";
168 cout << " metadata list list metadata info\n";
169 cout << " mdlog list list metadata log\n";
170 cout << " mdlog trim trim metadata log (use start-date, end-date or\n";
171 cout << " start-marker, end-marker)\n";
172 cout << " mdlog status read metadata log status\n";
173 cout << " bilog list list bucket index log\n";
174 cout << " bilog trim trim bucket index log (use start-marker, end-marker)\n";
175 cout << " datalog list list data log\n";
176 cout << " datalog trim trim data log\n";
177 cout << " datalog status read data log status\n";
178 cout << " opstate list list stateful operations entries (use client_id,\n";
179 cout << " op_id, object)\n";
180 cout << " opstate set set state on an entry (use client_id, op_id, object, state)\n";
181 cout << " opstate renew renew state on an entry (use client_id, op_id, object)\n";
182 cout << " opstate rm remove entry (use client_id, op_id, object)\n";
183 cout << " replicalog get get replica metadata log entry\n";
184 cout << " replicalog update update replica metadata log entry\n";
185 cout << " replicalog delete delete replica metadata log entry\n";
186 cout << " orphans find init and run search for leaked rados objects (use job-id, pool)\n";
187 cout << " orphans finish clean up search for leaked rados objects\n";
188 cout << " orphans list-jobs list the current job-ids for orphans search\n";
189 cout << " role create create a AWS role for use with STS\n";
190 cout << " role delete delete a role\n";
191 cout << " role get get a role\n";
192 cout << " role list list roles with specified path prefix\n";
193 cout << " role modify modify the assume role policy of an existing role\n";
194 cout << " role-policy put add/update permission policy to role\n";
195 cout << " role-policy list list policies attached to a role\n";
196 cout << " role-policy get get the specified inline policy document embedded with the given role\n";
197 cout << " role-policy delete delete policy attached to a role\n";
198 cout << " reshard add schedule a resharding of a bucket\n";
199 cout << " reshard list list all bucket resharding or scheduled to be reshared\n";
200 cout << " reshard process process of scheduled reshard jobs\n";
201 cout << " reshard cancel cancel resharding a bucket\n";
202 cout << " sync error list list sync error\n";
203 cout << " sync error trim trim sync error\n";
204 cout << "options:\n";
205 cout << " --tenant=<tenant> tenant name\n";
206 cout << " --uid=<id> user id\n";
207 cout << " --subuser=<name> subuser name\n";
208 cout << " --access-key=<key> S3 access key\n";
209 cout << " --email=<email>\n";
210 cout << " --secret/--secret-key=<key>\n";
211 cout << " specify secret key\n";
212 cout << " --gen-access-key generate random access key (for S3)\n";
213 cout << " --gen-secret generate random secret key\n";
214 cout << " --key-type=<type> key type, options are: swift, s3\n";
215 cout << " --temp-url-key[-2]=<key> temp url key\n";
216 cout << " --access=<access> Set access permissions for sub-user, should be one\n";
217 cout << " of read, write, readwrite, full\n";
218 cout << " --display-name=<name>\n";
219 cout << " --max-buckets max number of buckets for a user\n";
220 cout << " --admin set the admin flag on the user\n";
221 cout << " --system set the system flag on the user\n";
222 cout << " --bucket=<bucket> Specify the bucket name. Also used by the quota command.\n";
223 cout << " --pool=<pool> Specify the pool name. Also used to scan for leaked rados objects.\n";
224 cout << " --object=<object> object name\n";
225 cout << " --date=<date> date in the format yyyy-mm-dd\n";
226 cout << " --start-date=<date> start date in the format yyyy-mm-dd\n";
227 cout << " --end-date=<date> end date in the format yyyy-mm-dd\n";
228 cout << " --bucket-id=<bucket-id> bucket id\n";
229 cout << " --shard-id=<shard-id> optional for: \n";
230 cout << " mdlog list\n";
231 cout << " data sync status\n";
232 cout << " required for: \n";
233 cout << " mdlog trim\n";
234 cout << " replica mdlog get/delete\n";
235 cout << " replica datalog get/delete\n";
236 cout << " --max-entries=<entries> max entries for listing operations\n";
237 cout << " --metadata-key=<key> key to retrieve metadata from with metadata get\n";
238 cout << " --remote=<remote> zone or zonegroup id of remote gateway\n";
239 cout << " --period=<id> period id\n";
240 cout << " --epoch=<number> period epoch\n";
241 cout << " --commit commit the period during 'period update'\n";
242 cout << " --staging get staging period info\n";
243 cout << " --master set as master\n";
244 cout << " --master-url master url\n";
245 cout << " --master-zonegroup=<id> master zonegroup id\n";
246 cout << " --master-zone=<id> master zone id\n";
247 cout << " --rgw-realm=<name> realm name\n";
248 cout << " --realm-id=<id> realm id\n";
249 cout << " --realm-new-name=<name> realm new name\n";
250 cout << " --rgw-zonegroup=<name> zonegroup name\n";
251 cout << " --zonegroup-id=<id> zonegroup id\n";
252 cout << " --zonegroup-new-name=<name>\n";
253 cout << " zonegroup new name\n";
254 cout << " --rgw-zone=<name> name of zone in which radosgw is running\n";
255 cout << " --zone-id=<id> zone id\n";
256 cout << " --zone-new-name=<name> zone new name\n";
257 cout << " --source-zone specify the source zone (for data sync)\n";
258 cout << " --default set entity (realm, zonegroup, zone) as default\n";
259 cout << " --read-only set zone as read-only (when adding to zonegroup)\n";
260 cout << " --placement-id placement id for zonegroup placement commands\n";
261 cout << " --tags=<list> list of tags for zonegroup placement add and modify commands\n";
262 cout << " --tags-add=<list> list of tags to add for zonegroup placement modify command\n";
263 cout << " --tags-rm=<list> list of tags to remove for zonegroup placement modify command\n";
264 cout << " --endpoints=<list> zone endpoints\n";
265 cout << " --index-pool=<pool> placement target index pool\n";
266 cout << " --data-pool=<pool> placement target data pool\n";
267 cout << " --data-extra-pool=<pool> placement target data extra (non-ec) pool\n";
268 cout << " --placement-index-type=<type>\n";
269 cout << " placement target index type (normal, indexless, or #id)\n";
270 cout << " --compression=<type> placement target compression type (plugin name or empty/none)\n";
271 cout << " --tier-type=<type> zone tier type\n";
272 cout << " --tier-config=<k>=<v>[,...]\n";
273 cout << " set zone tier config keys, values\n";
274 cout << " --tier-config-rm=<k>[,...]\n";
275 cout << " unset zone tier config keys\n";
276 cout << " --sync-from-all[=false] set/reset whether zone syncs from all zonegroup peers\n";
277 cout << " --sync-from=[zone-name][,...]\n";
278 cout << " set list of zones to sync from\n";
279 cout << " --sync-from-rm=[zone-name][,...]\n";
280 cout << " remove zones from list of zones to sync from\n";
281 cout << " --fix besides checking bucket index, will also fix it\n";
282 cout << " --check-objects bucket check: rebuilds bucket index according to\n";
283 cout << " actual objects state\n";
284 cout << " --format=<format> specify output format for certain operations: xml,\n";
285 cout << " json\n";
286 cout << " --purge-data when specified, user removal will also purge all the\n";
287 cout << " user data\n";
288 cout << " --purge-keys when specified, subuser removal will also purge all the\n";
289 cout << " subuser keys\n";
290 cout << " --purge-objects remove a bucket's objects before deleting it\n";
291 cout << " (NOTE: required to delete a non-empty bucket)\n";
292 cout << " --sync-stats option to 'user stats', update user stats with current\n";
293 cout << " stats reported by user's buckets indexes\n";
294 cout << " --reset-stats option to 'user stats', reset stats in accordance with user buckets\n";
295 cout << " --show-log-entries=<flag> enable/disable dump of log entries on log show\n";
296 cout << " --show-log-sum=<flag> enable/disable dump of log summation on log show\n";
297 cout << " --skip-zero-entries log show only dumps entries that don't have zero value\n";
298 cout << " in one of the numeric field\n";
299 cout << " --infile=<file> specify a file to read in when setting data\n";
300 cout << " --state=<state string> specify a state for the opstate set command\n";
301 cout << " --replica-log-type replica log type (metadata, data, bucket), required for\n";
302 cout << " replica log operations\n";
303 cout << " --categories=<list> comma separated list of categories, used in usage show\n";
304 cout << " --caps=<caps> list of caps (e.g., \"usage=read, write; user=read\")\n";
305 cout << " --yes-i-really-mean-it required for certain operations\n";
306 cout << " --warnings-only when specified with bucket limit check, list\n";
307 cout << " only buckets nearing or over the current max\n";
308 cout << " objects per shard value\n";
309 cout << " --bypass-gc when specified with bucket deletion, triggers\n";
310 cout << " object deletions by not involving GC\n";
311 cout << " --inconsistent-index when specified with bucket deletion and bypass-gc set to true,\n";
312 cout << " ignores bucket index consistency\n";
313 cout << "\n";
314 cout << "<date> := \"YYYY-MM-DD[ hh:mm:ss]\"\n";
315 cout << "\nQuota options:\n";
316 cout << " --bucket specified bucket for quota command\n";
317 cout << " --max-objects specify max objects (negative value to disable)\n";
318 cout << " --max-size specify max size (in B/K/M/G/T, negative value to disable)\n";
319 cout << " --quota-scope scope of quota (bucket, user)\n";
320 cout << "\nOrphans search options:\n";
321 cout << " --pool data pool to scan for leaked rados objects in\n";
322 cout << " --num-shards num of shards to use for keeping the temporary scan info\n";
323 cout << " --orphan-stale-secs num of seconds to wait before declaring an object to be an orphan (default: 86400)\n";
324 cout << " --job-id set the job id (for orphans find)\n";
325 cout << " --max-concurrent-ios maximum concurrent ios for orphans find (default: 32)\n";
326 cout << "\nOrphans list-jobs options:\n";
327 cout << " --extra-info provide extra info in job list\n";
328 cout << "\nRole options:\n";
329 cout << " --role-name name of the role to create\n";
330 cout << " --path path to the role\n";
331 cout << " --assume-role-policy-doc the trust relationship policy document that grants an entity permission to assume the role\n";
332 cout << " --policy-name name of the policy document\n";
333 cout << " --policy-doc permission policy document\n";
334 cout << " --path-prefix path prefix for filtering roles\n";
335 cout << "\n";
336 generic_client_usage();
337 }
338
339 enum {
340 OPT_NO_CMD = 0,
341 OPT_USER_CREATE,
342 OPT_USER_INFO,
343 OPT_USER_MODIFY,
344 OPT_USER_RM,
345 OPT_USER_SUSPEND,
346 OPT_USER_ENABLE,
347 OPT_USER_CHECK,
348 OPT_USER_STATS,
349 OPT_USER_LIST,
350 OPT_SUBUSER_CREATE,
351 OPT_SUBUSER_MODIFY,
352 OPT_SUBUSER_RM,
353 OPT_KEY_CREATE,
354 OPT_KEY_RM,
355 OPT_BUCKETS_LIST,
356 OPT_BUCKET_LIMIT_CHECK,
357 OPT_BUCKET_LINK,
358 OPT_BUCKET_UNLINK,
359 OPT_BUCKET_STATS,
360 OPT_BUCKET_CHECK,
361 OPT_BUCKET_SYNC_STATUS,
362 OPT_BUCKET_SYNC_MARKERS,
363 OPT_BUCKET_SYNC_INIT,
364 OPT_BUCKET_SYNC_RUN,
365 OPT_BUCKET_SYNC_DISABLE,
366 OPT_BUCKET_SYNC_ENABLE,
367 OPT_BUCKET_RM,
368 OPT_BUCKET_REWRITE,
369 OPT_BUCKET_RESHARD,
370 OPT_POLICY,
371 OPT_POOL_ADD,
372 OPT_POOL_RM,
373 OPT_POOLS_LIST,
374 OPT_LOG_LIST,
375 OPT_LOG_SHOW,
376 OPT_LOG_RM,
377 OPT_USAGE_SHOW,
378 OPT_USAGE_TRIM,
379 OPT_OBJECT_RM,
380 OPT_OBJECT_UNLINK,
381 OPT_OBJECT_STAT,
382 OPT_OBJECT_REWRITE,
383 OPT_OBJECTS_EXPIRE,
384 OPT_BI_GET,
385 OPT_BI_PUT,
386 OPT_BI_LIST,
387 OPT_BI_PURGE,
388 OPT_OLH_GET,
389 OPT_OLH_READLOG,
390 OPT_QUOTA_SET,
391 OPT_QUOTA_ENABLE,
392 OPT_QUOTA_DISABLE,
393 OPT_GC_LIST,
394 OPT_GC_PROCESS,
395 OPT_LC_LIST,
396 OPT_LC_PROCESS,
397 OPT_ORPHANS_FIND,
398 OPT_ORPHANS_FINISH,
399 OPT_ORPHANS_LIST_JOBS,
400 OPT_ZONEGROUP_ADD,
401 OPT_ZONEGROUP_CREATE,
402 OPT_ZONEGROUP_DEFAULT,
403 OPT_ZONEGROUP_DELETE,
404 OPT_ZONEGROUP_GET,
405 OPT_ZONEGROUP_MODIFY,
406 OPT_ZONEGROUP_SET,
407 OPT_ZONEGROUP_LIST,
408 OPT_ZONEGROUP_REMOVE,
409 OPT_ZONEGROUP_RENAME,
410 OPT_ZONEGROUP_PLACEMENT_ADD,
411 OPT_ZONEGROUP_PLACEMENT_MODIFY,
412 OPT_ZONEGROUP_PLACEMENT_RM,
413 OPT_ZONEGROUP_PLACEMENT_LIST,
414 OPT_ZONEGROUP_PLACEMENT_DEFAULT,
415 OPT_ZONE_CREATE,
416 OPT_ZONE_DELETE,
417 OPT_ZONE_GET,
418 OPT_ZONE_MODIFY,
419 OPT_ZONE_SET,
420 OPT_ZONE_LIST,
421 OPT_ZONE_RENAME,
422 OPT_ZONE_DEFAULT,
423 OPT_ZONE_PLACEMENT_ADD,
424 OPT_ZONE_PLACEMENT_MODIFY,
425 OPT_ZONE_PLACEMENT_RM,
426 OPT_ZONE_PLACEMENT_LIST,
427 OPT_CAPS_ADD,
428 OPT_CAPS_RM,
429 OPT_METADATA_GET,
430 OPT_METADATA_PUT,
431 OPT_METADATA_RM,
432 OPT_METADATA_LIST,
433 OPT_METADATA_SYNC_STATUS,
434 OPT_METADATA_SYNC_INIT,
435 OPT_METADATA_SYNC_RUN,
436 OPT_MDLOG_LIST,
437 OPT_MDLOG_AUTOTRIM,
438 OPT_MDLOG_TRIM,
439 OPT_MDLOG_FETCH,
440 OPT_MDLOG_STATUS,
441 OPT_SYNC_ERROR_LIST,
442 OPT_SYNC_ERROR_TRIM,
443 OPT_BILOG_LIST,
444 OPT_BILOG_TRIM,
445 OPT_BILOG_STATUS,
446 OPT_BILOG_AUTOTRIM,
447 OPT_DATA_SYNC_STATUS,
448 OPT_DATA_SYNC_INIT,
449 OPT_DATA_SYNC_RUN,
450 OPT_DATALOG_LIST,
451 OPT_DATALOG_STATUS,
452 OPT_DATALOG_TRIM,
453 OPT_OPSTATE_LIST,
454 OPT_OPSTATE_SET,
455 OPT_OPSTATE_RENEW,
456 OPT_OPSTATE_RM,
457 OPT_REPLICALOG_GET,
458 OPT_REPLICALOG_UPDATE,
459 OPT_REPLICALOG_DELETE,
460 OPT_REALM_CREATE,
461 OPT_REALM_DELETE,
462 OPT_REALM_GET,
463 OPT_REALM_GET_DEFAULT,
464 OPT_REALM_LIST,
465 OPT_REALM_LIST_PERIODS,
466 OPT_REALM_REMOVE,
467 OPT_REALM_RENAME,
468 OPT_REALM_SET,
469 OPT_REALM_DEFAULT,
470 OPT_REALM_PULL,
471 OPT_PERIOD_DELETE,
472 OPT_PERIOD_GET,
473 OPT_PERIOD_GET_CURRENT,
474 OPT_PERIOD_PULL,
475 OPT_PERIOD_PUSH,
476 OPT_PERIOD_LIST,
477 OPT_PERIOD_UPDATE,
478 OPT_PERIOD_COMMIT,
479 OPT_GLOBAL_QUOTA_GET,
480 OPT_GLOBAL_QUOTA_SET,
481 OPT_GLOBAL_QUOTA_ENABLE,
482 OPT_GLOBAL_QUOTA_DISABLE,
483 OPT_SYNC_STATUS,
484 OPT_ROLE_CREATE,
485 OPT_ROLE_DELETE,
486 OPT_ROLE_GET,
487 OPT_ROLE_MODIFY,
488 OPT_ROLE_LIST,
489 OPT_ROLE_POLICY_PUT,
490 OPT_ROLE_POLICY_LIST,
491 OPT_ROLE_POLICY_GET,
492 OPT_ROLE_POLICY_DELETE,
493 OPT_RESHARD_ADD,
494 OPT_RESHARD_LIST,
495 OPT_RESHARD_STATUS,
496 OPT_RESHARD_PROCESS,
497 OPT_RESHARD_CANCEL,
498 };
499
500 static int get_cmd(const char *cmd, const char *prev_cmd, const char *prev_prev_cmd, bool *need_more)
501 {
502 *need_more = false;
503 // NOTE: please keep the checks in alphabetical order !!!
504 if (strcmp(cmd, "bi") == 0 ||
505 strcmp(cmd, "bilog") == 0 ||
506 strcmp(cmd, "buckets") == 0 ||
507 strcmp(cmd, "caps") == 0 ||
508 strcmp(cmd, "data") == 0 ||
509 strcmp(cmd, "datalog") == 0 ||
510 strcmp(cmd, "error") == 0 ||
511 strcmp(cmd, "gc") == 0 ||
512 strcmp(cmd, "global") == 0 ||
513 strcmp(cmd, "key") == 0 ||
514 strcmp(cmd, "log") == 0 ||
515 strcmp(cmd, "lc") == 0 ||
516 strcmp(cmd, "mdlog") == 0 ||
517 strcmp(cmd, "metadata") == 0 ||
518 strcmp(cmd, "object") == 0 ||
519 strcmp(cmd, "objects") == 0 ||
520 strcmp(cmd, "olh") == 0 ||
521 strcmp(cmd, "opstate") == 0 ||
522 strcmp(cmd, "orphans") == 0 ||
523 strcmp(cmd, "period") == 0 ||
524 strcmp(cmd, "placement") == 0 ||
525 strcmp(cmd, "pool") == 0 ||
526 strcmp(cmd, "pools") == 0 ||
527 strcmp(cmd, "quota") == 0 ||
528 strcmp(cmd, "realm") == 0 ||
529 strcmp(cmd, "replicalog") == 0 ||
530 strcmp(cmd, "role") == 0 ||
531 strcmp(cmd, "role-policy") == 0 ||
532 strcmp(cmd, "subuser") == 0 ||
533 strcmp(cmd, "sync") == 0 ||
534 strcmp(cmd, "usage") == 0 ||
535 strcmp(cmd, "user") == 0 ||
536 strcmp(cmd, "zone") == 0 ||
537 strcmp(cmd, "zonegroup") == 0 ||
538 strcmp(cmd, "zonegroups") == 0) {
539 *need_more = true;
540 return 0;
541 }
542
543 /*
544 * can do both radosgw-admin bucket reshard, and radosgw-admin reshard bucket
545 */
546 if (strcmp(cmd, "reshard") == 0 &&
547 !(prev_cmd && strcmp(prev_cmd, "bucket") == 0)) {
548 *need_more = true;
549 return 0;
550 }
551 if (strcmp(cmd, "bucket") == 0 &&
552 !(prev_cmd && strcmp(prev_cmd, "reshard") == 0)) {
553 *need_more = true;
554 return 0;
555 }
556
557 if (strcmp(cmd, "policy") == 0)
558 return OPT_POLICY;
559
560 if (!prev_cmd)
561 return -EINVAL;
562
563 if (strcmp(prev_cmd, "user") == 0) {
564 if (strcmp(cmd, "create") == 0)
565 return OPT_USER_CREATE;
566 if (strcmp(cmd, "info") == 0)
567 return OPT_USER_INFO;
568 if (strcmp(cmd, "modify") == 0)
569 return OPT_USER_MODIFY;
570 if (strcmp(cmd, "rm") == 0)
571 return OPT_USER_RM;
572 if (strcmp(cmd, "suspend") == 0)
573 return OPT_USER_SUSPEND;
574 if (strcmp(cmd, "enable") == 0)
575 return OPT_USER_ENABLE;
576 if (strcmp(cmd, "check") == 0)
577 return OPT_USER_CHECK;
578 if (strcmp(cmd, "stats") == 0)
579 return OPT_USER_STATS;
580 if (strcmp(cmd, "list") == 0)
581 return OPT_USER_LIST;
582 } else if (strcmp(prev_cmd, "subuser") == 0) {
583 if (strcmp(cmd, "create") == 0)
584 return OPT_SUBUSER_CREATE;
585 if (strcmp(cmd, "modify") == 0)
586 return OPT_SUBUSER_MODIFY;
587 if (strcmp(cmd, "rm") == 0)
588 return OPT_SUBUSER_RM;
589 } else if (strcmp(prev_cmd, "key") == 0) {
590 if (strcmp(cmd, "create") == 0)
591 return OPT_KEY_CREATE;
592 if (strcmp(cmd, "rm") == 0)
593 return OPT_KEY_RM;
594 } else if (strcmp(prev_cmd, "buckets") == 0) {
595 if (strcmp(cmd, "list") == 0)
596 return OPT_BUCKETS_LIST;
597 } else if (strcmp(prev_cmd, "bucket") == 0) {
598 if (strcmp(cmd, "list") == 0)
599 return OPT_BUCKETS_LIST;
600 if (strcmp(cmd, "link") == 0)
601 return OPT_BUCKET_LINK;
602 if (strcmp(cmd, "unlink") == 0)
603 return OPT_BUCKET_UNLINK;
604 if (strcmp(cmd, "stats") == 0)
605 return OPT_BUCKET_STATS;
606 if (strcmp(cmd, "rm") == 0)
607 return OPT_BUCKET_RM;
608 if (strcmp(cmd, "rewrite") == 0)
609 return OPT_BUCKET_REWRITE;
610 if (strcmp(cmd, "reshard") == 0)
611 return OPT_BUCKET_RESHARD;
612 if (strcmp(cmd, "check") == 0)
613 return OPT_BUCKET_CHECK;
614 if (strcmp(cmd, "sync") == 0) {
615 *need_more = true;
616 return 0;
617 }
618 if (strcmp(cmd, "limit") == 0) {
619 *need_more = true;
620 return 0;
621 }
622 } else if (prev_prev_cmd && strcmp(prev_prev_cmd, "bucket") == 0) {
623 if (strcmp(prev_cmd, "sync") == 0) {
624 if (strcmp(cmd, "status") == 0)
625 return OPT_BUCKET_SYNC_STATUS;
626 if (strcmp(cmd, "markers") == 0)
627 return OPT_BUCKET_SYNC_MARKERS;
628 if (strcmp(cmd, "init") == 0)
629 return OPT_BUCKET_SYNC_INIT;
630 if (strcmp(cmd, "run") == 0)
631 return OPT_BUCKET_SYNC_RUN;
632 if (strcmp(cmd, "disable") == 0)
633 return OPT_BUCKET_SYNC_DISABLE;
634 if (strcmp(cmd, "enable") == 0)
635 return OPT_BUCKET_SYNC_ENABLE;
636 } else if ((strcmp(prev_cmd, "limit") == 0) &&
637 (strcmp(cmd, "check") == 0)) {
638 return OPT_BUCKET_LIMIT_CHECK;
639 }
640 } else if (strcmp(prev_cmd, "log") == 0) {
641 if (strcmp(cmd, "list") == 0)
642 return OPT_LOG_LIST;
643 if (strcmp(cmd, "show") == 0)
644 return OPT_LOG_SHOW;
645 if (strcmp(cmd, "rm") == 0)
646 return OPT_LOG_RM;
647 } else if (strcmp(prev_cmd, "usage") == 0) {
648 if (strcmp(cmd, "show") == 0)
649 return OPT_USAGE_SHOW;
650 if (strcmp(cmd, "trim") == 0)
651 return OPT_USAGE_TRIM;
652 } else if (strcmp(prev_cmd, "caps") == 0) {
653 if (strcmp(cmd, "add") == 0)
654 return OPT_CAPS_ADD;
655 if (strcmp(cmd, "rm") == 0)
656 return OPT_CAPS_RM;
657 } else if (strcmp(prev_cmd, "pool") == 0) {
658 if (strcmp(cmd, "add") == 0)
659 return OPT_POOL_ADD;
660 if (strcmp(cmd, "rm") == 0)
661 return OPT_POOL_RM;
662 if (strcmp(cmd, "list") == 0)
663 return OPT_POOLS_LIST;
664 } else if (strcmp(prev_cmd, "pools") == 0) {
665 if (strcmp(cmd, "list") == 0)
666 return OPT_POOLS_LIST;
667 } else if (strcmp(prev_cmd, "object") == 0) {
668 if (strcmp(cmd, "rm") == 0)
669 return OPT_OBJECT_RM;
670 if (strcmp(cmd, "unlink") == 0)
671 return OPT_OBJECT_UNLINK;
672 if (strcmp(cmd, "stat") == 0)
673 return OPT_OBJECT_STAT;
674 if (strcmp(cmd, "rewrite") == 0)
675 return OPT_OBJECT_REWRITE;
676 } else if (strcmp(prev_cmd, "objects") == 0) {
677 if (strcmp(cmd, "expire") == 0)
678 return OPT_OBJECTS_EXPIRE;
679 } else if (strcmp(prev_cmd, "olh") == 0) {
680 if (strcmp(cmd, "get") == 0)
681 return OPT_OLH_GET;
682 if (strcmp(cmd, "readlog") == 0)
683 return OPT_OLH_READLOG;
684 } else if (strcmp(prev_cmd, "bi") == 0) {
685 if (strcmp(cmd, "get") == 0)
686 return OPT_BI_GET;
687 if (strcmp(cmd, "put") == 0)
688 return OPT_BI_PUT;
689 if (strcmp(cmd, "list") == 0)
690 return OPT_BI_LIST;
691 if (strcmp(cmd, "purge") == 0)
692 return OPT_BI_PURGE;
693 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "global") == 0) &&
694 (strcmp(prev_cmd, "quota") == 0)) {
695 if (strcmp(cmd, "get") == 0)
696 return OPT_GLOBAL_QUOTA_GET;
697 if (strcmp(cmd, "set") == 0)
698 return OPT_GLOBAL_QUOTA_SET;
699 if (strcmp(cmd, "enable") == 0)
700 return OPT_GLOBAL_QUOTA_ENABLE;
701 if (strcmp(cmd, "disable") == 0)
702 return OPT_GLOBAL_QUOTA_DISABLE;
703 } else if (strcmp(prev_cmd, "period") == 0) {
704 if (strcmp(cmd, "delete") == 0)
705 return OPT_PERIOD_DELETE;
706 if (strcmp(cmd, "get") == 0)
707 return OPT_PERIOD_GET;
708 if (strcmp(cmd, "get-current") == 0)
709 return OPT_PERIOD_GET_CURRENT;
710 if (strcmp(cmd, "pull") == 0)
711 return OPT_PERIOD_PULL;
712 if (strcmp(cmd, "push") == 0)
713 return OPT_PERIOD_PUSH;
714 if (strcmp(cmd, "list") == 0)
715 return OPT_PERIOD_LIST;
716 if (strcmp(cmd, "update") == 0)
717 return OPT_PERIOD_UPDATE;
718 if (strcmp(cmd, "commit") == 0)
719 return OPT_PERIOD_COMMIT;
720 } else if (strcmp(prev_cmd, "realm") == 0) {
721 if (strcmp(cmd, "create") == 0)
722 return OPT_REALM_CREATE;
723 if (strcmp(cmd, "delete") == 0)
724 return OPT_REALM_DELETE;
725 if (strcmp(cmd, "get") == 0)
726 return OPT_REALM_GET;
727 if (strcmp(cmd, "get-default") == 0)
728 return OPT_REALM_GET_DEFAULT;
729 if (strcmp(cmd, "list") == 0)
730 return OPT_REALM_LIST;
731 if (strcmp(cmd, "list-periods") == 0)
732 return OPT_REALM_LIST_PERIODS;
733 if (strcmp(cmd, "remove") == 0)
734 return OPT_REALM_REMOVE;
735 if (strcmp(cmd, "rename") == 0)
736 return OPT_REALM_RENAME;
737 if (strcmp(cmd, "set") == 0)
738 return OPT_REALM_SET;
739 if (strcmp(cmd, "default") == 0)
740 return OPT_REALM_DEFAULT;
741 if (strcmp(cmd, "pull") == 0)
742 return OPT_REALM_PULL;
743 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "zonegroup") == 0) &&
744 (strcmp(prev_cmd, "placement") == 0)) {
745 if (strcmp(cmd, "add") == 0)
746 return OPT_ZONEGROUP_PLACEMENT_ADD;
747 if (strcmp(cmd, "modify") == 0)
748 return OPT_ZONEGROUP_PLACEMENT_MODIFY;
749 if (strcmp(cmd, "rm") == 0)
750 return OPT_ZONEGROUP_PLACEMENT_RM;
751 if (strcmp(cmd, "list") == 0)
752 return OPT_ZONEGROUP_PLACEMENT_LIST;
753 if (strcmp(cmd, "default") == 0)
754 return OPT_ZONEGROUP_PLACEMENT_DEFAULT;
755 } else if (strcmp(prev_cmd, "zonegroup") == 0) {
756 if (strcmp(cmd, "add") == 0)
757 return OPT_ZONEGROUP_ADD;
758 if (strcmp(cmd, "create")== 0)
759 return OPT_ZONEGROUP_CREATE;
760 if (strcmp(cmd, "default") == 0)
761 return OPT_ZONEGROUP_DEFAULT;
762 if (strcmp(cmd, "delete") == 0)
763 return OPT_ZONEGROUP_DELETE;
764 if (strcmp(cmd, "get") == 0)
765 return OPT_ZONEGROUP_GET;
766 if (strcmp(cmd, "modify") == 0)
767 return OPT_ZONEGROUP_MODIFY;
768 if (strcmp(cmd, "list") == 0)
769 return OPT_ZONEGROUP_LIST;
770 if (strcmp(cmd, "set") == 0)
771 return OPT_ZONEGROUP_SET;
772 if (strcmp(cmd, "remove") == 0)
773 return OPT_ZONEGROUP_REMOVE;
774 if (strcmp(cmd, "rename") == 0)
775 return OPT_ZONEGROUP_RENAME;
776 } else if (strcmp(prev_cmd, "quota") == 0) {
777 if (strcmp(cmd, "set") == 0)
778 return OPT_QUOTA_SET;
779 if (strcmp(cmd, "enable") == 0)
780 return OPT_QUOTA_ENABLE;
781 if (strcmp(cmd, "disable") == 0)
782 return OPT_QUOTA_DISABLE;
783 } else if (strcmp(prev_cmd, "zonegroups") == 0) {
784 if (strcmp(cmd, "list") == 0)
785 return OPT_ZONEGROUP_LIST;
786 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "zone") == 0) &&
787 (strcmp(prev_cmd, "placement") == 0)) {
788 if (strcmp(cmd, "add") == 0)
789 return OPT_ZONE_PLACEMENT_ADD;
790 if (strcmp(cmd, "modify") == 0)
791 return OPT_ZONE_PLACEMENT_MODIFY;
792 if (strcmp(cmd, "rm") == 0)
793 return OPT_ZONE_PLACEMENT_RM;
794 if (strcmp(cmd, "list") == 0)
795 return OPT_ZONE_PLACEMENT_LIST;
796 } else if (strcmp(prev_cmd, "zone") == 0) {
797 if (strcmp(cmd, "delete") == 0)
798 return OPT_ZONE_DELETE;
799 if (strcmp(cmd, "create") == 0)
800 return OPT_ZONE_CREATE;
801 if (strcmp(cmd, "get") == 0)
802 return OPT_ZONE_GET;
803 if (strcmp(cmd, "set") == 0)
804 return OPT_ZONE_SET;
805 if (strcmp(cmd, "list") == 0)
806 return OPT_ZONE_LIST;
807 if (strcmp(cmd, "modify") == 0)
808 return OPT_ZONE_MODIFY;
809 if (strcmp(cmd, "rename") == 0)
810 return OPT_ZONE_RENAME;
811 if (strcmp(cmd, "default") == 0)
812 return OPT_ZONE_DEFAULT;
813 } else if (strcmp(prev_cmd, "zones") == 0) {
814 if (strcmp(cmd, "list") == 0)
815 return OPT_ZONE_LIST;
816 } else if (strcmp(prev_cmd, "gc") == 0) {
817 if (strcmp(cmd, "list") == 0)
818 return OPT_GC_LIST;
819 if (strcmp(cmd, "process") == 0)
820 return OPT_GC_PROCESS;
821 } else if (strcmp(prev_cmd, "lc") == 0) {
822 if (strcmp(cmd, "list") == 0)
823 return OPT_LC_LIST;
824 if (strcmp(cmd, "process") == 0)
825 return OPT_LC_PROCESS;
826 } else if (strcmp(prev_cmd, "orphans") == 0) {
827 if (strcmp(cmd, "find") == 0)
828 return OPT_ORPHANS_FIND;
829 if (strcmp(cmd, "finish") == 0)
830 return OPT_ORPHANS_FINISH;
831 if (strcmp(cmd, "list-jobs") == 0)
832 return OPT_ORPHANS_LIST_JOBS;
833 } else if (strcmp(prev_cmd, "metadata") == 0) {
834 if (strcmp(cmd, "get") == 0)
835 return OPT_METADATA_GET;
836 if (strcmp(cmd, "put") == 0)
837 return OPT_METADATA_PUT;
838 if (strcmp(cmd, "rm") == 0)
839 return OPT_METADATA_RM;
840 if (strcmp(cmd, "list") == 0)
841 return OPT_METADATA_LIST;
842 if (strcmp(cmd, "sync") == 0) {
843 *need_more = true;
844 return 0;
845 }
846 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "metadata") == 0) &&
847 (strcmp(prev_cmd, "sync") == 0)) {
848 if (strcmp(cmd, "status") == 0)
849 return OPT_METADATA_SYNC_STATUS;
850 if (strcmp(cmd, "init") == 0)
851 return OPT_METADATA_SYNC_INIT;
852 if (strcmp(cmd, "run") == 0)
853 return OPT_METADATA_SYNC_RUN;
854 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "sync") == 0) &&
855 (strcmp(prev_cmd, "error") == 0)) {
856 if (strcmp(cmd, "list") == 0)
857 return OPT_SYNC_ERROR_LIST;
858 if (strcmp(cmd, "trim") == 0)
859 return OPT_SYNC_ERROR_TRIM;
860 } else if (strcmp(prev_cmd, "mdlog") == 0) {
861 if (strcmp(cmd, "list") == 0)
862 return OPT_MDLOG_LIST;
863 if (strcmp(cmd, "autotrim") == 0)
864 return OPT_MDLOG_AUTOTRIM;
865 if (strcmp(cmd, "trim") == 0)
866 return OPT_MDLOG_TRIM;
867 if (strcmp(cmd, "fetch") == 0)
868 return OPT_MDLOG_FETCH;
869 if (strcmp(cmd, "status") == 0)
870 return OPT_MDLOG_STATUS;
871 } else if (strcmp(prev_cmd, "bilog") == 0) {
872 if (strcmp(cmd, "list") == 0)
873 return OPT_BILOG_LIST;
874 if (strcmp(cmd, "trim") == 0)
875 return OPT_BILOG_TRIM;
876 if (strcmp(cmd, "status") == 0)
877 return OPT_BILOG_STATUS;
878 if (strcmp(cmd, "autotrim") == 0)
879 return OPT_BILOG_AUTOTRIM;
880 } else if (strcmp(prev_cmd, "data") == 0) {
881 if (strcmp(cmd, "sync") == 0) {
882 *need_more = true;
883 return 0;
884 }
885 } else if (strcmp(prev_cmd, "datalog") == 0) {
886 if (strcmp(cmd, "list") == 0)
887 return OPT_DATALOG_LIST;
888 if (strcmp(cmd, "trim") == 0)
889 return OPT_DATALOG_TRIM;
890 if (strcmp(cmd, "status") == 0)
891 return OPT_DATALOG_STATUS;
892 } else if ((prev_prev_cmd && strcmp(prev_prev_cmd, "data") == 0) &&
893 (strcmp(prev_cmd, "sync") == 0)) {
894 if (strcmp(cmd, "status") == 0)
895 return OPT_DATA_SYNC_STATUS;
896 if (strcmp(cmd, "init") == 0)
897 return OPT_DATA_SYNC_INIT;
898 if (strcmp(cmd, "run") == 0)
899 return OPT_DATA_SYNC_RUN;
900 } else if (strcmp(prev_cmd, "opstate") == 0) {
901 if (strcmp(cmd, "list") == 0)
902 return OPT_OPSTATE_LIST;
903 if (strcmp(cmd, "set") == 0)
904 return OPT_OPSTATE_SET;
905 if (strcmp(cmd, "renew") == 0)
906 return OPT_OPSTATE_RENEW;
907 if (strcmp(cmd, "rm") == 0)
908 return OPT_OPSTATE_RM;
909 } else if (strcmp(prev_cmd, "replicalog") == 0) {
910 if (strcmp(cmd, "get") == 0)
911 return OPT_REPLICALOG_GET;
912 if (strcmp(cmd, "update") == 0)
913 return OPT_REPLICALOG_UPDATE;
914 if (strcmp(cmd, "delete") == 0)
915 return OPT_REPLICALOG_DELETE;
916 } else if (strcmp(prev_cmd, "sync") == 0) {
917 if (strcmp(cmd, "status") == 0)
918 return OPT_SYNC_STATUS;
919 } else if (strcmp(prev_cmd, "role") == 0) {
920 if (strcmp(cmd, "create") == 0)
921 return OPT_ROLE_CREATE;
922 if (strcmp(cmd, "delete") == 0)
923 return OPT_ROLE_DELETE;
924 if (strcmp(cmd, "get") == 0)
925 return OPT_ROLE_GET;
926 if (strcmp(cmd, "modify") == 0)
927 return OPT_ROLE_MODIFY;
928 if (strcmp(cmd, "list") == 0)
929 return OPT_ROLE_LIST;
930 } else if (strcmp(prev_cmd, "role-policy") == 0) {
931 if (strcmp(cmd, "put") == 0)
932 return OPT_ROLE_POLICY_PUT;
933 if (strcmp(cmd, "list") == 0)
934 return OPT_ROLE_POLICY_LIST;
935 if (strcmp(cmd, "get") == 0)
936 return OPT_ROLE_POLICY_GET;
937 if (strcmp(cmd, "delete") == 0)
938 return OPT_ROLE_POLICY_DELETE;
939 } else if (strcmp(prev_cmd, "reshard") == 0) {
940 if (strcmp(cmd, "bucket") == 0)
941 return OPT_BUCKET_RESHARD;
942 if (strcmp(cmd, "add") == 0)
943 return OPT_RESHARD_ADD;
944 if (strcmp(cmd, "list") == 0)
945 return OPT_RESHARD_LIST;
946 if (strcmp(cmd, "status") == 0)
947 return OPT_RESHARD_STATUS;
948 if (strcmp(cmd, "process") == 0)
949 return OPT_RESHARD_PROCESS;
950 if (strcmp(cmd, "cancel") == 0)
951 return OPT_RESHARD_CANCEL;
952 }
953
954 return -EINVAL;
955 }
956
957 enum ReplicaLogType {
958 ReplicaLog_Invalid = 0,
959 ReplicaLog_Metadata,
960 ReplicaLog_Data,
961 ReplicaLog_Bucket,
962 };
963
964 ReplicaLogType get_replicalog_type(const string& name) {
965 if (name == "md" || name == "meta" || name == "metadata")
966 return ReplicaLog_Metadata;
967 if (name == "data")
968 return ReplicaLog_Data;
969 if (name == "bucket")
970 return ReplicaLog_Bucket;
971
972 return ReplicaLog_Invalid;
973 }
974
975 BIIndexType get_bi_index_type(const string& type_str) {
976 if (type_str == "plain")
977 return PlainIdx;
978 if (type_str == "instance")
979 return InstanceIdx;
980 if (type_str == "olh")
981 return OLHIdx;
982
983 return InvalidIdx;
984 }
985
986 void dump_bi_entry(bufferlist& bl, BIIndexType index_type, Formatter *formatter)
987 {
988 bufferlist::iterator iter = bl.begin();
989 switch (index_type) {
990 case PlainIdx:
991 case InstanceIdx:
992 {
993 rgw_bucket_dir_entry entry;
994 ::decode(entry, iter);
995 encode_json("entry", entry, formatter);
996 }
997 break;
998 case OLHIdx:
999 {
1000 rgw_bucket_olh_entry entry;
1001 ::decode(entry, iter);
1002 encode_json("entry", entry, formatter);
1003 }
1004 break;
1005 default:
1006 ceph_abort();
1007 break;
1008 }
1009 }
1010
1011 static void show_user_info(RGWUserInfo& info, Formatter *formatter)
1012 {
1013 encode_json("user_info", info, formatter);
1014 formatter->flush(cout);
1015 cout << std::endl;
1016 }
1017
1018 static void show_perm_policy(string perm_policy, Formatter* formatter)
1019 {
1020 formatter->open_object_section("role");
1021 formatter->dump_string("Permission policy", perm_policy);
1022 formatter->close_section();
1023 formatter->flush(cout);
1024 }
1025
1026 static void show_policy_names(std::vector<string> policy_names, Formatter* formatter)
1027 {
1028 formatter->open_array_section("PolicyNames");
1029 for (const auto& it : policy_names) {
1030 formatter->dump_string("policyname", it);
1031 }
1032 formatter->close_section();
1033 formatter->flush(cout);
1034 }
1035
1036 static void show_role_info(RGWRole& role, Formatter* formatter)
1037 {
1038 formatter->open_object_section("role");
1039 role.dump(formatter);
1040 formatter->close_section();
1041 formatter->flush(cout);
1042 }
1043
1044 static void show_roles_info(vector<RGWRole>& roles, Formatter* formatter)
1045 {
1046 formatter->open_array_section("Roles");
1047 for (const auto& it : roles) {
1048 formatter->open_object_section("role");
1049 it.dump(formatter);
1050 formatter->close_section();
1051 }
1052 formatter->close_section();
1053 formatter->flush(cout);
1054 }
1055
1056 class StoreDestructor {
1057 RGWRados *store;
1058 public:
1059 explicit StoreDestructor(RGWRados *_s) : store(_s) {}
1060 ~StoreDestructor() {
1061 RGWStoreManager::close_storage(store);
1062 }
1063 };
1064
1065 static int init_bucket(const string& tenant_name, const string& bucket_name, const string& bucket_id,
1066 RGWBucketInfo& bucket_info, rgw_bucket& bucket, map<string, bufferlist> *pattrs = nullptr)
1067 {
1068 if (!bucket_name.empty()) {
1069 RGWObjectCtx obj_ctx(store);
1070 int r;
1071 if (bucket_id.empty()) {
1072 r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, nullptr, pattrs);
1073 } else {
1074 string bucket_instance_id = bucket_name + ":" + bucket_id;
1075 r = store->get_bucket_instance_info(obj_ctx, bucket_instance_id, bucket_info, NULL, pattrs);
1076 }
1077 if (r < 0) {
1078 cerr << "could not get bucket info for bucket=" << bucket_name << std::endl;
1079 return r;
1080 }
1081 bucket = bucket_info.bucket;
1082 }
1083 return 0;
1084 }
1085
1086 static int read_input(const string& infile, bufferlist& bl)
1087 {
1088 int fd = 0;
1089 if (infile.size()) {
1090 fd = open(infile.c_str(), O_RDONLY);
1091 if (fd < 0) {
1092 int err = -errno;
1093 cerr << "error reading input file " << infile << std::endl;
1094 return err;
1095 }
1096 }
1097
1098 #define READ_CHUNK 8196
1099 int r;
1100 int err;
1101
1102 do {
1103 char buf[READ_CHUNK];
1104
1105 r = safe_read(fd, buf, READ_CHUNK);
1106 if (r < 0) {
1107 err = -errno;
1108 cerr << "error while reading input" << std::endl;
1109 goto out;
1110 }
1111 bl.append(buf, r);
1112 } while (r > 0);
1113 err = 0;
1114
1115 out:
1116 if (infile.size()) {
1117 close(fd);
1118 }
1119 return err;
1120 }
1121
1122 template <class T>
1123 static int read_decode_json(const string& infile, T& t)
1124 {
1125 bufferlist bl;
1126 int ret = read_input(infile, bl);
1127 if (ret < 0) {
1128 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
1129 return ret;
1130 }
1131 JSONParser p;
1132 if (!p.parse(bl.c_str(), bl.length())) {
1133 cout << "failed to parse JSON" << std::endl;
1134 return -EINVAL;
1135 }
1136
1137 try {
1138 decode_json_obj(t, &p);
1139 } catch (JSONDecoder::err& e) {
1140 cout << "failed to decode JSON input: " << e.message << std::endl;
1141 return -EINVAL;
1142 }
1143 return 0;
1144 }
1145
1146 template <class T, class K>
1147 static int read_decode_json(const string& infile, T& t, K *k)
1148 {
1149 bufferlist bl;
1150 int ret = read_input(infile, bl);
1151 if (ret < 0) {
1152 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
1153 return ret;
1154 }
1155 JSONParser p;
1156 if (!p.parse(bl.c_str(), bl.length())) {
1157 cout << "failed to parse JSON" << std::endl;
1158 return -EINVAL;
1159 }
1160
1161 try {
1162 t.decode_json(&p, k);
1163 } catch (JSONDecoder::err& e) {
1164 cout << "failed to decode JSON input: " << e.message << std::endl;
1165 return -EINVAL;
1166 }
1167 return 0;
1168 }
1169
1170 static int parse_date_str(const string& date_str, utime_t& ut)
1171 {
1172 uint64_t epoch = 0;
1173 uint64_t nsec = 0;
1174
1175 if (!date_str.empty()) {
1176 int ret = utime_t::parse_date(date_str, &epoch, &nsec);
1177 if (ret < 0) {
1178 cerr << "ERROR: failed to parse date: " << date_str << std::endl;
1179 return -EINVAL;
1180 }
1181 }
1182
1183 ut = utime_t(epoch, nsec);
1184
1185 return 0;
1186 }
1187
1188 template <class T>
1189 static bool decode_dump(const char *field_name, bufferlist& bl, Formatter *f)
1190 {
1191 T t;
1192
1193 bufferlist::iterator iter = bl.begin();
1194
1195 try {
1196 ::decode(t, iter);
1197 } catch (buffer::error& err) {
1198 return false;
1199 }
1200
1201 encode_json(field_name, t, f);
1202
1203 return true;
1204 }
1205
1206 static bool dump_string(const char *field_name, bufferlist& bl, Formatter *f)
1207 {
1208 string val;
1209 if (bl.length() > 0) {
1210 val.assign(bl.c_str());
1211 }
1212 f->dump_string(field_name, val);
1213
1214 return true;
1215 }
1216
1217 void set_quota_info(RGWQuotaInfo& quota, int opt_cmd, int64_t max_size, int64_t max_objects,
1218 bool have_max_size, bool have_max_objects)
1219 {
1220 switch (opt_cmd) {
1221 case OPT_QUOTA_ENABLE:
1222 case OPT_GLOBAL_QUOTA_ENABLE:
1223 quota.enabled = true;
1224
1225 // falling through on purpose
1226
1227 case OPT_QUOTA_SET:
1228 case OPT_GLOBAL_QUOTA_SET:
1229 if (have_max_objects) {
1230 if (max_objects < 0) {
1231 quota.max_objects = -1;
1232 } else {
1233 quota.max_objects = max_objects;
1234 }
1235 }
1236 if (have_max_size) {
1237 if (max_size < 0) {
1238 quota.max_size = -1;
1239 } else {
1240 quota.max_size = rgw_rounded_kb(max_size) * 1024;
1241 }
1242 }
1243 break;
1244 case OPT_QUOTA_DISABLE:
1245 case OPT_GLOBAL_QUOTA_DISABLE:
1246 quota.enabled = false;
1247 break;
1248 }
1249 }
1250
1251 int set_bucket_quota(RGWRados *store, int opt_cmd,
1252 const string& tenant_name, const string& bucket_name,
1253 int64_t max_size, int64_t max_objects,
1254 bool have_max_size, bool have_max_objects)
1255 {
1256 RGWBucketInfo bucket_info;
1257 map<string, bufferlist> attrs;
1258 RGWObjectCtx obj_ctx(store);
1259 int r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, NULL, &attrs);
1260 if (r < 0) {
1261 cerr << "could not get bucket info for bucket=" << bucket_name << ": " << cpp_strerror(-r) << std::endl;
1262 return -r;
1263 }
1264
1265 set_quota_info(bucket_info.quota, opt_cmd, max_size, max_objects, have_max_size, have_max_objects);
1266
1267 r = store->put_bucket_instance_info(bucket_info, false, real_time(), &attrs);
1268 if (r < 0) {
1269 cerr << "ERROR: failed writing bucket instance info: " << cpp_strerror(-r) << std::endl;
1270 return -r;
1271 }
1272 return 0;
1273 }
1274
1275 int set_user_bucket_quota(int opt_cmd, RGWUser& user, RGWUserAdminOpState& op_state, int64_t max_size, int64_t max_objects,
1276 bool have_max_size, bool have_max_objects)
1277 {
1278 RGWUserInfo& user_info = op_state.get_user_info();
1279
1280 set_quota_info(user_info.bucket_quota, opt_cmd, max_size, max_objects, have_max_size, have_max_objects);
1281
1282 op_state.set_bucket_quota(user_info.bucket_quota);
1283
1284 string err;
1285 int r = user.modify(op_state, &err);
1286 if (r < 0) {
1287 cerr << "ERROR: failed updating user info: " << cpp_strerror(-r) << ": " << err << std::endl;
1288 return -r;
1289 }
1290 return 0;
1291 }
1292
1293 int set_user_quota(int opt_cmd, RGWUser& user, RGWUserAdminOpState& op_state, int64_t max_size, int64_t max_objects,
1294 bool have_max_size, bool have_max_objects)
1295 {
1296 RGWUserInfo& user_info = op_state.get_user_info();
1297
1298 set_quota_info(user_info.user_quota, opt_cmd, max_size, max_objects, have_max_size, have_max_objects);
1299
1300 op_state.set_user_quota(user_info.user_quota);
1301
1302 string err;
1303 int r = user.modify(op_state, &err);
1304 if (r < 0) {
1305 cerr << "ERROR: failed updating user info: " << cpp_strerror(-r) << ": " << err << std::endl;
1306 return -r;
1307 }
1308 return 0;
1309 }
1310
1311 static bool bucket_object_check_filter(const string& name)
1312 {
1313 rgw_obj_key k;
1314 string ns; /* empty namespace */
1315 return rgw_obj_key::oid_to_key_in_ns(name, &k, ns);
1316 }
1317
1318 int check_min_obj_stripe_size(RGWRados *store, RGWBucketInfo& bucket_info, rgw_obj& obj, uint64_t min_stripe_size, bool *need_rewrite)
1319 {
1320 map<string, bufferlist> attrs;
1321 uint64_t obj_size;
1322
1323 RGWObjectCtx obj_ctx(store);
1324 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
1325 RGWRados::Object::Read read_op(&op_target);
1326
1327 read_op.params.attrs = &attrs;
1328 read_op.params.obj_size = &obj_size;
1329
1330 int ret = read_op.prepare();
1331 if (ret < 0) {
1332 lderr(store->ctx()) << "ERROR: failed to stat object, returned error: " << cpp_strerror(-ret) << dendl;
1333 return ret;
1334 }
1335
1336 map<string, bufferlist>::iterator iter;
1337 iter = attrs.find(RGW_ATTR_MANIFEST);
1338 if (iter == attrs.end()) {
1339 *need_rewrite = (obj_size >= min_stripe_size);
1340 return 0;
1341 }
1342
1343 RGWObjManifest manifest;
1344
1345 try {
1346 bufferlist& bl = iter->second;
1347 bufferlist::iterator biter = bl.begin();
1348 ::decode(manifest, biter);
1349 } catch (buffer::error& err) {
1350 ldout(store->ctx(), 0) << "ERROR: failed to decode manifest" << dendl;
1351 return -EIO;
1352 }
1353
1354 map<uint64_t, RGWObjManifestPart>& objs = manifest.get_explicit_objs();
1355 map<uint64_t, RGWObjManifestPart>::iterator oiter;
1356 for (oiter = objs.begin(); oiter != objs.end(); ++oiter) {
1357 RGWObjManifestPart& part = oiter->second;
1358
1359 if (part.size >= min_stripe_size) {
1360 *need_rewrite = true;
1361 return 0;
1362 }
1363 }
1364 *need_rewrite = false;
1365
1366 return 0;
1367 }
1368
1369
1370 int check_obj_locator_underscore(RGWBucketInfo& bucket_info, rgw_obj& obj, rgw_obj_key& key, bool fix, bool remove_bad, Formatter *f) {
1371 f->open_object_section("object");
1372 f->open_object_section("key");
1373 f->dump_string("type", "head");
1374 f->dump_string("name", key.name);
1375 f->dump_string("instance", key.instance);
1376 f->close_section();
1377
1378 string oid;
1379 string locator;
1380
1381 get_obj_bucket_and_oid_loc(obj, oid, locator);
1382
1383 f->dump_string("oid", oid);
1384 f->dump_string("locator", locator);
1385
1386
1387 RGWObjectCtx obj_ctx(store);
1388
1389 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
1390 RGWRados::Object::Read read_op(&op_target);
1391
1392 int ret = read_op.prepare();
1393 bool needs_fixing = (ret == -ENOENT);
1394
1395 f->dump_bool("needs_fixing", needs_fixing);
1396
1397 string status = (needs_fixing ? "needs_fixing" : "ok");
1398
1399 if ((needs_fixing || remove_bad) && fix) {
1400 ret = store->fix_head_obj_locator(bucket_info, needs_fixing, remove_bad, key);
1401 if (ret < 0) {
1402 cerr << "ERROR: fix_head_object_locator() returned ret=" << ret << std::endl;
1403 goto done;
1404 }
1405 status = "fixed";
1406 }
1407
1408 done:
1409 f->dump_string("status", status);
1410
1411 f->close_section();
1412
1413 return 0;
1414 }
1415
1416 int check_obj_tail_locator_underscore(RGWBucketInfo& bucket_info, rgw_obj& obj, rgw_obj_key& key, bool fix, Formatter *f) {
1417 f->open_object_section("object");
1418 f->open_object_section("key");
1419 f->dump_string("type", "tail");
1420 f->dump_string("name", key.name);
1421 f->dump_string("instance", key.instance);
1422 f->close_section();
1423
1424 bool needs_fixing;
1425 string status;
1426
1427 int ret = store->fix_tail_obj_locator(bucket_info, key, fix, &needs_fixing);
1428 if (ret < 0) {
1429 cerr << "ERROR: fix_tail_object_locator_underscore() returned ret=" << ret << std::endl;
1430 status = "failed";
1431 } else {
1432 status = (needs_fixing && !fix ? "needs_fixing" : "ok");
1433 }
1434
1435 f->dump_bool("needs_fixing", needs_fixing);
1436 f->dump_string("status", status);
1437
1438 f->close_section();
1439
1440 return 0;
1441 }
1442
1443 int do_check_object_locator(const string& tenant_name, const string& bucket_name,
1444 bool fix, bool remove_bad, Formatter *f)
1445 {
1446 if (remove_bad && !fix) {
1447 cerr << "ERROR: can't have remove_bad specified without fix" << std::endl;
1448 return -EINVAL;
1449 }
1450
1451 RGWBucketInfo bucket_info;
1452 rgw_bucket bucket;
1453 string bucket_id;
1454
1455 f->open_object_section("bucket");
1456 f->dump_string("bucket", bucket_name);
1457 int ret = init_bucket(tenant_name, bucket_name, bucket_id, bucket_info, bucket);
1458 if (ret < 0) {
1459 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
1460 return ret;
1461 }
1462 bool truncated;
1463 int count = 0;
1464
1465 int max_entries = 1000;
1466
1467 string prefix;
1468 string delim;
1469 vector<rgw_bucket_dir_entry> result;
1470 map<string, bool> common_prefixes;
1471 string ns;
1472
1473 RGWRados::Bucket target(store, bucket_info);
1474 RGWRados::Bucket::List list_op(&target);
1475
1476 string marker;
1477
1478 list_op.params.prefix = prefix;
1479 list_op.params.delim = delim;
1480 list_op.params.marker = rgw_obj_key(marker);
1481 list_op.params.ns = ns;
1482 list_op.params.enforce_ns = true;
1483 list_op.params.list_versions = true;
1484
1485 f->open_array_section("check_objects");
1486 do {
1487 ret = list_op.list_objects(max_entries - count, &result, &common_prefixes, &truncated);
1488 if (ret < 0) {
1489 cerr << "ERROR: store->list_objects(): " << cpp_strerror(-ret) << std::endl;
1490 return -ret;
1491 }
1492
1493 count += result.size();
1494
1495 for (vector<rgw_bucket_dir_entry>::iterator iter = result.begin(); iter != result.end(); ++iter) {
1496 rgw_obj_key key = iter->key;
1497 rgw_obj obj(bucket, key);
1498
1499 if (key.name[0] == '_') {
1500 ret = check_obj_locator_underscore(bucket_info, obj, key, fix, remove_bad, f);
1501
1502 if (ret >= 0) {
1503 ret = check_obj_tail_locator_underscore(bucket_info, obj, key, fix, f);
1504 }
1505 }
1506 }
1507 f->flush(cout);
1508 } while (truncated && count < max_entries);
1509 f->close_section();
1510 f->close_section();
1511
1512 f->flush(cout);
1513
1514 return 0;
1515 }
1516
1517 int set_bucket_sync_enabled(RGWRados *store, int opt_cmd, const string& tenant_name, const string& bucket_name)
1518 {
1519 RGWBucketInfo bucket_info;
1520 map<string, bufferlist> attrs;
1521 RGWObjectCtx obj_ctx(store);
1522
1523 int r = store->get_bucket_info(obj_ctx, tenant_name, bucket_name, bucket_info, NULL, &attrs);
1524 if (r < 0) {
1525 cerr << "could not get bucket info for bucket=" << bucket_name << ": " << cpp_strerror(-r) << std::endl;
1526 return -r;
1527 }
1528
1529 if (opt_cmd == OPT_BUCKET_SYNC_ENABLE) {
1530 bucket_info.flags &= ~BUCKET_DATASYNC_DISABLED;
1531 } else if (opt_cmd == OPT_BUCKET_SYNC_DISABLE) {
1532 bucket_info.flags |= BUCKET_DATASYNC_DISABLED;
1533 }
1534
1535 r = store->put_bucket_instance_info(bucket_info, false, real_time(), &attrs);
1536 if (r < 0) {
1537 cerr << "ERROR: failed writing bucket instance info: " << cpp_strerror(-r) << std::endl;
1538 return -r;
1539 }
1540
1541 int shards_num = bucket_info.num_shards? bucket_info.num_shards : 1;
1542 int shard_id = bucket_info.num_shards? 0 : -1;
1543
1544 if (opt_cmd == OPT_BUCKET_SYNC_DISABLE) {
1545 r = store->stop_bi_log_entries(bucket_info, -1);
1546 if (r < 0) {
1547 lderr(store->ctx()) << "ERROR: failed writing stop bilog" << dendl;
1548 return r;
1549 }
1550 } else {
1551 r = store->resync_bi_log_entries(bucket_info, -1);
1552 if (r < 0) {
1553 lderr(store->ctx()) << "ERROR: failed writing resync bilog" << dendl;
1554 return r;
1555 }
1556 }
1557
1558 for (int i = 0; i < shards_num; ++i, ++shard_id) {
1559 r = store->data_log->add_entry(bucket_info.bucket, shard_id);
1560 if (r < 0) {
1561 lderr(store->ctx()) << "ERROR: failed writing data log" << dendl;
1562 return r;
1563 }
1564 }
1565
1566 return 0;
1567 }
1568
1569
1570 /// search for a matching zone/zonegroup id and return a connection if found
1571 static boost::optional<RGWRESTConn> get_remote_conn(RGWRados *store,
1572 const RGWZoneGroup& zonegroup,
1573 const std::string& remote)
1574 {
1575 boost::optional<RGWRESTConn> conn;
1576 if (remote == zonegroup.get_id()) {
1577 conn.emplace(store->ctx(), store, remote, zonegroup.endpoints);
1578 } else {
1579 for (const auto& z : zonegroup.zones) {
1580 const auto& zone = z.second;
1581 if (remote == zone.id) {
1582 conn.emplace(store->ctx(), store, remote, zone.endpoints);
1583 break;
1584 }
1585 }
1586 }
1587 return conn;
1588 }
1589
1590 /// search each zonegroup for a connection
1591 static boost::optional<RGWRESTConn> get_remote_conn(RGWRados *store,
1592 const RGWPeriodMap& period_map,
1593 const std::string& remote)
1594 {
1595 boost::optional<RGWRESTConn> conn;
1596 for (const auto& zg : period_map.zonegroups) {
1597 conn = get_remote_conn(store, zg.second, remote);
1598 if (conn) {
1599 break;
1600 }
1601 }
1602 return conn;
1603 }
1604
1605 // we expect a very small response
1606 static constexpr size_t MAX_REST_RESPONSE = 128 * 1024;
1607
1608 static int send_to_remote_gateway(RGWRESTConn* conn, req_info& info,
1609 bufferlist& in_data, JSONParser& parser)
1610 {
1611 if (!conn) {
1612 return -EINVAL;
1613 }
1614
1615 ceph::bufferlist response;
1616 rgw_user user;
1617 int ret = conn->forward(user, info, nullptr, MAX_REST_RESPONSE, &in_data, &response);
1618
1619 int parse_ret = parser.parse(response.c_str(), response.length());
1620 if (parse_ret < 0) {
1621 cerr << "failed to parse response" << std::endl;
1622 return parse_ret;
1623 }
1624 return ret;
1625 }
1626
1627 static int send_to_url(const string& url, const string& access,
1628 const string& secret, req_info& info,
1629 bufferlist& in_data, JSONParser& parser)
1630 {
1631 if (access.empty() || secret.empty()) {
1632 cerr << "An --access-key and --secret must be provided with --url." << std::endl;
1633 return -EINVAL;
1634 }
1635 RGWAccessKey key;
1636 key.id = access;
1637 key.key = secret;
1638
1639 param_vec_t params;
1640 RGWRESTSimpleRequest req(g_ceph_context, url, NULL, &params);
1641
1642 bufferlist response;
1643 int ret = req.forward_request(key, info, MAX_REST_RESPONSE, &in_data, &response);
1644
1645 int parse_ret = parser.parse(response.c_str(), response.length());
1646 if (parse_ret < 0) {
1647 cout << "failed to parse response" << std::endl;
1648 return parse_ret;
1649 }
1650 return ret;
1651 }
1652
1653 static int send_to_remote_or_url(RGWRESTConn *conn, const string& url,
1654 const string& access, const string& secret,
1655 req_info& info, bufferlist& in_data,
1656 JSONParser& parser)
1657 {
1658 if (url.empty()) {
1659 return send_to_remote_gateway(conn, info, in_data, parser);
1660 }
1661 return send_to_url(url, access, secret, info, in_data, parser);
1662 }
1663
1664 static int commit_period(RGWRealm& realm, RGWPeriod& period,
1665 string remote, const string& url,
1666 const string& access, const string& secret,
1667 bool force)
1668 {
1669 const string& master_zone = period.get_master_zone();
1670 if (master_zone.empty()) {
1671 cerr << "cannot commit period: period does not have a master zone of a master zonegroup" << std::endl;
1672 return -EINVAL;
1673 }
1674 // are we the period's master zone?
1675 if (store->get_zone_params().get_id() == master_zone) {
1676 // read the current period
1677 RGWPeriod current_period;
1678 int ret = current_period.init(g_ceph_context, store, realm.get_id());
1679 if (ret < 0) {
1680 cerr << "Error initializing current period: "
1681 << cpp_strerror(-ret) << std::endl;
1682 return ret;
1683 }
1684 // the master zone can commit locally
1685 ret = period.commit(realm, current_period, cerr, force);
1686 if (ret < 0) {
1687 cerr << "failed to commit period: " << cpp_strerror(-ret) << std::endl;
1688 }
1689 return ret;
1690 }
1691
1692 if (remote.empty() && url.empty()) {
1693 // use the new master zone's connection
1694 remote = master_zone;
1695 cout << "Sending period to new master zone " << remote << std::endl;
1696 }
1697 boost::optional<RGWRESTConn> conn;
1698 RGWRESTConn *remote_conn = nullptr;
1699 if (!remote.empty()) {
1700 conn = get_remote_conn(store, period.get_map(), remote);
1701 if (!conn) {
1702 cerr << "failed to find a zone or zonegroup for remote "
1703 << remote << std::endl;
1704 return -ENOENT;
1705 }
1706 remote_conn = &*conn;
1707 }
1708
1709 // push period to the master with an empty period id
1710 period.set_id("");
1711
1712 RGWEnv env;
1713 req_info info(g_ceph_context, &env);
1714 info.method = "POST";
1715 info.request_uri = "/admin/realm/period";
1716
1717 // json format into a bufferlist
1718 JSONFormatter jf(false);
1719 encode_json("period", period, &jf);
1720 bufferlist bl;
1721 jf.flush(bl);
1722
1723 JSONParser p;
1724 int ret = send_to_remote_or_url(remote_conn, url, access, secret, info, bl, p);
1725 if (ret < 0) {
1726 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
1727
1728 // did we parse an error message?
1729 auto message = p.find_obj("Message");
1730 if (message) {
1731 cerr << "Reason: " << message->get_data() << std::endl;
1732 }
1733 return ret;
1734 }
1735
1736 // decode the response and store it back
1737 try {
1738 decode_json_obj(period, &p);
1739 } catch (JSONDecoder::err& e) {
1740 cout << "failed to decode JSON input: " << e.message << std::endl;
1741 return -EINVAL;
1742 }
1743 if (period.get_id().empty()) {
1744 cerr << "Period commit got back an empty period id" << std::endl;
1745 return -EINVAL;
1746 }
1747 // the master zone gave us back the period that it committed, so it's
1748 // safe to save it as our latest epoch
1749 ret = period.store_info(false);
1750 if (ret < 0) {
1751 cerr << "Error storing committed period " << period.get_id() << ": "
1752 << cpp_strerror(ret) << std::endl;
1753 return ret;
1754 }
1755 ret = period.set_latest_epoch(period.get_epoch());
1756 if (ret < 0) {
1757 cerr << "Error updating period epoch: " << cpp_strerror(ret) << std::endl;
1758 return ret;
1759 }
1760 ret = period.reflect();
1761 if (ret < 0) {
1762 cerr << "Error updating local objects: " << cpp_strerror(ret) << std::endl;
1763 return ret;
1764 }
1765 realm.notify_new_period(period);
1766 return ret;
1767 }
1768
1769 static int update_period(const string& realm_id, const string& realm_name,
1770 const string& period_id, const string& period_epoch,
1771 bool commit, const string& remote, const string& url,
1772 const string& access, const string& secret,
1773 Formatter *formatter, bool force)
1774 {
1775 RGWRealm realm(realm_id, realm_name);
1776 int ret = realm.init(g_ceph_context, store);
1777 if (ret < 0 ) {
1778 cerr << "Error initializing realm " << cpp_strerror(-ret) << std::endl;
1779 return ret;
1780 }
1781 epoch_t epoch = 0;
1782 if (!period_epoch.empty()) {
1783 epoch = atoi(period_epoch.c_str());
1784 }
1785 RGWPeriod period(period_id, epoch);
1786 ret = period.init(g_ceph_context, store, realm.get_id());
1787 if (ret < 0) {
1788 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
1789 return ret;
1790 }
1791 period.fork();
1792 ret = period.update();
1793 if(ret < 0) {
1794 // Dropping the error message here, as both the ret codes were handled in
1795 // period.update()
1796 return ret;
1797 }
1798 ret = period.store_info(false);
1799 if (ret < 0) {
1800 cerr << "failed to store period: " << cpp_strerror(-ret) << std::endl;
1801 return ret;
1802 }
1803 if (commit) {
1804 ret = commit_period(realm, period, remote, url, access, secret, force);
1805 if (ret < 0) {
1806 cerr << "failed to commit period: " << cpp_strerror(-ret) << std::endl;
1807 return ret;
1808 }
1809 }
1810 encode_json("period", period, formatter);
1811 formatter->flush(cout);
1812 cout << std::endl;
1813 return 0;
1814 }
1815
1816 static int init_bucket_for_sync(const string& tenant, const string& bucket_name,
1817 const string& bucket_id, rgw_bucket& bucket)
1818 {
1819 RGWBucketInfo bucket_info;
1820
1821 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
1822 if (ret < 0) {
1823 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
1824 return ret;
1825 }
1826
1827 return 0;
1828 }
1829
1830 static int do_period_pull(RGWRESTConn *remote_conn, const string& url,
1831 const string& access_key, const string& secret_key,
1832 const string& realm_id, const string& realm_name,
1833 const string& period_id, const string& period_epoch,
1834 RGWPeriod *period)
1835 {
1836 RGWEnv env;
1837 req_info info(g_ceph_context, &env);
1838 info.method = "GET";
1839 info.request_uri = "/admin/realm/period";
1840
1841 map<string, string> &params = info.args.get_params();
1842 if (!realm_id.empty())
1843 params["realm_id"] = realm_id;
1844 if (!realm_name.empty())
1845 params["realm_name"] = realm_name;
1846 if (!period_id.empty())
1847 params["period_id"] = period_id;
1848 if (!period_epoch.empty())
1849 params["epoch"] = period_epoch;
1850
1851 bufferlist bl;
1852 JSONParser p;
1853 int ret = send_to_remote_or_url(remote_conn, url, access_key, secret_key,
1854 info, bl, p);
1855 if (ret < 0) {
1856 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
1857 return ret;
1858 }
1859 ret = period->init(g_ceph_context, store, false);
1860 if (ret < 0) {
1861 cerr << "faile to init period " << cpp_strerror(-ret) << std::endl;
1862 return ret;
1863 }
1864 try {
1865 decode_json_obj(*period, &p);
1866 } catch (JSONDecoder::err& e) {
1867 cout << "failed to decode JSON input: " << e.message << std::endl;
1868 return -EINVAL;
1869 }
1870 ret = period->store_info(false);
1871 if (ret < 0) {
1872 cerr << "Error storing period " << period->get_id() << ": " << cpp_strerror(ret) << std::endl;
1873 }
1874 // store latest epoch (ignore errors)
1875 period->update_latest_epoch(period->get_epoch());
1876 return 0;
1877 }
1878
1879 static int read_current_period_id(RGWRados* store, const std::string& realm_id,
1880 const std::string& realm_name,
1881 std::string* period_id)
1882 {
1883 RGWRealm realm(realm_id, realm_name);
1884 int ret = realm.init(g_ceph_context, store);
1885 if (ret < 0) {
1886 std::cerr << "failed to read realm: " << cpp_strerror(-ret) << std::endl;
1887 return ret;
1888 }
1889 *period_id = realm.get_current_period();
1890 return 0;
1891 }
1892
1893 void flush_ss(stringstream& ss, list<string>& l)
1894 {
1895 if (!ss.str().empty()) {
1896 l.push_back(ss.str());
1897 }
1898 ss.str("");
1899 }
1900
1901 stringstream& push_ss(stringstream& ss, list<string>& l, int tab = 0)
1902 {
1903 flush_ss(ss, l);
1904 if (tab > 0) {
1905 ss << setw(tab) << "" << setw(1);
1906 }
1907 return ss;
1908 }
1909
1910 static void get_md_sync_status(list<string>& status)
1911 {
1912 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
1913
1914 int ret = sync.init();
1915 if (ret < 0) {
1916 status.push_back(string("failed to retrieve sync info: sync.init() failed: ") + cpp_strerror(-ret));
1917 return;
1918 }
1919
1920 rgw_meta_sync_status sync_status;
1921 ret = sync.read_sync_status(&sync_status);
1922 if (ret < 0) {
1923 status.push_back(string("failed to read sync status: ") + cpp_strerror(-ret));
1924 return;
1925 }
1926
1927 string status_str;
1928 switch (sync_status.sync_info.state) {
1929 case rgw_meta_sync_info::StateInit:
1930 status_str = "init";
1931 break;
1932 case rgw_meta_sync_info::StateBuildingFullSyncMaps:
1933 status_str = "preparing for full sync";
1934 break;
1935 case rgw_meta_sync_info::StateSync:
1936 status_str = "syncing";
1937 break;
1938 default:
1939 status_str = "unknown";
1940 }
1941
1942 status.push_back(status_str);
1943
1944 uint64_t full_total = 0;
1945 uint64_t full_complete = 0;
1946
1947 int num_full = 0;
1948 int num_inc = 0;
1949 int total_shards = 0;
1950 set<int> shards_behind_set;
1951
1952 for (auto marker_iter : sync_status.sync_markers) {
1953 full_total += marker_iter.second.total_entries;
1954 total_shards++;
1955 int shard_id = marker_iter.first;
1956 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
1957 num_full++;
1958 full_complete += marker_iter.second.pos;
1959 shards_behind_set.insert(shard_id);
1960 } else {
1961 full_complete += marker_iter.second.total_entries;
1962 }
1963 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::IncrementalSync) {
1964 num_inc++;
1965 }
1966 }
1967
1968 stringstream ss;
1969 push_ss(ss, status) << "full sync: " << num_full << "/" << total_shards << " shards";
1970
1971 if (num_full > 0) {
1972 push_ss(ss, status) << "full sync: " << full_total - full_complete << " entries to sync";
1973 }
1974
1975 push_ss(ss, status) << "incremental sync: " << num_inc << "/" << total_shards << " shards";
1976
1977 rgw_mdlog_info log_info;
1978 ret = sync.read_log_info(&log_info);
1979 if (ret < 0) {
1980 status.push_back(string("failed to fetch local sync status: ") + cpp_strerror(-ret));
1981 return;
1982 }
1983
1984 map<int, RGWMetadataLogInfo> master_shards_info;
1985 string master_period = store->get_current_period_id();
1986
1987 ret = sync.read_master_log_shards_info(master_period, &master_shards_info);
1988 if (ret < 0) {
1989 status.push_back(string("failed to fetch master sync status: ") + cpp_strerror(-ret));
1990 return;
1991 }
1992
1993 map<int, string> shards_behind;
1994 if (sync_status.sync_info.period != master_period) {
1995 status.push_back(string("master is on a different period: master_period=" +
1996 master_period + " local_period=" + sync_status.sync_info.period));
1997 } else {
1998 for (auto local_iter : sync_status.sync_markers) {
1999 int shard_id = local_iter.first;
2000 auto iter = master_shards_info.find(shard_id);
2001
2002 if (iter == master_shards_info.end()) {
2003 /* huh? */
2004 derr << "ERROR: could not find remote sync shard status for shard_id=" << shard_id << dendl;
2005 continue;
2006 }
2007 auto master_marker = iter->second.marker;
2008 if (local_iter.second.state == rgw_meta_sync_marker::SyncState::IncrementalSync &&
2009 master_marker > local_iter.second.marker) {
2010 shards_behind[shard_id] = local_iter.second.marker;
2011 shards_behind_set.insert(shard_id);
2012 }
2013 }
2014 }
2015
2016 int total_behind = shards_behind.size() + (sync_status.sync_info.num_shards - num_inc);
2017 if (total_behind == 0) {
2018 push_ss(ss, status) << "metadata is caught up with master";
2019 } else {
2020 push_ss(ss, status) << "metadata is behind on " << total_behind << " shards";
2021
2022 push_ss(ss, status) << "behind shards: " << "[" << shards_behind_set << "]";
2023
2024 map<int, rgw_mdlog_shard_data> master_pos;
2025 ret = sync.read_master_log_shards_next(sync_status.sync_info.period, shards_behind, &master_pos);
2026 if (ret < 0) {
2027 derr << "ERROR: failed to fetch master next positions (" << cpp_strerror(-ret) << ")" << dendl;
2028 } else {
2029 ceph::real_time oldest;
2030 for (auto iter : master_pos) {
2031 rgw_mdlog_shard_data& shard_data = iter.second;
2032
2033 if (!shard_data.entries.empty()) {
2034 rgw_mdlog_entry& entry = shard_data.entries.front();
2035 if (ceph::real_clock::is_zero(oldest)) {
2036 oldest = entry.timestamp;
2037 } else if (!ceph::real_clock::is_zero(entry.timestamp) && entry.timestamp < oldest) {
2038 oldest = entry.timestamp;
2039 }
2040 }
2041 }
2042
2043 if (!ceph::real_clock::is_zero(oldest)) {
2044 push_ss(ss, status) << "oldest incremental change not applied: " << oldest;
2045 }
2046 }
2047 }
2048
2049 flush_ss(ss, status);
2050 }
2051
2052 static void get_data_sync_status(const string& source_zone, list<string>& status, int tab)
2053 {
2054 stringstream ss;
2055
2056 auto ziter = store->zone_by_id.find(source_zone);
2057 if (ziter == store->zone_by_id.end()) {
2058 push_ss(ss, status, tab) << string("zone not found");
2059 flush_ss(ss, status);
2060 return;
2061 }
2062 RGWZone& sz = ziter->second;
2063
2064 if (!store->zone_syncs_from(store->get_zone(), sz)) {
2065 push_ss(ss, status, tab) << string("not syncing from zone");
2066 flush_ss(ss, status);
2067 return;
2068 }
2069 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
2070
2071 int ret = sync.init();
2072 if (ret < 0) {
2073 push_ss(ss, status, tab) << string("failed to retrieve sync info: ") + cpp_strerror(-ret);
2074 flush_ss(ss, status);
2075 return;
2076 }
2077
2078 rgw_data_sync_status sync_status;
2079 ret = sync.read_sync_status(&sync_status);
2080 if (ret < 0 && ret != -ENOENT) {
2081 push_ss(ss, status, tab) << string("failed read sync status: ") + cpp_strerror(-ret);
2082 return;
2083 }
2084
2085 set<int> recovering_shards;
2086 ret = sync.read_recovering_shards(sync_status.sync_info.num_shards, recovering_shards);
2087 if (ret < 0 && ret != ENOENT) {
2088 push_ss(ss, status, tab) << string("failed read recovering shards: ") + cpp_strerror(-ret);
2089 return;
2090 }
2091
2092 string status_str;
2093 switch (sync_status.sync_info.state) {
2094 case rgw_data_sync_info::StateInit:
2095 status_str = "init";
2096 break;
2097 case rgw_data_sync_info::StateBuildingFullSyncMaps:
2098 status_str = "preparing for full sync";
2099 break;
2100 case rgw_data_sync_info::StateSync:
2101 status_str = "syncing";
2102 break;
2103 default:
2104 status_str = "unknown";
2105 }
2106
2107 push_ss(ss, status, tab) << status_str;
2108
2109 uint64_t full_total = 0;
2110 uint64_t full_complete = 0;
2111
2112 int num_full = 0;
2113 int num_inc = 0;
2114 int total_shards = 0;
2115 set<int> shards_behind_set;
2116
2117 for (auto marker_iter : sync_status.sync_markers) {
2118 int shard_id = marker_iter.first;
2119 full_total += marker_iter.second.total_entries;
2120 total_shards++;
2121 if (marker_iter.second.state == rgw_data_sync_marker::SyncState::FullSync) {
2122 num_full++;
2123 full_complete += marker_iter.second.pos;
2124 shards_behind_set.insert(shard_id);
2125 } else {
2126 full_complete += marker_iter.second.total_entries;
2127 }
2128 if (marker_iter.second.state == rgw_data_sync_marker::SyncState::IncrementalSync) {
2129 num_inc++;
2130 }
2131 }
2132
2133 push_ss(ss, status, tab) << "full sync: " << num_full << "/" << total_shards << " shards";
2134
2135 if (num_full > 0) {
2136 push_ss(ss, status, tab) << "full sync: " << full_total - full_complete << " buckets to sync";
2137 }
2138
2139 push_ss(ss, status, tab) << "incremental sync: " << num_inc << "/" << total_shards << " shards";
2140
2141 rgw_datalog_info log_info;
2142 ret = sync.read_log_info(&log_info);
2143 if (ret < 0) {
2144 push_ss(ss, status, tab) << string("failed to fetch local sync status: ") + cpp_strerror(-ret);
2145 return;
2146 }
2147
2148
2149 map<int, RGWDataChangesLogInfo> source_shards_info;
2150
2151 ret = sync.read_source_log_shards_info(&source_shards_info);
2152 if (ret < 0) {
2153 push_ss(ss, status, tab) << string("failed to fetch source sync status: ") + cpp_strerror(-ret);
2154 return;
2155 }
2156
2157 map<int, string> shards_behind;
2158
2159 for (auto local_iter : sync_status.sync_markers) {
2160 int shard_id = local_iter.first;
2161 auto iter = source_shards_info.find(shard_id);
2162
2163 if (iter == source_shards_info.end()) {
2164 /* huh? */
2165 derr << "ERROR: could not find remote sync shard status for shard_id=" << shard_id << dendl;
2166 continue;
2167 }
2168 auto master_marker = iter->second.marker;
2169 if (local_iter.second.state == rgw_data_sync_marker::SyncState::IncrementalSync &&
2170 master_marker > local_iter.second.marker) {
2171 shards_behind[shard_id] = local_iter.second.marker;
2172 shards_behind_set.insert(shard_id);
2173 }
2174 }
2175
2176 int total_behind = shards_behind.size() + (sync_status.sync_info.num_shards - num_inc);
2177 int total_recovering = recovering_shards.size();
2178 if (total_behind == 0 && total_recovering == 0) {
2179 push_ss(ss, status, tab) << "data is caught up with source";
2180 } else if (total_behind > 0) {
2181 push_ss(ss, status, tab) << "data is behind on " << total_behind << " shards";
2182
2183 push_ss(ss, status, tab) << "behind shards: " << "[" << shards_behind_set << "]" ;
2184
2185 map<int, rgw_datalog_shard_data> master_pos;
2186 ret = sync.read_source_log_shards_next(shards_behind, &master_pos);
2187 if (ret < 0) {
2188 derr << "ERROR: failed to fetch next positions (" << cpp_strerror(-ret) << ")" << dendl;
2189 } else {
2190 ceph::real_time oldest;
2191 for (auto iter : master_pos) {
2192 rgw_datalog_shard_data& shard_data = iter.second;
2193
2194 if (!shard_data.entries.empty()) {
2195 rgw_datalog_entry& entry = shard_data.entries.front();
2196 if (ceph::real_clock::is_zero(oldest)) {
2197 oldest = entry.timestamp;
2198 } else if (!ceph::real_clock::is_zero(entry.timestamp) && entry.timestamp < oldest) {
2199 oldest = entry.timestamp;
2200 }
2201 }
2202 }
2203
2204 if (!ceph::real_clock::is_zero(oldest)) {
2205 push_ss(ss, status, tab) << "oldest incremental change not applied: " << oldest;
2206 }
2207 }
2208 }
2209
2210 if (total_recovering > 0) {
2211 push_ss(ss, status, tab) << total_recovering << " shards are recovering";
2212 push_ss(ss, status, tab) << "recovering shards: " << "[" << recovering_shards << "]";
2213 }
2214
2215 flush_ss(ss, status);
2216 }
2217
2218 static void tab_dump(const string& header, int width, const list<string>& entries)
2219 {
2220 string s = header;
2221
2222 for (auto e : entries) {
2223 cout << std::setw(width) << s << std::setw(1) << " " << e << std::endl;
2224 s.clear();
2225 }
2226 }
2227
2228
2229 static void sync_status(Formatter *formatter)
2230 {
2231 RGWRealm& realm = store->realm;
2232 RGWZoneGroup& zonegroup = store->get_zonegroup();
2233 RGWZone& zone = store->get_zone();
2234
2235 int width = 15;
2236
2237 cout << std::setw(width) << "realm" << std::setw(1) << " " << realm.get_id() << " (" << realm.get_name() << ")" << std::endl;
2238 cout << std::setw(width) << "zonegroup" << std::setw(1) << " " << zonegroup.get_id() << " (" << zonegroup.get_name() << ")" << std::endl;
2239 cout << std::setw(width) << "zone" << std::setw(1) << " " << zone.id << " (" << zone.name << ")" << std::endl;
2240
2241 list<string> md_status;
2242
2243 if (store->is_meta_master()) {
2244 md_status.push_back("no sync (zone is master)");
2245 } else {
2246 get_md_sync_status(md_status);
2247 }
2248
2249 tab_dump("metadata sync", width, md_status);
2250
2251 list<string> data_status;
2252
2253 for (auto iter : store->zone_conn_map) {
2254 const string& source_id = iter.first;
2255 string source_str = "source: ";
2256 string s = source_str + source_id;
2257 auto siter = store->zone_by_id.find(source_id);
2258 if (siter != store->zone_by_id.end()) {
2259 s += string(" (") + siter->second.name + ")";
2260 }
2261 data_status.push_back(s);
2262 get_data_sync_status(source_id, data_status, source_str.size());
2263 }
2264
2265 tab_dump("data sync", width, data_status);
2266 }
2267
2268 struct indented {
2269 int w; // indent width
2270 boost::string_view header;
2271 indented(int w, boost::string_view header = "") : w(w), header(header) {}
2272 };
2273 std::ostream& operator<<(std::ostream& out, const indented& h) {
2274 return out << std::setw(h.w) << h.header << std::setw(1) << ' ';
2275 }
2276
2277 static int remote_bilog_markers(RGWRados *store, const RGWZone& source,
2278 RGWRESTConn *conn, const RGWBucketInfo& info,
2279 BucketIndexShardsManager *markers)
2280 {
2281 const auto instance_key = info.bucket.get_key();
2282 const rgw_http_param_pair params[] = {
2283 { "type" , "bucket-index" },
2284 { "bucket-instance", instance_key.c_str() },
2285 { "info" , nullptr },
2286 { nullptr, nullptr }
2287 };
2288 rgw_bucket_index_marker_info result;
2289 int r = conn->get_json_resource("/admin/log/", params, result);
2290 if (r < 0) {
2291 lderr(store->ctx()) << "failed to fetch remote log markers: " << cpp_strerror(r) << dendl;
2292 return r;
2293 }
2294 r = markers->from_string(result.max_marker, -1);
2295 if (r < 0) {
2296 lderr(store->ctx()) << "failed to decode remote log markers" << dendl;
2297 return r;
2298 }
2299 return 0;
2300 }
2301
2302 static int bucket_source_sync_status(RGWRados *store, const RGWZone& zone,
2303 const RGWZone& source, RGWRESTConn *conn,
2304 const RGWBucketInfo& bucket_info,
2305 int width, std::ostream& out)
2306 {
2307 out << indented{width, "source zone"} << source.id << " (" << source.name << ")\n";
2308
2309 // syncing from this zone?
2310 if (!zone.syncs_from(source.id)) {
2311 out << indented{width} << "not in sync_from\n";
2312 return 0;
2313 }
2314 std::vector<rgw_bucket_shard_sync_info> status;
2315 int r = rgw_bucket_sync_status(store, source.id, bucket_info, &status);
2316 if (r < 0) {
2317 lderr(store->ctx()) << "failed to read bucket sync status: " << cpp_strerror(r) << dendl;
2318 return r;
2319 }
2320
2321 int num_full = 0;
2322 int num_inc = 0;
2323 uint64_t full_complete = 0;
2324 const int total_shards = status.size();
2325
2326 using BucketSyncState = rgw_bucket_shard_sync_info::SyncState;
2327 for (size_t shard_id = 0; shard_id < total_shards; shard_id++) {
2328 auto& m = status[shard_id];
2329 if (m.state == BucketSyncState::StateFullSync) {
2330 num_full++;
2331 full_complete += m.full_marker.count;
2332 } else if (m.state == BucketSyncState::StateIncrementalSync) {
2333 num_inc++;
2334 }
2335 }
2336
2337 out << indented{width} << "full sync: " << num_full << "/" << total_shards << " shards\n";
2338 if (num_full > 0) {
2339 out << indented{width} << "full sync: " << full_complete << " objects completed\n";
2340 }
2341 out << indented{width} << "incremental sync: " << num_inc << "/" << total_shards << " shards\n";
2342
2343 BucketIndexShardsManager remote_markers;
2344 r = remote_bilog_markers(store, source, conn, bucket_info, &remote_markers);
2345 if (r < 0) {
2346 lderr(store->ctx()) << "failed to read remote log: " << cpp_strerror(r) << dendl;
2347 return r;
2348 }
2349
2350 std::set<int> shards_behind;
2351 for (auto& r : remote_markers.get()) {
2352 auto shard_id = r.first;
2353 auto& m = status[shard_id];
2354 if (r.second.empty()) {
2355 continue; // empty bucket index shard
2356 }
2357 auto pos = BucketIndexShardsManager::get_shard_marker(m.inc_marker.position);
2358 if (m.state != BucketSyncState::StateIncrementalSync || pos != r.second) {
2359 shards_behind.insert(shard_id);
2360 }
2361 }
2362 if (shards_behind.empty()) {
2363 out << indented{width} << "bucket is caught up with source\n";
2364 } else {
2365 out << indented{width} << "bucket is behind on " << shards_behind.size() << " shards\n";
2366 out << indented{width} << "behind shards: [" << shards_behind << "]\n" ;
2367 }
2368 return 0;
2369 }
2370
2371 static int bucket_sync_status(RGWRados *store, const RGWBucketInfo& info,
2372 const std::string& source_zone_id,
2373 std::ostream& out)
2374 {
2375 RGWRealm& realm = store->realm;
2376 RGWZoneGroup& zonegroup = store->get_zonegroup();
2377 RGWZone& zone = store->get_zone();
2378 constexpr int width = 15;
2379
2380 out << indented{width, "realm"} << realm.get_id() << " (" << realm.get_name() << ")\n";
2381 out << indented{width, "zonegroup"} << zonegroup.get_id() << " (" << zonegroup.get_name() << ")\n";
2382 out << indented{width, "zone"} << zone.id << " (" << zone.name << ")\n";
2383 out << indented{width, "bucket"} << info.bucket << "\n\n";
2384
2385 if (!info.datasync_flag_enabled()) {
2386 out << "Sync is disabled for bucket " << info.bucket.name << '\n';
2387 return 0;
2388 }
2389
2390 if (!source_zone_id.empty()) {
2391 auto z = zonegroup.zones.find(source_zone_id);
2392 if (z == zonegroup.zones.end()) {
2393 lderr(store->ctx()) << "Source zone not found in zonegroup "
2394 << zonegroup.get_name() << dendl;
2395 return -EINVAL;
2396 }
2397 auto c = store->zone_conn_map.find(source_zone_id);
2398 if (c == store->zone_conn_map.end()) {
2399 lderr(store->ctx()) << "No connection to zone " << z->second.name << dendl;
2400 return -EINVAL;
2401 }
2402 return bucket_source_sync_status(store, zone, z->second, c->second,
2403 info, width, out);
2404 }
2405
2406 for (const auto& z : zonegroup.zones) {
2407 auto c = store->zone_conn_map.find(z.second.id);
2408 if (c != store->zone_conn_map.end()) {
2409 bucket_source_sync_status(store, zone, z.second, c->second,
2410 info, width, out);
2411 }
2412 }
2413 return 0;
2414 }
2415
2416 static void parse_tier_config_param(const string& s, map<string, string, ltstr_nocase>& out)
2417 {
2418 list<string> confs;
2419 get_str_list(s, ",", confs);
2420 for (auto c : confs) {
2421 ssize_t pos = c.find("=");
2422 if (pos < 0) {
2423 out[c] = "";
2424 } else {
2425 out[c.substr(0, pos)] = c.substr(pos + 1);
2426 }
2427 }
2428 }
2429
2430 int check_reshard_bucket_params(RGWRados *store,
2431 const string& bucket_name,
2432 const string& tenant,
2433 const string& bucket_id,
2434 bool num_shards_specified,
2435 int num_shards,
2436 int yes_i_really_mean_it,
2437 rgw_bucket& bucket,
2438 RGWBucketInfo& bucket_info,
2439 map<string, bufferlist>& attrs)
2440 {
2441 if (bucket_name.empty()) {
2442 cerr << "ERROR: bucket not specified" << std::endl;
2443 return -EINVAL;
2444 }
2445
2446 if (!num_shards_specified) {
2447 cerr << "ERROR: --num-shards not specified" << std::endl;
2448 return -EINVAL;
2449 }
2450
2451 if (num_shards > (int)store->get_max_bucket_shards()) {
2452 cerr << "ERROR: num_shards too high, max value: " << store->get_max_bucket_shards() << std::endl;
2453 return -EINVAL;
2454 }
2455
2456 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket, &attrs);
2457 if (ret < 0) {
2458 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
2459 return -ret;
2460 }
2461
2462 int num_source_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
2463
2464 if (num_shards <= num_source_shards && !yes_i_really_mean_it) {
2465 cerr << "num shards is less or equal to current shards count" << std::endl
2466 << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
2467 return -EINVAL;
2468 }
2469 return 0;
2470 }
2471
2472 int create_new_bucket_instance(RGWRados *store,
2473 int new_num_shards,
2474 const RGWBucketInfo& bucket_info,
2475 map<string, bufferlist>& attrs,
2476 RGWBucketInfo& new_bucket_info)
2477 {
2478
2479 store->create_bucket_id(&new_bucket_info.bucket.bucket_id);
2480 new_bucket_info.bucket.oid.clear();
2481
2482 new_bucket_info.num_shards = new_num_shards;
2483 new_bucket_info.objv_tracker.clear();
2484
2485 int ret = store->init_bucket_index(new_bucket_info, new_bucket_info.num_shards);
2486 if (ret < 0) {
2487 cerr << "ERROR: failed to init new bucket indexes: " << cpp_strerror(-ret) << std::endl;
2488 return -ret;
2489 }
2490
2491 ret = store->put_bucket_instance_info(new_bucket_info, true, real_time(), &attrs);
2492 if (ret < 0) {
2493 cerr << "ERROR: failed to store new bucket instance info: " << cpp_strerror(-ret) << std::endl;
2494 return -ret;
2495 }
2496
2497 return 0;
2498 }
2499
2500
2501 int main(int argc, const char **argv)
2502 {
2503 vector<const char*> args;
2504 argv_to_vec(argc, (const char **)argv, args);
2505 env_to_vec(args);
2506
2507 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
2508 CODE_ENVIRONMENT_UTILITY, 0);
2509
2510 // for region -> zonegroup conversion (must happen before common_init_finish())
2511 if (!g_conf->rgw_region.empty() && g_conf->rgw_zonegroup.empty()) {
2512 g_conf->set_val_or_die("rgw_zonegroup", g_conf->rgw_region.c_str());
2513 }
2514
2515 common_init_finish(g_ceph_context);
2516
2517 rgw_user user_id;
2518 string tenant;
2519 std::string access_key, secret_key, user_email, display_name;
2520 std::string bucket_name, pool_name, object;
2521 rgw_pool pool;
2522 std::string date, subuser, access, format;
2523 std::string start_date, end_date;
2524 std::string key_type_str;
2525 std::string period_id, period_epoch, remote, url;
2526 std::string master_zonegroup, master_zone;
2527 std::string realm_name, realm_id, realm_new_name;
2528 std::string zone_name, zone_id, zone_new_name;
2529 std::string zonegroup_name, zonegroup_id, zonegroup_new_name;
2530 std::string api_name;
2531 std::string role_name, path, assume_role_doc, policy_name, perm_policy_doc, path_prefix;
2532 list<string> endpoints;
2533 int tmp_int;
2534 int sync_from_all_specified = false;
2535 bool sync_from_all = false;
2536 list<string> sync_from;
2537 list<string> sync_from_rm;
2538 std::string master_url;
2539 int is_master_int;
2540 int set_default = 0;
2541 bool is_master = false;
2542 bool is_master_set = false;
2543 int read_only_int;
2544 bool read_only = false;
2545 int is_read_only_set = false;
2546 int commit = false;
2547 int staging = false;
2548 int key_type = KEY_TYPE_UNDEFINED;
2549 rgw_bucket bucket;
2550 uint32_t perm_mask = 0;
2551 RGWUserInfo info;
2552 int opt_cmd = OPT_NO_CMD;
2553 bool need_more;
2554 int gen_access_key = 0;
2555 int gen_secret_key = 0;
2556 bool set_perm = false;
2557 bool set_temp_url_key = false;
2558 map<int, string> temp_url_keys;
2559 string bucket_id;
2560 Formatter *formatter = NULL;
2561 int purge_data = false;
2562 int pretty_format = false;
2563 int show_log_entries = true;
2564 int show_log_sum = true;
2565 int skip_zero_entries = false; // log show
2566 int purge_keys = false;
2567 int yes_i_really_mean_it = false;
2568 int delete_child_objects = false;
2569 int fix = false;
2570 int remove_bad = false;
2571 int check_head_obj_locator = false;
2572 int max_buckets = -1;
2573 bool max_buckets_specified = false;
2574 map<string, bool> categories;
2575 string caps;
2576 int check_objects = false;
2577 RGWUserAdminOpState user_op;
2578 RGWBucketAdminOpState bucket_op;
2579 string infile;
2580 string metadata_key;
2581 RGWObjVersionTracker objv_tracker;
2582 string marker;
2583 string start_marker;
2584 string end_marker;
2585 int max_entries = -1;
2586 bool max_entries_specified = false;
2587 int admin = false;
2588 bool admin_specified = false;
2589 int system = false;
2590 bool system_specified = false;
2591 int shard_id = -1;
2592 bool specified_shard_id = false;
2593 string daemon_id;
2594 bool specified_daemon_id = false;
2595 string client_id;
2596 string op_id;
2597 string state_str;
2598 string replica_log_type_str;
2599 ReplicaLogType replica_log_type = ReplicaLog_Invalid;
2600 string op_mask_str;
2601 string quota_scope;
2602 string object_version;
2603 string placement_id;
2604 list<string> tags;
2605 list<string> tags_add;
2606 list<string> tags_rm;
2607
2608 int64_t max_objects = -1;
2609 int64_t max_size = -1;
2610 bool have_max_objects = false;
2611 bool have_max_size = false;
2612 int include_all = false;
2613
2614 int sync_stats = false;
2615 int reset_stats = false;
2616 int bypass_gc = false;
2617 int warnings_only = false;
2618 int inconsistent_index = false;
2619
2620 int verbose = false;
2621
2622 int extra_info = false;
2623
2624 uint64_t min_rewrite_size = 4 * 1024 * 1024;
2625 uint64_t max_rewrite_size = ULLONG_MAX;
2626 uint64_t min_rewrite_stripe_size = 0;
2627
2628 BIIndexType bi_index_type = PlainIdx;
2629
2630 string job_id;
2631 int num_shards = 0;
2632 bool num_shards_specified = false;
2633 int max_concurrent_ios = 32;
2634 uint64_t orphan_stale_secs = (24 * 3600);
2635
2636 std::string val;
2637 std::ostringstream errs;
2638 string err;
2639 long long tmp = 0;
2640
2641 string source_zone_name;
2642 string source_zone; /* zone id */
2643
2644 string tier_type;
2645 bool tier_type_specified = false;
2646
2647 map<string, string, ltstr_nocase> tier_config_add;
2648 map<string, string, ltstr_nocase> tier_config_rm;
2649
2650 boost::optional<string> index_pool;
2651 boost::optional<string> data_pool;
2652 boost::optional<string> data_extra_pool;
2653 RGWBucketIndexType placement_index_type = RGWBIType_Normal;
2654 bool index_type_specified = false;
2655
2656 boost::optional<std::string> compression_type;
2657
2658 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
2659 if (ceph_argparse_double_dash(args, i)) {
2660 break;
2661 } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
2662 usage();
2663 assert(false);
2664 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--uid", (char*)NULL)) {
2665 user_id.from_str(val);
2666 } else if (ceph_argparse_witharg(args, i, &val, "--tenant", (char*)NULL)) {
2667 tenant = val;
2668 } else if (ceph_argparse_witharg(args, i, &val, "--access-key", (char*)NULL)) {
2669 access_key = val;
2670 } else if (ceph_argparse_witharg(args, i, &val, "--subuser", (char*)NULL)) {
2671 subuser = val;
2672 } else if (ceph_argparse_witharg(args, i, &val, "--secret", "--secret-key", (char*)NULL)) {
2673 secret_key = val;
2674 } else if (ceph_argparse_witharg(args, i, &val, "-e", "--email", (char*)NULL)) {
2675 user_email = val;
2676 user_op.user_email_specified=true;
2677 } else if (ceph_argparse_witharg(args, i, &val, "-n", "--display-name", (char*)NULL)) {
2678 display_name = val;
2679 } else if (ceph_argparse_witharg(args, i, &val, "-b", "--bucket", (char*)NULL)) {
2680 bucket_name = val;
2681 } else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) {
2682 pool_name = val;
2683 pool = rgw_pool(pool_name);
2684 } else if (ceph_argparse_witharg(args, i, &val, "-o", "--object", (char*)NULL)) {
2685 object = val;
2686 } else if (ceph_argparse_witharg(args, i, &val, "--object-version", (char*)NULL)) {
2687 object_version = val;
2688 } else if (ceph_argparse_witharg(args, i, &val, "--client-id", (char*)NULL)) {
2689 client_id = val;
2690 } else if (ceph_argparse_witharg(args, i, &val, "--op-id", (char*)NULL)) {
2691 op_id = val;
2692 } else if (ceph_argparse_witharg(args, i, &val, "--state", (char*)NULL)) {
2693 state_str = val;
2694 } else if (ceph_argparse_witharg(args, i, &val, "--op-mask", (char*)NULL)) {
2695 op_mask_str = val;
2696 } else if (ceph_argparse_witharg(args, i, &val, "--key-type", (char*)NULL)) {
2697 key_type_str = val;
2698 if (key_type_str.compare("swift") == 0) {
2699 key_type = KEY_TYPE_SWIFT;
2700 } else if (key_type_str.compare("s3") == 0) {
2701 key_type = KEY_TYPE_S3;
2702 } else {
2703 cerr << "bad key type: " << key_type_str << std::endl;
2704 usage();
2705 assert(false);
2706 }
2707 } else if (ceph_argparse_witharg(args, i, &val, "--job-id", (char*)NULL)) {
2708 job_id = val;
2709 } else if (ceph_argparse_binary_flag(args, i, &gen_access_key, NULL, "--gen-access-key", (char*)NULL)) {
2710 // do nothing
2711 } else if (ceph_argparse_binary_flag(args, i, &gen_secret_key, NULL, "--gen-secret", (char*)NULL)) {
2712 // do nothing
2713 } else if (ceph_argparse_binary_flag(args, i, &show_log_entries, NULL, "--show_log_entries", (char*)NULL)) {
2714 // do nothing
2715 } else if (ceph_argparse_binary_flag(args, i, &show_log_sum, NULL, "--show_log_sum", (char*)NULL)) {
2716 // do nothing
2717 } else if (ceph_argparse_binary_flag(args, i, &skip_zero_entries, NULL, "--skip_zero_entries", (char*)NULL)) {
2718 // do nothing
2719 } else if (ceph_argparse_binary_flag(args, i, &admin, NULL, "--admin", (char*)NULL)) {
2720 admin_specified = true;
2721 } else if (ceph_argparse_binary_flag(args, i, &system, NULL, "--system", (char*)NULL)) {
2722 system_specified = true;
2723 } else if (ceph_argparse_binary_flag(args, i, &verbose, NULL, "--verbose", (char*)NULL)) {
2724 // do nothing
2725 } else if (ceph_argparse_binary_flag(args, i, &staging, NULL, "--staging", (char*)NULL)) {
2726 // do nothing
2727 } else if (ceph_argparse_binary_flag(args, i, &commit, NULL, "--commit", (char*)NULL)) {
2728 // do nothing
2729 } else if (ceph_argparse_witharg(args, i, &tmp, errs, "-a", "--auth-uid", (char*)NULL)) {
2730 if (!errs.str().empty()) {
2731 cerr << errs.str() << std::endl;
2732 exit(EXIT_FAILURE);
2733 }
2734 } else if (ceph_argparse_witharg(args, i, &val, "--min-rewrite-size", (char*)NULL)) {
2735 min_rewrite_size = (uint64_t)atoll(val.c_str());
2736 } else if (ceph_argparse_witharg(args, i, &val, "--max-rewrite-size", (char*)NULL)) {
2737 max_rewrite_size = (uint64_t)atoll(val.c_str());
2738 } else if (ceph_argparse_witharg(args, i, &val, "--min-rewrite-stripe-size", (char*)NULL)) {
2739 min_rewrite_stripe_size = (uint64_t)atoll(val.c_str());
2740 } else if (ceph_argparse_witharg(args, i, &val, "--max-buckets", (char*)NULL)) {
2741 max_buckets = (int)strict_strtol(val.c_str(), 10, &err);
2742 if (!err.empty()) {
2743 cerr << "ERROR: failed to parse max buckets: " << err << std::endl;
2744 return EINVAL;
2745 }
2746 max_buckets_specified = true;
2747 } else if (ceph_argparse_witharg(args, i, &val, "--max-entries", (char*)NULL)) {
2748 max_entries = (int)strict_strtol(val.c_str(), 10, &err);
2749 max_entries_specified = true;
2750 if (!err.empty()) {
2751 cerr << "ERROR: failed to parse max entries: " << err << std::endl;
2752 return EINVAL;
2753 }
2754 } else if (ceph_argparse_witharg(args, i, &val, "--max-size", (char*)NULL)) {
2755 max_size = strict_iecstrtoll(val.c_str(), &err);
2756 if (!err.empty()) {
2757 cerr << "ERROR: failed to parse max size: " << err << std::endl;
2758 return EINVAL;
2759 }
2760 have_max_size = true;
2761 } else if (ceph_argparse_witharg(args, i, &val, "--max-objects", (char*)NULL)) {
2762 max_objects = (int64_t)strict_strtoll(val.c_str(), 10, &err);
2763 if (!err.empty()) {
2764 cerr << "ERROR: failed to parse max objects: " << err << std::endl;
2765 return EINVAL;
2766 }
2767 have_max_objects = true;
2768 } else if (ceph_argparse_witharg(args, i, &val, "--date", "--time", (char*)NULL)) {
2769 date = val;
2770 if (end_date.empty())
2771 end_date = date;
2772 } else if (ceph_argparse_witharg(args, i, &val, "--start-date", "--start-time", (char*)NULL)) {
2773 start_date = val;
2774 } else if (ceph_argparse_witharg(args, i, &val, "--end-date", "--end-time", (char*)NULL)) {
2775 end_date = val;
2776 } else if (ceph_argparse_witharg(args, i, &val, "--num-shards", (char*)NULL)) {
2777 num_shards = (int)strict_strtol(val.c_str(), 10, &err);
2778 if (!err.empty()) {
2779 cerr << "ERROR: failed to parse num shards: " << err << std::endl;
2780 return EINVAL;
2781 }
2782 num_shards_specified = true;
2783 } else if (ceph_argparse_witharg(args, i, &val, "--max-concurrent-ios", (char*)NULL)) {
2784 max_concurrent_ios = (int)strict_strtol(val.c_str(), 10, &err);
2785 if (!err.empty()) {
2786 cerr << "ERROR: failed to parse max concurrent ios: " << err << std::endl;
2787 return EINVAL;
2788 }
2789 } else if (ceph_argparse_witharg(args, i, &val, "--orphan-stale-secs", (char*)NULL)) {
2790 orphan_stale_secs = (uint64_t)strict_strtoll(val.c_str(), 10, &err);
2791 if (!err.empty()) {
2792 cerr << "ERROR: failed to parse orphan stale secs: " << err << std::endl;
2793 return EINVAL;
2794 }
2795 } else if (ceph_argparse_witharg(args, i, &val, "--shard-id", (char*)NULL)) {
2796 shard_id = (int)strict_strtol(val.c_str(), 10, &err);
2797 if (!err.empty()) {
2798 cerr << "ERROR: failed to parse shard id: " << err << std::endl;
2799 return EINVAL;
2800 }
2801 specified_shard_id = true;
2802 } else if (ceph_argparse_witharg(args, i, &val, "--daemon-id", (char*)NULL)) {
2803 daemon_id = val;
2804 specified_daemon_id = true;
2805 } else if (ceph_argparse_witharg(args, i, &val, "--access", (char*)NULL)) {
2806 access = val;
2807 perm_mask = rgw_str_to_perm(access.c_str());
2808 set_perm = true;
2809 } else if (ceph_argparse_witharg(args, i, &val, "--temp-url-key", (char*)NULL)) {
2810 temp_url_keys[0] = val;
2811 set_temp_url_key = true;
2812 } else if (ceph_argparse_witharg(args, i, &val, "--temp-url-key2", "--temp-url-key-2", (char*)NULL)) {
2813 temp_url_keys[1] = val;
2814 set_temp_url_key = true;
2815 } else if (ceph_argparse_witharg(args, i, &val, "--bucket-id", (char*)NULL)) {
2816 bucket_id = val;
2817 if (bucket_id.empty()) {
2818 cerr << "bad bucket-id" << std::endl;
2819 usage();
2820 assert(false);
2821 }
2822 } else if (ceph_argparse_witharg(args, i, &val, "--format", (char*)NULL)) {
2823 format = val;
2824 } else if (ceph_argparse_witharg(args, i, &val, "--categories", (char*)NULL)) {
2825 string cat_str = val;
2826 list<string> cat_list;
2827 list<string>::iterator iter;
2828 get_str_list(cat_str, cat_list);
2829 for (iter = cat_list.begin(); iter != cat_list.end(); ++iter) {
2830 categories[*iter] = true;
2831 }
2832 } else if (ceph_argparse_binary_flag(args, i, &delete_child_objects, NULL, "--purge-objects", (char*)NULL)) {
2833 // do nothing
2834 } else if (ceph_argparse_binary_flag(args, i, &pretty_format, NULL, "--pretty-format", (char*)NULL)) {
2835 // do nothing
2836 } else if (ceph_argparse_binary_flag(args, i, &purge_data, NULL, "--purge-data", (char*)NULL)) {
2837 delete_child_objects = purge_data;
2838 } else if (ceph_argparse_binary_flag(args, i, &purge_keys, NULL, "--purge-keys", (char*)NULL)) {
2839 // do nothing
2840 } else if (ceph_argparse_binary_flag(args, i, &yes_i_really_mean_it, NULL, "--yes-i-really-mean-it", (char*)NULL)) {
2841 // do nothing
2842 } else if (ceph_argparse_binary_flag(args, i, &fix, NULL, "--fix", (char*)NULL)) {
2843 // do nothing
2844 } else if (ceph_argparse_binary_flag(args, i, &remove_bad, NULL, "--remove-bad", (char*)NULL)) {
2845 // do nothing
2846 } else if (ceph_argparse_binary_flag(args, i, &check_head_obj_locator, NULL, "--check-head-obj-locator", (char*)NULL)) {
2847 // do nothing
2848 } else if (ceph_argparse_binary_flag(args, i, &check_objects, NULL, "--check-objects", (char*)NULL)) {
2849 // do nothing
2850 } else if (ceph_argparse_binary_flag(args, i, &sync_stats, NULL, "--sync-stats", (char*)NULL)) {
2851 // do nothing
2852 } else if (ceph_argparse_binary_flag(args, i, &reset_stats, NULL, "--reset-stats", (char*)NULL)) {
2853 // do nothing
2854 } else if (ceph_argparse_binary_flag(args, i, &include_all, NULL, "--include-all", (char*)NULL)) {
2855 // do nothing
2856 } else if (ceph_argparse_binary_flag(args, i, &extra_info, NULL, "--extra-info", (char*)NULL)) {
2857 // do nothing
2858 } else if (ceph_argparse_binary_flag(args, i, &bypass_gc, NULL, "--bypass-gc", (char*)NULL)) {
2859 // do nothing
2860 } else if (ceph_argparse_binary_flag(args, i, &warnings_only, NULL, "--warnings-only", (char*)NULL)) {
2861 // do nothing
2862 } else if (ceph_argparse_binary_flag(args, i, &inconsistent_index, NULL, "--inconsistent-index", (char*)NULL)) {
2863 // do nothing
2864 } else if (ceph_argparse_witharg(args, i, &val, "--caps", (char*)NULL)) {
2865 caps = val;
2866 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--infile", (char*)NULL)) {
2867 infile = val;
2868 } else if (ceph_argparse_witharg(args, i, &val, "--metadata-key", (char*)NULL)) {
2869 metadata_key = val;
2870 } else if (ceph_argparse_witharg(args, i, &val, "--marker", (char*)NULL)) {
2871 marker = val;
2872 } else if (ceph_argparse_witharg(args, i, &val, "--start-marker", (char*)NULL)) {
2873 start_marker = val;
2874 } else if (ceph_argparse_witharg(args, i, &val, "--end-marker", (char*)NULL)) {
2875 end_marker = val;
2876 } else if (ceph_argparse_witharg(args, i, &val, "--quota-scope", (char*)NULL)) {
2877 quota_scope = val;
2878 } else if (ceph_argparse_witharg(args, i, &val, "--replica-log-type", (char*)NULL)) {
2879 replica_log_type_str = val;
2880 replica_log_type = get_replicalog_type(replica_log_type_str);
2881 if (replica_log_type == ReplicaLog_Invalid) {
2882 cerr << "ERROR: invalid replica log type" << std::endl;
2883 return EINVAL;
2884 }
2885 } else if (ceph_argparse_witharg(args, i, &val, "--index-type", (char*)NULL)) {
2886 string index_type_str = val;
2887 bi_index_type = get_bi_index_type(index_type_str);
2888 if (bi_index_type == InvalidIdx) {
2889 cerr << "ERROR: invalid bucket index entry type" << std::endl;
2890 return EINVAL;
2891 }
2892 } else if (ceph_argparse_binary_flag(args, i, &is_master_int, NULL, "--master", (char*)NULL)) {
2893 is_master = (bool)is_master_int;
2894 is_master_set = true;
2895 } else if (ceph_argparse_binary_flag(args, i, &set_default, NULL, "--default", (char*)NULL)) {
2896 /* do nothing */
2897 } else if (ceph_argparse_binary_flag(args, i, &read_only_int, NULL, "--read-only", (char*)NULL)) {
2898 read_only = (bool)read_only_int;
2899 is_read_only_set = true;
2900 } else if (ceph_argparse_witharg(args, i, &val, "--master-url", (char*)NULL)) {
2901 master_url = val;
2902 } else if (ceph_argparse_witharg(args, i, &val, "--master-zonegroup", (char*)NULL)) {
2903 master_zonegroup = val;
2904 } else if (ceph_argparse_witharg(args, i, &val, "--master-zone", (char*)NULL)) {
2905 master_zone = val;
2906 } else if (ceph_argparse_witharg(args, i, &val, "--period", (char*)NULL)) {
2907 period_id = val;
2908 } else if (ceph_argparse_witharg(args, i, &val, "--epoch", (char*)NULL)) {
2909 period_epoch = val;
2910 } else if (ceph_argparse_witharg(args, i, &val, "--remote", (char*)NULL)) {
2911 remote = val;
2912 } else if (ceph_argparse_witharg(args, i, &val, "--url", (char*)NULL)) {
2913 url = val;
2914 } else if (ceph_argparse_witharg(args, i, &val, "--realm-id", (char*)NULL)) {
2915 realm_id = val;
2916 } else if (ceph_argparse_witharg(args, i, &val, "--realm-new-name", (char*)NULL)) {
2917 realm_new_name = val;
2918 } else if (ceph_argparse_witharg(args, i, &val, "--zonegroup-id", (char*)NULL)) {
2919 zonegroup_id = val;
2920 } else if (ceph_argparse_witharg(args, i, &val, "--zonegroup-new-name", (char*)NULL)) {
2921 zonegroup_new_name = val;
2922 } else if (ceph_argparse_witharg(args, i, &val, "--placement-id", (char*)NULL)) {
2923 placement_id = val;
2924 } else if (ceph_argparse_witharg(args, i, &val, "--tags", (char*)NULL)) {
2925 get_str_list(val, tags);
2926 } else if (ceph_argparse_witharg(args, i, &val, "--tags-add", (char*)NULL)) {
2927 get_str_list(val, tags_add);
2928 } else if (ceph_argparse_witharg(args, i, &val, "--tags-rm", (char*)NULL)) {
2929 get_str_list(val, tags_rm);
2930 } else if (ceph_argparse_witharg(args, i, &val, "--api-name", (char*)NULL)) {
2931 api_name = val;
2932 } else if (ceph_argparse_witharg(args, i, &val, "--zone-id", (char*)NULL)) {
2933 zone_id = val;
2934 } else if (ceph_argparse_witharg(args, i, &val, "--zone-new-name", (char*)NULL)) {
2935 zone_new_name = val;
2936 } else if (ceph_argparse_witharg(args, i, &val, "--endpoints", (char*)NULL)) {
2937 get_str_list(val, endpoints);
2938 } else if (ceph_argparse_witharg(args, i, &val, "--sync-from", (char*)NULL)) {
2939 get_str_list(val, sync_from);
2940 } else if (ceph_argparse_witharg(args, i, &val, "--sync-from-rm", (char*)NULL)) {
2941 get_str_list(val, sync_from_rm);
2942 } else if (ceph_argparse_binary_flag(args, i, &tmp_int, NULL, "--sync-from-all", (char*)NULL)) {
2943 sync_from_all = (bool)tmp_int;
2944 sync_from_all_specified = true;
2945 } else if (ceph_argparse_witharg(args, i, &val, "--source-zone", (char*)NULL)) {
2946 source_zone_name = val;
2947 } else if (ceph_argparse_witharg(args, i, &val, "--tier-type", (char*)NULL)) {
2948 tier_type = val;
2949 tier_type_specified = true;
2950 } else if (ceph_argparse_witharg(args, i, &val, "--tier-config", (char*)NULL)) {
2951 parse_tier_config_param(val, tier_config_add);
2952 } else if (ceph_argparse_witharg(args, i, &val, "--tier-config-rm", (char*)NULL)) {
2953 parse_tier_config_param(val, tier_config_rm);
2954 } else if (ceph_argparse_witharg(args, i, &val, "--index-pool", (char*)NULL)) {
2955 index_pool = val;
2956 } else if (ceph_argparse_witharg(args, i, &val, "--data-pool", (char*)NULL)) {
2957 data_pool = val;
2958 } else if (ceph_argparse_witharg(args, i, &val, "--data-extra-pool", (char*)NULL)) {
2959 data_extra_pool = val;
2960 } else if (ceph_argparse_witharg(args, i, &val, "--placement-index-type", (char*)NULL)) {
2961 if (val == "normal") {
2962 placement_index_type = RGWBIType_Normal;
2963 } else if (val == "indexless") {
2964 placement_index_type = RGWBIType_Indexless;
2965 } else {
2966 placement_index_type = (RGWBucketIndexType)strict_strtol(val.c_str(), 10, &err);
2967 if (!err.empty()) {
2968 cerr << "ERROR: failed to parse index type index: " << err << std::endl;
2969 return EINVAL;
2970 }
2971 }
2972 index_type_specified = true;
2973 } else if (ceph_argparse_witharg(args, i, &val, "--compression", (char*)NULL)) {
2974 compression_type = val;
2975 } else if (ceph_argparse_witharg(args, i, &val, "--role-name", (char*)NULL)) {
2976 role_name = val;
2977 } else if (ceph_argparse_witharg(args, i, &val, "--path", (char*)NULL)) {
2978 path = val;
2979 } else if (ceph_argparse_witharg(args, i, &val, "--assume-role-policy-doc", (char*)NULL)) {
2980 assume_role_doc = val;
2981 } else if (ceph_argparse_witharg(args, i, &val, "--policy-name", (char*)NULL)) {
2982 policy_name = val;
2983 } else if (ceph_argparse_witharg(args, i, &val, "--policy-doc", (char*)NULL)) {
2984 perm_policy_doc = val;
2985 } else if (ceph_argparse_witharg(args, i, &val, "--path-prefix", (char*)NULL)) {
2986 path_prefix = val;
2987 } else if (strncmp(*i, "-", 1) == 0) {
2988 cerr << "ERROR: invalid flag " << *i << std::endl;
2989 return EINVAL;
2990 } else {
2991 ++i;
2992 }
2993 }
2994
2995 if (args.empty()) {
2996 usage();
2997 assert(false);
2998 }
2999 else {
3000 const char *prev_cmd = NULL;
3001 const char *prev_prev_cmd = NULL;
3002 std::vector<const char*>::iterator i ;
3003 for (i = args.begin(); i != args.end(); ++i) {
3004 opt_cmd = get_cmd(*i, prev_cmd, prev_prev_cmd, &need_more);
3005 if (opt_cmd < 0) {
3006 cerr << "unrecognized arg " << *i << std::endl;
3007 usage();
3008 assert(false);
3009 }
3010 if (!need_more) {
3011 ++i;
3012 break;
3013 }
3014 prev_prev_cmd = prev_cmd;
3015 prev_cmd = *i;
3016 }
3017
3018 if (opt_cmd == OPT_NO_CMD) {
3019 usage();
3020 assert(false);
3021 }
3022
3023 /* some commands may have an optional extra param */
3024 if (i != args.end()) {
3025 switch (opt_cmd) {
3026 case OPT_METADATA_GET:
3027 case OPT_METADATA_PUT:
3028 case OPT_METADATA_RM:
3029 case OPT_METADATA_LIST:
3030 metadata_key = *i;
3031 break;
3032 default:
3033 break;
3034 }
3035 }
3036
3037 if (tenant.empty()) {
3038 tenant = user_id.tenant;
3039 } else {
3040 if (user_id.empty() && opt_cmd != OPT_ROLE_CREATE
3041 && opt_cmd != OPT_ROLE_DELETE
3042 && opt_cmd != OPT_ROLE_GET
3043 && opt_cmd != OPT_ROLE_MODIFY
3044 && opt_cmd != OPT_ROLE_LIST
3045 && opt_cmd != OPT_ROLE_POLICY_PUT
3046 && opt_cmd != OPT_ROLE_POLICY_LIST
3047 && opt_cmd != OPT_ROLE_POLICY_GET
3048 && opt_cmd != OPT_ROLE_POLICY_DELETE
3049 && opt_cmd != OPT_RESHARD_ADD
3050 && opt_cmd != OPT_RESHARD_CANCEL
3051 && opt_cmd != OPT_RESHARD_STATUS) {
3052 cerr << "ERROR: --tenant is set, but there's no user ID" << std::endl;
3053 return EINVAL;
3054 }
3055 user_id.tenant = tenant;
3056 }
3057 /* check key parameter conflict */
3058 if ((!access_key.empty()) && gen_access_key) {
3059 cerr << "ERROR: key parameter conflict, --access-key & --gen-access-key" << std::endl;
3060 return EINVAL;
3061 }
3062 if ((!secret_key.empty()) && gen_secret_key) {
3063 cerr << "ERROR: key parameter conflict, --secret & --gen-secret" << std::endl;
3064 return EINVAL;
3065 }
3066 }
3067
3068 // default to pretty json
3069 if (format.empty()) {
3070 format = "json";
3071 pretty_format = true;
3072 }
3073
3074 if (format == "xml")
3075 formatter = new XMLFormatter(pretty_format);
3076 else if (format == "json")
3077 formatter = new JSONFormatter(pretty_format);
3078 else {
3079 cerr << "unrecognized format: " << format << std::endl;
3080 usage();
3081 assert(false);
3082 }
3083
3084 realm_name = g_conf->rgw_realm;
3085 zone_name = g_conf->rgw_zone;
3086 zonegroup_name = g_conf->rgw_zonegroup;
3087
3088 RGWStreamFlusher f(formatter, cout);
3089
3090 // not a raw op if 'period update' needs to commit to master
3091 bool raw_period_update = opt_cmd == OPT_PERIOD_UPDATE && !commit;
3092 std::set<int> raw_storage_ops_list = {OPT_ZONEGROUP_ADD, OPT_ZONEGROUP_CREATE, OPT_ZONEGROUP_DELETE,
3093 OPT_ZONEGROUP_GET, OPT_ZONEGROUP_LIST,
3094 OPT_ZONEGROUP_SET, OPT_ZONEGROUP_DEFAULT,
3095 OPT_ZONEGROUP_RENAME, OPT_ZONEGROUP_MODIFY,
3096 OPT_ZONEGROUP_REMOVE,
3097 OPT_ZONEGROUP_PLACEMENT_ADD, OPT_ZONEGROUP_PLACEMENT_RM,
3098 OPT_ZONEGROUP_PLACEMENT_MODIFY, OPT_ZONEGROUP_PLACEMENT_LIST,
3099 OPT_ZONEGROUP_PLACEMENT_DEFAULT,
3100 OPT_ZONE_CREATE, OPT_ZONE_DELETE,
3101 OPT_ZONE_GET, OPT_ZONE_SET, OPT_ZONE_RENAME,
3102 OPT_ZONE_LIST, OPT_ZONE_MODIFY, OPT_ZONE_DEFAULT,
3103 OPT_ZONE_PLACEMENT_ADD, OPT_ZONE_PLACEMENT_RM,
3104 OPT_ZONE_PLACEMENT_MODIFY, OPT_ZONE_PLACEMENT_LIST,
3105 OPT_REALM_CREATE,
3106 OPT_PERIOD_DELETE, OPT_PERIOD_GET,
3107 OPT_PERIOD_PULL,
3108 OPT_PERIOD_GET_CURRENT, OPT_PERIOD_LIST,
3109 OPT_GLOBAL_QUOTA_GET, OPT_GLOBAL_QUOTA_SET,
3110 OPT_GLOBAL_QUOTA_ENABLE, OPT_GLOBAL_QUOTA_DISABLE,
3111 OPT_REALM_DELETE, OPT_REALM_GET, OPT_REALM_LIST,
3112 OPT_REALM_LIST_PERIODS,
3113 OPT_REALM_GET_DEFAULT, OPT_REALM_REMOVE,
3114 OPT_REALM_RENAME, OPT_REALM_SET,
3115 OPT_REALM_DEFAULT, OPT_REALM_PULL};
3116
3117 std::set<int> readonly_ops_list = {
3118 OPT_USER_INFO,
3119 OPT_USER_STATS,
3120 OPT_BUCKETS_LIST,
3121 OPT_BUCKET_LIMIT_CHECK,
3122 OPT_BUCKET_STATS,
3123 OPT_BUCKET_SYNC_STATUS,
3124 OPT_LOG_LIST,
3125 OPT_LOG_SHOW,
3126 OPT_USAGE_SHOW,
3127 OPT_OBJECT_STAT,
3128 OPT_BI_GET,
3129 OPT_BI_LIST,
3130 OPT_OLH_GET,
3131 OPT_OLH_READLOG,
3132 OPT_GC_LIST,
3133 OPT_LC_LIST,
3134 OPT_ORPHANS_LIST_JOBS,
3135 OPT_ZONEGROUP_GET,
3136 OPT_ZONEGROUP_LIST,
3137 OPT_ZONEGROUP_PLACEMENT_LIST,
3138 OPT_ZONE_GET,
3139 OPT_ZONE_LIST,
3140 OPT_ZONE_PLACEMENT_LIST,
3141 OPT_METADATA_GET,
3142 OPT_METADATA_LIST,
3143 OPT_METADATA_SYNC_STATUS,
3144 OPT_MDLOG_LIST,
3145 OPT_MDLOG_STATUS,
3146 OPT_SYNC_ERROR_LIST,
3147 OPT_BILOG_LIST,
3148 OPT_BILOG_STATUS,
3149 OPT_DATA_SYNC_STATUS,
3150 OPT_DATALOG_LIST,
3151 OPT_DATALOG_STATUS,
3152 OPT_OPSTATE_LIST,
3153 OPT_REPLICALOG_GET,
3154 OPT_REALM_GET,
3155 OPT_REALM_GET_DEFAULT,
3156 OPT_REALM_LIST,
3157 OPT_REALM_LIST_PERIODS,
3158 OPT_PERIOD_GET,
3159 OPT_PERIOD_GET_CURRENT,
3160 OPT_PERIOD_LIST,
3161 OPT_GLOBAL_QUOTA_GET,
3162 OPT_SYNC_STATUS,
3163 OPT_ROLE_GET,
3164 OPT_ROLE_LIST,
3165 OPT_ROLE_POLICY_LIST,
3166 OPT_ROLE_POLICY_GET,
3167 OPT_RESHARD_LIST,
3168 OPT_RESHARD_STATUS,
3169 };
3170
3171 bool raw_storage_op = (raw_storage_ops_list.find(opt_cmd) != raw_storage_ops_list.end() ||
3172 raw_period_update);
3173 bool need_cache = readonly_ops_list.find(opt_cmd) == readonly_ops_list.end();
3174
3175 if (raw_storage_op) {
3176 store = RGWStoreManager::get_raw_storage(g_ceph_context);
3177 } else {
3178 store = RGWStoreManager::get_storage(g_ceph_context, false, false, false, false, false,
3179 need_cache && g_conf->rgw_cache_enabled);
3180 }
3181 if (!store) {
3182 cerr << "couldn't init storage provider" << std::endl;
3183 return 5; //EIO
3184 }
3185
3186 if (!source_zone_name.empty()) {
3187 if (!store->find_zone_id_by_name(source_zone_name, &source_zone)) {
3188 cerr << "WARNING: cannot find source zone id for name=" << source_zone_name << std::endl;
3189 source_zone = source_zone_name;
3190 }
3191 }
3192
3193 rgw_user_init(store);
3194 rgw_bucket_init(store->meta_mgr);
3195
3196 struct rgw_curl_setup {
3197 rgw_curl_setup() {
3198 rgw::curl::setup_curl(boost::none);
3199 }
3200 ~rgw_curl_setup() {
3201 rgw::curl::cleanup_curl();
3202 }
3203 } curl_cleanup;
3204
3205 StoreDestructor store_destructor(store);
3206
3207 if (raw_storage_op) {
3208 switch (opt_cmd) {
3209 case OPT_PERIOD_DELETE:
3210 {
3211 if (period_id.empty()) {
3212 cerr << "missing period id" << std::endl;
3213 return EINVAL;
3214 }
3215 RGWPeriod period(period_id);
3216 int ret = period.init(g_ceph_context, store);
3217 if (ret < 0) {
3218 cerr << "period.init failed: " << cpp_strerror(-ret) << std::endl;
3219 return -ret;
3220 }
3221 ret = period.delete_obj();
3222 if (ret < 0) {
3223 cerr << "ERROR: couldn't delete period: " << cpp_strerror(-ret) << std::endl;
3224 return -ret;
3225 }
3226
3227 }
3228 break;
3229 case OPT_PERIOD_GET:
3230 {
3231 epoch_t epoch = 0;
3232 if (!period_epoch.empty()) {
3233 epoch = atoi(period_epoch.c_str());
3234 }
3235 if (staging) {
3236 RGWRealm realm(realm_id, realm_name);
3237 int ret = realm.init(g_ceph_context, store);
3238 if (ret < 0 ) {
3239 cerr << "Error initializing realm " << cpp_strerror(-ret) << std::endl;
3240 return -ret;
3241 }
3242 realm_id = realm.get_id();
3243 realm_name = realm.get_name();
3244 period_id = RGWPeriod::get_staging_id(realm_id);
3245 epoch = 1;
3246 }
3247 RGWPeriod period(period_id, epoch);
3248 int ret = period.init(g_ceph_context, store, realm_id, realm_name);
3249 if (ret < 0) {
3250 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
3251 return -ret;
3252 }
3253 encode_json("period", period, formatter);
3254 formatter->flush(cout);
3255 cout << std::endl;
3256 }
3257 break;
3258 case OPT_PERIOD_GET_CURRENT:
3259 {
3260 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
3261 if (ret < 0) {
3262 return -ret;
3263 }
3264 formatter->open_object_section("period_get_current");
3265 encode_json("current_period", period_id, formatter);
3266 formatter->close_section();
3267 formatter->flush(cout);
3268 }
3269 break;
3270 case OPT_PERIOD_LIST:
3271 {
3272 list<string> periods;
3273 int ret = store->list_periods(periods);
3274 if (ret < 0) {
3275 cerr << "failed to list periods: " << cpp_strerror(-ret) << std::endl;
3276 return -ret;
3277 }
3278 formatter->open_object_section("periods_list");
3279 encode_json("periods", periods, formatter);
3280 formatter->close_section();
3281 formatter->flush(cout);
3282 cout << std::endl;
3283 }
3284 break;
3285 case OPT_PERIOD_UPDATE:
3286 {
3287 int ret = update_period(realm_id, realm_name, period_id, period_epoch,
3288 commit, remote, url, access_key, secret_key,
3289 formatter, yes_i_really_mean_it);
3290 if (ret < 0) {
3291 return -ret;
3292 }
3293 }
3294 break;
3295 case OPT_PERIOD_PULL:
3296 {
3297 boost::optional<RGWRESTConn> conn;
3298 RGWRESTConn *remote_conn = nullptr;
3299 if (url.empty()) {
3300 // load current period for endpoints
3301 RGWRealm realm(realm_id, realm_name);
3302 int ret = realm.init(g_ceph_context, store);
3303 if (ret < 0) {
3304 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3305 return -ret;
3306 }
3307 RGWPeriod current_period(realm.get_current_period());
3308 ret = current_period.init(g_ceph_context, store);
3309 if (ret < 0) {
3310 cerr << "failed to init current period: " << cpp_strerror(-ret) << std::endl;
3311 return -ret;
3312 }
3313 if (remote.empty()) {
3314 // use realm master zone as remote
3315 remote = current_period.get_master_zone();
3316 }
3317 conn = get_remote_conn(store, current_period.get_map(), remote);
3318 if (!conn) {
3319 cerr << "failed to find a zone or zonegroup for remote "
3320 << remote << std::endl;
3321 return -ENOENT;
3322 }
3323 remote_conn = &*conn;
3324 }
3325
3326 RGWPeriod period;
3327 int ret = do_period_pull(remote_conn, url, access_key, secret_key,
3328 realm_id, realm_name, period_id, period_epoch,
3329 &period);
3330 if (ret < 0) {
3331 cerr << "period pull failed: " << cpp_strerror(-ret) << std::endl;
3332 return -ret;
3333 }
3334
3335 encode_json("period", period, formatter);
3336 formatter->flush(cout);
3337 cout << std::endl;
3338 }
3339 break;
3340 case OPT_GLOBAL_QUOTA_GET:
3341 case OPT_GLOBAL_QUOTA_SET:
3342 case OPT_GLOBAL_QUOTA_ENABLE:
3343 case OPT_GLOBAL_QUOTA_DISABLE:
3344 {
3345 if (realm_id.empty()) {
3346 RGWRealm realm(g_ceph_context, store);
3347 if (!realm_name.empty()) {
3348 // look up realm_id for the given realm_name
3349 int ret = realm.read_id(realm_name, realm_id);
3350 if (ret < 0) {
3351 cerr << "ERROR: failed to read realm for " << realm_name
3352 << ": " << cpp_strerror(-ret) << std::endl;
3353 return -ret;
3354 }
3355 } else {
3356 // use default realm_id when none is given
3357 int ret = realm.read_default_id(realm_id);
3358 if (ret < 0 && ret != -ENOENT) { // on ENOENT, use empty realm_id
3359 cerr << "ERROR: failed to read default realm: "
3360 << cpp_strerror(-ret) << std::endl;
3361 return -ret;
3362 }
3363 }
3364 }
3365
3366 RGWPeriodConfig period_config;
3367 int ret = period_config.read(store, realm_id);
3368 if (ret < 0 && ret != -ENOENT) {
3369 cerr << "ERROR: failed to read period config: "
3370 << cpp_strerror(-ret) << std::endl;
3371 return -ret;
3372 }
3373
3374 formatter->open_object_section("period_config");
3375 if (quota_scope == "bucket") {
3376 set_quota_info(period_config.bucket_quota, opt_cmd,
3377 max_size, max_objects,
3378 have_max_size, have_max_objects);
3379 encode_json("bucket quota", period_config.bucket_quota, formatter);
3380 } else if (quota_scope == "user") {
3381 set_quota_info(period_config.user_quota, opt_cmd,
3382 max_size, max_objects,
3383 have_max_size, have_max_objects);
3384 encode_json("user quota", period_config.user_quota, formatter);
3385 } else if (quota_scope.empty() && opt_cmd == OPT_GLOBAL_QUOTA_GET) {
3386 // if no scope is given for GET, print both
3387 encode_json("bucket quota", period_config.bucket_quota, formatter);
3388 encode_json("user quota", period_config.user_quota, formatter);
3389 } else {
3390 cerr << "ERROR: invalid quota scope specification. Please specify "
3391 "either --quota-scope=bucket, or --quota-scope=user" << std::endl;
3392 return EINVAL;
3393 }
3394 formatter->close_section();
3395
3396 if (opt_cmd != OPT_GLOBAL_QUOTA_GET) {
3397 // write the modified period config
3398 ret = period_config.write(store, realm_id);
3399 if (ret < 0) {
3400 cerr << "ERROR: failed to write period config: "
3401 << cpp_strerror(-ret) << std::endl;
3402 return -ret;
3403 }
3404 if (!realm_id.empty()) {
3405 cout << "Global quota changes saved. Use 'period update' to apply "
3406 "them to the staging period, and 'period commit' to commit the "
3407 "new period." << std::endl;
3408 } else {
3409 cout << "Global quota changes saved. They will take effect as "
3410 "the gateways are restarted." << std::endl;
3411 }
3412 }
3413
3414 formatter->flush(cout);
3415 cout << std::endl;
3416 }
3417 break;
3418 case OPT_REALM_CREATE:
3419 {
3420 if (realm_name.empty()) {
3421 cerr << "missing realm name" << std::endl;
3422 return EINVAL;
3423 }
3424
3425 RGWRealm realm(realm_name, g_ceph_context, store);
3426 int ret = realm.create();
3427 if (ret < 0) {
3428 cerr << "ERROR: couldn't create realm " << realm_name << ": " << cpp_strerror(-ret) << std::endl;
3429 return -ret;
3430 }
3431
3432 if (set_default) {
3433 ret = realm.set_as_default();
3434 if (ret < 0) {
3435 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3436 }
3437 }
3438
3439 encode_json("realm", realm, formatter);
3440 formatter->flush(cout);
3441 cout << std::endl;
3442 }
3443 break;
3444 case OPT_REALM_DELETE:
3445 {
3446 RGWRealm realm(realm_id, realm_name);
3447 if (realm_name.empty() && realm_id.empty()) {
3448 cerr << "missing realm name or id" << std::endl;
3449 return EINVAL;
3450 }
3451 int ret = realm.init(g_ceph_context, store);
3452 if (ret < 0) {
3453 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3454 return -ret;
3455 }
3456 ret = realm.delete_obj();
3457 if (ret < 0) {
3458 cerr << "ERROR: couldn't : " << cpp_strerror(-ret) << std::endl;
3459 return -ret;
3460 }
3461
3462 }
3463 break;
3464 case OPT_REALM_GET:
3465 {
3466 RGWRealm realm(realm_id, realm_name);
3467 int ret = realm.init(g_ceph_context, store);
3468 if (ret < 0) {
3469 if (ret == -ENOENT && realm_name.empty() && realm_id.empty()) {
3470 cerr << "missing realm name or id, or default realm not found" << std::endl;
3471 } else {
3472 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3473 }
3474 return -ret;
3475 }
3476 encode_json("realm", realm, formatter);
3477 formatter->flush(cout);
3478 cout << std::endl;
3479 }
3480 break;
3481 case OPT_REALM_GET_DEFAULT:
3482 {
3483 RGWRealm realm(g_ceph_context, store);
3484 string default_id;
3485 int ret = realm.read_default_id(default_id);
3486 if (ret == -ENOENT) {
3487 cout << "No default realm is set" << std::endl;
3488 return -ret;
3489 } else if (ret < 0) {
3490 cerr << "Error reading default realm:" << cpp_strerror(-ret) << std::endl;
3491 return -ret;
3492 }
3493 cout << "default realm: " << default_id << std::endl;
3494 }
3495 break;
3496 case OPT_REALM_LIST:
3497 {
3498 RGWRealm realm(g_ceph_context, store);
3499 string default_id;
3500 int ret = realm.read_default_id(default_id);
3501 if (ret < 0 && ret != -ENOENT) {
3502 cerr << "could not determine default realm: " << cpp_strerror(-ret) << std::endl;
3503 }
3504 list<string> realms;
3505 ret = store->list_realms(realms);
3506 if (ret < 0) {
3507 cerr << "failed to list realms: " << cpp_strerror(-ret) << std::endl;
3508 return -ret;
3509 }
3510 formatter->open_object_section("realms_list");
3511 encode_json("default_info", default_id, formatter);
3512 encode_json("realms", realms, formatter);
3513 formatter->close_section();
3514 formatter->flush(cout);
3515 cout << std::endl;
3516 }
3517 break;
3518 case OPT_REALM_LIST_PERIODS:
3519 {
3520 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
3521 if (ret < 0) {
3522 return -ret;
3523 }
3524 list<string> periods;
3525 ret = store->list_periods(period_id, periods);
3526 if (ret < 0) {
3527 cerr << "list periods failed: " << cpp_strerror(-ret) << std::endl;
3528 return -ret;
3529 }
3530 formatter->open_object_section("realm_periods_list");
3531 encode_json("current_period", period_id, formatter);
3532 encode_json("periods", periods, formatter);
3533 formatter->close_section();
3534 formatter->flush(cout);
3535 cout << std::endl;
3536 }
3537 break;
3538
3539 case OPT_REALM_RENAME:
3540 {
3541 RGWRealm realm(realm_id, realm_name);
3542 if (realm_new_name.empty()) {
3543 cerr << "missing realm new name" << std::endl;
3544 return EINVAL;
3545 }
3546 if (realm_name.empty() && realm_id.empty()) {
3547 cerr << "missing realm name or id" << std::endl;
3548 return EINVAL;
3549 }
3550 int ret = realm.init(g_ceph_context, store);
3551 if (ret < 0) {
3552 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3553 return -ret;
3554 }
3555 ret = realm.rename(realm_new_name);
3556 if (ret < 0) {
3557 cerr << "realm.rename failed: " << cpp_strerror(-ret) << std::endl;
3558 return -ret;
3559 }
3560 cout << "Realm name updated. Note that this change only applies to "
3561 "the current cluster, so this command must be run separately "
3562 "on each of the realm's other clusters." << std::endl;
3563 }
3564 break;
3565 case OPT_REALM_SET:
3566 {
3567 if (realm_id.empty() && realm_name.empty()) {
3568 cerr << "no realm name or id provided" << std::endl;
3569 return EINVAL;
3570 }
3571 RGWRealm realm(realm_id, realm_name);
3572 bool new_realm = false;
3573 int ret = realm.init(g_ceph_context, store);
3574 if (ret < 0 && ret != -ENOENT) {
3575 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3576 return -ret;
3577 } else if (ret == -ENOENT) {
3578 new_realm = true;
3579 }
3580 ret = read_decode_json(infile, realm);
3581 if (ret < 0) {
3582 return 1;
3583 }
3584 if (!realm_name.empty() && realm.get_name() != realm_name) {
3585 cerr << "mismatch between --rgw-realm " << realm_name << " and json input file name " <<
3586 realm.get_name() << std::endl;
3587 return EINVAL;
3588 }
3589 /* new realm */
3590 if (new_realm) {
3591 cout << "clearing period and epoch for new realm" << std::endl;
3592 realm.clear_current_period_and_epoch();
3593 ret = realm.create();
3594 if (ret < 0) {
3595 cerr << "ERROR: couldn't create new realm: " << cpp_strerror(-ret) << std::endl;
3596 return 1;
3597 }
3598 } else {
3599 ret = realm.update();
3600 if (ret < 0) {
3601 cerr << "ERROR: couldn't store realm info: " << cpp_strerror(-ret) << std::endl;
3602 return 1;
3603 }
3604 }
3605
3606 if (set_default) {
3607 ret = realm.set_as_default();
3608 if (ret < 0) {
3609 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3610 }
3611 }
3612 encode_json("realm", realm, formatter);
3613 formatter->flush(cout);
3614 }
3615 break;
3616
3617 case OPT_REALM_DEFAULT:
3618 {
3619 RGWRealm realm(realm_id, realm_name);
3620 int ret = realm.init(g_ceph_context, store);
3621 if (ret < 0) {
3622 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3623 return -ret;
3624 }
3625 ret = realm.set_as_default();
3626 if (ret < 0) {
3627 cerr << "failed to set realm as default: " << cpp_strerror(-ret) << std::endl;
3628 return -ret;
3629 }
3630 }
3631 break;
3632 case OPT_REALM_PULL:
3633 {
3634 if (url.empty()) {
3635 cerr << "A --url must be provided." << std::endl;
3636 return EINVAL;
3637 }
3638 RGWEnv env;
3639 req_info info(g_ceph_context, &env);
3640 info.method = "GET";
3641 info.request_uri = "/admin/realm";
3642
3643 map<string, string> &params = info.args.get_params();
3644 if (!realm_id.empty())
3645 params["id"] = realm_id;
3646 if (!realm_name.empty())
3647 params["name"] = realm_name;
3648
3649 bufferlist bl;
3650 JSONParser p;
3651 int ret = send_to_url(url, access_key, secret_key, info, bl, p);
3652 if (ret < 0) {
3653 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
3654 if (ret == -EACCES) {
3655 cerr << "If the realm has been changed on the master zone, the "
3656 "master zone's gateway may need to be restarted to recognize "
3657 "this user." << std::endl;
3658 }
3659 return -ret;
3660 }
3661 RGWRealm realm;
3662 realm.init(g_ceph_context, store, false);
3663 try {
3664 decode_json_obj(realm, &p);
3665 } catch (JSONDecoder::err& e) {
3666 cerr << "failed to decode JSON response: " << e.message << std::endl;
3667 return EINVAL;
3668 }
3669 RGWPeriod period;
3670 auto& current_period = realm.get_current_period();
3671 if (!current_period.empty()) {
3672 // pull the latest epoch of the realm's current period
3673 ret = do_period_pull(nullptr, url, access_key, secret_key,
3674 realm_id, realm_name, current_period, "",
3675 &period);
3676 if (ret < 0) {
3677 cerr << "could not fetch period " << current_period << std::endl;
3678 return -ret;
3679 }
3680 }
3681 ret = realm.create(false);
3682 if (ret < 0 && ret != -EEXIST) {
3683 cerr << "Error storing realm " << realm.get_id() << ": "
3684 << cpp_strerror(ret) << std::endl;
3685 return -ret;
3686 } else if (ret ==-EEXIST) {
3687 ret = realm.update();
3688 if (ret < 0) {
3689 cerr << "Error storing realm " << realm.get_id() << ": "
3690 << cpp_strerror(ret) << std::endl;
3691 }
3692 }
3693
3694 if (set_default) {
3695 ret = realm.set_as_default();
3696 if (ret < 0) {
3697 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3698 }
3699 }
3700
3701 encode_json("realm", realm, formatter);
3702 formatter->flush(cout);
3703 cout << std::endl;
3704 }
3705 return 0;
3706
3707 case OPT_ZONEGROUP_ADD:
3708 {
3709 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3710 cerr << "no zonegroup name or id provided" << std::endl;
3711 return EINVAL;
3712 }
3713
3714 RGWZoneGroup zonegroup(zonegroup_id,zonegroup_name);
3715 int ret = zonegroup.init(g_ceph_context, store);
3716 if (ret < 0) {
3717 cerr << "failed to initialize zonegroup " << zonegroup_name << " id " << zonegroup_id << " :"
3718 << cpp_strerror(-ret) << std::endl;
3719 return -ret;
3720 }
3721 RGWZoneParams zone(zone_id, zone_name);
3722 ret = zone.init(g_ceph_context, store);
3723 if (ret < 0) {
3724 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3725 return -ret;
3726 }
3727 if (zone.realm_id != zonegroup.realm_id) {
3728 zone.realm_id = zonegroup.realm_id;
3729 ret = zone.update();
3730 if (ret < 0) {
3731 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
3732 return -ret;
3733 }
3734 }
3735
3736 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
3737 zone.tier_config = tier_config_add;
3738
3739 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
3740
3741 ret = zonegroup.add_zone(zone,
3742 (is_master_set ? &is_master : NULL),
3743 (is_read_only_set ? &read_only : NULL),
3744 endpoints, ptier_type,
3745 psync_from_all, sync_from, sync_from_rm);
3746 if (ret < 0) {
3747 cerr << "failed to add zone " << zone_name << " to zonegroup " << zonegroup.get_name() << ": "
3748 << cpp_strerror(-ret) << std::endl;
3749 return -ret;
3750 }
3751
3752 encode_json("zonegroup", zonegroup, formatter);
3753 formatter->flush(cout);
3754 }
3755 break;
3756 case OPT_ZONEGROUP_CREATE:
3757 {
3758 if (zonegroup_name.empty()) {
3759 cerr << "Missing zonegroup name" << std::endl;
3760 return EINVAL;
3761 }
3762 RGWRealm realm(realm_id, realm_name);
3763 int ret = realm.init(g_ceph_context, store);
3764 if (ret < 0) {
3765 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3766 return -ret;
3767 }
3768
3769 RGWZoneGroup zonegroup(zonegroup_name, is_master, g_ceph_context, store, realm.get_id(), endpoints);
3770 zonegroup.api_name = (api_name.empty() ? zonegroup_name : api_name);
3771 ret = zonegroup.create();
3772 if (ret < 0) {
3773 cerr << "failed to create zonegroup " << zonegroup_name << ": " << cpp_strerror(-ret) << std::endl;
3774 return -ret;
3775 }
3776
3777 if (set_default) {
3778 ret = zonegroup.set_as_default();
3779 if (ret < 0) {
3780 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3781 }
3782 }
3783
3784 encode_json("zonegroup", zonegroup, formatter);
3785 formatter->flush(cout);
3786 cout << std::endl;
3787 }
3788 break;
3789 case OPT_ZONEGROUP_DEFAULT:
3790 {
3791 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3792 cerr << "no zonegroup name or id provided" << std::endl;
3793 return EINVAL;
3794 }
3795
3796 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3797 int ret = zonegroup.init(g_ceph_context, store);
3798 if (ret < 0) {
3799 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3800 return -ret;
3801 }
3802
3803 ret = zonegroup.set_as_default();
3804 if (ret < 0) {
3805 cerr << "failed to set zonegroup as default: " << cpp_strerror(-ret) << std::endl;
3806 return -ret;
3807 }
3808 }
3809 break;
3810 case OPT_ZONEGROUP_DELETE:
3811 {
3812 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3813 cerr << "no zonegroup name or id provided" << std::endl;
3814 return EINVAL;
3815 }
3816 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3817 int ret = zonegroup.init(g_ceph_context, store);
3818 if (ret < 0) {
3819 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3820 return -ret;
3821 }
3822 ret = zonegroup.delete_obj();
3823 if (ret < 0) {
3824 cerr << "ERROR: couldn't delete zonegroup: " << cpp_strerror(-ret) << std::endl;
3825 return -ret;
3826 }
3827 }
3828 break;
3829 case OPT_ZONEGROUP_GET:
3830 {
3831 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3832 int ret = zonegroup.init(g_ceph_context, store);
3833 if (ret < 0) {
3834 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3835 return -ret;
3836 }
3837
3838 encode_json("zonegroup", zonegroup, formatter);
3839 formatter->flush(cout);
3840 cout << std::endl;
3841 }
3842 break;
3843 case OPT_ZONEGROUP_LIST:
3844 {
3845 RGWZoneGroup zonegroup;
3846 int ret = zonegroup.init(g_ceph_context, store, false);
3847 if (ret < 0) {
3848 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3849 return -ret;
3850 }
3851
3852 list<string> zonegroups;
3853 ret = store->list_zonegroups(zonegroups);
3854 if (ret < 0) {
3855 cerr << "failed to list zonegroups: " << cpp_strerror(-ret) << std::endl;
3856 return -ret;
3857 }
3858 string default_zonegroup;
3859 ret = zonegroup.read_default_id(default_zonegroup);
3860 if (ret < 0 && ret != -ENOENT) {
3861 cerr << "could not determine default zonegroup: " << cpp_strerror(-ret) << std::endl;
3862 }
3863 formatter->open_object_section("zonegroups_list");
3864 encode_json("default_info", default_zonegroup, formatter);
3865 encode_json("zonegroups", zonegroups, formatter);
3866 formatter->close_section();
3867 formatter->flush(cout);
3868 cout << std::endl;
3869 }
3870 break;
3871 case OPT_ZONEGROUP_MODIFY:
3872 {
3873 RGWRealm realm(realm_id, realm_name);
3874 int ret = realm.init(g_ceph_context, store);
3875 if (ret < 0) {
3876 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3877 return -ret;
3878 }
3879
3880 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3881 ret = zonegroup.init(g_ceph_context, store);
3882 if (ret < 0) {
3883 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3884 return -ret;
3885 }
3886
3887 bool need_update = false;
3888
3889 if (!master_zone.empty()) {
3890 zonegroup.master_zone = master_zone;
3891 need_update = true;
3892 }
3893
3894 if (is_master_set) {
3895 zonegroup.update_master(is_master);
3896 need_update = true;
3897 }
3898
3899 if (!endpoints.empty()) {
3900 zonegroup.endpoints = endpoints;
3901 need_update = true;
3902 }
3903
3904 if (!api_name.empty()) {
3905 zonegroup.api_name = api_name;
3906 need_update = true;
3907 }
3908
3909 if (!realm_id.empty()) {
3910 zonegroup.realm_id = realm_id;
3911 need_update = true;
3912 } else if (!realm_name.empty()) {
3913 // get realm id from name
3914 RGWRealm realm{g_ceph_context, store};
3915 ret = realm.read_id(realm_name, zonegroup.realm_id);
3916 if (ret < 0) {
3917 cerr << "failed to find realm by name " << realm_name << std::endl;
3918 return -ret;
3919 }
3920 need_update = true;
3921 }
3922
3923 if (need_update) {
3924 ret = zonegroup.update();
3925 if (ret < 0) {
3926 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
3927 return -ret;
3928 }
3929 }
3930
3931 if (set_default) {
3932 ret = zonegroup.set_as_default();
3933 if (ret < 0) {
3934 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3935 }
3936 }
3937
3938 encode_json("zonegroup", zonegroup, formatter);
3939 formatter->flush(cout);
3940 }
3941 break;
3942 case OPT_ZONEGROUP_SET:
3943 {
3944 RGWRealm realm(realm_id, realm_name);
3945 int ret = realm.init(g_ceph_context, store);
3946 bool default_realm_not_exist = (ret == -ENOENT && realm_id.empty() && realm_name.empty());
3947
3948 if (ret < 0 && !default_realm_not_exist ) {
3949 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3950 return -ret;
3951 }
3952
3953 RGWZoneGroup zonegroup;
3954 ret = zonegroup.init(g_ceph_context, store, false);
3955 if (ret < 0) {
3956 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3957 return -ret;
3958 }
3959 ret = read_decode_json(infile, zonegroup);
3960 if (ret < 0) {
3961 return 1;
3962 }
3963 if (zonegroup.realm_id.empty() && !default_realm_not_exist) {
3964 zonegroup.realm_id = realm.get_id();
3965 }
3966 ret = zonegroup.create();
3967 if (ret < 0 && ret != -EEXIST) {
3968 cerr << "ERROR: couldn't create zonegroup info: " << cpp_strerror(-ret) << std::endl;
3969 return 1;
3970 } else if (ret == -EEXIST) {
3971 ret = zonegroup.update();
3972 if (ret < 0) {
3973 cerr << "ERROR: couldn't store zonegroup info: " << cpp_strerror(-ret) << std::endl;
3974 return 1;
3975 }
3976 }
3977
3978 if (set_default) {
3979 ret = zonegroup.set_as_default();
3980 if (ret < 0) {
3981 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3982 }
3983 }
3984
3985 encode_json("zonegroup", zonegroup, formatter);
3986 formatter->flush(cout);
3987 }
3988 break;
3989 case OPT_ZONEGROUP_REMOVE:
3990 {
3991 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3992 int ret = zonegroup.init(g_ceph_context, store);
3993 if (ret < 0) {
3994 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3995 return -ret;
3996 }
3997
3998 if (zone_id.empty()) {
3999 if (zone_name.empty()) {
4000 cerr << "no --zone-id or --rgw-zone name provided" << std::endl;
4001 return EINVAL;
4002 }
4003 // look up zone id by name
4004 for (auto& z : zonegroup.zones) {
4005 if (zone_name == z.second.name) {
4006 zone_id = z.second.id;
4007 break;
4008 }
4009 }
4010 if (zone_id.empty()) {
4011 cerr << "zone name " << zone_name << " not found in zonegroup "
4012 << zonegroup.get_name() << std::endl;
4013 return ENOENT;
4014 }
4015 }
4016
4017 ret = zonegroup.remove_zone(zone_id);
4018 if (ret < 0) {
4019 cerr << "failed to remove zone: " << cpp_strerror(-ret) << std::endl;
4020 return -ret;
4021 }
4022
4023 encode_json("zonegroup", zonegroup, formatter);
4024 formatter->flush(cout);
4025 }
4026 break;
4027 case OPT_ZONEGROUP_RENAME:
4028 {
4029 if (zonegroup_new_name.empty()) {
4030 cerr << " missing zonegroup new name" << std::endl;
4031 return EINVAL;
4032 }
4033 if (zonegroup_id.empty() && zonegroup_name.empty()) {
4034 cerr << "no zonegroup name or id provided" << std::endl;
4035 return EINVAL;
4036 }
4037 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4038 int ret = zonegroup.init(g_ceph_context, store);
4039 if (ret < 0) {
4040 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
4041 return -ret;
4042 }
4043 ret = zonegroup.rename(zonegroup_new_name);
4044 if (ret < 0) {
4045 cerr << "failed to rename zonegroup: " << cpp_strerror(-ret) << std::endl;
4046 return -ret;
4047 }
4048 }
4049 break;
4050 case OPT_ZONEGROUP_PLACEMENT_LIST:
4051 {
4052 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4053 int ret = zonegroup.init(g_ceph_context, store);
4054 if (ret < 0) {
4055 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
4056 return -ret;
4057 }
4058
4059 encode_json("placement_targets", zonegroup.placement_targets, formatter);
4060 formatter->flush(cout);
4061 cout << std::endl;
4062 }
4063 break;
4064 case OPT_ZONEGROUP_PLACEMENT_ADD:
4065 case OPT_ZONEGROUP_PLACEMENT_MODIFY:
4066 case OPT_ZONEGROUP_PLACEMENT_RM:
4067 case OPT_ZONEGROUP_PLACEMENT_DEFAULT:
4068 {
4069 if (placement_id.empty()) {
4070 cerr << "ERROR: --placement-id not specified" << std::endl;
4071 return EINVAL;
4072 }
4073
4074 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4075 int ret = zonegroup.init(g_ceph_context, store);
4076 if (ret < 0) {
4077 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
4078 return -ret;
4079 }
4080
4081 if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_ADD) {
4082 RGWZoneGroupPlacementTarget target;
4083 target.name = placement_id;
4084 for (auto& t : tags) {
4085 target.tags.insert(t);
4086 }
4087 zonegroup.placement_targets[placement_id] = target;
4088 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_MODIFY) {
4089 RGWZoneGroupPlacementTarget& target = zonegroup.placement_targets[placement_id];
4090 if (!tags.empty()) {
4091 target.tags.clear();
4092 for (auto& t : tags) {
4093 target.tags.insert(t);
4094 }
4095 }
4096 target.name = placement_id;
4097 for (auto& t : tags_rm) {
4098 target.tags.erase(t);
4099 }
4100 for (auto& t : tags_add) {
4101 target.tags.insert(t);
4102 }
4103 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_RM) {
4104 zonegroup.placement_targets.erase(placement_id);
4105 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_DEFAULT) {
4106 if (!zonegroup.placement_targets.count(placement_id)) {
4107 cerr << "failed to find a zonegroup placement target named '"
4108 << placement_id << "'" << std::endl;
4109 return -ENOENT;
4110 }
4111 zonegroup.default_placement = placement_id;
4112 }
4113
4114 zonegroup.post_process_params();
4115 ret = zonegroup.update();
4116 if (ret < 0) {
4117 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
4118 return -ret;
4119 }
4120
4121 encode_json("placement_targets", zonegroup.placement_targets, formatter);
4122 formatter->flush(cout);
4123 }
4124 break;
4125 case OPT_ZONE_CREATE:
4126 {
4127 if (zone_name.empty()) {
4128 cerr << "zone name not provided" << std::endl;
4129 return EINVAL;
4130 }
4131 int ret;
4132 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4133 /* if the user didn't provide zonegroup info , create stand alone zone */
4134 if (!zonegroup_id.empty() || !zonegroup_name.empty()) {
4135 ret = zonegroup.init(g_ceph_context, store);
4136 if (ret < 0) {
4137 cerr << "unable to initialize zonegroup " << zonegroup_name << ": " << cpp_strerror(-ret) << std::endl;
4138 return -ret;
4139 }
4140 if (realm_id.empty() && realm_name.empty()) {
4141 realm_id = zonegroup.realm_id;
4142 }
4143 }
4144
4145 RGWZoneParams zone(zone_id, zone_name);
4146 ret = zone.init(g_ceph_context, store, false);
4147 if (ret < 0) {
4148 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4149 return -ret;
4150 }
4151
4152 zone.system_key.id = access_key;
4153 zone.system_key.key = secret_key;
4154 zone.realm_id = realm_id;
4155 zone.tier_config = tier_config_add;
4156
4157 ret = zone.create();
4158 if (ret < 0) {
4159 cerr << "failed to create zone " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
4160 return -ret;
4161 }
4162
4163 if (!zonegroup_id.empty() || !zonegroup_name.empty()) {
4164 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
4165 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
4166 ret = zonegroup.add_zone(zone,
4167 (is_master_set ? &is_master : NULL),
4168 (is_read_only_set ? &read_only : NULL),
4169 endpoints,
4170 ptier_type,
4171 psync_from_all,
4172 sync_from, sync_from_rm);
4173 if (ret < 0) {
4174 cerr << "failed to add zone " << zone_name << " to zonegroup " << zonegroup.get_name()
4175 << ": " << cpp_strerror(-ret) << std::endl;
4176 return -ret;
4177 }
4178 }
4179
4180 if (set_default) {
4181 ret = zone.set_as_default();
4182 if (ret < 0) {
4183 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
4184 }
4185 }
4186
4187 encode_json("zone", zone, formatter);
4188 formatter->flush(cout);
4189 cout << std::endl;
4190 }
4191 break;
4192 case OPT_ZONE_DEFAULT:
4193 {
4194 RGWZoneGroup zonegroup(zonegroup_id,zonegroup_name);
4195 int ret = zonegroup.init(g_ceph_context, store);
4196 if (ret < 0) {
4197 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
4198 }
4199 if (zone_id.empty() && zone_name.empty()) {
4200 cerr << "no zone name or id provided" << std::endl;
4201 return EINVAL;
4202 }
4203 RGWZoneParams zone(zone_id, zone_name);
4204 ret = zone.init(g_ceph_context, store);
4205 if (ret < 0) {
4206 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4207 return -ret;
4208 }
4209 ret = zone.set_as_default();
4210 if (ret < 0) {
4211 cerr << "failed to set zone as default: " << cpp_strerror(-ret) << std::endl;
4212 return -ret;
4213 }
4214 }
4215 break;
4216 case OPT_ZONE_DELETE:
4217 {
4218 if (zone_id.empty() && zone_name.empty()) {
4219 cerr << "no zone name or id provided" << std::endl;
4220 return EINVAL;
4221 }
4222 RGWZoneParams zone(zone_id, zone_name);
4223 int ret = zone.init(g_ceph_context, store);
4224 if (ret < 0) {
4225 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4226 return -ret;
4227 }
4228
4229 list<string> zonegroups;
4230 ret = store->list_zonegroups(zonegroups);
4231 if (ret < 0) {
4232 cerr << "failed to list zonegroups: " << cpp_strerror(-ret) << std::endl;
4233 return -ret;
4234 }
4235
4236 for (list<string>::iterator iter = zonegroups.begin(); iter != zonegroups.end(); ++iter) {
4237 RGWZoneGroup zonegroup(string(), *iter);
4238 int ret = zonegroup.init(g_ceph_context, store);
4239 if (ret < 0) {
4240 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
4241 continue;
4242 }
4243 ret = zonegroup.remove_zone(zone.get_id());
4244 if (ret < 0 && ret != -ENOENT) {
4245 cerr << "failed to remove zone " << zone_name << " from zonegroup " << zonegroup.get_name() << ": "
4246 << cpp_strerror(-ret) << std::endl;
4247 }
4248 }
4249
4250 ret = zone.delete_obj();
4251 if (ret < 0) {
4252 cerr << "failed to delete zone " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
4253 return -ret;
4254 }
4255 }
4256 break;
4257 case OPT_ZONE_GET:
4258 {
4259 RGWZoneParams zone(zone_id, zone_name);
4260 int ret = zone.init(g_ceph_context, store);
4261 if (ret < 0) {
4262 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4263 return -ret;
4264 }
4265 encode_json("zone", zone, formatter);
4266 formatter->flush(cout);
4267 }
4268 break;
4269 case OPT_ZONE_SET:
4270 {
4271 RGWZoneParams zone(zone_name);
4272 int ret = zone.init(g_ceph_context, store, false);
4273 if (ret < 0) {
4274 return -ret;
4275 }
4276
4277 ret = zone.read();
4278 if (ret < 0 && ret != -ENOENT) {
4279 cerr << "zone.read() returned ret=" << ret << std::endl;
4280 return -ret;
4281 }
4282
4283 string orig_id = zone.get_id();
4284
4285 ret = read_decode_json(infile, zone);
4286 if (ret < 0) {
4287 return 1;
4288 }
4289
4290 if(zone.realm_id.empty()) {
4291 RGWRealm realm(realm_id, realm_name);
4292 int ret = realm.init(g_ceph_context, store);
4293 if (ret < 0 && ret != -ENOENT) {
4294 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
4295 return -ret;
4296 }
4297 zone.realm_id = realm.get_id();
4298 }
4299
4300 if( !zone_name.empty() && !zone.get_name().empty() && zone.get_name() != zone_name) {
4301 cerr << "Error: zone name" << zone_name << " is different than the zone name " << zone.get_name() << " in the provided json " << std::endl;
4302 return EINVAL;
4303 }
4304
4305 if (zone.get_name().empty()) {
4306 zone.set_name(zone_name);
4307 if (zone.get_name().empty()) {
4308 cerr << "no zone name specified" << std::endl;
4309 return EINVAL;
4310 }
4311 }
4312
4313 zone_name = zone.get_name();
4314
4315 if (zone.get_id().empty()) {
4316 zone.set_id(orig_id);
4317 }
4318
4319 if (zone.get_id().empty()) {
4320 cerr << "no zone name id the json provided, assuming old format" << std::endl;
4321 if (zone_name.empty()) {
4322 cerr << "missing zone name" << std::endl;
4323 return EINVAL;
4324 }
4325 zone.set_name(zone_name);
4326 zone.set_id(zone_name);
4327 }
4328
4329 cerr << "zone id " << zone.get_id();
4330 ret = zone.fix_pool_names();
4331 if (ret < 0) {
4332 cerr << "ERROR: couldn't fix zone: " << cpp_strerror(-ret) << std::endl;
4333 return -ret;
4334 }
4335 ret = zone.write(false);
4336 if (ret < 0) {
4337 cerr << "ERROR: couldn't create zone: " << cpp_strerror(-ret) << std::endl;
4338 return 1;
4339 }
4340
4341 if (set_default) {
4342 ret = zone.set_as_default();
4343 if (ret < 0) {
4344 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
4345 }
4346 }
4347
4348 encode_json("zone", zone, formatter);
4349 formatter->flush(cout);
4350 }
4351 break;
4352 case OPT_ZONE_LIST:
4353 {
4354 list<string> zones;
4355 int ret = store->list_zones(zones);
4356 if (ret < 0) {
4357 cerr << "failed to list zones: " << cpp_strerror(-ret) << std::endl;
4358 return -ret;
4359 }
4360
4361 RGWZoneParams zone;
4362 ret = zone.init(g_ceph_context, store, false);
4363 if (ret < 0) {
4364 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4365 return -ret;
4366 }
4367 string default_zone;
4368 ret = zone.read_default_id(default_zone);
4369 if (ret < 0 && ret != -ENOENT) {
4370 cerr << "could not determine default zone: " << cpp_strerror(-ret) << std::endl;
4371 }
4372 formatter->open_object_section("zones_list");
4373 encode_json("default_info", default_zone, formatter);
4374 encode_json("zones", zones, formatter);
4375 formatter->close_section();
4376 formatter->flush(cout);
4377 cout << std::endl;
4378 }
4379 break;
4380 case OPT_ZONE_MODIFY:
4381 {
4382 RGWZoneParams zone(zone_id, zone_name);
4383 int ret = zone.init(g_ceph_context, store);
4384 if (ret < 0) {
4385 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4386 return -ret;
4387 }
4388
4389 bool need_zone_update = false;
4390 if (!access_key.empty()) {
4391 zone.system_key.id = access_key;
4392 need_zone_update = true;
4393 }
4394
4395 if (!secret_key.empty()) {
4396 zone.system_key.key = secret_key;
4397 need_zone_update = true;
4398 }
4399
4400 if (!realm_id.empty()) {
4401 zone.realm_id = realm_id;
4402 need_zone_update = true;
4403 } else if (!realm_name.empty()) {
4404 // get realm id from name
4405 RGWRealm realm{g_ceph_context, store};
4406 ret = realm.read_id(realm_name, zone.realm_id);
4407 if (ret < 0) {
4408 cerr << "failed to find realm by name " << realm_name << std::endl;
4409 return -ret;
4410 }
4411 need_zone_update = true;
4412 }
4413
4414 for (auto add : tier_config_add) {
4415 zone.tier_config[add.first] = add.second;
4416 need_zone_update = true;
4417 }
4418
4419 for (auto rm : tier_config_rm) {
4420 zone.tier_config.erase(rm.first);
4421 need_zone_update = true;
4422 }
4423
4424 if (need_zone_update) {
4425 ret = zone.update();
4426 if (ret < 0) {
4427 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
4428 return -ret;
4429 }
4430 }
4431
4432 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4433 ret = zonegroup.init(g_ceph_context, store);
4434 if (ret < 0) {
4435 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
4436 return -ret;
4437 }
4438 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
4439
4440 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
4441
4442 ret = zonegroup.add_zone(zone,
4443 (is_master_set ? &is_master : NULL),
4444 (is_read_only_set ? &read_only : NULL),
4445 endpoints, ptier_type,
4446 psync_from_all, sync_from, sync_from_rm);
4447 if (ret < 0) {
4448 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
4449 return -ret;
4450 }
4451
4452 ret = zonegroup.update();
4453 if (ret < 0) {
4454 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
4455 return -ret;
4456 }
4457
4458 if (set_default) {
4459 ret = zone.set_as_default();
4460 if (ret < 0) {
4461 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
4462 }
4463 }
4464
4465 encode_json("zone", zone, formatter);
4466 formatter->flush(cout);
4467 }
4468 break;
4469 case OPT_ZONE_RENAME:
4470 {
4471 if (zone_new_name.empty()) {
4472 cerr << " missing zone new name" << std::endl;
4473 return EINVAL;
4474 }
4475 if (zone_id.empty() && zone_name.empty()) {
4476 cerr << "no zonegroup name or id provided" << std::endl;
4477 return EINVAL;
4478 }
4479 RGWZoneParams zone(zone_id,zone_name);
4480 int ret = zone.init(g_ceph_context, store);
4481 if (ret < 0) {
4482 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4483 return -ret;
4484 }
4485 ret = zone.rename(zone_new_name);
4486 if (ret < 0) {
4487 cerr << "failed to rename zone " << zone_name << " to " << zone_new_name << ": " << cpp_strerror(-ret)
4488 << std::endl;
4489 return -ret;
4490 }
4491 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4492 ret = zonegroup.init(g_ceph_context, store);
4493 if (ret < 0) {
4494 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
4495 } else {
4496 ret = zonegroup.rename_zone(zone);
4497 if (ret < 0 && ret ) {
4498 cerr << "Error in zonegroup rename for " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
4499 return -ret;
4500 }
4501 }
4502 }
4503 break;
4504 case OPT_ZONE_PLACEMENT_ADD:
4505 case OPT_ZONE_PLACEMENT_MODIFY:
4506 case OPT_ZONE_PLACEMENT_RM:
4507 {
4508 if (placement_id.empty()) {
4509 cerr << "ERROR: --placement-id not specified" << std::endl;
4510 return EINVAL;
4511 }
4512 // validate compression type
4513 if (compression_type && *compression_type != "random"
4514 && !Compressor::get_comp_alg_type(*compression_type)) {
4515 std::cerr << "Unrecognized compression type" << std::endl;
4516 return EINVAL;
4517 }
4518
4519 RGWZoneParams zone(zone_id, zone_name);
4520 int ret = zone.init(g_ceph_context, store);
4521 if (ret < 0) {
4522 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4523 return -ret;
4524 }
4525
4526 if (opt_cmd == OPT_ZONE_PLACEMENT_ADD) {
4527 // pool names are required
4528 if (!index_pool || index_pool->empty() ||
4529 !data_pool || data_pool->empty()) {
4530 cerr << "ERROR: need to specify both --index-pool and --data-pool" << std::endl;
4531 return EINVAL;
4532 }
4533
4534 RGWZonePlacementInfo& info = zone.placement_pools[placement_id];
4535
4536 info.index_pool = *index_pool;
4537 info.data_pool = *data_pool;
4538 if (data_extra_pool) {
4539 info.data_extra_pool = *data_extra_pool;
4540 }
4541 if (index_type_specified) {
4542 info.index_type = placement_index_type;
4543 }
4544 if (compression_type) {
4545 info.compression_type = *compression_type;
4546 }
4547 } else if (opt_cmd == OPT_ZONE_PLACEMENT_MODIFY) {
4548 auto p = zone.placement_pools.find(placement_id);
4549 if (p == zone.placement_pools.end()) {
4550 cerr << "ERROR: zone placement target '" << placement_id
4551 << "' not found" << std::endl;
4552 return -ENOENT;
4553 }
4554 auto& info = p->second;
4555 if (index_pool && !index_pool->empty()) {
4556 info.index_pool = *index_pool;
4557 }
4558 if (data_pool && !data_pool->empty()) {
4559 info.data_pool = *data_pool;
4560 }
4561 if (data_extra_pool) {
4562 info.data_extra_pool = *data_extra_pool;
4563 }
4564 if (index_type_specified) {
4565 info.index_type = placement_index_type;
4566 }
4567 if (compression_type) {
4568 info.compression_type = *compression_type;
4569 }
4570 } else if (opt_cmd == OPT_ZONE_PLACEMENT_RM) {
4571 zone.placement_pools.erase(placement_id);
4572 }
4573
4574 ret = zone.update();
4575 if (ret < 0) {
4576 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
4577 return -ret;
4578 }
4579
4580 encode_json("zone", zone, formatter);
4581 formatter->flush(cout);
4582 }
4583 break;
4584 case OPT_ZONE_PLACEMENT_LIST:
4585 {
4586 RGWZoneParams zone(zone_id, zone_name);
4587 int ret = zone.init(g_ceph_context, store);
4588 if (ret < 0) {
4589 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4590 return -ret;
4591 }
4592 encode_json("placement_pools", zone.placement_pools, formatter);
4593 formatter->flush(cout);
4594 }
4595 break;
4596 }
4597 return 0;
4598 }
4599
4600 if (!user_id.empty()) {
4601 user_op.set_user_id(user_id);
4602 bucket_op.set_user_id(user_id);
4603 }
4604
4605 if (!display_name.empty())
4606 user_op.set_display_name(display_name);
4607
4608 if (!user_email.empty())
4609 user_op.set_user_email(user_email);
4610
4611 if (!access_key.empty())
4612 user_op.set_access_key(access_key);
4613
4614 if (!secret_key.empty())
4615 user_op.set_secret_key(secret_key);
4616
4617 if (!subuser.empty())
4618 user_op.set_subuser(subuser);
4619
4620 if (!caps.empty())
4621 user_op.set_caps(caps);
4622
4623 user_op.set_purge_data(purge_data);
4624
4625 if (purge_keys)
4626 user_op.set_purge_keys();
4627
4628 if (gen_access_key)
4629 user_op.set_generate_key();
4630
4631 if (gen_secret_key)
4632 user_op.set_gen_secret(); // assume that a key pair should be created
4633
4634 if (max_buckets_specified)
4635 user_op.set_max_buckets(max_buckets);
4636
4637 if (admin_specified)
4638 user_op.set_admin(admin);
4639
4640 if (system_specified)
4641 user_op.set_system(system);
4642
4643 if (set_perm)
4644 user_op.set_perm(perm_mask);
4645
4646 if (set_temp_url_key) {
4647 map<int, string>::iterator iter = temp_url_keys.begin();
4648 for (; iter != temp_url_keys.end(); ++iter) {
4649 user_op.set_temp_url_key(iter->second, iter->first);
4650 }
4651 }
4652
4653 if (!op_mask_str.empty()) {
4654 uint32_t op_mask;
4655 int ret = rgw_parse_op_type_list(op_mask_str, &op_mask);
4656 if (ret < 0) {
4657 cerr << "failed to parse op_mask: " << cpp_strerror(-ret) << std::endl;
4658 return -ret;
4659 }
4660
4661 user_op.set_op_mask(op_mask);
4662 }
4663
4664 if (key_type != KEY_TYPE_UNDEFINED)
4665 user_op.set_key_type(key_type);
4666
4667 // set suspension operation parameters
4668 if (opt_cmd == OPT_USER_ENABLE)
4669 user_op.set_suspension(false);
4670 else if (opt_cmd == OPT_USER_SUSPEND)
4671 user_op.set_suspension(true);
4672
4673 // RGWUser to use for user operations
4674 RGWUser user;
4675 int ret = 0;
4676 if (!user_id.empty() || !subuser.empty()) {
4677 ret = user.init(store, user_op);
4678 if (ret < 0) {
4679 cerr << "user.init failed: " << cpp_strerror(-ret) << std::endl;
4680 return -ret;
4681 }
4682 }
4683
4684 /* populate bucket operation */
4685 bucket_op.set_bucket_name(bucket_name);
4686 bucket_op.set_object(object);
4687 bucket_op.set_check_objects(check_objects);
4688 bucket_op.set_delete_children(delete_child_objects);
4689 bucket_op.set_fix_index(fix);
4690 bucket_op.set_max_aio(max_concurrent_ios);
4691
4692 // required to gather errors from operations
4693 std::string err_msg;
4694
4695 bool output_user_info = true;
4696
4697 switch (opt_cmd) {
4698 case OPT_USER_INFO:
4699 break;
4700 case OPT_USER_CREATE:
4701 if (!user_op.has_existing_user()) {
4702 user_op.set_generate_key(); // generate a new key by default
4703 }
4704 ret = user.add(user_op, &err_msg);
4705 if (ret < 0) {
4706 cerr << "could not create user: " << err_msg << std::endl;
4707 if (ret == -ERR_INVALID_TENANT_NAME)
4708 ret = -EINVAL;
4709
4710 return -ret;
4711 }
4712 if (!subuser.empty()) {
4713 ret = user.subusers.add(user_op, &err_msg);
4714 if (ret < 0) {
4715 cerr << "could not create subuser: " << err_msg << std::endl;
4716 return -ret;
4717 }
4718 }
4719 break;
4720 case OPT_USER_RM:
4721 ret = user.remove(user_op, &err_msg);
4722 if (ret < 0) {
4723 cerr << "could not remove user: " << err_msg << std::endl;
4724 return -ret;
4725 }
4726
4727 output_user_info = false;
4728 break;
4729 case OPT_USER_ENABLE:
4730 case OPT_USER_SUSPEND:
4731 case OPT_USER_MODIFY:
4732 ret = user.modify(user_op, &err_msg);
4733 if (ret < 0) {
4734 cerr << "could not modify user: " << err_msg << std::endl;
4735 return -ret;
4736 }
4737
4738 break;
4739 case OPT_SUBUSER_CREATE:
4740 ret = user.subusers.add(user_op, &err_msg);
4741 if (ret < 0) {
4742 cerr << "could not create subuser: " << err_msg << std::endl;
4743 return -ret;
4744 }
4745
4746 break;
4747 case OPT_SUBUSER_MODIFY:
4748 ret = user.subusers.modify(user_op, &err_msg);
4749 if (ret < 0) {
4750 cerr << "could not modify subuser: " << err_msg << std::endl;
4751 return -ret;
4752 }
4753
4754 break;
4755 case OPT_SUBUSER_RM:
4756 ret = user.subusers.remove(user_op, &err_msg);
4757 if (ret < 0) {
4758 cerr << "could not remove subuser: " << err_msg << std::endl;
4759 return -ret;
4760 }
4761
4762 break;
4763 case OPT_CAPS_ADD:
4764 ret = user.caps.add(user_op, &err_msg);
4765 if (ret < 0) {
4766 cerr << "could not add caps: " << err_msg << std::endl;
4767 return -ret;
4768 }
4769
4770 break;
4771 case OPT_CAPS_RM:
4772 ret = user.caps.remove(user_op, &err_msg);
4773 if (ret < 0) {
4774 cerr << "could not remove caps: " << err_msg << std::endl;
4775 return -ret;
4776 }
4777
4778 break;
4779 case OPT_KEY_CREATE:
4780 ret = user.keys.add(user_op, &err_msg);
4781 if (ret < 0) {
4782 cerr << "could not create key: " << err_msg << std::endl;
4783 return -ret;
4784 }
4785
4786 break;
4787 case OPT_KEY_RM:
4788 ret = user.keys.remove(user_op, &err_msg);
4789 if (ret < 0) {
4790 cerr << "could not remove key: " << err_msg << std::endl;
4791 return -ret;
4792 }
4793 break;
4794 case OPT_PERIOD_PUSH:
4795 {
4796 RGWEnv env;
4797 req_info info(g_ceph_context, &env);
4798 info.method = "POST";
4799 info.request_uri = "/admin/realm/period";
4800
4801 map<string, string> &params = info.args.get_params();
4802 if (!realm_id.empty())
4803 params["realm_id"] = realm_id;
4804 if (!realm_name.empty())
4805 params["realm_name"] = realm_name;
4806 if (!period_id.empty())
4807 params["period_id"] = period_id;
4808 if (!period_epoch.empty())
4809 params["epoch"] = period_epoch;
4810
4811 // load the period
4812 RGWPeriod period(period_id);
4813 int ret = period.init(g_ceph_context, store);
4814 if (ret < 0) {
4815 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
4816 return -ret;
4817 }
4818 // json format into a bufferlist
4819 JSONFormatter jf(false);
4820 encode_json("period", period, &jf);
4821 bufferlist bl;
4822 jf.flush(bl);
4823
4824 JSONParser p;
4825 ret = send_to_remote_or_url(nullptr, url, access_key, secret_key,
4826 info, bl, p);
4827 if (ret < 0) {
4828 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
4829 return -ret;
4830 }
4831 }
4832 return 0;
4833 case OPT_PERIOD_UPDATE:
4834 {
4835 int ret = update_period(realm_id, realm_name, period_id, period_epoch,
4836 commit, remote, url, access_key, secret_key,
4837 formatter, yes_i_really_mean_it);
4838 if (ret < 0) {
4839 return -ret;
4840 }
4841 }
4842 return 0;
4843 case OPT_PERIOD_COMMIT:
4844 {
4845 // read realm and staging period
4846 RGWRealm realm(realm_id, realm_name);
4847 int ret = realm.init(g_ceph_context, store);
4848 if (ret < 0) {
4849 cerr << "Error initializing realm: " << cpp_strerror(-ret) << std::endl;
4850 return -ret;
4851 }
4852 RGWPeriod period(RGWPeriod::get_staging_id(realm.get_id()), 1);
4853 ret = period.init(g_ceph_context, store, realm.get_id());
4854 if (ret < 0) {
4855 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
4856 return -ret;
4857 }
4858 ret = commit_period(realm, period, remote, url, access_key, secret_key,
4859 yes_i_really_mean_it);
4860 if (ret < 0) {
4861 cerr << "failed to commit period: " << cpp_strerror(-ret) << std::endl;
4862 return -ret;
4863 }
4864
4865 encode_json("period", period, formatter);
4866 formatter->flush(cout);
4867 cout << std::endl;
4868 }
4869 return 0;
4870 case OPT_ROLE_CREATE:
4871 {
4872 if (role_name.empty()) {
4873 cerr << "ERROR: role name is empty" << std::endl;
4874 return -EINVAL;
4875 }
4876
4877 if (assume_role_doc.empty()) {
4878 cerr << "ERROR: assume role policy document is empty" << std::endl;
4879 return -EINVAL;
4880 }
4881 /* The following two calls will be replaced by read_decode_json or something
4882 similar when the code for AWS Policies is in places */
4883 bufferlist bl;
4884 int ret = read_input(assume_role_doc, bl);
4885 if (ret < 0) {
4886 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4887 return ret;
4888 }
4889 JSONParser p;
4890 if (!p.parse(bl.c_str(), bl.length())) {
4891 cout << "ERROR: failed to parse JSON: " << assume_role_doc << std::endl;
4892 return -EINVAL;
4893 }
4894 string trust_policy = bl.to_str();
4895 RGWRole role(g_ceph_context, store, role_name, path, trust_policy, tenant);
4896 ret = role.create(true);
4897 if (ret < 0) {
4898 return -ret;
4899 }
4900 show_role_info(role, formatter);
4901 return 0;
4902 }
4903 case OPT_ROLE_DELETE:
4904 {
4905 if (role_name.empty()) {
4906 cerr << "ERROR: empty role name" << std::endl;
4907 return -EINVAL;
4908 }
4909 RGWRole role(g_ceph_context, store, role_name, tenant);
4910 ret = role.delete_obj();
4911 if (ret < 0) {
4912 return -ret;
4913 }
4914 cout << "role: " << role_name << " successfully deleted" << std::endl;
4915 return 0;
4916 }
4917 case OPT_ROLE_GET:
4918 {
4919 if (role_name.empty()) {
4920 cerr << "ERROR: empty role name" << std::endl;
4921 return -EINVAL;
4922 }
4923 RGWRole role(g_ceph_context, store, role_name, tenant);
4924 ret = role.get();
4925 if (ret < 0) {
4926 return -ret;
4927 }
4928 show_role_info(role, formatter);
4929 return 0;
4930 }
4931 case OPT_ROLE_MODIFY:
4932 {
4933 if (role_name.empty()) {
4934 cerr << "ERROR: role name is empty" << std::endl;
4935 return -EINVAL;
4936 }
4937
4938 if (assume_role_doc.empty()) {
4939 cerr << "ERROR: assume role policy document is empty" << std::endl;
4940 return -EINVAL;
4941 }
4942
4943 /* The following two calls will be replaced by read_decode_json or something
4944 similar when the code for AWS Policies is in place */
4945 bufferlist bl;
4946 int ret = read_input(assume_role_doc, bl);
4947 if (ret < 0) {
4948 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4949 return ret;
4950 }
4951 JSONParser p;
4952 if (!p.parse(bl.c_str(), bl.length())) {
4953 cout << "ERROR: failed to parse JSON: " << assume_role_doc << std::endl;
4954 return -EINVAL;
4955 }
4956 string trust_policy = bl.to_str();
4957 RGWRole role(g_ceph_context, store, role_name, tenant);
4958 ret = role.get();
4959 if (ret < 0) {
4960 return -ret;
4961 }
4962 role.update_trust_policy(trust_policy);
4963 ret = role.update();
4964 if (ret < 0) {
4965 return -ret;
4966 }
4967 cout << "Assume role policy document updated successfully for role: " << role_name << std::endl;
4968 return 0;
4969 }
4970 case OPT_ROLE_LIST:
4971 {
4972 vector<RGWRole> result;
4973 ret = RGWRole::get_roles_by_path_prefix(store, g_ceph_context, path_prefix, tenant, result);
4974 if (ret < 0) {
4975 return -ret;
4976 }
4977 show_roles_info(result, formatter);
4978 return 0;
4979 }
4980 case OPT_ROLE_POLICY_PUT:
4981 {
4982 if (role_name.empty()) {
4983 cerr << "role name is empty" << std::endl;
4984 return -EINVAL;
4985 }
4986
4987 if (policy_name.empty()) {
4988 cerr << "policy name is empty" << std::endl;
4989 return -EINVAL;
4990 }
4991
4992 if (perm_policy_doc.empty()) {
4993 cerr << "permission policy document is empty" << std::endl;
4994 return -EINVAL;
4995 }
4996
4997 /* The following two calls will be replaced by read_decode_json or something
4998 similar, when code for AWS Policies is in place.*/
4999 bufferlist bl;
5000 int ret = read_input(perm_policy_doc, bl);
5001 if (ret < 0) {
5002 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
5003 return ret;
5004 }
5005 JSONParser p;
5006 if (!p.parse(bl.c_str(), bl.length())) {
5007 cout << "ERROR: failed to parse JSON: " << std::endl;
5008 return -EINVAL;
5009 }
5010 string perm_policy;
5011 perm_policy = bl.c_str();
5012
5013 RGWRole role(g_ceph_context, store, role_name, tenant);
5014 ret = role.get();
5015 if (ret < 0) {
5016 return -ret;
5017 }
5018 role.set_perm_policy(policy_name, perm_policy);
5019 ret = role.update();
5020 if (ret < 0) {
5021 return -ret;
5022 }
5023 cout << "Permission policy attached successfully" << std::endl;
5024 return 0;
5025 }
5026 case OPT_ROLE_POLICY_LIST:
5027 {
5028 if (role_name.empty()) {
5029 cerr << "ERROR: Role name is empty" << std::endl;
5030 return -EINVAL;
5031 }
5032 RGWRole role(g_ceph_context, store, role_name, tenant);
5033 ret = role.get();
5034 if (ret < 0) {
5035 return -ret;
5036 }
5037 std::vector<string> policy_names = role.get_role_policy_names();
5038 show_policy_names(policy_names, formatter);
5039 return 0;
5040 }
5041 case OPT_ROLE_POLICY_GET:
5042 {
5043 if (role_name.empty()) {
5044 cerr << "ERROR: role name is empty" << std::endl;
5045 return -EINVAL;
5046 }
5047
5048 if (policy_name.empty()) {
5049 cerr << "ERROR: policy name is empty" << std::endl;
5050 return -EINVAL;
5051 }
5052 RGWRole role(g_ceph_context, store, role_name, tenant);
5053 int ret = role.get();
5054 if (ret < 0) {
5055 return -ret;
5056 }
5057 string perm_policy;
5058 ret = role.get_role_policy(policy_name, perm_policy);
5059 if (ret < 0) {
5060 return -ret;
5061 }
5062 show_perm_policy(perm_policy, formatter);
5063 return 0;
5064 }
5065 case OPT_ROLE_POLICY_DELETE:
5066 {
5067 if (role_name.empty()) {
5068 cerr << "ERROR: role name is empty" << std::endl;
5069 return -EINVAL;
5070 }
5071
5072 if (policy_name.empty()) {
5073 cerr << "ERROR: policy name is empty" << std::endl;
5074 return -EINVAL;
5075 }
5076 RGWRole role(g_ceph_context, store, role_name, tenant);
5077 ret = role.get();
5078 if (ret < 0) {
5079 return -ret;
5080 }
5081 ret = role.delete_policy(policy_name);
5082 if (ret < 0) {
5083 return -ret;
5084 }
5085 ret = role.update();
5086 if (ret < 0) {
5087 return -ret;
5088 }
5089 cout << "Policy: " << policy_name << " successfully deleted for role: "
5090 << role_name << std::endl;
5091 return 0;
5092 }
5093 default:
5094 output_user_info = false;
5095 }
5096
5097 // output the result of a user operation
5098 if (output_user_info) {
5099 ret = user.info(info, &err_msg);
5100 if (ret < 0) {
5101 cerr << "could not fetch user info: " << err_msg << std::endl;
5102 return -ret;
5103 }
5104 show_user_info(info, formatter);
5105 }
5106
5107 if (opt_cmd == OPT_POLICY) {
5108 if (format == "xml") {
5109 int ret = RGWBucketAdminOp::dump_s3_policy(store, bucket_op, cout);
5110 if (ret < 0) {
5111 cerr << "ERROR: failed to get policy: " << cpp_strerror(-ret) << std::endl;
5112 return -ret;
5113 }
5114 } else {
5115 int ret = RGWBucketAdminOp::get_policy(store, bucket_op, f);
5116 if (ret < 0) {
5117 cerr << "ERROR: failed to get policy: " << cpp_strerror(-ret) << std::endl;
5118 return -ret;
5119 }
5120 }
5121 }
5122
5123 if (opt_cmd == OPT_BUCKET_LIMIT_CHECK) {
5124 void *handle;
5125 std::list<std::string> user_ids;
5126 metadata_key = "user";
5127 int max = 1000;
5128
5129 bool truncated;
5130
5131 if (! user_id.empty()) {
5132 user_ids.push_back(user_id.id);
5133 ret =
5134 RGWBucketAdminOp::limit_check(store, bucket_op, user_ids, f,
5135 warnings_only);
5136 } else {
5137 /* list users in groups of max-keys, then perform user-bucket
5138 * limit-check on each group */
5139 ret = store->meta_mgr->list_keys_init(metadata_key, &handle);
5140 if (ret < 0) {
5141 cerr << "ERROR: buckets limit check can't get user metadata_key: "
5142 << cpp_strerror(-ret) << std::endl;
5143 return -ret;
5144 }
5145
5146 do {
5147 ret = store->meta_mgr->list_keys_next(handle, max, user_ids,
5148 &truncated);
5149 if (ret < 0 && ret != -ENOENT) {
5150 cerr << "ERROR: buckets limit check lists_keys_next(): "
5151 << cpp_strerror(-ret) << std::endl;
5152 break;
5153 } else {
5154 /* ok, do the limit checks for this group */
5155 ret =
5156 RGWBucketAdminOp::limit_check(store, bucket_op, user_ids, f,
5157 warnings_only);
5158 if (ret < 0)
5159 break;
5160 }
5161 user_ids.clear();
5162 } while (truncated);
5163 store->meta_mgr->list_keys_complete(handle);
5164 }
5165 return -ret;
5166 } /* OPT_BUCKET_LIMIT_CHECK */
5167
5168 if (opt_cmd == OPT_BUCKETS_LIST) {
5169 if (bucket_name.empty()) {
5170 RGWBucketAdminOp::info(store, bucket_op, f);
5171 } else {
5172 RGWBucketInfo bucket_info;
5173 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5174 if (ret < 0) {
5175 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5176 return -ret;
5177 }
5178 formatter->open_array_section("entries");
5179 bool truncated;
5180 int count = 0;
5181 if (max_entries < 0)
5182 max_entries = 1000;
5183
5184 string prefix;
5185 string delim;
5186 vector<rgw_bucket_dir_entry> result;
5187 map<string, bool> common_prefixes;
5188 string ns;
5189
5190 RGWRados::Bucket target(store, bucket_info);
5191 RGWRados::Bucket::List list_op(&target);
5192
5193 list_op.params.prefix = prefix;
5194 list_op.params.delim = delim;
5195 list_op.params.marker = rgw_obj_key(marker);
5196 list_op.params.ns = ns;
5197 list_op.params.enforce_ns = false;
5198 list_op.params.list_versions = true;
5199
5200 do {
5201 ret = list_op.list_objects(max_entries - count, &result, &common_prefixes, &truncated);
5202 if (ret < 0) {
5203 cerr << "ERROR: store->list_objects(): " << cpp_strerror(-ret) << std::endl;
5204 return -ret;
5205 }
5206
5207 count += result.size();
5208
5209 for (vector<rgw_bucket_dir_entry>::iterator iter = result.begin(); iter != result.end(); ++iter) {
5210 rgw_bucket_dir_entry& entry = *iter;
5211 encode_json("entry", entry, formatter);
5212 }
5213 formatter->flush(cout);
5214 } while (truncated && count < max_entries);
5215
5216 formatter->close_section();
5217 formatter->flush(cout);
5218 } /* have bucket_name */
5219 } /* OPT_BUCKETS_LIST */
5220
5221 if (opt_cmd == OPT_BUCKET_STATS) {
5222 bucket_op.set_fetch_stats(true);
5223
5224 int r = RGWBucketAdminOp::info(store, bucket_op, f);
5225 if (r < 0) {
5226 cerr << "failure: " << cpp_strerror(-r) << ": " << err << std::endl;
5227 return -r;
5228 }
5229 }
5230
5231 if (opt_cmd == OPT_BUCKET_LINK) {
5232 bucket_op.set_bucket_id(bucket_id);
5233 string err;
5234 int r = RGWBucketAdminOp::link(store, bucket_op, &err);
5235 if (r < 0) {
5236 cerr << "failure: " << cpp_strerror(-r) << ": " << err << std::endl;
5237 return -r;
5238 }
5239 }
5240
5241 if (opt_cmd == OPT_BUCKET_UNLINK) {
5242 int r = RGWBucketAdminOp::unlink(store, bucket_op);
5243 if (r < 0) {
5244 cerr << "failure: " << cpp_strerror(-r) << std::endl;
5245 return -r;
5246 }
5247 }
5248
5249 if (opt_cmd == OPT_LOG_LIST) {
5250 // filter by date?
5251 if (date.size() && date.size() != 10) {
5252 cerr << "bad date format for '" << date << "', expect YYYY-MM-DD" << std::endl;
5253 return EINVAL;
5254 }
5255
5256 formatter->reset();
5257 formatter->open_array_section("logs");
5258 RGWAccessHandle h;
5259 int r = store->log_list_init(date, &h);
5260 if (r == -ENOENT) {
5261 // no logs.
5262 } else {
5263 if (r < 0) {
5264 cerr << "log list: error " << r << std::endl;
5265 return -r;
5266 }
5267 while (true) {
5268 string name;
5269 int r = store->log_list_next(h, &name);
5270 if (r == -ENOENT)
5271 break;
5272 if (r < 0) {
5273 cerr << "log list: error " << r << std::endl;
5274 return -r;
5275 }
5276 formatter->dump_string("object", name);
5277 }
5278 }
5279 formatter->close_section();
5280 formatter->flush(cout);
5281 cout << std::endl;
5282 }
5283
5284 if (opt_cmd == OPT_LOG_SHOW || opt_cmd == OPT_LOG_RM) {
5285 if (object.empty() && (date.empty() || bucket_name.empty() || bucket_id.empty())) {
5286 cerr << "specify an object or a date, bucket and bucket-id" << std::endl;
5287 usage();
5288 assert(false);
5289 }
5290
5291 string oid;
5292 if (!object.empty()) {
5293 oid = object;
5294 } else {
5295 oid = date;
5296 oid += "-";
5297 oid += bucket_id;
5298 oid += "-";
5299 oid += bucket_name;
5300 }
5301
5302 if (opt_cmd == OPT_LOG_SHOW) {
5303 RGWAccessHandle h;
5304
5305 int r = store->log_show_init(oid, &h);
5306 if (r < 0) {
5307 cerr << "error opening log " << oid << ": " << cpp_strerror(-r) << std::endl;
5308 return -r;
5309 }
5310
5311 formatter->reset();
5312 formatter->open_object_section("log");
5313
5314 struct rgw_log_entry entry;
5315
5316 // peek at first entry to get bucket metadata
5317 r = store->log_show_next(h, &entry);
5318 if (r < 0) {
5319 cerr << "error reading log " << oid << ": " << cpp_strerror(-r) << std::endl;
5320 return -r;
5321 }
5322 formatter->dump_string("bucket_id", entry.bucket_id);
5323 formatter->dump_string("bucket_owner", entry.bucket_owner.to_str());
5324 formatter->dump_string("bucket", entry.bucket);
5325
5326 uint64_t agg_time = 0;
5327 uint64_t agg_bytes_sent = 0;
5328 uint64_t agg_bytes_received = 0;
5329 uint64_t total_entries = 0;
5330
5331 if (show_log_entries)
5332 formatter->open_array_section("log_entries");
5333
5334 do {
5335 uint64_t total_time = entry.total_time.sec() * 1000000LL + entry.total_time.usec();
5336
5337 agg_time += total_time;
5338 agg_bytes_sent += entry.bytes_sent;
5339 agg_bytes_received += entry.bytes_received;
5340 total_entries++;
5341
5342 if (skip_zero_entries && entry.bytes_sent == 0 &&
5343 entry.bytes_received == 0)
5344 goto next;
5345
5346 if (show_log_entries) {
5347
5348 rgw_format_ops_log_entry(entry, formatter);
5349 formatter->flush(cout);
5350 }
5351 next:
5352 r = store->log_show_next(h, &entry);
5353 } while (r > 0);
5354
5355 if (r < 0) {
5356 cerr << "error reading log " << oid << ": " << cpp_strerror(-r) << std::endl;
5357 return -r;
5358 }
5359 if (show_log_entries)
5360 formatter->close_section();
5361
5362 if (show_log_sum) {
5363 formatter->open_object_section("log_sum");
5364 formatter->dump_int("bytes_sent", agg_bytes_sent);
5365 formatter->dump_int("bytes_received", agg_bytes_received);
5366 formatter->dump_int("total_time", agg_time);
5367 formatter->dump_int("total_entries", total_entries);
5368 formatter->close_section();
5369 }
5370 formatter->close_section();
5371 formatter->flush(cout);
5372 cout << std::endl;
5373 }
5374 if (opt_cmd == OPT_LOG_RM) {
5375 int r = store->log_remove(oid);
5376 if (r < 0) {
5377 cerr << "error removing log " << oid << ": " << cpp_strerror(-r) << std::endl;
5378 return -r;
5379 }
5380 }
5381 }
5382
5383 if (opt_cmd == OPT_POOL_ADD) {
5384 if (pool_name.empty()) {
5385 cerr << "need to specify pool to add!" << std::endl;
5386 usage();
5387 assert(false);
5388 }
5389
5390 int ret = store->add_bucket_placement(pool);
5391 if (ret < 0)
5392 cerr << "failed to add bucket placement: " << cpp_strerror(-ret) << std::endl;
5393 }
5394
5395 if (opt_cmd == OPT_POOL_RM) {
5396 if (pool_name.empty()) {
5397 cerr << "need to specify pool to remove!" << std::endl;
5398 usage();
5399 assert(false);
5400 }
5401
5402 int ret = store->remove_bucket_placement(pool);
5403 if (ret < 0)
5404 cerr << "failed to remove bucket placement: " << cpp_strerror(-ret) << std::endl;
5405 }
5406
5407 if (opt_cmd == OPT_POOLS_LIST) {
5408 set<rgw_pool> pools;
5409 int ret = store->list_placement_set(pools);
5410 if (ret < 0) {
5411 cerr << "could not list placement set: " << cpp_strerror(-ret) << std::endl;
5412 return -ret;
5413 }
5414 formatter->reset();
5415 formatter->open_array_section("pools");
5416 for (auto siter = pools.begin(); siter != pools.end(); ++siter) {
5417 formatter->open_object_section("pool");
5418 formatter->dump_string("name", siter->to_str());
5419 formatter->close_section();
5420 }
5421 formatter->close_section();
5422 formatter->flush(cout);
5423 cout << std::endl;
5424 }
5425
5426 if (opt_cmd == OPT_USAGE_SHOW) {
5427 uint64_t start_epoch = 0;
5428 uint64_t end_epoch = (uint64_t)-1;
5429
5430 int ret;
5431
5432 if (!start_date.empty()) {
5433 ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5434 if (ret < 0) {
5435 cerr << "ERROR: failed to parse start date" << std::endl;
5436 return 1;
5437 }
5438 }
5439 if (!end_date.empty()) {
5440 ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5441 if (ret < 0) {
5442 cerr << "ERROR: failed to parse end date" << std::endl;
5443 return 1;
5444 }
5445 }
5446
5447
5448 ret = RGWUsage::show(store, user_id, start_epoch, end_epoch,
5449 show_log_entries, show_log_sum, &categories,
5450 f);
5451 if (ret < 0) {
5452 cerr << "ERROR: failed to show usage" << std::endl;
5453 return 1;
5454 }
5455 }
5456
5457 if (opt_cmd == OPT_USAGE_TRIM) {
5458 if (user_id.empty() && !yes_i_really_mean_it) {
5459 cerr << "usage trim without user specified will remove *all* users data" << std::endl;
5460 cerr << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
5461 return 1;
5462 }
5463 int ret;
5464 uint64_t start_epoch = 0;
5465 uint64_t end_epoch = (uint64_t)-1;
5466
5467
5468 if (!start_date.empty()) {
5469 ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5470 if (ret < 0) {
5471 cerr << "ERROR: failed to parse start date" << std::endl;
5472 return 1;
5473 }
5474 }
5475
5476 if (!end_date.empty()) {
5477 ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5478 if (ret < 0) {
5479 cerr << "ERROR: failed to parse end date" << std::endl;
5480 return 1;
5481 }
5482 }
5483
5484 ret = RGWUsage::trim(store, user_id, start_epoch, end_epoch);
5485 if (ret < 0) {
5486 cerr << "ERROR: read_usage() returned ret=" << ret << std::endl;
5487 return 1;
5488 }
5489 }
5490
5491 if (opt_cmd == OPT_OLH_GET || opt_cmd == OPT_OLH_READLOG) {
5492 if (bucket_name.empty()) {
5493 cerr << "ERROR: bucket not specified" << std::endl;
5494 return EINVAL;
5495 }
5496 if (object.empty()) {
5497 cerr << "ERROR: object not specified" << std::endl;
5498 return EINVAL;
5499 }
5500 }
5501
5502 if (opt_cmd == OPT_OLH_GET) {
5503 RGWBucketInfo bucket_info;
5504 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5505 if (ret < 0) {
5506 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5507 return -ret;
5508 }
5509 RGWOLHInfo olh;
5510 rgw_obj obj(bucket, object);
5511 ret = store->get_olh(bucket_info, obj, &olh);
5512 if (ret < 0) {
5513 cerr << "ERROR: failed reading olh: " << cpp_strerror(-ret) << std::endl;
5514 return -ret;
5515 }
5516 encode_json("olh", olh, formatter);
5517 formatter->flush(cout);
5518 }
5519
5520 if (opt_cmd == OPT_OLH_READLOG) {
5521 RGWBucketInfo bucket_info;
5522 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5523 if (ret < 0) {
5524 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5525 return -ret;
5526 }
5527 map<uint64_t, vector<rgw_bucket_olh_log_entry> > log;
5528 bool is_truncated;
5529
5530 RGWObjectCtx rctx(store);
5531 rgw_obj obj(bucket, object);
5532
5533 RGWObjState *state;
5534
5535 ret = store->get_obj_state(&rctx, bucket_info, obj, &state, false); /* don't follow olh */
5536 if (ret < 0) {
5537 return -ret;
5538 }
5539
5540 ret = store->bucket_index_read_olh_log(bucket_info, *state, obj, 0, &log, &is_truncated);
5541 if (ret < 0) {
5542 cerr << "ERROR: failed reading olh: " << cpp_strerror(-ret) << std::endl;
5543 return -ret;
5544 }
5545 formatter->open_object_section("result");
5546 encode_json("is_truncated", is_truncated, formatter);
5547 encode_json("log", log, formatter);
5548 formatter->close_section();
5549 formatter->flush(cout);
5550 }
5551
5552 if (opt_cmd == OPT_BI_GET) {
5553 RGWBucketInfo bucket_info;
5554 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5555 if (ret < 0) {
5556 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5557 return -ret;
5558 }
5559 rgw_obj obj(bucket, object);
5560 if (!object_version.empty()) {
5561 obj.key.set_instance(object_version);
5562 }
5563
5564 rgw_cls_bi_entry entry;
5565
5566 ret = store->bi_get(bucket, obj, bi_index_type, &entry);
5567 if (ret < 0) {
5568 cerr << "ERROR: bi_get(): " << cpp_strerror(-ret) << std::endl;
5569 return -ret;
5570 }
5571
5572 encode_json("entry", entry, formatter);
5573 formatter->flush(cout);
5574 }
5575
5576 if (opt_cmd == OPT_BI_PUT) {
5577 RGWBucketInfo bucket_info;
5578 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5579 if (ret < 0) {
5580 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5581 return -ret;
5582 }
5583
5584 rgw_cls_bi_entry entry;
5585 cls_rgw_obj_key key;
5586 ret = read_decode_json(infile, entry, &key);
5587 if (ret < 0) {
5588 return 1;
5589 }
5590
5591 rgw_obj obj(bucket, key);
5592
5593 ret = store->bi_put(bucket, obj, entry);
5594 if (ret < 0) {
5595 cerr << "ERROR: bi_put(): " << cpp_strerror(-ret) << std::endl;
5596 return -ret;
5597 }
5598 }
5599
5600 if (opt_cmd == OPT_BI_LIST) {
5601 if (bucket_name.empty()) {
5602 cerr << "ERROR: bucket name not specified" << std::endl;
5603 return EINVAL;
5604 }
5605 RGWBucketInfo bucket_info;
5606 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5607 if (ret < 0) {
5608 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5609 return -ret;
5610 }
5611
5612 list<rgw_cls_bi_entry> entries;
5613 bool is_truncated;
5614 if (max_entries < 0) {
5615 max_entries = 1000;
5616 }
5617
5618 int max_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5619
5620 formatter->open_array_section("entries");
5621
5622 for (int i = 0; i < max_shards; i++) {
5623 RGWRados::BucketShard bs(store);
5624 int shard_id = (bucket_info.num_shards > 0 ? i : -1);
5625 int ret = bs.init(bucket, shard_id);
5626 marker.clear();
5627
5628 if (ret < 0) {
5629 cerr << "ERROR: bs.init(bucket=" << bucket << ", shard=" << shard_id << "): " << cpp_strerror(-ret) << std::endl;
5630 return -ret;
5631 }
5632
5633 do {
5634 entries.clear();
5635 ret = store->bi_list(bs, object, marker, max_entries, &entries, &is_truncated);
5636 if (ret < 0) {
5637 cerr << "ERROR: bi_list(): " << cpp_strerror(-ret) << std::endl;
5638 return -ret;
5639 }
5640
5641 list<rgw_cls_bi_entry>::iterator iter;
5642 for (iter = entries.begin(); iter != entries.end(); ++iter) {
5643 rgw_cls_bi_entry& entry = *iter;
5644 encode_json("entry", entry, formatter);
5645 marker = entry.idx;
5646 }
5647 formatter->flush(cout);
5648 } while (is_truncated);
5649 formatter->flush(cout);
5650 }
5651 formatter->close_section();
5652 formatter->flush(cout);
5653 }
5654
5655 if (opt_cmd == OPT_BI_PURGE) {
5656 if (bucket_name.empty()) {
5657 cerr << "ERROR: bucket name not specified" << std::endl;
5658 return EINVAL;
5659 }
5660 RGWBucketInfo bucket_info;
5661 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5662 if (ret < 0) {
5663 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5664 return -ret;
5665 }
5666
5667 RGWBucketInfo cur_bucket_info;
5668 rgw_bucket cur_bucket;
5669 ret = init_bucket(tenant, bucket_name, string(), cur_bucket_info, cur_bucket);
5670 if (ret < 0) {
5671 cerr << "ERROR: could not init current bucket info for bucket_name=" << bucket_name << ": " << cpp_strerror(-ret) << std::endl;
5672 return -ret;
5673 }
5674
5675 if (cur_bucket_info.bucket.bucket_id == bucket_info.bucket.bucket_id && !yes_i_really_mean_it) {
5676 cerr << "specified bucket instance points to a current bucket instance" << std::endl;
5677 cerr << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
5678 return EINVAL;
5679 }
5680
5681 int max_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5682
5683 for (int i = 0; i < max_shards; i++) {
5684 RGWRados::BucketShard bs(store);
5685 int shard_id = (bucket_info.num_shards > 0 ? i : -1);
5686 int ret = bs.init(bucket, shard_id);
5687 if (ret < 0) {
5688 cerr << "ERROR: bs.init(bucket=" << bucket << ", shard=" << shard_id << "): " << cpp_strerror(-ret) << std::endl;
5689 return -ret;
5690 }
5691
5692 ret = store->bi_remove(bs);
5693 if (ret < 0) {
5694 cerr << "ERROR: failed to remove bucket index object: " << cpp_strerror(-ret) << std::endl;
5695 return -ret;
5696 }
5697 }
5698 }
5699
5700 if (opt_cmd == OPT_OBJECT_RM) {
5701 RGWBucketInfo bucket_info;
5702 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5703 if (ret < 0) {
5704 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5705 return -ret;
5706 }
5707 rgw_obj_key key(object, object_version);
5708 ret = rgw_remove_object(store, bucket_info, bucket, key);
5709
5710 if (ret < 0) {
5711 cerr << "ERROR: object remove returned: " << cpp_strerror(-ret) << std::endl;
5712 return -ret;
5713 }
5714 }
5715
5716 if (opt_cmd == OPT_OBJECT_REWRITE) {
5717 if (bucket_name.empty()) {
5718 cerr << "ERROR: bucket not specified" << std::endl;
5719 return EINVAL;
5720 }
5721 if (object.empty()) {
5722 cerr << "ERROR: object not specified" << std::endl;
5723 return EINVAL;
5724 }
5725
5726 RGWBucketInfo bucket_info;
5727 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5728 if (ret < 0) {
5729 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5730 return -ret;
5731 }
5732
5733 rgw_obj obj(bucket, object);
5734 obj.key.set_instance(object_version);
5735 bool need_rewrite = true;
5736 if (min_rewrite_stripe_size > 0) {
5737 ret = check_min_obj_stripe_size(store, bucket_info, obj, min_rewrite_stripe_size, &need_rewrite);
5738 if (ret < 0) {
5739 ldout(store->ctx(), 0) << "WARNING: check_min_obj_stripe_size failed, r=" << ret << dendl;
5740 }
5741 }
5742 if (need_rewrite) {
5743 ret = store->rewrite_obj(bucket_info, obj);
5744 if (ret < 0) {
5745 cerr << "ERROR: object rewrite returned: " << cpp_strerror(-ret) << std::endl;
5746 return -ret;
5747 }
5748 } else {
5749 ldout(store->ctx(), 20) << "skipped object" << dendl;
5750 }
5751 }
5752
5753 if (opt_cmd == OPT_OBJECTS_EXPIRE) {
5754 if (!store->process_expire_objects()) {
5755 cerr << "ERROR: process_expire_objects() processing returned error." << std::endl;
5756 return 1;
5757 }
5758 }
5759
5760 if (opt_cmd == OPT_BUCKET_REWRITE) {
5761 if (bucket_name.empty()) {
5762 cerr << "ERROR: bucket not specified" << std::endl;
5763 return EINVAL;
5764 }
5765
5766 RGWBucketInfo bucket_info;
5767 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5768 if (ret < 0) {
5769 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5770 return -ret;
5771 }
5772
5773 uint64_t start_epoch = 0;
5774 uint64_t end_epoch = 0;
5775
5776 if (!end_date.empty()) {
5777 int ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5778 if (ret < 0) {
5779 cerr << "ERROR: failed to parse end date" << std::endl;
5780 return EINVAL;
5781 }
5782 }
5783 if (!start_date.empty()) {
5784 int ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5785 if (ret < 0) {
5786 cerr << "ERROR: failed to parse start date" << std::endl;
5787 return EINVAL;
5788 }
5789 }
5790
5791 bool is_truncated = true;
5792
5793 rgw_obj_index_key marker;
5794 string prefix;
5795
5796 formatter->open_object_section("result");
5797 formatter->dump_string("bucket", bucket_name);
5798 formatter->open_array_section("objects");
5799 while (is_truncated) {
5800 map<string, rgw_bucket_dir_entry> result;
5801 int r =
5802 store->cls_bucket_list_ordered(bucket_info, RGW_NO_SHARD, marker,
5803 prefix, 1000, true,
5804 result, &is_truncated, &marker,
5805 bucket_object_check_filter);
5806
5807 if (r < 0 && r != -ENOENT) {
5808 cerr << "ERROR: failed operation r=" << r << std::endl;
5809 }
5810
5811 if (r == -ENOENT)
5812 break;
5813
5814 map<string, rgw_bucket_dir_entry>::iterator iter;
5815 for (iter = result.begin(); iter != result.end(); ++iter) {
5816 rgw_obj_key key = iter->second.key;
5817 rgw_bucket_dir_entry& entry = iter->second;
5818
5819 formatter->open_object_section("object");
5820 formatter->dump_string("name", key.name);
5821 formatter->dump_string("instance", key.instance);
5822 formatter->dump_int("size", entry.meta.size);
5823 utime_t ut(entry.meta.mtime);
5824 ut.gmtime(formatter->dump_stream("mtime"));
5825
5826 if ((entry.meta.size < min_rewrite_size) ||
5827 (entry.meta.size > max_rewrite_size) ||
5828 (start_epoch > 0 && start_epoch > (uint64_t)ut.sec()) ||
5829 (end_epoch > 0 && end_epoch < (uint64_t)ut.sec())) {
5830 formatter->dump_string("status", "Skipped");
5831 } else {
5832 rgw_obj obj(bucket, key);
5833
5834 bool need_rewrite = true;
5835 if (min_rewrite_stripe_size > 0) {
5836 r = check_min_obj_stripe_size(store, bucket_info, obj, min_rewrite_stripe_size, &need_rewrite);
5837 if (r < 0) {
5838 ldout(store->ctx(), 0) << "WARNING: check_min_obj_stripe_size failed, r=" << r << dendl;
5839 }
5840 }
5841 if (!need_rewrite) {
5842 formatter->dump_string("status", "Skipped");
5843 } else {
5844 r = store->rewrite_obj(bucket_info, obj);
5845 if (r == 0) {
5846 formatter->dump_string("status", "Success");
5847 } else {
5848 formatter->dump_string("status", cpp_strerror(-r));
5849 }
5850 }
5851 }
5852 formatter->dump_int("flags", entry.flags);
5853
5854 formatter->close_section();
5855 formatter->flush(cout);
5856 }
5857 }
5858 formatter->close_section();
5859 formatter->close_section();
5860 formatter->flush(cout);
5861 }
5862
5863 if (opt_cmd == OPT_BUCKET_RESHARD) {
5864 rgw_bucket bucket;
5865 RGWBucketInfo bucket_info;
5866 map<string, bufferlist> attrs;
5867
5868 int ret = check_reshard_bucket_params(store,
5869 bucket_name,
5870 tenant,
5871 bucket_id,
5872 num_shards_specified,
5873 num_shards,
5874 yes_i_really_mean_it,
5875 bucket,
5876 bucket_info,
5877 attrs);
5878 if (ret < 0) {
5879 return ret;
5880 }
5881
5882 RGWBucketReshard br(store, bucket_info, attrs);
5883
5884 #define DEFAULT_RESHARD_MAX_ENTRIES 1000
5885 if (max_entries < 1) {
5886 max_entries = DEFAULT_RESHARD_MAX_ENTRIES;
5887 }
5888
5889 return br.execute(num_shards, max_entries,
5890 verbose, &cout, formatter);
5891 }
5892
5893 if (opt_cmd == OPT_RESHARD_ADD) {
5894 rgw_bucket bucket;
5895 RGWBucketInfo bucket_info;
5896 map<string, bufferlist> attrs;
5897
5898 int ret = check_reshard_bucket_params(store,
5899 bucket_name,
5900 tenant,
5901 bucket_id,
5902 num_shards_specified,
5903 num_shards,
5904 yes_i_really_mean_it,
5905 bucket,
5906 bucket_info,
5907 attrs);
5908 if (ret < 0) {
5909 return ret;
5910 }
5911
5912 int num_source_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5913
5914 RGWReshard reshard(store);
5915 cls_rgw_reshard_entry entry;
5916 entry.time = real_clock::now();
5917 entry.tenant = tenant;
5918 entry.bucket_name = bucket_name;
5919 entry.bucket_id = bucket_info.bucket.bucket_id;
5920 entry.old_num_shards = num_source_shards;
5921 entry.new_num_shards = num_shards;
5922
5923 return reshard.add(entry);
5924 }
5925
5926 if (opt_cmd == OPT_RESHARD_LIST) {
5927 list<cls_rgw_reshard_entry> entries;
5928 int ret;
5929 int count = 0;
5930 if (max_entries < 0) {
5931 max_entries = 1000;
5932 }
5933
5934 int num_logshards = store->ctx()->_conf->rgw_reshard_num_logs;
5935
5936 RGWReshard reshard(store);
5937
5938 formatter->open_array_section("reshard");
5939 for (int i = 0; i < num_logshards; i++) {
5940 bool is_truncated = true;
5941 string marker;
5942 do {
5943 entries.clear();
5944 ret = reshard.list(i, marker, max_entries, entries, &is_truncated);
5945 if (ret < 0) {
5946 cerr << "Error listing resharding buckets: " << cpp_strerror(-ret) << std::endl;
5947 return ret;
5948 }
5949 for (auto iter=entries.begin(); iter != entries.end(); ++iter) {
5950 cls_rgw_reshard_entry& entry = *iter;
5951 encode_json("entry", entry, formatter);
5952 entry.get_key(&marker);
5953 }
5954 count += entries.size();
5955 formatter->flush(cout);
5956 } while (is_truncated && count < max_entries);
5957
5958 if (count >= max_entries) {
5959 break;
5960 }
5961 }
5962
5963 formatter->close_section();
5964 formatter->flush(cout);
5965 return 0;
5966 }
5967
5968
5969 if (opt_cmd == OPT_RESHARD_STATUS) {
5970 if (bucket_name.empty()) {
5971 cerr << "ERROR: bucket not specified" << std::endl;
5972 return EINVAL;
5973 }
5974
5975 rgw_bucket bucket;
5976 RGWBucketInfo bucket_info;
5977 map<string, bufferlist> attrs;
5978 ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket, &attrs);
5979 if (ret < 0) {
5980 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5981 return -ret;
5982 }
5983
5984 RGWBucketReshard br(store, bucket_info, attrs);
5985 list<cls_rgw_bucket_instance_entry> status;
5986 int r = br.get_status(&status);
5987 if (r < 0) {
5988 cerr << "ERROR: could not get resharding status for bucket " << bucket_name << std::endl;
5989 return -r;
5990 }
5991
5992 encode_json("status", status, formatter);
5993 formatter->flush(cout);
5994 }
5995
5996 if (opt_cmd == OPT_RESHARD_PROCESS) {
5997 RGWReshard reshard(store, true, &cout);
5998
5999 int ret = reshard.process_all_logshards();
6000 if (ret < 0) {
6001 cerr << "ERROR: failed to process reshard logs, error=" << cpp_strerror(-ret) << std::endl;
6002 return -ret;
6003 }
6004 }
6005
6006 if (opt_cmd == OPT_RESHARD_CANCEL) {
6007 if (bucket_name.empty()) {
6008 cerr << "ERROR: bucket not specified" << std::endl;
6009 return EINVAL;
6010 }
6011
6012 rgw_bucket bucket;
6013 RGWBucketInfo bucket_info;
6014 map<string, bufferlist> attrs;
6015 ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket, &attrs);
6016 if (ret < 0) {
6017 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6018 return -ret;
6019 }
6020
6021 RGWBucketReshard br(store, bucket_info, attrs);
6022 int ret = br.cancel();
6023 if (ret < 0) {
6024 if (ret == -EBUSY) {
6025 cerr << "There is ongoing resharding, please retry after " << g_conf->rgw_reshard_bucket_lock_duration <<
6026 " seconds " << std::endl;
6027 } else {
6028 cerr << "Error canceling bucket " << bucket_name << " resharding: " << cpp_strerror(-ret) <<
6029 std::endl;
6030 }
6031 return ret;
6032 }
6033 RGWReshard reshard(store);
6034
6035 cls_rgw_reshard_entry entry;
6036 //entry.tenant = tenant;
6037 entry.bucket_name = bucket_name;
6038 //entry.bucket_id = bucket_id;
6039
6040 ret = reshard.remove(entry);
6041 if (ret < 0 && ret != -ENOENT) {
6042 cerr << "Error in getting bucket " << bucket_name << ": " << cpp_strerror(-ret) << std::endl;
6043 return ret;
6044 }
6045 }
6046
6047 if (opt_cmd == OPT_OBJECT_UNLINK) {
6048 RGWBucketInfo bucket_info;
6049 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6050 if (ret < 0) {
6051 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6052 return -ret;
6053 }
6054 list<rgw_obj_index_key> oid_list;
6055 rgw_obj_key key(object, object_version);
6056 rgw_obj_index_key index_key;
6057 key.get_index_key(&index_key);
6058 oid_list.push_back(index_key);
6059 ret = store->remove_objs_from_index(bucket_info, oid_list);
6060 if (ret < 0) {
6061 cerr << "ERROR: remove_obj_from_index() returned error: " << cpp_strerror(-ret) << std::endl;
6062 return 1;
6063 }
6064 }
6065
6066 if (opt_cmd == OPT_OBJECT_STAT) {
6067 RGWBucketInfo bucket_info;
6068 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6069 if (ret < 0) {
6070 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6071 return -ret;
6072 }
6073 rgw_obj obj(bucket, object);
6074 obj.key.set_instance(object_version);
6075
6076 uint64_t obj_size;
6077 map<string, bufferlist> attrs;
6078 RGWObjectCtx obj_ctx(store);
6079 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
6080 RGWRados::Object::Read read_op(&op_target);
6081
6082 read_op.params.attrs = &attrs;
6083 read_op.params.obj_size = &obj_size;
6084
6085 ret = read_op.prepare();
6086 if (ret < 0) {
6087 cerr << "ERROR: failed to stat object, returned error: " << cpp_strerror(-ret) << std::endl;
6088 return 1;
6089 }
6090 formatter->open_object_section("object_metadata");
6091 formatter->dump_string("name", object);
6092 formatter->dump_unsigned("size", obj_size);
6093
6094 map<string, bufferlist>::iterator iter;
6095 map<string, bufferlist> other_attrs;
6096 for (iter = attrs.begin(); iter != attrs.end(); ++iter) {
6097 bufferlist& bl = iter->second;
6098 bool handled = false;
6099 if (iter->first == RGW_ATTR_MANIFEST) {
6100 handled = decode_dump<RGWObjManifest>("manifest", bl, formatter);
6101 } else if (iter->first == RGW_ATTR_ACL) {
6102 handled = decode_dump<RGWAccessControlPolicy>("policy", bl, formatter);
6103 } else if (iter->first == RGW_ATTR_ID_TAG) {
6104 handled = dump_string("tag", bl, formatter);
6105 } else if (iter->first == RGW_ATTR_ETAG) {
6106 handled = dump_string("etag", bl, formatter);
6107 }
6108
6109 if (!handled)
6110 other_attrs[iter->first] = bl;
6111 }
6112
6113 formatter->open_object_section("attrs");
6114 for (iter = other_attrs.begin(); iter != other_attrs.end(); ++iter) {
6115 dump_string(iter->first.c_str(), iter->second, formatter);
6116 }
6117 formatter->close_section();
6118 formatter->close_section();
6119 formatter->flush(cout);
6120 }
6121
6122 if (opt_cmd == OPT_BUCKET_CHECK) {
6123 if (check_head_obj_locator) {
6124 if (bucket_name.empty()) {
6125 cerr << "ERROR: need to specify bucket name" << std::endl;
6126 return EINVAL;
6127 }
6128 do_check_object_locator(tenant, bucket_name, fix, remove_bad, formatter);
6129 } else {
6130 RGWBucketAdminOp::check_index(store, bucket_op, f);
6131 }
6132 }
6133
6134 if (opt_cmd == OPT_BUCKET_RM) {
6135 if (inconsistent_index == false) {
6136 RGWBucketAdminOp::remove_bucket(store, bucket_op, bypass_gc, true);
6137 } else {
6138 if (!yes_i_really_mean_it) {
6139 cerr << "using --inconsistent_index can corrupt the bucket index " << std::endl
6140 << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
6141 return 1;
6142 }
6143 RGWBucketAdminOp::remove_bucket(store, bucket_op, bypass_gc, false);
6144 }
6145 }
6146
6147 if (opt_cmd == OPT_GC_LIST) {
6148 int index = 0;
6149 bool truncated;
6150 formatter->open_array_section("entries");
6151
6152 do {
6153 list<cls_rgw_gc_obj_info> result;
6154 int ret = store->list_gc_objs(&index, marker, 1000, !include_all, result, &truncated);
6155 if (ret < 0) {
6156 cerr << "ERROR: failed to list objs: " << cpp_strerror(-ret) << std::endl;
6157 return 1;
6158 }
6159
6160
6161 list<cls_rgw_gc_obj_info>::iterator iter;
6162 for (iter = result.begin(); iter != result.end(); ++iter) {
6163 cls_rgw_gc_obj_info& info = *iter;
6164 formatter->open_object_section("chain_info");
6165 formatter->dump_string("tag", info.tag);
6166 formatter->dump_stream("time") << info.time;
6167 formatter->open_array_section("objs");
6168 list<cls_rgw_obj>::iterator liter;
6169 cls_rgw_obj_chain& chain = info.chain;
6170 for (liter = chain.objs.begin(); liter != chain.objs.end(); ++liter) {
6171 cls_rgw_obj& obj = *liter;
6172 encode_json("obj", obj, formatter);
6173 }
6174 formatter->close_section(); // objs
6175 formatter->close_section(); // obj_chain
6176 formatter->flush(cout);
6177 }
6178 } while (truncated);
6179 formatter->close_section();
6180 formatter->flush(cout);
6181 }
6182
6183 if (opt_cmd == OPT_GC_PROCESS) {
6184 int ret = store->process_gc();
6185 if (ret < 0) {
6186 cerr << "ERROR: gc processing returned error: " << cpp_strerror(-ret) << std::endl;
6187 return 1;
6188 }
6189 }
6190
6191 if (opt_cmd == OPT_LC_LIST) {
6192 formatter->open_array_section("lifecycle_list");
6193 map<string, int> bucket_lc_map;
6194 string marker;
6195 #define MAX_LC_LIST_ENTRIES 100
6196 if (max_entries < 0) {
6197 max_entries = MAX_LC_LIST_ENTRIES;
6198 }
6199 do {
6200 int ret = store->list_lc_progress(marker, max_entries, &bucket_lc_map);
6201 if (ret < 0) {
6202 cerr << "ERROR: failed to list objs: " << cpp_strerror(-ret) << std::endl;
6203 return 1;
6204 }
6205 map<string, int>::iterator iter;
6206 for (iter = bucket_lc_map.begin(); iter != bucket_lc_map.end(); ++iter) {
6207 formatter->open_object_section("bucket_lc_info");
6208 formatter->dump_string("bucket", iter->first);
6209 string lc_status = LC_STATUS[iter->second];
6210 formatter->dump_string("status", lc_status);
6211 formatter->close_section(); // objs
6212 formatter->flush(cout);
6213 marker = iter->first;
6214 }
6215 } while (!bucket_lc_map.empty());
6216
6217 formatter->close_section(); //lifecycle list
6218 formatter->flush(cout);
6219 }
6220
6221
6222 if (opt_cmd == OPT_LC_PROCESS) {
6223 int ret = store->process_lc();
6224 if (ret < 0) {
6225 cerr << "ERROR: lc processing returned error: " << cpp_strerror(-ret) << std::endl;
6226 return 1;
6227 }
6228 }
6229
6230 if (opt_cmd == OPT_ORPHANS_FIND) {
6231 if (!yes_i_really_mean_it) {
6232 cerr << "accidental removal of active objects can not be reversed; "
6233 << "do you really mean it? (requires --yes-i-really-mean-it)"
6234 << std::endl;
6235 return EINVAL;
6236 }
6237
6238 RGWOrphanSearch search(store, max_concurrent_ios, orphan_stale_secs);
6239
6240 if (job_id.empty()) {
6241 cerr << "ERROR: --job-id not specified" << std::endl;
6242 return EINVAL;
6243 }
6244 if (pool_name.empty()) {
6245 cerr << "ERROR: --pool not specified" << std::endl;
6246 return EINVAL;
6247 }
6248
6249 RGWOrphanSearchInfo info;
6250
6251 info.pool = pool;
6252 info.job_name = job_id;
6253 info.num_shards = num_shards;
6254
6255 int ret = search.init(job_id, &info);
6256 if (ret < 0) {
6257 cerr << "could not init search, ret=" << ret << std::endl;
6258 return -ret;
6259 }
6260 ret = search.run();
6261 if (ret < 0) {
6262 return -ret;
6263 }
6264 }
6265
6266 if (opt_cmd == OPT_ORPHANS_FINISH) {
6267 RGWOrphanSearch search(store, max_concurrent_ios, orphan_stale_secs);
6268
6269 if (job_id.empty()) {
6270 cerr << "ERROR: --job-id not specified" << std::endl;
6271 return EINVAL;
6272 }
6273 int ret = search.init(job_id, NULL);
6274 if (ret < 0) {
6275 if (ret == -ENOENT) {
6276 cerr << "job not found" << std::endl;
6277 }
6278 return -ret;
6279 }
6280 ret = search.finish();
6281 if (ret < 0) {
6282 return -ret;
6283 }
6284 }
6285
6286 if (opt_cmd == OPT_ORPHANS_LIST_JOBS){
6287 RGWOrphanStore orphan_store(store);
6288 int ret = orphan_store.init();
6289 if (ret < 0){
6290 cerr << "connection to cluster failed!" << std::endl;
6291 return -ret;
6292 }
6293
6294 map <string,RGWOrphanSearchState> m;
6295 ret = orphan_store.list_jobs(m);
6296 if (ret < 0) {
6297 cerr << "job list failed" << std::endl;
6298 return -ret;
6299 }
6300 formatter->open_array_section("entries");
6301 for (const auto &it: m){
6302 if (!extra_info){
6303 formatter->dump_string("job-id",it.first);
6304 } else {
6305 encode_json("orphan_search_state", it.second, formatter);
6306 }
6307 }
6308 formatter->close_section();
6309 formatter->flush(cout);
6310 }
6311
6312 if (opt_cmd == OPT_USER_CHECK) {
6313 check_bad_user_bucket_mapping(store, user_id, fix);
6314 }
6315
6316 if (opt_cmd == OPT_USER_STATS) {
6317 if (user_id.empty()) {
6318 cerr << "ERROR: uid not specified" << std::endl;
6319 return EINVAL;
6320 }
6321
6322 string user_str = user_id.to_str();
6323 if (reset_stats) {
6324 if (!bucket_name.empty()){
6325 cerr << "ERROR: recalculate doesn't work on buckets" << std::endl;
6326 return EINVAL;
6327 }
6328 ret = store->cls_user_reset_stats(user_str);
6329 if (ret < 0) {
6330 cerr << "ERROR: could not clear user stats: " << cpp_strerror(-ret) << std::endl;
6331 return -ret;
6332 }
6333 }
6334
6335 if (sync_stats) {
6336 if (!bucket_name.empty()) {
6337 int ret = rgw_bucket_sync_user_stats(store, tenant, bucket_name);
6338 if (ret < 0) {
6339 cerr << "ERROR: could not sync bucket stats: " << cpp_strerror(-ret) << std::endl;
6340 return -ret;
6341 }
6342 } else {
6343 int ret = rgw_user_sync_all_stats(store, user_id);
6344 if (ret < 0) {
6345 cerr << "ERROR: failed to sync user stats: " << cpp_strerror(-ret) << std::endl;
6346 return -ret;
6347 }
6348 }
6349 }
6350
6351 cls_user_header header;
6352 int ret = store->cls_user_get_header(user_str, &header);
6353 if (ret < 0) {
6354 if (ret == -ENOENT) { /* in case of ENOENT */
6355 cerr << "User has not been initialized or user does not exist" << std::endl;
6356 } else {
6357 cerr << "ERROR: can't read user: " << cpp_strerror(ret) << std::endl;
6358 }
6359 return -ret;
6360 }
6361
6362 encode_json("header", header, formatter);
6363 formatter->flush(cout);
6364 }
6365
6366 if (opt_cmd == OPT_METADATA_GET) {
6367 int ret = store->meta_mgr->get(metadata_key, formatter);
6368 if (ret < 0) {
6369 cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl;
6370 return -ret;
6371 }
6372
6373 formatter->flush(cout);
6374 }
6375
6376 if (opt_cmd == OPT_METADATA_PUT) {
6377 bufferlist bl;
6378 int ret = read_input(infile, bl);
6379 if (ret < 0) {
6380 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
6381 return -ret;
6382 }
6383 ret = store->meta_mgr->put(metadata_key, bl, RGWMetadataHandler::APPLY_ALWAYS);
6384 if (ret < 0) {
6385 cerr << "ERROR: can't put key: " << cpp_strerror(-ret) << std::endl;
6386 return -ret;
6387 }
6388 }
6389
6390 if (opt_cmd == OPT_METADATA_RM) {
6391 int ret = store->meta_mgr->remove(metadata_key);
6392 if (ret < 0) {
6393 cerr << "ERROR: can't remove key: " << cpp_strerror(-ret) << std::endl;
6394 return -ret;
6395 }
6396 }
6397
6398 if (opt_cmd == OPT_METADATA_LIST || opt_cmd == OPT_USER_LIST) {
6399 if (opt_cmd == OPT_USER_LIST) {
6400 metadata_key = "user";
6401 }
6402 void *handle;
6403 int max = 1000;
6404 int ret = store->meta_mgr->list_keys_init(metadata_key, marker, &handle);
6405 if (ret < 0) {
6406 cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl;
6407 return -ret;
6408 }
6409
6410 bool truncated;
6411 uint64_t count = 0;
6412
6413 if (max_entries_specified) {
6414 formatter->open_object_section("result");
6415 }
6416 formatter->open_array_section("keys");
6417
6418 uint64_t left;
6419 do {
6420 list<string> keys;
6421 left = (max_entries_specified ? max_entries - count : max);
6422 ret = store->meta_mgr->list_keys_next(handle, left, keys, &truncated);
6423 if (ret < 0 && ret != -ENOENT) {
6424 cerr << "ERROR: lists_keys_next(): " << cpp_strerror(-ret) << std::endl;
6425 return -ret;
6426 } if (ret != -ENOENT) {
6427 for (list<string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
6428 formatter->dump_string("key", *iter);
6429 ++count;
6430 }
6431 formatter->flush(cout);
6432 }
6433 } while (truncated && left > 0);
6434
6435 formatter->close_section();
6436
6437 if (max_entries_specified) {
6438 encode_json("truncated", truncated, formatter);
6439 encode_json("count", count, formatter);
6440 if (truncated) {
6441 encode_json("marker", store->meta_mgr->get_marker(handle), formatter);
6442 }
6443 formatter->close_section();
6444 }
6445 formatter->flush(cout);
6446
6447 store->meta_mgr->list_keys_complete(handle);
6448 }
6449
6450 if (opt_cmd == OPT_MDLOG_LIST) {
6451 utime_t start_time, end_time;
6452
6453 int ret = parse_date_str(start_date, start_time);
6454 if (ret < 0)
6455 return -ret;
6456
6457 ret = parse_date_str(end_date, end_time);
6458 if (ret < 0)
6459 return -ret;
6460
6461 int i = (specified_shard_id ? shard_id : 0);
6462
6463 if (period_id.empty()) {
6464 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
6465 if (ret < 0) {
6466 return -ret;
6467 }
6468 std::cerr << "No --period given, using current period="
6469 << period_id << std::endl;
6470 }
6471 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6472
6473 formatter->open_array_section("entries");
6474 for (; i < g_ceph_context->_conf->rgw_md_log_max_shards; i++) {
6475 void *handle;
6476 list<cls_log_entry> entries;
6477
6478
6479 meta_log->init_list_entries(i, start_time.to_real_time(), end_time.to_real_time(), marker, &handle);
6480 bool truncated;
6481 do {
6482 int ret = meta_log->list_entries(handle, 1000, entries, NULL, &truncated);
6483 if (ret < 0) {
6484 cerr << "ERROR: meta_log->list_entries(): " << cpp_strerror(-ret) << std::endl;
6485 return -ret;
6486 }
6487
6488 for (list<cls_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6489 cls_log_entry& entry = *iter;
6490 store->meta_mgr->dump_log_entry(entry, formatter);
6491 }
6492 formatter->flush(cout);
6493 } while (truncated);
6494
6495 meta_log->complete_list_entries(handle);
6496
6497 if (specified_shard_id)
6498 break;
6499 }
6500
6501
6502 formatter->close_section();
6503 formatter->flush(cout);
6504 }
6505
6506 if (opt_cmd == OPT_MDLOG_STATUS) {
6507 int i = (specified_shard_id ? shard_id : 0);
6508
6509 if (period_id.empty()) {
6510 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
6511 if (ret < 0) {
6512 return -ret;
6513 }
6514 std::cerr << "No --period given, using current period="
6515 << period_id << std::endl;
6516 }
6517 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6518
6519 formatter->open_array_section("entries");
6520
6521 for (; i < g_ceph_context->_conf->rgw_md_log_max_shards; i++) {
6522 RGWMetadataLogInfo info;
6523 meta_log->get_info(i, &info);
6524
6525 ::encode_json("info", info, formatter);
6526
6527 if (specified_shard_id)
6528 break;
6529 }
6530
6531
6532 formatter->close_section();
6533 formatter->flush(cout);
6534 }
6535
6536 if (opt_cmd == OPT_MDLOG_AUTOTRIM) {
6537 // need a full history for purging old mdlog periods
6538 store->meta_mgr->init_oldest_log_period();
6539
6540 RGWCoroutinesManager crs(store->ctx(), store->get_cr_registry());
6541 RGWHTTPManager http(store->ctx(), crs.get_completion_mgr());
6542 int ret = http.set_threaded();
6543 if (ret < 0) {
6544 cerr << "failed to initialize http client with " << cpp_strerror(ret) << std::endl;
6545 return -ret;
6546 }
6547
6548 auto num_shards = g_conf->rgw_md_log_max_shards;
6549 ret = crs.run(create_admin_meta_log_trim_cr(store, &http, num_shards));
6550 if (ret < 0) {
6551 cerr << "automated mdlog trim failed with " << cpp_strerror(ret) << std::endl;
6552 return -ret;
6553 }
6554 }
6555
6556 if (opt_cmd == OPT_MDLOG_TRIM) {
6557 utime_t start_time, end_time;
6558
6559 if (!specified_shard_id) {
6560 cerr << "ERROR: shard-id must be specified for trim operation" << std::endl;
6561 return EINVAL;
6562 }
6563
6564 int ret = parse_date_str(start_date, start_time);
6565 if (ret < 0)
6566 return -ret;
6567
6568 ret = parse_date_str(end_date, end_time);
6569 if (ret < 0)
6570 return -ret;
6571
6572 if (period_id.empty()) {
6573 std::cerr << "missing --period argument" << std::endl;
6574 return EINVAL;
6575 }
6576 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6577
6578 ret = meta_log->trim(shard_id, start_time.to_real_time(), end_time.to_real_time(), start_marker, end_marker);
6579 if (ret < 0) {
6580 cerr << "ERROR: meta_log->trim(): " << cpp_strerror(-ret) << std::endl;
6581 return -ret;
6582 }
6583 }
6584
6585 if (opt_cmd == OPT_SYNC_STATUS) {
6586 sync_status(formatter);
6587 }
6588
6589 if (opt_cmd == OPT_METADATA_SYNC_STATUS) {
6590 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6591
6592 int ret = sync.init();
6593 if (ret < 0) {
6594 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6595 return -ret;
6596 }
6597
6598 rgw_meta_sync_status sync_status;
6599 ret = sync.read_sync_status(&sync_status);
6600 if (ret < 0) {
6601 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6602 return -ret;
6603 }
6604
6605 formatter->open_object_section("summary");
6606 encode_json("sync_status", sync_status, formatter);
6607
6608 uint64_t full_total = 0;
6609 uint64_t full_complete = 0;
6610
6611 for (auto marker_iter : sync_status.sync_markers) {
6612 full_total += marker_iter.second.total_entries;
6613 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
6614 full_complete += marker_iter.second.pos;
6615 } else {
6616 full_complete += marker_iter.second.total_entries;
6617 }
6618 }
6619
6620 formatter->open_object_section("full_sync");
6621 encode_json("total", full_total, formatter);
6622 encode_json("complete", full_complete, formatter);
6623 formatter->close_section();
6624 formatter->close_section();
6625
6626 formatter->flush(cout);
6627
6628 }
6629
6630 if (opt_cmd == OPT_METADATA_SYNC_INIT) {
6631 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6632
6633 int ret = sync.init();
6634 if (ret < 0) {
6635 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6636 return -ret;
6637 }
6638 ret = sync.init_sync_status();
6639 if (ret < 0) {
6640 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6641 return -ret;
6642 }
6643 }
6644
6645
6646 if (opt_cmd == OPT_METADATA_SYNC_RUN) {
6647 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6648
6649 int ret = sync.init();
6650 if (ret < 0) {
6651 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6652 return -ret;
6653 }
6654
6655 ret = sync.run();
6656 if (ret < 0) {
6657 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6658 return -ret;
6659 }
6660 }
6661
6662 if (opt_cmd == OPT_DATA_SYNC_STATUS) {
6663 if (source_zone.empty()) {
6664 cerr << "ERROR: source zone not specified" << std::endl;
6665 return EINVAL;
6666 }
6667 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6668
6669 int ret = sync.init();
6670 if (ret < 0) {
6671 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6672 return -ret;
6673 }
6674
6675 rgw_data_sync_status sync_status;
6676 if (specified_shard_id) {
6677 set<string> pending_buckets;
6678 set<string> recovering_buckets;
6679 rgw_data_sync_marker sync_marker;
6680 ret = sync.read_shard_status(shard_id, pending_buckets, recovering_buckets, &sync_marker,
6681 max_entries_specified ? max_entries : 20);
6682 if (ret < 0 && ret != -ENOENT) {
6683 cerr << "ERROR: sync.read_shard_status() returned ret=" << ret << std::endl;
6684 return -ret;
6685 }
6686 formatter->open_object_section("summary");
6687 encode_json("shard_id", shard_id, formatter);
6688 encode_json("marker", sync_marker, formatter);
6689 encode_json("pending_buckets", pending_buckets, formatter);
6690 encode_json("recovering_buckets", recovering_buckets, formatter);
6691 formatter->close_section();
6692 formatter->flush(cout);
6693 } else {
6694 ret = sync.read_sync_status(&sync_status);
6695 if (ret < 0 && ret != -ENOENT) {
6696 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6697 return -ret;
6698 }
6699
6700 formatter->open_object_section("summary");
6701 encode_json("sync_status", sync_status, formatter);
6702
6703 uint64_t full_total = 0;
6704 uint64_t full_complete = 0;
6705
6706 for (auto marker_iter : sync_status.sync_markers) {
6707 full_total += marker_iter.second.total_entries;
6708 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
6709 full_complete += marker_iter.second.pos;
6710 } else {
6711 full_complete += marker_iter.second.total_entries;
6712 }
6713 }
6714
6715 formatter->open_object_section("full_sync");
6716 encode_json("total", full_total, formatter);
6717 encode_json("complete", full_complete, formatter);
6718 formatter->close_section();
6719 formatter->close_section();
6720
6721 formatter->flush(cout);
6722 }
6723 }
6724
6725 if (opt_cmd == OPT_DATA_SYNC_INIT) {
6726 if (source_zone.empty()) {
6727 cerr << "ERROR: source zone not specified" << std::endl;
6728 return EINVAL;
6729 }
6730
6731 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6732
6733 int ret = sync.init();
6734 if (ret < 0) {
6735 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6736 return -ret;
6737 }
6738
6739 ret = sync.init_sync_status();
6740 if (ret < 0) {
6741 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6742 return -ret;
6743 }
6744 }
6745
6746 if (opt_cmd == OPT_DATA_SYNC_RUN) {
6747 if (source_zone.empty()) {
6748 cerr << "ERROR: source zone not specified" << std::endl;
6749 return EINVAL;
6750 }
6751
6752 RGWSyncModuleInstanceRef sync_module;
6753 int ret = store->get_sync_modules_manager()->create_instance(g_ceph_context, store->get_zone().tier_type,
6754 store->get_zone_params().tier_config, &sync_module);
6755 if (ret < 0) {
6756 lderr(cct) << "ERROR: failed to init sync module instance, ret=" << ret << dendl;
6757 return ret;
6758 }
6759
6760 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone, sync_module);
6761
6762 ret = sync.init();
6763 if (ret < 0) {
6764 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6765 return -ret;
6766 }
6767
6768 ret = sync.run();
6769 if (ret < 0) {
6770 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6771 return -ret;
6772 }
6773 }
6774
6775 if (opt_cmd == OPT_BUCKET_SYNC_INIT) {
6776 if (source_zone.empty()) {
6777 cerr << "ERROR: source zone not specified" << std::endl;
6778 return EINVAL;
6779 }
6780 if (bucket_name.empty()) {
6781 cerr << "ERROR: bucket not specified" << std::endl;
6782 return EINVAL;
6783 }
6784 rgw_bucket bucket;
6785 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6786 if (ret < 0) {
6787 return -ret;
6788 }
6789 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6790
6791 ret = sync.init();
6792 if (ret < 0) {
6793 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6794 return -ret;
6795 }
6796 ret = sync.init_sync_status();
6797 if (ret < 0) {
6798 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6799 return -ret;
6800 }
6801 }
6802
6803 if ((opt_cmd == OPT_BUCKET_SYNC_DISABLE) || (opt_cmd == OPT_BUCKET_SYNC_ENABLE)) {
6804 if (bucket_name.empty()) {
6805 cerr << "ERROR: bucket not specified" << std::endl;
6806 return EINVAL;
6807 }
6808
6809 if (ret < 0) {
6810 cerr << "could not init realm " << ": " << cpp_strerror(-ret) << std::endl;
6811 return ret;
6812 }
6813 RGWPeriod period;
6814 ret = period.init(g_ceph_context, store, realm_id, realm_name, true);
6815 if (ret < 0) {
6816 cerr << "failed to init period " << ": " << cpp_strerror(-ret) << std::endl;
6817 return ret;
6818 }
6819
6820 if (!store->is_meta_master()) {
6821 cerr << "failed to update bucket sync: only allowed on meta master zone " << std::endl;
6822 cerr << period.get_master_zone() << " | " << period.get_realm() << std::endl;
6823 return EINVAL;
6824 }
6825
6826 rgw_obj obj(bucket, object);
6827 ret = set_bucket_sync_enabled(store, opt_cmd, tenant, bucket_name);
6828 if (ret < 0)
6829 return -ret;
6830 }
6831
6832 if (opt_cmd == OPT_BUCKET_SYNC_STATUS) {
6833 if (bucket_name.empty()) {
6834 cerr << "ERROR: bucket not specified" << std::endl;
6835 return EINVAL;
6836 }
6837 RGWBucketInfo bucket_info;
6838 rgw_bucket bucket;
6839 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6840 if (ret < 0) {
6841 return -ret;
6842 }
6843 bucket_sync_status(store, bucket_info, source_zone, std::cout);
6844 }
6845
6846 if (opt_cmd == OPT_BUCKET_SYNC_MARKERS) {
6847 if (source_zone.empty()) {
6848 cerr << "ERROR: source zone not specified" << std::endl;
6849 return EINVAL;
6850 }
6851 if (bucket_name.empty()) {
6852 cerr << "ERROR: bucket not specified" << std::endl;
6853 return EINVAL;
6854 }
6855 rgw_bucket bucket;
6856 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6857 if (ret < 0) {
6858 return -ret;
6859 }
6860 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6861
6862 ret = sync.init();
6863 if (ret < 0) {
6864 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6865 return -ret;
6866 }
6867 ret = sync.read_sync_status();
6868 if (ret < 0) {
6869 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6870 return -ret;
6871 }
6872
6873 map<int, rgw_bucket_shard_sync_info>& sync_status = sync.get_sync_status();
6874
6875 encode_json("sync_status", sync_status, formatter);
6876 formatter->flush(cout);
6877 }
6878
6879 if (opt_cmd == OPT_BUCKET_SYNC_RUN) {
6880 if (source_zone.empty()) {
6881 cerr << "ERROR: source zone not specified" << std::endl;
6882 return EINVAL;
6883 }
6884 if (bucket_name.empty()) {
6885 cerr << "ERROR: bucket not specified" << std::endl;
6886 return EINVAL;
6887 }
6888 rgw_bucket bucket;
6889 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6890 if (ret < 0) {
6891 return -ret;
6892 }
6893 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6894
6895 ret = sync.init();
6896 if (ret < 0) {
6897 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6898 return -ret;
6899 }
6900
6901 ret = sync.run();
6902 if (ret < 0) {
6903 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6904 return -ret;
6905 }
6906 }
6907
6908 if (opt_cmd == OPT_BILOG_LIST) {
6909 if (bucket_name.empty()) {
6910 cerr << "ERROR: bucket not specified" << std::endl;
6911 return EINVAL;
6912 }
6913 RGWBucketInfo bucket_info;
6914 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6915 if (ret < 0) {
6916 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6917 return -ret;
6918 }
6919 formatter->open_array_section("entries");
6920 bool truncated;
6921 int count = 0;
6922 if (max_entries < 0)
6923 max_entries = 1000;
6924
6925 do {
6926 list<rgw_bi_log_entry> entries;
6927 ret = store->list_bi_log_entries(bucket_info, shard_id, marker, max_entries - count, entries, &truncated);
6928 if (ret < 0) {
6929 cerr << "ERROR: list_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
6930 return -ret;
6931 }
6932
6933 count += entries.size();
6934
6935 for (list<rgw_bi_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6936 rgw_bi_log_entry& entry = *iter;
6937 encode_json("entry", entry, formatter);
6938
6939 marker = entry.id;
6940 }
6941 formatter->flush(cout);
6942 } while (truncated && count < max_entries);
6943
6944 formatter->close_section();
6945 formatter->flush(cout);
6946 }
6947
6948 if (opt_cmd == OPT_SYNC_ERROR_LIST) {
6949 if (max_entries < 0) {
6950 max_entries = 1000;
6951 }
6952
6953 bool truncated;
6954 utime_t start_time, end_time;
6955
6956 int ret = parse_date_str(start_date, start_time);
6957 if (ret < 0)
6958 return -ret;
6959
6960 ret = parse_date_str(end_date, end_time);
6961 if (ret < 0)
6962 return -ret;
6963
6964 if (shard_id < 0) {
6965 shard_id = 0;
6966 }
6967
6968 formatter->open_array_section("entries");
6969
6970 for (; shard_id < ERROR_LOGGER_SHARDS; ++shard_id) {
6971 formatter->open_object_section("shard");
6972 encode_json("shard_id", shard_id, formatter);
6973 formatter->open_array_section("entries");
6974
6975 int count = 0;
6976 string oid = RGWSyncErrorLogger::get_shard_oid(RGW_SYNC_ERROR_LOG_SHARD_PREFIX, shard_id);
6977
6978 do {
6979 list<cls_log_entry> entries;
6980 ret = store->time_log_list(oid, start_time.to_real_time(), end_time.to_real_time(),
6981 max_entries - count, entries, marker, &marker, &truncated);
6982 if (ret == -ENOENT) {
6983 break;
6984 }
6985 if (ret < 0) {
6986 cerr << "ERROR: store->time_log_list(): " << cpp_strerror(-ret) << std::endl;
6987 return -ret;
6988 }
6989
6990 count += entries.size();
6991
6992 for (auto& cls_entry : entries) {
6993 rgw_sync_error_info log_entry;
6994
6995 auto iter = cls_entry.data.begin();
6996 try {
6997 ::decode(log_entry, iter);
6998 } catch (buffer::error& err) {
6999 cerr << "ERROR: failed to decode log entry" << std::endl;
7000 continue;
7001 }
7002 formatter->open_object_section("entry");
7003 encode_json("id", cls_entry.id, formatter);
7004 encode_json("section", cls_entry.section, formatter);
7005 encode_json("name", cls_entry.name, formatter);
7006 encode_json("timestamp", cls_entry.timestamp, formatter);
7007 encode_json("info", log_entry, formatter);
7008 formatter->close_section();
7009 formatter->flush(cout);
7010 }
7011 } while (truncated && count < max_entries);
7012
7013 formatter->close_section();
7014 formatter->close_section();
7015
7016 if (specified_shard_id) {
7017 break;
7018 }
7019 }
7020
7021 formatter->close_section();
7022 formatter->flush(cout);
7023 }
7024
7025 if (opt_cmd == OPT_SYNC_ERROR_TRIM) {
7026 utime_t start_time, end_time;
7027 int ret = parse_date_str(start_date, start_time);
7028 if (ret < 0)
7029 return -ret;
7030
7031 ret = parse_date_str(end_date, end_time);
7032 if (ret < 0)
7033 return -ret;
7034
7035 if (shard_id < 0) {
7036 shard_id = 0;
7037 }
7038
7039 for (; shard_id < ERROR_LOGGER_SHARDS; ++shard_id) {
7040 string oid = RGWSyncErrorLogger::get_shard_oid(RGW_SYNC_ERROR_LOG_SHARD_PREFIX, shard_id);
7041 ret = store->time_log_trim(oid, start_time.to_real_time(), end_time.to_real_time(), start_marker, end_marker);
7042 if (ret < 0 && ret != -ENODATA) {
7043 cerr << "ERROR: sync error trim: " << cpp_strerror(-ret) << std::endl;
7044 return -ret;
7045 }
7046 if (specified_shard_id) {
7047 break;
7048 }
7049 }
7050 }
7051
7052 if (opt_cmd == OPT_BILOG_TRIM) {
7053 if (bucket_name.empty()) {
7054 cerr << "ERROR: bucket not specified" << std::endl;
7055 return EINVAL;
7056 }
7057 RGWBucketInfo bucket_info;
7058 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
7059 if (ret < 0) {
7060 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
7061 return -ret;
7062 }
7063 ret = store->trim_bi_log_entries(bucket_info, shard_id, start_marker, end_marker);
7064 if (ret < 0) {
7065 cerr << "ERROR: trim_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
7066 return -ret;
7067 }
7068 }
7069
7070 if (opt_cmd == OPT_BILOG_STATUS) {
7071 if (bucket_name.empty()) {
7072 cerr << "ERROR: bucket not specified" << std::endl;
7073 return EINVAL;
7074 }
7075 RGWBucketInfo bucket_info;
7076 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
7077 if (ret < 0) {
7078 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
7079 return -ret;
7080 }
7081 map<int, string> markers;
7082 ret = store->get_bi_log_status(bucket_info, shard_id, markers);
7083 if (ret < 0) {
7084 cerr << "ERROR: get_bi_log_status(): " << cpp_strerror(-ret) << std::endl;
7085 return -ret;
7086 }
7087 formatter->open_object_section("entries");
7088 encode_json("markers", markers, formatter);
7089 formatter->close_section();
7090 formatter->flush(cout);
7091 }
7092
7093 if (opt_cmd == OPT_BILOG_AUTOTRIM) {
7094 RGWCoroutinesManager crs(store->ctx(), store->get_cr_registry());
7095 RGWHTTPManager http(store->ctx(), crs.get_completion_mgr());
7096 int ret = http.set_threaded();
7097 if (ret < 0) {
7098 cerr << "failed to initialize http client with " << cpp_strerror(ret) << std::endl;
7099 return -ret;
7100 }
7101
7102 rgw::BucketTrimConfig config;
7103 configure_bucket_trim(store->ctx(), config);
7104
7105 rgw::BucketTrimManager trim(store, config);
7106 ret = trim.init();
7107 if (ret < 0) {
7108 cerr << "trim manager init failed with " << cpp_strerror(ret) << std::endl;
7109 return -ret;
7110 }
7111 ret = crs.run(trim.create_admin_bucket_trim_cr(&http));
7112 if (ret < 0) {
7113 cerr << "automated bilog trim failed with " << cpp_strerror(ret) << std::endl;
7114 return -ret;
7115 }
7116 }
7117
7118 if (opt_cmd == OPT_DATALOG_LIST) {
7119 formatter->open_array_section("entries");
7120 bool truncated;
7121 int count = 0;
7122 if (max_entries < 0)
7123 max_entries = 1000;
7124
7125 utime_t start_time, end_time;
7126
7127 int ret = parse_date_str(start_date, start_time);
7128 if (ret < 0)
7129 return -ret;
7130
7131 ret = parse_date_str(end_date, end_time);
7132 if (ret < 0)
7133 return -ret;
7134
7135 RGWDataChangesLog *log = store->data_log;
7136 RGWDataChangesLog::LogMarker marker;
7137
7138 do {
7139 list<rgw_data_change_log_entry> entries;
7140 ret = log->list_entries(start_time.to_real_time(), end_time.to_real_time(), max_entries - count, entries, marker, &truncated);
7141 if (ret < 0) {
7142 cerr << "ERROR: list_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
7143 return -ret;
7144 }
7145
7146 count += entries.size();
7147
7148 for (list<rgw_data_change_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
7149 rgw_data_change_log_entry& entry = *iter;
7150 if (!extra_info) {
7151 encode_json("entry", entry.entry, formatter);
7152 } else {
7153 encode_json("entry", entry, formatter);
7154 }
7155 }
7156 formatter->flush(cout);
7157 } while (truncated && count < max_entries);
7158
7159 formatter->close_section();
7160 formatter->flush(cout);
7161 }
7162
7163 if (opt_cmd == OPT_DATALOG_STATUS) {
7164 RGWDataChangesLog *log = store->data_log;
7165 int i = (specified_shard_id ? shard_id : 0);
7166
7167 formatter->open_array_section("entries");
7168 for (; i < g_ceph_context->_conf->rgw_data_log_num_shards; i++) {
7169 list<cls_log_entry> entries;
7170
7171 RGWDataChangesLogInfo info;
7172 log->get_info(i, &info);
7173
7174 ::encode_json("info", info, formatter);
7175
7176 if (specified_shard_id)
7177 break;
7178 }
7179
7180 formatter->close_section();
7181 formatter->flush(cout);
7182 }
7183
7184 if (opt_cmd == OPT_DATALOG_TRIM) {
7185 utime_t start_time, end_time;
7186
7187 int ret = parse_date_str(start_date, start_time);
7188 if (ret < 0)
7189 return -ret;
7190
7191 ret = parse_date_str(end_date, end_time);
7192 if (ret < 0)
7193 return -ret;
7194
7195 RGWDataChangesLog *log = store->data_log;
7196 ret = log->trim_entries(start_time.to_real_time(), end_time.to_real_time(), start_marker, end_marker);
7197 if (ret < 0) {
7198 cerr << "ERROR: trim_entries(): " << cpp_strerror(-ret) << std::endl;
7199 return -ret;
7200 }
7201 }
7202
7203 if (opt_cmd == OPT_OPSTATE_LIST) {
7204 RGWOpState oc(store);
7205
7206 int max = 1000;
7207
7208 void *handle;
7209 oc.init_list_entries(client_id, op_id, object, &handle);
7210 list<cls_statelog_entry> entries;
7211 bool done;
7212 formatter->open_array_section("entries");
7213 do {
7214 int ret = oc.list_entries(handle, max, entries, &done);
7215 if (ret < 0) {
7216 cerr << "oc.list_entries returned " << cpp_strerror(-ret) << std::endl;
7217 oc.finish_list_entries(handle);
7218 return -ret;
7219 }
7220
7221 for (list<cls_statelog_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
7222 oc.dump_entry(*iter, formatter);
7223 }
7224
7225 formatter->flush(cout);
7226 } while (!done);
7227 formatter->close_section();
7228 formatter->flush(cout);
7229 oc.finish_list_entries(handle);
7230 }
7231
7232 if (opt_cmd == OPT_OPSTATE_SET || opt_cmd == OPT_OPSTATE_RENEW) {
7233 RGWOpState oc(store);
7234
7235 RGWOpState::OpState state;
7236 if (object.empty() || client_id.empty() || op_id.empty()) {
7237 cerr << "ERROR: need to specify client_id, op_id, and object" << std::endl;
7238 return EINVAL;
7239 }
7240 if (state_str.empty()) {
7241 cerr << "ERROR: state was not specified" << std::endl;
7242 return EINVAL;
7243 }
7244 int ret = oc.state_from_str(state_str, &state);
7245 if (ret < 0) {
7246 cerr << "ERROR: invalid state: " << state_str << std::endl;
7247 return -ret;
7248 }
7249
7250 if (opt_cmd == OPT_OPSTATE_SET) {
7251 ret = oc.set_state(client_id, op_id, object, state);
7252 if (ret < 0) {
7253 cerr << "ERROR: failed to set state: " << cpp_strerror(-ret) << std::endl;
7254 return -ret;
7255 }
7256 } else {
7257 ret = oc.renew_state(client_id, op_id, object, state);
7258 if (ret < 0) {
7259 cerr << "ERROR: failed to renew state: " << cpp_strerror(-ret) << std::endl;
7260 return -ret;
7261 }
7262 }
7263 }
7264 if (opt_cmd == OPT_OPSTATE_RM) {
7265 RGWOpState oc(store);
7266
7267 if (object.empty() || client_id.empty() || op_id.empty()) {
7268 cerr << "ERROR: need to specify client_id, op_id, and object" << std::endl;
7269 return EINVAL;
7270 }
7271 ret = oc.remove_entry(client_id, op_id, object);
7272 if (ret < 0) {
7273 cerr << "ERROR: failed to set state: " << cpp_strerror(-ret) << std::endl;
7274 return -ret;
7275 }
7276 }
7277
7278 if (opt_cmd == OPT_REPLICALOG_GET || opt_cmd == OPT_REPLICALOG_UPDATE ||
7279 opt_cmd == OPT_REPLICALOG_DELETE) {
7280 if (replica_log_type_str.empty()) {
7281 cerr << "ERROR: need to specify --replica-log-type=<metadata | data | bucket>" << std::endl;
7282 return EINVAL;
7283 }
7284 }
7285
7286 if (opt_cmd == OPT_REPLICALOG_GET) {
7287 RGWReplicaBounds bounds;
7288 if (replica_log_type == ReplicaLog_Metadata) {
7289 if (!specified_shard_id) {
7290 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
7291 return EINVAL;
7292 }
7293
7294 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
7295 int ret = logger.get_bounds(shard_id, bounds);
7296 if (ret < 0)
7297 return -ret;
7298 } else if (replica_log_type == ReplicaLog_Data) {
7299 if (!specified_shard_id) {
7300 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
7301 return EINVAL;
7302 }
7303 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
7304 int ret = logger.get_bounds(shard_id, bounds);
7305 if (ret < 0)
7306 return -ret;
7307 } else if (replica_log_type == ReplicaLog_Bucket) {
7308 if (bucket_name.empty()) {
7309 cerr << "ERROR: bucket not specified" << std::endl;
7310 return EINVAL;
7311 }
7312 RGWBucketInfo bucket_info;
7313 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
7314 if (ret < 0) {
7315 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
7316 return -ret;
7317 }
7318
7319 RGWReplicaBucketLogger logger(store);
7320 ret = logger.get_bounds(bucket, shard_id, bounds);
7321 if (ret < 0)
7322 return -ret;
7323 } else { // shouldn't get here
7324 ceph_abort();
7325 }
7326 encode_json("bounds", bounds, formatter);
7327 formatter->flush(cout);
7328 cout << std::endl;
7329 }
7330
7331 if (opt_cmd == OPT_REPLICALOG_DELETE) {
7332 if (replica_log_type == ReplicaLog_Metadata) {
7333 if (!specified_shard_id) {
7334 cerr << "ERROR: shard-id must be specified for delete operation" << std::endl;
7335 return EINVAL;
7336 }
7337 if (!specified_daemon_id) {
7338 cerr << "ERROR: daemon-id must be specified for delete operation" << std::endl;
7339 return EINVAL;
7340 }
7341 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
7342 int ret = logger.delete_bound(shard_id, daemon_id, false);
7343 if (ret < 0)
7344 return -ret;
7345 } else if (replica_log_type == ReplicaLog_Data) {
7346 if (!specified_shard_id) {
7347 cerr << "ERROR: shard-id must be specified for delete operation" << std::endl;
7348 return EINVAL;
7349 }
7350 if (!specified_daemon_id) {
7351 cerr << "ERROR: daemon-id must be specified for delete operation" << std::endl;
7352 return EINVAL;
7353 }
7354 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
7355 int ret = logger.delete_bound(shard_id, daemon_id, false);
7356 if (ret < 0)
7357 return -ret;
7358 } else if (replica_log_type == ReplicaLog_Bucket) {
7359 if (bucket_name.empty()) {
7360 cerr << "ERROR: bucket not specified" << std::endl;
7361 return EINVAL;
7362 }
7363 RGWBucketInfo bucket_info;
7364 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
7365 if (ret < 0) {
7366 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
7367 return -ret;
7368 }
7369
7370 RGWReplicaBucketLogger logger(store);
7371 ret = logger.delete_bound(bucket, shard_id, daemon_id, false);
7372 if (ret < 0)
7373 return -ret;
7374 }
7375 }
7376
7377 if (opt_cmd == OPT_REPLICALOG_UPDATE) {
7378 if (marker.empty()) {
7379 cerr << "ERROR: marker was not specified" <<std::endl;
7380 return EINVAL;
7381 }
7382 utime_t time = ceph_clock_now();
7383 if (!date.empty()) {
7384 ret = parse_date_str(date, time);
7385 if (ret < 0) {
7386 cerr << "ERROR: failed to parse start date" << std::endl;
7387 return EINVAL;
7388 }
7389 }
7390 list<RGWReplicaItemMarker> entries;
7391 int ret = read_decode_json(infile, entries);
7392 if (ret < 0) {
7393 cerr << "ERROR: failed to decode entries" << std::endl;
7394 return EINVAL;
7395 }
7396 RGWReplicaBounds bounds;
7397 if (replica_log_type == ReplicaLog_Metadata) {
7398 if (!specified_shard_id) {
7399 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
7400 return EINVAL;
7401 }
7402
7403 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
7404 int ret = logger.update_bound(shard_id, daemon_id, marker, time, &entries);
7405 if (ret < 0) {
7406 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
7407 return -ret;
7408 }
7409 } else if (replica_log_type == ReplicaLog_Data) {
7410 if (!specified_shard_id) {
7411 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
7412 return EINVAL;
7413 }
7414 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
7415 int ret = logger.update_bound(shard_id, daemon_id, marker, time, &entries);
7416 if (ret < 0) {
7417 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
7418 return -ret;
7419 }
7420 } else if (replica_log_type == ReplicaLog_Bucket) {
7421 if (bucket_name.empty()) {
7422 cerr << "ERROR: bucket not specified" << std::endl;
7423 return EINVAL;
7424 }
7425 RGWBucketInfo bucket_info;
7426 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
7427 if (ret < 0) {
7428 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
7429 return -ret;
7430 }
7431
7432 RGWReplicaBucketLogger logger(store);
7433 ret = logger.update_bound(bucket, shard_id, daemon_id, marker, time, &entries);
7434 if (ret < 0) {
7435 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
7436 return -ret;
7437 }
7438 }
7439 }
7440
7441 bool quota_op = (opt_cmd == OPT_QUOTA_SET || opt_cmd == OPT_QUOTA_ENABLE || opt_cmd == OPT_QUOTA_DISABLE);
7442
7443 if (quota_op) {
7444 if (bucket_name.empty() && user_id.empty()) {
7445 cerr << "ERROR: bucket name or uid is required for quota operation" << std::endl;
7446 return EINVAL;
7447 }
7448
7449 if (!bucket_name.empty()) {
7450 if (!quota_scope.empty() && quota_scope != "bucket") {
7451 cerr << "ERROR: invalid quota scope specification." << std::endl;
7452 return EINVAL;
7453 }
7454 set_bucket_quota(store, opt_cmd, tenant, bucket_name,
7455 max_size, max_objects, have_max_size, have_max_objects);
7456 } else if (!user_id.empty()) {
7457 if (quota_scope == "bucket") {
7458 set_user_bucket_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects);
7459 } else if (quota_scope == "user") {
7460 set_user_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects);
7461 } else {
7462 cerr << "ERROR: invalid quota scope specification. Please specify either --quota-scope=bucket, or --quota-scope=user" << std::endl;
7463 return EINVAL;
7464 }
7465 }
7466 }
7467
7468 return 0;
7469 }