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 VNODETOINODE_H
9#define VNODETOINODE_H
10
11#include <lock.h>
12#include <SupportDefs.h>
13#include <util/AutoLock.h>
14
15#include "Inode.h"
16#include "InodeIdMap.h"
17#include "RootInode.h"
18
19class VnodeToInode {
20public:
21	inline				VnodeToInode(ino_t id, FileSystem* fileSystem);
22	inline				~VnodeToInode();
23
24	inline	void		Lock();
25	inline	void		Unlock();
26
27	inline	Inode*		GetPointer() const;
28			Inode*		Get();
29			void		Replace(Inode* newInode);
30
31			bool		Unlink(InodeNames* parent, const char* name);
32	inline	void		Clear();
33
34	inline	ino_t		ID() const;
35
36	inline	bool		IsRoot() const;
37private:
38			ino_t		fID;
39			rw_lock		fLock;
40
41			Inode*		fInode;
42			FileSystem*	fFileSystem;
43};
44
45class VnodeToInodeLocking {
46public:
47	inline bool Lock(VnodeToInode* vti)
48	{
49		vti->Lock();
50		return true;
51	}
52
53	inline void Unlock(VnodeToInode* vti)
54	{
55		vti->Unlock();
56	}
57};
58
59typedef AutoLocker<VnodeToInode, VnodeToInodeLocking> VnodeToInodeLocker;
60
61inline
62VnodeToInode::VnodeToInode(ino_t id, FileSystem* fileSystem)
63	:
64	fID(id),
65	fInode(NULL),
66	fFileSystem(fileSystem)
67{
68	rw_lock_init(&fLock, NULL);
69}
70
71
72inline
73VnodeToInode::~VnodeToInode()
74{
75	Clear();
76	if (fFileSystem != NULL && !IsRoot())
77		fFileSystem->InoIdMap()->RemoveEntry(fID);
78	rw_lock_destroy(&fLock);
79}
80
81
82inline void
83VnodeToInode::Lock()
84{
85	rw_lock_read_lock(&fLock);
86}
87
88
89inline void
90VnodeToInode::Unlock()
91{
92	rw_lock_read_unlock(&fLock);
93}
94
95
96inline void
97VnodeToInode::Clear()
98{
99	Replace(NULL);
100}
101
102
103inline bool
104VnodeToInode::IsRoot() const
105{
106	return fInode && fFileSystem && fInode->ID() == fFileSystem->Root()->ID();
107}
108
109
110inline Inode*
111VnodeToInode::GetPointer() const
112{
113	return fInode;
114}
115
116
117inline ino_t
118VnodeToInode::ID() const
119{
120	return fID;
121}
122
123#endif	// VNODETOINODE_H
124
125