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