1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::collections::BTreeMap;
use std::collections::HashMap;

use diskformat::*;

pub type BtrfsChunkItemsByOffset <'a> =
	BTreeMap <u64, BtrfsChunkItem <'a>>;

pub struct BtrfsChunkTree <'a> {
	chunk_items_by_offset: BtrfsChunkItemsByOffset <'a>,
}

impl <'a> BtrfsChunkTree <'a> {

	pub fn new (
		devices: & 'a BtrfsDeviceMap,
		superblock: & BtrfsSuperblock,
	) -> Result <BtrfsChunkTree <'a>, String> {

		let extent_tree_items =
			Self::read_system_extent_tree (
				devices,
				superblock,
			) ?;

		let mut chunk_items_by_offset =
			BtrfsChunkItemsByOffset::new ();

		for extent_tree_item in extent_tree_items.values () {

			match extent_tree_item {

				& BtrfsLeafItem::ChunkItem (chunk_item) => {

					chunk_items_by_offset.insert (
						chunk_item.key ().offset (),
						chunk_item);

				},

				_ => (),

			}

		}

		Ok (
			BtrfsChunkTree {
				chunk_items_by_offset: chunk_items_by_offset,
			}
		)

	}

	fn read_system_extent_tree (
		devices: & 'a BtrfsDeviceMap,
		superblock: & BtrfsSuperblock,
	) -> Result <HashMap <BtrfsKey, BtrfsLeafItem <'a>>, String> {

		let mut extent_tree_items: HashMap <BtrfsKey, BtrfsLeafItem> =
			HashMap::new ();

		Self::read_system_extent_tree_recurse (
			devices,
			superblock,
			superblock.chunk_tree_logical_address (),
			& mut extent_tree_items,
		) ?;

		Ok (extent_tree_items)

	}

	fn read_system_extent_tree_recurse (
		devices: & 'a BtrfsDeviceMap,
		superblock: & BtrfsSuperblock,
		logical_address: u64,
		extent_tree_items: & mut HashMap <BtrfsKey, BtrfsLeafItem <'a>>,
	) -> Result <(), String> {

		let (device_id, device_address) =
			superblock.system_logical_to_physical (
				logical_address,
			).expect ("Lookup logical address");

		let device =
			devices.get (
				& device_id,
			).expect ("Lookup device");

		let node_bytes =
			device.slice_at (
				device_address as usize,
				superblock.node_size () as usize);

		let node =
			BtrfsNode::from_bytes (
				node_bytes,
			) ?;

		match node {

			BtrfsNode::Internal (internal_node) => {

				for item in internal_node.items () {

					Self::read_system_extent_tree_recurse (
						devices,
						superblock,
						item.key ().offset (),
						extent_tree_items,
					) ?;

				}

			},

			BtrfsNode::Leaf (leaf_node) => {

				for item in leaf_node.items () {

					extent_tree_items.insert (
						item.key (),
						item,
					);

				}

			},

		}

		Ok (())

	}

	pub fn logical_to_physical_address (
		& self,
		logical_address: u64,
	) -> Option <(u64, u64)> {

		// TODO waiting for range to land in stable rust

		for (ref chunk_item_offset, ref chunk_item)
		in self.chunk_items_by_offset.iter () {

			let chunk_item_offset =
				** chunk_item_offset;

			if logical_address >= chunk_item_offset
			&& logical_address < (
				chunk_item_offset +
				chunk_item.data ().chunk_size ()
			) {

				let chunk_item_stripe =
					chunk_item.stripes () [0];

				return Some (
					(
						chunk_item_stripe.device_id (),
						logical_address
							- chunk_item_offset
							+ chunk_item_stripe.offset (),
					)
				);

			}

		}

		None

	}

}

// ex: noet ts=4 filetype=rust