xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Volume.h (revision 6d244f23b8667c0e4678b16aec1f4e55d44a2e51)
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 #include <lock.h>
28 
29 #include <util/DoublyLinkedList.h>
30 
31 #include "Entry.h"
32 #include "List.h"
33 #include "Query.h"
34 #include "String.h"
35 
36 class AllocationInfo;
37 class Block;
38 class BlockAllocator;
39 class BlockReference;
40 class Directory;
41 class DirectoryEntryTable;
42 class Entry;
43 class EntryListener;
44 class EntryListenerTree;
45 class Index;
46 class IndexDirectory;
47 class LastModifiedIndex;
48 class NameIndex;
49 class Node;
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(fs_volume* volume);
96 							~Volume();
97 
98 	status_t Mount(uint32 flags);
99 	status_t Unmount();
100 
101 	dev_t GetID() const { return fVolume != NULL ? fVolume->id : -1; }
102 	fs_volume* FSVolume() const { return fVolume; }
103 
104 	off_t GetBlockSize() const;
105 	off_t CountBlocks() const;
106 	off_t CountFreeBlocks() const;
107 
108 	status_t SetName(const char *name);
109 	const char *GetName() const;
110 
111 	Directory *GetRootDirectory() const		{ return fRootDirectory; }
112 
113 	status_t NewVNode(Node *node);
114 	status_t PublishVNode(Node *node);
115 	status_t GetVNode(ino_t id, Node **node);
116 	status_t GetVNode(Node *node);
117 	status_t PutVNode(ino_t id);
118 	status_t PutVNode(Node *node);
119 	status_t RemoveVNode(Node *node);
120 	status_t UnremoveVNode(Node *node);
121 
122 	// node table and listeners
123 	status_t NodeAdded(Node *node);
124 	status_t NodeRemoved(Node *node);
125 	status_t FindNode(ino_t id, Node **node);
126 	status_t AddNodeListener(NodeListener *listener, Node *node,
127 							 uint32 flags);
128 	status_t RemoveNodeListener(NodeListener *listener, Node *node);
129 
130 	// entry table and listeners
131 	status_t EntryAdded(ino_t id, Entry *entry);
132 	status_t EntryRemoved(ino_t id, Entry *entry);
133 	status_t FindEntry(ino_t id, const char *name, Entry **entry);
134 	status_t AddEntryListener(EntryListener *listener, Entry *entry,
135 							  uint32 flags);
136 	status_t RemoveEntryListener(EntryListener *listener, Entry *entry);
137 
138 	// node attributes
139 	status_t NodeAttributeAdded(ino_t id, Attribute *attribute);
140 	status_t NodeAttributeRemoved(ino_t id, Attribute *attribute);
141 
142 	// indices
143 	IndexDirectory *GetIndexDirectory() const	{ return fIndexDirectory; }
144 	NameIndex *GetNameIndex() const;
145 	LastModifiedIndex *GetLastModifiedIndex() const;
146 	SizeIndex *GetSizeIndex() const;
147 	Index *FindIndex(const char *name);
148 	AttributeIndex *FindAttributeIndex(const char *name, uint32 type);
149 
150 	// queries
151 	void AddQuery(Query *query);
152 	void RemoveQuery(Query *query);
153 	void UpdateLiveQueries(Entry *entry, Node* node, const char *attribute,
154 			int32 type, const uint8 *oldKey, size_t oldLength,
155 			const uint8 *newKey, size_t newLength);
156 
157 	ino_t NextNodeID() { return fNextNodeID++; }
158 
159 	status_t AllocateBlock(size_t size, BlockReference **block);
160 	void FreeBlock(BlockReference *block);
161 	BlockReference *ResizeBlock(BlockReference *block, size_t size);
162 	// debugging only
163 	bool CheckBlock(BlockReference *block, size_t size = 0);
164 	void GetAllocationInfo(AllocationInfo &info);
165 
166 	bigtime_t GetAccessTime() const	{ return fAccessTime; }
167 
168 	// locking
169 	bool ReadLock();
170 	void ReadUnlock();
171 	bool WriteLock();
172 	void WriteUnlock();
173 
174 	bool IteratorLock();
175 	void IteratorUnlock();
176 
177 protected:
178 	fs_volume*				fVolume;
179 
180 private:
181 	typedef DoublyLinkedList<Query>	QueryList;
182 
183 	ino_t					fNextNodeID;
184 	NodeTable				*fNodeTable;
185 	DirectoryEntryTable		*fDirectoryEntryTable;
186 	IndexDirectory			*fIndexDirectory;
187 	Directory				*fRootDirectory;
188 	String					fName;
189 	rw_lock					fLocker;
190 	recursive_lock			fIteratorLocker;
191 	recursive_lock			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