2011-12-16 00:04:31 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// 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/>.
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
#ifndef BLOCK_H
|
|
|
|
#define BLOCK_H
|
|
|
|
|
2014-07-22 21:11:39 +05:30
|
|
|
#include "block-cache/block_cache.h"
|
2011-10-24 22:34:19 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
2013-01-23 17:58:00 +05:30
|
|
|
#include <string>
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace persistent_data {
|
2014-07-25 15:05:04 +05:30
|
|
|
using namespace bcache;
|
|
|
|
|
2013-01-23 17:58:00 +05:30
|
|
|
uint32_t const MD_BLOCK_SIZE = 4096;
|
|
|
|
|
2014-07-22 21:11:39 +05:30
|
|
|
class block_manager : private boost::noncopyable {
|
2011-07-14 18:35:07 +05:30
|
|
|
public:
|
2020-04-30 19:32:43 +05:30
|
|
|
typedef std::shared_ptr<block_manager> ptr;
|
2014-07-22 21:11:39 +05:30
|
|
|
|
2013-01-12 01:46:40 +05:30
|
|
|
enum mode {
|
|
|
|
READ_ONLY,
|
|
|
|
READ_WRITE,
|
2013-01-15 08:03:09 +05:30
|
|
|
CREATE
|
2013-01-12 01:46:40 +05:30
|
|
|
};
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
block_manager(std::string const &path,
|
|
|
|
block_address nr_blocks,
|
|
|
|
unsigned max_concurrent_locks,
|
2015-07-07 18:17:00 +05:30
|
|
|
mode m,
|
|
|
|
bool excl = true);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
class read_ref {
|
|
|
|
public:
|
2020-04-30 19:00:01 +05:30
|
|
|
static uint32_t const BLOCK_SIZE = MD_BLOCK_SIZE;
|
2013-02-01 17:25:46 +05:30
|
|
|
|
2014-07-28 18:43:28 +05:30
|
|
|
read_ref(block_cache::block &b);
|
2014-07-25 20:44:24 +05:30
|
|
|
|
2011-10-24 22:34:19 +05:30
|
|
|
read_ref(read_ref const &rhs);
|
|
|
|
virtual ~read_ref();
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
block_address get_location() const;
|
2014-07-25 20:44:24 +05:30
|
|
|
void const *data() const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
protected:
|
2014-07-25 20:44:24 +05:30
|
|
|
block_cache::block &b_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// Inherited from read_ref, since you can read a block that's write
|
|
|
|
// locked.
|
|
|
|
class write_ref : public read_ref {
|
|
|
|
public:
|
2014-07-28 18:43:28 +05:30
|
|
|
write_ref(block_cache::block &b);
|
2014-08-21 15:47:20 +05:30
|
|
|
write_ref(block_cache::block &b, unsigned &ref_count);
|
|
|
|
write_ref(write_ref const &rhs);
|
|
|
|
~write_ref();
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
using read_ref::data;
|
2014-07-25 19:16:51 +05:30
|
|
|
void *data();
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2014-08-21 15:47:20 +05:30
|
|
|
private:
|
|
|
|
unsigned *ref_count_;
|
2014-07-25 20:44:24 +05:30
|
|
|
};
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
// Locking methods
|
|
|
|
read_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
read_lock(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
2014-07-25 19:16:51 +05:30
|
|
|
typename validator::ptr(new bcache::noop_validator())) const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
write_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
write_lock(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
2014-07-25 19:16:51 +05:30
|
|
|
typename validator::ptr(new bcache::noop_validator()));
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
write_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
write_lock_zero(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
2014-07-25 19:16:51 +05:30
|
|
|
typename validator::ptr(new bcache::noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
|
|
|
|
// The super block is the one that should be written last.
|
|
|
|
// Unlocking this block triggers the following events:
|
|
|
|
//
|
|
|
|
// i) synchronous write of all dirty blocks _except_ the
|
|
|
|
// superblock.
|
|
|
|
//
|
|
|
|
// ii) synchronous write of superblock
|
|
|
|
//
|
|
|
|
// If any locks are held at the time of the superblock
|
|
|
|
// being unlocked then an exception will be thrown.
|
|
|
|
write_ref superblock(block_address b,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
2014-07-25 19:16:51 +05:30
|
|
|
typename validator::ptr(new bcache::noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
write_ref superblock_zero(block_address b,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
2014-07-25 19:16:51 +05:30
|
|
|
typename validator::ptr(new bcache::noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
block_address get_nr_blocks() const;
|
|
|
|
|
2014-07-25 21:02:59 +05:30
|
|
|
void prefetch(block_address b) const;
|
2011-10-24 22:34:19 +05:30
|
|
|
void flush() const;
|
|
|
|
|
2013-04-29 17:01:30 +05:30
|
|
|
|
|
|
|
// This is just for unit tests, don't call in application
|
|
|
|
// code.
|
|
|
|
bool is_locked(block_address b) const;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
private:
|
2017-07-24 19:58:23 +05:30
|
|
|
uint64_t choose_cache_size(block_address nr_blocks) const;
|
2020-02-22 15:07:22 +05:30
|
|
|
file_utils::file_descriptor open_or_create_block_file(std::string const &path,
|
|
|
|
off_t file_size,
|
|
|
|
mode m, bool excl);
|
2011-07-13 19:39:33 +05:30
|
|
|
void check(block_address b) const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2020-02-22 15:07:22 +05:30
|
|
|
file_utils::file_descriptor fd_;
|
2014-07-25 15:05:04 +05:30
|
|
|
mutable block_cache bc_;
|
2014-08-21 15:47:20 +05:30
|
|
|
unsigned superblock_ref_count_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
2011-11-03 20:14:00 +05:30
|
|
|
|
|
|
|
// A little utility to help build validators
|
2014-07-25 19:16:51 +05:30
|
|
|
inline bcache::validator::ptr
|
|
|
|
mk_validator(bcache::validator *v) {
|
|
|
|
return bcache::validator::ptr(v);
|
2011-11-03 20:14:00 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|