Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / fs_types.cc
1
2 #include "include/fs_types.h"
3 #include "common/Formatter.h"
4 #include "include/ceph_features.h"
5
6 void dump(const ceph_file_layout& l, Formatter *f)
7 {
8   f->dump_unsigned("stripe_unit", l.fl_stripe_unit);
9   f->dump_unsigned("stripe_count", l.fl_stripe_count);
10   f->dump_unsigned("object_size", l.fl_object_size);
11   if (l.fl_cas_hash)
12     f->dump_unsigned("cas_hash", l.fl_cas_hash);
13   if (l.fl_object_stripe_unit)
14     f->dump_unsigned("object_stripe_unit", l.fl_object_stripe_unit);
15   if (l.fl_pg_pool)
16     f->dump_unsigned("pg_pool", l.fl_pg_pool);
17 }
18
19 void dump(const ceph_dir_layout& l, Formatter *f)
20 {
21   f->dump_unsigned("dir_hash", l.dl_dir_hash);
22 }
23
24
25 // file_layout_t
26
27 bool file_layout_t::is_valid() const
28 {
29   /* stripe unit, object size must be non-zero, 64k increment */
30   if (!stripe_unit || (stripe_unit & (CEPH_MIN_STRIPE_UNIT-1)))
31     return false;
32   if (!object_size || (object_size & (CEPH_MIN_STRIPE_UNIT-1)))
33     return false;
34   /* object size must be a multiple of stripe unit */
35   if (object_size < stripe_unit || object_size % stripe_unit)
36     return false;
37   /* stripe count must be non-zero */
38   if (!stripe_count)
39     return false;
40   return true;
41 }
42
43 void file_layout_t::from_legacy(const ceph_file_layout& fl)
44 {
45   stripe_unit = fl.fl_stripe_unit;
46   stripe_count = fl.fl_stripe_count;
47   object_size = fl.fl_object_size;
48   pool_id = (int32_t)fl.fl_pg_pool;
49   // in the legacy encoding, a zeroed structure was the default and
50   // would have pool 0 instead of -1.
51   if (pool_id == 0 && stripe_unit == 0 && stripe_count == 0 && object_size == 0)
52     pool_id = -1;
53   pool_ns.clear();
54 }
55
56 void file_layout_t::to_legacy(ceph_file_layout *fl) const
57 {
58   fl->fl_stripe_unit = stripe_unit;
59   fl->fl_stripe_count = stripe_count;
60   fl->fl_object_size = object_size;
61   fl->fl_cas_hash = 0;
62   fl->fl_object_stripe_unit = 0;
63   fl->fl_unused = 0;
64   // in the legacy encoding, pool 0 was undefined.
65   if (pool_id >= 0)
66     fl->fl_pg_pool = pool_id;
67   else
68     fl->fl_pg_pool = 0;
69 }
70
71 void file_layout_t::encode(bufferlist& bl, uint64_t features) const
72 {
73   if ((features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) == 0) {
74     ceph_file_layout fl;
75     assert((stripe_unit & 0xff) == 0);  // first byte must be 0
76     to_legacy(&fl);
77     ::encode(fl, bl);
78     return;
79   }
80
81   ENCODE_START(2, 2, bl);
82   ::encode(stripe_unit, bl);
83   ::encode(stripe_count, bl);
84   ::encode(object_size, bl);
85   ::encode(pool_id, bl);
86   ::encode(pool_ns, bl);
87   ENCODE_FINISH(bl);
88 }
89
90 void file_layout_t::decode(bufferlist::iterator& p)
91 {
92   if (*p == 0) {
93     ceph_file_layout fl;
94     ::decode(fl, p);
95     from_legacy(fl);
96     return;
97   }
98   DECODE_START(2, p);
99   ::decode(stripe_unit, p);
100   ::decode(stripe_count, p);
101   ::decode(object_size, p);
102   ::decode(pool_id, p);
103   ::decode(pool_ns, p);
104   DECODE_FINISH(p);
105 }
106
107 void file_layout_t::dump(Formatter *f) const
108 {
109   f->dump_unsigned("stripe_unit", stripe_unit);
110   f->dump_unsigned("stripe_count", stripe_count);
111   f->dump_unsigned("object_size", object_size);
112   f->dump_int("pool_id", pool_id);
113   f->dump_string("pool_ns", pool_ns);
114 }
115
116 void file_layout_t::generate_test_instances(list<file_layout_t*>& o)
117 {
118   o.push_back(new file_layout_t);
119   o.push_back(new file_layout_t);
120   o.back()->stripe_unit = 4096;
121   o.back()->stripe_count = 16;
122   o.back()->object_size = 1048576;
123   o.back()->pool_id = 3;
124   o.back()->pool_ns = "myns";
125 }
126
127 ostream& operator<<(ostream& out, const file_layout_t &layout)
128 {
129   JSONFormatter f;
130   layout.dump(&f);
131   f.flush(out);
132   return out;
133 }
134