VFS: local ram_fs instances

Move FS Node implementations from server/ram_fs to include/ram_fs.
Support embedded ram_fs instances in VFS configurations using <ram/>.
Add 'no space' handling to VFS symlink ops.

Fixes #1635
This commit is contained in:
Emery Hemingway 2015-07-22 00:32:42 -05:00 committed by Christian Helmuth
parent 9d37510d1d
commit f996697fd5
12 changed files with 474 additions and 30 deletions

View File

@ -0,0 +1,70 @@
#
# \brief Test for using the vfs local ram_fs
# \author Norman Feske
# \date 2014-04-10
#
# \author Emery Hemingway
# \date 2015-07-21
#
#
# Build
#
build { core init test/libc_vfs }
create_boot_directory
#
# Generate config
#
install_config {
<config>
<parent-provides>
<service name="ROM"/>
<service name="RAM"/>
<service name="CAP"/>
<service name="PD"/>
<service name="RM"/>
<service name="CPU"/>
<service name="LOG"/>
</parent-provides>
<start name="test-libc_vfs">
<resource name="RAM" quantum="2M"/>
<route>
<any-service> <parent/> </any-service>
</route>
<config>
<iterations value="1"/>
<libc stdout="/dev/log" cwd="/tmp" >
<vfs>
<dir name="tmp" >
<ram/>
</dir>
<dir name="dev"> <log/> </dir>
</vfs>
</libc>
</config>
</start>
</config>
}
#
# Boot modules
#
build_boot_image {
core init
ld.lib.so libc.lib.so
test-libc_vfs
}
#
# Execute test case
#
append qemu_args " -m 128 -nographic "
run_genode_until {.*child "test-libc_vfs" exited with exit value 0.*} 60
# vi: set ft=tcl :

View File

@ -712,6 +712,7 @@ int Libc::Vfs_plugin::symlink(const char *oldpath, const char *newpath)
case Result::SYMLINK_ERR_NO_ENTRY: errno = ENOENT; return -1;
case Result::SYMLINK_ERR_NAME_TOO_LONG: errno = ENAMETOOLONG; return -1;
case Result::SYMLINK_ERR_NO_PERM: errno = ENOSYS; return -1;
case Result::SYMLINK_ERR_NO_SPACE: errno = ENOSPC; return -1;
case Result::SYMLINK_OK: break;
}
return 0;

View File

@ -11,8 +11,8 @@
* under the terms of the GNU General Public License version 2.
*/
#ifndef _CHUNK_H_
#define _CHUNK_H_
#ifndef _INCLUDE__RAM_FS__CHUNK_H_
#define _INCLUDE__RAM_FS__CHUNK_H_
/* Genode includes */
#include <util/noncopyable.h>
@ -438,4 +438,4 @@ namespace File_system {
};
};
#endif /* _CHUNK_H_ */
#endif /* _INCLUDE__RAM_FS__CHUNK_H_ */

View File

