xref: /haiku/src/tests/add-ons/kernel/file_systems/bfs/btree/Inode.h (revision b6f76ebe7153b94820cf35f8db4facc158841abb)
18b40798fSAxel Dörfler #ifndef INODE_H
28b40798fSAxel Dörfler #define INODE_H
38b40798fSAxel Dörfler /* Inode - emulation for the B+Tree torture test
48b40798fSAxel Dörfler **
58b40798fSAxel Dörfler ** Initial version by Axel Dörfler, axeld@pinc-software.de
6*b6f76ebeSAugustin Cavalier ** This file may be used under the terms of the MIT License.
78b40798fSAxel Dörfler */
88b40798fSAxel Dörfler 
98b40798fSAxel Dörfler 
108b40798fSAxel Dörfler #include <SupportDefs.h>
118b40798fSAxel Dörfler #include <File.h>
128b40798fSAxel Dörfler 
138b40798fSAxel Dörfler #include "bfs.h"
1424e159e1SMichael Lotz #include "Utility.h"
1524e159e1SMichael Lotz 
1624e159e1SMichael Lotz 
1724e159e1SMichael Lotz #define ASSERT_READ_LOCKED_INODE(inode)		inode->AssertReadLocked()
1824e159e1SMichael Lotz #define ASSERT_WRITE_LOCKED_INODE(inode)	inode->AssertWriteLocked()
198b40798fSAxel Dörfler 
208b40798fSAxel Dörfler 
218b40798fSAxel Dörfler class Volume;
228b40798fSAxel Dörfler class Transaction;
238b40798fSAxel Dörfler 
248b40798fSAxel Dörfler 
258b40798fSAxel Dörfler class Inode {
268b40798fSAxel Dörfler 	public:
278b40798fSAxel Dörfler 		Inode(const char* name, int32 mode = S_STR_INDEX | S_ALLOW_DUPS);
288b40798fSAxel Dörfler 		~Inode();
298b40798fSAxel Dörfler 
Lock()30f6e59c50SAxel Dörfler 		rw_lock& Lock() { return fLock; }
318b40798fSAxel Dörfler 
328b40798fSAxel Dörfler 		status_t FindBlockRun(off_t pos, block_run& run, off_t& offset);
3324e159e1SMichael Lotz 		status_t Append(Transaction&, off_t bytes);
3424e159e1SMichael Lotz 		status_t SetFileSize(Transaction&, off_t bytes);
358b40798fSAxel Dörfler 
GetVolume()368b40798fSAxel Dörfler 		Volume* GetVolume() const { return fVolume; }
ID()378b40798fSAxel Dörfler 		off_t ID() const { return 0; }
Mode()388b40798fSAxel Dörfler 		int32 Mode() const { return fMode; }
Name()3924e159e1SMichael Lotz 		const char* Name() const { return "whatever"; }
BlockRun()408b40798fSAxel Dörfler 		block_run BlockRun() const { return block_run::Run(0, 0, 0); }
Parent()418b40798fSAxel Dörfler 		block_run Parent() const { return block_run::Run(0, 0, 0); }
BlockNumber()428b40798fSAxel Dörfler 		off_t BlockNumber() const { return 0; }
Node()438b40798fSAxel Dörfler 		bfs_inode* Node() { return (bfs_inode*)1; }
448b40798fSAxel Dörfler 
Size()458b40798fSAxel Dörfler 		off_t Size() const { return fSize; }
IsContainer()468b40798fSAxel Dörfler 		bool IsContainer() const { return true; }
IsDirectory()478b40798fSAxel Dörfler 		bool IsDirectory() const { return true; }
IsIndex()4824e159e1SMichael Lotz 		bool IsIndex() const { return is_index(Mode()); }
4924e159e1SMichael Lotz 
AssertReadLocked()5024e159e1SMichael Lotz 		void AssertReadLocked() { ASSERT_READ_LOCKED_RW_LOCK(&fLock); }
AssertWriteLocked()5124e159e1SMichael Lotz 		void AssertWriteLocked() { ASSERT_WRITE_LOCKED_RW_LOCK(&fLock); }
528b40798fSAxel Dörfler 
538b40798fSAxel Dörfler 	private:
54d3d255f3SAxel Dörfler 		friend void dump_inode(Inode& inode);
55d3d255f3SAxel Dörfler 
568b40798fSAxel Dörfler 		Volume*	fVolume;
578b40798fSAxel Dörfler 		BFile	fFile;
588b40798fSAxel Dörfler 		off_t	fSize;
59f6e59c50SAxel Dörfler 		rw_lock	fLock;
608b40798fSAxel Dörfler 		int32	fMode;
61d3d255f3SAxel Dörfler 
62d3d255f3SAxel Dörfler 		// for dump_inode() only:
63d3d255f3SAxel Dörfler 		off_t	fOldSize;
64d3d255f3SAxel Dörfler 		off_t	fOldLastModified;
65d3d255f3SAxel Dörfler 		off_t	fBlockNumber;
66d3d255f3SAxel Dörfler 		void*	fTree;
67d3d255f3SAxel Dörfler 		void*	fAttributes;
688b40798fSAxel Dörfler };
698b40798fSAxel Dörfler 
7024e159e1SMichael Lotz 
7124e159e1SMichael Lotz class InodeReadLocker {
7224e159e1SMichael Lotz public:
InodeReadLocker(Inode * inode)7324e159e1SMichael Lotz 	InodeReadLocker(Inode* inode)
7424e159e1SMichael Lotz 		:
7524e159e1SMichael Lotz 		fLock(&inode->Lock())
7624e159e1SMichael Lotz 	{
7724e159e1SMichael Lotz 		rw_lock_read_lock(fLock);
7824e159e1SMichael Lotz 	}
7924e159e1SMichael Lotz 
~InodeReadLocker()8024e159e1SMichael Lotz 	~InodeReadLocker()
8124e159e1SMichael Lotz 	{
8224e159e1SMichael Lotz 		if (fLock != NULL)
8324e159e1SMichael Lotz 			rw_lock_read_unlock(fLock);
8424e159e1SMichael Lotz 	}
8524e159e1SMichael Lotz 
Unlock()8624e159e1SMichael Lotz 	void Unlock()
8724e159e1SMichael Lotz 	{
8824e159e1SMichael Lotz 		if (fLock != NULL) {
8924e159e1SMichael Lotz 			rw_lock_read_unlock(fLock);
9024e159e1SMichael Lotz 			fLock = NULL;
9124e159e1SMichael Lotz 		}
9224e159e1SMichael Lotz 	}
9324e159e1SMichael Lotz 
9424e159e1SMichael Lotz private:
9524e159e1SMichael Lotz 	rw_lock*	fLock;
9624e159e1SMichael Lotz };
9724e159e1SMichael Lotz 
988b40798fSAxel Dörfler #endif	/* INODE_H */
99