Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_dencoder.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 "rgw_common.h"
5 #include "rgw_rados.h"
6 #include "rgw_log.h"
7 #include "rgw_acl.h"
8 #include "rgw_acl_s3.h"
9 #include "rgw_cache.h"
10
11 #include "common/Formatter.h"
12
13 static string shadow_ns = RGW_OBJ_NS_SHADOW;
14
15 static void init_bucket(rgw_bucket *b, const char *t, const char *n, const char *dp, const char *ip, const char *m, const char *id)
16 {
17   b->tenant = t;
18   b->name = n;
19   b->marker = m;
20   b->bucket_id = id;
21   b->explicit_placement.data_pool = rgw_pool(dp);
22   b->explicit_placement.index_pool = rgw_pool(ip);
23 }
24
25 void RGWObjManifestPart::generate_test_instances(std::list<RGWObjManifestPart*>& o)
26 {
27   o.push_back(new RGWObjManifestPart);
28
29   RGWObjManifestPart *p = new RGWObjManifestPart;
30   rgw_bucket b;
31   init_bucket(&b, "tenant", "bucket", ".pool", ".index_pool", "marker_", "12");
32
33   p->loc = rgw_obj(b, "object");
34   p->loc_ofs = 512 * 1024;
35   p->size = 128 * 1024;
36   o.push_back(p);
37 }
38
39 void RGWObjManifest::obj_iterator::seek(uint64_t o)
40 {
41   ofs = o;
42   if (manifest->explicit_objs) {
43     explicit_iter = manifest->objs.upper_bound(ofs);
44     if (explicit_iter != manifest->objs.begin()) {
45       --explicit_iter;
46     }
47     if (ofs >= manifest->obj_size) {
48       ofs = manifest->obj_size;
49       return;
50     }
51     update_explicit_pos();
52     update_location();
53     return;
54   }
55   if (o < manifest->get_head_size()) {
56     rule_iter = manifest->rules.begin();
57     stripe_ofs = 0;
58     stripe_size = manifest->get_head_size();
59     if (rule_iter != manifest->rules.end()) {
60       cur_part_id = rule_iter->second.start_part_num;
61       cur_override_prefix = rule_iter->second.override_prefix;
62     }
63     update_location();
64     return;
65   }
66
67   rule_iter = manifest->rules.upper_bound(ofs);
68   next_rule_iter = rule_iter;
69   if (rule_iter != manifest->rules.begin()) {
70     --rule_iter;
71   }
72
73   if (rule_iter == manifest->rules.end()) {
74     update_location();
75     return;
76   }
77
78   RGWObjManifestRule& rule = rule_iter->second;
79
80   if (rule.part_size > 0) {
81     cur_part_id = rule.start_part_num + (ofs - rule.start_ofs) / rule.part_size;
82   } else {
83     cur_part_id = rule.start_part_num;
84   }
85   part_ofs = rule.start_ofs + (cur_part_id - rule.start_part_num) * rule.part_size;
86
87   if (rule.stripe_max_size > 0) {
88     cur_stripe = (ofs - part_ofs) / rule.stripe_max_size;
89
90     stripe_ofs = part_ofs + cur_stripe * rule.stripe_max_size;
91     if (!cur_part_id && manifest->get_head_size() > 0) {
92       cur_stripe++;
93     }
94   } else {
95     cur_stripe = 0;
96     stripe_ofs = part_ofs;
97   }
98
99   if (!rule.part_size) {
100     stripe_size = rule.stripe_max_size;
101     stripe_size = MIN(manifest->get_obj_size() - stripe_ofs, stripe_size);
102   } else {
103     uint64_t next = MIN(stripe_ofs + rule.stripe_max_size, part_ofs + rule.part_size);
104     stripe_size = next - stripe_ofs;
105   }
106
107   cur_override_prefix = rule.override_prefix;
108
109   update_location();
110 }
111
112 void RGWObjManifest::obj_iterator::update_location()
113 {
114   if (manifest->explicit_objs) {
115     location = explicit_iter->second.loc;
116     return;
117   }
118
119   if (ofs < manifest->get_head_size()) {
120     location = manifest->get_obj();
121     location.set_placement_rule(manifest->get_head_placement_rule());
122     return;
123   }
124
125   manifest->get_implicit_location(cur_part_id, cur_stripe, ofs, &cur_override_prefix, &location);
126 }
127
128 void RGWObjManifest::obj_iterator::update_explicit_pos()
129 {
130   ofs = explicit_iter->first;
131   stripe_ofs = ofs;
132
133   map<uint64_t, RGWObjManifestPart>::iterator next_iter = explicit_iter;
134   ++next_iter;
135   if (next_iter != manifest->objs.end()) {
136     stripe_size = next_iter->first - ofs;
137   } else {
138     stripe_size = manifest->obj_size - ofs;
139   }
140 }
141
142 void RGWObjManifest::generate_test_instances(std::list<RGWObjManifest*>& o)
143 {
144   RGWObjManifest *m = new RGWObjManifest;
145   for (int i = 0; i<10; i++) {
146     RGWObjManifestPart p;
147     rgw_bucket b;
148     init_bucket(&b, "tenant", "bucket", ".pool", ".index_pool", "marker_", "12");
149     p.loc = rgw_obj(b, "object");
150     p.loc_ofs = 0;
151     p.size = 512 * 1024;
152     m->objs[(uint64_t)i * 512 * 1024] = p;
153   }
154   m->obj_size = 5 * 1024 * 1024;
155
156   o.push_back(m);
157
158   o.push_back(new RGWObjManifest);
159 }
160
161 void RGWObjManifest::get_implicit_location(uint64_t cur_part_id, uint64_t cur_stripe, uint64_t ofs, string *override_prefix, rgw_obj_select *location)
162 {
163   rgw_obj loc;
164
165   string& oid = loc.key.name;
166   string& ns = loc.key.ns;
167
168   if (!override_prefix || override_prefix->empty()) {
169     oid = prefix;
170   } else {
171     oid = *override_prefix;
172   }
173
174   if (!cur_part_id) {
175     if (ofs < max_head_size) {
176       location->set_placement_rule(head_placement_rule);
177       *location = obj;
178       return;
179     } else {
180       char buf[16];
181       snprintf(buf, sizeof(buf), "%d", (int)cur_stripe);
182       oid += buf;
183       ns = shadow_ns;
184     }
185   } else {
186     char buf[32];
187     if (cur_stripe == 0) {
188       snprintf(buf, sizeof(buf), ".%d", (int)cur_part_id);
189       oid += buf;
190       ns= RGW_OBJ_NS_MULTIPART;
191     } else {
192       snprintf(buf, sizeof(buf), ".%d_%d", (int)cur_part_id, (int)cur_stripe);
193       oid += buf;
194       ns = shadow_ns;
195     }
196   }
197
198   if (!tail_placement.bucket.name.empty()) {
199     loc.bucket = tail_placement.bucket;
200   } else {
201     loc.bucket = obj.bucket;
202   }
203
204   // Always overwrite instance with tail_instance
205   // to get the right shadow object location
206   loc.key.set_instance(tail_instance);
207
208   location->set_placement_rule(tail_placement.placement_rule);
209   *location = loc;
210 }
211
212
213
214 void rgw_log_entry::generate_test_instances(list<rgw_log_entry*>& o)
215 {
216   rgw_log_entry *e = new rgw_log_entry;
217   e->object_owner = "object_owner";
218   e->bucket_owner = "bucket_owner";
219   e->bucket = "bucket";
220   e->remote_addr = "1.2.3.4";
221   e->user = "user";
222   e->obj = rgw_obj_key("obj");
223   e->uri = "http://uri/bucket/obj";
224   e->http_status = "200";
225   e->error_code = "error_code";
226   e->bytes_sent = 1024;
227   e->bytes_received = 512;
228   e->obj_size = 2048;
229   e->user_agent = "user_agent";
230   e->referrer = "referrer";
231   e->bucket_id = "10";
232   o.push_back(e);
233   o.push_back(new rgw_log_entry);
234 }
235
236 void ACLPermission::generate_test_instances(list<ACLPermission*>& o)
237 {
238   ACLPermission *p = new ACLPermission;
239   p->set_permissions(RGW_PERM_WRITE_ACP);
240   o.push_back(p);
241   o.push_back(new ACLPermission);
242 }
243
244 void ACLGranteeType::generate_test_instances(list<ACLGranteeType*>& o)
245 {
246   ACLGranteeType *t = new ACLGranteeType;
247   t->set(ACL_TYPE_CANON_USER);
248   o.push_back(t);
249   o.push_back(new ACLGranteeType);
250 }
251
252 /* the following is copied here from rgw_acl_s3.cc, to avoid having to have excessive linking
253    with everything it needs */
254
255 #define RGW_URI_ALL_USERS       "http://acs.amazonaws.com/groups/global/AllUsers"
256 #define RGW_URI_AUTH_USERS      "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
257
258 static string rgw_uri_all_users = RGW_URI_ALL_USERS;
259 static string rgw_uri_auth_users = RGW_URI_AUTH_USERS;
260
261 ACLGroupTypeEnum ACLGrant::uri_to_group(string& uri)
262 {
263   // this is required for backward compatibility
264   return ACLGrant_S3::uri_to_group(uri);
265 }
266
267 ACLGroupTypeEnum ACLGrant_S3::uri_to_group(string& uri)
268 {
269   if (uri.compare(rgw_uri_all_users) == 0)
270     return ACL_GROUP_ALL_USERS;
271   else if (uri.compare(rgw_uri_auth_users) == 0)
272     return ACL_GROUP_AUTHENTICATED_USERS;
273
274   return ACL_GROUP_NONE;
275 }
276
277 void ACLGrant::generate_test_instances(list<ACLGrant*>& o)
278 {
279   rgw_user id("rgw");
280   string name, email;
281   name = "Mr. RGW";
282   email = "r@gw";
283
284   ACLGrant *g1 = new ACLGrant;
285   g1->set_canon(id, name, RGW_PERM_READ);
286   g1->email = email;
287   o.push_back(g1);
288
289   ACLGrant *g2 = new ACLGrant;
290   g1->set_group(ACL_GROUP_AUTHENTICATED_USERS, RGW_PERM_WRITE);
291   o.push_back(g2);
292
293   o.push_back(new ACLGrant);
294 }
295
296 void RGWAccessControlList::generate_test_instances(list<RGWAccessControlList*>& o)
297 {
298   RGWAccessControlList *acl = new RGWAccessControlList(NULL);
299
300   list<ACLGrant *> glist;
301   list<ACLGrant *>::iterator iter;
302
303   ACLGrant::generate_test_instances(glist);
304   for (iter = glist.begin(); iter != glist.end(); ++iter) {
305     ACLGrant *grant = *iter;
306     acl->add_grant(grant);
307
308     delete grant;
309   }
310   o.push_back(acl);
311   o.push_back(new RGWAccessControlList(NULL));
312 }
313
314 void ACLOwner::generate_test_instances(list<ACLOwner*>& o)
315 {
316   ACLOwner *owner = new ACLOwner;
317   owner->id = "rgw";
318   owner->display_name = "Mr. RGW";
319   o.push_back(owner);
320   o.push_back(new ACLOwner);
321 }
322
323 void RGWAccessControlPolicy::generate_test_instances(list<RGWAccessControlPolicy*>& o)
324 {
325   list<RGWAccessControlList *> acl_list;
326   list<RGWAccessControlList *>::iterator iter;
327   for (iter = acl_list.begin(); iter != acl_list.end(); ++iter) {
328     RGWAccessControlList::generate_test_instances(acl_list);
329     iter = acl_list.begin();
330
331     RGWAccessControlPolicy *p = new RGWAccessControlPolicy(NULL);
332     RGWAccessControlList *l = *iter;
333     p->acl = *l;
334
335     string name = "radosgw";
336     rgw_user id("rgw");
337     p->owner.set_name(name);
338     p->owner.set_id(id);
339
340     o.push_back(p);
341
342     delete l;
343   }
344
345   o.push_back(new RGWAccessControlPolicy(NULL));
346 }
347
348
349 void ObjectMetaInfo::generate_test_instances(list<ObjectMetaInfo*>& o)
350 {
351   ObjectMetaInfo *m = new ObjectMetaInfo;
352   m->size = 1024 * 1024;
353   o.push_back(m);
354   o.push_back(new ObjectMetaInfo);
355 }
356
357 void ObjectCacheInfo::generate_test_instances(list<ObjectCacheInfo*>& o)
358 {
359   ObjectCacheInfo *i = new ObjectCacheInfo;
360   i->status = 0;
361   i->flags = CACHE_FLAG_MODIFY_XATTRS;
362   string s = "this is a string";
363   string s2 = "this is a another string";
364   bufferlist data, data2;
365   ::encode(s, data);
366   ::encode(s2, data2);
367   i->data = data;
368   i->xattrs["x1"] = data;
369   i->xattrs["x2"] = data2;
370   i->rm_xattrs["r2"] = data2;
371   i->rm_xattrs["r3"] = data;
372   i->meta.size = 512 * 1024;
373   o.push_back(i);
374   o.push_back(new ObjectCacheInfo);
375 }
376
377 void RGWCacheNotifyInfo::generate_test_instances(list<RGWCacheNotifyInfo*>& o)
378 {
379   o.push_back(new RGWCacheNotifyInfo);
380 }
381
382 void RGWAccessKey::generate_test_instances(list<RGWAccessKey*>& o)
383 {
384   RGWAccessKey *k = new RGWAccessKey;
385   k->id = "id";
386   k->key = "key";
387   k->subuser = "subuser";
388   o.push_back(k);
389   o.push_back(new RGWAccessKey);
390 }
391
392 void RGWSubUser::generate_test_instances(list<RGWSubUser*>& o)
393 {
394   RGWSubUser *u = new RGWSubUser;
395   u->name = "name";
396   u->perm_mask = 0xf;
397   o.push_back(u);
398   o.push_back(new RGWSubUser);
399 }
400
401 void RGWUserInfo::generate_test_instances(list<RGWUserInfo*>& o)
402 {
403   RGWUserInfo *i = new RGWUserInfo;
404   i->auid = 1;
405   i->user_id = "user_id";
406   i->display_name =  "display_name";
407   i->user_email = "user@email";
408   RGWAccessKey k1, k2;
409   k1.id = "id1";
410   k1.key = "key1";
411   k2.id = "id2";
412   k2.subuser = "subuser";
413   RGWSubUser u;
414   u.name = "id2";
415   u.perm_mask = 0x1;
416   i->access_keys[k1.id] = k1;
417   i->swift_keys[k2.id] = k2;
418   i->subusers[u.name] = u;
419   o.push_back(i);
420
421   o.push_back(new RGWUserInfo);
422 }
423
424 void rgw_bucket::generate_test_instances(list<rgw_bucket*>& o)
425 {
426   rgw_bucket *b = new rgw_bucket;
427   init_bucket(b, "tenant", "name", "pool", ".index_pool", "marker", "123");
428   o.push_back(b);
429   o.push_back(new rgw_bucket);
430 }
431
432 void RGWBucketInfo::generate_test_instances(list<RGWBucketInfo*>& o)
433 {
434   RGWBucketInfo *i = new RGWBucketInfo;
435   init_bucket(&i->bucket, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
436   i->owner = "owner";
437   i->flags = BUCKET_SUSPENDED;
438   o.push_back(i);
439   o.push_back(new RGWBucketInfo);
440 }
441
442 void RGWZoneGroup::generate_test_instances(list<RGWZoneGroup*>& o)
443 {
444   RGWZoneGroup *r = new RGWZoneGroup;
445   o.push_back(r);
446   o.push_back(new RGWZoneGroup);
447 }
448
449 void RGWZone::generate_test_instances(list<RGWZone*> &o)
450 {
451   RGWZone *z = new RGWZone;
452   o.push_back(z);
453   o.push_back(new RGWZone);
454 }
455
456 void RGWZoneParams::generate_test_instances(list<RGWZoneParams*> &o)
457 {
458   o.push_back(new RGWZoneParams);
459   o.push_back(new RGWZoneParams); 
460 }
461
462 void RGWOLHInfo::generate_test_instances(list<RGWOLHInfo*> &o)
463 {
464   RGWOLHInfo *olh = new RGWOLHInfo;
465   olh->removed = false;
466   o.push_back(olh);
467   o.push_back(new RGWOLHInfo);
468 }
469
470 void RGWBucketEnt::generate_test_instances(list<RGWBucketEnt*>& o)
471 {
472   RGWBucketEnt *e = new RGWBucketEnt;
473   init_bucket(&e->bucket, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
474   e->size = 1024;
475   e->size_rounded = 4096;
476   e->count = 1;
477   o.push_back(e);
478   o.push_back(new RGWBucketEnt);
479 }
480
481 void RGWUploadPartInfo::generate_test_instances(list<RGWUploadPartInfo*>& o)
482 {
483   RGWUploadPartInfo *i = new RGWUploadPartInfo;
484   i->num = 1;
485   i->size = 10 * 1024 * 1024;
486   i->etag = "etag";
487   o.push_back(i);
488   o.push_back(new RGWUploadPartInfo);
489 }
490
491 void rgw_obj::generate_test_instances(list<rgw_obj*>& o)
492 {
493   rgw_bucket b;
494   init_bucket(&b, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
495   rgw_obj *obj = new rgw_obj(b, "object");
496   o.push_back(obj);
497   o.push_back(new rgw_obj);
498 }
499