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 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 Trim(uint64 offset, uint64 size, 50 uint64& trimmedSize); 51 52 status_t StartChecking(const check_control* control); 53 status_t StopChecking(check_control* control); 54 status_t CheckNextNode(check_control* control); 55 56 status_t CheckBlocks(off_t start, off_t length, 57 bool allocated = true); 58 status_t CheckBlockRun(block_run run, 59 const char* type = NULL, 60 bool allocated = true); 61 status_t CheckInode(Inode* inode, const char* name); 62 63 size_t BitmapSize() const; 64 65 #ifdef BFS_DEBUGGER_COMMANDS 66 void Dump(int32 index); 67 #endif 68 #ifdef DEBUG_FRAGMENTER 69 void Fragment(); 70 #endif 71 72 private: 73 status_t _RemoveInvalidNode(Inode* parent, BPlusTree* tree, 74 Inode* inode, const char* name); 75 #ifdef DEBUG_ALLOCATION_GROUPS 76 void _CheckGroup(int32 group) const; 77 #endif 78 bool _IsValidCheckControl(const check_control* control); 79 bool _CheckBitmapIsUsedAt(off_t block) const; 80 void _SetCheckBitmapAt(off_t block); 81 status_t _CheckInodeBlocks(Inode* inode, const char* name); 82 status_t _FinishBitmapPass(); 83 status_t _PrepareIndices(); 84 void _FreeIndices(); 85 status_t _AddInodeToIndex(Inode* inode); 86 status_t _WriteBackCheckBitmap(); 87 status_t _AddTrim(fs_trim_data& trimData, uint32 maxRanges, 88 uint64 offset, uint64 size); 89 status_t _TrimNext(fs_trim_data& trimData, uint32 maxRanges, 90 uint64 offset, uint64 size, bool force, 91 uint64& trimmedSize); 92 93 static status_t _Initialize(BlockAllocator* self); 94 95 private: 96 Volume* fVolume; 97 recursive_lock fLock; 98 AllocationGroup* fGroups; 99 int32 fNumGroups; 100 uint32 fBlocksPerGroup; 101 uint32 fNumBlocks; 102 103 uint32* fCheckBitmap; 104 check_cookie* fCheckCookie; 105 }; 106 107 #ifdef BFS_DEBUGGER_COMMANDS 108 #if BFS_TRACING 109 int dump_block_allocator_blocks(int argc, char** argv); 110 #endif 111 int dump_block_allocator(int argc, char** argv); 112 #endif 113 114 #endif // BLOCK_ALLOCATOR_H 115