BTRFS: Implement some space relevant helpers.

Signed-off-by: Augustin Cavalier <waddlesplash@gmail.com>
This commit is contained in:
hyche 2017-08-24 02:22:02 +07:00 committed by Augustin Cavalier
parent 4368661f03
commit 27ffe0583b
2 changed files with 65 additions and 2 deletions

View File

@ -140,6 +140,64 @@ BTree::Node::SearchSlot(const btrfs_key& key, int* slot, btree_traversing type)
}
/*
* calculate used space except the header.
* type is only for leaf node
* type 1: only item space
* type 2: only item data space
* type 3: both type 1 and 2
*/
int
BTree::Node::_CalculateSpace(uint32 from, uint32 to, uint8 type) const
{
if (to < from || to >= ItemCount())
return 0;
if (Level() != 0)
return sizeof(btrfs_index) * (to - from + 1);
uint32 result = 0;
if ((type & 1) == 1) {
result += sizeof(btrfs_entry) * (to - from + 1);
}
if ((type & 2) == 2) {
result += Item(from)->Offset() + Item(from)->Size()
- Item(to)->Offset();
}
return result;
}
int
BTree::Node::SpaceUsed() const
{
if (Level() == 0)
return _CalculateSpace(0, ItemCount() - 1, 3);
return _CalculateSpace(0, ItemCount() - 1, 0);
}
int
BTree::Node::SpaceLeft() const
{
return fVolume->BlockSize() - SpaceUsed() - sizeof(btrfs_header);
}
status_t
BTree::Node::_SpaceCheck(int length) const
{
// this is a little bit weird here because we can't find
// any suitable error code
if (length < 0 && std::abs(length) >= SpaceUsed())
return B_DIRECTORY_NOT_EMPTY; // not enough data to delete
if (length > 0 && length >= SpaceLeft())
return B_DEVICE_FULL; // no spare space
return B_OK;
}
// #pragma mark - BTree::Path implementation

View File

@ -128,18 +128,23 @@ public:
void SetTo(off_t block);
void SetToWritable(off_t block, int32 transactionId, bool empty);
int SpaceUsed() const;
int SpaceLeft() const;
off_t BlockNum() const { return fBlockNumber;}
bool IsWritable() const { return fWritable; }
status_t SearchSlot(const btrfs_key& key, int* slot,
btree_traversing type) const;
private:
private:
Node(const Node&);
Node& operator=(const Node&);
//no implementation
btrfs_stream* fNode;
status_t _SpaceCheck(int length) const;
int _CalculateSpace(uint32 from, uint32 to, uint8 type = 1) const;
btrfs_stream* fNode;
Volume* fVolume;
off_t fBlockNumber;
bool fWritable;