]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/SloppyCRCMap.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / SloppyCRCMap.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_COMMON_SLOPPYCRCMAP_H
5 #define CEPH_COMMON_SLOPPYCRCMAP_H
6
7 #include "include/encoding.h"
8 #include "common/Formatter.h"
9
10 #include <list>
11 #include <map>
12 #include <ostream>
13
14 /**
15 * SloppyCRCMap
16 *
17 * Opportunistically track CRCs on any reads or writes that cover full
18 * blocks. Verify read results when we have CRC data available for
19 * the given extent.
20 */
21 class SloppyCRCMap {
22 static const int crc_iv = 0xffffffff;
23
24 std::map<uint64_t, uint32_t> crc_map; // offset -> crc(-1)
25 uint32_t block_size;
26 uint32_t zero_crc;
27
28 public:
29 SloppyCRCMap(uint32_t b=0) {
30 set_block_size(b);
31 }
32
33 void set_block_size(uint32_t b) {
34 block_size = b;
35 //zero_crc = ceph_crc32c(0xffffffff, NULL, block_size);
36 if (b) {
37 bufferlist bl;
38 bl.append_zero(block_size);
39 zero_crc = bl.crc32c(crc_iv);
40 } else {
41 zero_crc = crc_iv;
42 }
43 }
44
45 /// update based on a write
46 void write(uint64_t offset, uint64_t len, const bufferlist& bl,
47 std::ostream *out = NULL);
48
49 /// update based on a truncate
50 void truncate(uint64_t offset);
51
52 /// update based on a zero/punch_hole
53 void zero(uint64_t offset, uint64_t len);
54
55 /// update based on a zero/punch_hole
56 void clone_range(uint64_t offset, uint64_t len, uint64_t srcoff, const SloppyCRCMap& src,
57 std::ostream *out = NULL);
58
59 /**
60 * validate a read result
61 *
62 * @param offset offset
63 * @param length length
64 * @param bl data read
65 * @param err option ostream to describe errors in detail
66 * @returns error count, 0 for success
67 */
68 int read(uint64_t offset, uint64_t len, const bufferlist& bl, std::ostream *err);
69
70 void encode(bufferlist& bl) const;
71 void decode(bufferlist::iterator& bl);
72 void dump(Formatter *f) const;
73 static void generate_test_instances(std::list<SloppyCRCMap*>& ls);
74 };
75 WRITE_CLASS_ENCODER(SloppyCRCMap)
76
77 #endif