1bf31ee39SPawel Dziepak /* 2bf31ee39SPawel Dziepak * Copyright 2012 Haiku, Inc. All rights reserved. 3bf31ee39SPawel Dziepak * Distributed under the terms of the MIT License. 4bf31ee39SPawel Dziepak * 5bf31ee39SPawel Dziepak * Authors: 6bf31ee39SPawel Dziepak * Paweł Dziepak, pdziepak@quarnos.org 7bf31ee39SPawel Dziepak */ 8bf31ee39SPawel Dziepak #ifndef INODE_H 9bf31ee39SPawel Dziepak #define INODE_H 10bf31ee39SPawel Dziepak 11bf31ee39SPawel Dziepak 127efb4c9bSPawel Dziepak #include "MetadataCache.h" 130dbff361SPawel Dziepak #include "NFS4Inode.h" 14eeabdab1SPawel Dziepak #include "OpenState.h" 15bf31ee39SPawel Dziepak 16bf31ee39SPawel Dziepak 1752aaad17SPawel Dziepak class Delegation; 1852aaad17SPawel Dziepak 190dbff361SPawel Dziepak class Inode : public NFS4Inode { 20bf31ee39SPawel Dziepak public: 2100a8558cSPawel Dziepak static status_t CreateInode(FileSystem* fs, const FileInfo& fi, 220792abe0SPawel Dziepak Inode** inode); 23e8c12d94SPawel Dziepak virtual ~Inode(); 24bf31ee39SPawel Dziepak 25bf31ee39SPawel Dziepak inline ino_t ID() const; 26d38e98d8SPawel Dziepak inline mode_t Type() const; 27e8c12d94SPawel Dziepak virtual const char* Name() const; 2800a8558cSPawel Dziepak inline FileSystem* GetFileSystem() const; 29bf31ee39SPawel Dziepak 3052aaad17SPawel Dziepak inline void SetOpenState(OpenState* state); 3152aaad17SPawel Dziepak 320bc98afdSPawel Dziepak inline void* FileCache(); 3313a90e37SPawel Dziepak status_t RevalidateFileCache(); 3413a90e37SPawel Dziepak 35e743e243SPawel Dziepak inline uint64 MaxFileSize(); 36e743e243SPawel Dziepak 37224d6022SPawel Dziepak inline uint64 Change(); 38224d6022SPawel Dziepak inline bool Dirty(); 39224d6022SPawel Dziepak 40b1fd656dSPawel Dziepak inline OpenState* GetOpenState(); 41b1fd656dSPawel Dziepak 424a153753SPawel Dziepak void SetDelegation(Delegation* delegation); 434a153753SPawel Dziepak void RecallDelegation(bool truncate = false); 44cde3994eSPawel Dziepak void RecallReadDelegation(); 454a153753SPawel Dziepak 460dbff361SPawel Dziepak status_t LookUp(const char* name, ino_t* id); 470dbff361SPawel Dziepak 480dbff361SPawel Dziepak status_t Access(int mode); 4975fe7b90SPawel Dziepak 5036577ed5SPawel Dziepak status_t Commit(); 51e2e5f06dSPawel Dziepak status_t SyncAndCommit(bool force = false); 5236577ed5SPawel Dziepak 530dbff361SPawel Dziepak status_t CreateObject(const char* name, const char* path, 540dbff361SPawel Dziepak int mode, FileType type); 55f5baf2efSPawel Dziepak 56f5baf2efSPawel Dziepak status_t CreateLink(const char* name, const char* path, 57f5baf2efSPawel Dziepak int mode); 58f5baf2efSPawel Dziepak 59c0de7acaSPawel Dziepak status_t Link(Inode* dir, const char* name); 607c6cdb83SPawel Dziepak status_t Remove(const char* name, FileType type, 61*dee0fd83SPawel Dziepak ino_t* id = NULL); 621ec6b0c1SPawel Dziepak static status_t Rename(Inode* from, Inode* to, 6320d1b02eSPawel Dziepak const char* fromName, const char* toName, 64*dee0fd83SPawel Dziepak bool attribute = false, ino_t* id = NULL); 65abba2fddSPawel Dziepak 6620d1b02eSPawel Dziepak status_t Stat(struct stat* st, 6720d1b02eSPawel Dziepak OpenAttrCookie* attr = NULL); 6820d1b02eSPawel Dziepak status_t WriteStat(const struct stat* st, uint32 mask, 6920d1b02eSPawel Dziepak OpenAttrCookie* attr = NULL); 707bfa4fe8SPawel Dziepak 7120bd3ee1SPawel Dziepak status_t Create(const char* name, int mode, int perms, 722c36184fSPawel Dziepak OpenFileCookie* cookie, 732c36184fSPawel Dziepak OpenDelegationData* data, ino_t* id); 742a292557SPawel Dziepak status_t Open(int mode, OpenFileCookie* cookie); 7560a8140dSPawel Dziepak status_t Close(OpenFileCookie* cookie); 7620d1b02eSPawel Dziepak 7720d1b02eSPawel Dziepak status_t OpenAttr(const char* name, int mode, 7820d1b02eSPawel Dziepak OpenAttrCookie* cookie, bool create, 7920d1b02eSPawel Dziepak int32 type = 0); 8020d1b02eSPawel Dziepak status_t CloseAttr(OpenAttrCookie* cookie); 8120d1b02eSPawel Dziepak 822f2e57d0SPawel Dziepak status_t Read(OpenFileCookie* cookie, off_t pos, 83e743e243SPawel Dziepak void* buffer, size_t* length); 84e36a5442SPawel Dziepak status_t Write(OpenFileCookie* cookie, off_t pos, 8586d2cab3SPawel Dziepak const void* buffer, size_t *_length); 862a292557SPawel Dziepak 8720d1b02eSPawel Dziepak status_t ReadDirect(OpenStateCookie* cookie, off_t pos, 88e743e243SPawel Dziepak void* buffer, size_t* length, bool* eof); 8920d1b02eSPawel Dziepak status_t WriteDirect(OpenStateCookie* cookie, off_t pos, 90e743e243SPawel Dziepak const void* buffer, size_t *_length); 91e743e243SPawel Dziepak 9269adfb6dSPawel Dziepak status_t CreateDir(const char* name, int mode); 93be4c74eaSPawel Dziepak status_t OpenDir(OpenDirCookie* cookie); 944b48348fSPawel Dziepak status_t ReadDir(void* buffer, uint32 size, 95be4c74eaSPawel Dziepak uint32* count, OpenDirCookie* cookie); 96212de720SPawel Dziepak 97f7c35cf4SPawel Dziepak status_t OpenAttrDir(OpenDirCookie* cookie); 98f7c35cf4SPawel Dziepak 997d0b8c97SPawel Dziepak status_t TestLock(OpenFileCookie* cookie, 1007d0b8c97SPawel Dziepak struct flock* lock); 1017d0b8c97SPawel Dziepak status_t AcquireLock(OpenFileCookie* cookie, 1027d0b8c97SPawel Dziepak const struct flock* lock, bool wait); 1037d0b8c97SPawel Dziepak status_t ReleaseLock(OpenFileCookie* cookie, 1047d0b8c97SPawel Dziepak const struct flock* lock); 1057d0b8c97SPawel Dziepak status_t ReleaseAllLocks(OpenFileCookie* cookie); 1067d0b8c97SPawel Dziepak 1073ae5e813SPawel Dziepak status_t GetDirSnapshot(DirectoryCacheSnapshot** 1083ae5e813SPawel Dziepak _snapshot, OpenDirCookie* cookie, 1092314d073SPawel Dziepak uint64* _change, bool attribute); 1103ae5e813SPawel Dziepak 11120d1b02eSPawel Dziepak status_t LoadAttrDirHandle(); 11220d1b02eSPawel Dziepak 113376eef4aSPawel Dziepak static inline ino_t FileIdToInoT(uint64 fileid); 1142f03ff09SPawel Dziepak protected: 1150792abe0SPawel Dziepak Inode(); 1160792abe0SPawel Dziepak 11749935f9bSPawel Dziepak void ReleaseOpenState(); 11849935f9bSPawel Dziepak 119eeabdab1SPawel Dziepak status_t CreateState(const char* name, int mode, 1202c36184fSPawel Dziepak int perms, OpenState* state, 1212c36184fSPawel Dziepak OpenDelegationData* data); 122eeabdab1SPawel Dziepak 123cde3994eSPawel Dziepak void ReturnDelegation(bool truncate); 124cde3994eSPawel Dziepak 1250dbff361SPawel Dziepak status_t ReadDirUp(struct dirent* de, uint32 pos, 126ed517d6cSPawel Dziepak uint32 size); 1270dbff361SPawel Dziepak status_t FillDirEntry(struct dirent* de, ino_t id, 1280dbff361SPawel Dziepak const char* name, uint32 pos, uint32 size); 1294b48348fSPawel Dziepak 1300dbff361SPawel Dziepak status_t ChildAdded(const char* name, uint64 fileID, 131df6a5eaeSPawel Dziepak const FileHandle& fileHandle); 132df6a5eaeSPawel Dziepak 13320d1b02eSPawel Dziepak status_t GetStat(struct stat* st, 13420d1b02eSPawel Dziepak OpenAttrCookie* attr = NULL); 13589f05134SPawel Dziepak 136da950cb2SPawel Dziepak char* AttrToFileName(const char* path); 137da950cb2SPawel Dziepak 1380dbff361SPawel Dziepak static inline status_t CheckLockType(short ltype, uint32 mode); 1390dbff361SPawel Dziepak 14052aaad17SPawel Dziepak private: 1410dbff361SPawel Dziepak uint32 fType; 1424b48348fSPawel Dziepak 1437efb4c9bSPawel Dziepak MetadataCache fMetaCache; 14475fe7b90SPawel Dziepak DirectoryCache* fCache; 145f7c35cf4SPawel Dziepak DirectoryCache* fAttrCache; 14613a90e37SPawel Dziepak 14752aaad17SPawel Dziepak rw_lock fDelegationLock; 14852aaad17SPawel Dziepak Delegation* fDelegation; 14952aaad17SPawel Dziepak 15013a90e37SPawel Dziepak uint64 fChange; 1510bc98afdSPawel Dziepak void* fFileCache; 15213a90e37SPawel Dziepak mutex fFileCacheLock; 153e743e243SPawel Dziepak uint64 fMaxFileSize; 154d4a75b9cSPawel Dziepak 15552aaad17SPawel Dziepak OpenState* fOpenState; 156eeabdab1SPawel Dziepak mutex fStateLock; 157eeabdab1SPawel Dziepak 158d4a75b9cSPawel Dziepak bool fWriteDirty; 159bf31ee39SPawel Dziepak }; 160bf31ee39SPawel Dziepak 161bf31ee39SPawel Dziepak 162bf31ee39SPawel Dziepak inline ino_t 1630dbff361SPawel Dziepak Inode::FileIdToInoT(uint64 fileid) 1644b48348fSPawel Dziepak { 1654b48348fSPawel Dziepak if (sizeof(ino_t) >= sizeof(uint64)) 1664b48348fSPawel Dziepak return fileid; 1674b48348fSPawel Dziepak else 1684b48348fSPawel Dziepak return (ino_t)fileid ^ (fileid >> 1694b48348fSPawel Dziepak (sizeof(uint64) - sizeof(ino_t)) * 8); 1704b48348fSPawel Dziepak } 1714b48348fSPawel Dziepak 1724b48348fSPawel Dziepak 1734b48348fSPawel Dziepak inline ino_t 174bf31ee39SPawel Dziepak Inode::ID() const 175bf31ee39SPawel Dziepak { 1760dbff361SPawel Dziepak return FileIdToInoT(fInfo.fFileId); 177bf31ee39SPawel Dziepak } 178bf31ee39SPawel Dziepak 179bf31ee39SPawel Dziepak 180d38e98d8SPawel Dziepak inline mode_t 181d38e98d8SPawel Dziepak Inode::Type() const 182d38e98d8SPawel Dziepak { 183d38e98d8SPawel Dziepak return sNFSFileTypeToHaiku[fType]; 184d38e98d8SPawel Dziepak } 185d38e98d8SPawel Dziepak 186d38e98d8SPawel Dziepak 18700a8558cSPawel Dziepak inline FileSystem* 18800a8558cSPawel Dziepak Inode::GetFileSystem() const 189be4c74eaSPawel Dziepak { 19000a8558cSPawel Dziepak return fFileSystem; 191be4c74eaSPawel Dziepak } 192be4c74eaSPawel Dziepak 193be4c74eaSPawel Dziepak 1940bc98afdSPawel Dziepak inline void* 1950bc98afdSPawel Dziepak Inode::FileCache() 1960bc98afdSPawel Dziepak { 1970bc98afdSPawel Dziepak return fFileCache; 1980bc98afdSPawel Dziepak } 1990bc98afdSPawel Dziepak 2000bc98afdSPawel Dziepak 20152aaad17SPawel Dziepak inline void 20252aaad17SPawel Dziepak Inode::SetOpenState(OpenState* state) 20352aaad17SPawel Dziepak { 20452aaad17SPawel Dziepak MutexLocker _(fStateLock); 20552aaad17SPawel Dziepak fOpenState = state; 20652aaad17SPawel Dziepak } 20752aaad17SPawel Dziepak 20852aaad17SPawel Dziepak 209e743e243SPawel Dziepak inline uint64 210e743e243SPawel Dziepak Inode::MaxFileSize() 211e743e243SPawel Dziepak { 212e743e243SPawel Dziepak return fMaxFileSize; 213e743e243SPawel Dziepak } 214e743e243SPawel Dziepak 215e743e243SPawel Dziepak 216224d6022SPawel Dziepak inline uint64 217224d6022SPawel Dziepak Inode::Change() 218224d6022SPawel Dziepak { 219224d6022SPawel Dziepak return fChange; 220224d6022SPawel Dziepak } 221224d6022SPawel Dziepak 222224d6022SPawel Dziepak 223224d6022SPawel Dziepak inline bool 224224d6022SPawel Dziepak Inode::Dirty() 225224d6022SPawel Dziepak { 226224d6022SPawel Dziepak return fWriteDirty; 227224d6022SPawel Dziepak } 228224d6022SPawel Dziepak 229224d6022SPawel Dziepak 230b1fd656dSPawel Dziepak inline OpenState* 231b1fd656dSPawel Dziepak Inode::GetOpenState() 232b1fd656dSPawel Dziepak { 233b1fd656dSPawel Dziepak return fOpenState; 234b1fd656dSPawel Dziepak } 235b1fd656dSPawel Dziepak 236b1fd656dSPawel Dziepak 237bf31ee39SPawel Dziepak #endif // INODE_H 238bf31ee39SPawel Dziepak 239