xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Volume.h (revision bab3f64413902761dc4b26c74a46c62a29bc6f86)
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>
276d244f23SAugustin Cavalier #include <lock.h>
28224e7c42SIngo Weinhold 
29aa085ffeSIngo Weinhold #include <util/DoublyLinkedList.h>
30aa085ffeSIngo Weinhold 
31224e7c42SIngo Weinhold #include "Entry.h"
32224e7c42SIngo Weinhold #include "List.h"
33224e7c42SIngo Weinhold #include "Query.h"
34224e7c42SIngo Weinhold #include "String.h"
35224e7c42SIngo Weinhold 
36224e7c42SIngo Weinhold class AllocationInfo;
37cbc07268SAugustin Cavalier class Attribute;
38cbc07268SAugustin Cavalier class AttributeIndex;
39224e7c42SIngo Weinhold class Directory;
40224e7c42SIngo Weinhold class DirectoryEntryTable;
41224e7c42SIngo Weinhold class Entry;
42224e7c42SIngo Weinhold class EntryListener;
43224e7c42SIngo Weinhold class EntryListenerTree;
44224e7c42SIngo Weinhold class Index;
45224e7c42SIngo Weinhold class IndexDirectory;
46224e7c42SIngo Weinhold class LastModifiedIndex;
47224e7c42SIngo Weinhold class NameIndex;
48224e7c42SIngo Weinhold class Node;
49224e7c42SIngo Weinhold class NodeListener;
50224e7c42SIngo Weinhold class NodeListenerTree;
51224e7c42SIngo Weinhold class NodeTable;
52224e7c42SIngo Weinhold class SizeIndex;
53224e7c42SIngo Weinhold 
54245aecdaSAxel Dörfler const ino_t kRootParentID = 0;
55224e7c42SIngo Weinhold 
56374d7a1eSAugustin Cavalier 
57224e7c42SIngo Weinhold class NodeListenerValue {
58224e7c42SIngo Weinhold public:
NodeListenerValue()596ad9efd6SIngo Weinhold 	inline NodeListenerValue() {}
NodeListenerValue(int)60224e7c42SIngo Weinhold 	inline NodeListenerValue(int) {}
NodeListenerValue(NodeListener * listener,Node * node,uint32 flags)61224e7c42SIngo Weinhold 	inline NodeListenerValue(NodeListener *listener, Node *node, uint32 flags)
62224e7c42SIngo Weinhold 		: listener(listener), node(node), flags(flags) {}
63224e7c42SIngo Weinhold 
64224e7c42SIngo Weinhold 	inline bool operator==(const NodeListenerValue &other)
65224e7c42SIngo Weinhold 		{ return listener == other.listener; }
66224e7c42SIngo Weinhold 
67224e7c42SIngo Weinhold 	NodeListener	*listener;
68224e7c42SIngo Weinhold 	Node			*node;
69224e7c42SIngo Weinhold 	uint32			flags;
70224e7c42SIngo Weinhold };
71224e7c42SIngo Weinhold typedef List<NodeListenerValue> NodeListenerList;
72224e7c42SIngo Weinhold 
73374d7a1eSAugustin Cavalier 
74224e7c42SIngo Weinhold class EntryListenerValue {
75224e7c42SIngo Weinhold public:
EntryListenerValue()766ad9efd6SIngo Weinhold 	inline EntryListenerValue() {}
EntryListenerValue(int)77224e7c42SIngo Weinhold 	inline EntryListenerValue(int) {}
EntryListenerValue(EntryListener * listener,Entry * entry,uint32 flags)78224e7c42SIngo Weinhold 	inline EntryListenerValue(EntryListener *listener, Entry *entry,
79224e7c42SIngo Weinhold 							  uint32 flags)
80224e7c42SIngo Weinhold 		: listener(listener), entry(entry), flags(flags) {}
81224e7c42SIngo Weinhold 
82224e7c42SIngo Weinhold 	inline bool operator==(const EntryListenerValue &other)
83224e7c42SIngo Weinhold 		{ return listener == other.listener; }
84224e7c42SIngo Weinhold 
85224e7c42SIngo Weinhold 	EntryListener	*listener;
86224e7c42SIngo Weinhold 	Entry			*entry;
87224e7c42SIngo Weinhold 	uint32			flags;
88224e7c42SIngo Weinhold };
89224e7c42SIngo Weinhold typedef List<EntryListenerValue> EntryListenerList;
90224e7c42SIngo Weinhold 
91374d7a1eSAugustin Cavalier 
92224e7c42SIngo Weinhold class Volume {
93224e7c42SIngo Weinhold public:
943de080c1SAlexander von Gluck IV 							Volume(fs_volume* volume);
95224e7c42SIngo Weinhold 							~Volume();
96224e7c42SIngo Weinhold 
9739313f22SAlexander von Gluck IV 	status_t Mount(uint32 flags);
98224e7c42SIngo Weinhold 	status_t Unmount();
99224e7c42SIngo Weinhold 
GetID()100c3e0275cSAugustin Cavalier 	dev_t GetID() const { return fVolume != NULL ? fVolume->id : -1; }
FSVolume()1016305a11cSAlexander von Gluck IV 	fs_volume* FSVolume() const { return fVolume; }
102224e7c42SIngo Weinhold 
103224e7c42SIngo Weinhold 	off_t CountBlocks() const;
104224e7c42SIngo Weinhold 	off_t CountFreeBlocks() const;
105224e7c42SIngo Weinhold 
106224e7c42SIngo Weinhold 	status_t SetName(const char *name);
107224e7c42SIngo Weinhold 	const char *GetName() const;
108224e7c42SIngo Weinhold 
GetRootDirectory()109224e7c42SIngo Weinhold 	Directory *GetRootDirectory() const		{ return fRootDirectory; }
110224e7c42SIngo Weinhold 
111224e7c42SIngo Weinhold 	status_t NewVNode(Node *node);
112c7a72423SIngo Weinhold 	status_t PublishVNode(Node *node);
113245aecdaSAxel Dörfler 	status_t GetVNode(ino_t id, Node **node);
114224e7c42SIngo Weinhold 	status_t GetVNode(Node *node);
115245aecdaSAxel Dörfler 	status_t PutVNode(ino_t id);
116224e7c42SIngo Weinhold 	status_t PutVNode(Node *node);
117224e7c42SIngo Weinhold 	status_t RemoveVNode(Node *node);
118224e7c42SIngo Weinhold 	status_t UnremoveVNode(Node *node);
119224e7c42SIngo Weinhold 
120224e7c42SIngo Weinhold 	// node table and listeners
121224e7c42SIngo Weinhold 	status_t NodeAdded(Node *node);
122224e7c42SIngo Weinhold 	status_t NodeRemoved(Node *node);
123245aecdaSAxel Dörfler 	status_t FindNode(ino_t id, Node **node);
124224e7c42SIngo Weinhold 	status_t AddNodeListener(NodeListener *listener, Node *node,
125224e7c42SIngo Weinhold 							 uint32 flags);
126224e7c42SIngo Weinhold 	status_t RemoveNodeListener(NodeListener *listener, Node *node);
127224e7c42SIngo Weinhold 
128224e7c42SIngo Weinhold 	// entry table and listeners
129245aecdaSAxel Dörfler 	status_t EntryAdded(ino_t id, Entry *entry);
130245aecdaSAxel Dörfler 	status_t EntryRemoved(ino_t id, Entry *entry);
131245aecdaSAxel Dörfler 	status_t FindEntry(ino_t id, const char *name, Entry **entry);
132224e7c42SIngo Weinhold 	status_t AddEntryListener(EntryListener *listener, Entry *entry,
133224e7c42SIngo Weinhold 							  uint32 flags);
134224e7c42SIngo Weinhold 	status_t RemoveEntryListener(EntryListener *listener, Entry *entry);
135224e7c42SIngo Weinhold 
136019d327dSAugustin Cavalier 	// node attributes
137245aecdaSAxel Dörfler 	status_t NodeAttributeAdded(ino_t id, Attribute *attribute);
138245aecdaSAxel Dörfler 	status_t NodeAttributeRemoved(ino_t id, Attribute *attribute);
139224e7c42SIngo Weinhold 
140224e7c42SIngo Weinhold 	// indices
GetIndexDirectory()141224e7c42SIngo Weinhold 	IndexDirectory *GetIndexDirectory() const	{ return fIndexDirectory; }
142224e7c42SIngo Weinhold 	NameIndex *GetNameIndex() const;
143224e7c42SIngo Weinhold 	LastModifiedIndex *GetLastModifiedIndex() const;
144224e7c42SIngo Weinhold 	SizeIndex *GetSizeIndex() const;
145224e7c42SIngo Weinhold 	Index *FindIndex(const char *name);
146224e7c42SIngo Weinhold 	AttributeIndex *FindAttributeIndex(const char *name, uint32 type);
147224e7c42SIngo Weinhold 
148224e7c42SIngo Weinhold 	// queries
149224e7c42SIngo Weinhold 	void AddQuery(Query *query);
150224e7c42SIngo Weinhold 	void RemoveQuery(Query *query);
151224e7c42SIngo Weinhold 	void UpdateLiveQueries(Entry *entry, Node* node, const char *attribute,
152224e7c42SIngo Weinhold 			int32 type, const uint8 *oldKey, size_t oldLength,
153224e7c42SIngo Weinhold 			const uint8 *newKey, size_t newLength);
154224e7c42SIngo Weinhold 
NextNodeID()155245aecdaSAxel Dörfler 	ino_t NextNodeID() { return fNextNodeID++; }
156224e7c42SIngo Weinhold 
157224e7c42SIngo Weinhold 	void GetAllocationInfo(AllocationInfo &info);
158224e7c42SIngo Weinhold 
GetAccessTime()159224e7c42SIngo Weinhold 	bigtime_t GetAccessTime() const	{ return fAccessTime; }
160224e7c42SIngo Weinhold 
161224e7c42SIngo Weinhold 	// locking
162224e7c42SIngo Weinhold 	bool ReadLock();
163224e7c42SIngo Weinhold 	void ReadUnlock();
164224e7c42SIngo Weinhold 	bool WriteLock();
165224e7c42SIngo Weinhold 	void WriteUnlock();
166224e7c42SIngo Weinhold 
167224e7c42SIngo Weinhold 	bool IteratorLock();
168224e7c42SIngo Weinhold 	void IteratorUnlock();
169224e7c42SIngo Weinhold 
AttributeIteratorLocker()170*bab3f644SAugustin Cavalier 	recursive_lock&	AttributeIteratorLocker() { return fAttributeIteratorLocker; }
171*bab3f644SAugustin Cavalier 
1726305a11cSAlexander von Gluck IV protected:
1736305a11cSAlexander von Gluck IV 	fs_volume*				fVolume;
1746305a11cSAlexander von Gluck IV 
175224e7c42SIngo Weinhold private:
176aa085ffeSIngo Weinhold 	typedef DoublyLinkedList<Query>	QueryList;
177224e7c42SIngo Weinhold 
178374d7a1eSAugustin Cavalier 	rw_lock					fLocker;
179374d7a1eSAugustin Cavalier 	String					fName;
180245aecdaSAxel Dörfler 	ino_t					fNextNodeID;
181224e7c42SIngo Weinhold 	NodeTable				*fNodeTable;
182224e7c42SIngo Weinhold 	DirectoryEntryTable		*fDirectoryEntryTable;
183224e7c42SIngo Weinhold 	IndexDirectory			*fIndexDirectory;
184224e7c42SIngo Weinhold 	Directory				*fRootDirectory;
185224e7c42SIngo Weinhold 	NodeListenerTree		*fNodeListeners;
186224e7c42SIngo Weinhold 	NodeListenerList		fAnyNodeListeners;
187224e7c42SIngo Weinhold 	EntryListenerTree		*fEntryListeners;
188224e7c42SIngo Weinhold 	EntryListenerList		fAnyEntryListeners;
189374d7a1eSAugustin Cavalier 
190374d7a1eSAugustin Cavalier 	recursive_lock			fIteratorLocker;
191*bab3f644SAugustin Cavalier 	recursive_lock			fAttributeIteratorLocker;
192374d7a1eSAugustin Cavalier 
193374d7a1eSAugustin Cavalier 	recursive_lock			fQueryLocker;
194224e7c42SIngo Weinhold 	QueryList				fQueries;
195374d7a1eSAugustin Cavalier 
196224e7c42SIngo Weinhold 	bigtime_t				fAccessTime;
197224e7c42SIngo Weinhold 	bool					fMounted;
198224e7c42SIngo Weinhold };
199224e7c42SIngo Weinhold 
200374d7a1eSAugustin Cavalier 
201224e7c42SIngo Weinhold #endif	// VOLUME_H
202