xref: /haiku/src/add-ons/kernel/file_systems/userlandfs/kernel_add_on/Volume.h (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 // Volume.h
2 
3 #ifndef USERLAND_FS_VOLUME_H
4 #define USERLAND_FS_VOLUME_H
5 
6 #include <fs_interface.h>
7 
8 #include "Referencable.h"
9 
10 namespace UserlandFSUtil {
11 
12 class Request;
13 class RequestAllocator;
14 class RequestHandler;
15 class RequestPort;
16 struct userlandfs_ioctl;
17 
18 }
19 
20 using UserlandFSUtil::Request;
21 using UserlandFSUtil::RequestAllocator;
22 using UserlandFSUtil::RequestHandler;
23 using UserlandFSUtil::RequestPort;
24 using UserlandFSUtil::userlandfs_ioctl;
25 
26 class FileSystem;
27 
28 class Volume : public Referencable {
29 public:
30 								Volume(FileSystem* fileSystem, dev_t id);
31 								~Volume();
32 
33 			FileSystem*			GetFileSystem() const;
34 			dev_t				GetID() const;
35 
36 			void*				GetUserlandVolume() const;
37 			ino_t				GetRootID() const;
38 			bool				IsMounting() const;
39 
40 			// client methods
41 			status_t			GetVNode(ino_t vnid, fs_vnode* node);
42 			status_t			PutVNode(ino_t vnid);
43 			status_t			NewVNode(ino_t vnid, fs_vnode node);
44 			status_t			PublishVNode(ino_t vnid, fs_vnode node);
45 			status_t			RemoveVNode(ino_t vnid);
46 			status_t			UnremoveVNode(ino_t vnid);
47 			status_t			GetVNodeRemoved(ino_t vnid, bool* removed);
48 
49 			// FS
50 			status_t			Mount(const char* device, uint32 flags,
51 									const char* parameters);
52 			status_t			Unmount();
53 			status_t			Sync();
54 			status_t			ReadFSInfo(fs_info* info);
55 			status_t			WriteFSInfo(const struct fs_info* info,
56 									uint32 mask);
57 
58 			// vnodes
59 			status_t			Lookup(fs_vnode dir, const char* entryName,
60 									ino_t* vnid, int* type);
61 			status_t			GetVNodeName(fs_vnode node, char* buffer,
62 									size_t bufferSize);
63 			status_t			ReadVNode(ino_t vnid, bool reenter,
64 									fs_vnode* node);
65 			status_t			WriteVNode(fs_vnode node, bool reenter);
66 			status_t			RemoveVNode(fs_vnode node, bool reenter);
67 
68 			// nodes
69 			status_t			IOCtl(fs_vnode node, fs_cookie cookie,
70 									uint32 command, void *buffer, size_t size);
71 			status_t			SetFlags(fs_vnode node, fs_cookie cookie,
72 									int flags);
73 			status_t			Select(fs_vnode node, fs_cookie cookie,
74 									uint8 event, uint32 ref, selectsync* sync);
75 			status_t			Deselect(fs_vnode node, fs_cookie cookie,
76 									uint8 event, selectsync* sync);
77 
78 			status_t			FSync(fs_vnode node);
79 
80 			status_t			ReadSymlink(fs_vnode node, char* buffer,
81 									size_t bufferSize, size_t* bytesRead);
82 			status_t			CreateSymlink(fs_vnode dir, const char* name,
83 									const char* target, int mode);
84 
85 			status_t			Link(fs_vnode dir, const char* name,
86 									fs_vnode node);
87 			status_t			Unlink(fs_vnode dir, const char* name);
88 			status_t			Rename(fs_vnode oldDir, const char* oldName,
89 									fs_vnode newDir, const char* newName);
90 
91 			status_t			Access(fs_vnode node, int mode);
92 			status_t			ReadStat(fs_vnode node, struct stat* st);
93 			status_t			WriteStat(fs_vnode node, const struct stat *st,
94 									uint32 mask);
95 
96 			// files
97 			status_t			Create(fs_vnode dir, const char* name,
98 									int openMode, int mode, fs_cookie* cookie,
99 									ino_t* vnid);
100 			status_t			Open(fs_vnode node, int openMode,
101 									fs_cookie* cookie);
102 			status_t			Close(fs_vnode node, fs_cookie cookie);
103 			status_t			FreeCookie(fs_vnode node, fs_cookie cookie);
104 			status_t			Read(fs_vnode node, fs_cookie cookie, off_t pos,
105 									void* buffer, size_t bufferSize,
106 									size_t* bytesRead);
107 			status_t			Write(fs_vnode node, fs_cookie cookie,
108 									off_t pos, const void* buffer,
109 									size_t bufferSize, size_t* bytesWritten);
110 
111 			// directories
112 			status_t			CreateDir(fs_vnode dir, const char* name,
113 									int mode, ino_t *newDir);
114 			status_t			RemoveDir(fs_vnode dir, const char* name);
115 			status_t			OpenDir(fs_vnode node, fs_cookie* cookie);
116 			status_t			CloseDir(fs_vnode node, fs_vnode cookie);
117 			status_t			FreeDirCookie(fs_vnode node, fs_vnode cookie);
118 			status_t			ReadDir(fs_vnode node, fs_vnode cookie,
119 									void* buffer, size_t bufferSize,
120 									uint32 count, uint32* countRead);
121 			status_t			RewindDir(fs_vnode node, fs_vnode cookie);
122 
123 			// attribute directories
124 			status_t			OpenAttrDir(fs_vnode node, fs_cookie *cookie);
125 			status_t			CloseAttrDir(fs_vnode node, fs_cookie cookie);
126 			status_t			FreeAttrDirCookie(fs_vnode node,
127 									fs_cookie cookie);
128 			status_t			ReadAttrDir(fs_vnode node, fs_cookie cookie,
129 									void* buffer, size_t bufferSize,
130 									uint32 count, uint32* countRead);
131 			status_t			RewindAttrDir(fs_vnode node, fs_cookie cookie);
132 
133 			// attributes
134 			status_t			CreateAttr(fs_vnode node, const char* name,
135 									uint32 type, int openMode,
136 									fs_cookie* cookie);
137 			status_t			OpenAttr(fs_vnode node, const char* name,
138 									int openMode, fs_cookie* cookie);
139 			status_t			CloseAttr(fs_vnode node, fs_cookie cookie);
140 			status_t			FreeAttrCookie(fs_vnode node, fs_cookie cookie);
141 			status_t			ReadAttr(fs_vnode node, fs_cookie cookie,
142 									off_t pos, void* buffer, size_t bufferSize,
143 									size_t* bytesRead);
144 			status_t			WriteAttr(fs_vnode node, fs_cookie cookie,
145 									off_t pos, const void* buffer,
146 									size_t bufferSize, size_t* bytesWritten);
147 			status_t			ReadAttrStat(fs_vnode node, fs_cookie cookie,
148 									struct stat *st);
149 			status_t			WriteAttrStat(fs_vnode node, fs_cookie cookie,
150 									const struct stat *st, int statMask);
151 			status_t			RenameAttr(fs_vnode oldNode,
152 									const char* oldName, fs_vnode newNode,
153 									const char* newName);
154 			status_t			RemoveAttr(fs_vnode node, const char* name);
155 
156 			// indices
157 			status_t			OpenIndexDir(fs_cookie *cookie);
158 			status_t			CloseIndexDir(fs_cookie cookie);
159 			status_t			FreeIndexDirCookie(fs_cookie cookie);
160 			status_t			ReadIndexDir(fs_cookie cookie, void* buffer,
161 									size_t bufferSize, uint32 count,
162 									uint32* countRead);
163 			status_t			RewindIndexDir(fs_cookie cookie);
164 			status_t			CreateIndex(const char* name, uint32 type,
165 									uint32 flags);
166 			status_t			RemoveIndex(const char* name);
167 			status_t			ReadIndexStat(const char *name,
168 									struct stat *st);
169 
170 			// queries
171 			status_t			OpenQuery(const char* queryString,
172 									uint32 flags, port_id port, uint32 token,
173 									fs_cookie *cookie);
174 			status_t			CloseQuery(fs_cookie cookie);
175 			status_t			FreeQueryCookie(fs_cookie cookie);
176 			status_t			ReadQuery(fs_cookie cookie, void* buffer,
177 									size_t bufferSize, uint32 count,
178 									uint32* countRead);
179 			status_t			RewindQuery(fs_cookie cookie);
180 
181 private:
182 			status_t			_Mount(const char* device, uint32 flags,
183 									const char* parameters);
184 			status_t			_Unmount();
185 			status_t			_ReadFSInfo(fs_info* info);
186 			status_t			_Lookup(fs_vnode dir, const char* entryName,
187 									ino_t* vnid, int* type);
188 			status_t			_WriteVNode(fs_vnode node, bool reenter);
189 			status_t			_ReadStat(fs_vnode node, struct stat* st);
190 			status_t			_Close(fs_vnode node, fs_cookie cookie);
191 			status_t			_FreeCookie(fs_vnode node, fs_cookie cookie);
192 			status_t			_CloseDir(fs_vnode node, fs_vnode cookie);
193 			status_t			_FreeDirCookie(fs_vnode node, fs_vnode cookie);
194 			status_t			_CloseAttrDir(fs_vnode node, fs_cookie cookie);
195 			status_t			_FreeAttrDirCookie(fs_vnode node,
196 									fs_cookie cookie);
197 			status_t			_CloseAttr(fs_vnode node, fs_cookie cookie);
198 			status_t			_FreeAttrCookie(fs_vnode node,
199 									fs_cookie cookie);
200 			status_t			_CloseIndexDir(fs_cookie cookie);
201 			status_t			_FreeIndexDirCookie(fs_cookie cookie);
202 			status_t			_CloseQuery(fs_cookie cookie);
203 			status_t			_FreeQueryCookie(fs_cookie cookie);
204 
205 			status_t			_SendRequest(RequestPort* port,
206 									RequestAllocator* allocator,
207 									RequestHandler* handler, Request** reply);
208 			status_t			_SendReceiptAck(RequestPort* port);
209 
210 			void				_IncrementVNodeCount(ino_t vnid);
211 			void				_DecrementVNodeCount(ino_t vnid);
212 
213 			status_t			_InternalIOCtl(userlandfs_ioctl* buffer,
214 									int32 bufferSize);
215 
216 			status_t			_PutAllPendingVNodes();
217 
218 private:
219 			struct MountVNodeMap;
220 			struct VNodeCountMap;
221 			class AutoIncrementer;
222 
223 			FileSystem*			fFileSystem;
224 			dev_t				fID;
225 			void*				fUserlandVolume;
226 			ino_t				fRootID;
227 			fs_vnode			fRootNode;
228 			MountVNodeMap*		fMountVNodes;
229 			vint32				fOpenFiles;
230 			vint32				fOpenDirectories;
231 			vint32				fOpenAttributeDirectories;
232 			vint32				fOpenAttributes;
233 			vint32				fOpenIndexDirectories;
234 			vint32				fOpenQueries;
235 			VNodeCountMap*		fVNodeCountMap;
236 									// Tracks the number of new/get_vnode()
237 									// calls to be balanced by the FS by
238 									// corresponding put_vnode()s.
239 	volatile bool				fVNodeCountingEnabled;
240 };
241 
242 #endif	// USERLAND_FS_VOLUME_H
243