]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/Utils.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / librbd / Utils.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <boost/algorithm/string.hpp>
5 #include <boost/lexical_cast.hpp>
6
7 #include "librbd/Utils.h"
8 #include "include/rbd_types.h"
9 #include "include/stringify.h"
10 #include "include/rbd/features.h"
11 #include "common/dout.h"
12
13 #define dout_subsys ceph_subsys_rbd
14 #undef dout_prefix
15 #define dout_prefix *_dout << "librbd: "
16
17 namespace librbd {
18 namespace util {
19
20 const std::string group_header_name(const std::string &group_id)
21 {
22 return RBD_GROUP_HEADER_PREFIX + group_id;
23 }
24
25 const std::string id_obj_name(const std::string &name)
26 {
27 return RBD_ID_PREFIX + name;
28 }
29
30 const std::string header_name(const std::string &image_id)
31 {
32 return RBD_HEADER_PREFIX + image_id;
33 }
34
35 const std::string old_header_name(const std::string &image_name)
36 {
37 return image_name + RBD_SUFFIX;
38 }
39
40 std::string unique_lock_name(const std::string &name, void *address) {
41 return name + " (" + stringify(address) + ")";
42 }
43
44 librados::AioCompletion *create_rados_callback(Context *on_finish) {
45 return create_rados_callback<Context, &Context::complete>(on_finish);
46 }
47
48 std::string generate_image_id(librados::IoCtx &ioctx) {
49 librados::Rados rados(ioctx);
50
51 uint64_t bid = rados.get_instance_id();
52 uint32_t extra = rand() % 0xFFFFFFFF;
53
54 ostringstream bid_ss;
55 bid_ss << std::hex << bid << std::hex << extra;
56 std::string id = bid_ss.str();
57
58 // ensure the image id won't overflow the fixed block name size
59 if (id.length() > RBD_MAX_IMAGE_ID_LENGTH) {
60 id = id.substr(id.length() - RBD_MAX_IMAGE_ID_LENGTH);
61 }
62
63 return id;
64 }
65
66 uint64_t get_rbd_default_features(CephContext* cct)
67 {
68 auto str_val = cct->_conf->get_val<std::string>("rbd_default_features");
69 return boost::lexical_cast<uint64_t>(str_val);
70 }
71
72 bool calc_sparse_extent(const bufferptr &bp,
73 size_t sparse_size,
74 uint64_t length,
75 size_t *write_offset,
76 size_t *write_length,
77 size_t *offset) {
78 size_t extent_size;
79 if (*offset + sparse_size > length) {
80 extent_size = length - *offset;
81 } else {
82 extent_size = sparse_size;
83 }
84
85 bufferptr extent(bp, *offset, extent_size);
86 *offset += extent_size;
87
88 bool extent_is_zero = extent.is_zero();
89 if (!extent_is_zero) {
90 *write_length += extent_size;
91 }
92 if (extent_is_zero && *write_length == 0) {
93 *write_offset += extent_size;
94 }
95
96 if ((extent_is_zero || *offset == length) && *write_length != 0) {
97 return true;
98 }
99 return false;
100 }
101 } // namespace util
102
103 } // namespace librbd