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/Utils.h"
26 #include "librbd/LibrbdWriteback.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/journal/StandardPolicy.h"
34 #include "osdc/Striper.h"
35 #include <boost/bind.hpp>
37 #define dout_subsys ceph_subsys_rbd
39 #define dout_prefix *_dout << "librbd::ImageCtx: "
47 using ceph::bufferlist
;
48 using librados::snap_t
;
49 using librados::IoCtx
;
55 class ThreadPoolSingleton
: public ThreadPool
{
57 ContextWQ
*op_work_queue
;
59 explicit ThreadPoolSingleton(CephContext
*cct
)
60 : ThreadPool(cct
, "librbd::thread_pool", "tp_librbd", 1,
62 op_work_queue(new ContextWQ("librbd::op_work_queue",
63 cct
->_conf
->rbd_op_thread_timeout
,
67 ~ThreadPoolSingleton() override
{
68 op_work_queue
->drain();
75 class SafeTimerSingleton
: public SafeTimer
{
79 explicit SafeTimerSingleton(CephContext
*cct
)
80 : SafeTimer(cct
, lock
, true),
81 lock("librbd::Journal::SafeTimerSingleton::lock") {
84 ~SafeTimerSingleton() {
85 Mutex::Locker
locker(lock
);
90 struct C_FlushCache
: public Context
{
94 C_FlushCache(ImageCtx
*_image_ctx
, Context
*_on_safe
)
95 : image_ctx(_image_ctx
), on_safe(_on_safe
) {
97 void finish(int r
) override
{
98 // successful cache flush indicates all IO is now safe
99 image_ctx
->flush_cache(on_safe
);
103 struct C_ShutDownCache
: public Context
{
107 C_ShutDownCache(ImageCtx
*_image_ctx
, Context
*_on_finish
)
108 : image_ctx(_image_ctx
), on_finish(_on_finish
) {
110 void finish(int r
) override
{
111 image_ctx
->object_cacher
->stop();
112 on_finish
->complete(r
);
116 struct C_InvalidateCache
: public Context
{
122 C_InvalidateCache(ImageCtx
*_image_ctx
, bool _purge_on_error
,
123 bool _reentrant_safe
, Context
*_on_finish
)
124 : image_ctx(_image_ctx
), purge_on_error(_purge_on_error
),
125 reentrant_safe(_reentrant_safe
), on_finish(_on_finish
) {
127 void finish(int r
) override
{
128 assert(image_ctx
->cache_lock
.is_locked());
129 CephContext
*cct
= image_ctx
->cct
;
131 if (r
== -EBLACKLISTED
) {
132 lderr(cct
) << "Blacklisted during flush! Purging cache..." << dendl
;
133 image_ctx
->object_cacher
->purge_set(image_ctx
->object_set
);
134 } else if (r
!= 0 && purge_on_error
) {
135 lderr(cct
) << "invalidate cache encountered error "
136 << cpp_strerror(r
) << " !Purging cache..." << dendl
;
137 image_ctx
->object_cacher
->purge_set(image_ctx
->object_set
);
139 lderr(cct
) << "flush_cache returned " << r
<< dendl
;
142 loff_t unclean
= image_ctx
->object_cacher
->release_set(
143 image_ctx
->object_set
);
147 lderr(cct
) << "could not release all objects from cache: "
148 << unclean
<< " bytes remain" << dendl
;
154 if (reentrant_safe
) {
155 on_finish
->complete(r
);
157 image_ctx
->op_work_queue
->queue(on_finish
, r
);
163 } // anonymous namespace
165 const string
ImageCtx::METADATA_CONF_PREFIX
= "conf_";
167 ImageCtx::ImageCtx(const string
&image_name
, const string
&image_id
,
168 const char *snap
, IoCtx
& p
, bool ro
)
169 : cct((CephContext
*)p
.cct()),
171 snap_id(CEPH_NOSNAP
),
174 flush_encountered(false),
175 exclusive_locked(false),
179 owner_lock(util::unique_lock_name("librbd::ImageCtx::owner_lock", this)),
180 md_lock(util::unique_lock_name("librbd::ImageCtx::md_lock", this)),
181 cache_lock(util::unique_lock_name("librbd::ImageCtx::cache_lock", this)),
182 snap_lock(util::unique_lock_name("librbd::ImageCtx::snap_lock", this)),
183 parent_lock(util::unique_lock_name("librbd::ImageCtx::parent_lock", this)),
184 object_map_lock(util::unique_lock_name("librbd::ImageCtx::object_map_lock", this)),
185 async_ops_lock(util::unique_lock_name("librbd::ImageCtx::async_ops_lock", this)),
186 copyup_list_lock(util::unique_lock_name("librbd::ImageCtx::copyup_list_lock", this)),
187 completed_reqs_lock(util::unique_lock_name("librbd::ImageCtx::completed_reqs_lock", this)),
190 order(0), size(0), features(0),
192 id(image_id
), parent(NULL
),
193 stripe_unit(0), stripe_count(0), flags(0),
194 object_cacher(NULL
), writeback_handler(NULL
), object_set(NULL
),
197 state(new ImageState
<>(this)),
198 operations(new Operations
<>(*this)),
199 exclusive_lock(nullptr), object_map(nullptr),
200 io_work_queue(nullptr), op_work_queue(nullptr),
202 trace_endpoint("librbd")
209 memset(&header
, 0, sizeof(header
));
211 ThreadPool
*thread_pool
;
212 get_thread_pool_instance(cct
, &thread_pool
, &op_work_queue
);
213 io_work_queue
= new io::ImageRequestWQ
<>(
214 this, "librbd::io_work_queue", cct
->_conf
->rbd_op_thread_timeout
,
217 if (cct
->_conf
->rbd_auto_exclusive_lock_until_manual_request
) {
218 exclusive_lock_policy
= new exclusive_lock::AutomaticPolicy(this);
220 exclusive_lock_policy
= new exclusive_lock::StandardPolicy(this);
222 journal_policy
= new journal::StandardPolicy
<ImageCtx
>(this);
225 ImageCtx::~ImageCtx() {
226 assert(image_watcher
== NULL
);
227 assert(exclusive_lock
== NULL
);
228 assert(object_map
== NULL
);
229 assert(journal
== NULL
);
230 assert(asok_hook
== NULL
);
236 delete object_cacher
;
237 object_cacher
= NULL
;
239 if (writeback_handler
) {
240 delete writeback_handler
;
241 writeback_handler
= NULL
;
247 delete[] format_string
;
250 data_ctx
.aio_flush();
251 io_work_queue
->drain();
253 delete journal_policy
;
254 delete exclusive_lock_policy
;
255 delete io_work_queue
;
260 void ImageCtx::init() {
261 assert(!header_oid
.empty());
262 assert(old_format
|| !id
.empty());
264 asok_hook
= new LibrbdAdminSocketHook(this);
266 string pname
= string("librbd-") + id
+ string("-") +
267 data_ctx
.get_pool_name() + string("-") + name
;
268 if (!snap_name
.empty()) {
273 trace_endpoint
.copy_name(pname
);
277 Mutex::Locker
l(cache_lock
);
278 ldout(cct
, 20) << "enabling caching..." << dendl
;
279 writeback_handler
= new LibrbdWriteback(this, cache_lock
);
281 uint64_t init_max_dirty
= cache_max_dirty
;
282 if (cache_writethrough_until_flush
)
284 ldout(cct
, 20) << "Initial cache settings:"
285 << " size=" << cache_size
286 << " num_objects=" << 10
287 << " max_dirty=" << init_max_dirty
288 << " target_dirty=" << cache_target_dirty
290 << cache_max_dirty_age
<< dendl
;
292 object_cacher
= new ObjectCacher(cct
, pname
, *writeback_handler
, cache_lock
,
295 10, /* reset this in init */
299 cache_block_writes_upfront
);
301 // size object cache appropriately
302 uint64_t obj
= cache_max_dirty_object
;
304 obj
= MIN(2000, MAX(10, cache_size
/ 100 / sizeof(ObjectCacher::Object
)));
306 ldout(cct
, 10) << " cache bytes " << cache_size
307 << " -> about " << obj
<< " objects" << dendl
;
308 object_cacher
->set_max_objects(obj
);
310 object_set
= new ObjectCacher::ObjectSet(NULL
, data_ctx
.get_id(), 0);
311 object_set
->return_enoent
= true;
312 object_cacher
->start();
315 readahead
.set_trigger_requests(readahead_trigger_requests
);
316 readahead
.set_max_readahead_size(readahead_max_bytes
);
319 void ImageCtx::shutdown() {
320 delete image_watcher
;
321 image_watcher
= nullptr;
327 void ImageCtx::init_layout()
329 if (stripe_unit
== 0 || stripe_count
== 0) {
330 stripe_unit
= 1ull << order
;
334 vector
<uint64_t> alignments
;
335 alignments
.push_back(stripe_count
<< order
); // object set (in file striping terminology)
336 alignments
.push_back(stripe_unit
* stripe_count
); // stripe
337 alignments
.push_back(stripe_unit
); // stripe unit
338 readahead
.set_alignments(alignments
);
340 layout
= file_layout_t();
341 layout
.stripe_unit
= stripe_unit
;
342 layout
.stripe_count
= stripe_count
;
343 layout
.object_size
= 1ull << order
;
344 layout
.pool_id
= data_ctx
.get_id(); // FIXME: pool id overflow?
346 delete[] format_string
;
347 size_t len
= object_prefix
.length() + 16;
348 format_string
= new char[len
];
350 snprintf(format_string
, len
, "%s.%%012llx", object_prefix
.c_str());
352 snprintf(format_string
, len
, "%s.%%016llx", object_prefix
.c_str());
355 ldout(cct
, 10) << "init_layout stripe_unit " << stripe_unit
356 << " stripe_count " << stripe_count
357 << " object_size " << layout
.object_size
358 << " prefix " << object_prefix
359 << " format " << format_string
363 void ImageCtx::perf_start(string name
) {
364 PerfCountersBuilder
plb(cct
, name
, l_librbd_first
, l_librbd_last
);
366 plb
.add_u64_counter(l_librbd_rd
, "rd", "Reads");
367 plb
.add_u64_counter(l_librbd_rd_bytes
, "rd_bytes", "Data size in reads");
368 plb
.add_time_avg(l_librbd_rd_latency
, "rd_latency", "Latency of reads");
369 plb
.add_u64_counter(l_librbd_wr
, "wr", "Writes");
370 plb
.add_u64_counter(l_librbd_wr_bytes
, "wr_bytes", "Written data");
371 plb
.add_time_avg(l_librbd_wr_latency
, "wr_latency", "Write latency");
372 plb
.add_u64_counter(l_librbd_discard
, "discard", "Discards");
373 plb
.add_u64_counter(l_librbd_discard_bytes
, "discard_bytes", "Discarded data");
374 plb
.add_time_avg(l_librbd_discard_latency
, "discard_latency", "Discard latency");
375 plb
.add_u64_counter(l_librbd_flush
, "flush", "Flushes");
376 plb
.add_u64_counter(l_librbd_aio_flush
, "aio_flush", "Async flushes");
377 plb
.add_time_avg(l_librbd_aio_flush_latency
, "aio_flush_latency", "Latency of async flushes");
378 plb
.add_u64_counter(l_librbd_ws
, "ws", "WriteSames");
379 plb
.add_u64_counter(l_librbd_ws_bytes
, "ws_bytes", "WriteSame data");
380 plb
.add_time_avg(l_librbd_ws_latency
, "ws_latency", "WriteSame latency");
381 plb
.add_u64_counter(l_librbd_snap_create
, "snap_create", "Snap creations");
382 plb
.add_u64_counter(l_librbd_snap_remove
, "snap_remove", "Snap removals");
383 plb
.add_u64_counter(l_librbd_snap_rollback
, "snap_rollback", "Snap rollbacks");
384 plb
.add_u64_counter(l_librbd_snap_rename
, "snap_rename", "Snap rename");
385 plb
.add_u64_counter(l_librbd_notify
, "notify", "Updated header notifications");
386 plb
.add_u64_counter(l_librbd_resize
, "resize", "Resizes");
387 plb
.add_u64_counter(l_librbd_readahead
, "readahead", "Read ahead");
388 plb
.add_u64_counter(l_librbd_readahead_bytes
, "readahead_bytes", "Data size in read ahead");
389 plb
.add_u64_counter(l_librbd_invalidate_cache
, "invalidate_cache", "Cache invalidates");
391 perfcounter
= plb
.create_perf_counters();
392 cct
->get_perfcounters_collection()->add(perfcounter
);
395 void ImageCtx::perf_stop() {
397 cct
->get_perfcounters_collection()->remove(perfcounter
);
401 void ImageCtx::set_read_flag(unsigned flag
) {
402 extra_read_flags
|= flag
;
405 int ImageCtx::get_read_flags(snap_t snap_id
) {
406 int flags
= librados::OPERATION_NOFLAG
| extra_read_flags
;
407 if (snap_id
== LIBRADOS_SNAP_HEAD
)
410 if (balance_snap_reads
)
411 flags
|= librados::OPERATION_BALANCE_READS
;
412 else if (localize_snap_reads
)
413 flags
|= librados::OPERATION_LOCALIZE_READS
;
417 int ImageCtx::snap_set(cls::rbd::SnapshotNamespace in_snap_namespace
,
420 assert(snap_lock
.is_wlocked());
421 snap_t in_snap_id
= get_snap_id(in_snap_namespace
, in_snap_name
);
422 if (in_snap_id
!= CEPH_NOSNAP
) {
423 snap_id
= in_snap_id
;
424 snap_namespace
= in_snap_namespace
;
425 snap_name
= in_snap_name
;
427 data_ctx
.snap_set_read(snap_id
);
433 void ImageCtx::snap_unset()
435 assert(snap_lock
.is_wlocked());
436 snap_id
= CEPH_NOSNAP
;
440 data_ctx
.snap_set_read(snap_id
);
443 snap_t
ImageCtx::get_snap_id(cls::rbd::SnapshotNamespace in_snap_namespace
,
444 string in_snap_name
) const
446 assert(snap_lock
.is_locked());
447 auto it
= snap_ids
.find({in_snap_namespace
, in_snap_name
});
448 if (it
!= snap_ids
.end())
453 const SnapInfo
* ImageCtx::get_snap_info(snap_t in_snap_id
) const
455 assert(snap_lock
.is_locked());
456 map
<snap_t
, SnapInfo
>::const_iterator it
=
457 snap_info
.find(in_snap_id
);
458 if (it
!= snap_info
.end())
463 int ImageCtx::get_snap_name(snap_t in_snap_id
,
464 string
*out_snap_name
) const
466 assert(snap_lock
.is_locked());
467 const SnapInfo
*info
= get_snap_info(in_snap_id
);
469 *out_snap_name
= info
->name
;
475 int ImageCtx::get_snap_namespace(snap_t in_snap_id
,
476 cls::rbd::SnapshotNamespace
*out_snap_namespace
) const
478 assert(snap_lock
.is_locked());
479 const SnapInfo
*info
= get_snap_info(in_snap_id
);
481 *out_snap_namespace
= info
->snap_namespace
;
487 int ImageCtx::get_parent_spec(snap_t in_snap_id
,
488 ParentSpec
*out_pspec
) const
490 const SnapInfo
*info
= get_snap_info(in_snap_id
);
492 *out_pspec
= info
->parent
.spec
;
498 uint64_t ImageCtx::get_current_size() const
500 assert(snap_lock
.is_locked());
504 uint64_t ImageCtx::get_object_size() const
506 return 1ull << order
;
509 string
ImageCtx::get_object_name(uint64_t num
) const {
510 char buf
[object_prefix
.length() + 32];
511 snprintf(buf
, sizeof(buf
), format_string
, num
);
515 uint64_t ImageCtx::get_stripe_unit() const
520 uint64_t ImageCtx::get_stripe_count() const
525 uint64_t ImageCtx::get_stripe_period() const
527 return stripe_count
* (1ull << order
);
530 utime_t
ImageCtx::get_create_timestamp() const
532 return create_timestamp
;
535 int ImageCtx::is_snap_protected(snap_t in_snap_id
,
536 bool *is_protected
) const
538 assert(snap_lock
.is_locked());
539 const SnapInfo
*info
= get_snap_info(in_snap_id
);
542 (info
->protection_status
== RBD_PROTECTION_STATUS_PROTECTED
);
548 int ImageCtx::is_snap_unprotected(snap_t in_snap_id
,
549 bool *is_unprotected
) const
551 assert(snap_lock
.is_locked());
552 const SnapInfo
*info
= get_snap_info(in_snap_id
);
555 (info
->protection_status
== RBD_PROTECTION_STATUS_UNPROTECTED
);
561 void ImageCtx::add_snap(cls::rbd::SnapshotNamespace in_snap_namespace
,
563 snap_t id
, uint64_t in_size
,
564 const ParentInfo
&parent
, uint8_t protection_status
,
565 uint64_t flags
, utime_t timestamp
)
567 assert(snap_lock
.is_wlocked());
569 SnapInfo
info(in_snap_name
, in_snap_namespace
,
570 in_size
, parent
, protection_status
, flags
, timestamp
);
571 snap_info
.insert({id
, info
});
572 snap_ids
.insert({{in_snap_namespace
, in_snap_name
}, id
});
575 void ImageCtx::rm_snap(cls::rbd::SnapshotNamespace in_snap_namespace
,
579 assert(snap_lock
.is_wlocked());
580 snaps
.erase(std::remove(snaps
.begin(), snaps
.end(), id
), snaps
.end());
582 snap_ids
.erase({in_snap_namespace
, in_snap_name
});
585 uint64_t ImageCtx::get_image_size(snap_t in_snap_id
) const
587 assert(snap_lock
.is_locked());
588 if (in_snap_id
== CEPH_NOSNAP
) {
589 if (!resize_reqs
.empty() &&
590 resize_reqs
.front()->shrinking()) {
591 return resize_reqs
.front()->get_image_size();
596 const SnapInfo
*info
= get_snap_info(in_snap_id
);
603 uint64_t ImageCtx::get_object_count(snap_t in_snap_id
) const {
604 assert(snap_lock
.is_locked());
605 uint64_t image_size
= get_image_size(in_snap_id
);
606 return Striper::get_num_objects(layout
, image_size
);
609 bool ImageCtx::test_features(uint64_t features
) const
611 RWLock::RLocker
l(snap_lock
);
612 return test_features(features
, snap_lock
);
615 bool ImageCtx::test_features(uint64_t in_features
,
616 const RWLock
&in_snap_lock
) const
618 assert(snap_lock
.is_locked());
619 return ((features
& in_features
) == in_features
);
622 int ImageCtx::get_flags(librados::snap_t _snap_id
, uint64_t *_flags
) const
624 assert(snap_lock
.is_locked());
625 if (_snap_id
== CEPH_NOSNAP
) {
629 const SnapInfo
*info
= get_snap_info(_snap_id
);
631 *_flags
= info
->flags
;
637 int ImageCtx::test_flags(uint64_t flags
, bool *flags_set
) const
639 RWLock::RLocker
l(snap_lock
);
640 return test_flags(flags
, snap_lock
, flags_set
);
643 int ImageCtx::test_flags(uint64_t flags
, const RWLock
&in_snap_lock
,
644 bool *flags_set
) const
646 assert(snap_lock
.is_locked());
648 int r
= get_flags(snap_id
, &snap_flags
);
652 *flags_set
= ((snap_flags
& flags
) == flags
);
656 int ImageCtx::update_flags(snap_t in_snap_id
, uint64_t flag
, bool enabled
)
658 assert(snap_lock
.is_wlocked());
660 if (in_snap_id
== CEPH_NOSNAP
) {
663 map
<snap_t
, SnapInfo
>::iterator it
= snap_info
.find(in_snap_id
);
664 if (it
== snap_info
.end()) {
667 _flags
= &it
->second
.flags
;
678 const ParentInfo
* ImageCtx::get_parent_info(snap_t in_snap_id
) const
680 assert(snap_lock
.is_locked());
681 assert(parent_lock
.is_locked());
682 if (in_snap_id
== CEPH_NOSNAP
)
684 const SnapInfo
*info
= get_snap_info(in_snap_id
);
686 return &info
->parent
;
690 int64_t ImageCtx::get_parent_pool_id(snap_t in_snap_id
) const
692 const ParentInfo
*info
= get_parent_info(in_snap_id
);
694 return info
->spec
.pool_id
;
698 string
ImageCtx::get_parent_image_id(snap_t in_snap_id
) const
700 const ParentInfo
*info
= get_parent_info(in_snap_id
);
702 return info
->spec
.image_id
;
706 uint64_t ImageCtx::get_parent_snap_id(snap_t in_snap_id
) const
708 const ParentInfo
*info
= get_parent_info(in_snap_id
);
710 return info
->spec
.snap_id
;
714 int ImageCtx::get_parent_overlap(snap_t in_snap_id
, uint64_t *overlap
) const
716 assert(snap_lock
.is_locked());
717 const ParentInfo
*info
= get_parent_info(in_snap_id
);
719 *overlap
= info
->overlap
;
725 void ImageCtx::aio_read_from_cache(object_t o
, uint64_t object_no
,
726 bufferlist
*bl
, size_t len
,
727 uint64_t off
, Context
*onfinish
,
728 int fadvise_flags
, ZTracer::Trace
*trace
) {
729 snap_lock
.get_read();
730 ObjectCacher::OSDRead
*rd
= object_cacher
->prepare_read(snap_id
, bl
, fadvise_flags
);
731 snap_lock
.put_read();
732 ObjectExtent
extent(o
, object_no
, off
, len
, 0);
733 extent
.oloc
.pool
= data_ctx
.get_id();
734 extent
.buffer_extents
.push_back(make_pair(0, len
));
735 rd
->extents
.push_back(extent
);
737 int r
= object_cacher
->readx(rd
, object_set
, onfinish
, trace
);
740 onfinish
->complete(r
);
743 void ImageCtx::write_to_cache(object_t o
, const bufferlist
& bl
, size_t len
,
744 uint64_t off
, Context
*onfinish
,
745 int fadvise_flags
, uint64_t journal_tid
,
746 ZTracer::Trace
*trace
) {
747 snap_lock
.get_read();
748 ObjectCacher::OSDWrite
*wr
= object_cacher
->prepare_write(
749 snapc
, bl
, ceph::real_time::min(), fadvise_flags
, journal_tid
);
750 snap_lock
.put_read();
751 ObjectExtent
extent(o
, 0, off
, len
, 0);
752 extent
.oloc
.pool
= data_ctx
.get_id();
753 // XXX: nspace is always default, io_ctx_impl field private
754 //extent.oloc.nspace = data_ctx.io_ctx_impl->oloc.nspace;
755 extent
.buffer_extents
.push_back(make_pair(0, len
));
756 wr
->extents
.push_back(extent
);
758 Mutex::Locker
l(cache_lock
);
759 object_cacher
->writex(wr
, object_set
, onfinish
, trace
);
763 void ImageCtx::user_flushed() {
764 if (object_cacher
&& cache_writethrough_until_flush
) {
766 bool flushed_before
= flush_encountered
;
769 uint64_t max_dirty
= cache_max_dirty
;
770 if (!flushed_before
&& max_dirty
> 0) {
772 flush_encountered
= true;
775 ldout(cct
, 10) << "saw first user flush, enabling writeback" << dendl
;
776 Mutex::Locker
l(cache_lock
);
777 object_cacher
->set_max_dirty(max_dirty
);
782 void ImageCtx::flush_cache(Context
*onfinish
) {
784 object_cacher
->flush_set(object_set
, onfinish
);
788 void ImageCtx::shut_down_cache(Context
*on_finish
) {
789 if (object_cacher
== NULL
) {
790 on_finish
->complete(0);
795 object_cacher
->release_set(object_set
);
798 C_ShutDownCache
*shut_down
= new C_ShutDownCache(this, on_finish
);
799 flush_cache(new C_InvalidateCache(this, true, false, shut_down
));
802 int ImageCtx::invalidate_cache(bool purge_on_error
) {
803 flush_async_operations();
804 if (object_cacher
== NULL
) {
809 object_cacher
->release_set(object_set
);
813 flush_cache(new C_InvalidateCache(this, purge_on_error
, true, &ctx
));
815 int result
= ctx
.wait();
819 void ImageCtx::invalidate_cache(bool purge_on_error
, Context
*on_finish
) {
820 if (object_cacher
== NULL
) {
821 op_work_queue
->queue(on_finish
, 0);
826 object_cacher
->release_set(object_set
);
829 flush_cache(new C_InvalidateCache(this, purge_on_error
, false, on_finish
));
832 void ImageCtx::clear_nonexistence_cache() {
833 assert(cache_lock
.is_locked());
836 object_cacher
->clear_nonexistence(object_set
);
839 bool ImageCtx::is_cache_empty() {
840 Mutex::Locker
locker(cache_lock
);
841 return object_cacher
->set_is_empty(object_set
);
844 void ImageCtx::register_watch(Context
*on_finish
) {
845 assert(image_watcher
== NULL
);
846 image_watcher
= new ImageWatcher
<>(*this);
847 image_watcher
->register_watch(on_finish
);
850 uint64_t ImageCtx::prune_parent_extents(vector
<pair
<uint64_t,uint64_t> >& objectx
,
853 // drop extents completely beyond the overlap
854 while (!objectx
.empty() && objectx
.back().first
>= overlap
)
857 // trim final overlapping extent
858 if (!objectx
.empty() && objectx
.back().first
+ objectx
.back().second
> overlap
)
859 objectx
.back().second
= overlap
- objectx
.back().first
;
862 for (vector
<pair
<uint64_t,uint64_t> >::iterator p
= objectx
.begin();
866 ldout(cct
, 10) << "prune_parent_extents image overlap " << overlap
867 << ", object overlap " << len
868 << " from image extents " << objectx
<< dendl
;
872 void ImageCtx::flush_async_operations() {
874 flush_async_operations(&ctx
);
878 void ImageCtx::flush_async_operations(Context
*on_finish
) {
880 Mutex::Locker
l(async_ops_lock
);
881 if (!async_ops
.empty()) {
882 ldout(cct
, 20) << "flush async operations: " << on_finish
<< " "
883 << "count=" << async_ops
.size() << dendl
;
884 async_ops
.front()->add_flush_context(on_finish
);
888 on_finish
->complete(0);
891 int ImageCtx::flush() {
892 C_SaferCond cond_ctx
;
894 return cond_ctx
.wait();
897 void ImageCtx::flush(Context
*on_safe
) {
898 // ensure no locks are held when flush is complete
899 on_safe
= util::create_async_context_callback(*this, on_safe
);
901 if (object_cacher
!= NULL
) {
902 // flush cache after completing all in-flight AIO ops
903 on_safe
= new C_FlushCache(this, on_safe
);
905 flush_async_operations(on_safe
);
908 void ImageCtx::cancel_async_requests() {
910 cancel_async_requests(&ctx
);
914 void ImageCtx::cancel_async_requests(Context
*on_finish
) {
916 Mutex::Locker
async_ops_locker(async_ops_lock
);
917 if (!async_requests
.empty()) {
918 ldout(cct
, 10) << "canceling async requests: count="
919 << async_requests
.size() << dendl
;
920 for (auto req
: async_requests
) {
921 ldout(cct
, 10) << "canceling async request: " << req
<< dendl
;
924 async_requests_waiters
.push_back(on_finish
);
929 on_finish
->complete(0);
932 void ImageCtx::clear_pending_completions() {
933 Mutex::Locker
l(completed_reqs_lock
);
934 ldout(cct
, 10) << "clear pending AioCompletion: count="
935 << completed_reqs
.size() << dendl
;
936 completed_reqs
.clear();
939 bool ImageCtx::_filter_metadata_confs(const string
&prefix
,
940 map
<string
, bool> &configs
,
941 const map
<string
, bufferlist
> &pairs
,
942 map
<string
, bufferlist
> *res
) {
943 size_t conf_prefix_len
= prefix
.size();
945 for (auto it
: pairs
) {
946 if (it
.first
.compare(0, MIN(conf_prefix_len
, it
.first
.size()), prefix
) > 0)
949 if (it
.first
.size() <= conf_prefix_len
)
952 string key
= it
.first
.substr(conf_prefix_len
, it
.first
.size() - conf_prefix_len
);
953 auto cit
= configs
.find(key
);
954 if (cit
!= configs
.end()) {
956 res
->insert(make_pair(key
, it
.second
));
962 void ImageCtx::apply_metadata(const std::map
<std::string
, bufferlist
> &meta
) {
963 ldout(cct
, 20) << __func__
<< dendl
;
964 std::map
<string
, bool> configs
= boost::assign::map_list_of(
965 "rbd_non_blocking_aio", false)(
967 "rbd_cache_writethrough_until_flush", false)(
968 "rbd_cache_size", false)(
969 "rbd_cache_max_dirty", false)(
970 "rbd_cache_target_dirty", false)(
971 "rbd_cache_max_dirty_age", false)(
972 "rbd_cache_max_dirty_object", false)(
973 "rbd_cache_block_writes_upfront", false)(
974 "rbd_concurrent_management_ops", false)(
975 "rbd_balance_snap_reads", false)(
976 "rbd_localize_snap_reads", false)(
977 "rbd_balance_parent_reads", false)(
978 "rbd_localize_parent_reads", false)(
979 "rbd_readahead_trigger_requests", false)(
980 "rbd_readahead_max_bytes", false)(
981 "rbd_readahead_disable_after_bytes", false)(
982 "rbd_clone_copy_on_read", false)(
983 "rbd_blacklist_on_break_lock", false)(
984 "rbd_blacklist_expire_seconds", false)(
985 "rbd_request_timed_out_seconds", false)(
986 "rbd_journal_order", false)(
987 "rbd_journal_splay_width", false)(
988 "rbd_journal_commit_age", false)(
989 "rbd_journal_object_flush_interval", false)(
990 "rbd_journal_object_flush_bytes", false)(
991 "rbd_journal_object_flush_age", false)(
992 "rbd_journal_pool", false)(
993 "rbd_journal_max_payload_bytes", false)(
994 "rbd_journal_max_concurrent_object_sets", false)(
995 "rbd_mirroring_resync_after_disconnect", false)(
996 "rbd_mirroring_replay_delay", false)(
997 "rbd_skip_partial_discard", false);
999 md_config_t local_config_t
;
1000 std::map
<std::string
, bufferlist
> res
;
1002 _filter_metadata_confs(METADATA_CONF_PREFIX
, configs
, meta
, &res
);
1003 for (auto it
: res
) {
1004 std::string
val(it
.second
.c_str(), it
.second
.length());
1005 int j
= local_config_t
.set_val(it
.first
.c_str(), val
);
1007 lderr(cct
) << __func__
<< " failed to set config " << it
.first
1008 << " with value " << it
.second
.c_str() << ": " << j
1013 #define ASSIGN_OPTION(config) \
1015 string key = "rbd_"; \
1016 key = key + #config; \
1018 config = local_config_t.rbd_##config; \
1020 config = cct->_conf->rbd_##config; \
1023 ASSIGN_OPTION(non_blocking_aio
);
1024 ASSIGN_OPTION(cache
);
1025 ASSIGN_OPTION(cache_writethrough_until_flush
);
1026 ASSIGN_OPTION(cache_size
);
1027 ASSIGN_OPTION(cache_max_dirty
);
1028 ASSIGN_OPTION(cache_target_dirty
);
1029 ASSIGN_OPTION(cache_max_dirty_age
);
1030 ASSIGN_OPTION(cache_max_dirty_object
);
1031 ASSIGN_OPTION(cache_block_writes_upfront
);
1032 ASSIGN_OPTION(concurrent_management_ops
);
1033 ASSIGN_OPTION(balance_snap_reads
);
1034 ASSIGN_OPTION(localize_snap_reads
);
1035 ASSIGN_OPTION(balance_parent_reads
);
1036 ASSIGN_OPTION(localize_parent_reads
);
1037 ASSIGN_OPTION(readahead_trigger_requests
);
1038 ASSIGN_OPTION(readahead_max_bytes
);
1039 ASSIGN_OPTION(readahead_disable_after_bytes
);
1040 ASSIGN_OPTION(clone_copy_on_read
);
1041 ASSIGN_OPTION(blacklist_on_break_lock
);
1042 ASSIGN_OPTION(blacklist_expire_seconds
);
1043 ASSIGN_OPTION(request_timed_out_seconds
);
1044 ASSIGN_OPTION(enable_alloc_hint
);
1045 ASSIGN_OPTION(journal_order
);
1046 ASSIGN_OPTION(journal_splay_width
);
1047 ASSIGN_OPTION(journal_commit_age
);
1048 ASSIGN_OPTION(journal_object_flush_interval
);
1049 ASSIGN_OPTION(journal_object_flush_bytes
);
1050 ASSIGN_OPTION(journal_object_flush_age
);
1051 ASSIGN_OPTION(journal_pool
);
1052 ASSIGN_OPTION(journal_max_payload_bytes
);
1053 ASSIGN_OPTION(journal_max_concurrent_object_sets
);
1054 ASSIGN_OPTION(mirroring_resync_after_disconnect
);
1055 ASSIGN_OPTION(mirroring_replay_delay
);
1056 ASSIGN_OPTION(skip_partial_discard
);
1059 ExclusiveLock
<ImageCtx
> *ImageCtx::create_exclusive_lock() {
1060 return new ExclusiveLock
<ImageCtx
>(*this);
1063 ObjectMap
<ImageCtx
> *ImageCtx::create_object_map(uint64_t snap_id
) {
1064 return new ObjectMap
<ImageCtx
>(*this, snap_id
);
1067 Journal
<ImageCtx
> *ImageCtx::create_journal() {
1068 return new Journal
<ImageCtx
>(*this);
1071 void ImageCtx::set_image_name(const std::string
&image_name
) {
1072 // update the name so rename can be invoked repeatedly
1073 RWLock::RLocker
owner_locker(owner_lock
);
1074 RWLock::WLocker
snap_locker(snap_lock
);
1077 header_oid
= util::old_header_name(image_name
);
1081 void ImageCtx::notify_update() {
1082 state
->handle_update_notification();
1083 ImageWatcher
<>::notify_header_update(md_ctx
, header_oid
);
1086 void ImageCtx::notify_update(Context
*on_finish
) {
1087 state
->handle_update_notification();
1088 image_watcher
->notify_header_update(on_finish
);
1091 exclusive_lock::Policy
*ImageCtx::get_exclusive_lock_policy() const {
1092 assert(owner_lock
.is_locked());
1093 assert(exclusive_lock_policy
!= nullptr);
1094 return exclusive_lock_policy
;
1097 void ImageCtx::set_exclusive_lock_policy(exclusive_lock::Policy
*policy
) {
1098 assert(owner_lock
.is_wlocked());
1099 assert(policy
!= nullptr);
1100 delete exclusive_lock_policy
;
1101 exclusive_lock_policy
= policy
;
1104 journal::Policy
*ImageCtx::get_journal_policy() const {
1105 assert(snap_lock
.is_locked());
1106 assert(journal_policy
!= nullptr);
1107 return journal_policy
;
1110 void ImageCtx::set_journal_policy(journal::Policy
*policy
) {
1111 assert(snap_lock
.is_wlocked());
1112 assert(policy
!= nullptr);
1113 delete journal_policy
;
1114 journal_policy
= policy
;
1117 void ImageCtx::get_thread_pool_instance(CephContext
*cct
,
1118 ThreadPool
**thread_pool
,
1119 ContextWQ
**op_work_queue
) {
1120 ThreadPoolSingleton
*thread_pool_singleton
;
1121 cct
->lookup_or_create_singleton_object
<ThreadPoolSingleton
>(
1122 thread_pool_singleton
, "librbd::thread_pool");
1123 *thread_pool
= thread_pool_singleton
;
1124 *op_work_queue
= thread_pool_singleton
->op_work_queue
;
1127 void ImageCtx::get_timer_instance(CephContext
*cct
, SafeTimer
**timer
,
1128 Mutex
**timer_lock
) {
1129 SafeTimerSingleton
*safe_timer_singleton
;
1130 cct
->lookup_or_create_singleton_object
<SafeTimerSingleton
>(
1131 safe_timer_singleton
, "librbd::journal::safe_timer");
1132 *timer
= safe_timer_singleton
;
1133 *timer_lock
= &safe_timer_singleton
->lock
;