1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include <boost/assign/list_of.hpp>
7 #include "common/ceph_context.h"
8 #include "common/dout.h"
9 #include "common/errno.h"
10 #include "common/perf_counters.h"
11 #include "common/WorkQueue.h"
12 #include "common/Timer.h"
14 #include "librbd/AsyncRequest.h"
15 #include "librbd/ExclusiveLock.h"
16 #include "librbd/internal.h"
17 #include "librbd/ImageCtx.h"
18 #include "librbd/ImageState.h"
19 #include "librbd/ImageWatcher.h"
20 #include "librbd/Journal.h"
21 #include "librbd/LibrbdAdminSocketHook.h"
22 #include "librbd/ObjectMap.h"
23 #include "librbd/Operations.h"
24 #include "librbd/operation/ResizeRequest.h"
25 #include "librbd/Types.h"
26 #include "librbd/Utils.h"
27 #include "librbd/exclusive_lock/AutomaticPolicy.h"
28 #include "librbd/exclusive_lock/StandardPolicy.h"
29 #include "librbd/io/AioCompletion.h"
30 #include "librbd/io/AsyncOperation.h"
31 #include "librbd/io/ImageRequestWQ.h"
32 #include "librbd/io/ObjectDispatcher.h"
33 #include "librbd/journal/StandardPolicy.h"
35 #include "osdc/Striper.h"
36 #include <boost/bind.hpp>
37 #include <boost/algorithm/string/predicate.hpp>
39 #define dout_subsys ceph_subsys_rbd
41 #define dout_prefix *_dout << "librbd::ImageCtx: "
49 using ceph::bufferlist
;
50 using librados::snap_t
;
51 using librados::IoCtx
;
57 class ThreadPoolSingleton
: public ThreadPool
{
59 ContextWQ
*op_work_queue
;
61 explicit ThreadPoolSingleton(CephContext
*cct
)
62 : ThreadPool(cct
, "librbd::thread_pool", "tp_librbd", 1,
64 op_work_queue(new ContextWQ("librbd::op_work_queue",
65 cct
->_conf
.get_val
<uint64_t>("rbd_op_thread_timeout"),
69 ~ThreadPoolSingleton() override
{
70 op_work_queue
->drain();
77 class SafeTimerSingleton
: public SafeTimer
{
79 ceph::mutex lock
= ceph::make_mutex("librbd::Journal::SafeTimerSingleton::lock");
81 explicit SafeTimerSingleton(CephContext
*cct
)
82 : SafeTimer(cct
, lock
, true) {
85 ~SafeTimerSingleton() {
86 std::lock_guard locker
{lock
};
91 } // anonymous namespace
93 const string
ImageCtx::METADATA_CONF_PREFIX
= "conf_";
95 ImageCtx::ImageCtx(const string
&image_name
, const string
&image_id
,
96 const char *snap
, IoCtx
& p
, bool ro
)
97 : cct((CephContext
*)p
.cct()),
100 snap_id(CEPH_NOSNAP
),
103 read_only_flags(ro
? IMAGE_READ_ONLY_FLAG_USER
: 0U),
104 exclusive_locked(false),
108 owner_lock(ceph::make_shared_mutex(util::unique_lock_name("librbd::ImageCtx::owner_lock", this))),
109 image_lock(ceph::make_shared_mutex(util::unique_lock_name("librbd::ImageCtx::image_lock", this))),
110 timestamp_lock(ceph::make_shared_mutex(util::unique_lock_name("librbd::ImageCtx::timestamp_lock", this))),
111 async_ops_lock(ceph::make_mutex(util::unique_lock_name("librbd::ImageCtx::async_ops_lock", this))),
112 copyup_list_lock(ceph::make_mutex(util::unique_lock_name("librbd::ImageCtx::copyup_list_lock", this))),
115 order(0), size(0), features(0),
117 id(image_id
), parent(NULL
),
118 stripe_unit(0), stripe_count(0), flags(0),
121 state(new ImageState
<>(this)),
122 operations(new Operations
<>(*this)),
123 exclusive_lock(nullptr), object_map(nullptr),
124 io_work_queue(nullptr), op_work_queue(nullptr),
125 external_callback_completions(32),
126 event_socket_completions(32),
128 trace_endpoint("librbd")
135 // FIPS zeroization audit 20191117: this memset is not security related.
136 memset(&header
, 0, sizeof(header
));
138 ThreadPool
*thread_pool
;
139 get_thread_pool_instance(cct
, &thread_pool
, &op_work_queue
);
140 io_work_queue
= new io::ImageRequestWQ
<>(
141 this, "librbd::io_work_queue",
142 cct
->_conf
.get_val
<uint64_t>("rbd_op_thread_timeout"),
144 io_object_dispatcher
= new io::ObjectDispatcher
<>(this);
146 if (cct
->_conf
.get_val
<bool>("rbd_auto_exclusive_lock_until_manual_request")) {
147 exclusive_lock_policy
= new exclusive_lock::AutomaticPolicy(this);
149 exclusive_lock_policy
= new exclusive_lock::StandardPolicy(this);
151 journal_policy
= new journal::StandardPolicy
<ImageCtx
>(this);
154 ImageCtx::ImageCtx(const string
&image_name
, const string
&image_id
,
155 uint64_t snap_id
, IoCtx
& p
, bool ro
)
156 : ImageCtx(image_name
, image_id
, "", p
, ro
) {
157 open_snap_id
= snap_id
;
160 ImageCtx::~ImageCtx() {
161 ceph_assert(image_watcher
== NULL
);
162 ceph_assert(exclusive_lock
== NULL
);
163 ceph_assert(object_map
== NULL
);
164 ceph_assert(journal
== NULL
);
165 ceph_assert(asok_hook
== NULL
);
170 delete[] format_string
;
173 if (data_ctx
.is_valid()) {
174 data_ctx
.aio_flush();
176 io_work_queue
->drain();
178 delete io_object_dispatcher
;
180 delete journal_policy
;
181 delete exclusive_lock_policy
;
182 delete io_work_queue
;
187 void ImageCtx::init() {
188 ceph_assert(!header_oid
.empty());
189 ceph_assert(old_format
|| !id
.empty());
191 asok_hook
= new LibrbdAdminSocketHook(this);
193 string pname
= string("librbd-") + id
+ string("-") +
194 md_ctx
.get_pool_name() + string("-") + name
;
195 if (!snap_name
.empty()) {
200 trace_endpoint
.copy_name(pname
);
203 ceph_assert(image_watcher
== NULL
);
204 image_watcher
= new ImageWatcher
<>(*this);
207 void ImageCtx::shutdown() {
208 delete image_watcher
;
209 image_watcher
= nullptr;
215 void ImageCtx::init_layout(int64_t pool_id
)
217 if (stripe_unit
== 0 || stripe_count
== 0) {
218 stripe_unit
= 1ull << order
;
222 vector
<uint64_t> alignments
;
223 alignments
.push_back(stripe_count
<< order
); // object set (in file striping terminology)
224 alignments
.push_back(stripe_unit
* stripe_count
); // stripe
225 alignments
.push_back(stripe_unit
); // stripe unit
226 readahead
.set_alignments(alignments
);
228 layout
= file_layout_t();
229 layout
.stripe_unit
= stripe_unit
;
230 layout
.stripe_count
= stripe_count
;
231 layout
.object_size
= 1ull << order
;
232 layout
.pool_id
= pool_id
; // FIXME: pool id overflow?
234 delete[] format_string
;
235 size_t len
= object_prefix
.length() + 16;
236 format_string
= new char[len
];
238 snprintf(format_string
, len
, "%s.%%012llx", object_prefix
.c_str());
240 snprintf(format_string
, len
, "%s.%%016llx", object_prefix
.c_str());
243 ldout(cct
, 10) << "init_layout stripe_unit " << stripe_unit
244 << " stripe_count " << stripe_count
245 << " object_size " << layout
.object_size
246 << " prefix " << object_prefix
247 << " format " << format_string
251 void ImageCtx::perf_start(string name
) {
252 auto perf_prio
= PerfCountersBuilder::PRIO_DEBUGONLY
;
253 if (child
== nullptr) {
254 // ensure top-level IO stats are exported for librbd daemons
255 perf_prio
= PerfCountersBuilder::PRIO_USEFUL
;
258 PerfCountersBuilder
plb(cct
, name
, l_librbd_first
, l_librbd_last
);
260 plb
.add_u64_counter(l_librbd_rd
, "rd", "Reads", "r", perf_prio
);
261 plb
.add_u64_counter(l_librbd_rd_bytes
, "rd_bytes", "Data size in reads",
262 "rb", perf_prio
, unit_t(UNIT_BYTES
));
263 plb
.add_time_avg(l_librbd_rd_latency
, "rd_latency", "Latency of reads",
265 plb
.add_u64_counter(l_librbd_wr
, "wr", "Writes", "w", perf_prio
);
266 plb
.add_u64_counter(l_librbd_wr_bytes
, "wr_bytes", "Written data",
267 "wb", perf_prio
, unit_t(UNIT_BYTES
));
268 plb
.add_time_avg(l_librbd_wr_latency
, "wr_latency", "Write latency",
270 plb
.add_u64_counter(l_librbd_discard
, "discard", "Discards");
271 plb
.add_u64_counter(l_librbd_discard_bytes
, "discard_bytes", "Discarded data", NULL
, 0, unit_t(UNIT_BYTES
));
272 plb
.add_time_avg(l_librbd_discard_latency
, "discard_latency", "Discard latency");
273 plb
.add_u64_counter(l_librbd_flush
, "flush", "Flushes");
274 plb
.add_time_avg(l_librbd_flush_latency
, "flush_latency", "Latency of flushes");
275 plb
.add_u64_counter(l_librbd_ws
, "ws", "WriteSames");
276 plb
.add_u64_counter(l_librbd_ws_bytes
, "ws_bytes", "WriteSame data", NULL
, 0, unit_t(UNIT_BYTES
));
277 plb
.add_time_avg(l_librbd_ws_latency
, "ws_latency", "WriteSame latency");
278 plb
.add_u64_counter(l_librbd_cmp
, "cmp", "CompareAndWrites");
279 plb
.add_u64_counter(l_librbd_cmp_bytes
, "cmp_bytes", "Data size in cmps", NULL
, 0, unit_t(UNIT_BYTES
));
280 plb
.add_time_avg(l_librbd_cmp_latency
, "cmp_latency", "Latency of cmps");
281 plb
.add_u64_counter(l_librbd_snap_create
, "snap_create", "Snap creations");
282 plb
.add_u64_counter(l_librbd_snap_remove
, "snap_remove", "Snap removals");
283 plb
.add_u64_counter(l_librbd_snap_rollback
, "snap_rollback", "Snap rollbacks");
284 plb
.add_u64_counter(l_librbd_snap_rename
, "snap_rename", "Snap rename");
285 plb
.add_u64_counter(l_librbd_notify
, "notify", "Updated header notifications");
286 plb
.add_u64_counter(l_librbd_resize
, "resize", "Resizes");
287 plb
.add_u64_counter(l_librbd_readahead
, "readahead", "Read ahead");
288 plb
.add_u64_counter(l_librbd_readahead_bytes
, "readahead_bytes", "Data size in read ahead", NULL
, 0, unit_t(UNIT_BYTES
));
289 plb
.add_u64_counter(l_librbd_invalidate_cache
, "invalidate_cache", "Cache invalidates");
291 plb
.add_time(l_librbd_opened_time
, "opened_time", "Opened time",
293 plb
.add_time(l_librbd_lock_acquired_time
, "lock_acquired_time",
294 "Lock acquired time", "lats", perf_prio
);
296 perfcounter
= plb
.create_perf_counters();
297 cct
->get_perfcounters_collection()->add(perfcounter
);
299 perfcounter
->tset(l_librbd_opened_time
, ceph_clock_now());
302 void ImageCtx::perf_stop() {
303 ceph_assert(perfcounter
);
304 cct
->get_perfcounters_collection()->remove(perfcounter
);
308 void ImageCtx::set_read_flag(unsigned flag
) {
309 extra_read_flags
|= flag
;
312 int ImageCtx::get_read_flags(snap_t snap_id
) {
313 int flags
= librados::OPERATION_NOFLAG
| read_flags
;
317 flags
= librados::OPERATION_NOFLAG
| extra_read_flags
;
318 if (snap_id
== LIBRADOS_SNAP_HEAD
)
321 if (config
.get_val
<bool>("rbd_balance_snap_reads"))
322 flags
|= librados::OPERATION_BALANCE_READS
;
323 else if (config
.get_val
<bool>("rbd_localize_snap_reads"))
324 flags
|= librados::OPERATION_LOCALIZE_READS
;
328 int ImageCtx::snap_set(uint64_t in_snap_id
) {
329 ceph_assert(ceph_mutex_is_wlocked(image_lock
));
330 auto it
= snap_info
.find(in_snap_id
);
331 if (in_snap_id
!= CEPH_NOSNAP
&& it
!= snap_info
.end()) {
332 snap_id
= in_snap_id
;
333 snap_namespace
= it
->second
.snap_namespace
;
334 snap_name
= it
->second
.name
;
336 if (data_ctx
.is_valid()) {
337 data_ctx
.snap_set_read(snap_id
);
344 void ImageCtx::snap_unset()
346 ceph_assert(ceph_mutex_is_wlocked(image_lock
));
347 snap_id
= CEPH_NOSNAP
;
351 if (data_ctx
.is_valid()) {
352 data_ctx
.snap_set_read(snap_id
);
356 snap_t
ImageCtx::get_snap_id(const cls::rbd::SnapshotNamespace
& in_snap_namespace
,
357 const string
& in_snap_name
) const
359 ceph_assert(ceph_mutex_is_locked(image_lock
));
360 auto it
= snap_ids
.find({in_snap_namespace
, in_snap_name
});
361 if (it
!= snap_ids
.end()) {
367 const SnapInfo
* ImageCtx::get_snap_info(snap_t in_snap_id
) const
369 ceph_assert(ceph_mutex_is_locked(image_lock
));
370 map
<snap_t
, SnapInfo
>::const_iterator it
=
371 snap_info
.find(in_snap_id
);
372 if (it
!= snap_info
.end())
377 int ImageCtx::get_snap_name(snap_t in_snap_id
,
378 string
*out_snap_name
) const
380 ceph_assert(ceph_mutex_is_locked(image_lock
));
381 const SnapInfo
*info
= get_snap_info(in_snap_id
);
383 *out_snap_name
= info
->name
;
389 int ImageCtx::get_snap_namespace(snap_t in_snap_id
,
390 cls::rbd::SnapshotNamespace
*out_snap_namespace
) const
392 ceph_assert(ceph_mutex_is_locked(image_lock
));
393 const SnapInfo
*info
= get_snap_info(in_snap_id
);
395 *out_snap_namespace
= info
->snap_namespace
;
401 int ImageCtx::get_parent_spec(snap_t in_snap_id
,
402 cls::rbd::ParentImageSpec
*out_pspec
) const
404 const SnapInfo
*info
= get_snap_info(in_snap_id
);
406 *out_pspec
= info
->parent
.spec
;
412 uint64_t ImageCtx::get_current_size() const
414 ceph_assert(ceph_mutex_is_locked(image_lock
));
418 uint64_t ImageCtx::get_object_size() const
420 return 1ull << order
;
423 string
ImageCtx::get_object_name(uint64_t num
) const {
424 return util::data_object_name(this, num
);
427 uint64_t ImageCtx::get_stripe_unit() const
432 uint64_t ImageCtx::get_stripe_count() const
437 uint64_t ImageCtx::get_stripe_period() const
439 return stripe_count
* (1ull << order
);
442 utime_t
ImageCtx::get_create_timestamp() const
444 return create_timestamp
;
447 utime_t
ImageCtx::get_access_timestamp() const
449 return access_timestamp
;
452 utime_t
ImageCtx::get_modify_timestamp() const
454 return modify_timestamp
;
457 void ImageCtx::set_access_timestamp(utime_t at
)
459 ceph_assert(ceph_mutex_is_wlocked(timestamp_lock
));
460 access_timestamp
= at
;
463 void ImageCtx::set_modify_timestamp(utime_t mt
)
465 ceph_assert(ceph_mutex_is_locked(timestamp_lock
));
466 modify_timestamp
= mt
;
469 int ImageCtx::is_snap_protected(snap_t in_snap_id
,
470 bool *is_protected
) const
472 ceph_assert(ceph_mutex_is_locked(image_lock
));
473 const SnapInfo
*info
= get_snap_info(in_snap_id
);
476 (info
->protection_status
== RBD_PROTECTION_STATUS_PROTECTED
);
482 int ImageCtx::is_snap_unprotected(snap_t in_snap_id
,
483 bool *is_unprotected
) const
485 ceph_assert(ceph_mutex_is_locked(image_lock
));
486 const SnapInfo
*info
= get_snap_info(in_snap_id
);
489 (info
->protection_status
== RBD_PROTECTION_STATUS_UNPROTECTED
);
495 void ImageCtx::add_snap(cls::rbd::SnapshotNamespace in_snap_namespace
,
497 snap_t id
, uint64_t in_size
,
498 const ParentImageInfo
&parent
,
499 uint8_t protection_status
, uint64_t flags
,
502 ceph_assert(ceph_mutex_is_wlocked(image_lock
));
504 SnapInfo
info(in_snap_name
, in_snap_namespace
,
505 in_size
, parent
, protection_status
, flags
, timestamp
);
506 snap_info
.insert({id
, info
});
507 snap_ids
.insert({{in_snap_namespace
, in_snap_name
}, id
});
510 void ImageCtx::rm_snap(cls::rbd::SnapshotNamespace in_snap_namespace
,
514 ceph_assert(ceph_mutex_is_wlocked(image_lock
));
515 snaps
.erase(std::remove(snaps
.begin(), snaps
.end(), id
), snaps
.end());
517 snap_ids
.erase({in_snap_namespace
, in_snap_name
});
520 uint64_t ImageCtx::get_image_size(snap_t in_snap_id
) const
522 ceph_assert(ceph_mutex_is_locked(image_lock
));
523 if (in_snap_id
== CEPH_NOSNAP
) {
524 if (!resize_reqs
.empty() &&
525 resize_reqs
.front()->shrinking()) {
526 return resize_reqs
.front()->get_image_size();
531 const SnapInfo
*info
= get_snap_info(in_snap_id
);
538 uint64_t ImageCtx::get_object_count(snap_t in_snap_id
) const {
539 ceph_assert(ceph_mutex_is_locked(image_lock
));
540 uint64_t image_size
= get_image_size(in_snap_id
);
541 return Striper::get_num_objects(layout
, image_size
);
544 bool ImageCtx::test_features(uint64_t features
) const
546 std::shared_lock l
{image_lock
};
547 return test_features(features
, image_lock
);
550 bool ImageCtx::test_features(uint64_t in_features
,
551 const ceph::shared_mutex
&in_image_lock
) const
553 ceph_assert(ceph_mutex_is_locked(image_lock
));
554 return ((features
& in_features
) == in_features
);
557 bool ImageCtx::test_op_features(uint64_t in_op_features
) const
559 std::shared_lock l
{image_lock
};
560 return test_op_features(in_op_features
, image_lock
);
563 bool ImageCtx::test_op_features(uint64_t in_op_features
,
564 const ceph::shared_mutex
&in_image_lock
) const
566 ceph_assert(ceph_mutex_is_locked(image_lock
));
567 return ((op_features
& in_op_features
) == in_op_features
);
570 int ImageCtx::get_flags(librados::snap_t _snap_id
, uint64_t *_flags
) const
572 ceph_assert(ceph_mutex_is_locked(image_lock
));
573 if (_snap_id
== CEPH_NOSNAP
) {
577 const SnapInfo
*info
= get_snap_info(_snap_id
);
579 *_flags
= info
->flags
;
585 int ImageCtx::test_flags(librados::snap_t in_snap_id
,
586 uint64_t flags
, bool *flags_set
) const
588 std::shared_lock l
{image_lock
};
589 return test_flags(in_snap_id
, flags
, image_lock
, flags_set
);
592 int ImageCtx::test_flags(librados::snap_t in_snap_id
,
594 const ceph::shared_mutex
&in_image_lock
,
595 bool *flags_set
) const
597 ceph_assert(ceph_mutex_is_locked(image_lock
));
599 int r
= get_flags(in_snap_id
, &snap_flags
);
603 *flags_set
= ((snap_flags
& flags
) == flags
);
607 int ImageCtx::update_flags(snap_t in_snap_id
, uint64_t flag
, bool enabled
)
609 ceph_assert(ceph_mutex_is_wlocked(image_lock
));
611 if (in_snap_id
== CEPH_NOSNAP
) {
614 map
<snap_t
, SnapInfo
>::iterator it
= snap_info
.find(in_snap_id
);
615 if (it
== snap_info
.end()) {
618 _flags
= &it
->second
.flags
;
629 const ParentImageInfo
* ImageCtx::get_parent_info(snap_t in_snap_id
) const
631 ceph_assert(ceph_mutex_is_locked(image_lock
));
632 if (in_snap_id
== CEPH_NOSNAP
)
634 const SnapInfo
*info
= get_snap_info(in_snap_id
);
636 return &info
->parent
;
640 int64_t ImageCtx::get_parent_pool_id(snap_t in_snap_id
) const
642 const auto info
= get_parent_info(in_snap_id
);
644 return info
->spec
.pool_id
;
648 string
ImageCtx::get_parent_image_id(snap_t in_snap_id
) const
650 const auto info
= get_parent_info(in_snap_id
);
652 return info
->spec
.image_id
;
656 uint64_t ImageCtx::get_parent_snap_id(snap_t in_snap_id
) const
658 const auto info
= get_parent_info(in_snap_id
);
660 return info
->spec
.snap_id
;
664 int ImageCtx::get_parent_overlap(snap_t in_snap_id
, uint64_t *overlap
) const
666 ceph_assert(ceph_mutex_is_locked(image_lock
));
667 const auto info
= get_parent_info(in_snap_id
);
669 *overlap
= info
->overlap
;
675 void ImageCtx::register_watch(Context
*on_finish
) {
676 ceph_assert(image_watcher
!= NULL
);
677 image_watcher
->register_watch(on_finish
);
680 uint64_t ImageCtx::prune_parent_extents(vector
<pair
<uint64_t,uint64_t> >& objectx
,
683 // drop extents completely beyond the overlap
684 while (!objectx
.empty() && objectx
.back().first
>= overlap
)
687 // trim final overlapping extent
688 if (!objectx
.empty() && objectx
.back().first
+ objectx
.back().second
> overlap
)
689 objectx
.back().second
= overlap
- objectx
.back().first
;
692 for (vector
<pair
<uint64_t,uint64_t> >::iterator p
= objectx
.begin();
696 ldout(cct
, 10) << "prune_parent_extents image overlap " << overlap
697 << ", object overlap " << len
698 << " from image extents " << objectx
<< dendl
;
702 void ImageCtx::cancel_async_requests() {
704 cancel_async_requests(&ctx
);
708 void ImageCtx::cancel_async_requests(Context
*on_finish
) {
710 std::lock_guard async_ops_locker
{async_ops_lock
};
711 if (!async_requests
.empty()) {
712 ldout(cct
, 10) << "canceling async requests: count="
713 << async_requests
.size() << dendl
;
714 for (auto req
: async_requests
) {
715 ldout(cct
, 10) << "canceling async request: " << req
<< dendl
;
718 async_requests_waiters
.push_back(on_finish
);
723 on_finish
->complete(0);
726 void ImageCtx::apply_metadata(const std::map
<std::string
, bufferlist
> &meta
,
728 ldout(cct
, 20) << __func__
<< dendl
;
730 // reset settings back to global defaults
731 for (auto& key
: config_overrides
) {
733 int r
= cct
->_conf
.get_val(key
, &value
);
736 config
.set_val(key
, value
);
738 config_overrides
.clear();
740 // extract config overrides
741 for (auto meta_pair
: meta
) {
742 if (!boost::starts_with(meta_pair
.first
, METADATA_CONF_PREFIX
)) {
746 std::string key
= meta_pair
.first
.substr(METADATA_CONF_PREFIX
.size());
747 if (!boost::starts_with(key
, "rbd_")) {
748 // ignore non-RBD configuration keys
749 // TODO use option schema to determine applicable subsystem
750 ldout(cct
, 0) << __func__
<< ": ignoring config " << key
<< dendl
;
754 if (config
.find_option(key
) != nullptr) {
755 std::string
val(meta_pair
.second
.c_str(), meta_pair
.second
.length());
756 int r
= config
.set_val(key
, val
);
758 ldout(cct
, 20) << __func__
<< ": " << key
<< "=" << val
<< dendl
;
759 config_overrides
.insert(key
);
761 lderr(cct
) << __func__
<< ": failed to set config " << key
<< " "
762 << "with value " << val
<< ": " << cpp_strerror(r
)
768 #define ASSIGN_OPTION(param, type) \
769 param = config.get_val<type>("rbd_"#param)
771 bool skip_partial_discard
= true;
772 ASSIGN_OPTION(non_blocking_aio
, bool);
773 ASSIGN_OPTION(cache
, bool);
774 ASSIGN_OPTION(sparse_read_threshold_bytes
, Option::size_t);
775 ASSIGN_OPTION(readahead_max_bytes
, Option::size_t);
776 ASSIGN_OPTION(readahead_disable_after_bytes
, Option::size_t);
777 ASSIGN_OPTION(clone_copy_on_read
, bool);
778 ASSIGN_OPTION(enable_alloc_hint
, bool);
779 ASSIGN_OPTION(mirroring_replay_delay
, uint64_t);
780 ASSIGN_OPTION(mtime_update_interval
, uint64_t);
781 ASSIGN_OPTION(atime_update_interval
, uint64_t);
782 ASSIGN_OPTION(skip_partial_discard
, bool);
783 ASSIGN_OPTION(discard_granularity_bytes
, uint64_t);
784 ASSIGN_OPTION(blkin_trace_all
, bool);
788 if (sparse_read_threshold_bytes
== 0) {
789 sparse_read_threshold_bytes
= get_object_size();
791 if (!skip_partial_discard
) {
792 discard_granularity_bytes
= 0;
795 alloc_hint_flags
= 0;
796 auto compression_hint
= config
.get_val
<std::string
>("rbd_compression_hint");
797 if (compression_hint
== "compressible") {
798 alloc_hint_flags
|= librados::ALLOC_HINT_FLAG_COMPRESSIBLE
;
799 } else if (compression_hint
== "incompressible") {
800 alloc_hint_flags
|= librados::ALLOC_HINT_FLAG_INCOMPRESSIBLE
;
803 librados::Rados
rados(md_ctx
);
804 int8_t require_osd_release
;
805 int r
= rados
.get_min_compatible_osd(&require_osd_release
);
806 if (r
== 0 && require_osd_release
>= CEPH_RELEASE_OCTOPUS
) {
808 auto read_policy
= config
.get_val
<std::string
>("rbd_read_from_replica_policy");
809 if (read_policy
== "balance") {
810 read_flags
|= CEPH_OSD_FLAG_BALANCE_READS
;
811 } else if (read_policy
== "localize") {
812 read_flags
|= CEPH_OSD_FLAG_LOCALIZE_READS
;
816 io_work_queue
->apply_qos_schedule_tick_min(
817 config
.get_val
<uint64_t>("rbd_qos_schedule_tick_min"));
819 io_work_queue
->apply_qos_limit(
820 RBD_QOS_IOPS_THROTTLE
,
821 config
.get_val
<uint64_t>("rbd_qos_iops_limit"),
822 config
.get_val
<uint64_t>("rbd_qos_iops_burst"));
823 io_work_queue
->apply_qos_limit(
824 RBD_QOS_BPS_THROTTLE
,
825 config
.get_val
<uint64_t>("rbd_qos_bps_limit"),
826 config
.get_val
<uint64_t>("rbd_qos_bps_burst"));
827 io_work_queue
->apply_qos_limit(
828 RBD_QOS_READ_IOPS_THROTTLE
,
829 config
.get_val
<uint64_t>("rbd_qos_read_iops_limit"),
830 config
.get_val
<uint64_t>("rbd_qos_read_iops_burst"));
831 io_work_queue
->apply_qos_limit(
832 RBD_QOS_WRITE_IOPS_THROTTLE
,
833 config
.get_val
<uint64_t>("rbd_qos_write_iops_limit"),
834 config
.get_val
<uint64_t>("rbd_qos_write_iops_burst"));
835 io_work_queue
->apply_qos_limit(
836 RBD_QOS_READ_BPS_THROTTLE
,
837 config
.get_val
<uint64_t>("rbd_qos_read_bps_limit"),
838 config
.get_val
<uint64_t>("rbd_qos_read_bps_burst"));
839 io_work_queue
->apply_qos_limit(
840 RBD_QOS_WRITE_BPS_THROTTLE
,
841 config
.get_val
<uint64_t>("rbd_qos_write_bps_limit"),
842 config
.get_val
<uint64_t>("rbd_qos_write_bps_burst"));
844 if (!disable_zero_copy
&&
845 config
.get_val
<bool>("rbd_disable_zero_copy_writes")) {
846 ldout(cct
, 5) << this << ": disabling zero-copy writes" << dendl
;
847 disable_zero_copy
= true;
851 ExclusiveLock
<ImageCtx
> *ImageCtx::create_exclusive_lock() {
852 return new ExclusiveLock
<ImageCtx
>(*this);
855 ObjectMap
<ImageCtx
> *ImageCtx::create_object_map(uint64_t snap_id
) {
856 return new ObjectMap
<ImageCtx
>(*this, snap_id
);
859 Journal
<ImageCtx
> *ImageCtx::create_journal() {
860 return new Journal
<ImageCtx
>(*this);
863 void ImageCtx::set_image_name(const std::string
&image_name
) {
864 // update the name so rename can be invoked repeatedly
865 std::shared_lock owner_locker
{owner_lock
};
866 std::unique_lock image_locker
{image_lock
};
869 header_oid
= util::old_header_name(image_name
);
873 void ImageCtx::notify_update() {
874 state
->handle_update_notification();
875 ImageWatcher
<>::notify_header_update(md_ctx
, header_oid
);
878 void ImageCtx::notify_update(Context
*on_finish
) {
879 state
->handle_update_notification();
880 image_watcher
->notify_header_update(on_finish
);
883 exclusive_lock::Policy
*ImageCtx::get_exclusive_lock_policy() const {
884 ceph_assert(ceph_mutex_is_locked(owner_lock
));
885 ceph_assert(exclusive_lock_policy
!= nullptr);
886 return exclusive_lock_policy
;
889 void ImageCtx::set_exclusive_lock_policy(exclusive_lock::Policy
*policy
) {
890 ceph_assert(ceph_mutex_is_wlocked(owner_lock
));
891 ceph_assert(policy
!= nullptr);
892 delete exclusive_lock_policy
;
893 exclusive_lock_policy
= policy
;
896 journal::Policy
*ImageCtx::get_journal_policy() const {
897 ceph_assert(ceph_mutex_is_locked(image_lock
));
898 ceph_assert(journal_policy
!= nullptr);
899 return journal_policy
;
902 void ImageCtx::set_journal_policy(journal::Policy
*policy
) {
903 ceph_assert(ceph_mutex_is_wlocked(image_lock
));
904 ceph_assert(policy
!= nullptr);
905 delete journal_policy
;
906 journal_policy
= policy
;
909 void ImageCtx::get_thread_pool_instance(CephContext
*cct
,
910 ThreadPool
**thread_pool
,
911 ContextWQ
**op_work_queue
) {
912 auto thread_pool_singleton
=
913 &cct
->lookup_or_create_singleton_object
<ThreadPoolSingleton
>(
914 "librbd::thread_pool", false, cct
);
915 *thread_pool
= thread_pool_singleton
;
916 *op_work_queue
= thread_pool_singleton
->op_work_queue
;
919 void ImageCtx::get_timer_instance(CephContext
*cct
, SafeTimer
**timer
,
920 ceph::mutex
**timer_lock
) {
921 auto safe_timer_singleton
=
922 &cct
->lookup_or_create_singleton_object
<SafeTimerSingleton
>(
923 "librbd::journal::safe_timer", false, cct
);
924 *timer
= safe_timer_singleton
;
925 *timer_lock
= &safe_timer_singleton
->lock
;