VFS: nonblocking interface

The VFS library can be used in single-threaded or multi-threaded
environments and depending on that, signals are handled by the same thread
which uses the VFS library or possibly by a different thread. If a VFS
plugin needs to block to wait for a signal, there is currently no way
which works reliably in both environments.

For this reason, this commit makes the interface of the VFS library
nonblocking, similar to the File_system session interface.

The most important changes are:

- Directories are created and opened with the 'opendir()' function and the
  directory entries are read with the recently introduced 'queue_read()'
  and 'complete_read()' functions.

- Symbolic links are created and opened with the 'openlink()' function and
  the link target is read with the 'queue_read()' and 'complete_read()'
  functions and written with the 'write()' function.

- The 'write()' function does not wait for signals anymore. This can have
  the effect that data written by a VFS library user has not been
  processed by a file system server yet when the library user asks for the
  size of the file or closes it (both done with RPC functions at the file
  system server). For this reason, a user of the VFS library should
  request synchronization before calling 'stat()' or 'close()'. To make
  sure that a file system server has processed all write request packets
  which a client submitted before the synchronization request,
  synchronization is now requested at the file system server with a
  synchronization packet instead of an RPC function. Because of this
  change, the synchronization interface of the VFS library is now split
  into 'queue_sync()' and 'complete_sync()' functions.

Fixes #2399
This commit is contained in:
Christian Prochaska
2017-08-15 20:51:53 +02:00
committed by Christian Helmuth
parent 8312950e2f
commit b0935ef9b2
49 changed files with 4361 additions and 2307 deletions

View File

