]>
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"
13 #define dout_subsys ceph_subsys_rbd
15 #define dout_prefix *_dout << "librbd: "
20 const std::string
group_header_name(const std::string
&group_id
)
22 return RBD_GROUP_HEADER_PREFIX
+ group_id
;
25 const std::string
id_obj_name(const std::string
&name
)
27 return RBD_ID_PREFIX
+ name
;
30 const std::string
header_name(const std::string
&image_id
)
32 return RBD_HEADER_PREFIX
+ image_id
;
35 const std::string
old_header_name(const std::string
&image_name
)
37 return image_name
+ RBD_SUFFIX
;
40 std::string
unique_lock_name(const std::string
&name
, void *address
) {
41 return name
+ " (" + stringify(address
) + ")";
44 librados::AioCompletion
*create_rados_callback(Context
*on_finish
) {
45 return create_rados_callback
<Context
, &Context::complete
>(on_finish
);
48 std::string
generate_image_id(librados::IoCtx
&ioctx
) {
49 librados::Rados
rados(ioctx
);
51 uint64_t bid
= rados
.get_instance_id();
52 uint32_t extra
= rand() % 0xFFFFFFFF;
55 bid_ss
<< std::hex
<< bid
<< std::hex
<< extra
;
56 std::string id
= bid_ss
.str();
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
);
66 uint64_t get_rbd_default_features(CephContext
* cct
)
68 auto str_val
= cct
->_conf
->get_val
<std::string
>("rbd_default_features");
69 return boost::lexical_cast
<uint64_t>(str_val
);
72 bool calc_sparse_extent(const bufferptr
&bp
,
79 if (*offset
+ sparse_size
> length
) {
80 extent_size
= length
- *offset
;
82 extent_size
= sparse_size
;
85 bufferptr
extent(bp
, *offset
, extent_size
);
86 *offset
+= extent_size
;
88 bool extent_is_zero
= extent
.is_zero();
89 if (!extent_is_zero
) {
90 *write_length
+= extent_size
;
92 if (extent_is_zero
&& *write_length
== 0) {
93 *write_offset
+= extent_size
;
96 if ((extent_is_zero
|| *offset
== length
) && *write_length
!= 0) {
103 } // namespace librbd