xref: /haiku/src/add-ons/kernel/file_systems/udf/MetadataPartition.cpp (revision 25a7b01d15612846f332751841da3579db313082)
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 */
MetadataPartition(Volume * volume,uint16 parentNumber,Partition & parentPartition,uint32 metadataFileLocation,uint32 metadataMirrorFileLocation,uint32 metadataBitmapFileLocation,uint32 allocationUnitSize,uint16 alignmentUnitSize,bool metadataIsDuplicated)15 MetadataPartition::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 */
~MetadataPartition()48 MetadataPartition::~MetadataPartition()
49 {
50 	delete fMetadataIcb;
51 	delete fMetadataMirrorIcb;
52 }
53 
54 /*! \brief Maps the given logical block to a physical block on disc.
55 */
56 status_t
MapBlock(uint32 logicalBlock,off_t & physicalBlock)57 MetadataPartition::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 */
76 status_t
InitCheck()77 MetadataPartition::InitCheck()
78 {
79 	return fInitStatus;
80 }
81