@ -4,16 +4,16 @@
* \date 2012-04-11
*/
#ifndef _DIRECTORY_H_
#define _DIRECTORY_H_
#ifndef _INCLUDE__RAM_FS__DIRECTORY_H_
#define _INCLUDE__RAM_FS__DIRECTORY_H_
/* Genode includes */
#include <file_system/util.h>
/* local includes */
#include <node.h>
#include <file.h>
#include <symlink.h>
#include <ram_fs/node.h>
#include <ram_fs/file.h>
#include <ram_fs/symlink.h>
namespace File_system {
@ -28,6 +28,13 @@ namespace File_system {
Directory(char const *name) : _num_entries(0) { Node::name(name); }
Node *entry_unsynchronized(size_t index)
{
Node *node = _entries.first();
for (unsigned i = 0; i < index && node; node = node->next(), i++);
return node;
}
bool has_sub_node_unsynchronized(char const *name) const
{
Node const *sub_node = _entries.first();
@ -184,9 +191,7 @@ namespace File_system {
return 0;
}
/* find list element */
Node *node = _entries.first();
for (unsigned i = 0; i < index && node; node = node->next(), i++);
Node *node = entry_unsynchronized(index);
/* index out of range */
if (!node)
@ -213,4 +218,4 @@ namespace File_system {
};
}
#endif /* _DIRECTORY_H_ */
#endif /* _INCLUDE__RAM_FS__DIRECTORY_H_ */

View File

@ -4,15 +4,15 @@
* \date 2012-04-11
*/
#ifndef _FILE_H_
#define _FILE_H_
#ifndef _INCLUDE__RAM_FS__FILE_H_
#define _INCLUDE__RAM_FS__FILE_H_
/* Genode includes */
#include <base/allocator.h>
/* local includes */
#include <node.h>
#include <chunk.h>
#include <ram_fs/node.h>
#include <ram_fs/chunk.h>
namespace File_system {
@ -103,4 +103,4 @@ namespace File_system {
};
}
#endif /* _FILE_H_ */
#endif /* _INCLUDE__RAM_FS__FILE_H_ */

View File

@ -4,8 +4,8 @@
* \date 2012-04-11
*/
#ifndef _NODE_H_
#define _NODE_H_
#ifndef _INCLUDE__RAM_FS__NODE_H_
#define _INCLUDE__RAM_FS__NODE_H_
/* Genode includes */
#include <file_system/listener.h>
@ -55,4 +55,4 @@ namespace File_system {
}
#endif /* _NODE_H_ */
#endif /* _INCLUDE__RAM_FS__NODE_H_ */

View File

@ -4,11 +4,11 @@
* \date 2012-04-11
*/
#ifndef _SYMLINK_H_
#define _SYMLINK_H_
#ifndef _INCLUDE__RAM_FS__SYMLINK_H_
#define _INCLUDE__RAM_FS__SYMLINK_H_
/* local includes */
#include <node.h>
#include <ram_fs/node.h>
namespace File_system {
@ -44,4 +44,4 @@ namespace File_system {
};
}
#endif /* _SYMLINK_H_ */
#endif /* _INCLUDE__RAM_FS__SYMLINK_H_ */

View File

@ -160,8 +160,8 @@ struct Vfs::Directory_service
*************/
enum Symlink_result { SYMLINK_ERR_EXISTS, SYMLINK_ERR_NO_ENTRY,
SYMLINK_ERR_NAME_TOO_LONG, SYMLINK_ERR_NO_PERM,
SYMLINK_OK };
SYMLINK_ERR_NO_SPACE, SYMLINK_ERR_NO_PERM,
SYMLINK_ERR_NAME_TOO_LONG, SYMLINK_OK };
virtual Symlink_result symlink(char const *from, char const *to) = 0;

View File

@ -0,0 +1,368 @@
/*
* \brief Embedded RAM VFS
* \author Emery Hemingway
* \date 2015-07-21
*/
/*
* Copyright (C) 2015 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _INCLUDE__VFS__RAM_FILE_SYSTEM_H_
#define _INCLUDE__VFS__RAM_FILE_SYSTEM_H_
#include <vfs/dir_file_system.h>
#include <ram_fs/directory.h>
namespace Vfs {
using namespace ::File_system;
class Ram_file_system;
}
class Vfs::Ram_file_system : public File_system, private ::File_system::Directory
{
private:
struct Ram_vfs_handle : Vfs_handle
{
private:
::File_system::Node *_node;
public:
Ram_vfs_handle(File_system &fs, int status_flags,
::File_system::Node *node)
: Vfs_handle(fs, fs, status_flags), _node(node)
{ }
::File_system::Node *node() const { return _node; }
};
public:
/**
* Constructor
*/
Ram_file_system(Xml_node config)
: Directory("/") { }
/***************************
** File_system interface **
***************************/
static char const *name() { return "ram"; }
void sync() { }
/********************************
** File I/O service interface **
********************************/
Write_result write(Vfs_handle *vfs_handle,
char const *buf, file_size buf_size,
file_size &out_count)
{
Ram_vfs_handle const *handle = static_cast<Ram_vfs_handle *>(vfs_handle);
out_count = handle->node()->write(buf, buf_size, handle->seek());
return WRITE_OK;
}
Read_result read(Vfs_handle *vfs_handle, char *dst, file_size count,
file_size &out_count)
{
Ram_vfs_handle const *handle = static_cast<Ram_vfs_handle *>(vfs_handle);
out_count = handle->node()->read(dst, count, handle->seek());
return READ_OK;
}
Ftruncate_result ftruncate(Vfs_handle *vfs_handle, file_size len)
{
Ram_vfs_handle const *handle = static_cast<Ram_vfs_handle *>(vfs_handle);
File *file = dynamic_cast<File *>(handle->node());
if (!file)
return FTRUNCATE_ERR_INTERRUPT;
file->truncate(len);
return FTRUNCATE_OK;
}
void register_read_ready_sigh(Vfs_handle *vfs_handle,
Signal_context_capability sigh)
{ }
/*********************************
** Directory service interface **
*********************************/
Dataspace_capability dataspace(char const *path)
{
Ram_dataspace_capability ds_cap;
char *local_addr = 0;
try {
File *file = lookup_and_lock_file(path);
Node_lock_guard guard(file);
ds_cap = env()->ram_session()->alloc(file->length());
local_addr = env()->rm_session()->attach(ds_cap);
file->read(local_addr, file->length(), 0);
env()->rm_session()->detach(local_addr);
} catch(...) {
env()->rm_session()->detach(local_addr);
env()->ram_session()->free(ds_cap);
return Dataspace_capability();
}
return ds_cap;
}
void release(char const *path, Dataspace_capability ds_cap)
{
env()->ram_session()->free(static_cap_cast<Genode::Ram_dataspace>(ds_cap));
}
Open_result open(char const *path, unsigned mode, Vfs_handle **handle)
{
bool const create = mode & OPEN_MODE_CREATE;
char const *name = basename(++path);
Node *node;
try {
if (create) {
Directory *parent = lookup_and_lock_parent(path);
Node_lock_guard parent_guard(parent);
if (parent->has_sub_node_unsynchronized(name))
return OPEN_ERR_EXISTS;
node = new (env()->heap())
File(*env()->heap(), name);
parent->adopt_unsynchronized(node);
} else {
node = lookup_and_lock(path);
node->unlock();
}
} catch (Lookup_failed) {
return OPEN_ERR_UNACCESSIBLE;
}
*handle = new (env()->heap())
Ram_vfs_handle(*this, mode, node);
return OPEN_OK;
}
Stat_result stat(char const *path, Stat &stat)
{
try {
Node *node = lookup_and_lock(++path);
Node_lock_guard node_guard(node);
memset(&stat, 0x00, sizeof(stat));
stat.inode = node->inode();
File *file = dynamic_cast<File *>(node);
if (file) {
stat.size = file->length();
stat.mode = STAT_MODE_FILE | 0777;
return STAT_OK;
}
Directory *dir = dynamic_cast<Directory *>(node);
if (dir) {
stat.size = dir->num_entries();
stat.mode = STAT_MODE_DIRECTORY | 0777;
return STAT_OK;
}
Symlink *symlink = dynamic_cast<Symlink *>(node);
if (symlink) {
stat.size = symlink->length();
stat.mode = STAT_MODE_SYMLINK | 0777;
return STAT_OK;
}
} catch (Lookup_failed) { }
return STAT_ERR_NO_ENTRY;
}
Dirent_result dirent(char const *path, file_offset index, Dirent &dirent)
{
try {
Directory *dir = lookup_and_lock_dir(++path);
Node_lock_guard guard(dir);
Node *node = dir->entry_unsynchronized(index);
if (!node) {
dirent.fileno = 0;
dirent.type = DIRENT_TYPE_END;
dirent.name[0] = '\0';
return DIRENT_OK;
}
dirent.fileno = index+1;
if (dynamic_cast<File *>(node))
dirent.type = DIRENT_TYPE_FILE;
else if (dynamic_cast<Directory *>(node))
dirent.type = DIRENT_TYPE_DIRECTORY;
else if (dynamic_cast<Symlink *>(node))
dirent.type = DIRENT_TYPE_SYMLINK;
strncpy(dirent.name, node->name(), sizeof(dirent.name));
} catch (Lookup_failed) {
return DIRENT_ERR_INVALID_PATH;
}
return DIRENT_OK;
}
Unlink_result unlink(char const *path)
{
try {
Directory *parent = lookup_and_lock_parent(++path);
Node_lock_guard parent_guard(parent);
char const *name = basename(path);
Node *node = parent->lookup_and_lock(name);
parent->discard_unsynchronized(node);
destroy(env()->heap(), node);
} catch (Lookup_failed) {
return UNLINK_ERR_NO_ENTRY;
}
return UNLINK_OK;
}
Readlink_result readlink(char const *path, char *buf,
file_size buf_size, file_size &out_len)
{
try {
Symlink *symlink = lookup_and_lock_symlink(++path);
Node_lock_guard guard(symlink);
out_len = symlink->read(buf, buf_size, 0);
} catch (Lookup_failed) {
return READLINK_ERR_NO_ENTRY;
}
return READLINK_OK;
}
Rename_result rename(char const *from, char const *to)
{
try {
Directory *from_dir = lookup_and_lock_parent(++from);
Node_lock_guard from_guard(from_dir);
Directory *to_dir = lookup_and_lock_parent(++to);
Node_lock_guard to_guard(to_dir);
Node *node = from_dir->lookup_and_lock(basename(from));
Node_lock_guard guard(node);
from_dir->discard_unsynchronized(node);
node->name(basename(to));
to_dir->adopt_unsynchronized(node);
} catch (Lookup_failed) {
return RENAME_ERR_NO_ENTRY;
}
return RENAME_OK;
}
Mkdir_result mkdir(char const *path, unsigned mode)
{
try {
Directory *parent = lookup_and_lock_parent(++path);
Node_lock_guard parent_guard(parent);
char const *name = basename(path);
if (strlen(name) > MAX_NAME_LEN)
return MKDIR_ERR_NAME_TOO_LONG;
if (parent->has_sub_node_unsynchronized(name))
return MKDIR_ERR_EXISTS;
parent->adopt_unsynchronized(new (env()->heap())
Directory(name));
} catch (Lookup_failed) {
return MKDIR_ERR_NO_ENTRY;
} catch (Allocator::Out_of_memory) {
return MKDIR_ERR_NO_SPACE;
}
return MKDIR_OK;
}
Symlink_result symlink(char const *from, char const *to)
{
try {
Directory *parent = lookup_and_lock_parent(++to);
Node_lock_guard parent_guard(parent);
char const *name = basename(to);
if (strlen(name) > MAX_NAME_LEN)
return SYMLINK_ERR_NAME_TOO_LONG;
if (parent->has_sub_node_unsynchronized(name))
return SYMLINK_ERR_EXISTS;
Symlink * const symlink = new (env()->heap())
Symlink(name);
parent->adopt_unsynchronized(symlink);
symlink->write(from, strlen(from), 0);
} catch (Lookup_failed) {
return SYMLINK_ERR_NO_ENTRY;
} catch (Allocator::Out_of_memory) {
return SYMLINK_ERR_NO_SPACE;
}
return SYMLINK_OK;
}
file_size num_dirent(char const *path)
{
try {
Directory *dir = lookup_and_lock_dir(++path);
Node_lock_guard guard(dir);
return dir->num_entries();
} catch (Lookup_failed) {
return 0;
}
}
bool is_directory(char const *path)
{
try {
lookup_and_lock_dir(++path)->unlock();
return true;
} catch (...) {
return false;
}
}
char const *leaf_path(char const *path)
{
try {
lookup_and_lock(path+1)->unlock();
return path;
} catch (...) { }
return 0;
}
};
#endif /* _INCLUDE__VFS__RAM_FILE_SYSTEM_H_ */

View File

@ -26,6 +26,7 @@
#include <vfs/rom_file_system.h>
#include <vfs/inline_file_system.h>
#include <vfs/rtc_file_system.h>
#include <vfs/ram_file_system.h>
class Default_file_system_factory : public Vfs::Global_file_system_factory
@ -199,6 +200,7 @@ class Default_file_system_factory : public Vfs::Global_file_system_factory
_add_builtin_fs<Vfs::Rom_file_system>();
_add_builtin_fs<Vfs::Inline_file_system>();
_add_builtin_fs<Vfs::Rtc_file_system>();
_add_builtin_fs<Vfs::Ram_file_system>();
}
};

View File

@ -22,7 +22,7 @@
#include <util/xml_node.h>
/* local includes */
#include <directory.h>
#include <ram_fs/directory.h>
/*************************

View File

@ -7,9 +7,7 @@
/* Genode includes */
#include <base/env.h>
#include <base/printf.h>
/* local 'ram_fs' include */
#include <chunk.h>
#include <ram_fs/chunk.h>
namespace File_system {
typedef Chunk<2> Chunk_level_3;