16f4c36e2SJérôme Duval /*
26f4c36e2SJérôme Duval * Copyright 2011, Haiku Inc. All rights reserved.
36f4c36e2SJérôme Duval * This file may be used under the terms of the MIT License.
46f4c36e2SJérôme Duval *
56f4c36e2SJérôme Duval * Authors:
66f4c36e2SJérôme Duval * Jérôme Duval
7*99768086Shyche * Chế Vũ Gia Hy
86f4c36e2SJérôme Duval */
96f4c36e2SJérôme Duval
106f4c36e2SJérôme Duval
116f4c36e2SJérôme Duval #include "Chunk.h"
126f4c36e2SJérôme Duval
136f4c36e2SJérôme Duval
146f4c36e2SJérôme Duval //#define TRACE_BTRFS
156f4c36e2SJérôme Duval #ifdef TRACE_BTRFS
166f4c36e2SJérôme Duval # define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
176f4c36e2SJérôme Duval #else
186f4c36e2SJérôme Duval # define TRACE(x...) ;
196f4c36e2SJérôme Duval #endif
206f4c36e2SJérôme Duval # define FATAL(x...) dprintf("\33[34mbtrfs:\33[0m " x)
216f4c36e2SJérôme Duval
226f4c36e2SJérôme Duval
Chunk(btrfs_chunk * chunk,fsblock_t offset)23299aba38Shyche Chunk::Chunk(btrfs_chunk* chunk, fsblock_t offset)
246f4c36e2SJérôme Duval :
256f4c36e2SJérôme Duval fChunk(NULL),
266f4c36e2SJérôme Duval fInitStatus(B_OK)
276f4c36e2SJérôme Duval {
286f4c36e2SJérôme Duval fChunkOffset = offset;
29299aba38Shyche fChunk = (btrfs_chunk*)malloc(sizeof(btrfs_chunk)
30299aba38Shyche + chunk->StripeCount() * sizeof(btrfs_stripe));
31ddc2a758SMurai Takashi if (fChunk == NULL) {
326f4c36e2SJérôme Duval fInitStatus = B_NO_MEMORY;
33ddc2a758SMurai Takashi return;
34ddc2a758SMurai Takashi }
35ddc2a758SMurai Takashi
36299aba38Shyche memcpy(fChunk, chunk, sizeof(btrfs_chunk)
37299aba38Shyche + chunk->StripeCount() * sizeof(btrfs_stripe));
386f4c36e2SJérôme Duval
39267a780aSJérôme Duval TRACE("chunk[0] length %" B_PRIu64 " owner %" B_PRIu64 " stripe_length %"
40267a780aSJérôme Duval B_PRIu64 " type %" B_PRIu64 " stripe_count %u sub_stripes %u "
41267a780aSJérôme Duval "sector_size %" B_PRIu32 "\n", chunk->Length(), chunk->Owner(),
42267a780aSJérôme Duval chunk->StripeLength(), chunk->Type(), chunk->StripeCount(),
43267a780aSJérôme Duval chunk->SubStripes(), chunk->SectorSize());
446f4c36e2SJérôme Duval for (int32 i = 0; i < chunk->StripeCount(); i++) {
45267a780aSJérôme Duval TRACE("chunk.stripe[%" B_PRId32 "].physical %" B_PRId64 " deviceid %"
46267a780aSJérôme Duval B_PRId64 "\n", i, chunk->stripes[i].Offset(),
47267a780aSJérôme Duval chunk->stripes[i].DeviceID());
486f4c36e2SJérôme Duval }
496f4c36e2SJérôme Duval }
506f4c36e2SJérôme Duval
516f4c36e2SJérôme Duval
~Chunk()526f4c36e2SJérôme Duval Chunk::~Chunk()
536f4c36e2SJérôme Duval {
546f4c36e2SJérôme Duval free(fChunk);
556f4c36e2SJérôme Duval }
566f4c36e2SJérôme Duval
576f4c36e2SJérôme Duval
586f4c36e2SJérôme Duval uint32
Size() const596f4c36e2SJérôme Duval Chunk::Size() const
606f4c36e2SJérôme Duval {
61299aba38Shyche return sizeof(btrfs_chunk)
62299aba38Shyche + fChunk->StripeCount() * sizeof(btrfs_stripe);
636f4c36e2SJérôme Duval }
646f4c36e2SJérôme Duval
656f4c36e2SJérôme Duval
666f4c36e2SJérôme Duval status_t
FindBlock(off_t logical,off_t & physical)676f4c36e2SJérôme Duval Chunk::FindBlock(off_t logical, off_t& physical)
686f4c36e2SJérôme Duval {
696f4c36e2SJérôme Duval if (fChunk == NULL)
706f4c36e2SJérôme Duval return B_NO_INIT;
716f4c36e2SJérôme Duval
72267a780aSJérôme Duval if (logical < (off_t)fChunkOffset
73267a780aSJérôme Duval || logical > (off_t)(fChunkOffset + fChunk->Length()))
746f4c36e2SJérôme Duval return B_BAD_VALUE;
756f4c36e2SJérôme Duval
766f4c36e2SJérôme Duval // only one stripe
776f4c36e2SJérôme Duval physical = logical + fChunk->stripes[0].Offset() - fChunkOffset;
786f4c36e2SJérôme Duval return B_OK;
796f4c36e2SJérôme Duval }
806f4c36e2SJérôme Duval
81