xref: /haiku/src/add-ons/kernel/file_systems/btrfs/Chunk.cpp (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
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 
25 Chunk::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 		return;
36 	}
37 
38 	memcpy(fChunk, chunk, sizeof(struct btrfs_chunk)
39 		+ chunk->StripeCount() * sizeof(struct btrfs_stripe));
40 
41 	TRACE("chunk[0] length %" B_PRIu64 " owner %" B_PRIu64 " stripe_length %"
42 		B_PRIu64 " type %" B_PRIu64 " stripe_count %u sub_stripes %u "
43 		"sector_size %" B_PRIu32 "\n", chunk->Length(), chunk->Owner(),
44 		chunk->StripeLength(), chunk->Type(), chunk->StripeCount(),
45 		chunk->SubStripes(), chunk->SectorSize());
46 	for(int32 i = 0; i < chunk->StripeCount(); i++) {
47 		TRACE("chunk.stripe[%" B_PRId32 "].physical %" B_PRId64 " deviceid %"
48 			B_PRId64 "\n", i, chunk->stripes[i].Offset(),
49 			chunk->stripes[i].DeviceID());
50 	}
51 }
52 
53 
54 Chunk::~Chunk()
55 {
56 	free(fChunk);
57 }
58 
59 
60 uint32
61 Chunk::Size() const
62 {
63 	return sizeof(struct btrfs_chunk)
64 		+ fChunk->StripeCount() * sizeof(struct btrfs_stripe);
65 }
66 
67 
68 status_t
69 Chunk::FindBlock(off_t logical, off_t &physical)
70 {
71 	if (fChunk == NULL)
72 		return B_NO_INIT;
73 
74 	if (logical < (off_t)fChunkOffset
75 		|| logical > (off_t)(fChunkOffset + fChunk->Length()))
76 			return B_BAD_VALUE;
77 
78 	// only one stripe
79 	physical = logical + fChunk->stripes[0].Offset() - fChunkOffset;
80 	return B_OK;
81 }
82 
83