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