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