BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / add-ons / kernel / file_systems / btrfs / Chunk.cpp
blobcba77ce43ebe4fb0da2cc12f38b26d0ebc8eefc0
1 /*
2 * Copyright 2011, Haiku Inc. All rights reserved.
3 * This file may be used under the terms of the MIT License.
5 * Authors:
6 * Jérôme Duval
7 */
10 #include "Chunk.h"
13 //#define TRACE_BTRFS
14 #ifdef TRACE_BTRFS
15 # define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
16 #else
17 # define TRACE(x...) ;
18 #endif
19 # define FATAL(x...) dprintf("\33[34mbtrfs:\33[0m " x)
22 Chunk::Chunk(btrfs_chunk* chunk, fsblock_t offset)
24 fChunk(NULL),
25 fInitStatus(B_OK)
27 fChunkOffset = offset;
28 fChunk = (btrfs_chunk*)malloc(sizeof(btrfs_chunk)
29 + chunk->StripeCount() * sizeof(btrfs_stripe));
30 if (fChunk == NULL) {
31 fInitStatus = B_NO_MEMORY;
32 return;
35 memcpy(fChunk, chunk, sizeof(btrfs_chunk)
36 + chunk->StripeCount() * sizeof(btrfs_stripe));
38 TRACE("chunk[0] length %" B_PRIu64 " owner %" B_PRIu64 " stripe_length %"
39 B_PRIu64 " type %" B_PRIu64 " stripe_count %u sub_stripes %u "
40 "sector_size %" B_PRIu32 "\n", chunk->Length(), chunk->Owner(),
41 chunk->StripeLength(), chunk->Type(), chunk->StripeCount(),
42 chunk->SubStripes(), chunk->SectorSize());
43 for (int32 i = 0; i < chunk->StripeCount(); i++) {
44 TRACE("chunk.stripe[%" B_PRId32 "].physical %" B_PRId64 " deviceid %"
45 B_PRId64 "\n", i, chunk->stripes[i].Offset(),
46 chunk->stripes[i].DeviceID());
51 Chunk::~Chunk()
53 free(fChunk);
57 uint32
58 Chunk::Size() const
60 return sizeof(btrfs_chunk)
61 + fChunk->StripeCount() * sizeof(btrfs_stripe);
65 status_t
66 Chunk::FindBlock(off_t logical, off_t& physical)
68 if (fChunk == NULL)
69 return B_NO_INIT;
71 if (logical < (off_t)fChunkOffset
72 || logical > (off_t)(fChunkOffset + fChunk->Length()))
73 return B_BAD_VALUE;
75 // only one stripe
76 physical = logical + fChunk->stripes[0].Offset() - fChunkOffset;
77 return B_OK;