]>
Commit | Line | Data |
---|---|---|
1 | // vim: ts=8 sw=2 smarttab | |
2 | /* | |
3 | * Ceph - scalable distributed file system | |
4 | * | |
5 | * Copyright (C) 2014 Red Hat | |
6 | * | |
7 | * This is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License version 2.1, as published by the Free Software | |
10 | * Foundation. See file COPYING. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <unistd.h> | |
15 | #include <stdlib.h> | |
16 | #include <sys/types.h> | |
17 | #include <sys/stat.h> | |
18 | #include <fcntl.h> | |
19 | ||
20 | #include "include/cpp-btree/btree_set.h" | |
21 | ||
22 | #include "BlueStore.h" | |
23 | #include "os/kv.h" | |
24 | #include "include/compat.h" | |
25 | #include "include/intarith.h" | |
26 | #include "include/stringify.h" | |
27 | #include "common/errno.h" | |
28 | #include "common/safe_io.h" | |
29 | #include "Allocator.h" | |
30 | #include "FreelistManager.h" | |
31 | #include "BlueFS.h" | |
32 | #include "BlueRocksEnv.h" | |
33 | #include "auth/Crypto.h" | |
34 | #include "common/EventTrace.h" | |
35 | ||
36 | #define dout_context cct | |
37 | #define dout_subsys ceph_subsys_bluestore | |
38 | ||
39 | using bid_t = decltype(BlueStore::Blob::id); | |
40 | ||
41 | // bluestore_cache_onode | |
42 | MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Onode, bluestore_onode, | |
43 | bluestore_cache_onode); | |
44 | ||
45 | // bluestore_cache_other | |
46 | MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Buffer, bluestore_buffer, | |
47 | bluestore_cache_other); | |
48 | MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Extent, bluestore_extent, | |
49 | bluestore_cache_other); | |
50 | MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Blob, bluestore_blob, | |
51 | bluestore_cache_other); | |
52 | MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::SharedBlob, bluestore_shared_blob, | |
53 | bluestore_cache_other); | |
54 | ||
55 | // bluestore_txc | |
56 | MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::TransContext, bluestore_transcontext, | |
57 | bluestore_txc); | |
58 | ||
59 | ||
60 | // kv store prefixes | |
61 | const string PREFIX_SUPER = "S"; // field -> value | |
62 | const string PREFIX_STAT = "T"; // field -> value(int64 array) | |
63 | const string PREFIX_COLL = "C"; // collection name -> cnode_t | |
64 | const string PREFIX_OBJ = "O"; // object name -> onode_t | |
65 | const string PREFIX_OMAP = "M"; // u64 + keyname -> value | |
66 | const string PREFIX_DEFERRED = "L"; // id -> deferred_transaction_t | |
67 | const string PREFIX_ALLOC = "B"; // u64 offset -> u64 length (freelist) | |
68 | const string PREFIX_SHARED_BLOB = "X"; // u64 offset -> shared_blob_t | |
69 | ||
70 | // write a label in the first block. always use this size. note that | |
71 | // bluefs makes a matching assumption about the location of its | |
72 | // superblock (always the second block of the device). | |
73 | #define BDEV_LABEL_BLOCK_SIZE 4096 | |
74 | ||
75 | // reserve: label (4k) + bluefs super (4k), which means we start at 8k. | |
76 | #define SUPER_RESERVED 8192 | |
77 | ||
78 | #define OBJECT_MAX_SIZE 0xffffffff // 32 bits | |
79 | ||
80 | ||
81 | /* | |
82 | * extent map blob encoding | |
83 | * | |
84 | * we use the low bits of the blobid field to indicate some common scenarios | |
85 | * and spanning vs local ids. See ExtentMap::{encode,decode}_some(). | |
86 | */ | |
87 | #define BLOBID_FLAG_CONTIGUOUS 0x1 // this extent starts at end of previous | |
88 | #define BLOBID_FLAG_ZEROOFFSET 0x2 // blob_offset is 0 | |
89 | #define BLOBID_FLAG_SAMELENGTH 0x4 // length matches previous extent | |
90 | #define BLOBID_FLAG_SPANNING 0x8 // has spanning blob id | |
91 | #define BLOBID_SHIFT_BITS 4 | |
92 | ||
93 | /* | |
94 | * object name key structure | |
95 | * | |
96 | * encoded u8: shard + 2^7 (so that it sorts properly) | |
97 | * encoded u64: poolid + 2^63 (so that it sorts properly) | |
98 | * encoded u32: hash (bit reversed) | |
99 | * | |
100 | * escaped string: namespace | |
101 | * | |
102 | * escaped string: key or object name | |
103 | * 1 char: '<', '=', or '>'. if =, then object key == object name, and | |
104 | * we are done. otherwise, we are followed by the object name. | |
105 | * escaped string: object name (unless '=' above) | |
106 | * | |
107 | * encoded u64: snap | |
108 | * encoded u64: generation | |
109 | * 'o' | |
110 | */ | |
111 | #define ONODE_KEY_SUFFIX 'o' | |
112 | ||
113 | /* | |
114 | * extent shard key | |
115 | * | |
116 | * object prefix key | |
117 | * u32 | |
118 | * 'x' | |
119 | */ | |
120 | #define EXTENT_SHARD_KEY_SUFFIX 'x' | |
121 | ||
122 | /* | |
123 | * string encoding in the key | |
124 | * | |
125 | * The key string needs to lexicographically sort the same way that | |
126 | * ghobject_t does. We do this by escaping anything <= to '#' with # | |
127 | * plus a 2 digit hex string, and anything >= '~' with ~ plus the two | |
128 | * hex digits. | |
129 | * | |
130 | * We use ! as a terminator for strings; this works because it is < # | |
131 | * and will get escaped if it is present in the string. | |
132 | * | |
133 | */ | |
134 | template<typename S> | |
135 | static void append_escaped(const string &in, S *out) | |
136 | { | |
137 | char hexbyte[in.length() * 3 + 1]; | |
138 | char* ptr = &hexbyte[0]; | |
139 | for (string::const_iterator i = in.begin(); i != in.end(); ++i) { | |
140 | if (*i <= '#') { | |
141 | *ptr++ = '#'; | |
142 | *ptr++ = "0123456789abcdef"[(*i >> 4) & 0x0f]; | |
143 | *ptr++ = "0123456789abcdef"[*i & 0x0f]; | |
144 | } else if (*i >= '~') { | |
145 | *ptr++ = '~'; | |
146 | *ptr++ = "0123456789abcdef"[(*i >> 4) & 0x0f]; | |
147 | *ptr++ = "0123456789abcdef"[*i & 0x0f]; | |
148 | } else { | |
149 | *ptr++ = *i; | |
150 | } | |
151 | } | |
152 | *ptr++ = '!'; | |
153 | out->append(hexbyte, ptr - &hexbyte[0]); | |
154 | } | |
155 | ||
156 | inline unsigned h2i(char c) | |
157 | { | |
158 | if ((c >= '0') && (c <= '9')) { | |
159 | return c - 0x30; | |
160 | } else if ((c >= 'a') && (c <= 'f')) { | |
161 | return c - 'a' + 10; | |
162 | } else if ((c >= 'A') && (c <= 'F')) { | |
163 | return c - 'A' + 10; | |
164 | } else { | |
165 | return 256; // make it always larger than 255 | |
166 | } | |
167 | } | |
168 | ||
169 | static int decode_escaped(const char *p, string *out) | |
170 | { | |
171 | char buff[256]; | |
172 | char* ptr = &buff[0]; | |
173 | char* max = &buff[252]; | |
174 | const char *orig_p = p; | |
175 | while (*p && *p != '!') { | |
176 | if (*p == '#' || *p == '~') { | |
177 | unsigned hex = 0; | |
178 | p++; | |
179 | hex = h2i(*p++) << 4; | |
180 | if (hex > 255) { | |
181 | return -EINVAL; | |
182 | } | |
183 | hex |= h2i(*p++); | |
184 | if (hex > 255) { | |
185 | return -EINVAL; | |
186 | } | |
187 | *ptr++ = hex; | |
188 | } else { | |
189 | *ptr++ = *p++; | |
190 | } | |
191 | if (ptr > max) { | |
192 | out->append(buff, ptr-buff); | |
193 | ptr = &buff[0]; | |
194 | } | |
195 | } | |
196 | if (ptr != buff) { | |
197 | out->append(buff, ptr-buff); | |
198 | } | |
199 | return p - orig_p; | |
200 | } | |
201 | ||
202 | // some things we encode in binary (as le32 or le64); print the | |
203 | // resulting key strings nicely | |
204 | template<typename S> | |
205 | static string pretty_binary_string(const S& in) | |
206 | { | |
207 | char buf[10]; | |
208 | string out; | |
209 | out.reserve(in.length() * 3); | |
210 | enum { NONE, HEX, STRING } mode = NONE; | |
211 | unsigned from = 0, i; | |
212 | for (i=0; i < in.length(); ++i) { | |
213 | if ((in[i] < 32 || (unsigned char)in[i] > 126) || | |
214 | (mode == HEX && in.length() - i >= 4 && | |
215 | ((in[i] < 32 || (unsigned char)in[i] > 126) || | |
216 | (in[i+1] < 32 || (unsigned char)in[i+1] > 126) || | |
217 | (in[i+2] < 32 || (unsigned char)in[i+2] > 126) || | |
218 | (in[i+3] < 32 || (unsigned char)in[i+3] > 126)))) { | |
219 | if (mode == STRING) { | |
220 | out.append(in.c_str() + from, i - from); | |
221 | out.push_back('\''); | |
222 | } | |
223 | if (mode != HEX) { | |
224 | out.append("0x"); | |
225 | mode = HEX; | |
226 | } | |
227 | if (in.length() - i >= 4) { | |
228 | // print a whole u32 at once | |
229 | snprintf(buf, sizeof(buf), "%08x", | |
230 | (uint32_t)(((unsigned char)in[i] << 24) | | |
231 | ((unsigned char)in[i+1] << 16) | | |
232 | ((unsigned char)in[i+2] << 8) | | |
233 | ((unsigned char)in[i+3] << 0))); | |
234 | i += 3; | |
235 | } else { | |
236 | snprintf(buf, sizeof(buf), "%02x", (int)(unsigned char)in[i]); | |
237 | } | |
238 | out.append(buf); | |
239 | } else { | |
240 | if (mode != STRING) { | |
241 | out.push_back('\''); | |
242 | mode = STRING; | |
243 | from = i; | |
244 | } | |
245 | } | |
246 | } | |
247 | if (mode == STRING) { | |
248 | out.append(in.c_str() + from, i - from); | |
249 | out.push_back('\''); | |
250 | } | |
251 | return out; | |
252 | } | |
253 | ||
254 | template<typename T> | |
255 | static void _key_encode_shard(shard_id_t shard, T *key) | |
256 | { | |
257 | key->push_back((char)((uint8_t)shard.id + (uint8_t)0x80)); | |
258 | } | |
259 | ||
260 | static const char *_key_decode_shard(const char *key, shard_id_t *pshard) | |
261 | { | |
262 | pshard->id = (uint8_t)*key - (uint8_t)0x80; | |
263 | return key + 1; | |
264 | } | |
265 | ||
266 | static void get_coll_key_range(const coll_t& cid, int bits, | |
267 | string *temp_start, string *temp_end, | |
268 | string *start, string *end) | |
269 | { | |
270 | temp_start->clear(); | |
271 | temp_end->clear(); | |
272 | start->clear(); | |
273 | end->clear(); | |
274 | ||
275 | spg_t pgid; | |
276 | if (cid.is_pg(&pgid)) { | |
277 | _key_encode_shard(pgid.shard, start); | |
278 | *temp_start = *start; | |
279 | ||
280 | _key_encode_u64(pgid.pool() + 0x8000000000000000ull, start); | |
281 | _key_encode_u64((-2ll - pgid.pool()) + 0x8000000000000000ull, temp_start); | |
282 | ||
283 | *end = *start; | |
284 | *temp_end = *temp_start; | |
285 | ||
286 | uint32_t reverse_hash = hobject_t::_reverse_bits(pgid.ps()); | |
287 | _key_encode_u32(reverse_hash, start); | |
288 | _key_encode_u32(reverse_hash, temp_start); | |
289 | ||
290 | uint64_t end_hash = reverse_hash + (1ull << (32 - bits)); | |
291 | if (end_hash > 0xffffffffull) | |
292 | end_hash = 0xffffffffull; | |
293 | ||
294 | _key_encode_u32(end_hash, end); | |
295 | _key_encode_u32(end_hash, temp_end); | |
296 | } else { | |
297 | _key_encode_shard(shard_id_t::NO_SHARD, start); | |
298 | _key_encode_u64(-1ull + 0x8000000000000000ull, start); | |
299 | *end = *start; | |
300 | _key_encode_u32(0, start); | |
301 | _key_encode_u32(0xffffffff, end); | |
302 | ||
303 | // no separate temp section | |
304 | *temp_start = *end; | |
305 | *temp_end = *end; | |
306 | } | |
307 | } | |
308 | ||
309 | static void get_shared_blob_key(uint64_t sbid, string *key) | |
310 | { | |
311 | key->clear(); | |
312 | _key_encode_u64(sbid, key); | |
313 | } | |
314 | ||
315 | static int get_key_shared_blob(const string& key, uint64_t *sbid) | |
316 | { | |
317 | const char *p = key.c_str(); | |
318 | if (key.length() < sizeof(uint64_t)) | |
319 | return -1; | |
320 | _key_decode_u64(p, sbid); | |
321 | return 0; | |
322 | } | |
323 | ||
324 | template<typename S> | |
325 | static int get_key_object(const S& key, ghobject_t *oid) | |
326 | { | |
327 | int r; | |
328 | const char *p = key.c_str(); | |
329 | ||
330 | if (key.length() < 1 + 8 + 4) | |
331 | return -1; | |
332 | p = _key_decode_shard(p, &oid->shard_id); | |
333 | ||
334 | uint64_t pool; | |
335 | p = _key_decode_u64(p, &pool); | |
336 | oid->hobj.pool = pool - 0x8000000000000000ull; | |
337 | ||
338 | unsigned hash; | |
339 | p = _key_decode_u32(p, &hash); | |
340 | ||
341 | oid->hobj.set_bitwise_key_u32(hash); | |
342 | ||
343 | r = decode_escaped(p, &oid->hobj.nspace); | |
344 | if (r < 0) | |
345 | return -2; | |
346 | p += r + 1; | |
347 | ||
348 | string k; | |
349 | r = decode_escaped(p, &k); | |
350 | if (r < 0) | |
351 | return -3; | |
352 | p += r + 1; | |
353 | if (*p == '=') { | |
354 | // no key | |
355 | ++p; | |
356 | oid->hobj.oid.name = k; | |
357 | } else if (*p == '<' || *p == '>') { | |
358 | // key + name | |
359 | ++p; | |
360 | r = decode_escaped(p, &oid->hobj.oid.name); | |
361 | if (r < 0) | |
362 | return -5; | |
363 | p += r + 1; | |
364 | oid->hobj.set_key(k); | |
365 | } else { | |
366 | // malformed | |
367 | return -6; | |
368 | } | |
369 | ||
370 | p = _key_decode_u64(p, &oid->hobj.snap.val); | |
371 | p = _key_decode_u64(p, &oid->generation); | |
372 | ||
373 | if (*p != ONODE_KEY_SUFFIX) { | |
374 | return -7; | |
375 | } | |
376 | p++; | |
377 | if (*p) { | |
378 | // if we get something other than a null terminator here, | |
379 | // something goes wrong. | |
380 | return -8; | |
381 | } | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | template<typename S> | |
387 | static void get_object_key(CephContext *cct, const ghobject_t& oid, S *key) | |
388 | { | |
389 | key->clear(); | |
390 | ||
391 | size_t max_len = 1 + 8 + 4 + | |
392 | (oid.hobj.nspace.length() * 3 + 1) + | |
393 | (oid.hobj.get_key().length() * 3 + 1) + | |
394 | 1 + // for '<', '=', or '>' | |
395 | (oid.hobj.oid.name.length() * 3 + 1) + | |
396 | 8 + 8 + 1; | |
397 | key->reserve(max_len); | |
398 | ||
399 | _key_encode_shard(oid.shard_id, key); | |
400 | _key_encode_u64(oid.hobj.pool + 0x8000000000000000ull, key); | |
401 | _key_encode_u32(oid.hobj.get_bitwise_key_u32(), key); | |
402 | ||
403 | append_escaped(oid.hobj.nspace, key); | |
404 | ||
405 | if (oid.hobj.get_key().length()) { | |
406 | // is a key... could be < = or >. | |
407 | append_escaped(oid.hobj.get_key(), key); | |
408 | // (ASCII chars < = and > sort in that order, yay) | |
409 | int r = oid.hobj.get_key().compare(oid.hobj.oid.name); | |
410 | if (r) { | |
411 | key->append(r > 0 ? ">" : "<"); | |
412 | append_escaped(oid.hobj.oid.name, key); | |
413 | } else { | |
414 | // same as no key | |
415 | key->append("="); | |
416 | } | |
417 | } else { | |
418 | // no key | |
419 | append_escaped(oid.hobj.oid.name, key); | |
420 | key->append("="); | |
421 | } | |
422 | ||
423 | _key_encode_u64(oid.hobj.snap, key); | |
424 | _key_encode_u64(oid.generation, key); | |
425 | ||
426 | key->push_back(ONODE_KEY_SUFFIX); | |
427 | ||
428 | // sanity check | |
429 | if (true) { | |
430 | ghobject_t t; | |
431 | int r = get_key_object(*key, &t); | |
432 | if (r || t != oid) { | |
433 | derr << " r " << r << dendl; | |
434 | derr << "key " << pretty_binary_string(*key) << dendl; | |
435 | derr << "oid " << oid << dendl; | |
436 | derr << " t " << t << dendl; | |
437 | assert(r == 0 && t == oid); | |
438 | } | |
439 | } | |
440 | } | |
441 | ||
442 | ||
443 | // extent shard keys are the onode key, plus a u32, plus 'x'. the trailing | |
444 | // char lets us quickly test whether it is a shard key without decoding any | |
445 | // of the prefix bytes. | |
446 | template<typename S> | |
447 | static void get_extent_shard_key(const S& onode_key, uint32_t offset, | |
448 | string *key) | |
449 | { | |
450 | key->clear(); | |
451 | key->reserve(onode_key.length() + 4 + 1); | |
452 | key->append(onode_key.c_str(), onode_key.size()); | |
453 | _key_encode_u32(offset, key); | |
454 | key->push_back(EXTENT_SHARD_KEY_SUFFIX); | |
455 | } | |
456 | ||
457 | static void rewrite_extent_shard_key(uint32_t offset, string *key) | |
458 | { | |
459 | assert(key->size() > sizeof(uint32_t) + 1); | |
460 | assert(*key->rbegin() == EXTENT_SHARD_KEY_SUFFIX); | |
461 | _key_encode_u32(offset, key->size() - sizeof(uint32_t) - 1, key); | |
462 | } | |
463 | ||
464 | template<typename S> | |
465 | static void generate_extent_shard_key_and_apply( | |
466 | const S& onode_key, | |
467 | uint32_t offset, | |
468 | string *key, | |
469 | std::function<void(const string& final_key)> apply) | |
470 | { | |
471 | if (key->empty()) { // make full key | |
472 | assert(!onode_key.empty()); | |
473 | get_extent_shard_key(onode_key, offset, key); | |
474 | } else { | |
475 | rewrite_extent_shard_key(offset, key); | |
476 | } | |
477 | apply(*key); | |
478 | } | |
479 | ||
480 | int get_key_extent_shard(const string& key, string *onode_key, uint32_t *offset) | |
481 | { | |
482 | assert(key.size() > sizeof(uint32_t) + 1); | |
483 | assert(*key.rbegin() == EXTENT_SHARD_KEY_SUFFIX); | |
484 | int okey_len = key.size() - sizeof(uint32_t) - 1; | |
485 | *onode_key = key.substr(0, okey_len); | |
486 | const char *p = key.data() + okey_len; | |
487 | _key_decode_u32(p, offset); | |
488 | return 0; | |
489 | } | |
490 | ||
491 | static bool is_extent_shard_key(const string& key) | |
492 | { | |
493 | return *key.rbegin() == EXTENT_SHARD_KEY_SUFFIX; | |
494 | } | |
495 | ||
496 | // '-' < '.' < '~' | |
497 | static void get_omap_header(uint64_t id, string *out) | |
498 | { | |
499 | _key_encode_u64(id, out); | |
500 | out->push_back('-'); | |
501 | } | |
502 | ||
503 | // hmm, I don't think there's any need to escape the user key since we | |
504 | // have a clean prefix. | |
505 | static void get_omap_key(uint64_t id, const string& key, string *out) | |
506 | { | |
507 | _key_encode_u64(id, out); | |
508 | out->push_back('.'); | |
509 | out->append(key); | |
510 | } | |
511 | ||
512 | static void rewrite_omap_key(uint64_t id, string old, string *out) | |
513 | { | |
514 | _key_encode_u64(id, out); | |
515 | out->append(old.c_str() + out->length(), old.size() - out->length()); | |
516 | } | |
517 | ||
518 | static void decode_omap_key(const string& key, string *user_key) | |
519 | { | |
520 | *user_key = key.substr(sizeof(uint64_t) + 1); | |
521 | } | |
522 | ||
523 | static void get_omap_tail(uint64_t id, string *out) | |
524 | { | |
525 | _key_encode_u64(id, out); | |
526 | out->push_back('~'); | |
527 | } | |
528 | ||
529 | static void get_deferred_key(uint64_t seq, string *out) | |
530 | { | |
531 | _key_encode_u64(seq, out); | |
532 | } | |
533 | ||
534 | ||
535 | // merge operators | |
536 | ||
537 | struct Int64ArrayMergeOperator : public KeyValueDB::MergeOperator { | |
538 | void merge_nonexistent( | |
539 | const char *rdata, size_t rlen, std::string *new_value) override { | |
540 | *new_value = std::string(rdata, rlen); | |
541 | } | |
542 | void merge( | |
543 | const char *ldata, size_t llen, | |
544 | const char *rdata, size_t rlen, | |
545 | std::string *new_value) override { | |
546 | assert(llen == rlen); | |
547 | assert((rlen % 8) == 0); | |
548 | new_value->resize(rlen); | |
549 | const __le64* lv = (const __le64*)ldata; | |
550 | const __le64* rv = (const __le64*)rdata; | |
551 | __le64* nv = &(__le64&)new_value->at(0); | |
552 | for (size_t i = 0; i < rlen >> 3; ++i) { | |
553 | nv[i] = lv[i] + rv[i]; | |
554 | } | |
555 | } | |
556 | // We use each operator name and each prefix to construct the | |
557 | // overall RocksDB operator name for consistency check at open time. | |
558 | string name() const override { | |
559 | return "int64_array"; | |
560 | } | |
561 | }; | |
562 | ||
563 | ||
564 | // Buffer | |
565 | ||
566 | ostream& operator<<(ostream& out, const BlueStore::Buffer& b) | |
567 | { | |
568 | out << "buffer(" << &b << " space " << b.space << " 0x" << std::hex | |
569 | << b.offset << "~" << b.length << std::dec | |
570 | << " " << BlueStore::Buffer::get_state_name(b.state); | |
571 | if (b.flags) | |
572 | out << " " << BlueStore::Buffer::get_flag_name(b.flags); | |
573 | return out << ")"; | |
574 | } | |
575 | ||
576 | // Garbage Collector | |
577 | ||
578 | void BlueStore::GarbageCollector::process_protrusive_extents( | |
579 | const BlueStore::ExtentMap& extent_map, | |
580 | uint64_t start_offset, | |
581 | uint64_t end_offset, | |
582 | uint64_t start_touch_offset, | |
583 | uint64_t end_touch_offset, | |
584 | uint64_t min_alloc_size) | |
585 | { | |
586 | assert(start_offset <= start_touch_offset && end_offset>= end_touch_offset); | |
587 | ||
588 | uint64_t lookup_start_offset = P2ALIGN(start_offset, min_alloc_size); | |
589 | uint64_t lookup_end_offset = ROUND_UP_TO(end_offset, min_alloc_size); | |
590 | ||
591 | dout(30) << __func__ << " (hex): [" << std::hex | |
592 | << lookup_start_offset << ", " << lookup_end_offset | |
593 | << ")" << std::dec << dendl; | |
594 | ||
595 | for (auto it = extent_map.seek_lextent(lookup_start_offset); | |
596 | it != extent_map.extent_map.end() && | |
597 | it->logical_offset < lookup_end_offset; | |
598 | ++it) { | |
599 | uint64_t alloc_unit_start = it->logical_offset / min_alloc_size; | |
600 | uint64_t alloc_unit_end = (it->logical_end() - 1) / min_alloc_size; | |
601 | ||
602 | dout(30) << __func__ << " " << *it | |
603 | << "alloc_units: " << alloc_unit_start << ".." << alloc_unit_end | |
604 | << dendl; | |
605 | ||
606 | Blob* b = it->blob.get(); | |
607 | ||
608 | if (it->logical_offset >=start_touch_offset && | |
609 | it->logical_end() <= end_touch_offset) { | |
610 | // Process extents within the range affected by | |
611 | // the current write request. | |
612 | // Need to take into account if existing extents | |
613 | // can be merged with them (uncompressed case) | |
614 | if (!b->get_blob().is_compressed()) { | |
615 | if (blob_info_counted && used_alloc_unit == alloc_unit_start) { | |
616 | --blob_info_counted->expected_allocations; // don't need to allocate | |
617 | // new AU for compressed | |
618 | // data since another | |
619 | // collocated uncompressed | |
620 | // blob already exists | |
621 | dout(30) << __func__ << " --expected:" | |
622 | << alloc_unit_start << dendl; | |
623 | } | |
624 | used_alloc_unit = alloc_unit_end; | |
625 | blob_info_counted = nullptr; | |
626 | } | |
627 | } else if (b->get_blob().is_compressed()) { | |
628 | ||
629 | // additionally we take compressed blobs that were not impacted | |
630 | // by the write into account too | |
631 | BlobInfo& bi = | |
632 | affected_blobs.emplace( | |
633 | b, BlobInfo(b->get_referenced_bytes())).first->second; | |
634 | ||
635 | int adjust = | |
636 | (used_alloc_unit && used_alloc_unit == alloc_unit_start) ? 0 : 1; | |
637 | bi.expected_allocations += alloc_unit_end - alloc_unit_start + adjust; | |
638 | dout(30) << __func__ << " expected_allocations=" | |
639 | << bi.expected_allocations << " end_au:" | |
640 | << alloc_unit_end << dendl; | |
641 | ||
642 | blob_info_counted = &bi; | |
643 | used_alloc_unit = alloc_unit_end; | |
644 | ||
645 | assert(it->length <= bi.referenced_bytes); | |
646 | bi.referenced_bytes -= it->length; | |
647 | dout(30) << __func__ << " affected_blob:" << *b | |
648 | << " unref 0x" << std::hex << it->length | |
649 | << " referenced = 0x" << bi.referenced_bytes | |
650 | << std::dec << dendl; | |
651 | // NOTE: we can't move specific blob to resulting GC list here | |
652 | // when reference counter == 0 since subsequent extents might | |
653 | // decrement its expected_allocation. | |
654 | // Hence need to enumerate all the extents first. | |
655 | if (!bi.collect_candidate) { | |
656 | bi.first_lextent = it; | |
657 | bi.collect_candidate = true; | |
658 | } | |
659 | bi.last_lextent = it; | |
660 | } else { | |
661 | if (blob_info_counted && used_alloc_unit == alloc_unit_start) { | |
662 | // don't need to allocate new AU for compressed data since another | |
663 | // collocated uncompressed blob already exists | |
664 | --blob_info_counted->expected_allocations; | |
665 | dout(30) << __func__ << " --expected_allocations:" | |
666 | << alloc_unit_start << dendl; | |
667 | } | |
668 | used_alloc_unit = alloc_unit_end; | |
669 | blob_info_counted = nullptr; | |
670 | } | |
671 | } | |
672 | ||
673 | for (auto b_it = affected_blobs.begin(); | |
674 | b_it != affected_blobs.end(); | |
675 | ++b_it) { | |
676 | Blob* b = b_it->first; | |
677 | BlobInfo& bi = b_it->second; | |
678 | if (bi.referenced_bytes == 0) { | |
679 | uint64_t len_on_disk = b_it->first->get_blob().get_ondisk_length(); | |
680 | int64_t blob_expected_for_release = | |
681 | ROUND_UP_TO(len_on_disk, min_alloc_size) / min_alloc_size; | |
682 | ||
683 | dout(30) << __func__ << " " << *(b_it->first) | |
684 | << " expected4release=" << blob_expected_for_release | |
685 | << " expected_allocations=" << bi.expected_allocations | |
686 | << dendl; | |
687 | int64_t benefit = blob_expected_for_release - bi.expected_allocations; | |
688 | if (benefit >= g_conf->bluestore_gc_enable_blob_threshold) { | |
689 | if (bi.collect_candidate) { | |
690 | auto it = bi.first_lextent; | |
691 | bool bExit = false; | |
692 | do { | |
693 | if (it->blob.get() == b) { | |
694 | extents_to_collect.emplace_back(it->logical_offset, it->length); | |
695 | } | |
696 | bExit = it == bi.last_lextent; | |
697 | ++it; | |
698 | } while (!bExit); | |
699 | } | |
700 | expected_for_release += blob_expected_for_release; | |
701 | expected_allocations += bi.expected_allocations; | |
702 | } | |
703 | } | |
704 | } | |
705 | } | |
706 | ||
707 | int64_t BlueStore::GarbageCollector::estimate( | |
708 | uint64_t start_offset, | |
709 | uint64_t length, | |
710 | const BlueStore::ExtentMap& extent_map, | |
711 | const BlueStore::old_extent_map_t& old_extents, | |
712 | uint64_t min_alloc_size) | |
713 | { | |
714 | ||
715 | affected_blobs.clear(); | |
716 | extents_to_collect.clear(); | |
717 | used_alloc_unit = boost::optional<uint64_t >(); | |
718 | blob_info_counted = nullptr; | |
719 | ||
720 | gc_start_offset = start_offset; | |
721 | gc_end_offset = start_offset + length; | |
722 | ||
723 | uint64_t end_offset = start_offset + length; | |
724 | ||
725 | for (auto it = old_extents.begin(); it != old_extents.end(); ++it) { | |
726 | Blob* b = it->e.blob.get(); | |
727 | if (b->get_blob().is_compressed()) { | |
728 | ||
729 | // update gc_start_offset/gc_end_offset if needed | |
730 | gc_start_offset = min(gc_start_offset, (uint64_t)it->e.blob_start()); | |
731 | gc_end_offset = max(gc_end_offset, (uint64_t)it->e.blob_end()); | |
732 | ||
733 | auto o = it->e.logical_offset; | |
734 | auto l = it->e.length; | |
735 | ||
736 | uint64_t ref_bytes = b->get_referenced_bytes(); | |
737 | // micro optimization to bypass blobs that have no more references | |
738 | if (ref_bytes != 0) { | |
739 | dout(30) << __func__ << " affected_blob:" << *b | |
740 | << " unref 0x" << std::hex << o << "~" << l | |
741 | << std::dec << dendl; | |
742 | affected_blobs.emplace(b, BlobInfo(ref_bytes)); | |
743 | } | |
744 | } | |
745 | } | |
746 | dout(30) << __func__ << " gc range(hex): [" << std::hex | |
747 | << gc_start_offset << ", " << gc_end_offset | |
748 | << ")" << std::dec << dendl; | |
749 | ||
750 | // enumerate preceeding extents to check if they reference affected blobs | |
751 | if (gc_start_offset < start_offset || gc_end_offset > end_offset) { | |
752 | process_protrusive_extents(extent_map, | |
753 | gc_start_offset, | |
754 | gc_end_offset, | |
755 | start_offset, | |
756 | end_offset, | |
757 | min_alloc_size); | |
758 | } | |
759 | return expected_for_release - expected_allocations; | |
760 | } | |
761 | ||
762 | // Cache | |
763 | ||
764 | BlueStore::Cache *BlueStore::Cache::create(CephContext* cct, string type, | |
765 | PerfCounters *logger) | |
766 | { | |
767 | Cache *c = nullptr; | |
768 | ||
769 | if (type == "lru") | |
770 | c = new LRUCache(cct); | |
771 | else if (type == "2q") | |
772 | c = new TwoQCache(cct); | |
773 | else | |
774 | assert(0 == "unrecognized cache type"); | |
775 | ||
776 | c->logger = logger; | |
777 | return c; | |
778 | } | |
779 | ||
780 | void BlueStore::Cache::trim_all() | |
781 | { | |
782 | std::lock_guard<std::recursive_mutex> l(lock); | |
783 | _trim(0, 0); | |
784 | } | |
785 | ||
786 | void BlueStore::Cache::trim( | |
787 | uint64_t target_bytes, | |
788 | float target_meta_ratio, | |
789 | float target_data_ratio, | |
790 | float bytes_per_onode) | |
791 | { | |
792 | std::lock_guard<std::recursive_mutex> l(lock); | |
793 | uint64_t current_meta = _get_num_onodes() * bytes_per_onode; | |
794 | uint64_t current_buffer = _get_buffer_bytes(); | |
795 | uint64_t current = current_meta + current_buffer; | |
796 | ||
797 | uint64_t target_meta = target_bytes * target_meta_ratio; | |
798 | uint64_t target_buffer = target_bytes * target_data_ratio; | |
799 | ||
800 | // correct for overflow or float imprecision | |
801 | target_meta = min(target_bytes, target_meta); | |
802 | target_buffer = min(target_bytes - target_meta, target_buffer); | |
803 | ||
804 | if (current <= target_bytes) { | |
805 | dout(10) << __func__ | |
806 | << " shard target " << pretty_si_t(target_bytes) | |
807 | << " meta/data ratios " << target_meta_ratio | |
808 | << " + " << target_data_ratio << " (" | |
809 | << pretty_si_t(target_meta) << " + " | |
810 | << pretty_si_t(target_buffer) << "), " | |
811 | << " current " << pretty_si_t(current) << " (" | |
812 | << pretty_si_t(current_meta) << " + " | |
813 | << pretty_si_t(current_buffer) << ")" | |
814 | << dendl; | |
815 | return; | |
816 | } | |
817 | ||
818 | uint64_t need_to_free = current - target_bytes; | |
819 | uint64_t free_buffer = 0; | |
820 | uint64_t free_meta = 0; | |
821 | if (current_buffer > target_buffer) { | |
822 | free_buffer = current_buffer - target_buffer; | |
823 | if (free_buffer > need_to_free) { | |
824 | free_buffer = need_to_free; | |
825 | } | |
826 | } | |
827 | free_meta = need_to_free - free_buffer; | |
828 | ||
829 | // start bounds at what we have now | |
830 | uint64_t max_buffer = current_buffer - free_buffer; | |
831 | uint64_t max_meta = current_meta - free_meta; | |
832 | uint64_t max_onodes = max_meta / bytes_per_onode; | |
833 | ||
834 | dout(10) << __func__ | |
835 | << " shard target " << pretty_si_t(target_bytes) | |
836 | << " ratio " << target_meta_ratio << " (" | |
837 | << pretty_si_t(target_meta) << " + " | |
838 | << pretty_si_t(target_buffer) << "), " | |
839 | << " current " << pretty_si_t(current) << " (" | |
840 | << pretty_si_t(current_meta) << " + " | |
841 | << pretty_si_t(current_buffer) << ")," | |
842 | << " need_to_free " << pretty_si_t(need_to_free) << " (" | |
843 | << pretty_si_t(free_meta) << " + " | |
844 | << pretty_si_t(free_buffer) << ")" | |
845 | << " -> max " << max_onodes << " onodes + " | |
846 | << max_buffer << " buffer" | |
847 | << dendl; | |
848 | _trim(max_onodes, max_buffer); | |
849 | } | |
850 | ||
851 | ||
852 | // LRUCache | |
853 | #undef dout_prefix | |
854 | #define dout_prefix *_dout << "bluestore.LRUCache(" << this << ") " | |
855 | ||
856 | void BlueStore::LRUCache::_touch_onode(OnodeRef& o) | |
857 | { | |
858 | auto p = onode_lru.iterator_to(*o); | |
859 | onode_lru.erase(p); | |
860 | onode_lru.push_front(*o); | |
861 | } | |
862 | ||
863 | void BlueStore::LRUCache::_trim(uint64_t onode_max, uint64_t buffer_max) | |
864 | { | |
865 | dout(20) << __func__ << " onodes " << onode_lru.size() << " / " << onode_max | |
866 | << " buffers " << buffer_size << " / " << buffer_max | |
867 | << dendl; | |
868 | ||
869 | _audit("trim start"); | |
870 | ||
871 | // buffers | |
872 | while (buffer_size > buffer_max) { | |
873 | auto i = buffer_lru.rbegin(); | |
874 | if (i == buffer_lru.rend()) { | |
875 | // stop if buffer_lru is now empty | |
876 | break; | |
877 | } | |
878 | ||
879 | Buffer *b = &*i; | |
880 | assert(b->is_clean()); | |
881 | dout(20) << __func__ << " rm " << *b << dendl; | |
882 | b->space->_rm_buffer(this, b); | |
883 | } | |
884 | ||
885 | // onodes | |
886 | int num = onode_lru.size() - onode_max; | |
887 | if (num <= 0) | |
888 | return; // don't even try | |
889 | ||
890 | auto p = onode_lru.end(); | |
891 | assert(p != onode_lru.begin()); | |
892 | --p; | |
893 | int skipped = 0; | |
894 | int max_skipped = g_conf->bluestore_cache_trim_max_skip_pinned; | |
895 | while (num > 0) { | |
896 | Onode *o = &*p; | |
897 | int refs = o->nref.load(); | |
898 | if (refs > 1) { | |
899 | dout(20) << __func__ << " " << o->oid << " has " << refs | |
900 | << " refs, skipping" << dendl; | |
901 | if (++skipped >= max_skipped) { | |
902 | dout(20) << __func__ << " maximum skip pinned reached; stopping with " | |
903 | << num << " left to trim" << dendl; | |
904 | break; | |
905 | } | |
906 | ||
907 | if (p == onode_lru.begin()) { | |
908 | break; | |
909 | } else { | |
910 | p--; | |
911 | num--; | |
912 | continue; | |
913 | } | |
914 | } | |
915 | dout(30) << __func__ << " rm " << o->oid << dendl; | |
916 | if (p != onode_lru.begin()) { | |
917 | onode_lru.erase(p--); | |
918 | } else { | |
919 | onode_lru.erase(p); | |
920 | assert(num == 1); | |
921 | } | |
922 | o->get(); // paranoia | |
923 | o->c->onode_map.remove(o->oid); | |
924 | o->put(); | |
925 | --num; | |
926 | } | |
927 | } | |
928 | ||
929 | #ifdef DEBUG_CACHE | |
930 | void BlueStore::LRUCache::_audit(const char *when) | |
931 | { | |
932 | dout(10) << __func__ << " " << when << " start" << dendl; | |
933 | uint64_t s = 0; | |
934 | for (auto i = buffer_lru.begin(); i != buffer_lru.end(); ++i) { | |
935 | s += i->length; | |
936 | } | |
937 | if (s != buffer_size) { | |
938 | derr << __func__ << " buffer_size " << buffer_size << " actual " << s | |
939 | << dendl; | |
940 | for (auto i = buffer_lru.begin(); i != buffer_lru.end(); ++i) { | |
941 | derr << __func__ << " " << *i << dendl; | |
942 | } | |
943 | assert(s == buffer_size); | |
944 | } | |
945 | dout(20) << __func__ << " " << when << " buffer_size " << buffer_size | |
946 | << " ok" << dendl; | |
947 | } | |
948 | #endif | |
949 | ||
950 | // TwoQCache | |
951 | #undef dout_prefix | |
952 | #define dout_prefix *_dout << "bluestore.2QCache(" << this << ") " | |
953 | ||
954 | ||
955 | void BlueStore::TwoQCache::_touch_onode(OnodeRef& o) | |
956 | { | |
957 | auto p = onode_lru.iterator_to(*o); | |
958 | onode_lru.erase(p); | |
959 | onode_lru.push_front(*o); | |
960 | } | |
961 | ||
962 | void BlueStore::TwoQCache::_add_buffer(Buffer *b, int level, Buffer *near) | |
963 | { | |
964 | dout(20) << __func__ << " level " << level << " near " << near | |
965 | << " on " << *b | |
966 | << " which has cache_private " << b->cache_private << dendl; | |
967 | if (near) { | |
968 | b->cache_private = near->cache_private; | |
969 | switch (b->cache_private) { | |
970 | case BUFFER_WARM_IN: | |
971 | buffer_warm_in.insert(buffer_warm_in.iterator_to(*near), *b); | |
972 | break; | |
973 | case BUFFER_WARM_OUT: | |
974 | assert(b->is_empty()); | |
975 | buffer_warm_out.insert(buffer_warm_out.iterator_to(*near), *b); | |
976 | break; | |
977 | case BUFFER_HOT: | |
978 | buffer_hot.insert(buffer_hot.iterator_to(*near), *b); | |
979 | break; | |
980 | default: | |
981 | assert(0 == "bad cache_private"); | |
982 | } | |
983 | } else if (b->cache_private == BUFFER_NEW) { | |
984 | b->cache_private = BUFFER_WARM_IN; | |
985 | if (level > 0) { | |
986 | buffer_warm_in.push_front(*b); | |
987 | } else { | |
988 | // take caller hint to start at the back of the warm queue | |
989 | buffer_warm_in.push_back(*b); | |
990 | } | |
991 | } else { | |
992 | // we got a hint from discard | |
993 | switch (b->cache_private) { | |
994 | case BUFFER_WARM_IN: | |
995 | // stay in warm_in. move to front, even though 2Q doesn't actually | |
996 | // do this. | |
997 | dout(20) << __func__ << " move to front of warm " << *b << dendl; | |
998 | buffer_warm_in.push_front(*b); | |
999 | break; | |
1000 | case BUFFER_WARM_OUT: | |
1001 | b->cache_private = BUFFER_HOT; | |
1002 | // move to hot. fall-thru | |
1003 | case BUFFER_HOT: | |
1004 | dout(20) << __func__ << " move to front of hot " << *b << dendl; | |
1005 | buffer_hot.push_front(*b); | |
1006 | break; | |
1007 | default: | |
1008 | assert(0 == "bad cache_private"); | |
1009 | } | |
1010 | } | |
1011 | if (!b->is_empty()) { | |
1012 | buffer_bytes += b->length; | |
1013 | buffer_list_bytes[b->cache_private] += b->length; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | void BlueStore::TwoQCache::_rm_buffer(Buffer *b) | |
1018 | { | |
1019 | dout(20) << __func__ << " " << *b << dendl; | |
1020 | if (!b->is_empty()) { | |
1021 | assert(buffer_bytes >= b->length); | |
1022 | buffer_bytes -= b->length; | |
1023 | assert(buffer_list_bytes[b->cache_private] >= b->length); | |
1024 | buffer_list_bytes[b->cache_private] -= b->length; | |
1025 | } | |
1026 | switch (b->cache_private) { | |
1027 | case BUFFER_WARM_IN: | |
1028 | buffer_warm_in.erase(buffer_warm_in.iterator_to(*b)); | |
1029 | break; | |
1030 | case BUFFER_WARM_OUT: | |
1031 | buffer_warm_out.erase(buffer_warm_out.iterator_to(*b)); | |
1032 | break; | |
1033 | case BUFFER_HOT: | |
1034 | buffer_hot.erase(buffer_hot.iterator_to(*b)); | |
1035 | break; | |
1036 | default: | |
1037 | assert(0 == "bad cache_private"); | |
1038 | } | |
1039 | } | |
1040 | ||
1041 | void BlueStore::TwoQCache::_move_buffer(Cache *srcc, Buffer *b) | |
1042 | { | |
1043 | TwoQCache *src = static_cast<TwoQCache*>(srcc); | |
1044 | src->_rm_buffer(b); | |
1045 | ||
1046 | // preserve which list we're on (even if we can't preserve the order!) | |
1047 | switch (b->cache_private) { | |
1048 | case BUFFER_WARM_IN: | |
1049 | assert(!b->is_empty()); | |
1050 | buffer_warm_in.push_back(*b); | |
1051 | break; | |
1052 | case BUFFER_WARM_OUT: | |
1053 | assert(b->is_empty()); | |
1054 | buffer_warm_out.push_back(*b); | |
1055 | break; | |
1056 | case BUFFER_HOT: | |
1057 | assert(!b->is_empty()); | |
1058 | buffer_hot.push_back(*b); | |
1059 | break; | |
1060 | default: | |
1061 | assert(0 == "bad cache_private"); | |
1062 | } | |
1063 | if (!b->is_empty()) { | |
1064 | buffer_bytes += b->length; | |
1065 | buffer_list_bytes[b->cache_private] += b->length; | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | void BlueStore::TwoQCache::_adjust_buffer_size(Buffer *b, int64_t delta) | |
1070 | { | |
1071 | dout(20) << __func__ << " delta " << delta << " on " << *b << dendl; | |
1072 | if (!b->is_empty()) { | |
1073 | assert((int64_t)buffer_bytes + delta >= 0); | |
1074 | buffer_bytes += delta; | |
1075 | assert((int64_t)buffer_list_bytes[b->cache_private] + delta >= 0); | |
1076 | buffer_list_bytes[b->cache_private] += delta; | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | void BlueStore::TwoQCache::_trim(uint64_t onode_max, uint64_t buffer_max) | |
1081 | { | |
1082 | dout(20) << __func__ << " onodes " << onode_lru.size() << " / " << onode_max | |
1083 | << " buffers " << buffer_bytes << " / " << buffer_max | |
1084 | << dendl; | |
1085 | ||
1086 | _audit("trim start"); | |
1087 | ||
1088 | // buffers | |
1089 | if (buffer_bytes > buffer_max) { | |
1090 | uint64_t kin = buffer_max * cct->_conf->bluestore_2q_cache_kin_ratio; | |
1091 | uint64_t khot = buffer_max - kin; | |
1092 | ||
1093 | // pre-calculate kout based on average buffer size too, | |
1094 | // which is typical(the warm_in and hot lists may change later) | |
1095 | uint64_t kout = 0; | |
1096 | uint64_t buffer_num = buffer_hot.size() + buffer_warm_in.size(); | |
1097 | if (buffer_num) { | |
1098 | uint64_t buffer_avg_size = buffer_bytes / buffer_num; | |
1099 | assert(buffer_avg_size); | |
1100 | uint64_t calculated_buffer_num = buffer_max / buffer_avg_size; | |
1101 | kout = calculated_buffer_num * cct->_conf->bluestore_2q_cache_kout_ratio; | |
1102 | } | |
1103 | ||
1104 | if (buffer_list_bytes[BUFFER_HOT] < khot) { | |
1105 | // hot is small, give slack to warm_in | |
1106 | kin += khot - buffer_list_bytes[BUFFER_HOT]; | |
1107 | } else if (buffer_list_bytes[BUFFER_WARM_IN] < kin) { | |
1108 | // warm_in is small, give slack to hot | |
1109 | khot += kin - buffer_list_bytes[BUFFER_WARM_IN]; | |
1110 | } | |
1111 | ||
1112 | // adjust warm_in list | |
1113 | int64_t to_evict_bytes = buffer_list_bytes[BUFFER_WARM_IN] - kin; | |
1114 | uint64_t evicted = 0; | |
1115 | ||
1116 | while (to_evict_bytes > 0) { | |
1117 | auto p = buffer_warm_in.rbegin(); | |
1118 | if (p == buffer_warm_in.rend()) { | |
1119 | // stop if warm_in list is now empty | |
1120 | break; | |
1121 | } | |
1122 | ||
1123 | Buffer *b = &*p; | |
1124 | assert(b->is_clean()); | |
1125 | dout(20) << __func__ << " buffer_warm_in -> out " << *b << dendl; | |
1126 | assert(buffer_bytes >= b->length); | |
1127 | buffer_bytes -= b->length; | |
1128 | assert(buffer_list_bytes[BUFFER_WARM_IN] >= b->length); | |
1129 | buffer_list_bytes[BUFFER_WARM_IN] -= b->length; | |
1130 | to_evict_bytes -= b->length; | |
1131 | evicted += b->length; | |
1132 | b->state = Buffer::STATE_EMPTY; | |
1133 | b->data.clear(); | |
1134 | buffer_warm_in.erase(buffer_warm_in.iterator_to(*b)); | |
1135 | buffer_warm_out.push_front(*b); | |
1136 | b->cache_private = BUFFER_WARM_OUT; | |
1137 | } | |
1138 | ||
1139 | if (evicted > 0) { | |
1140 | dout(20) << __func__ << " evicted " << prettybyte_t(evicted) | |
1141 | << " from warm_in list, done evicting warm_in buffers" | |
1142 | << dendl; | |
1143 | } | |
1144 | ||
1145 | // adjust hot list | |
1146 | to_evict_bytes = buffer_list_bytes[BUFFER_HOT] - khot; | |
1147 | evicted = 0; | |
1148 | ||
1149 | while (to_evict_bytes > 0) { | |
1150 | auto p = buffer_hot.rbegin(); | |
1151 | if (p == buffer_hot.rend()) { | |
1152 | // stop if hot list is now empty | |
1153 | break; | |
1154 | } | |
1155 | ||
1156 | Buffer *b = &*p; | |
1157 | dout(20) << __func__ << " buffer_hot rm " << *b << dendl; | |
1158 | assert(b->is_clean()); | |
1159 | // adjust evict size before buffer goes invalid | |
1160 | to_evict_bytes -= b->length; | |
1161 | evicted += b->length; | |
1162 | b->space->_rm_buffer(this, b); | |
1163 | } | |
1164 | ||
1165 | if (evicted > 0) { | |
1166 | dout(20) << __func__ << " evicted " << prettybyte_t(evicted) | |
1167 | << " from hot list, done evicting hot buffers" | |
1168 | << dendl; | |
1169 | } | |
1170 | ||
1171 | // adjust warm out list too, if necessary | |
1172 | int64_t num = buffer_warm_out.size() - kout; | |
1173 | while (num-- > 0) { | |
1174 | Buffer *b = &*buffer_warm_out.rbegin(); | |
1175 | assert(b->is_empty()); | |
1176 | dout(20) << __func__ << " buffer_warm_out rm " << *b << dendl; | |
1177 | b->space->_rm_buffer(this, b); | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | // onodes | |
1182 | int num = onode_lru.size() - onode_max; | |
1183 | if (num <= 0) | |
1184 | return; // don't even try | |
1185 | ||
1186 | auto p = onode_lru.end(); | |
1187 | assert(p != onode_lru.begin()); | |
1188 | --p; | |
1189 | int skipped = 0; | |
1190 | int max_skipped = g_conf->bluestore_cache_trim_max_skip_pinned; | |
1191 | while (num > 0) { | |
1192 | Onode *o = &*p; | |
1193 | dout(20) << __func__ << " considering " << o << dendl; | |
1194 | int refs = o->nref.load(); | |
1195 | if (refs > 1) { | |
1196 | dout(20) << __func__ << " " << o->oid << " has " << refs | |
1197 | << " refs; skipping" << dendl; | |
1198 | if (++skipped >= max_skipped) { | |
1199 | dout(20) << __func__ << " maximum skip pinned reached; stopping with " | |
1200 | << num << " left to trim" << dendl; | |
1201 | break; | |
1202 | } | |
1203 | ||
1204 | if (p == onode_lru.begin()) { | |
1205 | break; | |
1206 | } else { | |
1207 | p--; | |
1208 | num--; | |
1209 | continue; | |
1210 | } | |
1211 | } | |
1212 | dout(30) << __func__ << " " << o->oid << " num=" << num <<" lru size="<<onode_lru.size()<< dendl; | |
1213 | if (p != onode_lru.begin()) { | |
1214 | onode_lru.erase(p--); | |
1215 | } else { | |
1216 | onode_lru.erase(p); | |
1217 | assert(num == 1); | |
1218 | } | |
1219 | o->get(); // paranoia | |
1220 | o->c->onode_map.remove(o->oid); | |
1221 | o->put(); | |
1222 | --num; | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | #ifdef DEBUG_CACHE | |
1227 | void BlueStore::TwoQCache::_audit(const char *when) | |
1228 | { | |
1229 | dout(10) << __func__ << " " << when << " start" << dendl; | |
1230 | uint64_t s = 0; | |
1231 | for (auto i = buffer_hot.begin(); i != buffer_hot.end(); ++i) { | |
1232 | s += i->length; | |
1233 | } | |
1234 | ||
1235 | uint64_t hot_bytes = s; | |
1236 | if (hot_bytes != buffer_list_bytes[BUFFER_HOT]) { | |
1237 | derr << __func__ << " hot_list_bytes " | |
1238 | << buffer_list_bytes[BUFFER_HOT] | |
1239 | << " != actual " << hot_bytes | |
1240 | << dendl; | |
1241 | assert(hot_bytes == buffer_list_bytes[BUFFER_HOT]); | |
1242 | } | |
1243 | ||
1244 | for (auto i = buffer_warm_in.begin(); i != buffer_warm_in.end(); ++i) { | |
1245 | s += i->length; | |
1246 | } | |
1247 | ||
1248 | uint64_t warm_in_bytes = s - hot_bytes; | |
1249 | if (warm_in_bytes != buffer_list_bytes[BUFFER_WARM_IN]) { | |
1250 | derr << __func__ << " warm_in_list_bytes " | |
1251 | << buffer_list_bytes[BUFFER_WARM_IN] | |
1252 | << " != actual " << warm_in_bytes | |
1253 | << dendl; | |
1254 | assert(warm_in_bytes == buffer_list_bytes[BUFFER_WARM_IN]); | |
1255 | } | |
1256 | ||
1257 | if (s != buffer_bytes) { | |
1258 | derr << __func__ << " buffer_bytes " << buffer_bytes << " actual " << s | |
1259 | << dendl; | |
1260 | assert(s == buffer_bytes); | |
1261 | } | |
1262 | ||
1263 | dout(20) << __func__ << " " << when << " buffer_bytes " << buffer_bytes | |
1264 | << " ok" << dendl; | |
1265 | } | |
1266 | #endif | |
1267 | ||
1268 | ||
1269 | // BufferSpace | |
1270 | ||
1271 | #undef dout_prefix | |
1272 | #define dout_prefix *_dout << "bluestore.BufferSpace(" << this << " in " << cache << ") " | |
1273 | ||
1274 | void BlueStore::BufferSpace::_clear(Cache* cache) | |
1275 | { | |
1276 | // note: we already hold cache->lock | |
1277 | ldout(cache->cct, 20) << __func__ << dendl; | |
1278 | while (!buffer_map.empty()) { | |
1279 | _rm_buffer(cache, buffer_map.begin()); | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | int BlueStore::BufferSpace::_discard(Cache* cache, uint32_t offset, uint32_t length) | |
1284 | { | |
1285 | // note: we already hold cache->lock | |
1286 | ldout(cache->cct, 20) << __func__ << std::hex << " 0x" << offset << "~" << length | |
1287 | << std::dec << dendl; | |
1288 | int cache_private = 0; | |
1289 | cache->_audit("discard start"); | |
1290 | auto i = _data_lower_bound(offset); | |
1291 | uint32_t end = offset + length; | |
1292 | while (i != buffer_map.end()) { | |
1293 | Buffer *b = i->second.get(); | |
1294 | if (b->offset >= end) { | |
1295 | break; | |
1296 | } | |
1297 | if (b->cache_private > cache_private) { | |
1298 | cache_private = b->cache_private; | |
1299 | } | |
1300 | if (b->offset < offset) { | |
1301 | int64_t front = offset - b->offset; | |
1302 | if (b->end() > end) { | |
1303 | // drop middle (split) | |
1304 | uint32_t tail = b->end() - end; | |
1305 | if (b->data.length()) { | |
1306 | bufferlist bl; | |
1307 | bl.substr_of(b->data, b->length - tail, tail); | |
1308 | Buffer *nb = new Buffer(this, b->state, b->seq, end, bl); | |
1309 | nb->maybe_rebuild(); | |
1310 | _add_buffer(cache, nb, 0, b); | |
1311 | } else { | |
1312 | _add_buffer(cache, new Buffer(this, b->state, b->seq, end, tail), | |
1313 | 0, b); | |
1314 | } | |
1315 | if (!b->is_writing()) { | |
1316 | cache->_adjust_buffer_size(b, front - (int64_t)b->length); | |
1317 | } | |
1318 | b->truncate(front); | |
1319 | b->maybe_rebuild(); | |
1320 | cache->_audit("discard end 1"); | |
1321 | break; | |
1322 | } else { | |
1323 | // drop tail | |
1324 | if (!b->is_writing()) { | |
1325 | cache->_adjust_buffer_size(b, front - (int64_t)b->length); | |
1326 | } | |
1327 | b->truncate(front); | |
1328 | b->maybe_rebuild(); | |
1329 | ++i; | |
1330 | continue; | |
1331 | } | |
1332 | } | |
1333 | if (b->end() <= end) { | |
1334 | // drop entire buffer | |
1335 | _rm_buffer(cache, i++); | |
1336 | continue; | |
1337 | } | |
1338 | // drop front | |
1339 | uint32_t keep = b->end() - end; | |
1340 | if (b->data.length()) { | |
1341 | bufferlist bl; | |
1342 | bl.substr_of(b->data, b->length - keep, keep); | |
1343 | Buffer *nb = new Buffer(this, b->state, b->seq, end, bl); | |
1344 | nb->maybe_rebuild(); | |
1345 | _add_buffer(cache, nb, 0, b); | |
1346 | } else { | |
1347 | _add_buffer(cache, new Buffer(this, b->state, b->seq, end, keep), 0, b); | |
1348 | } | |
1349 | _rm_buffer(cache, i); | |
1350 | cache->_audit("discard end 2"); | |
1351 | break; | |
1352 | } | |
1353 | return cache_private; | |
1354 | } | |
1355 | ||
1356 | void BlueStore::BufferSpace::read( | |
1357 | Cache* cache, | |
1358 | uint32_t offset, | |
1359 | uint32_t length, | |
1360 | BlueStore::ready_regions_t& res, | |
1361 | interval_set<uint32_t>& res_intervals) | |
1362 | { | |
1363 | res.clear(); | |
1364 | res_intervals.clear(); | |
1365 | uint32_t want_bytes = length; | |
1366 | uint32_t end = offset + length; | |
1367 | ||
1368 | { | |
1369 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1370 | for (auto i = _data_lower_bound(offset); | |
1371 | i != buffer_map.end() && offset < end && i->first < end; | |
1372 | ++i) { | |
1373 | Buffer *b = i->second.get(); | |
1374 | assert(b->end() > offset); | |
1375 | if (b->is_writing() || b->is_clean()) { | |
1376 | if (b->offset < offset) { | |
1377 | uint32_t skip = offset - b->offset; | |
1378 | uint32_t l = MIN(length, b->length - skip); | |
1379 | res[offset].substr_of(b->data, skip, l); | |
1380 | res_intervals.insert(offset, l); | |
1381 | offset += l; | |
1382 | length -= l; | |
1383 | if (!b->is_writing()) { | |
1384 | cache->_touch_buffer(b); | |
1385 | } | |
1386 | continue; | |
1387 | } | |
1388 | if (b->offset > offset) { | |
1389 | uint32_t gap = b->offset - offset; | |
1390 | if (length <= gap) { | |
1391 | break; | |
1392 | } | |
1393 | offset += gap; | |
1394 | length -= gap; | |
1395 | } | |
1396 | if (!b->is_writing()) { | |
1397 | cache->_touch_buffer(b); | |
1398 | } | |
1399 | if (b->length > length) { | |
1400 | res[offset].substr_of(b->data, 0, length); | |
1401 | res_intervals.insert(offset, length); | |
1402 | break; | |
1403 | } else { | |
1404 | res[offset].append(b->data); | |
1405 | res_intervals.insert(offset, b->length); | |
1406 | if (b->length == length) | |
1407 | break; | |
1408 | offset += b->length; | |
1409 | length -= b->length; | |
1410 | } | |
1411 | } | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | uint64_t hit_bytes = res_intervals.size(); | |
1416 | assert(hit_bytes <= want_bytes); | |
1417 | uint64_t miss_bytes = want_bytes - hit_bytes; | |
1418 | cache->logger->inc(l_bluestore_buffer_hit_bytes, hit_bytes); | |
1419 | cache->logger->inc(l_bluestore_buffer_miss_bytes, miss_bytes); | |
1420 | } | |
1421 | ||
1422 | void BlueStore::BufferSpace::finish_write(Cache* cache, uint64_t seq) | |
1423 | { | |
1424 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1425 | ||
1426 | auto i = writing.begin(); | |
1427 | while (i != writing.end()) { | |
1428 | if (i->seq > seq) { | |
1429 | break; | |
1430 | } | |
1431 | if (i->seq < seq) { | |
1432 | ++i; | |
1433 | continue; | |
1434 | } | |
1435 | ||
1436 | Buffer *b = &*i; | |
1437 | assert(b->is_writing()); | |
1438 | ||
1439 | if (b->flags & Buffer::FLAG_NOCACHE) { | |
1440 | writing.erase(i++); | |
1441 | ldout(cache->cct, 20) << __func__ << " discard " << *b << dendl; | |
1442 | buffer_map.erase(b->offset); | |
1443 | } else { | |
1444 | b->state = Buffer::STATE_CLEAN; | |
1445 | writing.erase(i++); | |
1446 | b->maybe_rebuild(); | |
1447 | b->data.reassign_to_mempool(mempool::mempool_bluestore_cache_data); | |
1448 | cache->_add_buffer(b, 1, nullptr); | |
1449 | ldout(cache->cct, 20) << __func__ << " added " << *b << dendl; | |
1450 | } | |
1451 | } | |
1452 | ||
1453 | cache->_audit("finish_write end"); | |
1454 | } | |
1455 | ||
1456 | void BlueStore::BufferSpace::split(Cache* cache, size_t pos, BlueStore::BufferSpace &r) | |
1457 | { | |
1458 | std::lock_guard<std::recursive_mutex> lk(cache->lock); | |
1459 | if (buffer_map.empty()) | |
1460 | return; | |
1461 | ||
1462 | auto p = --buffer_map.end(); | |
1463 | while (true) { | |
1464 | if (p->second->end() <= pos) | |
1465 | break; | |
1466 | ||
1467 | if (p->second->offset < pos) { | |
1468 | ldout(cache->cct, 30) << __func__ << " cut " << *p->second << dendl; | |
1469 | size_t left = pos - p->second->offset; | |
1470 | size_t right = p->second->length - left; | |
1471 | if (p->second->data.length()) { | |
1472 | bufferlist bl; | |
1473 | bl.substr_of(p->second->data, left, right); | |
1474 | r._add_buffer(cache, new Buffer(&r, p->second->state, p->second->seq, 0, bl), | |
1475 | 0, p->second.get()); | |
1476 | } else { | |
1477 | r._add_buffer(cache, new Buffer(&r, p->second->state, p->second->seq, 0, right), | |
1478 | 0, p->second.get()); | |
1479 | } | |
1480 | cache->_adjust_buffer_size(p->second.get(), -right); | |
1481 | p->second->truncate(left); | |
1482 | break; | |
1483 | } | |
1484 | ||
1485 | assert(p->second->end() > pos); | |
1486 | ldout(cache->cct, 30) << __func__ << " move " << *p->second << dendl; | |
1487 | if (p->second->data.length()) { | |
1488 | r._add_buffer(cache, new Buffer(&r, p->second->state, p->second->seq, | |
1489 | p->second->offset - pos, p->second->data), | |
1490 | 0, p->second.get()); | |
1491 | } else { | |
1492 | r._add_buffer(cache, new Buffer(&r, p->second->state, p->second->seq, | |
1493 | p->second->offset - pos, p->second->length), | |
1494 | 0, p->second.get()); | |
1495 | } | |
1496 | if (p == buffer_map.begin()) { | |
1497 | _rm_buffer(cache, p); | |
1498 | break; | |
1499 | } else { | |
1500 | _rm_buffer(cache, p--); | |
1501 | } | |
1502 | } | |
1503 | assert(writing.empty()); | |
1504 | } | |
1505 | ||
1506 | // OnodeSpace | |
1507 | ||
1508 | #undef dout_prefix | |
1509 | #define dout_prefix *_dout << "bluestore.OnodeSpace(" << this << " in " << cache << ") " | |
1510 | ||
1511 | BlueStore::OnodeRef BlueStore::OnodeSpace::add(const ghobject_t& oid, OnodeRef o) | |
1512 | { | |
1513 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1514 | auto p = onode_map.find(oid); | |
1515 | if (p != onode_map.end()) { | |
1516 | ldout(cache->cct, 30) << __func__ << " " << oid << " " << o | |
1517 | << " raced, returning existing " << p->second | |
1518 | << dendl; | |
1519 | return p->second; | |
1520 | } | |
1521 | ldout(cache->cct, 30) << __func__ << " " << oid << " " << o << dendl; | |
1522 | onode_map[oid] = o; | |
1523 | cache->_add_onode(o, 1); | |
1524 | return o; | |
1525 | } | |
1526 | ||
1527 | BlueStore::OnodeRef BlueStore::OnodeSpace::lookup(const ghobject_t& oid) | |
1528 | { | |
1529 | ldout(cache->cct, 30) << __func__ << dendl; | |
1530 | OnodeRef o; | |
1531 | bool hit = false; | |
1532 | ||
1533 | { | |
1534 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1535 | ceph::unordered_map<ghobject_t,OnodeRef>::iterator p = onode_map.find(oid); | |
1536 | if (p == onode_map.end()) { | |
1537 | ldout(cache->cct, 30) << __func__ << " " << oid << " miss" << dendl; | |
1538 | } else { | |
1539 | ldout(cache->cct, 30) << __func__ << " " << oid << " hit " << p->second | |
1540 | << dendl; | |
1541 | cache->_touch_onode(p->second); | |
1542 | hit = true; | |
1543 | o = p->second; | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | if (hit) { | |
1548 | cache->logger->inc(l_bluestore_onode_hits); | |
1549 | } else { | |
1550 | cache->logger->inc(l_bluestore_onode_misses); | |
1551 | } | |
1552 | return o; | |
1553 | } | |
1554 | ||
1555 | void BlueStore::OnodeSpace::clear() | |
1556 | { | |
1557 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1558 | ldout(cache->cct, 10) << __func__ << dendl; | |
1559 | for (auto &p : onode_map) { | |
1560 | cache->_rm_onode(p.second); | |
1561 | } | |
1562 | onode_map.clear(); | |
1563 | } | |
1564 | ||
1565 | bool BlueStore::OnodeSpace::empty() | |
1566 | { | |
1567 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1568 | return onode_map.empty(); | |
1569 | } | |
1570 | ||
1571 | void BlueStore::OnodeSpace::rename( | |
1572 | OnodeRef& oldo, | |
1573 | const ghobject_t& old_oid, | |
1574 | const ghobject_t& new_oid, | |
1575 | const mempool::bluestore_cache_other::string& new_okey) | |
1576 | { | |
1577 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1578 | ldout(cache->cct, 30) << __func__ << " " << old_oid << " -> " << new_oid | |
1579 | << dendl; | |
1580 | ceph::unordered_map<ghobject_t,OnodeRef>::iterator po, pn; | |
1581 | po = onode_map.find(old_oid); | |
1582 | pn = onode_map.find(new_oid); | |
1583 | assert(po != pn); | |
1584 | ||
1585 | assert(po != onode_map.end()); | |
1586 | if (pn != onode_map.end()) { | |
1587 | ldout(cache->cct, 30) << __func__ << " removing target " << pn->second | |
1588 | << dendl; | |
1589 | cache->_rm_onode(pn->second); | |
1590 | onode_map.erase(pn); | |
1591 | } | |
1592 | OnodeRef o = po->second; | |
1593 | ||
1594 | // install a non-existent onode at old location | |
1595 | oldo.reset(new Onode(o->c, old_oid, o->key)); | |
1596 | po->second = oldo; | |
1597 | cache->_add_onode(po->second, 1); | |
1598 | ||
1599 | // add at new position and fix oid, key | |
1600 | onode_map.insert(make_pair(new_oid, o)); | |
1601 | cache->_touch_onode(o); | |
1602 | o->oid = new_oid; | |
1603 | o->key = new_okey; | |
1604 | } | |
1605 | ||
1606 | bool BlueStore::OnodeSpace::map_any(std::function<bool(OnodeRef)> f) | |
1607 | { | |
1608 | std::lock_guard<std::recursive_mutex> l(cache->lock); | |
1609 | ldout(cache->cct, 20) << __func__ << dendl; | |
1610 | for (auto& i : onode_map) { | |
1611 | if (f(i.second)) { | |
1612 | return true; | |
1613 | } | |
1614 | } | |
1615 | return false; | |
1616 | } | |
1617 | ||
1618 | void BlueStore::OnodeSpace::dump(CephContext *cct, int lvl) | |
1619 | { | |
1620 | for (auto& i : onode_map) { | |
1621 | ldout(cct, lvl) << i.first << " : " << i.second << dendl; | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | // SharedBlob | |
1626 | ||
1627 | #undef dout_prefix | |
1628 | #define dout_prefix *_dout << "bluestore.sharedblob(" << this << ") " | |
1629 | ||
1630 | ostream& operator<<(ostream& out, const BlueStore::SharedBlob& sb) | |
1631 | { | |
1632 | out << "SharedBlob(" << &sb; | |
1633 | ||
1634 | if (sb.loaded) { | |
1635 | out << " loaded " << *sb.persistent; | |
1636 | } else { | |
1637 | out << " sbid 0x" << std::hex << sb.sbid_unloaded << std::dec; | |
1638 | } | |
1639 | return out << ")"; | |
1640 | } | |
1641 | ||
1642 | BlueStore::SharedBlob::SharedBlob(uint64_t i, Collection *_coll) | |
1643 | : coll(_coll), sbid_unloaded(i) | |
1644 | { | |
1645 | assert(sbid_unloaded > 0); | |
1646 | if (get_cache()) { | |
1647 | get_cache()->add_blob(); | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | BlueStore::SharedBlob::~SharedBlob() | |
1652 | { | |
1653 | if (get_cache()) { // the dummy instances have a nullptr | |
1654 | std::lock_guard<std::recursive_mutex> l(get_cache()->lock); | |
1655 | bc._clear(get_cache()); | |
1656 | get_cache()->rm_blob(); | |
1657 | } | |
1658 | if (loaded && persistent) { | |
1659 | delete persistent; | |
1660 | } | |
1661 | } | |
1662 | ||
1663 | void BlueStore::SharedBlob::put() | |
1664 | { | |
1665 | if (--nref == 0) { | |
1666 | ldout(coll->store->cct, 20) << __func__ << " " << this | |
1667 | << " removing self from set " << get_parent() | |
1668 | << dendl; | |
1669 | if (get_parent()) { | |
1670 | if (get_parent()->try_remove(this)) { | |
1671 | delete this; | |
1672 | } else { | |
1673 | ldout(coll->store->cct, 20) | |
1674 | << __func__ << " " << this << " lost race to remove myself from set" | |
1675 | << dendl; | |
1676 | } | |
1677 | } else { | |
1678 | delete this; | |
1679 | } | |
1680 | } | |
1681 | } | |
1682 | ||
1683 | void BlueStore::SharedBlob::get_ref(uint64_t offset, uint32_t length) | |
1684 | { | |
1685 | assert(persistent); | |
1686 | persistent->ref_map.get(offset, length); | |
1687 | } | |
1688 | ||
1689 | void BlueStore::SharedBlob::put_ref(uint64_t offset, uint32_t length, | |
1690 | PExtentVector *r, | |
1691 | set<SharedBlob*> *maybe_unshared) | |
1692 | { | |
1693 | assert(persistent); | |
1694 | bool maybe = false; | |
1695 | persistent->ref_map.put(offset, length, r, maybe_unshared ? &maybe : nullptr); | |
1696 | if (maybe_unshared && maybe) { | |
1697 | maybe_unshared->insert(this); | |
1698 | } | |
1699 | } | |
1700 | ||
1701 | // SharedBlobSet | |
1702 | ||
1703 | #undef dout_prefix | |
1704 | #define dout_prefix *_dout << "bluestore.sharedblobset(" << this << ") " | |
1705 | ||
1706 | void BlueStore::SharedBlobSet::dump(CephContext *cct, int lvl) | |
1707 | { | |
1708 | std::lock_guard<std::mutex> l(lock); | |
1709 | for (auto& i : sb_map) { | |
1710 | ldout(cct, lvl) << i.first << " : " << *i.second << dendl; | |
1711 | } | |
1712 | } | |
1713 | ||
1714 | // Blob | |
1715 | ||
1716 | #undef dout_prefix | |
1717 | #define dout_prefix *_dout << "bluestore.blob(" << this << ") " | |
1718 | ||
1719 | ostream& operator<<(ostream& out, const BlueStore::Blob& b) | |
1720 | { | |
1721 | out << "Blob(" << &b; | |
1722 | if (b.is_spanning()) { | |
1723 | out << " spanning " << b.id; | |
1724 | } | |
1725 | out << " " << b.get_blob() << " " << b.get_blob_use_tracker(); | |
1726 | if (b.shared_blob) { | |
1727 | out << " " << *b.shared_blob; | |
1728 | } else { | |
1729 | out << " (shared_blob=NULL)"; | |
1730 | } | |
1731 | out << ")"; | |
1732 | return out; | |
1733 | } | |
1734 | ||
1735 | void BlueStore::Blob::discard_unallocated(Collection *coll) | |
1736 | { | |
1737 | if (get_blob().is_shared()) { | |
1738 | return; | |
1739 | } | |
1740 | if (get_blob().is_compressed()) { | |
1741 | bool discard = false; | |
1742 | bool all_invalid = true; | |
1743 | for (auto e : get_blob().get_extents()) { | |
1744 | if (!e.is_valid()) { | |
1745 | discard = true; | |
1746 | } else { | |
1747 | all_invalid = false; | |
1748 | } | |
1749 | } | |
1750 | assert(discard == all_invalid); // in case of compressed blob all | |
1751 | // or none pextents are invalid. | |
1752 | if (discard) { | |
1753 | shared_blob->bc.discard(shared_blob->get_cache(), 0, | |
1754 | get_blob().get_logical_length()); | |
1755 | } | |
1756 | } else { | |
1757 | size_t pos = 0; | |
1758 | for (auto e : get_blob().get_extents()) { | |
1759 | if (!e.is_valid()) { | |
1760 | ldout(coll->store->cct, 20) << __func__ << " 0x" << std::hex << pos | |
1761 | << "~" << e.length | |
1762 | << std::dec << dendl; | |
1763 | shared_blob->bc.discard(shared_blob->get_cache(), pos, e.length); | |
1764 | } | |
1765 | pos += e.length; | |
1766 | } | |
1767 | if (get_blob().can_prune_tail()) { | |
1768 | dirty_blob().prune_tail(); | |
1769 | used_in_blob.prune_tail(get_blob().get_ondisk_length()); | |
1770 | auto cct = coll->store->cct; //used by dout | |
1771 | dout(20) << __func__ << " pruned tail, now " << get_blob() << dendl; | |
1772 | } | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | void BlueStore::Blob::get_ref( | |
1777 | Collection *coll, | |
1778 | uint32_t offset, | |
1779 | uint32_t length) | |
1780 | { | |
1781 | // Caller has to initialize Blob's logical length prior to increment | |
1782 | // references. Otherwise one is neither unable to determine required | |
1783 | // amount of counters in case of per-au tracking nor obtain min_release_size | |
1784 | // for single counter mode. | |
1785 | assert(get_blob().get_logical_length() != 0); | |
1786 | auto cct = coll->store->cct; | |
1787 | dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length | |
1788 | << std::dec << " " << *this << dendl; | |
1789 | ||
1790 | if (used_in_blob.is_empty()) { | |
1791 | uint32_t min_release_size = | |
1792 | get_blob().get_release_size(coll->store->min_alloc_size); | |
1793 | uint64_t l = get_blob().get_logical_length(); | |
1794 | dout(20) << __func__ << " init 0x" << std::hex << l << ", " | |
1795 | << min_release_size << std::dec << dendl; | |
1796 | used_in_blob.init(l, min_release_size); | |
1797 | } | |
1798 | used_in_blob.get( | |
1799 | offset, | |
1800 | length); | |
1801 | } | |
1802 | ||
1803 | bool BlueStore::Blob::put_ref( | |
1804 | Collection *coll, | |
1805 | uint32_t offset, | |
1806 | uint32_t length, | |
1807 | PExtentVector *r) | |
1808 | { | |
1809 | PExtentVector logical; | |
1810 | ||
1811 | auto cct = coll->store->cct; | |
1812 | dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length | |
1813 | << std::dec << " " << *this << dendl; | |
1814 | ||
1815 | bool empty = used_in_blob.put( | |
1816 | offset, | |
1817 | length, | |
1818 | &logical); | |
1819 | r->clear(); | |
1820 | // nothing to release | |
1821 | if (!empty && logical.empty()) { | |
1822 | return false; | |
1823 | } | |
1824 | ||
1825 | bluestore_blob_t& b = dirty_blob(); | |
1826 | return b.release_extents(empty, logical, r); | |
1827 | } | |
1828 | ||
1829 | bool BlueStore::Blob::can_reuse_blob(uint32_t min_alloc_size, | |
1830 | uint32_t target_blob_size, | |
1831 | uint32_t b_offset, | |
1832 | uint32_t *length0) { | |
1833 | assert(min_alloc_size); | |
1834 | assert(target_blob_size); | |
1835 | if (!get_blob().is_mutable()) { | |
1836 | return false; | |
1837 | } | |
1838 | ||
1839 | uint32_t length = *length0; | |
1840 | uint32_t end = b_offset + length; | |
1841 | ||
1842 | // Currently for the sake of simplicity we omit blob reuse if data is | |
1843 | // unaligned with csum chunk. Later we can perform padding if needed. | |
1844 | if (get_blob().has_csum() && | |
1845 | ((b_offset % get_blob().get_csum_chunk_size()) != 0 || | |
1846 | (end % get_blob().get_csum_chunk_size()) != 0)) { | |
1847 | return false; | |
1848 | } | |
1849 | ||
1850 | auto blen = get_blob().get_logical_length(); | |
1851 | uint32_t new_blen = blen; | |
1852 | ||
1853 | // make sure target_blob_size isn't less than current blob len | |
1854 | target_blob_size = MAX(blen, target_blob_size); | |
1855 | ||
1856 | if (b_offset >= blen) { | |
1857 | // new data totally stands out of the existing blob | |
1858 | new_blen = end; | |
1859 | } else { | |
1860 | // new data overlaps with the existing blob | |
1861 | new_blen = MAX(blen, end); | |
1862 | ||
1863 | uint32_t overlap = 0; | |
1864 | if (new_blen > blen) { | |
1865 | overlap = blen - b_offset; | |
1866 | } else { | |
1867 | overlap = length; | |
1868 | } | |
1869 | ||
1870 | if (!get_blob().is_unallocated(b_offset, overlap)) { | |
1871 | // abort if any piece of the overlap has already been allocated | |
1872 | return false; | |
1873 | } | |
1874 | } | |
1875 | ||
1876 | if (new_blen > blen) { | |
1877 | int64_t overflow = int64_t(new_blen) - target_blob_size; | |
1878 | // Unable to decrease the provided length to fit into max_blob_size | |
1879 | if (overflow >= length) { | |
1880 | return false; | |
1881 | } | |
1882 | ||
1883 | // FIXME: in some cases we could reduce unused resolution | |
1884 | if (get_blob().has_unused()) { | |
1885 | return false; | |
1886 | } | |
1887 | ||
1888 | if (overflow > 0) { | |
1889 | new_blen -= overflow; | |
1890 | length -= overflow; | |
1891 | *length0 = length; | |
1892 | } | |
1893 | ||
1894 | if (new_blen > blen) { | |
1895 | dirty_blob().add_tail(new_blen); | |
1896 | used_in_blob.add_tail(new_blen, | |
1897 | get_blob().get_release_size(min_alloc_size)); | |
1898 | } | |
1899 | } | |
1900 | return true; | |
1901 | } | |
1902 | ||
1903 | void BlueStore::Blob::split(Collection *coll, uint32_t blob_offset, Blob *r) | |
1904 | { | |
1905 | auto cct = coll->store->cct; //used by dout | |
1906 | dout(10) << __func__ << " 0x" << std::hex << blob_offset << std::dec | |
1907 | << " start " << *this << dendl; | |
1908 | assert(blob.can_split()); | |
1909 | assert(used_in_blob.can_split()); | |
1910 | bluestore_blob_t &lb = dirty_blob(); | |
1911 | bluestore_blob_t &rb = r->dirty_blob(); | |
1912 | ||
1913 | used_in_blob.split( | |
1914 | blob_offset, | |
1915 | &(r->used_in_blob)); | |
1916 | ||
1917 | lb.split(blob_offset, rb); | |
1918 | shared_blob->bc.split(shared_blob->get_cache(), blob_offset, r->shared_blob->bc); | |
1919 | ||
1920 | dout(10) << __func__ << " 0x" << std::hex << blob_offset << std::dec | |
1921 | << " finish " << *this << dendl; | |
1922 | dout(10) << __func__ << " 0x" << std::hex << blob_offset << std::dec | |
1923 | << " and " << *r << dendl; | |
1924 | } | |
1925 | ||
1926 | #ifndef CACHE_BLOB_BL | |
1927 | void BlueStore::Blob::decode( | |
1928 | Collection *coll, | |
1929 | bufferptr::iterator& p, | |
1930 | uint64_t struct_v, | |
1931 | uint64_t* sbid, | |
1932 | bool include_ref_map) | |
1933 | { | |
1934 | denc(blob, p, struct_v); | |
1935 | if (blob.is_shared()) { | |
1936 | denc(*sbid, p); | |
1937 | } | |
1938 | if (include_ref_map) { | |
1939 | if (struct_v > 1) { | |
1940 | used_in_blob.decode(p); | |
1941 | } else { | |
1942 | used_in_blob.clear(); | |
1943 | bluestore_extent_ref_map_t legacy_ref_map; | |
1944 | legacy_ref_map.decode(p); | |
1945 | for (auto r : legacy_ref_map.ref_map) { | |
1946 | get_ref( | |
1947 | coll, | |
1948 | r.first, | |
1949 | r.second.refs * r.second.length); | |
1950 | } | |
1951 | } | |
1952 | } | |
1953 | } | |
1954 | #endif | |
1955 | ||
1956 | // Extent | |
1957 | ||
1958 | ostream& operator<<(ostream& out, const BlueStore::Extent& e) | |
1959 | { | |
1960 | return out << std::hex << "0x" << e.logical_offset << "~" << e.length | |
1961 | << ": 0x" << e.blob_offset << "~" << e.length << std::dec | |
1962 | << " " << *e.blob; | |
1963 | } | |
1964 | ||
1965 | // OldExtent | |
1966 | BlueStore::OldExtent* BlueStore::OldExtent::create(CollectionRef c, | |
1967 | uint32_t lo, | |
1968 | uint32_t o, | |
1969 | uint32_t l, | |
1970 | BlobRef& b) { | |
1971 | OldExtent* oe = new OldExtent(lo, o, l, b); | |
1972 | b->put_ref(c.get(), o, l, &(oe->r)); | |
1973 | oe->blob_empty = b->get_referenced_bytes() == 0; | |
1974 | return oe; | |
1975 | } | |
1976 | ||
1977 | // ExtentMap | |
1978 | ||
1979 | #undef dout_prefix | |
1980 | #define dout_prefix *_dout << "bluestore.extentmap(" << this << ") " | |
1981 | ||
1982 | BlueStore::ExtentMap::ExtentMap(Onode *o) | |
1983 | : onode(o), | |
1984 | inline_bl( | |
1985 | o->c->store->cct->_conf->bluestore_extent_map_inline_shard_prealloc_size) { | |
1986 | } | |
1987 | ||
1988 | void BlueStore::ExtentMap::update(KeyValueDB::Transaction t, | |
1989 | bool force) | |
1990 | { | |
1991 | auto cct = onode->c->store->cct; //used by dout | |
1992 | dout(20) << __func__ << " " << onode->oid << (force ? " force" : "") << dendl; | |
1993 | if (onode->onode.extent_map_shards.empty()) { | |
1994 | if (inline_bl.length() == 0) { | |
1995 | unsigned n; | |
1996 | // we need to encode inline_bl to measure encoded length | |
1997 | bool never_happen = encode_some(0, OBJECT_MAX_SIZE, inline_bl, &n); | |
1998 | inline_bl.reassign_to_mempool(mempool::mempool_bluestore_cache_other); | |
1999 | assert(!never_happen); | |
2000 | size_t len = inline_bl.length(); | |
2001 | dout(20) << __func__ << " inline shard " << len << " bytes from " << n | |
2002 | << " extents" << dendl; | |
2003 | if (!force && len > cct->_conf->bluestore_extent_map_shard_max_size) { | |
2004 | request_reshard(0, OBJECT_MAX_SIZE); | |
2005 | return; | |
2006 | } | |
2007 | } | |
2008 | // will persist in the onode key. | |
2009 | } else { | |
2010 | // pending shard update | |
2011 | struct dirty_shard_t { | |
2012 | Shard *shard; | |
2013 | bufferlist bl; | |
2014 | dirty_shard_t(Shard *s) : shard(s) {} | |
2015 | }; | |
2016 | vector<dirty_shard_t> encoded_shards; | |
2017 | // allocate slots for all shards in a single call instead of | |
2018 | // doing multiple allocations - one per each dirty shard | |
2019 | encoded_shards.reserve(shards.size()); | |
2020 | ||
2021 | auto p = shards.begin(); | |
2022 | auto prev_p = p; | |
2023 | while (p != shards.end()) { | |
2024 | assert(p->shard_info->offset >= prev_p->shard_info->offset); | |
2025 | auto n = p; | |
2026 | ++n; | |
2027 | if (p->dirty) { | |
2028 | uint32_t endoff; | |
2029 | if (n == shards.end()) { | |
2030 | endoff = OBJECT_MAX_SIZE; | |
2031 | } else { | |
2032 | endoff = n->shard_info->offset; | |
2033 | } | |
2034 | encoded_shards.emplace_back(dirty_shard_t(&(*p))); | |
2035 | bufferlist& bl = encoded_shards.back().bl; | |
2036 | if (encode_some(p->shard_info->offset, endoff - p->shard_info->offset, | |
2037 | bl, &p->extents)) { | |
2038 | if (force) { | |
2039 | derr << __func__ << " encode_some needs reshard" << dendl; | |
2040 | assert(!force); | |
2041 | } | |
2042 | } | |
2043 | size_t len = bl.length(); | |
2044 | ||
2045 | dout(20) << __func__ << " shard 0x" << std::hex | |
2046 | << p->shard_info->offset << std::dec << " is " << len | |
2047 | << " bytes (was " << p->shard_info->bytes << ") from " | |
2048 | << p->extents << " extents" << dendl; | |
2049 | ||
2050 | if (!force) { | |
2051 | if (len > cct->_conf->bluestore_extent_map_shard_max_size) { | |
2052 | // we are big; reshard ourselves | |
2053 | request_reshard(p->shard_info->offset, endoff); | |
2054 | } | |
2055 | // avoid resharding the trailing shard, even if it is small | |
2056 | else if (n != shards.end() && | |
2057 | len < g_conf->bluestore_extent_map_shard_min_size) { | |
2058 | assert(endoff != OBJECT_MAX_SIZE); | |
2059 | if (p == shards.begin()) { | |
2060 | // we are the first shard, combine with next shard | |
2061 | request_reshard(p->shard_info->offset, endoff + 1); | |
2062 | } else { | |
2063 | // combine either with the previous shard or the next, | |
2064 | // whichever is smaller | |
2065 | if (prev_p->shard_info->bytes > n->shard_info->bytes) { | |
2066 | request_reshard(p->shard_info->offset, endoff + 1); | |
2067 | } else { | |
2068 | request_reshard(prev_p->shard_info->offset, endoff); | |
2069 | } | |
2070 | } | |
2071 | } | |
2072 | } | |
2073 | } | |
2074 | prev_p = p; | |
2075 | p = n; | |
2076 | } | |
2077 | if (needs_reshard()) { | |
2078 | return; | |
2079 | } | |
2080 | ||
2081 | // schedule DB update for dirty shards | |
2082 | string key; | |
2083 | for (auto& it : encoded_shards) { | |
2084 | it.shard->dirty = false; | |
2085 | it.shard->shard_info->bytes = it.bl.length(); | |
2086 | generate_extent_shard_key_and_apply( | |
2087 | onode->key, | |
2088 | it.shard->shard_info->offset, | |
2089 | &key, | |
2090 | [&](const string& final_key) { | |
2091 | t->set(PREFIX_OBJ, final_key, it.bl); | |
2092 | } | |
2093 | ); | |
2094 | } | |
2095 | } | |
2096 | } | |
2097 | ||
2098 | bid_t BlueStore::ExtentMap::allocate_spanning_blob_id() | |
2099 | { | |
2100 | if (spanning_blob_map.empty()) | |
2101 | return 0; | |
2102 | bid_t bid = spanning_blob_map.rbegin()->first + 1; | |
2103 | // bid is valid and available. | |
2104 | if (bid >= 0) | |
2105 | return bid; | |
2106 | // Find next unused bid; | |
2107 | bid = rand() % (numeric_limits<bid_t>::max() + 1); | |
2108 | const auto begin_bid = bid; | |
2109 | do { | |
2110 | if (!spanning_blob_map.count(bid)) | |
2111 | return bid; | |
2112 | else { | |
2113 | bid++; | |
2114 | if (bid < 0) bid = 0; | |
2115 | } | |
2116 | } while (bid != begin_bid); | |
2117 | assert(0 == "no available blob id"); | |
2118 | } | |
2119 | ||
2120 | void BlueStore::ExtentMap::reshard( | |
2121 | KeyValueDB *db, | |
2122 | KeyValueDB::Transaction t) | |
2123 | { | |
2124 | auto cct = onode->c->store->cct; // used by dout | |
2125 | ||
2126 | dout(10) << __func__ << " 0x[" << std::hex << needs_reshard_begin << "," | |
2127 | << needs_reshard_end << ")" << std::dec | |
2128 | << " of " << onode->onode.extent_map_shards.size() | |
2129 | << " shards on " << onode->oid << dendl; | |
2130 | for (auto& p : spanning_blob_map) { | |
2131 | dout(20) << __func__ << " spanning blob " << p.first << " " << *p.second | |
2132 | << dendl; | |
2133 | } | |
2134 | // determine shard index range | |
2135 | unsigned si_begin = 0, si_end = 0; | |
2136 | if (!shards.empty()) { | |
2137 | while (si_begin + 1 < shards.size() && | |
2138 | shards[si_begin + 1].shard_info->offset <= needs_reshard_begin) { | |
2139 | ++si_begin; | |
2140 | } | |
2141 | needs_reshard_begin = shards[si_begin].shard_info->offset; | |
2142 | for (si_end = si_begin; si_end < shards.size(); ++si_end) { | |
2143 | if (shards[si_end].shard_info->offset >= needs_reshard_end) { | |
2144 | needs_reshard_end = shards[si_end].shard_info->offset; | |
2145 | break; | |
2146 | } | |
2147 | } | |
2148 | if (si_end == shards.size()) { | |
2149 | needs_reshard_end = OBJECT_MAX_SIZE; | |
2150 | } | |
2151 | dout(20) << __func__ << " shards [" << si_begin << "," << si_end << ")" | |
2152 | << " over 0x[" << std::hex << needs_reshard_begin << "," | |
2153 | << needs_reshard_end << ")" << std::dec << dendl; | |
2154 | } | |
2155 | ||
2156 | fault_range(db, needs_reshard_begin, (needs_reshard_end - needs_reshard_begin)); | |
2157 | ||
2158 | // we may need to fault in a larger interval later must have all | |
2159 | // referring extents for spanning blobs loaded in order to have | |
2160 | // accurate use_tracker values. | |
2161 | uint32_t spanning_scan_begin = needs_reshard_begin; | |
2162 | uint32_t spanning_scan_end = needs_reshard_end; | |
2163 | ||
2164 | // remove old keys | |
2165 | string key; | |
2166 | for (unsigned i = si_begin; i < si_end; ++i) { | |
2167 | generate_extent_shard_key_and_apply( | |
2168 | onode->key, shards[i].shard_info->offset, &key, | |
2169 | [&](const string& final_key) { | |
2170 | t->rmkey(PREFIX_OBJ, final_key); | |
2171 | } | |
2172 | ); | |
2173 | } | |
2174 | ||
2175 | // calculate average extent size | |
2176 | unsigned bytes = 0; | |
2177 | unsigned extents = 0; | |
2178 | if (onode->onode.extent_map_shards.empty()) { | |
2179 | bytes = inline_bl.length(); | |
2180 | extents = extent_map.size(); | |
2181 | } else { | |
2182 | for (unsigned i = si_begin; i < si_end; ++i) { | |
2183 | bytes += shards[i].shard_info->bytes; | |
2184 | extents += shards[i].extents; | |
2185 | } | |
2186 | } | |
2187 | unsigned target = cct->_conf->bluestore_extent_map_shard_target_size; | |
2188 | unsigned slop = target * | |
2189 | cct->_conf->bluestore_extent_map_shard_target_size_slop; | |
2190 | unsigned extent_avg = bytes / MAX(1, extents); | |
2191 | dout(20) << __func__ << " extent_avg " << extent_avg << ", target " << target | |
2192 | << ", slop " << slop << dendl; | |
2193 | ||
2194 | // reshard | |
2195 | unsigned estimate = 0; | |
2196 | unsigned offset = needs_reshard_begin; | |
2197 | vector<bluestore_onode_t::shard_info> new_shard_info; | |
2198 | unsigned max_blob_end = 0; | |
2199 | Extent dummy(needs_reshard_begin); | |
2200 | for (auto e = extent_map.lower_bound(dummy); | |
2201 | e != extent_map.end(); | |
2202 | ++e) { | |
2203 | if (e->logical_offset >= needs_reshard_end) { | |
2204 | break; | |
2205 | } | |
2206 | dout(30) << " extent " << *e << dendl; | |
2207 | ||
2208 | // disfavor shard boundaries that span a blob | |
2209 | bool would_span = (e->logical_offset < max_blob_end) || e->blob_offset; | |
2210 | if (estimate && | |
2211 | estimate + extent_avg > target + (would_span ? slop : 0)) { | |
2212 | // new shard | |
2213 | if (offset == needs_reshard_begin) { | |
2214 | new_shard_info.emplace_back(bluestore_onode_t::shard_info()); | |
2215 | new_shard_info.back().offset = offset; | |
2216 | dout(20) << __func__ << " new shard 0x" << std::hex << offset | |
2217 | << std::dec << dendl; | |
2218 | } | |
2219 | offset = e->logical_offset; | |
2220 | new_shard_info.emplace_back(bluestore_onode_t::shard_info()); | |
2221 | new_shard_info.back().offset = offset; | |
2222 | dout(20) << __func__ << " new shard 0x" << std::hex << offset | |
2223 | << std::dec << dendl; | |
2224 | estimate = 0; | |
2225 | } | |
2226 | estimate += extent_avg; | |
2227 | unsigned bs = e->blob_start(); | |
2228 | if (bs < spanning_scan_begin) { | |
2229 | spanning_scan_begin = bs; | |
2230 | } | |
2231 | uint32_t be = e->blob_end(); | |
2232 | if (be > max_blob_end) { | |
2233 | max_blob_end = be; | |
2234 | } | |
2235 | if (be > spanning_scan_end) { | |
2236 | spanning_scan_end = be; | |
2237 | } | |
2238 | } | |
2239 | if (new_shard_info.empty() && (si_begin > 0 || | |
2240 | si_end < shards.size())) { | |
2241 | // we resharded a partial range; we must produce at least one output | |
2242 | // shard | |
2243 | new_shard_info.emplace_back(bluestore_onode_t::shard_info()); | |
2244 | new_shard_info.back().offset = needs_reshard_begin; | |
2245 | dout(20) << __func__ << " new shard 0x" << std::hex << needs_reshard_begin | |
2246 | << std::dec << " (singleton degenerate case)" << dendl; | |
2247 | } | |
2248 | ||
2249 | auto& sv = onode->onode.extent_map_shards; | |
2250 | dout(20) << __func__ << " new " << new_shard_info << dendl; | |
2251 | dout(20) << __func__ << " old " << sv << dendl; | |
2252 | if (sv.empty()) { | |
2253 | // no old shards to keep | |
2254 | sv.swap(new_shard_info); | |
2255 | init_shards(true, true); | |
2256 | } else { | |
2257 | // splice in new shards | |
2258 | sv.erase(sv.begin() + si_begin, sv.begin() + si_end); | |
2259 | shards.erase(shards.begin() + si_begin, shards.begin() + si_end); | |
2260 | sv.insert( | |
2261 | sv.begin() + si_begin, | |
2262 | new_shard_info.begin(), | |
2263 | new_shard_info.end()); | |
2264 | shards.insert(shards.begin() + si_begin, new_shard_info.size(), Shard()); | |
2265 | si_end = si_begin + new_shard_info.size(); | |
2266 | ||
2267 | assert(sv.size() == shards.size()); | |
2268 | ||
2269 | // note that we need to update every shard_info of shards here, | |
2270 | // as sv might have been totally re-allocated above | |
2271 | for (unsigned i = 0; i < shards.size(); i++) { | |
2272 | shards[i].shard_info = &sv[i]; | |
2273 | } | |
2274 | ||
2275 | // mark newly added shards as dirty | |
2276 | for (unsigned i = si_begin; i < si_end; ++i) { | |
2277 | shards[i].loaded = true; | |
2278 | shards[i].dirty = true; | |
2279 | } | |
2280 | } | |
2281 | dout(20) << __func__ << " fin " << sv << dendl; | |
2282 | inline_bl.clear(); | |
2283 | ||
2284 | if (sv.empty()) { | |
2285 | // no more shards; unspan all previously spanning blobs | |
2286 | auto p = spanning_blob_map.begin(); | |
2287 | while (p != spanning_blob_map.end()) { | |
2288 | p->second->id = -1; | |
2289 | dout(30) << __func__ << " un-spanning " << *p->second << dendl; | |
2290 | p = spanning_blob_map.erase(p); | |
2291 | } | |
2292 | } else { | |
2293 | // identify new spanning blobs | |
2294 | dout(20) << __func__ << " checking spanning blobs 0x[" << std::hex | |
2295 | << spanning_scan_begin << "," << spanning_scan_end << ")" << dendl; | |
2296 | if (spanning_scan_begin < needs_reshard_begin) { | |
2297 | fault_range(db, spanning_scan_begin, | |
2298 | needs_reshard_begin - spanning_scan_begin); | |
2299 | } | |
2300 | if (spanning_scan_end > needs_reshard_end) { | |
2301 | fault_range(db, needs_reshard_end, | |
2302 | spanning_scan_end - needs_reshard_end); | |
2303 | } | |
2304 | auto sp = sv.begin() + si_begin; | |
2305 | auto esp = sv.end(); | |
2306 | unsigned shard_start = sp->offset; | |
2307 | unsigned shard_end; | |
2308 | ++sp; | |
2309 | if (sp == esp) { | |
2310 | shard_end = OBJECT_MAX_SIZE; | |
2311 | } else { | |
2312 | shard_end = sp->offset; | |
2313 | } | |
2314 | Extent dummy(needs_reshard_begin); | |
2315 | for (auto e = extent_map.lower_bound(dummy); e != extent_map.end(); ++e) { | |
2316 | if (e->logical_offset >= needs_reshard_end) { | |
2317 | break; | |
2318 | } | |
2319 | dout(30) << " extent " << *e << dendl; | |
2320 | while (e->logical_offset >= shard_end) { | |
2321 | shard_start = shard_end; | |
2322 | assert(sp != esp); | |
2323 | ++sp; | |
2324 | if (sp == esp) { | |
2325 | shard_end = OBJECT_MAX_SIZE; | |
2326 | } else { | |
2327 | shard_end = sp->offset; | |
2328 | } | |
2329 | dout(30) << __func__ << " shard 0x" << std::hex << shard_start | |
2330 | << " to 0x" << shard_end << std::dec << dendl; | |
2331 | } | |
2332 | if (e->blob_escapes_range(shard_start, shard_end - shard_start)) { | |
2333 | if (!e->blob->is_spanning()) { | |
2334 | // We have two options: (1) split the blob into pieces at the | |
2335 | // shard boundaries (and adjust extents accordingly), or (2) | |
2336 | // mark it spanning. We prefer to cut the blob if we can. Note that | |
2337 | // we may have to split it multiple times--potentially at every | |
2338 | // shard boundary. | |
2339 | bool must_span = false; | |
2340 | BlobRef b = e->blob; | |
2341 | if (b->can_split()) { | |
2342 | uint32_t bstart = e->blob_start(); | |
2343 | uint32_t bend = e->blob_end(); | |
2344 | for (const auto& sh : shards) { | |
2345 | if (bstart < sh.shard_info->offset && | |
2346 | bend > sh.shard_info->offset) { | |
2347 | uint32_t blob_offset = sh.shard_info->offset - bstart; | |
2348 | if (b->can_split_at(blob_offset)) { | |
2349 | dout(20) << __func__ << " splitting blob, bstart 0x" | |
2350 | << std::hex << bstart << " blob_offset 0x" | |
2351 | << blob_offset << std::dec << " " << *b << dendl; | |
2352 | b = split_blob(b, blob_offset, sh.shard_info->offset); | |
2353 | // switch b to the new right-hand side, in case it | |
2354 | // *also* has to get split. | |
2355 | bstart += blob_offset; | |
2356 | onode->c->store->logger->inc(l_bluestore_blob_split); | |
2357 | } else { | |
2358 | must_span = true; | |
2359 | break; | |
2360 | } | |
2361 | } | |
2362 | } | |
2363 | } else { | |
2364 | must_span = true; | |
2365 | } | |
2366 | if (must_span) { | |
2367 | auto bid = allocate_spanning_blob_id(); | |
2368 | b->id = bid; | |
2369 | spanning_blob_map[b->id] = b; | |
2370 | dout(20) << __func__ << " adding spanning " << *b << dendl; | |
2371 | } | |
2372 | } | |
2373 | } else { | |
2374 | if (e->blob->is_spanning()) { | |
2375 | spanning_blob_map.erase(e->blob->id); | |
2376 | e->blob->id = -1; | |
2377 | dout(30) << __func__ << " un-spanning " << *e->blob << dendl; | |
2378 | } | |
2379 | } | |
2380 | } | |
2381 | } | |
2382 | ||
2383 | clear_needs_reshard(); | |
2384 | } | |
2385 | ||
2386 | bool BlueStore::ExtentMap::encode_some( | |
2387 | uint32_t offset, | |
2388 | uint32_t length, | |
2389 | bufferlist& bl, | |
2390 | unsigned *pn) | |
2391 | { | |
2392 | auto cct = onode->c->store->cct; //used by dout | |
2393 | Extent dummy(offset); | |
2394 | auto start = extent_map.lower_bound(dummy); | |
2395 | uint32_t end = offset + length; | |
2396 | ||
2397 | __u8 struct_v = 2; // Version 2 differs from v1 in blob's ref_map | |
2398 | // serialization only. Hence there is no specific | |
2399 | // handling at ExtentMap level. | |
2400 | ||
2401 | unsigned n = 0; | |
2402 | size_t bound = 0; | |
2403 | bool must_reshard = false; | |
2404 | for (auto p = start; | |
2405 | p != extent_map.end() && p->logical_offset < end; | |
2406 | ++p, ++n) { | |
2407 | assert(p->logical_offset >= offset); | |
2408 | p->blob->last_encoded_id = -1; | |
2409 | if (!p->blob->is_spanning() && p->blob_escapes_range(offset, length)) { | |
2410 | dout(30) << __func__ << " 0x" << std::hex << offset << "~" << length | |
2411 | << std::dec << " hit new spanning blob " << *p << dendl; | |
2412 | request_reshard(p->blob_start(), p->blob_end()); | |
2413 | must_reshard = true; | |
2414 | } | |
2415 | if (!must_reshard) { | |
2416 | denc_varint(0, bound); // blobid | |
2417 | denc_varint(0, bound); // logical_offset | |
2418 | denc_varint(0, bound); // len | |
2419 | denc_varint(0, bound); // blob_offset | |
2420 | ||
2421 | p->blob->bound_encode( | |
2422 | bound, | |
2423 | struct_v, | |
2424 | p->blob->shared_blob->get_sbid(), | |
2425 | false); | |
2426 | } | |
2427 | } | |
2428 | if (must_reshard) { | |
2429 | return true; | |
2430 | } | |
2431 | ||
2432 | denc(struct_v, bound); | |
2433 | denc_varint(0, bound); // number of extents | |
2434 | ||
2435 | { | |
2436 | auto app = bl.get_contiguous_appender(bound); | |
2437 | denc(struct_v, app); | |
2438 | denc_varint(n, app); | |
2439 | if (pn) { | |
2440 | *pn = n; | |
2441 | } | |
2442 | ||
2443 | n = 0; | |
2444 | uint64_t pos = 0; | |
2445 | uint64_t prev_len = 0; | |
2446 | for (auto p = start; | |
2447 | p != extent_map.end() && p->logical_offset < end; | |
2448 | ++p, ++n) { | |
2449 | unsigned blobid; | |
2450 | bool include_blob = false; | |
2451 | if (p->blob->is_spanning()) { | |
2452 | blobid = p->blob->id << BLOBID_SHIFT_BITS; | |
2453 | blobid |= BLOBID_FLAG_SPANNING; | |
2454 | } else if (p->blob->last_encoded_id < 0) { | |
2455 | p->blob->last_encoded_id = n + 1; // so it is always non-zero | |
2456 | include_blob = true; | |
2457 | blobid = 0; // the decoder will infer the id from n | |
2458 | } else { | |
2459 | blobid = p->blob->last_encoded_id << BLOBID_SHIFT_BITS; | |
2460 | } | |
2461 | if (p->logical_offset == pos) { | |
2462 | blobid |= BLOBID_FLAG_CONTIGUOUS; | |
2463 | } | |
2464 | if (p->blob_offset == 0) { | |
2465 | blobid |= BLOBID_FLAG_ZEROOFFSET; | |
2466 | } | |
2467 | if (p->length == prev_len) { | |
2468 | blobid |= BLOBID_FLAG_SAMELENGTH; | |
2469 | } else { | |
2470 | prev_len = p->length; | |
2471 | } | |
2472 | denc_varint(blobid, app); | |
2473 | if ((blobid & BLOBID_FLAG_CONTIGUOUS) == 0) { | |
2474 | denc_varint_lowz(p->logical_offset - pos, app); | |
2475 | } | |
2476 | if ((blobid & BLOBID_FLAG_ZEROOFFSET) == 0) { | |
2477 | denc_varint_lowz(p->blob_offset, app); | |
2478 | } | |
2479 | if ((blobid & BLOBID_FLAG_SAMELENGTH) == 0) { | |
2480 | denc_varint_lowz(p->length, app); | |
2481 | } | |
2482 | pos = p->logical_end(); | |
2483 | if (include_blob) { | |
2484 | p->blob->encode(app, struct_v, p->blob->shared_blob->get_sbid(), false); | |
2485 | } | |
2486 | } | |
2487 | } | |
2488 | /*derr << __func__ << bl << dendl; | |
2489 | derr << __func__ << ":"; | |
2490 | bl.hexdump(*_dout); | |
2491 | *_dout << dendl; | |
2492 | */ | |
2493 | return false; | |
2494 | } | |
2495 | ||
2496 | unsigned BlueStore::ExtentMap::decode_some(bufferlist& bl) | |
2497 | { | |
2498 | auto cct = onode->c->store->cct; //used by dout | |
2499 | /* | |
2500 | derr << __func__ << ":"; | |
2501 | bl.hexdump(*_dout); | |
2502 | *_dout << dendl; | |
2503 | */ | |
2504 | ||
2505 | assert(bl.get_num_buffers() <= 1); | |
2506 | auto p = bl.front().begin_deep(); | |
2507 | __u8 struct_v; | |
2508 | denc(struct_v, p); | |
2509 | // Version 2 differs from v1 in blob's ref_map | |
2510 | // serialization only. Hence there is no specific | |
2511 | // handling at ExtentMap level below. | |
2512 | assert(struct_v == 1 || struct_v == 2); | |
2513 | ||
2514 | uint32_t num; | |
2515 | denc_varint(num, p); | |
2516 | vector<BlobRef> blobs(num); | |
2517 | uint64_t pos = 0; | |
2518 | uint64_t prev_len = 0; | |
2519 | unsigned n = 0; | |
2520 | ||
2521 | while (!p.end()) { | |
2522 | Extent *le = new Extent(); | |
2523 | uint64_t blobid; | |
2524 | denc_varint(blobid, p); | |
2525 | if ((blobid & BLOBID_FLAG_CONTIGUOUS) == 0) { | |
2526 | uint64_t gap; | |
2527 | denc_varint_lowz(gap, p); | |
2528 | pos += gap; | |
2529 | } | |
2530 | le->logical_offset = pos; | |
2531 | if ((blobid & BLOBID_FLAG_ZEROOFFSET) == 0) { | |
2532 | denc_varint_lowz(le->blob_offset, p); | |
2533 | } else { | |
2534 | le->blob_offset = 0; | |
2535 | } | |
2536 | if ((blobid & BLOBID_FLAG_SAMELENGTH) == 0) { | |
2537 | denc_varint_lowz(prev_len, p); | |
2538 | } | |
2539 | le->length = prev_len; | |
2540 | ||
2541 | if (blobid & BLOBID_FLAG_SPANNING) { | |
2542 | dout(30) << __func__ << " getting spanning blob " | |
2543 | << (blobid >> BLOBID_SHIFT_BITS) << dendl; | |
2544 | le->assign_blob(get_spanning_blob(blobid >> BLOBID_SHIFT_BITS)); | |
2545 | } else { | |
2546 | blobid >>= BLOBID_SHIFT_BITS; | |
2547 | if (blobid) { | |
2548 | le->assign_blob(blobs[blobid - 1]); | |
2549 | assert(le->blob); | |
2550 | } else { | |
2551 | Blob *b = new Blob(); | |
2552 | uint64_t sbid = 0; | |
2553 | b->decode(onode->c, p, struct_v, &sbid, false); | |
2554 | blobs[n] = b; | |
2555 | onode->c->open_shared_blob(sbid, b); | |
2556 | le->assign_blob(b); | |
2557 | } | |
2558 | // we build ref_map dynamically for non-spanning blobs | |
2559 | le->blob->get_ref( | |
2560 | onode->c, | |
2561 | le->blob_offset, | |
2562 | le->length); | |
2563 | } | |
2564 | pos += prev_len; | |
2565 | ++n; | |
2566 | extent_map.insert(*le); | |
2567 | } | |
2568 | ||
2569 | assert(n == num); | |
2570 | return num; | |
2571 | } | |
2572 | ||
2573 | void BlueStore::ExtentMap::bound_encode_spanning_blobs(size_t& p) | |
2574 | { | |
2575 | // Version 2 differs from v1 in blob's ref_map | |
2576 | // serialization only. Hence there is no specific | |
2577 | // handling at ExtentMap level. | |
2578 | __u8 struct_v = 2; | |
2579 | ||
2580 | denc(struct_v, p); | |
2581 | denc_varint((uint32_t)0, p); | |
2582 | size_t key_size = 0; | |
2583 | denc_varint((uint32_t)0, key_size); | |
2584 | p += spanning_blob_map.size() * key_size; | |
2585 | for (const auto& i : spanning_blob_map) { | |
2586 | i.second->bound_encode(p, struct_v, i.second->shared_blob->get_sbid(), true); | |
2587 | } | |
2588 | } | |
2589 | ||
2590 | void BlueStore::ExtentMap::encode_spanning_blobs( | |
2591 | bufferlist::contiguous_appender& p) | |
2592 | { | |
2593 | // Version 2 differs from v1 in blob's ref_map | |
2594 | // serialization only. Hence there is no specific | |
2595 | // handling at ExtentMap level. | |
2596 | __u8 struct_v = 2; | |
2597 | ||
2598 | denc(struct_v, p); | |
2599 | denc_varint(spanning_blob_map.size(), p); | |
2600 | for (auto& i : spanning_blob_map) { | |
2601 | denc_varint(i.second->id, p); | |
2602 | i.second->encode(p, struct_v, i.second->shared_blob->get_sbid(), true); | |
2603 | } | |
2604 | } | |
2605 | ||
2606 | void BlueStore::ExtentMap::decode_spanning_blobs( | |
2607 | bufferptr::iterator& p) | |
2608 | { | |
2609 | __u8 struct_v; | |
2610 | denc(struct_v, p); | |
2611 | // Version 2 differs from v1 in blob's ref_map | |
2612 | // serialization only. Hence there is no specific | |
2613 | // handling at ExtentMap level. | |
2614 | assert(struct_v == 1 || struct_v == 2); | |
2615 | ||
2616 | unsigned n; | |
2617 | denc_varint(n, p); | |
2618 | while (n--) { | |
2619 | BlobRef b(new Blob()); | |
2620 | denc_varint(b->id, p); | |
2621 | spanning_blob_map[b->id] = b; | |
2622 | uint64_t sbid = 0; | |
2623 | b->decode(onode->c, p, struct_v, &sbid, true); | |
2624 | onode->c->open_shared_blob(sbid, b); | |
2625 | } | |
2626 | } | |
2627 | ||
2628 | void BlueStore::ExtentMap::init_shards(bool loaded, bool dirty) | |
2629 | { | |
2630 | shards.resize(onode->onode.extent_map_shards.size()); | |
2631 | unsigned i = 0; | |
2632 | for (auto &s : onode->onode.extent_map_shards) { | |
2633 | shards[i].shard_info = &s; | |
2634 | shards[i].loaded = loaded; | |
2635 | shards[i].dirty = dirty; | |
2636 | ++i; | |
2637 | } | |
2638 | } | |
2639 | ||
2640 | void BlueStore::ExtentMap::fault_range( | |
2641 | KeyValueDB *db, | |
2642 | uint32_t offset, | |
2643 | uint32_t length) | |
2644 | { | |
2645 | auto cct = onode->c->store->cct; //used by dout | |
2646 | dout(30) << __func__ << " 0x" << std::hex << offset << "~" << length | |
2647 | << std::dec << dendl; | |
2648 | auto start = seek_shard(offset); | |
2649 | auto last = seek_shard(offset + length); | |
2650 | ||
2651 | if (start < 0) | |
2652 | return; | |
2653 | ||
2654 | assert(last >= start); | |
2655 | string key; | |
2656 | while (start <= last) { | |
2657 | assert((size_t)start < shards.size()); | |
2658 | auto p = &shards[start]; | |
2659 | if (!p->loaded) { | |
2660 | dout(30) << __func__ << " opening shard 0x" << std::hex | |
2661 | << p->shard_info->offset << std::dec << dendl; | |
2662 | bufferlist v; | |
2663 | generate_extent_shard_key_and_apply( | |
2664 | onode->key, p->shard_info->offset, &key, | |
2665 | [&](const string& final_key) { | |
2666 | int r = db->get(PREFIX_OBJ, final_key, &v); | |
2667 | if (r < 0) { | |
2668 | derr << __func__ << " missing shard 0x" << std::hex | |
2669 | << p->shard_info->offset << std::dec << " for " << onode->oid | |
2670 | << dendl; | |
2671 | assert(r >= 0); | |
2672 | } | |
2673 | } | |
2674 | ); | |
2675 | p->extents = decode_some(v); | |
2676 | p->loaded = true; | |
2677 | dout(20) << __func__ << " open shard 0x" << std::hex | |
2678 | << p->shard_info->offset << std::dec | |
2679 | << " (" << v.length() << " bytes)" << dendl; | |
2680 | assert(p->dirty == false); | |
2681 | assert(v.length() == p->shard_info->bytes); | |
2682 | onode->c->store->logger->inc(l_bluestore_onode_shard_misses); | |
2683 | } else { | |
2684 | onode->c->store->logger->inc(l_bluestore_onode_shard_hits); | |
2685 | } | |
2686 | ++start; | |
2687 | } | |
2688 | } | |
2689 | ||
2690 | void BlueStore::ExtentMap::dirty_range( | |
2691 | uint32_t offset, | |
2692 | uint32_t length) | |
2693 | { | |
2694 | auto cct = onode->c->store->cct; //used by dout | |
2695 | dout(30) << __func__ << " 0x" << std::hex << offset << "~" << length | |
2696 | << std::dec << dendl; | |
2697 | if (shards.empty()) { | |
2698 | dout(20) << __func__ << " mark inline shard dirty" << dendl; | |
2699 | inline_bl.clear(); | |
2700 | return; | |
2701 | } | |
2702 | auto start = seek_shard(offset); | |
2703 | auto last = seek_shard(offset + length); | |
2704 | if (start < 0) | |
2705 | return; | |
2706 | ||
2707 | assert(last >= start); | |
2708 | while (start <= last) { | |
2709 | assert((size_t)start < shards.size()); | |
2710 | auto p = &shards[start]; | |
2711 | if (!p->loaded) { | |
2712 | dout(20) << __func__ << " shard 0x" << std::hex << p->shard_info->offset | |
2713 | << std::dec << " is not loaded, can't mark dirty" << dendl; | |
2714 | assert(0 == "can't mark unloaded shard dirty"); | |
2715 | } | |
2716 | if (!p->dirty) { | |
2717 | dout(20) << __func__ << " mark shard 0x" << std::hex | |
2718 | << p->shard_info->offset << std::dec << " dirty" << dendl; | |
2719 | p->dirty = true; | |
2720 | } | |
2721 | ++start; | |
2722 | } | |
2723 | } | |
2724 | ||
2725 | BlueStore::extent_map_t::iterator BlueStore::ExtentMap::find( | |
2726 | uint64_t offset) | |
2727 | { | |
2728 | Extent dummy(offset); | |
2729 | return extent_map.find(dummy); | |
2730 | } | |
2731 | ||
2732 | BlueStore::extent_map_t::iterator BlueStore::ExtentMap::seek_lextent( | |
2733 | uint64_t offset) | |
2734 | { | |
2735 | Extent dummy(offset); | |
2736 | auto fp = extent_map.lower_bound(dummy); | |
2737 | if (fp != extent_map.begin()) { | |
2738 | --fp; | |
2739 | if (fp->logical_end() <= offset) { | |
2740 | ++fp; | |
2741 | } | |
2742 | } | |
2743 | return fp; | |
2744 | } | |
2745 | ||
2746 | BlueStore::extent_map_t::const_iterator BlueStore::ExtentMap::seek_lextent( | |
2747 | uint64_t offset) const | |
2748 | { | |
2749 | Extent dummy(offset); | |
2750 | auto fp = extent_map.lower_bound(dummy); | |
2751 | if (fp != extent_map.begin()) { | |
2752 | --fp; | |
2753 | if (fp->logical_end() <= offset) { | |
2754 | ++fp; | |
2755 | } | |
2756 | } | |
2757 | return fp; | |
2758 | } | |
2759 | ||
2760 | bool BlueStore::ExtentMap::has_any_lextents(uint64_t offset, uint64_t length) | |
2761 | { | |
2762 | auto fp = seek_lextent(offset); | |
2763 | if (fp == extent_map.end() || fp->logical_offset >= offset + length) { | |
2764 | return false; | |
2765 | } | |
2766 | return true; | |
2767 | } | |
2768 | ||
2769 | int BlueStore::ExtentMap::compress_extent_map( | |
2770 | uint64_t offset, | |
2771 | uint64_t length) | |
2772 | { | |
2773 | auto cct = onode->c->store->cct; //used by dout | |
2774 | if (extent_map.empty()) | |
2775 | return 0; | |
2776 | int removed = 0; | |
2777 | auto p = seek_lextent(offset); | |
2778 | if (p != extent_map.begin()) { | |
2779 | --p; // start to the left of offset | |
2780 | } | |
2781 | // the caller should have just written to this region | |
2782 | assert(p != extent_map.end()); | |
2783 | ||
2784 | // identify the *next* shard | |
2785 | auto pshard = shards.begin(); | |
2786 | while (pshard != shards.end() && | |
2787 | p->logical_offset >= pshard->shard_info->offset) { | |
2788 | ++pshard; | |
2789 | } | |
2790 | uint64_t shard_end; | |
2791 | if (pshard != shards.end()) { | |
2792 | shard_end = pshard->shard_info->offset; | |
2793 | } else { | |
2794 | shard_end = OBJECT_MAX_SIZE; | |
2795 | } | |
2796 | ||
2797 | auto n = p; | |
2798 | for (++n; n != extent_map.end(); p = n++) { | |
2799 | if (n->logical_offset > offset + length) { | |
2800 | break; // stop after end | |
2801 | } | |
2802 | while (n != extent_map.end() && | |
2803 | p->logical_end() == n->logical_offset && | |
2804 | p->blob == n->blob && | |
2805 | p->blob_offset + p->length == n->blob_offset && | |
2806 | n->logical_offset < shard_end) { | |
2807 | dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length | |
2808 | << " next shard 0x" << shard_end << std::dec | |
2809 | << " merging " << *p << " and " << *n << dendl; | |
2810 | p->length += n->length; | |
2811 | rm(n++); | |
2812 | ++removed; | |
2813 | } | |
2814 | if (n == extent_map.end()) { | |
2815 | break; | |
2816 | } | |
2817 | if (n->logical_offset >= shard_end) { | |
2818 | assert(pshard != shards.end()); | |
2819 | ++pshard; | |
2820 | if (pshard != shards.end()) { | |
2821 | shard_end = pshard->shard_info->offset; | |
2822 | } else { | |
2823 | shard_end = OBJECT_MAX_SIZE; | |
2824 | } | |
2825 | } | |
2826 | } | |
2827 | if (removed && onode) { | |
2828 | onode->c->store->logger->inc(l_bluestore_extent_compress, removed); | |
2829 | } | |
2830 | return removed; | |
2831 | } | |
2832 | ||
2833 | void BlueStore::ExtentMap::punch_hole( | |
2834 | CollectionRef &c, | |
2835 | uint64_t offset, | |
2836 | uint64_t length, | |
2837 | old_extent_map_t *old_extents) | |
2838 | { | |
2839 | auto p = seek_lextent(offset); | |
2840 | uint64_t end = offset + length; | |
2841 | while (p != extent_map.end()) { | |
2842 | if (p->logical_offset >= end) { | |
2843 | break; | |
2844 | } | |
2845 | if (p->logical_offset < offset) { | |
2846 | if (p->logical_end() > end) { | |
2847 | // split and deref middle | |
2848 | uint64_t front = offset - p->logical_offset; | |
2849 | OldExtent* oe = OldExtent::create(c, offset, p->blob_offset + front, | |
2850 | length, p->blob); | |
2851 | old_extents->push_back(*oe); | |
2852 | add(end, | |
2853 | p->blob_offset + front + length, | |
2854 | p->length - front - length, | |
2855 | p->blob); | |
2856 | p->length = front; | |
2857 | break; | |
2858 | } else { | |
2859 | // deref tail | |
2860 | assert(p->logical_end() > offset); // else seek_lextent bug | |
2861 | uint64_t keep = offset - p->logical_offset; | |
2862 | OldExtent* oe = OldExtent::create(c, offset, p->blob_offset + keep, | |
2863 | p->length - keep, p->blob); | |
2864 | old_extents->push_back(*oe); | |
2865 | p->length = keep; | |
2866 | ++p; | |
2867 | continue; | |
2868 | } | |
2869 | } | |
2870 | if (p->logical_offset + p->length <= end) { | |
2871 | // deref whole lextent | |
2872 | OldExtent* oe = OldExtent::create(c, p->logical_offset, p->blob_offset, | |
2873 | p->length, p->blob); | |
2874 | old_extents->push_back(*oe); | |
2875 | rm(p++); | |
2876 | continue; | |
2877 | } | |
2878 | // deref head | |
2879 | uint64_t keep = p->logical_end() - end; | |
2880 | OldExtent* oe = OldExtent::create(c, p->logical_offset, p->blob_offset, | |
2881 | p->length - keep, p->blob); | |
2882 | old_extents->push_back(*oe); | |
2883 | ||
2884 | add(end, p->blob_offset + p->length - keep, keep, p->blob); | |
2885 | rm(p); | |
2886 | break; | |
2887 | } | |
2888 | } | |
2889 | ||
2890 | BlueStore::Extent *BlueStore::ExtentMap::set_lextent( | |
2891 | CollectionRef &c, | |
2892 | uint64_t logical_offset, | |
2893 | uint64_t blob_offset, uint64_t length, BlobRef b, | |
2894 | old_extent_map_t *old_extents) | |
2895 | { | |
2896 | // We need to have completely initialized Blob to increment its ref counters. | |
2897 | assert(b->get_blob().get_logical_length() != 0); | |
2898 | ||
2899 | // Do get_ref prior to punch_hole to prevent from putting reused blob into | |
2900 | // old_extents list if we overwre the blob totally | |
2901 | // This might happen during WAL overwrite. | |
2902 | b->get_ref(onode->c, blob_offset, length); | |
2903 | ||
2904 | if (old_extents) { | |
2905 | punch_hole(c, logical_offset, length, old_extents); | |
2906 | } | |
2907 | ||
2908 | Extent *le = new Extent(logical_offset, blob_offset, length, b); | |
2909 | extent_map.insert(*le); | |
2910 | if (spans_shard(logical_offset, length)) { | |
2911 | request_reshard(logical_offset, logical_offset + length); | |
2912 | } | |
2913 | return le; | |
2914 | } | |
2915 | ||
2916 | BlueStore::BlobRef BlueStore::ExtentMap::split_blob( | |
2917 | BlobRef lb, | |
2918 | uint32_t blob_offset, | |
2919 | uint32_t pos) | |
2920 | { | |
2921 | auto cct = onode->c->store->cct; //used by dout | |
2922 | ||
2923 | uint32_t end_pos = pos + lb->get_blob().get_logical_length() - blob_offset; | |
2924 | dout(20) << __func__ << " 0x" << std::hex << pos << " end 0x" << end_pos | |
2925 | << " blob_offset 0x" << blob_offset << std::dec << " " << *lb | |
2926 | << dendl; | |
2927 | BlobRef rb = onode->c->new_blob(); | |
2928 | lb->split(onode->c, blob_offset, rb.get()); | |
2929 | ||
2930 | for (auto ep = seek_lextent(pos); | |
2931 | ep != extent_map.end() && ep->logical_offset < end_pos; | |
2932 | ++ep) { | |
2933 | if (ep->blob != lb) { | |
2934 | continue; | |
2935 | } | |
2936 | if (ep->logical_offset < pos) { | |
2937 | // split extent | |
2938 | size_t left = pos - ep->logical_offset; | |
2939 | Extent *ne = new Extent(pos, 0, ep->length - left, rb); | |
2940 | extent_map.insert(*ne); | |
2941 | ep->length = left; | |
2942 | dout(30) << __func__ << " split " << *ep << dendl; | |
2943 | dout(30) << __func__ << " to " << *ne << dendl; | |
2944 | } else { | |
2945 | // switch blob | |
2946 | assert(ep->blob_offset >= blob_offset); | |
2947 | ||
2948 | ep->blob = rb; | |
2949 | ep->blob_offset -= blob_offset; | |
2950 | dout(30) << __func__ << " adjusted " << *ep << dendl; | |
2951 | } | |
2952 | } | |
2953 | return rb; | |
2954 | } | |
2955 | ||
2956 | // Onode | |
2957 | ||
2958 | #undef dout_prefix | |
2959 | #define dout_prefix *_dout << "bluestore.onode(" << this << ")." << __func__ << " " | |
2960 | ||
2961 | void BlueStore::Onode::flush() | |
2962 | { | |
2963 | if (flushing_count.load()) { | |
2964 | ldout(c->store->cct, 20) << __func__ << " cnt:" << flushing_count << dendl; | |
2965 | std::unique_lock<std::mutex> l(flush_lock); | |
2966 | while (flushing_count.load()) { | |
2967 | flush_cond.wait(l); | |
2968 | } | |
2969 | } | |
2970 | ldout(c->store->cct, 20) << __func__ << " done" << dendl; | |
2971 | } | |
2972 | ||
2973 | // ======================================================= | |
2974 | // WriteContext | |
2975 | ||
2976 | /// Checks for writes to the same pextent within a blob | |
2977 | bool BlueStore::WriteContext::has_conflict( | |
2978 | BlobRef b, | |
2979 | uint64_t loffs, | |
2980 | uint64_t loffs_end, | |
2981 | uint64_t min_alloc_size) | |
2982 | { | |
2983 | assert((loffs % min_alloc_size) == 0); | |
2984 | assert((loffs_end % min_alloc_size) == 0); | |
2985 | for (auto w : writes) { | |
2986 | if (b == w.b) { | |
2987 | auto loffs2 = P2ALIGN(w.logical_offset, min_alloc_size); | |
2988 | auto loffs2_end = P2ROUNDUP(w.logical_offset + w.length0, min_alloc_size); | |
2989 | if ((loffs <= loffs2 && loffs_end > loffs2) || | |
2990 | (loffs >= loffs2 && loffs < loffs2_end)) { | |
2991 | return true; | |
2992 | } | |
2993 | } | |
2994 | } | |
2995 | return false; | |
2996 | } | |
2997 | ||
2998 | // ======================================================= | |
2999 | ||
3000 | // DeferredBatch | |
3001 | #undef dout_prefix | |
3002 | #define dout_prefix *_dout << "bluestore.DeferredBatch(" << this << ") " | |
3003 | ||
3004 | void BlueStore::DeferredBatch::prepare_write( | |
3005 | CephContext *cct, | |
3006 | uint64_t seq, uint64_t offset, uint64_t length, | |
3007 | bufferlist::const_iterator& blp) | |
3008 | { | |
3009 | _discard(cct, offset, length); | |
3010 | auto i = iomap.insert(make_pair(offset, deferred_io())); | |
3011 | assert(i.second); // this should be a new insertion | |
3012 | i.first->second.seq = seq; | |
3013 | blp.copy(length, i.first->second.bl); | |
3014 | i.first->second.bl.reassign_to_mempool( | |
3015 | mempool::mempool_bluestore_writing_deferred); | |
3016 | dout(20) << __func__ << " seq " << seq | |
3017 | << " 0x" << std::hex << offset << "~" << length | |
3018 | << " crc " << i.first->second.bl.crc32c(-1) | |
3019 | << std::dec << dendl; | |
3020 | seq_bytes[seq] += length; | |
3021 | #ifdef DEBUG_DEFERRED | |
3022 | _audit(cct); | |
3023 | #endif | |
3024 | } | |
3025 | ||
3026 | void BlueStore::DeferredBatch::_discard( | |
3027 | CephContext *cct, uint64_t offset, uint64_t length) | |
3028 | { | |
3029 | generic_dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length | |
3030 | << std::dec << dendl; | |
3031 | auto p = iomap.lower_bound(offset); | |
3032 | if (p != iomap.begin()) { | |
3033 | --p; | |
3034 | auto end = p->first + p->second.bl.length(); | |
3035 | if (end > offset) { | |
3036 | bufferlist head; | |
3037 | head.substr_of(p->second.bl, 0, offset - p->first); | |
3038 | dout(20) << __func__ << " keep head " << p->second.seq | |
3039 | << " 0x" << std::hex << p->first << "~" << p->second.bl.length() | |
3040 | << " -> 0x" << head.length() << std::dec << dendl; | |
3041 | auto i = seq_bytes.find(p->second.seq); | |
3042 | assert(i != seq_bytes.end()); | |
3043 | if (end > offset + length) { | |
3044 | bufferlist tail; | |
3045 | tail.substr_of(p->second.bl, offset + length - p->first, | |
3046 | end - (offset + length)); | |
3047 | dout(20) << __func__ << " keep tail " << p->second.seq | |
3048 | << " 0x" << std::hex << p->first << "~" << p->second.bl.length() | |
3049 | << " -> 0x" << tail.length() << std::dec << dendl; | |
3050 | auto &n = iomap[offset + length]; | |
3051 | n.bl.swap(tail); | |
3052 | n.seq = p->second.seq; | |
3053 | i->second -= length; | |
3054 | } else { | |
3055 | i->second -= end - offset; | |
3056 | } | |
3057 | assert(i->second >= 0); | |
3058 | p->second.bl.swap(head); | |
3059 | } | |
3060 | ++p; | |
3061 | } | |
3062 | while (p != iomap.end()) { | |
3063 | if (p->first >= offset + length) { | |
3064 | break; | |
3065 | } | |
3066 | auto i = seq_bytes.find(p->second.seq); | |
3067 | assert(i != seq_bytes.end()); | |
3068 | auto end = p->first + p->second.bl.length(); | |
3069 | if (end > offset + length) { | |
3070 | unsigned drop_front = offset + length - p->first; | |
3071 | unsigned keep_tail = end - (offset + length); | |
3072 | dout(20) << __func__ << " truncate front " << p->second.seq | |
3073 | << " 0x" << std::hex << p->first << "~" << p->second.bl.length() | |
3074 | << " drop_front 0x" << drop_front << " keep_tail 0x" << keep_tail | |
3075 | << " to 0x" << (offset + length) << "~" << keep_tail | |
3076 | << std::dec << dendl; | |
3077 | auto &s = iomap[offset + length]; | |
3078 | s.seq = p->second.seq; | |
3079 | s.bl.substr_of(p->second.bl, drop_front, keep_tail); | |
3080 | i->second -= drop_front; | |
3081 | } else { | |
3082 | dout(20) << __func__ << " drop " << p->second.seq | |
3083 | << " 0x" << std::hex << p->first << "~" << p->second.bl.length() | |
3084 | << std::dec << dendl; | |
3085 | i->second -= p->second.bl.length(); | |
3086 | } | |
3087 | assert(i->second >= 0); | |
3088 | p = iomap.erase(p); | |
3089 | } | |
3090 | } | |
3091 | ||
3092 | void BlueStore::DeferredBatch::_audit(CephContext *cct) | |
3093 | { | |
3094 | map<uint64_t,int> sb; | |
3095 | for (auto p : seq_bytes) { | |
3096 | sb[p.first] = 0; // make sure we have the same set of keys | |
3097 | } | |
3098 | uint64_t pos = 0; | |
3099 | for (auto& p : iomap) { | |
3100 | assert(p.first >= pos); | |
3101 | sb[p.second.seq] += p.second.bl.length(); | |
3102 | pos = p.first + p.second.bl.length(); | |
3103 | } | |
3104 | assert(sb == seq_bytes); | |
3105 | } | |
3106 | ||
3107 | ||
3108 | // Collection | |
3109 | ||
3110 | #undef dout_prefix | |
3111 | #define dout_prefix *_dout << "bluestore(" << store->path << ").collection(" << cid << " " << this << ") " | |
3112 | ||
3113 | BlueStore::Collection::Collection(BlueStore *ns, Cache *c, coll_t cid) | |
3114 | : store(ns), | |
3115 | cache(c), | |
3116 | cid(cid), | |
3117 | lock("BlueStore::Collection::lock", true, false), | |
3118 | exists(true), | |
3119 | onode_map(c) | |
3120 | { | |
3121 | } | |
3122 | ||
3123 | void BlueStore::Collection::open_shared_blob(uint64_t sbid, BlobRef b) | |
3124 | { | |
3125 | assert(!b->shared_blob); | |
3126 | const bluestore_blob_t& blob = b->get_blob(); | |
3127 | if (!blob.is_shared()) { | |
3128 | b->shared_blob = new SharedBlob(this); | |
3129 | return; | |
3130 | } | |
3131 | ||
3132 | b->shared_blob = shared_blob_set.lookup(sbid); | |
3133 | if (b->shared_blob) { | |
3134 | ldout(store->cct, 10) << __func__ << " sbid 0x" << std::hex << sbid | |
3135 | << std::dec << " had " << *b->shared_blob << dendl; | |
3136 | } else { | |
3137 | b->shared_blob = new SharedBlob(sbid, this); | |
3138 | shared_blob_set.add(this, b->shared_blob.get()); | |
3139 | ldout(store->cct, 10) << __func__ << " sbid 0x" << std::hex << sbid | |
3140 | << std::dec << " opened " << *b->shared_blob | |
3141 | << dendl; | |
3142 | } | |
3143 | } | |
3144 | ||
3145 | void BlueStore::Collection::load_shared_blob(SharedBlobRef sb) | |
3146 | { | |
3147 | if (!sb->is_loaded()) { | |
3148 | ||
3149 | bufferlist v; | |
3150 | string key; | |
3151 | auto sbid = sb->get_sbid(); | |
3152 | get_shared_blob_key(sbid, &key); | |
3153 | int r = store->db->get(PREFIX_SHARED_BLOB, key, &v); | |
3154 | if (r < 0) { | |
3155 | lderr(store->cct) << __func__ << " sbid 0x" << std::hex << sbid | |
3156 | << std::dec << " not found at key " | |
3157 | << pretty_binary_string(key) << dendl; | |
3158 | assert(0 == "uh oh, missing shared_blob"); | |
3159 | } | |
3160 | ||
3161 | sb->loaded = true; | |
3162 | sb->persistent = new bluestore_shared_blob_t(sbid); | |
3163 | bufferlist::iterator p = v.begin(); | |
3164 | ::decode(*(sb->persistent), p); | |
3165 | ldout(store->cct, 10) << __func__ << " sbid 0x" << std::hex << sbid | |
3166 | << std::dec << " loaded shared_blob " << *sb << dendl; | |
3167 | } | |
3168 | } | |
3169 | ||
3170 | void BlueStore::Collection::make_blob_shared(uint64_t sbid, BlobRef b) | |
3171 | { | |
3172 | ldout(store->cct, 10) << __func__ << " " << *b << dendl; | |
3173 | assert(!b->shared_blob->is_loaded()); | |
3174 | ||
3175 | // update blob | |
3176 | bluestore_blob_t& blob = b->dirty_blob(); | |
3177 | blob.set_flag(bluestore_blob_t::FLAG_SHARED); | |
3178 | ||
3179 | // update shared blob | |
3180 | b->shared_blob->loaded = true; | |
3181 | b->shared_blob->persistent = new bluestore_shared_blob_t(sbid); | |
3182 | shared_blob_set.add(this, b->shared_blob.get()); | |
3183 | for (auto p : blob.get_extents()) { | |
3184 | if (p.is_valid()) { | |
3185 | b->shared_blob->get_ref( | |
3186 | p.offset, | |
3187 | p.length); | |
3188 | } | |
3189 | } | |
3190 | ldout(store->cct, 20) << __func__ << " now " << *b << dendl; | |
3191 | } | |
3192 | ||
3193 | uint64_t BlueStore::Collection::make_blob_unshared(SharedBlob *sb) | |
3194 | { | |
3195 | ldout(store->cct, 10) << __func__ << " " << *sb << dendl; | |
3196 | assert(sb->is_loaded()); | |
3197 | ||
3198 | uint64_t sbid = sb->get_sbid(); | |
3199 | shared_blob_set.remove(sb); | |
3200 | sb->loaded = false; | |
3201 | delete sb->persistent; | |
3202 | sb->sbid_unloaded = 0; | |
3203 | ldout(store->cct, 20) << __func__ << " now " << *sb << dendl; | |
3204 | return sbid; | |
3205 | } | |
3206 | ||
3207 | BlueStore::OnodeRef BlueStore::Collection::get_onode( | |
3208 | const ghobject_t& oid, | |
3209 | bool create) | |
3210 | { | |
3211 | assert(create ? lock.is_wlocked() : lock.is_locked()); | |
3212 | ||
3213 | spg_t pgid; | |
3214 | if (cid.is_pg(&pgid)) { | |
3215 | if (!oid.match(cnode.bits, pgid.ps())) { | |
3216 | lderr(store->cct) << __func__ << " oid " << oid << " not part of " | |
3217 | << pgid << " bits " << cnode.bits << dendl; | |
3218 | ceph_abort(); | |
3219 | } | |
3220 | } | |
3221 | ||
3222 | OnodeRef o = onode_map.lookup(oid); | |
3223 | if (o) | |
3224 | return o; | |
3225 | ||
3226 | mempool::bluestore_cache_other::string key; | |
3227 | get_object_key(store->cct, oid, &key); | |
3228 | ||
3229 | ldout(store->cct, 20) << __func__ << " oid " << oid << " key " | |
3230 | << pretty_binary_string(key) << dendl; | |
3231 | ||
3232 | bufferlist v; | |
3233 | int r = store->db->get(PREFIX_OBJ, key.c_str(), key.size(), &v); | |
3234 | ldout(store->cct, 20) << " r " << r << " v.len " << v.length() << dendl; | |
3235 | Onode *on; | |
3236 | if (v.length() == 0) { | |
3237 | assert(r == -ENOENT); | |
3238 | if (!store->cct->_conf->bluestore_debug_misc && | |
3239 | !create) | |
3240 | return OnodeRef(); | |
3241 | ||
3242 | // new object, new onode | |
3243 | on = new Onode(this, oid, key); | |
3244 | } else { | |
3245 | // loaded | |
3246 | assert(r >= 0); | |
3247 | on = new Onode(this, oid, key); | |
3248 | on->exists = true; | |
3249 | bufferptr::iterator p = v.front().begin_deep(); | |
3250 | on->onode.decode(p); | |
3251 | for (auto& i : on->onode.attrs) { | |
3252 | i.second.reassign_to_mempool(mempool::mempool_bluestore_cache_other); | |
3253 | } | |
3254 | ||
3255 | // initialize extent_map | |
3256 | on->extent_map.decode_spanning_blobs(p); | |
3257 | if (on->onode.extent_map_shards.empty()) { | |
3258 | denc(on->extent_map.inline_bl, p); | |
3259 | on->extent_map.decode_some(on->extent_map.inline_bl); | |
3260 | on->extent_map.inline_bl.reassign_to_mempool( | |
3261 | mempool::mempool_bluestore_cache_other); | |
3262 | } else { | |
3263 | on->extent_map.init_shards(false, false); | |
3264 | } | |
3265 | } | |
3266 | o.reset(on); | |
3267 | return onode_map.add(oid, o); | |
3268 | } | |
3269 | ||
3270 | void BlueStore::Collection::split_cache( | |
3271 | Collection *dest) | |
3272 | { | |
3273 | ldout(store->cct, 10) << __func__ << " to " << dest << dendl; | |
3274 | ||
3275 | // lock (one or both) cache shards | |
3276 | std::lock(cache->lock, dest->cache->lock); | |
3277 | std::lock_guard<std::recursive_mutex> l(cache->lock, std::adopt_lock); | |
3278 | std::lock_guard<std::recursive_mutex> l2(dest->cache->lock, std::adopt_lock); | |
3279 | ||
3280 | int destbits = dest->cnode.bits; | |
3281 | spg_t destpg; | |
3282 | bool is_pg = dest->cid.is_pg(&destpg); | |
3283 | assert(is_pg); | |
3284 | ||
3285 | auto p = onode_map.onode_map.begin(); | |
3286 | while (p != onode_map.onode_map.end()) { | |
3287 | if (!p->second->oid.match(destbits, destpg.pgid.ps())) { | |
3288 | // onode does not belong to this child | |
3289 | ++p; | |
3290 | } else { | |
3291 | OnodeRef o = p->second; | |
3292 | ldout(store->cct, 20) << __func__ << " moving " << o << " " << o->oid | |
3293 | << dendl; | |
3294 | ||
3295 | cache->_rm_onode(p->second); | |
3296 | p = onode_map.onode_map.erase(p); | |
3297 | ||
3298 | o->c = dest; | |
3299 | dest->cache->_add_onode(o, 1); | |
3300 | dest->onode_map.onode_map[o->oid] = o; | |
3301 | dest->onode_map.cache = dest->cache; | |
3302 | ||
3303 | // move over shared blobs and buffers. cover shared blobs from | |
3304 | // both extent map and spanning blob map (the full extent map | |
3305 | // may not be faulted in) | |
3306 | vector<SharedBlob*> sbvec; | |
3307 | for (auto& e : o->extent_map.extent_map) { | |
3308 | sbvec.push_back(e.blob->shared_blob.get()); | |
3309 | } | |
3310 | for (auto& b : o->extent_map.spanning_blob_map) { | |
3311 | sbvec.push_back(b.second->shared_blob.get()); | |
3312 | } | |
3313 | for (auto sb : sbvec) { | |
3314 | if (sb->coll == dest) { | |
3315 | ldout(store->cct, 20) << __func__ << " already moved " << *sb | |
3316 | << dendl; | |
3317 | continue; | |
3318 | } | |
3319 | ldout(store->cct, 20) << __func__ << " moving " << *sb << dendl; | |
3320 | if (sb->get_sbid()) { | |
3321 | ldout(store->cct, 20) << __func__ | |
3322 | << " moving registration " << *sb << dendl; | |
3323 | shared_blob_set.remove(sb); | |
3324 | dest->shared_blob_set.add(dest, sb); | |
3325 | } | |
3326 | sb->coll = dest; | |
3327 | if (dest->cache != cache) { | |
3328 | for (auto& i : sb->bc.buffer_map) { | |
3329 | if (!i.second->is_writing()) { | |
3330 | ldout(store->cct, 20) << __func__ << " moving " << *i.second | |
3331 | << dendl; | |
3332 | dest->cache->_move_buffer(cache, i.second.get()); | |
3333 | } | |
3334 | } | |
3335 | } | |
3336 | } | |
3337 | } | |
3338 | } | |
3339 | } | |
3340 | ||
3341 | // ======================================================= | |
3342 | ||
3343 | void *BlueStore::MempoolThread::entry() | |
3344 | { | |
3345 | Mutex::Locker l(lock); | |
3346 | while (!stop) { | |
3347 | uint64_t meta_bytes = | |
3348 | mempool::bluestore_cache_other::allocated_bytes() + | |
3349 | mempool::bluestore_cache_onode::allocated_bytes(); | |
3350 | uint64_t onode_num = | |
3351 | mempool::bluestore_cache_onode::allocated_items(); | |
3352 | ||
3353 | if (onode_num < 2) { | |
3354 | onode_num = 2; | |
3355 | } | |
3356 | ||
3357 | float bytes_per_onode = (float)meta_bytes / (float)onode_num; | |
3358 | size_t num_shards = store->cache_shards.size(); | |
3359 | float target_ratio = store->cache_meta_ratio + store->cache_data_ratio; | |
3360 | // A little sloppy but should be close enough | |
3361 | uint64_t shard_target = target_ratio * (store->cache_size / num_shards); | |
3362 | ||
3363 | for (auto i : store->cache_shards) { | |
3364 | i->trim(shard_target, | |
3365 | store->cache_meta_ratio, | |
3366 | store->cache_data_ratio, | |
3367 | bytes_per_onode); | |
3368 | } | |
3369 | ||
3370 | store->_update_cache_logger(); | |
3371 | ||
3372 | utime_t wait; | |
3373 | wait += store->cct->_conf->bluestore_cache_trim_interval; | |
3374 | cond.WaitInterval(lock, wait); | |
3375 | } | |
3376 | stop = false; | |
3377 | return NULL; | |
3378 | } | |
3379 | ||
3380 | // ======================================================= | |
3381 | ||
3382 | // OmapIteratorImpl | |
3383 | ||
3384 | #undef dout_prefix | |
3385 | #define dout_prefix *_dout << "bluestore.OmapIteratorImpl(" << this << ") " | |
3386 | ||
3387 | BlueStore::OmapIteratorImpl::OmapIteratorImpl( | |
3388 | CollectionRef c, OnodeRef o, KeyValueDB::Iterator it) | |
3389 | : c(c), o(o), it(it) | |
3390 | { | |
3391 | RWLock::RLocker l(c->lock); | |
3392 | if (o->onode.has_omap()) { | |
3393 | get_omap_key(o->onode.nid, string(), &head); | |
3394 | get_omap_tail(o->onode.nid, &tail); | |
3395 | it->lower_bound(head); | |
3396 | } | |
3397 | } | |
3398 | ||
3399 | int BlueStore::OmapIteratorImpl::seek_to_first() | |
3400 | { | |
3401 | RWLock::RLocker l(c->lock); | |
3402 | if (o->onode.has_omap()) { | |
3403 | it->lower_bound(head); | |
3404 | } else { | |
3405 | it = KeyValueDB::Iterator(); | |
3406 | } | |
3407 | return 0; | |
3408 | } | |
3409 | ||
3410 | int BlueStore::OmapIteratorImpl::upper_bound(const string& after) | |
3411 | { | |
3412 | RWLock::RLocker l(c->lock); | |
3413 | if (o->onode.has_omap()) { | |
3414 | string key; | |
3415 | get_omap_key(o->onode.nid, after, &key); | |
3416 | ldout(c->store->cct,20) << __func__ << " after " << after << " key " | |
3417 | << pretty_binary_string(key) << dendl; | |
3418 | it->upper_bound(key); | |
3419 | } else { | |
3420 | it = KeyValueDB::Iterator(); | |
3421 | } | |
3422 | return 0; | |
3423 | } | |
3424 | ||
3425 | int BlueStore::OmapIteratorImpl::lower_bound(const string& to) | |
3426 | { | |
3427 | RWLock::RLocker l(c->lock); | |
3428 | if (o->onode.has_omap()) { | |
3429 | string key; | |
3430 | get_omap_key(o->onode.nid, to, &key); | |
3431 | ldout(c->store->cct,20) << __func__ << " to " << to << " key " | |
3432 | << pretty_binary_string(key) << dendl; | |
3433 | it->lower_bound(key); | |
3434 | } else { | |
3435 | it = KeyValueDB::Iterator(); | |
3436 | } | |
3437 | return 0; | |
3438 | } | |
3439 | ||
3440 | bool BlueStore::OmapIteratorImpl::valid() | |
3441 | { | |
3442 | RWLock::RLocker l(c->lock); | |
3443 | bool r = o->onode.has_omap() && it && it->valid() && | |
3444 | it->raw_key().second <= tail; | |
3445 | if (it && it->valid()) { | |
3446 | ldout(c->store->cct,20) << __func__ << " is at " | |
3447 | << pretty_binary_string(it->raw_key().second) | |
3448 | << dendl; | |
3449 | } | |
3450 | return r; | |
3451 | } | |
3452 | ||
3453 | int BlueStore::OmapIteratorImpl::next(bool validate) | |
3454 | { | |
3455 | RWLock::RLocker l(c->lock); | |
3456 | if (o->onode.has_omap()) { | |
3457 | it->next(); | |
3458 | return 0; | |
3459 | } else { | |
3460 | return -1; | |
3461 | } | |
3462 | } | |
3463 | ||
3464 | string BlueStore::OmapIteratorImpl::key() | |
3465 | { | |
3466 | RWLock::RLocker l(c->lock); | |
3467 | assert(it->valid()); | |
3468 | string db_key = it->raw_key().second; | |
3469 | string user_key; | |
3470 | decode_omap_key(db_key, &user_key); | |
3471 | return user_key; | |
3472 | } | |
3473 | ||
3474 | bufferlist BlueStore::OmapIteratorImpl::value() | |
3475 | { | |
3476 | RWLock::RLocker l(c->lock); | |
3477 | assert(it->valid()); | |
3478 | return it->value(); | |
3479 | } | |
3480 | ||
3481 | ||
3482 | // ===================================== | |
3483 | ||
3484 | #undef dout_prefix | |
3485 | #define dout_prefix *_dout << "bluestore(" << path << ") " | |
3486 | ||
3487 | ||
3488 | static void aio_cb(void *priv, void *priv2) | |
3489 | { | |
3490 | BlueStore *store = static_cast<BlueStore*>(priv); | |
3491 | BlueStore::AioContext *c = static_cast<BlueStore::AioContext*>(priv2); | |
3492 | c->aio_finish(store); | |
3493 | } | |
3494 | ||
3495 | BlueStore::BlueStore(CephContext *cct, const string& path) | |
3496 | : ObjectStore(cct, path), | |
3497 | throttle_bytes(cct, "bluestore_throttle_bytes", | |
3498 | cct->_conf->bluestore_throttle_bytes), | |
3499 | throttle_deferred_bytes(cct, "bluestore_throttle_deferred_bytes", | |
3500 | cct->_conf->bluestore_throttle_bytes + | |
3501 | cct->_conf->bluestore_throttle_deferred_bytes), | |
3502 | deferred_finisher(cct, "defered_finisher", "dfin"), | |
3503 | kv_sync_thread(this), | |
3504 | kv_finalize_thread(this), | |
3505 | mempool_thread(this) | |
3506 | { | |
3507 | _init_logger(); | |
3508 | cct->_conf->add_observer(this); | |
3509 | set_cache_shards(1); | |
3510 | } | |
3511 | ||
3512 | BlueStore::BlueStore(CephContext *cct, | |
3513 | const string& path, | |
3514 | uint64_t _min_alloc_size) | |
3515 | : ObjectStore(cct, path), | |
3516 | throttle_bytes(cct, "bluestore_throttle_bytes", | |
3517 | cct->_conf->bluestore_throttle_bytes), | |
3518 | throttle_deferred_bytes(cct, "bluestore_throttle_deferred_bytes", | |
3519 | cct->_conf->bluestore_throttle_bytes + | |
3520 | cct->_conf->bluestore_throttle_deferred_bytes), | |
3521 | deferred_finisher(cct, "defered_finisher", "dfin"), | |
3522 | kv_sync_thread(this), | |
3523 | kv_finalize_thread(this), | |
3524 | min_alloc_size(_min_alloc_size), | |
3525 | min_alloc_size_order(ctz(_min_alloc_size)), | |
3526 | mempool_thread(this) | |
3527 | { | |
3528 | _init_logger(); | |
3529 | cct->_conf->add_observer(this); | |
3530 | set_cache_shards(1); | |
3531 | } | |
3532 | ||
3533 | BlueStore::~BlueStore() | |
3534 | { | |
3535 | for (auto f : finishers) { | |
3536 | delete f; | |
3537 | } | |
3538 | finishers.clear(); | |
3539 | ||
3540 | cct->_conf->remove_observer(this); | |
3541 | _shutdown_logger(); | |
3542 | assert(!mounted); | |
3543 | assert(db == NULL); | |
3544 | assert(bluefs == NULL); | |
3545 | assert(fsid_fd < 0); | |
3546 | assert(path_fd < 0); | |
3547 | for (auto i : cache_shards) { | |
3548 | delete i; | |
3549 | } | |
3550 | cache_shards.clear(); | |
3551 | } | |
3552 | ||
3553 | const char **BlueStore::get_tracked_conf_keys() const | |
3554 | { | |
3555 | static const char* KEYS[] = { | |
3556 | "bluestore_csum_type", | |
3557 | "bluestore_compression_mode", | |
3558 | "bluestore_compression_algorithm", | |
3559 | "bluestore_compression_min_blob_size", | |
3560 | "bluestore_compression_min_blob_size_ssd", | |
3561 | "bluestore_compression_min_blob_size_hdd", | |
3562 | "bluestore_compression_max_blob_size", | |
3563 | "bluestore_compression_max_blob_size_ssd", | |
3564 | "bluestore_compression_max_blob_size_hdd", | |
3565 | "bluestore_compression_required_ratio", | |
3566 | "bluestore_max_alloc_size", | |
3567 | "bluestore_prefer_deferred_size", | |
3568 | "bluestore_prefer_deferred_size_hdd", | |
3569 | "bluestore_prefer_deferred_size_ssd", | |
3570 | "bluestore_deferred_batch_ops", | |
3571 | "bluestore_deferred_batch_ops_hdd", | |
3572 | "bluestore_deferred_batch_ops_ssd", | |
3573 | "bluestore_throttle_bytes", | |
3574 | "bluestore_throttle_deferred_bytes", | |
3575 | "bluestore_throttle_cost_per_io_hdd", | |
3576 | "bluestore_throttle_cost_per_io_ssd", | |
3577 | "bluestore_throttle_cost_per_io", | |
3578 | "bluestore_max_blob_size", | |
3579 | "bluestore_max_blob_size_ssd", | |
3580 | "bluestore_max_blob_size_hdd", | |
3581 | NULL | |
3582 | }; | |
3583 | return KEYS; | |
3584 | } | |
3585 | ||
3586 | void BlueStore::handle_conf_change(const struct md_config_t *conf, | |
3587 | const std::set<std::string> &changed) | |
3588 | { | |
3589 | if (changed.count("bluestore_csum_type")) { | |
3590 | _set_csum(); | |
3591 | } | |
3592 | if (changed.count("bluestore_compression_mode") || | |
3593 | changed.count("bluestore_compression_algorithm") || | |
3594 | changed.count("bluestore_compression_min_blob_size") || | |
3595 | changed.count("bluestore_compression_max_blob_size")) { | |
3596 | if (bdev) { | |
3597 | _set_compression(); | |
3598 | } | |
3599 | } | |
3600 | if (changed.count("bluestore_max_blob_size") || | |
3601 | changed.count("bluestore_max_blob_size_ssd") || | |
3602 | changed.count("bluestore_max_blob_size_hdd")) { | |
3603 | if (bdev) { | |
3604 | // only after startup | |
3605 | _set_blob_size(); | |
3606 | } | |
3607 | } | |
3608 | if (changed.count("bluestore_prefer_deferred_size") || | |
3609 | changed.count("bluestore_prefer_deferred_size_hdd") || | |
3610 | changed.count("bluestore_prefer_deferred_size_ssd") || | |
3611 | changed.count("bluestore_max_alloc_size") || | |
3612 | changed.count("bluestore_deferred_batch_ops") || | |
3613 | changed.count("bluestore_deferred_batch_ops_hdd") || | |
3614 | changed.count("bluestore_deferred_batch_ops_ssd")) { | |
3615 | if (bdev) { | |
3616 | // only after startup | |
3617 | _set_alloc_sizes(); | |
3618 | } | |
3619 | } | |
3620 | if (changed.count("bluestore_throttle_cost_per_io") || | |
3621 | changed.count("bluestore_throttle_cost_per_io_hdd") || | |
3622 | changed.count("bluestore_throttle_cost_per_io_ssd")) { | |
3623 | if (bdev) { | |
3624 | _set_throttle_params(); | |
3625 | } | |
3626 | } | |
3627 | if (changed.count("bluestore_throttle_bytes")) { | |
3628 | throttle_bytes.reset_max(conf->bluestore_throttle_bytes); | |
3629 | throttle_deferred_bytes.reset_max( | |
3630 | conf->bluestore_throttle_bytes + conf->bluestore_throttle_deferred_bytes); | |
3631 | } | |
3632 | if (changed.count("bluestore_throttle_deferred_bytes")) { | |
3633 | throttle_deferred_bytes.reset_max( | |
3634 | conf->bluestore_throttle_bytes + conf->bluestore_throttle_deferred_bytes); | |
3635 | } | |
3636 | } | |
3637 | ||
3638 | void BlueStore::_set_compression() | |
3639 | { | |
3640 | auto m = Compressor::get_comp_mode_type(cct->_conf->bluestore_compression_mode); | |
3641 | if (m) { | |
3642 | comp_mode = *m; | |
3643 | } else { | |
3644 | derr << __func__ << " unrecognized value '" | |
3645 | << cct->_conf->bluestore_compression_mode | |
3646 | << "' for bluestore_compression_mode, reverting to 'none'" | |
3647 | << dendl; | |
3648 | comp_mode = Compressor::COMP_NONE; | |
3649 | } | |
3650 | ||
3651 | compressor = nullptr; | |
3652 | ||
3653 | if (comp_mode == Compressor::COMP_NONE) { | |
3654 | dout(10) << __func__ << " compression mode set to 'none', " | |
3655 | << "ignore other compression setttings" << dendl; | |
3656 | return; | |
3657 | } | |
3658 | ||
3659 | if (cct->_conf->bluestore_compression_min_blob_size) { | |
3660 | comp_min_blob_size = cct->_conf->bluestore_compression_min_blob_size; | |
3661 | } else { | |
3662 | assert(bdev); | |
3663 | if (bdev->is_rotational()) { | |
3664 | comp_min_blob_size = cct->_conf->bluestore_compression_min_blob_size_hdd; | |
3665 | } else { | |
3666 | comp_min_blob_size = cct->_conf->bluestore_compression_min_blob_size_ssd; | |
3667 | } | |
3668 | } | |
3669 | ||
3670 | if (cct->_conf->bluestore_compression_max_blob_size) { | |
3671 | comp_max_blob_size = cct->_conf->bluestore_compression_max_blob_size; | |
3672 | } else { | |
3673 | assert(bdev); | |
3674 | if (bdev->is_rotational()) { | |
3675 | comp_max_blob_size = cct->_conf->bluestore_compression_max_blob_size_hdd; | |
3676 | } else { | |
3677 | comp_max_blob_size = cct->_conf->bluestore_compression_max_blob_size_ssd; | |
3678 | } | |
3679 | } | |
3680 | ||
3681 | auto& alg_name = cct->_conf->bluestore_compression_algorithm; | |
3682 | if (!alg_name.empty()) { | |
3683 | compressor = Compressor::create(cct, alg_name); | |
3684 | if (!compressor) { | |
3685 | derr << __func__ << " unable to initialize " << alg_name.c_str() << " compressor" | |
3686 | << dendl; | |
3687 | } | |
3688 | } | |
3689 | ||
3690 | dout(10) << __func__ << " mode " << Compressor::get_comp_mode_name(comp_mode) | |
3691 | << " alg " << (compressor ? compressor->get_type_name() : "(none)") | |
3692 | << dendl; | |
3693 | } | |
3694 | ||
3695 | void BlueStore::_set_csum() | |
3696 | { | |
3697 | csum_type = Checksummer::CSUM_NONE; | |
3698 | int t = Checksummer::get_csum_string_type(cct->_conf->bluestore_csum_type); | |
3699 | if (t > Checksummer::CSUM_NONE) | |
3700 | csum_type = t; | |
3701 | ||
3702 | dout(10) << __func__ << " csum_type " | |
3703 | << Checksummer::get_csum_type_string(csum_type) | |
3704 | << dendl; | |
3705 | } | |
3706 | ||
3707 | void BlueStore::_set_throttle_params() | |
3708 | { | |
3709 | if (cct->_conf->bluestore_throttle_cost_per_io) { | |
3710 | throttle_cost_per_io = cct->_conf->bluestore_throttle_cost_per_io; | |
3711 | } else { | |
3712 | assert(bdev); | |
3713 | if (bdev->is_rotational()) { | |
3714 | throttle_cost_per_io = cct->_conf->bluestore_throttle_cost_per_io_hdd; | |
3715 | } else { | |
3716 | throttle_cost_per_io = cct->_conf->bluestore_throttle_cost_per_io_ssd; | |
3717 | } | |
3718 | } | |
3719 | ||
3720 | dout(10) << __func__ << " throttle_cost_per_io " << throttle_cost_per_io | |
3721 | << dendl; | |
3722 | } | |
3723 | void BlueStore::_set_blob_size() | |
3724 | { | |
3725 | if (cct->_conf->bluestore_max_blob_size) { | |
3726 | max_blob_size = cct->_conf->bluestore_max_blob_size; | |
3727 | } else { | |
3728 | assert(bdev); | |
3729 | if (bdev->is_rotational()) { | |
3730 | max_blob_size = cct->_conf->bluestore_max_blob_size_hdd; | |
3731 | } else { | |
3732 | max_blob_size = cct->_conf->bluestore_max_blob_size_ssd; | |
3733 | } | |
3734 | } | |
3735 | dout(10) << __func__ << " max_blob_size 0x" << std::hex << max_blob_size | |
3736 | << std::dec << dendl; | |
3737 | } | |
3738 | ||
3739 | int BlueStore::_set_cache_sizes() | |
3740 | { | |
3741 | assert(bdev); | |
3742 | if (cct->_conf->bluestore_cache_size) { | |
3743 | cache_size = cct->_conf->bluestore_cache_size; | |
3744 | } else { | |
3745 | // choose global cache size based on backend type | |
3746 | if (bdev->is_rotational()) { | |
3747 | cache_size = cct->_conf->bluestore_cache_size_hdd; | |
3748 | } else { | |
3749 | cache_size = cct->_conf->bluestore_cache_size_ssd; | |
3750 | } | |
3751 | } | |
3752 | cache_meta_ratio = cct->_conf->bluestore_cache_meta_ratio; | |
3753 | cache_kv_ratio = cct->_conf->bluestore_cache_kv_ratio; | |
3754 | ||
3755 | double cache_kv_max = cct->_conf->bluestore_cache_kv_max; | |
3756 | double cache_kv_max_ratio = 0; | |
3757 | ||
3758 | // if cache_kv_max is negative, disable it | |
3759 | if (cache_size > 0 && cache_kv_max >= 0) { | |
3760 | cache_kv_max_ratio = (double) cache_kv_max / (double) cache_size; | |
3761 | if (cache_kv_max_ratio < 1.0 && cache_kv_max_ratio < cache_kv_ratio) { | |
3762 | dout(1) << __func__ << " max " << cache_kv_max_ratio | |
3763 | << " < ratio " << cache_kv_ratio | |
3764 | << dendl; | |
3765 | cache_meta_ratio = cache_meta_ratio + cache_kv_ratio - cache_kv_max_ratio; | |
3766 | cache_kv_ratio = cache_kv_max_ratio; | |
3767 | } | |
3768 | } | |
3769 | ||
3770 | cache_data_ratio = | |
3771 | (double)1.0 - (double)cache_meta_ratio - (double)cache_kv_ratio; | |
3772 | ||
3773 | if (cache_meta_ratio < 0 || cache_meta_ratio > 1.0) { | |
3774 | derr << __func__ << " bluestore_cache_meta_ratio (" << cache_meta_ratio | |
3775 | << ") must be in range [0,1.0]" << dendl; | |
3776 | return -EINVAL; | |
3777 | } | |
3778 | if (cache_kv_ratio < 0 || cache_kv_ratio > 1.0) { | |
3779 | derr << __func__ << " bluestore_cache_kv_ratio (" << cache_kv_ratio | |
3780 | << ") must be in range [0,1.0]" << dendl; | |
3781 | return -EINVAL; | |
3782 | } | |
3783 | if (cache_meta_ratio + cache_kv_ratio > 1.0) { | |
3784 | derr << __func__ << " bluestore_cache_meta_ratio (" << cache_meta_ratio | |
3785 | << ") + bluestore_cache_kv_ratio (" << cache_kv_ratio | |
3786 | << ") = " << cache_meta_ratio + cache_kv_ratio << "; must be <= 1.0" | |
3787 | << dendl; | |
3788 | return -EINVAL; | |
3789 | } | |
3790 | if (cache_data_ratio < 0) { | |
3791 | // deal with floating point imprecision | |
3792 | cache_data_ratio = 0; | |
3793 | } | |
3794 | dout(1) << __func__ << " cache_size " << cache_size | |
3795 | << " meta " << cache_meta_ratio | |
3796 | << " kv " << cache_kv_ratio | |
3797 | << " data " << cache_data_ratio | |
3798 | << dendl; | |
3799 | return 0; | |
3800 | } | |
3801 | ||
3802 | int BlueStore::write_meta(const std::string& key, const std::string& value) | |
3803 | { | |
3804 | bluestore_bdev_label_t label; | |
3805 | string p = path + "/block"; | |
3806 | int r = _read_bdev_label(cct, p, &label); | |
3807 | if (r < 0) { | |
3808 | return ObjectStore::write_meta(key, value); | |
3809 | } | |
3810 | label.meta[key] = value; | |
3811 | r = _write_bdev_label(cct, p, label); | |
3812 | assert(r == 0); | |
3813 | return ObjectStore::write_meta(key, value); | |
3814 | } | |
3815 | ||
3816 | int BlueStore::read_meta(const std::string& key, std::string *value) | |
3817 | { | |
3818 | bluestore_bdev_label_t label; | |
3819 | string p = path + "/block"; | |
3820 | int r = _read_bdev_label(cct, p, &label); | |
3821 | if (r < 0) { | |
3822 | return ObjectStore::read_meta(key, value); | |
3823 | } | |
3824 | auto i = label.meta.find(key); | |
3825 | if (i == label.meta.end()) { | |
3826 | return ObjectStore::read_meta(key, value); | |
3827 | } | |
3828 | *value = i->second; | |
3829 | return 0; | |
3830 | } | |
3831 | ||
3832 | void BlueStore::_init_logger() | |
3833 | { | |
3834 | PerfCountersBuilder b(cct, "bluestore", | |
3835 | l_bluestore_first, l_bluestore_last); | |
3836 | b.add_time_avg(l_bluestore_kv_flush_lat, "kv_flush_lat", | |
3837 | "Average kv_thread flush latency", | |
3838 | "fl_l", PerfCountersBuilder::PRIO_INTERESTING); | |
3839 | b.add_time_avg(l_bluestore_kv_commit_lat, "kv_commit_lat", | |
3840 | "Average kv_thread commit latency"); | |
3841 | b.add_time_avg(l_bluestore_kv_lat, "kv_lat", | |
3842 | "Average kv_thread sync latency", | |
3843 | "k_l", PerfCountersBuilder::PRIO_INTERESTING); | |
3844 | b.add_time_avg(l_bluestore_state_prepare_lat, "state_prepare_lat", | |
3845 | "Average prepare state latency"); | |
3846 | b.add_time_avg(l_bluestore_state_aio_wait_lat, "state_aio_wait_lat", | |
3847 | "Average aio_wait state latency", | |
3848 | "io_l", PerfCountersBuilder::PRIO_INTERESTING); | |
3849 | b.add_time_avg(l_bluestore_state_io_done_lat, "state_io_done_lat", | |
3850 | "Average io_done state latency"); | |
3851 | b.add_time_avg(l_bluestore_state_kv_queued_lat, "state_kv_queued_lat", | |
3852 | "Average kv_queued state latency"); | |
3853 | b.add_time_avg(l_bluestore_state_kv_committing_lat, "state_kv_commiting_lat", | |
3854 | "Average kv_commiting state latency"); | |
3855 | b.add_time_avg(l_bluestore_state_kv_done_lat, "state_kv_done_lat", | |
3856 | "Average kv_done state latency"); | |
3857 | b.add_time_avg(l_bluestore_state_deferred_queued_lat, "state_deferred_queued_lat", | |
3858 | "Average deferred_queued state latency"); | |
3859 | b.add_time_avg(l_bluestore_state_deferred_aio_wait_lat, "state_deferred_aio_wait_lat", | |
3860 | "Average aio_wait state latency"); | |
3861 | b.add_time_avg(l_bluestore_state_deferred_cleanup_lat, "state_deferred_cleanup_lat", | |
3862 | "Average cleanup state latency"); | |
3863 | b.add_time_avg(l_bluestore_state_finishing_lat, "state_finishing_lat", | |
3864 | "Average finishing state latency"); | |
3865 | b.add_time_avg(l_bluestore_state_done_lat, "state_done_lat", | |
3866 | "Average done state latency"); | |
3867 | b.add_time_avg(l_bluestore_throttle_lat, "throttle_lat", | |
3868 | "Average submit throttle latency", | |
3869 | "th_l", PerfCountersBuilder::PRIO_CRITICAL); | |
3870 | b.add_time_avg(l_bluestore_submit_lat, "submit_lat", | |
3871 | "Average submit latency", | |
3872 | "s_l", PerfCountersBuilder::PRIO_CRITICAL); | |
3873 | b.add_time_avg(l_bluestore_commit_lat, "commit_lat", | |
3874 | "Average commit latency", | |
3875 | "c_l", PerfCountersBuilder::PRIO_CRITICAL); | |
3876 | b.add_time_avg(l_bluestore_read_lat, "read_lat", | |
3877 | "Average read latency", | |
3878 | "r_l", PerfCountersBuilder::PRIO_CRITICAL); | |
3879 | b.add_time_avg(l_bluestore_read_onode_meta_lat, "read_onode_meta_lat", | |
3880 | "Average read onode metadata latency"); | |
3881 | b.add_time_avg(l_bluestore_read_wait_aio_lat, "read_wait_aio_lat", | |
3882 | "Average read latency"); | |
3883 | b.add_time_avg(l_bluestore_compress_lat, "compress_lat", | |
3884 | "Average compress latency"); | |
3885 | b.add_time_avg(l_bluestore_decompress_lat, "decompress_lat", | |
3886 | "Average decompress latency"); | |
3887 | b.add_time_avg(l_bluestore_csum_lat, "csum_lat", | |
3888 | "Average checksum latency"); | |
3889 | b.add_u64_counter(l_bluestore_compress_success_count, "compress_success_count", | |
3890 | "Sum for beneficial compress ops"); | |
3891 | b.add_u64_counter(l_bluestore_compress_rejected_count, "compress_rejected_count", | |
3892 | "Sum for compress ops rejected due to low net gain of space"); | |
3893 | b.add_u64_counter(l_bluestore_write_pad_bytes, "write_pad_bytes", | |
3894 | "Sum for write-op padded bytes"); | |
3895 | b.add_u64_counter(l_bluestore_deferred_write_ops, "deferred_write_ops", | |
3896 | "Sum for deferred write op"); | |
3897 | b.add_u64_counter(l_bluestore_deferred_write_bytes, "deferred_write_bytes", | |
3898 | "Sum for deferred write bytes", "def"); | |
3899 | b.add_u64_counter(l_bluestore_write_penalty_read_ops, "write_penalty_read_ops", | |
3900 | "Sum for write penalty read ops"); | |
3901 | b.add_u64(l_bluestore_allocated, "bluestore_allocated", | |
3902 | "Sum for allocated bytes"); | |
3903 | b.add_u64(l_bluestore_stored, "bluestore_stored", | |
3904 | "Sum for stored bytes"); | |
3905 | b.add_u64(l_bluestore_compressed, "bluestore_compressed", | |
3906 | "Sum for stored compressed bytes"); | |
3907 | b.add_u64(l_bluestore_compressed_allocated, "bluestore_compressed_allocated", | |
3908 | "Sum for bytes allocated for compressed data"); | |
3909 | b.add_u64(l_bluestore_compressed_original, "bluestore_compressed_original", | |
3910 | "Sum for original bytes that were compressed"); | |
3911 | ||
3912 | b.add_u64(l_bluestore_onodes, "bluestore_onodes", | |
3913 | "Number of onodes in cache"); | |
3914 | b.add_u64_counter(l_bluestore_onode_hits, "bluestore_onode_hits", | |
3915 | "Sum for onode-lookups hit in the cache"); | |
3916 | b.add_u64_counter(l_bluestore_onode_misses, "bluestore_onode_misses", | |
3917 | "Sum for onode-lookups missed in the cache"); | |
3918 | b.add_u64_counter(l_bluestore_onode_shard_hits, "bluestore_onode_shard_hits", | |
3919 | "Sum for onode-shard lookups hit in the cache"); | |
3920 | b.add_u64_counter(l_bluestore_onode_shard_misses, | |
3921 | "bluestore_onode_shard_misses", | |
3922 | "Sum for onode-shard lookups missed in the cache"); | |
3923 | b.add_u64(l_bluestore_extents, "bluestore_extents", | |
3924 | "Number of extents in cache"); | |
3925 | b.add_u64(l_bluestore_blobs, "bluestore_blobs", | |
3926 | "Number of blobs in cache"); | |
3927 | b.add_u64(l_bluestore_buffers, "bluestore_buffers", | |
3928 | "Number of buffers in cache"); | |
3929 | b.add_u64(l_bluestore_buffer_bytes, "bluestore_buffer_bytes", | |
3930 | "Number of buffer bytes in cache"); | |
3931 | b.add_u64(l_bluestore_buffer_hit_bytes, "bluestore_buffer_hit_bytes", | |
3932 | "Sum for bytes of read hit in the cache"); | |
3933 | b.add_u64(l_bluestore_buffer_miss_bytes, "bluestore_buffer_miss_bytes", | |
3934 | "Sum for bytes of read missed in the cache"); | |
3935 | ||
3936 | b.add_u64_counter(l_bluestore_write_big, "bluestore_write_big", | |
3937 | "Large aligned writes into fresh blobs"); | |
3938 | b.add_u64_counter(l_bluestore_write_big_bytes, "bluestore_write_big_bytes", | |
3939 | "Large aligned writes into fresh blobs (bytes)"); | |
3940 | b.add_u64_counter(l_bluestore_write_big_blobs, "bluestore_write_big_blobs", | |
3941 | "Large aligned writes into fresh blobs (blobs)"); | |
3942 | b.add_u64_counter(l_bluestore_write_small, "bluestore_write_small", | |
3943 | "Small writes into existing or sparse small blobs"); | |
3944 | b.add_u64_counter(l_bluestore_write_small_bytes, "bluestore_write_small_bytes", | |
3945 | "Small writes into existing or sparse small blobs (bytes)"); | |
3946 | b.add_u64_counter(l_bluestore_write_small_unused, | |
3947 | "bluestore_write_small_unused", | |
3948 | "Small writes into unused portion of existing blob"); | |
3949 | b.add_u64_counter(l_bluestore_write_small_deferred, | |
3950 | "bluestore_write_small_deferred", | |
3951 | "Small overwrites using deferred"); | |
3952 | b.add_u64_counter(l_bluestore_write_small_pre_read, | |
3953 | "bluestore_write_small_pre_read", | |
3954 | "Small writes that required we read some data (possibly " | |
3955 | "cached) to fill out the block"); | |
3956 | b.add_u64_counter(l_bluestore_write_small_new, "bluestore_write_small_new", | |
3957 | "Small write into new (sparse) blob"); | |
3958 | ||
3959 | b.add_u64_counter(l_bluestore_txc, "bluestore_txc", "Transactions committed"); | |
3960 | b.add_u64_counter(l_bluestore_onode_reshard, "bluestore_onode_reshard", | |
3961 | "Onode extent map reshard events"); | |
3962 | b.add_u64_counter(l_bluestore_blob_split, "bluestore_blob_split", | |
3963 | "Sum for blob splitting due to resharding"); | |
3964 | b.add_u64_counter(l_bluestore_extent_compress, "bluestore_extent_compress", | |
3965 | "Sum for extents that have been removed due to compression"); | |
3966 | b.add_u64_counter(l_bluestore_gc_merged, "bluestore_gc_merged", | |
3967 | "Sum for extents that have been merged due to garbage " | |
3968 | "collection"); | |
3969 | b.add_u64_counter(l_bluestore_read_eio, "bluestore_read_eio", | |
3970 | "Read EIO errors propagated to high level callers"); | |
3971 | logger = b.create_perf_counters(); | |
3972 | cct->get_perfcounters_collection()->add(logger); | |
3973 | } | |
3974 | ||
3975 | int BlueStore::_reload_logger() | |
3976 | { | |
3977 | struct store_statfs_t store_statfs; | |
3978 | ||
3979 | int r = statfs(&store_statfs); | |
3980 | if(r >= 0) { | |
3981 | logger->set(l_bluestore_allocated, store_statfs.allocated); | |
3982 | logger->set(l_bluestore_stored, store_statfs.stored); | |
3983 | logger->set(l_bluestore_compressed, store_statfs.compressed); | |
3984 | logger->set(l_bluestore_compressed_allocated, store_statfs.compressed_allocated); | |
3985 | logger->set(l_bluestore_compressed_original, store_statfs.compressed_original); | |
3986 | } | |
3987 | return r; | |
3988 | } | |
3989 | ||
3990 | void BlueStore::_shutdown_logger() | |
3991 | { | |
3992 | cct->get_perfcounters_collection()->remove(logger); | |
3993 | delete logger; | |
3994 | } | |
3995 | ||
3996 | int BlueStore::get_block_device_fsid(CephContext* cct, const string& path, | |
3997 | uuid_d *fsid) | |
3998 | { | |
3999 | bluestore_bdev_label_t label; | |
4000 | int r = _read_bdev_label(cct, path, &label); | |
4001 | if (r < 0) | |
4002 | return r; | |
4003 | *fsid = label.osd_uuid; | |
4004 | return 0; | |
4005 | } | |
4006 | ||
4007 | int BlueStore::_open_path() | |
4008 | { | |
4009 | // sanity check(s) | |
4010 | if (cct->_conf->get_val<uint64_t>("osd_max_object_size") >= | |
4011 | 4*1024*1024*1024ull) { | |
4012 | derr << __func__ << " osd_max_object_size >= 4GB; BlueStore has hard limit of 4GB." << dendl; | |
4013 | return -EINVAL; | |
4014 | } | |
4015 | assert(path_fd < 0); | |
4016 | path_fd = TEMP_FAILURE_RETRY(::open(path.c_str(), O_DIRECTORY)); | |
4017 | if (path_fd < 0) { | |
4018 | int r = -errno; | |
4019 | derr << __func__ << " unable to open " << path << ": " << cpp_strerror(r) | |
4020 | << dendl; | |
4021 | return r; | |
4022 | } | |
4023 | return 0; | |
4024 | } | |
4025 | ||
4026 | void BlueStore::_close_path() | |
4027 | { | |
4028 | VOID_TEMP_FAILURE_RETRY(::close(path_fd)); | |
4029 | path_fd = -1; | |
4030 | } | |
4031 | ||
4032 | int BlueStore::_write_bdev_label(CephContext *cct, | |
4033 | string path, bluestore_bdev_label_t label) | |
4034 | { | |
4035 | dout(10) << __func__ << " path " << path << " label " << label << dendl; | |
4036 | bufferlist bl; | |
4037 | ::encode(label, bl); | |
4038 | uint32_t crc = bl.crc32c(-1); | |
4039 | ::encode(crc, bl); | |
4040 | assert(bl.length() <= BDEV_LABEL_BLOCK_SIZE); | |
4041 | bufferptr z(BDEV_LABEL_BLOCK_SIZE - bl.length()); | |
4042 | z.zero(); | |
4043 | bl.append(std::move(z)); | |
4044 | ||
4045 | int fd = TEMP_FAILURE_RETRY(::open(path.c_str(), O_WRONLY)); | |
4046 | if (fd < 0) { | |
4047 | fd = -errno; | |
4048 | derr << __func__ << " failed to open " << path << ": " << cpp_strerror(fd) | |
4049 | << dendl; | |
4050 | return fd; | |
4051 | } | |
4052 | int r = bl.write_fd(fd); | |
4053 | if (r < 0) { | |
4054 | derr << __func__ << " failed to write to " << path | |
4055 | << ": " << cpp_strerror(r) << dendl; | |
4056 | } | |
4057 | r = ::fsync(fd); | |
4058 | if (r < 0) { | |
4059 | derr << __func__ << " failed to fsync " << path | |
4060 | << ": " << cpp_strerror(r) << dendl; | |
4061 | } | |
4062 | VOID_TEMP_FAILURE_RETRY(::close(fd)); | |
4063 | return r; | |
4064 | } | |
4065 | ||
4066 | int BlueStore::_read_bdev_label(CephContext* cct, string path, | |
4067 | bluestore_bdev_label_t *label) | |
4068 | { | |
4069 | dout(10) << __func__ << dendl; | |
4070 | int fd = TEMP_FAILURE_RETRY(::open(path.c_str(), O_RDONLY)); | |
4071 | if (fd < 0) { | |
4072 | fd = -errno; | |
4073 | derr << __func__ << " failed to open " << path << ": " << cpp_strerror(fd) | |
4074 | << dendl; | |
4075 | return fd; | |
4076 | } | |
4077 | bufferlist bl; | |
4078 | int r = bl.read_fd(fd, BDEV_LABEL_BLOCK_SIZE); | |
4079 | VOID_TEMP_FAILURE_RETRY(::close(fd)); | |
4080 | if (r < 0) { | |
4081 | derr << __func__ << " failed to read from " << path | |
4082 | << ": " << cpp_strerror(r) << dendl; | |
4083 | return r; | |
4084 | } | |
4085 | ||
4086 | uint32_t crc, expected_crc; | |
4087 | bufferlist::iterator p = bl.begin(); | |
4088 | try { | |
4089 | ::decode(*label, p); | |
4090 | bufferlist t; | |
4091 | t.substr_of(bl, 0, p.get_off()); | |
4092 | crc = t.crc32c(-1); | |
4093 | ::decode(expected_crc, p); | |
4094 | } | |
4095 | catch (buffer::error& e) { | |
4096 | dout(2) << __func__ << " unable to decode label at offset " << p.get_off() | |
4097 | << ": " << e.what() | |
4098 | << dendl; | |
4099 | return -ENOENT; | |
4100 | } | |
4101 | if (crc != expected_crc) { | |
4102 | derr << __func__ << " bad crc on label, expected " << expected_crc | |
4103 | << " != actual " << crc << dendl; | |
4104 | return -EIO; | |
4105 | } | |
4106 | dout(10) << __func__ << " got " << *label << dendl; | |
4107 | return 0; | |
4108 | } | |
4109 | ||
4110 | int BlueStore::_check_or_set_bdev_label( | |
4111 | string path, uint64_t size, string desc, bool create) | |
4112 | { | |
4113 | bluestore_bdev_label_t label; | |
4114 | if (create) { | |
4115 | label.osd_uuid = fsid; | |
4116 | label.size = size; | |
4117 | label.btime = ceph_clock_now(); | |
4118 | label.description = desc; | |
4119 | int r = _write_bdev_label(cct, path, label); | |
4120 | if (r < 0) | |
4121 | return r; | |
4122 | } else { | |
4123 | int r = _read_bdev_label(cct, path, &label); | |
4124 | if (r < 0) | |
4125 | return r; | |
4126 | if (cct->_conf->bluestore_debug_permit_any_bdev_label) { | |
4127 | dout(20) << __func__ << " bdev " << path << " fsid " << label.osd_uuid | |
4128 | << " and fsid " << fsid << " check bypassed" << dendl; | |
4129 | } | |
4130 | else if (label.osd_uuid != fsid) { | |
4131 | derr << __func__ << " bdev " << path << " fsid " << label.osd_uuid | |
4132 | << " does not match our fsid " << fsid << dendl; | |
4133 | return -EIO; | |
4134 | } | |
4135 | } | |
4136 | return 0; | |
4137 | } | |
4138 | ||
4139 | void BlueStore::_set_alloc_sizes(void) | |
4140 | { | |
4141 | max_alloc_size = cct->_conf->bluestore_max_alloc_size; | |
4142 | ||
4143 | if (cct->_conf->bluestore_prefer_deferred_size) { | |
4144 | prefer_deferred_size = cct->_conf->bluestore_prefer_deferred_size; | |
4145 | } else { | |
4146 | assert(bdev); | |
4147 | if (bdev->is_rotational()) { | |
4148 | prefer_deferred_size = cct->_conf->bluestore_prefer_deferred_size_hdd; | |
4149 | } else { | |
4150 | prefer_deferred_size = cct->_conf->bluestore_prefer_deferred_size_ssd; | |
4151 | } | |
4152 | } | |
4153 | ||
4154 | if (cct->_conf->bluestore_deferred_batch_ops) { | |
4155 | deferred_batch_ops = cct->_conf->bluestore_deferred_batch_ops; | |
4156 | } else { | |
4157 | assert(bdev); | |
4158 | if (bdev->is_rotational()) { | |
4159 | deferred_batch_ops = cct->_conf->bluestore_deferred_batch_ops_hdd; | |
4160 | } else { | |
4161 | deferred_batch_ops = cct->_conf->bluestore_deferred_batch_ops_ssd; | |
4162 | } | |
4163 | } | |
4164 | ||
4165 | dout(10) << __func__ << " min_alloc_size 0x" << std::hex << min_alloc_size | |
4166 | << std::dec << " order " << min_alloc_size_order | |
4167 | << " max_alloc_size 0x" << std::hex << max_alloc_size | |
4168 | << " prefer_deferred_size 0x" << prefer_deferred_size | |
4169 | << std::dec | |
4170 | << " deferred_batch_ops " << deferred_batch_ops | |
4171 | << dendl; | |
4172 | } | |
4173 | ||
4174 | int BlueStore::_open_bdev(bool create) | |
4175 | { | |
4176 | assert(bdev == NULL); | |
4177 | string p = path + "/block"; | |
4178 | bdev = BlockDevice::create(cct, p, aio_cb, static_cast<void*>(this)); | |
4179 | int r = bdev->open(p); | |
4180 | if (r < 0) | |
4181 | goto fail; | |
4182 | ||
4183 | if (bdev->supported_bdev_label()) { | |
4184 | r = _check_or_set_bdev_label(p, bdev->get_size(), "main", create); | |
4185 | if (r < 0) | |
4186 | goto fail_close; | |
4187 | } | |
4188 | ||
4189 | // initialize global block parameters | |
4190 | block_size = bdev->get_block_size(); | |
4191 | block_mask = ~(block_size - 1); | |
4192 | block_size_order = ctz(block_size); | |
4193 | assert(block_size == 1u << block_size_order); | |
4194 | // and set cache_size based on device type | |
4195 | r = _set_cache_sizes(); | |
4196 | if (r < 0) { | |
4197 | goto fail_close; | |
4198 | } | |
4199 | return 0; | |
4200 | ||
4201 | fail_close: | |
4202 | bdev->close(); | |
4203 | fail: | |
4204 | delete bdev; | |
4205 | bdev = NULL; | |
4206 | return r; | |
4207 | } | |
4208 | ||
4209 | void BlueStore::_close_bdev() | |
4210 | { | |
4211 | assert(bdev); | |
4212 | bdev->close(); | |
4213 | delete bdev; | |
4214 | bdev = NULL; | |
4215 | } | |
4216 | ||
4217 | int BlueStore::_open_fm(bool create) | |
4218 | { | |
4219 | assert(fm == NULL); | |
4220 | fm = FreelistManager::create(cct, freelist_type, db, PREFIX_ALLOC); | |
4221 | ||
4222 | if (create) { | |
4223 | // initialize freespace | |
4224 | dout(20) << __func__ << " initializing freespace" << dendl; | |
4225 | KeyValueDB::Transaction t = db->get_transaction(); | |
4226 | { | |
4227 | bufferlist bl; | |
4228 | bl.append(freelist_type); | |
4229 | t->set(PREFIX_SUPER, "freelist_type", bl); | |
4230 | } | |
4231 | // being able to allocate in units less than bdev block size | |
4232 | // seems to be a bad idea. | |
4233 | assert( cct->_conf->bdev_block_size <= (int64_t)min_alloc_size); | |
4234 | fm->create(bdev->get_size(), (int64_t)min_alloc_size, t); | |
4235 | ||
4236 | // allocate superblock reserved space. note that we do not mark | |
4237 | // bluefs space as allocated in the freelist; we instead rely on | |
4238 | // bluefs_extents. | |
4239 | uint64_t reserved = ROUND_UP_TO(MAX(SUPER_RESERVED, min_alloc_size), | |
4240 | min_alloc_size); | |
4241 | fm->allocate(0, reserved, t); | |
4242 | ||
4243 | if (cct->_conf->bluestore_bluefs) { | |
4244 | assert(bluefs_extents.num_intervals() == 1); | |
4245 | interval_set<uint64_t>::iterator p = bluefs_extents.begin(); | |
4246 | reserved = ROUND_UP_TO(p.get_start() + p.get_len(), min_alloc_size); | |
4247 | dout(20) << __func__ << " reserved 0x" << std::hex << reserved << std::dec | |
4248 | << " for bluefs" << dendl; | |
4249 | bufferlist bl; | |
4250 | ::encode(bluefs_extents, bl); | |
4251 | t->set(PREFIX_SUPER, "bluefs_extents", bl); | |
4252 | dout(20) << __func__ << " bluefs_extents 0x" << std::hex << bluefs_extents | |
4253 | << std::dec << dendl; | |
4254 | } | |
4255 | ||
4256 | if (cct->_conf->bluestore_debug_prefill > 0) { | |
4257 | uint64_t end = bdev->get_size() - reserved; | |
4258 | dout(1) << __func__ << " pre-fragmenting freespace, using " | |
4259 | << cct->_conf->bluestore_debug_prefill << " with max free extent " | |
4260 | << cct->_conf->bluestore_debug_prefragment_max << dendl; | |
4261 | uint64_t start = P2ROUNDUP(reserved, min_alloc_size); | |
4262 | uint64_t max_b = cct->_conf->bluestore_debug_prefragment_max / min_alloc_size; | |
4263 | float r = cct->_conf->bluestore_debug_prefill; | |
4264 | r /= 1.0 - r; | |
4265 | bool stop = false; | |
4266 | ||
4267 | while (!stop && start < end) { | |
4268 | uint64_t l = (rand() % max_b + 1) * min_alloc_size; | |
4269 | if (start + l > end) { | |
4270 | l = end - start; | |
4271 | l = P2ALIGN(l, min_alloc_size); | |
4272 | } | |
4273 | assert(start + l <= end); | |
4274 | ||
4275 | uint64_t u = 1 + (uint64_t)(r * (double)l); | |
4276 | u = P2ROUNDUP(u, min_alloc_size); | |
4277 | if (start + l + u > end) { | |
4278 | u = end - (start + l); | |
4279 | // trim to align so we don't overflow again | |
4280 | u = P2ALIGN(u, min_alloc_size); | |
4281 | stop = true; | |
4282 | } | |
4283 | assert(start + l + u <= end); | |
4284 | ||
4285 | dout(20) << " free 0x" << std::hex << start << "~" << l | |
4286 | << " use 0x" << u << std::dec << dendl; | |
4287 | ||
4288 | if (u == 0) { | |
4289 | // break if u has been trimmed to nothing | |
4290 | break; | |
4291 | } | |
4292 | ||
4293 | fm->allocate(start + l, u, t); | |
4294 | start += l + u; | |
4295 | } | |
4296 | } | |
4297 | db->submit_transaction_sync(t); | |
4298 | } | |
4299 | ||
4300 | int r = fm->init(bdev->get_size()); | |
4301 | if (r < 0) { | |
4302 | derr << __func__ << " freelist init failed: " << cpp_strerror(r) << dendl; | |
4303 | delete fm; | |
4304 | fm = NULL; | |
4305 | return r; | |
4306 | } | |
4307 | return 0; | |
4308 | } | |
4309 | ||
4310 | void BlueStore::_close_fm() | |
4311 | { | |
4312 | dout(10) << __func__ << dendl; | |
4313 | assert(fm); | |
4314 | fm->shutdown(); | |
4315 | delete fm; | |
4316 | fm = NULL; | |
4317 | } | |
4318 | ||
4319 | int BlueStore::_open_alloc() | |
4320 | { | |
4321 | assert(alloc == NULL); | |
4322 | assert(bdev->get_size()); | |
4323 | alloc = Allocator::create(cct, cct->_conf->bluestore_allocator, | |
4324 | bdev->get_size(), | |
4325 | min_alloc_size); | |
4326 | if (!alloc) { | |
4327 | lderr(cct) << __func__ << " Allocator::unknown alloc type " | |
4328 | << cct->_conf->bluestore_allocator | |
4329 | << dendl; | |
4330 | return -EINVAL; | |
4331 | } | |
4332 | ||
4333 | uint64_t num = 0, bytes = 0; | |
4334 | ||
4335 | dout(1) << __func__ << " opening allocation metadata" << dendl; | |
4336 | // initialize from freelist | |
4337 | fm->enumerate_reset(); | |
4338 | uint64_t offset, length; | |
4339 | while (fm->enumerate_next(&offset, &length)) { | |
4340 | alloc->init_add_free(offset, length); | |
4341 | ++num; | |
4342 | bytes += length; | |
4343 | } | |
4344 | fm->enumerate_reset(); | |
4345 | dout(1) << __func__ << " loaded " << pretty_si_t(bytes) | |
4346 | << " in " << num << " extents" | |
4347 | << dendl; | |
4348 | ||
4349 | // also mark bluefs space as allocated | |
4350 | for (auto e = bluefs_extents.begin(); e != bluefs_extents.end(); ++e) { | |
4351 | alloc->init_rm_free(e.get_start(), e.get_len()); | |
4352 | } | |
4353 | dout(10) << __func__ << " marked bluefs_extents 0x" << std::hex | |
4354 | << bluefs_extents << std::dec << " as allocated" << dendl; | |
4355 | ||
4356 | return 0; | |
4357 | } | |
4358 | ||
4359 | void BlueStore::_close_alloc() | |
4360 | { | |
4361 | assert(alloc); | |
4362 | alloc->shutdown(); | |
4363 | delete alloc; | |
4364 | alloc = NULL; | |
4365 | } | |
4366 | ||
4367 | int BlueStore::_open_fsid(bool create) | |
4368 | { | |
4369 | assert(fsid_fd < 0); | |
4370 | int flags = O_RDWR; | |
4371 | if (create) | |
4372 | flags |= O_CREAT; | |
4373 | fsid_fd = ::openat(path_fd, "fsid", flags, 0644); | |
4374 | if (fsid_fd < 0) { | |
4375 | int err = -errno; | |
4376 | derr << __func__ << " " << cpp_strerror(err) << dendl; | |
4377 | return err; | |
4378 | } | |
4379 | return 0; | |
4380 | } | |
4381 | ||
4382 | int BlueStore::_read_fsid(uuid_d *uuid) | |
4383 | { | |
4384 | char fsid_str[40]; | |
4385 | memset(fsid_str, 0, sizeof(fsid_str)); | |
4386 | int ret = safe_read(fsid_fd, fsid_str, sizeof(fsid_str)); | |
4387 | if (ret < 0) { | |
4388 | derr << __func__ << " failed: " << cpp_strerror(ret) << dendl; | |
4389 | return ret; | |
4390 | } | |
4391 | if (ret > 36) | |
4392 | fsid_str[36] = 0; | |
4393 | else | |
4394 | fsid_str[ret] = 0; | |
4395 | if (!uuid->parse(fsid_str)) { | |
4396 | derr << __func__ << " unparsable uuid " << fsid_str << dendl; | |
4397 | return -EINVAL; | |
4398 | } | |
4399 | return 0; | |
4400 | } | |
4401 | ||
4402 | int BlueStore::_write_fsid() | |
4403 | { | |
4404 | int r = ::ftruncate(fsid_fd, 0); | |
4405 | if (r < 0) { | |
4406 | r = -errno; | |
4407 | derr << __func__ << " fsid truncate failed: " << cpp_strerror(r) << dendl; | |
4408 | return r; | |
4409 | } | |
4410 | string str = stringify(fsid) + "\n"; | |
4411 | r = safe_write(fsid_fd, str.c_str(), str.length()); | |
4412 | if (r < 0) { | |
4413 | derr << __func__ << " fsid write failed: " << cpp_strerror(r) << dendl; | |
4414 | return r; | |
4415 | } | |
4416 | r = ::fsync(fsid_fd); | |
4417 | if (r < 0) { | |
4418 | r = -errno; | |
4419 | derr << __func__ << " fsid fsync failed: " << cpp_strerror(r) << dendl; | |
4420 | return r; | |
4421 | } | |
4422 | return 0; | |
4423 | } | |
4424 | ||
4425 | void BlueStore::_close_fsid() | |
4426 | { | |
4427 | VOID_TEMP_FAILURE_RETRY(::close(fsid_fd)); | |
4428 | fsid_fd = -1; | |
4429 | } | |
4430 | ||
4431 | int BlueStore::_lock_fsid() | |
4432 | { | |
4433 | struct flock l; | |
4434 | memset(&l, 0, sizeof(l)); | |
4435 | l.l_type = F_WRLCK; | |
4436 | l.l_whence = SEEK_SET; | |
4437 | int r = ::fcntl(fsid_fd, F_SETLK, &l); | |
4438 | if (r < 0) { | |
4439 | int err = errno; | |
4440 | derr << __func__ << " failed to lock " << path << "/fsid" | |
4441 | << " (is another ceph-osd still running?)" | |
4442 | << cpp_strerror(err) << dendl; | |
4443 | return -err; | |
4444 | } | |
4445 | return 0; | |
4446 | } | |
4447 | ||
4448 | bool BlueStore::is_rotational() | |
4449 | { | |
4450 | if (bdev) { | |
4451 | return bdev->is_rotational(); | |
4452 | } | |
4453 | ||
4454 | bool rotational = true; | |
4455 | int r = _open_path(); | |
4456 | if (r < 0) | |
4457 | goto out; | |
4458 | r = _open_fsid(false); | |
4459 | if (r < 0) | |
4460 | goto out_path; | |
4461 | r = _read_fsid(&fsid); | |
4462 | if (r < 0) | |
4463 | goto out_fsid; | |
4464 | r = _lock_fsid(); | |
4465 | if (r < 0) | |
4466 | goto out_fsid; | |
4467 | r = _open_bdev(false); | |
4468 | if (r < 0) | |
4469 | goto out_fsid; | |
4470 | rotational = bdev->is_rotational(); | |
4471 | _close_bdev(); | |
4472 | out_fsid: | |
4473 | _close_fsid(); | |
4474 | out_path: | |
4475 | _close_path(); | |
4476 | out: | |
4477 | return rotational; | |
4478 | } | |
4479 | ||
4480 | bool BlueStore::is_journal_rotational() | |
4481 | { | |
4482 | if (!bluefs) { | |
4483 | dout(5) << __func__ << " bluefs disabled, default to store media type" | |
4484 | << dendl; | |
4485 | return is_rotational(); | |
4486 | } | |
4487 | dout(10) << __func__ << " " << (int)bluefs->wal_is_rotational() << dendl; | |
4488 | return bluefs->wal_is_rotational(); | |
4489 | } | |
4490 | ||
4491 | bool BlueStore::test_mount_in_use() | |
4492 | { | |
4493 | // most error conditions mean the mount is not in use (e.g., because | |
4494 | // it doesn't exist). only if we fail to lock do we conclude it is | |
4495 | // in use. | |
4496 | bool ret = false; | |
4497 | int r = _open_path(); | |
4498 | if (r < 0) | |
4499 | return false; | |
4500 | r = _open_fsid(false); | |
4501 | if (r < 0) | |
4502 | goto out_path; | |
4503 | r = _lock_fsid(); | |
4504 | if (r < 0) | |
4505 | ret = true; // if we can't lock, it is in use | |
4506 | _close_fsid(); | |
4507 | out_path: | |
4508 | _close_path(); | |
4509 | return ret; | |
4510 | } | |
4511 | ||
4512 | int BlueStore::_open_db(bool create) | |
4513 | { | |
4514 | int r; | |
4515 | assert(!db); | |
4516 | string fn = path + "/db"; | |
4517 | string options; | |
4518 | stringstream err; | |
4519 | ceph::shared_ptr<Int64ArrayMergeOperator> merge_op(new Int64ArrayMergeOperator); | |
4520 | ||
4521 | string kv_backend; | |
4522 | if (create) { | |
4523 | kv_backend = cct->_conf->bluestore_kvbackend; | |
4524 | } else { | |
4525 | r = read_meta("kv_backend", &kv_backend); | |
4526 | if (r < 0) { | |
4527 | derr << __func__ << " unable to read 'kv_backend' meta" << dendl; | |
4528 | return -EIO; | |
4529 | } | |
4530 | } | |
4531 | dout(10) << __func__ << " kv_backend = " << kv_backend << dendl; | |
4532 | ||
4533 | bool do_bluefs; | |
4534 | if (create) { | |
4535 | do_bluefs = cct->_conf->bluestore_bluefs; | |
4536 | } else { | |
4537 | string s; | |
4538 | r = read_meta("bluefs", &s); | |
4539 | if (r < 0) { | |
4540 | derr << __func__ << " unable to read 'bluefs' meta" << dendl; | |
4541 | return -EIO; | |
4542 | } | |
4543 | if (s == "1") { | |
4544 | do_bluefs = true; | |
4545 | } else if (s == "0") { | |
4546 | do_bluefs = false; | |
4547 | } else { | |
4548 | derr << __func__ << " bluefs = " << s << " : not 0 or 1, aborting" | |
4549 | << dendl; | |
4550 | return -EIO; | |
4551 | } | |
4552 | } | |
4553 | dout(10) << __func__ << " do_bluefs = " << do_bluefs << dendl; | |
4554 | ||
4555 | rocksdb::Env *env = NULL; | |
4556 | if (do_bluefs) { | |
4557 | dout(10) << __func__ << " initializing bluefs" << dendl; | |
4558 | if (kv_backend != "rocksdb") { | |
4559 | derr << " backend must be rocksdb to use bluefs" << dendl; | |
4560 | return -EINVAL; | |
4561 | } | |
4562 | bluefs = new BlueFS(cct); | |
4563 | ||
4564 | string bfn; | |
4565 | struct stat st; | |
4566 | ||
4567 | if (read_meta("path_block.db", &bfn) < 0) { | |
4568 | bfn = path + "/block.db"; | |
4569 | } | |
4570 | if (::stat(bfn.c_str(), &st) == 0) { | |
4571 | r = bluefs->add_block_device(BlueFS::BDEV_DB, bfn); | |
4572 | if (r < 0) { | |
4573 | derr << __func__ << " add block device(" << bfn << ") returned: " | |
4574 | << cpp_strerror(r) << dendl; | |
4575 | goto free_bluefs; | |
4576 | } | |
4577 | ||
4578 | if (bluefs->bdev_support_label(BlueFS::BDEV_DB)) { | |
4579 | r = _check_or_set_bdev_label( | |
4580 | bfn, | |
4581 | bluefs->get_block_device_size(BlueFS::BDEV_DB), | |
4582 | "bluefs db", create); | |
4583 | if (r < 0) { | |
4584 | derr << __func__ | |
4585 | << " check block device(" << bfn << ") label returned: " | |
4586 | << cpp_strerror(r) << dendl; | |
4587 | goto free_bluefs; | |
4588 | } | |
4589 | } | |
4590 | if (create) { | |
4591 | bluefs->add_block_extent( | |
4592 | BlueFS::BDEV_DB, | |
4593 | SUPER_RESERVED, | |
4594 | bluefs->get_block_device_size(BlueFS::BDEV_DB) - SUPER_RESERVED); | |
4595 | } | |
4596 | bluefs_shared_bdev = BlueFS::BDEV_SLOW; | |
4597 | bluefs_single_shared_device = false; | |
4598 | } else if (::lstat(bfn.c_str(), &st) == -1) { | |
4599 | bluefs_shared_bdev = BlueFS::BDEV_DB; | |
4600 | } else { | |
4601 | //symlink exist is bug | |
4602 | derr << __func__ << " " << bfn << " link target doesn't exist" << dendl; | |
4603 | r = -errno; | |
4604 | goto free_bluefs; | |
4605 | } | |
4606 | ||
4607 | // shared device | |
4608 | if (read_meta("path_block", &bfn) < 0) { | |
4609 | bfn = path + "/block"; | |
4610 | } | |
4611 | r = bluefs->add_block_device(bluefs_shared_bdev, bfn); | |
4612 | if (r < 0) { | |
4613 | derr << __func__ << " add block device(" << bfn << ") returned: " | |
4614 | << cpp_strerror(r) << dendl; | |
4615 | goto free_bluefs; | |
4616 | } | |
4617 | if (create) { | |
4618 | // note: we always leave the first SUPER_RESERVED (8k) of the device unused | |
4619 | uint64_t initial = | |
4620 | bdev->get_size() * (cct->_conf->bluestore_bluefs_min_ratio + | |
4621 | cct->_conf->bluestore_bluefs_gift_ratio); | |
4622 | initial = MAX(initial, cct->_conf->bluestore_bluefs_min); | |
4623 | if (cct->_conf->bluefs_alloc_size % min_alloc_size) { | |
4624 | derr << __func__ << " bluefs_alloc_size 0x" << std::hex | |
4625 | << cct->_conf->bluefs_alloc_size << " is not a multiple of " | |
4626 | << "min_alloc_size 0x" << min_alloc_size << std::dec << dendl; | |
4627 | r = -EINVAL; | |
4628 | goto free_bluefs; | |
4629 | } | |
4630 | // align to bluefs's alloc_size | |
4631 | initial = P2ROUNDUP(initial, cct->_conf->bluefs_alloc_size); | |
4632 | // put bluefs in the middle of the device in case it is an HDD | |
4633 | uint64_t start = P2ALIGN((bdev->get_size() - initial) / 2, | |
4634 | cct->_conf->bluefs_alloc_size); | |
4635 | bluefs->add_block_extent(bluefs_shared_bdev, start, initial); | |
4636 | bluefs_extents.insert(start, initial); | |
4637 | } | |
4638 | ||
4639 | if (read_meta("path_block.wal", &bfn) < 0) { | |
4640 | bfn = path + "/block.wal"; | |
4641 | } | |
4642 | if (::stat(bfn.c_str(), &st) == 0) { | |
4643 | r = bluefs->add_block_device(BlueFS::BDEV_WAL, bfn); | |
4644 | if (r < 0) { | |
4645 | derr << __func__ << " add block device(" << bfn << ") returned: " | |
4646 | << cpp_strerror(r) << dendl; | |
4647 | goto free_bluefs; | |
4648 | } | |
4649 | ||
4650 | if (bluefs->bdev_support_label(BlueFS::BDEV_WAL)) { | |
4651 | r = _check_or_set_bdev_label( | |
4652 | bfn, | |
4653 | bluefs->get_block_device_size(BlueFS::BDEV_WAL), | |
4654 | "bluefs wal", create); | |
4655 | if (r < 0) { | |
4656 | derr << __func__ << " check block device(" << bfn | |
4657 | << ") label returned: " << cpp_strerror(r) << dendl; | |
4658 | goto free_bluefs; | |
4659 | } | |
4660 | } | |
4661 | ||
4662 | if (create) { | |
4663 | bluefs->add_block_extent( | |
4664 | BlueFS::BDEV_WAL, BDEV_LABEL_BLOCK_SIZE, | |
4665 | bluefs->get_block_device_size(BlueFS::BDEV_WAL) - | |
4666 | BDEV_LABEL_BLOCK_SIZE); | |
4667 | } | |
4668 | cct->_conf->set_val("rocksdb_separate_wal_dir", "true"); | |
4669 | bluefs_single_shared_device = false; | |
4670 | } else if (::lstat(bfn.c_str(), &st) == -1) { | |
4671 | cct->_conf->set_val("rocksdb_separate_wal_dir", "false"); | |
4672 | } else { | |
4673 | //symlink exist is bug | |
4674 | derr << __func__ << " " << bfn << " link target doesn't exist" << dendl; | |
4675 | r = -errno; | |
4676 | goto free_bluefs; | |
4677 | } | |
4678 | ||
4679 | if (create) { | |
4680 | bluefs->mkfs(fsid); | |
4681 | } | |
4682 | r = bluefs->mount(); | |
4683 | if (r < 0) { | |
4684 | derr << __func__ << " failed bluefs mount: " << cpp_strerror(r) << dendl; | |
4685 | goto free_bluefs; | |
4686 | } | |
4687 | if (cct->_conf->bluestore_bluefs_env_mirror) { | |
4688 | rocksdb::Env *a = new BlueRocksEnv(bluefs); | |
4689 | rocksdb::Env *b = rocksdb::Env::Default(); | |
4690 | if (create) { | |
4691 | string cmd = "rm -rf " + path + "/db " + | |
4692 | path + "/db.slow " + | |
4693 | path + "/db.wal"; | |
4694 | int r = system(cmd.c_str()); | |
4695 | (void)r; | |
4696 | } | |
4697 | env = new rocksdb::EnvMirror(b, a, false, true); | |
4698 | } else { | |
4699 | env = new BlueRocksEnv(bluefs); | |
4700 | ||
4701 | // simplify the dir names, too, as "seen" by rocksdb | |
4702 | fn = "db"; | |
4703 | } | |
4704 | ||
4705 | if (bluefs_shared_bdev == BlueFS::BDEV_SLOW) { | |
4706 | // we have both block.db and block; tell rocksdb! | |
4707 | // note: the second (last) size value doesn't really matter | |
4708 | ostringstream db_paths; | |
4709 | uint64_t db_size = bluefs->get_block_device_size(BlueFS::BDEV_DB); | |
4710 | uint64_t slow_size = bluefs->get_block_device_size(BlueFS::BDEV_SLOW); | |
4711 | db_paths << fn << "," | |
4712 | << (uint64_t)(db_size * 95 / 100) << " " | |
4713 | << fn + ".slow" << "," | |
4714 | << (uint64_t)(slow_size * 95 / 100); | |
4715 | cct->_conf->set_val("rocksdb_db_paths", db_paths.str(), false); | |
4716 | dout(10) << __func__ << " set rocksdb_db_paths to " | |
4717 | << cct->_conf->get_val<std::string>("rocksdb_db_paths") << dendl; | |
4718 | } | |
4719 | ||
4720 | if (create) { | |
4721 | env->CreateDir(fn); | |
4722 | if (cct->_conf->rocksdb_separate_wal_dir) | |
4723 | env->CreateDir(fn + ".wal"); | |
4724 | if (cct->_conf->get_val<std::string>("rocksdb_db_paths").length()) | |
4725 | env->CreateDir(fn + ".slow"); | |
4726 | } | |
4727 | } else if (create) { | |
4728 | int r = ::mkdir(fn.c_str(), 0755); | |
4729 | if (r < 0) | |
4730 | r = -errno; | |
4731 | if (r < 0 && r != -EEXIST) { | |
4732 | derr << __func__ << " failed to create " << fn << ": " << cpp_strerror(r) | |
4733 | << dendl; | |
4734 | return r; | |
4735 | } | |
4736 | ||
4737 | // wal_dir, too! | |
4738 | if (cct->_conf->rocksdb_separate_wal_dir) { | |
4739 | string walfn = path + "/db.wal"; | |
4740 | r = ::mkdir(walfn.c_str(), 0755); | |
4741 | if (r < 0) | |
4742 | r = -errno; | |
4743 | if (r < 0 && r != -EEXIST) { | |
4744 | derr << __func__ << " failed to create " << walfn | |
4745 | << ": " << cpp_strerror(r) | |
4746 | << dendl; | |
4747 | return r; | |
4748 | } | |
4749 | } | |
4750 | } | |
4751 | ||
4752 | db = KeyValueDB::create(cct, | |
4753 | kv_backend, | |
4754 | fn, | |
4755 | static_cast<void*>(env)); | |
4756 | if (!db) { | |
4757 | derr << __func__ << " error creating db" << dendl; | |
4758 | if (bluefs) { | |
4759 | bluefs->umount(); | |
4760 | delete bluefs; | |
4761 | bluefs = NULL; | |
4762 | } | |
4763 | // delete env manually here since we can't depend on db to do this | |
4764 | // under this case | |
4765 | delete env; | |
4766 | env = NULL; | |
4767 | return -EIO; | |
4768 | } | |
4769 | ||
4770 | FreelistManager::setup_merge_operators(db); | |
4771 | db->set_merge_operator(PREFIX_STAT, merge_op); | |
4772 | ||
4773 | db->set_cache_size(cache_size * cache_kv_ratio); | |
4774 | ||
4775 | if (kv_backend == "rocksdb") | |
4776 | options = cct->_conf->bluestore_rocksdb_options; | |
4777 | db->init(options); | |
4778 | if (create) | |
4779 | r = db->create_and_open(err); | |
4780 | else | |
4781 | r = db->open(err); | |
4782 | if (r) { | |
4783 | derr << __func__ << " erroring opening db: " << err.str() << dendl; | |
4784 | if (bluefs) { | |
4785 | bluefs->umount(); | |
4786 | delete bluefs; | |
4787 | bluefs = NULL; | |
4788 | } | |
4789 | delete db; | |
4790 | db = NULL; | |
4791 | return -EIO; | |
4792 | } | |
4793 | dout(1) << __func__ << " opened " << kv_backend | |
4794 | << " path " << fn << " options " << options << dendl; | |
4795 | return 0; | |
4796 | ||
4797 | free_bluefs: | |
4798 | assert(bluefs); | |
4799 | delete bluefs; | |
4800 | bluefs = NULL; | |
4801 | return r; | |
4802 | } | |
4803 | ||
4804 | void BlueStore::_close_db() | |
4805 | { | |
4806 | assert(db); | |
4807 | delete db; | |
4808 | db = NULL; | |
4809 | if (bluefs) { | |
4810 | bluefs->umount(); | |
4811 | delete bluefs; | |
4812 | bluefs = NULL; | |
4813 | } | |
4814 | } | |
4815 | ||
4816 | int BlueStore::_reconcile_bluefs_freespace() | |
4817 | { | |
4818 | dout(10) << __func__ << dendl; | |
4819 | interval_set<uint64_t> bset; | |
4820 | int r = bluefs->get_block_extents(bluefs_shared_bdev, &bset); | |
4821 | assert(r == 0); | |
4822 | if (bset == bluefs_extents) { | |
4823 | dout(10) << __func__ << " we agree bluefs has 0x" << std::hex << bset | |
4824 | << std::dec << dendl; | |
4825 | return 0; | |
4826 | } | |
4827 | dout(10) << __func__ << " bluefs says 0x" << std::hex << bset << std::dec | |
4828 | << dendl; | |
4829 | dout(10) << __func__ << " super says 0x" << std::hex << bluefs_extents | |
4830 | << std::dec << dendl; | |
4831 | ||
4832 | interval_set<uint64_t> overlap; | |
4833 | overlap.intersection_of(bset, bluefs_extents); | |
4834 | ||
4835 | bset.subtract(overlap); | |
4836 | if (!bset.empty()) { | |
4837 | derr << __func__ << " bluefs extra 0x" << std::hex << bset << std::dec | |
4838 | << dendl; | |
4839 | return -EIO; | |
4840 | } | |
4841 | ||
4842 | interval_set<uint64_t> super_extra; | |
4843 | super_extra = bluefs_extents; | |
4844 | super_extra.subtract(overlap); | |
4845 | if (!super_extra.empty()) { | |
4846 | // This is normal: it can happen if we commit to give extents to | |
4847 | // bluefs and we crash before bluefs commits that it owns them. | |
4848 | dout(10) << __func__ << " super extra " << super_extra << dendl; | |
4849 | for (interval_set<uint64_t>::iterator p = super_extra.begin(); | |
4850 | p != super_extra.end(); | |
4851 | ++p) { | |
4852 | bluefs->add_block_extent(bluefs_shared_bdev, p.get_start(), p.get_len()); | |
4853 | } | |
4854 | } | |
4855 | ||
4856 | return 0; | |
4857 | } | |
4858 | ||
4859 | int BlueStore::_balance_bluefs_freespace(PExtentVector *extents) | |
4860 | { | |
4861 | int ret = 0; | |
4862 | assert(bluefs); | |
4863 | ||
4864 | vector<pair<uint64_t,uint64_t>> bluefs_usage; // <free, total> ... | |
4865 | bluefs->get_usage(&bluefs_usage); | |
4866 | assert(bluefs_usage.size() > bluefs_shared_bdev); | |
4867 | ||
4868 | // fixme: look at primary bdev only for now | |
4869 | uint64_t bluefs_free = bluefs_usage[bluefs_shared_bdev].first; | |
4870 | uint64_t bluefs_total = bluefs_usage[bluefs_shared_bdev].second; | |
4871 | float bluefs_free_ratio = (float)bluefs_free / (float)bluefs_total; | |
4872 | ||
4873 | uint64_t my_free = alloc->get_free(); | |
4874 | uint64_t total = bdev->get_size(); | |
4875 | float my_free_ratio = (float)my_free / (float)total; | |
4876 | ||
4877 | uint64_t total_free = bluefs_free + my_free; | |
4878 | ||
4879 | float bluefs_ratio = (float)bluefs_free / (float)total_free; | |
4880 | ||
4881 | dout(10) << __func__ | |
4882 | << " bluefs " << pretty_si_t(bluefs_free) | |
4883 | << " free (" << bluefs_free_ratio | |
4884 | << ") bluestore " << pretty_si_t(my_free) | |
4885 | << " free (" << my_free_ratio | |
4886 | << "), bluefs_ratio " << bluefs_ratio | |
4887 | << dendl; | |
4888 | ||
4889 | uint64_t gift = 0; | |
4890 | uint64_t reclaim = 0; | |
4891 | if (bluefs_ratio < cct->_conf->bluestore_bluefs_min_ratio) { | |
4892 | gift = cct->_conf->bluestore_bluefs_gift_ratio * total_free; | |
4893 | dout(10) << __func__ << " bluefs_ratio " << bluefs_ratio | |
4894 | << " < min_ratio " << cct->_conf->bluestore_bluefs_min_ratio | |
4895 | << ", should gift " << pretty_si_t(gift) << dendl; | |
4896 | } else if (bluefs_ratio > cct->_conf->bluestore_bluefs_max_ratio) { | |
4897 | reclaim = cct->_conf->bluestore_bluefs_reclaim_ratio * total_free; | |
4898 | if (bluefs_total - reclaim < cct->_conf->bluestore_bluefs_min) | |
4899 | reclaim = bluefs_total - cct->_conf->bluestore_bluefs_min; | |
4900 | dout(10) << __func__ << " bluefs_ratio " << bluefs_ratio | |
4901 | << " > max_ratio " << cct->_conf->bluestore_bluefs_max_ratio | |
4902 | << ", should reclaim " << pretty_si_t(reclaim) << dendl; | |
4903 | } | |
4904 | ||
4905 | // don't take over too much of the freespace | |
4906 | uint64_t free_cap = cct->_conf->bluestore_bluefs_max_ratio * total_free; | |
4907 | if (bluefs_total < cct->_conf->bluestore_bluefs_min && | |
4908 | cct->_conf->bluestore_bluefs_min < free_cap) { | |
4909 | uint64_t g = cct->_conf->bluestore_bluefs_min - bluefs_total; | |
4910 | dout(10) << __func__ << " bluefs_total " << bluefs_total | |
4911 | << " < min " << cct->_conf->bluestore_bluefs_min | |
4912 | << ", should gift " << pretty_si_t(g) << dendl; | |
4913 | if (g > gift) | |
4914 | gift = g; | |
4915 | reclaim = 0; | |
4916 | } | |
4917 | uint64_t min_free = cct->_conf->get_val<uint64_t>("bluestore_bluefs_min_free"); | |
4918 | if (bluefs_free < min_free && | |
4919 | min_free < free_cap) { | |
4920 | uint64_t g = min_free - bluefs_free; | |
4921 | dout(10) << __func__ << " bluefs_free " << bluefs_total | |
4922 | << " < min " << min_free | |
4923 | << ", should gift " << pretty_si_t(g) << dendl; | |
4924 | if (g > gift) | |
4925 | gift = g; | |
4926 | reclaim = 0; | |
4927 | } | |
4928 | ||
4929 | if (gift) { | |
4930 | // round up to alloc size | |
4931 | gift = P2ROUNDUP(gift, cct->_conf->bluefs_alloc_size); | |
4932 | ||
4933 | // hard cap to fit into 32 bits | |
4934 | gift = MIN(gift, 1ull<<31); | |
4935 | dout(10) << __func__ << " gifting " << gift | |
4936 | << " (" << pretty_si_t(gift) << ")" << dendl; | |
4937 | ||
4938 | // fixme: just do one allocation to start... | |
4939 | int r = alloc->reserve(gift); | |
4940 | assert(r == 0); | |
4941 | ||
4942 | AllocExtentVector exts; | |
4943 | int64_t alloc_len = alloc->allocate(gift, cct->_conf->bluefs_alloc_size, | |
4944 | 0, 0, &exts); | |
4945 | ||
4946 | if (alloc_len < (int64_t)gift) { | |
4947 | derr << __func__ << " allocate failed on 0x" << std::hex << gift | |
4948 | << " min_alloc_size 0x" << min_alloc_size << std::dec << dendl; | |
4949 | alloc->dump(); | |
4950 | assert(0 == "allocate failed, wtf"); | |
4951 | return -ENOSPC; | |
4952 | } | |
4953 | for (auto& p : exts) { | |
4954 | bluestore_pextent_t e = bluestore_pextent_t(p); | |
4955 | dout(1) << __func__ << " gifting " << e << " to bluefs" << dendl; | |
4956 | extents->push_back(e); | |
4957 | } | |
4958 | gift = 0; | |
4959 | ||
4960 | ret = 1; | |
4961 | } | |
4962 | ||
4963 | // reclaim from bluefs? | |
4964 | if (reclaim) { | |
4965 | // round up to alloc size | |
4966 | reclaim = P2ROUNDUP(reclaim, cct->_conf->bluefs_alloc_size); | |
4967 | ||
4968 | // hard cap to fit into 32 bits | |
4969 | reclaim = MIN(reclaim, 1ull<<31); | |
4970 | dout(10) << __func__ << " reclaiming " << reclaim | |
4971 | << " (" << pretty_si_t(reclaim) << ")" << dendl; | |
4972 | ||
4973 | while (reclaim > 0) { | |
4974 | // NOTE: this will block and do IO. | |
4975 | AllocExtentVector extents; | |
4976 | int r = bluefs->reclaim_blocks(bluefs_shared_bdev, reclaim, | |
4977 | &extents); | |
4978 | if (r < 0) { | |
4979 | derr << __func__ << " failed to reclaim space from bluefs" | |
4980 | << dendl; | |
4981 | break; | |
4982 | } | |
4983 | for (auto e : extents) { | |
4984 | bluefs_extents.erase(e.offset, e.length); | |
4985 | bluefs_extents_reclaiming.insert(e.offset, e.length); | |
4986 | reclaim -= e.length; | |
4987 | } | |
4988 | } | |
4989 | ||
4990 | ret = 1; | |
4991 | } | |
4992 | ||
4993 | return ret; | |
4994 | } | |
4995 | ||
4996 | void BlueStore::_commit_bluefs_freespace( | |
4997 | const PExtentVector& bluefs_gift_extents) | |
4998 | { | |
4999 | dout(10) << __func__ << dendl; | |
5000 | for (auto& p : bluefs_gift_extents) { | |
5001 | bluefs->add_block_extent(bluefs_shared_bdev, p.offset, p.length); | |
5002 | } | |
5003 | } | |
5004 | ||
5005 | int BlueStore::_open_collections(int *errors) | |
5006 | { | |
5007 | assert(coll_map.empty()); | |
5008 | KeyValueDB::Iterator it = db->get_iterator(PREFIX_COLL); | |
5009 | for (it->upper_bound(string()); | |
5010 | it->valid(); | |
5011 | it->next()) { | |
5012 | coll_t cid; | |
5013 | if (cid.parse(it->key())) { | |
5014 | CollectionRef c( | |
5015 | new Collection( | |
5016 | this, | |
5017 | cache_shards[cid.hash_to_shard(cache_shards.size())], | |
5018 | cid)); | |
5019 | bufferlist bl = it->value(); | |
5020 | bufferlist::iterator p = bl.begin(); | |
5021 | try { | |
5022 | ::decode(c->cnode, p); | |
5023 | } catch (buffer::error& e) { | |
5024 | derr << __func__ << " failed to decode cnode, key:" | |
5025 | << pretty_binary_string(it->key()) << dendl; | |
5026 | return -EIO; | |
5027 | } | |
5028 | dout(20) << __func__ << " opened " << cid << " " << c << dendl; | |
5029 | coll_map[cid] = c; | |
5030 | } else { | |
5031 | derr << __func__ << " unrecognized collection " << it->key() << dendl; | |
5032 | if (errors) | |
5033 | (*errors)++; | |
5034 | } | |
5035 | } | |
5036 | return 0; | |
5037 | } | |
5038 | ||
5039 | void BlueStore::_open_statfs() | |
5040 | { | |
5041 | bufferlist bl; | |
5042 | int r = db->get(PREFIX_STAT, "bluestore_statfs", &bl); | |
5043 | if (r >= 0) { | |
5044 | if (size_t(bl.length()) >= sizeof(vstatfs.values)) { | |
5045 | auto it = bl.begin(); | |
5046 | vstatfs.decode(it); | |
5047 | } else { | |
5048 | dout(10) << __func__ << " store_statfs is corrupt, using empty" << dendl; | |
5049 | } | |
5050 | } | |
5051 | else { | |
5052 | dout(10) << __func__ << " store_statfs missed, using empty" << dendl; | |
5053 | } | |
5054 | } | |
5055 | ||
5056 | int BlueStore::_setup_block_symlink_or_file( | |
5057 | string name, | |
5058 | string epath, | |
5059 | uint64_t size, | |
5060 | bool create) | |
5061 | { | |
5062 | dout(20) << __func__ << " name " << name << " path " << epath | |
5063 | << " size " << size << " create=" << (int)create << dendl; | |
5064 | int r = 0; | |
5065 | int flags = O_RDWR; | |
5066 | if (create) | |
5067 | flags |= O_CREAT; | |
5068 | if (epath.length()) { | |
5069 | r = ::symlinkat(epath.c_str(), path_fd, name.c_str()); | |
5070 | if (r < 0) { | |
5071 | r = -errno; | |
5072 | derr << __func__ << " failed to create " << name << " symlink to " | |
5073 | << epath << ": " << cpp_strerror(r) << dendl; | |
5074 | return r; | |
5075 | } | |
5076 | ||
5077 | if (!epath.compare(0, strlen(SPDK_PREFIX), SPDK_PREFIX)) { | |
5078 | int fd = ::openat(path_fd, epath.c_str(), flags, 0644); | |
5079 | if (fd < 0) { | |
5080 | r = -errno; | |
5081 | derr << __func__ << " failed to open " << epath << " file: " | |
5082 | << cpp_strerror(r) << dendl; | |
5083 | return r; | |
5084 | } | |
5085 | string serial_number = epath.substr(strlen(SPDK_PREFIX)); | |
5086 | r = ::write(fd, serial_number.c_str(), serial_number.size()); | |
5087 | assert(r == (int)serial_number.size()); | |
5088 | dout(1) << __func__ << " created " << name << " symlink to " | |
5089 | << epath << dendl; | |
5090 | VOID_TEMP_FAILURE_RETRY(::close(fd)); | |
5091 | } | |
5092 | } | |
5093 | if (size) { | |
5094 | int fd = ::openat(path_fd, name.c_str(), flags, 0644); | |
5095 | if (fd >= 0) { | |
5096 | // block file is present | |
5097 | struct stat st; | |
5098 | int r = ::fstat(fd, &st); | |
5099 | if (r == 0 && | |
5100 | S_ISREG(st.st_mode) && // if it is a regular file | |
5101 | st.st_size == 0) { // and is 0 bytes | |
5102 | r = ::ftruncate(fd, size); | |
5103 | if (r < 0) { | |
5104 | r = -errno; | |
5105 | derr << __func__ << " failed to resize " << name << " file to " | |
5106 | << size << ": " << cpp_strerror(r) << dendl; | |
5107 | VOID_TEMP_FAILURE_RETRY(::close(fd)); | |
5108 | return r; | |
5109 | } | |
5110 | ||
5111 | if (cct->_conf->bluestore_block_preallocate_file) { | |
5112 | #ifdef HAVE_POSIX_FALLOCATE | |
5113 | r = ::posix_fallocate(fd, 0, size); | |
5114 | if (r) { | |
5115 | derr << __func__ << " failed to prefallocate " << name << " file to " | |
5116 | << size << ": " << cpp_strerror(r) << dendl; | |
5117 | VOID_TEMP_FAILURE_RETRY(::close(fd)); | |
5118 | return -r; | |
5119 | } | |
5120 | #else | |
5121 | char data[1024*128]; | |
5122 | for (uint64_t off = 0; off < size; off += sizeof(data)) { | |
5123 | if (off + sizeof(data) > size) | |
5124 | r = ::write(fd, data, size - off); | |
5125 | else | |
5126 | r = ::write(fd, data, sizeof(data)); | |
5127 | if (r < 0) { | |
5128 | r = -errno; | |
5129 | derr << __func__ << " failed to prefallocate w/ write " << name << " file to " | |
5130 | << size << ": " << cpp_strerror(r) << dendl; | |
5131 | VOID_TEMP_FAILURE_RETRY(::close(fd)); | |
5132 | return r; | |
5133 | } | |
5134 | } | |
5135 | #endif | |
5136 | } | |
5137 | dout(1) << __func__ << " resized " << name << " file to " | |
5138 | << pretty_si_t(size) << "B" << dendl; | |
5139 | } | |
5140 | VOID_TEMP_FAILURE_RETRY(::close(fd)); | |
5141 | } else { | |
5142 | int r = -errno; | |
5143 | if (r != -ENOENT) { | |
5144 | derr << __func__ << " failed to open " << name << " file: " | |
5145 | << cpp_strerror(r) << dendl; | |
5146 | return r; | |
5147 | } | |
5148 | } | |
5149 | } | |
5150 | return 0; | |
5151 | } | |
5152 | ||
5153 | int BlueStore::mkfs() | |
5154 | { | |
5155 | dout(1) << __func__ << " path " << path << dendl; | |
5156 | int r; | |
5157 | uuid_d old_fsid; | |
5158 | ||
5159 | { | |
5160 | string done; | |
5161 | r = read_meta("mkfs_done", &done); | |
5162 | if (r == 0) { | |
5163 | dout(1) << __func__ << " already created" << dendl; | |
5164 | if (cct->_conf->bluestore_fsck_on_mkfs) { | |
5165 | r = fsck(cct->_conf->bluestore_fsck_on_mkfs_deep); | |
5166 | if (r < 0) { | |
5167 | derr << __func__ << " fsck found fatal error: " << cpp_strerror(r) | |
5168 | << dendl; | |
5169 | return r; | |
5170 | } | |
5171 | if (r > 0) { | |
5172 | derr << __func__ << " fsck found " << r << " errors" << dendl; | |
5173 | r = -EIO; | |
5174 | } | |
5175 | } | |
5176 | return r; // idempotent | |
5177 | } | |
5178 | } | |
5179 | ||
5180 | { | |
5181 | string type; | |
5182 | r = read_meta("type", &type); | |
5183 | if (r == 0) { | |
5184 | if (type != "bluestore") { | |
5185 | derr << __func__ << " expected bluestore, but type is " << type << dendl; | |
5186 | return -EIO; | |
5187 | } | |
5188 | } else { | |
5189 | r = write_meta("type", "bluestore"); | |
5190 | if (r < 0) | |
5191 | return r; | |
5192 | } | |
5193 | } | |
5194 | ||
5195 | freelist_type = "bitmap"; | |
5196 | ||
5197 | r = _open_path(); | |
5198 | if (r < 0) | |
5199 | return r; | |
5200 | ||
5201 | r = _open_fsid(true); | |
5202 | if (r < 0) | |
5203 | goto out_path_fd; | |
5204 | ||
5205 | r = _lock_fsid(); | |
5206 | if (r < 0) | |
5207 | goto out_close_fsid; | |
5208 | ||
5209 | r = _read_fsid(&old_fsid); | |
5210 | if (r < 0 || old_fsid.is_zero()) { | |
5211 | if (fsid.is_zero()) { | |
5212 | fsid.generate_random(); | |
5213 | dout(1) << __func__ << " generated fsid " << fsid << dendl; | |
5214 | } else { | |
5215 | dout(1) << __func__ << " using provided fsid " << fsid << dendl; | |
5216 | } | |
5217 | // we'll write it later. | |
5218 | } else { | |
5219 | if (!fsid.is_zero() && fsid != old_fsid) { | |
5220 | derr << __func__ << " on-disk fsid " << old_fsid | |
5221 | << " != provided " << fsid << dendl; | |
5222 | r = -EINVAL; | |
5223 | goto out_close_fsid; | |
5224 | } | |
5225 | fsid = old_fsid; | |
5226 | } | |
5227 | ||
5228 | r = _setup_block_symlink_or_file("block", cct->_conf->bluestore_block_path, | |
5229 | cct->_conf->bluestore_block_size, | |
5230 | cct->_conf->bluestore_block_create); | |
5231 | if (r < 0) | |
5232 | goto out_close_fsid; | |
5233 | if (cct->_conf->bluestore_bluefs) { | |
5234 | r = _setup_block_symlink_or_file("block.wal", cct->_conf->bluestore_block_wal_path, | |
5235 | cct->_conf->bluestore_block_wal_size, | |
5236 | cct->_conf->bluestore_block_wal_create); | |
5237 | if (r < 0) | |
5238 | goto out_close_fsid; | |
5239 | r = _setup_block_symlink_or_file("block.db", cct->_conf->bluestore_block_db_path, | |
5240 | cct->_conf->bluestore_block_db_size, | |
5241 | cct->_conf->bluestore_block_db_create); | |
5242 | if (r < 0) | |
5243 | goto out_close_fsid; | |
5244 | } | |
5245 | ||
5246 | r = _open_bdev(true); | |
5247 | if (r < 0) | |
5248 | goto out_close_fsid; | |
5249 | ||
5250 | { | |
5251 | string wal_path = cct->_conf->get_val<string>("bluestore_block_wal_path"); | |
5252 | if (wal_path.size()) { | |
5253 | write_meta("path_block.wal", wal_path); | |
5254 | } | |
5255 | string db_path = cct->_conf->get_val<string>("bluestore_block_db_path"); | |
5256 | if (db_path.size()) { | |
5257 | write_meta("path_block.db", db_path); | |
5258 | } | |
5259 | } | |
5260 | ||
5261 | // choose min_alloc_size | |
5262 | if (cct->_conf->bluestore_min_alloc_size) { | |
5263 | min_alloc_size = cct->_conf->bluestore_min_alloc_size; | |
5264 | } else { | |
5265 | assert(bdev); | |
5266 | if (bdev->is_rotational()) { | |
5267 | min_alloc_size = cct->_conf->bluestore_min_alloc_size_hdd; | |
5268 | } else { | |
5269 | min_alloc_size = cct->_conf->bluestore_min_alloc_size_ssd; | |
5270 | } | |
5271 | } | |
5272 | ||
5273 | // make sure min_alloc_size is power of 2 aligned. | |
5274 | if (!ISP2(min_alloc_size)) { | |
5275 | derr << __func__ << " min_alloc_size 0x" | |
5276 | << std::hex << min_alloc_size << std::dec | |
5277 | << " is not power of 2 aligned!" | |
5278 | << dendl; | |
5279 | r = -EINVAL; | |
5280 | goto out_close_bdev; | |
5281 | } | |
5282 | ||
5283 | r = _open_db(true); | |
5284 | if (r < 0) | |
5285 | goto out_close_bdev; | |
5286 | ||
5287 | r = _open_fm(true); | |
5288 | if (r < 0) | |
5289 | goto out_close_db; | |
5290 | ||
5291 | { | |
5292 | KeyValueDB::Transaction t = db->get_transaction(); | |
5293 | { | |
5294 | bufferlist bl; | |
5295 | ::encode((uint64_t)0, bl); | |
5296 | t->set(PREFIX_SUPER, "nid_max", bl); | |
5297 | t->set(PREFIX_SUPER, "blobid_max", bl); | |
5298 | } | |
5299 | ||
5300 | { | |
5301 | bufferlist bl; | |
5302 | ::encode((uint64_t)min_alloc_size, bl); | |
5303 | t->set(PREFIX_SUPER, "min_alloc_size", bl); | |
5304 | } | |
5305 | ||
5306 | ondisk_format = latest_ondisk_format; | |
5307 | _prepare_ondisk_format_super(t); | |
5308 | db->submit_transaction_sync(t); | |
5309 | } | |
5310 | ||
5311 | ||
5312 | r = write_meta("kv_backend", cct->_conf->bluestore_kvbackend); | |
5313 | if (r < 0) | |
5314 | goto out_close_fm; | |
5315 | ||
5316 | r = write_meta("bluefs", stringify(bluefs ? 1 : 0)); | |
5317 | if (r < 0) | |
5318 | goto out_close_fm; | |
5319 | ||
5320 | if (fsid != old_fsid) { | |
5321 | r = _write_fsid(); | |
5322 | if (r < 0) { | |
5323 | derr << __func__ << " error writing fsid: " << cpp_strerror(r) << dendl; | |
5324 | goto out_close_fm; | |
5325 | } | |
5326 | } | |
5327 | ||
5328 | out_close_fm: | |
5329 | _close_fm(); | |
5330 | out_close_db: | |
5331 | _close_db(); | |
5332 | out_close_bdev: | |
5333 | _close_bdev(); | |
5334 | out_close_fsid: | |
5335 | _close_fsid(); | |
5336 | out_path_fd: | |
5337 | _close_path(); | |
5338 | ||
5339 | if (r == 0 && | |
5340 | cct->_conf->bluestore_fsck_on_mkfs) { | |
5341 | int rc = fsck(cct->_conf->bluestore_fsck_on_mkfs_deep); | |
5342 | if (rc < 0) | |
5343 | return rc; | |
5344 | if (rc > 0) { | |
5345 | derr << __func__ << " fsck found " << rc << " errors" << dendl; | |
5346 | r = -EIO; | |
5347 | } | |
5348 | } | |
5349 | ||
5350 | if (r == 0) { | |
5351 | // indicate success by writing the 'mkfs_done' file | |
5352 | r = write_meta("mkfs_done", "yes"); | |
5353 | } | |
5354 | ||
5355 | if (r < 0) { | |
5356 | derr << __func__ << " failed, " << cpp_strerror(r) << dendl; | |
5357 | } else { | |
5358 | dout(0) << __func__ << " success" << dendl; | |
5359 | } | |
5360 | return r; | |
5361 | } | |
5362 | ||
5363 | void BlueStore::set_cache_shards(unsigned num) | |
5364 | { | |
5365 | dout(10) << __func__ << " " << num << dendl; | |
5366 | size_t old = cache_shards.size(); | |
5367 | assert(num >= old); | |
5368 | cache_shards.resize(num); | |
5369 | for (unsigned i = old; i < num; ++i) { | |
5370 | cache_shards[i] = Cache::create(cct, cct->_conf->bluestore_cache_type, | |
5371 | logger); | |
5372 | } | |
5373 | } | |
5374 | ||
5375 | int BlueStore::_mount(bool kv_only) | |
5376 | { | |
5377 | dout(1) << __func__ << " path " << path << dendl; | |
5378 | ||
5379 | _kv_only = kv_only; | |
5380 | ||
5381 | { | |
5382 | string type; | |
5383 | int r = read_meta("type", &type); | |
5384 | if (r < 0) { | |
5385 | derr << __func__ << " failed to load os-type: " << cpp_strerror(r) | |
5386 | << dendl; | |
5387 | return r; | |
5388 | } | |
5389 | ||
5390 | if (type != "bluestore") { | |
5391 | derr << __func__ << " expected bluestore, but type is " << type << dendl; | |
5392 | return -EIO; | |
5393 | } | |
5394 | } | |
5395 | ||
5396 | if (cct->_conf->bluestore_fsck_on_mount) { | |
5397 | int rc = fsck(cct->_conf->bluestore_fsck_on_mount_deep); | |
5398 | if (rc < 0) | |
5399 | return rc; | |
5400 | if (rc > 0) { | |
5401 | derr << __func__ << " fsck found " << rc << " errors" << dendl; | |
5402 | return -EIO; | |
5403 | } | |
5404 | } | |
5405 | ||
5406 | int r = _open_path(); | |
5407 | if (r < 0) | |
5408 | return r; | |
5409 | r = _open_fsid(false); | |
5410 | if (r < 0) | |
5411 | goto out_path; | |
5412 | ||
5413 | r = _read_fsid(&fsid); | |
5414 | if (r < 0) | |
5415 | goto out_fsid; | |
5416 | ||
5417 | r = _lock_fsid(); | |
5418 | if (r < 0) | |
5419 | goto out_fsid; | |
5420 | ||
5421 | r = _open_bdev(false); | |
5422 | if (r < 0) | |
5423 | goto out_fsid; | |
5424 | ||
5425 | r = _open_db(false); | |
5426 | if (r < 0) | |
5427 | goto out_bdev; | |
5428 | ||
5429 | if (kv_only) | |
5430 | return 0; | |
5431 | ||
5432 | r = _open_super_meta(); | |
5433 | if (r < 0) | |
5434 | goto out_db; | |
5435 | ||
5436 | r = _open_fm(false); | |
5437 | if (r < 0) | |
5438 | goto out_db; | |
5439 | ||
5440 | r = _open_alloc(); | |
5441 | if (r < 0) | |
5442 | goto out_fm; | |
5443 | ||
5444 | r = _open_collections(); | |
5445 | if (r < 0) | |
5446 | goto out_alloc; | |
5447 | ||
5448 | r = _reload_logger(); | |
5449 | if (r < 0) | |
5450 | goto out_coll; | |
5451 | ||
5452 | if (bluefs) { | |
5453 | r = _reconcile_bluefs_freespace(); | |
5454 | if (r < 0) | |
5455 | goto out_coll; | |
5456 | } | |
5457 | ||
5458 | _kv_start(); | |
5459 | ||
5460 | r = _deferred_replay(); | |
5461 | if (r < 0) | |
5462 | goto out_stop; | |
5463 | ||
5464 | mempool_thread.init(); | |
5465 | ||
5466 | ||
5467 | mounted = true; | |
5468 | return 0; | |
5469 | ||
5470 | out_stop: | |
5471 | _kv_stop(); | |
5472 | out_coll: | |
5473 | _flush_cache(); | |
5474 | out_alloc: | |
5475 | _close_alloc(); | |
5476 | out_fm: | |
5477 | _close_fm(); | |
5478 | out_db: | |
5479 | _close_db(); | |
5480 | out_bdev: | |
5481 | _close_bdev(); | |
5482 | out_fsid: | |
5483 | _close_fsid(); | |
5484 | out_path: | |
5485 | _close_path(); | |
5486 | return r; | |
5487 | } | |
5488 | ||
5489 | int BlueStore::umount() | |
5490 | { | |
5491 | assert(_kv_only || mounted); | |
5492 | dout(1) << __func__ << dendl; | |
5493 | ||
5494 | _osr_drain_all(); | |
5495 | _osr_unregister_all(); | |
5496 | ||
5497 | mounted = false; | |
5498 | if (!_kv_only) { | |
5499 | mempool_thread.shutdown(); | |
5500 | dout(20) << __func__ << " stopping kv thread" << dendl; | |
5501 | _kv_stop(); | |
5502 | _reap_collections(); | |
5503 | _flush_cache(); | |
5504 | dout(20) << __func__ << " closing" << dendl; | |
5505 | ||
5506 | _close_alloc(); | |
5507 | _close_fm(); | |
5508 | } | |
5509 | _close_db(); | |
5510 | _close_bdev(); | |
5511 | _close_fsid(); | |
5512 | _close_path(); | |
5513 | ||
5514 | if (cct->_conf->bluestore_fsck_on_umount) { | |
5515 | int rc = fsck(cct->_conf->bluestore_fsck_on_umount_deep); | |
5516 | if (rc < 0) | |
5517 | return rc; | |
5518 | if (rc > 0) { | |
5519 | derr << __func__ << " fsck found " << rc << " errors" << dendl; | |
5520 | return -EIO; | |
5521 | } | |
5522 | } | |
5523 | return 0; | |
5524 | } | |
5525 | ||
5526 | static void apply(uint64_t off, | |
5527 | uint64_t len, | |
5528 | uint64_t granularity, | |
5529 | BlueStore::mempool_dynamic_bitset &bitset, | |
5530 | std::function<void(uint64_t, | |
5531 | BlueStore::mempool_dynamic_bitset &)> f) { | |
5532 | auto end = ROUND_UP_TO(off + len, granularity); | |
5533 | while (off < end) { | |
5534 | uint64_t pos = off / granularity; | |
5535 | f(pos, bitset); | |
5536 | off += granularity; | |
5537 | } | |
5538 | } | |
5539 | ||
5540 | int BlueStore::_fsck_check_extents( | |
5541 | const ghobject_t& oid, | |
5542 | const PExtentVector& extents, | |
5543 | bool compressed, | |
5544 | mempool_dynamic_bitset &used_blocks, | |
5545 | uint64_t granularity, | |
5546 | store_statfs_t& expected_statfs) | |
5547 | { | |
5548 | dout(30) << __func__ << " oid " << oid << " extents " << extents << dendl; | |
5549 | int errors = 0; | |
5550 | for (auto e : extents) { | |
5551 | if (!e.is_valid()) | |
5552 | continue; | |
5553 | expected_statfs.allocated += e.length; | |
5554 | if (compressed) { | |
5555 | expected_statfs.compressed_allocated += e.length; | |
5556 | } | |
5557 | bool already = false; | |
5558 | apply( | |
5559 | e.offset, e.length, granularity, used_blocks, | |
5560 | [&](uint64_t pos, mempool_dynamic_bitset &bs) { | |
5561 | assert(pos < bs.size()); | |
5562 | if (bs.test(pos)) | |
5563 | already = true; | |
5564 | else | |
5565 | bs.set(pos); | |
5566 | }); | |
5567 | if (already) { | |
5568 | derr << " " << oid << " extent " << e | |
5569 | << " or a subset is already allocated" << dendl; | |
5570 | ++errors; | |
5571 | } | |
5572 | if (e.end() > bdev->get_size()) { | |
5573 | derr << " " << oid << " extent " << e | |
5574 | << " past end of block device" << dendl; | |
5575 | ++errors; | |
5576 | } | |
5577 | } | |
5578 | return errors; | |
5579 | } | |
5580 | ||
5581 | int BlueStore::_fsck(bool deep, bool repair) | |
5582 | { | |
5583 | dout(1) << __func__ | |
5584 | << (repair ? " fsck" : " repair") | |
5585 | << (deep ? " (deep)" : " (shallow)") << " start" << dendl; | |
5586 | int errors = 0; | |
5587 | int repaired = 0; | |
5588 | ||
5589 | typedef btree::btree_set< | |
5590 | uint64_t,std::less<uint64_t>, | |
5591 | mempool::bluestore_fsck::pool_allocator<uint64_t>> uint64_t_btree_t; | |
5592 | uint64_t_btree_t used_nids; | |
5593 | uint64_t_btree_t used_omap_head; | |
5594 | uint64_t_btree_t used_sbids; | |
5595 | ||
5596 | mempool_dynamic_bitset used_blocks; | |
5597 | KeyValueDB::Iterator it; | |
5598 | store_statfs_t expected_statfs, actual_statfs; | |
5599 | struct sb_info_t { | |
5600 | list<ghobject_t> oids; | |
5601 | SharedBlobRef sb; | |
5602 | bluestore_extent_ref_map_t ref_map; | |
5603 | bool compressed; | |
5604 | }; | |
5605 | mempool::bluestore_fsck::map<uint64_t,sb_info_t> sb_info; | |
5606 | ||
5607 | uint64_t num_objects = 0; | |
5608 | uint64_t num_extents = 0; | |
5609 | uint64_t num_blobs = 0; | |
5610 | uint64_t num_spanning_blobs = 0; | |
5611 | uint64_t num_shared_blobs = 0; | |
5612 | uint64_t num_sharded_objects = 0; | |
5613 | uint64_t num_object_shards = 0; | |
5614 | ||
5615 | utime_t start = ceph_clock_now(); | |
5616 | ||
5617 | int r = _open_path(); | |
5618 | if (r < 0) | |
5619 | return r; | |
5620 | r = _open_fsid(false); | |
5621 | if (r < 0) | |
5622 | goto out_path; | |
5623 | ||
5624 | r = _read_fsid(&fsid); | |
5625 | if (r < 0) | |
5626 | goto out_fsid; | |
5627 | ||
5628 | r = _lock_fsid(); | |
5629 | if (r < 0) | |
5630 | goto out_fsid; | |
5631 | ||
5632 | r = _open_bdev(false); | |
5633 | if (r < 0) | |
5634 | goto out_fsid; | |
5635 | ||
5636 | r = _open_db(false); | |
5637 | if (r < 0) | |
5638 | goto out_bdev; | |
5639 | ||
5640 | r = _open_super_meta(); | |
5641 | if (r < 0) | |
5642 | goto out_db; | |
5643 | ||
5644 | r = _open_fm(false); | |
5645 | if (r < 0) | |
5646 | goto out_db; | |
5647 | ||
5648 | r = _open_alloc(); | |
5649 | if (r < 0) | |
5650 | goto out_fm; | |
5651 | ||
5652 | r = _open_collections(&errors); | |
5653 | if (r < 0) | |
5654 | goto out_alloc; | |
5655 | ||
5656 | mempool_thread.init(); | |
5657 | ||
5658 | // we need finishers and kv_{sync,finalize}_thread *just* for replay | |
5659 | _kv_start(); | |
5660 | r = _deferred_replay(); | |
5661 | _kv_stop(); | |
5662 | if (r < 0) | |
5663 | goto out_scan; | |
5664 | ||
5665 | used_blocks.resize(fm->get_alloc_units()); | |
5666 | apply( | |
5667 | 0, MAX(min_alloc_size, SUPER_RESERVED), fm->get_alloc_size(), used_blocks, | |
5668 | [&](uint64_t pos, mempool_dynamic_bitset &bs) { | |
5669 | assert(pos < bs.size()); | |
5670 | bs.set(pos); | |
5671 | } | |
5672 | ); | |
5673 | ||
5674 | if (bluefs) { | |
5675 | for (auto e = bluefs_extents.begin(); e != bluefs_extents.end(); ++e) { | |
5676 | apply( | |
5677 | e.get_start(), e.get_len(), fm->get_alloc_size(), used_blocks, | |
5678 | [&](uint64_t pos, mempool_dynamic_bitset &bs) { | |
5679 | assert(pos < bs.size()); | |
5680 | bs.set(pos); | |
5681 | } | |
5682 | ); | |
5683 | } | |
5684 | r = bluefs->fsck(); | |
5685 | if (r < 0) { | |
5686 | goto out_scan; | |
5687 | } | |
5688 | if (r > 0) | |
5689 | errors += r; | |
5690 | } | |
5691 | ||
5692 | // get expected statfs; fill unaffected fields to be able to compare | |
5693 | // structs | |
5694 | statfs(&actual_statfs); | |
5695 | expected_statfs.total = actual_statfs.total; | |
5696 | expected_statfs.available = actual_statfs.available; | |
5697 | ||
5698 | // walk PREFIX_OBJ | |
5699 | dout(1) << __func__ << " walking object keyspace" << dendl; | |
5700 | it = db->get_iterator(PREFIX_OBJ); | |
5701 | if (it) { | |
5702 | CollectionRef c; | |
5703 | spg_t pgid; | |
5704 | mempool::bluestore_fsck::list<string> expecting_shards; | |
5705 | for (it->lower_bound(string()); it->valid(); it->next()) { | |
5706 | if (g_conf->bluestore_debug_fsck_abort) { | |
5707 | goto out_scan; | |
5708 | } | |
5709 | dout(30) << " key " << pretty_binary_string(it->key()) << dendl; | |
5710 | if (is_extent_shard_key(it->key())) { | |
5711 | while (!expecting_shards.empty() && | |
5712 | expecting_shards.front() < it->key()) { | |
5713 | derr << "fsck error: missing shard key " | |
5714 | << pretty_binary_string(expecting_shards.front()) | |
5715 | << dendl; | |
5716 | ++errors; | |
5717 | expecting_shards.pop_front(); | |
5718 | } | |
5719 | if (!expecting_shards.empty() && | |
5720 | expecting_shards.front() == it->key()) { | |
5721 | // all good | |
5722 | expecting_shards.pop_front(); | |
5723 | continue; | |
5724 | } | |
5725 | ||
5726 | uint32_t offset; | |
5727 | string okey; | |
5728 | get_key_extent_shard(it->key(), &okey, &offset); | |
5729 | derr << "fsck error: stray shard 0x" << std::hex << offset | |
5730 | << std::dec << dendl; | |
5731 | if (expecting_shards.empty()) { | |
5732 | derr << "fsck error: " << pretty_binary_string(it->key()) | |
5733 | << " is unexpected" << dendl; | |
5734 | ++errors; | |
5735 | continue; | |
5736 | } | |
5737 | while (expecting_shards.front() > it->key()) { | |
5738 | derr << "fsck error: saw " << pretty_binary_string(it->key()) | |
5739 | << dendl; | |
5740 | derr << "fsck error: exp " | |
5741 | << pretty_binary_string(expecting_shards.front()) << dendl; | |
5742 | ++errors; | |
5743 | expecting_shards.pop_front(); | |
5744 | if (expecting_shards.empty()) { | |
5745 | break; | |
5746 | } | |
5747 | } | |
5748 | continue; | |
5749 | } | |
5750 | ||
5751 | ghobject_t oid; | |
5752 | int r = get_key_object(it->key(), &oid); | |
5753 | if (r < 0) { | |
5754 | derr << "fsck error: bad object key " | |
5755 | << pretty_binary_string(it->key()) << dendl; | |
5756 | ++errors; | |
5757 | continue; | |
5758 | } | |
5759 | if (!c || | |
5760 | oid.shard_id != pgid.shard || | |
5761 | oid.hobj.pool != (int64_t)pgid.pool() || | |
5762 | !c->contains(oid)) { | |
5763 | c = nullptr; | |
5764 | for (ceph::unordered_map<coll_t, CollectionRef>::iterator p = | |
5765 | coll_map.begin(); | |
5766 | p != coll_map.end(); | |
5767 | ++p) { | |
5768 | if (p->second->contains(oid)) { | |
5769 | c = p->second; | |
5770 | break; | |
5771 | } | |
5772 | } | |
5773 | if (!c) { | |
5774 | derr << "fsck error: stray object " << oid | |
5775 | << " not owned by any collection" << dendl; | |
5776 | ++errors; | |
5777 | continue; | |
5778 | } | |
5779 | c->cid.is_pg(&pgid); | |
5780 | dout(20) << __func__ << " collection " << c->cid << dendl; | |
5781 | } | |
5782 | ||
5783 | if (!expecting_shards.empty()) { | |
5784 | for (auto &k : expecting_shards) { | |
5785 | derr << "fsck error: missing shard key " | |
5786 | << pretty_binary_string(k) << dendl; | |
5787 | } | |
5788 | ++errors; | |
5789 | expecting_shards.clear(); | |
5790 | } | |
5791 | ||
5792 | dout(10) << __func__ << " " << oid << dendl; | |
5793 | RWLock::RLocker l(c->lock); | |
5794 | OnodeRef o = c->get_onode(oid, false); | |
5795 | if (o->onode.nid) { | |
5796 | if (o->onode.nid > nid_max) { | |
5797 | derr << "fsck error: " << oid << " nid " << o->onode.nid | |
5798 | << " > nid_max " << nid_max << dendl; | |
5799 | ++errors; | |
5800 | } | |
5801 | if (used_nids.count(o->onode.nid)) { | |
5802 | derr << "fsck error: " << oid << " nid " << o->onode.nid | |
5803 | << " already in use" << dendl; | |
5804 | ++errors; | |
5805 | continue; // go for next object | |
5806 | } | |
5807 | used_nids.insert(o->onode.nid); | |
5808 | } | |
5809 | ++num_objects; | |
5810 | num_spanning_blobs += o->extent_map.spanning_blob_map.size(); | |
5811 | o->extent_map.fault_range(db, 0, OBJECT_MAX_SIZE); | |
5812 | _dump_onode(o, 30); | |
5813 | // shards | |
5814 | if (!o->extent_map.shards.empty()) { | |
5815 | ++num_sharded_objects; | |
5816 | num_object_shards += o->extent_map.shards.size(); | |
5817 | } | |
5818 | for (auto& s : o->extent_map.shards) { | |
5819 | dout(20) << __func__ << " shard " << *s.shard_info << dendl; | |
5820 | expecting_shards.push_back(string()); | |
5821 | get_extent_shard_key(o->key, s.shard_info->offset, | |
5822 | &expecting_shards.back()); | |
5823 | if (s.shard_info->offset >= o->onode.size) { | |
5824 | derr << "fsck error: " << oid << " shard 0x" << std::hex | |
5825 | << s.shard_info->offset << " past EOF at 0x" << o->onode.size | |
5826 | << std::dec << dendl; | |
5827 | ++errors; | |
5828 | } | |
5829 | } | |
5830 | // lextents | |
5831 | map<BlobRef,bluestore_blob_t::unused_t> referenced; | |
5832 | uint64_t pos = 0; | |
5833 | mempool::bluestore_fsck::map<BlobRef, | |
5834 | bluestore_blob_use_tracker_t> ref_map; | |
5835 | for (auto& l : o->extent_map.extent_map) { | |
5836 | dout(20) << __func__ << " " << l << dendl; | |
5837 | if (l.logical_offset < pos) { | |
5838 | derr << "fsck error: " << oid << " lextent at 0x" | |
5839 | << std::hex << l.logical_offset | |
5840 | << " overlaps with the previous, which ends at 0x" << pos | |
5841 | << std::dec << dendl; | |
5842 | ++errors; | |
5843 | } | |
5844 | if (o->extent_map.spans_shard(l.logical_offset, l.length)) { | |
5845 | derr << "fsck error: " << oid << " lextent at 0x" | |
5846 | << std::hex << l.logical_offset << "~" << l.length | |
5847 | << " spans a shard boundary" | |
5848 | << std::dec << dendl; | |
5849 | ++errors; | |
5850 | } | |
5851 | pos = l.logical_offset + l.length; | |
5852 | expected_statfs.stored += l.length; | |
5853 | assert(l.blob); | |
5854 | const bluestore_blob_t& blob = l.blob->get_blob(); | |
5855 | ||
5856 | auto& ref = ref_map[l.blob]; | |
5857 | if (ref.is_empty()) { | |
5858 | uint32_t min_release_size = blob.get_release_size(min_alloc_size); | |
5859 | uint32_t l = blob.get_logical_length(); | |
5860 | ref.init(l, min_release_size); | |
5861 | } | |
5862 | ref.get( | |
5863 | l.blob_offset, | |
5864 | l.length); | |
5865 | ++num_extents; | |
5866 | if (blob.has_unused()) { | |
5867 | auto p = referenced.find(l.blob); | |
5868 | bluestore_blob_t::unused_t *pu; | |
5869 | if (p == referenced.end()) { | |
5870 | pu = &referenced[l.blob]; | |
5871 | } else { | |
5872 | pu = &p->second; | |
5873 | } | |
5874 | uint64_t blob_len = blob.get_logical_length(); | |
5875 | assert((blob_len % (sizeof(*pu)*8)) == 0); | |
5876 | assert(l.blob_offset + l.length <= blob_len); | |
5877 | uint64_t chunk_size = blob_len / (sizeof(*pu)*8); | |
5878 | uint64_t start = l.blob_offset / chunk_size; | |
5879 | uint64_t end = | |
5880 | ROUND_UP_TO(l.blob_offset + l.length, chunk_size) / chunk_size; | |
5881 | for (auto i = start; i < end; ++i) { | |
5882 | (*pu) |= (1u << i); | |
5883 | } | |
5884 | } | |
5885 | } | |
5886 | for (auto &i : referenced) { | |
5887 | dout(20) << __func__ << " referenced 0x" << std::hex << i.second | |
5888 | << std::dec << " for " << *i.first << dendl; | |
5889 | const bluestore_blob_t& blob = i.first->get_blob(); | |
5890 | if (i.second & blob.unused) { | |
5891 | derr << "fsck error: " << oid << " blob claims unused 0x" | |
5892 | << std::hex << blob.unused | |
5893 | << " but extents reference 0x" << i.second | |
5894 | << " on blob " << *i.first << dendl; | |
5895 | ++errors; | |
5896 | } | |
5897 | if (blob.has_csum()) { | |
5898 | uint64_t blob_len = blob.get_logical_length(); | |
5899 | uint64_t unused_chunk_size = blob_len / (sizeof(blob.unused)*8); | |
5900 | unsigned csum_count = blob.get_csum_count(); | |
5901 | unsigned csum_chunk_size = blob.get_csum_chunk_size(); | |
5902 | for (unsigned p = 0; p < csum_count; ++p) { | |
5903 | unsigned pos = p * csum_chunk_size; | |
5904 | unsigned firstbit = pos / unused_chunk_size; // [firstbit,lastbit] | |
5905 | unsigned lastbit = (pos + csum_chunk_size - 1) / unused_chunk_size; | |
5906 | unsigned mask = 1u << firstbit; | |
5907 | for (unsigned b = firstbit + 1; b <= lastbit; ++b) { | |
5908 | mask |= 1u << b; | |
5909 | } | |
5910 | if ((blob.unused & mask) == mask) { | |
5911 | // this csum chunk region is marked unused | |
5912 | if (blob.get_csum_item(p) != 0) { | |
5913 | derr << "fsck error: " << oid | |
5914 | << " blob claims csum chunk 0x" << std::hex << pos | |
5915 | << "~" << csum_chunk_size | |
5916 | << " is unused (mask 0x" << mask << " of unused 0x" | |
5917 | << blob.unused << ") but csum is non-zero 0x" | |
5918 | << blob.get_csum_item(p) << std::dec << " on blob " | |
5919 | << *i.first << dendl; | |
5920 | ++errors; | |
5921 | } | |
5922 | } | |
5923 | } | |
5924 | } | |
5925 | } | |
5926 | for (auto &i : ref_map) { | |
5927 | ++num_blobs; | |
5928 | const bluestore_blob_t& blob = i.first->get_blob(); | |
5929 | bool equal = i.first->get_blob_use_tracker().equal(i.second); | |
5930 | if (!equal) { | |
5931 | derr << "fsck error: " << oid << " blob " << *i.first | |
5932 | << " doesn't match expected ref_map " << i.second << dendl; | |
5933 | ++errors; | |
5934 | } | |
5935 | if (blob.is_compressed()) { | |
5936 | expected_statfs.compressed += blob.get_compressed_payload_length(); | |
5937 | expected_statfs.compressed_original += | |
5938 | i.first->get_referenced_bytes(); | |
5939 | } | |
5940 | if (blob.is_shared()) { | |
5941 | if (i.first->shared_blob->get_sbid() > blobid_max) { | |
5942 | derr << "fsck error: " << oid << " blob " << blob | |
5943 | << " sbid " << i.first->shared_blob->get_sbid() << " > blobid_max " | |
5944 | << blobid_max << dendl; | |
5945 | ++errors; | |
5946 | } else if (i.first->shared_blob->get_sbid() == 0) { | |
5947 | derr << "fsck error: " << oid << " blob " << blob | |
5948 | << " marked as shared but has uninitialized sbid" | |
5949 | << dendl; | |
5950 | ++errors; | |
5951 | } | |
5952 | sb_info_t& sbi = sb_info[i.first->shared_blob->get_sbid()]; | |
5953 | sbi.sb = i.first->shared_blob; | |
5954 | sbi.oids.push_back(oid); | |
5955 | sbi.compressed = blob.is_compressed(); | |
5956 | for (auto e : blob.get_extents()) { | |
5957 | if (e.is_valid()) { | |
5958 | sbi.ref_map.get(e.offset, e.length); | |
5959 | } | |
5960 | } | |
5961 | } else { | |
5962 | errors += _fsck_check_extents(oid, blob.get_extents(), | |
5963 | blob.is_compressed(), | |
5964 | used_blocks, | |
5965 | fm->get_alloc_size(), | |
5966 | expected_statfs); | |
5967 | } | |
5968 | } | |
5969 | if (deep) { | |
5970 | bufferlist bl; | |
5971 | int r = _do_read(c.get(), o, 0, o->onode.size, bl, 0); | |
5972 | if (r < 0) { | |
5973 | ++errors; | |
5974 | derr << "fsck error: " << oid << " error during read: " | |
5975 | << cpp_strerror(r) << dendl; | |
5976 | } | |
5977 | } | |
5978 | // omap | |
5979 | if (o->onode.has_omap()) { | |
5980 | if (used_omap_head.count(o->onode.nid)) { | |
5981 | derr << "fsck error: " << oid << " omap_head " << o->onode.nid | |
5982 | << " already in use" << dendl; | |
5983 | ++errors; | |
5984 | } else { | |
5985 | used_omap_head.insert(o->onode.nid); | |
5986 | } | |
5987 | } | |
5988 | } | |
5989 | } | |
5990 | dout(1) << __func__ << " checking shared_blobs" << dendl; | |
5991 | it = db->get_iterator(PREFIX_SHARED_BLOB); | |
5992 | if (it) { | |
5993 | for (it->lower_bound(string()); it->valid(); it->next()) { | |
5994 | string key = it->key(); | |
5995 | uint64_t sbid; | |
5996 | if (get_key_shared_blob(key, &sbid)) { | |
5997 | derr << "fsck error: bad key '" << key | |
5998 | << "' in shared blob namespace" << dendl; | |
5999 | ++errors; | |
6000 | continue; | |
6001 | } | |
6002 | auto p = sb_info.find(sbid); | |
6003 | if (p == sb_info.end()) { | |
6004 | derr << "fsck error: found stray shared blob data for sbid 0x" | |
6005 | << std::hex << sbid << std::dec << dendl; | |
6006 | ++errors; | |
6007 | } else { | |
6008 | ++num_shared_blobs; | |
6009 | sb_info_t& sbi = p->second; | |
6010 | bluestore_shared_blob_t shared_blob(sbid); | |
6011 | bufferlist bl = it->value(); | |
6012 | bufferlist::iterator blp = bl.begin(); | |
6013 | ::decode(shared_blob, blp); | |
6014 | dout(20) << __func__ << " " << *sbi.sb << " " << shared_blob << dendl; | |
6015 | if (shared_blob.ref_map != sbi.ref_map) { | |
6016 | derr << "fsck error: shared blob 0x" << std::hex << sbid | |
6017 | << std::dec << " ref_map " << shared_blob.ref_map | |
6018 | << " != expected " << sbi.ref_map << dendl; | |
6019 | ++errors; | |
6020 | } | |
6021 | PExtentVector extents; | |
6022 | for (auto &r : shared_blob.ref_map.ref_map) { | |
6023 | extents.emplace_back(bluestore_pextent_t(r.first, r.second.length)); | |
6024 | } | |
6025 | errors += _fsck_check_extents(p->second.oids.front(), | |
6026 | extents, | |
6027 | p->second.compressed, | |
6028 | used_blocks, | |
6029 | fm->get_alloc_size(), | |
6030 | expected_statfs); | |
6031 | sb_info.erase(p); | |
6032 | } | |
6033 | } | |
6034 | } | |
6035 | for (auto &p : sb_info) { | |
6036 | derr << "fsck error: shared_blob 0x" << p.first | |
6037 | << " key is missing (" << *p.second.sb << ")" << dendl; | |
6038 | ++errors; | |
6039 | } | |
6040 | if (!(actual_statfs == expected_statfs)) { | |
6041 | derr << "fsck error: actual " << actual_statfs | |
6042 | << " != expected " << expected_statfs << dendl; | |
6043 | ++errors; | |
6044 | } | |
6045 | ||
6046 | dout(1) << __func__ << " checking for stray omap data" << dendl; | |
6047 | it = db->get_iterator(PREFIX_OMAP); | |
6048 | if (it) { | |
6049 | for (it->lower_bound(string()); it->valid(); it->next()) { | |
6050 | uint64_t omap_head; | |
6051 | _key_decode_u64(it->key().c_str(), &omap_head); | |
6052 | if (used_omap_head.count(omap_head) == 0) { | |
6053 | derr << "fsck error: found stray omap data on omap_head " | |
6054 | << omap_head << dendl; | |
6055 | ++errors; | |
6056 | } | |
6057 | } | |
6058 | } | |
6059 | ||
6060 | dout(1) << __func__ << " checking deferred events" << dendl; | |
6061 | it = db->get_iterator(PREFIX_DEFERRED); | |
6062 | if (it) { | |
6063 | for (it->lower_bound(string()); it->valid(); it->next()) { | |
6064 | bufferlist bl = it->value(); | |
6065 | bufferlist::iterator p = bl.begin(); | |
6066 | bluestore_deferred_transaction_t wt; | |
6067 | try { | |
6068 | ::decode(wt, p); | |
6069 | } catch (buffer::error& e) { | |
6070 | derr << "fsck error: failed to decode deferred txn " | |
6071 | << pretty_binary_string(it->key()) << dendl; | |
6072 | r = -EIO; | |
6073 | goto out_scan; | |
6074 | } | |
6075 | dout(20) << __func__ << " deferred " << wt.seq | |
6076 | << " ops " << wt.ops.size() | |
6077 | << " released 0x" << std::hex << wt.released << std::dec << dendl; | |
6078 | for (auto e = wt.released.begin(); e != wt.released.end(); ++e) { | |
6079 | apply( | |
6080 | e.get_start(), e.get_len(), fm->get_alloc_size(), used_blocks, | |
6081 | [&](uint64_t pos, mempool_dynamic_bitset &bs) { | |
6082 | assert(pos < bs.size()); | |
6083 | bs.set(pos); | |
6084 | } | |
6085 | ); | |
6086 | } | |
6087 | } | |
6088 | } | |
6089 | ||
6090 | dout(1) << __func__ << " checking freelist vs allocated" << dendl; | |
6091 | { | |
6092 | // remove bluefs_extents from used set since the freelist doesn't | |
6093 | // know they are allocated. | |
6094 | for (auto e = bluefs_extents.begin(); e != bluefs_extents.end(); ++e) { | |
6095 | apply( | |
6096 | e.get_start(), e.get_len(), fm->get_alloc_size(), used_blocks, | |
6097 | [&](uint64_t pos, mempool_dynamic_bitset &bs) { | |
6098 | assert(pos < bs.size()); | |
6099 | bs.reset(pos); | |
6100 | } | |
6101 | ); | |
6102 | } | |
6103 | fm->enumerate_reset(); | |
6104 | uint64_t offset, length; | |
6105 | while (fm->enumerate_next(&offset, &length)) { | |
6106 | bool intersects = false; | |
6107 | apply( | |
6108 | offset, length, fm->get_alloc_size(), used_blocks, | |
6109 | [&](uint64_t pos, mempool_dynamic_bitset &bs) { | |
6110 | assert(pos < bs.size()); | |
6111 | if (bs.test(pos)) { | |
6112 | intersects = true; | |
6113 | } else { | |
6114 | bs.set(pos); | |
6115 | } | |
6116 | } | |
6117 | ); | |
6118 | if (intersects) { | |
6119 | if (offset == SUPER_RESERVED && | |
6120 | length == min_alloc_size - SUPER_RESERVED) { | |
6121 | // this is due to the change just after luminous to min_alloc_size | |
6122 | // granularity allocations, and our baked in assumption at the top | |
6123 | // of _fsck that 0~ROUND_UP_TO(SUPER_RESERVED,min_alloc_size) is used | |
6124 | // (vs luminous's ROUND_UP_TO(SUPER_RESERVED,block_size)). harmless, | |
6125 | // since we will never allocate this region below min_alloc_size. | |
6126 | dout(10) << __func__ << " ignoring free extent between SUPER_RESERVED" | |
6127 | << " and min_alloc_size, 0x" << std::hex << offset << "~" | |
6128 | << length << dendl; | |
6129 | } else { | |
6130 | derr << "fsck error: free extent 0x" << std::hex << offset | |
6131 | << "~" << length << std::dec | |
6132 | << " intersects allocated blocks" << dendl; | |
6133 | ++errors; | |
6134 | } | |
6135 | } | |
6136 | } | |
6137 | fm->enumerate_reset(); | |
6138 | size_t count = used_blocks.count(); | |
6139 | if (used_blocks.size() != count) { | |
6140 | assert(used_blocks.size() > count); | |
6141 | ++errors; | |
6142 | used_blocks.flip(); | |
6143 | size_t start = used_blocks.find_first(); | |
6144 | while (start != decltype(used_blocks)::npos) { | |
6145 | size_t cur = start; | |
6146 | while (true) { | |
6147 | size_t next = used_blocks.find_next(cur); | |
6148 | if (next != cur + 1) { | |
6149 | derr << "fsck error: leaked extent 0x" << std::hex | |
6150 | << ((uint64_t)start * fm->get_alloc_size()) << "~" | |
6151 | << ((cur + 1 - start) * fm->get_alloc_size()) << std::dec | |
6152 | << dendl; | |
6153 | start = next; | |
6154 | break; | |
6155 | } | |
6156 | cur = next; | |
6157 | } | |
6158 | } | |
6159 | used_blocks.flip(); | |
6160 | } | |
6161 | } | |
6162 | ||
6163 | out_scan: | |
6164 | mempool_thread.shutdown(); | |
6165 | _flush_cache(); | |
6166 | out_alloc: | |
6167 | _close_alloc(); | |
6168 | out_fm: | |
6169 | _close_fm(); | |
6170 | out_db: | |
6171 | it.reset(); // before db is closed | |
6172 | _close_db(); | |
6173 | out_bdev: | |
6174 | _close_bdev(); | |
6175 | out_fsid: | |
6176 | _close_fsid(); | |
6177 | out_path: | |
6178 | _close_path(); | |
6179 | ||
6180 | // fatal errors take precedence | |
6181 | if (r < 0) | |
6182 | return r; | |
6183 | ||
6184 | dout(2) << __func__ << " " << num_objects << " objects, " | |
6185 | << num_sharded_objects << " of them sharded. " | |
6186 | << dendl; | |
6187 | dout(2) << __func__ << " " << num_extents << " extents to " | |
6188 | << num_blobs << " blobs, " | |
6189 | << num_spanning_blobs << " spanning, " | |
6190 | << num_shared_blobs << " shared." | |
6191 | << dendl; | |
6192 | ||
6193 | utime_t duration = ceph_clock_now() - start; | |
6194 | dout(1) << __func__ << " finish with " << errors << " errors, " << repaired | |
6195 | << " repaired, " << (errors - repaired) << " remaining in " | |
6196 | << duration << " seconds" << dendl; | |
6197 | return errors - repaired; | |
6198 | } | |
6199 | ||
6200 | void BlueStore::collect_metadata(map<string,string> *pm) | |
6201 | { | |
6202 | dout(10) << __func__ << dendl; | |
6203 | bdev->collect_metadata("bluestore_bdev_", pm); | |
6204 | if (bluefs) { | |
6205 | (*pm)["bluefs"] = "1"; | |
6206 | (*pm)["bluefs_single_shared_device"] = stringify((int)bluefs_single_shared_device); | |
6207 | bluefs->collect_metadata(pm); | |
6208 | } else { | |
6209 | (*pm)["bluefs"] = "0"; | |
6210 | } | |
6211 | } | |
6212 | ||
6213 | int BlueStore::statfs(struct store_statfs_t *buf) | |
6214 | { | |
6215 | buf->reset(); | |
6216 | buf->total = bdev->get_size(); | |
6217 | buf->available = alloc->get_free(); | |
6218 | ||
6219 | if (bluefs) { | |
6220 | // part of our shared device is "free" according to BlueFS | |
6221 | // Don't include bluestore_bluefs_min because that space can't | |
6222 | // be used for any other purpose. | |
6223 | buf->available += bluefs->get_free(bluefs_shared_bdev) - cct->_conf->bluestore_bluefs_min; | |
6224 | ||
6225 | // include dedicated db, too, if that isn't the shared device. | |
6226 | if (bluefs_shared_bdev != BlueFS::BDEV_DB) { | |
6227 | buf->total += bluefs->get_total(BlueFS::BDEV_DB); | |
6228 | } | |
6229 | } | |
6230 | ||
6231 | { | |
6232 | std::lock_guard<std::mutex> l(vstatfs_lock); | |
6233 | ||
6234 | buf->allocated = vstatfs.allocated(); | |
6235 | buf->stored = vstatfs.stored(); | |
6236 | buf->compressed = vstatfs.compressed(); | |
6237 | buf->compressed_original = vstatfs.compressed_original(); | |
6238 | buf->compressed_allocated = vstatfs.compressed_allocated(); | |
6239 | } | |
6240 | ||
6241 | dout(20) << __func__ << *buf << dendl; | |
6242 | return 0; | |
6243 | } | |
6244 | ||
6245 | // --------------- | |
6246 | // cache | |
6247 | ||
6248 | BlueStore::CollectionRef BlueStore::_get_collection(const coll_t& cid) | |
6249 | { | |
6250 | RWLock::RLocker l(coll_lock); | |
6251 | ceph::unordered_map<coll_t,CollectionRef>::iterator cp = coll_map.find(cid); | |
6252 | if (cp == coll_map.end()) | |
6253 | return CollectionRef(); | |
6254 | return cp->second; | |
6255 | } | |
6256 | ||
6257 | void BlueStore::_queue_reap_collection(CollectionRef& c) | |
6258 | { | |
6259 | dout(10) << __func__ << " " << c << " " << c->cid << dendl; | |
6260 | std::lock_guard<std::mutex> l(reap_lock); | |
6261 | removed_collections.push_back(c); | |
6262 | } | |
6263 | ||
6264 | void BlueStore::_reap_collections() | |
6265 | { | |
6266 | list<CollectionRef> removed_colls; | |
6267 | { | |
6268 | std::lock_guard<std::mutex> l(reap_lock); | |
6269 | removed_colls.swap(removed_collections); | |
6270 | } | |
6271 | ||
6272 | bool all_reaped = true; | |
6273 | ||
6274 | for (list<CollectionRef>::iterator p = removed_colls.begin(); | |
6275 | p != removed_colls.end(); | |
6276 | ++p) { | |
6277 | CollectionRef c = *p; | |
6278 | dout(10) << __func__ << " " << c << " " << c->cid << dendl; | |
6279 | if (c->onode_map.map_any([&](OnodeRef o) { | |
6280 | assert(!o->exists); | |
6281 | if (o->flushing_count.load()) { | |
6282 | dout(10) << __func__ << " " << c << " " << c->cid << " " << o->oid | |
6283 | << " flush_txns " << o->flushing_count << dendl; | |
6284 | return false; | |
6285 | } | |
6286 | return true; | |
6287 | })) { | |
6288 | all_reaped = false; | |
6289 | continue; | |
6290 | } | |
6291 | c->onode_map.clear(); | |
6292 | dout(10) << __func__ << " " << c << " " << c->cid << " done" << dendl; | |
6293 | } | |
6294 | ||
6295 | if (all_reaped) { | |
6296 | dout(10) << __func__ << " all reaped" << dendl; | |
6297 | } | |
6298 | } | |
6299 | ||
6300 | void BlueStore::_update_cache_logger() | |
6301 | { | |
6302 | uint64_t num_onodes = 0; | |
6303 | uint64_t num_extents = 0; | |
6304 | uint64_t num_blobs = 0; | |
6305 | uint64_t num_buffers = 0; | |
6306 | uint64_t num_buffer_bytes = 0; | |
6307 | for (auto c : cache_shards) { | |
6308 | c->add_stats(&num_onodes, &num_extents, &num_blobs, | |
6309 | &num_buffers, &num_buffer_bytes); | |
6310 | } | |
6311 | logger->set(l_bluestore_onodes, num_onodes); | |
6312 | logger->set(l_bluestore_extents, num_extents); | |
6313 | logger->set(l_bluestore_blobs, num_blobs); | |
6314 | logger->set(l_bluestore_buffers, num_buffers); | |
6315 | logger->set(l_bluestore_buffer_bytes, num_buffer_bytes); | |
6316 | } | |
6317 | ||
6318 | // --------------- | |
6319 | // read operations | |
6320 | ||
6321 | ObjectStore::CollectionHandle BlueStore::open_collection(const coll_t& cid) | |
6322 | { | |
6323 | return _get_collection(cid); | |
6324 | } | |
6325 | ||
6326 | bool BlueStore::exists(const coll_t& cid, const ghobject_t& oid) | |
6327 | { | |
6328 | CollectionHandle c = _get_collection(cid); | |
6329 | if (!c) | |
6330 | return false; | |
6331 | return exists(c, oid); | |
6332 | } | |
6333 | ||
6334 | bool BlueStore::exists(CollectionHandle &c_, const ghobject_t& oid) | |
6335 | { | |
6336 | Collection *c = static_cast<Collection *>(c_.get()); | |
6337 | dout(10) << __func__ << " " << c->cid << " " << oid << dendl; | |
6338 | if (!c->exists) | |
6339 | return false; | |
6340 | ||
6341 | bool r = true; | |
6342 | ||
6343 | { | |
6344 | RWLock::RLocker l(c->lock); | |
6345 | OnodeRef o = c->get_onode(oid, false); | |
6346 | if (!o || !o->exists) | |
6347 | r = false; | |
6348 | } | |
6349 | ||
6350 | return r; | |
6351 | } | |
6352 | ||
6353 | int BlueStore::stat( | |
6354 | const coll_t& cid, | |
6355 | const ghobject_t& oid, | |
6356 | struct stat *st, | |
6357 | bool allow_eio) | |
6358 | { | |
6359 | CollectionHandle c = _get_collection(cid); | |
6360 | if (!c) | |
6361 | return -ENOENT; | |
6362 | return stat(c, oid, st, allow_eio); | |
6363 | } | |
6364 | ||
6365 | int BlueStore::stat( | |
6366 | CollectionHandle &c_, | |
6367 | const ghobject_t& oid, | |
6368 | struct stat *st, | |
6369 | bool allow_eio) | |
6370 | { | |
6371 | Collection *c = static_cast<Collection *>(c_.get()); | |
6372 | if (!c->exists) | |
6373 | return -ENOENT; | |
6374 | dout(10) << __func__ << " " << c->get_cid() << " " << oid << dendl; | |
6375 | ||
6376 | { | |
6377 | RWLock::RLocker l(c->lock); | |
6378 | OnodeRef o = c->get_onode(oid, false); | |
6379 | if (!o || !o->exists) | |
6380 | return -ENOENT; | |
6381 | st->st_size = o->onode.size; | |
6382 | st->st_blksize = 4096; | |
6383 | st->st_blocks = (st->st_size + st->st_blksize - 1) / st->st_blksize; | |
6384 | st->st_nlink = 1; | |
6385 | } | |
6386 | ||
6387 | int r = 0; | |
6388 | if (_debug_mdata_eio(oid)) { | |
6389 | r = -EIO; | |
6390 | derr << __func__ << " " << c->cid << " " << oid << " INJECT EIO" << dendl; | |
6391 | } | |
6392 | return r; | |
6393 | } | |
6394 | int BlueStore::set_collection_opts( | |
6395 | const coll_t& cid, | |
6396 | const pool_opts_t& opts) | |
6397 | { | |
6398 | CollectionHandle ch = _get_collection(cid); | |
6399 | if (!ch) | |
6400 | return -ENOENT; | |
6401 | Collection *c = static_cast<Collection *>(ch.get()); | |
6402 | dout(15) << __func__ << " " << cid << " options " << opts << dendl; | |
6403 | if (!c->exists) | |
6404 | return -ENOENT; | |
6405 | RWLock::WLocker l(c->lock); | |
6406 | c->pool_opts = opts; | |
6407 | return 0; | |
6408 | } | |
6409 | ||
6410 | int BlueStore::read( | |
6411 | const coll_t& cid, | |
6412 | const ghobject_t& oid, | |
6413 | uint64_t offset, | |
6414 | size_t length, | |
6415 | bufferlist& bl, | |
6416 | uint32_t op_flags) | |
6417 | { | |
6418 | CollectionHandle c = _get_collection(cid); | |
6419 | if (!c) | |
6420 | return -ENOENT; | |
6421 | return read(c, oid, offset, length, bl, op_flags); | |
6422 | } | |
6423 | ||
6424 | int BlueStore::read( | |
6425 | CollectionHandle &c_, | |
6426 | const ghobject_t& oid, | |
6427 | uint64_t offset, | |
6428 | size_t length, | |
6429 | bufferlist& bl, | |
6430 | uint32_t op_flags) | |
6431 | { | |
6432 | utime_t start = ceph_clock_now(); | |
6433 | Collection *c = static_cast<Collection *>(c_.get()); | |
6434 | const coll_t &cid = c->get_cid(); | |
6435 | dout(15) << __func__ << " " << cid << " " << oid | |
6436 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
6437 | << dendl; | |
6438 | if (!c->exists) | |
6439 | return -ENOENT; | |
6440 | ||
6441 | bl.clear(); | |
6442 | int r; | |
6443 | { | |
6444 | RWLock::RLocker l(c->lock); | |
6445 | utime_t start1 = ceph_clock_now(); | |
6446 | OnodeRef o = c->get_onode(oid, false); | |
6447 | logger->tinc(l_bluestore_read_onode_meta_lat, ceph_clock_now() - start1); | |
6448 | if (!o || !o->exists) { | |
6449 | r = -ENOENT; | |
6450 | goto out; | |
6451 | } | |
6452 | ||
6453 | if (offset == length && offset == 0) | |
6454 | length = o->onode.size; | |
6455 | ||
6456 | r = _do_read(c, o, offset, length, bl, op_flags); | |
6457 | if (r == -EIO) { | |
6458 | logger->inc(l_bluestore_read_eio); | |
6459 | } | |
6460 | } | |
6461 | ||
6462 | out: | |
6463 | if (r == 0 && _debug_data_eio(oid)) { | |
6464 | r = -EIO; | |
6465 | derr << __func__ << " " << c->cid << " " << oid << " INJECT EIO" << dendl; | |
6466 | } else if (cct->_conf->bluestore_debug_random_read_err && | |
6467 | (rand() % (int)(cct->_conf->bluestore_debug_random_read_err * 100.0)) == 0) { | |
6468 | dout(0) << __func__ << ": inject random EIO" << dendl; | |
6469 | r = -EIO; | |
6470 | } | |
6471 | dout(10) << __func__ << " " << cid << " " << oid | |
6472 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
6473 | << " = " << r << dendl; | |
6474 | logger->tinc(l_bluestore_read_lat, ceph_clock_now() - start); | |
6475 | return r; | |
6476 | } | |
6477 | ||
6478 | // -------------------------------------------------------- | |
6479 | // intermediate data structures used while reading | |
6480 | struct region_t { | |
6481 | uint64_t logical_offset; | |
6482 | uint64_t blob_xoffset; //region offset within the blob | |
6483 | uint64_t length; | |
6484 | bufferlist bl; | |
6485 | ||
6486 | // used later in read process | |
6487 | uint64_t front = 0; | |
6488 | uint64_t r_off = 0; | |
6489 | ||
6490 | region_t(uint64_t offset, uint64_t b_offs, uint64_t len) | |
6491 | : logical_offset(offset), | |
6492 | blob_xoffset(b_offs), | |
6493 | length(len){} | |
6494 | region_t(const region_t& from) | |
6495 | : logical_offset(from.logical_offset), | |
6496 | blob_xoffset(from.blob_xoffset), | |
6497 | length(from.length){} | |
6498 | ||
6499 | friend ostream& operator<<(ostream& out, const region_t& r) { | |
6500 | return out << "0x" << std::hex << r.logical_offset << ":" | |
6501 | << r.blob_xoffset << "~" << r.length << std::dec; | |
6502 | } | |
6503 | }; | |
6504 | ||
6505 | typedef list<region_t> regions2read_t; | |
6506 | typedef map<BlueStore::BlobRef, regions2read_t> blobs2read_t; | |
6507 | ||
6508 | int BlueStore::_do_read( | |
6509 | Collection *c, | |
6510 | OnodeRef o, | |
6511 | uint64_t offset, | |
6512 | size_t length, | |
6513 | bufferlist& bl, | |
6514 | uint32_t op_flags) | |
6515 | { | |
6516 | FUNCTRACE(); | |
6517 | int r = 0; | |
6518 | ||
6519 | dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length | |
6520 | << " size 0x" << o->onode.size << " (" << std::dec | |
6521 | << o->onode.size << ")" << dendl; | |
6522 | bl.clear(); | |
6523 | ||
6524 | if (offset >= o->onode.size) { | |
6525 | return r; | |
6526 | } | |
6527 | ||
6528 | // generally, don't buffer anything, unless the client explicitly requests | |
6529 | // it. | |
6530 | bool buffered = false; | |
6531 | if (op_flags & CEPH_OSD_OP_FLAG_FADVISE_WILLNEED) { | |
6532 | dout(20) << __func__ << " will do buffered read" << dendl; | |
6533 | buffered = true; | |
6534 | } else if (cct->_conf->bluestore_default_buffered_read && | |
6535 | (op_flags & (CEPH_OSD_OP_FLAG_FADVISE_DONTNEED | | |
6536 | CEPH_OSD_OP_FLAG_FADVISE_NOCACHE)) == 0) { | |
6537 | dout(20) << __func__ << " defaulting to buffered read" << dendl; | |
6538 | buffered = true; | |
6539 | } | |
6540 | ||
6541 | if (offset + length > o->onode.size) { | |
6542 | length = o->onode.size - offset; | |
6543 | } | |
6544 | ||
6545 | utime_t start = ceph_clock_now(); | |
6546 | o->extent_map.fault_range(db, offset, length); | |
6547 | logger->tinc(l_bluestore_read_onode_meta_lat, ceph_clock_now() - start); | |
6548 | _dump_onode(o); | |
6549 | ||
6550 | ready_regions_t ready_regions; | |
6551 | ||
6552 | // build blob-wise list to of stuff read (that isn't cached) | |
6553 | blobs2read_t blobs2read; | |
6554 | unsigned left = length; | |
6555 | uint64_t pos = offset; | |
6556 | unsigned num_regions = 0; | |
6557 | auto lp = o->extent_map.seek_lextent(offset); | |
6558 | while (left > 0 && lp != o->extent_map.extent_map.end()) { | |
6559 | if (pos < lp->logical_offset) { | |
6560 | unsigned hole = lp->logical_offset - pos; | |
6561 | if (hole >= left) { | |
6562 | break; | |
6563 | } | |
6564 | dout(30) << __func__ << " hole 0x" << std::hex << pos << "~" << hole | |
6565 | << std::dec << dendl; | |
6566 | pos += hole; | |
6567 | left -= hole; | |
6568 | } | |
6569 | BlobRef bptr = lp->blob; | |
6570 | unsigned l_off = pos - lp->logical_offset; | |
6571 | unsigned b_off = l_off + lp->blob_offset; | |
6572 | unsigned b_len = std::min(left, lp->length - l_off); | |
6573 | ||
6574 | ready_regions_t cache_res; | |
6575 | interval_set<uint32_t> cache_interval; | |
6576 | bptr->shared_blob->bc.read( | |
6577 | bptr->shared_blob->get_cache(), b_off, b_len, cache_res, cache_interval); | |
6578 | dout(20) << __func__ << " blob " << *bptr << std::hex | |
6579 | << " need 0x" << b_off << "~" << b_len | |
6580 | << " cache has 0x" << cache_interval | |
6581 | << std::dec << dendl; | |
6582 | ||
6583 | auto pc = cache_res.begin(); | |
6584 | while (b_len > 0) { | |
6585 | unsigned l; | |
6586 | if (pc != cache_res.end() && | |
6587 | pc->first == b_off) { | |
6588 | l = pc->second.length(); | |
6589 | ready_regions[pos].claim(pc->second); | |
6590 | dout(30) << __func__ << " use cache 0x" << std::hex << pos << ": 0x" | |
6591 | << b_off << "~" << l << std::dec << dendl; | |
6592 | ++pc; | |
6593 | } else { | |
6594 | l = b_len; | |
6595 | if (pc != cache_res.end()) { | |
6596 | assert(pc->first > b_off); | |
6597 | l = pc->first - b_off; | |
6598 | } | |
6599 | dout(30) << __func__ << " will read 0x" << std::hex << pos << ": 0x" | |
6600 | << b_off << "~" << l << std::dec << dendl; | |
6601 | blobs2read[bptr].emplace_back(region_t(pos, b_off, l)); | |
6602 | ++num_regions; | |
6603 | } | |
6604 | pos += l; | |
6605 | b_off += l; | |
6606 | left -= l; | |
6607 | b_len -= l; | |
6608 | } | |
6609 | ++lp; | |
6610 | } | |
6611 | ||
6612 | // read raw blob data. use aio if we have >1 blobs to read. | |
6613 | start = ceph_clock_now(); // for the sake of simplicity | |
6614 | // measure the whole block below. | |
6615 | // The error isn't that much... | |
6616 | vector<bufferlist> compressed_blob_bls; | |
6617 | IOContext ioc(cct, NULL, true); // allow EIO | |
6618 | for (auto& p : blobs2read) { | |
6619 | BlobRef bptr = p.first; | |
6620 | dout(20) << __func__ << " blob " << *bptr << std::hex | |
6621 | << " need " << p.second << std::dec << dendl; | |
6622 | if (bptr->get_blob().is_compressed()) { | |
6623 | // read the whole thing | |
6624 | if (compressed_blob_bls.empty()) { | |
6625 | // ensure we avoid any reallocation on subsequent blobs | |
6626 | compressed_blob_bls.reserve(blobs2read.size()); | |
6627 | } | |
6628 | compressed_blob_bls.push_back(bufferlist()); | |
6629 | bufferlist& bl = compressed_blob_bls.back(); | |
6630 | r = bptr->get_blob().map( | |
6631 | 0, bptr->get_blob().get_ondisk_length(), | |
6632 | [&](uint64_t offset, uint64_t length) { | |
6633 | int r; | |
6634 | // use aio if there are more regions to read than those in this blob | |
6635 | if (num_regions > p.second.size()) { | |
6636 | r = bdev->aio_read(offset, length, &bl, &ioc); | |
6637 | } else { | |
6638 | r = bdev->read(offset, length, &bl, &ioc, false); | |
6639 | } | |
6640 | if (r < 0) | |
6641 | return r; | |
6642 | return 0; | |
6643 | }); | |
6644 | if (r < 0) { | |
6645 | derr << __func__ << " bdev-read failed: " << cpp_strerror(r) << dendl; | |
6646 | if (r == -EIO) { | |
6647 | // propagate EIO to caller | |
6648 | return r; | |
6649 | } | |
6650 | assert(r == 0); | |
6651 | } | |
6652 | } else { | |
6653 | // read the pieces | |
6654 | for (auto& reg : p.second) { | |
6655 | // determine how much of the blob to read | |
6656 | uint64_t chunk_size = bptr->get_blob().get_chunk_size(block_size); | |
6657 | reg.r_off = reg.blob_xoffset; | |
6658 | uint64_t r_len = reg.length; | |
6659 | reg.front = reg.r_off % chunk_size; | |
6660 | if (reg.front) { | |
6661 | reg.r_off -= reg.front; | |
6662 | r_len += reg.front; | |
6663 | } | |
6664 | unsigned tail = r_len % chunk_size; | |
6665 | if (tail) { | |
6666 | r_len += chunk_size - tail; | |
6667 | } | |
6668 | dout(20) << __func__ << " region 0x" << std::hex | |
6669 | << reg.logical_offset | |
6670 | << ": 0x" << reg.blob_xoffset << "~" << reg.length | |
6671 | << " reading 0x" << reg.r_off << "~" << r_len << std::dec | |
6672 | << dendl; | |
6673 | ||
6674 | // read it | |
6675 | r = bptr->get_blob().map( | |
6676 | reg.r_off, r_len, | |
6677 | [&](uint64_t offset, uint64_t length) { | |
6678 | int r; | |
6679 | // use aio if there is more than one region to read | |
6680 | if (num_regions > 1) { | |
6681 | r = bdev->aio_read(offset, length, ®.bl, &ioc); | |
6682 | } else { | |
6683 | r = bdev->read(offset, length, ®.bl, &ioc, false); | |
6684 | } | |
6685 | if (r < 0) | |
6686 | return r; | |
6687 | return 0; | |
6688 | }); | |
6689 | if (r < 0) { | |
6690 | derr << __func__ << " bdev-read failed: " << cpp_strerror(r) | |
6691 | << dendl; | |
6692 | if (r == -EIO) { | |
6693 | // propagate EIO to caller | |
6694 | return r; | |
6695 | } | |
6696 | assert(r == 0); | |
6697 | } | |
6698 | assert(reg.bl.length() == r_len); | |
6699 | } | |
6700 | } | |
6701 | } | |
6702 | if (ioc.has_pending_aios()) { | |
6703 | bdev->aio_submit(&ioc); | |
6704 | dout(20) << __func__ << " waiting for aio" << dendl; | |
6705 | ioc.aio_wait(); | |
6706 | r = ioc.get_return_value(); | |
6707 | if (r < 0) { | |
6708 | assert(r == -EIO); // no other errors allowed | |
6709 | return -EIO; | |
6710 | } | |
6711 | } | |
6712 | logger->tinc(l_bluestore_read_wait_aio_lat, ceph_clock_now() - start); | |
6713 | ||
6714 | // enumerate and decompress desired blobs | |
6715 | auto p = compressed_blob_bls.begin(); | |
6716 | blobs2read_t::iterator b2r_it = blobs2read.begin(); | |
6717 | while (b2r_it != blobs2read.end()) { | |
6718 | BlobRef bptr = b2r_it->first; | |
6719 | dout(20) << __func__ << " blob " << *bptr << std::hex | |
6720 | << " need 0x" << b2r_it->second << std::dec << dendl; | |
6721 | if (bptr->get_blob().is_compressed()) { | |
6722 | assert(p != compressed_blob_bls.end()); | |
6723 | bufferlist& compressed_bl = *p++; | |
6724 | if (_verify_csum(o, &bptr->get_blob(), 0, compressed_bl, | |
6725 | b2r_it->second.front().logical_offset) < 0) { | |
6726 | return -EIO; | |
6727 | } | |
6728 | bufferlist raw_bl; | |
6729 | r = _decompress(compressed_bl, &raw_bl); | |
6730 | if (r < 0) | |
6731 | return r; | |
6732 | if (buffered) { | |
6733 | bptr->shared_blob->bc.did_read(bptr->shared_blob->get_cache(), 0, | |
6734 | raw_bl); | |
6735 | } | |
6736 | for (auto& i : b2r_it->second) { | |
6737 | ready_regions[i.logical_offset].substr_of( | |
6738 | raw_bl, i.blob_xoffset, i.length); | |
6739 | } | |
6740 | } else { | |
6741 | for (auto& reg : b2r_it->second) { | |
6742 | if (_verify_csum(o, &bptr->get_blob(), reg.r_off, reg.bl, | |
6743 | reg.logical_offset) < 0) { | |
6744 | return -EIO; | |
6745 | } | |
6746 | if (buffered) { | |
6747 | bptr->shared_blob->bc.did_read(bptr->shared_blob->get_cache(), | |
6748 | reg.r_off, reg.bl); | |
6749 | } | |
6750 | ||
6751 | // prune and keep result | |
6752 | ready_regions[reg.logical_offset].substr_of( | |
6753 | reg.bl, reg.front, reg.length); | |
6754 | } | |
6755 | } | |
6756 | ++b2r_it; | |
6757 | } | |
6758 | ||
6759 | // generate a resulting buffer | |
6760 | auto pr = ready_regions.begin(); | |
6761 | auto pr_end = ready_regions.end(); | |
6762 | pos = 0; | |
6763 | while (pos < length) { | |
6764 | if (pr != pr_end && pr->first == pos + offset) { | |
6765 | dout(30) << __func__ << " assemble 0x" << std::hex << pos | |
6766 | << ": data from 0x" << pr->first << "~" << pr->second.length() | |
6767 | << std::dec << dendl; | |
6768 | pos += pr->second.length(); | |
6769 | bl.claim_append(pr->second); | |
6770 | ++pr; | |
6771 | } else { | |
6772 | uint64_t l = length - pos; | |
6773 | if (pr != pr_end) { | |
6774 | assert(pr->first > pos + offset); | |
6775 | l = pr->first - (pos + offset); | |
6776 | } | |
6777 | dout(30) << __func__ << " assemble 0x" << std::hex << pos | |
6778 | << ": zeros for 0x" << (pos + offset) << "~" << l | |
6779 | << std::dec << dendl; | |
6780 | bl.append_zero(l); | |
6781 | pos += l; | |
6782 | } | |
6783 | } | |
6784 | assert(bl.length() == length); | |
6785 | assert(pos == length); | |
6786 | assert(pr == pr_end); | |
6787 | r = bl.length(); | |
6788 | return r; | |
6789 | } | |
6790 | ||
6791 | int BlueStore::_verify_csum(OnodeRef& o, | |
6792 | const bluestore_blob_t* blob, uint64_t blob_xoffset, | |
6793 | const bufferlist& bl, | |
6794 | uint64_t logical_offset) const | |
6795 | { | |
6796 | int bad; | |
6797 | uint64_t bad_csum; | |
6798 | utime_t start = ceph_clock_now(); | |
6799 | int r = blob->verify_csum(blob_xoffset, bl, &bad, &bad_csum); | |
6800 | if (r < 0) { | |
6801 | if (r == -1) { | |
6802 | PExtentVector pex; | |
6803 | blob->map( | |
6804 | bad, | |
6805 | blob->get_csum_chunk_size(), | |
6806 | [&](uint64_t offset, uint64_t length) { | |
6807 | pex.emplace_back(bluestore_pextent_t(offset, length)); | |
6808 | return 0; | |
6809 | }); | |
6810 | derr << __func__ << " bad " | |
6811 | << Checksummer::get_csum_type_string(blob->csum_type) | |
6812 | << "/0x" << std::hex << blob->get_csum_chunk_size() | |
6813 | << " checksum at blob offset 0x" << bad | |
6814 | << ", got 0x" << bad_csum << ", expected 0x" | |
6815 | << blob->get_csum_item(bad / blob->get_csum_chunk_size()) << std::dec | |
6816 | << ", device location " << pex | |
6817 | << ", logical extent 0x" << std::hex | |
6818 | << (logical_offset + bad - blob_xoffset) << "~" | |
6819 | << blob->get_csum_chunk_size() << std::dec | |
6820 | << ", object " << o->oid | |
6821 | << dendl; | |
6822 | } else { | |
6823 | derr << __func__ << " failed with exit code: " << cpp_strerror(r) << dendl; | |
6824 | } | |
6825 | } | |
6826 | logger->tinc(l_bluestore_csum_lat, ceph_clock_now() - start); | |
6827 | return r; | |
6828 | } | |
6829 | ||
6830 | int BlueStore::_decompress(bufferlist& source, bufferlist* result) | |
6831 | { | |
6832 | int r = 0; | |
6833 | utime_t start = ceph_clock_now(); | |
6834 | bufferlist::iterator i = source.begin(); | |
6835 | bluestore_compression_header_t chdr; | |
6836 | ::decode(chdr, i); | |
6837 | int alg = int(chdr.type); | |
6838 | CompressorRef cp = compressor; | |
6839 | if (!cp || (int)cp->get_type() != alg) { | |
6840 | cp = Compressor::create(cct, alg); | |
6841 | } | |
6842 | ||
6843 | if (!cp.get()) { | |
6844 | // if compressor isn't available - error, because cannot return | |
6845 | // decompressed data? | |
6846 | derr << __func__ << " can't load decompressor " << alg << dendl; | |
6847 | r = -EIO; | |
6848 | } else { | |
6849 | r = cp->decompress(i, chdr.length, *result); | |
6850 | if (r < 0) { | |
6851 | derr << __func__ << " decompression failed with exit code " << r << dendl; | |
6852 | r = -EIO; | |
6853 | } | |
6854 | } | |
6855 | logger->tinc(l_bluestore_decompress_lat, ceph_clock_now() - start); | |
6856 | return r; | |
6857 | } | |
6858 | ||
6859 | // this stores fiemap into interval_set, other variations | |
6860 | // use it internally | |
6861 | int BlueStore::_fiemap( | |
6862 | CollectionHandle &c_, | |
6863 | const ghobject_t& oid, | |
6864 | uint64_t offset, | |
6865 | size_t length, | |
6866 | interval_set<uint64_t>& destset) | |
6867 | { | |
6868 | Collection *c = static_cast<Collection *>(c_.get()); | |
6869 | if (!c->exists) | |
6870 | return -ENOENT; | |
6871 | { | |
6872 | RWLock::RLocker l(c->lock); | |
6873 | ||
6874 | OnodeRef o = c->get_onode(oid, false); | |
6875 | if (!o || !o->exists) { | |
6876 | return -ENOENT; | |
6877 | } | |
6878 | _dump_onode(o); | |
6879 | ||
6880 | dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length | |
6881 | << " size 0x" << o->onode.size << std::dec << dendl; | |
6882 | ||
6883 | boost::intrusive::set<Extent>::iterator ep, eend; | |
6884 | if (offset >= o->onode.size) | |
6885 | goto out; | |
6886 | ||
6887 | if (offset + length > o->onode.size) { | |
6888 | length = o->onode.size - offset; | |
6889 | } | |
6890 | ||
6891 | o->extent_map.fault_range(db, offset, length); | |
6892 | eend = o->extent_map.extent_map.end(); | |
6893 | ep = o->extent_map.seek_lextent(offset); | |
6894 | while (length > 0) { | |
6895 | dout(20) << __func__ << " offset " << offset << dendl; | |
6896 | if (ep != eend && ep->logical_offset + ep->length <= offset) { | |
6897 | ++ep; | |
6898 | continue; | |
6899 | } | |
6900 | ||
6901 | uint64_t x_len = length; | |
6902 | if (ep != eend && ep->logical_offset <= offset) { | |
6903 | uint64_t x_off = offset - ep->logical_offset; | |
6904 | x_len = MIN(x_len, ep->length - x_off); | |
6905 | dout(30) << __func__ << " lextent 0x" << std::hex << offset << "~" | |
6906 | << x_len << std::dec << " blob " << ep->blob << dendl; | |
6907 | destset.insert(offset, x_len); | |
6908 | length -= x_len; | |
6909 | offset += x_len; | |
6910 | if (x_off + x_len == ep->length) | |
6911 | ++ep; | |
6912 | continue; | |
6913 | } | |
6914 | if (ep != eend && | |
6915 | ep->logical_offset > offset && | |
6916 | ep->logical_offset - offset < x_len) { | |
6917 | x_len = ep->logical_offset - offset; | |
6918 | } | |
6919 | offset += x_len; | |
6920 | length -= x_len; | |
6921 | } | |
6922 | } | |
6923 | ||
6924 | out: | |
6925 | dout(20) << __func__ << " 0x" << std::hex << offset << "~" << length | |
6926 | << " size = 0x(" << destset << ")" << std::dec << dendl; | |
6927 | return 0; | |
6928 | } | |
6929 | ||
6930 | int BlueStore::fiemap( | |
6931 | const coll_t& cid, | |
6932 | const ghobject_t& oid, | |
6933 | uint64_t offset, | |
6934 | size_t len, | |
6935 | bufferlist& bl) | |
6936 | { | |
6937 | CollectionHandle c = _get_collection(cid); | |
6938 | if (!c) | |
6939 | return -ENOENT; | |
6940 | return fiemap(c, oid, offset, len, bl); | |
6941 | } | |
6942 | ||
6943 | int BlueStore::fiemap( | |
6944 | CollectionHandle &c_, | |
6945 | const ghobject_t& oid, | |
6946 | uint64_t offset, | |
6947 | size_t length, | |
6948 | bufferlist& bl) | |
6949 | { | |
6950 | interval_set<uint64_t> m; | |
6951 | int r = _fiemap(c_, oid, offset, length, m); | |
6952 | if (r >= 0) { | |
6953 | ::encode(m, bl); | |
6954 | } | |
6955 | return r; | |
6956 | } | |
6957 | ||
6958 | int BlueStore::fiemap( | |
6959 | const coll_t& cid, | |
6960 | const ghobject_t& oid, | |
6961 | uint64_t offset, | |
6962 | size_t len, | |
6963 | map<uint64_t, uint64_t>& destmap) | |
6964 | { | |
6965 | CollectionHandle c = _get_collection(cid); | |
6966 | if (!c) | |
6967 | return -ENOENT; | |
6968 | return fiemap(c, oid, offset, len, destmap); | |
6969 | } | |
6970 | ||
6971 | int BlueStore::fiemap( | |
6972 | CollectionHandle &c_, | |
6973 | const ghobject_t& oid, | |
6974 | uint64_t offset, | |
6975 | size_t length, | |
6976 | map<uint64_t, uint64_t>& destmap) | |
6977 | { | |
6978 | interval_set<uint64_t> m; | |
6979 | int r = _fiemap(c_, oid, offset, length, m); | |
6980 | if (r >= 0) { | |
6981 | m.move_into(destmap); | |
6982 | } | |
6983 | return r; | |
6984 | } | |
6985 | ||
6986 | int BlueStore::getattr( | |
6987 | const coll_t& cid, | |
6988 | const ghobject_t& oid, | |
6989 | const char *name, | |
6990 | bufferptr& value) | |
6991 | { | |
6992 | CollectionHandle c = _get_collection(cid); | |
6993 | if (!c) | |
6994 | return -ENOENT; | |
6995 | return getattr(c, oid, name, value); | |
6996 | } | |
6997 | ||
6998 | int BlueStore::getattr( | |
6999 | CollectionHandle &c_, | |
7000 | const ghobject_t& oid, | |
7001 | const char *name, | |
7002 | bufferptr& value) | |
7003 | { | |
7004 | Collection *c = static_cast<Collection *>(c_.get()); | |
7005 | dout(15) << __func__ << " " << c->cid << " " << oid << " " << name << dendl; | |
7006 | if (!c->exists) | |
7007 | return -ENOENT; | |
7008 | ||
7009 | int r; | |
7010 | { | |
7011 | RWLock::RLocker l(c->lock); | |
7012 | mempool::bluestore_cache_other::string k(name); | |
7013 | ||
7014 | OnodeRef o = c->get_onode(oid, false); | |
7015 | if (!o || !o->exists) { | |
7016 | r = -ENOENT; | |
7017 | goto out; | |
7018 | } | |
7019 | ||
7020 | if (!o->onode.attrs.count(k)) { | |
7021 | r = -ENODATA; | |
7022 | goto out; | |
7023 | } | |
7024 | value = o->onode.attrs[k]; | |
7025 | r = 0; | |
7026 | } | |
7027 | out: | |
7028 | if (r == 0 && _debug_mdata_eio(oid)) { | |
7029 | r = -EIO; | |
7030 | derr << __func__ << " " << c->cid << " " << oid << " INJECT EIO" << dendl; | |
7031 | } | |
7032 | dout(10) << __func__ << " " << c->cid << " " << oid << " " << name | |
7033 | << " = " << r << dendl; | |
7034 | return r; | |
7035 | } | |
7036 | ||
7037 | ||
7038 | int BlueStore::getattrs( | |
7039 | const coll_t& cid, | |
7040 | const ghobject_t& oid, | |
7041 | map<string,bufferptr>& aset) | |
7042 | { | |
7043 | CollectionHandle c = _get_collection(cid); | |
7044 | if (!c) | |
7045 | return -ENOENT; | |
7046 | return getattrs(c, oid, aset); | |
7047 | } | |
7048 | ||
7049 | int BlueStore::getattrs( | |
7050 | CollectionHandle &c_, | |
7051 | const ghobject_t& oid, | |
7052 | map<string,bufferptr>& aset) | |
7053 | { | |
7054 | Collection *c = static_cast<Collection *>(c_.get()); | |
7055 | dout(15) << __func__ << " " << c->cid << " " << oid << dendl; | |
7056 | if (!c->exists) | |
7057 | return -ENOENT; | |
7058 | ||
7059 | int r; | |
7060 | { | |
7061 | RWLock::RLocker l(c->lock); | |
7062 | ||
7063 | OnodeRef o = c->get_onode(oid, false); | |
7064 | if (!o || !o->exists) { | |
7065 | r = -ENOENT; | |
7066 | goto out; | |
7067 | } | |
7068 | for (auto& i : o->onode.attrs) { | |
7069 | aset.emplace(i.first.c_str(), i.second); | |
7070 | } | |
7071 | r = 0; | |
7072 | } | |
7073 | ||
7074 | out: | |
7075 | if (r == 0 && _debug_mdata_eio(oid)) { | |
7076 | r = -EIO; | |
7077 | derr << __func__ << " " << c->cid << " " << oid << " INJECT EIO" << dendl; | |
7078 | } | |
7079 | dout(10) << __func__ << " " << c->cid << " " << oid | |
7080 | << " = " << r << dendl; | |
7081 | return r; | |
7082 | } | |
7083 | ||
7084 | int BlueStore::list_collections(vector<coll_t>& ls) | |
7085 | { | |
7086 | RWLock::RLocker l(coll_lock); | |
7087 | for (ceph::unordered_map<coll_t, CollectionRef>::iterator p = coll_map.begin(); | |
7088 | p != coll_map.end(); | |
7089 | ++p) | |
7090 | ls.push_back(p->first); | |
7091 | return 0; | |
7092 | } | |
7093 | ||
7094 | bool BlueStore::collection_exists(const coll_t& c) | |
7095 | { | |
7096 | RWLock::RLocker l(coll_lock); | |
7097 | return coll_map.count(c); | |
7098 | } | |
7099 | ||
7100 | int BlueStore::collection_empty(const coll_t& cid, bool *empty) | |
7101 | { | |
7102 | dout(15) << __func__ << " " << cid << dendl; | |
7103 | vector<ghobject_t> ls; | |
7104 | ghobject_t next; | |
7105 | int r = collection_list(cid, ghobject_t(), ghobject_t::get_max(), 1, | |
7106 | &ls, &next); | |
7107 | if (r < 0) { | |
7108 | derr << __func__ << " collection_list returned: " << cpp_strerror(r) | |
7109 | << dendl; | |
7110 | return r; | |
7111 | } | |
7112 | *empty = ls.empty(); | |
7113 | dout(10) << __func__ << " " << cid << " = " << (int)(*empty) << dendl; | |
7114 | return 0; | |
7115 | } | |
7116 | ||
7117 | int BlueStore::collection_bits(const coll_t& cid) | |
7118 | { | |
7119 | dout(15) << __func__ << " " << cid << dendl; | |
7120 | CollectionRef c = _get_collection(cid); | |
7121 | if (!c) | |
7122 | return -ENOENT; | |
7123 | RWLock::RLocker l(c->lock); | |
7124 | dout(10) << __func__ << " " << cid << " = " << c->cnode.bits << dendl; | |
7125 | return c->cnode.bits; | |
7126 | } | |
7127 | ||
7128 | int BlueStore::collection_list( | |
7129 | const coll_t& cid, const ghobject_t& start, const ghobject_t& end, int max, | |
7130 | vector<ghobject_t> *ls, ghobject_t *pnext) | |
7131 | { | |
7132 | CollectionHandle c = _get_collection(cid); | |
7133 | if (!c) | |
7134 | return -ENOENT; | |
7135 | return collection_list(c, start, end, max, ls, pnext); | |
7136 | } | |
7137 | ||
7138 | int BlueStore::collection_list( | |
7139 | CollectionHandle &c_, const ghobject_t& start, const ghobject_t& end, int max, | |
7140 | vector<ghobject_t> *ls, ghobject_t *pnext) | |
7141 | { | |
7142 | Collection *c = static_cast<Collection *>(c_.get()); | |
7143 | dout(15) << __func__ << " " << c->cid | |
7144 | << " start " << start << " end " << end << " max " << max << dendl; | |
7145 | int r; | |
7146 | { | |
7147 | RWLock::RLocker l(c->lock); | |
7148 | r = _collection_list(c, start, end, max, ls, pnext); | |
7149 | } | |
7150 | ||
7151 | dout(10) << __func__ << " " << c->cid | |
7152 | << " start " << start << " end " << end << " max " << max | |
7153 | << " = " << r << ", ls.size() = " << ls->size() | |
7154 | << ", next = " << (pnext ? *pnext : ghobject_t()) << dendl; | |
7155 | return r; | |
7156 | } | |
7157 | ||
7158 | int BlueStore::_collection_list( | |
7159 | Collection *c, const ghobject_t& start, const ghobject_t& end, int max, | |
7160 | vector<ghobject_t> *ls, ghobject_t *pnext) | |
7161 | { | |
7162 | ||
7163 | if (!c->exists) | |
7164 | return -ENOENT; | |
7165 | ||
7166 | int r = 0; | |
7167 | ghobject_t static_next; | |
7168 | KeyValueDB::Iterator it; | |
7169 | string temp_start_key, temp_end_key; | |
7170 | string start_key, end_key; | |
7171 | bool set_next = false; | |
7172 | string pend; | |
7173 | bool temp; | |
7174 | ||
7175 | if (!pnext) | |
7176 | pnext = &static_next; | |
7177 | ||
7178 | if (start == ghobject_t::get_max() || | |
7179 | start.hobj.is_max()) { | |
7180 | goto out; | |
7181 | } | |
7182 | get_coll_key_range(c->cid, c->cnode.bits, &temp_start_key, &temp_end_key, | |
7183 | &start_key, &end_key); | |
7184 | dout(20) << __func__ | |
7185 | << " range " << pretty_binary_string(temp_start_key) | |
7186 | << " to " << pretty_binary_string(temp_end_key) | |
7187 | << " and " << pretty_binary_string(start_key) | |
7188 | << " to " << pretty_binary_string(end_key) | |
7189 | << " start " << start << dendl; | |
7190 | it = db->get_iterator(PREFIX_OBJ); | |
7191 | if (start == ghobject_t() || | |
7192 | start.hobj == hobject_t() || | |
7193 | start == c->cid.get_min_hobj()) { | |
7194 | it->upper_bound(temp_start_key); | |
7195 | temp = true; | |
7196 | } else { | |
7197 | string k; | |
7198 | get_object_key(cct, start, &k); | |
7199 | if (start.hobj.is_temp()) { | |
7200 | temp = true; | |
7201 | assert(k >= temp_start_key && k < temp_end_key); | |
7202 | } else { | |
7203 | temp = false; | |
7204 | assert(k >= start_key && k < end_key); | |
7205 | } | |
7206 | dout(20) << " start from " << pretty_binary_string(k) | |
7207 | << " temp=" << (int)temp << dendl; | |
7208 | it->lower_bound(k); | |
7209 | } | |
7210 | if (end.hobj.is_max()) { | |
7211 | pend = temp ? temp_end_key : end_key; | |
7212 | } else { | |
7213 | get_object_key(cct, end, &end_key); | |
7214 | if (end.hobj.is_temp()) { | |
7215 | if (temp) | |
7216 | pend = end_key; | |
7217 | else | |
7218 | goto out; | |
7219 | } else { | |
7220 | pend = temp ? temp_end_key : end_key; | |
7221 | } | |
7222 | } | |
7223 | dout(20) << __func__ << " pend " << pretty_binary_string(pend) << dendl; | |
7224 | while (true) { | |
7225 | if (!it->valid() || it->key() >= pend) { | |
7226 | if (!it->valid()) | |
7227 | dout(20) << __func__ << " iterator not valid (end of db?)" << dendl; | |
7228 | else | |
7229 | dout(20) << __func__ << " key " << pretty_binary_string(it->key()) | |
7230 | << " >= " << end << dendl; | |
7231 | if (temp) { | |
7232 | if (end.hobj.is_temp()) { | |
7233 | break; | |
7234 | } | |
7235 | dout(30) << __func__ << " switch to non-temp namespace" << dendl; | |
7236 | temp = false; | |
7237 | it->upper_bound(start_key); | |
7238 | pend = end_key; | |
7239 | dout(30) << __func__ << " pend " << pretty_binary_string(pend) << dendl; | |
7240 | continue; | |
7241 | } | |
7242 | break; | |
7243 | } | |
7244 | dout(30) << __func__ << " key " << pretty_binary_string(it->key()) << dendl; | |
7245 | if (is_extent_shard_key(it->key())) { | |
7246 | it->next(); | |
7247 | continue; | |
7248 | } | |
7249 | ghobject_t oid; | |
7250 | int r = get_key_object(it->key(), &oid); | |
7251 | assert(r == 0); | |
7252 | dout(20) << __func__ << " oid " << oid << " end " << end << dendl; | |
7253 | if (ls->size() >= (unsigned)max) { | |
7254 | dout(20) << __func__ << " reached max " << max << dendl; | |
7255 | *pnext = oid; | |
7256 | set_next = true; | |
7257 | break; | |
7258 | } | |
7259 | ls->push_back(oid); | |
7260 | it->next(); | |
7261 | } | |
7262 | out: | |
7263 | if (!set_next) { | |
7264 | *pnext = ghobject_t::get_max(); | |
7265 | } | |
7266 | ||
7267 | return r; | |
7268 | } | |
7269 | ||
7270 | int BlueStore::omap_get( | |
7271 | const coll_t& cid, ///< [in] Collection containing oid | |
7272 | const ghobject_t &oid, ///< [in] Object containing omap | |
7273 | bufferlist *header, ///< [out] omap header | |
7274 | map<string, bufferlist> *out /// < [out] Key to value map | |
7275 | ) | |
7276 | { | |
7277 | CollectionHandle c = _get_collection(cid); | |
7278 | if (!c) | |
7279 | return -ENOENT; | |
7280 | return omap_get(c, oid, header, out); | |
7281 | } | |
7282 | ||
7283 | int BlueStore::omap_get( | |
7284 | CollectionHandle &c_, ///< [in] Collection containing oid | |
7285 | const ghobject_t &oid, ///< [in] Object containing omap | |
7286 | bufferlist *header, ///< [out] omap header | |
7287 | map<string, bufferlist> *out /// < [out] Key to value map | |
7288 | ) | |
7289 | { | |
7290 | Collection *c = static_cast<Collection *>(c_.get()); | |
7291 | dout(15) << __func__ << " " << c->get_cid() << " oid " << oid << dendl; | |
7292 | if (!c->exists) | |
7293 | return -ENOENT; | |
7294 | RWLock::RLocker l(c->lock); | |
7295 | int r = 0; | |
7296 | OnodeRef o = c->get_onode(oid, false); | |
7297 | if (!o || !o->exists) { | |
7298 | r = -ENOENT; | |
7299 | goto out; | |
7300 | } | |
7301 | if (!o->onode.has_omap()) | |
7302 | goto out; | |
7303 | o->flush(); | |
7304 | { | |
7305 | KeyValueDB::Iterator it = db->get_iterator(PREFIX_OMAP); | |
7306 | string head, tail; | |
7307 | get_omap_header(o->onode.nid, &head); | |
7308 | get_omap_tail(o->onode.nid, &tail); | |
7309 | it->lower_bound(head); | |
7310 | while (it->valid()) { | |
7311 | if (it->key() == head) { | |
7312 | dout(30) << __func__ << " got header" << dendl; | |
7313 | *header = it->value(); | |
7314 | } else if (it->key() >= tail) { | |
7315 | dout(30) << __func__ << " reached tail" << dendl; | |
7316 | break; | |
7317 | } else { | |
7318 | string user_key; | |
7319 | decode_omap_key(it->key(), &user_key); | |
7320 | dout(30) << __func__ << " got " << pretty_binary_string(it->key()) | |
7321 | << " -> " << user_key << dendl; | |
7322 | (*out)[user_key] = it->value(); | |
7323 | } | |
7324 | it->next(); | |
7325 | } | |
7326 | } | |
7327 | out: | |
7328 | dout(10) << __func__ << " " << c->get_cid() << " oid " << oid << " = " << r | |
7329 | << dendl; | |
7330 | return r; | |
7331 | } | |
7332 | ||
7333 | int BlueStore::omap_get_header( | |
7334 | const coll_t& cid, ///< [in] Collection containing oid | |
7335 | const ghobject_t &oid, ///< [in] Object containing omap | |
7336 | bufferlist *header, ///< [out] omap header | |
7337 | bool allow_eio ///< [in] don't assert on eio | |
7338 | ) | |
7339 | { | |
7340 | CollectionHandle c = _get_collection(cid); | |
7341 | if (!c) | |
7342 | return -ENOENT; | |
7343 | return omap_get_header(c, oid, header, allow_eio); | |
7344 | } | |
7345 | ||
7346 | int BlueStore::omap_get_header( | |
7347 | CollectionHandle &c_, ///< [in] Collection containing oid | |
7348 | const ghobject_t &oid, ///< [in] Object containing omap | |
7349 | bufferlist *header, ///< [out] omap header | |
7350 | bool allow_eio ///< [in] don't assert on eio | |
7351 | ) | |
7352 | { | |
7353 | Collection *c = static_cast<Collection *>(c_.get()); | |
7354 | dout(15) << __func__ << " " << c->get_cid() << " oid " << oid << dendl; | |
7355 | if (!c->exists) | |
7356 | return -ENOENT; | |
7357 | RWLock::RLocker l(c->lock); | |
7358 | int r = 0; | |
7359 | OnodeRef o = c->get_onode(oid, false); | |
7360 | if (!o || !o->exists) { | |
7361 | r = -ENOENT; | |
7362 | goto out; | |
7363 | } | |
7364 | if (!o->onode.has_omap()) | |
7365 | goto out; | |
7366 | o->flush(); | |
7367 | { | |
7368 | string head; | |
7369 | get_omap_header(o->onode.nid, &head); | |
7370 | if (db->get(PREFIX_OMAP, head, header) >= 0) { | |
7371 | dout(30) << __func__ << " got header" << dendl; | |
7372 | } else { | |
7373 | dout(30) << __func__ << " no header" << dendl; | |
7374 | } | |
7375 | } | |
7376 | out: | |
7377 | dout(10) << __func__ << " " << c->get_cid() << " oid " << oid << " = " << r | |
7378 | << dendl; | |
7379 | return r; | |
7380 | } | |
7381 | ||
7382 | int BlueStore::omap_get_keys( | |
7383 | const coll_t& cid, ///< [in] Collection containing oid | |
7384 | const ghobject_t &oid, ///< [in] Object containing omap | |
7385 | set<string> *keys ///< [out] Keys defined on oid | |
7386 | ) | |
7387 | { | |
7388 | CollectionHandle c = _get_collection(cid); | |
7389 | if (!c) | |
7390 | return -ENOENT; | |
7391 | return omap_get_keys(c, oid, keys); | |
7392 | } | |
7393 | ||
7394 | int BlueStore::omap_get_keys( | |
7395 | CollectionHandle &c_, ///< [in] Collection containing oid | |
7396 | const ghobject_t &oid, ///< [in] Object containing omap | |
7397 | set<string> *keys ///< [out] Keys defined on oid | |
7398 | ) | |
7399 | { | |
7400 | Collection *c = static_cast<Collection *>(c_.get()); | |
7401 | dout(15) << __func__ << " " << c->get_cid() << " oid " << oid << dendl; | |
7402 | if (!c->exists) | |
7403 | return -ENOENT; | |
7404 | RWLock::RLocker l(c->lock); | |
7405 | int r = 0; | |
7406 | OnodeRef o = c->get_onode(oid, false); | |
7407 | if (!o || !o->exists) { | |
7408 | r = -ENOENT; | |
7409 | goto out; | |
7410 | } | |
7411 | if (!o->onode.has_omap()) | |
7412 | goto out; | |
7413 | o->flush(); | |
7414 | { | |
7415 | KeyValueDB::Iterator it = db->get_iterator(PREFIX_OMAP); | |
7416 | string head, tail; | |
7417 | get_omap_key(o->onode.nid, string(), &head); | |
7418 | get_omap_tail(o->onode.nid, &tail); | |
7419 | it->lower_bound(head); | |
7420 | while (it->valid()) { | |
7421 | if (it->key() >= tail) { | |
7422 | dout(30) << __func__ << " reached tail" << dendl; | |
7423 | break; | |
7424 | } | |
7425 | string user_key; | |
7426 | decode_omap_key(it->key(), &user_key); | |
7427 | dout(30) << __func__ << " got " << pretty_binary_string(it->key()) | |
7428 | << " -> " << user_key << dendl; | |
7429 | keys->insert(user_key); | |
7430 | it->next(); | |
7431 | } | |
7432 | } | |
7433 | out: | |
7434 | dout(10) << __func__ << " " << c->get_cid() << " oid " << oid << " = " << r | |
7435 | << dendl; | |
7436 | return r; | |
7437 | } | |
7438 | ||
7439 | int BlueStore::omap_get_values( | |
7440 | const coll_t& cid, ///< [in] Collection containing oid | |
7441 | const ghobject_t &oid, ///< [in] Object containing omap | |
7442 | const set<string> &keys, ///< [in] Keys to get | |
7443 | map<string, bufferlist> *out ///< [out] Returned keys and values | |
7444 | ) | |
7445 | { | |
7446 | CollectionHandle c = _get_collection(cid); | |
7447 | if (!c) | |
7448 | return -ENOENT; | |
7449 | return omap_get_values(c, oid, keys, out); | |
7450 | } | |
7451 | ||
7452 | int BlueStore::omap_get_values( | |
7453 | CollectionHandle &c_, ///< [in] Collection containing oid | |
7454 | const ghobject_t &oid, ///< [in] Object containing omap | |
7455 | const set<string> &keys, ///< [in] Keys to get | |
7456 | map<string, bufferlist> *out ///< [out] Returned keys and values | |
7457 | ) | |
7458 | { | |
7459 | Collection *c = static_cast<Collection *>(c_.get()); | |
7460 | dout(15) << __func__ << " " << c->get_cid() << " oid " << oid << dendl; | |
7461 | if (!c->exists) | |
7462 | return -ENOENT; | |
7463 | RWLock::RLocker l(c->lock); | |
7464 | int r = 0; | |
7465 | string final_key; | |
7466 | OnodeRef o = c->get_onode(oid, false); | |
7467 | if (!o || !o->exists) { | |
7468 | r = -ENOENT; | |
7469 | goto out; | |
7470 | } | |
7471 | if (!o->onode.has_omap()) | |
7472 | goto out; | |
7473 | o->flush(); | |
7474 | _key_encode_u64(o->onode.nid, &final_key); | |
7475 | final_key.push_back('.'); | |
7476 | for (set<string>::const_iterator p = keys.begin(); p != keys.end(); ++p) { | |
7477 | final_key.resize(9); // keep prefix | |
7478 | final_key += *p; | |
7479 | bufferlist val; | |
7480 | if (db->get(PREFIX_OMAP, final_key, &val) >= 0) { | |
7481 | dout(30) << __func__ << " got " << pretty_binary_string(final_key) | |
7482 | << " -> " << *p << dendl; | |
7483 | out->insert(make_pair(*p, val)); | |
7484 | } | |
7485 | } | |
7486 | out: | |
7487 | dout(10) << __func__ << " " << c->get_cid() << " oid " << oid << " = " << r | |
7488 | << dendl; | |
7489 | return r; | |
7490 | } | |
7491 | ||
7492 | int BlueStore::omap_check_keys( | |
7493 | const coll_t& cid, ///< [in] Collection containing oid | |
7494 | const ghobject_t &oid, ///< [in] Object containing omap | |
7495 | const set<string> &keys, ///< [in] Keys to check | |
7496 | set<string> *out ///< [out] Subset of keys defined on oid | |
7497 | ) | |
7498 | { | |
7499 | CollectionHandle c = _get_collection(cid); | |
7500 | if (!c) | |
7501 | return -ENOENT; | |
7502 | return omap_check_keys(c, oid, keys, out); | |
7503 | } | |
7504 | ||
7505 | int BlueStore::omap_check_keys( | |
7506 | CollectionHandle &c_, ///< [in] Collection containing oid | |
7507 | const ghobject_t &oid, ///< [in] Object containing omap | |
7508 | const set<string> &keys, ///< [in] Keys to check | |
7509 | set<string> *out ///< [out] Subset of keys defined on oid | |
7510 | ) | |
7511 | { | |
7512 | Collection *c = static_cast<Collection *>(c_.get()); | |
7513 | dout(15) << __func__ << " " << c->get_cid() << " oid " << oid << dendl; | |
7514 | if (!c->exists) | |
7515 | return -ENOENT; | |
7516 | RWLock::RLocker l(c->lock); | |
7517 | int r = 0; | |
7518 | string final_key; | |
7519 | OnodeRef o = c->get_onode(oid, false); | |
7520 | if (!o || !o->exists) { | |
7521 | r = -ENOENT; | |
7522 | goto out; | |
7523 | } | |
7524 | if (!o->onode.has_omap()) | |
7525 | goto out; | |
7526 | o->flush(); | |
7527 | _key_encode_u64(o->onode.nid, &final_key); | |
7528 | final_key.push_back('.'); | |
7529 | for (set<string>::const_iterator p = keys.begin(); p != keys.end(); ++p) { | |
7530 | final_key.resize(9); // keep prefix | |
7531 | final_key += *p; | |
7532 | bufferlist val; | |
7533 | if (db->get(PREFIX_OMAP, final_key, &val) >= 0) { | |
7534 | dout(30) << __func__ << " have " << pretty_binary_string(final_key) | |
7535 | << " -> " << *p << dendl; | |
7536 | out->insert(*p); | |
7537 | } else { | |
7538 | dout(30) << __func__ << " miss " << pretty_binary_string(final_key) | |
7539 | << " -> " << *p << dendl; | |
7540 | } | |
7541 | } | |
7542 | out: | |
7543 | dout(10) << __func__ << " " << c->get_cid() << " oid " << oid << " = " << r | |
7544 | << dendl; | |
7545 | return r; | |
7546 | } | |
7547 | ||
7548 | ObjectMap::ObjectMapIterator BlueStore::get_omap_iterator( | |
7549 | const coll_t& cid, ///< [in] collection | |
7550 | const ghobject_t &oid ///< [in] object | |
7551 | ) | |
7552 | { | |
7553 | CollectionHandle c = _get_collection(cid); | |
7554 | if (!c) { | |
7555 | dout(10) << __func__ << " " << cid << "doesn't exist" <<dendl; | |
7556 | return ObjectMap::ObjectMapIterator(); | |
7557 | } | |
7558 | return get_omap_iterator(c, oid); | |
7559 | } | |
7560 | ||
7561 | ObjectMap::ObjectMapIterator BlueStore::get_omap_iterator( | |
7562 | CollectionHandle &c_, ///< [in] collection | |
7563 | const ghobject_t &oid ///< [in] object | |
7564 | ) | |
7565 | { | |
7566 | Collection *c = static_cast<Collection *>(c_.get()); | |
7567 | dout(10) << __func__ << " " << c->get_cid() << " " << oid << dendl; | |
7568 | if (!c->exists) { | |
7569 | return ObjectMap::ObjectMapIterator(); | |
7570 | } | |
7571 | RWLock::RLocker l(c->lock); | |
7572 | OnodeRef o = c->get_onode(oid, false); | |
7573 | if (!o || !o->exists) { | |
7574 | dout(10) << __func__ << " " << oid << "doesn't exist" <<dendl; | |
7575 | return ObjectMap::ObjectMapIterator(); | |
7576 | } | |
7577 | o->flush(); | |
7578 | dout(10) << __func__ << " has_omap = " << (int)o->onode.has_omap() <<dendl; | |
7579 | KeyValueDB::Iterator it = db->get_iterator(PREFIX_OMAP); | |
7580 | return ObjectMap::ObjectMapIterator(new OmapIteratorImpl(c, o, it)); | |
7581 | } | |
7582 | ||
7583 | // ----------------- | |
7584 | // write helpers | |
7585 | ||
7586 | void BlueStore::_prepare_ondisk_format_super(KeyValueDB::Transaction& t) | |
7587 | { | |
7588 | dout(10) << __func__ << " ondisk_format " << ondisk_format | |
7589 | << " min_compat_ondisk_format " << min_compat_ondisk_format | |
7590 | << dendl; | |
7591 | assert(ondisk_format == latest_ondisk_format); | |
7592 | { | |
7593 | bufferlist bl; | |
7594 | ::encode(ondisk_format, bl); | |
7595 | t->set(PREFIX_SUPER, "ondisk_format", bl); | |
7596 | } | |
7597 | { | |
7598 | bufferlist bl; | |
7599 | ::encode(min_compat_ondisk_format, bl); | |
7600 | t->set(PREFIX_SUPER, "min_compat_ondisk_format", bl); | |
7601 | } | |
7602 | } | |
7603 | ||
7604 | int BlueStore::_open_super_meta() | |
7605 | { | |
7606 | // nid | |
7607 | { | |
7608 | nid_max = 0; | |
7609 | bufferlist bl; | |
7610 | db->get(PREFIX_SUPER, "nid_max", &bl); | |
7611 | bufferlist::iterator p = bl.begin(); | |
7612 | try { | |
7613 | uint64_t v; | |
7614 | ::decode(v, p); | |
7615 | nid_max = v; | |
7616 | } catch (buffer::error& e) { | |
7617 | derr << __func__ << " unable to read nid_max" << dendl; | |
7618 | return -EIO; | |
7619 | } | |
7620 | dout(10) << __func__ << " old nid_max " << nid_max << dendl; | |
7621 | nid_last = nid_max.load(); | |
7622 | } | |
7623 | ||
7624 | // blobid | |
7625 | { | |
7626 | blobid_max = 0; | |
7627 | bufferlist bl; | |
7628 | db->get(PREFIX_SUPER, "blobid_max", &bl); | |
7629 | bufferlist::iterator p = bl.begin(); | |
7630 | try { | |
7631 | uint64_t v; | |
7632 | ::decode(v, p); | |
7633 | blobid_max = v; | |
7634 | } catch (buffer::error& e) { | |
7635 | derr << __func__ << " unable to read blobid_max" << dendl; | |
7636 | return -EIO; | |
7637 | } | |
7638 | dout(10) << __func__ << " old blobid_max " << blobid_max << dendl; | |
7639 | blobid_last = blobid_max.load(); | |
7640 | } | |
7641 | ||
7642 | // freelist | |
7643 | { | |
7644 | bufferlist bl; | |
7645 | db->get(PREFIX_SUPER, "freelist_type", &bl); | |
7646 | if (bl.length()) { | |
7647 | freelist_type = std::string(bl.c_str(), bl.length()); | |
7648 | dout(10) << __func__ << " freelist_type " << freelist_type << dendl; | |
7649 | } else { | |
7650 | assert("Not Support extent freelist manager" == 0); | |
7651 | } | |
7652 | } | |
7653 | ||
7654 | // bluefs alloc | |
7655 | if (cct->_conf->bluestore_bluefs) { | |
7656 | bluefs_extents.clear(); | |
7657 | bufferlist bl; | |
7658 | db->get(PREFIX_SUPER, "bluefs_extents", &bl); | |
7659 | bufferlist::iterator p = bl.begin(); | |
7660 | try { | |
7661 | ::decode(bluefs_extents, p); | |
7662 | } | |
7663 | catch (buffer::error& e) { | |
7664 | derr << __func__ << " unable to read bluefs_extents" << dendl; | |
7665 | return -EIO; | |
7666 | } | |
7667 | dout(10) << __func__ << " bluefs_extents 0x" << std::hex << bluefs_extents | |
7668 | << std::dec << dendl; | |
7669 | } | |
7670 | ||
7671 | // ondisk format | |
7672 | int32_t compat_ondisk_format = 0; | |
7673 | { | |
7674 | bufferlist bl; | |
7675 | int r = db->get(PREFIX_SUPER, "ondisk_format", &bl); | |
7676 | if (r < 0) { | |
7677 | // base case: kraken bluestore is v1 and readable by v1 | |
7678 | dout(20) << __func__ << " missing ondisk_format; assuming kraken" | |
7679 | << dendl; | |
7680 | ondisk_format = 1; | |
7681 | compat_ondisk_format = 1; | |
7682 | } else { | |
7683 | auto p = bl.begin(); | |
7684 | try { | |
7685 | ::decode(ondisk_format, p); | |
7686 | } catch (buffer::error& e) { | |
7687 | derr << __func__ << " unable to read ondisk_format" << dendl; | |
7688 | return -EIO; | |
7689 | } | |
7690 | bl.clear(); | |
7691 | { | |
7692 | r = db->get(PREFIX_SUPER, "min_compat_ondisk_format", &bl); | |
7693 | assert(!r); | |
7694 | auto p = bl.begin(); | |
7695 | try { | |
7696 | ::decode(compat_ondisk_format, p); | |
7697 | } catch (buffer::error& e) { | |
7698 | derr << __func__ << " unable to read compat_ondisk_format" << dendl; | |
7699 | return -EIO; | |
7700 | } | |
7701 | } | |
7702 | } | |
7703 | dout(10) << __func__ << " ondisk_format " << ondisk_format | |
7704 | << " compat_ondisk_format " << compat_ondisk_format | |
7705 | << dendl; | |
7706 | } | |
7707 | ||
7708 | if (latest_ondisk_format < compat_ondisk_format) { | |
7709 | derr << __func__ << " compat_ondisk_format is " | |
7710 | << compat_ondisk_format << " but we only understand version " | |
7711 | << latest_ondisk_format << dendl; | |
7712 | return -EPERM; | |
7713 | } | |
7714 | if (ondisk_format < latest_ondisk_format) { | |
7715 | int r = _upgrade_super(); | |
7716 | if (r < 0) { | |
7717 | return r; | |
7718 | } | |
7719 | } | |
7720 | ||
7721 | { | |
7722 | bufferlist bl; | |
7723 | db->get(PREFIX_SUPER, "min_alloc_size", &bl); | |
7724 | auto p = bl.begin(); | |
7725 | try { | |
7726 | uint64_t val; | |
7727 | ::decode(val, p); | |
7728 | min_alloc_size = val; | |
7729 | min_alloc_size_order = ctz(val); | |
7730 | assert(min_alloc_size == 1u << min_alloc_size_order); | |
7731 | } catch (buffer::error& e) { | |
7732 | derr << __func__ << " unable to read min_alloc_size" << dendl; | |
7733 | return -EIO; | |
7734 | } | |
7735 | dout(10) << __func__ << " min_alloc_size 0x" << std::hex << min_alloc_size | |
7736 | << std::dec << dendl; | |
7737 | } | |
7738 | _open_statfs(); | |
7739 | _set_alloc_sizes(); | |
7740 | _set_throttle_params(); | |
7741 | ||
7742 | _set_csum(); | |
7743 | _set_compression(); | |
7744 | _set_blob_size(); | |
7745 | ||
7746 | return 0; | |
7747 | } | |
7748 | ||
7749 | int BlueStore::_upgrade_super() | |
7750 | { | |
7751 | dout(1) << __func__ << " from " << ondisk_format << ", latest " | |
7752 | << latest_ondisk_format << dendl; | |
7753 | assert(ondisk_format > 0); | |
7754 | assert(ondisk_format < latest_ondisk_format); | |
7755 | ||
7756 | if (ondisk_format == 1) { | |
7757 | // changes: | |
7758 | // - super: added ondisk_format | |
7759 | // - super: added min_readable_ondisk_format | |
7760 | // - super: added min_compat_ondisk_format | |
7761 | // - super: added min_alloc_size | |
7762 | // - super: removed min_min_alloc_size | |
7763 | KeyValueDB::Transaction t = db->get_transaction(); | |
7764 | { | |
7765 | bufferlist bl; | |
7766 | db->get(PREFIX_SUPER, "min_min_alloc_size", &bl); | |
7767 | auto p = bl.begin(); | |
7768 | try { | |
7769 | uint64_t val; | |
7770 | ::decode(val, p); | |
7771 | min_alloc_size = val; | |
7772 | } catch (buffer::error& e) { | |
7773 | derr << __func__ << " failed to read min_min_alloc_size" << dendl; | |
7774 | return -EIO; | |
7775 | } | |
7776 | t->set(PREFIX_SUPER, "min_alloc_size", bl); | |
7777 | t->rmkey(PREFIX_SUPER, "min_min_alloc_size"); | |
7778 | } | |
7779 | ondisk_format = 2; | |
7780 | _prepare_ondisk_format_super(t); | |
7781 | int r = db->submit_transaction_sync(t); | |
7782 | assert(r == 0); | |
7783 | } | |
7784 | ||
7785 | // done | |
7786 | dout(1) << __func__ << " done" << dendl; | |
7787 | return 0; | |
7788 | } | |
7789 | ||
7790 | void BlueStore::_assign_nid(TransContext *txc, OnodeRef o) | |
7791 | { | |
7792 | if (o->onode.nid) { | |
7793 | assert(o->exists); | |
7794 | return; | |
7795 | } | |
7796 | uint64_t nid = ++nid_last; | |
7797 | dout(20) << __func__ << " " << nid << dendl; | |
7798 | o->onode.nid = nid; | |
7799 | txc->last_nid = nid; | |
7800 | o->exists = true; | |
7801 | } | |
7802 | ||
7803 | uint64_t BlueStore::_assign_blobid(TransContext *txc) | |
7804 | { | |
7805 | uint64_t bid = ++blobid_last; | |
7806 | dout(20) << __func__ << " " << bid << dendl; | |
7807 | txc->last_blobid = bid; | |
7808 | return bid; | |
7809 | } | |
7810 | ||
7811 | void BlueStore::get_db_statistics(Formatter *f) | |
7812 | { | |
7813 | db->get_statistics(f); | |
7814 | } | |
7815 | ||
7816 | BlueStore::TransContext *BlueStore::_txc_create(OpSequencer *osr) | |
7817 | { | |
7818 | TransContext *txc = new TransContext(cct, osr); | |
7819 | txc->t = db->get_transaction(); | |
7820 | osr->queue_new(txc); | |
7821 | dout(20) << __func__ << " osr " << osr << " = " << txc | |
7822 | << " seq " << txc->seq << dendl; | |
7823 | return txc; | |
7824 | } | |
7825 | ||
7826 | void BlueStore::_txc_calc_cost(TransContext *txc) | |
7827 | { | |
7828 | // this is about the simplest model for transaction cost you can | |
7829 | // imagine. there is some fixed overhead cost by saying there is a | |
7830 | // minimum of one "io". and then we have some cost per "io" that is | |
7831 | // a configurable (with different hdd and ssd defaults), and add | |
7832 | // that to the bytes value. | |
7833 | int ios = 1; // one "io" for the kv commit | |
7834 | for (auto& p : txc->ioc.pending_aios) { | |
7835 | ios += p.iov.size(); | |
7836 | } | |
7837 | auto cost = throttle_cost_per_io.load(); | |
7838 | txc->cost = ios * cost + txc->bytes; | |
7839 | dout(10) << __func__ << " " << txc << " cost " << txc->cost << " (" | |
7840 | << ios << " ios * " << cost << " + " << txc->bytes | |
7841 | << " bytes)" << dendl; | |
7842 | } | |
7843 | ||
7844 | void BlueStore::_txc_update_store_statfs(TransContext *txc) | |
7845 | { | |
7846 | if (txc->statfs_delta.is_empty()) | |
7847 | return; | |
7848 | ||
7849 | logger->inc(l_bluestore_allocated, txc->statfs_delta.allocated()); | |
7850 | logger->inc(l_bluestore_stored, txc->statfs_delta.stored()); | |
7851 | logger->inc(l_bluestore_compressed, txc->statfs_delta.compressed()); | |
7852 | logger->inc(l_bluestore_compressed_allocated, txc->statfs_delta.compressed_allocated()); | |
7853 | logger->inc(l_bluestore_compressed_original, txc->statfs_delta.compressed_original()); | |
7854 | ||
7855 | { | |
7856 | std::lock_guard<std::mutex> l(vstatfs_lock); | |
7857 | vstatfs += txc->statfs_delta; | |
7858 | } | |
7859 | ||
7860 | bufferlist bl; | |
7861 | txc->statfs_delta.encode(bl); | |
7862 | ||
7863 | txc->t->merge(PREFIX_STAT, "bluestore_statfs", bl); | |
7864 | txc->statfs_delta.reset(); | |
7865 | } | |
7866 | ||
7867 | void BlueStore::_txc_state_proc(TransContext *txc) | |
7868 | { | |
7869 | while (true) { | |
7870 | dout(10) << __func__ << " txc " << txc | |
7871 | << " " << txc->get_state_name() << dendl; | |
7872 | switch (txc->state) { | |
7873 | case TransContext::STATE_PREPARE: | |
7874 | txc->log_state_latency(logger, l_bluestore_state_prepare_lat); | |
7875 | if (txc->ioc.has_pending_aios()) { | |
7876 | txc->state = TransContext::STATE_AIO_WAIT; | |
7877 | txc->had_ios = true; | |
7878 | _txc_aio_submit(txc); | |
7879 | return; | |
7880 | } | |
7881 | // ** fall-thru ** | |
7882 | ||
7883 | case TransContext::STATE_AIO_WAIT: | |
7884 | txc->log_state_latency(logger, l_bluestore_state_aio_wait_lat); | |
7885 | _txc_finish_io(txc); // may trigger blocked txc's too | |
7886 | return; | |
7887 | ||
7888 | case TransContext::STATE_IO_DONE: | |
7889 | //assert(txc->osr->qlock.is_locked()); // see _txc_finish_io | |
7890 | if (txc->had_ios) { | |
7891 | ++txc->osr->txc_with_unstable_io; | |
7892 | } | |
7893 | txc->log_state_latency(logger, l_bluestore_state_io_done_lat); | |
7894 | txc->state = TransContext::STATE_KV_QUEUED; | |
7895 | if (cct->_conf->bluestore_sync_submit_transaction) { | |
7896 | if (txc->last_nid >= nid_max || | |
7897 | txc->last_blobid >= blobid_max) { | |
7898 | dout(20) << __func__ | |
7899 | << " last_{nid,blobid} exceeds max, submit via kv thread" | |
7900 | << dendl; | |
7901 | } else if (txc->osr->kv_committing_serially) { | |
7902 | dout(20) << __func__ << " prior txc submitted via kv thread, us too" | |
7903 | << dendl; | |
7904 | // note: this is starvation-prone. once we have a txc in a busy | |
7905 | // sequencer that is committing serially it is possible to keep | |
7906 | // submitting new transactions fast enough that we get stuck doing | |
7907 | // so. the alternative is to block here... fixme? | |
7908 | } else if (txc->osr->txc_with_unstable_io) { | |
7909 | dout(20) << __func__ << " prior txc(s) with unstable ios " | |
7910 | << txc->osr->txc_with_unstable_io.load() << dendl; | |
7911 | } else if (cct->_conf->bluestore_debug_randomize_serial_transaction && | |
7912 | rand() % cct->_conf->bluestore_debug_randomize_serial_transaction | |
7913 | == 0) { | |
7914 | dout(20) << __func__ << " DEBUG randomly forcing submit via kv thread" | |
7915 | << dendl; | |
7916 | } else { | |
7917 | txc->state = TransContext::STATE_KV_SUBMITTED; | |
7918 | int r = cct->_conf->bluestore_debug_omit_kv_commit ? 0 : db->submit_transaction(txc->t); | |
7919 | assert(r == 0); | |
7920 | _txc_applied_kv(txc); | |
7921 | } | |
7922 | } | |
7923 | { | |
7924 | std::lock_guard<std::mutex> l(kv_lock); | |
7925 | kv_queue.push_back(txc); | |
7926 | kv_cond.notify_one(); | |
7927 | if (txc->state != TransContext::STATE_KV_SUBMITTED) { | |
7928 | kv_queue_unsubmitted.push_back(txc); | |
7929 | ++txc->osr->kv_committing_serially; | |
7930 | } | |
7931 | if (txc->had_ios) | |
7932 | kv_ios++; | |
7933 | kv_throttle_costs += txc->cost; | |
7934 | } | |
7935 | return; | |
7936 | case TransContext::STATE_KV_SUBMITTED: | |
7937 | txc->log_state_latency(logger, l_bluestore_state_kv_committing_lat); | |
7938 | txc->state = TransContext::STATE_KV_DONE; | |
7939 | _txc_committed_kv(txc); | |
7940 | // ** fall-thru ** | |
7941 | ||
7942 | case TransContext::STATE_KV_DONE: | |
7943 | txc->log_state_latency(logger, l_bluestore_state_kv_done_lat); | |
7944 | if (txc->deferred_txn) { | |
7945 | txc->state = TransContext::STATE_DEFERRED_QUEUED; | |
7946 | _deferred_queue(txc); | |
7947 | return; | |
7948 | } | |
7949 | txc->state = TransContext::STATE_FINISHING; | |
7950 | break; | |
7951 | ||
7952 | case TransContext::STATE_DEFERRED_CLEANUP: | |
7953 | txc->log_state_latency(logger, l_bluestore_state_deferred_cleanup_lat); | |
7954 | txc->state = TransContext::STATE_FINISHING; | |
7955 | // ** fall-thru ** | |
7956 | ||
7957 | case TransContext::STATE_FINISHING: | |
7958 | txc->log_state_latency(logger, l_bluestore_state_finishing_lat); | |
7959 | _txc_finish(txc); | |
7960 | return; | |
7961 | ||
7962 | default: | |
7963 | derr << __func__ << " unexpected txc " << txc | |
7964 | << " state " << txc->get_state_name() << dendl; | |
7965 | assert(0 == "unexpected txc state"); | |
7966 | return; | |
7967 | } | |
7968 | } | |
7969 | } | |
7970 | ||
7971 | void BlueStore::_txc_finish_io(TransContext *txc) | |
7972 | { | |
7973 | dout(20) << __func__ << " " << txc << dendl; | |
7974 | ||
7975 | /* | |
7976 | * we need to preserve the order of kv transactions, | |
7977 | * even though aio will complete in any order. | |
7978 | */ | |
7979 | ||
7980 | OpSequencer *osr = txc->osr.get(); | |
7981 | std::lock_guard<std::mutex> l(osr->qlock); | |
7982 | txc->state = TransContext::STATE_IO_DONE; | |
7983 | ||
7984 | // release aio contexts (including pinned buffers). | |
7985 | txc->ioc.running_aios.clear(); | |
7986 | ||
7987 | OpSequencer::q_list_t::iterator p = osr->q.iterator_to(*txc); | |
7988 | while (p != osr->q.begin()) { | |
7989 | --p; | |
7990 | if (p->state < TransContext::STATE_IO_DONE) { | |
7991 | dout(20) << __func__ << " " << txc << " blocked by " << &*p << " " | |
7992 | << p->get_state_name() << dendl; | |
7993 | return; | |
7994 | } | |
7995 | if (p->state > TransContext::STATE_IO_DONE) { | |
7996 | ++p; | |
7997 | break; | |
7998 | } | |
7999 | } | |
8000 | do { | |
8001 | _txc_state_proc(&*p++); | |
8002 | } while (p != osr->q.end() && | |
8003 | p->state == TransContext::STATE_IO_DONE); | |
8004 | ||
8005 | if (osr->kv_submitted_waiters && | |
8006 | osr->_is_all_kv_submitted()) { | |
8007 | osr->qcond.notify_all(); | |
8008 | } | |
8009 | } | |
8010 | ||
8011 | void BlueStore::_txc_write_nodes(TransContext *txc, KeyValueDB::Transaction t) | |
8012 | { | |
8013 | dout(20) << __func__ << " txc " << txc | |
8014 | << " onodes " << txc->onodes | |
8015 | << " shared_blobs " << txc->shared_blobs | |
8016 | << dendl; | |
8017 | ||
8018 | // finalize onodes | |
8019 | for (auto o : txc->onodes) { | |
8020 | // finalize extent_map shards | |
8021 | o->extent_map.update(t, false); | |
8022 | if (o->extent_map.needs_reshard()) { | |
8023 | o->extent_map.reshard(db, t); | |
8024 | o->extent_map.update(t, true); | |
8025 | if (o->extent_map.needs_reshard()) { | |
8026 | dout(20) << __func__ << " warning: still wants reshard, check options?" | |
8027 | << dendl; | |
8028 | o->extent_map.clear_needs_reshard(); | |
8029 | } | |
8030 | logger->inc(l_bluestore_onode_reshard); | |
8031 | } | |
8032 | ||
8033 | // bound encode | |
8034 | size_t bound = 0; | |
8035 | denc(o->onode, bound); | |
8036 | o->extent_map.bound_encode_spanning_blobs(bound); | |
8037 | if (o->onode.extent_map_shards.empty()) { | |
8038 | denc(o->extent_map.inline_bl, bound); | |
8039 | } | |
8040 | ||
8041 | // encode | |
8042 | bufferlist bl; | |
8043 | unsigned onode_part, blob_part, extent_part; | |
8044 | { | |
8045 | auto p = bl.get_contiguous_appender(bound, true); | |
8046 | denc(o->onode, p); | |
8047 | onode_part = p.get_logical_offset(); | |
8048 | o->extent_map.encode_spanning_blobs(p); | |
8049 | blob_part = p.get_logical_offset() - onode_part; | |
8050 | if (o->onode.extent_map_shards.empty()) { | |
8051 | denc(o->extent_map.inline_bl, p); | |
8052 | } | |
8053 | extent_part = p.get_logical_offset() - onode_part - blob_part; | |
8054 | } | |
8055 | ||
8056 | dout(20) << " onode " << o->oid << " is " << bl.length() | |
8057 | << " (" << onode_part << " bytes onode + " | |
8058 | << blob_part << " bytes spanning blobs + " | |
8059 | << extent_part << " bytes inline extents)" | |
8060 | << dendl; | |
8061 | t->set(PREFIX_OBJ, o->key.c_str(), o->key.size(), bl); | |
8062 | o->flushing_count++; | |
8063 | } | |
8064 | ||
8065 | // objects we modified but didn't affect the onode | |
8066 | auto p = txc->modified_objects.begin(); | |
8067 | while (p != txc->modified_objects.end()) { | |
8068 | if (txc->onodes.count(*p) == 0) { | |
8069 | (*p)->flushing_count++; | |
8070 | ++p; | |
8071 | } else { | |
8072 | // remove dups with onodes list to avoid problems in _txc_finish | |
8073 | p = txc->modified_objects.erase(p); | |
8074 | } | |
8075 | } | |
8076 | ||
8077 | // finalize shared_blobs | |
8078 | for (auto sb : txc->shared_blobs) { | |
8079 | string key; | |
8080 | auto sbid = sb->get_sbid(); | |
8081 | get_shared_blob_key(sbid, &key); | |
8082 | if (sb->persistent->empty()) { | |
8083 | dout(20) << " shared_blob 0x" << std::hex << sbid << std::dec | |
8084 | << " is empty" << dendl; | |
8085 | t->rmkey(PREFIX_SHARED_BLOB, key); | |
8086 | } else { | |
8087 | bufferlist bl; | |
8088 | ::encode(*(sb->persistent), bl); | |
8089 | dout(20) << " shared_blob 0x" << std::hex << sbid << std::dec | |
8090 | << " is " << bl.length() << " " << *sb << dendl; | |
8091 | t->set(PREFIX_SHARED_BLOB, key, bl); | |
8092 | } | |
8093 | } | |
8094 | } | |
8095 | ||
8096 | void BlueStore::BSPerfTracker::update_from_perfcounters( | |
8097 | PerfCounters &logger) | |
8098 | { | |
8099 | os_commit_latency.consume_next( | |
8100 | logger.get_tavg_ms( | |
8101 | l_bluestore_commit_lat)); | |
8102 | os_apply_latency.consume_next( | |
8103 | logger.get_tavg_ms( | |
8104 | l_bluestore_commit_lat)); | |
8105 | } | |
8106 | ||
8107 | void BlueStore::_txc_finalize_kv(TransContext *txc, KeyValueDB::Transaction t) | |
8108 | { | |
8109 | dout(20) << __func__ << " txc " << txc << std::hex | |
8110 | << " allocated 0x" << txc->allocated | |
8111 | << " released 0x" << txc->released | |
8112 | << std::dec << dendl; | |
8113 | ||
8114 | // We have to handle the case where we allocate *and* deallocate the | |
8115 | // same region in this transaction. The freelist doesn't like that. | |
8116 | // (Actually, the only thing that cares is the BitmapFreelistManager | |
8117 | // debug check. But that's important.) | |
8118 | interval_set<uint64_t> tmp_allocated, tmp_released; | |
8119 | interval_set<uint64_t> *pallocated = &txc->allocated; | |
8120 | interval_set<uint64_t> *preleased = &txc->released; | |
8121 | if (!txc->allocated.empty() && !txc->released.empty()) { | |
8122 | interval_set<uint64_t> overlap; | |
8123 | overlap.intersection_of(txc->allocated, txc->released); | |
8124 | if (!overlap.empty()) { | |
8125 | tmp_allocated = txc->allocated; | |
8126 | tmp_allocated.subtract(overlap); | |
8127 | tmp_released = txc->released; | |
8128 | tmp_released.subtract(overlap); | |
8129 | dout(20) << __func__ << " overlap 0x" << std::hex << overlap | |
8130 | << ", new allocated 0x" << tmp_allocated | |
8131 | << " released 0x" << tmp_released << std::dec | |
8132 | << dendl; | |
8133 | pallocated = &tmp_allocated; | |
8134 | preleased = &tmp_released; | |
8135 | } | |
8136 | } | |
8137 | ||
8138 | // update freelist with non-overlap sets | |
8139 | for (interval_set<uint64_t>::iterator p = pallocated->begin(); | |
8140 | p != pallocated->end(); | |
8141 | ++p) { | |
8142 | fm->allocate(p.get_start(), p.get_len(), t); | |
8143 | } | |
8144 | for (interval_set<uint64_t>::iterator p = preleased->begin(); | |
8145 | p != preleased->end(); | |
8146 | ++p) { | |
8147 | dout(20) << __func__ << " release 0x" << std::hex << p.get_start() | |
8148 | << "~" << p.get_len() << std::dec << dendl; | |
8149 | fm->release(p.get_start(), p.get_len(), t); | |
8150 | } | |
8151 | ||
8152 | _txc_update_store_statfs(txc); | |
8153 | } | |
8154 | ||
8155 | void BlueStore::_txc_applied_kv(TransContext *txc) | |
8156 | { | |
8157 | for (auto ls : { &txc->onodes, &txc->modified_objects }) { | |
8158 | for (auto& o : *ls) { | |
8159 | dout(20) << __func__ << " onode " << o << " had " << o->flushing_count | |
8160 | << dendl; | |
8161 | if (--o->flushing_count == 0) { | |
8162 | std::lock_guard<std::mutex> l(o->flush_lock); | |
8163 | o->flush_cond.notify_all(); | |
8164 | } | |
8165 | } | |
8166 | } | |
8167 | } | |
8168 | ||
8169 | void BlueStore::_txc_committed_kv(TransContext *txc) | |
8170 | { | |
8171 | dout(20) << __func__ << " txc " << txc << dendl; | |
8172 | ||
8173 | // warning: we're calling onreadable_sync inside the sequencer lock | |
8174 | if (txc->onreadable_sync) { | |
8175 | txc->onreadable_sync->complete(0); | |
8176 | txc->onreadable_sync = NULL; | |
8177 | } | |
8178 | unsigned n = txc->osr->parent->shard_hint.hash_to_shard(m_finisher_num); | |
8179 | if (txc->oncommit) { | |
8180 | logger->tinc(l_bluestore_commit_lat, ceph_clock_now() - txc->start); | |
8181 | finishers[n]->queue(txc->oncommit); | |
8182 | txc->oncommit = NULL; | |
8183 | } | |
8184 | if (txc->onreadable) { | |
8185 | finishers[n]->queue(txc->onreadable); | |
8186 | txc->onreadable = NULL; | |
8187 | } | |
8188 | ||
8189 | if (!txc->oncommits.empty()) { | |
8190 | finishers[n]->queue(txc->oncommits); | |
8191 | } | |
8192 | } | |
8193 | ||
8194 | void BlueStore::_txc_finish(TransContext *txc) | |
8195 | { | |
8196 | dout(20) << __func__ << " " << txc << " onodes " << txc->onodes << dendl; | |
8197 | assert(txc->state == TransContext::STATE_FINISHING); | |
8198 | ||
8199 | for (auto& sb : txc->shared_blobs_written) { | |
8200 | sb->bc.finish_write(sb->get_cache(), txc->seq); | |
8201 | } | |
8202 | txc->shared_blobs_written.clear(); | |
8203 | ||
8204 | while (!txc->removed_collections.empty()) { | |
8205 | _queue_reap_collection(txc->removed_collections.front()); | |
8206 | txc->removed_collections.pop_front(); | |
8207 | } | |
8208 | ||
8209 | OpSequencerRef osr = txc->osr; | |
8210 | bool empty = false; | |
8211 | bool submit_deferred = false; | |
8212 | OpSequencer::q_list_t releasing_txc; | |
8213 | { | |
8214 | std::lock_guard<std::mutex> l(osr->qlock); | |
8215 | txc->state = TransContext::STATE_DONE; | |
8216 | bool notify = false; | |
8217 | while (!osr->q.empty()) { | |
8218 | TransContext *txc = &osr->q.front(); | |
8219 | dout(20) << __func__ << " txc " << txc << " " << txc->get_state_name() | |
8220 | << dendl; | |
8221 | if (txc->state != TransContext::STATE_DONE) { | |
8222 | if (txc->state == TransContext::STATE_PREPARE && | |
8223 | deferred_aggressive) { | |
8224 | // for _osr_drain_preceding() | |
8225 | notify = true; | |
8226 | } | |
8227 | if (txc->state == TransContext::STATE_DEFERRED_QUEUED && | |
8228 | osr->q.size() > g_conf->bluestore_max_deferred_txc) { | |
8229 | submit_deferred = true; | |
8230 | } | |
8231 | break; | |
8232 | } | |
8233 | ||
8234 | osr->q.pop_front(); | |
8235 | releasing_txc.push_back(*txc); | |
8236 | notify = true; | |
8237 | } | |
8238 | if (notify) { | |
8239 | osr->qcond.notify_all(); | |
8240 | } | |
8241 | if (osr->q.empty()) { | |
8242 | dout(20) << __func__ << " osr " << osr << " q now empty" << dendl; | |
8243 | empty = true; | |
8244 | } | |
8245 | } | |
8246 | while (!releasing_txc.empty()) { | |
8247 | // release to allocator only after all preceding txc's have also | |
8248 | // finished any deferred writes that potentially land in these | |
8249 | // blocks | |
8250 | auto txc = &releasing_txc.front(); | |
8251 | _txc_release_alloc(txc); | |
8252 | releasing_txc.pop_front(); | |
8253 | txc->log_state_latency(logger, l_bluestore_state_done_lat); | |
8254 | delete txc; | |
8255 | } | |
8256 | ||
8257 | if (submit_deferred) { | |
8258 | // we're pinning memory; flush! we could be more fine-grained here but | |
8259 | // i'm not sure it's worth the bother. | |
8260 | deferred_try_submit(); | |
8261 | } | |
8262 | ||
8263 | if (empty && osr->zombie) { | |
8264 | dout(10) << __func__ << " reaping empty zombie osr " << osr << dendl; | |
8265 | osr->_unregister(); | |
8266 | } | |
8267 | } | |
8268 | ||
8269 | void BlueStore::_txc_release_alloc(TransContext *txc) | |
8270 | { | |
8271 | // update allocator with full released set | |
8272 | if (!cct->_conf->bluestore_debug_no_reuse_blocks) { | |
8273 | dout(10) << __func__ << " " << txc << " " << txc->released << dendl; | |
8274 | for (interval_set<uint64_t>::iterator p = txc->released.begin(); | |
8275 | p != txc->released.end(); | |
8276 | ++p) { | |
8277 | alloc->release(p.get_start(), p.get_len()); | |
8278 | } | |
8279 | } | |
8280 | ||
8281 | txc->allocated.clear(); | |
8282 | txc->released.clear(); | |
8283 | } | |
8284 | ||
8285 | void BlueStore::_osr_drain_preceding(TransContext *txc) | |
8286 | { | |
8287 | OpSequencer *osr = txc->osr.get(); | |
8288 | dout(10) << __func__ << " " << txc << " osr " << osr << dendl; | |
8289 | ++deferred_aggressive; // FIXME: maybe osr-local aggressive flag? | |
8290 | { | |
8291 | // submit anything pending | |
8292 | deferred_lock.lock(); | |
8293 | if (osr->deferred_pending) { | |
8294 | _deferred_submit_unlock(osr); | |
8295 | } else { | |
8296 | deferred_lock.unlock(); | |
8297 | } | |
8298 | } | |
8299 | { | |
8300 | // wake up any previously finished deferred events | |
8301 | std::lock_guard<std::mutex> l(kv_lock); | |
8302 | kv_cond.notify_one(); | |
8303 | } | |
8304 | osr->drain_preceding(txc); | |
8305 | --deferred_aggressive; | |
8306 | dout(10) << __func__ << " " << osr << " done" << dendl; | |
8307 | } | |
8308 | ||
8309 | void BlueStore::_osr_drain_all() | |
8310 | { | |
8311 | dout(10) << __func__ << dendl; | |
8312 | ||
8313 | set<OpSequencerRef> s; | |
8314 | { | |
8315 | std::lock_guard<std::mutex> l(osr_lock); | |
8316 | s = osr_set; | |
8317 | } | |
8318 | dout(20) << __func__ << " osr_set " << s << dendl; | |
8319 | ||
8320 | ++deferred_aggressive; | |
8321 | { | |
8322 | // submit anything pending | |
8323 | deferred_try_submit(); | |
8324 | } | |
8325 | { | |
8326 | // wake up any previously finished deferred events | |
8327 | std::lock_guard<std::mutex> l(kv_lock); | |
8328 | kv_cond.notify_one(); | |
8329 | } | |
8330 | { | |
8331 | std::lock_guard<std::mutex> l(kv_finalize_lock); | |
8332 | kv_finalize_cond.notify_one(); | |
8333 | } | |
8334 | for (auto osr : s) { | |
8335 | dout(20) << __func__ << " drain " << osr << dendl; | |
8336 | osr->drain(); | |
8337 | } | |
8338 | --deferred_aggressive; | |
8339 | ||
8340 | dout(10) << __func__ << " done" << dendl; | |
8341 | } | |
8342 | ||
8343 | void BlueStore::_osr_unregister_all() | |
8344 | { | |
8345 | set<OpSequencerRef> s; | |
8346 | { | |
8347 | std::lock_guard<std::mutex> l(osr_lock); | |
8348 | s = osr_set; | |
8349 | } | |
8350 | dout(10) << __func__ << " " << s << dendl; | |
8351 | for (auto osr : s) { | |
8352 | osr->_unregister(); | |
8353 | ||
8354 | if (!osr->zombie) { | |
8355 | // break link from Sequencer to us so that this OpSequencer | |
8356 | // instance can die with this mount/umount cycle. note that | |
8357 | // we assume umount() will not race against ~Sequencer. | |
8358 | assert(osr->parent); | |
8359 | osr->parent->p.reset(); | |
8360 | } | |
8361 | } | |
8362 | // nobody should be creating sequencers during umount either. | |
8363 | { | |
8364 | std::lock_guard<std::mutex> l(osr_lock); | |
8365 | assert(osr_set.empty()); | |
8366 | } | |
8367 | } | |
8368 | ||
8369 | void BlueStore::_kv_start() | |
8370 | { | |
8371 | dout(10) << __func__ << dendl; | |
8372 | ||
8373 | if (cct->_conf->bluestore_shard_finishers) { | |
8374 | if (cct->_conf->osd_op_num_shards) { | |
8375 | m_finisher_num = cct->_conf->osd_op_num_shards; | |
8376 | } else { | |
8377 | assert(bdev); | |
8378 | if (bdev->is_rotational()) { | |
8379 | m_finisher_num = cct->_conf->osd_op_num_shards_hdd; | |
8380 | } else { | |
8381 | m_finisher_num = cct->_conf->osd_op_num_shards_ssd; | |
8382 | } | |
8383 | } | |
8384 | } | |
8385 | ||
8386 | assert(m_finisher_num != 0); | |
8387 | ||
8388 | for (int i = 0; i < m_finisher_num; ++i) { | |
8389 | ostringstream oss; | |
8390 | oss << "finisher-" << i; | |
8391 | Finisher *f = new Finisher(cct, oss.str(), "finisher"); | |
8392 | finishers.push_back(f); | |
8393 | } | |
8394 | ||
8395 | deferred_finisher.start(); | |
8396 | for (auto f : finishers) { | |
8397 | f->start(); | |
8398 | } | |
8399 | kv_sync_thread.create("bstore_kv_sync"); | |
8400 | kv_finalize_thread.create("bstore_kv_final"); | |
8401 | } | |
8402 | ||
8403 | void BlueStore::_kv_stop() | |
8404 | { | |
8405 | dout(10) << __func__ << dendl; | |
8406 | { | |
8407 | std::unique_lock<std::mutex> l(kv_lock); | |
8408 | while (!kv_sync_started) { | |
8409 | kv_cond.wait(l); | |
8410 | } | |
8411 | kv_stop = true; | |
8412 | kv_cond.notify_all(); | |
8413 | } | |
8414 | { | |
8415 | std::unique_lock<std::mutex> l(kv_finalize_lock); | |
8416 | while (!kv_finalize_started) { | |
8417 | kv_finalize_cond.wait(l); | |
8418 | } | |
8419 | kv_finalize_stop = true; | |
8420 | kv_finalize_cond.notify_all(); | |
8421 | } | |
8422 | kv_sync_thread.join(); | |
8423 | kv_finalize_thread.join(); | |
8424 | { | |
8425 | std::lock_guard<std::mutex> l(kv_lock); | |
8426 | kv_stop = false; | |
8427 | } | |
8428 | { | |
8429 | std::lock_guard<std::mutex> l(kv_finalize_lock); | |
8430 | kv_finalize_stop = false; | |
8431 | } | |
8432 | dout(10) << __func__ << " stopping finishers" << dendl; | |
8433 | deferred_finisher.wait_for_empty(); | |
8434 | deferred_finisher.stop(); | |
8435 | for (auto f : finishers) { | |
8436 | f->wait_for_empty(); | |
8437 | f->stop(); | |
8438 | } | |
8439 | dout(10) << __func__ << " stopped" << dendl; | |
8440 | } | |
8441 | ||
8442 | void BlueStore::_kv_sync_thread() | |
8443 | { | |
8444 | dout(10) << __func__ << " start" << dendl; | |
8445 | std::unique_lock<std::mutex> l(kv_lock); | |
8446 | assert(!kv_sync_started); | |
8447 | kv_sync_started = true; | |
8448 | kv_cond.notify_all(); | |
8449 | while (true) { | |
8450 | assert(kv_committing.empty()); | |
8451 | if (kv_queue.empty() && | |
8452 | ((deferred_done_queue.empty() && deferred_stable_queue.empty()) || | |
8453 | !deferred_aggressive)) { | |
8454 | if (kv_stop) | |
8455 | break; | |
8456 | dout(20) << __func__ << " sleep" << dendl; | |
8457 | kv_cond.wait(l); | |
8458 | dout(20) << __func__ << " wake" << dendl; | |
8459 | } else { | |
8460 | deque<TransContext*> kv_submitting; | |
8461 | deque<DeferredBatch*> deferred_done, deferred_stable; | |
8462 | uint64_t aios = 0, costs = 0; | |
8463 | ||
8464 | dout(20) << __func__ << " committing " << kv_queue.size() | |
8465 | << " submitting " << kv_queue_unsubmitted.size() | |
8466 | << " deferred done " << deferred_done_queue.size() | |
8467 | << " stable " << deferred_stable_queue.size() | |
8468 | << dendl; | |
8469 | kv_committing.swap(kv_queue); | |
8470 | kv_submitting.swap(kv_queue_unsubmitted); | |
8471 | deferred_done.swap(deferred_done_queue); | |
8472 | deferred_stable.swap(deferred_stable_queue); | |
8473 | aios = kv_ios; | |
8474 | costs = kv_throttle_costs; | |
8475 | kv_ios = 0; | |
8476 | kv_throttle_costs = 0; | |
8477 | utime_t start = ceph_clock_now(); | |
8478 | l.unlock(); | |
8479 | ||
8480 | dout(30) << __func__ << " committing " << kv_committing << dendl; | |
8481 | dout(30) << __func__ << " submitting " << kv_submitting << dendl; | |
8482 | dout(30) << __func__ << " deferred_done " << deferred_done << dendl; | |
8483 | dout(30) << __func__ << " deferred_stable " << deferred_stable << dendl; | |
8484 | ||
8485 | bool force_flush = false; | |
8486 | // if bluefs is sharing the same device as data (only), then we | |
8487 | // can rely on the bluefs commit to flush the device and make | |
8488 | // deferred aios stable. that means that if we do have done deferred | |
8489 | // txcs AND we are not on a single device, we need to force a flush. | |
8490 | if (bluefs_single_shared_device && bluefs) { | |
8491 | if (aios) { | |
8492 | force_flush = true; | |
8493 | } else if (kv_committing.empty() && kv_submitting.empty() && | |
8494 | deferred_stable.empty()) { | |
8495 | force_flush = true; // there's nothing else to commit! | |
8496 | } else if (deferred_aggressive) { | |
8497 | force_flush = true; | |
8498 | } | |
8499 | } else | |
8500 | force_flush = true; | |
8501 | ||
8502 | if (force_flush) { | |
8503 | dout(20) << __func__ << " num_aios=" << aios | |
8504 | << " force_flush=" << (int)force_flush | |
8505 | << ", flushing, deferred done->stable" << dendl; | |
8506 | // flush/barrier on block device | |
8507 | bdev->flush(); | |
8508 | ||
8509 | // if we flush then deferred done are now deferred stable | |
8510 | deferred_stable.insert(deferred_stable.end(), deferred_done.begin(), | |
8511 | deferred_done.end()); | |
8512 | deferred_done.clear(); | |
8513 | } | |
8514 | utime_t after_flush = ceph_clock_now(); | |
8515 | ||
8516 | // we will use one final transaction to force a sync | |
8517 | KeyValueDB::Transaction synct = db->get_transaction(); | |
8518 | ||
8519 | // increase {nid,blobid}_max? note that this covers both the | |
8520 | // case where we are approaching the max and the case we passed | |
8521 | // it. in either case, we increase the max in the earlier txn | |
8522 | // we submit. | |
8523 | uint64_t new_nid_max = 0, new_blobid_max = 0; | |
8524 | if (nid_last + cct->_conf->bluestore_nid_prealloc/2 > nid_max) { | |
8525 | KeyValueDB::Transaction t = | |
8526 | kv_submitting.empty() ? synct : kv_submitting.front()->t; | |
8527 | new_nid_max = nid_last + cct->_conf->bluestore_nid_prealloc; | |
8528 | bufferlist bl; | |
8529 | ::encode(new_nid_max, bl); | |
8530 | t->set(PREFIX_SUPER, "nid_max", bl); | |
8531 | dout(10) << __func__ << " new_nid_max " << new_nid_max << dendl; | |
8532 | } | |
8533 | if (blobid_last + cct->_conf->bluestore_blobid_prealloc/2 > blobid_max) { | |
8534 | KeyValueDB::Transaction t = | |
8535 | kv_submitting.empty() ? synct : kv_submitting.front()->t; | |
8536 | new_blobid_max = blobid_last + cct->_conf->bluestore_blobid_prealloc; | |
8537 | bufferlist bl; | |
8538 | ::encode(new_blobid_max, bl); | |
8539 | t->set(PREFIX_SUPER, "blobid_max", bl); | |
8540 | dout(10) << __func__ << " new_blobid_max " << new_blobid_max << dendl; | |
8541 | } | |
8542 | ||
8543 | for (auto txc : kv_committing) { | |
8544 | if (txc->state == TransContext::STATE_KV_QUEUED) { | |
8545 | txc->log_state_latency(logger, l_bluestore_state_kv_queued_lat); | |
8546 | int r = cct->_conf->bluestore_debug_omit_kv_commit ? 0 : db->submit_transaction(txc->t); | |
8547 | assert(r == 0); | |
8548 | _txc_applied_kv(txc); | |
8549 | --txc->osr->kv_committing_serially; | |
8550 | txc->state = TransContext::STATE_KV_SUBMITTED; | |
8551 | if (txc->osr->kv_submitted_waiters) { | |
8552 | std::lock_guard<std::mutex> l(txc->osr->qlock); | |
8553 | if (txc->osr->_is_all_kv_submitted()) { | |
8554 | txc->osr->qcond.notify_all(); | |
8555 | } | |
8556 | } | |
8557 | ||
8558 | } else { | |
8559 | assert(txc->state == TransContext::STATE_KV_SUBMITTED); | |
8560 | txc->log_state_latency(logger, l_bluestore_state_kv_queued_lat); | |
8561 | } | |
8562 | if (txc->had_ios) { | |
8563 | --txc->osr->txc_with_unstable_io; | |
8564 | } | |
8565 | } | |
8566 | ||
8567 | // release throttle *before* we commit. this allows new ops | |
8568 | // to be prepared and enter pipeline while we are waiting on | |
8569 | // the kv commit sync/flush. then hopefully on the next | |
8570 | // iteration there will already be ops awake. otherwise, we | |
8571 | // end up going to sleep, and then wake up when the very first | |
8572 | // transaction is ready for commit. | |
8573 | throttle_bytes.put(costs); | |
8574 | ||
8575 | PExtentVector bluefs_gift_extents; | |
8576 | if (bluefs && | |
8577 | after_flush - bluefs_last_balance > | |
8578 | cct->_conf->bluestore_bluefs_balance_interval) { | |
8579 | bluefs_last_balance = after_flush; | |
8580 | int r = _balance_bluefs_freespace(&bluefs_gift_extents); | |
8581 | assert(r >= 0); | |
8582 | if (r > 0) { | |
8583 | for (auto& p : bluefs_gift_extents) { | |
8584 | bluefs_extents.insert(p.offset, p.length); | |
8585 | } | |
8586 | bufferlist bl; | |
8587 | ::encode(bluefs_extents, bl); | |
8588 | dout(10) << __func__ << " bluefs_extents now 0x" << std::hex | |
8589 | << bluefs_extents << std::dec << dendl; | |
8590 | synct->set(PREFIX_SUPER, "bluefs_extents", bl); | |
8591 | } | |
8592 | } | |
8593 | ||
8594 | // cleanup sync deferred keys | |
8595 | for (auto b : deferred_stable) { | |
8596 | for (auto& txc : b->txcs) { | |
8597 | bluestore_deferred_transaction_t& wt = *txc.deferred_txn; | |
8598 | if (!wt.released.empty()) { | |
8599 | // kraken replay compat only | |
8600 | txc.released = wt.released; | |
8601 | dout(10) << __func__ << " deferred txn has released " | |
8602 | << txc.released | |
8603 | << " (we just upgraded from kraken) on " << &txc << dendl; | |
8604 | _txc_finalize_kv(&txc, synct); | |
8605 | } | |
8606 | // cleanup the deferred | |
8607 | string key; | |
8608 | get_deferred_key(wt.seq, &key); | |
8609 | synct->rm_single_key(PREFIX_DEFERRED, key); | |
8610 | } | |
8611 | } | |
8612 | ||
8613 | // submit synct synchronously (block and wait for it to commit) | |
8614 | int r = cct->_conf->bluestore_debug_omit_kv_commit ? 0 : db->submit_transaction_sync(synct); | |
8615 | assert(r == 0); | |
8616 | ||
8617 | if (new_nid_max) { | |
8618 | nid_max = new_nid_max; | |
8619 | dout(10) << __func__ << " nid_max now " << nid_max << dendl; | |
8620 | } | |
8621 | if (new_blobid_max) { | |
8622 | blobid_max = new_blobid_max; | |
8623 | dout(10) << __func__ << " blobid_max now " << blobid_max << dendl; | |
8624 | } | |
8625 | ||
8626 | { | |
8627 | utime_t finish = ceph_clock_now(); | |
8628 | utime_t dur_flush = after_flush - start; | |
8629 | utime_t dur_kv = finish - after_flush; | |
8630 | utime_t dur = finish - start; | |
8631 | dout(20) << __func__ << " committed " << kv_committing.size() | |
8632 | << " cleaned " << deferred_stable.size() | |
8633 | << " in " << dur | |
8634 | << " (" << dur_flush << " flush + " << dur_kv << " kv commit)" | |
8635 | << dendl; | |
8636 | logger->tinc(l_bluestore_kv_flush_lat, dur_flush); | |
8637 | logger->tinc(l_bluestore_kv_commit_lat, dur_kv); | |
8638 | logger->tinc(l_bluestore_kv_lat, dur); | |
8639 | } | |
8640 | ||
8641 | if (bluefs) { | |
8642 | if (!bluefs_gift_extents.empty()) { | |
8643 | _commit_bluefs_freespace(bluefs_gift_extents); | |
8644 | } | |
8645 | for (auto p = bluefs_extents_reclaiming.begin(); | |
8646 | p != bluefs_extents_reclaiming.end(); | |
8647 | ++p) { | |
8648 | dout(20) << __func__ << " releasing old bluefs 0x" << std::hex | |
8649 | << p.get_start() << "~" << p.get_len() << std::dec | |
8650 | << dendl; | |
8651 | alloc->release(p.get_start(), p.get_len()); | |
8652 | } | |
8653 | bluefs_extents_reclaiming.clear(); | |
8654 | } | |
8655 | ||
8656 | { | |
8657 | std::unique_lock<std::mutex> m(kv_finalize_lock); | |
8658 | if (kv_committing_to_finalize.empty()) { | |
8659 | kv_committing_to_finalize.swap(kv_committing); | |
8660 | } else { | |
8661 | kv_committing_to_finalize.insert( | |
8662 | kv_committing_to_finalize.end(), | |
8663 | kv_committing.begin(), | |
8664 | kv_committing.end()); | |
8665 | kv_committing.clear(); | |
8666 | } | |
8667 | if (deferred_stable_to_finalize.empty()) { | |
8668 | deferred_stable_to_finalize.swap(deferred_stable); | |
8669 | } else { | |
8670 | deferred_stable_to_finalize.insert( | |
8671 | deferred_stable_to_finalize.end(), | |
8672 | deferred_stable.begin(), | |
8673 | deferred_stable.end()); | |
8674 | deferred_stable.clear(); | |
8675 | } | |
8676 | kv_finalize_cond.notify_one(); | |
8677 | } | |
8678 | ||
8679 | l.lock(); | |
8680 | // previously deferred "done" are now "stable" by virtue of this | |
8681 | // commit cycle. | |
8682 | deferred_stable_queue.swap(deferred_done); | |
8683 | } | |
8684 | } | |
8685 | dout(10) << __func__ << " finish" << dendl; | |
8686 | kv_sync_started = false; | |
8687 | } | |
8688 | ||
8689 | void BlueStore::_kv_finalize_thread() | |
8690 | { | |
8691 | deque<TransContext*> kv_committed; | |
8692 | deque<DeferredBatch*> deferred_stable; | |
8693 | dout(10) << __func__ << " start" << dendl; | |
8694 | std::unique_lock<std::mutex> l(kv_finalize_lock); | |
8695 | assert(!kv_finalize_started); | |
8696 | kv_finalize_started = true; | |
8697 | kv_finalize_cond.notify_all(); | |
8698 | while (true) { | |
8699 | assert(kv_committed.empty()); | |
8700 | assert(deferred_stable.empty()); | |
8701 | if (kv_committing_to_finalize.empty() && | |
8702 | deferred_stable_to_finalize.empty()) { | |
8703 | if (kv_finalize_stop) | |
8704 | break; | |
8705 | dout(20) << __func__ << " sleep" << dendl; | |
8706 | kv_finalize_cond.wait(l); | |
8707 | dout(20) << __func__ << " wake" << dendl; | |
8708 | } else { | |
8709 | kv_committed.swap(kv_committing_to_finalize); | |
8710 | deferred_stable.swap(deferred_stable_to_finalize); | |
8711 | l.unlock(); | |
8712 | dout(20) << __func__ << " kv_committed " << kv_committed << dendl; | |
8713 | dout(20) << __func__ << " deferred_stable " << deferred_stable << dendl; | |
8714 | ||
8715 | while (!kv_committed.empty()) { | |
8716 | TransContext *txc = kv_committed.front(); | |
8717 | assert(txc->state == TransContext::STATE_KV_SUBMITTED); | |
8718 | _txc_state_proc(txc); | |
8719 | kv_committed.pop_front(); | |
8720 | } | |
8721 | ||
8722 | for (auto b : deferred_stable) { | |
8723 | auto p = b->txcs.begin(); | |
8724 | while (p != b->txcs.end()) { | |
8725 | TransContext *txc = &*p; | |
8726 | p = b->txcs.erase(p); // unlink here because | |
8727 | _txc_state_proc(txc); // this may destroy txc | |
8728 | } | |
8729 | delete b; | |
8730 | } | |
8731 | deferred_stable.clear(); | |
8732 | ||
8733 | if (!deferred_aggressive) { | |
8734 | if (deferred_queue_size >= deferred_batch_ops.load() || | |
8735 | throttle_deferred_bytes.past_midpoint()) { | |
8736 | deferred_try_submit(); | |
8737 | } | |
8738 | } | |
8739 | ||
8740 | // this is as good a place as any ... | |
8741 | _reap_collections(); | |
8742 | ||
8743 | l.lock(); | |
8744 | } | |
8745 | } | |
8746 | dout(10) << __func__ << " finish" << dendl; | |
8747 | kv_finalize_started = false; | |
8748 | } | |
8749 | ||
8750 | bluestore_deferred_op_t *BlueStore::_get_deferred_op( | |
8751 | TransContext *txc, OnodeRef o) | |
8752 | { | |
8753 | if (!txc->deferred_txn) { | |
8754 | txc->deferred_txn = new bluestore_deferred_transaction_t; | |
8755 | } | |
8756 | txc->deferred_txn->ops.push_back(bluestore_deferred_op_t()); | |
8757 | return &txc->deferred_txn->ops.back(); | |
8758 | } | |
8759 | ||
8760 | void BlueStore::_deferred_queue(TransContext *txc) | |
8761 | { | |
8762 | dout(20) << __func__ << " txc " << txc << " osr " << txc->osr << dendl; | |
8763 | deferred_lock.lock(); | |
8764 | if (!txc->osr->deferred_pending && | |
8765 | !txc->osr->deferred_running) { | |
8766 | deferred_queue.push_back(*txc->osr); | |
8767 | } | |
8768 | if (!txc->osr->deferred_pending) { | |
8769 | txc->osr->deferred_pending = new DeferredBatch(cct, txc->osr.get()); | |
8770 | } | |
8771 | ++deferred_queue_size; | |
8772 | txc->osr->deferred_pending->txcs.push_back(*txc); | |
8773 | bluestore_deferred_transaction_t& wt = *txc->deferred_txn; | |
8774 | for (auto opi = wt.ops.begin(); opi != wt.ops.end(); ++opi) { | |
8775 | const auto& op = *opi; | |
8776 | assert(op.op == bluestore_deferred_op_t::OP_WRITE); | |
8777 | bufferlist::const_iterator p = op.data.begin(); | |
8778 | for (auto e : op.extents) { | |
8779 | txc->osr->deferred_pending->prepare_write( | |
8780 | cct, wt.seq, e.offset, e.length, p); | |
8781 | } | |
8782 | } | |
8783 | if (deferred_aggressive && | |
8784 | !txc->osr->deferred_running) { | |
8785 | _deferred_submit_unlock(txc->osr.get()); | |
8786 | } else { | |
8787 | deferred_lock.unlock(); | |
8788 | } | |
8789 | } | |
8790 | ||
8791 | void BlueStore::deferred_try_submit() | |
8792 | { | |
8793 | dout(20) << __func__ << " " << deferred_queue.size() << " osrs, " | |
8794 | << deferred_queue_size << " txcs" << dendl; | |
8795 | std::lock_guard<std::mutex> l(deferred_lock); | |
8796 | vector<OpSequencerRef> osrs; | |
8797 | osrs.reserve(deferred_queue.size()); | |
8798 | for (auto& osr : deferred_queue) { | |
8799 | osrs.push_back(&osr); | |
8800 | } | |
8801 | for (auto& osr : osrs) { | |
8802 | if (osr->deferred_pending) { | |
8803 | if (!osr->deferred_running) { | |
8804 | _deferred_submit_unlock(osr.get()); | |
8805 | deferred_lock.lock(); | |
8806 | } else { | |
8807 | dout(20) << __func__ << " osr " << osr << " already has running" | |
8808 | << dendl; | |
8809 | } | |
8810 | } else { | |
8811 | dout(20) << __func__ << " osr " << osr << " has no pending" << dendl; | |
8812 | } | |
8813 | } | |
8814 | } | |
8815 | ||
8816 | void BlueStore::_deferred_submit_unlock(OpSequencer *osr) | |
8817 | { | |
8818 | dout(10) << __func__ << " osr " << osr | |
8819 | << " " << osr->deferred_pending->iomap.size() << " ios pending " | |
8820 | << dendl; | |
8821 | assert(osr->deferred_pending); | |
8822 | assert(!osr->deferred_running); | |
8823 | ||
8824 | auto b = osr->deferred_pending; | |
8825 | deferred_queue_size -= b->seq_bytes.size(); | |
8826 | assert(deferred_queue_size >= 0); | |
8827 | ||
8828 | osr->deferred_running = osr->deferred_pending; | |
8829 | osr->deferred_pending = nullptr; | |
8830 | ||
8831 | uint64_t start = 0, pos = 0; | |
8832 | bufferlist bl; | |
8833 | auto i = b->iomap.begin(); | |
8834 | while (true) { | |
8835 | if (i == b->iomap.end() || i->first != pos) { | |
8836 | if (bl.length()) { | |
8837 | dout(20) << __func__ << " write 0x" << std::hex | |
8838 | << start << "~" << bl.length() | |
8839 | << " crc " << bl.crc32c(-1) << std::dec << dendl; | |
8840 | if (!g_conf->bluestore_debug_omit_block_device_write) { | |
8841 | logger->inc(l_bluestore_deferred_write_ops); | |
8842 | logger->inc(l_bluestore_deferred_write_bytes, bl.length()); | |
8843 | int r = bdev->aio_write(start, bl, &b->ioc, false); | |
8844 | assert(r == 0); | |
8845 | } | |
8846 | } | |
8847 | if (i == b->iomap.end()) { | |
8848 | break; | |
8849 | } | |
8850 | start = 0; | |
8851 | pos = i->first; | |
8852 | bl.clear(); | |
8853 | } | |
8854 | dout(20) << __func__ << " seq " << i->second.seq << " 0x" | |
8855 | << std::hex << pos << "~" << i->second.bl.length() << std::dec | |
8856 | << dendl; | |
8857 | if (!bl.length()) { | |
8858 | start = pos; | |
8859 | } | |
8860 | pos += i->second.bl.length(); | |
8861 | bl.claim_append(i->second.bl); | |
8862 | ++i; | |
8863 | } | |
8864 | ||
8865 | deferred_lock.unlock(); | |
8866 | bdev->aio_submit(&b->ioc); | |
8867 | } | |
8868 | ||
8869 | struct C_DeferredTrySubmit : public Context { | |
8870 | BlueStore *store; | |
8871 | C_DeferredTrySubmit(BlueStore *s) : store(s) {} | |
8872 | void finish(int r) { | |
8873 | store->deferred_try_submit(); | |
8874 | } | |
8875 | }; | |
8876 | ||
8877 | void BlueStore::_deferred_aio_finish(OpSequencer *osr) | |
8878 | { | |
8879 | dout(10) << __func__ << " osr " << osr << dendl; | |
8880 | assert(osr->deferred_running); | |
8881 | DeferredBatch *b = osr->deferred_running; | |
8882 | ||
8883 | { | |
8884 | std::lock_guard<std::mutex> l(deferred_lock); | |
8885 | assert(osr->deferred_running == b); | |
8886 | osr->deferred_running = nullptr; | |
8887 | if (!osr->deferred_pending) { | |
8888 | dout(20) << __func__ << " dequeueing" << dendl; | |
8889 | auto q = deferred_queue.iterator_to(*osr); | |
8890 | deferred_queue.erase(q); | |
8891 | } else if (deferred_aggressive) { | |
8892 | dout(20) << __func__ << " queuing async deferred_try_submit" << dendl; | |
8893 | deferred_finisher.queue(new C_DeferredTrySubmit(this)); | |
8894 | } else { | |
8895 | dout(20) << __func__ << " leaving queued, more pending" << dendl; | |
8896 | } | |
8897 | } | |
8898 | ||
8899 | { | |
8900 | uint64_t costs = 0; | |
8901 | std::lock_guard<std::mutex> l2(osr->qlock); | |
8902 | for (auto& i : b->txcs) { | |
8903 | TransContext *txc = &i; | |
8904 | txc->state = TransContext::STATE_DEFERRED_CLEANUP; | |
8905 | costs += txc->cost; | |
8906 | } | |
8907 | osr->qcond.notify_all(); | |
8908 | throttle_deferred_bytes.put(costs); | |
8909 | std::lock_guard<std::mutex> l(kv_lock); | |
8910 | deferred_done_queue.emplace_back(b); | |
8911 | } | |
8912 | ||
8913 | // in the normal case, do not bother waking up the kv thread; it will | |
8914 | // catch us on the next commit anyway. | |
8915 | if (deferred_aggressive) { | |
8916 | std::lock_guard<std::mutex> l(kv_lock); | |
8917 | kv_cond.notify_one(); | |
8918 | } | |
8919 | } | |
8920 | ||
8921 | int BlueStore::_deferred_replay() | |
8922 | { | |
8923 | dout(10) << __func__ << " start" << dendl; | |
8924 | OpSequencerRef osr = new OpSequencer(cct, this); | |
8925 | int count = 0; | |
8926 | int r = 0; | |
8927 | KeyValueDB::Iterator it = db->get_iterator(PREFIX_DEFERRED); | |
8928 | for (it->lower_bound(string()); it->valid(); it->next(), ++count) { | |
8929 | dout(20) << __func__ << " replay " << pretty_binary_string(it->key()) | |
8930 | << dendl; | |
8931 | bluestore_deferred_transaction_t *deferred_txn = | |
8932 | new bluestore_deferred_transaction_t; | |
8933 | bufferlist bl = it->value(); | |
8934 | bufferlist::iterator p = bl.begin(); | |
8935 | try { | |
8936 | ::decode(*deferred_txn, p); | |
8937 | } catch (buffer::error& e) { | |
8938 | derr << __func__ << " failed to decode deferred txn " | |
8939 | << pretty_binary_string(it->key()) << dendl; | |
8940 | delete deferred_txn; | |
8941 | r = -EIO; | |
8942 | goto out; | |
8943 | } | |
8944 | TransContext *txc = _txc_create(osr.get()); | |
8945 | txc->deferred_txn = deferred_txn; | |
8946 | txc->state = TransContext::STATE_KV_DONE; | |
8947 | _txc_state_proc(txc); | |
8948 | } | |
8949 | out: | |
8950 | dout(20) << __func__ << " draining osr" << dendl; | |
8951 | _osr_drain_all(); | |
8952 | osr->discard(); | |
8953 | dout(10) << __func__ << " completed " << count << " events" << dendl; | |
8954 | return r; | |
8955 | } | |
8956 | ||
8957 | // --------------------------- | |
8958 | // transactions | |
8959 | ||
8960 | int BlueStore::queue_transactions( | |
8961 | Sequencer *posr, | |
8962 | vector<Transaction>& tls, | |
8963 | TrackedOpRef op, | |
8964 | ThreadPool::TPHandle *handle) | |
8965 | { | |
8966 | FUNCTRACE(); | |
8967 | Context *onreadable; | |
8968 | Context *ondisk; | |
8969 | Context *onreadable_sync; | |
8970 | ObjectStore::Transaction::collect_contexts( | |
8971 | tls, &onreadable, &ondisk, &onreadable_sync); | |
8972 | ||
8973 | if (cct->_conf->objectstore_blackhole) { | |
8974 | dout(0) << __func__ << " objectstore_blackhole = TRUE, dropping transaction" | |
8975 | << dendl; | |
8976 | delete ondisk; | |
8977 | delete onreadable; | |
8978 | delete onreadable_sync; | |
8979 | return 0; | |
8980 | } | |
8981 | utime_t start = ceph_clock_now(); | |
8982 | // set up the sequencer | |
8983 | OpSequencer *osr; | |
8984 | assert(posr); | |
8985 | if (posr->p) { | |
8986 | osr = static_cast<OpSequencer *>(posr->p.get()); | |
8987 | dout(10) << __func__ << " existing " << osr << " " << *osr << dendl; | |
8988 | } else { | |
8989 | osr = new OpSequencer(cct, this); | |
8990 | osr->parent = posr; | |
8991 | posr->p = osr; | |
8992 | dout(10) << __func__ << " new " << osr << " " << *osr << dendl; | |
8993 | } | |
8994 | ||
8995 | // prepare | |
8996 | TransContext *txc = _txc_create(osr); | |
8997 | txc->onreadable = onreadable; | |
8998 | txc->onreadable_sync = onreadable_sync; | |
8999 | txc->oncommit = ondisk; | |
9000 | ||
9001 | for (vector<Transaction>::iterator p = tls.begin(); p != tls.end(); ++p) { | |
9002 | (*p).set_osr(osr); | |
9003 | txc->bytes += (*p).get_num_bytes(); | |
9004 | _txc_add_transaction(txc, &(*p)); | |
9005 | } | |
9006 | _txc_calc_cost(txc); | |
9007 | ||
9008 | _txc_write_nodes(txc, txc->t); | |
9009 | ||
9010 | // journal deferred items | |
9011 | if (txc->deferred_txn) { | |
9012 | txc->deferred_txn->seq = ++deferred_seq; | |
9013 | bufferlist bl; | |
9014 | ::encode(*txc->deferred_txn, bl); | |
9015 | string key; | |
9016 | get_deferred_key(txc->deferred_txn->seq, &key); | |
9017 | txc->t->set(PREFIX_DEFERRED, key, bl); | |
9018 | } | |
9019 | ||
9020 | _txc_finalize_kv(txc, txc->t); | |
9021 | if (handle) | |
9022 | handle->suspend_tp_timeout(); | |
9023 | ||
9024 | utime_t tstart = ceph_clock_now(); | |
9025 | throttle_bytes.get(txc->cost); | |
9026 | if (txc->deferred_txn) { | |
9027 | // ensure we do not block here because of deferred writes | |
9028 | if (!throttle_deferred_bytes.get_or_fail(txc->cost)) { | |
9029 | dout(10) << __func__ << " failed get throttle_deferred_bytes, aggressive" | |
9030 | << dendl; | |
9031 | ++deferred_aggressive; | |
9032 | deferred_try_submit(); | |
9033 | { | |
9034 | // wake up any previously finished deferred events | |
9035 | std::lock_guard<std::mutex> l(kv_lock); | |
9036 | kv_cond.notify_one(); | |
9037 | } | |
9038 | throttle_deferred_bytes.get(txc->cost); | |
9039 | --deferred_aggressive; | |
9040 | } | |
9041 | } | |
9042 | utime_t tend = ceph_clock_now(); | |
9043 | ||
9044 | if (handle) | |
9045 | handle->reset_tp_timeout(); | |
9046 | ||
9047 | logger->inc(l_bluestore_txc); | |
9048 | ||
9049 | // execute (start) | |
9050 | _txc_state_proc(txc); | |
9051 | ||
9052 | logger->tinc(l_bluestore_submit_lat, ceph_clock_now() - start); | |
9053 | logger->tinc(l_bluestore_throttle_lat, tend - tstart); | |
9054 | return 0; | |
9055 | } | |
9056 | ||
9057 | void BlueStore::_txc_aio_submit(TransContext *txc) | |
9058 | { | |
9059 | dout(10) << __func__ << " txc " << txc << dendl; | |
9060 | bdev->aio_submit(&txc->ioc); | |
9061 | } | |
9062 | ||
9063 | void BlueStore::_txc_add_transaction(TransContext *txc, Transaction *t) | |
9064 | { | |
9065 | Transaction::iterator i = t->begin(); | |
9066 | ||
9067 | _dump_transaction(t); | |
9068 | ||
9069 | vector<CollectionRef> cvec(i.colls.size()); | |
9070 | unsigned j = 0; | |
9071 | for (vector<coll_t>::iterator p = i.colls.begin(); p != i.colls.end(); | |
9072 | ++p, ++j) { | |
9073 | cvec[j] = _get_collection(*p); | |
9074 | } | |
9075 | vector<OnodeRef> ovec(i.objects.size()); | |
9076 | ||
9077 | for (int pos = 0; i.have_op(); ++pos) { | |
9078 | Transaction::Op *op = i.decode_op(); | |
9079 | int r = 0; | |
9080 | ||
9081 | // no coll or obj | |
9082 | if (op->op == Transaction::OP_NOP) | |
9083 | continue; | |
9084 | ||
9085 | // collection operations | |
9086 | CollectionRef &c = cvec[op->cid]; | |
9087 | switch (op->op) { | |
9088 | case Transaction::OP_RMCOLL: | |
9089 | { | |
9090 | const coll_t &cid = i.get_cid(op->cid); | |
9091 | r = _remove_collection(txc, cid, &c); | |
9092 | if (!r) | |
9093 | continue; | |
9094 | } | |
9095 | break; | |
9096 | ||
9097 | case Transaction::OP_MKCOLL: | |
9098 | { | |
9099 | assert(!c); | |
9100 | const coll_t &cid = i.get_cid(op->cid); | |
9101 | r = _create_collection(txc, cid, op->split_bits, &c); | |
9102 | if (!r) | |
9103 | continue; | |
9104 | } | |
9105 | break; | |
9106 | ||
9107 | case Transaction::OP_SPLIT_COLLECTION: | |
9108 | assert(0 == "deprecated"); | |
9109 | break; | |
9110 | ||
9111 | case Transaction::OP_SPLIT_COLLECTION2: | |
9112 | { | |
9113 | uint32_t bits = op->split_bits; | |
9114 | uint32_t rem = op->split_rem; | |
9115 | r = _split_collection(txc, c, cvec[op->dest_cid], bits, rem); | |
9116 | if (!r) | |
9117 | continue; | |
9118 | } | |
9119 | break; | |
9120 | ||
9121 | case Transaction::OP_COLL_HINT: | |
9122 | { | |
9123 | uint32_t type = op->hint_type; | |
9124 | bufferlist hint; | |
9125 | i.decode_bl(hint); | |
9126 | bufferlist::iterator hiter = hint.begin(); | |
9127 | if (type == Transaction::COLL_HINT_EXPECTED_NUM_OBJECTS) { | |
9128 | uint32_t pg_num; | |
9129 | uint64_t num_objs; | |
9130 | ::decode(pg_num, hiter); | |
9131 | ::decode(num_objs, hiter); | |
9132 | dout(10) << __func__ << " collection hint objects is a no-op, " | |
9133 | << " pg_num " << pg_num << " num_objects " << num_objs | |
9134 | << dendl; | |
9135 | } else { | |
9136 | // Ignore the hint | |
9137 | dout(10) << __func__ << " unknown collection hint " << type << dendl; | |
9138 | } | |
9139 | continue; | |
9140 | } | |
9141 | break; | |
9142 | ||
9143 | case Transaction::OP_COLL_SETATTR: | |
9144 | r = -EOPNOTSUPP; | |
9145 | break; | |
9146 | ||
9147 | case Transaction::OP_COLL_RMATTR: | |
9148 | r = -EOPNOTSUPP; | |
9149 | break; | |
9150 | ||
9151 | case Transaction::OP_COLL_RENAME: | |
9152 | assert(0 == "not implemented"); | |
9153 | break; | |
9154 | } | |
9155 | if (r < 0) { | |
9156 | derr << __func__ << " error " << cpp_strerror(r) | |
9157 | << " not handled on operation " << op->op | |
9158 | << " (op " << pos << ", counting from 0)" << dendl; | |
9159 | _dump_transaction(t, 0); | |
9160 | assert(0 == "unexpected error"); | |
9161 | } | |
9162 | ||
9163 | // these operations implicity create the object | |
9164 | bool create = false; | |
9165 | if (op->op == Transaction::OP_TOUCH || | |
9166 | op->op == Transaction::OP_WRITE || | |
9167 | op->op == Transaction::OP_ZERO) { | |
9168 | create = true; | |
9169 | } | |
9170 | ||
9171 | // object operations | |
9172 | RWLock::WLocker l(c->lock); | |
9173 | OnodeRef &o = ovec[op->oid]; | |
9174 | if (!o) { | |
9175 | ghobject_t oid = i.get_oid(op->oid); | |
9176 | o = c->get_onode(oid, create); | |
9177 | } | |
9178 | if (!create && (!o || !o->exists)) { | |
9179 | dout(10) << __func__ << " op " << op->op << " got ENOENT on " | |
9180 | << i.get_oid(op->oid) << dendl; | |
9181 | r = -ENOENT; | |
9182 | goto endop; | |
9183 | } | |
9184 | ||
9185 | switch (op->op) { | |
9186 | case Transaction::OP_TOUCH: | |
9187 | r = _touch(txc, c, o); | |
9188 | break; | |
9189 | ||
9190 | case Transaction::OP_WRITE: | |
9191 | { | |
9192 | uint64_t off = op->off; | |
9193 | uint64_t len = op->len; | |
9194 | uint32_t fadvise_flags = i.get_fadvise_flags(); | |
9195 | bufferlist bl; | |
9196 | i.decode_bl(bl); | |
9197 | r = _write(txc, c, o, off, len, bl, fadvise_flags); | |
9198 | } | |
9199 | break; | |
9200 | ||
9201 | case Transaction::OP_ZERO: | |
9202 | { | |
9203 | uint64_t off = op->off; | |
9204 | uint64_t len = op->len; | |
9205 | r = _zero(txc, c, o, off, len); | |
9206 | } | |
9207 | break; | |
9208 | ||
9209 | case Transaction::OP_TRIMCACHE: | |
9210 | { | |
9211 | // deprecated, no-op | |
9212 | } | |
9213 | break; | |
9214 | ||
9215 | case Transaction::OP_TRUNCATE: | |
9216 | { | |
9217 | uint64_t off = op->off; | |
9218 | r = _truncate(txc, c, o, off); | |
9219 | } | |
9220 | break; | |
9221 | ||
9222 | case Transaction::OP_REMOVE: | |
9223 | { | |
9224 | r = _remove(txc, c, o); | |
9225 | } | |
9226 | break; | |
9227 | ||
9228 | case Transaction::OP_SETATTR: | |
9229 | { | |
9230 | string name = i.decode_string(); | |
9231 | bufferptr bp; | |
9232 | i.decode_bp(bp); | |
9233 | r = _setattr(txc, c, o, name, bp); | |
9234 | } | |
9235 | break; | |
9236 | ||
9237 | case Transaction::OP_SETATTRS: | |
9238 | { | |
9239 | map<string, bufferptr> aset; | |
9240 | i.decode_attrset(aset); | |
9241 | r = _setattrs(txc, c, o, aset); | |
9242 | } | |
9243 | break; | |
9244 | ||
9245 | case Transaction::OP_RMATTR: | |
9246 | { | |
9247 | string name = i.decode_string(); | |
9248 | r = _rmattr(txc, c, o, name); | |
9249 | } | |
9250 | break; | |
9251 | ||
9252 | case Transaction::OP_RMATTRS: | |
9253 | { | |
9254 | r = _rmattrs(txc, c, o); | |
9255 | } | |
9256 | break; | |
9257 | ||
9258 | case Transaction::OP_CLONE: | |
9259 | { | |
9260 | OnodeRef& no = ovec[op->dest_oid]; | |
9261 | if (!no) { | |
9262 | const ghobject_t& noid = i.get_oid(op->dest_oid); | |
9263 | no = c->get_onode(noid, true); | |
9264 | } | |
9265 | r = _clone(txc, c, o, no); | |
9266 | } | |
9267 | break; | |
9268 | ||
9269 | case Transaction::OP_CLONERANGE: | |
9270 | assert(0 == "deprecated"); | |
9271 | break; | |
9272 | ||
9273 | case Transaction::OP_CLONERANGE2: | |
9274 | { | |
9275 | OnodeRef& no = ovec[op->dest_oid]; | |
9276 | if (!no) { | |
9277 | const ghobject_t& noid = i.get_oid(op->dest_oid); | |
9278 | no = c->get_onode(noid, true); | |
9279 | } | |
9280 | uint64_t srcoff = op->off; | |
9281 | uint64_t len = op->len; | |
9282 | uint64_t dstoff = op->dest_off; | |
9283 | r = _clone_range(txc, c, o, no, srcoff, len, dstoff); | |
9284 | } | |
9285 | break; | |
9286 | ||
9287 | case Transaction::OP_COLL_ADD: | |
9288 | assert(0 == "not implemented"); | |
9289 | break; | |
9290 | ||
9291 | case Transaction::OP_COLL_REMOVE: | |
9292 | assert(0 == "not implemented"); | |
9293 | break; | |
9294 | ||
9295 | case Transaction::OP_COLL_MOVE: | |
9296 | assert(0 == "deprecated"); | |
9297 | break; | |
9298 | ||
9299 | case Transaction::OP_COLL_MOVE_RENAME: | |
9300 | case Transaction::OP_TRY_RENAME: | |
9301 | { | |
9302 | assert(op->cid == op->dest_cid); | |
9303 | const ghobject_t& noid = i.get_oid(op->dest_oid); | |
9304 | OnodeRef& no = ovec[op->dest_oid]; | |
9305 | if (!no) { | |
9306 | no = c->get_onode(noid, false); | |
9307 | } | |
9308 | r = _rename(txc, c, o, no, noid); | |
9309 | } | |
9310 | break; | |
9311 | ||
9312 | case Transaction::OP_OMAP_CLEAR: | |
9313 | { | |
9314 | r = _omap_clear(txc, c, o); | |
9315 | } | |
9316 | break; | |
9317 | case Transaction::OP_OMAP_SETKEYS: | |
9318 | { | |
9319 | bufferlist aset_bl; | |
9320 | i.decode_attrset_bl(&aset_bl); | |
9321 | r = _omap_setkeys(txc, c, o, aset_bl); | |
9322 | } | |
9323 | break; | |
9324 | case Transaction::OP_OMAP_RMKEYS: | |
9325 | { | |
9326 | bufferlist keys_bl; | |
9327 | i.decode_keyset_bl(&keys_bl); | |
9328 | r = _omap_rmkeys(txc, c, o, keys_bl); | |
9329 | } | |
9330 | break; | |
9331 | case Transaction::OP_OMAP_RMKEYRANGE: | |
9332 | { | |
9333 | string first, last; | |
9334 | first = i.decode_string(); | |
9335 | last = i.decode_string(); | |
9336 | r = _omap_rmkey_range(txc, c, o, first, last); | |
9337 | } | |
9338 | break; | |
9339 | case Transaction::OP_OMAP_SETHEADER: | |
9340 | { | |
9341 | bufferlist bl; | |
9342 | i.decode_bl(bl); | |
9343 | r = _omap_setheader(txc, c, o, bl); | |
9344 | } | |
9345 | break; | |
9346 | ||
9347 | case Transaction::OP_SETALLOCHINT: | |
9348 | { | |
9349 | r = _set_alloc_hint(txc, c, o, | |
9350 | op->expected_object_size, | |
9351 | op->expected_write_size, | |
9352 | op->alloc_hint_flags); | |
9353 | } | |
9354 | break; | |
9355 | ||
9356 | default: | |
9357 | derr << __func__ << "bad op " << op->op << dendl; | |
9358 | ceph_abort(); | |
9359 | } | |
9360 | ||
9361 | endop: | |
9362 | if (r < 0) { | |
9363 | bool ok = false; | |
9364 | ||
9365 | if (r == -ENOENT && !(op->op == Transaction::OP_CLONERANGE || | |
9366 | op->op == Transaction::OP_CLONE || | |
9367 | op->op == Transaction::OP_CLONERANGE2 || | |
9368 | op->op == Transaction::OP_COLL_ADD || | |
9369 | op->op == Transaction::OP_SETATTR || | |
9370 | op->op == Transaction::OP_SETATTRS || | |
9371 | op->op == Transaction::OP_RMATTR || | |
9372 | op->op == Transaction::OP_OMAP_SETKEYS || | |
9373 | op->op == Transaction::OP_OMAP_RMKEYS || | |
9374 | op->op == Transaction::OP_OMAP_RMKEYRANGE || | |
9375 | op->op == Transaction::OP_OMAP_SETHEADER)) | |
9376 | // -ENOENT is usually okay | |
9377 | ok = true; | |
9378 | if (r == -ENODATA) | |
9379 | ok = true; | |
9380 | ||
9381 | if (!ok) { | |
9382 | const char *msg = "unexpected error code"; | |
9383 | ||
9384 | if (r == -ENOENT && (op->op == Transaction::OP_CLONERANGE || | |
9385 | op->op == Transaction::OP_CLONE || | |
9386 | op->op == Transaction::OP_CLONERANGE2)) | |
9387 | msg = "ENOENT on clone suggests osd bug"; | |
9388 | ||
9389 | if (r == -ENOSPC) | |
9390 | // For now, if we hit _any_ ENOSPC, crash, before we do any damage | |
9391 | // by partially applying transactions. | |
9392 | msg = "ENOSPC from bluestore, misconfigured cluster"; | |
9393 | ||
9394 | if (r == -ENOTEMPTY) { | |
9395 | msg = "ENOTEMPTY suggests garbage data in osd data dir"; | |
9396 | } | |
9397 | ||
9398 | derr << __func__ << " error " << cpp_strerror(r) | |
9399 | << " not handled on operation " << op->op | |
9400 | << " (op " << pos << ", counting from 0)" | |
9401 | << dendl; | |
9402 | derr << msg << dendl; | |
9403 | _dump_transaction(t, 0); | |
9404 | assert(0 == "unexpected error"); | |
9405 | } | |
9406 | } | |
9407 | } | |
9408 | } | |
9409 | ||
9410 | ||
9411 | ||
9412 | // ----------------- | |
9413 | // write operations | |
9414 | ||
9415 | int BlueStore::_touch(TransContext *txc, | |
9416 | CollectionRef& c, | |
9417 | OnodeRef &o) | |
9418 | { | |
9419 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
9420 | int r = 0; | |
9421 | _assign_nid(txc, o); | |
9422 | txc->write_onode(o); | |
9423 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
9424 | return r; | |
9425 | } | |
9426 | ||
9427 | void BlueStore::_dump_onode(OnodeRef o, int log_level) | |
9428 | { | |
9429 | if (!cct->_conf->subsys.should_gather(ceph_subsys_bluestore, log_level)) | |
9430 | return; | |
9431 | dout(log_level) << __func__ << " " << o << " " << o->oid | |
9432 | << " nid " << o->onode.nid | |
9433 | << " size 0x" << std::hex << o->onode.size | |
9434 | << " (" << std::dec << o->onode.size << ")" | |
9435 | << " expected_object_size " << o->onode.expected_object_size | |
9436 | << " expected_write_size " << o->onode.expected_write_size | |
9437 | << " in " << o->onode.extent_map_shards.size() << " shards" | |
9438 | << ", " << o->extent_map.spanning_blob_map.size() | |
9439 | << " spanning blobs" | |
9440 | << dendl; | |
9441 | for (auto p = o->onode.attrs.begin(); | |
9442 | p != o->onode.attrs.end(); | |
9443 | ++p) { | |
9444 | dout(log_level) << __func__ << " attr " << p->first | |
9445 | << " len " << p->second.length() << dendl; | |
9446 | } | |
9447 | _dump_extent_map(o->extent_map, log_level); | |
9448 | } | |
9449 | ||
9450 | void BlueStore::_dump_extent_map(ExtentMap &em, int log_level) | |
9451 | { | |
9452 | uint64_t pos = 0; | |
9453 | for (auto& s : em.shards) { | |
9454 | dout(log_level) << __func__ << " shard " << *s.shard_info | |
9455 | << (s.loaded ? " (loaded)" : "") | |
9456 | << (s.dirty ? " (dirty)" : "") | |
9457 | << dendl; | |
9458 | } | |
9459 | for (auto& e : em.extent_map) { | |
9460 | dout(log_level) << __func__ << " " << e << dendl; | |
9461 | assert(e.logical_offset >= pos); | |
9462 | pos = e.logical_offset + e.length; | |
9463 | const bluestore_blob_t& blob = e.blob->get_blob(); | |
9464 | if (blob.has_csum()) { | |
9465 | vector<uint64_t> v; | |
9466 | unsigned n = blob.get_csum_count(); | |
9467 | for (unsigned i = 0; i < n; ++i) | |
9468 | v.push_back(blob.get_csum_item(i)); | |
9469 | dout(log_level) << __func__ << " csum: " << std::hex << v << std::dec | |
9470 | << dendl; | |
9471 | } | |
9472 | std::lock_guard<std::recursive_mutex> l(e.blob->shared_blob->get_cache()->lock); | |
9473 | for (auto& i : e.blob->shared_blob->bc.buffer_map) { | |
9474 | dout(log_level) << __func__ << " 0x" << std::hex << i.first | |
9475 | << "~" << i.second->length << std::dec | |
9476 | << " " << *i.second << dendl; | |
9477 | } | |
9478 | } | |
9479 | } | |
9480 | ||
9481 | void BlueStore::_dump_transaction(Transaction *t, int log_level) | |
9482 | { | |
9483 | dout(log_level) << " transaction dump:\n"; | |
9484 | JSONFormatter f(true); | |
9485 | f.open_object_section("transaction"); | |
9486 | t->dump(&f); | |
9487 | f.close_section(); | |
9488 | f.flush(*_dout); | |
9489 | *_dout << dendl; | |
9490 | } | |
9491 | ||
9492 | void BlueStore::_pad_zeros( | |
9493 | bufferlist *bl, uint64_t *offset, | |
9494 | uint64_t chunk_size) | |
9495 | { | |
9496 | auto length = bl->length(); | |
9497 | dout(30) << __func__ << " 0x" << std::hex << *offset << "~" << length | |
9498 | << " chunk_size 0x" << chunk_size << std::dec << dendl; | |
9499 | dout(40) << "before:\n"; | |
9500 | bl->hexdump(*_dout); | |
9501 | *_dout << dendl; | |
9502 | // front | |
9503 | size_t front_pad = *offset % chunk_size; | |
9504 | size_t back_pad = 0; | |
9505 | size_t pad_count = 0; | |
9506 | if (front_pad) { | |
9507 | size_t front_copy = MIN(chunk_size - front_pad, length); | |
9508 | bufferptr z = buffer::create_page_aligned(chunk_size); | |
9509 | z.zero(0, front_pad, false); | |
9510 | pad_count += front_pad; | |
9511 | bl->copy(0, front_copy, z.c_str() + front_pad); | |
9512 | if (front_copy + front_pad < chunk_size) { | |
9513 | back_pad = chunk_size - (length + front_pad); | |
9514 | z.zero(front_pad + length, back_pad, false); | |
9515 | pad_count += back_pad; | |
9516 | } | |
9517 | bufferlist old, t; | |
9518 | old.swap(*bl); | |
9519 | t.substr_of(old, front_copy, length - front_copy); | |
9520 | bl->append(z); | |
9521 | bl->claim_append(t); | |
9522 | *offset -= front_pad; | |
9523 | length += pad_count; | |
9524 | } | |
9525 | ||
9526 | // back | |
9527 | uint64_t end = *offset + length; | |
9528 | unsigned back_copy = end % chunk_size; | |
9529 | if (back_copy) { | |
9530 | assert(back_pad == 0); | |
9531 | back_pad = chunk_size - back_copy; | |
9532 | assert(back_copy <= length); | |
9533 | bufferptr tail(chunk_size); | |
9534 | bl->copy(length - back_copy, back_copy, tail.c_str()); | |
9535 | tail.zero(back_copy, back_pad, false); | |
9536 | bufferlist old; | |
9537 | old.swap(*bl); | |
9538 | bl->substr_of(old, 0, length - back_copy); | |
9539 | bl->append(tail); | |
9540 | length += back_pad; | |
9541 | pad_count += back_pad; | |
9542 | } | |
9543 | dout(20) << __func__ << " pad 0x" << std::hex << front_pad << " + 0x" | |
9544 | << back_pad << " on front/back, now 0x" << *offset << "~" | |
9545 | << length << std::dec << dendl; | |
9546 | dout(40) << "after:\n"; | |
9547 | bl->hexdump(*_dout); | |
9548 | *_dout << dendl; | |
9549 | if (pad_count) | |
9550 | logger->inc(l_bluestore_write_pad_bytes, pad_count); | |
9551 | assert(bl->length() == length); | |
9552 | } | |
9553 | ||
9554 | void BlueStore::_do_write_small( | |
9555 | TransContext *txc, | |
9556 | CollectionRef &c, | |
9557 | OnodeRef o, | |
9558 | uint64_t offset, uint64_t length, | |
9559 | bufferlist::iterator& blp, | |
9560 | WriteContext *wctx) | |
9561 | { | |
9562 | dout(10) << __func__ << " 0x" << std::hex << offset << "~" << length | |
9563 | << std::dec << dendl; | |
9564 | assert(length < min_alloc_size); | |
9565 | uint64_t end_offs = offset + length; | |
9566 | ||
9567 | logger->inc(l_bluestore_write_small); | |
9568 | logger->inc(l_bluestore_write_small_bytes, length); | |
9569 | ||
9570 | bufferlist bl; | |
9571 | blp.copy(length, bl); | |
9572 | ||
9573 | // Look for an existing mutable blob we can use. | |
9574 | auto begin = o->extent_map.extent_map.begin(); | |
9575 | auto end = o->extent_map.extent_map.end(); | |
9576 | auto ep = o->extent_map.seek_lextent(offset); | |
9577 | if (ep != begin) { | |
9578 | --ep; | |
9579 | if (ep->blob_end() <= offset) { | |
9580 | ++ep; | |
9581 | } | |
9582 | } | |
9583 | auto prev_ep = ep; | |
9584 | if (prev_ep != begin) { | |
9585 | --prev_ep; | |
9586 | } else { | |
9587 | prev_ep = end; // to avoid this extent check as it's a duplicate | |
9588 | } | |
9589 | ||
9590 | auto max_bsize = MAX(wctx->target_blob_size, min_alloc_size); | |
9591 | auto min_off = offset >= max_bsize ? offset - max_bsize : 0; | |
9592 | uint32_t alloc_len = min_alloc_size; | |
9593 | auto offset0 = P2ALIGN(offset, alloc_len); | |
9594 | ||
9595 | bool any_change; | |
9596 | ||
9597 | // search suitable extent in both forward and reverse direction in | |
9598 | // [offset - target_max_blob_size, offset + target_max_blob_size] range | |
9599 | // then check if blob can be reused via can_reuse_blob func or apply | |
9600 | // direct/deferred write (the latter for extents including or higher | |
9601 | // than 'offset' only). | |
9602 | do { | |
9603 | any_change = false; | |
9604 | ||
9605 | if (ep != end && ep->logical_offset < offset + max_bsize) { | |
9606 | BlobRef b = ep->blob; | |
9607 | auto bstart = ep->blob_start(); | |
9608 | dout(20) << __func__ << " considering " << *b | |
9609 | << " bstart 0x" << std::hex << bstart << std::dec << dendl; | |
9610 | if (bstart >= end_offs) { | |
9611 | dout(20) << __func__ << " ignoring distant " << *b << dendl; | |
9612 | } else if (!b->get_blob().is_mutable()) { | |
9613 | dout(20) << __func__ << " ignoring immutable " << *b << dendl; | |
9614 | } else if (ep->logical_offset % min_alloc_size != | |
9615 | ep->blob_offset % min_alloc_size) { | |
9616 | dout(20) << __func__ << " ignoring offset-skewed " << *b << dendl; | |
9617 | } else { | |
9618 | uint64_t chunk_size = b->get_blob().get_chunk_size(block_size); | |
9619 | // can we pad our head/tail out with zeros? | |
9620 | uint64_t head_pad, tail_pad; | |
9621 | head_pad = P2PHASE(offset, chunk_size); | |
9622 | tail_pad = P2NPHASE(end_offs, chunk_size); | |
9623 | if (head_pad || tail_pad) { | |
9624 | o->extent_map.fault_range(db, offset - head_pad, | |
9625 | end_offs - offset + head_pad + tail_pad); | |
9626 | } | |
9627 | if (head_pad && | |
9628 | o->extent_map.has_any_lextents(offset - head_pad, chunk_size)) { | |
9629 | head_pad = 0; | |
9630 | } | |
9631 | if (tail_pad && o->extent_map.has_any_lextents(end_offs, tail_pad)) { | |
9632 | tail_pad = 0; | |
9633 | } | |
9634 | ||
9635 | uint64_t b_off = offset - head_pad - bstart; | |
9636 | uint64_t b_len = length + head_pad + tail_pad; | |
9637 | ||
9638 | // direct write into unused blocks of an existing mutable blob? | |
9639 | if ((b_off % chunk_size == 0 && b_len % chunk_size == 0) && | |
9640 | b->get_blob().get_ondisk_length() >= b_off + b_len && | |
9641 | b->get_blob().is_unused(b_off, b_len) && | |
9642 | b->get_blob().is_allocated(b_off, b_len)) { | |
9643 | _apply_padding(head_pad, tail_pad, bl); | |
9644 | ||
9645 | dout(20) << __func__ << " write to unused 0x" << std::hex | |
9646 | << b_off << "~" << b_len | |
9647 | << " pad 0x" << head_pad << " + 0x" << tail_pad | |
9648 | << std::dec << " of mutable " << *b << dendl; | |
9649 | _buffer_cache_write(txc, b, b_off, bl, | |
9650 | wctx->buffered ? 0 : Buffer::FLAG_NOCACHE); | |
9651 | ||
9652 | if (!g_conf->bluestore_debug_omit_block_device_write) { | |
9653 | if (b_len <= prefer_deferred_size) { | |
9654 | dout(20) << __func__ << " deferring small 0x" << std::hex | |
9655 | << b_len << std::dec << " unused write via deferred" << dendl; | |
9656 | bluestore_deferred_op_t *op = _get_deferred_op(txc, o); | |
9657 | op->op = bluestore_deferred_op_t::OP_WRITE; | |
9658 | b->get_blob().map( | |
9659 | b_off, b_len, | |
9660 | [&](uint64_t offset, uint64_t length) { | |
9661 | op->extents.emplace_back(bluestore_pextent_t(offset, length)); | |
9662 | return 0; | |
9663 | }); | |
9664 | op->data = bl; | |
9665 | } else { | |
9666 | b->get_blob().map_bl( | |
9667 | b_off, bl, | |
9668 | [&](uint64_t offset, bufferlist& t) { | |
9669 | bdev->aio_write(offset, t, | |
9670 | &txc->ioc, wctx->buffered); | |
9671 | }); | |
9672 | } | |
9673 | } | |
9674 | b->dirty_blob().calc_csum(b_off, bl); | |
9675 | dout(20) << __func__ << " lex old " << *ep << dendl; | |
9676 | Extent *le = o->extent_map.set_lextent(c, offset, b_off + head_pad, length, | |
9677 | b, | |
9678 | &wctx->old_extents); | |
9679 | b->dirty_blob().mark_used(le->blob_offset, le->length); | |
9680 | txc->statfs_delta.stored() += le->length; | |
9681 | dout(20) << __func__ << " lex " << *le << dendl; | |
9682 | logger->inc(l_bluestore_write_small_unused); | |
9683 | return; | |
9684 | } | |
9685 | // read some data to fill out the chunk? | |
9686 | uint64_t head_read = P2PHASE(b_off, chunk_size); | |
9687 | uint64_t tail_read = P2NPHASE(b_off + b_len, chunk_size); | |
9688 | if ((head_read || tail_read) && | |
9689 | (b->get_blob().get_ondisk_length() >= b_off + b_len + tail_read) && | |
9690 | head_read + tail_read < min_alloc_size) { | |
9691 | b_off -= head_read; | |
9692 | b_len += head_read + tail_read; | |
9693 | ||
9694 | } else { | |
9695 | head_read = tail_read = 0; | |
9696 | } | |
9697 | ||
9698 | // chunk-aligned deferred overwrite? | |
9699 | if (b->get_blob().get_ondisk_length() >= b_off + b_len && | |
9700 | b_off % chunk_size == 0 && | |
9701 | b_len % chunk_size == 0 && | |
9702 | b->get_blob().is_allocated(b_off, b_len)) { | |
9703 | ||
9704 | _apply_padding(head_pad, tail_pad, bl); | |
9705 | ||
9706 | dout(20) << __func__ << " reading head 0x" << std::hex << head_read | |
9707 | << " and tail 0x" << tail_read << std::dec << dendl; | |
9708 | if (head_read) { | |
9709 | bufferlist head_bl; | |
9710 | int r = _do_read(c.get(), o, offset - head_pad - head_read, head_read, | |
9711 | head_bl, 0); | |
9712 | assert(r >= 0 && r <= (int)head_read); | |
9713 | size_t zlen = head_read - r; | |
9714 | if (zlen) { | |
9715 | head_bl.append_zero(zlen); | |
9716 | logger->inc(l_bluestore_write_pad_bytes, zlen); | |
9717 | } | |
9718 | bl.claim_prepend(head_bl); | |
9719 | logger->inc(l_bluestore_write_penalty_read_ops); | |
9720 | } | |
9721 | if (tail_read) { | |
9722 | bufferlist tail_bl; | |
9723 | int r = _do_read(c.get(), o, offset + length + tail_pad, tail_read, | |
9724 | tail_bl, 0); | |
9725 | assert(r >= 0 && r <= (int)tail_read); | |
9726 | size_t zlen = tail_read - r; | |
9727 | if (zlen) { | |
9728 | tail_bl.append_zero(zlen); | |
9729 | logger->inc(l_bluestore_write_pad_bytes, zlen); | |
9730 | } | |
9731 | bl.claim_append(tail_bl); | |
9732 | logger->inc(l_bluestore_write_penalty_read_ops); | |
9733 | } | |
9734 | logger->inc(l_bluestore_write_small_pre_read); | |
9735 | ||
9736 | bluestore_deferred_op_t *op = _get_deferred_op(txc, o); | |
9737 | op->op = bluestore_deferred_op_t::OP_WRITE; | |
9738 | _buffer_cache_write(txc, b, b_off, bl, | |
9739 | wctx->buffered ? 0 : Buffer::FLAG_NOCACHE); | |
9740 | ||
9741 | int r = b->get_blob().map( | |
9742 | b_off, b_len, | |
9743 | [&](uint64_t offset, uint64_t length) { | |
9744 | op->extents.emplace_back(bluestore_pextent_t(offset, length)); | |
9745 | return 0; | |
9746 | }); | |
9747 | assert(r == 0); | |
9748 | if (b->get_blob().csum_type) { | |
9749 | b->dirty_blob().calc_csum(b_off, bl); | |
9750 | } | |
9751 | op->data.claim(bl); | |
9752 | dout(20) << __func__ << " deferred write 0x" << std::hex << b_off << "~" | |
9753 | << b_len << std::dec << " of mutable " << *b | |
9754 | << " at " << op->extents << dendl; | |
9755 | Extent *le = o->extent_map.set_lextent(c, offset, offset - bstart, length, | |
9756 | b, &wctx->old_extents); | |
9757 | b->dirty_blob().mark_used(le->blob_offset, le->length); | |
9758 | txc->statfs_delta.stored() += le->length; | |
9759 | dout(20) << __func__ << " lex " << *le << dendl; | |
9760 | logger->inc(l_bluestore_write_small_deferred); | |
9761 | return; | |
9762 | } | |
9763 | // try to reuse blob if we can | |
9764 | if (b->can_reuse_blob(min_alloc_size, | |
9765 | max_bsize, | |
9766 | offset0 - bstart, | |
9767 | &alloc_len)) { | |
9768 | assert(alloc_len == min_alloc_size); // expecting data always | |
9769 | // fit into reused blob | |
9770 | // Need to check for pending writes desiring to | |
9771 | // reuse the same pextent. The rationale is that during GC two chunks | |
9772 | // from garbage blobs(compressed?) can share logical space within the same | |
9773 | // AU. That's in turn might be caused by unaligned len in clone_range2. | |
9774 | // Hence the second write will fail in an attempt to reuse blob at | |
9775 | // do_alloc_write(). | |
9776 | if (!wctx->has_conflict(b, | |
9777 | offset0, | |
9778 | offset0 + alloc_len, | |
9779 | min_alloc_size)) { | |
9780 | ||
9781 | // we can't reuse pad_head/pad_tail since they might be truncated | |
9782 | // due to existent extents | |
9783 | uint64_t b_off = offset - bstart; | |
9784 | uint64_t b_off0 = b_off; | |
9785 | _pad_zeros(&bl, &b_off0, chunk_size); | |
9786 | ||
9787 | dout(20) << __func__ << " reuse blob " << *b << std::hex | |
9788 | << " (0x" << b_off0 << "~" << bl.length() << ")" | |
9789 | << " (0x" << b_off << "~" << length << ")" | |
9790 | << std::dec << dendl; | |
9791 | ||
9792 | o->extent_map.punch_hole(c, offset, length, &wctx->old_extents); | |
9793 | wctx->write(offset, b, alloc_len, b_off0, bl, b_off, length, | |
9794 | false, false); | |
9795 | logger->inc(l_bluestore_write_small_unused); | |
9796 | return; | |
9797 | } | |
9798 | } | |
9799 | } | |
9800 | ++ep; | |
9801 | any_change = true; | |
9802 | } // if (ep != end && ep->logical_offset < offset + max_bsize) | |
9803 | ||
9804 | // check extent for reuse in reverse order | |
9805 | if (prev_ep != end && prev_ep->logical_offset >= min_off) { | |
9806 | BlobRef b = prev_ep->blob; | |
9807 | auto bstart = prev_ep->blob_start(); | |
9808 | dout(20) << __func__ << " considering " << *b | |
9809 | << " bstart 0x" << std::hex << bstart << std::dec << dendl; | |
9810 | if (b->can_reuse_blob(min_alloc_size, | |
9811 | max_bsize, | |
9812 | offset0 - bstart, | |
9813 | &alloc_len)) { | |
9814 | assert(alloc_len == min_alloc_size); // expecting data always | |
9815 | // fit into reused blob | |
9816 | // Need to check for pending writes desiring to | |
9817 | // reuse the same pextent. The rationale is that during GC two chunks | |
9818 | // from garbage blobs(compressed?) can share logical space within the same | |
9819 | // AU. That's in turn might be caused by unaligned len in clone_range2. | |
9820 | // Hence the second write will fail in an attempt to reuse blob at | |
9821 | // do_alloc_write(). | |
9822 | if (!wctx->has_conflict(b, | |
9823 | offset0, | |
9824 | offset0 + alloc_len, | |
9825 | min_alloc_size)) { | |
9826 | ||
9827 | uint64_t chunk_size = b->get_blob().get_chunk_size(block_size); | |
9828 | uint64_t b_off = offset - bstart; | |
9829 | uint64_t b_off0 = b_off; | |
9830 | _pad_zeros(&bl, &b_off0, chunk_size); | |
9831 | ||
9832 | dout(20) << __func__ << " reuse blob " << *b << std::hex | |
9833 | << " (0x" << b_off0 << "~" << bl.length() << ")" | |
9834 | << " (0x" << b_off << "~" << length << ")" | |
9835 | << std::dec << dendl; | |
9836 | ||
9837 | o->extent_map.punch_hole(c, offset, length, &wctx->old_extents); | |
9838 | wctx->write(offset, b, alloc_len, b_off0, bl, b_off, length, | |
9839 | false, false); | |
9840 | logger->inc(l_bluestore_write_small_unused); | |
9841 | return; | |
9842 | } | |
9843 | } | |
9844 | if (prev_ep != begin) { | |
9845 | --prev_ep; | |
9846 | any_change = true; | |
9847 | } else { | |
9848 | prev_ep = end; // to avoid useless first extent re-check | |
9849 | } | |
9850 | } // if (prev_ep != end && prev_ep->logical_offset >= min_off) | |
9851 | } while (any_change); | |
9852 | ||
9853 | // new blob. | |
9854 | ||
9855 | BlobRef b = c->new_blob(); | |
9856 | uint64_t b_off = P2PHASE(offset, alloc_len); | |
9857 | uint64_t b_off0 = b_off; | |
9858 | _pad_zeros(&bl, &b_off0, block_size); | |
9859 | o->extent_map.punch_hole(c, offset, length, &wctx->old_extents); | |
9860 | wctx->write(offset, b, alloc_len, b_off0, bl, b_off, length, true, true); | |
9861 | logger->inc(l_bluestore_write_small_new); | |
9862 | ||
9863 | return; | |
9864 | } | |
9865 | ||
9866 | void BlueStore::_do_write_big( | |
9867 | TransContext *txc, | |
9868 | CollectionRef &c, | |
9869 | OnodeRef o, | |
9870 | uint64_t offset, uint64_t length, | |
9871 | bufferlist::iterator& blp, | |
9872 | WriteContext *wctx) | |
9873 | { | |
9874 | dout(10) << __func__ << " 0x" << std::hex << offset << "~" << length | |
9875 | << " target_blob_size 0x" << wctx->target_blob_size << std::dec | |
9876 | << " compress " << (int)wctx->compress | |
9877 | << dendl; | |
9878 | logger->inc(l_bluestore_write_big); | |
9879 | logger->inc(l_bluestore_write_big_bytes, length); | |
9880 | o->extent_map.punch_hole(c, offset, length, &wctx->old_extents); | |
9881 | auto max_bsize = MAX(wctx->target_blob_size, min_alloc_size); | |
9882 | while (length > 0) { | |
9883 | bool new_blob = false; | |
9884 | uint32_t l = MIN(max_bsize, length); | |
9885 | BlobRef b; | |
9886 | uint32_t b_off = 0; | |
9887 | ||
9888 | //attempting to reuse existing blob | |
9889 | if (!wctx->compress) { | |
9890 | // look for an existing mutable blob we can reuse | |
9891 | auto begin = o->extent_map.extent_map.begin(); | |
9892 | auto end = o->extent_map.extent_map.end(); | |
9893 | auto ep = o->extent_map.seek_lextent(offset); | |
9894 | auto prev_ep = ep; | |
9895 | if (prev_ep != begin) { | |
9896 | --prev_ep; | |
9897 | } else { | |
9898 | prev_ep = end; // to avoid this extent check as it's a duplicate | |
9899 | } | |
9900 | auto min_off = offset >= max_bsize ? offset - max_bsize : 0; | |
9901 | // search suitable extent in both forward and reverse direction in | |
9902 | // [offset - target_max_blob_size, offset + target_max_blob_size] range | |
9903 | // then check if blob can be reused via can_reuse_blob func. | |
9904 | bool any_change; | |
9905 | do { | |
9906 | any_change = false; | |
9907 | if (ep != end && ep->logical_offset < offset + max_bsize) { | |
9908 | if (offset >= ep->blob_start() && | |
9909 | ep->blob->can_reuse_blob(min_alloc_size, max_bsize, | |
9910 | offset - ep->blob_start(), | |
9911 | &l)) { | |
9912 | b = ep->blob; | |
9913 | b_off = offset - ep->blob_start(); | |
9914 | prev_ep = end; // to avoid check below | |
9915 | dout(20) << __func__ << " reuse blob " << *b << std::hex | |
9916 | << " (0x" << b_off << "~" << l << ")" << std::dec << dendl; | |
9917 | } else { | |
9918 | ++ep; | |
9919 | any_change = true; | |
9920 | } | |
9921 | } | |
9922 | ||
9923 | if (prev_ep != end && prev_ep->logical_offset >= min_off) { | |
9924 | if (prev_ep->blob->can_reuse_blob(min_alloc_size, max_bsize, | |
9925 | offset - prev_ep->blob_start(), | |
9926 | &l)) { | |
9927 | b = prev_ep->blob; | |
9928 | b_off = offset - prev_ep->blob_start(); | |
9929 | dout(20) << __func__ << " reuse blob " << *b << std::hex | |
9930 | << " (0x" << b_off << "~" << l << ")" << std::dec << dendl; | |
9931 | } else if (prev_ep != begin) { | |
9932 | --prev_ep; | |
9933 | any_change = true; | |
9934 | } else { | |
9935 | prev_ep = end; // to avoid useless first extent re-check | |
9936 | } | |
9937 | } | |
9938 | } while (b == nullptr && any_change); | |
9939 | } | |
9940 | if (b == nullptr) { | |
9941 | b = c->new_blob(); | |
9942 | b_off = 0; | |
9943 | new_blob = true; | |
9944 | } | |
9945 | ||
9946 | bufferlist t; | |
9947 | blp.copy(l, t); | |
9948 | wctx->write(offset, b, l, b_off, t, b_off, l, false, new_blob); | |
9949 | offset += l; | |
9950 | length -= l; | |
9951 | logger->inc(l_bluestore_write_big_blobs); | |
9952 | } | |
9953 | } | |
9954 | ||
9955 | int BlueStore::_do_alloc_write( | |
9956 | TransContext *txc, | |
9957 | CollectionRef coll, | |
9958 | OnodeRef o, | |
9959 | WriteContext *wctx) | |
9960 | { | |
9961 | dout(20) << __func__ << " txc " << txc | |
9962 | << " " << wctx->writes.size() << " blobs" | |
9963 | << dendl; | |
9964 | if (wctx->writes.empty()) { | |
9965 | return 0; | |
9966 | } | |
9967 | ||
9968 | CompressorRef c; | |
9969 | double crr = 0; | |
9970 | if (wctx->compress) { | |
9971 | c = select_option( | |
9972 | "compression_algorithm", | |
9973 | compressor, | |
9974 | [&]() { | |
9975 | string val; | |
9976 | if (coll->pool_opts.get(pool_opts_t::COMPRESSION_ALGORITHM, &val)) { | |
9977 | CompressorRef cp = compressor; | |
9978 | if (!cp || cp->get_type_name() != val) { | |
9979 | cp = Compressor::create(cct, val); | |
9980 | } | |
9981 | return boost::optional<CompressorRef>(cp); | |
9982 | } | |
9983 | return boost::optional<CompressorRef>(); | |
9984 | } | |
9985 | ); | |
9986 | ||
9987 | crr = select_option( | |
9988 | "compression_required_ratio", | |
9989 | cct->_conf->bluestore_compression_required_ratio, | |
9990 | [&]() { | |
9991 | double val; | |
9992 | if (coll->pool_opts.get(pool_opts_t::COMPRESSION_REQUIRED_RATIO, &val)) { | |
9993 | return boost::optional<double>(val); | |
9994 | } | |
9995 | return boost::optional<double>(); | |
9996 | } | |
9997 | ); | |
9998 | } | |
9999 | ||
10000 | // checksum | |
10001 | int csum = csum_type.load(); | |
10002 | csum = select_option( | |
10003 | "csum_type", | |
10004 | csum, | |
10005 | [&]() { | |
10006 | int val; | |
10007 | if (coll->pool_opts.get(pool_opts_t::CSUM_TYPE, &val)) { | |
10008 | return boost::optional<int>(val); | |
10009 | } | |
10010 | return boost::optional<int>(); | |
10011 | } | |
10012 | ); | |
10013 | ||
10014 | // compress (as needed) and calc needed space | |
10015 | uint64_t need = 0; | |
10016 | auto max_bsize = MAX(wctx->target_blob_size, min_alloc_size); | |
10017 | for (auto& wi : wctx->writes) { | |
10018 | if (c && wi.blob_length > min_alloc_size) { | |
10019 | utime_t start = ceph_clock_now(); | |
10020 | ||
10021 | // compress | |
10022 | assert(wi.b_off == 0); | |
10023 | assert(wi.blob_length == wi.bl.length()); | |
10024 | ||
10025 | // FIXME: memory alignment here is bad | |
10026 | bufferlist t; | |
10027 | int r = c->compress(wi.bl, t); | |
10028 | assert(r == 0); | |
10029 | ||
10030 | bluestore_compression_header_t chdr; | |
10031 | chdr.type = c->get_type(); | |
10032 | chdr.length = t.length(); | |
10033 | ::encode(chdr, wi.compressed_bl); | |
10034 | wi.compressed_bl.claim_append(t); | |
10035 | ||
10036 | wi.compressed_len = wi.compressed_bl.length(); | |
10037 | uint64_t newlen = P2ROUNDUP(wi.compressed_len, min_alloc_size); | |
10038 | uint64_t want_len_raw = wi.blob_length * crr; | |
10039 | uint64_t want_len = P2ROUNDUP(want_len_raw, min_alloc_size); | |
10040 | if (newlen <= want_len && newlen < wi.blob_length) { | |
10041 | // Cool. We compressed at least as much as we were hoping to. | |
10042 | // pad out to min_alloc_size | |
10043 | wi.compressed_bl.append_zero(newlen - wi.compressed_len); | |
10044 | logger->inc(l_bluestore_write_pad_bytes, newlen - wi.compressed_len); | |
10045 | dout(20) << __func__ << std::hex << " compressed 0x" << wi.blob_length | |
10046 | << " -> 0x" << wi.compressed_len << " => 0x" << newlen | |
10047 | << " with " << c->get_type() | |
10048 | << std::dec << dendl; | |
10049 | txc->statfs_delta.compressed() += wi.compressed_len; | |
10050 | txc->statfs_delta.compressed_original() += wi.blob_length; | |
10051 | txc->statfs_delta.compressed_allocated() += newlen; | |
10052 | logger->inc(l_bluestore_compress_success_count); | |
10053 | wi.compressed = true; | |
10054 | need += newlen; | |
10055 | } else { | |
10056 | dout(20) << __func__ << std::hex << " 0x" << wi.blob_length | |
10057 | << " compressed to 0x" << wi.compressed_len << " -> 0x" << newlen | |
10058 | << " with " << c->get_type() | |
10059 | << ", which is more than required 0x" << want_len_raw | |
10060 | << " -> 0x" << want_len | |
10061 | << ", leaving uncompressed" | |
10062 | << std::dec << dendl; | |
10063 | logger->inc(l_bluestore_compress_rejected_count); | |
10064 | need += wi.blob_length; | |
10065 | } | |
10066 | logger->tinc(l_bluestore_compress_lat, | |
10067 | ceph_clock_now() - start); | |
10068 | } else { | |
10069 | need += wi.blob_length; | |
10070 | } | |
10071 | } | |
10072 | int r = alloc->reserve(need); | |
10073 | if (r < 0) { | |
10074 | derr << __func__ << " failed to reserve 0x" << std::hex << need << std::dec | |
10075 | << dendl; | |
10076 | return r; | |
10077 | } | |
10078 | AllocExtentVector prealloc; | |
10079 | prealloc.reserve(2 * wctx->writes.size());; | |
10080 | int prealloc_left = 0; | |
10081 | prealloc_left = alloc->allocate( | |
10082 | need, min_alloc_size, need, | |
10083 | 0, &prealloc); | |
10084 | assert(prealloc_left == (int64_t)need); | |
10085 | dout(20) << __func__ << " prealloc " << prealloc << dendl; | |
10086 | auto prealloc_pos = prealloc.begin(); | |
10087 | ||
10088 | for (auto& wi : wctx->writes) { | |
10089 | BlobRef b = wi.b; | |
10090 | bluestore_blob_t& dblob = b->dirty_blob(); | |
10091 | uint64_t b_off = wi.b_off; | |
10092 | bufferlist *l = &wi.bl; | |
10093 | uint64_t final_length = wi.blob_length; | |
10094 | uint64_t csum_length = wi.blob_length; | |
10095 | unsigned csum_order = block_size_order; | |
10096 | if (wi.compressed) { | |
10097 | final_length = wi.compressed_bl.length(); | |
10098 | csum_length = final_length; | |
10099 | csum_order = ctz(csum_length); | |
10100 | l = &wi.compressed_bl; | |
10101 | dblob.set_compressed(wi.blob_length, wi.compressed_len); | |
10102 | } else if (wi.new_blob) { | |
10103 | // initialize newly created blob only | |
10104 | assert(dblob.is_mutable()); | |
10105 | if (l->length() != wi.blob_length) { | |
10106 | // hrm, maybe we could do better here, but let's not bother. | |
10107 | dout(20) << __func__ << " forcing csum_order to block_size_order " | |
10108 | << block_size_order << dendl; | |
10109 | csum_order = block_size_order; | |
10110 | } else { | |
10111 | csum_order = std::min(wctx->csum_order, ctz(l->length())); | |
10112 | } | |
10113 | // try to align blob with max_blob_size to improve | |
10114 | // its reuse ratio, e.g. in case of reverse write | |
10115 | uint32_t suggested_boff = | |
10116 | (wi.logical_offset - (wi.b_off0 - wi.b_off)) % max_bsize; | |
10117 | if ((suggested_boff % (1 << csum_order)) == 0 && | |
10118 | suggested_boff + final_length <= max_bsize && | |
10119 | suggested_boff > b_off) { | |
10120 | dout(20) << __func__ << " forcing blob_offset to 0x" | |
10121 | << std::hex << suggested_boff << std::dec << dendl; | |
10122 | assert(suggested_boff >= b_off); | |
10123 | csum_length += suggested_boff - b_off; | |
10124 | b_off = suggested_boff; | |
10125 | } | |
10126 | if (csum != Checksummer::CSUM_NONE) { | |
10127 | dout(20) << __func__ << " initialize csum setting for new blob " << *b | |
10128 | << " csum_type " << Checksummer::get_csum_type_string(csum) | |
10129 | << " csum_order " << csum_order | |
10130 | << " csum_length 0x" << std::hex << csum_length << std::dec | |
10131 | << dendl; | |
10132 | dblob.init_csum(csum, csum_order, csum_length); | |
10133 | } | |
10134 | } | |
10135 | ||
10136 | AllocExtentVector extents; | |
10137 | int64_t left = final_length; | |
10138 | while (left > 0) { | |
10139 | assert(prealloc_left > 0); | |
10140 | if (prealloc_pos->length <= left) { | |
10141 | prealloc_left -= prealloc_pos->length; | |
10142 | left -= prealloc_pos->length; | |
10143 | txc->statfs_delta.allocated() += prealloc_pos->length; | |
10144 | extents.push_back(*prealloc_pos); | |
10145 | ++prealloc_pos; | |
10146 | } else { | |
10147 | extents.emplace_back(prealloc_pos->offset, left); | |
10148 | prealloc_pos->offset += left; | |
10149 | prealloc_pos->length -= left; | |
10150 | prealloc_left -= left; | |
10151 | txc->statfs_delta.allocated() += left; | |
10152 | left = 0; | |
10153 | break; | |
10154 | } | |
10155 | } | |
10156 | for (auto& p : extents) { | |
10157 | txc->allocated.insert(p.offset, p.length); | |
10158 | } | |
10159 | dblob.allocated(P2ALIGN(b_off, min_alloc_size), final_length, extents); | |
10160 | ||
10161 | dout(20) << __func__ << " blob " << *b << dendl; | |
10162 | if (dblob.has_csum()) { | |
10163 | dblob.calc_csum(b_off, *l); | |
10164 | } | |
10165 | ||
10166 | if (wi.mark_unused) { | |
10167 | auto b_end = b_off + wi.bl.length(); | |
10168 | if (b_off) { | |
10169 | dblob.add_unused(0, b_off); | |
10170 | } | |
10171 | if (b_end < wi.blob_length) { | |
10172 | dblob.add_unused(b_end, wi.blob_length - b_end); | |
10173 | } | |
10174 | } | |
10175 | ||
10176 | Extent *le = o->extent_map.set_lextent(coll, wi.logical_offset, | |
10177 | b_off + (wi.b_off0 - wi.b_off), | |
10178 | wi.length0, | |
10179 | wi.b, | |
10180 | nullptr); | |
10181 | wi.b->dirty_blob().mark_used(le->blob_offset, le->length); | |
10182 | txc->statfs_delta.stored() += le->length; | |
10183 | dout(20) << __func__ << " lex " << *le << dendl; | |
10184 | _buffer_cache_write(txc, wi.b, b_off, wi.bl, | |
10185 | wctx->buffered ? 0 : Buffer::FLAG_NOCACHE); | |
10186 | ||
10187 | // queue io | |
10188 | if (!g_conf->bluestore_debug_omit_block_device_write) { | |
10189 | if (l->length() <= prefer_deferred_size.load()) { | |
10190 | dout(20) << __func__ << " deferring small 0x" << std::hex | |
10191 | << l->length() << std::dec << " write via deferred" << dendl; | |
10192 | bluestore_deferred_op_t *op = _get_deferred_op(txc, o); | |
10193 | op->op = bluestore_deferred_op_t::OP_WRITE; | |
10194 | int r = b->get_blob().map( | |
10195 | b_off, l->length(), | |
10196 | [&](uint64_t offset, uint64_t length) { | |
10197 | op->extents.emplace_back(bluestore_pextent_t(offset, length)); | |
10198 | return 0; | |
10199 | }); | |
10200 | assert(r == 0); | |
10201 | op->data = *l; | |
10202 | } else { | |
10203 | b->get_blob().map_bl( | |
10204 | b_off, *l, | |
10205 | [&](uint64_t offset, bufferlist& t) { | |
10206 | bdev->aio_write(offset, t, &txc->ioc, false); | |
10207 | }); | |
10208 | } | |
10209 | } | |
10210 | } | |
10211 | assert(prealloc_pos == prealloc.end()); | |
10212 | assert(prealloc_left == 0); | |
10213 | return 0; | |
10214 | } | |
10215 | ||
10216 | void BlueStore::_wctx_finish( | |
10217 | TransContext *txc, | |
10218 | CollectionRef& c, | |
10219 | OnodeRef o, | |
10220 | WriteContext *wctx, | |
10221 | set<SharedBlob*> *maybe_unshared_blobs) | |
10222 | { | |
10223 | auto oep = wctx->old_extents.begin(); | |
10224 | while (oep != wctx->old_extents.end()) { | |
10225 | auto &lo = *oep; | |
10226 | oep = wctx->old_extents.erase(oep); | |
10227 | dout(20) << __func__ << " lex_old " << lo.e << dendl; | |
10228 | BlobRef b = lo.e.blob; | |
10229 | const bluestore_blob_t& blob = b->get_blob(); | |
10230 | if (blob.is_compressed()) { | |
10231 | if (lo.blob_empty) { | |
10232 | txc->statfs_delta.compressed() -= blob.get_compressed_payload_length(); | |
10233 | } | |
10234 | txc->statfs_delta.compressed_original() -= lo.e.length; | |
10235 | } | |
10236 | auto& r = lo.r; | |
10237 | txc->statfs_delta.stored() -= lo.e.length; | |
10238 | if (!r.empty()) { | |
10239 | dout(20) << __func__ << " blob release " << r << dendl; | |
10240 | if (blob.is_shared()) { | |
10241 | PExtentVector final; | |
10242 | c->load_shared_blob(b->shared_blob); | |
10243 | for (auto e : r) { | |
10244 | b->shared_blob->put_ref( | |
10245 | e.offset, e.length, &final, | |
10246 | b->is_referenced() ? nullptr : maybe_unshared_blobs); | |
10247 | } | |
10248 | dout(20) << __func__ << " shared_blob release " << final | |
10249 | << " from " << *b->shared_blob << dendl; | |
10250 | txc->write_shared_blob(b->shared_blob); | |
10251 | r.clear(); | |
10252 | r.swap(final); | |
10253 | } | |
10254 | } | |
10255 | // we can't invalidate our logical extents as we drop them because | |
10256 | // other lextents (either in our onode or others) may still | |
10257 | // reference them. but we can throw out anything that is no | |
10258 | // longer allocated. Note that this will leave behind edge bits | |
10259 | // that are no longer referenced but not deallocated (until they | |
10260 | // age out of the cache naturally). | |
10261 | b->discard_unallocated(c.get()); | |
10262 | for (auto e : r) { | |
10263 | dout(20) << __func__ << " release " << e << dendl; | |
10264 | txc->released.insert(e.offset, e.length); | |
10265 | txc->statfs_delta.allocated() -= e.length; | |
10266 | if (blob.is_compressed()) { | |
10267 | txc->statfs_delta.compressed_allocated() -= e.length; | |
10268 | } | |
10269 | } | |
10270 | delete &lo; | |
10271 | if (b->is_spanning() && !b->is_referenced()) { | |
10272 | dout(20) << __func__ << " spanning_blob_map removing empty " << *b | |
10273 | << dendl; | |
10274 | o->extent_map.spanning_blob_map.erase(b->id); | |
10275 | } | |
10276 | } | |
10277 | } | |
10278 | ||
10279 | void BlueStore::_do_write_data( | |
10280 | TransContext *txc, | |
10281 | CollectionRef& c, | |
10282 | OnodeRef o, | |
10283 | uint64_t offset, | |
10284 | uint64_t length, | |
10285 | bufferlist& bl, | |
10286 | WriteContext *wctx) | |
10287 | { | |
10288 | uint64_t end = offset + length; | |
10289 | bufferlist::iterator p = bl.begin(); | |
10290 | ||
10291 | if (offset / min_alloc_size == (end - 1) / min_alloc_size && | |
10292 | (length != min_alloc_size)) { | |
10293 | // we fall within the same block | |
10294 | _do_write_small(txc, c, o, offset, length, p, wctx); | |
10295 | } else { | |
10296 | uint64_t head_offset, head_length; | |
10297 | uint64_t middle_offset, middle_length; | |
10298 | uint64_t tail_offset, tail_length; | |
10299 | ||
10300 | head_offset = offset; | |
10301 | head_length = P2NPHASE(offset, min_alloc_size); | |
10302 | ||
10303 | tail_offset = P2ALIGN(end, min_alloc_size); | |
10304 | tail_length = P2PHASE(end, min_alloc_size); | |
10305 | ||
10306 | middle_offset = head_offset + head_length; | |
10307 | middle_length = length - head_length - tail_length; | |
10308 | ||
10309 | if (head_length) { | |
10310 | _do_write_small(txc, c, o, head_offset, head_length, p, wctx); | |
10311 | } | |
10312 | ||
10313 | if (middle_length) { | |
10314 | _do_write_big(txc, c, o, middle_offset, middle_length, p, wctx); | |
10315 | } | |
10316 | ||
10317 | if (tail_length) { | |
10318 | _do_write_small(txc, c, o, tail_offset, tail_length, p, wctx); | |
10319 | } | |
10320 | } | |
10321 | } | |
10322 | ||
10323 | void BlueStore::_choose_write_options( | |
10324 | CollectionRef& c, | |
10325 | OnodeRef o, | |
10326 | uint32_t fadvise_flags, | |
10327 | WriteContext *wctx) | |
10328 | { | |
10329 | if (fadvise_flags & CEPH_OSD_OP_FLAG_FADVISE_WILLNEED) { | |
10330 | dout(20) << __func__ << " will do buffered write" << dendl; | |
10331 | wctx->buffered = true; | |
10332 | } else if (cct->_conf->bluestore_default_buffered_write && | |
10333 | (fadvise_flags & (CEPH_OSD_OP_FLAG_FADVISE_DONTNEED | | |
10334 | CEPH_OSD_OP_FLAG_FADVISE_NOCACHE)) == 0) { | |
10335 | dout(20) << __func__ << " defaulting to buffered write" << dendl; | |
10336 | wctx->buffered = true; | |
10337 | } | |
10338 | ||
10339 | // apply basic csum block size | |
10340 | wctx->csum_order = block_size_order; | |
10341 | ||
10342 | // compression parameters | |
10343 | unsigned alloc_hints = o->onode.alloc_hint_flags; | |
10344 | auto cm = select_option( | |
10345 | "compression_mode", | |
10346 | comp_mode.load(), | |
10347 | [&]() { | |
10348 | string val; | |
10349 | if(c->pool_opts.get(pool_opts_t::COMPRESSION_MODE, &val)) { | |
10350 | return boost::optional<Compressor::CompressionMode>( | |
10351 | Compressor::get_comp_mode_type(val)); | |
10352 | } | |
10353 | return boost::optional<Compressor::CompressionMode>(); | |
10354 | } | |
10355 | ); | |
10356 | ||
10357 | wctx->compress = (cm != Compressor::COMP_NONE) && | |
10358 | ((cm == Compressor::COMP_FORCE) || | |
10359 | (cm == Compressor::COMP_AGGRESSIVE && | |
10360 | (alloc_hints & CEPH_OSD_ALLOC_HINT_FLAG_INCOMPRESSIBLE) == 0) || | |
10361 | (cm == Compressor::COMP_PASSIVE && | |
10362 | (alloc_hints & CEPH_OSD_ALLOC_HINT_FLAG_COMPRESSIBLE))); | |
10363 | ||
10364 | if ((alloc_hints & CEPH_OSD_ALLOC_HINT_FLAG_SEQUENTIAL_READ) && | |
10365 | (alloc_hints & CEPH_OSD_ALLOC_HINT_FLAG_RANDOM_READ) == 0 && | |
10366 | (alloc_hints & (CEPH_OSD_ALLOC_HINT_FLAG_IMMUTABLE | | |
10367 | CEPH_OSD_ALLOC_HINT_FLAG_APPEND_ONLY)) && | |
10368 | (alloc_hints & CEPH_OSD_ALLOC_HINT_FLAG_RANDOM_WRITE) == 0) { | |
10369 | ||
10370 | dout(20) << __func__ << " will prefer large blob and csum sizes" << dendl; | |
10371 | ||
10372 | if (o->onode.expected_write_size) { | |
10373 | wctx->csum_order = std::max(min_alloc_size_order, | |
10374 | (uint8_t)ctz(o->onode.expected_write_size)); | |
10375 | } else { | |
10376 | wctx->csum_order = min_alloc_size_order; | |
10377 | } | |
10378 | ||
10379 | if (wctx->compress) { | |
10380 | wctx->target_blob_size = select_option( | |
10381 | "compression_max_blob_size", | |
10382 | comp_max_blob_size.load(), | |
10383 | [&]() { | |
10384 | int val; | |
10385 | if(c->pool_opts.get(pool_opts_t::COMPRESSION_MAX_BLOB_SIZE, &val)) { | |
10386 | return boost::optional<uint64_t>((uint64_t)val); | |
10387 | } | |
10388 | return boost::optional<uint64_t>(); | |
10389 | } | |
10390 | ); | |
10391 | } | |
10392 | } else { | |
10393 | if (wctx->compress) { | |
10394 | wctx->target_blob_size = select_option( | |
10395 | "compression_min_blob_size", | |
10396 | comp_min_blob_size.load(), | |
10397 | [&]() { | |
10398 | int val; | |
10399 | if(c->pool_opts.get(pool_opts_t::COMPRESSION_MIN_BLOB_SIZE, &val)) { | |
10400 | return boost::optional<uint64_t>((uint64_t)val); | |
10401 | } | |
10402 | return boost::optional<uint64_t>(); | |
10403 | } | |
10404 | ); | |
10405 | } | |
10406 | } | |
10407 | ||
10408 | uint64_t max_bsize = max_blob_size.load(); | |
10409 | if (wctx->target_blob_size == 0 || wctx->target_blob_size > max_bsize) { | |
10410 | wctx->target_blob_size = max_bsize; | |
10411 | } | |
10412 | ||
10413 | // set the min blob size floor at 2x the min_alloc_size, or else we | |
10414 | // won't be able to allocate a smaller extent for the compressed | |
10415 | // data. | |
10416 | if (wctx->compress && | |
10417 | wctx->target_blob_size < min_alloc_size * 2) { | |
10418 | wctx->target_blob_size = min_alloc_size * 2; | |
10419 | } | |
10420 | ||
10421 | dout(20) << __func__ << " prefer csum_order " << wctx->csum_order | |
10422 | << " target_blob_size 0x" << std::hex << wctx->target_blob_size | |
10423 | << std::dec << dendl; | |
10424 | } | |
10425 | ||
10426 | int BlueStore::_do_gc( | |
10427 | TransContext *txc, | |
10428 | CollectionRef& c, | |
10429 | OnodeRef o, | |
10430 | const GarbageCollector& gc, | |
10431 | const WriteContext& wctx, | |
10432 | uint64_t *dirty_start, | |
10433 | uint64_t *dirty_end) | |
10434 | { | |
10435 | auto& extents_to_collect = gc.get_extents_to_collect(); | |
10436 | ||
10437 | WriteContext wctx_gc; | |
10438 | wctx_gc.fork(wctx); // make a clone for garbage collection | |
10439 | ||
10440 | for (auto it = extents_to_collect.begin(); | |
10441 | it != extents_to_collect.end(); | |
10442 | ++it) { | |
10443 | bufferlist bl; | |
10444 | int r = _do_read(c.get(), o, it->offset, it->length, bl, 0); | |
10445 | assert(r == (int)it->length); | |
10446 | ||
10447 | o->extent_map.fault_range(db, it->offset, it->length); | |
10448 | _do_write_data(txc, c, o, it->offset, it->length, bl, &wctx_gc); | |
10449 | logger->inc(l_bluestore_gc_merged, it->length); | |
10450 | ||
10451 | if (*dirty_start > it->offset) { | |
10452 | *dirty_start = it->offset; | |
10453 | } | |
10454 | ||
10455 | if (*dirty_end < it->offset + it->length) { | |
10456 | *dirty_end = it->offset + it->length; | |
10457 | } | |
10458 | } | |
10459 | ||
10460 | dout(30) << __func__ << " alloc write" << dendl; | |
10461 | int r = _do_alloc_write(txc, c, o, &wctx_gc); | |
10462 | if (r < 0) { | |
10463 | derr << __func__ << " _do_alloc_write failed with " << cpp_strerror(r) | |
10464 | << dendl; | |
10465 | return r; | |
10466 | } | |
10467 | ||
10468 | _wctx_finish(txc, c, o, &wctx_gc); | |
10469 | return 0; | |
10470 | } | |
10471 | ||
10472 | int BlueStore::_do_write( | |
10473 | TransContext *txc, | |
10474 | CollectionRef& c, | |
10475 | OnodeRef o, | |
10476 | uint64_t offset, | |
10477 | uint64_t length, | |
10478 | bufferlist& bl, | |
10479 | uint32_t fadvise_flags) | |
10480 | { | |
10481 | int r = 0; | |
10482 | ||
10483 | dout(20) << __func__ | |
10484 | << " " << o->oid | |
10485 | << " 0x" << std::hex << offset << "~" << length | |
10486 | << " - have 0x" << o->onode.size | |
10487 | << " (" << std::dec << o->onode.size << ")" | |
10488 | << " bytes" | |
10489 | << " fadvise_flags 0x" << std::hex << fadvise_flags << std::dec | |
10490 | << dendl; | |
10491 | _dump_onode(o); | |
10492 | ||
10493 | if (length == 0) { | |
10494 | return 0; | |
10495 | } | |
10496 | ||
10497 | uint64_t end = offset + length; | |
10498 | ||
10499 | GarbageCollector gc(c->store->cct); | |
10500 | int64_t benefit; | |
10501 | auto dirty_start = offset; | |
10502 | auto dirty_end = end; | |
10503 | ||
10504 | WriteContext wctx; | |
10505 | _choose_write_options(c, o, fadvise_flags, &wctx); | |
10506 | o->extent_map.fault_range(db, offset, length); | |
10507 | _do_write_data(txc, c, o, offset, length, bl, &wctx); | |
10508 | r = _do_alloc_write(txc, c, o, &wctx); | |
10509 | if (r < 0) { | |
10510 | derr << __func__ << " _do_alloc_write failed with " << cpp_strerror(r) | |
10511 | << dendl; | |
10512 | goto out; | |
10513 | } | |
10514 | ||
10515 | // NB: _wctx_finish() will empty old_extents | |
10516 | // so we must do gc estimation before that | |
10517 | benefit = gc.estimate(offset, | |
10518 | length, | |
10519 | o->extent_map, | |
10520 | wctx.old_extents, | |
10521 | min_alloc_size); | |
10522 | ||
10523 | _wctx_finish(txc, c, o, &wctx); | |
10524 | if (end > o->onode.size) { | |
10525 | dout(20) << __func__ << " extending size to 0x" << std::hex << end | |
10526 | << std::dec << dendl; | |
10527 | o->onode.size = end; | |
10528 | } | |
10529 | ||
10530 | if (benefit >= g_conf->bluestore_gc_enable_total_threshold) { | |
10531 | if (!gc.get_extents_to_collect().empty()) { | |
10532 | dout(20) << __func__ << " perform garbage collection, " | |
10533 | << "expected benefit = " << benefit << " AUs" << dendl; | |
10534 | r = _do_gc(txc, c, o, gc, wctx, &dirty_start, &dirty_end); | |
10535 | if (r < 0) { | |
10536 | derr << __func__ << " _do_gc failed with " << cpp_strerror(r) | |
10537 | << dendl; | |
10538 | goto out; | |
10539 | } | |
10540 | } | |
10541 | } | |
10542 | ||
10543 | o->extent_map.compress_extent_map(dirty_start, dirty_end - dirty_start); | |
10544 | o->extent_map.dirty_range(dirty_start, dirty_end - dirty_start); | |
10545 | ||
10546 | r = 0; | |
10547 | ||
10548 | out: | |
10549 | return r; | |
10550 | } | |
10551 | ||
10552 | int BlueStore::_write(TransContext *txc, | |
10553 | CollectionRef& c, | |
10554 | OnodeRef& o, | |
10555 | uint64_t offset, size_t length, | |
10556 | bufferlist& bl, | |
10557 | uint32_t fadvise_flags) | |
10558 | { | |
10559 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10560 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
10561 | << dendl; | |
10562 | int r = 0; | |
10563 | if (offset + length >= OBJECT_MAX_SIZE) { | |
10564 | r = -E2BIG; | |
10565 | } else { | |
10566 | _assign_nid(txc, o); | |
10567 | r = _do_write(txc, c, o, offset, length, bl, fadvise_flags); | |
10568 | txc->write_onode(o); | |
10569 | } | |
10570 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
10571 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
10572 | << " = " << r << dendl; | |
10573 | return r; | |
10574 | } | |
10575 | ||
10576 | int BlueStore::_zero(TransContext *txc, | |
10577 | CollectionRef& c, | |
10578 | OnodeRef& o, | |
10579 | uint64_t offset, size_t length) | |
10580 | { | |
10581 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10582 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
10583 | << dendl; | |
10584 | int r = 0; | |
10585 | if (offset + length >= OBJECT_MAX_SIZE) { | |
10586 | r = -E2BIG; | |
10587 | } else { | |
10588 | _assign_nid(txc, o); | |
10589 | r = _do_zero(txc, c, o, offset, length); | |
10590 | } | |
10591 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
10592 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
10593 | << " = " << r << dendl; | |
10594 | return r; | |
10595 | } | |
10596 | ||
10597 | int BlueStore::_do_zero(TransContext *txc, | |
10598 | CollectionRef& c, | |
10599 | OnodeRef& o, | |
10600 | uint64_t offset, size_t length) | |
10601 | { | |
10602 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10603 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
10604 | << dendl; | |
10605 | int r = 0; | |
10606 | ||
10607 | _dump_onode(o); | |
10608 | ||
10609 | WriteContext wctx; | |
10610 | o->extent_map.fault_range(db, offset, length); | |
10611 | o->extent_map.punch_hole(c, offset, length, &wctx.old_extents); | |
10612 | o->extent_map.dirty_range(offset, length); | |
10613 | _wctx_finish(txc, c, o, &wctx); | |
10614 | ||
10615 | if (length > 0 && offset + length > o->onode.size) { | |
10616 | o->onode.size = offset + length; | |
10617 | dout(20) << __func__ << " extending size to " << offset + length | |
10618 | << dendl; | |
10619 | } | |
10620 | txc->write_onode(o); | |
10621 | ||
10622 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
10623 | << " 0x" << std::hex << offset << "~" << length << std::dec | |
10624 | << " = " << r << dendl; | |
10625 | return r; | |
10626 | } | |
10627 | ||
10628 | void BlueStore::_do_truncate( | |
10629 | TransContext *txc, CollectionRef& c, OnodeRef o, uint64_t offset, | |
10630 | set<SharedBlob*> *maybe_unshared_blobs) | |
10631 | { | |
10632 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10633 | << " 0x" << std::hex << offset << std::dec << dendl; | |
10634 | ||
10635 | _dump_onode(o, 30); | |
10636 | ||
10637 | if (offset == o->onode.size) | |
10638 | return; | |
10639 | ||
10640 | if (offset < o->onode.size) { | |
10641 | WriteContext wctx; | |
10642 | uint64_t length = o->onode.size - offset; | |
10643 | o->extent_map.fault_range(db, offset, length); | |
10644 | o->extent_map.punch_hole(c, offset, length, &wctx.old_extents); | |
10645 | o->extent_map.dirty_range(offset, length); | |
10646 | _wctx_finish(txc, c, o, &wctx, maybe_unshared_blobs); | |
10647 | ||
10648 | // if we have shards past EOF, ask for a reshard | |
10649 | if (!o->onode.extent_map_shards.empty() && | |
10650 | o->onode.extent_map_shards.back().offset >= offset) { | |
10651 | dout(10) << __func__ << " request reshard past EOF" << dendl; | |
10652 | if (offset) { | |
10653 | o->extent_map.request_reshard(offset - 1, offset + length); | |
10654 | } else { | |
10655 | o->extent_map.request_reshard(0, length); | |
10656 | } | |
10657 | } | |
10658 | } | |
10659 | ||
10660 | o->onode.size = offset; | |
10661 | ||
10662 | txc->write_onode(o); | |
10663 | } | |
10664 | ||
10665 | int BlueStore::_truncate(TransContext *txc, | |
10666 | CollectionRef& c, | |
10667 | OnodeRef& o, | |
10668 | uint64_t offset) | |
10669 | { | |
10670 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10671 | << " 0x" << std::hex << offset << std::dec | |
10672 | << dendl; | |
10673 | int r = 0; | |
10674 | if (offset >= OBJECT_MAX_SIZE) { | |
10675 | r = -E2BIG; | |
10676 | } else { | |
10677 | _do_truncate(txc, c, o, offset); | |
10678 | } | |
10679 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
10680 | << " 0x" << std::hex << offset << std::dec | |
10681 | << " = " << r << dendl; | |
10682 | return r; | |
10683 | } | |
10684 | ||
10685 | int BlueStore::_do_remove( | |
10686 | TransContext *txc, | |
10687 | CollectionRef& c, | |
10688 | OnodeRef o) | |
10689 | { | |
10690 | set<SharedBlob*> maybe_unshared_blobs; | |
10691 | bool is_gen = !o->oid.is_no_gen(); | |
10692 | _do_truncate(txc, c, o, 0, is_gen ? &maybe_unshared_blobs : nullptr); | |
10693 | if (o->onode.has_omap()) { | |
10694 | o->flush(); | |
10695 | _do_omap_clear(txc, o->onode.nid); | |
10696 | } | |
10697 | o->exists = false; | |
10698 | string key; | |
10699 | for (auto &s : o->extent_map.shards) { | |
10700 | dout(20) << __func__ << " removing shard 0x" << std::hex | |
10701 | << s.shard_info->offset << std::dec << dendl; | |
10702 | generate_extent_shard_key_and_apply(o->key, s.shard_info->offset, &key, | |
10703 | [&](const string& final_key) { | |
10704 | txc->t->rmkey(PREFIX_OBJ, final_key); | |
10705 | } | |
10706 | ); | |
10707 | } | |
10708 | txc->t->rmkey(PREFIX_OBJ, o->key.c_str(), o->key.size()); | |
10709 | txc->removed(o); | |
10710 | o->extent_map.clear(); | |
10711 | o->onode = bluestore_onode_t(); | |
10712 | _debug_obj_on_delete(o->oid); | |
10713 | ||
10714 | if (!is_gen || maybe_unshared_blobs.empty()) { | |
10715 | return 0; | |
10716 | } | |
10717 | ||
10718 | // see if we can unshare blobs still referenced by the head | |
10719 | dout(10) << __func__ << " gen and maybe_unshared_blobs " | |
10720 | << maybe_unshared_blobs << dendl; | |
10721 | ghobject_t nogen = o->oid; | |
10722 | nogen.generation = ghobject_t::NO_GEN; | |
10723 | OnodeRef h = c->onode_map.lookup(nogen); | |
10724 | ||
10725 | if (!h || !h->exists) { | |
10726 | return 0; | |
10727 | } | |
10728 | ||
10729 | dout(20) << __func__ << " checking for unshareable blobs on " << h | |
10730 | << " " << h->oid << dendl; | |
10731 | map<SharedBlob*,bluestore_extent_ref_map_t> expect; | |
10732 | for (auto& e : h->extent_map.extent_map) { | |
10733 | const bluestore_blob_t& b = e.blob->get_blob(); | |
10734 | SharedBlob *sb = e.blob->shared_blob.get(); | |
10735 | if (b.is_shared() && | |
10736 | sb->loaded && | |
10737 | maybe_unshared_blobs.count(sb)) { | |
10738 | if (b.is_compressed()) { | |
10739 | expect[sb].get(0, b.get_ondisk_length()); | |
10740 | } else { | |
10741 | b.map(e.blob_offset, e.length, [&](uint64_t off, uint64_t len) { | |
10742 | expect[sb].get(off, len); | |
10743 | return 0; | |
10744 | }); | |
10745 | } | |
10746 | } | |
10747 | } | |
10748 | ||
10749 | vector<SharedBlob*> unshared_blobs; | |
10750 | unshared_blobs.reserve(maybe_unshared_blobs.size()); | |
10751 | for (auto& p : expect) { | |
10752 | dout(20) << " ? " << *p.first << " vs " << p.second << dendl; | |
10753 | if (p.first->persistent->ref_map == p.second) { | |
10754 | SharedBlob *sb = p.first; | |
10755 | dout(20) << __func__ << " unsharing " << *sb << dendl; | |
10756 | unshared_blobs.push_back(sb); | |
10757 | txc->unshare_blob(sb); | |
10758 | uint64_t sbid = c->make_blob_unshared(sb); | |
10759 | string key; | |
10760 | get_shared_blob_key(sbid, &key); | |
10761 | txc->t->rmkey(PREFIX_SHARED_BLOB, key); | |
10762 | } | |
10763 | } | |
10764 | ||
10765 | if (unshared_blobs.empty()) { | |
10766 | return 0; | |
10767 | } | |
10768 | ||
10769 | for (auto& e : h->extent_map.extent_map) { | |
10770 | const bluestore_blob_t& b = e.blob->get_blob(); | |
10771 | SharedBlob *sb = e.blob->shared_blob.get(); | |
10772 | if (b.is_shared() && | |
10773 | std::find(unshared_blobs.begin(), unshared_blobs.end(), | |
10774 | sb) != unshared_blobs.end()) { | |
10775 | dout(20) << __func__ << " unsharing " << e << dendl; | |
10776 | bluestore_blob_t& blob = e.blob->dirty_blob(); | |
10777 | blob.clear_flag(bluestore_blob_t::FLAG_SHARED); | |
10778 | h->extent_map.dirty_range(e.logical_offset, 1); | |
10779 | } | |
10780 | } | |
10781 | txc->write_onode(h); | |
10782 | ||
10783 | return 0; | |
10784 | } | |
10785 | ||
10786 | int BlueStore::_remove(TransContext *txc, | |
10787 | CollectionRef& c, | |
10788 | OnodeRef &o) | |
10789 | { | |
10790 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
10791 | int r = _do_remove(txc, c, o); | |
10792 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
10793 | return r; | |
10794 | } | |
10795 | ||
10796 | int BlueStore::_setattr(TransContext *txc, | |
10797 | CollectionRef& c, | |
10798 | OnodeRef& o, | |
10799 | const string& name, | |
10800 | bufferptr& val) | |
10801 | { | |
10802 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10803 | << " " << name << " (" << val.length() << " bytes)" | |
10804 | << dendl; | |
10805 | int r = 0; | |
10806 | if (val.is_partial()) { | |
10807 | auto& b = o->onode.attrs[name.c_str()] = bufferptr(val.c_str(), | |
10808 | val.length()); | |
10809 | b.reassign_to_mempool(mempool::mempool_bluestore_cache_other); | |
10810 | } else { | |
10811 | auto& b = o->onode.attrs[name.c_str()] = val; | |
10812 | b.reassign_to_mempool(mempool::mempool_bluestore_cache_other); | |
10813 | } | |
10814 | txc->write_onode(o); | |
10815 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
10816 | << " " << name << " (" << val.length() << " bytes)" | |
10817 | << " = " << r << dendl; | |
10818 | return r; | |
10819 | } | |
10820 | ||
10821 | int BlueStore::_setattrs(TransContext *txc, | |
10822 | CollectionRef& c, | |
10823 | OnodeRef& o, | |
10824 | const map<string,bufferptr>& aset) | |
10825 | { | |
10826 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10827 | << " " << aset.size() << " keys" | |
10828 | << dendl; | |
10829 | int r = 0; | |
10830 | for (map<string,bufferptr>::const_iterator p = aset.begin(); | |
10831 | p != aset.end(); ++p) { | |
10832 | if (p->second.is_partial()) { | |
10833 | auto& b = o->onode.attrs[p->first.c_str()] = | |
10834 | bufferptr(p->second.c_str(), p->second.length()); | |
10835 | b.reassign_to_mempool(mempool::mempool_bluestore_cache_other); | |
10836 | } else { | |
10837 | auto& b = o->onode.attrs[p->first.c_str()] = p->second; | |
10838 | b.reassign_to_mempool(mempool::mempool_bluestore_cache_other); | |
10839 | } | |
10840 | } | |
10841 | txc->write_onode(o); | |
10842 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
10843 | << " " << aset.size() << " keys" | |
10844 | << " = " << r << dendl; | |
10845 | return r; | |
10846 | } | |
10847 | ||
10848 | ||
10849 | int BlueStore::_rmattr(TransContext *txc, | |
10850 | CollectionRef& c, | |
10851 | OnodeRef& o, | |
10852 | const string& name) | |
10853 | { | |
10854 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
10855 | << " " << name << dendl; | |
10856 | int r = 0; | |
10857 | auto it = o->onode.attrs.find(name.c_str()); | |
10858 | if (it == o->onode.attrs.end()) | |
10859 | goto out; | |
10860 | ||
10861 | o->onode.attrs.erase(it); | |
10862 | txc->write_onode(o); | |
10863 | ||
10864 | out: | |
10865 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
10866 | << " " << name << " = " << r << dendl; | |
10867 | return r; | |
10868 | } | |
10869 | ||
10870 | int BlueStore::_rmattrs(TransContext *txc, | |
10871 | CollectionRef& c, | |
10872 | OnodeRef& o) | |
10873 | { | |
10874 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
10875 | int r = 0; | |
10876 | ||
10877 | if (o->onode.attrs.empty()) | |
10878 | goto out; | |
10879 | ||
10880 | o->onode.attrs.clear(); | |
10881 | txc->write_onode(o); | |
10882 | ||
10883 | out: | |
10884 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
10885 | return r; | |
10886 | } | |
10887 | ||
10888 | void BlueStore::_do_omap_clear(TransContext *txc, uint64_t id) | |
10889 | { | |
10890 | KeyValueDB::Iterator it = db->get_iterator(PREFIX_OMAP); | |
10891 | string prefix, tail; | |
10892 | get_omap_header(id, &prefix); | |
10893 | get_omap_tail(id, &tail); | |
10894 | it->lower_bound(prefix); | |
10895 | while (it->valid()) { | |
10896 | if (it->key() >= tail) { | |
10897 | dout(30) << __func__ << " stop at " << pretty_binary_string(tail) | |
10898 | << dendl; | |
10899 | break; | |
10900 | } | |
10901 | txc->t->rmkey(PREFIX_OMAP, it->key()); | |
10902 | dout(30) << __func__ << " rm " << pretty_binary_string(it->key()) << dendl; | |
10903 | it->next(); | |
10904 | } | |
10905 | } | |
10906 | ||
10907 | int BlueStore::_omap_clear(TransContext *txc, | |
10908 | CollectionRef& c, | |
10909 | OnodeRef& o) | |
10910 | { | |
10911 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
10912 | int r = 0; | |
10913 | if (o->onode.has_omap()) { | |
10914 | o->flush(); | |
10915 | _do_omap_clear(txc, o->onode.nid); | |
10916 | o->onode.clear_omap_flag(); | |
10917 | txc->write_onode(o); | |
10918 | } | |
10919 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
10920 | return r; | |
10921 | } | |
10922 | ||
10923 | int BlueStore::_omap_setkeys(TransContext *txc, | |
10924 | CollectionRef& c, | |
10925 | OnodeRef& o, | |
10926 | bufferlist &bl) | |
10927 | { | |
10928 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
10929 | int r; | |
10930 | bufferlist::iterator p = bl.begin(); | |
10931 | __u32 num; | |
10932 | if (!o->onode.has_omap()) { | |
10933 | o->onode.set_omap_flag(); | |
10934 | txc->write_onode(o); | |
10935 | } else { | |
10936 | txc->note_modified_object(o); | |
10937 | } | |
10938 | string final_key; | |
10939 | _key_encode_u64(o->onode.nid, &final_key); | |
10940 | final_key.push_back('.'); | |
10941 | ::decode(num, p); | |
10942 | while (num--) { | |
10943 | string key; | |
10944 | bufferlist value; | |
10945 | ::decode(key, p); | |
10946 | ::decode(value, p); | |
10947 | final_key.resize(9); // keep prefix | |
10948 | final_key += key; | |
10949 | dout(30) << __func__ << " " << pretty_binary_string(final_key) | |
10950 | << " <- " << key << dendl; | |
10951 | txc->t->set(PREFIX_OMAP, final_key, value); | |
10952 | } | |
10953 | r = 0; | |
10954 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
10955 | return r; | |
10956 | } | |
10957 | ||
10958 | int BlueStore::_omap_setheader(TransContext *txc, | |
10959 | CollectionRef& c, | |
10960 | OnodeRef &o, | |
10961 | bufferlist& bl) | |
10962 | { | |
10963 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
10964 | int r; | |
10965 | string key; | |
10966 | if (!o->onode.has_omap()) { | |
10967 | o->onode.set_omap_flag(); | |
10968 | txc->write_onode(o); | |
10969 | } else { | |
10970 | txc->note_modified_object(o); | |
10971 | } | |
10972 | get_omap_header(o->onode.nid, &key); | |
10973 | txc->t->set(PREFIX_OMAP, key, bl); | |
10974 | r = 0; | |
10975 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
10976 | return r; | |
10977 | } | |
10978 | ||
10979 | int BlueStore::_omap_rmkeys(TransContext *txc, | |
10980 | CollectionRef& c, | |
10981 | OnodeRef& o, | |
10982 | bufferlist& bl) | |
10983 | { | |
10984 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
10985 | int r = 0; | |
10986 | bufferlist::iterator p = bl.begin(); | |
10987 | __u32 num; | |
10988 | string final_key; | |
10989 | ||
10990 | if (!o->onode.has_omap()) { | |
10991 | goto out; | |
10992 | } | |
10993 | _key_encode_u64(o->onode.nid, &final_key); | |
10994 | final_key.push_back('.'); | |
10995 | ::decode(num, p); | |
10996 | while (num--) { | |
10997 | string key; | |
10998 | ::decode(key, p); | |
10999 | final_key.resize(9); // keep prefix | |
11000 | final_key += key; | |
11001 | dout(30) << __func__ << " rm " << pretty_binary_string(final_key) | |
11002 | << " <- " << key << dendl; | |
11003 | txc->t->rmkey(PREFIX_OMAP, final_key); | |
11004 | } | |
11005 | txc->note_modified_object(o); | |
11006 | ||
11007 | out: | |
11008 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
11009 | return r; | |
11010 | } | |
11011 | ||
11012 | int BlueStore::_omap_rmkey_range(TransContext *txc, | |
11013 | CollectionRef& c, | |
11014 | OnodeRef& o, | |
11015 | const string& first, const string& last) | |
11016 | { | |
11017 | dout(15) << __func__ << " " << c->cid << " " << o->oid << dendl; | |
11018 | KeyValueDB::Iterator it; | |
11019 | string key_first, key_last; | |
11020 | int r = 0; | |
11021 | if (!o->onode.has_omap()) { | |
11022 | goto out; | |
11023 | } | |
11024 | o->flush(); | |
11025 | it = db->get_iterator(PREFIX_OMAP); | |
11026 | get_omap_key(o->onode.nid, first, &key_first); | |
11027 | get_omap_key(o->onode.nid, last, &key_last); | |
11028 | it->lower_bound(key_first); | |
11029 | while (it->valid()) { | |
11030 | if (it->key() >= key_last) { | |
11031 | dout(30) << __func__ << " stop at " << pretty_binary_string(key_last) | |
11032 | << dendl; | |
11033 | break; | |
11034 | } | |
11035 | txc->t->rmkey(PREFIX_OMAP, it->key()); | |
11036 | dout(30) << __func__ << " rm " << pretty_binary_string(it->key()) << dendl; | |
11037 | it->next(); | |
11038 | } | |
11039 | txc->note_modified_object(o); | |
11040 | ||
11041 | out: | |
11042 | dout(10) << __func__ << " " << c->cid << " " << o->oid << " = " << r << dendl; | |
11043 | return r; | |
11044 | } | |
11045 | ||
11046 | int BlueStore::_set_alloc_hint( | |
11047 | TransContext *txc, | |
11048 | CollectionRef& c, | |
11049 | OnodeRef& o, | |
11050 | uint64_t expected_object_size, | |
11051 | uint64_t expected_write_size, | |
11052 | uint32_t flags) | |
11053 | { | |
11054 | dout(15) << __func__ << " " << c->cid << " " << o->oid | |
11055 | << " object_size " << expected_object_size | |
11056 | << " write_size " << expected_write_size | |
11057 | << " flags " << ceph_osd_alloc_hint_flag_string(flags) | |
11058 | << dendl; | |
11059 | int r = 0; | |
11060 | o->onode.expected_object_size = expected_object_size; | |
11061 | o->onode.expected_write_size = expected_write_size; | |
11062 | o->onode.alloc_hint_flags = flags; | |
11063 | txc->write_onode(o); | |
11064 | dout(10) << __func__ << " " << c->cid << " " << o->oid | |
11065 | << " object_size " << expected_object_size | |
11066 | << " write_size " << expected_write_size | |
11067 | << " flags " << ceph_osd_alloc_hint_flag_string(flags) | |
11068 | << " = " << r << dendl; | |
11069 | return r; | |
11070 | } | |
11071 | ||
11072 | int BlueStore::_clone(TransContext *txc, | |
11073 | CollectionRef& c, | |
11074 | OnodeRef& oldo, | |
11075 | OnodeRef& newo) | |
11076 | { | |
11077 | dout(15) << __func__ << " " << c->cid << " " << oldo->oid << " -> " | |
11078 | << newo->oid << dendl; | |
11079 | int r = 0; | |
11080 | if (oldo->oid.hobj.get_hash() != newo->oid.hobj.get_hash()) { | |
11081 | derr << __func__ << " mismatched hash on " << oldo->oid | |
11082 | << " and " << newo->oid << dendl; | |
11083 | return -EINVAL; | |
11084 | } | |
11085 | ||
11086 | _assign_nid(txc, newo); | |
11087 | ||
11088 | // clone data | |
11089 | oldo->flush(); | |
11090 | _do_truncate(txc, c, newo, 0); | |
11091 | if (cct->_conf->bluestore_clone_cow) { | |
11092 | _do_clone_range(txc, c, oldo, newo, 0, oldo->onode.size, 0); | |
11093 | } else { | |
11094 | bufferlist bl; | |
11095 | r = _do_read(c.get(), oldo, 0, oldo->onode.size, bl, 0); | |
11096 | if (r < 0) | |
11097 | goto out; | |
11098 | r = _do_write(txc, c, newo, 0, oldo->onode.size, bl, 0); | |
11099 | if (r < 0) | |
11100 | goto out; | |
11101 | } | |
11102 | ||
11103 | // clone attrs | |
11104 | newo->onode.attrs = oldo->onode.attrs; | |
11105 | ||
11106 | // clone omap | |
11107 | if (newo->onode.has_omap()) { | |
11108 | dout(20) << __func__ << " clearing old omap data" << dendl; | |
11109 | newo->flush(); | |
11110 | _do_omap_clear(txc, newo->onode.nid); | |
11111 | } | |
11112 | if (oldo->onode.has_omap()) { | |
11113 | dout(20) << __func__ << " copying omap data" << dendl; | |
11114 | if (!newo->onode.has_omap()) { | |
11115 | newo->onode.set_omap_flag(); | |
11116 | } | |
11117 | KeyValueDB::Iterator it = db->get_iterator(PREFIX_OMAP); | |
11118 | string head, tail; | |
11119 | get_omap_header(oldo->onode.nid, &head); | |
11120 | get_omap_tail(oldo->onode.nid, &tail); | |
11121 | it->lower_bound(head); | |
11122 | while (it->valid()) { | |
11123 | if (it->key() >= tail) { | |
11124 | dout(30) << __func__ << " reached tail" << dendl; | |
11125 | break; | |
11126 | } else { | |
11127 | dout(30) << __func__ << " got header/data " | |
11128 | << pretty_binary_string(it->key()) << dendl; | |
11129 | string key; | |
11130 | rewrite_omap_key(newo->onode.nid, it->key(), &key); | |
11131 | txc->t->set(PREFIX_OMAP, key, it->value()); | |
11132 | } | |
11133 | it->next(); | |
11134 | } | |
11135 | } else { | |
11136 | newo->onode.clear_omap_flag(); | |
11137 | } | |
11138 | ||
11139 | txc->write_onode(newo); | |
11140 | r = 0; | |
11141 | ||
11142 | out: | |
11143 | dout(10) << __func__ << " " << c->cid << " " << oldo->oid << " -> " | |
11144 | << newo->oid << " = " << r << dendl; | |
11145 | return r; | |
11146 | } | |
11147 | ||
11148 | int BlueStore::_do_clone_range( | |
11149 | TransContext *txc, | |
11150 | CollectionRef& c, | |
11151 | OnodeRef& oldo, | |
11152 | OnodeRef& newo, | |
11153 | uint64_t srcoff, | |
11154 | uint64_t length, | |
11155 | uint64_t dstoff) | |
11156 | { | |
11157 | dout(15) << __func__ << " " << c->cid << " " << oldo->oid << " -> " | |
11158 | << newo->oid | |
11159 | << " 0x" << std::hex << srcoff << "~" << length << " -> " | |
11160 | << " 0x" << dstoff << "~" << length << std::dec << dendl; | |
11161 | oldo->extent_map.fault_range(db, srcoff, length); | |
11162 | newo->extent_map.fault_range(db, dstoff, length); | |
11163 | _dump_onode(oldo); | |
11164 | _dump_onode(newo); | |
11165 | ||
11166 | // hmm, this could go into an ExtentMap::dup() method. | |
11167 | vector<BlobRef> id_to_blob(oldo->extent_map.extent_map.size()); | |
11168 | for (auto &e : oldo->extent_map.extent_map) { | |
11169 | e.blob->last_encoded_id = -1; | |
11170 | } | |
11171 | int n = 0; | |
11172 | uint64_t end = srcoff + length; | |
11173 | uint32_t dirty_range_begin = 0; | |
11174 | uint32_t dirty_range_end = 0; | |
11175 | bool src_dirty = false; | |
11176 | for (auto ep = oldo->extent_map.seek_lextent(srcoff); | |
11177 | ep != oldo->extent_map.extent_map.end(); | |
11178 | ++ep) { | |
11179 | auto& e = *ep; | |
11180 | if (e.logical_offset >= end) { | |
11181 | break; | |
11182 | } | |
11183 | dout(20) << __func__ << " src " << e << dendl; | |
11184 | BlobRef cb; | |
11185 | bool blob_duped = true; | |
11186 | if (e.blob->last_encoded_id >= 0) { | |
11187 | // blob is already duped | |
11188 | cb = id_to_blob[e.blob->last_encoded_id]; | |
11189 | blob_duped = false; | |
11190 | } else { | |
11191 | // dup the blob | |
11192 | const bluestore_blob_t& blob = e.blob->get_blob(); | |
11193 | // make sure it is shared | |
11194 | if (!blob.is_shared()) { | |
11195 | c->make_blob_shared(_assign_blobid(txc), e.blob); | |
11196 | if (!src_dirty) { | |
11197 | src_dirty = true; | |
11198 | dirty_range_begin = e.logical_offset; | |
11199 | } | |
11200 | assert(e.logical_end() > 0); | |
11201 | // -1 to exclude next potential shard | |
11202 | dirty_range_end = e.logical_end() - 1; | |
11203 | } else { | |
11204 | c->load_shared_blob(e.blob->shared_blob); | |
11205 | } | |
11206 | cb = new Blob(); | |
11207 | e.blob->last_encoded_id = n; | |
11208 | id_to_blob[n] = cb; | |
11209 | e.blob->dup(*cb); | |
11210 | // bump the extent refs on the copied blob's extents | |
11211 | for (auto p : blob.get_extents()) { | |
11212 | if (p.is_valid()) { | |
11213 | e.blob->shared_blob->get_ref(p.offset, p.length); | |
11214 | } | |
11215 | } | |
11216 | txc->write_shared_blob(e.blob->shared_blob); | |
11217 | dout(20) << __func__ << " new " << *cb << dendl; | |
11218 | } | |
11219 | // dup extent | |
11220 | int skip_front, skip_back; | |
11221 | if (e.logical_offset < srcoff) { | |
11222 | skip_front = srcoff - e.logical_offset; | |
11223 | } else { | |
11224 | skip_front = 0; | |
11225 | } | |
11226 | if (e.logical_end() > end) { | |
11227 | skip_back = e.logical_end() - end; | |
11228 | } else { | |
11229 | skip_back = 0; | |
11230 | } | |
11231 | Extent *ne = new Extent(e.logical_offset + skip_front + dstoff - srcoff, | |
11232 | e.blob_offset + skip_front, | |
11233 | e.length - skip_front - skip_back, cb); | |
11234 | newo->extent_map.extent_map.insert(*ne); | |
11235 | ne->blob->get_ref(c.get(), ne->blob_offset, ne->length); | |
11236 | // fixme: we may leave parts of new blob unreferenced that could | |
11237 | // be freed (relative to the shared_blob). | |
11238 | txc->statfs_delta.stored() += ne->length; | |
11239 | if (e.blob->get_blob().is_compressed()) { | |
11240 | txc->statfs_delta.compressed_original() += ne->length; | |
11241 | if (blob_duped){ | |
11242 | txc->statfs_delta.compressed() += | |
11243 | cb->get_blob().get_compressed_payload_length(); | |
11244 | } | |
11245 | } | |
11246 | dout(20) << __func__ << " dst " << *ne << dendl; | |
11247 | ++n; | |
11248 | } | |
11249 | if (src_dirty) { | |
11250 | oldo->extent_map.dirty_range(dirty_range_begin, | |
11251 | dirty_range_end - dirty_range_begin); | |
11252 | txc->write_onode(oldo); | |
11253 | } | |
11254 | txc->write_onode(newo); | |
11255 | ||
11256 | if (dstoff + length > newo->onode.size) { | |
11257 | newo->onode.size = dstoff + length; | |
11258 | } | |
11259 | newo->extent_map.dirty_range(dstoff, length); | |
11260 | _dump_onode(oldo); | |
11261 | _dump_onode(newo); | |
11262 | return 0; | |
11263 | } | |
11264 | ||
11265 | int BlueStore::_clone_range(TransContext *txc, | |
11266 | CollectionRef& c, | |
11267 | OnodeRef& oldo, | |
11268 | OnodeRef& newo, | |
11269 | uint64_t srcoff, uint64_t length, uint64_t dstoff) | |
11270 | { | |
11271 | dout(15) << __func__ << " " << c->cid << " " << oldo->oid << " -> " | |
11272 | << newo->oid << " from 0x" << std::hex << srcoff << "~" << length | |
11273 | << " to offset 0x" << dstoff << std::dec << dendl; | |
11274 | int r = 0; | |
11275 | ||
11276 | if (srcoff + length >= OBJECT_MAX_SIZE || | |
11277 | dstoff + length >= OBJECT_MAX_SIZE) { | |
11278 | r = -E2BIG; | |
11279 | goto out; | |
11280 | } | |
11281 | if (srcoff + length > oldo->onode.size) { | |
11282 | r = -EINVAL; | |
11283 | goto out; | |
11284 | } | |
11285 | ||
11286 | _assign_nid(txc, newo); | |
11287 | ||
11288 | if (length > 0) { | |
11289 | if (cct->_conf->bluestore_clone_cow) { | |
11290 | _do_zero(txc, c, newo, dstoff, length); | |
11291 | _do_clone_range(txc, c, oldo, newo, srcoff, length, dstoff); | |
11292 | } else { | |
11293 | bufferlist bl; | |
11294 | r = _do_read(c.get(), oldo, srcoff, length, bl, 0); | |
11295 | if (r < 0) | |
11296 | goto out; | |
11297 | r = _do_write(txc, c, newo, dstoff, bl.length(), bl, 0); | |
11298 | if (r < 0) | |
11299 | goto out; | |
11300 | } | |
11301 | } | |
11302 | ||
11303 | txc->write_onode(newo); | |
11304 | r = 0; | |
11305 | ||
11306 | out: | |
11307 | dout(10) << __func__ << " " << c->cid << " " << oldo->oid << " -> " | |
11308 | << newo->oid << " from 0x" << std::hex << srcoff << "~" << length | |
11309 | << " to offset 0x" << dstoff << std::dec | |
11310 | << " = " << r << dendl; | |
11311 | return r; | |
11312 | } | |
11313 | ||
11314 | int BlueStore::_rename(TransContext *txc, | |
11315 | CollectionRef& c, | |
11316 | OnodeRef& oldo, | |
11317 | OnodeRef& newo, | |
11318 | const ghobject_t& new_oid) | |
11319 | { | |
11320 | dout(15) << __func__ << " " << c->cid << " " << oldo->oid << " -> " | |
11321 | << new_oid << dendl; | |
11322 | int r; | |
11323 | ghobject_t old_oid = oldo->oid; | |
11324 | mempool::bluestore_cache_other::string new_okey; | |
11325 | ||
11326 | if (newo) { | |
11327 | if (newo->exists) { | |
11328 | r = -EEXIST; | |
11329 | goto out; | |
11330 | } | |
11331 | assert(txc->onodes.count(newo) == 0); | |
11332 | } | |
11333 | ||
11334 | txc->t->rmkey(PREFIX_OBJ, oldo->key.c_str(), oldo->key.size()); | |
11335 | ||
11336 | // rewrite shards | |
11337 | { | |
11338 | oldo->extent_map.fault_range(db, 0, oldo->onode.size); | |
11339 | get_object_key(cct, new_oid, &new_okey); | |
11340 | string key; | |
11341 | for (auto &s : oldo->extent_map.shards) { | |
11342 | generate_extent_shard_key_and_apply(oldo->key, s.shard_info->offset, &key, | |
11343 | [&](const string& final_key) { | |
11344 | txc->t->rmkey(PREFIX_OBJ, final_key); | |
11345 | } | |
11346 | ); | |
11347 | s.dirty = true; | |
11348 | } | |
11349 | } | |
11350 | ||
11351 | newo = oldo; | |
11352 | txc->write_onode(newo); | |
11353 | ||
11354 | // this adjusts oldo->{oid,key}, and reset oldo to a fresh empty | |
11355 | // Onode in the old slot | |
11356 | c->onode_map.rename(oldo, old_oid, new_oid, new_okey); | |
11357 | r = 0; | |
11358 | ||
11359 | out: | |
11360 | dout(10) << __func__ << " " << c->cid << " " << old_oid << " -> " | |
11361 | << new_oid << " = " << r << dendl; | |
11362 | return r; | |
11363 | } | |
11364 | ||
11365 | // collections | |
11366 | ||
11367 | int BlueStore::_create_collection( | |
11368 | TransContext *txc, | |
11369 | const coll_t &cid, | |
11370 | unsigned bits, | |
11371 | CollectionRef *c) | |
11372 | { | |
11373 | dout(15) << __func__ << " " << cid << " bits " << bits << dendl; | |
11374 | int r; | |
11375 | bufferlist bl; | |
11376 | ||
11377 | { | |
11378 | RWLock::WLocker l(coll_lock); | |
11379 | if (*c) { | |
11380 | r = -EEXIST; | |
11381 | goto out; | |
11382 | } | |
11383 | c->reset( | |
11384 | new Collection( | |
11385 | this, | |
11386 | cache_shards[cid.hash_to_shard(cache_shards.size())], | |
11387 | cid)); | |
11388 | (*c)->cnode.bits = bits; | |
11389 | coll_map[cid] = *c; | |
11390 | } | |
11391 | ::encode((*c)->cnode, bl); | |
11392 | txc->t->set(PREFIX_COLL, stringify(cid), bl); | |
11393 | r = 0; | |
11394 | ||
11395 | out: | |
11396 | dout(10) << __func__ << " " << cid << " bits " << bits << " = " << r << dendl; | |
11397 | return r; | |
11398 | } | |
11399 | ||
11400 | int BlueStore::_remove_collection(TransContext *txc, const coll_t &cid, | |
11401 | CollectionRef *c) | |
11402 | { | |
11403 | dout(15) << __func__ << " " << cid << dendl; | |
11404 | int r; | |
11405 | ||
11406 | { | |
11407 | RWLock::WLocker l(coll_lock); | |
11408 | if (!*c) { | |
11409 | r = -ENOENT; | |
11410 | goto out; | |
11411 | } | |
11412 | size_t nonexistent_count = 0; | |
11413 | assert((*c)->exists); | |
11414 | if ((*c)->onode_map.map_any([&](OnodeRef o) { | |
11415 | if (o->exists) { | |
11416 | dout(10) << __func__ << " " << o->oid << " " << o | |
11417 | << " exists in onode_map" << dendl; | |
11418 | return true; | |
11419 | } | |
11420 | ++nonexistent_count; | |
11421 | return false; | |
11422 | })) { | |
11423 | r = -ENOTEMPTY; | |
11424 | goto out; | |
11425 | } | |
11426 | ||
11427 | vector<ghobject_t> ls; | |
11428 | ghobject_t next; | |
11429 | // Enumerate onodes in db, up to nonexistent_count + 1 | |
11430 | // then check if all of them are marked as non-existent. | |
11431 | // Bypass the check if returned number is greater than nonexistent_count | |
11432 | r = _collection_list(c->get(), ghobject_t(), ghobject_t::get_max(), | |
11433 | nonexistent_count + 1, &ls, &next); | |
11434 | if (r >= 0) { | |
11435 | bool exists = false; //ls.size() > nonexistent_count; | |
11436 | for (auto it = ls.begin(); !exists && it < ls.end(); ++it) { | |
11437 | dout(10) << __func__ << " oid " << *it << dendl; | |
11438 | auto onode = (*c)->onode_map.lookup(*it); | |
11439 | exists = !onode || onode->exists; | |
11440 | if (exists) { | |
11441 | dout(10) << __func__ << " " << *it | |
11442 | << " exists in db" << dendl; | |
11443 | } | |
11444 | } | |
11445 | if (!exists) { | |
11446 | coll_map.erase(cid); | |
11447 | txc->removed_collections.push_back(*c); | |
11448 | (*c)->exists = false; | |
11449 | c->reset(); | |
11450 | txc->t->rmkey(PREFIX_COLL, stringify(cid)); | |
11451 | r = 0; | |
11452 | } else { | |
11453 | dout(10) << __func__ << " " << cid | |
11454 | << " is non-empty" << dendl; | |
11455 | r = -ENOTEMPTY; | |
11456 | } | |
11457 | } | |
11458 | } | |
11459 | ||
11460 | out: | |
11461 | dout(10) << __func__ << " " << cid << " = " << r << dendl; | |
11462 | return r; | |
11463 | } | |
11464 | ||
11465 | int BlueStore::_split_collection(TransContext *txc, | |
11466 | CollectionRef& c, | |
11467 | CollectionRef& d, | |
11468 | unsigned bits, int rem) | |
11469 | { | |
11470 | dout(15) << __func__ << " " << c->cid << " to " << d->cid << " " | |
11471 | << " bits " << bits << dendl; | |
11472 | RWLock::WLocker l(c->lock); | |
11473 | RWLock::WLocker l2(d->lock); | |
11474 | int r; | |
11475 | ||
11476 | // flush all previous deferred writes on this sequencer. this is a bit | |
11477 | // heavyweight, but we need to make sure all deferred writes complete | |
11478 | // before we split as the new collection's sequencer may need to order | |
11479 | // this after those writes, and we don't bother with the complexity of | |
11480 | // moving those TransContexts over to the new osr. | |
11481 | _osr_drain_preceding(txc); | |
11482 | ||
11483 | // move any cached items (onodes and referenced shared blobs) that will | |
11484 | // belong to the child collection post-split. leave everything else behind. | |
11485 | // this may include things that don't strictly belong to the now-smaller | |
11486 | // parent split, but the OSD will always send us a split for every new | |
11487 | // child. | |
11488 | ||
11489 | spg_t pgid, dest_pgid; | |
11490 | bool is_pg = c->cid.is_pg(&pgid); | |
11491 | assert(is_pg); | |
11492 | is_pg = d->cid.is_pg(&dest_pgid); | |
11493 | assert(is_pg); | |
11494 | ||
11495 | // the destination should initially be empty. | |
11496 | assert(d->onode_map.empty()); | |
11497 | assert(d->shared_blob_set.empty()); | |
11498 | assert(d->cnode.bits == bits); | |
11499 | ||
11500 | c->split_cache(d.get()); | |
11501 | ||
11502 | // adjust bits. note that this will be redundant for all but the first | |
11503 | // split call for this parent (first child). | |
11504 | c->cnode.bits = bits; | |
11505 | assert(d->cnode.bits == bits); | |
11506 | r = 0; | |
11507 | ||
11508 | bufferlist bl; | |
11509 | ::encode(c->cnode, bl); | |
11510 | txc->t->set(PREFIX_COLL, stringify(c->cid), bl); | |
11511 | ||
11512 | dout(10) << __func__ << " " << c->cid << " to " << d->cid << " " | |
11513 | << " bits " << bits << " = " << r << dendl; | |
11514 | return r; | |
11515 | } | |
11516 | ||
11517 | // DB key value Histogram | |
11518 | #define KEY_SLAB 32 | |
11519 | #define VALUE_SLAB 64 | |
11520 | ||
11521 | const string prefix_onode = "o"; | |
11522 | const string prefix_onode_shard = "x"; | |
11523 | const string prefix_other = "Z"; | |
11524 | ||
11525 | int BlueStore::DBHistogram::get_key_slab(size_t sz) | |
11526 | { | |
11527 | return (sz/KEY_SLAB); | |
11528 | } | |
11529 | ||
11530 | string BlueStore::DBHistogram::get_key_slab_to_range(int slab) | |
11531 | { | |
11532 | int lower_bound = slab * KEY_SLAB; | |
11533 | int upper_bound = (slab + 1) * KEY_SLAB; | |
11534 | string ret = "[" + stringify(lower_bound) + "," + stringify(upper_bound) + ")"; | |
11535 | return ret; | |
11536 | } | |
11537 | ||
11538 | int BlueStore::DBHistogram::get_value_slab(size_t sz) | |
11539 | { | |
11540 | return (sz/VALUE_SLAB); | |
11541 | } | |
11542 | ||
11543 | string BlueStore::DBHistogram::get_value_slab_to_range(int slab) | |
11544 | { | |
11545 | int lower_bound = slab * VALUE_SLAB; | |
11546 | int upper_bound = (slab + 1) * VALUE_SLAB; | |
11547 | string ret = "[" + stringify(lower_bound) + "," + stringify(upper_bound) + ")"; | |
11548 | return ret; | |
11549 | } | |
11550 | ||
11551 | void BlueStore::DBHistogram::update_hist_entry(map<string, map<int, struct key_dist> > &key_hist, | |
11552 | const string &prefix, size_t key_size, size_t value_size) | |
11553 | { | |
11554 | uint32_t key_slab = get_key_slab(key_size); | |
11555 | uint32_t value_slab = get_value_slab(value_size); | |
11556 | key_hist[prefix][key_slab].count++; | |
11557 | key_hist[prefix][key_slab].max_len = MAX(key_size, key_hist[prefix][key_slab].max_len); | |
11558 | key_hist[prefix][key_slab].val_map[value_slab].count++; | |
11559 | key_hist[prefix][key_slab].val_map[value_slab].max_len = | |
11560 | MAX(value_size, key_hist[prefix][key_slab].val_map[value_slab].max_len); | |
11561 | } | |
11562 | ||
11563 | void BlueStore::DBHistogram::dump(Formatter *f) | |
11564 | { | |
11565 | f->open_object_section("rocksdb_value_distribution"); | |
11566 | for (auto i : value_hist) { | |
11567 | f->dump_unsigned(get_value_slab_to_range(i.first).data(), i.second); | |
11568 | } | |
11569 | f->close_section(); | |
11570 | ||
11571 | f->open_object_section("rocksdb_key_value_histogram"); | |
11572 | for (auto i : key_hist) { | |
11573 | f->dump_string("prefix", i.first); | |
11574 | f->open_object_section("key_hist"); | |
11575 | for ( auto k : i.second) { | |
11576 | f->dump_unsigned(get_key_slab_to_range(k.first).data(), k.second.count); | |
11577 | f->dump_unsigned("max_len", k.second.max_len); | |
11578 | f->open_object_section("value_hist"); | |
11579 | for ( auto j : k.second.val_map) { | |
11580 | f->dump_unsigned(get_value_slab_to_range(j.first).data(), j.second.count); | |
11581 | f->dump_unsigned("max_len", j.second.max_len); | |
11582 | } | |
11583 | f->close_section(); | |
11584 | } | |
11585 | f->close_section(); | |
11586 | } | |
11587 | f->close_section(); | |
11588 | } | |
11589 | ||
11590 | //Itrerates through the db and collects the stats | |
11591 | void BlueStore::generate_db_histogram(Formatter *f) | |
11592 | { | |
11593 | //globals | |
11594 | uint64_t num_onodes = 0; | |
11595 | uint64_t num_shards = 0; | |
11596 | uint64_t num_super = 0; | |
11597 | uint64_t num_coll = 0; | |
11598 | uint64_t num_omap = 0; | |
11599 | uint64_t num_deferred = 0; | |
11600 | uint64_t num_alloc = 0; | |
11601 | uint64_t num_stat = 0; | |
11602 | uint64_t num_others = 0; | |
11603 | uint64_t num_shared_shards = 0; | |
11604 | size_t max_key_size =0, max_value_size = 0; | |
11605 | uint64_t total_key_size = 0, total_value_size = 0; | |
11606 | size_t key_size = 0, value_size = 0; | |
11607 | DBHistogram hist; | |
11608 | ||
11609 | utime_t start = ceph_clock_now(); | |
11610 | ||
11611 | KeyValueDB::WholeSpaceIterator iter = db->get_iterator(); | |
11612 | iter->seek_to_first(); | |
11613 | while (iter->valid()) { | |
11614 | dout(30) << __func__ << " Key: " << iter->key() << dendl; | |
11615 | key_size = iter->key_size(); | |
11616 | value_size = iter->value_size(); | |
11617 | hist.value_hist[hist.get_value_slab(value_size)]++; | |
11618 | max_key_size = MAX(max_key_size, key_size); | |
11619 | max_value_size = MAX(max_value_size, value_size); | |
11620 | total_key_size += key_size; | |
11621 | total_value_size += value_size; | |
11622 | ||
11623 | pair<string,string> key(iter->raw_key()); | |
11624 | ||
11625 | if (key.first == PREFIX_SUPER) { | |
11626 | hist.update_hist_entry(hist.key_hist, PREFIX_SUPER, key_size, value_size); | |
11627 | num_super++; | |
11628 | } else if (key.first == PREFIX_STAT) { | |
11629 | hist.update_hist_entry(hist.key_hist, PREFIX_STAT, key_size, value_size); | |
11630 | num_stat++; | |
11631 | } else if (key.first == PREFIX_COLL) { | |
11632 | hist.update_hist_entry(hist.key_hist, PREFIX_COLL, key_size, value_size); | |
11633 | num_coll++; | |
11634 | } else if (key.first == PREFIX_OBJ) { | |
11635 | if (key.second.back() == ONODE_KEY_SUFFIX) { | |
11636 | hist.update_hist_entry(hist.key_hist, prefix_onode, key_size, value_size); | |
11637 | num_onodes++; | |
11638 | } else { | |
11639 | hist.update_hist_entry(hist.key_hist, prefix_onode_shard, key_size, value_size); | |
11640 | num_shards++; | |
11641 | } | |
11642 | } else if (key.first == PREFIX_OMAP) { | |
11643 | hist.update_hist_entry(hist.key_hist, PREFIX_OMAP, key_size, value_size); | |
11644 | num_omap++; | |
11645 | } else if (key.first == PREFIX_DEFERRED) { | |
11646 | hist.update_hist_entry(hist.key_hist, PREFIX_DEFERRED, key_size, value_size); | |
11647 | num_deferred++; | |
11648 | } else if (key.first == PREFIX_ALLOC || key.first == "b" ) { | |
11649 | hist.update_hist_entry(hist.key_hist, PREFIX_ALLOC, key_size, value_size); | |
11650 | num_alloc++; | |
11651 | } else if (key.first == PREFIX_SHARED_BLOB) { | |
11652 | hist.update_hist_entry(hist.key_hist, PREFIX_SHARED_BLOB, key_size, value_size); | |
11653 | num_shared_shards++; | |
11654 | } else { | |
11655 | hist.update_hist_entry(hist.key_hist, prefix_other, key_size, value_size); | |
11656 | num_others++; | |
11657 | } | |
11658 | iter->next(); | |
11659 | } | |
11660 | ||
11661 | utime_t duration = ceph_clock_now() - start; | |
11662 | f->open_object_section("rocksdb_key_value_stats"); | |
11663 | f->dump_unsigned("num_onodes", num_onodes); | |
11664 | f->dump_unsigned("num_shards", num_shards); | |
11665 | f->dump_unsigned("num_super", num_super); | |
11666 | f->dump_unsigned("num_coll", num_coll); | |
11667 | f->dump_unsigned("num_omap", num_omap); | |
11668 | f->dump_unsigned("num_deferred", num_deferred); | |
11669 | f->dump_unsigned("num_alloc", num_alloc); | |
11670 | f->dump_unsigned("num_stat", num_stat); | |
11671 | f->dump_unsigned("num_shared_shards", num_shared_shards); | |
11672 | f->dump_unsigned("num_others", num_others); | |
11673 | f->dump_unsigned("max_key_size", max_key_size); | |
11674 | f->dump_unsigned("max_value_size", max_value_size); | |
11675 | f->dump_unsigned("total_key_size", total_key_size); | |
11676 | f->dump_unsigned("total_value_size", total_value_size); | |
11677 | f->close_section(); | |
11678 | ||
11679 | hist.dump(f); | |
11680 | ||
11681 | dout(20) << __func__ << " finished in " << duration << " seconds" << dendl; | |
11682 | ||
11683 | } | |
11684 | ||
11685 | void BlueStore::_flush_cache() | |
11686 | { | |
11687 | dout(10) << __func__ << dendl; | |
11688 | for (auto i : cache_shards) { | |
11689 | i->trim_all(); | |
11690 | assert(i->empty()); | |
11691 | } | |
11692 | for (auto& p : coll_map) { | |
11693 | if (!p.second->onode_map.empty()) { | |
11694 | derr << __func__ << "stray onodes on " << p.first << dendl; | |
11695 | p.second->onode_map.dump(cct, 0); | |
11696 | } | |
11697 | if (!p.second->shared_blob_set.empty()) { | |
11698 | derr << __func__ << " stray shared blobs on " << p.first << dendl; | |
11699 | p.second->shared_blob_set.dump(cct, 0); | |
11700 | } | |
11701 | assert(p.second->onode_map.empty()); | |
11702 | assert(p.second->shared_blob_set.empty()); | |
11703 | } | |
11704 | coll_map.clear(); | |
11705 | } | |
11706 | ||
11707 | // For external caller. | |
11708 | // We use a best-effort policy instead, e.g., | |
11709 | // we don't care if there are still some pinned onodes/data in the cache | |
11710 | // after this command is completed. | |
11711 | void BlueStore::flush_cache() | |
11712 | { | |
11713 | dout(10) << __func__ << dendl; | |
11714 | for (auto i : cache_shards) { | |
11715 | i->trim_all(); | |
11716 | } | |
11717 | } | |
11718 | ||
11719 | void BlueStore::_apply_padding(uint64_t head_pad, | |
11720 | uint64_t tail_pad, | |
11721 | bufferlist& padded) | |
11722 | { | |
11723 | if (head_pad) { | |
11724 | padded.prepend_zero(head_pad); | |
11725 | } | |
11726 | if (tail_pad) { | |
11727 | padded.append_zero(tail_pad); | |
11728 | } | |
11729 | if (head_pad || tail_pad) { | |
11730 | dout(20) << __func__ << " can pad head 0x" << std::hex << head_pad | |
11731 | << " tail 0x" << tail_pad << std::dec << dendl; | |
11732 | logger->inc(l_bluestore_write_pad_bytes, head_pad + tail_pad); | |
11733 | } | |
11734 | } | |
11735 | ||
11736 | // =========================================== |