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
18class Inode;
19class RootInode;
20
21struct 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
32class FileSystem : public DoublyLinkedListLinkImpl<FileSystem> {
33public:
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();
81private:
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
121inline RootInode*
122FileSystem::Root()
123{
124	return fRoot;
125}
126
127
128inline uint32
129FileSystem::OpenFilesCount()
130{
131	return fOpenCount;
132}
133
134
135inline bool
136FileSystem::IsAttrSupported(Attribute attr) const
137{
138	return sIsAttrSet(attr, fSupAttrs, 2);
139}
140
141
142inline uint32
143FileSystem::ExpireType() const
144{
145	return fExpireType;
146}
147
148
149inline RPC::Server*
150FileSystem::Server()
151{
152	ASSERT(fServer != NULL);
153	return fServer;
154}
155
156
157inline NFS4Server*
158FileSystem::NFSServer()
159{
160	ASSERT(fServer->PrivateData() != NULL);
161	return reinterpret_cast<NFS4Server*>(fServer->PrivateData());
162}
163
164
165inline const char**
166FileSystem::Path() const
167{
168	ASSERT(fPath != NULL);
169	return fPath;
170}
171
172
173inline const FileSystemId&
174FileSystem::FsId() const
175{
176	return fFsId;
177}
178
179
180inline uint64
181FileSystem::AllocFileId()
182{
183	return atomic_add64(&fId, 1);
184}
185
186
187inline dev_t
188FileSystem::DevId() const
189{
190	return fDevId;
191}
192
193
194inline InodeIdMap*
195FileSystem::InoIdMap()
196{
197	return &fInoIdMap;
198}
199
200
201inline uint64
202FileSystem::OpenOwner() const
203{
204	return fOpenOwner;
205}
206
207
208inline uint32
209FileSystem::OpenOwnerSequenceLock()
210{
211	mutex_lock(&fOpenOwnerLock);
212	return fOpenOwnerSequence;
213}
214
215
216inline void
217FileSystem::OpenOwnerSequenceUnlock(uint32 sequence)
218{
219	fOpenOwnerSequence = sequence;
220	mutex_unlock(&fOpenOwnerLock);
221}
222
223
224inline bool
225FileSystem::NamedAttrs()
226{
227	return fNamedAttrs;
228}
229
230
231inline void
232FileSystem::SetNamedAttrs(bool attrs)
233{
234	fNamedAttrs = attrs;
235}
236
237
238inline const MountConfiguration&
239FileSystem::GetConfiguration()
240{
241	return fConfiguration;
242}
243
244
245inline mutex&
246FileSystem::CreateFileLock()
247{
248	return fCreateFileLock;
249}
250
251
252#endif	// FILESYSTEM_H
253
254