1/*
2 * Copyright 2011, Haiku Inc. All rights reserved.
3 * This file may be used under the terms of the MIT License.
4 *
5 * Authors:
6 *		J��r��me Duval
7 *		Ch��� V�� Gia Hy
8 */
9
10
11#include "Chunk.h"
12
13
14//#define TRACE_BTRFS
15#ifdef TRACE_BTRFS
16#	define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
17#else
18#	define TRACE(x...) ;
19#endif
20#	define FATAL(x...) dprintf("\33[34mbtrfs:\33[0m " x)
21
22
23Chunk::Chunk(btrfs_chunk* chunk, fsblock_t offset)
24	:
25	fChunk(NULL),
26	fInitStatus(B_OK)
27{
28	fChunkOffset = offset;
29	fChunk = (btrfs_chunk*)malloc(sizeof(btrfs_chunk)
30		+ chunk->StripeCount() * sizeof(btrfs_stripe));
31	if (fChunk == NULL) {
32		fInitStatus = B_NO_MEMORY;
33		return;
34	}
35
36	memcpy(fChunk, chunk, sizeof(btrfs_chunk)
37		+ chunk->StripeCount() * sizeof(btrfs_stripe));
38
39	TRACE("chunk[0] length %" B_PRIu64 " owner %" B_PRIu64 " stripe_length %"
40		B_PRIu64 " type %" B_PRIu64 " stripe_count %u sub_stripes %u "
41		"sector_size %" B_PRIu32 "\n", chunk->Length(), chunk->Owner(),
42		chunk->StripeLength(), chunk->Type(), chunk->StripeCount(),
43		chunk->SubStripes(), chunk->SectorSize());
44	for (int32 i = 0; i < chunk->StripeCount(); i++) {
45		TRACE("chunk.stripe[%" B_PRId32 "].physical %" B_PRId64 " deviceid %"
46			B_PRId64 "\n", i, chunk->stripes[i].Offset(),
47			chunk->stripes[i].DeviceID());
48	}
49}
50
51
52Chunk::~Chunk()
53{
54	free(fChunk);
55}
56
57
58uint32
59Chunk::Size() const
60{
61	return sizeof(btrfs_chunk)
62		+ fChunk->StripeCount() * sizeof(btrfs_stripe);
63}
64
65
66status_t
67Chunk::FindBlock(off_t logical, off_t& physical)
68{
69	if (fChunk == NULL)
70		return B_NO_INIT;
71
72	if (logical < (off_t)fChunkOffset
73		|| logical > (off_t)(fChunkOffset + fChunk->Length()))
74		return B_BAD_VALUE;
75
76	// only one stripe
77	physical = logical + fChunk->stripes[0].Offset() - fChunkOffset;
78	return B_OK;
79}
80
81