1/*
2 * Copyright 2012, Jérôme Duval, korli@users.berlios.de.
3 * Copyright 2003 Tyler Dauwalder, tyler@dauwalder.net
4 * This file may be used under the terms of the MIT License.
5 */
6
7
8#include "MetadataPartition.h"
9
10#include "Icb.h"
11
12
13/*! \brief Creates a new MetadataPartition object.
14*/
15MetadataPartition::MetadataPartition(Volume *volume,
16	uint16 parentNumber, Partition &parentPartition, uint32 metadataFileLocation,
17	uint32 metadataMirrorFileLocation, uint32 metadataBitmapFileLocation,
18	uint32 allocationUnitSize, uint16 alignmentUnitSize,
19	bool metadataIsDuplicated)
20	: fPartition(parentNumber),
21	fParentPartition(parentPartition),
22	fAllocationUnitSize(allocationUnitSize),
23	fAlignmentUnitSize(alignmentUnitSize),
24	fMetadataIsDuplicated(metadataIsDuplicated),
25	fInitStatus(B_NO_INIT),
26	fMetadataIcb(NULL)
27{
28	long_address address;
29	address.set_to(metadataFileLocation, fPartition);
30
31	fMetadataIcb = new(nothrow) Icb(volume, address);
32	if (fMetadataIcb == NULL || fMetadataIcb->InitCheck() != B_OK)
33		fInitStatus = B_NO_MEMORY;
34	else
35		fInitStatus = B_OK;
36
37	address.set_to(metadataMirrorFileLocation, fPartition);
38
39	fMetadataMirrorIcb = new(nothrow) Icb(volume, address);
40	if (fMetadataMirrorIcb == NULL
41		|| fMetadataMirrorIcb->InitCheck() != B_OK) {
42		fInitStatus = B_NO_MEMORY;
43	}
44}
45
46/*! \brief Destroys the MetadataPartition object.
47*/
48MetadataPartition::~MetadataPartition()
49{
50	delete fMetadataIcb;
51	delete fMetadataMirrorIcb;
52}
53
54/*! \brief Maps the given logical block to a physical block on disc.
55*/
56status_t
57MetadataPartition::MapBlock(uint32 logicalBlock, off_t &physicalBlock)
58{
59	off_t block = 0;
60	bool isRecorded;
61	status_t status = fMetadataIcb->FindBlock(logicalBlock, block, isRecorded);
62	if (status != B_OK)
63		return status;
64	if (!isRecorded) {
65		status = fMetadataMirrorIcb->FindBlock(logicalBlock, block, isRecorded);
66		if (status != B_OK)
67			return status;
68		if (!isRecorded)
69			return B_BAD_DATA;
70	}
71	return fParentPartition.MapBlock(block, physicalBlock);
72}
73
74/*! Returns the initialization status of the object.
75*/
76status_t
77MetadataPartition::InitCheck()
78{
79	return fInitStatus;
80}
81