xref: /haiku/src/add-ons/kernel/file_systems/btrfs/Volume.cpp (revision e433b3cfc3f089f7681f6d4e81d43f950ca6a440)
1 /*
2  * Copyright 2011, Jérôme Duval, korli@users.berlios.de.
3  * Copyright 2008-2010, Axel Dörfler, axeld@pinc-software.de.
4  * This file may be used under the terms of the MIT License.
5  */
6 
7 
8 //! Superblock, mounting, etc.
9 
10 
11 #include "Volume.h"
12 #include "BPlusTree.h"
13 #include "CachedBlock.h"
14 #include "Chunk.h"
15 #include "Inode.h"
16 
17 
18 //#define TRACE_BTRFS
19 #ifdef TRACE_BTRFS
20 #	define TRACE(x...) dprintf("\33[34mbtrfs:\33[0m " x)
21 #else
22 #	define TRACE(x...) ;
23 #endif
24 #	define ERROR(x...) dprintf("\33[34mbtrfs:\33[0m " x)
25 
26 
27 class DeviceOpener {
28 public:
29 								DeviceOpener(int fd, int mode);
30 								DeviceOpener(const char* device, int mode);
31 								~DeviceOpener();
32 
33 			int					Open(const char* device, int mode);
34 			int					Open(int fd, int mode);
35 			void*				InitCache(off_t numBlocks, uint32 blockSize);
36 			void				RemoveCache(bool allowWrites);
37 
38 			void				Keep();
39 
40 			int					Device() const { return fDevice; }
41 			int					Mode() const { return fMode; }
42 			bool				IsReadOnly() const
43 									{ return _IsReadOnly(fMode); }
44 
45 			status_t			GetSize(off_t* _size, uint32* _blockSize = NULL);
46 
47 private:
48 	static	bool				_IsReadOnly(int mode)
49 									{ return (mode & O_RWMASK) == O_RDONLY;}
50 	static	bool				_IsReadWrite(int mode)
51 									{ return (mode & O_RWMASK) == O_RDWR;}
52 
53 			int					fDevice;
54 			int					fMode;
55 			void*				fBlockCache;
56 };
57 
58 
59 DeviceOpener::DeviceOpener(const char* device, int mode)
60 	:
61 	fBlockCache(NULL)
62 {
63 	Open(device, mode);
64 }
65 
66 
67 DeviceOpener::DeviceOpener(int fd, int mode)
68 	:
69 	fBlockCache(NULL)
70 {
71 	Open(fd, mode);
72 }
73 
74 
75 DeviceOpener::~DeviceOpener()
76 {
77 	if (fDevice >= 0) {
78 		RemoveCache(false);
79 		close(fDevice);
80 	}
81 }
82 
83 
84 int
85 DeviceOpener::Open(const char* device, int mode)
86 {
87 	fDevice = open(device, mode | O_NOCACHE);
88 	if (fDevice < 0)
89 		fDevice = errno;
90 
91 	if (fDevice < 0 && _IsReadWrite(mode)) {
92 		// try again to open read-only (don't rely on a specific error code)
93 		return Open(device, O_RDONLY | O_NOCACHE);
94 	}
95 
96 	if (fDevice >= 0) {
97 		// opening succeeded
98 		fMode = mode;
99 		if (_IsReadWrite(mode)) {
100 			// check out if the device really allows for read/write access
101 			device_geometry geometry;
102 			if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry)) {
103 				if (geometry.read_only) {
104 					// reopen device read-only
105 					close(fDevice);
106 					return Open(device, O_RDONLY | O_NOCACHE);
107 				}
108 			}
109 		}
110 	}
111 
112 	return fDevice;
113 }
114 
115 
116 int
117 DeviceOpener::Open(int fd, int mode)
118 {
119 	fDevice = dup(fd);
120 	if (fDevice < 0)
121 		return errno;
122 
123 	fMode = mode;
124 
125 	return fDevice;
126 }
127 
128 
129 void*
130 DeviceOpener::InitCache(off_t numBlocks, uint32 blockSize)
131 {
132 	return fBlockCache = block_cache_create(fDevice, numBlocks, blockSize,
133 		IsReadOnly());
134 }
135 
136 
137 void
138 DeviceOpener::RemoveCache(bool allowWrites)
139 {
140 	if (fBlockCache == NULL)
141 		return;
142 
143 	block_cache_delete(fBlockCache, allowWrites);
144 	fBlockCache = NULL;
145 }
146 
147 
148 void
149 DeviceOpener::Keep()
150 {
151 	fDevice = -1;
152 }
153 
154 
155 /*!	Returns the size of the device in bytes. It uses B_GET_GEOMETRY
156 	to compute the size, or fstat() if that failed.
157 */
158 status_t
159 DeviceOpener::GetSize(off_t* _size, uint32* _blockSize)
160 {
161 	device_geometry geometry;
162 	if (ioctl(fDevice, B_GET_GEOMETRY, &geometry) < 0) {
163 		// maybe it's just a file
164 		struct stat stat;
165 		if (fstat(fDevice, &stat) < 0)
166 			return B_ERROR;
167 
168 		if (_size)
169 			*_size = stat.st_size;
170 		if (_blockSize)	// that shouldn't cause us any problems
171 			*_blockSize = 512;
172 
173 		return B_OK;
174 	}
175 
176 	if (_size) {
177 		*_size = 1ULL * geometry.head_count * geometry.cylinder_count
178 			* geometry.sectors_per_track * geometry.bytes_per_sector;
179 	}
180 	if (_blockSize)
181 		*_blockSize = geometry.bytes_per_sector;
182 
183 	return B_OK;
184 }
185 
186 
187 //	#pragma mark -
188 
189 
190 bool
191 btrfs_super_block::IsValid()
192 {
193 	// TODO: check some more values!
194 	if (strncmp(magic, BTRFS_SUPER_BLOCK_MAGIC, sizeof(magic)) != 0)
195 		return false;
196 
197 	return true;
198 }
199 
200 
201 //	#pragma mark -
202 
203 
204 Volume::Volume(fs_volume* volume)
205 	:
206 	fFSVolume(volume),
207 	fFlags(0),
208 	fChunk(NULL),
209 	fChunkTree(NULL)
210 {
211 	mutex_init(&fLock, "btrfs volume");
212 }
213 
214 
215 Volume::~Volume()
216 {
217 	TRACE("Volume destructor.\n");
218 }
219 
220 
221 bool
222 Volume::IsValidSuperBlock()
223 {
224 	return fSuperBlock.IsValid();
225 }
226 
227 
228 const char*
229 Volume::Name() const
230 {
231 	if (fSuperBlock.label[0])
232 		return fSuperBlock.label;
233 
234 	return fName;
235 }
236 
237 
238 status_t
239 Volume::Mount(const char* deviceName, uint32 flags)
240 {
241 	flags |= B_MOUNT_READ_ONLY;
242 		// we only support read-only for now
243 
244 	if ((flags & B_MOUNT_READ_ONLY) != 0) {
245 		TRACE("Volume::Mount(): Read only\n");
246 	} else {
247 		TRACE("Volume::Mount(): Read write\n");
248 	}
249 
250 	DeviceOpener opener(deviceName, (flags & B_MOUNT_READ_ONLY) != 0
251 		? O_RDONLY : O_RDWR);
252 	fDevice = opener.Device();
253 	if (fDevice < B_OK) {
254 		ERROR("Volume::Mount(): couldn't open device\n");
255 		return fDevice;
256 	}
257 
258 	if (opener.IsReadOnly())
259 		fFlags |= VOLUME_READ_ONLY;
260 
261 	// read the superblock
262 	status_t status = Identify(fDevice, &fSuperBlock);
263 	if (status != B_OK) {
264 		ERROR("Volume::Mount(): Identify() failed\n");
265 		return status;
266 	}
267 
268 	fBlockSize = fSuperBlock.BlockSize();
269 	TRACE("block size %" B_PRIu32 "\n", fBlockSize);
270 
271 	uint8* start = (uint8*)&fSuperBlock.system_chunk_array[0];
272 	uint8* end = (uint8*)&fSuperBlock.system_chunk_array[2048];
273 	while (start < end) {
274 		struct btrfs_key* key = (struct btrfs_key*)start;
275 		TRACE("system_chunk_array object_id 0x%" B_PRIx64 " offset 0x%"
276 			B_PRIx64 " type 0x%x\n", key->ObjectID(), key->Offset(),
277 			key->Type());
278 		if (key->Type() != BTRFS_KEY_TYPE_CHUNK_ITEM) {
279 			break;
280 		}
281 
282 		struct btrfs_chunk* chunk = (struct btrfs_chunk*)(key + 1);
283 		fChunk = new(std::nothrow) Chunk(chunk, key->Offset());
284 		if (fChunk == NULL)
285 			return B_ERROR;
286 		start += sizeof(struct btrfs_key) + fChunk->Size();
287 	}
288 
289 	TRACE("Volume::Mount() generation: %" B_PRIu64 "\n",
290 		fSuperBlock.Generation());
291 	fsblock_t physical = 0;
292 	FindBlock(fSuperBlock.Root(), physical);
293 	TRACE("Volume::Mount() root: %" B_PRIu64 " (physical %" B_PRIu64 ")\n",
294 		fSuperBlock.Root(), physical);
295 	FindBlock(fSuperBlock.ChunkRoot(), physical);
296 	TRACE("Volume::Mount() chunk_root: %" B_PRIu64 " (physical %" B_PRIu64
297 		")\n", fSuperBlock.ChunkRoot(), physical);
298 	FindBlock(fSuperBlock.LogRoot(), physical);
299 	TRACE("Volume::Mount() log_root: %" B_PRIu64 " (physical %" B_PRIu64 ")\n",
300 		fSuperBlock.LogRoot(), physical);
301 
302 	// check if the device size is large enough to hold the file system
303 	off_t diskSize;
304 	status = opener.GetSize(&diskSize);
305 	if (status != B_OK)
306 		return status;
307 	if (diskSize < (off_t)fSuperBlock.TotalSize())
308 		return B_BAD_VALUE;
309 
310 	fBlockCache = opener.InitCache(fSuperBlock.TotalSize() / fBlockSize,
311 		fBlockSize);
312 	if (fBlockCache == NULL)
313 		return B_ERROR;
314 
315 	TRACE("Volume::Mount(): Initialized block cache: %p\n", fBlockCache);
316 
317 	fChunkTree = new(std::nothrow) BPlusTree(this, fSuperBlock.ChunkRoot());
318 	if (fChunkTree == NULL)
319 		return B_NO_MEMORY;
320 
321 	FindBlock(fSuperBlock.Root(), physical);
322 	TRACE("Volume::Mount() root: %" B_PRIu64 " (physical %" B_PRIu64 ")\n",
323 		fSuperBlock.Root(), physical);
324 	FindBlock(fSuperBlock.ChunkRoot(), physical);
325 	TRACE("Volume::Mount() chunk_root: %" B_PRIu64 " (physical %" B_PRIu64
326 		")\n", fSuperBlock.ChunkRoot(), physical);
327 	FindBlock(fSuperBlock.LogRoot(), physical);
328 	TRACE("Volume::Mount() log_root: %" B_PRIu64 " (physical %" B_PRIu64 ")\n",
329 		fSuperBlock.LogRoot(), physical);
330 
331 	fRootTree = new(std::nothrow) BPlusTree(this, fSuperBlock.Root());
332 	if (fRootTree == NULL)
333 		return B_NO_MEMORY;
334 	TRACE("Volume::Mount(): Searching extent root\n");
335 	struct btrfs_key search_key;
336 	search_key.SetOffset(0);
337 	search_key.SetType(BTRFS_KEY_TYPE_ROOT_ITEM);
338 	search_key.SetObjectID(BTRFS_OBJECT_ID_EXTENT_TREE);
339 	struct btrfs_root* root;
340 	if (fRootTree->FindNext(search_key, (void**)&root) != B_OK) {
341 		ERROR("Volume::Mount(): Couldn't find extent root\n");
342 		return B_ERROR;
343 	}
344 	TRACE("Volume::Mount(): Found extent root: %" B_PRIu64 "\n",
345 		root->BlockNum());
346 	fExtentTree = new(std::nothrow) BPlusTree(this, root->BlockNum());
347 	free(root);
348 	if (fExtentTree == NULL)
349 		return B_NO_MEMORY;
350 
351 	search_key.SetOffset(0);
352 	search_key.SetObjectID(BTRFS_OBJECT_ID_FS_TREE);
353 	if (fRootTree->FindNext(search_key, (void**)&root) != B_OK) {
354 		ERROR("Volume::Mount(): Couldn't find fs root\n");
355 		return B_ERROR;
356 	}
357 	TRACE("Volume::Mount(): Found fs root: %" B_PRIu64 "\n", root->BlockNum());
358 	fFSTree = new(std::nothrow) BPlusTree(this, root->BlockNum());
359 	free(root);
360 	if (fFSTree == NULL)
361 		return B_NO_MEMORY;
362 
363 	search_key.SetOffset(0);
364 	search_key.SetObjectID(BTRFS_OBJECT_ID_DEV_TREE);
365 	if (fRootTree->FindNext(search_key, (void**)&root) != B_OK) {
366 		ERROR("Volume::Mount(): Couldn't find dev root\n");
367 		return B_ERROR;
368 	}
369 	TRACE("Volume::Mount(): Found dev root: %" B_PRIu64 "\n",
370 		root->BlockNum());
371 	fDevTree = new(std::nothrow) BPlusTree(this, root->BlockNum());
372 	free(root);
373 	if (fDevTree == NULL)
374 		return B_NO_MEMORY;
375 
376 	search_key.SetOffset(0);
377 	search_key.SetObjectID(BTRFS_OBJECT_ID_CHECKSUM_TREE);
378 	if (fRootTree->FindNext(search_key, (void**)&root) != B_OK) {
379 		ERROR("Volume::Mount(): Couldn't find checksum root\n");
380 		return B_ERROR;
381 	}
382 	TRACE("Volume::Mount(): Found checksum root: %" B_PRIu64 "\n",
383 		root->BlockNum());
384 	fChecksumTree = new(std::nothrow) BPlusTree(this, root->BlockNum());
385 	free(root);
386 	if (fChecksumTree == NULL)
387 		return B_NO_MEMORY;
388 
389 	// ready
390 	status = get_vnode(fFSVolume, BTRFS_OBJECT_ID_CHUNK_TREE,
391 		(void**)&fRootNode);
392 	if (status != B_OK) {
393 		ERROR("could not create root node: get_vnode() failed!\n");
394 		return status;
395 	}
396 
397 	TRACE("Volume::Mount(): Found root node: %" B_PRIu64 " (%s)\n",
398 		fRootNode->ID(), strerror(fRootNode->InitCheck()));
399 
400 	// all went fine
401 	opener.Keep();
402 
403 	if (!fSuperBlock.label[0]) {
404 		// generate a more or less descriptive volume name
405 		off_t divisor = 1ULL << 40;
406 		char unit = 'T';
407 		if (diskSize < divisor) {
408 			divisor = 1UL << 30;
409 			unit = 'G';
410 			if (diskSize < divisor) {
411 				divisor = 1UL << 20;
412 				unit = 'M';
413 			}
414 		}
415 
416 		double size = double((10 * diskSize + divisor - 1) / divisor);
417 			// %g in the kernel does not support precision...
418 
419 		snprintf(fName, sizeof(fName), "%g %cB Btrfs Volume",
420 			size / 10, unit);
421 	}
422 
423 	return B_OK;
424 }
425 
426 
427 status_t
428 Volume::Unmount()
429 {
430 	TRACE("Volume::Unmount()\n");
431 	delete fExtentTree;
432 	delete fChecksumTree;
433 	delete fFSTree;
434 	delete fDevTree;
435 	fExtentTree = NULL;
436 	fChecksumTree = NULL;
437 	fFSTree = NULL;
438 	fDevTree = NULL;
439 
440 	TRACE("Volume::Unmount(): Putting root node\n");
441 	put_vnode(fFSVolume, RootNode()->ID());
442 	TRACE("Volume::Unmount(): Deleting the block cache\n");
443 	block_cache_delete(fBlockCache, !IsReadOnly());
444 	TRACE("Volume::Unmount(): Closing device\n");
445 	close(fDevice);
446 
447 	TRACE("Volume::Unmount(): Done\n");
448 	return B_OK;
449 }
450 
451 
452 status_t
453 Volume::LoadSuperBlock()
454 {
455 	CachedBlock cached(this);
456 	const uint8* block = cached.SetTo(BTRFS_SUPER_BLOCK_OFFSET / fBlockSize);
457 
458 	if (block == NULL)
459 		return B_IO_ERROR;
460 
461 	memcpy(&fSuperBlock, block + BTRFS_SUPER_BLOCK_OFFSET % fBlockSize,
462 		sizeof(fSuperBlock));
463 
464 	return B_OK;
465 }
466 
467 
468 status_t
469 Volume::FindBlock(off_t logical, fsblock_t& physicalBlock)
470 {
471 	off_t physical;
472 	status_t status = FindBlock(logical, physical);
473 	if (status != B_OK)
474 		return status;
475 	physicalBlock = physical / fBlockSize;
476 	return B_OK;
477 }
478 
479 
480 status_t
481 Volume::FindBlock(off_t logical, off_t& physical)
482 {
483 	if (fChunkTree == NULL
484 		|| (logical >= (off_t)fChunk->Offset()
485 			&& logical < (off_t)fChunk->End())) {
486 		// try with fChunk
487 		return fChunk->FindBlock(logical, physical);
488 	}
489 
490 	struct btrfs_key search_key;
491 	search_key.SetOffset(logical);
492 	search_key.SetType(BTRFS_KEY_TYPE_CHUNK_ITEM);
493 	search_key.SetObjectID(BTRFS_OBJECT_ID_CHUNK_TREE);
494 	struct btrfs_chunk* chunk;
495 	size_t chunk_length;
496 	status_t status = fChunkTree->FindPrevious(search_key, (void**)&chunk,
497 		&chunk_length);
498 	if (status != B_OK)
499 		return status;
500 
501 	Chunk _chunk(chunk, search_key.Offset());
502 	free(chunk);
503 	status = _chunk.FindBlock(logical, physical);
504 	if (status != B_OK)
505 			return status;
506 	TRACE("Volume::FindBlock(): logical: %" B_PRIdOFF ", physical: %" B_PRIdOFF
507 		"\n", logical, physical);
508 	return B_OK;
509 }
510 
511 
512 //	#pragma mark - Disk scanning and initialization
513 
514 
515 /*static*/ status_t
516 Volume::Identify(int fd, btrfs_super_block* superBlock)
517 {
518 	if (read_pos(fd, BTRFS_SUPER_BLOCK_OFFSET, superBlock,
519 			sizeof(btrfs_super_block)) != sizeof(btrfs_super_block))
520 		return B_IO_ERROR;
521 
522 	if (!superBlock->IsValid()) {
523 		ERROR("invalid superblock!\n");
524 		return B_BAD_VALUE;
525 	}
526 
527 	return B_OK;
528 }
529 
530