]>
git.proxmox.com Git - ceph.git/blob - 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
4 #include <boost/algorithm/string.hpp>
5 #include <boost/lexical_cast.hpp>
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 #include "librbd/ImageCtx.h"
14 #define dout_subsys ceph_subsys_rbd
16 #define dout_prefix *_dout << "librbd: "
21 const std::string
group_header_name(const std::string
&group_id
)
23 return RBD_GROUP_HEADER_PREFIX
+ group_id
;
26 const std::string
id_obj_name(const std::string
&name
)
28 return RBD_ID_PREFIX
+ name
;
31 const std::string
header_name(const std::string
&image_id
)
33 return RBD_HEADER_PREFIX
+ image_id
;
36 const std::string
old_header_name(const std::string
&image_name
)
38 return image_name
+ RBD_SUFFIX
;
41 std::string
unique_lock_name(const std::string
&name
, void *address
) {
42 return name
+ " (" + stringify(address
) + ")";
45 librados::AioCompletion
*create_rados_callback(Context
*on_finish
) {
46 return create_rados_callback
<Context
, &Context::complete
>(on_finish
);
49 std::string
generate_image_id(librados::IoCtx
&ioctx
) {
50 librados::Rados
rados(ioctx
);
52 uint64_t bid
= rados
.get_instance_id();
53 uint32_t extra
= rand() % 0xFFFFFFFF;
56 bid_ss
<< std::hex
<< bid
<< std::hex
<< extra
;
57 std::string id
= bid_ss
.str();
59 // ensure the image id won't overflow the fixed block name size
60 if (id
.length() > RBD_MAX_IMAGE_ID_LENGTH
) {
61 id
= id
.substr(id
.length() - RBD_MAX_IMAGE_ID_LENGTH
);
67 uint64_t get_rbd_default_features(CephContext
* cct
)
69 auto str_val
= cct
->_conf
->get_val
<std::string
>("rbd_default_features");
70 return boost::lexical_cast
<uint64_t>(str_val
);
73 bool calc_sparse_extent(const bufferptr
&bp
,
80 if (*offset
+ sparse_size
> length
) {
81 extent_size
= length
- *offset
;
83 extent_size
= sparse_size
;
86 bufferptr
extent(bp
, *offset
, extent_size
);
87 *offset
+= extent_size
;
89 bool extent_is_zero
= extent
.is_zero();
90 if (!extent_is_zero
) {
91 *write_length
+= extent_size
;
93 if (extent_is_zero
&& *write_length
== 0) {
94 *write_offset
+= extent_size
;
97 if ((extent_is_zero
|| *offset
== length
) && *write_length
!= 0) {
103 bool is_metadata_config_override(const std::string
& metadata_key
,
104 std::string
* config_key
) {
105 size_t prefix_len
= librbd::ImageCtx::METADATA_CONF_PREFIX
.size();
106 if (metadata_key
.size() > prefix_len
&&
107 metadata_key
.compare(0, prefix_len
,
108 librbd::ImageCtx::METADATA_CONF_PREFIX
) == 0) {
109 *config_key
= metadata_key
.substr(prefix_len
,
110 metadata_key
.size() - prefix_len
);
117 } // namespace librbd