]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_admin.cc
update sources to v12.1.3
[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 int admin = false;
2400 bool admin_specified = false;
2401 int system = false;
2402 bool system_specified = false;
2403 int shard_id = -1;
2404 bool specified_shard_id = false;
2405 string daemon_id;
2406 bool specified_daemon_id = false;
2407 string client_id;
2408 string op_id;
2409 string state_str;
2410 string replica_log_type_str;
2411 ReplicaLogType replica_log_type = ReplicaLog_Invalid;
2412 string op_mask_str;
2413 string quota_scope;
2414 string object_version;
2415 string placement_id;
2416 list<string> tags;
2417 list<string> tags_add;
2418 list<string> tags_rm;
2419
2420 int64_t max_objects = -1;
2421 int64_t max_size = -1;
2422 bool have_max_objects = false;
2423 bool have_max_size = false;
2424 int include_all = false;
2425
2426 int sync_stats = false;
2427 int bypass_gc = false;
2428 int warnings_only = false;
2429 int inconsistent_index = false;
2430
2431 int verbose = false;
2432
2433 int extra_info = false;
2434
2435 uint64_t min_rewrite_size = 4 * 1024 * 1024;
2436 uint64_t max_rewrite_size = ULLONG_MAX;
2437 uint64_t min_rewrite_stripe_size = 0;
2438
2439 BIIndexType bi_index_type = PlainIdx;
2440
2441 string job_id;
2442 int num_shards = 0;
2443 bool num_shards_specified = false;
2444 int max_concurrent_ios = 32;
2445 uint64_t orphan_stale_secs = (24 * 3600);
2446
2447 std::string val;
2448 std::ostringstream errs;
2449 string err;
2450 long long tmp = 0;
2451
2452 string source_zone_name;
2453 string source_zone; /* zone id */
2454
2455 string tier_type;
2456 bool tier_type_specified = false;
2457
2458 map<string, string, ltstr_nocase> tier_config_add;
2459 map<string, string, ltstr_nocase> tier_config_rm;
2460
2461 boost::optional<string> index_pool;
2462 boost::optional<string> data_pool;
2463 boost::optional<string> data_extra_pool;
2464 RGWBucketIndexType placement_index_type = RGWBIType_Normal;
2465 bool index_type_specified = false;
2466
2467 boost::optional<std::string> compression_type;
2468
2469 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
2470 if (ceph_argparse_double_dash(args, i)) {
2471 break;
2472 } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
2473 usage();
2474 assert(false);
2475 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--uid", (char*)NULL)) {
2476 user_id.from_str(val);
2477 } else if (ceph_argparse_witharg(args, i, &val, "--tenant", (char*)NULL)) {
2478 tenant = val;
2479 } else if (ceph_argparse_witharg(args, i, &val, "--access-key", (char*)NULL)) {
2480 access_key = val;
2481 } else if (ceph_argparse_witharg(args, i, &val, "--subuser", (char*)NULL)) {
2482 subuser = val;
2483 } else if (ceph_argparse_witharg(args, i, &val, "--secret", "--secret-key", (char*)NULL)) {
2484 secret_key = val;
2485 } else if (ceph_argparse_witharg(args, i, &val, "-e", "--email", (char*)NULL)) {
2486 user_email = val;
2487 user_op.user_email_specified=true;
2488 } else if (ceph_argparse_witharg(args, i, &val, "-n", "--display-name", (char*)NULL)) {
2489 display_name = val;
2490 } else if (ceph_argparse_witharg(args, i, &val, "-b", "--bucket", (char*)NULL)) {
2491 bucket_name = val;
2492 } else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) {
2493 pool_name = val;
2494 pool = rgw_pool(pool_name);
2495 } else if (ceph_argparse_witharg(args, i, &val, "-o", "--object", (char*)NULL)) {
2496 object = val;
2497 } else if (ceph_argparse_witharg(args, i, &val, "--object-version", (char*)NULL)) {
2498 object_version = val;
2499 } else if (ceph_argparse_witharg(args, i, &val, "--client-id", (char*)NULL)) {
2500 client_id = val;
2501 } else if (ceph_argparse_witharg(args, i, &val, "--op-id", (char*)NULL)) {
2502 op_id = val;
2503 } else if (ceph_argparse_witharg(args, i, &val, "--state", (char*)NULL)) {
2504 state_str = val;
2505 } else if (ceph_argparse_witharg(args, i, &val, "--op-mask", (char*)NULL)) {
2506 op_mask_str = val;
2507 } else if (ceph_argparse_witharg(args, i, &val, "--key-type", (char*)NULL)) {
2508 key_type_str = val;
2509 if (key_type_str.compare("swift") == 0) {
2510 key_type = KEY_TYPE_SWIFT;
2511 } else if (key_type_str.compare("s3") == 0) {
2512 key_type = KEY_TYPE_S3;
2513 } else {
2514 cerr << "bad key type: " << key_type_str << std::endl;
2515 usage();
2516 assert(false);
2517 }
2518 } else if (ceph_argparse_witharg(args, i, &val, "--job-id", (char*)NULL)) {
2519 job_id = val;
2520 } else if (ceph_argparse_binary_flag(args, i, &gen_access_key, NULL, "--gen-access-key", (char*)NULL)) {
2521 // do nothing
2522 } else if (ceph_argparse_binary_flag(args, i, &gen_secret_key, NULL, "--gen-secret", (char*)NULL)) {
2523 // do nothing
2524 } else if (ceph_argparse_binary_flag(args, i, &show_log_entries, NULL, "--show_log_entries", (char*)NULL)) {
2525 // do nothing
2526 } else if (ceph_argparse_binary_flag(args, i, &show_log_sum, NULL, "--show_log_sum", (char*)NULL)) {
2527 // do nothing
2528 } else if (ceph_argparse_binary_flag(args, i, &skip_zero_entries, NULL, "--skip_zero_entries", (char*)NULL)) {
2529 // do nothing
2530 } else if (ceph_argparse_binary_flag(args, i, &admin, NULL, "--admin", (char*)NULL)) {
2531 admin_specified = true;
2532 } else if (ceph_argparse_binary_flag(args, i, &system, NULL, "--system", (char*)NULL)) {
2533 system_specified = true;
2534 } else if (ceph_argparse_binary_flag(args, i, &verbose, NULL, "--verbose", (char*)NULL)) {
2535 // do nothing
2536 } else if (ceph_argparse_binary_flag(args, i, &staging, NULL, "--staging", (char*)NULL)) {
2537 // do nothing
2538 } else if (ceph_argparse_binary_flag(args, i, &commit, NULL, "--commit", (char*)NULL)) {
2539 // do nothing
2540 } else if (ceph_argparse_witharg(args, i, &tmp, errs, "-a", "--auth-uid", (char*)NULL)) {
2541 if (!errs.str().empty()) {
2542 cerr << errs.str() << std::endl;
2543 exit(EXIT_FAILURE);
2544 }
2545 } else if (ceph_argparse_witharg(args, i, &val, "--min-rewrite-size", (char*)NULL)) {
2546 min_rewrite_size = (uint64_t)atoll(val.c_str());
2547 } else if (ceph_argparse_witharg(args, i, &val, "--max-rewrite-size", (char*)NULL)) {
2548 max_rewrite_size = (uint64_t)atoll(val.c_str());
2549 } else if (ceph_argparse_witharg(args, i, &val, "--min-rewrite-stripe-size", (char*)NULL)) {
2550 min_rewrite_stripe_size = (uint64_t)atoll(val.c_str());
2551 } else if (ceph_argparse_witharg(args, i, &val, "--max-buckets", (char*)NULL)) {
2552 max_buckets = (int)strict_strtol(val.c_str(), 10, &err);
2553 if (!err.empty()) {
2554 cerr << "ERROR: failed to parse max buckets: " << err << std::endl;
2555 return EINVAL;
2556 }
2557 max_buckets_specified = true;
2558 } else if (ceph_argparse_witharg(args, i, &val, "--max-entries", (char*)NULL)) {
2559 max_entries = (int)strict_strtol(val.c_str(), 10, &err);
2560 if (!err.empty()) {
2561 cerr << "ERROR: failed to parse max entries: " << err << std::endl;
2562 return EINVAL;
2563 }
2564 } else if (ceph_argparse_witharg(args, i, &val, "--max-size", (char*)NULL)) {
2565 max_size = strict_si_cast<int64_t>(val.c_str(), &err);
2566 if (!err.empty()) {
2567 cerr << "ERROR: failed to parse max size: " << err << std::endl;
2568 return EINVAL;
2569 }
2570 have_max_size = true;
2571 } else if (ceph_argparse_witharg(args, i, &val, "--max-objects", (char*)NULL)) {
2572 max_objects = (int64_t)strict_strtoll(val.c_str(), 10, &err);
2573 if (!err.empty()) {
2574 cerr << "ERROR: failed to parse max objects: " << err << std::endl;
2575 return EINVAL;
2576 }
2577 have_max_objects = true;
2578 } else if (ceph_argparse_witharg(args, i, &val, "--date", "--time", (char*)NULL)) {
2579 date = val;
2580 if (end_date.empty())
2581 end_date = date;
2582 } else if (ceph_argparse_witharg(args, i, &val, "--start-date", "--start-time", (char*)NULL)) {
2583 start_date = val;
2584 } else if (ceph_argparse_witharg(args, i, &val, "--end-date", "--end-time", (char*)NULL)) {
2585 end_date = val;
2586 } else if (ceph_argparse_witharg(args, i, &val, "--num-shards", (char*)NULL)) {
2587 num_shards = (int)strict_strtol(val.c_str(), 10, &err);
2588 if (!err.empty()) {
2589 cerr << "ERROR: failed to parse num shards: " << err << std::endl;
2590 return EINVAL;
2591 }
2592 num_shards_specified = true;
2593 } else if (ceph_argparse_witharg(args, i, &val, "--max-concurrent-ios", (char*)NULL)) {
2594 max_concurrent_ios = (int)strict_strtol(val.c_str(), 10, &err);
2595 if (!err.empty()) {
2596 cerr << "ERROR: failed to parse max concurrent ios: " << err << std::endl;
2597 return EINVAL;
2598 }
2599 } else if (ceph_argparse_witharg(args, i, &val, "--orphan-stale-secs", (char*)NULL)) {
2600 orphan_stale_secs = (uint64_t)strict_strtoll(val.c_str(), 10, &err);
2601 if (!err.empty()) {
2602 cerr << "ERROR: failed to parse orphan stale secs: " << err << std::endl;
2603 return EINVAL;
2604 }
2605 } else if (ceph_argparse_witharg(args, i, &val, "--shard-id", (char*)NULL)) {
2606 shard_id = (int)strict_strtol(val.c_str(), 10, &err);
2607 if (!err.empty()) {
2608 cerr << "ERROR: failed to parse shard id: " << err << std::endl;
2609 return EINVAL;
2610 }
2611 specified_shard_id = true;
2612 } else if (ceph_argparse_witharg(args, i, &val, "--daemon-id", (char*)NULL)) {
2613 daemon_id = val;
2614 specified_daemon_id = true;
2615 } else if (ceph_argparse_witharg(args, i, &val, "--access", (char*)NULL)) {
2616 access = val;
2617 perm_mask = rgw_str_to_perm(access.c_str());
2618 set_perm = true;
2619 } else if (ceph_argparse_witharg(args, i, &val, "--temp-url-key", (char*)NULL)) {
2620 temp_url_keys[0] = val;
2621 set_temp_url_key = true;
2622 } else if (ceph_argparse_witharg(args, i, &val, "--temp-url-key2", "--temp-url-key-2", (char*)NULL)) {
2623 temp_url_keys[1] = val;
2624 set_temp_url_key = true;
2625 } else if (ceph_argparse_witharg(args, i, &val, "--bucket-id", (char*)NULL)) {
2626 bucket_id = val;
2627 if (bucket_id.empty()) {
2628 cerr << "bad bucket-id" << std::endl;
2629 usage();
2630 assert(false);
2631 }
2632 } else if (ceph_argparse_witharg(args, i, &val, "--format", (char*)NULL)) {
2633 format = val;
2634 } else if (ceph_argparse_witharg(args, i, &val, "--categories", (char*)NULL)) {
2635 string cat_str = val;
2636 list<string> cat_list;
2637 list<string>::iterator iter;
2638 get_str_list(cat_str, cat_list);
2639 for (iter = cat_list.begin(); iter != cat_list.end(); ++iter) {
2640 categories[*iter] = true;
2641 }
2642 } else if (ceph_argparse_binary_flag(args, i, &delete_child_objects, NULL, "--purge-objects", (char*)NULL)) {
2643 // do nothing
2644 } else if (ceph_argparse_binary_flag(args, i, &pretty_format, NULL, "--pretty-format", (char*)NULL)) {
2645 // do nothing
2646 } else if (ceph_argparse_binary_flag(args, i, &purge_data, NULL, "--purge-data", (char*)NULL)) {
2647 delete_child_objects = purge_data;
2648 } else if (ceph_argparse_binary_flag(args, i, &purge_keys, NULL, "--purge-keys", (char*)NULL)) {
2649 // do nothing
2650 } else if (ceph_argparse_binary_flag(args, i, &yes_i_really_mean_it, NULL, "--yes-i-really-mean-it", (char*)NULL)) {
2651 // do nothing
2652 } else if (ceph_argparse_binary_flag(args, i, &fix, NULL, "--fix", (char*)NULL)) {
2653 // do nothing
2654 } else if (ceph_argparse_binary_flag(args, i, &remove_bad, NULL, "--remove-bad", (char*)NULL)) {
2655 // do nothing
2656 } else if (ceph_argparse_binary_flag(args, i, &check_head_obj_locator, NULL, "--check-head-obj-locator", (char*)NULL)) {
2657 // do nothing
2658 } else if (ceph_argparse_binary_flag(args, i, &check_objects, NULL, "--check-objects", (char*)NULL)) {
2659 // do nothing
2660 } else if (ceph_argparse_binary_flag(args, i, &sync_stats, NULL, "--sync-stats", (char*)NULL)) {
2661 // do nothing
2662 } else if (ceph_argparse_binary_flag(args, i, &include_all, NULL, "--include-all", (char*)NULL)) {
2663 // do nothing
2664 } else if (ceph_argparse_binary_flag(args, i, &extra_info, NULL, "--extra-info", (char*)NULL)) {
2665 // do nothing
2666 } else if (ceph_argparse_binary_flag(args, i, &bypass_gc, NULL, "--bypass-gc", (char*)NULL)) {
2667 // do nothing
2668 } else if (ceph_argparse_binary_flag(args, i, &warnings_only, NULL, "--warnings-only", (char*)NULL)) {
2669 // do nothing
2670 } else if (ceph_argparse_binary_flag(args, i, &inconsistent_index, NULL, "--inconsistent-index", (char*)NULL)) {
2671 // do nothing
2672 } else if (ceph_argparse_witharg(args, i, &val, "--caps", (char*)NULL)) {
2673 caps = val;
2674 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--infile", (char*)NULL)) {
2675 infile = val;
2676 } else if (ceph_argparse_witharg(args, i, &val, "--metadata-key", (char*)NULL)) {
2677 metadata_key = val;
2678 } else if (ceph_argparse_witharg(args, i, &val, "--marker", (char*)NULL)) {
2679 marker = val;
2680 } else if (ceph_argparse_witharg(args, i, &val, "--start-marker", (char*)NULL)) {
2681 start_marker = val;
2682 } else if (ceph_argparse_witharg(args, i, &val, "--end-marker", (char*)NULL)) {
2683 end_marker = val;
2684 } else if (ceph_argparse_witharg(args, i, &val, "--quota-scope", (char*)NULL)) {
2685 quota_scope = val;
2686 } else if (ceph_argparse_witharg(args, i, &val, "--replica-log-type", (char*)NULL)) {
2687 replica_log_type_str = val;
2688 replica_log_type = get_replicalog_type(replica_log_type_str);
2689 if (replica_log_type == ReplicaLog_Invalid) {
2690 cerr << "ERROR: invalid replica log type" << std::endl;
2691 return EINVAL;
2692 }
2693 } else if (ceph_argparse_witharg(args, i, &val, "--index-type", (char*)NULL)) {
2694 string index_type_str = val;
2695 bi_index_type = get_bi_index_type(index_type_str);
2696 if (bi_index_type == InvalidIdx) {
2697 cerr << "ERROR: invalid bucket index entry type" << std::endl;
2698 return EINVAL;
2699 }
2700 } else if (ceph_argparse_binary_flag(args, i, &is_master_int, NULL, "--master", (char*)NULL)) {
2701 is_master = (bool)is_master_int;
2702 is_master_set = true;
2703 } else if (ceph_argparse_binary_flag(args, i, &set_default, NULL, "--default", (char*)NULL)) {
2704 /* do nothing */
2705 } else if (ceph_argparse_binary_flag(args, i, &read_only_int, NULL, "--read-only", (char*)NULL)) {
2706 read_only = (bool)read_only_int;
2707 is_read_only_set = true;
2708 } else if (ceph_argparse_witharg(args, i, &val, "--master-url", (char*)NULL)) {
2709 master_url = val;
2710 } else if (ceph_argparse_witharg(args, i, &val, "--master-zonegroup", (char*)NULL)) {
2711 master_zonegroup = val;
2712 } else if (ceph_argparse_witharg(args, i, &val, "--master-zone", (char*)NULL)) {
2713 master_zone = val;
2714 } else if (ceph_argparse_witharg(args, i, &val, "--period", (char*)NULL)) {
2715 period_id = val;
2716 } else if (ceph_argparse_witharg(args, i, &val, "--epoch", (char*)NULL)) {
2717 period_epoch = val;
2718 } else if (ceph_argparse_witharg(args, i, &val, "--remote", (char*)NULL)) {
2719 remote = val;
2720 } else if (ceph_argparse_witharg(args, i, &val, "--url", (char*)NULL)) {
2721 url = val;
2722 } else if (ceph_argparse_witharg(args, i, &val, "--realm-id", (char*)NULL)) {
2723 realm_id = val;
2724 } else if (ceph_argparse_witharg(args, i, &val, "--realm-new-name", (char*)NULL)) {
2725 realm_new_name = val;
2726 } else if (ceph_argparse_witharg(args, i, &val, "--zonegroup-id", (char*)NULL)) {
2727 zonegroup_id = val;
2728 } else if (ceph_argparse_witharg(args, i, &val, "--zonegroup-new-name", (char*)NULL)) {
2729 zonegroup_new_name = val;
2730 } else if (ceph_argparse_witharg(args, i, &val, "--placement-id", (char*)NULL)) {
2731 placement_id = val;
2732 } else if (ceph_argparse_witharg(args, i, &val, "--tags", (char*)NULL)) {
2733 get_str_list(val, tags);
2734 } else if (ceph_argparse_witharg(args, i, &val, "--tags-add", (char*)NULL)) {
2735 get_str_list(val, tags_add);
2736 } else if (ceph_argparse_witharg(args, i, &val, "--tags-rm", (char*)NULL)) {
2737 get_str_list(val, tags_rm);
2738 } else if (ceph_argparse_witharg(args, i, &val, "--api-name", (char*)NULL)) {
2739 api_name = val;
2740 } else if (ceph_argparse_witharg(args, i, &val, "--zone-id", (char*)NULL)) {
2741 zone_id = val;
2742 } else if (ceph_argparse_witharg(args, i, &val, "--zone-new-name", (char*)NULL)) {
2743 zone_new_name = val;
2744 } else if (ceph_argparse_witharg(args, i, &val, "--endpoints", (char*)NULL)) {
2745 get_str_list(val, endpoints);
2746 } else if (ceph_argparse_witharg(args, i, &val, "--sync-from", (char*)NULL)) {
2747 get_str_list(val, sync_from);
2748 } else if (ceph_argparse_witharg(args, i, &val, "--sync-from-rm", (char*)NULL)) {
2749 get_str_list(val, sync_from_rm);
2750 } else if (ceph_argparse_binary_flag(args, i, &tmp_int, NULL, "--sync-from-all", (char*)NULL)) {
2751 sync_from_all = (bool)tmp_int;
2752 sync_from_all_specified = true;
2753 } else if (ceph_argparse_witharg(args, i, &val, "--source-zone", (char*)NULL)) {
2754 source_zone_name = val;
2755 } else if (ceph_argparse_witharg(args, i, &val, "--tier-type", (char*)NULL)) {
2756 tier_type = val;
2757 tier_type_specified = true;
2758 } else if (ceph_argparse_witharg(args, i, &val, "--tier-config", (char*)NULL)) {
2759 parse_tier_config_param(val, tier_config_add);
2760 } else if (ceph_argparse_witharg(args, i, &val, "--tier-config-rm", (char*)NULL)) {
2761 parse_tier_config_param(val, tier_config_rm);
2762 } else if (ceph_argparse_witharg(args, i, &val, "--index-pool", (char*)NULL)) {
2763 index_pool = val;
2764 } else if (ceph_argparse_witharg(args, i, &val, "--data-pool", (char*)NULL)) {
2765 data_pool = val;
2766 } else if (ceph_argparse_witharg(args, i, &val, "--data-extra-pool", (char*)NULL)) {
2767 data_extra_pool = val;
2768 } else if (ceph_argparse_witharg(args, i, &val, "--placement-index-type", (char*)NULL)) {
2769 if (val == "normal") {
2770 placement_index_type = RGWBIType_Normal;
2771 } else if (val == "indexless") {
2772 placement_index_type = RGWBIType_Indexless;
2773 } else {
2774 placement_index_type = (RGWBucketIndexType)strict_strtol(val.c_str(), 10, &err);
2775 if (!err.empty()) {
2776 cerr << "ERROR: failed to parse index type index: " << err << std::endl;
2777 return EINVAL;
2778 }
2779 }
2780 index_type_specified = true;
2781 } else if (ceph_argparse_witharg(args, i, &val, "--compression", (char*)NULL)) {
2782 compression_type = val;
2783 } else if (ceph_argparse_witharg(args, i, &val, "--role-name", (char*)NULL)) {
2784 role_name = val;
2785 } else if (ceph_argparse_witharg(args, i, &val, "--path", (char*)NULL)) {
2786 path = val;
2787 } else if (ceph_argparse_witharg(args, i, &val, "--assume-role-policy-doc", (char*)NULL)) {
2788 assume_role_doc = val;
2789 } else if (ceph_argparse_witharg(args, i, &val, "--policy-name", (char*)NULL)) {
2790 policy_name = val;
2791 } else if (ceph_argparse_witharg(args, i, &val, "--policy-doc", (char*)NULL)) {
2792 perm_policy_doc = val;
2793 } else if (ceph_argparse_witharg(args, i, &val, "--path-prefix", (char*)NULL)) {
2794 path_prefix = val;
2795 } else if (strncmp(*i, "-", 1) == 0) {
2796 cerr << "ERROR: invalid flag " << *i << std::endl;
2797 return EINVAL;
2798 } else {
2799 ++i;
2800 }
2801 }
2802
2803 if (args.empty()) {
2804 usage();
2805 assert(false);
2806 }
2807 else {
2808 const char *prev_cmd = NULL;
2809 const char *prev_prev_cmd = NULL;
2810 std::vector<const char*>::iterator i ;
2811 for (i = args.begin(); i != args.end(); ++i) {
2812 opt_cmd = get_cmd(*i, prev_cmd, prev_prev_cmd, &need_more);
2813 if (opt_cmd < 0) {
2814 cerr << "unrecognized arg " << *i << std::endl;
2815 usage();
2816 assert(false);
2817 }
2818 if (!need_more) {
2819 ++i;
2820 break;
2821 }
2822 prev_prev_cmd = prev_cmd;
2823 prev_cmd = *i;
2824 }
2825
2826 if (opt_cmd == OPT_NO_CMD) {
2827 usage();
2828 assert(false);
2829 }
2830
2831 /* some commands may have an optional extra param */
2832 if (i != args.end()) {
2833 switch (opt_cmd) {
2834 case OPT_METADATA_GET:
2835 case OPT_METADATA_PUT:
2836 case OPT_METADATA_RM:
2837 case OPT_METADATA_LIST:
2838 metadata_key = *i;
2839 break;
2840 default:
2841 break;
2842 }
2843 }
2844
2845 if (tenant.empty()) {
2846 tenant = user_id.tenant;
2847 } else {
2848 if (user_id.empty() && opt_cmd != OPT_ROLE_CREATE
2849 && opt_cmd != OPT_ROLE_DELETE
2850 && opt_cmd != OPT_ROLE_GET
2851 && opt_cmd != OPT_ROLE_MODIFY
2852 && opt_cmd != OPT_ROLE_LIST
2853 && opt_cmd != OPT_ROLE_POLICY_PUT
2854 && opt_cmd != OPT_ROLE_POLICY_LIST
2855 && opt_cmd != OPT_ROLE_POLICY_GET
2856 && opt_cmd != OPT_ROLE_POLICY_DELETE) {
2857 cerr << "ERROR: --tenant is set, but there's no user ID" << std::endl;
2858 return EINVAL;
2859 }
2860 user_id.tenant = tenant;
2861 }
2862 /* check key parameter conflict */
2863 if ((!access_key.empty()) && gen_access_key) {
2864 cerr << "ERROR: key parameter conflict, --access-key & --gen-access-key" << std::endl;
2865 return EINVAL;
2866 }
2867 if ((!secret_key.empty()) && gen_secret_key) {
2868 cerr << "ERROR: key parameter conflict, --secret & --gen-secret" << std::endl;
2869 return EINVAL;
2870 }
2871 }
2872
2873 // default to pretty json
2874 if (format.empty()) {
2875 format = "json";
2876 pretty_format = true;
2877 }
2878
2879 if (format == "xml")
2880 formatter = new XMLFormatter(pretty_format);
2881 else if (format == "json")
2882 formatter = new JSONFormatter(pretty_format);
2883 else {
2884 cerr << "unrecognized format: " << format << std::endl;
2885 usage();
2886 assert(false);
2887 }
2888
2889 realm_name = g_conf->rgw_realm;
2890 zone_name = g_conf->rgw_zone;
2891 zonegroup_name = g_conf->rgw_zonegroup;
2892
2893 RGWStreamFlusher f(formatter, cout);
2894
2895 // not a raw op if 'period update' needs to commit to master
2896 bool raw_period_update = opt_cmd == OPT_PERIOD_UPDATE && !commit;
2897 std::set<int> raw_storage_ops_list = {OPT_ZONEGROUP_ADD, OPT_ZONEGROUP_CREATE, OPT_ZONEGROUP_DELETE,
2898 OPT_ZONEGROUP_GET, OPT_ZONEGROUP_LIST,
2899 OPT_ZONEGROUP_SET, OPT_ZONEGROUP_DEFAULT,
2900 OPT_ZONEGROUP_RENAME, OPT_ZONEGROUP_MODIFY,
2901 OPT_ZONEGROUP_REMOVE,
2902 OPT_ZONEGROUP_PLACEMENT_ADD, OPT_ZONEGROUP_PLACEMENT_RM,
2903 OPT_ZONEGROUP_PLACEMENT_MODIFY, OPT_ZONEGROUP_PLACEMENT_LIST,
2904 OPT_ZONEGROUP_PLACEMENT_DEFAULT,
2905 OPT_ZONE_CREATE, OPT_ZONE_DELETE,
2906 OPT_ZONE_GET, OPT_ZONE_SET, OPT_ZONE_RENAME,
2907 OPT_ZONE_LIST, OPT_ZONE_MODIFY, OPT_ZONE_DEFAULT,
2908 OPT_ZONE_PLACEMENT_ADD, OPT_ZONE_PLACEMENT_RM,
2909 OPT_ZONE_PLACEMENT_MODIFY, OPT_ZONE_PLACEMENT_LIST,
2910 OPT_REALM_CREATE,
2911 OPT_PERIOD_DELETE, OPT_PERIOD_GET,
2912 OPT_PERIOD_PULL,
2913 OPT_PERIOD_GET_CURRENT, OPT_PERIOD_LIST,
2914 OPT_GLOBAL_QUOTA_GET, OPT_GLOBAL_QUOTA_SET,
2915 OPT_GLOBAL_QUOTA_ENABLE, OPT_GLOBAL_QUOTA_DISABLE,
2916 OPT_REALM_DELETE, OPT_REALM_GET, OPT_REALM_LIST,
2917 OPT_REALM_LIST_PERIODS,
2918 OPT_REALM_GET_DEFAULT, OPT_REALM_REMOVE,
2919 OPT_REALM_RENAME, OPT_REALM_SET,
2920 OPT_REALM_DEFAULT, OPT_REALM_PULL};
2921
2922
2923 bool raw_storage_op = (raw_storage_ops_list.find(opt_cmd) != raw_storage_ops_list.end() ||
2924 raw_period_update);
2925
2926 if (raw_storage_op) {
2927 store = RGWStoreManager::get_raw_storage(g_ceph_context);
2928 } else {
2929 store = RGWStoreManager::get_storage(g_ceph_context, false, false, false, false, false);
2930 }
2931 if (!store) {
2932 cerr << "couldn't init storage provider" << std::endl;
2933 return 5; //EIO
2934 }
2935
2936 if (!source_zone_name.empty()) {
2937 if (!store->find_zone_id_by_name(source_zone_name, &source_zone)) {
2938 cerr << "WARNING: cannot find source zone id for name=" << source_zone_name << std::endl;
2939 source_zone = source_zone_name;
2940 }
2941 }
2942
2943 rgw_user_init(store);
2944 rgw_bucket_init(store->meta_mgr);
2945
2946 StoreDestructor store_destructor(store);
2947
2948 if (raw_storage_op) {
2949 switch (opt_cmd) {
2950 case OPT_PERIOD_DELETE:
2951 {
2952 if (period_id.empty()) {
2953 cerr << "missing period id" << std::endl;
2954 return EINVAL;
2955 }
2956 RGWPeriod period(period_id);
2957 int ret = period.init(g_ceph_context, store);
2958 if (ret < 0) {
2959 cerr << "period.init failed: " << cpp_strerror(-ret) << std::endl;
2960 return -ret;
2961 }
2962 ret = period.delete_obj();
2963 if (ret < 0) {
2964 cerr << "ERROR: couldn't delete period: " << cpp_strerror(-ret) << std::endl;
2965 return -ret;
2966 }
2967
2968 }
2969 break;
2970 case OPT_PERIOD_GET:
2971 {
2972 epoch_t epoch = 0;
2973 if (!period_epoch.empty()) {
2974 epoch = atoi(period_epoch.c_str());
2975 }
2976 if (staging) {
2977 RGWRealm realm(realm_id, realm_name);
2978 int ret = realm.init(g_ceph_context, store);
2979 if (ret < 0 ) {
2980 cerr << "Error initializing realm " << cpp_strerror(-ret) << std::endl;
2981 return -ret;
2982 }
2983 realm_id = realm.get_id();
2984 realm_name = realm.get_name();
2985 period_id = RGWPeriod::get_staging_id(realm_id);
2986 epoch = 1;
2987 }
2988 RGWPeriod period(period_id, epoch);
2989 int ret = period.init(g_ceph_context, store, realm_id, realm_name);
2990 if (ret < 0) {
2991 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
2992 return -ret;
2993 }
2994 encode_json("period", period, formatter);
2995 formatter->flush(cout);
2996 cout << std::endl;
2997 }
2998 break;
2999 case OPT_PERIOD_GET_CURRENT:
3000 {
3001 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
3002 if (ret < 0) {
3003 return -ret;
3004 }
3005 formatter->open_object_section("period_get_current");
3006 encode_json("current_period", period_id, formatter);
3007 formatter->close_section();
3008 formatter->flush(cout);
3009 }
3010 break;
3011 case OPT_PERIOD_LIST:
3012 {
3013 list<string> periods;
3014 int ret = store->list_periods(periods);
3015 if (ret < 0) {
3016 cerr << "failed to list periods: " << cpp_strerror(-ret) << std::endl;
3017 return -ret;
3018 }
3019 formatter->open_object_section("periods_list");
3020 encode_json("periods", periods, formatter);
3021 formatter->close_section();
3022 formatter->flush(cout);
3023 cout << std::endl;
3024 }
3025 break;
3026 case OPT_PERIOD_UPDATE:
3027 {
3028 int ret = update_period(realm_id, realm_name, period_id, period_epoch,
3029 commit, remote, url, access_key, secret_key,
3030 formatter, yes_i_really_mean_it);
3031 if (ret < 0) {
3032 return -ret;
3033 }
3034 }
3035 break;
3036 case OPT_PERIOD_PULL:
3037 {
3038 boost::optional<RGWRESTConn> conn;
3039 RGWRESTConn *remote_conn = nullptr;
3040 if (url.empty()) {
3041 // load current period for endpoints
3042 RGWRealm realm(realm_id, realm_name);
3043 int ret = realm.init(g_ceph_context, store);
3044 if (ret < 0) {
3045 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3046 return -ret;
3047 }
3048 RGWPeriod current_period(realm.get_current_period());
3049 ret = current_period.init(g_ceph_context, store);
3050 if (ret < 0) {
3051 cerr << "failed to init current period: " << cpp_strerror(-ret) << std::endl;
3052 return -ret;
3053 }
3054 if (remote.empty()) {
3055 // use realm master zone as remote
3056 remote = current_period.get_master_zone();
3057 }
3058 conn = get_remote_conn(store, current_period.get_map(), remote);
3059 if (!conn) {
3060 cerr << "failed to find a zone or zonegroup for remote "
3061 << remote << std::endl;
3062 return -ENOENT;
3063 }
3064 remote_conn = &*conn;
3065 }
3066
3067 RGWPeriod period;
3068 int ret = do_period_pull(remote_conn, url, access_key, secret_key,
3069 realm_id, realm_name, period_id, period_epoch,
3070 &period);
3071 if (ret < 0) {
3072 cerr << "period pull failed: " << cpp_strerror(-ret) << std::endl;
3073 return -ret;
3074 }
3075
3076 encode_json("period", period, formatter);
3077 formatter->flush(cout);
3078 cout << std::endl;
3079 }
3080 break;
3081 case OPT_GLOBAL_QUOTA_GET:
3082 case OPT_GLOBAL_QUOTA_SET:
3083 case OPT_GLOBAL_QUOTA_ENABLE:
3084 case OPT_GLOBAL_QUOTA_DISABLE:
3085 {
3086 if (realm_id.empty()) {
3087 RGWRealm realm(g_ceph_context, store);
3088 if (!realm_name.empty()) {
3089 // look up realm_id for the given realm_name
3090 int ret = realm.read_id(realm_name, realm_id);
3091 if (ret < 0) {
3092 cerr << "ERROR: failed to read realm for " << realm_name
3093 << ": " << cpp_strerror(-ret) << std::endl;
3094 return -ret;
3095 }
3096 } else {
3097 // use default realm_id when none is given
3098 int ret = realm.read_default_id(realm_id);
3099 if (ret < 0 && ret != -ENOENT) { // on ENOENT, use empty realm_id
3100 cerr << "ERROR: failed to read default realm: "
3101 << cpp_strerror(-ret) << std::endl;
3102 return -ret;
3103 }
3104 }
3105 }
3106
3107 RGWPeriodConfig period_config;
3108 int ret = period_config.read(store, realm_id);
3109 if (ret < 0 && ret != -ENOENT) {
3110 cerr << "ERROR: failed to read period config: "
3111 << cpp_strerror(-ret) << std::endl;
3112 return -ret;
3113 }
3114
3115 formatter->open_object_section("period_config");
3116 if (quota_scope == "bucket") {
3117 set_quota_info(period_config.bucket_quota, opt_cmd,
3118 max_size, max_objects,
3119 have_max_size, have_max_objects);
3120 encode_json("bucket quota", period_config.bucket_quota, formatter);
3121 } else if (quota_scope == "user") {
3122 set_quota_info(period_config.user_quota, opt_cmd,
3123 max_size, max_objects,
3124 have_max_size, have_max_objects);
3125 encode_json("user quota", period_config.user_quota, formatter);
3126 } else if (quota_scope.empty() && opt_cmd == OPT_GLOBAL_QUOTA_GET) {
3127 // if no scope is given for GET, print both
3128 encode_json("bucket quota", period_config.bucket_quota, formatter);
3129 encode_json("user quota", period_config.user_quota, formatter);
3130 } else {
3131 cerr << "ERROR: invalid quota scope specification. Please specify "
3132 "either --quota-scope=bucket, or --quota-scope=user" << std::endl;
3133 return EINVAL;
3134 }
3135 formatter->close_section();
3136
3137 if (opt_cmd != OPT_GLOBAL_QUOTA_GET) {
3138 // write the modified period config
3139 ret = period_config.write(store, realm_id);
3140 if (ret < 0) {
3141 cerr << "ERROR: failed to write period config: "
3142 << cpp_strerror(-ret) << std::endl;
3143 return -ret;
3144 }
3145 if (!realm_id.empty()) {
3146 cout << "Global quota changes saved. Use 'period update' to apply "
3147 "them to the staging period, and 'period commit' to commit the "
3148 "new period." << std::endl;
3149 } else {
3150 cout << "Global quota changes saved. They will take effect as "
3151 "the gateways are restarted." << std::endl;
3152 }
3153 }
3154
3155 formatter->flush(cout);
3156 cout << std::endl;
3157 }
3158 break;
3159 case OPT_REALM_CREATE:
3160 {
3161 if (realm_name.empty()) {
3162 cerr << "missing realm name" << std::endl;
3163 return EINVAL;
3164 }
3165
3166 RGWRealm realm(realm_name, g_ceph_context, store);
3167 int ret = realm.create();
3168 if (ret < 0) {
3169 cerr << "ERROR: couldn't create realm " << realm_name << ": " << cpp_strerror(-ret) << std::endl;
3170 return -ret;
3171 }
3172
3173 if (set_default) {
3174 ret = realm.set_as_default();
3175 if (ret < 0) {
3176 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3177 }
3178 }
3179
3180 encode_json("realm", realm, formatter);
3181 formatter->flush(cout);
3182 cout << std::endl;
3183 }
3184 break;
3185 case OPT_REALM_DELETE:
3186 {
3187 RGWRealm realm(realm_id, realm_name);
3188 if (realm_name.empty() && realm_id.empty()) {
3189 cerr << "missing realm name or id" << std::endl;
3190 return EINVAL;
3191 }
3192 int ret = realm.init(g_ceph_context, store);
3193 if (ret < 0) {
3194 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3195 return -ret;
3196 }
3197 ret = realm.delete_obj();
3198 if (ret < 0) {
3199 cerr << "ERROR: couldn't : " << cpp_strerror(-ret) << std::endl;
3200 return -ret;
3201 }
3202
3203 }
3204 break;
3205 case OPT_REALM_GET:
3206 {
3207 RGWRealm realm(realm_id, realm_name);
3208 int ret = realm.init(g_ceph_context, store);
3209 if (ret < 0) {
3210 if (ret == -ENOENT && realm_name.empty() && realm_id.empty()) {
3211 cerr << "missing realm name or id, or default realm not found" << std::endl;
3212 } else {
3213 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3214 }
3215 return -ret;
3216 }
3217 encode_json("realm", realm, formatter);
3218 formatter->flush(cout);
3219 cout << std::endl;
3220 }
3221 break;
3222 case OPT_REALM_GET_DEFAULT:
3223 {
3224 RGWRealm realm(g_ceph_context, store);
3225 string default_id;
3226 int ret = realm.read_default_id(default_id);
3227 if (ret == -ENOENT) {
3228 cout << "No default realm is set" << std::endl;
3229 return -ret;
3230 } else if (ret < 0) {
3231 cerr << "Error reading default realm:" << cpp_strerror(-ret) << std::endl;
3232 return -ret;
3233 }
3234 cout << "default realm: " << default_id << std::endl;
3235 }
3236 break;
3237 case OPT_REALM_LIST:
3238 {
3239 RGWRealm realm(g_ceph_context, store);
3240 string default_id;
3241 int ret = realm.read_default_id(default_id);
3242 if (ret < 0 && ret != -ENOENT) {
3243 cerr << "could not determine default realm: " << cpp_strerror(-ret) << std::endl;
3244 }
3245 list<string> realms;
3246 ret = store->list_realms(realms);
3247 if (ret < 0) {
3248 cerr << "failed to list realms: " << cpp_strerror(-ret) << std::endl;
3249 return -ret;
3250 }
3251 formatter->open_object_section("realms_list");
3252 encode_json("default_info", default_id, formatter);
3253 encode_json("realms", realms, formatter);
3254 formatter->close_section();
3255 formatter->flush(cout);
3256 cout << std::endl;
3257 }
3258 break;
3259 case OPT_REALM_LIST_PERIODS:
3260 {
3261 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
3262 if (ret < 0) {
3263 return -ret;
3264 }
3265 list<string> periods;
3266 ret = store->list_periods(period_id, periods);
3267 if (ret < 0) {
3268 cerr << "list periods failed: " << cpp_strerror(-ret) << std::endl;
3269 return -ret;
3270 }
3271 formatter->open_object_section("realm_periods_list");
3272 encode_json("current_period", period_id, formatter);
3273 encode_json("periods", periods, formatter);
3274 formatter->close_section();
3275 formatter->flush(cout);
3276 cout << std::endl;
3277 }
3278 break;
3279
3280 case OPT_REALM_RENAME:
3281 {
3282 RGWRealm realm(realm_id, realm_name);
3283 if (realm_new_name.empty()) {
3284 cerr << "missing realm new name" << std::endl;
3285 return EINVAL;
3286 }
3287 if (realm_name.empty() && realm_id.empty()) {
3288 cerr << "missing realm name or id" << std::endl;
3289 return EINVAL;
3290 }
3291 int ret = realm.init(g_ceph_context, store);
3292 if (ret < 0) {
3293 cerr << "realm.init failed: " << cpp_strerror(-ret) << std::endl;
3294 return -ret;
3295 }
3296 ret = realm.rename(realm_new_name);
3297 if (ret < 0) {
3298 cerr << "realm.rename failed: " << cpp_strerror(-ret) << std::endl;
3299 return -ret;
3300 }
3301 cout << "Realm name updated. Note that this change only applies to "
3302 "the current cluster, so this command must be run separately "
3303 "on each of the realm's other clusters." << std::endl;
3304 }
3305 break;
3306 case OPT_REALM_SET:
3307 {
3308 if (realm_id.empty() && realm_name.empty()) {
3309 cerr << "no realm name or id provided" << std::endl;
3310 return EINVAL;
3311 }
3312 RGWRealm realm(realm_id, realm_name);
3313 bool new_realm = false;
3314 int ret = realm.init(g_ceph_context, store);
3315 if (ret < 0 && ret != -ENOENT) {
3316 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3317 return -ret;
3318 } else if (ret == -ENOENT) {
3319 new_realm = true;
3320 }
3321 ret = read_decode_json(infile, realm);
3322 if (ret < 0) {
3323 return 1;
3324 }
3325 if (!realm_name.empty() && realm.get_name() != realm_name) {
3326 cerr << "mismatch between --rgw-realm " << realm_name << " and json input file name " <<
3327 realm.get_name() << std::endl;
3328 return EINVAL;
3329 }
3330 /* new realm */
3331 if (new_realm) {
3332 cout << "clearing period and epoch for new realm" << std::endl;
3333 realm.clear_current_period_and_epoch();
3334 ret = realm.create();
3335 if (ret < 0) {
3336 cerr << "ERROR: couldn't create new realm: " << cpp_strerror(-ret) << std::endl;
3337 return 1;
3338 }
3339 } else {
3340 ret = realm.update();
3341 if (ret < 0) {
3342 cerr << "ERROR: couldn't store realm info: " << cpp_strerror(-ret) << std::endl;
3343 return 1;
3344 }
3345 }
3346
3347 if (set_default) {
3348 ret = realm.set_as_default();
3349 if (ret < 0) {
3350 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3351 }
3352 }
3353 encode_json("realm", realm, formatter);
3354 formatter->flush(cout);
3355 }
3356 break;
3357
3358 case OPT_REALM_DEFAULT:
3359 {
3360 RGWRealm realm(realm_id, realm_name);
3361 int ret = realm.init(g_ceph_context, store);
3362 if (ret < 0) {
3363 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3364 return -ret;
3365 }
3366 ret = realm.set_as_default();
3367 if (ret < 0) {
3368 cerr << "failed to set realm as default: " << cpp_strerror(-ret) << std::endl;
3369 return -ret;
3370 }
3371 }
3372 break;
3373 case OPT_REALM_PULL:
3374 {
3375 if (url.empty()) {
3376 cerr << "A --url must be provided." << std::endl;
3377 return EINVAL;
3378 }
3379 RGWEnv env;
3380 req_info info(g_ceph_context, &env);
3381 info.method = "GET";
3382 info.request_uri = "/admin/realm";
3383
3384 map<string, string> &params = info.args.get_params();
3385 if (!realm_id.empty())
3386 params["id"] = realm_id;
3387 if (!realm_name.empty())
3388 params["name"] = realm_name;
3389
3390 bufferlist bl;
3391 JSONParser p;
3392 int ret = send_to_url(url, access_key, secret_key, info, bl, p);
3393 if (ret < 0) {
3394 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
3395 if (ret == -EACCES) {
3396 cerr << "If the realm has been changed on the master zone, the "
3397 "master zone's gateway may need to be restarted to recognize "
3398 "this user." << std::endl;
3399 }
3400 return -ret;
3401 }
3402 RGWRealm realm;
3403 realm.init(g_ceph_context, store, false);
3404 try {
3405 decode_json_obj(realm, &p);
3406 } catch (JSONDecoder::err& e) {
3407 cerr << "failed to decode JSON response: " << e.message << std::endl;
3408 return EINVAL;
3409 }
3410 RGWPeriod period;
3411 auto& current_period = realm.get_current_period();
3412 if (!current_period.empty()) {
3413 // pull the latest epoch of the realm's current period
3414 ret = do_period_pull(nullptr, url, access_key, secret_key,
3415 realm_id, realm_name, current_period, "",
3416 &period);
3417 if (ret < 0) {
3418 cerr << "could not fetch period " << current_period << std::endl;
3419 return -ret;
3420 }
3421 }
3422 ret = realm.create(false);
3423 if (ret < 0 && ret != -EEXIST) {
3424 cerr << "Error storing realm " << realm.get_id() << ": "
3425 << cpp_strerror(ret) << std::endl;
3426 return -ret;
3427 } else if (ret ==-EEXIST) {
3428 ret = realm.update();
3429 if (ret < 0) {
3430 cerr << "Error storing realm " << realm.get_id() << ": "
3431 << cpp_strerror(ret) << std::endl;
3432 }
3433 }
3434
3435 if (set_default) {
3436 ret = realm.set_as_default();
3437 if (ret < 0) {
3438 cerr << "failed to set realm " << realm_name << " as default: " << cpp_strerror(-ret) << std::endl;
3439 }
3440 }
3441
3442 encode_json("realm", realm, formatter);
3443 formatter->flush(cout);
3444 cout << std::endl;
3445 }
3446 return 0;
3447
3448 case OPT_ZONEGROUP_ADD:
3449 {
3450 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3451 cerr << "no zonegroup name or id provided" << std::endl;
3452 return EINVAL;
3453 }
3454
3455 RGWZoneGroup zonegroup(zonegroup_id,zonegroup_name);
3456 int ret = zonegroup.init(g_ceph_context, store);
3457 if (ret < 0) {
3458 cerr << "failed to initialize zonegroup " << zonegroup_name << " id " << zonegroup_id << " :"
3459 << cpp_strerror(-ret) << std::endl;
3460 return -ret;
3461 }
3462 RGWZoneParams zone(zone_id, zone_name);
3463 ret = zone.init(g_ceph_context, store);
3464 if (ret < 0) {
3465 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3466 return -ret;
3467 }
3468 if (zone.realm_id != zonegroup.realm_id) {
3469 zone.realm_id = zonegroup.realm_id;
3470 ret = zone.update();
3471 if (ret < 0) {
3472 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
3473 return -ret;
3474 }
3475 }
3476
3477 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
3478 zone.tier_config = tier_config_add;
3479
3480 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
3481
3482 ret = zonegroup.add_zone(zone,
3483 (is_master_set ? &is_master : NULL),
3484 (is_read_only_set ? &read_only : NULL),
3485 endpoints, ptier_type,
3486 psync_from_all, sync_from, sync_from_rm);
3487 if (ret < 0) {
3488 cerr << "failed to add zone " << zone_name << " to zonegroup " << zonegroup.get_name() << ": "
3489 << cpp_strerror(-ret) << std::endl;
3490 return -ret;
3491 }
3492
3493 encode_json("zonegroup", zonegroup, formatter);
3494 formatter->flush(cout);
3495 }
3496 break;
3497 case OPT_ZONEGROUP_CREATE:
3498 {
3499 if (zonegroup_name.empty()) {
3500 cerr << "Missing zonegroup name" << std::endl;
3501 return EINVAL;
3502 }
3503 RGWRealm realm(realm_id, realm_name);
3504 int ret = realm.init(g_ceph_context, store);
3505 if (ret < 0) {
3506 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3507 return -ret;
3508 }
3509
3510 RGWZoneGroup zonegroup(zonegroup_name, is_master, g_ceph_context, store, realm.get_id(), endpoints);
3511 zonegroup.api_name = (api_name.empty() ? zonegroup_name : api_name);
3512 ret = zonegroup.create();
3513 if (ret < 0) {
3514 cerr << "failed to create zonegroup " << zonegroup_name << ": " << cpp_strerror(-ret) << std::endl;
3515 return -ret;
3516 }
3517
3518 if (set_default) {
3519 ret = zonegroup.set_as_default();
3520 if (ret < 0) {
3521 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3522 }
3523 }
3524
3525 encode_json("zonegroup", zonegroup, formatter);
3526 formatter->flush(cout);
3527 cout << std::endl;
3528 }
3529 break;
3530 case OPT_ZONEGROUP_DEFAULT:
3531 {
3532 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3533 cerr << "no zonegroup name or id provided" << std::endl;
3534 return EINVAL;
3535 }
3536
3537 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3538 int ret = zonegroup.init(g_ceph_context, store);
3539 if (ret < 0) {
3540 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3541 return -ret;
3542 }
3543
3544 ret = zonegroup.set_as_default();
3545 if (ret < 0) {
3546 cerr << "failed to set zonegroup as default: " << cpp_strerror(-ret) << std::endl;
3547 return -ret;
3548 }
3549 }
3550 break;
3551 case OPT_ZONEGROUP_DELETE:
3552 {
3553 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3554 cerr << "no zonegroup name or id provided" << std::endl;
3555 return EINVAL;
3556 }
3557 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3558 int ret = zonegroup.init(g_ceph_context, store);
3559 if (ret < 0) {
3560 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3561 return -ret;
3562 }
3563 ret = zonegroup.delete_obj();
3564 if (ret < 0) {
3565 cerr << "ERROR: couldn't delete zonegroup: " << cpp_strerror(-ret) << std::endl;
3566 return -ret;
3567 }
3568 }
3569 break;
3570 case OPT_ZONEGROUP_GET:
3571 {
3572 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3573 int ret = zonegroup.init(g_ceph_context, store);
3574 if (ret < 0) {
3575 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3576 return -ret;
3577 }
3578
3579 encode_json("zonegroup", zonegroup, formatter);
3580 formatter->flush(cout);
3581 cout << std::endl;
3582 }
3583 break;
3584 case OPT_ZONEGROUP_LIST:
3585 {
3586 RGWZoneGroup zonegroup;
3587 int ret = zonegroup.init(g_ceph_context, store, false);
3588 if (ret < 0) {
3589 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3590 return -ret;
3591 }
3592
3593 list<string> zonegroups;
3594 ret = store->list_zonegroups(zonegroups);
3595 if (ret < 0) {
3596 cerr << "failed to list zonegroups: " << cpp_strerror(-ret) << std::endl;
3597 return -ret;
3598 }
3599 string default_zonegroup;
3600 ret = zonegroup.read_default_id(default_zonegroup);
3601 if (ret < 0 && ret != -ENOENT) {
3602 cerr << "could not determine default zonegroup: " << cpp_strerror(-ret) << std::endl;
3603 }
3604 formatter->open_object_section("zonegroups_list");
3605 encode_json("default_info", default_zonegroup, formatter);
3606 encode_json("zonegroups", zonegroups, formatter);
3607 formatter->close_section();
3608 formatter->flush(cout);
3609 cout << std::endl;
3610 }
3611 break;
3612 case OPT_ZONEGROUP_MODIFY:
3613 {
3614 RGWRealm realm(realm_id, realm_name);
3615 int ret = realm.init(g_ceph_context, store);
3616 if (ret < 0) {
3617 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3618 return -ret;
3619 }
3620
3621 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3622 ret = zonegroup.init(g_ceph_context, store);
3623 if (ret < 0) {
3624 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3625 return -ret;
3626 }
3627
3628 bool need_update = false;
3629
3630 if (!master_zone.empty()) {
3631 zonegroup.master_zone = master_zone;
3632 need_update = true;
3633 }
3634
3635 if (is_master_set) {
3636 zonegroup.update_master(is_master);
3637 need_update = true;
3638 }
3639
3640 if (!endpoints.empty()) {
3641 zonegroup.endpoints = endpoints;
3642 need_update = true;
3643 }
3644
3645 if (!api_name.empty()) {
3646 zonegroup.api_name = api_name;
3647 need_update = true;
3648 }
3649
3650 if (!realm_id.empty()) {
3651 zonegroup.realm_id = realm_id;
3652 need_update = true;
3653 } else if (!realm_name.empty()) {
3654 // get realm id from name
3655 RGWRealm realm{g_ceph_context, store};
3656 ret = realm.read_id(realm_name, zonegroup.realm_id);
3657 if (ret < 0) {
3658 cerr << "failed to find realm by name " << realm_name << std::endl;
3659 return -ret;
3660 }
3661 need_update = true;
3662 }
3663
3664 if (need_update) {
3665 ret = zonegroup.update();
3666 if (ret < 0) {
3667 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
3668 return -ret;
3669 }
3670 }
3671
3672 if (set_default) {
3673 ret = zonegroup.set_as_default();
3674 if (ret < 0) {
3675 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3676 }
3677 }
3678
3679 encode_json("zonegroup", zonegroup, formatter);
3680 formatter->flush(cout);
3681 }
3682 break;
3683 case OPT_ZONEGROUP_SET:
3684 {
3685 RGWRealm realm(realm_id, realm_name);
3686 int ret = realm.init(g_ceph_context, store);
3687 if (ret < 0) {
3688 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
3689 return -ret;
3690 }
3691
3692 RGWZoneGroup zonegroup;
3693 ret = zonegroup.init(g_ceph_context, store, false);
3694 if (ret < 0) {
3695 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3696 return -ret;
3697 }
3698 ret = read_decode_json(infile, zonegroup);
3699 if (ret < 0) {
3700 return 1;
3701 }
3702 if (zonegroup.realm_id.empty()) {
3703 zonegroup.realm_id = realm.get_id();
3704 }
3705 ret = zonegroup.create();
3706 if (ret < 0 && ret != -EEXIST) {
3707 cerr << "ERROR: couldn't create zonegroup info: " << cpp_strerror(-ret) << std::endl;
3708 return 1;
3709 } else if (ret == -EEXIST) {
3710 ret = zonegroup.update();
3711 if (ret < 0) {
3712 cerr << "ERROR: couldn't store zonegroup info: " << cpp_strerror(-ret) << std::endl;
3713 return 1;
3714 }
3715 }
3716
3717 if (set_default) {
3718 ret = zonegroup.set_as_default();
3719 if (ret < 0) {
3720 cerr << "failed to set zonegroup " << zonegroup_name << " as default: " << cpp_strerror(-ret) << std::endl;
3721 }
3722 }
3723
3724 encode_json("zonegroup", zonegroup, formatter);
3725 formatter->flush(cout);
3726 }
3727 break;
3728 case OPT_ZONEGROUP_REMOVE:
3729 {
3730 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3731 int ret = zonegroup.init(g_ceph_context, store);
3732 if (ret < 0) {
3733 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3734 return -ret;
3735 }
3736
3737 if (zone_id.empty()) {
3738 if (zone_name.empty()) {
3739 cerr << "no --zone-id or --rgw-zone name provided" << std::endl;
3740 return EINVAL;
3741 }
3742 // look up zone id by name
3743 for (auto& z : zonegroup.zones) {
3744 if (zone_name == z.second.name) {
3745 zone_id = z.second.id;
3746 break;
3747 }
3748 }
3749 if (zone_id.empty()) {
3750 cerr << "zone name " << zone_name << " not found in zonegroup "
3751 << zonegroup.get_name() << std::endl;
3752 return ENOENT;
3753 }
3754 }
3755
3756 ret = zonegroup.remove_zone(zone_id);
3757 if (ret < 0) {
3758 cerr << "failed to remove zone: " << cpp_strerror(-ret) << std::endl;
3759 return -ret;
3760 }
3761
3762 encode_json("zonegroup", zonegroup, formatter);
3763 formatter->flush(cout);
3764 }
3765 break;
3766 case OPT_ZONEGROUP_RENAME:
3767 {
3768 if (zonegroup_new_name.empty()) {
3769 cerr << " missing zonegroup new name" << std::endl;
3770 return EINVAL;
3771 }
3772 if (zonegroup_id.empty() && zonegroup_name.empty()) {
3773 cerr << "no zonegroup name or id provided" << std::endl;
3774 return EINVAL;
3775 }
3776 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3777 int ret = zonegroup.init(g_ceph_context, store);
3778 if (ret < 0) {
3779 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3780 return -ret;
3781 }
3782 ret = zonegroup.rename(zonegroup_new_name);
3783 if (ret < 0) {
3784 cerr << "failed to rename zonegroup: " << cpp_strerror(-ret) << std::endl;
3785 return -ret;
3786 }
3787 }
3788 break;
3789 case OPT_ZONEGROUP_PLACEMENT_LIST:
3790 {
3791 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3792 int ret = zonegroup.init(g_ceph_context, store);
3793 if (ret < 0) {
3794 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3795 return -ret;
3796 }
3797
3798 encode_json("placement_targets", zonegroup.placement_targets, formatter);
3799 formatter->flush(cout);
3800 cout << std::endl;
3801 }
3802 break;
3803 case OPT_ZONEGROUP_PLACEMENT_ADD:
3804 case OPT_ZONEGROUP_PLACEMENT_MODIFY:
3805 case OPT_ZONEGROUP_PLACEMENT_RM:
3806 case OPT_ZONEGROUP_PLACEMENT_DEFAULT:
3807 {
3808 if (placement_id.empty()) {
3809 cerr << "ERROR: --placement-id not specified" << std::endl;
3810 return EINVAL;
3811 }
3812
3813 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3814 int ret = zonegroup.init(g_ceph_context, store);
3815 if (ret < 0) {
3816 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
3817 return -ret;
3818 }
3819
3820 if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_ADD) {
3821 RGWZoneGroupPlacementTarget target;
3822 target.name = placement_id;
3823 for (auto& t : tags) {
3824 target.tags.insert(t);
3825 }
3826 zonegroup.placement_targets[placement_id] = target;
3827 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_MODIFY) {
3828 RGWZoneGroupPlacementTarget& target = zonegroup.placement_targets[placement_id];
3829 if (!tags.empty()) {
3830 target.tags.clear();
3831 for (auto& t : tags) {
3832 target.tags.insert(t);
3833 }
3834 }
3835 target.name = placement_id;
3836 for (auto& t : tags_rm) {
3837 target.tags.erase(t);
3838 }
3839 for (auto& t : tags_add) {
3840 target.tags.insert(t);
3841 }
3842 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_RM) {
3843 zonegroup.placement_targets.erase(placement_id);
3844 } else if (opt_cmd == OPT_ZONEGROUP_PLACEMENT_DEFAULT) {
3845 if (!zonegroup.placement_targets.count(placement_id)) {
3846 cerr << "failed to find a zonegroup placement target named '"
3847 << placement_id << "'" << std::endl;
3848 return -ENOENT;
3849 }
3850 zonegroup.default_placement = placement_id;
3851 }
3852
3853 zonegroup.post_process_params();
3854 ret = zonegroup.update();
3855 if (ret < 0) {
3856 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
3857 return -ret;
3858 }
3859
3860 encode_json("placement_targets", zonegroup.placement_targets, formatter);
3861 formatter->flush(cout);
3862 }
3863 break;
3864 case OPT_ZONE_CREATE:
3865 {
3866 if (zone_name.empty()) {
3867 cerr << "zone name not provided" << std::endl;
3868 return EINVAL;
3869 }
3870 int ret;
3871 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
3872 /* if the user didn't provide zonegroup info , create stand alone zone */
3873 if (!zonegroup_id.empty() || !zonegroup_name.empty()) {
3874 ret = zonegroup.init(g_ceph_context, store);
3875 if (ret < 0) {
3876 cerr << "unable to initialize zonegroup " << zonegroup_name << ": " << cpp_strerror(-ret) << std::endl;
3877 return -ret;
3878 }
3879 if (realm_id.empty() && realm_name.empty()) {
3880 realm_id = zonegroup.realm_id;
3881 }
3882 }
3883
3884 RGWZoneParams zone(zone_id, zone_name);
3885 ret = zone.init(g_ceph_context, store, false);
3886 if (ret < 0) {
3887 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3888 return -ret;
3889 }
3890
3891 zone.system_key.id = access_key;
3892 zone.system_key.key = secret_key;
3893 zone.realm_id = realm_id;
3894 zone.tier_config = tier_config_add;
3895
3896 ret = zone.create();
3897 if (ret < 0) {
3898 cerr << "failed to create zone " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
3899 return -ret;
3900 }
3901
3902 if (!zonegroup_id.empty() || !zonegroup_name.empty()) {
3903 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
3904 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
3905 ret = zonegroup.add_zone(zone,
3906 (is_master_set ? &is_master : NULL),
3907 (is_read_only_set ? &read_only : NULL),
3908 endpoints,
3909 ptier_type,
3910 psync_from_all,
3911 sync_from, sync_from_rm);
3912 if (ret < 0) {
3913 cerr << "failed to add zone " << zone_name << " to zonegroup " << zonegroup.get_name()
3914 << ": " << cpp_strerror(-ret) << std::endl;
3915 return -ret;
3916 }
3917 }
3918
3919 if (set_default) {
3920 ret = zone.set_as_default();
3921 if (ret < 0) {
3922 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
3923 }
3924 }
3925
3926 encode_json("zone", zone, formatter);
3927 formatter->flush(cout);
3928 cout << std::endl;
3929 }
3930 break;
3931 case OPT_ZONE_DEFAULT:
3932 {
3933 RGWZoneGroup zonegroup(zonegroup_id,zonegroup_name);
3934 int ret = zonegroup.init(g_ceph_context, store);
3935 if (ret < 0) {
3936 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
3937 }
3938 if (zone_id.empty() && zone_name.empty()) {
3939 cerr << "no zone name or id provided" << std::endl;
3940 return EINVAL;
3941 }
3942 RGWZoneParams zone(zone_id, zone_name);
3943 ret = zone.init(g_ceph_context, store);
3944 if (ret < 0) {
3945 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3946 return -ret;
3947 }
3948 ret = zone.set_as_default();
3949 if (ret < 0) {
3950 cerr << "failed to set zone as default: " << cpp_strerror(-ret) << std::endl;
3951 return -ret;
3952 }
3953 }
3954 break;
3955 case OPT_ZONE_DELETE:
3956 {
3957 if (zone_id.empty() && zone_name.empty()) {
3958 cerr << "no zone name or id provided" << std::endl;
3959 return EINVAL;
3960 }
3961 RGWZoneParams zone(zone_id, zone_name);
3962 int ret = zone.init(g_ceph_context, store);
3963 if (ret < 0) {
3964 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
3965 return -ret;
3966 }
3967
3968 list<string> zonegroups;
3969 ret = store->list_zonegroups(zonegroups);
3970 if (ret < 0) {
3971 cerr << "failed to list zonegroups: " << cpp_strerror(-ret) << std::endl;
3972 return -ret;
3973 }
3974
3975 for (list<string>::iterator iter = zonegroups.begin(); iter != zonegroups.end(); ++iter) {
3976 RGWZoneGroup zonegroup(string(), *iter);
3977 int ret = zonegroup.init(g_ceph_context, store);
3978 if (ret < 0) {
3979 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
3980 continue;
3981 }
3982 ret = zonegroup.remove_zone(zone.get_id());
3983 if (ret < 0 && ret != -ENOENT) {
3984 cerr << "failed to remove zone " << zone_name << " from zonegroup " << zonegroup.get_name() << ": "
3985 << cpp_strerror(-ret) << std::endl;
3986 }
3987 }
3988
3989 ret = zone.delete_obj();
3990 if (ret < 0) {
3991 cerr << "failed to delete zone " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
3992 return -ret;
3993 }
3994 }
3995 break;
3996 case OPT_ZONE_GET:
3997 {
3998 RGWZoneParams zone(zone_id, zone_name);
3999 int ret = zone.init(g_ceph_context, store);
4000 if (ret < 0) {
4001 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4002 return -ret;
4003 }
4004 encode_json("zone", zone, formatter);
4005 formatter->flush(cout);
4006 }
4007 break;
4008 case OPT_ZONE_SET:
4009 {
4010 RGWZoneParams zone(zone_name);
4011 int ret = zone.init(g_ceph_context, store, false);
4012 if (ret < 0) {
4013 return -ret;
4014 }
4015
4016 ret = zone.read();
4017 if (ret < 0 && ret != -ENOENT) {
4018 cerr << "zone.read() returned ret=" << ret << std::endl;
4019 return -ret;
4020 }
4021
4022 string orig_id = zone.get_id();
4023
4024 ret = read_decode_json(infile, zone);
4025 if (ret < 0) {
4026 return 1;
4027 }
4028
4029 if(zone.realm_id.empty()) {
4030 RGWRealm realm(realm_id, realm_name);
4031 int ret = realm.init(g_ceph_context, store);
4032 if (ret < 0 && ret != -ENOENT) {
4033 cerr << "failed to init realm: " << cpp_strerror(-ret) << std::endl;
4034 return -ret;
4035 }
4036 zone.realm_id = realm.get_id();
4037 }
4038
4039 if( !zone_name.empty() && !zone.get_name().empty() && zone.get_name() != zone_name) {
4040 cerr << "Error: zone name" << zone_name << " is different than the zone name " << zone.get_name() << " in the provided json " << std::endl;
4041 return EINVAL;
4042 }
4043
4044 if (zone.get_name().empty()) {
4045 zone.set_name(zone_name);
4046 if (zone.get_name().empty()) {
4047 cerr << "no zone name specified" << std::endl;
4048 return EINVAL;
4049 }
4050 }
4051
4052 zone_name = zone.get_name();
4053
4054 if (zone.get_id().empty()) {
4055 zone.set_id(orig_id);
4056 }
4057
4058 if (zone.get_id().empty()) {
4059 cerr << "no zone name id the json provided, assuming old format" << std::endl;
4060 if (zone_name.empty()) {
4061 cerr << "missing zone name" << std::endl;
4062 return EINVAL;
4063 }
4064 zone.set_name(zone_name);
4065 zone.set_id(zone_name);
4066 }
4067
4068 cerr << "zone id " << zone.get_id();
4069 ret = zone.fix_pool_names();
4070 if (ret < 0) {
4071 cerr << "ERROR: couldn't fix zone: " << cpp_strerror(-ret) << std::endl;
4072 return -ret;
4073 }
4074 ret = zone.write(false);
4075 if (ret < 0) {
4076 cerr << "ERROR: couldn't create zone: " << cpp_strerror(-ret) << std::endl;
4077 return 1;
4078 }
4079
4080 if (set_default) {
4081 ret = zone.set_as_default();
4082 if (ret < 0) {
4083 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
4084 }
4085 }
4086
4087 encode_json("zone", zone, formatter);
4088 formatter->flush(cout);
4089 }
4090 break;
4091 case OPT_ZONE_LIST:
4092 {
4093 list<string> zones;
4094 int ret = store->list_zones(zones);
4095 if (ret < 0) {
4096 cerr << "failed to list zones: " << cpp_strerror(-ret) << std::endl;
4097 return -ret;
4098 }
4099
4100 RGWZoneParams zone;
4101 ret = zone.init(g_ceph_context, store, false);
4102 if (ret < 0) {
4103 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4104 return -ret;
4105 }
4106 string default_zone;
4107 ret = zone.read_default_id(default_zone);
4108 if (ret < 0 && ret != -ENOENT) {
4109 cerr << "could not determine default zone: " << cpp_strerror(-ret) << std::endl;
4110 }
4111 formatter->open_object_section("zones_list");
4112 encode_json("default_info", default_zone, formatter);
4113 encode_json("zones", zones, formatter);
4114 formatter->close_section();
4115 formatter->flush(cout);
4116 cout << std::endl;
4117 }
4118 break;
4119 case OPT_ZONE_MODIFY:
4120 {
4121 RGWZoneParams zone(zone_id, zone_name);
4122 int ret = zone.init(g_ceph_context, store);
4123 if (ret < 0) {
4124 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4125 return -ret;
4126 }
4127
4128 bool need_zone_update = false;
4129 if (!access_key.empty()) {
4130 zone.system_key.id = access_key;
4131 need_zone_update = true;
4132 }
4133
4134 if (!secret_key.empty()) {
4135 zone.system_key.key = secret_key;
4136 need_zone_update = true;
4137 }
4138
4139 if (!realm_id.empty()) {
4140 zone.realm_id = realm_id;
4141 need_zone_update = true;
4142 } else if (!realm_name.empty()) {
4143 // get realm id from name
4144 RGWRealm realm{g_ceph_context, store};
4145 ret = realm.read_id(realm_name, zone.realm_id);
4146 if (ret < 0) {
4147 cerr << "failed to find realm by name " << realm_name << std::endl;
4148 return -ret;
4149 }
4150 need_zone_update = true;
4151 }
4152
4153 for (auto add : tier_config_add) {
4154 zone.tier_config[add.first] = add.second;
4155 need_zone_update = true;
4156 }
4157
4158 for (auto rm : tier_config_rm) {
4159 zone.tier_config.erase(rm.first);
4160 need_zone_update = true;
4161 }
4162
4163 if (need_zone_update) {
4164 ret = zone.update();
4165 if (ret < 0) {
4166 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
4167 return -ret;
4168 }
4169 }
4170
4171 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4172 ret = zonegroup.init(g_ceph_context, store);
4173 if (ret < 0) {
4174 cerr << "failed to init zonegroup: " << cpp_strerror(-ret) << std::endl;
4175 return -ret;
4176 }
4177 string *ptier_type = (tier_type_specified ? &tier_type : nullptr);
4178
4179 bool *psync_from_all = (sync_from_all_specified ? &sync_from_all : nullptr);
4180
4181 ret = zonegroup.add_zone(zone,
4182 (is_master_set ? &is_master : NULL),
4183 (is_read_only_set ? &read_only : NULL),
4184 endpoints, ptier_type,
4185 psync_from_all, sync_from, sync_from_rm);
4186 if (ret < 0) {
4187 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
4188 return -ret;
4189 }
4190
4191 ret = zonegroup.update();
4192 if (ret < 0) {
4193 cerr << "failed to update zonegroup: " << cpp_strerror(-ret) << std::endl;
4194 return -ret;
4195 }
4196
4197 if (set_default) {
4198 ret = zone.set_as_default();
4199 if (ret < 0) {
4200 cerr << "failed to set zone " << zone_name << " as default: " << cpp_strerror(-ret) << std::endl;
4201 }
4202 }
4203
4204 encode_json("zone", zone, formatter);
4205 formatter->flush(cout);
4206 }
4207 break;
4208 case OPT_ZONE_RENAME:
4209 {
4210 if (zone_new_name.empty()) {
4211 cerr << " missing zone new name" << std::endl;
4212 return EINVAL;
4213 }
4214 if (zone_id.empty() && zone_name.empty()) {
4215 cerr << "no zonegroup name or id provided" << std::endl;
4216 return EINVAL;
4217 }
4218 RGWZoneParams zone(zone_id,zone_name);
4219 int ret = zone.init(g_ceph_context, store);
4220 if (ret < 0) {
4221 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4222 return -ret;
4223 }
4224 ret = zone.rename(zone_new_name);
4225 if (ret < 0) {
4226 cerr << "failed to rename zone " << zone_name << " to " << zone_new_name << ": " << cpp_strerror(-ret)
4227 << std::endl;
4228 return -ret;
4229 }
4230 RGWZoneGroup zonegroup(zonegroup_id, zonegroup_name);
4231 ret = zonegroup.init(g_ceph_context, store);
4232 if (ret < 0) {
4233 cerr << "WARNING: failed to initialize zonegroup " << zonegroup_name << std::endl;
4234 } else {
4235 ret = zonegroup.rename_zone(zone);
4236 if (ret < 0 && ret ) {
4237 cerr << "Error in zonegroup rename for " << zone_name << ": " << cpp_strerror(-ret) << std::endl;
4238 return -ret;
4239 }
4240 }
4241 }
4242 break;
4243 case OPT_ZONE_PLACEMENT_ADD:
4244 case OPT_ZONE_PLACEMENT_MODIFY:
4245 case OPT_ZONE_PLACEMENT_RM:
4246 {
4247 if (placement_id.empty()) {
4248 cerr << "ERROR: --placement-id not specified" << std::endl;
4249 return EINVAL;
4250 }
4251 RGWZoneParams zone(zone_id, zone_name);
4252 int ret = zone.init(g_ceph_context, store);
4253 if (ret < 0) {
4254 cerr << "failed to init zone: " << cpp_strerror(-ret) << std::endl;
4255 return -ret;
4256 }
4257
4258 if (opt_cmd == OPT_ZONE_PLACEMENT_ADD) {
4259 // pool names are required
4260 if (!index_pool || index_pool->empty() ||
4261 !data_pool || data_pool->empty()) {
4262 cerr << "ERROR: need to specify both --index-pool and --data-pool" << std::endl;
4263 return EINVAL;
4264 }
4265
4266 RGWZonePlacementInfo& info = zone.placement_pools[placement_id];
4267
4268 info.index_pool = *index_pool;
4269 info.data_pool = *data_pool;
4270 if (data_extra_pool) {
4271 info.data_extra_pool = *data_extra_pool;
4272 }
4273 if (index_type_specified) {
4274 info.index_type = placement_index_type;
4275 }
4276 if (compression_type) {
4277 info.compression_type = *compression_type;
4278 }
4279 } else if (opt_cmd == OPT_ZONE_PLACEMENT_MODIFY) {
4280 auto p = zone.placement_pools.find(placement_id);
4281 if (p == zone.placement_pools.end()) {
4282 cerr << "ERROR: zone placement target '" << placement_id
4283 << "' not found" << std::endl;
4284 return -ENOENT;
4285 }
4286 auto& info = p->second;
4287 if (index_pool && !index_pool->empty()) {
4288 info.index_pool = *index_pool;
4289 }
4290 if (data_pool && !data_pool->empty()) {
4291 info.data_pool = *data_pool;
4292 }
4293 if (data_extra_pool) {
4294 info.data_extra_pool = *data_extra_pool;
4295 }
4296 if (index_type_specified) {
4297 info.index_type = placement_index_type;
4298 }
4299 if (compression_type) {
4300 info.compression_type = *compression_type;
4301 }
4302 } else if (opt_cmd == OPT_ZONE_PLACEMENT_RM) {
4303 zone.placement_pools.erase(placement_id);
4304 }
4305
4306 ret = zone.update();
4307 if (ret < 0) {
4308 cerr << "failed to save zone info: " << cpp_strerror(-ret) << std::endl;
4309 return -ret;
4310 }
4311
4312 encode_json("zone", zone, formatter);
4313 formatter->flush(cout);
4314 }
4315 break;
4316 case OPT_ZONE_PLACEMENT_LIST:
4317 {
4318 RGWZoneParams zone(zone_id, zone_name);
4319 int ret = zone.init(g_ceph_context, store);
4320 if (ret < 0) {
4321 cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
4322 return -ret;
4323 }
4324 encode_json("placement_pools", zone.placement_pools, formatter);
4325 formatter->flush(cout);
4326 }
4327 break;
4328 }
4329 return 0;
4330 }
4331
4332 if (!user_id.empty()) {
4333 user_op.set_user_id(user_id);
4334 bucket_op.set_user_id(user_id);
4335 }
4336
4337 if (!display_name.empty())
4338 user_op.set_display_name(display_name);
4339
4340 if (!user_email.empty())
4341 user_op.set_user_email(user_email);
4342
4343 if (!access_key.empty())
4344 user_op.set_access_key(access_key);
4345
4346 if (!secret_key.empty())
4347 user_op.set_secret_key(secret_key);
4348
4349 if (!subuser.empty())
4350 user_op.set_subuser(subuser);
4351
4352 if (!caps.empty())
4353 user_op.set_caps(caps);
4354
4355 user_op.set_purge_data(purge_data);
4356
4357 if (purge_keys)
4358 user_op.set_purge_keys();
4359
4360 if (gen_access_key)
4361 user_op.set_generate_key();
4362
4363 if (gen_secret_key)
4364 user_op.set_gen_secret(); // assume that a key pair should be created
4365
4366 if (max_buckets_specified)
4367 user_op.set_max_buckets(max_buckets);
4368
4369 if (admin_specified)
4370 user_op.set_admin(admin);
4371
4372 if (system_specified)
4373 user_op.set_system(system);
4374
4375 if (set_perm)
4376 user_op.set_perm(perm_mask);
4377
4378 if (set_temp_url_key) {
4379 map<int, string>::iterator iter = temp_url_keys.begin();
4380 for (; iter != temp_url_keys.end(); ++iter) {
4381 user_op.set_temp_url_key(iter->second, iter->first);
4382 }
4383 }
4384
4385 if (!op_mask_str.empty()) {
4386 uint32_t op_mask;
4387 int ret = rgw_parse_op_type_list(op_mask_str, &op_mask);
4388 if (ret < 0) {
4389 cerr << "failed to parse op_mask: " << cpp_strerror(-ret) << std::endl;
4390 return -ret;
4391 }
4392
4393 user_op.set_op_mask(op_mask);
4394 }
4395
4396 if (key_type != KEY_TYPE_UNDEFINED)
4397 user_op.set_key_type(key_type);
4398
4399 // set suspension operation parameters
4400 if (opt_cmd == OPT_USER_ENABLE)
4401 user_op.set_suspension(false);
4402 else if (opt_cmd == OPT_USER_SUSPEND)
4403 user_op.set_suspension(true);
4404
4405 // RGWUser to use for user operations
4406 RGWUser user;
4407 int ret = 0;
4408 if (!user_id.empty() || !subuser.empty()) {
4409 ret = user.init(store, user_op);
4410 if (ret < 0) {
4411 cerr << "user.init failed: " << cpp_strerror(-ret) << std::endl;
4412 return -ret;
4413 }
4414 }
4415
4416 /* populate bucket operation */
4417 bucket_op.set_bucket_name(bucket_name);
4418 bucket_op.set_object(object);
4419 bucket_op.set_check_objects(check_objects);
4420 bucket_op.set_delete_children(delete_child_objects);
4421 bucket_op.set_fix_index(fix);
4422 bucket_op.set_max_aio(max_concurrent_ios);
4423
4424 // required to gather errors from operations
4425 std::string err_msg;
4426
4427 bool output_user_info = true;
4428
4429 switch (opt_cmd) {
4430 case OPT_USER_INFO:
4431 break;
4432 case OPT_USER_CREATE:
4433 if (!user_op.has_existing_user()) {
4434 user_op.set_generate_key(); // generate a new key by default
4435 }
4436 ret = user.add(user_op, &err_msg);
4437 if (ret < 0) {
4438 cerr << "could not create user: " << err_msg << std::endl;
4439 if (ret == -ERR_INVALID_TENANT_NAME)
4440 ret = -EINVAL;
4441
4442 return -ret;
4443 }
4444 if (!subuser.empty()) {
4445 ret = user.subusers.add(user_op, &err_msg);
4446 if (ret < 0) {
4447 cerr << "could not create subuser: " << err_msg << std::endl;
4448 return -ret;
4449 }
4450 }
4451 break;
4452 case OPT_USER_RM:
4453 ret = user.remove(user_op, &err_msg);
4454 if (ret < 0) {
4455 cerr << "could not remove user: " << err_msg << std::endl;
4456 return -ret;
4457 }
4458
4459 output_user_info = false;
4460 break;
4461 case OPT_USER_ENABLE:
4462 case OPT_USER_SUSPEND:
4463 case OPT_USER_MODIFY:
4464 ret = user.modify(user_op, &err_msg);
4465 if (ret < 0) {
4466 cerr << "could not modify user: " << err_msg << std::endl;
4467 return -ret;
4468 }
4469
4470 break;
4471 case OPT_SUBUSER_CREATE:
4472 ret = user.subusers.add(user_op, &err_msg);
4473 if (ret < 0) {
4474 cerr << "could not create subuser: " << err_msg << std::endl;
4475 return -ret;
4476 }
4477
4478 break;
4479 case OPT_SUBUSER_MODIFY:
4480 ret = user.subusers.modify(user_op, &err_msg);
4481 if (ret < 0) {
4482 cerr << "could not modify subuser: " << err_msg << std::endl;
4483 return -ret;
4484 }
4485
4486 break;
4487 case OPT_SUBUSER_RM:
4488 ret = user.subusers.remove(user_op, &err_msg);
4489 if (ret < 0) {
4490 cerr << "could not remove subuser: " << err_msg << std::endl;
4491 return -ret;
4492 }
4493
4494 break;
4495 case OPT_CAPS_ADD:
4496 ret = user.caps.add(user_op, &err_msg);
4497 if (ret < 0) {
4498 cerr << "could not add caps: " << err_msg << std::endl;
4499 return -ret;
4500 }
4501
4502 break;
4503 case OPT_CAPS_RM:
4504 ret = user.caps.remove(user_op, &err_msg);
4505 if (ret < 0) {
4506 cerr << "could not remove caps: " << err_msg << std::endl;
4507 return -ret;
4508 }
4509
4510 break;
4511 case OPT_KEY_CREATE:
4512 ret = user.keys.add(user_op, &err_msg);
4513 if (ret < 0) {
4514 cerr << "could not create key: " << err_msg << std::endl;
4515 return -ret;
4516 }
4517
4518 break;
4519 case OPT_KEY_RM:
4520 ret = user.keys.remove(user_op, &err_msg);
4521 if (ret < 0) {
4522 cerr << "could not remove key: " << err_msg << std::endl;
4523 return -ret;
4524 }
4525 break;
4526 case OPT_PERIOD_PUSH:
4527 {
4528 RGWEnv env;
4529 req_info info(g_ceph_context, &env);
4530 info.method = "POST";
4531 info.request_uri = "/admin/realm/period";
4532
4533 map<string, string> &params = info.args.get_params();
4534 if (!realm_id.empty())
4535 params["realm_id"] = realm_id;
4536 if (!realm_name.empty())
4537 params["realm_name"] = realm_name;
4538 if (!period_id.empty())
4539 params["period_id"] = period_id;
4540 if (!period_epoch.empty())
4541 params["epoch"] = period_epoch;
4542
4543 // load the period
4544 RGWPeriod period(period_id);
4545 int ret = period.init(g_ceph_context, store);
4546 if (ret < 0) {
4547 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
4548 return -ret;
4549 }
4550 // json format into a bufferlist
4551 JSONFormatter jf(false);
4552 encode_json("period", period, &jf);
4553 bufferlist bl;
4554 jf.flush(bl);
4555
4556 JSONParser p;
4557 ret = send_to_remote_or_url(nullptr, url, access_key, secret_key,
4558 info, bl, p);
4559 if (ret < 0) {
4560 cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
4561 return -ret;
4562 }
4563 }
4564 return 0;
4565 case OPT_PERIOD_UPDATE:
4566 {
4567 int ret = update_period(realm_id, realm_name, period_id, period_epoch,
4568 commit, remote, url, access_key, secret_key,
4569 formatter, yes_i_really_mean_it);
4570 if (ret < 0) {
4571 return -ret;
4572 }
4573 }
4574 return 0;
4575 case OPT_PERIOD_COMMIT:
4576 {
4577 // read realm and staging period
4578 RGWRealm realm(realm_id, realm_name);
4579 int ret = realm.init(g_ceph_context, store);
4580 if (ret < 0) {
4581 cerr << "Error initializing realm: " << cpp_strerror(-ret) << std::endl;
4582 return -ret;
4583 }
4584 RGWPeriod period(RGWPeriod::get_staging_id(realm.get_id()), 1);
4585 ret = period.init(g_ceph_context, store, realm.get_id());
4586 if (ret < 0) {
4587 cerr << "period init failed: " << cpp_strerror(-ret) << std::endl;
4588 return -ret;
4589 }
4590 ret = commit_period(realm, period, remote, url, access_key, secret_key,
4591 yes_i_really_mean_it);
4592 if (ret < 0) {
4593 cerr << "failed to commit period: " << cpp_strerror(-ret) << std::endl;
4594 return -ret;
4595 }
4596
4597 encode_json("period", period, formatter);
4598 formatter->flush(cout);
4599 cout << std::endl;
4600 }
4601 return 0;
4602 case OPT_ROLE_CREATE:
4603 {
4604 if (role_name.empty()) {
4605 cerr << "ERROR: role name is empty" << std::endl;
4606 return -EINVAL;
4607 }
4608
4609 if (assume_role_doc.empty()) {
4610 cerr << "ERROR: assume role policy document is empty" << std::endl;
4611 return -EINVAL;
4612 }
4613 /* The following two calls will be replaced by read_decode_json or something
4614 similar when the code for AWS Policies is in places */
4615 bufferlist bl;
4616 int ret = read_input(assume_role_doc, bl);
4617 if (ret < 0) {
4618 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4619 return ret;
4620 }
4621 JSONParser p;
4622 if (!p.parse(bl.c_str(), bl.length())) {
4623 cout << "ERROR: failed to parse JSON: " << assume_role_doc << std::endl;
4624 return -EINVAL;
4625 }
4626 string trust_policy = bl.to_str();
4627 RGWRole role(g_ceph_context, store, role_name, path, trust_policy, tenant);
4628 ret = role.create(true);
4629 if (ret < 0) {
4630 return -ret;
4631 }
4632 show_role_info(role, formatter);
4633 return 0;
4634 }
4635 case OPT_ROLE_DELETE:
4636 {
4637 if (role_name.empty()) {
4638 cerr << "ERROR: empty role name" << std::endl;
4639 return -EINVAL;
4640 }
4641 RGWRole role(g_ceph_context, store, role_name, tenant);
4642 ret = role.delete_obj();
4643 if (ret < 0) {
4644 return -ret;
4645 }
4646 cout << "role: " << role_name << " successfully deleted" << std::endl;
4647 return 0;
4648 }
4649 case OPT_ROLE_GET:
4650 {
4651 if (role_name.empty()) {
4652 cerr << "ERROR: empty role name" << std::endl;
4653 return -EINVAL;
4654 }
4655 RGWRole role(g_ceph_context, store, role_name, tenant);
4656 ret = role.get();
4657 if (ret < 0) {
4658 return -ret;
4659 }
4660 show_role_info(role, formatter);
4661 return 0;
4662 }
4663 case OPT_ROLE_MODIFY:
4664 {
4665 if (role_name.empty()) {
4666 cerr << "ERROR: role name is empty" << std::endl;
4667 return -EINVAL;
4668 }
4669
4670 if (assume_role_doc.empty()) {
4671 cerr << "ERROR: assume role policy document is empty" << std::endl;
4672 return -EINVAL;
4673 }
4674
4675 /* The following two calls will be replaced by read_decode_json or something
4676 similar when the code for AWS Policies is in place */
4677 bufferlist bl;
4678 int ret = read_input(assume_role_doc, bl);
4679 if (ret < 0) {
4680 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4681 return ret;
4682 }
4683 JSONParser p;
4684 if (!p.parse(bl.c_str(), bl.length())) {
4685 cout << "ERROR: failed to parse JSON: " << assume_role_doc << std::endl;
4686 return -EINVAL;
4687 }
4688 string trust_policy = bl.to_str();
4689 RGWRole role(g_ceph_context, store, role_name, tenant);
4690 ret = role.get();
4691 if (ret < 0) {
4692 return -ret;
4693 }
4694 role.update_trust_policy(trust_policy);
4695 ret = role.update();
4696 if (ret < 0) {
4697 return -ret;
4698 }
4699 cout << "Assume role policy document updated successfully for role: " << role_name << std::endl;
4700 return 0;
4701 }
4702 case OPT_ROLE_LIST:
4703 {
4704 vector<RGWRole> result;
4705 ret = RGWRole::get_roles_by_path_prefix(store, g_ceph_context, path_prefix, tenant, result);
4706 if (ret < 0) {
4707 return -ret;
4708 }
4709 show_roles_info(result, formatter);
4710 return 0;
4711 }
4712 case OPT_ROLE_POLICY_PUT:
4713 {
4714 if (role_name.empty()) {
4715 cerr << "role name is empty" << std::endl;
4716 return -EINVAL;
4717 }
4718
4719 if (policy_name.empty()) {
4720 cerr << "policy name is empty" << std::endl;
4721 return -EINVAL;
4722 }
4723
4724 if (perm_policy_doc.empty()) {
4725 cerr << "permission policy document is empty" << std::endl;
4726 return -EINVAL;
4727 }
4728
4729 /* The following two calls will be replaced by read_decode_json or something
4730 similar, when code for AWS Policies is in place.*/
4731 bufferlist bl;
4732 int ret = read_input(perm_policy_doc, bl);
4733 if (ret < 0) {
4734 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
4735 return ret;
4736 }
4737 JSONParser p;
4738 if (!p.parse(bl.c_str(), bl.length())) {
4739 cout << "ERROR: failed to parse JSON: " << std::endl;
4740 return -EINVAL;
4741 }
4742 string perm_policy;
4743 perm_policy = bl.c_str();
4744
4745 RGWRole role(g_ceph_context, store, role_name, tenant);
4746 ret = role.get();
4747 if (ret < 0) {
4748 return -ret;
4749 }
4750 role.set_perm_policy(policy_name, perm_policy);
4751 ret = role.update();
4752 if (ret < 0) {
4753 return -ret;
4754 }
4755 cout << "Permission policy attached successfully" << std::endl;
4756 return 0;
4757 }
4758 case OPT_ROLE_POLICY_LIST:
4759 {
4760 if (role_name.empty()) {
4761 cerr << "ERROR: Role name is empty" << std::endl;
4762 return -EINVAL;
4763 }
4764 RGWRole role(g_ceph_context, store, role_name, tenant);
4765 ret = role.get();
4766 if (ret < 0) {
4767 return -ret;
4768 }
4769 std::vector<string> policy_names = role.get_role_policy_names();
4770 show_policy_names(policy_names, formatter);
4771 return 0;
4772 }
4773 case OPT_ROLE_POLICY_GET:
4774 {
4775 if (role_name.empty()) {
4776 cerr << "ERROR: role name is empty" << std::endl;
4777 return -EINVAL;
4778 }
4779
4780 if (policy_name.empty()) {
4781 cerr << "ERROR: policy name is empty" << std::endl;
4782 return -EINVAL;
4783 }
4784 RGWRole role(g_ceph_context, store, role_name, tenant);
4785 int ret = role.get();
4786 if (ret < 0) {
4787 return -ret;
4788 }
4789 string perm_policy;
4790 ret = role.get_role_policy(policy_name, perm_policy);
4791 if (ret < 0) {
4792 return -ret;
4793 }
4794 show_perm_policy(perm_policy, formatter);
4795 return 0;
4796 }
4797 case OPT_ROLE_POLICY_DELETE:
4798 {
4799 if (role_name.empty()) {
4800 cerr << "ERROR: role name is empty" << std::endl;
4801 return -EINVAL;
4802 }
4803
4804 if (policy_name.empty()) {
4805 cerr << "ERROR: policy name is empty" << std::endl;
4806 return -EINVAL;
4807 }
4808 RGWRole role(g_ceph_context, store, role_name, tenant);
4809 ret = role.get();
4810 if (ret < 0) {
4811 return -ret;
4812 }
4813 ret = role.delete_policy(policy_name);
4814 if (ret < 0) {
4815 return -ret;
4816 }
4817 ret = role.update();
4818 if (ret < 0) {
4819 return -ret;
4820 }
4821 cout << "Policy: " << policy_name << " successfully deleted for role: "
4822 << role_name << std::endl;
4823 return 0;
4824 }
4825 default:
4826 output_user_info = false;
4827 }
4828
4829 // output the result of a user operation
4830 if (output_user_info) {
4831 ret = user.info(info, &err_msg);
4832 if (ret < 0) {
4833 cerr << "could not fetch user info: " << err_msg << std::endl;
4834 return -ret;
4835 }
4836 show_user_info(info, formatter);
4837 }
4838
4839 if (opt_cmd == OPT_POLICY) {
4840 if (format == "xml") {
4841 int ret = RGWBucketAdminOp::dump_s3_policy(store, bucket_op, cout);
4842 if (ret < 0) {
4843 cerr << "ERROR: failed to get policy: " << cpp_strerror(-ret) << std::endl;
4844 return -ret;
4845 }
4846 } else {
4847 int ret = RGWBucketAdminOp::get_policy(store, bucket_op, f);
4848 if (ret < 0) {
4849 cerr << "ERROR: failed to get policy: " << cpp_strerror(-ret) << std::endl;
4850 return -ret;
4851 }
4852 }
4853 }
4854
4855 if (opt_cmd == OPT_BUCKET_LIMIT_CHECK) {
4856 void *handle;
4857 std::list<std::string> user_ids;
4858 metadata_key = "user";
4859 int max = 1000;
4860
4861 bool truncated;
4862
4863 if (! user_id.empty()) {
4864 user_ids.push_back(user_id.id);
4865 ret =
4866 RGWBucketAdminOp::limit_check(store, bucket_op, user_ids, f,
4867 warnings_only);
4868 } else {
4869 /* list users in groups of max-keys, then perform user-bucket
4870 * limit-check on each group */
4871 ret = store->meta_mgr->list_keys_init(metadata_key, &handle);
4872 if (ret < 0) {
4873 cerr << "ERROR: buckets limit check can't get user metadata_key: "
4874 << cpp_strerror(-ret) << std::endl;
4875 return -ret;
4876 }
4877
4878 do {
4879 ret = store->meta_mgr->list_keys_next(handle, max, user_ids,
4880 &truncated);
4881 if (ret < 0 && ret != -ENOENT) {
4882 cerr << "ERROR: buckets limit check lists_keys_next(): "
4883 << cpp_strerror(-ret) << std::endl;
4884 break;
4885 } else {
4886 /* ok, do the limit checks for this group */
4887 ret =
4888 RGWBucketAdminOp::limit_check(store, bucket_op, user_ids, f,
4889 warnings_only);
4890 if (ret < 0)
4891 break;
4892 }
4893 user_ids.clear();
4894 } while (truncated);
4895 store->meta_mgr->list_keys_complete(handle);
4896 }
4897 return -ret;
4898 } /* OPT_BUCKET_LIMIT_CHECK */
4899
4900 if (opt_cmd == OPT_BUCKETS_LIST) {
4901 if (bucket_name.empty()) {
4902 RGWBucketAdminOp::info(store, bucket_op, f);
4903 } else {
4904 RGWBucketInfo bucket_info;
4905 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
4906 if (ret < 0) {
4907 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
4908 return -ret;
4909 }
4910 formatter->open_array_section("entries");
4911 bool truncated;
4912 int count = 0;
4913 if (max_entries < 0)
4914 max_entries = 1000;
4915
4916 string prefix;
4917 string delim;
4918 vector<rgw_bucket_dir_entry> result;
4919 map<string, bool> common_prefixes;
4920 string ns;
4921
4922 RGWRados::Bucket target(store, bucket_info);
4923 RGWRados::Bucket::List list_op(&target);
4924
4925 list_op.params.prefix = prefix;
4926 list_op.params.delim = delim;
4927 list_op.params.marker = rgw_obj_key(marker);
4928 list_op.params.ns = ns;
4929 list_op.params.enforce_ns = false;
4930 list_op.params.list_versions = true;
4931
4932 do {
4933 ret = list_op.list_objects(max_entries - count, &result, &common_prefixes, &truncated);
4934 if (ret < 0) {
4935 cerr << "ERROR: store->list_objects(): " << cpp_strerror(-ret) << std::endl;
4936 return -ret;
4937 }
4938
4939 count += result.size();
4940
4941 for (vector<rgw_bucket_dir_entry>::iterator iter = result.begin(); iter != result.end(); ++iter) {
4942 rgw_bucket_dir_entry& entry = *iter;
4943 encode_json("entry", entry, formatter);
4944 }
4945 formatter->flush(cout);
4946 } while (truncated && count < max_entries);
4947
4948 formatter->close_section();
4949 formatter->flush(cout);
4950 } /* have bucket_name */
4951 } /* OPT_BUCKETS_LIST */
4952
4953 if (opt_cmd == OPT_BUCKET_STATS) {
4954 bucket_op.set_fetch_stats(true);
4955
4956 int r = RGWBucketAdminOp::info(store, bucket_op, f);
4957 if (r < 0) {
4958 cerr << "failure: " << cpp_strerror(-r) << ": " << err << std::endl;
4959 return -r;
4960 }
4961 }
4962
4963 if (opt_cmd == OPT_BUCKET_LINK) {
4964 bucket_op.set_bucket_id(bucket_id);
4965 string err;
4966 int r = RGWBucketAdminOp::link(store, bucket_op, &err);
4967 if (r < 0) {
4968 cerr << "failure: " << cpp_strerror(-r) << ": " << err << std::endl;
4969 return -r;
4970 }
4971 }
4972
4973 if (opt_cmd == OPT_BUCKET_UNLINK) {
4974 int r = RGWBucketAdminOp::unlink(store, bucket_op);
4975 if (r < 0) {
4976 cerr << "failure: " << cpp_strerror(-r) << std::endl;
4977 return -r;
4978 }
4979 }
4980
4981 if (opt_cmd == OPT_LOG_LIST) {
4982 // filter by date?
4983 if (date.size() && date.size() != 10) {
4984 cerr << "bad date format for '" << date << "', expect YYYY-MM-DD" << std::endl;
4985 return EINVAL;
4986 }
4987
4988 formatter->reset();
4989 formatter->open_array_section("logs");
4990 RGWAccessHandle h;
4991 int r = store->log_list_init(date, &h);
4992 if (r == -ENOENT) {
4993 // no logs.
4994 } else {
4995 if (r < 0) {
4996 cerr << "log list: error " << r << std::endl;
4997 return -r;
4998 }
4999 while (true) {
5000 string name;
5001 int r = store->log_list_next(h, &name);
5002 if (r == -ENOENT)
5003 break;
5004 if (r < 0) {
5005 cerr << "log list: error " << r << std::endl;
5006 return -r;
5007 }
5008 formatter->dump_string("object", name);
5009 }
5010 }
5011 formatter->close_section();
5012 formatter->flush(cout);
5013 cout << std::endl;
5014 }
5015
5016 if (opt_cmd == OPT_LOG_SHOW || opt_cmd == OPT_LOG_RM) {
5017 if (object.empty() && (date.empty() || bucket_name.empty() || bucket_id.empty())) {
5018 cerr << "specify an object or a date, bucket and bucket-id" << std::endl;
5019 usage();
5020 assert(false);
5021 }
5022
5023 string oid;
5024 if (!object.empty()) {
5025 oid = object;
5026 } else {
5027 oid = date;
5028 oid += "-";
5029 oid += bucket_id;
5030 oid += "-";
5031 oid += bucket_name;
5032 }
5033
5034 if (opt_cmd == OPT_LOG_SHOW) {
5035 RGWAccessHandle h;
5036
5037 int r = store->log_show_init(oid, &h);
5038 if (r < 0) {
5039 cerr << "error opening log " << oid << ": " << cpp_strerror(-r) << std::endl;
5040 return -r;
5041 }
5042
5043 formatter->reset();
5044 formatter->open_object_section("log");
5045
5046 struct rgw_log_entry entry;
5047
5048 // peek at first entry to get bucket metadata
5049 r = store->log_show_next(h, &entry);
5050 if (r < 0) {
5051 cerr << "error reading log " << oid << ": " << cpp_strerror(-r) << std::endl;
5052 return -r;
5053 }
5054 formatter->dump_string("bucket_id", entry.bucket_id);
5055 formatter->dump_string("bucket_owner", entry.bucket_owner.to_str());
5056 formatter->dump_string("bucket", entry.bucket);
5057
5058 uint64_t agg_time = 0;
5059 uint64_t agg_bytes_sent = 0;
5060 uint64_t agg_bytes_received = 0;
5061 uint64_t total_entries = 0;
5062
5063 if (show_log_entries)
5064 formatter->open_array_section("log_entries");
5065
5066 do {
5067 uint64_t total_time = entry.total_time.sec() * 1000000LL + entry.total_time.usec();
5068
5069 agg_time += total_time;
5070 agg_bytes_sent += entry.bytes_sent;
5071 agg_bytes_received += entry.bytes_received;
5072 total_entries++;
5073
5074 if (skip_zero_entries && entry.bytes_sent == 0 &&
5075 entry.bytes_received == 0)
5076 goto next;
5077
5078 if (show_log_entries) {
5079
5080 rgw_format_ops_log_entry(entry, formatter);
5081 formatter->flush(cout);
5082 }
5083 next:
5084 r = store->log_show_next(h, &entry);
5085 } while (r > 0);
5086
5087 if (r < 0) {
5088 cerr << "error reading log " << oid << ": " << cpp_strerror(-r) << std::endl;
5089 return -r;
5090 }
5091 if (show_log_entries)
5092 formatter->close_section();
5093
5094 if (show_log_sum) {
5095 formatter->open_object_section("log_sum");
5096 formatter->dump_int("bytes_sent", agg_bytes_sent);
5097 formatter->dump_int("bytes_received", agg_bytes_received);
5098 formatter->dump_int("total_time", agg_time);
5099 formatter->dump_int("total_entries", total_entries);
5100 formatter->close_section();
5101 }
5102 formatter->close_section();
5103 formatter->flush(cout);
5104 cout << std::endl;
5105 }
5106 if (opt_cmd == OPT_LOG_RM) {
5107 int r = store->log_remove(oid);
5108 if (r < 0) {
5109 cerr << "error removing log " << oid << ": " << cpp_strerror(-r) << std::endl;
5110 return -r;
5111 }
5112 }
5113 }
5114
5115 if (opt_cmd == OPT_POOL_ADD) {
5116 if (pool_name.empty()) {
5117 cerr << "need to specify pool to add!" << std::endl;
5118 usage();
5119 assert(false);
5120 }
5121
5122 int ret = store->add_bucket_placement(pool);
5123 if (ret < 0)
5124 cerr << "failed to add bucket placement: " << cpp_strerror(-ret) << std::endl;
5125 }
5126
5127 if (opt_cmd == OPT_POOL_RM) {
5128 if (pool_name.empty()) {
5129 cerr << "need to specify pool to remove!" << std::endl;
5130 usage();
5131 assert(false);
5132 }
5133
5134 int ret = store->remove_bucket_placement(pool);
5135 if (ret < 0)
5136 cerr << "failed to remove bucket placement: " << cpp_strerror(-ret) << std::endl;
5137 }
5138
5139 if (opt_cmd == OPT_POOLS_LIST) {
5140 set<rgw_pool> pools;
5141 int ret = store->list_placement_set(pools);
5142 if (ret < 0) {
5143 cerr << "could not list placement set: " << cpp_strerror(-ret) << std::endl;
5144 return -ret;
5145 }
5146 formatter->reset();
5147 formatter->open_array_section("pools");
5148 for (auto siter = pools.begin(); siter != pools.end(); ++siter) {
5149 formatter->open_object_section("pool");
5150 formatter->dump_string("name", siter->to_str());
5151 formatter->close_section();
5152 }
5153 formatter->close_section();
5154 formatter->flush(cout);
5155 cout << std::endl;
5156 }
5157
5158 if (opt_cmd == OPT_USAGE_SHOW) {
5159 uint64_t start_epoch = 0;
5160 uint64_t end_epoch = (uint64_t)-1;
5161
5162 int ret;
5163
5164 if (!start_date.empty()) {
5165 ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5166 if (ret < 0) {
5167 cerr << "ERROR: failed to parse start date" << std::endl;
5168 return 1;
5169 }
5170 }
5171 if (!end_date.empty()) {
5172 ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5173 if (ret < 0) {
5174 cerr << "ERROR: failed to parse end date" << std::endl;
5175 return 1;
5176 }
5177 }
5178
5179
5180 ret = RGWUsage::show(store, user_id, start_epoch, end_epoch,
5181 show_log_entries, show_log_sum, &categories,
5182 f);
5183 if (ret < 0) {
5184 cerr << "ERROR: failed to show usage" << std::endl;
5185 return 1;
5186 }
5187 }
5188
5189 if (opt_cmd == OPT_USAGE_TRIM) {
5190 if (user_id.empty() && !yes_i_really_mean_it) {
5191 cerr << "usage trim without user specified will remove *all* users data" << std::endl;
5192 cerr << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
5193 return 1;
5194 }
5195 int ret;
5196 uint64_t start_epoch = 0;
5197 uint64_t end_epoch = (uint64_t)-1;
5198
5199
5200 if (!start_date.empty()) {
5201 ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5202 if (ret < 0) {
5203 cerr << "ERROR: failed to parse start date" << std::endl;
5204 return 1;
5205 }
5206 }
5207
5208 if (!end_date.empty()) {
5209 ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5210 if (ret < 0) {
5211 cerr << "ERROR: failed to parse end date" << std::endl;
5212 return 1;
5213 }
5214 }
5215
5216 ret = RGWUsage::trim(store, user_id, start_epoch, end_epoch);
5217 if (ret < 0) {
5218 cerr << "ERROR: read_usage() returned ret=" << ret << std::endl;
5219 return 1;
5220 }
5221 }
5222
5223 if (opt_cmd == OPT_OLH_GET || opt_cmd == OPT_OLH_READLOG) {
5224 if (bucket_name.empty()) {
5225 cerr << "ERROR: bucket not specified" << std::endl;
5226 return EINVAL;
5227 }
5228 if (object.empty()) {
5229 cerr << "ERROR: object not specified" << std::endl;
5230 return EINVAL;
5231 }
5232 }
5233
5234 if (opt_cmd == OPT_OLH_GET) {
5235 RGWBucketInfo bucket_info;
5236 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5237 if (ret < 0) {
5238 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5239 return -ret;
5240 }
5241 RGWOLHInfo olh;
5242 rgw_obj obj(bucket, object);
5243 ret = store->get_olh(bucket_info, obj, &olh);
5244 if (ret < 0) {
5245 cerr << "ERROR: failed reading olh: " << cpp_strerror(-ret) << std::endl;
5246 return -ret;
5247 }
5248 encode_json("olh", olh, formatter);
5249 formatter->flush(cout);
5250 }
5251
5252 if (opt_cmd == OPT_OLH_READLOG) {
5253 RGWBucketInfo bucket_info;
5254 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5255 if (ret < 0) {
5256 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5257 return -ret;
5258 }
5259 map<uint64_t, vector<rgw_bucket_olh_log_entry> > log;
5260 bool is_truncated;
5261
5262 RGWObjectCtx rctx(store);
5263 rgw_obj obj(bucket, object);
5264
5265 RGWObjState *state;
5266
5267 ret = store->get_obj_state(&rctx, bucket_info, obj, &state, false); /* don't follow olh */
5268 if (ret < 0) {
5269 return -ret;
5270 }
5271
5272 ret = store->bucket_index_read_olh_log(bucket_info, *state, obj, 0, &log, &is_truncated);
5273 if (ret < 0) {
5274 cerr << "ERROR: failed reading olh: " << cpp_strerror(-ret) << std::endl;
5275 return -ret;
5276 }
5277 formatter->open_object_section("result");
5278 encode_json("is_truncated", is_truncated, formatter);
5279 encode_json("log", log, formatter);
5280 formatter->close_section();
5281 formatter->flush(cout);
5282 }
5283
5284 if (opt_cmd == OPT_BI_GET) {
5285 RGWBucketInfo bucket_info;
5286 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5287 if (ret < 0) {
5288 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5289 return -ret;
5290 }
5291 rgw_obj obj(bucket, object);
5292 if (!object_version.empty()) {
5293 obj.key.set_instance(object_version);
5294 }
5295
5296 rgw_cls_bi_entry entry;
5297
5298 ret = store->bi_get(bucket, obj, bi_index_type, &entry);
5299 if (ret < 0) {
5300 cerr << "ERROR: bi_get(): " << cpp_strerror(-ret) << std::endl;
5301 return -ret;
5302 }
5303
5304 encode_json("entry", entry, formatter);
5305 formatter->flush(cout);
5306 }
5307
5308 if (opt_cmd == OPT_BI_PUT) {
5309 RGWBucketInfo bucket_info;
5310 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5311 if (ret < 0) {
5312 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5313 return -ret;
5314 }
5315
5316 rgw_cls_bi_entry entry;
5317 cls_rgw_obj_key key;
5318 ret = read_decode_json(infile, entry, &key);
5319 if (ret < 0) {
5320 return 1;
5321 }
5322
5323 rgw_obj obj(bucket, key);
5324
5325 ret = store->bi_put(bucket, obj, entry);
5326 if (ret < 0) {
5327 cerr << "ERROR: bi_put(): " << cpp_strerror(-ret) << std::endl;
5328 return -ret;
5329 }
5330 }
5331
5332 if (opt_cmd == OPT_BI_LIST) {
5333 if (bucket_name.empty()) {
5334 cerr << "ERROR: bucket name not specified" << std::endl;
5335 return EINVAL;
5336 }
5337 RGWBucketInfo bucket_info;
5338 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5339 if (ret < 0) {
5340 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5341 return -ret;
5342 }
5343
5344 list<rgw_cls_bi_entry> entries;
5345 bool is_truncated;
5346 if (max_entries < 0) {
5347 max_entries = 1000;
5348 }
5349
5350 int max_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5351
5352 formatter->open_array_section("entries");
5353
5354 for (int i = 0; i < max_shards; i++) {
5355 RGWRados::BucketShard bs(store);
5356 int shard_id = (bucket_info.num_shards > 0 ? i : -1);
5357 int ret = bs.init(bucket, shard_id);
5358 marker.clear();
5359
5360 if (ret < 0) {
5361 cerr << "ERROR: bs.init(bucket=" << bucket << ", shard=" << shard_id << "): " << cpp_strerror(-ret) << std::endl;
5362 return -ret;
5363 }
5364
5365 do {
5366 entries.clear();
5367 ret = store->bi_list(bs, object, marker, max_entries, &entries, &is_truncated);
5368 if (ret < 0) {
5369 cerr << "ERROR: bi_list(): " << cpp_strerror(-ret) << std::endl;
5370 return -ret;
5371 }
5372
5373 list<rgw_cls_bi_entry>::iterator iter;
5374 for (iter = entries.begin(); iter != entries.end(); ++iter) {
5375 rgw_cls_bi_entry& entry = *iter;
5376 encode_json("entry", entry, formatter);
5377 marker = entry.idx;
5378 }
5379 formatter->flush(cout);
5380 } while (is_truncated);
5381 formatter->flush(cout);
5382 }
5383 formatter->close_section();
5384 formatter->flush(cout);
5385 }
5386
5387 if (opt_cmd == OPT_BI_PURGE) {
5388 if (bucket_name.empty()) {
5389 cerr << "ERROR: bucket name not specified" << std::endl;
5390 return EINVAL;
5391 }
5392 RGWBucketInfo bucket_info;
5393 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5394 if (ret < 0) {
5395 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5396 return -ret;
5397 }
5398
5399 RGWBucketInfo cur_bucket_info;
5400 rgw_bucket cur_bucket;
5401 ret = init_bucket(tenant, bucket_name, string(), cur_bucket_info, cur_bucket);
5402 if (ret < 0) {
5403 cerr << "ERROR: could not init current bucket info for bucket_name=" << bucket_name << ": " << cpp_strerror(-ret) << std::endl;
5404 return -ret;
5405 }
5406
5407 if (cur_bucket_info.bucket.bucket_id == bucket_info.bucket.bucket_id && !yes_i_really_mean_it) {
5408 cerr << "specified bucket instance points to a current bucket instance" << std::endl;
5409 cerr << "do you really mean it? (requires --yes-i-really-mean-it)" << std::endl;
5410 return EINVAL;
5411 }
5412
5413 int max_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5414
5415 for (int i = 0; i < max_shards; i++) {
5416 RGWRados::BucketShard bs(store);
5417 int shard_id = (bucket_info.num_shards > 0 ? i : -1);
5418 int ret = bs.init(bucket, shard_id);
5419 if (ret < 0) {
5420 cerr << "ERROR: bs.init(bucket=" << bucket << ", shard=" << shard_id << "): " << cpp_strerror(-ret) << std::endl;
5421 return -ret;
5422 }
5423
5424 ret = store->bi_remove(bs);
5425 if (ret < 0) {
5426 cerr << "ERROR: failed to remove bucket index object: " << cpp_strerror(-ret) << std::endl;
5427 return -ret;
5428 }
5429 }
5430 }
5431
5432 if (opt_cmd == OPT_OBJECT_RM) {
5433 RGWBucketInfo bucket_info;
5434 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5435 if (ret < 0) {
5436 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5437 return -ret;
5438 }
5439 rgw_obj_key key(object, object_version);
5440 ret = rgw_remove_object(store, bucket_info, bucket, key);
5441
5442 if (ret < 0) {
5443 cerr << "ERROR: object remove returned: " << cpp_strerror(-ret) << std::endl;
5444 return -ret;
5445 }
5446 }
5447
5448 if (opt_cmd == OPT_OBJECT_REWRITE) {
5449 if (bucket_name.empty()) {
5450 cerr << "ERROR: bucket not specified" << std::endl;
5451 return EINVAL;
5452 }
5453 if (object.empty()) {
5454 cerr << "ERROR: object not specified" << std::endl;
5455 return EINVAL;
5456 }
5457
5458 RGWBucketInfo bucket_info;
5459 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5460 if (ret < 0) {
5461 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5462 return -ret;
5463 }
5464
5465 rgw_obj obj(bucket, object);
5466 obj.key.set_instance(object_version);
5467 bool need_rewrite = true;
5468 if (min_rewrite_stripe_size > 0) {
5469 ret = check_min_obj_stripe_size(store, bucket_info, obj, min_rewrite_stripe_size, &need_rewrite);
5470 if (ret < 0) {
5471 ldout(store->ctx(), 0) << "WARNING: check_min_obj_stripe_size failed, r=" << ret << dendl;
5472 }
5473 }
5474 if (need_rewrite) {
5475 ret = store->rewrite_obj(bucket_info, obj);
5476 if (ret < 0) {
5477 cerr << "ERROR: object rewrite returned: " << cpp_strerror(-ret) << std::endl;
5478 return -ret;
5479 }
5480 } else {
5481 ldout(store->ctx(), 20) << "skipped object" << dendl;
5482 }
5483 }
5484
5485 if (opt_cmd == OPT_OBJECTS_EXPIRE) {
5486 int ret = store->process_expire_objects();
5487 if (ret < 0) {
5488 cerr << "ERROR: process_expire_objects() processing returned error: " << cpp_strerror(-ret) << std::endl;
5489 return 1;
5490 }
5491 }
5492
5493 if (opt_cmd == OPT_BUCKET_REWRITE) {
5494 if (bucket_name.empty()) {
5495 cerr << "ERROR: bucket not specified" << std::endl;
5496 return EINVAL;
5497 }
5498
5499 RGWBucketInfo bucket_info;
5500 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5501 if (ret < 0) {
5502 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5503 return -ret;
5504 }
5505
5506 uint64_t start_epoch = 0;
5507 uint64_t end_epoch = 0;
5508
5509 if (!end_date.empty()) {
5510 int ret = utime_t::parse_date(end_date, &end_epoch, NULL);
5511 if (ret < 0) {
5512 cerr << "ERROR: failed to parse end date" << std::endl;
5513 return EINVAL;
5514 }
5515 }
5516 if (!start_date.empty()) {
5517 int ret = utime_t::parse_date(start_date, &start_epoch, NULL);
5518 if (ret < 0) {
5519 cerr << "ERROR: failed to parse start date" << std::endl;
5520 return EINVAL;
5521 }
5522 }
5523
5524 bool is_truncated = true;
5525
5526 rgw_obj_index_key marker;
5527 string prefix;
5528
5529 formatter->open_object_section("result");
5530 formatter->dump_string("bucket", bucket_name);
5531 formatter->open_array_section("objects");
5532 while (is_truncated) {
5533 map<string, rgw_bucket_dir_entry> result;
5534 int r = store->cls_bucket_list(bucket_info, RGW_NO_SHARD, marker, prefix, 1000, true,
5535 result, &is_truncated, &marker,
5536 bucket_object_check_filter);
5537
5538 if (r < 0 && r != -ENOENT) {
5539 cerr << "ERROR: failed operation r=" << r << std::endl;
5540 }
5541
5542 if (r == -ENOENT)
5543 break;
5544
5545 map<string, rgw_bucket_dir_entry>::iterator iter;
5546 for (iter = result.begin(); iter != result.end(); ++iter) {
5547 rgw_obj_key key = iter->second.key;
5548 rgw_bucket_dir_entry& entry = iter->second;
5549
5550 formatter->open_object_section("object");
5551 formatter->dump_string("name", key.name);
5552 formatter->dump_string("instance", key.instance);
5553 formatter->dump_int("size", entry.meta.size);
5554 utime_t ut(entry.meta.mtime);
5555 ut.gmtime(formatter->dump_stream("mtime"));
5556
5557 if ((entry.meta.size < min_rewrite_size) ||
5558 (entry.meta.size > max_rewrite_size) ||
5559 (start_epoch > 0 && start_epoch > (uint64_t)ut.sec()) ||
5560 (end_epoch > 0 && end_epoch < (uint64_t)ut.sec())) {
5561 formatter->dump_string("status", "Skipped");
5562 } else {
5563 rgw_obj obj(bucket, key);
5564
5565 bool need_rewrite = true;
5566 if (min_rewrite_stripe_size > 0) {
5567 r = check_min_obj_stripe_size(store, bucket_info, obj, min_rewrite_stripe_size, &need_rewrite);
5568 if (r < 0) {
5569 ldout(store->ctx(), 0) << "WARNING: check_min_obj_stripe_size failed, r=" << r << dendl;
5570 }
5571 }
5572 if (!need_rewrite) {
5573 formatter->dump_string("status", "Skipped");
5574 } else {
5575 r = store->rewrite_obj(bucket_info, obj);
5576 if (r == 0) {
5577 formatter->dump_string("status", "Success");
5578 } else {
5579 formatter->dump_string("status", cpp_strerror(-r));
5580 }
5581 }
5582 }
5583 formatter->dump_int("flags", entry.flags);
5584
5585 formatter->close_section();
5586 formatter->flush(cout);
5587 }
5588 }
5589 formatter->close_section();
5590 formatter->close_section();
5591 formatter->flush(cout);
5592 }
5593
5594 if (opt_cmd == OPT_BUCKET_RESHARD) {
5595 rgw_bucket bucket;
5596 RGWBucketInfo bucket_info;
5597 map<string, bufferlist> attrs;
5598
5599 int ret = check_reshard_bucket_params(store,
5600 bucket_name,
5601 tenant,
5602 bucket_id,
5603 num_shards_specified,
5604 num_shards,
5605 yes_i_really_mean_it,
5606 bucket,
5607 bucket_info,
5608 attrs);
5609 if (ret < 0) {
5610 return ret;
5611 }
5612
5613 RGWBucketReshard br(store, bucket_info, attrs);
5614
5615 #define DEFAULT_RESHARD_MAX_ENTRIES 1000
5616 if (max_entries < 1) {
5617 max_entries = DEFAULT_RESHARD_MAX_ENTRIES;
5618 }
5619
5620 return br.execute(num_shards, max_entries,
5621 verbose, &cout, formatter);
5622 }
5623
5624 if (opt_cmd == OPT_RESHARD_ADD) {
5625 rgw_bucket bucket;
5626 RGWBucketInfo bucket_info;
5627 map<string, bufferlist> attrs;
5628
5629 int ret = check_reshard_bucket_params(store,
5630 bucket_name,
5631 tenant,
5632 bucket_id,
5633 num_shards_specified,
5634 num_shards,
5635 yes_i_really_mean_it,
5636 bucket,
5637 bucket_info,
5638 attrs);
5639 if (ret < 0) {
5640 return ret;
5641 }
5642
5643 int num_source_shards = (bucket_info.num_shards > 0 ? bucket_info.num_shards : 1);
5644
5645 RGWReshard reshard(store);
5646 cls_rgw_reshard_entry entry;
5647 entry.time = real_clock::now();
5648 entry.tenant = tenant;
5649 entry.bucket_name = bucket_name;
5650 entry.bucket_id = bucket_info.bucket.bucket_id;
5651 entry.old_num_shards = num_source_shards;
5652 entry.new_num_shards = num_shards;
5653
5654 return reshard.add(entry);
5655 }
5656
5657 if (opt_cmd == OPT_RESHARD_LIST) {
5658 list<cls_rgw_reshard_entry> entries;
5659 int ret;
5660 int count = 0;
5661 if (max_entries < 0) {
5662 max_entries = 1000;
5663 }
5664
5665 int num_logshards = store->ctx()->_conf->rgw_reshard_num_logs;
5666
5667 RGWReshard reshard(store);
5668
5669 formatter->open_array_section("reshard");
5670 for (int i = 0; i < num_logshards; i++) {
5671 bool is_truncated = true;
5672 string marker;
5673 do {
5674 entries.clear();
5675 ret = reshard.list(i, marker, max_entries, entries, &is_truncated);
5676 if (ret < 0) {
5677 cerr << "Error listing resharding buckets: " << cpp_strerror(-ret) << std::endl;
5678 return ret;
5679 }
5680 for (auto iter=entries.begin(); iter != entries.end(); ++iter) {
5681 cls_rgw_reshard_entry& entry = *iter;
5682 encode_json("entry", entry, formatter);
5683 entry.get_key(&marker);
5684 }
5685 count += entries.size();
5686 formatter->flush(cout);
5687 } while (is_truncated && count < max_entries);
5688
5689 if (count >= max_entries) {
5690 break;
5691 }
5692 }
5693
5694 formatter->close_section();
5695 formatter->flush(cout);
5696 return 0;
5697 }
5698
5699
5700 if (opt_cmd == OPT_RESHARD_STATUS) {
5701 if (bucket_name.empty()) {
5702 cerr << "ERROR: bucket not specified" << std::endl;
5703 return EINVAL;
5704 }
5705
5706 rgw_bucket bucket;
5707 RGWBucketInfo bucket_info;
5708 map<string, bufferlist> attrs;
5709 ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket, &attrs);
5710 if (ret < 0) {
5711 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5712 return -ret;
5713 }
5714
5715 RGWBucketReshard br(store, bucket_info, attrs);
5716 list<cls_rgw_bucket_instance_entry> status;
5717 int r = br.get_status(&status);
5718 if (r < 0) {
5719 cerr << "ERROR: could not get resharding status for bucket " << bucket_name << std::endl;
5720 return -r;
5721 }
5722
5723 encode_json("status", status, formatter);
5724 formatter->flush(cout);
5725 }
5726
5727 if (opt_cmd == OPT_RESHARD_PROCESS) {
5728 RGWReshard reshard(store, true, &cout);
5729
5730 int ret = reshard.process_all_logshards();
5731 if (ret < 0) {
5732 cerr << "ERROR: failed to process reshard logs, error=" << cpp_strerror(-ret) << std::endl;
5733 return -ret;
5734 }
5735 }
5736
5737 if (opt_cmd == OPT_RESHARD_CANCEL) {
5738 RGWReshard reshard(store);
5739
5740 if (bucket_name.empty()) {
5741 cerr << "ERROR: bucket not specified" << std::endl;
5742 return EINVAL;
5743 }
5744 cls_rgw_reshard_entry entry;
5745 //entry.tenant = tenant;
5746 entry.bucket_name = bucket_name;
5747 //entry.bucket_id = bucket_id;
5748 int ret = reshard.get(entry);
5749 if (ret < 0) {
5750 cerr << "Error in getting bucket " << bucket_name << ": " << cpp_strerror(-ret) << std::endl;
5751 return ret;
5752 }
5753
5754 /* TBD stop running resharding */
5755
5756 ret =reshard.remove(entry);
5757 if (ret < 0) {
5758 cerr << "Error removing bucket " << bucket_name << " for resharding queue: " << cpp_strerror(-ret) <<
5759 std::endl;
5760 return ret;
5761 }
5762 }
5763
5764 if (opt_cmd == OPT_OBJECT_UNLINK) {
5765 RGWBucketInfo bucket_info;
5766 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5767 if (ret < 0) {
5768 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5769 return -ret;
5770 }
5771 list<rgw_obj_index_key> oid_list;
5772 rgw_obj_key key(object, object_version);
5773 rgw_obj_index_key index_key;
5774 key.get_index_key(&index_key);
5775 oid_list.push_back(index_key);
5776 ret = store->remove_objs_from_index(bucket_info, oid_list);
5777 if (ret < 0) {
5778 cerr << "ERROR: remove_obj_from_index() returned error: " << cpp_strerror(-ret) << std::endl;
5779 return 1;
5780 }
5781 }
5782
5783 if (opt_cmd == OPT_OBJECT_STAT) {
5784 RGWBucketInfo bucket_info;
5785 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
5786 if (ret < 0) {
5787 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
5788 return -ret;
5789 }
5790 rgw_obj obj(bucket, object);
5791 obj.key.set_instance(object_version);
5792
5793 uint64_t obj_size;
5794 map<string, bufferlist> attrs;
5795 RGWObjectCtx obj_ctx(store);
5796 RGWRados::Object op_target(store, bucket_info, obj_ctx, obj);
5797 RGWRados::Object::Read read_op(&op_target);
5798
5799 read_op.params.attrs = &attrs;
5800 read_op.params.obj_size = &obj_size;
5801
5802 ret = read_op.prepare();
5803 if (ret < 0) {
5804 cerr << "ERROR: failed to stat object, returned error: " << cpp_strerror(-ret) << std::endl;
5805 return 1;
5806 }
5807 formatter->open_object_section("object_metadata");
5808 formatter->dump_string("name", object);
5809 formatter->dump_unsigned("size", obj_size);
5810
5811 map<string, bufferlist>::iterator iter;
5812 map<string, bufferlist> other_attrs;
5813 for (iter = attrs.begin(); iter != attrs.end(); ++iter) {
5814 bufferlist& bl = iter->second;
5815 bool handled = false;
5816 if (iter->first == RGW_ATTR_MANIFEST) {
5817 handled = decode_dump<RGWObjManifest>("manifest", bl, formatter);
5818 } else if (iter->first == RGW_ATTR_ACL) {
5819 handled = decode_dump<RGWAccessControlPolicy>("policy", bl, formatter);
5820 } else if (iter->first == RGW_ATTR_ID_TAG) {
5821 handled = dump_string("tag", bl, formatter);
5822 } else if (iter->first == RGW_ATTR_ETAG) {
5823 handled = dump_string("etag", bl, formatter);
5824 }
5825
5826 if (!handled)
5827 other_attrs[iter->first] = bl;
5828 }
5829
5830 formatter->open_object_section("attrs");
5831 for (iter = other_attrs.begin(); iter != other_attrs.end(); ++iter) {
5832 dump_string(iter->first.c_str(), iter->second, formatter);
5833 }
5834 formatter->close_section();
5835 formatter->close_section();
5836 formatter->flush(cout);
5837 }
5838
5839 if (opt_cmd == OPT_BUCKET_CHECK) {
5840 if (check_head_obj_locator) {
5841 if (bucket_name.empty()) {
5842 cerr << "ERROR: need to specify bucket name" << std::endl;
5843 return EINVAL;
5844 }
5845 do_check_object_locator(tenant, bucket_name, fix, remove_bad, formatter);
5846 } else {
5847 RGWBucketAdminOp::check_index(store, bucket_op, f);
5848 }
5849 }
5850
5851 if (opt_cmd == OPT_BUCKET_RM) {
5852 if (inconsistent_index == false) {
5853 RGWBucketAdminOp::remove_bucket(store, bucket_op, bypass_gc, true);
5854 } else {
5855 RGWBucketAdminOp::remove_bucket(store, bucket_op, bypass_gc, false);
5856 }
5857 }
5858
5859 if (opt_cmd == OPT_GC_LIST) {
5860 int index = 0;
5861 bool truncated;
5862 formatter->open_array_section("entries");
5863
5864 do {
5865 list<cls_rgw_gc_obj_info> result;
5866 int ret = store->list_gc_objs(&index, marker, 1000, !include_all, result, &truncated);
5867 if (ret < 0) {
5868 cerr << "ERROR: failed to list objs: " << cpp_strerror(-ret) << std::endl;
5869 return 1;
5870 }
5871
5872
5873 list<cls_rgw_gc_obj_info>::iterator iter;
5874 for (iter = result.begin(); iter != result.end(); ++iter) {
5875 cls_rgw_gc_obj_info& info = *iter;
5876 formatter->open_object_section("chain_info");
5877 formatter->dump_string("tag", info.tag);
5878 formatter->dump_stream("time") << info.time;
5879 formatter->open_array_section("objs");
5880 list<cls_rgw_obj>::iterator liter;
5881 cls_rgw_obj_chain& chain = info.chain;
5882 for (liter = chain.objs.begin(); liter != chain.objs.end(); ++liter) {
5883 cls_rgw_obj& obj = *liter;
5884 encode_json("obj", obj, formatter);
5885 }
5886 formatter->close_section(); // objs
5887 formatter->close_section(); // obj_chain
5888 formatter->flush(cout);
5889 }
5890 } while (truncated);
5891 formatter->close_section();
5892 formatter->flush(cout);
5893 }
5894
5895 if (opt_cmd == OPT_GC_PROCESS) {
5896 int ret = store->process_gc();
5897 if (ret < 0) {
5898 cerr << "ERROR: gc processing returned error: " << cpp_strerror(-ret) << std::endl;
5899 return 1;
5900 }
5901 }
5902
5903 if (opt_cmd == OPT_LC_LIST) {
5904 formatter->open_array_section("lifecycle_list");
5905 map<string, int> bucket_lc_map;
5906 string marker;
5907 #define MAX_LC_LIST_ENTRIES 100
5908 if (max_entries < 0) {
5909 max_entries = MAX_LC_LIST_ENTRIES;
5910 }
5911 do {
5912 int ret = store->list_lc_progress(marker, max_entries, &bucket_lc_map);
5913 if (ret < 0) {
5914 cerr << "ERROR: failed to list objs: " << cpp_strerror(-ret) << std::endl;
5915 return 1;
5916 }
5917 map<string, int>::iterator iter;
5918 for (iter = bucket_lc_map.begin(); iter != bucket_lc_map.end(); ++iter) {
5919 formatter->open_object_section("bucket_lc_info");
5920 formatter->dump_string("bucket", iter->first);
5921 string lc_status = LC_STATUS[iter->second];
5922 formatter->dump_string("status", lc_status);
5923 formatter->close_section(); // objs
5924 formatter->flush(cout);
5925 marker = iter->first;
5926 }
5927 } while (!bucket_lc_map.empty());
5928
5929 formatter->close_section(); //lifecycle list
5930 formatter->flush(cout);
5931 }
5932
5933
5934 if (opt_cmd == OPT_LC_PROCESS) {
5935 int ret = store->process_lc();
5936 if (ret < 0) {
5937 cerr << "ERROR: lc processing returned error: " << cpp_strerror(-ret) << std::endl;
5938 return 1;
5939 }
5940 }
5941
5942 if (opt_cmd == OPT_ORPHANS_FIND) {
5943 RGWOrphanSearch search(store, max_concurrent_ios, orphan_stale_secs);
5944
5945 if (job_id.empty()) {
5946 cerr << "ERROR: --job-id not specified" << std::endl;
5947 return EINVAL;
5948 }
5949 if (pool_name.empty()) {
5950 cerr << "ERROR: --pool not specified" << std::endl;
5951 return EINVAL;
5952 }
5953
5954 RGWOrphanSearchInfo info;
5955
5956 info.pool = pool;
5957 info.job_name = job_id;
5958 info.num_shards = num_shards;
5959
5960 int ret = search.init(job_id, &info);
5961 if (ret < 0) {
5962 cerr << "could not init search, ret=" << ret << std::endl;
5963 return -ret;
5964 }
5965 ret = search.run();
5966 if (ret < 0) {
5967 return -ret;
5968 }
5969 }
5970
5971 if (opt_cmd == OPT_ORPHANS_FINISH) {
5972 RGWOrphanSearch search(store, max_concurrent_ios, orphan_stale_secs);
5973
5974 if (job_id.empty()) {
5975 cerr << "ERROR: --job-id not specified" << std::endl;
5976 return EINVAL;
5977 }
5978 int ret = search.init(job_id, NULL);
5979 if (ret < 0) {
5980 if (ret == -ENOENT) {
5981 cerr << "job not found" << std::endl;
5982 }
5983 return -ret;
5984 }
5985 ret = search.finish();
5986 if (ret < 0) {
5987 return -ret;
5988 }
5989 }
5990
5991 if (opt_cmd == OPT_ORPHANS_LIST_JOBS){
5992 RGWOrphanStore orphan_store(store);
5993 int ret = orphan_store.init();
5994 if (ret < 0){
5995 cerr << "connection to cluster failed!" << std::endl;
5996 return -ret;
5997 }
5998
5999 map <string,RGWOrphanSearchState> m;
6000 ret = orphan_store.list_jobs(m);
6001 if (ret < 0) {
6002 cerr << "job list failed" << std::endl;
6003 return -ret;
6004 }
6005 formatter->open_array_section("entries");
6006 for (const auto &it: m){
6007 if (!extra_info){
6008 formatter->dump_string("job-id",it.first);
6009 } else {
6010 encode_json("orphan_search_state", it.second, formatter);
6011 }
6012 }
6013 formatter->close_section();
6014 formatter->flush(cout);
6015 }
6016
6017 if (opt_cmd == OPT_USER_CHECK) {
6018 check_bad_user_bucket_mapping(store, user_id, fix);
6019 }
6020
6021 if (opt_cmd == OPT_USER_STATS) {
6022 if (sync_stats) {
6023 if (!bucket_name.empty()) {
6024 int ret = rgw_bucket_sync_user_stats(store, tenant, bucket_name);
6025 if (ret < 0) {
6026 cerr << "ERROR: could not sync bucket stats: " << cpp_strerror(-ret) << std::endl;
6027 return -ret;
6028 }
6029 } else {
6030 int ret = rgw_user_sync_all_stats(store, user_id);
6031 if (ret < 0) {
6032 cerr << "ERROR: failed to sync user stats: " << cpp_strerror(-ret) << std::endl;
6033 return -ret;
6034 }
6035 }
6036 }
6037
6038 if (user_id.empty()) {
6039 cerr << "ERROR: uid not specified" << std::endl;
6040 return EINVAL;
6041 }
6042 cls_user_header header;
6043 string user_str = user_id.to_str();
6044 int ret = store->cls_user_get_header(user_str, &header);
6045 if (ret < 0) {
6046 if (ret == -ENOENT) { /* in case of ENOENT */
6047 cerr << "User has not been initialized or user does not exist" << std::endl;
6048 } else {
6049 cerr << "ERROR: can't read user: " << cpp_strerror(ret) << std::endl;
6050 }
6051 return -ret;
6052 }
6053
6054 encode_json("header", header, formatter);
6055 formatter->flush(cout);
6056 }
6057
6058 if (opt_cmd == OPT_METADATA_GET) {
6059 int ret = store->meta_mgr->get(metadata_key, formatter);
6060 if (ret < 0) {
6061 cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl;
6062 return -ret;
6063 }
6064
6065 formatter->flush(cout);
6066 }
6067
6068 if (opt_cmd == OPT_METADATA_PUT) {
6069 bufferlist bl;
6070 int ret = read_input(infile, bl);
6071 if (ret < 0) {
6072 cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
6073 return -ret;
6074 }
6075 ret = store->meta_mgr->put(metadata_key, bl, RGWMetadataHandler::APPLY_ALWAYS);
6076 if (ret < 0) {
6077 cerr << "ERROR: can't put key: " << cpp_strerror(-ret) << std::endl;
6078 return -ret;
6079 }
6080 }
6081
6082 if (opt_cmd == OPT_METADATA_RM) {
6083 int ret = store->meta_mgr->remove(metadata_key);
6084 if (ret < 0) {
6085 cerr << "ERROR: can't remove key: " << cpp_strerror(-ret) << std::endl;
6086 return -ret;
6087 }
6088 }
6089
6090 if (opt_cmd == OPT_METADATA_LIST || opt_cmd == OPT_USER_LIST) {
6091 if (opt_cmd == OPT_USER_LIST) {
6092 metadata_key = "user";
6093 }
6094 void *handle;
6095 int max = 1000;
6096 int ret = store->meta_mgr->list_keys_init(metadata_key, &handle);
6097 if (ret < 0) {
6098 cerr << "ERROR: can't get key: " << cpp_strerror(-ret) << std::endl;
6099 return -ret;
6100 }
6101
6102 bool truncated;
6103
6104 formatter->open_array_section("keys");
6105
6106 do {
6107 list<string> keys;
6108 ret = store->meta_mgr->list_keys_next(handle, max, keys, &truncated);
6109 if (ret < 0 && ret != -ENOENT) {
6110 cerr << "ERROR: lists_keys_next(): " << cpp_strerror(-ret) << std::endl;
6111 return -ret;
6112 } if (ret != -ENOENT) {
6113 for (list<string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
6114 formatter->dump_string("key", *iter);
6115 }
6116 formatter->flush(cout);
6117 }
6118 } while (truncated);
6119
6120 formatter->close_section();
6121 formatter->flush(cout);
6122
6123 store->meta_mgr->list_keys_complete(handle);
6124 }
6125
6126 if (opt_cmd == OPT_MDLOG_LIST) {
6127 utime_t start_time, end_time;
6128
6129 int ret = parse_date_str(start_date, start_time);
6130 if (ret < 0)
6131 return -ret;
6132
6133 ret = parse_date_str(end_date, end_time);
6134 if (ret < 0)
6135 return -ret;
6136
6137 int i = (specified_shard_id ? shard_id : 0);
6138
6139 if (period_id.empty()) {
6140 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
6141 if (ret < 0) {
6142 return -ret;
6143 }
6144 std::cerr << "No --period given, using current period="
6145 << period_id << std::endl;
6146 }
6147 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6148
6149 formatter->open_array_section("entries");
6150 for (; i < g_ceph_context->_conf->rgw_md_log_max_shards; i++) {
6151 void *handle;
6152 list<cls_log_entry> entries;
6153
6154
6155 meta_log->init_list_entries(i, start_time.to_real_time(), end_time.to_real_time(), marker, &handle);
6156 bool truncated;
6157 do {
6158 int ret = meta_log->list_entries(handle, 1000, entries, NULL, &truncated);
6159 if (ret < 0) {
6160 cerr << "ERROR: meta_log->list_entries(): " << cpp_strerror(-ret) << std::endl;
6161 return -ret;
6162 }
6163
6164 for (list<cls_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6165 cls_log_entry& entry = *iter;
6166 store->meta_mgr->dump_log_entry(entry, formatter);
6167 }
6168 formatter->flush(cout);
6169 } while (truncated);
6170
6171 meta_log->complete_list_entries(handle);
6172
6173 if (specified_shard_id)
6174 break;
6175 }
6176
6177
6178 formatter->close_section();
6179 formatter->flush(cout);
6180 }
6181
6182 if (opt_cmd == OPT_MDLOG_STATUS) {
6183 int i = (specified_shard_id ? shard_id : 0);
6184
6185 if (period_id.empty()) {
6186 int ret = read_current_period_id(store, realm_id, realm_name, &period_id);
6187 if (ret < 0) {
6188 return -ret;
6189 }
6190 std::cerr << "No --period given, using current period="
6191 << period_id << std::endl;
6192 }
6193 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6194
6195 formatter->open_array_section("entries");
6196
6197 for (; i < g_ceph_context->_conf->rgw_md_log_max_shards; i++) {
6198 RGWMetadataLogInfo info;
6199 meta_log->get_info(i, &info);
6200
6201 ::encode_json("info", info, formatter);
6202
6203 if (specified_shard_id)
6204 break;
6205 }
6206
6207
6208 formatter->close_section();
6209 formatter->flush(cout);
6210 }
6211
6212 if (opt_cmd == OPT_MDLOG_AUTOTRIM) {
6213 // need a full history for purging old mdlog periods
6214 store->meta_mgr->init_oldest_log_period();
6215
6216 RGWCoroutinesManager crs(store->ctx(), store->get_cr_registry());
6217 RGWHTTPManager http(store->ctx(), crs.get_completion_mgr());
6218 int ret = http.set_threaded();
6219 if (ret < 0) {
6220 cerr << "failed to initialize http client with " << cpp_strerror(ret) << std::endl;
6221 return -ret;
6222 }
6223
6224 auto num_shards = g_conf->rgw_md_log_max_shards;
6225 ret = crs.run(create_admin_meta_log_trim_cr(store, &http, num_shards));
6226 if (ret < 0) {
6227 cerr << "automated mdlog trim failed with " << cpp_strerror(ret) << std::endl;
6228 return -ret;
6229 }
6230 }
6231
6232 if (opt_cmd == OPT_MDLOG_TRIM) {
6233 utime_t start_time, end_time;
6234
6235 if (!specified_shard_id) {
6236 cerr << "ERROR: shard-id must be specified for trim operation" << std::endl;
6237 return EINVAL;
6238 }
6239
6240 int ret = parse_date_str(start_date, start_time);
6241 if (ret < 0)
6242 return -ret;
6243
6244 ret = parse_date_str(end_date, end_time);
6245 if (ret < 0)
6246 return -ret;
6247
6248 if (period_id.empty()) {
6249 std::cerr << "missing --period argument" << std::endl;
6250 return EINVAL;
6251 }
6252 RGWMetadataLog *meta_log = store->meta_mgr->get_log(period_id);
6253
6254 ret = meta_log->trim(shard_id, start_time.to_real_time(), end_time.to_real_time(), start_marker, end_marker);
6255 if (ret < 0) {
6256 cerr << "ERROR: meta_log->trim(): " << cpp_strerror(-ret) << std::endl;
6257 return -ret;
6258 }
6259 }
6260
6261 if (opt_cmd == OPT_SYNC_STATUS) {
6262 sync_status(formatter);
6263 }
6264
6265 if (opt_cmd == OPT_METADATA_SYNC_STATUS) {
6266 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6267
6268 int ret = sync.init();
6269 if (ret < 0) {
6270 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6271 return -ret;
6272 }
6273
6274 rgw_meta_sync_status sync_status;
6275 ret = sync.read_sync_status(&sync_status);
6276 if (ret < 0) {
6277 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6278 return -ret;
6279 }
6280
6281 formatter->open_object_section("summary");
6282 encode_json("sync_status", sync_status, formatter);
6283
6284 uint64_t full_total = 0;
6285 uint64_t full_complete = 0;
6286
6287 for (auto marker_iter : sync_status.sync_markers) {
6288 full_total += marker_iter.second.total_entries;
6289 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
6290 full_complete += marker_iter.second.pos;
6291 } else {
6292 full_complete += marker_iter.second.total_entries;
6293 }
6294 }
6295
6296 formatter->open_object_section("full_sync");
6297 encode_json("total", full_total, formatter);
6298 encode_json("complete", full_complete, formatter);
6299 formatter->close_section();
6300 formatter->close_section();
6301
6302 formatter->flush(cout);
6303
6304 }
6305
6306 if (opt_cmd == OPT_METADATA_SYNC_INIT) {
6307 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6308
6309 int ret = sync.init();
6310 if (ret < 0) {
6311 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6312 return -ret;
6313 }
6314 ret = sync.init_sync_status();
6315 if (ret < 0) {
6316 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6317 return -ret;
6318 }
6319 }
6320
6321
6322 if (opt_cmd == OPT_METADATA_SYNC_RUN) {
6323 RGWMetaSyncStatusManager sync(store, store->get_async_rados());
6324
6325 int ret = sync.init();
6326 if (ret < 0) {
6327 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6328 return -ret;
6329 }
6330
6331 ret = sync.run();
6332 if (ret < 0) {
6333 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6334 return -ret;
6335 }
6336 }
6337
6338 if (opt_cmd == OPT_DATA_SYNC_STATUS) {
6339 if (source_zone.empty()) {
6340 cerr << "ERROR: source zone not specified" << std::endl;
6341 return EINVAL;
6342 }
6343 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6344
6345 int ret = sync.init();
6346 if (ret < 0) {
6347 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6348 return -ret;
6349 }
6350
6351 rgw_data_sync_status sync_status;
6352 ret = sync.read_sync_status(&sync_status);
6353 if (ret < 0 && ret != -ENOENT) {
6354 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6355 return -ret;
6356 }
6357
6358 formatter->open_object_section("summary");
6359 encode_json("sync_status", sync_status, formatter);
6360
6361 uint64_t full_total = 0;
6362 uint64_t full_complete = 0;
6363
6364 for (auto marker_iter : sync_status.sync_markers) {
6365 full_total += marker_iter.second.total_entries;
6366 if (marker_iter.second.state == rgw_meta_sync_marker::SyncState::FullSync) {
6367 full_complete += marker_iter.second.pos;
6368 } else {
6369 full_complete += marker_iter.second.total_entries;
6370 }
6371 }
6372
6373 formatter->open_object_section("full_sync");
6374 encode_json("total", full_total, formatter);
6375 encode_json("complete", full_complete, formatter);
6376 formatter->close_section();
6377 formatter->close_section();
6378
6379 formatter->flush(cout);
6380 }
6381
6382 if (opt_cmd == OPT_DATA_SYNC_INIT) {
6383 if (source_zone.empty()) {
6384 cerr << "ERROR: source zone not specified" << std::endl;
6385 return EINVAL;
6386 }
6387 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6388
6389 int ret = sync.init();
6390 if (ret < 0) {
6391 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6392 return -ret;
6393 }
6394
6395 ret = sync.init_sync_status();
6396 if (ret < 0) {
6397 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6398 return -ret;
6399 }
6400 }
6401
6402 if (opt_cmd == OPT_DATA_SYNC_RUN) {
6403 if (source_zone.empty()) {
6404 cerr << "ERROR: source zone not specified" << std::endl;
6405 return EINVAL;
6406 }
6407 RGWDataSyncStatusManager sync(store, store->get_async_rados(), source_zone);
6408
6409 int ret = sync.init();
6410 if (ret < 0) {
6411 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6412 return -ret;
6413 }
6414
6415 ret = sync.run();
6416 if (ret < 0) {
6417 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6418 return -ret;
6419 }
6420 }
6421
6422 if (opt_cmd == OPT_BUCKET_SYNC_INIT) {
6423 if (source_zone.empty()) {
6424 cerr << "ERROR: source zone not specified" << std::endl;
6425 return EINVAL;
6426 }
6427 if (bucket_name.empty()) {
6428 cerr << "ERROR: bucket not specified" << std::endl;
6429 return EINVAL;
6430 }
6431 rgw_bucket bucket;
6432 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6433 if (ret < 0) {
6434 return -ret;
6435 }
6436 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6437
6438 ret = sync.init();
6439 if (ret < 0) {
6440 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6441 return -ret;
6442 }
6443 ret = sync.init_sync_status();
6444 if (ret < 0) {
6445 cerr << "ERROR: sync.init_sync_status() returned ret=" << ret << std::endl;
6446 return -ret;
6447 }
6448 }
6449
6450 if ((opt_cmd == OPT_BUCKET_SYNC_DISABLE) || (opt_cmd == OPT_BUCKET_SYNC_ENABLE)) {
6451 if (bucket_name.empty()) {
6452 cerr << "ERROR: bucket not specified" << std::endl;
6453 return EINVAL;
6454 }
6455
6456 if (ret < 0) {
6457 cerr << "could not init realm " << ": " << cpp_strerror(-ret) << std::endl;
6458 return ret;
6459 }
6460 RGWPeriod period;
6461 ret = period.init(g_ceph_context, store, realm_id, realm_name, true);
6462 if (ret < 0) {
6463 cerr << "failed to init period " << ": " << cpp_strerror(-ret) << std::endl;
6464 return ret;
6465 }
6466
6467 if (!store->is_meta_master()) {
6468 cerr << "failed to update bucket sync: only allowed on meta master zone " << std::endl;
6469 cerr << period.get_master_zone() << " | " << period.get_realm() << std::endl;
6470 return EINVAL;
6471 }
6472
6473 rgw_obj obj(bucket, object);
6474 ret = set_bucket_sync_enabled(store, opt_cmd, tenant, bucket_name);
6475 if (ret < 0)
6476 return -ret;
6477 }
6478
6479 if (opt_cmd == OPT_BUCKET_SYNC_STATUS) {
6480 if (source_zone.empty()) {
6481 cerr << "ERROR: source zone not specified" << std::endl;
6482 return EINVAL;
6483 }
6484 if (bucket_name.empty()) {
6485 cerr << "ERROR: bucket not specified" << std::endl;
6486 return EINVAL;
6487 }
6488 rgw_bucket bucket;
6489 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6490 if (ret < 0) {
6491 return -ret;
6492 }
6493 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6494
6495 ret = sync.init();
6496 if (ret < 0) {
6497 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6498 return -ret;
6499 }
6500 ret = sync.read_sync_status();
6501 if (ret < 0) {
6502 cerr << "ERROR: sync.read_sync_status() returned ret=" << ret << std::endl;
6503 return -ret;
6504 }
6505
6506 map<int, rgw_bucket_shard_sync_info>& sync_status = sync.get_sync_status();
6507
6508 encode_json("sync_status", sync_status, formatter);
6509 formatter->flush(cout);
6510 }
6511
6512 if (opt_cmd == OPT_BUCKET_SYNC_RUN) {
6513 if (source_zone.empty()) {
6514 cerr << "ERROR: source zone not specified" << std::endl;
6515 return EINVAL;
6516 }
6517 if (bucket_name.empty()) {
6518 cerr << "ERROR: bucket not specified" << std::endl;
6519 return EINVAL;
6520 }
6521 rgw_bucket bucket;
6522 int ret = init_bucket_for_sync(tenant, bucket_name, bucket_id, bucket);
6523 if (ret < 0) {
6524 return -ret;
6525 }
6526 RGWBucketSyncStatusManager sync(store, source_zone, bucket);
6527
6528 ret = sync.init();
6529 if (ret < 0) {
6530 cerr << "ERROR: sync.init() returned ret=" << ret << std::endl;
6531 return -ret;
6532 }
6533
6534 ret = sync.run();
6535 if (ret < 0) {
6536 cerr << "ERROR: sync.run() returned ret=" << ret << std::endl;
6537 return -ret;
6538 }
6539 }
6540
6541 if (opt_cmd == OPT_BILOG_LIST) {
6542 if (bucket_name.empty()) {
6543 cerr << "ERROR: bucket not specified" << std::endl;
6544 return EINVAL;
6545 }
6546 RGWBucketInfo bucket_info;
6547 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6548 if (ret < 0) {
6549 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6550 return -ret;
6551 }
6552 formatter->open_array_section("entries");
6553 bool truncated;
6554 int count = 0;
6555 if (max_entries < 0)
6556 max_entries = 1000;
6557
6558 do {
6559 list<rgw_bi_log_entry> entries;
6560 ret = store->list_bi_log_entries(bucket_info, shard_id, marker, max_entries - count, entries, &truncated);
6561 if (ret < 0) {
6562 cerr << "ERROR: list_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
6563 return -ret;
6564 }
6565
6566 count += entries.size();
6567
6568 for (list<rgw_bi_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6569 rgw_bi_log_entry& entry = *iter;
6570 encode_json("entry", entry, formatter);
6571
6572 marker = entry.id;
6573 }
6574 formatter->flush(cout);
6575 } while (truncated && count < max_entries);
6576
6577 formatter->close_section();
6578 formatter->flush(cout);
6579 }
6580
6581 if (opt_cmd == OPT_SYNC_ERROR_LIST) {
6582 if (max_entries < 0) {
6583 max_entries = 1000;
6584 }
6585
6586 bool truncated;
6587 utime_t start_time, end_time;
6588
6589 int ret = parse_date_str(start_date, start_time);
6590 if (ret < 0)
6591 return -ret;
6592
6593 ret = parse_date_str(end_date, end_time);
6594 if (ret < 0)
6595 return -ret;
6596
6597 if (shard_id < 0) {
6598 shard_id = 0;
6599 }
6600
6601 formatter->open_array_section("entries");
6602
6603 for (; shard_id < ERROR_LOGGER_SHARDS; ++shard_id) {
6604 formatter->open_object_section("shard");
6605 encode_json("shard_id", shard_id, formatter);
6606 formatter->open_array_section("entries");
6607
6608 int count = 0;
6609 string oid = RGWSyncErrorLogger::get_shard_oid(RGW_SYNC_ERROR_LOG_SHARD_PREFIX, shard_id);
6610
6611 do {
6612 list<cls_log_entry> entries;
6613 ret = store->time_log_list(oid, start_time.to_real_time(), end_time.to_real_time(),
6614 max_entries - count, entries, marker, &marker, &truncated);
6615 if (ret == -ENOENT) {
6616 break;
6617 }
6618 if (ret < 0) {
6619 cerr << "ERROR: store->time_log_list(): " << cpp_strerror(-ret) << std::endl;
6620 return -ret;
6621 }
6622
6623 count += entries.size();
6624
6625 for (auto& cls_entry : entries) {
6626 rgw_sync_error_info log_entry;
6627
6628 auto iter = cls_entry.data.begin();
6629 try {
6630 ::decode(log_entry, iter);
6631 } catch (buffer::error& err) {
6632 cerr << "ERROR: failed to decode log entry" << std::endl;
6633 continue;
6634 }
6635 formatter->open_object_section("entry");
6636 encode_json("id", cls_entry.id, formatter);
6637 encode_json("section", cls_entry.section, formatter);
6638 encode_json("name", cls_entry.name, formatter);
6639 encode_json("timestamp", cls_entry.timestamp, formatter);
6640 encode_json("info", log_entry, formatter);
6641 formatter->close_section();
6642 formatter->flush(cout);
6643 }
6644 } while (truncated && count < max_entries);
6645
6646 formatter->close_section();
6647 formatter->close_section();
6648
6649 if (specified_shard_id) {
6650 break;
6651 }
6652 }
6653
6654 formatter->close_section();
6655 formatter->flush(cout);
6656 }
6657
6658 if (opt_cmd == OPT_BILOG_TRIM) {
6659 if (bucket_name.empty()) {
6660 cerr << "ERROR: bucket not specified" << std::endl;
6661 return EINVAL;
6662 }
6663 RGWBucketInfo bucket_info;
6664 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6665 if (ret < 0) {
6666 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6667 return -ret;
6668 }
6669 ret = store->trim_bi_log_entries(bucket_info, shard_id, start_marker, end_marker);
6670 if (ret < 0) {
6671 cerr << "ERROR: trim_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
6672 return -ret;
6673 }
6674 }
6675
6676 if (opt_cmd == OPT_BILOG_STATUS) {
6677 if (bucket_name.empty()) {
6678 cerr << "ERROR: bucket not specified" << std::endl;
6679 return EINVAL;
6680 }
6681 RGWBucketInfo bucket_info;
6682 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6683 if (ret < 0) {
6684 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6685 return -ret;
6686 }
6687 map<int, string> markers;
6688 ret = store->get_bi_log_status(bucket_info, shard_id, markers);
6689 if (ret < 0) {
6690 cerr << "ERROR: get_bi_log_status(): " << cpp_strerror(-ret) << std::endl;
6691 return -ret;
6692 }
6693 formatter->open_object_section("entries");
6694 encode_json("markers", markers, formatter);
6695 formatter->close_section();
6696 formatter->flush(cout);
6697 }
6698
6699 if (opt_cmd == OPT_DATALOG_LIST) {
6700 formatter->open_array_section("entries");
6701 bool truncated;
6702 int count = 0;
6703 if (max_entries < 0)
6704 max_entries = 1000;
6705
6706 utime_t start_time, end_time;
6707
6708 int ret = parse_date_str(start_date, start_time);
6709 if (ret < 0)
6710 return -ret;
6711
6712 ret = parse_date_str(end_date, end_time);
6713 if (ret < 0)
6714 return -ret;
6715
6716 RGWDataChangesLog *log = store->data_log;
6717 RGWDataChangesLog::LogMarker marker;
6718
6719 do {
6720 list<rgw_data_change_log_entry> entries;
6721 ret = log->list_entries(start_time.to_real_time(), end_time.to_real_time(), max_entries - count, entries, marker, &truncated);
6722 if (ret < 0) {
6723 cerr << "ERROR: list_bi_log_entries(): " << cpp_strerror(-ret) << std::endl;
6724 return -ret;
6725 }
6726
6727 count += entries.size();
6728
6729 for (list<rgw_data_change_log_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6730 rgw_data_change_log_entry& entry = *iter;
6731 if (!extra_info) {
6732 encode_json("entry", entry.entry, formatter);
6733 } else {
6734 encode_json("entry", entry, formatter);
6735 }
6736 }
6737 formatter->flush(cout);
6738 } while (truncated && count < max_entries);
6739
6740 formatter->close_section();
6741 formatter->flush(cout);
6742 }
6743
6744 if (opt_cmd == OPT_DATALOG_STATUS) {
6745 RGWDataChangesLog *log = store->data_log;
6746 int i = (specified_shard_id ? shard_id : 0);
6747
6748 formatter->open_array_section("entries");
6749 for (; i < g_ceph_context->_conf->rgw_data_log_num_shards; i++) {
6750 list<cls_log_entry> entries;
6751
6752 RGWDataChangesLogInfo info;
6753 log->get_info(i, &info);
6754
6755 ::encode_json("info", info, formatter);
6756
6757 if (specified_shard_id)
6758 break;
6759 }
6760
6761 formatter->close_section();
6762 formatter->flush(cout);
6763 }
6764
6765 if (opt_cmd == OPT_DATALOG_TRIM) {
6766 utime_t start_time, end_time;
6767
6768 int ret = parse_date_str(start_date, start_time);
6769 if (ret < 0)
6770 return -ret;
6771
6772 ret = parse_date_str(end_date, end_time);
6773 if (ret < 0)
6774 return -ret;
6775
6776 RGWDataChangesLog *log = store->data_log;
6777 ret = log->trim_entries(start_time.to_real_time(), end_time.to_real_time(), start_marker, end_marker);
6778 if (ret < 0) {
6779 cerr << "ERROR: trim_entries(): " << cpp_strerror(-ret) << std::endl;
6780 return -ret;
6781 }
6782 }
6783
6784 if (opt_cmd == OPT_OPSTATE_LIST) {
6785 RGWOpState oc(store);
6786
6787 int max = 1000;
6788
6789 void *handle;
6790 oc.init_list_entries(client_id, op_id, object, &handle);
6791 list<cls_statelog_entry> entries;
6792 bool done;
6793 formatter->open_array_section("entries");
6794 do {
6795 int ret = oc.list_entries(handle, max, entries, &done);
6796 if (ret < 0) {
6797 cerr << "oc.list_entries returned " << cpp_strerror(-ret) << std::endl;
6798 oc.finish_list_entries(handle);
6799 return -ret;
6800 }
6801
6802 for (list<cls_statelog_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
6803 oc.dump_entry(*iter, formatter);
6804 }
6805
6806 formatter->flush(cout);
6807 } while (!done);
6808 formatter->close_section();
6809 formatter->flush(cout);
6810 oc.finish_list_entries(handle);
6811 }
6812
6813 if (opt_cmd == OPT_OPSTATE_SET || opt_cmd == OPT_OPSTATE_RENEW) {
6814 RGWOpState oc(store);
6815
6816 RGWOpState::OpState state;
6817 if (object.empty() || client_id.empty() || op_id.empty()) {
6818 cerr << "ERROR: need to specify client_id, op_id, and object" << std::endl;
6819 return EINVAL;
6820 }
6821 if (state_str.empty()) {
6822 cerr << "ERROR: state was not specified" << std::endl;
6823 return EINVAL;
6824 }
6825 int ret = oc.state_from_str(state_str, &state);
6826 if (ret < 0) {
6827 cerr << "ERROR: invalid state: " << state_str << std::endl;
6828 return -ret;
6829 }
6830
6831 if (opt_cmd == OPT_OPSTATE_SET) {
6832 ret = oc.set_state(client_id, op_id, object, state);
6833 if (ret < 0) {
6834 cerr << "ERROR: failed to set state: " << cpp_strerror(-ret) << std::endl;
6835 return -ret;
6836 }
6837 } else {
6838 ret = oc.renew_state(client_id, op_id, object, state);
6839 if (ret < 0) {
6840 cerr << "ERROR: failed to renew state: " << cpp_strerror(-ret) << std::endl;
6841 return -ret;
6842 }
6843 }
6844 }
6845 if (opt_cmd == OPT_OPSTATE_RM) {
6846 RGWOpState oc(store);
6847
6848 if (object.empty() || client_id.empty() || op_id.empty()) {
6849 cerr << "ERROR: need to specify client_id, op_id, and object" << std::endl;
6850 return EINVAL;
6851 }
6852 ret = oc.remove_entry(client_id, op_id, object);
6853 if (ret < 0) {
6854 cerr << "ERROR: failed to set state: " << cpp_strerror(-ret) << std::endl;
6855 return -ret;
6856 }
6857 }
6858
6859 if (opt_cmd == OPT_REPLICALOG_GET || opt_cmd == OPT_REPLICALOG_UPDATE ||
6860 opt_cmd == OPT_REPLICALOG_DELETE) {
6861 if (replica_log_type_str.empty()) {
6862 cerr << "ERROR: need to specify --replica-log-type=<metadata | data | bucket>" << std::endl;
6863 return EINVAL;
6864 }
6865 }
6866
6867 if (opt_cmd == OPT_REPLICALOG_GET) {
6868 RGWReplicaBounds bounds;
6869 if (replica_log_type == ReplicaLog_Metadata) {
6870 if (!specified_shard_id) {
6871 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6872 return EINVAL;
6873 }
6874
6875 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
6876 int ret = logger.get_bounds(shard_id, bounds);
6877 if (ret < 0)
6878 return -ret;
6879 } else if (replica_log_type == ReplicaLog_Data) {
6880 if (!specified_shard_id) {
6881 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6882 return EINVAL;
6883 }
6884 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
6885 int ret = logger.get_bounds(shard_id, bounds);
6886 if (ret < 0)
6887 return -ret;
6888 } else if (replica_log_type == ReplicaLog_Bucket) {
6889 if (bucket_name.empty()) {
6890 cerr << "ERROR: bucket not specified" << std::endl;
6891 return EINVAL;
6892 }
6893 RGWBucketInfo bucket_info;
6894 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6895 if (ret < 0) {
6896 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6897 return -ret;
6898 }
6899
6900 RGWReplicaBucketLogger logger(store);
6901 ret = logger.get_bounds(bucket, shard_id, bounds);
6902 if (ret < 0)
6903 return -ret;
6904 } else { // shouldn't get here
6905 ceph_abort();
6906 }
6907 encode_json("bounds", bounds, formatter);
6908 formatter->flush(cout);
6909 cout << std::endl;
6910 }
6911
6912 if (opt_cmd == OPT_REPLICALOG_DELETE) {
6913 if (replica_log_type == ReplicaLog_Metadata) {
6914 if (!specified_shard_id) {
6915 cerr << "ERROR: shard-id must be specified for delete operation" << std::endl;
6916 return EINVAL;
6917 }
6918 if (!specified_daemon_id) {
6919 cerr << "ERROR: daemon-id must be specified for delete operation" << std::endl;
6920 return EINVAL;
6921 }
6922 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
6923 int ret = logger.delete_bound(shard_id, daemon_id, false);
6924 if (ret < 0)
6925 return -ret;
6926 } else if (replica_log_type == ReplicaLog_Data) {
6927 if (!specified_shard_id) {
6928 cerr << "ERROR: shard-id must be specified for delete operation" << std::endl;
6929 return EINVAL;
6930 }
6931 if (!specified_daemon_id) {
6932 cerr << "ERROR: daemon-id must be specified for delete operation" << std::endl;
6933 return EINVAL;
6934 }
6935 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
6936 int ret = logger.delete_bound(shard_id, daemon_id, false);
6937 if (ret < 0)
6938 return -ret;
6939 } else if (replica_log_type == ReplicaLog_Bucket) {
6940 if (bucket_name.empty()) {
6941 cerr << "ERROR: bucket not specified" << std::endl;
6942 return EINVAL;
6943 }
6944 RGWBucketInfo bucket_info;
6945 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
6946 if (ret < 0) {
6947 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
6948 return -ret;
6949 }
6950
6951 RGWReplicaBucketLogger logger(store);
6952 ret = logger.delete_bound(bucket, shard_id, daemon_id, false);
6953 if (ret < 0)
6954 return -ret;
6955 }
6956 }
6957
6958 if (opt_cmd == OPT_REPLICALOG_UPDATE) {
6959 if (marker.empty()) {
6960 cerr << "ERROR: marker was not specified" <<std::endl;
6961 return EINVAL;
6962 }
6963 utime_t time = ceph_clock_now();
6964 if (!date.empty()) {
6965 ret = parse_date_str(date, time);
6966 if (ret < 0) {
6967 cerr << "ERROR: failed to parse start date" << std::endl;
6968 return EINVAL;
6969 }
6970 }
6971 list<RGWReplicaItemMarker> entries;
6972 int ret = read_decode_json(infile, entries);
6973 if (ret < 0) {
6974 cerr << "ERROR: failed to decode entries" << std::endl;
6975 return EINVAL;
6976 }
6977 RGWReplicaBounds bounds;
6978 if (replica_log_type == ReplicaLog_Metadata) {
6979 if (!specified_shard_id) {
6980 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6981 return EINVAL;
6982 }
6983
6984 RGWReplicaObjectLogger logger(store, pool, META_REPLICA_LOG_OBJ_PREFIX);
6985 int ret = logger.update_bound(shard_id, daemon_id, marker, time, &entries);
6986 if (ret < 0) {
6987 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
6988 return -ret;
6989 }
6990 } else if (replica_log_type == ReplicaLog_Data) {
6991 if (!specified_shard_id) {
6992 cerr << "ERROR: shard-id must be specified for get operation" << std::endl;
6993 return EINVAL;
6994 }
6995 RGWReplicaObjectLogger logger(store, pool, DATA_REPLICA_LOG_OBJ_PREFIX);
6996 int ret = logger.update_bound(shard_id, daemon_id, marker, time, &entries);
6997 if (ret < 0) {
6998 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
6999 return -ret;
7000 }
7001 } else if (replica_log_type == ReplicaLog_Bucket) {
7002 if (bucket_name.empty()) {
7003 cerr << "ERROR: bucket not specified" << std::endl;
7004 return EINVAL;
7005 }
7006 RGWBucketInfo bucket_info;
7007 int ret = init_bucket(tenant, bucket_name, bucket_id, bucket_info, bucket);
7008 if (ret < 0) {
7009 cerr << "ERROR: could not init bucket: " << cpp_strerror(-ret) << std::endl;
7010 return -ret;
7011 }
7012
7013 RGWReplicaBucketLogger logger(store);
7014 ret = logger.update_bound(bucket, shard_id, daemon_id, marker, time, &entries);
7015 if (ret < 0) {
7016 cerr << "ERROR: failed to update bounds: " << cpp_strerror(-ret) << std::endl;
7017 return -ret;
7018 }
7019 }
7020 }
7021
7022 bool quota_op = (opt_cmd == OPT_QUOTA_SET || opt_cmd == OPT_QUOTA_ENABLE || opt_cmd == OPT_QUOTA_DISABLE);
7023
7024 if (quota_op) {
7025 if (bucket_name.empty() && user_id.empty()) {
7026 cerr << "ERROR: bucket name or uid is required for quota operation" << std::endl;
7027 return EINVAL;
7028 }
7029
7030 if (!bucket_name.empty()) {
7031 if (!quota_scope.empty() && quota_scope != "bucket") {
7032 cerr << "ERROR: invalid quota scope specification." << std::endl;
7033 return EINVAL;
7034 }
7035 set_bucket_quota(store, opt_cmd, tenant, bucket_name,
7036 max_size, max_objects, have_max_size, have_max_objects);
7037 } else if (!user_id.empty()) {
7038 if (quota_scope == "bucket") {
7039 set_user_bucket_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects);
7040 } else if (quota_scope == "user") {
7041 set_user_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects);
7042 } else {
7043 cerr << "ERROR: invalid quota scope specification. Please specify either --quota-scope=bucket, or --quota-scope=user" << std::endl;
7044 return EINVAL;
7045 }
7046 }
7047 }
7048
7049 return 0;
7050 }