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: 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 59 typedef AutoLocker<VnodeToInode, VnodeToInodeLocking> VnodeToInodeLocker; 60 61 inline 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 73 VnodeToInode::~VnodeToInode() 74 { 75 Replace(NULL); 76 if (fFileSystem != NULL && !IsRoot()) 77 fFileSystem->InoIdMap()->RemoveEntry(fID); 78 rw_lock_destroy(&fLock); 79 } 80 81 82 inline void 83 VnodeToInode::Lock() 84 { 85 rw_lock_read_lock(&fLock); 86 } 87 88 89 inline void 90 VnodeToInode::Unlock() 91 { 92 rw_lock_read_unlock(&fLock); 93 } 94 95 96 inline void 97 VnodeToInode::Clear() 98 { 99 WriteLocker _(fLock); 100 if (!IsRoot()) 101 delete fInode; 102 fInode = NULL; 103 } 104 105 106 inline bool 107 VnodeToInode::IsRoot() const 108 { 109 return fInode && fFileSystem && fInode->ID() == fFileSystem->Root()->ID(); 110 } 111 112 113 inline Inode* 114 VnodeToInode::GetPointer() const 115 { 116 return fInode; 117 } 118 119 120 inline ino_t 121 VnodeToInode::ID() const 122 { 123 return fID; 124 } 125 126 #endif // VNODETOINODE_H 127 128