vfs.h revision 59ae1c81
1/*
2 * Copyright 2003-2005, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef KERNEL_BOOT_VFS_H
6#define KERNEL_BOOT_VFS_H
7
8
9#include <dirent.h>
10
11#include <SupportDefs.h>
12
13#include <util/DoublyLinkedList.h>
14#include <boot/stage2_args.h>
15
16
17#ifdef __cplusplus
18
19struct file_map_run;
20struct stat;
21
22/** This is the base class for all VFS nodes */
23
24class Node : public DoublyLinkedListLinkImpl<Node> {
25	public:
26		Node();
27		virtual ~Node();
28
29		virtual status_t Open(void **_cookie, int mode);
30		virtual status_t Close(void *cookie);
31
32		virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer,
33			size_t bufferSize) = 0;
34		virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer,
35			size_t bufferSize) = 0;
36
37		virtual status_t ReadLink(char* buffer, size_t bufferSize);
38
39		virtual status_t GetName(char *nameBuffer, size_t bufferSize) const;
40		virtual status_t GetFileMap(struct file_map_run *runs, int32 *count);
41		virtual int32 Type() const;
42		virtual off_t Size() const;
43		virtual ino_t Inode() const;
44
45		void Stat(struct stat& stat);
46
47		status_t Acquire();
48		status_t Release();
49
50	protected:
51		int32		fRefCount;
52};
53
54typedef DoublyLinkedList<Node> NodeList;
55typedef NodeList::Iterator NodeIterator;
56
57
58class Directory : public Node {
59	public:
60		Directory();
61
62		virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize);
63		virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize);
64
65		virtual int32 Type() const;
66
67		virtual Node* Lookup(const char* name, bool traverseLinks);
68		virtual Node* LookupDontTraverse(const char* name) = 0;
69
70		virtual status_t GetNextEntry(void *cookie, char *nameBuffer, size_t bufferSize) = 0;
71		virtual status_t GetNextNode(void *cookie, Node **_node) = 0;
72		virtual status_t Rewind(void *cookie) = 0;
73		virtual bool IsEmpty() = 0;
74
75		virtual status_t CreateFile(const char *name, mode_t permissions,
76			Node **_node);
77};
78
79/** The console based nodes don't need cookies for I/O, they
80 *	also don't support to change the stream position.
81 *	Live is simple in the boot loader :-)
82 */
83
84class ConsoleNode : public Node {
85	public:
86		ConsoleNode();
87
88		virtual ssize_t Read(void *buffer, size_t bufferSize);
89		virtual ssize_t Write(const void *buffer, size_t bufferSize);
90};
91
92
93class MemoryDisk : public Node {
94	public:
95		MemoryDisk(const uint8* data, size_t size, const char* name);
96
97		virtual ssize_t ReadAt(void* cookie, off_t pos, void* buffer,
98			size_t bufferSize);
99		virtual ssize_t WriteAt(void* cookie, off_t pos, const void* buffer,
100			size_t bufferSize);
101
102		virtual off_t Size() const;
103		virtual status_t GetName(char *nameBuffer, size_t bufferSize) const;
104
105	private:
106		const uint8*	fData;
107		size_t			fSize;
108		char			fName[64];
109};
110
111
112class BootVolume {
113public:
114								BootVolume();
115								~BootVolume();
116
117			status_t			SetTo(Directory* rootDirectory);
118			void				Unset();
119
120			bool				IsValid() const
121									{ return fRootDirectory != NULL; }
122
123			Directory*			RootDirectory() const
124									{ return fRootDirectory; }
125			Directory*			SystemDirectory() const
126									{ return fSystemDirectory; }
127			bool				IsPackaged() const
128									{ return fPackaged; }
129
130private:
131			int					_OpenSystemPackage();
132
133private:
134			Directory*			fRootDirectory;
135				// root directory of the volume
136			Directory*			fSystemDirectory;
137				// "system" directory of the volume; if packaged the root
138				// directory of the mounted packagefs
139			bool				fPackaged;
140				// indicates whether the boot volume's system is packaged
141};
142
143
144/* function prototypes */
145
146extern status_t vfs_init(stage2_args *args);
147extern status_t register_boot_file_system(BootVolume& bootVolume);
148extern status_t get_boot_file_system(stage2_args* args,
149			BootVolume& _bootVolume);
150extern status_t mount_file_systems(stage2_args *args);
151extern int open_node(Node *node, int mode);
152extern int open_from(Directory *directory, const char *path, int mode,
153			mode_t permissions = 0);
154extern DIR* open_directory(Directory* baseDirectory, const char* path);
155extern status_t get_stat(Directory* directory, const char* path,
156			struct stat& st);
157
158extern Node* get_node_from(int fd);
159	// returns a reference
160extern Directory* directory_from(DIR* dir);
161	// does not return a reference
162
163extern status_t add_partitions_for(int fd, bool mountFileSystems, bool isBootDevice = false);
164extern status_t add_partitions_for(Node *device, bool mountFileSystems, bool isBootDevice = false);
165
166#endif	/* __cplusplus */
167
168#endif	/* KERNEL_BOOT_VFS_H */
169