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 18 class VnodeToInode { 19 public: 20 inline VnodeToInode(ino_t id, FileSystem* fileSystem); 21 inline ~VnodeToInode(); 22 23 inline void Lock(); 24 inline void Unlock(); 25 26 Inode* Get(); 27 void Replace(Inode* newInode); 28 29 inline void Remove(); 30 inline void Clear(); 31 32 inline ino_t ID(); 33 private: 34 ino_t fID; 35 rw_lock fLock; 36 37 Inode* fInode; 38 FileSystem* fFileSystem; 39 }; 40 41 class VnodeToInodeLocking { 42 public: 43 inline bool Lock(VnodeToInode* vti) 44 { 45 vti->Lock(); 46 return true; 47 } 48 49 inline void Unlock(VnodeToInode* vti) 50 { 51 vti->Unlock(); 52 } 53 }; 54 55 typedef AutoLocker<VnodeToInode, VnodeToInodeLocking> VnodeToInodeLocker; 56 57 inline 58 VnodeToInode::VnodeToInode(ino_t id, FileSystem* fileSystem) 59 : 60 fID(id), 61 fInode(NULL), 62 fFileSystem(fileSystem) 63 { 64 rw_lock_init(&fLock, NULL); 65 } 66 67 68 inline 69 VnodeToInode::~VnodeToInode() 70 { 71 Remove(); 72 if (fFileSystem != NULL) 73 fFileSystem->InoIdMap()->RemoveEntry(fID); 74 rw_lock_destroy(&fLock); 75 } 76 77 78 inline void 79 VnodeToInode::Lock() 80 { 81 rw_lock_read_lock(&fLock); 82 } 83 84 85 inline void 86 VnodeToInode::Unlock() 87 { 88 rw_lock_read_unlock(&fLock); 89 } 90 91 92 inline void 93 VnodeToInode::Remove() 94 { 95 Replace(NULL); 96 } 97 98 99 inline void 100 VnodeToInode::Clear() 101 { 102 WriteLocker _(fLock); 103 delete fInode; 104 fInode = NULL; 105 } 106 107 108 inline ino_t 109 VnodeToInode::ID() 110 { 111 return fID; 112 } 113 114 #endif // VNODETOINODE_H 115 116