Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 /**
11  * SloppyCRCMap
12  *
13  * Opportunistically track CRCs on any reads or writes that cover full
14  * blocks.  Verify read results when we have CRC data available for
15  * the given extent.
16  */
17 class SloppyCRCMap {
18   static const int crc_iv = 0xffffffff;
19
20   std::map<uint64_t, uint32_t> crc_map;  // offset -> crc(-1)
21   uint32_t block_size;
22   uint32_t zero_crc;
23
24 public:
25   SloppyCRCMap(uint32_t b=0) {
26     set_block_size(b);
27   }
28
29   void set_block_size(uint32_t b) {
30     block_size = b;
31     //zero_crc = ceph_crc32c(0xffffffff, NULL, block_size);
32     if (b) {
33       bufferlist bl;
34       bl.append_zero(block_size);
35       zero_crc = bl.crc32c(crc_iv);
36     } else {
37       zero_crc = crc_iv;
38     }
39   }
40
41   /// update based on a write
42   void write(uint64_t offset, uint64_t len, const bufferlist& bl,
43              std::ostream *out = NULL);
44
45   /// update based on a truncate
46   void truncate(uint64_t offset);
47
48   /// update based on a zero/punch_hole
49   void zero(uint64_t offset, uint64_t len);
50
51   /// update based on a zero/punch_hole
52   void clone_range(uint64_t offset, uint64_t len, uint64_t srcoff, const SloppyCRCMap& src,
53                    std::ostream *out = NULL);
54
55   /**
56    * validate a read result
57    *
58    * @param offset offset
59    * @param length length
60    * @param bl data read
61    * @param err option ostream to describe errors in detail
62    * @returns error count, 0 for success
63    */
64   int read(uint64_t offset, uint64_t len, const bufferlist& bl, std::ostream *err);
65
66   void encode(bufferlist& bl) const;
67   void decode(bufferlist::iterator& bl);
68   void dump(Formatter *f) const;
69   static void generate_test_instances(std::list<SloppyCRCMap*>& ls);
70 };
71 WRITE_CLASS_ENCODER(SloppyCRCMap)
72
73 #endif