]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | #include <errno.h> | |
4 | #include <boost/assign/list_of.hpp> | |
5 | #include <stddef.h> | |
6 | ||
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" | |
13 | ||
7c673cae FG |
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" | |
b32b8144 | 25 | #include "librbd/Types.h" |
7c673cae FG |
26 | #include "librbd/Utils.h" |
27 | #include "librbd/LibrbdWriteback.h" | |
28 | #include "librbd/exclusive_lock/AutomaticPolicy.h" | |
29 | #include "librbd/exclusive_lock/StandardPolicy.h" | |
30 | #include "librbd/io/AioCompletion.h" | |
31f18b77 | 31 | #include "librbd/io/AsyncOperation.h" |
7c673cae | 32 | #include "librbd/io/ImageRequestWQ.h" |
11fdf7f2 | 33 | #include "librbd/io/ObjectDispatcher.h" |
7c673cae FG |
34 | #include "librbd/journal/StandardPolicy.h" |
35 | ||
36 | #include "osdc/Striper.h" | |
37 | #include <boost/bind.hpp> | |
11fdf7f2 | 38 | #include <boost/algorithm/string/predicate.hpp> |
7c673cae FG |
39 | |
40 | #define dout_subsys ceph_subsys_rbd | |
41 | #undef dout_prefix | |
42 | #define dout_prefix *_dout << "librbd::ImageCtx: " | |
43 | ||
44 | using std::map; | |
45 | using std::pair; | |
46 | using std::set; | |
47 | using std::string; | |
48 | using std::vector; | |
49 | ||
50 | using ceph::bufferlist; | |
51 | using librados::snap_t; | |
52 | using librados::IoCtx; | |
53 | ||
54 | namespace librbd { | |
55 | ||
56 | namespace { | |
57 | ||
58 | class ThreadPoolSingleton : public ThreadPool { | |
59 | public: | |
60 | ContextWQ *op_work_queue; | |
61 | ||
62 | explicit ThreadPoolSingleton(CephContext *cct) | |
63 | : ThreadPool(cct, "librbd::thread_pool", "tp_librbd", 1, | |
64 | "rbd_op_threads"), | |
65 | op_work_queue(new ContextWQ("librbd::op_work_queue", | |
11fdf7f2 | 66 | cct->_conf.get_val<uint64_t>("rbd_op_thread_timeout"), |
7c673cae FG |
67 | this)) { |
68 | start(); | |
69 | } | |
70 | ~ThreadPoolSingleton() override { | |
71 | op_work_queue->drain(); | |
72 | delete op_work_queue; | |
73 | ||
74 | stop(); | |
75 | } | |
76 | }; | |
77 | ||
78 | class SafeTimerSingleton : public SafeTimer { | |
79 | public: | |
80 | Mutex lock; | |
81 | ||
82 | explicit SafeTimerSingleton(CephContext *cct) | |
83 | : SafeTimer(cct, lock, true), | |
84 | lock("librbd::Journal::SafeTimerSingleton::lock") { | |
85 | init(); | |
86 | } | |
87 | ~SafeTimerSingleton() { | |
88 | Mutex::Locker locker(lock); | |
89 | shutdown(); | |
90 | } | |
91 | }; | |
92 | ||
7c673cae FG |
93 | } // anonymous namespace |
94 | ||
95 | const string ImageCtx::METADATA_CONF_PREFIX = "conf_"; | |
96 | ||
97 | ImageCtx::ImageCtx(const string &image_name, const string &image_id, | |
98 | const char *snap, IoCtx& p, bool ro) | |
99 | : cct((CephContext*)p.cct()), | |
11fdf7f2 | 100 | config(cct->_conf), |
7c673cae FG |
101 | perfcounter(NULL), |
102 | snap_id(CEPH_NOSNAP), | |
103 | snap_exists(true), | |
104 | read_only(ro), | |
7c673cae FG |
105 | exclusive_locked(false), |
106 | name(image_name), | |
107 | image_watcher(NULL), | |
108 | journal(NULL), | |
109 | owner_lock(util::unique_lock_name("librbd::ImageCtx::owner_lock", this)), | |
110 | md_lock(util::unique_lock_name("librbd::ImageCtx::md_lock", this)), | |
7c673cae | 111 | snap_lock(util::unique_lock_name("librbd::ImageCtx::snap_lock", this)), |
11fdf7f2 | 112 | timestamp_lock(util::unique_lock_name("librbd::ImageCtx::timestamp_lock", this)), |
7c673cae FG |
113 | parent_lock(util::unique_lock_name("librbd::ImageCtx::parent_lock", this)), |
114 | object_map_lock(util::unique_lock_name("librbd::ImageCtx::object_map_lock", this)), | |
115 | async_ops_lock(util::unique_lock_name("librbd::ImageCtx::async_ops_lock", this)), | |
116 | copyup_list_lock(util::unique_lock_name("librbd::ImageCtx::copyup_list_lock", this)), | |
117 | completed_reqs_lock(util::unique_lock_name("librbd::ImageCtx::completed_reqs_lock", this)), | |
118 | extra_read_flags(0), | |
11fdf7f2 | 119 | old_format(false), |
7c673cae FG |
120 | order(0), size(0), features(0), |
121 | format_string(NULL), | |
122 | id(image_id), parent(NULL), | |
123 | stripe_unit(0), stripe_count(0), flags(0), | |
7c673cae FG |
124 | readahead(), |
125 | total_bytes_read(0), | |
126 | state(new ImageState<>(this)), | |
127 | operations(new Operations<>(*this)), | |
128 | exclusive_lock(nullptr), object_map(nullptr), | |
129 | io_work_queue(nullptr), op_work_queue(nullptr), | |
31f18b77 FG |
130 | asok_hook(nullptr), |
131 | trace_endpoint("librbd") | |
7c673cae FG |
132 | { |
133 | md_ctx.dup(p); | |
134 | data_ctx.dup(p); | |
135 | if (snap) | |
136 | snap_name = snap; | |
137 | ||
138 | memset(&header, 0, sizeof(header)); | |
139 | ||
140 | ThreadPool *thread_pool; | |
141 | get_thread_pool_instance(cct, &thread_pool, &op_work_queue); | |
224ce89b | 142 | io_work_queue = new io::ImageRequestWQ<>( |
181888fb | 143 | this, "librbd::io_work_queue", |
11fdf7f2 | 144 | cct->_conf.get_val<uint64_t>("rbd_op_thread_timeout"), |
7c673cae | 145 | thread_pool); |
11fdf7f2 | 146 | io_object_dispatcher = new io::ObjectDispatcher<>(this); |
7c673cae | 147 | |
11fdf7f2 | 148 | if (cct->_conf.get_val<bool>("rbd_auto_exclusive_lock_until_manual_request")) { |
7c673cae FG |
149 | exclusive_lock_policy = new exclusive_lock::AutomaticPolicy(this); |
150 | } else { | |
151 | exclusive_lock_policy = new exclusive_lock::StandardPolicy(this); | |
152 | } | |
153 | journal_policy = new journal::StandardPolicy<ImageCtx>(this); | |
154 | } | |
155 | ||
11fdf7f2 TL |
156 | ImageCtx::ImageCtx(const string &image_name, const string &image_id, |
157 | uint64_t snap_id, IoCtx& p, bool ro) | |
158 | : ImageCtx(image_name, image_id, "", p, ro) { | |
159 | open_snap_id = snap_id; | |
160 | } | |
161 | ||
7c673cae | 162 | ImageCtx::~ImageCtx() { |
11fdf7f2 TL |
163 | ceph_assert(image_watcher == NULL); |
164 | ceph_assert(exclusive_lock == NULL); | |
165 | ceph_assert(object_map == NULL); | |
166 | ceph_assert(journal == NULL); | |
167 | ceph_assert(asok_hook == NULL); | |
7c673cae FG |
168 | |
169 | if (perfcounter) { | |
170 | perf_stop(); | |
171 | } | |
7c673cae FG |
172 | delete[] format_string; |
173 | ||
174 | md_ctx.aio_flush(); | |
eafe8130 TL |
175 | if (data_ctx.is_valid()) { |
176 | data_ctx.aio_flush(); | |
177 | } | |
7c673cae FG |
178 | io_work_queue->drain(); |
179 | ||
11fdf7f2 TL |
180 | delete io_object_dispatcher; |
181 | ||
7c673cae FG |
182 | delete journal_policy; |
183 | delete exclusive_lock_policy; | |
184 | delete io_work_queue; | |
185 | delete operations; | |
186 | delete state; | |
187 | } | |
188 | ||
189 | void ImageCtx::init() { | |
11fdf7f2 TL |
190 | ceph_assert(!header_oid.empty()); |
191 | ceph_assert(old_format || !id.empty()); | |
7c673cae FG |
192 | |
193 | asok_hook = new LibrbdAdminSocketHook(this); | |
194 | ||
195 | string pname = string("librbd-") + id + string("-") + | |
eafe8130 | 196 | md_ctx.get_pool_name() + string("-") + name; |
7c673cae FG |
197 | if (!snap_name.empty()) { |
198 | pname += "-"; | |
199 | pname += snap_name; | |
200 | } | |
201 | ||
31f18b77 | 202 | trace_endpoint.copy_name(pname); |
7c673cae FG |
203 | perf_start(pname); |
204 | ||
11fdf7f2 TL |
205 | ceph_assert(image_watcher == NULL); |
206 | image_watcher = new ImageWatcher<>(*this); | |
7c673cae FG |
207 | } |
208 | ||
209 | void ImageCtx::shutdown() { | |
210 | delete image_watcher; | |
211 | image_watcher = nullptr; | |
212 | ||
213 | delete asok_hook; | |
214 | asok_hook = nullptr; | |
215 | } | |
216 | ||
eafe8130 | 217 | void ImageCtx::init_layout(int64_t pool_id) |
7c673cae FG |
218 | { |
219 | if (stripe_unit == 0 || stripe_count == 0) { | |
220 | stripe_unit = 1ull << order; | |
221 | stripe_count = 1; | |
222 | } | |
223 | ||
224 | vector<uint64_t> alignments; | |
225 | alignments.push_back(stripe_count << order); // object set (in file striping terminology) | |
226 | alignments.push_back(stripe_unit * stripe_count); // stripe | |
227 | alignments.push_back(stripe_unit); // stripe unit | |
228 | readahead.set_alignments(alignments); | |
229 | ||
230 | layout = file_layout_t(); | |
231 | layout.stripe_unit = stripe_unit; | |
232 | layout.stripe_count = stripe_count; | |
233 | layout.object_size = 1ull << order; | |
eafe8130 | 234 | layout.pool_id = pool_id; // FIXME: pool id overflow? |
7c673cae FG |
235 | |
236 | delete[] format_string; | |
237 | size_t len = object_prefix.length() + 16; | |
238 | format_string = new char[len]; | |
239 | if (old_format) { | |
240 | snprintf(format_string, len, "%s.%%012llx", object_prefix.c_str()); | |
241 | } else { | |
242 | snprintf(format_string, len, "%s.%%016llx", object_prefix.c_str()); | |
243 | } | |
244 | ||
245 | ldout(cct, 10) << "init_layout stripe_unit " << stripe_unit | |
246 | << " stripe_count " << stripe_count | |
247 | << " object_size " << layout.object_size | |
248 | << " prefix " << object_prefix | |
249 | << " format " << format_string | |
250 | << dendl; | |
251 | } | |
252 | ||
253 | void ImageCtx::perf_start(string name) { | |
b32b8144 FG |
254 | auto perf_prio = PerfCountersBuilder::PRIO_DEBUGONLY; |
255 | if (child == nullptr) { | |
256 | // ensure top-level IO stats are exported for librbd daemons | |
257 | perf_prio = PerfCountersBuilder::PRIO_USEFUL; | |
258 | } | |
259 | ||
7c673cae FG |
260 | PerfCountersBuilder plb(cct, name, l_librbd_first, l_librbd_last); |
261 | ||
b32b8144 FG |
262 | plb.add_u64_counter(l_librbd_rd, "rd", "Reads", "r", perf_prio); |
263 | plb.add_u64_counter(l_librbd_rd_bytes, "rd_bytes", "Data size in reads", | |
11fdf7f2 | 264 | "rb", perf_prio, unit_t(UNIT_BYTES)); |
b32b8144 FG |
265 | plb.add_time_avg(l_librbd_rd_latency, "rd_latency", "Latency of reads", |
266 | "rl", perf_prio); | |
267 | plb.add_u64_counter(l_librbd_wr, "wr", "Writes", "w", perf_prio); | |
268 | plb.add_u64_counter(l_librbd_wr_bytes, "wr_bytes", "Written data", | |
11fdf7f2 | 269 | "wb", perf_prio, unit_t(UNIT_BYTES)); |
b32b8144 FG |
270 | plb.add_time_avg(l_librbd_wr_latency, "wr_latency", "Write latency", |
271 | "wl", perf_prio); | |
7c673cae | 272 | plb.add_u64_counter(l_librbd_discard, "discard", "Discards"); |
11fdf7f2 | 273 | plb.add_u64_counter(l_librbd_discard_bytes, "discard_bytes", "Discarded data", NULL, 0, unit_t(UNIT_BYTES)); |
7c673cae FG |
274 | plb.add_time_avg(l_librbd_discard_latency, "discard_latency", "Discard latency"); |
275 | plb.add_u64_counter(l_librbd_flush, "flush", "Flushes"); | |
11fdf7f2 | 276 | plb.add_time_avg(l_librbd_flush_latency, "flush_latency", "Latency of flushes"); |
7c673cae | 277 | plb.add_u64_counter(l_librbd_ws, "ws", "WriteSames"); |
11fdf7f2 | 278 | plb.add_u64_counter(l_librbd_ws_bytes, "ws_bytes", "WriteSame data", NULL, 0, unit_t(UNIT_BYTES)); |
7c673cae | 279 | plb.add_time_avg(l_librbd_ws_latency, "ws_latency", "WriteSame latency"); |
c07f9fc5 | 280 | plb.add_u64_counter(l_librbd_cmp, "cmp", "CompareAndWrites"); |
11fdf7f2 | 281 | plb.add_u64_counter(l_librbd_cmp_bytes, "cmp_bytes", "Data size in cmps", NULL, 0, unit_t(UNIT_BYTES)); |
c07f9fc5 | 282 | plb.add_time_avg(l_librbd_cmp_latency, "cmp_latency", "Latency of cmps"); |
7c673cae FG |
283 | plb.add_u64_counter(l_librbd_snap_create, "snap_create", "Snap creations"); |
284 | plb.add_u64_counter(l_librbd_snap_remove, "snap_remove", "Snap removals"); | |
285 | plb.add_u64_counter(l_librbd_snap_rollback, "snap_rollback", "Snap rollbacks"); | |
286 | plb.add_u64_counter(l_librbd_snap_rename, "snap_rename", "Snap rename"); | |
287 | plb.add_u64_counter(l_librbd_notify, "notify", "Updated header notifications"); | |
288 | plb.add_u64_counter(l_librbd_resize, "resize", "Resizes"); | |
289 | plb.add_u64_counter(l_librbd_readahead, "readahead", "Read ahead"); | |
11fdf7f2 | 290 | plb.add_u64_counter(l_librbd_readahead_bytes, "readahead_bytes", "Data size in read ahead", NULL, 0, unit_t(UNIT_BYTES)); |
7c673cae FG |
291 | plb.add_u64_counter(l_librbd_invalidate_cache, "invalidate_cache", "Cache invalidates"); |
292 | ||
b32b8144 FG |
293 | plb.add_time(l_librbd_opened_time, "opened_time", "Opened time", |
294 | "ots", perf_prio); | |
295 | plb.add_time(l_librbd_lock_acquired_time, "lock_acquired_time", | |
296 | "Lock acquired time", "lats", perf_prio); | |
297 | ||
7c673cae FG |
298 | perfcounter = plb.create_perf_counters(); |
299 | cct->get_perfcounters_collection()->add(perfcounter); | |
b32b8144 FG |
300 | |
301 | perfcounter->tset(l_librbd_opened_time, ceph_clock_now()); | |
7c673cae FG |
302 | } |
303 | ||
304 | void ImageCtx::perf_stop() { | |
11fdf7f2 | 305 | ceph_assert(perfcounter); |
7c673cae FG |
306 | cct->get_perfcounters_collection()->remove(perfcounter); |
307 | delete perfcounter; | |
308 | } | |
309 | ||
310 | void ImageCtx::set_read_flag(unsigned flag) { | |
311 | extra_read_flags |= flag; | |
312 | } | |
313 | ||
314 | int ImageCtx::get_read_flags(snap_t snap_id) { | |
315 | int flags = librados::OPERATION_NOFLAG | extra_read_flags; | |
316 | if (snap_id == LIBRADOS_SNAP_HEAD) | |
317 | return flags; | |
318 | ||
11fdf7f2 | 319 | if (config.get_val<bool>("rbd_balance_snap_reads")) |
7c673cae | 320 | flags |= librados::OPERATION_BALANCE_READS; |
11fdf7f2 | 321 | else if (config.get_val<bool>("rbd_localize_snap_reads")) |
7c673cae FG |
322 | flags |= librados::OPERATION_LOCALIZE_READS; |
323 | return flags; | |
324 | } | |
325 | ||
11fdf7f2 TL |
326 | int ImageCtx::snap_set(uint64_t in_snap_id) { |
327 | ceph_assert(snap_lock.is_wlocked()); | |
328 | auto it = snap_info.find(in_snap_id); | |
329 | if (in_snap_id != CEPH_NOSNAP && it != snap_info.end()) { | |
7c673cae | 330 | snap_id = in_snap_id; |
11fdf7f2 TL |
331 | snap_namespace = it->second.snap_namespace; |
332 | snap_name = it->second.name; | |
7c673cae | 333 | snap_exists = true; |
eafe8130 TL |
334 | if (data_ctx.is_valid()) { |
335 | data_ctx.snap_set_read(snap_id); | |
336 | } | |
7c673cae FG |
337 | return 0; |
338 | } | |
339 | return -ENOENT; | |
340 | } | |
341 | ||
342 | void ImageCtx::snap_unset() | |
343 | { | |
11fdf7f2 | 344 | ceph_assert(snap_lock.is_wlocked()); |
7c673cae FG |
345 | snap_id = CEPH_NOSNAP; |
346 | snap_namespace = {}; | |
347 | snap_name = ""; | |
348 | snap_exists = true; | |
eafe8130 TL |
349 | if (data_ctx.is_valid()) { |
350 | data_ctx.snap_set_read(snap_id); | |
351 | } | |
7c673cae FG |
352 | } |
353 | ||
11fdf7f2 TL |
354 | snap_t ImageCtx::get_snap_id(const cls::rbd::SnapshotNamespace& in_snap_namespace, |
355 | const string& in_snap_name) const | |
7c673cae | 356 | { |
11fdf7f2 | 357 | ceph_assert(snap_lock.is_locked()); |
7c673cae | 358 | auto it = snap_ids.find({in_snap_namespace, in_snap_name}); |
11fdf7f2 | 359 | if (it != snap_ids.end()) { |
7c673cae | 360 | return it->second; |
11fdf7f2 | 361 | } |
7c673cae FG |
362 | return CEPH_NOSNAP; |
363 | } | |
364 | ||
365 | const SnapInfo* ImageCtx::get_snap_info(snap_t in_snap_id) const | |
366 | { | |
11fdf7f2 | 367 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
368 | map<snap_t, SnapInfo>::const_iterator it = |
369 | snap_info.find(in_snap_id); | |
370 | if (it != snap_info.end()) | |
371 | return &it->second; | |
11fdf7f2 | 372 | return nullptr; |
7c673cae FG |
373 | } |
374 | ||
375 | int ImageCtx::get_snap_name(snap_t in_snap_id, | |
376 | string *out_snap_name) const | |
377 | { | |
11fdf7f2 | 378 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
379 | const SnapInfo *info = get_snap_info(in_snap_id); |
380 | if (info) { | |
381 | *out_snap_name = info->name; | |
382 | return 0; | |
383 | } | |
384 | return -ENOENT; | |
385 | } | |
386 | ||
387 | int ImageCtx::get_snap_namespace(snap_t in_snap_id, | |
388 | cls::rbd::SnapshotNamespace *out_snap_namespace) const | |
389 | { | |
11fdf7f2 | 390 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
391 | const SnapInfo *info = get_snap_info(in_snap_id); |
392 | if (info) { | |
393 | *out_snap_namespace = info->snap_namespace; | |
394 | return 0; | |
395 | } | |
396 | return -ENOENT; | |
397 | } | |
398 | ||
399 | int ImageCtx::get_parent_spec(snap_t in_snap_id, | |
11fdf7f2 | 400 | cls::rbd::ParentImageSpec *out_pspec) const |
7c673cae FG |
401 | { |
402 | const SnapInfo *info = get_snap_info(in_snap_id); | |
403 | if (info) { | |
404 | *out_pspec = info->parent.spec; | |
405 | return 0; | |
406 | } | |
407 | return -ENOENT; | |
408 | } | |
409 | ||
410 | uint64_t ImageCtx::get_current_size() const | |
411 | { | |
11fdf7f2 | 412 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
413 | return size; |
414 | } | |
415 | ||
416 | uint64_t ImageCtx::get_object_size() const | |
417 | { | |
418 | return 1ull << order; | |
419 | } | |
420 | ||
421 | string ImageCtx::get_object_name(uint64_t num) const { | |
422 | char buf[object_prefix.length() + 32]; | |
423 | snprintf(buf, sizeof(buf), format_string, num); | |
424 | return string(buf); | |
425 | } | |
426 | ||
427 | uint64_t ImageCtx::get_stripe_unit() const | |
428 | { | |
429 | return stripe_unit; | |
430 | } | |
431 | ||
432 | uint64_t ImageCtx::get_stripe_count() const | |
433 | { | |
434 | return stripe_count; | |
435 | } | |
436 | ||
437 | uint64_t ImageCtx::get_stripe_period() const | |
438 | { | |
439 | return stripe_count * (1ull << order); | |
440 | } | |
441 | ||
31f18b77 FG |
442 | utime_t ImageCtx::get_create_timestamp() const |
443 | { | |
444 | return create_timestamp; | |
445 | } | |
446 | ||
11fdf7f2 TL |
447 | utime_t ImageCtx::get_access_timestamp() const |
448 | { | |
449 | return access_timestamp; | |
450 | } | |
451 | ||
452 | utime_t ImageCtx::get_modify_timestamp() const | |
453 | { | |
454 | return modify_timestamp; | |
455 | } | |
456 | ||
457 | void ImageCtx::set_access_timestamp(utime_t at) | |
458 | { | |
459 | ceph_assert(timestamp_lock.is_wlocked()); | |
460 | access_timestamp = at; | |
461 | } | |
462 | ||
463 | void ImageCtx::set_modify_timestamp(utime_t mt) | |
464 | { | |
465 | ceph_assert(timestamp_lock.is_locked()); | |
466 | modify_timestamp = mt; | |
467 | } | |
468 | ||
7c673cae FG |
469 | int ImageCtx::is_snap_protected(snap_t in_snap_id, |
470 | bool *is_protected) const | |
471 | { | |
11fdf7f2 | 472 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
473 | const SnapInfo *info = get_snap_info(in_snap_id); |
474 | if (info) { | |
475 | *is_protected = | |
476 | (info->protection_status == RBD_PROTECTION_STATUS_PROTECTED); | |
477 | return 0; | |
478 | } | |
479 | return -ENOENT; | |
480 | } | |
481 | ||
482 | int ImageCtx::is_snap_unprotected(snap_t in_snap_id, | |
483 | bool *is_unprotected) const | |
484 | { | |
11fdf7f2 | 485 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
486 | const SnapInfo *info = get_snap_info(in_snap_id); |
487 | if (info) { | |
488 | *is_unprotected = | |
489 | (info->protection_status == RBD_PROTECTION_STATUS_UNPROTECTED); | |
490 | return 0; | |
491 | } | |
492 | return -ENOENT; | |
493 | } | |
494 | ||
495 | void ImageCtx::add_snap(cls::rbd::SnapshotNamespace in_snap_namespace, | |
496 | string in_snap_name, | |
497 | snap_t id, uint64_t in_size, | |
11fdf7f2 TL |
498 | const ParentImageInfo &parent, |
499 | uint8_t protection_status, uint64_t flags, | |
500 | utime_t timestamp) | |
7c673cae | 501 | { |
11fdf7f2 | 502 | ceph_assert(snap_lock.is_wlocked()); |
7c673cae FG |
503 | snaps.push_back(id); |
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}); | |
508 | } | |
509 | ||
510 | void ImageCtx::rm_snap(cls::rbd::SnapshotNamespace in_snap_namespace, | |
511 | string in_snap_name, | |
512 | snap_t id) | |
513 | { | |
11fdf7f2 | 514 | ceph_assert(snap_lock.is_wlocked()); |
7c673cae FG |
515 | snaps.erase(std::remove(snaps.begin(), snaps.end(), id), snaps.end()); |
516 | snap_info.erase(id); | |
517 | snap_ids.erase({in_snap_namespace, in_snap_name}); | |
518 | } | |
519 | ||
520 | uint64_t ImageCtx::get_image_size(snap_t in_snap_id) const | |
521 | { | |
11fdf7f2 | 522 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
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(); | |
527 | } | |
528 | return size; | |
529 | } | |
530 | ||
531 | const SnapInfo *info = get_snap_info(in_snap_id); | |
532 | if (info) { | |
533 | return info->size; | |
534 | } | |
535 | return 0; | |
536 | } | |
537 | ||
538 | uint64_t ImageCtx::get_object_count(snap_t in_snap_id) const { | |
11fdf7f2 | 539 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
540 | uint64_t image_size = get_image_size(in_snap_id); |
541 | return Striper::get_num_objects(layout, image_size); | |
542 | } | |
543 | ||
544 | bool ImageCtx::test_features(uint64_t features) const | |
545 | { | |
546 | RWLock::RLocker l(snap_lock); | |
547 | return test_features(features, snap_lock); | |
548 | } | |
549 | ||
550 | bool ImageCtx::test_features(uint64_t in_features, | |
551 | const RWLock &in_snap_lock) const | |
552 | { | |
11fdf7f2 | 553 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
554 | return ((features & in_features) == in_features); |
555 | } | |
556 | ||
11fdf7f2 TL |
557 | bool ImageCtx::test_op_features(uint64_t in_op_features) const |
558 | { | |
559 | RWLock::RLocker snap_locker(snap_lock); | |
560 | return test_op_features(in_op_features, snap_lock); | |
561 | } | |
562 | ||
563 | bool ImageCtx::test_op_features(uint64_t in_op_features, | |
564 | const RWLock &in_snap_lock) const | |
565 | { | |
566 | ceph_assert(snap_lock.is_locked()); | |
567 | return ((op_features & in_op_features) == in_op_features); | |
568 | } | |
569 | ||
7c673cae FG |
570 | int ImageCtx::get_flags(librados::snap_t _snap_id, uint64_t *_flags) const |
571 | { | |
11fdf7f2 | 572 | ceph_assert(snap_lock.is_locked()); |
7c673cae FG |
573 | if (_snap_id == CEPH_NOSNAP) { |
574 | *_flags = flags; | |
575 | return 0; | |
576 | } | |
577 | const SnapInfo *info = get_snap_info(_snap_id); | |
578 | if (info) { | |
579 | *_flags = info->flags; | |
580 | return 0; | |
581 | } | |
582 | return -ENOENT; | |
583 | } | |
584 | ||
91327a77 AA |
585 | int ImageCtx::test_flags(librados::snap_t in_snap_id, |
586 | uint64_t flags, bool *flags_set) const | |
7c673cae FG |
587 | { |
588 | RWLock::RLocker l(snap_lock); | |
91327a77 | 589 | return test_flags(in_snap_id, flags, snap_lock, flags_set); |
7c673cae FG |
590 | } |
591 | ||
91327a77 AA |
592 | int ImageCtx::test_flags(librados::snap_t in_snap_id, |
593 | uint64_t flags, const RWLock &in_snap_lock, | |
31f18b77 | 594 | bool *flags_set) const |
7c673cae | 595 | { |
11fdf7f2 | 596 | ceph_assert(snap_lock.is_locked()); |
7c673cae | 597 | uint64_t snap_flags; |
91327a77 | 598 | int r = get_flags(in_snap_id, &snap_flags); |
31f18b77 FG |
599 | if (r < 0) { |
600 | return r; | |
601 | } | |
602 | *flags_set = ((snap_flags & flags) == flags); | |
603 | return 0; | |
7c673cae FG |
604 | } |
605 | ||
606 | int ImageCtx::update_flags(snap_t in_snap_id, uint64_t flag, bool enabled) | |
607 | { | |
11fdf7f2 | 608 | ceph_assert(snap_lock.is_wlocked()); |
7c673cae FG |
609 | uint64_t *_flags; |
610 | if (in_snap_id == CEPH_NOSNAP) { | |
611 | _flags = &flags; | |
612 | } else { | |
613 | map<snap_t, SnapInfo>::iterator it = snap_info.find(in_snap_id); | |
614 | if (it == snap_info.end()) { | |
615 | return -ENOENT; | |
616 | } | |
617 | _flags = &it->second.flags; | |
618 | } | |
619 | ||
620 | if (enabled) { | |
621 | (*_flags) |= flag; | |
622 | } else { | |
623 | (*_flags) &= ~flag; | |
624 | } | |
625 | return 0; | |
626 | } | |
627 | ||
11fdf7f2 | 628 | const ParentImageInfo* ImageCtx::get_parent_info(snap_t in_snap_id) const |
7c673cae | 629 | { |
11fdf7f2 TL |
630 | ceph_assert(snap_lock.is_locked()); |
631 | ceph_assert(parent_lock.is_locked()); | |
7c673cae FG |
632 | if (in_snap_id == CEPH_NOSNAP) |
633 | return &parent_md; | |
634 | const SnapInfo *info = get_snap_info(in_snap_id); | |
635 | if (info) | |
636 | return &info->parent; | |
637 | return NULL; | |
638 | } | |
639 | ||
640 | int64_t ImageCtx::get_parent_pool_id(snap_t in_snap_id) const | |
641 | { | |
11fdf7f2 | 642 | const auto info = get_parent_info(in_snap_id); |
7c673cae FG |
643 | if (info) |
644 | return info->spec.pool_id; | |
645 | return -1; | |
646 | } | |
647 | ||
648 | string ImageCtx::get_parent_image_id(snap_t in_snap_id) const | |
649 | { | |
11fdf7f2 | 650 | const auto info = get_parent_info(in_snap_id); |
7c673cae FG |
651 | if (info) |
652 | return info->spec.image_id; | |
653 | return ""; | |
654 | } | |
655 | ||
656 | uint64_t ImageCtx::get_parent_snap_id(snap_t in_snap_id) const | |
657 | { | |
11fdf7f2 | 658 | const auto info = get_parent_info(in_snap_id); |
7c673cae FG |
659 | if (info) |
660 | return info->spec.snap_id; | |
661 | return CEPH_NOSNAP; | |
662 | } | |
663 | ||
664 | int ImageCtx::get_parent_overlap(snap_t in_snap_id, uint64_t *overlap) const | |
665 | { | |
11fdf7f2 TL |
666 | ceph_assert(snap_lock.is_locked()); |
667 | const auto info = get_parent_info(in_snap_id); | |
7c673cae FG |
668 | if (info) { |
669 | *overlap = info->overlap; | |
670 | return 0; | |
671 | } | |
672 | return -ENOENT; | |
673 | } | |
674 | ||
7c673cae | 675 | void ImageCtx::register_watch(Context *on_finish) { |
11fdf7f2 | 676 | ceph_assert(image_watcher != NULL); |
7c673cae FG |
677 | image_watcher->register_watch(on_finish); |
678 | } | |
679 | ||
680 | uint64_t ImageCtx::prune_parent_extents(vector<pair<uint64_t,uint64_t> >& objectx, | |
681 | uint64_t overlap) | |
682 | { | |
683 | // drop extents completely beyond the overlap | |
684 | while (!objectx.empty() && objectx.back().first >= overlap) | |
685 | objectx.pop_back(); | |
686 | ||
687 | // trim final overlapping extent | |
688 | if (!objectx.empty() && objectx.back().first + objectx.back().second > overlap) | |
689 | objectx.back().second = overlap - objectx.back().first; | |
690 | ||
691 | uint64_t len = 0; | |
692 | for (vector<pair<uint64_t,uint64_t> >::iterator p = objectx.begin(); | |
693 | p != objectx.end(); | |
694 | ++p) | |
695 | len += p->second; | |
696 | ldout(cct, 10) << "prune_parent_extents image overlap " << overlap | |
697 | << ", object overlap " << len | |
698 | << " from image extents " << objectx << dendl; | |
699 | return len; | |
700 | } | |
701 | ||
7c673cae FG |
702 | void ImageCtx::cancel_async_requests() { |
703 | C_SaferCond ctx; | |
704 | cancel_async_requests(&ctx); | |
705 | ctx.wait(); | |
706 | } | |
707 | ||
708 | void ImageCtx::cancel_async_requests(Context *on_finish) { | |
709 | { | |
710 | Mutex::Locker 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; | |
716 | req->cancel(); | |
717 | } | |
718 | async_requests_waiters.push_back(on_finish); | |
719 | return; | |
720 | } | |
721 | } | |
722 | ||
723 | on_finish->complete(0); | |
724 | } | |
725 | ||
726 | void ImageCtx::clear_pending_completions() { | |
727 | Mutex::Locker l(completed_reqs_lock); | |
728 | ldout(cct, 10) << "clear pending AioCompletion: count=" | |
729 | << completed_reqs.size() << dendl; | |
730 | completed_reqs.clear(); | |
731 | } | |
732 | ||
11fdf7f2 TL |
733 | void ImageCtx::apply_metadata(const std::map<std::string, bufferlist> &meta, |
734 | bool thread_safe) { | |
735 | ldout(cct, 20) << __func__ << dendl; | |
7c673cae | 736 | |
11fdf7f2 TL |
737 | // reset settings back to global defaults |
738 | for (auto& key : config_overrides) { | |
739 | std::string value; | |
740 | int r = cct->_conf.get_val(key, &value); | |
741 | ceph_assert(r == 0); | |
742 | ||
743 | config.set_val(key, value); | |
744 | } | |
745 | config_overrides.clear(); | |
7c673cae | 746 | |
11fdf7f2 TL |
747 | // extract config overrides |
748 | for (auto meta_pair : meta) { | |
749 | if (!boost::starts_with(meta_pair.first, METADATA_CONF_PREFIX)) { | |
7c673cae | 750 | continue; |
11fdf7f2 | 751 | } |
7c673cae | 752 | |
11fdf7f2 TL |
753 | std::string key = meta_pair.first.substr(METADATA_CONF_PREFIX.size()); |
754 | if (!boost::starts_with(key, "rbd_")) { | |
755 | // ignore non-RBD configuration keys | |
756 | // TODO use option schema to determine applicable subsystem | |
757 | ldout(cct, 0) << __func__ << ": ignoring config " << key << dendl; | |
758 | continue; | |
7c673cae | 759 | } |
7c673cae | 760 | |
11fdf7f2 TL |
761 | if (config.find_option(key) != nullptr) { |
762 | std::string val(meta_pair.second.c_str(), meta_pair.second.length()); | |
763 | int r = config.set_val(key, val); | |
764 | if (r >= 0) { | |
765 | ldout(cct, 20) << __func__ << ": " << key << "=" << val << dendl; | |
766 | config_overrides.insert(key); | |
767 | } else { | |
768 | lderr(cct) << __func__ << ": failed to set config " << key << " " | |
769 | << "with value " << val << ": " << cpp_strerror(r) | |
770 | << dendl; | |
771 | } | |
7c673cae FG |
772 | } |
773 | } | |
774 | ||
11fdf7f2 TL |
775 | #define ASSIGN_OPTION(param, type) \ |
776 | param = config.get_val<type>("rbd_"#param) | |
7c673cae | 777 | |
11fdf7f2 | 778 | bool skip_partial_discard = true; |
181888fb FG |
779 | ASSIGN_OPTION(non_blocking_aio, bool); |
780 | ASSIGN_OPTION(cache, bool); | |
781 | ASSIGN_OPTION(cache_writethrough_until_flush, bool); | |
11fdf7f2 TL |
782 | ASSIGN_OPTION(cache_max_dirty, Option::size_t); |
783 | ASSIGN_OPTION(sparse_read_threshold_bytes, Option::size_t); | |
784 | ASSIGN_OPTION(readahead_max_bytes, Option::size_t); | |
785 | ASSIGN_OPTION(readahead_disable_after_bytes, Option::size_t); | |
181888fb | 786 | ASSIGN_OPTION(clone_copy_on_read, bool); |
181888fb | 787 | ASSIGN_OPTION(enable_alloc_hint, bool); |
11fdf7f2 TL |
788 | ASSIGN_OPTION(mirroring_replay_delay, uint64_t); |
789 | ASSIGN_OPTION(mtime_update_interval, uint64_t); | |
790 | ASSIGN_OPTION(atime_update_interval, uint64_t); | |
181888fb | 791 | ASSIGN_OPTION(skip_partial_discard, bool); |
11fdf7f2 | 792 | ASSIGN_OPTION(discard_granularity_bytes, uint64_t); |
181888fb | 793 | ASSIGN_OPTION(blkin_trace_all, bool); |
b32b8144 | 794 | |
11fdf7f2 | 795 | #undef ASSIGN_OPTION |
b32b8144 FG |
796 | |
797 | if (sparse_read_threshold_bytes == 0) { | |
798 | sparse_read_threshold_bytes = get_object_size(); | |
799 | } | |
11fdf7f2 TL |
800 | if (!skip_partial_discard) { |
801 | discard_granularity_bytes = 0; | |
802 | } | |
803 | ||
804 | io_work_queue->apply_qos_schedule_tick_min( | |
805 | config.get_val<uint64_t>("rbd_qos_schedule_tick_min")); | |
806 | ||
807 | io_work_queue->apply_qos_limit( | |
808 | RBD_QOS_IOPS_THROTTLE, | |
809 | config.get_val<uint64_t>("rbd_qos_iops_limit"), | |
810 | config.get_val<uint64_t>("rbd_qos_iops_burst")); | |
811 | io_work_queue->apply_qos_limit( | |
812 | RBD_QOS_BPS_THROTTLE, | |
813 | config.get_val<uint64_t>("rbd_qos_bps_limit"), | |
814 | config.get_val<uint64_t>("rbd_qos_bps_burst")); | |
815 | io_work_queue->apply_qos_limit( | |
816 | RBD_QOS_READ_IOPS_THROTTLE, | |
817 | config.get_val<uint64_t>("rbd_qos_read_iops_limit"), | |
818 | config.get_val<uint64_t>("rbd_qos_read_iops_burst")); | |
819 | io_work_queue->apply_qos_limit( | |
820 | RBD_QOS_WRITE_IOPS_THROTTLE, | |
821 | config.get_val<uint64_t>("rbd_qos_write_iops_limit"), | |
822 | config.get_val<uint64_t>("rbd_qos_write_iops_burst")); | |
823 | io_work_queue->apply_qos_limit( | |
824 | RBD_QOS_READ_BPS_THROTTLE, | |
825 | config.get_val<uint64_t>("rbd_qos_read_bps_limit"), | |
826 | config.get_val<uint64_t>("rbd_qos_read_bps_burst")); | |
827 | io_work_queue->apply_qos_limit( | |
828 | RBD_QOS_WRITE_BPS_THROTTLE, | |
829 | config.get_val<uint64_t>("rbd_qos_write_bps_limit"), | |
830 | config.get_val<uint64_t>("rbd_qos_write_bps_burst")); | |
7c673cae FG |
831 | } |
832 | ||
833 | ExclusiveLock<ImageCtx> *ImageCtx::create_exclusive_lock() { | |
834 | return new ExclusiveLock<ImageCtx>(*this); | |
835 | } | |
836 | ||
837 | ObjectMap<ImageCtx> *ImageCtx::create_object_map(uint64_t snap_id) { | |
838 | return new ObjectMap<ImageCtx>(*this, snap_id); | |
839 | } | |
840 | ||
841 | Journal<ImageCtx> *ImageCtx::create_journal() { | |
842 | return new Journal<ImageCtx>(*this); | |
843 | } | |
844 | ||
845 | void ImageCtx::set_image_name(const std::string &image_name) { | |
846 | // update the name so rename can be invoked repeatedly | |
847 | RWLock::RLocker owner_locker(owner_lock); | |
848 | RWLock::WLocker snap_locker(snap_lock); | |
849 | name = image_name; | |
850 | if (old_format) { | |
851 | header_oid = util::old_header_name(image_name); | |
852 | } | |
853 | } | |
854 | ||
855 | void ImageCtx::notify_update() { | |
856 | state->handle_update_notification(); | |
857 | ImageWatcher<>::notify_header_update(md_ctx, header_oid); | |
858 | } | |
859 | ||
860 | void ImageCtx::notify_update(Context *on_finish) { | |
861 | state->handle_update_notification(); | |
862 | image_watcher->notify_header_update(on_finish); | |
863 | } | |
864 | ||
865 | exclusive_lock::Policy *ImageCtx::get_exclusive_lock_policy() const { | |
11fdf7f2 TL |
866 | ceph_assert(owner_lock.is_locked()); |
867 | ceph_assert(exclusive_lock_policy != nullptr); | |
7c673cae FG |
868 | return exclusive_lock_policy; |
869 | } | |
870 | ||
871 | void ImageCtx::set_exclusive_lock_policy(exclusive_lock::Policy *policy) { | |
11fdf7f2 TL |
872 | ceph_assert(owner_lock.is_wlocked()); |
873 | ceph_assert(policy != nullptr); | |
7c673cae FG |
874 | delete exclusive_lock_policy; |
875 | exclusive_lock_policy = policy; | |
876 | } | |
877 | ||
878 | journal::Policy *ImageCtx::get_journal_policy() const { | |
11fdf7f2 TL |
879 | ceph_assert(snap_lock.is_locked()); |
880 | ceph_assert(journal_policy != nullptr); | |
7c673cae FG |
881 | return journal_policy; |
882 | } | |
883 | ||
884 | void ImageCtx::set_journal_policy(journal::Policy *policy) { | |
11fdf7f2 TL |
885 | ceph_assert(snap_lock.is_wlocked()); |
886 | ceph_assert(policy != nullptr); | |
7c673cae FG |
887 | delete journal_policy; |
888 | journal_policy = policy; | |
889 | } | |
890 | ||
28e407b8 AA |
891 | bool ImageCtx::is_writeback_cache_enabled() const { |
892 | return (cache && cache_max_dirty > 0); | |
893 | } | |
894 | ||
7c673cae FG |
895 | void ImageCtx::get_thread_pool_instance(CephContext *cct, |
896 | ThreadPool **thread_pool, | |
897 | ContextWQ **op_work_queue) { | |
11fdf7f2 TL |
898 | auto thread_pool_singleton = |
899 | &cct->lookup_or_create_singleton_object<ThreadPoolSingleton>( | |
900 | "librbd::thread_pool", false, cct); | |
7c673cae FG |
901 | *thread_pool = thread_pool_singleton; |
902 | *op_work_queue = thread_pool_singleton->op_work_queue; | |
903 | } | |
904 | ||
905 | void ImageCtx::get_timer_instance(CephContext *cct, SafeTimer **timer, | |
906 | Mutex **timer_lock) { | |
11fdf7f2 TL |
907 | auto safe_timer_singleton = |
908 | &cct->lookup_or_create_singleton_object<SafeTimerSingleton>( | |
909 | "librbd::journal::safe_timer", false, cct); | |
7c673cae FG |
910 | *timer = safe_timer_singleton; |
911 | *timer_lock = &safe_timer_singleton->lock; | |
912 | } | |
913 | } |