xref: /haiku/src/add-ons/kernel/file_systems/nfs4/FileSystem.h (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2012 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Paweł Dziepak, pdziepak@quarnos.org
7  */
8 #ifndef FILESYSTEM_H
9 #define FILESYSTEM_H
10 
11 
12 #include "Delegation.h"
13 #include "InodeIdMap.h"
14 #include "NFS4Defs.h"
15 #include "NFS4Server.h"
16 
17 
18 class Inode;
19 class RootInode;
20 
21 struct MountConfiguration {
22 	bool		fHard;
23 	int			fRetryLimit;
24 	bigtime_t	fRequestTimeout;
25 
26 	bool		fEmulateNamedAttrs;
27 	bool		fCacheMetadata;
28 
29 	bigtime_t	fDirectoryCacheTime;
30 };
31 
32 class FileSystem : public DoublyLinkedListLinkImpl<FileSystem> {
33 public:
34 	static	status_t			Mount(FileSystem** pfs, RPC::Server* serv,
35 									const char* serverName, const char* path,
36 									dev_t id,
37 									const MountConfiguration& configuration);
38 								~FileSystem();
39 
40 			status_t			GetInode(ino_t id, Inode** inode);
41 	inline	RootInode*			Root();
42 
43 			status_t			Migrate(const RPC::Server* serv);
44 
45 			DoublyLinkedList<OpenState>&	OpenFilesLock();
46 			void				OpenFilesUnlock();
47 	inline	uint32				OpenFilesCount();
48 			void				AddOpenFile(OpenState* state);
49 			void				RemoveOpenFile(OpenState* state);
50 
51 			DoublyLinkedList<Delegation>&	DelegationsLock();
52 			void				DelegationsUnlock();
53 			void				AddDelegation(Delegation* delegation);
54 			void				RemoveDelegation(Delegation* delegation);
55 			Delegation*			GetDelegation(const FileHandle& handle);
56 
57 	inline	bool				IsAttrSupported(Attribute attr) const;
58 	inline	uint32				ExpireType() const;
59 
60 	inline	RPC::Server*		Server();
61 	inline	NFS4Server*			NFSServer();
62 
63 	inline	const char**		Path() const;
64 	inline	const FileSystemId&	FsId() const;
65 
66 	inline	uint64				AllocFileId();
67 
68 	inline	dev_t				DevId() const;
69 	inline	InodeIdMap*			InoIdMap();
70 
71 	inline	uint64				OpenOwner() const;
72 	inline	uint32				OpenOwnerSequenceLock();
73 	inline	void				OpenOwnerSequenceUnlock(uint32 sequence);
74 
75 	inline	bool				NamedAttrs();
76 	inline	void				SetNamedAttrs(bool attrs);
77 
78 	inline	const MountConfiguration&	GetConfiguration();
79 
80 	inline	mutex&				CreateFileLock();
81 private:
82 								FileSystem(const MountConfiguration& config);
83 
84 	static	status_t			_ParsePath(RequestBuilder& req, uint32& count,
85 									const char* _path);
86 
87 			mutex				fCreateFileLock;
88 
89 			mutex				fDelegationLock;
90 			DoublyLinkedList<Delegation>	fDelegationList;
91 			AVLTreeMap<FileHandle, Delegation*> fHandleToDelegation;
92 
93 			DoublyLinkedList<OpenState>		fOpenFiles;
94 			uint32				fOpenCount;
95 			mutex				fOpenLock;
96 
97 			uint64				fOpenOwner;
98 			uint32				fOpenOwnerSequence;
99 			mutex				fOpenOwnerLock;
100 
101 			uint32				fExpireType;
102 			uint32				fSupAttrs[2];
103 			bool				fNamedAttrs;
104 
105 			FileSystemId		fFsId;
106 			const char**		fPath;
107 
108 			RootInode*			fRoot;
109 
110 			RPC::Server*		fServer;
111 
112 			int64				fId;
113 			dev_t				fDevId;
114 
115 			InodeIdMap			fInoIdMap;
116 
117 			MountConfiguration	fConfiguration;
118 };
119 
120 
121 inline RootInode*
122 FileSystem::Root()
123 {
124 	return fRoot;
125 }
126 
127 
128 inline uint32
129 FileSystem::OpenFilesCount()
130 {
131 	return fOpenCount;
132 }
133 
134 
135 inline bool
136 FileSystem::IsAttrSupported(Attribute attr) const
137 {
138 	return sIsAttrSet(attr, fSupAttrs, 2);
139 }
140 
141 
142 inline uint32
143 FileSystem::ExpireType() const
144 {
145 	return fExpireType;
146 }
147 
148 
149 inline RPC::Server*
150 FileSystem::Server()
151 {
152 	ASSERT(fServer != NULL);
153 	return fServer;
154 }
155 
156 
157 inline NFS4Server*
158 FileSystem::NFSServer()
159 {
160 	ASSERT(fServer->PrivateData() != NULL);
161 	return reinterpret_cast<NFS4Server*>(fServer->PrivateData());
162 }
163 
164 
165 inline const char**
166 FileSystem::Path() const
167 {
168 	ASSERT(fPath != NULL);
169 	return fPath;
170 }
171 
172 
173 inline const FileSystemId&
174 FileSystem::FsId() const
175 {
176 	return fFsId;
177 }
178 
179 
180 inline uint64
181 FileSystem::AllocFileId()
182 {
183 	return atomic_add64(&fId, 1);
184 }
185 
186 
187 inline dev_t
188 FileSystem::DevId() const
189 {
190 	return fDevId;
191 }
192 
193 
194 inline InodeIdMap*
195 FileSystem::InoIdMap()
196 {
197 	return &fInoIdMap;
198 }
199 
200 
201 inline uint64
202 FileSystem::OpenOwner() const
203 {
204 	return fOpenOwner;
205 }
206 
207 
208 inline uint32
209 FileSystem::OpenOwnerSequenceLock()
210 {
211 	mutex_lock(&fOpenOwnerLock);
212 	return fOpenOwnerSequence;
213 }
214 
215 
216 inline void
217 FileSystem::OpenOwnerSequenceUnlock(uint32 sequence)
218 {
219 	fOpenOwnerSequence = sequence;
220 	mutex_unlock(&fOpenOwnerLock);
221 }
222 
223 
224 inline bool
225 FileSystem::NamedAttrs()
226 {
227 	return fNamedAttrs;
228 }
229 
230 
231 inline void
232 FileSystem::SetNamedAttrs(bool attrs)
233 {
234 	fNamedAttrs = attrs;
235 }
236 
237 
238 inline const MountConfiguration&
239 FileSystem::GetConfiguration()
240 {
241 	return fConfiguration;
242 }
243 
244 
245 inline mutex&
246 FileSystem::CreateFileLock()
247 {
248 	return fCreateFileLock;
249 }
250 
251 
252 #endif	// FILESYSTEM_H
253 
254