xref: /haiku/src/tests/system/kernel/file_corruption/fs/Node.h (revision 6fc498dafd3c15788fcaefc39f4e8e8e746b2bce)
1 /*
2  * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef NODE_H
6 #define NODE_H
7 
8 
9 #include <sys/types.h>
10 
11 #include <AutoLocker.h>
12 
13 #include <lock.h>
14 
15 #include "checksumfs.h"
16 
17 
18 class Transaction;
19 class Volume;
20 
21 
22 enum {
23 	NODE_ACCESSED,
24 	NODE_STAT_CHANGED,
25 	NODE_MODIFIED
26 };
27 
28 
29 class Node {
30 public:
31 								Node(Volume* volume, uint64 blockIndex,
32 									const checksumfs_node& nodeData);
33 								Node(Volume* volume, mode_t mode);
34 	virtual						~Node();
35 
36 			void				SetBlockIndex(uint64 blockIndex);
37 
38 	virtual	status_t			InitForVFS();
39 	virtual	void				DeletingNode();
40 
41 	virtual	status_t			Resize(uint64 newSize, bool fillWithZeroes,
42 									Transaction& transaction);
43 	virtual	status_t			Read(off_t pos, void* buffer, size_t size,
44 									size_t& _bytesRead);
45 	virtual	status_t			Write(off_t pos, const void* buffer,
46 									size_t size, size_t& _bytesWritten,
47 									bool& _sizeChanged);
48 	virtual	status_t			Sync();
49 
NodeData()50 	inline	const checksumfs_node& NodeData() const	{ return fNode; }
GetVolume()51 	inline	Volume*				GetVolume() const	{ return fVolume; }
BlockIndex()52 	inline	uint64				BlockIndex() const	{ return fBlockIndex; }
Mode()53 	inline	uint32				Mode() const		{ return fNode.mode; }
54 	inline	uint32				AttributeType() const;
55 	inline	uint64				ParentDirectory() const;
56 	inline	uint64				AttributeDirectory() const;
HardLinks()57 	inline	uint32				HardLinks() const	{ return fNode.hardLinks; }
UID()58 	inline	uint32				UID() const			{ return fNode.uid; }
GID()59 	inline	uint32				GID() const			{ return fNode.gid; }
Size()60 	inline	uint64				Size() const		{ return fNode.size; }
AccessedTime()61 	inline	uint64				AccessedTime() const { return fAccessedTime; }
62 	inline	uint64				CreationTime() const;
63 	inline	uint64				ModificationTime() const;
64 	inline	uint64				ChangeTime() const;
65 
66 			void				SetMode(uint32 mode);
67 			void				SetAttributeType(uint32 type);
68 			void				SetParentDirectory(uint32 blockIndex);
69 			void				SetAttributeDirectory(uint32 blockIndex);
70 			void				SetHardLinks(uint32 value);
71 			void				SetUID(uint32 uid);
72 			void				SetGID(uint32 gid);
73 			void				SetSize(uint64 size);
74 			void				SetAccessedTime(uint64 time);
75 			void				SetCreationTime(uint64 time);
76 			void				SetModificationTime(uint64 time);
77 			void				SetChangeTime(uint64 time);
78 
79 			void				Touched(int32 mode);
80 
81 	inline	bool				ReadLock();
82 	inline	bool				ReadLockWithTimeout(uint32 timeoutFlags,
83 									bigtime_t timeout);
84 	inline	void				ReadUnlock();
85 	inline	bool				WriteLock();
86 	inline	void				WriteUnlock();
87 
88 	virtual	void				RevertNodeData(const checksumfs_node& nodeData);
89 
90 			status_t			Flush(Transaction& transaction);
91 
92 private:
93 			void				_Init();
94 
95 private:
96 			rw_lock				fLock;
97 			Volume*				fVolume;
98 			uint64				fBlockIndex;
99 			uint64				fAccessedTime;
100 			checksumfs_node		fNode;
101 			bool				fNodeDataDirty;
102 };
103 
104 
105 uint32
AttributeType()106 Node::AttributeType() const
107 {
108 	return fNode.attributeType;
109 }
110 
111 
112 uint64
ParentDirectory()113 Node::ParentDirectory() const
114 {
115 	return fNode.parentDirectory;
116 }
117 
118 
119 uint64
AttributeDirectory()120 Node::AttributeDirectory() const
121 {
122 	return fNode.attributeDirectory;
123 }
124 
125 
126 uint64
CreationTime()127 Node::CreationTime() const
128 {
129 	return fNode.creationTime;
130 }
131 
132 
133 uint64
ModificationTime()134 Node::ModificationTime() const
135 {
136 	return fNode.modificationTime;
137 }
138 
139 
140 uint64
ChangeTime()141 Node::ChangeTime() const
142 {
143 	return fNode.changeTime;
144 }
145 
146 
147 bool
ReadLock()148 Node::ReadLock()
149 {
150 	return rw_lock_read_lock(&fLock) == B_OK;
151 }
152 
153 
154 bool
ReadLockWithTimeout(uint32 timeoutFlags,bigtime_t timeout)155 Node::ReadLockWithTimeout(uint32 timeoutFlags, bigtime_t timeout)
156 {
157 	return rw_lock_read_lock_with_timeout(&fLock, timeoutFlags, timeout)
158 		== B_OK;
159 }
160 
161 
162 void
ReadUnlock()163 Node::ReadUnlock()
164 {
165 	rw_lock_read_unlock(&fLock);
166 }
167 
168 
169 bool
WriteLock()170 Node::WriteLock()
171 {
172 	return rw_lock_write_lock(&fLock) == B_OK;
173 }
174 
175 
176 void
WriteUnlock()177 Node::WriteUnlock()
178 {
179 	rw_lock_write_unlock(&fLock);
180 }
181 
182 
183 typedef AutoLocker<Node, AutoLockerReadLocking<Node> > NodeReadLocker;
184 typedef AutoLocker<Node, AutoLockerWriteLocking<Node> > NodeWriteLocker;
185 
186 
187 #endif	// NODE_H
188