MentOS  0.8.0
The Mentoring Operating System
Macros | Functions | Variables
vfs.c File Reference

Headers for Virtual File System (VFS). More...

Macros

#define __DEBUG_HEADER__   "[VFS ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 

Functions

void vfs_init (void)
 Initialize the Virtual File System (VFS). This function sets up necessary resources and structures for the VFS. It must be called before any other VFS functions.
 
int vfs_register_filesystem (file_system_type *fs)
 Register a new filesystem. More...
 
int vfs_unregister_filesystem (file_system_type *fs)
 Unregister a new filesystem. More...
 
int vfs_register_superblock (const char *name, const char *path, file_system_type *type, vfs_file_t *root)
 Register a superblock for the filesystem. More...
 
int vfs_unregister_superblock (super_block_t *sb)
 Unregister a superblock. More...
 
super_block_tvfs_get_superblock (const char *path)
 Searches for the mountpoint of the given path. More...
 
vfs_file_tvfs_open_abspath (const char *absolute_path, int flags, mode_t mode)
 Open a file given its absolute path. More...
 
vfs_file_tvfs_open (const char *path, int flags, mode_t mode)
 Given a pathname for a file, vfs_open() returns a file struct, used to access the file. More...
 
int vfs_close (vfs_file_t *file)
 Decreases the number of references to a given file, if the references number reaches 0, close the file. More...
 
ssize_t vfs_read (vfs_file_t *file, void *buf, size_t offset, size_t nbytes)
 Read data from a file. More...
 
ssize_t vfs_write (vfs_file_t *file, const void *buf, size_t offset, size_t nbytes)
 Write data to a file. More...
 
off_t vfs_lseek (vfs_file_t *file, off_t offset, int whence)
 Repositions the file offset inside a file. More...
 
ssize_t vfs_getdents (vfs_file_t *file, dirent_t *dirp, off_t off, size_t count)
 
int vfs_ioctl (vfs_file_t *file, int request, void *data)
 Perform the I/O control operation specified by REQUEST on FD. One argument may follow; its presence and type depend on REQUEST. More...
 
int vfs_unlink (const char *path)
 Delete a name and possibly the file it refers to. More...
 
int vfs_mkdir (const char *path, mode_t mode)
 Creates a new directory at the given path. More...
 
int vfs_rmdir (const char *path)
 Removes the given directory. More...
 
vfs_file_tvfs_creat (const char *path, mode_t mode)
 Creates a new file or rewrite an existing one. More...
 
ssize_t vfs_readlink (const char *path, char *buffer, size_t bufsize)
 Read the symbolic link, if present. More...
 
int vfs_symlink (const char *linkname, const char *path)
 Creates a symbolic link. More...
 
int vfs_stat (const char *path, stat_t *buf)
 Stat the file at the given path. More...
 
int vfs_fstat (vfs_file_t *file, stat_t *buf)
 Stat the given file. More...
 
int vfs_mount (const char *type, const char *path, const char *args)
 Mount the path as a filesystem of the given type. More...
 
void vfs_lock (vfs_file_t *file)
 Locks the access to the given file. More...
 
int vfs_extend_task_fd_list (struct task_struct *task)
 Extends the file descriptor list for the given task. More...
 
int vfs_init_task (task_struct *task)
 Initialize the file descriptor list for the given task. More...
 
int vfs_dup_task (task_struct *task, task_struct *old_task)
 Duplicate the file descriptor list of old_task into new_task. More...
 
int vfs_destroy_task (task_struct *task)
 Destroy the file descriptor list for the given task. More...
 
int get_unused_fd (void)
 Find the smallest available fd. More...
 
int sys_dup (int fd)
 Return new smallest available file desriptor. More...
 
static int __valid_open_permissions (const mode_t mask, const int flags, const int read, const int write)
 Checks the valid open permission. More...
 
int vfs_valid_open_permissions (int flags, mode_t mask, uid_t uid, gid_t gid)
 Check if the requested open flags against the file mask. More...
 
int vfs_valid_exec_permission (task_struct *task, vfs_file_t *file)
 Check if the file is exectuable. More...
 

Variables

static hashmap_tvfs_filesystems
 The hashmap that associates a type of Filesystem name to its mount function;.
 
static list_head vfs_super_blocks
 The list of superblocks.
 
static const unsigned vfs_filesystems_max = 10
 The maximum number of filesystem types.
 
static spinlock_t vfs_spinlock_refcount
 Lock for refcount field.
 
static spinlock_t vfs_spinlock
 Spinlock for the entire virtual filesystem.
 
static kmem_cache_tvfs_superblock_cache
 VFS memory cache for superblocks.
 
kmem_cache_tvfs_file_cache
 VFS memory cache for files. More...
 

Detailed Description

Headers for Virtual File System (VFS).

Function Documentation

◆ __valid_open_permissions()

