xref: /haiku/src/add-ons/kernel/file_systems/bfs/BlockAllocator.h (revision b6b0567fbd186f8ce8a0c90bdc7a7b5b4c649678)
1 /*
2  * Copyright 2001-2009, Axel Dörfler, axeld@pinc-software.de.
3  * This file may be used under the terms of the MIT License.
4  */
5 #ifndef BLOCK_ALLOCATOR_H
6 #define BLOCK_ALLOCATOR_H
7 
8 
9 #include "system_dependencies.h"
10 
11 
12 class AllocationGroup;
13 class BPlusTree;
14 class Inode;
15 class Transaction;
16 class Volume;
17 struct disk_super_block;
18 struct block_run;
19 struct check_control;
20 struct check_cookie;
21 
22 
23 //#define DEBUG_ALLOCATION_GROUPS
24 
25 
26 class BlockAllocator {
27 public:
28 							BlockAllocator(Volume* volume);
29 							~BlockAllocator();
30 
31 			status_t		Initialize(bool full = true);
32 			status_t		InitializeAndClearBitmap(Transaction& transaction);
33 
34 			void			Uninitialize();
35 
36 			status_t		AllocateForInode(Transaction& transaction,
37 								const block_run* parent, mode_t type,
38 								block_run& run);
39 			status_t		Allocate(Transaction& transaction, Inode* inode,
40 								off_t numBlocks, block_run& run,
41 								uint16 minimum = 1);
42 			status_t		Free(Transaction& transaction, block_run run);
43 
44 			status_t		AllocateBlocks(Transaction& transaction,
45 								int32 group, uint16 start, uint16 numBlocks,
46 								uint16 minimum, block_run& run);
47 
48 			status_t		StartChecking(check_control* control);
49 			status_t		StopChecking(check_control* control);
50 			status_t		CheckNextNode(check_control* control);
51 
52 			status_t		CheckBlocks(off_t start, off_t length,
53 								bool allocated = true);
54 			status_t		CheckBlockRun(block_run run,
55 								const char* type = NULL,
56 								check_control* control = NULL,
57 								bool allocated = true);
58 			status_t		CheckInode(Inode* inode,
59 								check_control* control = NULL);
60 
61 			size_t			BitmapSize() const;
62 
63 #ifdef BFS_DEBUGGER_COMMANDS
64 			void			Dump(int32 index);
65 #endif
66 
67 private:
68 			status_t		_RemoveInvalidNode(Inode* parent, BPlusTree* tree,
69 								Inode* inode, const char* name);
70 #ifdef DEBUG_ALLOCATION_GROUPS
71 			void			_CheckGroup(int32 group) const;
72 #endif
73 			bool			_IsValidCheckControl(check_control* control);
74 			bool			_CheckBitmapIsUsedAt(off_t block) const;
75 			void			_SetCheckBitmapAt(off_t block);
76 
77 	static	status_t		_Initialize(BlockAllocator* self);
78 
79 			Volume*			fVolume;
80 			mutex			fLock;
81 			AllocationGroup* fGroups;
82 			int32			fNumGroups;
83 			uint32			fBlocksPerGroup;
84 			uint32			fNumBlocks;
85 
86 			uint32*			fCheckBitmap;
87 			check_cookie*	fCheckCookie;
88 };
89 
90 #ifdef BFS_DEBUGGER_COMMANDS
91 #if BFS_TRACING
92 int dump_block_allocator_blocks(int argc, char** argv);
93 #endif
94 int dump_block_allocator(int argc, char** argv);
95 #endif
96 
97 #endif	// BLOCK_ALLOCATOR_H
98