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