aboutsummaryrefslogtreecommitdiffstats
path: root/src/add-ons/kernel/file_systems/btrfs/Chunk.cpp
blob: 523ee2ed14e37c21309245cc46ffbb0ab828c0ce (plain) (blame)
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
/*
 * Copyright 2011, Haiku Inc. All rights reserved.
 * This file may be used under the terms of the MIT License.
 *
 * Authors:
 *		Jérôme Duval
 *		Chế Vũ Gia Hy
 */


#include "Chunk.h"


//#define TRACE_BTRFS
#ifdef TRACE_BTRFS
#	define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
#else
#	define TRACE(x...) ;
#endif
#	define FATAL(x...) dprintf("\33[34mbtrfs:\33[0m " x)


Chunk::Chunk(btrfs_chunk* chunk, fsblock_t offset)
	:
	fChunk(NULL),
	fInitStatus(B_OK)
{
	fChunkOffset = offset;
	fChunk = (btrfs_chunk*)malloc(sizeof(btrfs_chunk)
		+ chunk->StripeCount() * sizeof(btrfs_stripe));
	if (fChunk == NULL) {
		fInitStatus = B_NO_MEMORY;
		return;
	}

	memcpy(fChunk, chunk, sizeof(btrfs_chunk)
		+ chunk->StripeCount() * sizeof(btrfs_stripe));

	TRACE("chunk[0] length %" B_PRIu64 " owner %" B_PRIu64 " stripe_length %"
		B_PRIu64 " type %" B_PRIu64 " stripe_count %u sub_stripes %u "
		"sector_size %" B_PRIu32 "\n", chunk->Length(), chunk->Owner(),
		chunk->StripeLength(), chunk->Type(), chunk->StripeCount(),
		chunk->SubStripes(), chunk->SectorSize());
	for (int32 i = 0; i < chunk->StripeCount(); i++) {
		TRACE("chunk.stripe[%" B_PRId32 "].physical %" B_PRId64 " deviceid %"
			B_PRId64 "\n", i, chunk->stripes[i].Offset(),
			chunk->stripes[i].DeviceID());
	}
}


Chunk::~Chunk()
{
	free(fChunk);
}


uint32
Chunk::Size() const
{
	return sizeof(btrfs_chunk)
		+ fChunk->StripeCount() * sizeof(btrfs_stripe);
}


status_t
Chunk::FindBlock(off_t logical, off_t& physical)
{
	if (fChunk == NULL)
		return B_NO_INIT;

	if (logical < (off_t)fChunkOffset
		|| logical > (off_t)(fChunkOffset + fChunk->Length()))
		return B_BAD_VALUE;

	// only one stripe
	physical = logical + fChunk->stripes[0].Offset() - fChunkOffset;
	return B_OK;
}