1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Inktank Storage, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
18 #include "erasure-code/ErasureCodeInterface.h"
19 #include "include/buffer_fwd.h"
20 #include "include/assert.h"
21 #include "include/encoding.h"
22 #include "common/Formatter.h"
26 const uint64_t CHUNK_ALIGNMENT = 64;
27 const uint64_t CHUNK_INFO = 8;
28 const uint64_t CHUNK_PADDING = 8;
29 const uint64_t CHUNK_OVERHEAD = 16; // INFO + PADDING
32 const uint64_t stripe_width;
33 const uint64_t chunk_size;
35 stripe_info_t(uint64_t stripe_size, uint64_t stripe_width)
36 : stripe_width(stripe_width),
37 chunk_size(stripe_width / stripe_size) {
38 assert(stripe_width % stripe_size == 0);
40 bool logical_offset_is_stripe_aligned(uint64_t logical) const {
41 return (logical % stripe_width) == 0;
43 uint64_t get_stripe_width() const {
46 uint64_t get_chunk_size() const {
49 uint64_t logical_to_prev_chunk_offset(uint64_t offset) const {
50 return (offset / stripe_width) * chunk_size;
52 uint64_t logical_to_next_chunk_offset(uint64_t offset) const {
53 return ((offset + stripe_width - 1)/ stripe_width) * chunk_size;
55 uint64_t logical_to_prev_stripe_offset(uint64_t offset) const {
56 return offset - (offset % stripe_width);
58 uint64_t logical_to_next_stripe_offset(uint64_t offset) const {
59 return ((offset % stripe_width) ?
60 (offset - (offset % stripe_width) + stripe_width) :
63 uint64_t aligned_logical_offset_to_chunk_offset(uint64_t offset) const {
64 assert(offset % stripe_width == 0);
65 return (offset / stripe_width) * chunk_size;
67 uint64_t aligned_chunk_offset_to_logical_offset(uint64_t offset) const {
68 assert(offset % chunk_size == 0);
69 return (offset / chunk_size) * stripe_width;
71 std::pair<uint64_t, uint64_t> aligned_offset_len_to_chunk(
72 std::pair<uint64_t, uint64_t> in) const {
73 return std::make_pair(
74 aligned_logical_offset_to_chunk_offset(in.first),
75 aligned_logical_offset_to_chunk_offset(in.second));
77 std::pair<uint64_t, uint64_t> offset_len_to_stripe_bounds(
78 std::pair<uint64_t, uint64_t> in) const {
79 uint64_t off = logical_to_prev_stripe_offset(in.first);
80 uint64_t len = logical_to_next_stripe_offset(
81 (in.first - off) + in.second);
82 return std::make_pair(off, len);
87 const stripe_info_t &sinfo,
88 ErasureCodeInterfaceRef &ec_impl,
89 std::map<int, bufferlist> &to_decode,
93 const stripe_info_t &sinfo,
94 ErasureCodeInterfaceRef &ec_impl,
95 std::map<int, bufferlist> &to_decode,
96 std::map<int, bufferlist*> &out);
99 const stripe_info_t &sinfo,
100 ErasureCodeInterfaceRef &ec_impl,
102 const std::set<int> &want,
103 std::map<int, bufferlist> *out);
106 uint64_t total_chunk_size = 0;
107 std::vector<uint32_t> cumulative_shard_hashes;
109 // purely ephemeral, represents the size once all in-flight ops commit
110 uint64_t projected_total_chunk_size = 0;
113 explicit HashInfo(unsigned num_chunks) :
114 cumulative_shard_hashes(num_chunks, -1) {}
115 void append(uint64_t old_size, std::map<int, bufferlist> &to_append);
117 total_chunk_size = 0;
118 cumulative_shard_hashes = std::vector<uint32_t>(
119 cumulative_shard_hashes.size(),
122 void encode(bufferlist &bl) const;
123 void decode(bufferlist::iterator &bl);
124 void dump(Formatter *f) const;
125 static void generate_test_instances(std::list<HashInfo*>& o);
126 uint32_t get_chunk_hash(int shard) const {
127 assert((unsigned)shard < cumulative_shard_hashes.size());
128 return cumulative_shard_hashes[shard];
130 uint64_t get_total_chunk_size() const {
131 return total_chunk_size;
133 uint64_t get_projected_total_chunk_size() const {
134 return projected_total_chunk_size;
136 uint64_t get_total_logical_size(const stripe_info_t &sinfo) const {
137 return get_total_chunk_size() *
138 (sinfo.get_stripe_width()/sinfo.get_chunk_size());
140 uint64_t get_projected_total_logical_size(const stripe_info_t &sinfo) const {
141 return get_projected_total_chunk_size() *
142 (sinfo.get_stripe_width()/sinfo.get_chunk_size());
144 void set_projected_total_logical_size(
145 const stripe_info_t &sinfo,
146 uint64_t logical_size) {
147 assert(sinfo.logical_offset_is_stripe_aligned(logical_size));
148 projected_total_chunk_size = sinfo.aligned_logical_offset_to_chunk_offset(
151 void set_total_chunk_size_clear_hash(uint64_t new_chunk_size) {
152 cumulative_shard_hashes.clear();
153 total_chunk_size = new_chunk_size;
155 bool has_chunk_hash() const {
156 return !cumulative_shard_hashes.empty();
158 void update_to(const HashInfo &rhs) {
159 auto ptcs = projected_total_chunk_size;
161 projected_total_chunk_size = ptcs;
165 typedef ceph::shared_ptr<HashInfo> HashInfoRef;
167 bool is_hinfo_key_string(const std::string &key);
168 const std::string &get_hinfo_key();
171 WRITE_CLASS_ENCODER(ECUtil::HashInfo)