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