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