vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / kernel / file_systems / btrfs / Chunk.cpp
blob523ee2ed14e37c21309245cc46ffbb0ab828c0ce
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 * Chế Vũ Gia Hy
8 */
11 #include "Chunk.h"
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)
23 Chunk::Chunk(btrfs_chunk* chunk, fsblock_t offset)
25 fChunk(NULL),
26 fInitStatus(B_OK)
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;
36 memcpy(fChunk, chunk, sizeof(btrfs_chunk)
37 + chunk->StripeCount() * sizeof(btrfs_stripe));
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());
52 Chunk::~Chunk()
54 free(fChunk);
58 uint32
59 Chunk::Size() const
61 return sizeof(btrfs_chunk)
62 + fChunk->StripeCount() * sizeof(btrfs_stripe);
66 status_t
67 Chunk::FindBlock(off_t logical, off_t& physical)
69 if (fChunk == NULL)
70 return B_NO_INIT;
72 if (logical < (off_t)fChunkOffset
73 || logical > (off_t)(fChunkOffset + fChunk->Length()))
74 return B_BAD_VALUE;
76 // only one stripe
77 physical = logical + fChunk->stripes[0].Offset() - fChunkOffset;
78 return B_OK;