xref: /haiku/src/add-ons/kernel/file_systems/bfs/BlockAllocator.h (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2  * Copyright 2001-2010, 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 //#define DEBUG_FRAGMENTER
25 
26 
27 class BlockAllocator {
28 public:
29 							BlockAllocator(Volume* volume);
30 							~BlockAllocator();
31 
32 			status_t		Initialize(bool full = true);
33 			status_t		InitializeAndClearBitmap(Transaction& transaction);
34 
35 			void			Uninitialize();
36 
37 			status_t		AllocateForInode(Transaction& transaction,
38 								const block_run* parent, mode_t type,
39 								block_run& run);
40 			status_t		Allocate(Transaction& transaction, Inode* inode,
41 								off_t numBlocks, block_run& run,
42 								uint16 minimum = 1);
43 			status_t		Free(Transaction& transaction, block_run run);
44 
45 			status_t		AllocateBlocks(Transaction& transaction,
46 								int32 group, uint16 start, uint16 numBlocks,
47 								uint16 minimum, block_run& run);
48 
49 			status_t		StartChecking(const check_control* control);
50 			status_t		StopChecking(check_control* control);
51 			status_t		CheckNextNode(check_control* control);
52 
53 			status_t		CheckBlocks(off_t start, off_t length,
54 								bool allocated = true);
55 			status_t		CheckBlockRun(block_run run,
56 								const char* type = NULL,
57 								bool allocated = true);
58 			status_t		CheckInode(Inode* inode);
59 
60 			size_t			BitmapSize() const;
61 
62 #ifdef BFS_DEBUGGER_COMMANDS
63 			void			Dump(int32 index);
64 #endif
65 #ifdef DEBUG_FRAGMENTER
66 			void			Fragment();
67 #endif
68 
69 private:
70 			status_t		_RemoveInvalidNode(Inode* parent, BPlusTree* tree,
71 								Inode* inode, const char* name);
72 #ifdef DEBUG_ALLOCATION_GROUPS
73 			void			_CheckGroup(int32 group) const;
74 #endif
75 			bool			_IsValidCheckControl(const check_control* control);
76 			bool			_CheckBitmapIsUsedAt(off_t block) const;
77 			void			_SetCheckBitmapAt(off_t block);
78 
79 	static	status_t		_Initialize(BlockAllocator* self);
80 
81 			Volume*			fVolume;
82 			mutex			fLock;
83 			AllocationGroup* fGroups;
84 			int32			fNumGroups;
85 			uint32			fBlocksPerGroup;
86 			uint32			fNumBlocks;
87 
88 			uint32*			fCheckBitmap;
89 			check_cookie*	fCheckCookie;
90 };
91 
92 #ifdef BFS_DEBUGGER_COMMANDS
93 #if BFS_TRACING
94 int dump_block_allocator_blocks(int argc, char** argv);
95 #endif
96 int dump_block_allocator(int argc, char** argv);
97 #endif
98 
99 #endif	// BLOCK_ALLOCATOR_H
100