static int __valid_open_permissions ( const mode_t  mask,
const int  flags,
const int  read,
const int  write 
)
inlinestatic

Checks the valid open permission.

Parameters
maskthe mask we need to check against.
flagsthe flags we want to check.
readthe read permissions we want to check.
writethe write permissions we want to check.
Returns
0 on falure, success otherwise.

◆ get_unused_fd()

int get_unused_fd ( void  )

Find the smallest available fd.

Returns
-errno on fail, fd on success.

◆ sys_dup()

int sys_dup ( int  fd)

Return new smallest available file desriptor.

Parameters
fdthe descriptor of the file we want to duplicate.
Returns
-errno on fail, fd on success.

◆ vfs_close()

int vfs_close ( vfs_file_t file)

Decreases the number of references to a given file, if the references number reaches 0, close the file.

Parameters
fileA pointer to the file structure.
Returns
The result of the call.

◆ vfs_creat()

vfs_file_t* vfs_creat ( const char *  path,
mode_t  mode 
)

Creates a new file or rewrite an existing one.

Parameters
pathpath to the file.
modemode for file creation.
Returns
file descriptor number, -1 otherwise and errno is set to indicate the error.

It is equivalent to: open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)

◆ vfs_destroy_task()

int vfs_destroy_task ( struct task_struct task)

Destroy the file descriptor list for the given task.

Parameters
taskThe task for which we destroy the file descriptor list.
Returns
0 on fail, 1 on success.

◆ vfs_dup_task()

int vfs_dup_task ( struct task_struct new_task,
struct task_struct old_task 
)

Duplicate the file descriptor list of old_task into new_task.

Parameters
new_taskThe task where we clone the file descriptor list.
old_taskThe task from which we clone the file descriptor list.
Returns
0 on fail, 1 on success.

◆ vfs_extend_task_fd_list()

int vfs_extend_task_fd_list ( struct task_struct task)

Extends the file descriptor list for the given task.

Parameters
taskThe task for which we extend the file descriptor list.
Returns
0 on fail, 1 on success.

◆ vfs_fstat()

int vfs_fstat ( vfs_file_t file,
stat_t buf 
)

Stat the given file.

Parameters
filePointer to the file for which we are retrieving the statistics.
bufBuffer where we are storing the statistics.
Returns
0 on success, -errno on failure.

◆ vfs_get_superblock()

super_block_t* vfs_get_superblock ( const char *  absolute_path)

Searches for the mountpoint of the given path.

Parameters
absolute_pathPath for which we want to search the mountpoint.
Returns
Pointer to the super_block_t of the mountpoint, or NULL if not found.

◆ vfs_getdents()

ssize_t vfs_getdents ( vfs_file_t file,
dirent_t dirp,
off_t  off,
size_t  count 
)

Provide access to the directory entries.

Parameters
fileThe directory for which we accessing the entries.
dirpThe buffer where de data should be placed.
offThe offset from which we start reading the entries.
countThe size of the buffer.
Returns
On success, the number of bytes read is returned. On end of directory, 0 is returned. On error, -1 is returned, and errno is set appropriately.

◆ vfs_init_task()

int vfs_init_task ( struct task_struct task)

Initialize the file descriptor list for the given task.

Parameters
taskThe task for which we initialize the file descriptor list.
Returns
0 on fail, 1 on success.

◆ vfs_ioctl()

int vfs_ioctl ( vfs_file_t file,
int  request,
void *  data 
)

Perform the I/O control operation specified by REQUEST on FD. One argument may follow; its presence and type depend on REQUEST.

Parameters
fileThe file for which we are executing the operations.
requestThe device-dependent request code
dataAn untyped pointer to memory.
Returns
Return value depends on REQUEST. Usually -1 indicates error.

◆ vfs_lock()

void vfs_lock ( vfs_file_t file)

Locks the access to the given file.

Parameters
fileThe file to lock.

◆ vfs_lseek()

off_t vfs_lseek ( vfs_file_t file,
off_t  offset,
int  whence 
)

Repositions the file offset inside a file.

Parameters
fileThe file for which we reposition the offest.
offsetThe offest to use for the operation.
whenceThe type of operation.
Returns
Upon successful completion, returns the resulting offset location as measured in bytes from the beginning of the file. On error, the value (off_t) -1 is returned and errno is set to indicate the error.

◆ vfs_mkdir()

int vfs_mkdir ( const char *  path,
mode_t  mode 
)

Creates a new directory at the given path.

Parameters
pathThe path of the new directory.
modeThe permission of the new directory.
Returns
Returns a negative value on failure.

◆ vfs_mount()

int vfs_mount ( const char *  type,
const char *  path,
const char *  args 
)

Mount the path as a filesystem of the given type.

