]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2004- Sage Weil <sage@newdream.net> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
11fdf7f2 | 15 | #include <string_view> |
94b18763 | 16 | |
7c673cae FG |
17 | #include "snap.h" |
18 | ||
19 | #include "common/Formatter.h" | |
20 | ||
20effc67 | 21 | using namespace std; |
7c673cae FG |
22 | /* |
23 | * SnapInfo | |
24 | */ | |
25 | ||
26 | void SnapInfo::encode(bufferlist& bl) const | |
27 | { | |
f67539c2 | 28 | ENCODE_START(3, 2, bl); |
11fdf7f2 TL |
29 | encode(snapid, bl); |
30 | encode(ino, bl); | |
31 | encode(stamp, bl); | |
32 | encode(name, bl); | |
f67539c2 | 33 | encode(metadata, bl); |
7c673cae FG |
34 | ENCODE_FINISH(bl); |
35 | } | |
36 | ||
11fdf7f2 | 37 | void SnapInfo::decode(bufferlist::const_iterator& bl) |
7c673cae | 38 | { |
f67539c2 | 39 | DECODE_START_LEGACY_COMPAT_LEN(3, 2, 2, bl); |
11fdf7f2 TL |
40 | decode(snapid, bl); |
41 | decode(ino, bl); | |
42 | decode(stamp, bl); | |
43 | decode(name, bl); | |
f67539c2 TL |
44 | if (struct_v >= 3) { |
45 | decode(metadata, bl); | |
46 | } | |
7c673cae FG |
47 | DECODE_FINISH(bl); |
48 | } | |
49 | ||
50 | void SnapInfo::dump(Formatter *f) const | |
51 | { | |
52 | f->dump_unsigned("snapid", snapid); | |
53 | f->dump_unsigned("ino", ino); | |
54 | f->dump_stream("stamp") << stamp; | |
55 | f->dump_string("name", name); | |
f67539c2 TL |
56 | f->open_object_section("metadata"); |
57 | for (auto &[key, value] : metadata) { | |
58 | f->dump_string(key, value); | |
59 | } | |
60 | f->close_section(); | |
7c673cae FG |
61 | } |
62 | ||
9f95a23c | 63 | void SnapInfo::generate_test_instances(std::list<SnapInfo*>& ls) |
7c673cae FG |
64 | { |
65 | ls.push_back(new SnapInfo); | |
66 | ls.push_back(new SnapInfo); | |
67 | ls.back()->snapid = 1; | |
68 | ls.back()->ino = 2; | |
69 | ls.back()->stamp = utime_t(3, 4); | |
70 | ls.back()->name = "foo"; | |
f67539c2 | 71 | ls.back()->metadata = {{"foo", "bar"}}; |
7c673cae FG |
72 | } |
73 | ||
74 | ostream& operator<<(ostream& out, const SnapInfo &sn) | |
75 | { | |
76 | return out << "snap(" << sn.snapid | |
77 | << " " << sn.ino | |
78 | << " '" << sn.name | |
79 | << "' " << sn.stamp << ")"; | |
80 | } | |
81 | ||
11fdf7f2 | 82 | std::string_view SnapInfo::get_long_name() const |
7c673cae | 83 | { |
11fdf7f2 TL |
84 | if (long_name.empty() || |
85 | long_name.compare(1, name.size(), name) || | |
86 | long_name.find_last_of("_") != name.size() + 1) { | |
7c673cae FG |
87 | char nm[80]; |
88 | snprintf(nm, sizeof(nm), "_%s_%llu", name.c_str(), (unsigned long long)ino); | |
89 | long_name = nm; | |
90 | } | |
91 | return long_name; | |
92 | } | |
93 | ||
94 | /* | |
95 | * snaplink_t | |
96 | */ | |
97 | ||
98 | void snaplink_t::encode(bufferlist& bl) const | |
99 | { | |
100 | ENCODE_START(2, 2, bl); | |
11fdf7f2 TL |
101 | encode(ino, bl); |
102 | encode(first, bl); | |
7c673cae FG |
103 | ENCODE_FINISH(bl); |
104 | } | |
105 | ||
11fdf7f2 | 106 | void snaplink_t::decode(bufferlist::const_iterator& bl) |
7c673cae FG |
107 | { |
108 | DECODE_START_LEGACY_COMPAT_LEN(2, 2, 2, bl); | |
11fdf7f2 TL |
109 | decode(ino, bl); |
110 | decode(first, bl); | |
7c673cae FG |
111 | DECODE_FINISH(bl); |
112 | } | |
113 | ||
114 | void snaplink_t::dump(Formatter *f) const | |
115 | { | |
116 | f->dump_unsigned("ino", ino); | |
117 | f->dump_unsigned("first", first); | |
118 | } | |
119 | ||
9f95a23c | 120 | void snaplink_t::generate_test_instances(std::list<snaplink_t*>& ls) |
7c673cae FG |
121 | { |
122 | ls.push_back(new snaplink_t); | |
123 | ls.push_back(new snaplink_t); | |
124 | ls.back()->ino = 2; | |
125 | ls.back()->first = 123; | |
126 | } | |
127 | ||
128 | ostream& operator<<(ostream& out, const snaplink_t &l) | |
129 | { | |
130 | return out << l.ino << "@" << l.first; | |
131 | } | |
132 | ||
133 | /* | |
134 | * sr_t | |
135 | */ | |
136 | ||
137 | void sr_t::encode(bufferlist& bl) const | |
138 | { | |
11fdf7f2 TL |
139 | ENCODE_START(6, 4, bl); |
140 | encode(seq, bl); | |
141 | encode(created, bl); | |
142 | encode(last_created, bl); | |
143 | encode(last_destroyed, bl); | |
144 | encode(current_parent_since, bl); | |
145 | encode(snaps, bl); | |
146 | encode(past_parents, bl); | |
147 | encode(past_parent_snaps, bl); | |
148 | encode(flags, bl); | |
7c673cae FG |
149 | ENCODE_FINISH(bl); |
150 | } | |
151 | ||
11fdf7f2 | 152 | void sr_t::decode(bufferlist::const_iterator& p) |
7c673cae | 153 | { |
11fdf7f2 | 154 | DECODE_START_LEGACY_COMPAT_LEN(6, 4, 4, p); |
7c673cae FG |
155 | if (struct_v == 2) { |
156 | __u8 struct_v; | |
11fdf7f2 | 157 | decode(struct_v, p); // yes, really: extra byte for v2 encoding only, see 6ee52e7d. |
7c673cae | 158 | } |
11fdf7f2 TL |
159 | decode(seq, p); |
160 | decode(created, p); | |
161 | decode(last_created, p); | |
162 | decode(last_destroyed, p); | |
163 | decode(current_parent_since, p); | |
164 | decode(snaps, p); | |
165 | decode(past_parents, p); | |
166 | if (struct_v >= 5) | |
167 | decode(past_parent_snaps, p); | |
168 | if (struct_v >= 6) | |
169 | decode(flags, p); | |
170 | else | |
171 | flags = 0; | |
7c673cae FG |
172 | DECODE_FINISH(p); |
173 | } | |
174 | ||
175 | void sr_t::dump(Formatter *f) const | |
176 | { | |
177 | f->dump_unsigned("seq", seq); | |
178 | f->dump_unsigned("created", created); | |
179 | f->dump_unsigned("last_created", last_created); | |
180 | f->dump_unsigned("last_destroyed", last_destroyed); | |
181 | f->dump_unsigned("current_parent_since", current_parent_since); | |
182 | ||
183 | f->open_array_section("snaps"); | |
184 | for (map<snapid_t,SnapInfo>::const_iterator p = snaps.begin(); p != snaps.end(); ++p) { | |
185 | f->open_object_section("snapinfo"); | |
186 | f->dump_unsigned("last", p->first); | |
187 | p->second.dump(f); | |
188 | f->close_section(); | |
189 | } | |
190 | f->close_section(); | |
191 | ||
192 | f->open_array_section("past_parents"); | |
193 | for (map<snapid_t,snaplink_t>::const_iterator p = past_parents.begin(); p != past_parents.end(); ++p) { | |
194 | f->open_object_section("past_parent"); | |
195 | f->dump_unsigned("last", p->first); | |
196 | p->second.dump(f); | |
197 | f->close_section(); | |
198 | } | |
199 | f->close_section(); | |
11fdf7f2 TL |
200 | |
201 | f->open_array_section("past_parent_snaps"); | |
202 | for (auto p = past_parent_snaps.begin(); p != past_parent_snaps.end(); ++p) { | |
203 | f->open_object_section("snapinfo"); | |
204 | f->dump_unsigned("snapid", *p); | |
205 | f->close_section(); | |
206 | } | |
207 | f->close_section(); | |
7c673cae FG |
208 | } |
209 | ||
9f95a23c | 210 | void sr_t::generate_test_instances(std::list<sr_t*>& ls) |
7c673cae FG |
211 | { |
212 | ls.push_back(new sr_t); | |
213 | ls.push_back(new sr_t); | |
214 | ls.back()->seq = 1; | |
215 | ls.back()->created = 2; | |
216 | ls.back()->last_created = 3; | |
217 | ls.back()->last_destroyed = 4; | |
218 | ls.back()->current_parent_since = 5; | |
219 | ls.back()->snaps[123].snapid = 7; | |
220 | ls.back()->snaps[123].ino = 8; | |
221 | ls.back()->snaps[123].stamp = utime_t(9, 10); | |
222 | ls.back()->snaps[123].name = "name1"; | |
223 | ls.back()->past_parents[12].ino = 12; | |
224 | ls.back()->past_parents[12].first = 3; | |
11fdf7f2 TL |
225 | |
226 | ls.back()->past_parent_snaps.insert(5); | |
227 | ls.back()->past_parent_snaps.insert(6); | |
7c673cae FG |
228 | } |
229 |