Skip to content

Commit

Permalink
WIP: libc: make Sync object accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
atopia committed Aug 12, 2024
1 parent a3d45c2 commit 6337d4a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 36 deletions.
41 changes: 41 additions & 0 deletions repos/libports/src/lib/libc/internal/sync.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* \brief File synchronization interface
* \author Benjamin Lamowski
* \date 2024-07-16
*/

/*
* Copyright (C) 2024 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/

#ifndef _LIBC__INTERNAL__SYNC_H_
#define _LIBC__INTERNAL__SYNC_H_

/* libc-internal includes */
#include <internal/init.h>
#include <internal/plugin.h>
#include <internal/vfs_plugin.h>

namespace Libc {

This comment has been minimized.

Copy link
@chelmuth

chelmuth Sep 2, 2024

coding style: see kqueue.h for inspiration how to prevent needless indentation

class Sync
{
private:

This comment has been minimized.

Copy link
@chelmuth

chelmuth Sep 2, 2024

coding style: private: and friends are followed by an empty line

enum { INITIAL, TIMESTAMP_UPDATED, QUEUED, COMPLETE } _state { INITIAL };

Vfs::Vfs_handle &_vfs_handle;
Vfs::Timestamp _mtime { Vfs::Timestamp::INVALID };

public:
Sync(Vfs::Vfs_handle &vfs_handle, Libc::Vfs_plugin::Update_mtime update_mtime,
Libc::Current_real_time &current_real_time);

Sync(Vfs::Vfs_handle &vfs_handle, Plugin & plugin);

bool complete();
};
}

#endif /* _LIBC__INTERNAL__SYNC_H_ */
5 changes: 5 additions & 0 deletions repos/libports/src/lib/libc/internal/vfs_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ namespace Libc { class Vfs_plugin; }

class Libc::Vfs_plugin final : public Plugin
{
/*
* Needed to construct a sync object from a Vfs_plugin / Plugin reference.
*/
friend class Sync;

This comment has been minimized.

Copy link
@chelmuth

chelmuth Sep 2, 2024

Please consider moving class Sync here (as a sub-class of Vfs_plugin) that is permitted to access private members.

This comment has been minimized.

Copy link
@atopia

atopia Sep 4, 2024

Author Owner

The reason for moving Sync to a separate header was that we need a Sync constructible inside of Aio_plugin. To me, constructing a Sub-class of the Vfs_plugin inside of Aio_plugin seems worse than having the friend.

This comment has been minimized.

Copy link
@chelmuth

chelmuth Sep 5, 2024

Sorry, but I see this with different eyes: Vfs_plugin::Sync would represent an eligible public interface to the VFS sync mechanism in form of a C++ object.

This comment has been minimized.

Copy link
@atopia

atopia Sep 5, 2024

Author Owner

I agree that's another way to see it. I've pushed fixup commits to both branches.


public:

enum class Update_mtime { NO, YES };
Expand Down
85 changes: 49 additions & 36 deletions repos/libports/src/lib/libc/vfs_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <internal/mem_alloc.h>
#include <internal/errno.h>
#include <internal/init.h>
#include <internal/sync.h>
#include <internal/monitor.h>
#include <internal/current_time.h>

Expand Down Expand Up @@ -536,51 +537,63 @@ Libc::File_descriptor *Libc::Vfs_plugin::open(char const *path, int flags)
}


struct Sync
Libc::Sync::Sync(Vfs::Vfs_handle &vfs_handle, Libc::Vfs_plugin::Update_mtime update_mtime,
Libc::Current_real_time &current_real_time)
:
_vfs_handle(vfs_handle)
{
enum { INITIAL, TIMESTAMP_UPDATED, QUEUED, COMPLETE } state { INITIAL };
if (update_mtime == Libc::Vfs_plugin::Update_mtime::NO
|| !current_real_time.has_real_time()) {

Vfs::Vfs_handle &vfs_handle;
Vfs::Timestamp mtime { Vfs::Timestamp::INVALID };
_state = TIMESTAMP_UPDATED;

Sync(Vfs::Vfs_handle &vfs_handle, Libc::Vfs_plugin::Update_mtime update_mtime,
Libc::Current_real_time &current_real_time)
:
vfs_handle(vfs_handle)
{
if (update_mtime == Libc::Vfs_plugin::Update_mtime::NO
|| !current_real_time.has_real_time()) {
} else {
timespec const ts = current_real_time.current_real_time();

state = TIMESTAMP_UPDATED;
_mtime = { .value = (long long)ts.tv_sec };
}
}

} else {
timespec const ts = current_real_time.current_real_time();

mtime = { .value = (long long)ts.tv_sec };
}

Libc::Sync::Sync(Vfs::Vfs_handle &vfs_handle, Plugin & plugin)
:
_vfs_handle(vfs_handle)
{
Vfs_plugin & vfs_plugin = reinterpret_cast<Vfs_plugin &>(plugin);
if (vfs_plugin._update_mtime == Libc::Vfs_plugin::Update_mtime::NO
|| !vfs_plugin._current_real_time.has_real_time()) {

_state = TIMESTAMP_UPDATED;

} else {
timespec const ts = vfs_plugin._current_real_time.current_real_time();

_mtime = { .value = (long long)ts.tv_sec };
}
}

bool complete()
{
switch (state) {
case Sync::INITIAL:
if (!vfs_handle.fs().update_modification_timestamp(&vfs_handle, mtime))
return false;
state = Sync::TIMESTAMP_UPDATED; [[ fallthrough ]];
case Sync::TIMESTAMP_UPDATED:
if (!vfs_handle.fs().queue_sync(&vfs_handle))
return false;
state = Sync::QUEUED; [[ fallthrough ]];
case Sync::QUEUED:
if (vfs_handle.fs().complete_sync(&vfs_handle) == Vfs::File_io_service::SYNC_QUEUED)
return false;
state = Sync::COMPLETE; [[ fallthrough ]];
case Sync::COMPLETE:
break;
}
return true;

bool Libc::Sync::complete()
{
switch (_state) {
case Sync::INITIAL:
if (!_vfs_handle.fs().update_modification_timestamp(&_vfs_handle, _mtime))
return false;
_state = Sync::TIMESTAMP_UPDATED; [[ fallthrough ]];
case Sync::TIMESTAMP_UPDATED:
if (!_vfs_handle.fs().queue_sync(&_vfs_handle))
return false;
_state = Sync::QUEUED; [[ fallthrough ]];
case Sync::QUEUED:
if (_vfs_handle.fs().complete_sync(&_vfs_handle) == Vfs::File_io_service::SYNC_QUEUED)
return false;
_state = Sync::COMPLETE; [[ fallthrough ]];
case Sync::COMPLETE:
break;
}
};
return true;
}


int Libc::Vfs_plugin::close_from_kernel(File_descriptor *fd)
Expand Down

0 comments on commit 6337d4a

Please sign in to comment.