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
124d120407SPawel Dziepak #include "DirectoryCache.h"
137efb4c9bSPawel Dziepak #include "MetadataCache.h"
140dbff361SPawel Dziepak #include "NFS4Inode.h"
15eeabdab1SPawel Dziepak #include "OpenState.h"
16bf31ee39SPawel Dziepak
17bf31ee39SPawel Dziepak
1852aaad17SPawel Dziepak class Delegation;
1952aaad17SPawel Dziepak
200dbff361SPawel Dziepak class Inode : public NFS4Inode {
21bf31ee39SPawel Dziepak public:
2200a8558cSPawel Dziepak static status_t CreateInode(FileSystem* fs, const FileInfo& fi,
230792abe0SPawel Dziepak Inode** inode);
24e8c12d94SPawel Dziepak virtual ~Inode();
25bf31ee39SPawel Dziepak
26bf31ee39SPawel Dziepak inline ino_t ID() const;
27d38e98d8SPawel Dziepak inline mode_t Type() const;
28e8c12d94SPawel Dziepak virtual const char* Name() const;
2900a8558cSPawel Dziepak inline FileSystem* GetFileSystem() const;
30bf31ee39SPawel Dziepak
3152aaad17SPawel Dziepak inline void SetOpenState(OpenState* state);
3252aaad17SPawel Dziepak
330bc98afdSPawel Dziepak inline void* FileCache();
3413a90e37SPawel Dziepak status_t RevalidateFileCache();
3513a90e37SPawel Dziepak
36e743e243SPawel Dziepak inline uint64 MaxFileSize();
37e743e243SPawel Dziepak
38224d6022SPawel Dziepak inline uint64 Change();
39224d6022SPawel Dziepak inline bool Dirty();
40224d6022SPawel Dziepak
41b1fd656dSPawel Dziepak inline OpenState* GetOpenState();
42b1fd656dSPawel Dziepak
434a153753SPawel Dziepak void SetDelegation(Delegation* delegation);
444a153753SPawel Dziepak void RecallDelegation(bool truncate = false);
45cde3994eSPawel Dziepak void RecallReadDelegation();
464a153753SPawel Dziepak
470dbff361SPawel Dziepak status_t LookUp(const char* name, ino_t* id);
480dbff361SPawel Dziepak
490dbff361SPawel Dziepak status_t Access(int mode);
5075fe7b90SPawel Dziepak
5136577ed5SPawel Dziepak status_t Commit();
52e2e5f06dSPawel Dziepak status_t SyncAndCommit(bool force = false);
5336577ed5SPawel Dziepak
540dbff361SPawel Dziepak status_t CreateObject(const char* name, const char* path,
55b352cbf6SPawel Dziepak int mode, FileType type, ino_t* id);
56f5baf2efSPawel Dziepak
57f5baf2efSPawel Dziepak status_t CreateLink(const char* name, const char* path,
58b352cbf6SPawel Dziepak int mode, ino_t* id);
59f5baf2efSPawel Dziepak
60c0de7acaSPawel Dziepak status_t Link(Inode* dir, const char* name);
617c6cdb83SPawel Dziepak status_t Remove(const char* name, FileType type,
62dee0fd83SPawel Dziepak ino_t* id = NULL);
631ec6b0c1SPawel Dziepak static status_t Rename(Inode* from, Inode* to,
6420d1b02eSPawel Dziepak const char* fromName, const char* toName,
65*efc29cc5SPawel Dziepak bool attribute = false, ino_t* id = NULL,
66*efc29cc5SPawel Dziepak ino_t* oldID = NULL);
67abba2fddSPawel Dziepak
6820d1b02eSPawel Dziepak status_t Stat(struct stat* st,
6920d1b02eSPawel Dziepak OpenAttrCookie* attr = NULL);
7020d1b02eSPawel Dziepak status_t WriteStat(const struct stat* st, uint32 mask,
7120d1b02eSPawel Dziepak OpenAttrCookie* attr = NULL);
727bfa4fe8SPawel Dziepak
7320bd3ee1SPawel Dziepak status_t Create(const char* name, int mode, int perms,
742c36184fSPawel Dziepak OpenFileCookie* cookie,
752c36184fSPawel Dziepak OpenDelegationData* data, ino_t* id);
762a292557SPawel Dziepak status_t Open(int mode, OpenFileCookie* cookie);
7760a8140dSPawel Dziepak status_t Close(OpenFileCookie* cookie);
7820d1b02eSPawel Dziepak
7920d1b02eSPawel Dziepak status_t OpenAttr(const char* name, int mode,
8020d1b02eSPawel Dziepak OpenAttrCookie* cookie, bool create,
8120d1b02eSPawel Dziepak int32 type = 0);
8220d1b02eSPawel Dziepak status_t CloseAttr(OpenAttrCookie* cookie);
8320d1b02eSPawel Dziepak
842f2e57d0SPawel Dziepak status_t Read(OpenFileCookie* cookie, off_t pos,
85e743e243SPawel Dziepak void* buffer, size_t* length);
86e36a5442SPawel Dziepak status_t Write(OpenFileCookie* cookie, off_t pos,
8786d2cab3SPawel Dziepak const void* buffer, size_t* _length);
882a292557SPawel Dziepak
8920d1b02eSPawel Dziepak status_t ReadDirect(OpenStateCookie* cookie, off_t pos,
90e743e243SPawel Dziepak void* buffer, size_t* length, bool* eof);
9120d1b02eSPawel Dziepak status_t WriteDirect(OpenStateCookie* cookie, off_t pos,
92e743e243SPawel Dziepak const void* buffer, size_t* _length);
93e743e243SPawel Dziepak
94b352cbf6SPawel Dziepak status_t CreateDir(const char* name, int mode,
95b352cbf6SPawel Dziepak ino_t* id);
96be4c74eaSPawel Dziepak status_t OpenDir(OpenDirCookie* cookie);
974b48348fSPawel Dziepak status_t ReadDir(void* buffer, uint32 size,
98be4c74eaSPawel Dziepak uint32* count, OpenDirCookie* cookie);
99212de720SPawel Dziepak
100f7c35cf4SPawel Dziepak status_t OpenAttrDir(OpenDirCookie* cookie);
101f7c35cf4SPawel Dziepak
1027d0b8c97SPawel Dziepak status_t TestLock(OpenFileCookie* cookie,
1037d0b8c97SPawel Dziepak struct flock* lock);
1047d0b8c97SPawel Dziepak status_t AcquireLock(OpenFileCookie* cookie,
1057d0b8c97SPawel Dziepak const struct flock* lock, bool wait);
1067d0b8c97SPawel Dziepak status_t ReleaseLock(OpenFileCookie* cookie,
1077d0b8c97SPawel Dziepak const struct flock* lock);
1087d0b8c97SPawel Dziepak status_t ReleaseAllLocks(OpenFileCookie* cookie);
1097d0b8c97SPawel Dziepak
1103ae5e813SPawel Dziepak status_t GetDirSnapshot(DirectoryCacheSnapshot**
1113ae5e813SPawel Dziepak _snapshot, OpenDirCookie* cookie,
1122314d073SPawel Dziepak uint64* _change, bool attribute);
1133ae5e813SPawel Dziepak
11420d1b02eSPawel Dziepak status_t LoadAttrDirHandle();
11520d1b02eSPawel Dziepak
116376eef4aSPawel Dziepak static inline ino_t FileIdToInoT(uint64 fileid);
11783444c23SPawel Dziepak
11883444c23SPawel Dziepak void BeginAIOOp();
11983444c23SPawel Dziepak void EndAIOOp();
12083444c23SPawel Dziepak inline void WaitAIOComplete();
1212f03ff09SPawel Dziepak protected:
1220792abe0SPawel Dziepak Inode();
1230792abe0SPawel Dziepak
12449935f9bSPawel Dziepak void ReleaseOpenState();
12549935f9bSPawel Dziepak
126eeabdab1SPawel Dziepak status_t CreateState(const char* name, int mode,
1272c36184fSPawel Dziepak int perms, OpenState* state,
1282c36184fSPawel Dziepak OpenDelegationData* data);
129eeabdab1SPawel Dziepak
130cde3994eSPawel Dziepak void ReturnDelegation(bool truncate);
131cde3994eSPawel Dziepak
1320dbff361SPawel Dziepak status_t ReadDirUp(struct dirent* de, uint32 pos,
133ed517d6cSPawel Dziepak uint32 size);
1340dbff361SPawel Dziepak status_t FillDirEntry(struct dirent* de, ino_t id,
1350dbff361SPawel Dziepak const char* name, uint32 pos, uint32 size);
1364b48348fSPawel Dziepak
1370dbff361SPawel Dziepak status_t ChildAdded(const char* name, uint64 fileID,
138df6a5eaeSPawel Dziepak const FileHandle& fileHandle);
139df6a5eaeSPawel Dziepak
14020d1b02eSPawel Dziepak status_t GetStat(struct stat* st,
14120d1b02eSPawel Dziepak OpenAttrCookie* attr = NULL);
14289f05134SPawel Dziepak
143da950cb2SPawel Dziepak char* AttrToFileName(const char* path);
144da950cb2SPawel Dziepak
1450dbff361SPawel Dziepak static inline status_t CheckLockType(short ltype, uint32 mode);
1460dbff361SPawel Dziepak
14752aaad17SPawel Dziepak private:
1480dbff361SPawel Dziepak uint32 fType;
1494b48348fSPawel Dziepak
1507efb4c9bSPawel Dziepak MetadataCache fMetaCache;
15175fe7b90SPawel Dziepak DirectoryCache* fCache;
152f7c35cf4SPawel Dziepak DirectoryCache* fAttrCache;
15313a90e37SPawel Dziepak
15452aaad17SPawel Dziepak rw_lock fDelegationLock;
15552aaad17SPawel Dziepak Delegation* fDelegation;
15652aaad17SPawel Dziepak
15713a90e37SPawel Dziepak uint64 fChange;
1580bc98afdSPawel Dziepak void* fFileCache;
15913a90e37SPawel Dziepak mutex fFileCacheLock;
160e743e243SPawel Dziepak uint64 fMaxFileSize;
161d4a75b9cSPawel Dziepak
16252aaad17SPawel Dziepak OpenState* fOpenState;
163eeabdab1SPawel Dziepak mutex fStateLock;
164eeabdab1SPawel Dziepak
16515a18a6bSPawel Dziepak rw_lock fWriteLock;
166d4a75b9cSPawel Dziepak bool fWriteDirty;
16783444c23SPawel Dziepak
16883444c23SPawel Dziepak sem_id fAIOWait;
16983444c23SPawel Dziepak uint32 fAIOCount;
17083444c23SPawel Dziepak mutex fAIOLock;
171bf31ee39SPawel Dziepak };
172bf31ee39SPawel Dziepak
173bf31ee39SPawel Dziepak
17483444c23SPawel Dziepak inline void
WaitAIOComplete()17583444c23SPawel Dziepak Inode::WaitAIOComplete()
17683444c23SPawel Dziepak {
17783444c23SPawel Dziepak acquire_sem(fAIOWait);
17883444c23SPawel Dziepak release_sem(fAIOWait);
17983444c23SPawel Dziepak }
18083444c23SPawel Dziepak
18183444c23SPawel Dziepak
182bf31ee39SPawel Dziepak inline ino_t
FileIdToInoT(uint64 fileid)1830dbff361SPawel Dziepak Inode::FileIdToInoT(uint64 fileid)
1844b48348fSPawel Dziepak {
1854b48348fSPawel Dziepak if (sizeof(ino_t) >= sizeof(uint64))
1864b48348fSPawel Dziepak return fileid;
1874b48348fSPawel Dziepak else
1884b48348fSPawel Dziepak return (ino_t)fileid ^ (fileid >>
1894b48348fSPawel Dziepak (sizeof(uint64) - sizeof(ino_t)) * 8);
1904b48348fSPawel Dziepak }
1914b48348fSPawel Dziepak
1924b48348fSPawel Dziepak
1934b48348fSPawel Dziepak inline ino_t
ID()194bf31ee39SPawel Dziepak Inode::ID() const
195bf31ee39SPawel Dziepak {
1960dbff361SPawel Dziepak return FileIdToInoT(fInfo.fFileId);
197bf31ee39SPawel Dziepak }
198bf31ee39SPawel Dziepak
199bf31ee39SPawel Dziepak
200d38e98d8SPawel Dziepak inline mode_t
Type()201d38e98d8SPawel Dziepak Inode::Type() const
202d38e98d8SPawel Dziepak {
203d38e98d8SPawel Dziepak return sNFSFileTypeToHaiku[fType];
204d38e98d8SPawel Dziepak }
205d38e98d8SPawel Dziepak
206d38e98d8SPawel Dziepak
20700a8558cSPawel Dziepak inline FileSystem*
GetFileSystem()20800a8558cSPawel Dziepak Inode::GetFileSystem() const
209be4c74eaSPawel Dziepak {
2101e67a2cdSPawel Dziepak ASSERT(fFileSystem != NULL);
21100a8558cSPawel Dziepak return fFileSystem;
212be4c74eaSPawel Dziepak }
213be4c74eaSPawel Dziepak
214be4c74eaSPawel Dziepak
2150bc98afdSPawel Dziepak inline void*
FileCache()2160bc98afdSPawel Dziepak Inode::FileCache()
2170bc98afdSPawel Dziepak {
2180bc98afdSPawel Dziepak return fFileCache;
2190bc98afdSPawel Dziepak }
2200bc98afdSPawel Dziepak
2210bc98afdSPawel Dziepak
22252aaad17SPawel Dziepak inline void
SetOpenState(OpenState * state)22352aaad17SPawel Dziepak Inode::SetOpenState(OpenState* state)
22452aaad17SPawel Dziepak {
2251e67a2cdSPawel Dziepak ASSERT(state != NULL);
22652aaad17SPawel Dziepak fOpenState = state;
22752aaad17SPawel Dziepak }
22852aaad17SPawel Dziepak
22952aaad17SPawel Dziepak
230e743e243SPawel Dziepak inline uint64
MaxFileSize()231e743e243SPawel Dziepak Inode::MaxFileSize()
232e743e243SPawel Dziepak {
233e743e243SPawel Dziepak return fMaxFileSize;
234e743e243SPawel Dziepak }
235e743e243SPawel Dziepak
236e743e243SPawel Dziepak
237224d6022SPawel Dziepak inline uint64
Change()238224d6022SPawel Dziepak Inode::Change()
239224d6022SPawel Dziepak {
240224d6022SPawel Dziepak return fChange;
241224d6022SPawel Dziepak }
242224d6022SPawel Dziepak
243224d6022SPawel Dziepak
244224d6022SPawel Dziepak inline bool
Dirty()245224d6022SPawel Dziepak Inode::Dirty()
246224d6022SPawel Dziepak {
247224d6022SPawel Dziepak return fWriteDirty;
248224d6022SPawel Dziepak }
249224d6022SPawel Dziepak
250224d6022SPawel Dziepak
251b1fd656dSPawel Dziepak inline OpenState*
GetOpenState()252b1fd656dSPawel Dziepak Inode::GetOpenState()
253b1fd656dSPawel Dziepak {
254b1fd656dSPawel Dziepak return fOpenState;
255b1fd656dSPawel Dziepak }
256b1fd656dSPawel Dziepak
257b1fd656dSPawel Dziepak
258bf31ee39SPawel Dziepak #endif // INODE_H
259bf31ee39SPawel Dziepak
260