Parameters
typeThe type of filesystem
pathThe path to where it should be mounter.
argsThe arguments passed to the filesystem mount callback.
Returns
0 on success, a negative number if fails and errno is set.

◆ vfs_open()

vfs_file_t* vfs_open ( const char *  path,
int  flags,
mode_t  mode 
)

Given a pathname for a file, vfs_open() returns a file struct, used to access the file.

Parameters
pathA pathname for a file.
flagsUsed to set the file status flags and file access modes of the open file description.
modeSpecifies the file mode bits be applied when a new file is created.
Returns
Returns a file struct, or NULL.

◆ vfs_open_abspath()

vfs_file_t* vfs_open_abspath ( const char *  absolute_path,
int  flags,
mode_t  mode 
)

Open a file given its absolute path.

Parameters
absolute_pathAn absolute path to the file.
flagsUsed to set the file status flags and access modes.
modeSpecifies the file mode bits to apply when creating a new file.
Returns
Pointer to the opened vfs_file_t structure, or NULL on error.

◆ vfs_read()

ssize_t vfs_read ( vfs_file_t file,
void *  buf,
size_t  offset,
size_t  nbytes 
)

Read data from a file.

Parameters
fileThe file structure used to reference a file.
bufThe buffer.
offsetThe offset from which the function starts to read.
nbytesThe number of bytes to read.
Returns
The number of read characters.

◆ vfs_readlink()

ssize_t vfs_readlink ( const char *  path,
char *  buffer,
size_t  bufsize 
)

Read the symbolic link, if present.

Parameters
paththe path to the symbolic link.
bufferthe buffer where we will store the symbolic link path.
bufsizethe size of the buffer.
Returns
The number of read characters on success, -1 otherwise and errno is set to indicate the error.

◆ vfs_register_filesystem()

int vfs_register_filesystem ( file_system_type fs)

Register a new filesystem.

Parameters
fsA pointer to the information concerning the new filesystem.
Returns
The outcome of the operation, 0 if fails.

◆ vfs_register_superblock()

int vfs_register_superblock ( const char *  name,
const char *  path,
file_system_type type,
vfs_file_t root 
)

Register a superblock for the filesystem.

Parameters
nameThe name of the superblock.
pathThe path associated with the superblock.
typeA pointer to the filesystem type.
rootA pointer to the root file of the filesystem.
Returns
1 on success, 0 on failure.

◆ vfs_rmdir()

int vfs_rmdir ( const char *  path)

Removes the given directory.

Parameters
pathThe path to the directory to remove.
Returns
Returns a negative value on failure.

◆ vfs_stat()

int vfs_stat ( const char *  path,
stat_t buf 
)

Stat the file at the given path.

Parameters
pathPath to the file for which we are retrieving the statistics.
bufBuffer where we are storing the statistics.
Returns
0 on success, -errno on failure.

◆ vfs_symlink()

int vfs_symlink ( const char *  linkname,
const char *  path 
)

Creates a symbolic link.

Parameters
linknamethe name of the link.
paththe entity it is linking to.
Returns
0 on success, a negative number if fails and errno is set.

◆ vfs_unlink()

int vfs_unlink ( const char *  path)

Delete a name and possibly the file it refers to.

Parameters
pathThe path to the file.
Returns
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

◆ vfs_unregister_filesystem()

int vfs_unregister_filesystem ( file_system_type fs)

Unregister a new filesystem.

Parameters
fsA pointer to the information concerning the filesystem.
Returns
The outcome of the operation, 0 if fails.

◆ vfs_unregister_superblock()

int vfs_unregister_superblock ( super_block_t sb)

Unregister a superblock.

Parameters
sbA pointer to the superblock to unregister.
Returns
1 on success, 0 on failure.

◆ vfs_valid_exec_permission()

int vfs_valid_exec_permission ( struct task_struct task,
vfs_file_t file 
)

Check if the file is exectuable.

Parameters
taskThe task to execute the file.
fileThe file to execute.
Returns
0 on fail, 1 on success.

◆ vfs_valid_open_permissions()

int vfs_valid_open_permissions ( int  flags,
mode_t  mask,
uid_t  uid,
gid_t  gid 
)

Check if the requested open flags against the file mask.

Parameters
flagsThe requested open flags.
maskThe permissions of the file.
uidThe owner of the task opening the file.
gidThe group of the task opening the file.
Returns
0 on fail, 1 on success.

◆ vfs_write()

ssize_t vfs_write ( vfs_file_t file,
const void *  buf,
size_t  offset,
size_t  nbytes 
)

Write data to a file.

Parameters
fileThe file structure used to reference a file.
bufThe buffer.
offsetThe offset from which the function starts to write.
nbytesThe number of bytes to write.
Returns
The number of written characters.

Variable Documentation

◆ vfs_file_cache

kmem_cache_t* vfs_file_cache

VFS memory cache for files.

Cache for file structures in the VFS.