1 /* 2 * Copyright 2011, Jérôme Duval, korli@users.berlios.de. 3 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. 4 * This file may be used under the terms of the MIT License. 5 */ 6 #ifndef INODE_H 7 #define INODE_H 8 9 10 #include <fs_cache.h> 11 #include <lock.h> 12 #include <string.h> 13 14 #include "ext2.h" 15 #include "Volume.h" 16 17 18 //#define TRACE_EXT2 19 #ifdef TRACE_EXT2 20 # define TRACEI(x...) dprintf("\33[34mext2:\33[0m " x) 21 #else 22 # define TRACEI(x...) ; 23 #endif 24 25 26 class Inode : public TransactionListener { 27 public: 28 Inode(Volume* volume, ino_t id); 29 ~Inode(); 30 31 status_t InitCheck(); 32 33 ino_t ID() const { return fID; } 34 35 rw_lock* Lock() { return &fLock; } 36 void WriteLockInTransaction(Transaction& transaction); 37 38 status_t UpdateNodeFromDisk(); 39 status_t WriteBack(Transaction& transaction); 40 41 recursive_lock& SmallDataLock() { return fSmallDataLock; } 42 43 bool IsDirectory() const 44 { return S_ISDIR(Mode()); } 45 bool IsFile() const 46 { return S_ISREG(Mode()); } 47 bool IsSymLink() const 48 { return S_ISLNK(Mode()); } 49 status_t CheckPermissions(int accessMode) const; 50 51 bool IsDeleted() const { return fUnlinked; } 52 bool HasExtraAttributes() const 53 { return fHasExtraAttributes; } 54 bool IsIndexed() const 55 { return fVolume->IndexedDirectories() 56 && (Flags() & EXT2_INODE_INDEXED) != 0; } 57 58 mode_t Mode() const { return fNode.Mode(); } 59 int32 Flags() const { return fNode.Flags(); } 60 61 off_t Size() const { return fNode.Size(); } 62 void GetChangeTime(struct timespec *timespec) const 63 { fNode.GetChangeTime(timespec, fHasExtraAttributes); } 64 void GetModificationTime(struct timespec *timespec) const 65 { fNode.GetModificationTime(timespec, 66 fHasExtraAttributes); } 67 void GetCreationTime(struct timespec *timespec) const 68 { fNode.GetCreationTime(timespec, 69 fHasExtraAttributes); } 70 void GetAccessTime(struct timespec *timespec) const 71 { fNode.GetAccessTime(timespec, fHasExtraAttributes); } 72 void SetChangeTime(const struct timespec *timespec) 73 { fNode.SetChangeTime(timespec, fHasExtraAttributes); } 74 void SetModificationTime(const struct timespec *timespec) 75 { fNode.SetModificationTime(timespec, 76 fHasExtraAttributes); } 77 void SetCreationTime(const struct timespec *timespec) 78 { fNode.SetCreationTime(timespec, 79 fHasExtraAttributes); } 80 void SetAccessTime(const struct timespec *timespec) 81 { fNode.SetAccessTime(timespec, fHasExtraAttributes); } 82 void IncrementNumLinks(Transaction& transaction); 83 84 //::Volume* _Volume() const { return fVolume; } 85 Volume* GetVolume() const { return fVolume; } 86 87 status_t FindBlock(off_t offset, fsblock_t& block, 88 uint32 *_count = NULL); 89 status_t ReadAt(off_t pos, uint8 *buffer, size_t *length); 90 status_t WriteAt(Transaction& transaction, off_t pos, 91 const uint8* buffer, size_t* length); 92 status_t FillGapWithZeros(off_t start, off_t end); 93 94 status_t Resize(Transaction& transaction, off_t size); 95 96 ext2_inode& Node() { return fNode; } 97 98 status_t InitDirectory(Transaction& transaction, Inode* parent); 99 100 status_t Unlink(Transaction& transaction); 101 102 static status_t Create(Transaction& transaction, Inode* parent, 103 const char* name, int32 mode, int openMode, 104 uint8 type, bool* _created = NULL, 105 ino_t* _id = NULL, Inode** _inode = NULL, 106 fs_vnode_ops* vnodeOps = NULL, 107 uint32 publishFlags = 0); 108 static void _BigtimeToTimespec(bigtime_t time, 109 struct timespec *timespec) 110 { timespec->tv_sec = time / 1000000LL; 111 timespec->tv_nsec = (time % 1000000LL) * 1000; } 112 113 void* FileCache() const { return fCache; } 114 void* Map() const { return fMap; } 115 status_t CreateFileCache(); 116 void DeleteFileCache(); 117 bool HasFileCache() { return fCache != NULL; } 118 status_t EnableFileCache(); 119 status_t DisableFileCache(); 120 121 status_t Sync(); 122 123 124 125 protected: 126 virtual void TransactionDone(bool success); 127 virtual void RemovedFromTransaction(); 128 129 130 private: 131 Inode(Volume* volume); 132 Inode(const Inode&); 133 Inode &operator=(const Inode&); 134 // no implementation 135 136 status_t _EnlargeDataStream(Transaction& transaction, 137 off_t size); 138 status_t _ShrinkDataStream(Transaction& transaction, 139 off_t size); 140 141 uint64 _NumBlocks(); 142 status_t _SetNumBlocks(uint64 numBlocks); 143 144 rw_lock fLock; 145 ::Volume* fVolume; 146 ino_t fID; 147 void* fCache; 148 void* fMap; 149 bool fUnlinked; 150 bool fHasExtraAttributes; 151 ext2_inode fNode; 152 uint32 fNodeSize; 153 // Inodes have a variable size, but the important 154 // information is always the same size (except in ext4) 155 status_t fInitStatus; 156 157 mutable recursive_lock fSmallDataLock; 158 }; 159 160 161 // The Vnode class provides a convenience layer upon get_vnode(), so that 162 // you don't have to call put_vnode() anymore, which may make code more 163 // readable in some cases 164 165 class Vnode { 166 public: 167 Vnode(Volume* volume, ino_t id) 168 : 169 fInode(NULL) 170 { 171 SetTo(volume, id); 172 } 173 174 Vnode() 175 : 176 fStatus(B_NO_INIT), 177 fInode(NULL) 178 { 179 } 180 181 ~Vnode() 182 { 183 Unset(); 184 } 185 186 status_t InitCheck() 187 { 188 return fStatus; 189 } 190 191 void Unset() 192 { 193 if (fInode != NULL) { 194 put_vnode(fInode->GetVolume()->FSVolume(), fInode->ID()); 195 fInode = NULL; 196 fStatus = B_NO_INIT; 197 } 198 } 199 200 status_t SetTo(Volume* volume, ino_t id) 201 { 202 Unset(); 203 204 return fStatus = get_vnode(volume->FSVolume(), id, (void**)&fInode); 205 } 206 207 status_t Get(Inode** _inode) 208 { 209 *_inode = fInode; 210 return fStatus; 211 } 212 213 void Keep() 214 { 215 TRACEI("Vnode::Keep()\n"); 216 fInode = NULL; 217 } 218 219 status_t Publish(Transaction& transaction, Inode* inode, 220 fs_vnode_ops* vnodeOps, uint32 publishFlags) 221 { 222 TRACEI("Vnode::Publish()\n"); 223 Volume* volume = transaction.GetVolume(); 224 225 status_t status = B_OK; 226 227 if (!inode->IsSymLink() && volume->ID() >= 0) { 228 TRACEI("Vnode::Publish(): Publishing volume: %p, %" B_PRIdINO 229 ", %p, %p, %" B_PRIu16 ", %" B_PRIx32 "\n", volume->FSVolume(), 230 inode->ID(), inode, vnodeOps != NULL ? vnodeOps : &gExt2VnodeOps, 231 inode->Mode(), publishFlags); 232 status = publish_vnode(volume->FSVolume(), inode->ID(), inode, 233 vnodeOps != NULL ? vnodeOps : &gExt2VnodeOps, inode->Mode(), 234 publishFlags); 235 TRACEI("Vnode::Publish(): Result: %s\n", strerror(status)); 236 } 237 238 if (status == B_OK) { 239 TRACEI("Vnode::Publish(): Preparing internal data\n"); 240 fInode = inode; 241 fStatus = B_OK; 242 243 cache_add_transaction_listener(volume->BlockCache(), 244 transaction.ID(), TRANSACTION_ABORTED, &_TransactionListener, 245 inode); 246 } 247 248 return status; 249 } 250 251 private: 252 status_t fStatus; 253 Inode* fInode; 254 255 // TODO: How to apply coding style here? 256 static void _TransactionListener(int32 id, int32 event, void* _inode) 257 { 258 Inode* inode = (Inode*)_inode; 259 260 if (event == TRANSACTION_ABORTED) { 261 // TODO: Unpublish? 262 panic("Transaction %d aborted, inode %p still exists!\n", (int)id, 263 inode); 264 } 265 } 266 }; 267 268 #endif // INODE_H 269