xref: /haiku/src/add-ons/kernel/file_systems/bfs/BlockAllocator.h (revision 5b189b0e1e2f51f367bfcb126b2f00a3702f352d)
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 
57 			recursive_lock&	Lock() { return fLock; }
58 
59 #ifdef BFS_DEBUGGER_COMMANDS
60 			void			Dump(int32 index);
61 #endif
62 #ifdef DEBUG_FRAGMENTER
63 			void			Fragment();
64 #endif
65 
66 private:
67 #ifdef DEBUG_ALLOCATION_GROUPS
68 			void			_CheckGroup(int32 group) const;
69 #endif
70 			status_t		_AddTrim(fs_trim_data& trimData, uint32 maxRanges,
71 								uint64 offset, uint64 size);
72 			status_t		_TrimNext(fs_trim_data& trimData, uint32 maxRanges,
73 								uint64 offset, uint64 size, bool force,
74 								uint64& trimmedSize);
75 
76 	static	status_t		_Initialize(BlockAllocator* self);
77 
78 private:
79 			Volume*			fVolume;
80 			recursive_lock	fLock;
81 			AllocationGroup* fGroups;
82 			int32			fNumGroups;
83 			uint32			fBlocksPerGroup;
84 			uint32			fNumBlocks;
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