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