]> git.proxmox.com Git - ceph.git/blame - ceph/src/include/types.h
update sources to v12.1.1
[ceph.git] / ceph / src / include / types.h
CommitLineData
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-2006 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#ifndef CEPH_TYPES_H
15#define CEPH_TYPES_H
16
17// this is needed for ceph_fs to compile in userland
18#include "int_types.h"
19#include "byteorder.h"
20
21#include "uuid.h"
22
23#include <netinet/in.h>
24#include <fcntl.h>
25#include <string.h>
26
27// <macro hackery>
28// temporarily remap __le* to ceph_le* for benefit of shared kernel/userland headers
29#define __le16 ceph_le16
30#define __le32 ceph_le32
31#define __le64 ceph_le64
32#include "ceph_fs.h"
33#include "ceph_frag.h"
34#include "rbd_types.h"
35#undef __le16
36#undef __le32
37#undef __le64
38// </macro hackery>
39
40
41#ifdef __cplusplus
42#ifndef _BACKWARD_BACKWARD_WARNING_H
43#define _BACKWARD_BACKWARD_WARNING_H // make gcc 4.3 shut up about hash_*
44#endif
45#endif
46
47extern "C" {
48#include <stdint.h>
49#include <sys/types.h>
50#include <sys/stat.h>
51#include "statlite.h"
52}
53
54#include <string>
55#include <list>
56#include <set>
57#include <map>
58#include <vector>
59#include <iostream>
60#include <iomanip>
61
62using namespace std;
63
64#include "include/unordered_map.h"
65
66#include "object.h"
67#include "intarith.h"
68
69#include "acconfig.h"
70
71#include "assert.h"
72
73// DARWIN compatibility
74#ifdef DARWIN
75typedef long long loff_t;
76typedef long long off64_t;
77#define O_DIRECT 00040000
78#endif
79
80// FreeBSD compatibility
81#ifdef __FreeBSD__
82typedef off_t loff_t;
83typedef off_t off64_t;
84#endif
85
86#if defined(__sun) || defined(_AIX)
87typedef off_t loff_t;
88#endif
89
90
91// -- io helpers --
92
31f18b77
FG
93// Forward declare all the I/O helpers so strict ADL can find them in
94// the case of containers of containers. I'm tempted to abstract this
95// stuff using template templates like I did for denc.
96
97template<class A, class B>
98inline ostream& operator<<(ostream&out, const pair<A,B>& v);
99template<class A, class Alloc>
100inline ostream& operator<<(ostream& out, const vector<A,Alloc>& v);
101template<class A, class Comp, class Alloc>
102inline ostream& operator<<(ostream& out, const deque<A,Alloc>& v);
103template<class A, class B, class C>
104inline ostream& operator<<(ostream&out, const boost::tuple<A, B, C> &t);
105template<class A, class Alloc>
106inline ostream& operator<<(ostream& out, const list<A,Alloc>& ilist);
107template<class A, class Comp, class Alloc>
108inline ostream& operator<<(ostream& out, const set<A, Comp, Alloc>& iset);
109template<class A, class Comp, class Alloc>
110inline ostream& operator<<(ostream& out, const multiset<A,Comp,Alloc>& iset);
111template<class A, class B, class Comp, class Alloc>
112inline ostream& operator<<(ostream& out, const map<A,B,Comp,Alloc>& m);
113template<class A, class B, class Comp, class Alloc>
114inline ostream& operator<<(ostream& out, const multimap<A,B,Comp,Alloc>& m);
115
7c673cae
FG
116template<class A, class B>
117inline ostream& operator<<(ostream& out, const pair<A,B>& v) {
118 return out << v.first << "," << v.second;
119}
120
121template<class A, class Alloc>
122inline ostream& operator<<(ostream& out, const vector<A,Alloc>& v) {
123 out << "[";
124 for (auto p = v.begin(); p != v.end(); ++p) {
125 if (p != v.begin()) out << ",";
126 out << *p;
127 }
128 out << "]";
129 return out;
130}
131template<class A, class Alloc>
132inline ostream& operator<<(ostream& out, const deque<A,Alloc>& v) {
133 out << "<";
134 for (auto p = v.begin(); p != v.end(); ++p) {
135 if (p != v.begin()) out << ",";
136 out << *p;
137 }
138 out << ">";
139 return out;
140}
141
142template<class A, class B, class C>
143inline ostream& operator<<(ostream&out, const boost::tuple<A, B, C> &t) {
144 out << boost::get<0>(t) <<"," << boost::get<1>(t) << "," << boost::get<2>(t);
145 return out;
146}
147
148template<class A, class Alloc>
149inline ostream& operator<<(ostream& out, const list<A,Alloc>& ilist) {
150 for (auto it = ilist.begin();
151 it != ilist.end();
152 ++it) {
153 if (it != ilist.begin()) out << ",";
154 out << *it;
155 }
156 return out;
157}
158
159template<class A, class Comp, class Alloc>
160inline ostream& operator<<(ostream& out, const set<A, Comp, Alloc>& iset) {
161 for (auto it = iset.begin();
162 it != iset.end();
163 ++it) {
164 if (it != iset.begin()) out << ",";
165 out << *it;
166 }
167 return out;
168}
169
170template<class A, class Comp, class Alloc>
171inline ostream& operator<<(ostream& out, const multiset<A,Comp,Alloc>& iset) {
172 for (auto it = iset.begin();
173 it != iset.end();
174 ++it) {
175 if (it != iset.begin()) out << ",";
176 out << *it;
177 }
178 return out;
179}
180
181template<class A, class B, class Comp, class Alloc>
182inline ostream& operator<<(ostream& out, const map<A,B,Comp,Alloc>& m)
183{
184 out << "{";
185 for (auto it = m.begin();
186 it != m.end();
187 ++it) {
188 if (it != m.begin()) out << ",";
189 out << it->first << "=" << it->second;
190 }
191 out << "}";
192 return out;
193}
194
195template<class A, class B, class Comp, class Alloc>
196inline ostream& operator<<(ostream& out, const multimap<A,B,Comp,Alloc>& m)
197{
198 out << "{{";
199 for (auto it = m.begin();
200 it != m.end();
201 ++it) {
202 if (it != m.begin()) out << ",";
203 out << it->first << "=" << it->second;
204 }
205 out << "}}";
206 return out;
207}
208
209
210
211
212/*
213 * comparators for stl containers
214 */
215// for ceph::unordered_map:
216// ceph::unordered_map<const char*, long, hash<const char*>, eqstr> vals;
217struct eqstr
218{
219 bool operator()(const char* s1, const char* s2) const
220 {
221 return strcmp(s1, s2) == 0;
222 }
223};
224
225// for set, map
226struct ltstr
227{
228 bool operator()(const char* s1, const char* s2) const
229 {
230 return strcmp(s1, s2) < 0;
231 }
232};
233
234
235namespace ceph {
236 class Formatter;
237}
238
239#include "encoding.h"
240
241WRITE_RAW_ENCODER(ceph_fsid)
242WRITE_RAW_ENCODER(ceph_file_layout)
243WRITE_RAW_ENCODER(ceph_dir_layout)
244WRITE_RAW_ENCODER(ceph_mds_session_head)
245WRITE_RAW_ENCODER(ceph_mds_request_head_legacy)
246WRITE_RAW_ENCODER(ceph_mds_request_head)
247WRITE_RAW_ENCODER(ceph_mds_request_release)
248WRITE_RAW_ENCODER(ceph_filelock)
249WRITE_RAW_ENCODER(ceph_mds_caps_head)
250WRITE_RAW_ENCODER(ceph_mds_caps_body_legacy)
251WRITE_RAW_ENCODER(ceph_mds_cap_peer)
252WRITE_RAW_ENCODER(ceph_mds_cap_release)
253WRITE_RAW_ENCODER(ceph_mds_cap_item)
254WRITE_RAW_ENCODER(ceph_mds_lease)
255WRITE_RAW_ENCODER(ceph_mds_snap_head)
256WRITE_RAW_ENCODER(ceph_mds_snap_realm)
257WRITE_RAW_ENCODER(ceph_mds_reply_head)
258WRITE_RAW_ENCODER(ceph_mds_reply_cap)
259WRITE_RAW_ENCODER(ceph_mds_cap_reconnect)
260WRITE_RAW_ENCODER(ceph_mds_snaprealm_reconnect)
261WRITE_RAW_ENCODER(ceph_frag_tree_split)
262WRITE_RAW_ENCODER(ceph_osd_reply_head)
263WRITE_RAW_ENCODER(ceph_osd_op)
264WRITE_RAW_ENCODER(ceph_msg_header)
265WRITE_RAW_ENCODER(ceph_msg_footer)
266WRITE_RAW_ENCODER(ceph_msg_footer_old)
267WRITE_RAW_ENCODER(ceph_mon_subscribe_item)
268
269WRITE_RAW_ENCODER(ceph_mon_statfs)
270WRITE_RAW_ENCODER(ceph_mon_statfs_reply)
271
272// ----------------------
273// some basic types
274
275// NOTE: these must match ceph_fs.h typedefs
276typedef uint64_t ceph_tid_t; // transaction id
277typedef uint64_t version_t;
278typedef __u32 epoch_t; // map epoch (32bits -> 13 epochs/second for 10 years)
279
280// --------------------------------------
281// identify individual mount clients by 64bit value
282
283struct client_t {
284 int64_t v;
285
286 // cppcheck-suppress noExplicitConstructor
287 client_t(int64_t _v = -2) : v(_v) {}
288
289 void encode(bufferlist& bl) const {
290 ::encode(v, bl);
291 }
292 void decode(bufferlist::iterator& bl) {
293 ::decode(v, bl);
294 }
295};
296WRITE_CLASS_ENCODER(client_t)
297
298static inline bool operator==(const client_t& l, const client_t& r) { return l.v == r.v; }
299static inline bool operator!=(const client_t& l, const client_t& r) { return l.v != r.v; }
300static inline bool operator<(const client_t& l, const client_t& r) { return l.v < r.v; }
301static inline bool operator<=(const client_t& l, const client_t& r) { return l.v <= r.v; }
302static inline bool operator>(const client_t& l, const client_t& r) { return l.v > r.v; }
303static inline bool operator>=(const client_t& l, const client_t& r) { return l.v >= r.v; }
304
305static inline bool operator>=(const client_t& l, int64_t o) { return l.v >= o; }
306static inline bool operator<(const client_t& l, int64_t o) { return l.v < o; }
307
308inline ostream& operator<<(ostream& out, const client_t& c) {
309 return out << c.v;
310}
311
312
313
314// --
315
316struct prettybyte_t {
317 uint64_t v;
318 // cppcheck-suppress noExplicitConstructor
319 prettybyte_t(uint64_t _v) : v(_v) {}
320};
321
322inline ostream& operator<<(ostream& out, const prettybyte_t& b)
323{
324 uint64_t bump_after = 100;
325 if (b.v > bump_after << 60)
326 return out << (b.v >> 60) << " EB";
327 if (b.v > bump_after << 50)
328 return out << (b.v >> 50) << " PB";
329 if (b.v > bump_after << 40)
330 return out << (b.v >> 40) << " TB";
331 if (b.v > bump_after << 30)
332 return out << (b.v >> 30) << " GB";
333 if (b.v > bump_after << 20)
334 return out << (b.v >> 20) << " MB";
335 if (b.v > bump_after << 10)
336 return out << (b.v >> 10) << " kB";
337 return out << b.v << " bytes";
338}
339
340struct si_t {
341 uint64_t v;
342 // cppcheck-suppress noExplicitConstructor
343 si_t(uint64_t _v) : v(_v) {}
344};
345
346inline ostream& operator<<(ostream& out, const si_t& b)
347{
348 uint64_t bump_after = 100;
349 if (b.v > bump_after << 60)
350 return out << (b.v >> 60) << "E";
351 if (b.v > bump_after << 50)
352 return out << (b.v >> 50) << "P";
353 if (b.v > bump_after << 40)
354 return out << (b.v >> 40) << "T";
355 if (b.v > bump_after << 30)
356 return out << (b.v >> 30) << "G";
357 if (b.v > bump_after << 20)
358 return out << (b.v >> 20) << "M";
359 if (b.v > bump_after << 10)
360 return out << (b.v >> 10) << "k";
361 return out << b.v;
362}
363
364struct pretty_si_t {
365 uint64_t v;
366 // cppcheck-suppress noExplicitConstructor
367 pretty_si_t(uint64_t _v) : v(_v) {}
368};
369
370inline ostream& operator<<(ostream& out, const pretty_si_t& b)
371{
372 uint64_t bump_after = 100;
373 if (b.v > bump_after << 60)
374 return out << (b.v >> 60) << " E";
375 if (b.v > bump_after << 50)
376 return out << (b.v >> 50) << " P";
377 if (b.v > bump_after << 40)
378 return out << (b.v >> 40) << " T";
379 if (b.v > bump_after << 30)
380 return out << (b.v >> 30) << " G";
381 if (b.v > bump_after << 20)
382 return out << (b.v >> 20) << " M";
383 if (b.v > bump_after << 10)
384 return out << (b.v >> 10) << " k";
385 return out << b.v << " ";
386}
387
388struct kb_t {
389 uint64_t v;
390 // cppcheck-suppress noExplicitConstructor
391 kb_t(uint64_t _v) : v(_v) {}
392};
393
394inline ostream& operator<<(ostream& out, const kb_t& kb)
395{
396 uint64_t bump_after = 100;
397 if (kb.v > bump_after << 40)
398 return out << (kb.v >> 40) << " PB";
399 if (kb.v > bump_after << 30)
400 return out << (kb.v >> 30) << " TB";
401 if (kb.v > bump_after << 20)
402 return out << (kb.v >> 20) << " GB";
403 if (kb.v > bump_after << 10)
404 return out << (kb.v >> 10) << " MB";
405 return out << kb.v << " kB";
406}
407
408inline ostream& operator<<(ostream& out, const ceph_mon_subscribe_item& i)
409{
410 return out << i.start
411 << ((i.flags & CEPH_SUBSCRIBE_ONETIME) ? "" : "+");
412}
413
7c673cae
FG
414struct weightf_t {
415 float v;
416 // cppcheck-suppress noExplicitConstructor
417 weightf_t(float _v) : v(_v) {}
418};
419
420inline ostream& operator<<(ostream& out, const weightf_t& w)
421{
422 if (w.v < -0.01) {
423 return out << "-";
424 } else if (w.v < 0.000001) {
425 return out << "0";
426 } else {
427 std::streamsize p = out.precision();
428 return out << std::fixed << std::setprecision(5) << w.v << std::setprecision(p);
429 }
430}
431
432struct shard_id_t {
433 int8_t id;
434
435 shard_id_t() : id(0) {}
436 explicit shard_id_t(int8_t _id) : id(_id) {}
437
438 operator int8_t() const { return id; }
439
440 const static shard_id_t NO_SHARD;
441
442 void encode(bufferlist &bl) const {
443 ::encode(id, bl);
444 }
445 void decode(bufferlist::iterator &bl) {
446 ::decode(id, bl);
447 }
448};
449WRITE_CLASS_ENCODER(shard_id_t)
450WRITE_EQ_OPERATORS_1(shard_id_t, id)
451WRITE_CMP_OPERATORS_1(shard_id_t, id)
452ostream &operator<<(ostream &lhs, const shard_id_t &rhs);
453
31f18b77
FG
454#if defined(__sun) || defined(_AIX) || defined(DARWIN) || defined(__FreeBSD__)
455__s32 ceph_to_hostos_errno(__s32 e);
456__s32 hostos_to_ceph_errno(__s32 e);
7c673cae 457#else
31f18b77
FG
458#define ceph_to_hostos_errno(e) (e)
459#define hostos_to_ceph_errno(e) (e)
7c673cae
FG
460#endif
461
462struct errorcode32_t {
463 int32_t code;
464
465 errorcode32_t() : code(0) {}
466 // cppcheck-suppress noExplicitConstructor
467 errorcode32_t(int32_t i) : code(i) {}
468
224ce89b
WB
469 operator int() const { return code; }
470 int* operator&() { return &code; }
471 int operator==(int i) { return code == i; }
472 int operator>(int i) { return code > i; }
473 int operator>=(int i) { return code >= i; }
474 int operator<(int i) { return code < i; }
475 int operator<=(int i) { return code <= i; }
7c673cae
FG
476
477 void encode(bufferlist &bl) const {
31f18b77
FG
478 __s32 newcode = hostos_to_ceph_errno(code);
479 ::encode(newcode, bl);
7c673cae
FG
480 }
481 void decode(bufferlist::iterator &bl) {
482 ::decode(code, bl);
31f18b77 483 code = ceph_to_hostos_errno(code);
7c673cae
FG
484 }
485};
486WRITE_CLASS_ENCODER(errorcode32_t)
487WRITE_EQ_OPERATORS_1(errorcode32_t, code)
488WRITE_CMP_OPERATORS_1(errorcode32_t, code)
489
490
491#endif