-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.tcc
372 lines (319 loc) · 9.1 KB
/
block.tcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#include "block.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <boost/bind.hpp>
#include <stdexcept>
#include <sstream>
using namespace boost;
using namespace persistent_data;
using namespace std;
//----------------------------------------------------------------
template <uint32_t BlockSize>
block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks, bool writeable)
: nr_blocks_(nr_blocks),
writeable_(writeable)
{
// fd_ = ::open(path.c_str(), writeable ? (O_RDWR | O_CREAT) : O_RDONLY, 0666);
fd_ = ::open(path.c_str(), O_DIRECT | O_SYNC | (writeable ? O_RDWR : O_RDONLY), 0666);
if (fd_ < 0)
throw std::runtime_error("couldn't open file");
}
template <uint32_t BlockSize>
block_io<BlockSize>::~block_io()
{
::close(fd_);
}
template <uint32_t BlockSize>
void
block_io<BlockSize>::read_buffer(block_address location, buffer<BlockSize> &buffer) const
{
off_t r;
r = ::lseek(fd_, BlockSize * location, SEEK_SET);
if (r == (off_t) -1)
throw std::runtime_error("lseek failed");
ssize_t n;
size_t remaining = BlockSize;
unsigned char *buf = buffer.raw();
do {
n = ::read(fd_, buf, remaining);
if (n > 0) {
remaining -= n;
buf += n;
}
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0)
throw std::runtime_error("read failed");
}
template <uint32_t BlockSize>
void
block_io<BlockSize>::write_buffer(block_address location, buffer<BlockSize> const &buffer)
{
off_t r;
r = ::lseek(fd_, BlockSize * location, SEEK_SET);
if (r == (off_t) -1)
throw std::runtime_error("lseek failed");
ssize_t n;
size_t remaining = BlockSize;
unsigned char const *buf = buffer.raw();
do {
n = ::write(fd_, buf, remaining);
if (n > 0) {
remaining -= n;
buf += n;
}
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0) {
std::ostringstream out;
out << "write failed to block " << location
<< ", block size = " << BlockSize
<< ", remaining = " << remaining
<< ", n = " << n
<< ", errno = " << errno
<< ", fd_ = " << fd_
<< std::endl;
throw std::runtime_error(out.str());
}
}
//----------------------------------------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::block::block(typename block_io<BlockSize>::ptr io,
block_address location,
block_type bt,
typename validator::ptr v,
bool zero)
: io_(io),
location_(location),
data_(new buffer<BlockSize>()),
validator_(v),
bt_(bt),
dirty_(false)
{
if (zero) {
memset(data_->raw(), 0, BlockSize);
dirty_ = true;
} else {
io_->read_buffer(location_, *data_);
validator_->check(*data_, location_);
}
}
template <uint32_t BlockSize>
block_manager<BlockSize>::block::~block()
{
flush();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::flush()
{
if (dirty_) {
validator_->prepare(*data_, location_);
io_->write_buffer(location_, *data_);
}
}
//----------------------------------------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::read_ref(block_manager<BlockSize> const &bm,
block_ptr b)
: bm_(bm),
block_(b),
holders_(new unsigned)
{
*holders_ = 1;
}
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::read_ref(read_ref const &rhs)
: bm_(rhs.bm_),
block_(rhs.block_),
holders_(rhs.holders_)
{
(*holders_)++;
}
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::~read_ref()
{
if (!--(*holders_)) {
if (block_->bt_ == BT_SUPERBLOCK) {
bm_.flush();
bm_.cache_.put(block_);
bm_.flush();
} else
bm_.cache_.put(block_);
delete holders_;
}
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref const &
block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs)
{
if (this != &rhs) {
block_ = rhs.block_;
bm_ = rhs.bm_;
holders_ = rhs.holders_;
(*holders_)++;
}
}
template <uint32_t BlockSize>
block_address
block_manager<BlockSize>::read_ref::get_location() const
{
return block_->location_;
}
template <uint32_t BlockSize>
buffer<BlockSize> const &
block_manager<BlockSize>::read_ref::data() const
{
return *block_->data_;
}
//--------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::write_ref::write_ref(block_manager<BlockSize> const &bm,
block_ptr b)
: read_ref(bm, b)
{
b->dirty_ = true;
}
template <uint32_t BlockSize>
buffer<BlockSize> &
block_manager<BlockSize>::write_ref::data()
{
return *read_ref::block_->data_;
}
//----------------------------------------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::block_manager(std::string const &path,
block_address nr_blocks,
unsigned max_concurrent_blocks,
bool writeable)
: io_(new block_io<BlockSize>(path, nr_blocks, writeable)),
cache_(max(64u, max_concurrent_blocks))
{
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref
block_manager<BlockSize>::read_lock(block_address location,
typename block_manager<BlockSize>::validator::ptr v) const
{
check(location);
boost::optional<block_ptr> cached_block = cache_.get(location);
if (cached_block) {
(*cached_block)->check_read_lockable();
return read_ref(*this, *cached_block);
}
block_ptr b(new block(io_, location, BT_NORMAL, v));
cache_.insert(b);
return read_ref(*this, b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock(block_address location,
typename block_manager<BlockSize>::validator::ptr v)
{
check(location);
boost::optional<block_ptr> cached_block = cache_.get(location);
if (cached_block) {
(*cached_block)->check_write_lockable();
return write_ref(*this, *cached_block);
}
block_ptr b(new block(io_, location, BT_NORMAL, v));
cache_.insert(b);
return write_ref(*this, b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock_zero(block_address location,
typename block_manager<BlockSize>::validator::ptr v)
{
check(location);
boost::optional<block_ptr> cached_block = cache_.get(location);
if (cached_block) {
(*cached_block)->check_write_lockable();
memset((*cached_block)->data_->raw(), 0, BlockSize);
return write_ref(*this, *cached_block);
}
block_ptr b(new block(io_, location, BT_NORMAL, v, true));
cache_.insert(b);
return write_ref(*this, b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::superblock(block_address location,
typename block_manager<BlockSize>::validator::ptr v)
{
check(location);
boost::optional<block_ptr> cached_block = cache_.get(location);
if (cached_block) {
(*cached_block)->check_write_lockable();
(*cached_block)->bt_ = BT_SUPERBLOCK;
(*cached_block)->validator_ = v;
return write_ref(*this, *cached_block);
}
block_ptr b(new block(io_, location, BT_SUPERBLOCK, v));
cache_.insert(b);
return write_ref(*this, b);
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::superblock_zero(block_address location,
typename block_manager<BlockSize>::validator::ptr v)
{
check(location);
boost::optional<block_ptr> cached_block = cache_.get(location);
if (cached_block) {
(*cached_block)->check_write_lockable();
memset((*cached_block)->data_->raw(), 0, BlockSize); // FIXME: add a zero method to buffer
(*cached_block)->validator_ = v;
return write_ref(*this, *cached_block);
}
block_ptr b(new block(io_, location, BT_SUPERBLOCK,
mk_validator(new noop_validator), true));
b->validator_ = v;
cache_.insert(b);
return write_ref(*this, b);
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::check(block_address b) const
{
if (b >= io_->get_nr_blocks())
throw std::runtime_error("block address out of bounds");
}
template <uint32_t BlockSize>
block_address
block_manager<BlockSize>::get_nr_blocks() const
{
return io_->get_nr_blocks();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::write_block(block_ptr b) const
{
b->flush();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::flush() const
{
cache_.iterate_unheld(
boost::bind(&block_manager<BlockSize>::write_block, this, _1));
}
//----------------------------------------------------------------