xref: /haiku/src/add-ons/kernel/file_systems/nfs4/MetadataCache.h (revision 25a7b01d15612846f332751841da3579db313082)
17efb4c9bSPawel Dziepak /*
27efb4c9bSPawel Dziepak  * Copyright 2012 Haiku, Inc. All rights reserved.
37efb4c9bSPawel Dziepak  * Distributed under the terms of the MIT License.
47efb4c9bSPawel Dziepak  *
57efb4c9bSPawel Dziepak  * Authors:
67efb4c9bSPawel Dziepak  *		Paweł Dziepak, pdziepak@quarnos.org
77efb4c9bSPawel Dziepak  */
87efb4c9bSPawel Dziepak #ifndef METADATACACHE_H
97efb4c9bSPawel Dziepak #define METADATACACHE_H
107efb4c9bSPawel Dziepak 
117efb4c9bSPawel Dziepak 
123ae5e813SPawel Dziepak #include <fs_interface.h>
137efb4c9bSPawel Dziepak #include <lock.h>
147efb4c9bSPawel Dziepak #include <SupportDefs.h>
157efb4c9bSPawel Dziepak #include <util/AutoLock.h>
167efb4c9bSPawel Dziepak #include <util/AVLTreeMap.h>
177efb4c9bSPawel Dziepak 
187efb4c9bSPawel Dziepak 
193ae5e813SPawel Dziepak class Inode;
203ae5e813SPawel Dziepak 
217efb4c9bSPawel Dziepak struct AccessEntry {
227efb4c9bSPawel Dziepak 	time_t	fExpire;
2320d1b02eSPawel Dziepak 	bool	fForceValid;
2420d1b02eSPawel Dziepak 
257efb4c9bSPawel Dziepak 	uint32	fAllowed;
267efb4c9bSPawel Dziepak };
277efb4c9bSPawel Dziepak 
287efb4c9bSPawel Dziepak class MetadataCache {
297efb4c9bSPawel Dziepak public:
303ae5e813SPawel Dziepak 								MetadataCache(Inode* inode);
317efb4c9bSPawel Dziepak 								~MetadataCache();
327efb4c9bSPawel Dziepak 
337efb4c9bSPawel Dziepak 					status_t	GetStat(struct stat* st);
347efb4c9bSPawel Dziepak 					void		SetStat(const struct stat& st);
357efb4c9bSPawel Dziepak 					void		GrowFile(size_t newSize);
367efb4c9bSPawel Dziepak 
377efb4c9bSPawel Dziepak 					status_t	GetAccess(uid_t uid, uint32* allowed);
387efb4c9bSPawel Dziepak 					void		SetAccess(uid_t uid, uint32 allowed);
397efb4c9bSPawel Dziepak 
40cde3994eSPawel Dziepak 					status_t	LockValid();
41cde3994eSPawel Dziepak 					void		UnlockValid();
42cde3994eSPawel Dziepak 
437efb4c9bSPawel Dziepak 	inline			void		InvalidateStat();
447efb4c9bSPawel Dziepak 	inline			void		InvalidateAccess();
457efb4c9bSPawel Dziepak 
467efb4c9bSPawel Dziepak 	inline			void		Invalidate();
477efb4c9bSPawel Dziepak 
487efb4c9bSPawel Dziepak 	static const	time_t		kExpirationTime	= 60;
493ae5e813SPawel Dziepak 
503ae5e813SPawel Dziepak protected:
513ae5e813SPawel Dziepak 					void		NotifyChanges(const struct stat* oldStat,
523ae5e813SPawel Dziepak 									const struct stat* newStat);
533ae5e813SPawel Dziepak 
547efb4c9bSPawel Dziepak private:
557efb4c9bSPawel Dziepak 					struct stat	fStatCache;
567efb4c9bSPawel Dziepak 					time_t		fExpire;
57cde3994eSPawel Dziepak 					bool		fForceValid;
587efb4c9bSPawel Dziepak 
593ae5e813SPawel Dziepak 					Inode*		fInode;
603ae5e813SPawel Dziepak 					bool		fInited;
613ae5e813SPawel Dziepak 
627efb4c9bSPawel Dziepak 					AVLTreeMap<uid_t, AccessEntry>	fAccessCache;
637efb4c9bSPawel Dziepak 
647efb4c9bSPawel Dziepak 					mutex		fLock;
657efb4c9bSPawel Dziepak };
667efb4c9bSPawel Dziepak 
677efb4c9bSPawel Dziepak 
687efb4c9bSPawel Dziepak inline void
InvalidateStat()697efb4c9bSPawel Dziepak MetadataCache::InvalidateStat()
707efb4c9bSPawel Dziepak {
717efb4c9bSPawel Dziepak 	MutexLocker _(fLock);
72*8568341aSPawel Dziepak 	if (!fForceValid)
737efb4c9bSPawel Dziepak 		fExpire = 0;
747efb4c9bSPawel Dziepak }
757efb4c9bSPawel Dziepak 
767efb4c9bSPawel Dziepak 
777efb4c9bSPawel Dziepak inline void
InvalidateAccess()787efb4c9bSPawel Dziepak MetadataCache::InvalidateAccess()
797efb4c9bSPawel Dziepak {
807efb4c9bSPawel Dziepak 	MutexLocker _(fLock);
81*8568341aSPawel Dziepak 	if (!fForceValid)
827efb4c9bSPawel Dziepak 		fAccessCache.MakeEmpty();
837efb4c9bSPawel Dziepak }
847efb4c9bSPawel Dziepak 
857efb4c9bSPawel Dziepak 
867efb4c9bSPawel Dziepak inline void
Invalidate()877efb4c9bSPawel Dziepak MetadataCache::Invalidate()
887efb4c9bSPawel Dziepak {
897efb4c9bSPawel Dziepak 	InvalidateStat();
907efb4c9bSPawel Dziepak 	InvalidateAccess();
917efb4c9bSPawel Dziepak }
927efb4c9bSPawel Dziepak 
937efb4c9bSPawel Dziepak 
947efb4c9bSPawel Dziepak #endif	// METADATACACHE_H
957efb4c9bSPawel Dziepak 
96