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