metalsvm/include/metalsvm/fs.h
2011-08-02 12:00:25 +02:00

250 lines
6.9 KiB
C
Executable file

/*
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
* RWTH Aachen University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of MetalSVM.
*/
/**
* @author Stefan Lankes
* @file include/metalsvm/fs.h
* @brief Filesystem related functions and structures
*/
#ifndef __FS_H__
#define __FS_H__
#include <metalsvm/stddef.h>
#include <metalsvm/spinlock_types.h>
#define FS_FILE 0x01
#define FS_DIRECTORY 0x02
#define FS_CHARDEVICE 0x03
//#define FS_BLOCKDEVICE 0x04
//#define FS_PIPE 0x05
//#define FS_SYMLINK 0x06
//#define FS_MOUNTPOINT 0x08 // Is the file an active mountpoint?
/*file descriptor init*/
#define NR_OPEN 10
#define FS_INIT { [0 ... NR_OPEN-1] = {NULL, 0, 0} }
/*open flags*/
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 64
#define O_EXCL 128
//#define O_NOCTTY 256
#define O_TRUNC 512
#define O_APPEND 1024
//#define O_NDELAY 2048
//#define O_SYNC 4096
//#define O_ASYNC 8192
/*lseek defines*/
#ifndef SEEK_SET
#define SEEK_SET 0 /* set file offset to offset */
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1 /* set file offset to current plus offset */
#endif
#ifndef SEEK_END
#define SEEK_END 2 /* set file offset to EOF plus offset */
#endif
struct vfs_node;
struct fildes;
/** @defgroup fsprototypes FS access function prototypes
*
* These typedefs define the type of callbacks - called when read/write/open/close are called.\n
* They aren't as well documented as the read_fs and so on functions. Just look there for further information.
*
* @{
*/
/** @brief Read function pointer */
typedef ssize_t (*read_type_t) (struct fildes *, uint8_t*, size_t);
/** @brief Write function pointer */
typedef ssize_t (*write_type_t) (struct fildes *, uint8_t*, size_t);
/** @brief Open function pointer */
typedef int (*open_type_t) (struct fildes *, const char *name);
/** @brief Close function pointer */
typedef int (*close_type_t) (struct vfs_node *);
/** @brief Read directory function pointer */
typedef struct dirent *(*readdir_type_t) (struct vfs_node *, uint32_t);
/** @brief Find directory function pointer */
typedef struct vfs_node *(*finddir_type_t) (struct vfs_node *, const char *name);
/** @brief Make directory function pointer */
typedef struct vfs_node *(*mkdir_type_t) (struct vfs_node *, const char *name);
/** @} */
#define MAX_DATABLOCKS 12
#define MAX_DIRENTRIES 32
#define MAX_DATAENTRIES 4096
/** @brief Block list structure. VFS nodes keep those in a list */
typedef struct block_list {
/// Array with pointers to data blocks
void* data[MAX_DATABLOCKS];
/// Pointer to the next block_list in the list
struct block_list* next;
} block_list_t;
typedef struct vfs_node {
/// The permissions mask.
uint32_t mask;
/// The owning user.
uint32_t uid;
/// The owning group.
uint32_t gid;
/// Includes the node type. See #defines above.
uint32_t type;
/// Open handler function pointer
open_type_t open;
/// Close handler function pointer
close_type_t close;
/// Read handler function pointer
read_type_t read;
/// Write handler function pointer
write_type_t write;
/// Read dir handler function pointer
readdir_type_t readdir;
/// Find dir handler function pointer
finddir_type_t finddir;
/// Make dir handler function pointer
mkdir_type_t mkdir;
/// Lock variable to thread-protect this structure
spinlock_t lock;
/// Block size
size_t block_size;
/// List of blocks
block_list_t block_list;
} vfs_node_t;
/** @brief file descriptor structure */
typedef struct fildes {
vfs_node_t* node; /* */
off_t offset; /* */
int flags; /* */
int mode; /* */
} fildes_t;
/** @brief Directory entry structure */
typedef struct dirent {
/// Directory name
char name[MAX_FNAME];
/// Corresponding VFS node pointer
vfs_node_t* vfs_node;
} dirent_t;
/** @brief Dir block structure which will keep directory entries */
typedef struct {
/// Array of directory entries
dirent_t entries[MAX_DIRENTRIES];
} dir_block_t;
typedef struct {
///Array of data entries
char entries[MAX_DATAENTRIES];
} data_block_t;
extern vfs_node_t* fs_root; // The root of the filesystem.
/** @defgroup fsfunc FS related functions
*
* Standard read/write/open/close/mkdir functions. Note that these are all suffixed with
* _fs to distinguish them from the read/write/open/close which deal with file descriptors,
* not file nodes.
*
* @{
*/
/** @brief Read from file system into the buffer
* @param node Pointer to the node to read from
* @param buffer Pointer to buffer to write into
* @param size Number of bytes to read
* @param offset Offset position of the source range
* @return
* - number of bytes copied (size)
* - 0 on error
*/
ssize_t read_fs(fildes_t* file, uint8_t* buffer, size_t size);
/** @brief Write into the file system from the buffer
* @param node Pointer to the node to write to
* @param buffer Pointer to buffer to read from
* @param size Number of bytes to read
* @param offset Offset position of the destination range
* @return
* - number of bytes copied (size)
* - 0 on error
*/
ssize_t write_fs(fildes_t* file, uint8_t* buffer, size_t size);
/** @brief Yet to be documented */
int open_fs(fildes_t* file, const char* fname);
/** @brief Yet to be documented */
int close_fs(vfs_node_t * node);
/** @brief Get dir entry at index
* @param node VFS node to get dir entry from
* @param index Index position of desired dir entry
* @return
* - The desired dir entry
* - NULL on failure
*/
struct dirent *readdir_fs(vfs_node_t * node, uint32_t index);
/** @brief Find a directory by looking for the dir name
* @param node The node where to start the search from
* @param name The dir name string
* @return
* - a VFS node pointer
* - NULL on failure
*/
vfs_node_t* finddir_fs(vfs_node_t * node, const char *name);
/** @brief Make a new directory in a VFS node
* @param node Pointer to the node where the dir is to create in
* @param name Name of the new directory
* @return
* - new VFS node pointer
* - NULL on failure
*/
vfs_node_t* mkdir_fs(vfs_node_t* node, const char* name);
/** @brief Find a node within root file system
* @param name The node name
* @return
* - VFS node pointer
* - NULL on failure
*/
vfs_node_t* findnode_fs(const char* name);
/* @} */
int null_init(vfs_node_t* node, const char* name);
int stdin_init(vfs_node_t* node, const char* name);
int stdout_init(vfs_node_t* node, const char* name);
int stderr_init(vfs_node_t* node, const char* name);
int initrd_init(void);
#endif