Inode.h revision 6bfb10d3
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			bool		IsDirectory() const
41							{ return S_ISDIR(Mode()); }
42			bool		IsFile() const
43							{ return S_ISREG(Mode()); }
44			bool		IsSymLink() const
45							{ return S_ISLNK(Mode()); }
46			status_t	CheckPermissions(int accessMode) const;
47
48			bool		IsDeleted() const { return fUnlinked; }
49			bool		HasExtraAttributes() const
50							{ return fHasExtraAttributes; }
51
52			mode_t		Mode() const { return fNode.Mode(); }
53			int32		Flags() const { return fNode.Flags(); }
54
55			off_t		Size() const { return fNode.Size(); }
56			void		GetChangeTime(struct timespec *timespec) const
57						{ fNode.GetChangeTime(timespec, fHasExtraAttributes); }
58			void		GetModificationTime(struct timespec *timespec) const
59						{ fNode.GetModificationTime(timespec,
60							fHasExtraAttributes); }
61			void		GetCreationTime(struct timespec *timespec) const
62						{ fNode.GetCreationTime(timespec,
63							fHasExtraAttributes); }
64			void		GetAccessTime(struct timespec *timespec) const
65						{ fNode.GetAccessTime(timespec, fHasExtraAttributes); }
66			void		SetChangeTime(const struct timespec *timespec)
67						{ fNode.SetChangeTime(timespec, fHasExtraAttributes); }
68			void		SetModificationTime(const struct timespec *timespec)
69						{ fNode.SetModificationTime(timespec,
70							fHasExtraAttributes); }
71			void		SetCreationTime(const struct timespec *timespec)
72						{ fNode.SetCreationTime(timespec,
73							fHasExtraAttributes); }
74			void		SetAccessTime(const struct timespec *timespec)
75						{ fNode.SetAccessTime(timespec, fHasExtraAttributes); }
76
77			//::Volume* _Volume() const { return fVolume; }
78			Volume*		GetVolume() const { return fVolume; }
79
80			status_t	FindBlock(off_t offset, uint32& block);
81			status_t	ReadAt(off_t pos, uint8 *buffer, size_t *length);
82			status_t	WriteAt(Transaction& transaction, off_t pos,
83							const uint8* buffer, size_t* length);
84			status_t	FillGapWithZeros(off_t start, off_t end);
85
86			status_t	Resize(Transaction& transaction, off_t size);
87
88			status_t	AttributeBlockReadAt(off_t pos, uint8 *buffer,
89							size_t *length);
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			ext2_xattr_header* fAttributesBlock;
150			status_t	fInitStatus;
151};
152
153
154// The Vnode class provides a convenience layer upon get_vnode(), so that
155// you don't have to call put_vnode() anymore, which may make code more
156// readable in some cases
157
158class Vnode {
159public:
160	Vnode(Volume* volume, ino_t id)
161		:
162		fInode(NULL)
163	{
164		SetTo(volume, id);
165	}
166
167	Vnode()
168		:
169		fStatus(B_NO_INIT),
170		fInode(NULL)
171	{
172	}
173
174	~Vnode()
175	{
176		Unset();
177	}
178
179	status_t InitCheck()
180	{
181		return fStatus;
182	}
183
184	void Unset()
185	{
186		if (fInode != NULL) {
187			put_vnode(fInode->GetVolume()->FSVolume(), fInode->ID());
188			fInode = NULL;
189			fStatus = B_NO_INIT;
190		}
191	}
192
193	status_t SetTo(Volume* volume, ino_t id)
194	{
195		Unset();
196
197		return fStatus = get_vnode(volume->FSVolume(), id, (void**)&fInode);
198	}
199
200	status_t Get(Inode** _inode)
201	{
202		*_inode = fInode;
203		return fStatus;
204	}
205
206	void Keep()
207	{
208		TRACEI("Vnode::Keep()\n");
209		fInode = NULL;
210	}
211
212	status_t Publish(Transaction& transaction, Inode* inode,
213		fs_vnode_ops* vnodeOps, uint32 publishFlags)
214	{
215		TRACEI("Vnode::Publish()\n");
216		Volume* volume = transaction.GetVolume();
217
218		status_t status = B_OK;
219
220		if (!inode->IsSymLink() && volume->ID() >= 0) {
221			TRACEI("Vnode::Publish(): Publishing vnode: %d, %d, %p, %p, %x, "
222				"%x\n", (int)volume->FSVolume(), (int)inode->ID(), inode,
223				vnodeOps != NULL ? vnodeOps : &gExt2VnodeOps, (int)inode->Mode(),
224				(int)publishFlags);
225			status = publish_vnode(volume->FSVolume(), inode->ID(), inode,
226				vnodeOps != NULL ? vnodeOps : &gExt2VnodeOps, inode->Mode(),
227				publishFlags);
228			TRACEI("Vnode::Publish(): Result: %s\n", strerror(status));
229		}
230
231		if (status == B_OK) {
232			TRACEI("Vnode::Publish(): Preparing internal data\n");
233			fInode = inode;
234			fStatus = B_OK;
235
236			cache_add_transaction_listener(volume->BlockCache(),
237				transaction.ID(), TRANSACTION_ABORTED, &_TransactionListener,
238				inode);
239		}
240
241		return status;
242	}
243
244private:
245	status_t	fStatus;
246	Inode*		fInode;
247
248	// TODO: How to apply coding style here?
249	static void _TransactionListener(int32 id, int32 event, void* _inode)
250	{
251		Inode* inode = (Inode*)_inode;
252
253		if (event == TRANSACTION_ABORTED) {
254			// TODO: Unpublish?
255			panic("Transaction %d aborted, inode %p still exists!\n", (int)id,
256				inode);
257		}
258	}
259};
260
261#endif	// INODE_H
262