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