xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Volume.h (revision 3de080c1861217c806c494b8ff797c064fba2a0a)
1224e7c42SIngo Weinhold // Volume.h
2224e7c42SIngo Weinhold //
3224e7c42SIngo Weinhold // Copyright (c) 2003, Ingo Weinhold (bonefish@cs.tu-berlin.de)
4224e7c42SIngo Weinhold //
5224e7c42SIngo Weinhold // This program is free software; you can redistribute it and/or modify
6224e7c42SIngo Weinhold // it under the terms of the GNU General Public License as published by
7224e7c42SIngo Weinhold // the Free Software Foundation; either version 2 of the License, or
8224e7c42SIngo Weinhold // (at your option) any later version.
9224e7c42SIngo Weinhold //
10224e7c42SIngo Weinhold // This program is distributed in the hope that it will be useful,
11224e7c42SIngo Weinhold // but WITHOUT ANY WARRANTY; without even the implied warranty of
12224e7c42SIngo Weinhold // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13224e7c42SIngo Weinhold // GNU General Public License for more details.
14224e7c42SIngo Weinhold //
15224e7c42SIngo Weinhold // You should have received a copy of the GNU General Public License
16224e7c42SIngo Weinhold // along with this program; if not, write to the Free Software
17224e7c42SIngo Weinhold // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18224e7c42SIngo Weinhold //
19224e7c42SIngo Weinhold // You can alternatively use *this file* under the terms of the the MIT
20224e7c42SIngo Weinhold // license included in this package.
21224e7c42SIngo Weinhold 
22224e7c42SIngo Weinhold #ifndef VOLUME_H
23224e7c42SIngo Weinhold #define VOLUME_H
24224e7c42SIngo Weinhold 
25c7a72423SIngo Weinhold #include <fs_interface.h>
26224e7c42SIngo Weinhold #include <SupportDefs.h>
27224e7c42SIngo Weinhold 
28aa085ffeSIngo Weinhold #include <util/DoublyLinkedList.h>
29aa085ffeSIngo Weinhold 
30224e7c42SIngo Weinhold #include "Entry.h"
31224e7c42SIngo Weinhold #include "List.h"
32224e7c42SIngo Weinhold #include "Locker.h"
33224e7c42SIngo Weinhold #include "Query.h"
34224e7c42SIngo Weinhold #include "String.h"
35224e7c42SIngo Weinhold 
36224e7c42SIngo Weinhold class AllocationInfo;
37224e7c42SIngo Weinhold class Block;
38224e7c42SIngo Weinhold class BlockAllocator;
39224e7c42SIngo Weinhold class BlockReference;
40224e7c42SIngo Weinhold class Directory;
41224e7c42SIngo Weinhold class DirectoryEntryTable;
42224e7c42SIngo Weinhold class Entry;
43224e7c42SIngo Weinhold class EntryListener;
44224e7c42SIngo Weinhold class EntryListenerTree;
45224e7c42SIngo Weinhold class Index;
46224e7c42SIngo Weinhold class IndexDirectory;
47224e7c42SIngo Weinhold class LastModifiedIndex;
48224e7c42SIngo Weinhold class NameIndex;
49224e7c42SIngo Weinhold class Node;
50224e7c42SIngo Weinhold class NodeAttributeTable;
51224e7c42SIngo Weinhold class NodeListener;
52224e7c42SIngo Weinhold class NodeListenerTree;
53224e7c42SIngo Weinhold class NodeTable;
54224e7c42SIngo Weinhold class SizeIndex;
55224e7c42SIngo Weinhold 
56245aecdaSAxel Dörfler const ino_t kRootParentID = 0;
57224e7c42SIngo Weinhold 
58224e7c42SIngo Weinhold // NodeListenerValue
59224e7c42SIngo Weinhold class NodeListenerValue {
60224e7c42SIngo Weinhold public:
616ad9efd6SIngo Weinhold 	inline NodeListenerValue() {}
62224e7c42SIngo Weinhold 	inline NodeListenerValue(int) {}
63224e7c42SIngo Weinhold 	inline NodeListenerValue(NodeListener *listener, Node *node, uint32 flags)
64224e7c42SIngo Weinhold 		: listener(listener), node(node), flags(flags) {}
65224e7c42SIngo Weinhold 
66224e7c42SIngo Weinhold 	inline bool operator==(const NodeListenerValue &other)
67224e7c42SIngo Weinhold 		{ return listener == other.listener; }
68224e7c42SIngo Weinhold 
69224e7c42SIngo Weinhold 	NodeListener	*listener;
70224e7c42SIngo Weinhold 	Node			*node;
71224e7c42SIngo Weinhold 	uint32			flags;
72224e7c42SIngo Weinhold };
73224e7c42SIngo Weinhold typedef List<NodeListenerValue> NodeListenerList;
74224e7c42SIngo Weinhold 
75224e7c42SIngo Weinhold // EntryListenerValue
76224e7c42SIngo Weinhold class EntryListenerValue {
77224e7c42SIngo Weinhold public:
786ad9efd6SIngo Weinhold 	inline EntryListenerValue() {}
79224e7c42SIngo Weinhold 	inline EntryListenerValue(int) {}
80224e7c42SIngo Weinhold 	inline EntryListenerValue(EntryListener *listener, Entry *entry,
81224e7c42SIngo Weinhold 							  uint32 flags)
82224e7c42SIngo Weinhold 		: listener(listener), entry(entry), flags(flags) {}
83224e7c42SIngo Weinhold 
84224e7c42SIngo Weinhold 	inline bool operator==(const EntryListenerValue &other)
85224e7c42SIngo Weinhold 		{ return listener == other.listener; }
86224e7c42SIngo Weinhold 
87224e7c42SIngo Weinhold 	EntryListener	*listener;
88224e7c42SIngo Weinhold 	Entry			*entry;
89224e7c42SIngo Weinhold 	uint32			flags;
90224e7c42SIngo Weinhold };
91224e7c42SIngo Weinhold typedef List<EntryListenerValue> EntryListenerList;
92224e7c42SIngo Weinhold 
93224e7c42SIngo Weinhold // Volume
94224e7c42SIngo Weinhold class Volume {
95224e7c42SIngo Weinhold public:
96*3de080c1SAlexander von Gluck IV 							Volume(fs_volume* volume);
97224e7c42SIngo Weinhold 							~Volume();
98224e7c42SIngo Weinhold 
99245aecdaSAxel Dörfler 	status_t Mount(dev_t nsid);
100224e7c42SIngo Weinhold 	status_t Unmount();
101224e7c42SIngo Weinhold 
102245aecdaSAxel Dörfler 	dev_t GetID() const { return fID; }
103224e7c42SIngo Weinhold 
104224e7c42SIngo Weinhold 	off_t GetBlockSize() const;
105224e7c42SIngo Weinhold 	off_t CountBlocks() const;
106224e7c42SIngo Weinhold 	off_t CountFreeBlocks() const;
107224e7c42SIngo Weinhold 
108224e7c42SIngo Weinhold 	status_t SetName(const char *name);
109224e7c42SIngo Weinhold 	const char *GetName() const;
110224e7c42SIngo Weinhold 
111224e7c42SIngo Weinhold 	Directory *GetRootDirectory() const		{ return fRootDirectory; }
112224e7c42SIngo Weinhold 
113224e7c42SIngo Weinhold 	status_t NewVNode(Node *node);
114c7a72423SIngo Weinhold 	status_t PublishVNode(Node *node);
115245aecdaSAxel Dörfler 	status_t GetVNode(ino_t id, Node **node);
116224e7c42SIngo Weinhold 	status_t GetVNode(Node *node);
117245aecdaSAxel Dörfler 	status_t PutVNode(ino_t id);
118224e7c42SIngo Weinhold 	status_t PutVNode(Node *node);
119224e7c42SIngo Weinhold 	status_t RemoveVNode(Node *node);
120224e7c42SIngo Weinhold 	status_t UnremoveVNode(Node *node);
121224e7c42SIngo Weinhold 
122224e7c42SIngo Weinhold 	// node table and listeners
123224e7c42SIngo Weinhold 	status_t NodeAdded(Node *node);
124224e7c42SIngo Weinhold 	status_t NodeRemoved(Node *node);
125245aecdaSAxel Dörfler 	status_t FindNode(ino_t id, Node **node);
126224e7c42SIngo Weinhold 	status_t AddNodeListener(NodeListener *listener, Node *node,
127224e7c42SIngo Weinhold 							 uint32 flags);
128224e7c42SIngo Weinhold 	status_t RemoveNodeListener(NodeListener *listener, Node *node);
129224e7c42SIngo Weinhold 
130224e7c42SIngo Weinhold 	// entry table and listeners
131245aecdaSAxel Dörfler 	status_t EntryAdded(ino_t id, Entry *entry);
132245aecdaSAxel Dörfler 	status_t EntryRemoved(ino_t id, Entry *entry);
133245aecdaSAxel Dörfler 	status_t FindEntry(ino_t id, const char *name, Entry **entry);
134224e7c42SIngo Weinhold 	status_t AddEntryListener(EntryListener *listener, Entry *entry,
135224e7c42SIngo Weinhold 							  uint32 flags);
136224e7c42SIngo Weinhold 	status_t RemoveEntryListener(EntryListener *listener, Entry *entry);
137224e7c42SIngo Weinhold 
138224e7c42SIngo Weinhold 	// node attribute table
139245aecdaSAxel Dörfler 	status_t NodeAttributeAdded(ino_t id, Attribute *attribute);
140245aecdaSAxel Dörfler 	status_t NodeAttributeRemoved(ino_t id, Attribute *attribute);
141245aecdaSAxel Dörfler 	status_t FindNodeAttribute(ino_t id, const char *name,
142224e7c42SIngo Weinhold 							   Attribute **attribute);
143224e7c42SIngo Weinhold 
144224e7c42SIngo Weinhold 	// indices
145224e7c42SIngo Weinhold 	IndexDirectory *GetIndexDirectory() const	{ return fIndexDirectory; }
146224e7c42SIngo Weinhold 	NameIndex *GetNameIndex() const;
147224e7c42SIngo Weinhold 	LastModifiedIndex *GetLastModifiedIndex() const;
148224e7c42SIngo Weinhold 	SizeIndex *GetSizeIndex() const;
149224e7c42SIngo Weinhold 	Index *FindIndex(const char *name);
150224e7c42SIngo Weinhold 	AttributeIndex *FindAttributeIndex(const char *name, uint32 type);
151224e7c42SIngo Weinhold 
152224e7c42SIngo Weinhold 	// queries
153224e7c42SIngo Weinhold 	void AddQuery(Query *query);
154224e7c42SIngo Weinhold 	void RemoveQuery(Query *query);
155224e7c42SIngo Weinhold 	void UpdateLiveQueries(Entry *entry, Node* node, const char *attribute,
156224e7c42SIngo Weinhold 			int32 type, const uint8 *oldKey, size_t oldLength,
157224e7c42SIngo Weinhold 			const uint8 *newKey, size_t newLength);
158224e7c42SIngo Weinhold 
159245aecdaSAxel Dörfler 	ino_t NextNodeID() { return fNextNodeID++; }
160224e7c42SIngo Weinhold 
161224e7c42SIngo Weinhold 	status_t AllocateBlock(size_t size, BlockReference **block);
162224e7c42SIngo Weinhold 	void FreeBlock(BlockReference *block);
163224e7c42SIngo Weinhold 	BlockReference *ResizeBlock(BlockReference *block, size_t size);
164224e7c42SIngo Weinhold 	// debugging only
165224e7c42SIngo Weinhold 	bool CheckBlock(BlockReference *block, size_t size = 0);
166224e7c42SIngo Weinhold 	void GetAllocationInfo(AllocationInfo &info);
167224e7c42SIngo Weinhold 
168224e7c42SIngo Weinhold 	bigtime_t GetAccessTime() const	{ return fAccessTime; }
169224e7c42SIngo Weinhold 
170224e7c42SIngo Weinhold 	// locking
171224e7c42SIngo Weinhold 	bool ReadLock();
172224e7c42SIngo Weinhold 	void ReadUnlock();
173224e7c42SIngo Weinhold 	bool WriteLock();
174224e7c42SIngo Weinhold 	void WriteUnlock();
175224e7c42SIngo Weinhold 
176224e7c42SIngo Weinhold 	bool IteratorLock();
177224e7c42SIngo Weinhold 	void IteratorUnlock();
178224e7c42SIngo Weinhold 
179224e7c42SIngo Weinhold private:
180aa085ffeSIngo Weinhold 	typedef DoublyLinkedList<Query>	QueryList;
181224e7c42SIngo Weinhold 
182245aecdaSAxel Dörfler 	dev_t					fID;
183245aecdaSAxel Dörfler 	ino_t					fNextNodeID;
184224e7c42SIngo Weinhold 	NodeTable				*fNodeTable;
185224e7c42SIngo Weinhold 	DirectoryEntryTable		*fDirectoryEntryTable;
186224e7c42SIngo Weinhold 	NodeAttributeTable		*fNodeAttributeTable;
187224e7c42SIngo Weinhold 	IndexDirectory			*fIndexDirectory;
188224e7c42SIngo Weinhold 	Directory				*fRootDirectory;
189224e7c42SIngo Weinhold 	String					fName;
190224e7c42SIngo Weinhold 	Locker					fLocker;
191224e7c42SIngo Weinhold 	Locker					fIteratorLocker;
192224e7c42SIngo Weinhold 	Locker					fQueryLocker;
193224e7c42SIngo Weinhold 	NodeListenerTree		*fNodeListeners;
194224e7c42SIngo Weinhold 	NodeListenerList		fAnyNodeListeners;
195224e7c42SIngo Weinhold 	EntryListenerTree		*fEntryListeners;
196224e7c42SIngo Weinhold 	EntryListenerList		fAnyEntryListeners;
197224e7c42SIngo Weinhold 	QueryList				fQueries;
198224e7c42SIngo Weinhold 	BlockAllocator			*fBlockAllocator;
199224e7c42SIngo Weinhold 	off_t					fBlockSize;
200224e7c42SIngo Weinhold 	off_t					fAllocatedBlocks;
201224e7c42SIngo Weinhold 	bigtime_t				fAccessTime;
202224e7c42SIngo Weinhold 	bool					fMounted;
203224e7c42SIngo Weinhold };
204224e7c42SIngo Weinhold 
205224e7c42SIngo Weinhold #endif	// VOLUME_H
206