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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::mem;

use diskformat::*;

#[ repr (C, packed) ]
#[ derive (Copy) ]
pub struct BtrfsSuperblock {
	checksum: BtrfsChecksum,
	fs_uuid: BtrfsUuid,
	physical_address: u64,
	flags: u64,
	magic: [u8; 0x8],
	generation: u64,
	root_tree_logical_address: u64,
	chunk_tree_logical_address: u64,
	log_tree_logical_address: u64,
	log_root_transid: u64,
	total_bytes: u64,
	bytes_used: u64,
	root_dir_objectid: u64,
	num_devices: u64,
	sector_size: u32,
	node_size: u32,
	leaf_size: u32,
	stripesize: u32,
	system_chunks_size: u32,
	chunk_root_generation: u64,
	compat_flags: u64,
	compat_ro_flags: u64,
	incompat_flags: u64,
	csum_type: u16,
	root_level: u8,
	chunk_root_level: u8,
	log_root_level: u8,
	dev_item: BtrfsDevItem,
	label: BtrfsLabel,
	reserved: [u8; 0x100],
	system_chunks: [u8; 0x800],
	unused: [u8; 0x4d5],
}

pub struct BtrfsSuperblockSystemChunks <'a> {
	_superblock: & 'a BtrfsSuperblock,
	address: * const u8,
	end_address: * const u8,
}

impl BtrfsSuperblock {

	pub fn fs_uuid (& self) -> BtrfsUuid {
		self.fs_uuid
	}

	pub fn system_chunks (& self) -> BtrfsSuperblockSystemChunks {

		let start_address =
			self.system_chunks.as_ptr ();

		let end_address = unsafe {
			start_address.offset (
				self.system_chunks_size as isize,
			)
		};

		BtrfsSuperblockSystemChunks {
			_superblock: self,
			address: start_address,
			end_address: end_address,
		}

	}

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

		for system_chunk in self.system_chunks () {

			if logical_address < system_chunk.key ().offset ()
			|| logical_address >= (
				system_chunk.key ().offset ()
				+ system_chunk.data ().chunk_size ()
			) {
				continue;
			}

			let system_chunk_stripe =
				system_chunk.stripes () [0];

			return Some (
				(
					system_chunk_stripe.device_id (),
					logical_address
						- system_chunk.key ().offset ()
						+ system_chunk_stripe.offset (),
				)
			);

		}

		None

	}

	pub fn magic (& self) -> [u8; 0x8] {
		self.magic
	}

	pub fn root_tree_logical_address (& self) -> u64 {
		self.root_tree_logical_address
	}

	pub fn chunk_tree_logical_address (& self) -> u64 {
		self.chunk_tree_logical_address
	}

	pub fn sector_size (& self) -> u32 {
		self.sector_size
	}

	pub fn node_size (& self) -> u32 {
		self.node_size
	}

	pub fn leaf_size (& self) -> u32 {
		self.leaf_size
	}

	pub fn dev_item (& self) -> & BtrfsDevItem {
		& self.dev_item
	}

}

impl Clone for BtrfsSuperblock {

	fn clone (
		& self,
	) -> Self {
		* self
	}

}

impl <'a> Iterator for BtrfsSuperblockSystemChunks <'a> {

	type Item = BtrfsChunkItemSimple <'a>;

	fn next (
		& mut self,
	) -> Option <BtrfsChunkItemSimple <'a>> {

		if self.address < self.end_address {

			// get key

			let chunk_item_key = unsafe {
				& * (
					self.address
						as * const BtrfsKey
				)
			};

			self.address = unsafe {
				self.address.offset (
					mem::size_of::<BtrfsKey> () as isize,
				)
			};

			// get data

			let chunk_item_data = unsafe {
				& * (
					self.address
						as * const BtrfsChunkItemData
				)
			};

			self.address = unsafe {
				self.address.offset (
					mem::size_of::<BtrfsChunkItemData> () as isize,
				)
			};

			// skip chunk item stripes

			self.address = unsafe {
				self.address.offset (
					mem::size_of::<BtrfsChunkItemStripeData> () as isize
						* chunk_item_data.num_stripes () as isize,
				)
			};

			// return

			Some (
				BtrfsChunkItemSimple::new (
					chunk_item_key,
					chunk_item_data,
				).expect ("Invalid chunk item")
			)

		} else {

			None

		}

	}

}

// ex: noet ts=4 filetype=rust