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 */
8
9
10#include "Chunk.h"
11
12#include <stdlib.h>
13#include <string.h>
14
15
16//#define TRACE_BTRFS
17#ifdef TRACE_BTRFS
18#	define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
19#else
20#	define TRACE(x...) ;
21#endif
22#	define FATAL(x...) dprintf("\33[34mbtrfs:\33[0m " x)
23
24
25Chunk::Chunk(struct btrfs_chunk* chunk, fsblock_t offset)
26	:
27	fChunk(NULL),
28	fInitStatus(B_OK)
29{
30	fChunkOffset = offset;
31	fChunk = (struct btrfs_chunk*)malloc(sizeof(struct btrfs_chunk)
32		+ chunk->StripeCount() * sizeof(struct btrfs_stripe));
33	if (fChunk == NULL)
34		fInitStatus = B_NO_MEMORY;
35	memcpy(fChunk, chunk, sizeof(struct btrfs_chunk)
36		+ chunk->StripeCount() * sizeof(struct btrfs_stripe));
37
38	TRACE("chunk[0] length %llu owner %llu stripe_length %llu type %llu "
39			"stripe_count %u sub_stripes %u sector_size %lu\n", chunk->Length(),
40			chunk->Owner(), chunk->StripeLength(), chunk->Type(),
41			chunk->StripeCount(), chunk->SubStripes(), chunk->SectorSize());
42	for(int32 i = 0; i < chunk->StripeCount(); i++) {
43		TRACE("chunk.stripe[%ld].physical %lld deviceid %lld\n", i,
44			chunk->stripes[i].Offset(), chunk->stripes[i].DeviceID());
45	}
46}
47
48
49Chunk::~Chunk()
50{
51	free(fChunk);
52}
53
54
55uint32
56Chunk::Size() const
57{
58	return sizeof(struct btrfs_chunk)
59		+ fChunk->StripeCount() * sizeof(struct btrfs_stripe);
60}
61
62
63status_t
64Chunk::FindBlock(off_t logical, off_t &physical)
65{
66	if (fChunk == NULL)
67		return B_NO_INIT;
68
69	if (logical < fChunkOffset
70		|| logical > (fChunkOffset + fChunk->Length()))
71			return B_BAD_VALUE;
72
73	// only one stripe
74	physical = logical + fChunk->stripes[0].Offset() - fChunkOffset;
75	return B_OK;
76}
77
78