xref: /haiku/src/add-ons/kernel/file_systems/bfs/Volume.h (revision 3c6e2dd68577c34d93e17f19711f6245bf6d0915)
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 VOLUME_H
6 #define VOLUME_H
7 
8 
9 #include "system_dependencies.h"
10 
11 #include "bfs.h"
12 #include "BlockAllocator.h"
13 
14 class Journal;
15 class Inode;
16 class Query;
17 
18 enum volume_flags {
19 	VOLUME_READ_ONLY	= 0x0001
20 };
21 
22 enum volume_initialize_flags {
23 	VOLUME_NO_INDICES	= 0x0001,
24 };
25 
26 typedef DoublyLinkedList<Inode> InodeList;
27 
28 class Volume {
29 public:
30 							Volume(fs_volume* volume);
31 							~Volume();
32 
33 			status_t		Mount(const char* device, uint32 flags);
34 			status_t		Unmount();
35 			status_t		Initialize(int fd, const char* name,
36 								uint32 blockSize, uint32 flags);
37 
38 			bool			IsInitializing() const { return fVolume == NULL; }
39 
40 			bool			IsValidSuperBlock();
41 			bool			IsReadOnly() const;
42 			void			Panic();
43 			mutex&			Lock();
44 
45 			block_run		Root() const { return fSuperBlock.root_dir; }
46 			Inode*			RootNode() const { return fRootNode; }
47 			block_run		Indices() const { return fSuperBlock.indices; }
48 			Inode*			IndicesNode() const { return fIndicesNode; }
49 			block_run		Log() const { return fSuperBlock.log_blocks; }
50 			vint32&			LogStart() { return fLogStart; }
51 			vint32&			LogEnd() { return fLogEnd; }
52 			int				Device() const { return fDevice; }
53 
54 			dev_t			ID() const { return fVolume ? fVolume->id : -1; }
55 			fs_volume*		FSVolume() const { return fVolume; }
56 			const char*		Name() const { return fSuperBlock.name; }
57 
58 			off_t			NumBlocks() const
59 								{ return fSuperBlock.NumBlocks(); }
60 			off_t			UsedBlocks() const
61 								{ return fSuperBlock.UsedBlocks(); }
62 			off_t			FreeBlocks() const
63 								{ return NumBlocks() - UsedBlocks(); }
64 
65 			uint32			BlockSize() const { return fBlockSize; }
66 			uint32			BlockShift() const { return fBlockShift; }
67 			uint32			InodeSize() const
68 								{ return fSuperBlock.InodeSize(); }
69 			uint32			AllocationGroups() const
70 								{ return fSuperBlock.AllocationGroups(); }
71 			uint32			AllocationGroupShift() const
72 								{ return fAllocationGroupShift; }
73 			disk_super_block& SuperBlock() { return fSuperBlock; }
74 
75 			off_t			ToOffset(block_run run) const
76 								{ return ToBlock(run) << BlockShift(); }
77 			off_t			ToBlock(block_run run) const
78 								{ return ((((off_t)run.AllocationGroup())
79 										<< AllocationGroupShift())
80 									| (off_t)run.Start()); }
81 			block_run		ToBlockRun(off_t block) const;
82 			status_t		ValidateBlockRun(block_run run);
83 
84 			off_t			ToVnode(block_run run) const
85 								{ return ToBlock(run); }
86 			off_t			ToVnode(off_t block) const { return block; }
87 			off_t			VnodeToBlock(ino_t id) const { return (off_t)id; }
88 
89 			status_t		CreateIndicesRoot(Transaction& transaction);
90 
91 			InodeList&		RemovedInodes() { return fRemovedInodes; }
92 				// This list is guarded by the transaction lock
93 
94 			// block bitmap
95 			BlockAllocator&	Allocator();
96 			status_t		AllocateForInode(Transaction& transaction,
97 								const Inode* parent, mode_t type,
98 								block_run& run);
99 			status_t		AllocateForInode(Transaction& transaction,
100 								const block_run* parent, mode_t type,
101 								block_run& run);
102 			status_t		Allocate(Transaction& transaction, Inode* inode,
103 								off_t numBlocks, block_run& run,
104 								uint16 minimum = 1);
105 			status_t		Free(Transaction& transaction, block_run run);
106 			void			SetCheckingThread(thread_id thread)
107 								{ fCheckingThread = thread; }
108 			bool			IsCheckingThread() const
109 								{ return find_thread(NULL) == fCheckingThread; }
110 
111 			// cache access
112 			status_t		WriteSuperBlock();
113 			status_t		FlushDevice();
114 
115 			// queries
116 			void			UpdateLiveQueries(Inode* inode,
117 								const char* attribute, int32 type,
118 								const uint8* oldKey, size_t oldLength,
119 								const uint8* newKey, size_t newLength);
120 			void			UpdateLiveQueriesRenameMove(Inode* inode,
121 								ino_t oldDirectoryID, const char* oldName,
122 								ino_t newDirectoryID, const char* newName);
123 
124 			bool			CheckForLiveQuery(const char* attribute);
125 			void			AddQuery(Query* query);
126 			void			RemoveQuery(Query* query);
127 
128 			status_t		Sync();
129 			Journal*		GetJournal(off_t refBlock) const;
130 
131 			void*			BlockCache() { return fBlockCache; }
132 
133 	static	status_t		CheckSuperBlock(const uint8* data,
134 								uint32* _offset = NULL);
135 	static	status_t		Identify(int fd, disk_super_block* superBlock);
136 
137 protected:
138 			fs_volume*		fVolume;
139 			int				fDevice;
140 			disk_super_block fSuperBlock;
141 
142 			uint32			fBlockSize;
143 			uint32			fBlockShift;
144 			uint32			fAllocationGroupShift;
145 
146 			BlockAllocator	fBlockAllocator;
147 			mutex			fLock;
148 			Journal*		fJournal;
149 			vint32			fLogStart;
150 			vint32			fLogEnd;
151 
152 			Inode*			fRootNode;
153 			Inode*			fIndicesNode;
154 
155 			vint32			fDirtyCachedBlocks;
156 
157 			mutex			fQueryLock;
158 			SinglyLinkedList<Query> fQueries;
159 
160 			uint32			fFlags;
161 
162 			void*			fBlockCache;
163 			thread_id		fCheckingThread;
164 
165 			InodeList		fRemovedInodes;
166 };
167 
168 
169 // inline functions
170 
171 inline bool
172 Volume::IsReadOnly() const
173 {
174 	 return fFlags & VOLUME_READ_ONLY;
175 }
176 
177 
178 inline mutex&
179 Volume::Lock()
180 {
181 	 return fLock;
182 }
183 
184 
185 inline BlockAllocator&
186 Volume::Allocator()
187 {
188 	 return fBlockAllocator;
189 }
190 
191 
192 inline status_t
193 Volume::AllocateForInode(Transaction& transaction, const block_run* parent,
194 	mode_t type, block_run& run)
195 {
196 	return fBlockAllocator.AllocateForInode(transaction, parent, type, run);
197 }
198 
199 
200 inline status_t
201 Volume::Allocate(Transaction& transaction, Inode* inode, off_t numBlocks,
202 	block_run& run, uint16 minimum)
203 {
204 	return fBlockAllocator.Allocate(transaction, inode, numBlocks, run,
205 		minimum);
206 }
207 
208 
209 inline status_t
210 Volume::Free(Transaction& transaction, block_run run)
211 {
212 	return fBlockAllocator.Free(transaction, run);
213 }
214 
215 
216 inline status_t
217 Volume::FlushDevice()
218 {
219 	return block_cache_sync(fBlockCache);
220 }
221 
222 
223 inline Journal*
224 Volume::GetJournal(off_t /*refBlock*/) const
225 {
226 	 return fJournal;
227 }
228 
229 
230 #endif	// VOLUME_H
231