- Implemented first time functions.
[planlOS.git] / system / include / fs / fs.h
blobba63dd9232e0cd210297026bfacb4f1a904b59f0
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 /**
22 * \file fs.h
23 * File system access functions.
26 #ifndef FS_FS_H_INCLUDED
27 #define FS_FS_H_INCLUDED
29 #include <stdint.h>
30 #include "mm/memory.h"
32 /**
33 * File system driver can only create one file system.
35 #define FS_DRIVER_SINGLE 1
36 /**
37 * File system driver can not be mounted manually.
39 #define FS_DRIVER_NOMOUNT 2
40 /**
41 * File system does not need any data source (e.g. /dev).
43 #define FS_DRIVER_NODATA 4
45 struct FsRequest;
47 /**
48 * File system information.
50 typedef struct FsFileSystem
52 /**
53 * Path at which the file system was mounted.
55 char *path;
56 /**
57 * Device file which works as the data source for this file system.
59 struct FsFileHandle *device;
60 /**
61 * Driver which made this file system.
63 struct FsFileSystemDriver *driver;
65 /**
66 * Callback for unmounting the file system.
68 int (*unmount)(struct FsFileSystem *fs);
69 /**
70 * Callback for file system requests.
72 int (*query_request)(struct FsFileSystem *fs, struct FsRequest *request);
73 } FsFileSystem;
75 /**
76 * File system driver.
78 typedef struct FsFileSystemDriver
80 /**
81 * File system driver flags.
83 uint32_t flags;
85 /**
86 * Mounts a file system using this driver.
88 FsFileSystem *(*mount)(struct FsFileSystemDriver *driver, const char *path,
89 const char *device, uint32_t flags);
90 } FsFileSystemDriver;
92 /**
93 * File information as created by a file system.
95 typedef struct FsFile
97 /**
98 * Number of file handles referring to this file. The file is not closed
99 * unless this goes down to 0.
101 uint32_t refcount;
103 * File system which created this file.
105 FsFileSystem *fs;
106 } FsFile;
109 * File handle information
111 typedef struct FsFileHandle
114 * The file which this file handle refers to.
116 FsFile *file;
118 * Current reading/writing position.
120 uint64_t position;
122 * File flags as specified at fsOpen().
124 uint32_t flags;
125 } FsFileHandle;
128 * Initializes the file system.
130 int fsInit(void);
133 * Registers a file system driver with the given name.
135 int fsRegisterDriver(FsFileSystemDriver *driver, const char *name);
137 * Removes a file system driver.
139 int fsUnregisterDriver(FsFileSystemDriver *driver);
141 * Returns the file system driver with the given name.
143 FsFileSystemDriver *fsGetDriver(const char *name);
145 * Mounts a file system onto a directory.
146 * \param driver Driver to be used.
147 * \param path Path for the file system.
148 * \param device Device to be used as the data source for the file system.
149 * \param flags Mounting flags like readonly access.
151 int fsMount(FsFileSystemDriver *driver, const char *path, const char *device,
152 uint32_t flags);
154 * Unmounts the file system at the given path.
156 int fsUnmount(const char *path);
158 #define FS_OPEN_CREATE 1
159 #define FS_OPEN_READ 2
160 #define FS_OPEN_WRITE 4
161 #define FS_OPEN_RW 6
163 #define FS_MKNOD_FILE 1
164 #define FS_MKNOD_DIR 2
167 * Opens a file for use from within the kernel and creates a file handle.
168 * \param filename Absolute file name.
169 * \param mode Flags for the open request.
171 FsFileHandle *fsOpen(const char *filename, uint32_t mode);
173 * Closes a file handle.
175 int fsClose(FsFileHandle *file);
177 * Creates a file system node.
179 int fsMknod(const char *filename, uint32_t mode);
182 * Writes data to an opened file.
184 int fsWrite(FsFileHandle *file, void *buffer, int size);
186 * Reads data from an opened file.
188 int fsRead(FsFileHandle *file, void *buffer, int size, int blocking);
190 * Changes the current position within a file. The position can be different
191 * within multiple file handles even if they point to one file internally.
192 * \param file File handle to be used.
193 * \param offset New offset, relative to whence.
194 * \param whence Origin for the seek request, can be 0 (from the start of the
195 * file), 1 (from the current position) or 2 (from the end of the file).
196 * \return Current absolute file offset.
198 uint64_t fsSeek(FsFileHandle *file, int64_t offset, int whence);
200 * Issues a file ioctl request.
202 int fsIOCTL(FsFileHandle *file, int request, ...);
204 * Returns the buffer size for the ioctl parameter or 0 if only one number is
205 * passed to it directly as an argument.
207 int fsGetIOCTLSize(FsFileHandle *file, int request);
209 #endif