1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/neorados/RADOS.hpp"
5 #include "test/librbd/mock/MockImageCtx.h"
6 #include "test/librbd/mock/MockSafeTimer.h"
7 #include "test/librbd/mock/crypto/MockEncryptionFormat.h"
8 #include "librbd/io/AsyncOperation.h"
10 static MockSafeTimer
*s_timer
;
11 static ceph::mutex
*s_timer_lock
;
15 MockImageCtx
* MockImageCtx::s_instance
= nullptr;
17 MockImageCtx::MockImageCtx(librbd::ImageCtx
&image_ctx
)
18 : image_ctx(&image_ctx
),
20 perfcounter(image_ctx
.perfcounter
),
21 snap_namespace(image_ctx
.snap_namespace
),
22 snap_name(image_ctx
.snap_name
),
23 snap_id(image_ctx
.snap_id
),
24 snap_exists(image_ctx
.snap_exists
),
25 snapc(image_ctx
.snapc
),
26 snaps(image_ctx
.snaps
),
27 snap_info(image_ctx
.snap_info
),
28 snap_ids(image_ctx
.snap_ids
),
29 old_format(image_ctx
.old_format
),
30 read_only(image_ctx
.read_only
),
31 read_only_flags(image_ctx
.read_only_flags
),
32 read_only_mask(image_ctx
.read_only_mask
),
33 clone_copy_on_read(image_ctx
.clone_copy_on_read
),
34 lockers(image_ctx
.lockers
),
35 exclusive_locked(image_ctx
.exclusive_locked
),
36 lock_tag(image_ctx
.lock_tag
),
37 asio_engine(image_ctx
.asio_engine
),
38 rados_api(image_ctx
.rados_api
),
39 owner_lock(image_ctx
.owner_lock
),
40 image_lock(image_ctx
.image_lock
),
41 timestamp_lock(image_ctx
.timestamp_lock
),
42 async_ops_lock(image_ctx
.async_ops_lock
),
43 copyup_list_lock(image_ctx
.copyup_list_lock
),
44 order(image_ctx
.order
),
46 features(image_ctx
.features
),
47 flags(image_ctx
.flags
),
48 op_features(image_ctx
.op_features
),
49 operations_disabled(image_ctx
.operations_disabled
),
50 stripe_unit(image_ctx
.stripe_unit
),
51 stripe_count(image_ctx
.stripe_count
),
52 object_prefix(image_ctx
.object_prefix
),
53 header_oid(image_ctx
.header_oid
),
56 parent_md(image_ctx
.parent_md
),
57 format_string(image_ctx
.format_string
),
58 group_spec(image_ctx
.group_spec
),
59 layout(image_ctx
.layout
),
60 io_image_dispatcher(new io::MockImageDispatcher()),
61 io_object_dispatcher(new io::MockObjectDispatcher()),
62 op_work_queue(new MockContextWQ()),
63 plugin_registry(new MockPluginRegistry()),
64 readahead_max_bytes(image_ctx
.readahead_max_bytes
),
65 event_socket(image_ctx
.event_socket
),
66 parent(NULL
), operations(new MockOperations()),
67 state(new MockImageState()),
68 image_watcher(NULL
), object_map(NULL
),
69 exclusive_lock(NULL
), journal(NULL
),
70 trace_endpoint(image_ctx
.trace_endpoint
),
71 sparse_read_threshold_bytes(image_ctx
.sparse_read_threshold_bytes
),
72 discard_granularity_bytes(image_ctx
.discard_granularity_bytes
),
73 mirroring_replay_delay(image_ctx
.mirroring_replay_delay
),
74 non_blocking_aio(image_ctx
.non_blocking_aio
),
75 blkin_trace_all(image_ctx
.blkin_trace_all
),
76 enable_alloc_hint(image_ctx
.enable_alloc_hint
),
77 alloc_hint_flags(image_ctx
.alloc_hint_flags
),
78 read_flags(image_ctx
.read_flags
),
79 ignore_migrating(image_ctx
.ignore_migrating
),
80 enable_sparse_copyup(image_ctx
.enable_sparse_copyup
),
81 mtime_update_interval(image_ctx
.mtime_update_interval
),
82 atime_update_interval(image_ctx
.atime_update_interval
),
83 cache(image_ctx
.cache
),
84 config(image_ctx
.config
)
86 md_ctx
.dup(image_ctx
.md_ctx
);
87 data_ctx
.dup(image_ctx
.data_ctx
);
89 if (image_ctx
.image_watcher
!= NULL
) {
90 image_watcher
= new MockImageWatcher();
94 MockImageCtx::~MockImageCtx() {
95 wait_for_async_requests();
97 image_ctx
->md_ctx
.aio_flush();
98 image_ctx
->data_ctx
.aio_flush();
99 image_ctx
->op_work_queue
->drain();
102 delete image_watcher
;
103 delete op_work_queue
;
104 delete plugin_registry
;
105 delete io_image_dispatcher
;
106 delete io_object_dispatcher
;
109 void MockImageCtx::set_timer_instance(MockSafeTimer
*timer
,
110 ceph::mutex
*timer_lock
) {
112 s_timer_lock
= timer_lock
;
115 void MockImageCtx::get_timer_instance(CephContext
*cct
, MockSafeTimer
**timer
,
116 ceph::mutex
**timer_lock
) {
118 *timer_lock
= s_timer_lock
;
121 void MockImageCtx::wait_for_async_ops() {
122 io::AsyncOperation async_op
;
123 async_op
.start_op(*image_ctx
);
126 async_op
.flush(&ctx
);
129 async_op
.finish_op();
132 IOContext
MockImageCtx::get_data_io_context() {
133 auto ctx
= std::make_shared
<neorados::IOContext
>(
134 data_ctx
.get_id(), data_ctx
.get_namespace());
135 if (snap_id
!= CEPH_NOSNAP
) {
136 ctx
->read_snap(snap_id
);
138 if (!snapc
.snaps
.empty()) {
139 ctx
->write_snap_context(
140 {{snapc
.seq
, {snapc
.snaps
.begin(), snapc
.snaps
.end()}}});
145 IOContext
MockImageCtx::duplicate_data_io_context() {
146 return std::make_shared
<neorados::IOContext
>(*get_data_io_context());
149 } // namespace librbd