2014-04-07 16:15:40 +02:00
|
|
|
/*
|
|
|
|
* \brief Block device file system
|
|
|
|
* \author Josef Soentgen
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2013-12-20
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2013-2017 Genode Labs GmbH
|
2014-04-07 16:15:40 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2014-04-07 16:15:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__VFS__BLOCK_FILE_SYSTEM_H_
|
|
|
|
#define _INCLUDE__VFS__BLOCK_FILE_SYSTEM_H_
|
|
|
|
|
|
|
|
#include <base/allocator_avl.h>
|
|
|
|
#include <block_session/connection.h>
|
|
|
|
#include <vfs/single_file_system.h>
|
|
|
|
|
|
|
|
namespace Vfs { class Block_file_system; }
|
|
|
|
|
|
|
|
|
|
|
|
class Vfs::Block_file_system : public Single_file_system
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2018-04-03 15:59:35 +02:00
|
|
|
Vfs::Env &_env;
|
2016-05-25 15:47:22 +02:00
|
|
|
|
|
|
|
typedef Genode::String<64> Label;
|
|
|
|
Label _label;
|
2014-04-07 16:15:40 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Serialize access to packet stream of the block session
|
|
|
|
*/
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
Lock _lock { };
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2019-04-03 16:05:10 +02:00
|
|
|
char *_block_buffer;
|
|
|
|
unsigned _block_buffer_count;
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2019-04-03 16:05:10 +02:00
|
|
|
Genode::Allocator_avl _tx_block_alloc { &_env.alloc() };
|
|
|
|
|
|
|
|
Block::Connection _block {
|
2018-04-03 15:59:35 +02:00
|
|
|
_env.env(), &_tx_block_alloc, 128*1024, _label.string() };
|
2019-04-03 16:05:10 +02:00
|
|
|
|
|
|
|
Block::Session::Info const _info { _block.info() };
|
|
|
|
|
2014-04-07 16:15:40 +02:00
|
|
|
Block::Session::Tx::Source *_tx_source;
|
|
|
|
|
2019-04-03 16:05:10 +02:00
|
|
|
bool _writeable;
|
2014-04-07 16:15:40 +02:00
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
Genode::Signal_receiver _signal_receiver { };
|
|
|
|
Genode::Signal_context _signal_context { };
|
2016-10-27 19:44:33 +02:00
|
|
|
Genode::Signal_context_capability _source_submit_cap;
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
/*
|
|
|
|
* Noncopyable
|
|
|
|
*/
|
|
|
|
Block_file_system(Block_file_system const &);
|
|
|
|
Block_file_system &operator = (Block_file_system const &);
|
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
class Block_vfs_handle : public Single_vfs_handle
|
2014-04-07 16:15:40 +02:00
|
|
|
{
|
2017-08-15 20:51:53 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
Genode::Allocator &_alloc;
|
|
|
|
Label &_label;
|
|
|
|
Lock &_lock;
|
|
|
|
char *_block_buffer;
|
|
|
|
unsigned &_block_buffer_count;
|
|
|
|
Genode::Allocator_avl &_tx_block_alloc;
|
|
|
|
Block::Connection &_block;
|
2019-04-03 16:05:10 +02:00
|
|
|
Genode::size_t const _block_size;
|
|
|
|
Block::sector_t const _block_count;
|
2017-08-15 20:51:53 +02:00
|
|
|
Block::Session::Tx::Source *_tx_source;
|
2019-04-03 16:05:10 +02:00
|
|
|
bool const _writeable;
|
2017-08-15 20:51:53 +02:00
|
|
|
Genode::Signal_receiver &_signal_receiver;
|
|
|
|
Genode::Signal_context &_signal_context;
|
|
|
|
Genode::Signal_context_capability &_source_submit_cap;
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
/*
|
|
|
|
* Noncopyable
|
|
|
|
*/
|
|
|
|
Block_vfs_handle(Block_vfs_handle const &);
|
|
|
|
Block_vfs_handle &operator = (Block_vfs_handle const &);
|
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
file_size _block_io(file_size nr, void *buf, file_size sz,
|
|
|
|
bool write, bool bulk = false)
|
|
|
|
{
|
|
|
|
Block::Packet_descriptor::Opcode op;
|
|
|
|
op = write ? Block::Packet_descriptor::WRITE : Block::Packet_descriptor::READ;
|
|
|
|
|
|
|
|
file_size packet_size = bulk ? sz : _block_size;
|
|
|
|
file_size packet_count = bulk ? (sz / _block_size) : 1;
|
|
|
|
|
|
|
|
Block::Packet_descriptor packet;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (packet_count > _block_buffer_count) {
|
|
|
|
packet_size = _block_buffer_count * _block_size;
|
|
|
|
packet_count = _block_buffer_count;
|
|
|
|
}
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
|
|
|
|
packet = _tx_source->alloc_packet(packet_size);
|
|
|
|
break;
|
|
|
|
} catch (Block::Session::Tx::Source::Packet_alloc_failed) {
|
|
|
|
if (!_tx_source->ready_to_submit())
|
|
|
|
_signal_receiver.wait_for_signal();
|
|
|
|
else {
|
|
|
|
if (packet_count > 1) {
|
|
|
|
packet_size /= 2;
|
|
|
|
packet_count /= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Lock::Guard guard(_lock);
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
Block::Packet_descriptor p(packet, op, nr, packet_count);
|
2016-10-27 19:44:33 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
if (write)
|
|
|
|
Genode::memcpy(_tx_source->packet_content(p), buf, packet_size);
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
_tx_source->submit_packet(p);
|
|
|
|
p = _tx_source->get_acked_packet();
|
|
|
|
|
|
|
|
if (!p.succeeded()) {
|
|
|
|
Genode::error("Could not read block(s)");
|
|
|
|
_tx_source->release_packet(p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!write)
|
|
|
|
Genode::memcpy(buf, _tx_source->packet_content(p), packet_size);
|
2016-10-27 19:44:33 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
_tx_source->release_packet(p);
|
|
|
|
return packet_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Block_vfs_handle(Directory_service &ds,
|
|
|
|
File_io_service &fs,
|
|
|
|
Genode::Allocator &alloc,
|
|
|
|
Label &label,
|
|
|
|
Lock &lock,
|
|
|
|
char *block_buffer,
|
|
|
|
unsigned &block_buffer_count,
|
|
|
|
Genode::Allocator_avl &tx_block_alloc,
|
|
|
|
Block::Connection &block,
|
2019-04-03 16:05:10 +02:00
|
|
|
Genode::size_t block_size,
|
|
|
|
Block::sector_t block_count,
|
2017-08-15 20:51:53 +02:00
|
|
|
Block::Session::Tx::Source *tx_source,
|
2019-04-03 16:05:10 +02:00
|
|
|
bool writeable,
|
2017-08-15 20:51:53 +02:00
|
|
|
Genode::Signal_receiver &signal_receiver,
|
|
|
|
Genode::Signal_context &signal_context,
|
|
|
|
Genode::Signal_context_capability &source_submit_cap)
|
|
|
|
: Single_vfs_handle(ds, fs, alloc, 0),
|
|
|
|
_alloc(alloc),
|
|
|
|
_label(label),
|
|
|
|
_lock(lock),
|
|
|
|
_block_buffer(block_buffer),
|
|
|
|
_block_buffer_count(block_buffer_count),
|
|
|
|
_tx_block_alloc(tx_block_alloc),
|
|
|
|
_block(block),
|
|
|
|
_block_size(block_size),
|
|
|
|
_block_count(block_count),
|
|
|
|
_tx_source(tx_source),
|
|
|
|
_writeable(writeable),
|
|
|
|
_signal_receiver(signal_receiver),
|
|
|
|
_signal_context(signal_context),
|
|
|
|
_source_submit_cap(source_submit_cap)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Read_result read(char *dst, file_size count,
|
|
|
|
file_size &out_count) override
|
|
|
|
{
|
|
|
|
file_size seek_offset = seek();
|
|
|
|
|
|
|
|
file_size read = 0;
|
|
|
|
while (count > 0) {
|
|
|
|
file_size displ = 0;
|
|
|
|
file_size length = 0;
|
|
|
|
file_size nbytes = 0;
|
|
|
|
file_size blk_nr = seek_offset / _block_size;
|
|
|
|
|
|
|
|
displ = seek_offset % _block_size;
|
|
|
|
|
|
|
|
if ((displ + count) > _block_size)
|
|
|
|
length = (_block_size - displ);
|
|
|
|
else
|
|
|
|
length = count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We take a shortcut and read the blocks all at once if the
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
* offset is aligned on a block boundary and the count is a
|
2017-08-15 20:51:53 +02:00
|
|
|
* multiple of the block size, e.g. 4K reads will be read at
|
|
|
|
* once.
|
|
|
|
*
|
|
|
|
* XXX this is quite hackish because we have to omit partial
|
|
|
|
* blocks at the end.
|
|
|
|
*/
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
if (displ == 0 && !(count < _block_size)) {
|
2017-08-15 20:51:53 +02:00
|
|
|
file_size bytes_left = count - (count % _block_size);
|
|
|
|
|
|
|
|
nbytes = _block_io(blk_nr, dst + read, bytes_left, false, true);
|
|
|
|
if (nbytes == 0) {
|
|
|
|
Genode::error("error while reading block:", blk_nr, " from block device");
|
|
|
|
return READ_ERR_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
read += nbytes;
|
|
|
|
count -= nbytes;
|
|
|
|
seek_offset += nbytes;
|
|
|
|
|
|
|
|
continue;
|
2016-10-27 19:44:33 +02:00
|
|
|
}
|
2017-08-15 20:51:53 +02:00
|
|
|
|
|
|
|
nbytes = _block_io(blk_nr, _block_buffer, _block_size, false);
|
|
|
|
if ((unsigned)nbytes != _block_size) {
|
|
|
|
Genode::error("error while reading block:", blk_nr, " from block device");
|
|
|
|
return READ_ERR_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
Genode::memcpy(dst + read, _block_buffer + displ, length);
|
|
|
|
|
|
|
|
read += length;
|
|
|
|
count -= length;
|
|
|
|
seek_offset += length;
|
2016-10-27 19:44:33 +02:00
|
|
|
}
|
2017-08-15 20:51:53 +02:00
|
|
|
|
|
|
|
out_count = read;
|
|
|
|
|
|
|
|
return READ_OK;
|
|
|
|
|
2016-10-27 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
Write_result write(char const *buf, file_size count,
|
|
|
|
file_size &out_count) override
|
|
|
|
{
|
|
|
|
if (!_writeable) {
|
|
|
|
Genode::error("block device is not writeable");
|
|
|
|
return WRITE_ERR_INVALID;
|
|
|
|
}
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
file_size seek_offset = seek();
|
|
|
|
|
|
|
|
file_size written = 0;
|
|
|
|
while (count > 0) {
|
|
|
|
file_size displ = 0;
|
|
|
|
file_size length = 0;
|
|
|
|
file_size nbytes = 0;
|
|
|
|
file_size blk_nr = seek_offset / _block_size;
|
|
|
|
|
|
|
|
displ = seek_offset % _block_size;
|
|
|
|
|
|
|
|
if ((displ + count) > _block_size)
|
|
|
|
length = (_block_size - displ);
|
|
|
|
else
|
|
|
|
length = count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We take a shortcut and write as much as possible without
|
|
|
|
* using the block buffer if the offset is aligned on a block
|
|
|
|
* boundary and the count is a multiple of the block size,
|
|
|
|
* e.g. 4K writes will be written at once.
|
|
|
|
*
|
|
|
|
* XXX this is quite hackish because we have to omit partial
|
|
|
|
* blocks at the end.
|
|
|
|
*/
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
if (displ == 0 && !(count < _block_size)) {
|
2017-08-15 20:51:53 +02:00
|
|
|
file_size bytes_left = count - (count % _block_size);
|
|
|
|
|
|
|
|
nbytes = _block_io(blk_nr, (void*)(buf + written),
|
|
|
|
bytes_left, true, true);
|
|
|
|
if (nbytes == 0) {
|
|
|
|
Genode::error("error while write block:", blk_nr, " to block device");
|
|
|
|
return WRITE_ERR_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
written += nbytes;
|
|
|
|
count -= nbytes;
|
|
|
|
seek_offset += nbytes;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
/*
|
|
|
|
* The offset is not aligned on a block boundary. Therefore
|
|
|
|
* we need to read the block to the block buffer first and
|
|
|
|
* put the buffer content at the right offset before we can
|
|
|
|
* write the whole block back. In addition if length is less
|
|
|
|
* than block size, we also have to read the block first.
|
|
|
|
*/
|
|
|
|
if (displ > 0 || length < _block_size)
|
|
|
|
_block_io(blk_nr, _block_buffer, _block_size, false);
|
|
|
|
|
|
|
|
Genode::memcpy(_block_buffer + displ, buf + written, length);
|
|
|
|
|
|
|
|
nbytes = _block_io(blk_nr, _block_buffer, _block_size, true);
|
|
|
|
if ((unsigned)nbytes != _block_size) {
|
|
|
|
Genode::error("error while writing block:", blk_nr, " to block_device");
|
|
|
|
return WRITE_ERR_INVALID;
|
|
|
|
}
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
written += length;
|
|
|
|
count -= length;
|
|
|
|
seek_offset += length;
|
|
|
|
}
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
out_count = written;
|
2014-04-07 16:15:40 +02:00
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
return WRITE_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:39:08 +01:00
|
|
|
bool read_ready() override { return true; }
|
2017-08-15 20:51:53 +02:00
|
|
|
};
|
2014-04-07 16:15:40 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2018-04-03 15:59:35 +02:00
|
|
|
Block_file_system(Vfs::Env &env, Genode::Xml_node config)
|
2014-04-07 16:15:40 +02:00
|
|
|
:
|
|
|
|
Single_file_system(NODE_TYPE_BLOCK_DEVICE, name(), config),
|
2018-04-03 15:59:35 +02:00
|
|
|
_env(env),
|
2016-05-25 15:47:22 +02:00
|
|
|
_label(config.attribute_value("label", Label())),
|
2014-04-07 16:15:40 +02:00
|
|
|
_block_buffer(0),
|
2019-01-21 10:48:39 +01:00
|
|
|
_block_buffer_count(config.attribute_value("block_buffer_count", 1UL)),
|
2014-04-07 16:15:40 +02:00
|
|
|
_tx_source(_block.tx()),
|
2019-04-03 16:05:10 +02:00
|
|
|
_writeable(_info.writeable),
|
2016-10-27 19:44:33 +02:00
|
|
|
_source_submit_cap(_signal_receiver.manage(&_signal_context))
|
2014-04-07 16:15:40 +02:00
|
|
|
{
|
2019-04-03 16:05:10 +02:00
|
|
|
_block_buffer = new (_env.alloc())
|
|
|
|
char[_block_buffer_count * _info.block_size];
|
2016-10-27 19:44:33 +02:00
|
|
|
|
|
|
|
_block.tx_channel()->sigh_ready_to_submit(_source_submit_cap);
|
2014-04-07 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~Block_file_system()
|
|
|
|
{
|
2016-10-27 19:44:33 +02:00
|
|
|
_signal_receiver.dissolve(&_signal_context);
|
|
|
|
|
2018-04-03 15:59:35 +02:00
|
|
|
destroy(_env.alloc(), _block_buffer);
|
2014-04-07 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 19:03:23 +01:00
|
|
|
static char const *name() { return "block"; }
|
|
|
|
char const *type() override { return "block"; }
|
2014-04-07 16:15:40 +02:00
|
|
|
|
|
|
|
/*********************************
|
|
|
|
** Directory service interface **
|
|
|
|
*********************************/
|
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
Open_result open(char const *path, unsigned,
|
|
|
|
Vfs_handle **out_handle,
|
|
|
|
Allocator &alloc) override
|
|
|
|
{
|
|
|
|
if (!_single_file(path))
|
|
|
|
return OPEN_ERR_UNACCESSIBLE;
|
|
|
|
|
2018-01-12 20:47:48 +01:00
|
|
|
try {
|
|
|
|
*out_handle = new (alloc) Block_vfs_handle(*this, *this, alloc,
|
|
|
|
_label, _lock,
|
|
|
|
_block_buffer,
|
|
|
|
_block_buffer_count,
|
|
|
|
_tx_block_alloc,
|
|
|
|
_block,
|
2019-04-03 16:05:10 +02:00
|
|
|
_info.block_size,
|
|
|
|
_info.block_count,
|
2018-01-12 20:47:48 +01:00
|
|
|
_tx_source,
|
2019-04-03 16:05:10 +02:00
|
|
|
_info.writeable,
|
2018-01-12 20:47:48 +01:00
|
|
|
_signal_receiver,
|
|
|
|
_signal_context,
|
|
|
|
_source_submit_cap);
|
|
|
|
return OPEN_OK;
|
|
|
|
}
|
|
|
|
catch (Genode::Out_of_ram) { return OPEN_ERR_OUT_OF_RAM; }
|
|
|
|
catch (Genode::Out_of_caps) { return OPEN_ERR_OUT_OF_CAPS; }
|
2017-08-15 20:51:53 +02:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:15:40 +02:00
|
|
|
Stat_result stat(char const *path, Stat &out) override
|
|
|
|
{
|
|
|
|
Stat_result const result = Single_file_system::stat(path, out);
|
2019-04-03 16:05:10 +02:00
|
|
|
out.size = _info.block_count * _info.block_size;
|
2014-04-07 16:15:40 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
** File I/O service interface **
|
|
|
|
********************************/
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
Ftruncate_result ftruncate(Vfs_handle *, file_size) override
|
2014-04-07 16:15:40 +02:00
|
|
|
{
|
|
|
|
return FTRUNCATE_OK;
|
|
|
|
}
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
Ioctl_result ioctl(Vfs_handle *, Ioctl_opcode opcode, Ioctl_arg,
|
2014-04-07 16:15:40 +02:00
|
|
|
Ioctl_out &out) override
|
|
|
|
{
|
|
|
|
switch (opcode) {
|
|
|
|
case IOCTL_OP_DIOCGMEDIASIZE:
|
|
|
|
|
2019-04-03 16:05:10 +02:00
|
|
|
out.diocgmediasize.size = _info.block_count * _info.block_size;
|
2014-04-07 16:15:40 +02:00
|
|
|
return IOCTL_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::warning("invalid ioctl request ", (int)opcode);
|
2014-04-07 16:15:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* never reached */
|
|
|
|
return IOCTL_ERR_INVALID;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__VFS__BLOCK_FILE_SYSTEM_H_ */
|