@ -16,6 +16,7 @@
#ifndef _INCLUDE__VFS__DIR_FILE_SYSTEM_H_
#define _INCLUDE__VFS__DIR_FILE_SYSTEM_H_
#include <base/registry.h>
#include <vfs/file_system_factory.h>
#include <vfs/vfs_handle.h>
@ -31,6 +32,34 @@ class Vfs::Dir_file_system : public File_system
private:
struct Dir_vfs_handle : Vfs_handle
{
struct Sync_dir_handle_element;
typedef Genode::Registry<Sync_dir_handle_element> Sync_dir_handle_registry;
struct Sync_dir_handle_element : Sync_dir_handle_registry::Element
{
Vfs_handle &vfs_handle;
Sync_dir_handle_element(Sync_dir_handle_registry &registry,
Vfs_handle &vfs_handle)
: Sync_dir_handle_registry::Element(registry, *this),
vfs_handle(vfs_handle) { }
};
Absolute_path path;
File_system *fs_for_complete_read { nullptr };
Vfs_handle *fs_dir_handle { nullptr };
Sync_dir_handle_registry sync_dir_handle_registry;
Dir_vfs_handle(Directory_service &ds,
File_io_service &fs,
Genode::Allocator &alloc,
char const *path)
: Vfs_handle(ds, fs, alloc, 0),
path(path) { }
};
/* pointer to first child file system */
File_system *_first_file_system;
@ -130,6 +159,9 @@ class Vfs::Dir_file_system : public File_system
if (_root())
return path;
if (strcmp(path, "/") == 0)
return path;
/* skip heading slash in path if present */
if (path[0] == '/')
path++;
@ -150,49 +182,6 @@ class Vfs::Dir_file_system : public File_system
return path;
}
/**
* The 'path' is relative to the child file systems.
*/
Dirent_result _dirent_of_file_systems(char const *path, file_offset index, Dirent &out)
{
int base = 0;
for (File_system *fs = _first_file_system; fs; fs = fs->next) {
/*
* Determine number of matching directory entries within
* the current file system.
*/
int const fs_num_dirent = fs->num_dirent(path);
/*
* Query directory entry if index lies with the file
* system.
*/
if (index - base < fs_num_dirent) {
index = index - base;
return fs->dirent(path, index, out);;
}
/* adjust base index for next file system */
base += fs_num_dirent;
}
out.type = DIRENT_TYPE_END;
return DIRENT_OK;
}
void _dirent_of_this_dir_node(file_offset index, Dirent &out)
{
if (index == 0) {
strncpy(out.name, _name, sizeof(out.name));
out.type = DIRENT_TYPE_DIRECTORY;
out.fileno = 1;
} else {
out.type = DIRENT_TYPE_END;
}
}
/*
* Accumulate number of directory entries that match in any of
* our sub file systems.
@ -206,6 +195,100 @@ class Vfs::Dir_file_system : public File_system
return cnt;
}
bool _queue_read_of_file_systems(Dir_vfs_handle *dir_vfs_handle)
{
file_offset index = dir_vfs_handle->seek() / sizeof(Dirent);
char const *sub_path = _sub_path(dir_vfs_handle->path.base());
if (strlen(sub_path) == 0)
sub_path = "/";
int base = 0;
for (File_system *fs = _first_file_system; fs; fs = fs->next) {
/*
* Determine number of matching directory entries within
* the current file system.
*/
int const fs_num_dirent = fs->num_dirent(sub_path);
/*
* Query directory entry if index lies with the file
* system.
*/
if (index - base < fs_num_dirent) {
dir_vfs_handle->fs_for_complete_read = fs;
Opendir_result opendir_result =
fs->opendir(sub_path, false,
&dir_vfs_handle->fs_dir_handle,
dir_vfs_handle->alloc());
/*
* Errors of this kind can only be communicated by
* 'complete_read()'
*/
if (opendir_result != OPENDIR_OK)
return true;
dir_vfs_handle->fs_dir_handle->context =
dir_vfs_handle->context;
index = index - base;
dir_vfs_handle->fs_dir_handle->seek(index * sizeof(Dirent));
bool result = fs->queue_read(dir_vfs_handle->fs_dir_handle,
sizeof(Dirent));
return result;
}
/* adjust base index for next file system */
base += fs_num_dirent;
}
return true;
}
Read_result _complete_read_of_file_systems(Dir_vfs_handle *dir_vfs_handle,
char *dst, file_size count,
file_size &out_count)
{
if (!dir_vfs_handle->fs_for_complete_read ||
!dir_vfs_handle->fs_dir_handle) {
/*
* no fs was found for the given index or
* fs->opendir() failed
*/
if (count < sizeof(Dirent))
return READ_ERR_INVALID;
Dirent *dirent = (Dirent*)dst;
*dirent = Dirent();
out_count = sizeof(Dirent);
return READ_OK;
}
Read_result result = dir_vfs_handle->fs_for_complete_read->
complete_read(dir_vfs_handle->fs_dir_handle,
dst, count, out_count);
if (result != READ_OK)
return result;
dir_vfs_handle->fs_for_complete_read->close(dir_vfs_handle->fs_dir_handle);
dir_vfs_handle->fs_dir_handle = nullptr;
dir_vfs_handle->fs_for_complete_read = nullptr;
return result;
}
public:
Dir_file_system(Genode::Env &env,
@ -330,28 +413,6 @@ class Vfs::Dir_file_system : public File_system
return STAT_ERR_NO_ENTRY;
}
Dirent_result dirent(char const *path, file_offset index, Dirent &out) override
{
if (_root())
return _dirent_of_file_systems(path, index, out);
if (strcmp(path, "/") == 0) {
_dirent_of_this_dir_node(index, out);
return DIRENT_OK;
}
/* path contains at least one element */
/* remove current element from path */
path = _sub_path(path);
/* path does not lie within our tree */
if (!path)
return DIRENT_ERR_INVALID_PATH;
return _dirent_of_file_systems(*path ? path : "/", index, out);
}
file_size num_dirent(char const *path) override
{
if (_root()) {
@ -383,7 +444,11 @@ class Vfs::Dir_file_system : public File_system
*/
bool directory(char const *path) override
{
if (strcmp(path, "/") == 0)
return true;
path = _sub_path(path);
if (!path)
return false;
@ -472,6 +537,65 @@ class Vfs::Dir_file_system : public File_system
return OPEN_ERR_UNACCESSIBLE;
}
Opendir_result opendir(char const *path, bool create,
Vfs_handle **out_handle, Allocator &alloc) override
{
/* path equals "/" (for reading the name of this directory) */
if (strcmp(path, "/") == 0) {
if (create)
return OPENDIR_ERR_PERMISSION_DENIED;
*out_handle = new (alloc) Dir_vfs_handle(*this, *this, alloc,
path);
return OPENDIR_OK;
}
char const *sub_path = _sub_path(path);
if (!sub_path)
return OPENDIR_ERR_LOOKUP_FAILED;
if (create) {
auto opendir_fn = [&] (File_system &fs, char const *path)
{
Vfs_handle *tmp_handle;
Opendir_result opendir_result =
fs.opendir(path, true, &tmp_handle, alloc);
if (opendir_result == OPENDIR_OK)
fs.close(tmp_handle);
return opendir_result;
};
Opendir_result opendir_result =
_dir_op(OPENDIR_ERR_LOOKUP_FAILED,
OPENDIR_ERR_PERMISSION_DENIED,
OPENDIR_OK,
path, opendir_fn);
if (opendir_result != OPENDIR_OK)
return opendir_result;
}
*out_handle = new (alloc) Dir_vfs_handle(*this, *this, alloc,
path);
return OPENDIR_OK;
}
Openlink_result openlink(char const *path, bool create,
Vfs_handle **out_handle,
Allocator &alloc) override
{
auto openlink_fn = [&] (File_system &fs, char const *path)
{
return fs.openlink(path, create, out_handle, alloc);
};
return _dir_op(OPENLINK_ERR_LOOKUP_FAILED,
OPENLINK_ERR_PERMISSION_DENIED,
OPENLINK_OK,
path, openlink_fn);
}
void close(Vfs_handle *handle) override
{
if (handle && (&handle->ds() == this))
@ -489,18 +613,6 @@ class Vfs::Dir_file_system : public File_system
path, unlink_fn);
}
Readlink_result readlink(char const *path, char *buf, file_size buf_size,
file_size &out_len) override
{
auto readlink_fn = [&] (File_system &fs, char const *path)
{
return fs.readlink(path, buf, buf_size, out_len);
};
return _dir_op(READLINK_ERR_NO_ENTRY, READLINK_ERR_NO_ENTRY, READLINK_OK,
path, readlink_fn);
}
Rename_result rename(char const *from_path, char const *to_path) override
{
from_path = _sub_path(from_path);
@ -535,29 +647,6 @@ class Vfs::Dir_file_system : public File_system
return final;
}
Symlink_result symlink(char const *from, char const *to) override
{
auto symlink_fn = [&] (File_system &fs, char const *to)
{
return fs.symlink(from, to);
};
return _dir_op(SYMLINK_ERR_NO_ENTRY, SYMLINK_ERR_NO_PERM, SYMLINK_OK,
to, symlink_fn);
}
Mkdir_result mkdir(char const *path, unsigned mode) override
{
auto mkdir_fn = [&] (File_system &fs, char const *path)
{
return fs.mkdir(path, mode);
};
return _dir_op(MKDIR_ERR_NO_ENTRY, MKDIR_ERR_NO_PERM, MKDIR_OK,
path, mkdir_fn);
}
/***************************
** File_system interface **
***************************/
@ -565,25 +654,6 @@ class Vfs::Dir_file_system : public File_system
char const *name() const { return "dir"; }
char const *type() override { return "dir"; }
/**
* Synchronize all file systems
*/
void sync(char const *path) override
{
if (strcmp("/", path, 2) == 0) {
for (File_system *fs = _first_file_system; fs; fs = fs->next)
fs->sync("/");
return;
}
path = _sub_path(path);
if (!path)
return;
for (File_system *fs = _first_file_system; fs; fs = fs->next)
fs->sync(path);
}
void apply_config(Genode::Xml_node const &node) override
{
using namespace Genode;
@ -614,11 +684,56 @@ class Vfs::Dir_file_system : public File_system
return WRITE_ERR_INVALID;
}
Read_result read(Vfs_handle *, char *, file_size, file_size &) override
bool queue_read(Vfs_handle *vfs_handle, file_size count) override
{
return READ_ERR_INVALID;
Dir_vfs_handle *dir_vfs_handle =
static_cast<Dir_vfs_handle*>(vfs_handle);
if (_root())
return _queue_read_of_file_systems(dir_vfs_handle);
if (strcmp(dir_vfs_handle->path.base(), "/") == 0)
return true;
return _queue_read_of_file_systems(dir_vfs_handle);
}
Read_result complete_read(Vfs_handle *vfs_handle,
char *dst, file_size count,
file_size &out_count) override
{
out_count = 0;
if (count < sizeof(Dirent))
return READ_ERR_INVALID;
Dir_vfs_handle *dir_vfs_handle =
static_cast<Dir_vfs_handle*>(vfs_handle);
if (_root())
return _complete_read_of_file_systems(dir_vfs_handle, dst, count, out_count);
if (strcmp(dir_vfs_handle->path.base(), "/") == 0) {
Dirent *dirent = (Dirent*)dst;
file_offset index = vfs_handle->seek() / sizeof(Dirent);
if (index == 0) {
strncpy(dirent->name, _name, sizeof(dirent->name));
dirent->type = DIRENT_TYPE_DIRECTORY;
dirent->fileno = 1;
} else {
dirent->type = DIRENT_TYPE_END;
}
out_count = sizeof(Dirent);
return READ_OK;
}
return _complete_read_of_file_systems(dir_vfs_handle, dst, count, out_count);
}
Ftruncate_result ftruncate(Vfs_handle *, file_size) override
{
return FTRUNCATE_ERR_NO_PERM;
@ -639,6 +754,84 @@ class Vfs::Dir_file_system : public File_system
return handle->fs().notify_read_ready(handle);
}
bool queue_sync(Vfs_handle *vfs_handle) override
{
Dir_vfs_handle *dir_vfs_handle =
static_cast<Dir_vfs_handle*>(vfs_handle);
char const *sub_path = _sub_path(dir_vfs_handle->path.base());
if (strlen(sub_path) == 0)
sub_path = "/";
/*
* Call 'opendir()' on each file system and, if successful,
* 'queue_sync()'. If one file system's 'queue_sync()' returns
* false, this function returns false. Any VFS handles returned by
* 'opendir()' are kept in a registry.
*/
for (File_system *fs = _first_file_system; fs; fs = fs->next) {
bool fs_dir_already_open = false;
dir_vfs_handle->sync_dir_handle_registry.for_each(
[&] (Dir_vfs_handle::Sync_dir_handle_element &sync_dir_handle_element) {
if (&sync_dir_handle_element.vfs_handle.fs() == fs) {
fs_dir_already_open = true;
return;
}
}
);
if (fs_dir_already_open)
continue;
Vfs_handle *sync_dir_handle;
if (fs->opendir(sub_path, false, &sync_dir_handle,
dir_vfs_handle->alloc()) != OPENDIR_OK)
continue;
sync_dir_handle->context = dir_vfs_handle->context;
new (dir_vfs_handle->alloc())
Dir_vfs_handle::Sync_dir_handle_element(
dir_vfs_handle->sync_dir_handle_registry,
*sync_dir_handle);
if (!sync_dir_handle->fs().queue_sync(sync_dir_handle))
return false;
}
return true;
}
Sync_result complete_sync(Vfs_handle *vfs_handle) override
{
Sync_result result = SYNC_OK;
Dir_vfs_handle *dir_vfs_handle =
static_cast<Dir_vfs_handle*>(vfs_handle);
dir_vfs_handle->sync_dir_handle_registry.for_each(
[&] (Dir_vfs_handle::Sync_dir_handle_element &sync_dir_handle_element) {
Vfs_handle *vfs_handle = &sync_dir_handle_element.vfs_handle;
result =
vfs_handle->fs().complete_sync(vfs_handle);
if (result != SYNC_OK)
return;
vfs_handle->ds().close(vfs_handle);
destroy(vfs_handle->alloc(), &sync_dir_handle_element);
}
);
return result;
}
};
#endif /* _INCLUDE__VFS__DIR_FILE_SYSTEM_H_ */