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.
21 #include "include/memory.h"
22 #include "erasure-code/ErasureCodeInterface.h"
23 #include "include/buffer_fwd.h"
24 #include "include/assert.h"
25 #include "include/encoding.h"
26 #include "common/Formatter.h"
30 const uint64_t CHUNK_ALIGNMENT
= 64;
31 const uint64_t CHUNK_INFO
= 8;
32 const uint64_t CHUNK_PADDING
= 8;
33 const uint64_t CHUNK_OVERHEAD
= 16; // INFO + PADDING
36 const uint64_t stripe_width
;
37 const uint64_t chunk_size
;
39 stripe_info_t(uint64_t stripe_size
, uint64_t stripe_width
)
40 : stripe_width(stripe_width
),
41 chunk_size(stripe_width
/ stripe_size
) {
42 assert(stripe_width
% stripe_size
== 0);
44 bool logical_offset_is_stripe_aligned(uint64_t logical
) const {
45 return (logical
% stripe_width
) == 0;
47 uint64_t get_stripe_width() const {
50 uint64_t get_chunk_size() const {
53 uint64_t logical_to_prev_chunk_offset(uint64_t offset
) const {
54 return (offset
/ stripe_width
) * chunk_size
;
56 uint64_t logical_to_next_chunk_offset(uint64_t offset
) const {
57 return ((offset
+ stripe_width
- 1)/ stripe_width
) * chunk_size
;
59 uint64_t logical_to_prev_stripe_offset(uint64_t offset
) const {
60 return offset
- (offset
% stripe_width
);
62 uint64_t logical_to_next_stripe_offset(uint64_t offset
) const {
63 return ((offset
% stripe_width
) ?
64 (offset
- (offset
% stripe_width
) + stripe_width
) :
67 uint64_t aligned_logical_offset_to_chunk_offset(uint64_t offset
) const {
68 assert(offset
% stripe_width
== 0);
69 return (offset
/ stripe_width
) * chunk_size
;
71 uint64_t aligned_chunk_offset_to_logical_offset(uint64_t offset
) const {
72 assert(offset
% chunk_size
== 0);
73 return (offset
/ chunk_size
) * stripe_width
;
75 pair
<uint64_t, uint64_t> aligned_offset_len_to_chunk(
76 pair
<uint64_t, uint64_t> in
) const {
78 aligned_logical_offset_to_chunk_offset(in
.first
),
79 aligned_logical_offset_to_chunk_offset(in
.second
));
81 pair
<uint64_t, uint64_t> offset_len_to_stripe_bounds(
82 pair
<uint64_t, uint64_t> in
) const {
83 uint64_t off
= logical_to_prev_stripe_offset(in
.first
);
84 uint64_t len
= logical_to_next_stripe_offset(
85 (in
.first
- off
) + in
.second
);
86 return make_pair(off
, len
);
91 const stripe_info_t
&sinfo
,
92 ErasureCodeInterfaceRef
&ec_impl
,
93 map
<int, bufferlist
> &to_decode
,
97 const stripe_info_t
&sinfo
,
98 ErasureCodeInterfaceRef
&ec_impl
,
99 map
<int, bufferlist
> &to_decode
,
100 map
<int, bufferlist
*> &out
);
103 const stripe_info_t
&sinfo
,
104 ErasureCodeInterfaceRef
&ec_impl
,
106 const set
<int> &want
,
107 map
<int, bufferlist
> *out
);
110 uint64_t total_chunk_size
= 0;
111 vector
<uint32_t> cumulative_shard_hashes
;
113 // purely ephemeral, represents the size once all in-flight ops commit
114 uint64_t projected_total_chunk_size
= 0;
117 explicit HashInfo(unsigned num_chunks
) :
118 cumulative_shard_hashes(num_chunks
, -1) {}
119 void append(uint64_t old_size
, map
<int, bufferlist
> &to_append
);
121 total_chunk_size
= 0;
122 cumulative_shard_hashes
= vector
<uint32_t>(
123 cumulative_shard_hashes
.size(),
126 void encode(bufferlist
&bl
) const;
127 void decode(bufferlist::iterator
&bl
);
128 void dump(Formatter
*f
) const;
129 static void generate_test_instances(list
<HashInfo
*>& o
);
130 uint32_t get_chunk_hash(int shard
) const {
131 assert((unsigned)shard
< cumulative_shard_hashes
.size());
132 return cumulative_shard_hashes
[shard
];
134 uint64_t get_total_chunk_size() const {
135 return total_chunk_size
;
137 uint64_t get_projected_total_chunk_size() const {
138 return projected_total_chunk_size
;
140 uint64_t get_total_logical_size(const stripe_info_t
&sinfo
) const {
141 return get_total_chunk_size() *
142 (sinfo
.get_stripe_width()/sinfo
.get_chunk_size());
144 uint64_t get_projected_total_logical_size(const stripe_info_t
&sinfo
) const {
145 return get_projected_total_chunk_size() *
146 (sinfo
.get_stripe_width()/sinfo
.get_chunk_size());
148 void set_projected_total_logical_size(
149 const stripe_info_t
&sinfo
,
150 uint64_t logical_size
) {
151 assert(sinfo
.logical_offset_is_stripe_aligned(logical_size
));
152 projected_total_chunk_size
= sinfo
.aligned_logical_offset_to_chunk_offset(
155 void set_total_chunk_size_clear_hash(uint64_t new_chunk_size
) {
156 cumulative_shard_hashes
.clear();
157 total_chunk_size
= new_chunk_size
;
159 bool has_chunk_hash() const {
160 return !cumulative_shard_hashes
.empty();
162 void update_to(const HashInfo
&rhs
) {
163 auto ptcs
= projected_total_chunk_size
;
165 projected_total_chunk_size
= ptcs
;
169 typedef ceph::shared_ptr
<HashInfo
> HashInfoRef
;
171 bool is_hinfo_key_string(const string
&key
);
172 const string
&get_hinfo_key();
175 WRITE_CLASS_ENCODER(ECUtil::HashInfo
)