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
19 class VnodeToInode {
20 public:
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;
37 private:
38 ino_t fID;
39 rw_lock fLock;
40
41 Inode* fInode;
42 FileSystem* fFileSystem;
43 };
44
45 class VnodeToInodeLocking {
46 public:
Lock(VnodeToInode * vti)47 inline bool Lock(VnodeToInode* vti)
48 {
49 vti->Lock();
50 return true;
51 }
52
Unlock(VnodeToInode * vti)53 inline void Unlock(VnodeToInode* vti)
54 {
55 vti->Unlock();
56 }
57 };
58
59 typedef AutoLocker<VnodeToInode, VnodeToInodeLocking> VnodeToInodeLocker;
60
61 inline
VnodeToInode(ino_t id,FileSystem * fileSystem)62 VnodeToInode::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
72 inline
~VnodeToInode()73 VnodeToInode::~VnodeToInode()
74 {
75 Clear();
76 if (fFileSystem != NULL && !IsRoot())
77 fFileSystem->InoIdMap()->RemoveEntry(fID);
78 rw_lock_destroy(&fLock);
79 }
80
81
82 inline void
Lock()83 VnodeToInode::Lock()
84 {
85 rw_lock_read_lock(&fLock);
86 }
87
88
89 inline void
Unlock()90 VnodeToInode::Unlock()
91 {
92 rw_lock_read_unlock(&fLock);
93 }
94
95
96 inline void
Clear()97 VnodeToInode::Clear()
98 {
99 Replace(NULL);
100 }
101
102
103 inline bool
IsRoot()104 VnodeToInode::IsRoot() const
105 {
106 return fInode && fFileSystem && fInode->ID() == fFileSystem->Root()->ID();
107 }
108
109
110 inline Inode*
GetPointer()111 VnodeToInode::GetPointer() const
112 {
113 return fInode;
114 }
115
116
117 inline ino_t
ID()118 VnodeToInode::ID() const
119 {
120 return fID;
121 }
122
123 #endif // VNODETOINODE_H
124
125