xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Volume.h (revision 1b8f7f13a3dc70e0e903cb94248220b40b732204)
1 // Volume.h
2 //
3 // Copyright (c) 2003, Ingo Weinhold (bonefish@cs.tu-berlin.de)
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //
19 // You can alternatively use *this file* under the terms of the the MIT
20 // license included in this package.
21 
22 #ifndef VOLUME_H
23 #define VOLUME_H
24 
25 #include <fs_interface.h>
26 #include <SupportDefs.h>
27 
28 #include "DLList.h"
29 #include "Entry.h"
30 #include "List.h"
31 #include "Locker.h"
32 #include "Query.h"
33 #include "String.h"
34 
35 class AllocationInfo;
36 class Block;
37 class BlockAllocator;
38 class BlockReference;
39 class Directory;
40 class DirectoryEntryTable;
41 class Entry;
42 class EntryListener;
43 class EntryListenerTree;
44 class Index;
45 class IndexDirectory;
46 class LastModifiedIndex;
47 class NameIndex;
48 class Node;
49 class NodeAttributeTable;
50 class NodeListener;
51 class NodeListenerTree;
52 class NodeTable;
53 class SizeIndex;
54 
55 const ino_t kRootParentID = 0;
56 
57 // NodeListenerValue
58 class NodeListenerValue {
59 public:
60 	inline NodeListenerValue() {}
61 	inline NodeListenerValue(int) {}
62 	inline NodeListenerValue(NodeListener *listener, Node *node, uint32 flags)
63 		: listener(listener), node(node), flags(flags) {}
64 
65 	inline bool operator==(const NodeListenerValue &other)
66 		{ return listener == other.listener; }
67 
68 	NodeListener	*listener;
69 	Node			*node;
70 	uint32			flags;
71 };
72 typedef List<NodeListenerValue> NodeListenerList;
73 
74 // EntryListenerValue
75 class EntryListenerValue {
76 public:
77 	inline EntryListenerValue() {}
78 	inline EntryListenerValue(int) {}
79 	inline EntryListenerValue(EntryListener *listener, Entry *entry,
80 							  uint32 flags)
81 		: listener(listener), entry(entry), flags(flags) {}
82 
83 	inline bool operator==(const EntryListenerValue &other)
84 		{ return listener == other.listener; }
85 
86 	EntryListener	*listener;
87 	Entry			*entry;
88 	uint32			flags;
89 };
90 typedef List<EntryListenerValue> EntryListenerList;
91 
92 // Volume
93 class Volume {
94 public:
95 	Volume();
96 	~Volume();
97 
98 	status_t Mount(dev_t nsid);
99 	status_t Unmount();
100 
101 	dev_t GetID() const { return fID; }
102 
103 	off_t GetBlockSize() const;
104 	off_t CountBlocks() const;
105 	off_t CountFreeBlocks() const;
106 
107 	status_t SetName(const char *name);
108 	const char *GetName() const;
109 
110 	Directory *GetRootDirectory() const		{ return fRootDirectory; }
111 
112 	status_t NewVNode(Node *node);
113 	status_t PublishVNode(Node *node);
114 	status_t GetVNode(ino_t id, Node **node);
115 	status_t GetVNode(Node *node);
116 	status_t PutVNode(ino_t id);
117 	status_t PutVNode(Node *node);
118 	status_t RemoveVNode(Node *node);
119 	status_t UnremoveVNode(Node *node);
120 
121 	// node table and listeners
122 	status_t NodeAdded(Node *node);
123 	status_t NodeRemoved(Node *node);
124 	status_t FindNode(ino_t id, Node **node);
125 	status_t AddNodeListener(NodeListener *listener, Node *node,
126 							 uint32 flags);
127 	status_t RemoveNodeListener(NodeListener *listener, Node *node);
128 
129 	// entry table and listeners
130 	status_t EntryAdded(ino_t id, Entry *entry);
131 	status_t EntryRemoved(ino_t id, Entry *entry);
132 	status_t FindEntry(ino_t id, const char *name, Entry **entry);
133 	status_t AddEntryListener(EntryListener *listener, Entry *entry,
134 							  uint32 flags);
135 	status_t RemoveEntryListener(EntryListener *listener, Entry *entry);
136 
137 	// node attribute table
138 	status_t NodeAttributeAdded(ino_t id, Attribute *attribute);
139 	status_t NodeAttributeRemoved(ino_t id, Attribute *attribute);
140 	status_t FindNodeAttribute(ino_t id, const char *name,
141 							   Attribute **attribute);
142 
143 	// indices
144 	IndexDirectory *GetIndexDirectory() const	{ return fIndexDirectory; }
145 	NameIndex *GetNameIndex() const;
146 	LastModifiedIndex *GetLastModifiedIndex() const;
147 	SizeIndex *GetSizeIndex() const;
148 	Index *FindIndex(const char *name);
149 	AttributeIndex *FindAttributeIndex(const char *name, uint32 type);
150 
151 	// queries
152 	void AddQuery(Query *query);
153 	void RemoveQuery(Query *query);
154 	void UpdateLiveQueries(Entry *entry, Node* node, const char *attribute,
155 			int32 type, const uint8 *oldKey, size_t oldLength,
156 			const uint8 *newKey, size_t newLength);
157 
158 	ino_t NextNodeID() { return fNextNodeID++; }
159 
160 	status_t AllocateBlock(size_t size, BlockReference **block);
161 	void FreeBlock(BlockReference *block);
162 	BlockReference *ResizeBlock(BlockReference *block, size_t size);
163 	// debugging only
164 	bool CheckBlock(BlockReference *block, size_t size = 0);
165 	void GetAllocationInfo(AllocationInfo &info);
166 
167 	bigtime_t GetAccessTime() const	{ return fAccessTime; }
168 
169 	// locking
170 	bool ReadLock();
171 	void ReadUnlock();
172 	bool WriteLock();
173 	void WriteUnlock();
174 
175 	bool IteratorLock();
176 	void IteratorUnlock();
177 
178 private:
179 	typedef DLList<Query>	QueryList;
180 
181 	dev_t					fID;
182 	ino_t					fNextNodeID;
183 	NodeTable				*fNodeTable;
184 	DirectoryEntryTable		*fDirectoryEntryTable;
185 	NodeAttributeTable		*fNodeAttributeTable;
186 	IndexDirectory			*fIndexDirectory;
187 	Directory				*fRootDirectory;
188 	String					fName;
189 	Locker					fLocker;
190 	Locker					fIteratorLocker;
191 	Locker					fQueryLocker;
192 	NodeListenerTree		*fNodeListeners;
193 	NodeListenerList		fAnyNodeListeners;
194 	EntryListenerTree		*fEntryListeners;
195 	EntryListenerList		fAnyEntryListeners;
196 	QueryList				fQueries;
197 	BlockAllocator			*fBlockAllocator;
198 	off_t					fBlockSize;
199 	off_t					fAllocatedBlocks;
200 	bigtime_t				fAccessTime;
201 	bool					fMounted;
202 };
203 
204 #endif	// VOLUME_H
205