Skip to content

Commit

Permalink
[fs][minfs] Replace the vfs-dispatcher with libasync
Browse files Browse the repository at this point in the history
Change-Id: Icfc3566510ae0e5f29ac2f0615681c00c831ece9
  • Loading branch information
Sean Klein authored and CQ bot account: [email protected] committed Sep 8, 2017
1 parent b8f10ee commit a12054f
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 16 deletions.
14 changes: 6 additions & 8 deletions system/uapp/minfs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#include <fbl/unique_ptr.h>

#ifdef __Fuchsia__
#include "fs/mxio-dispatcher.h"
#include <async/loop.h>
#include <fs/async-dispatcher.h>
#endif

#include "minfs-private.h"
Expand Down Expand Up @@ -58,18 +59,15 @@ int do_minfs_mount(fbl::unique_ptr<minfs::Bcache> bc, int argc, char** argv) {
return MX_ERR_BAD_STATE;
}

static const uint32_t kPoolSize = 4;
fbl::unique_ptr<fs::VfsDispatcher> dispatcher;
async::Loop loop;
fs::AsyncDispatcher dispatcher(loop.async());
minfs::vfs.SetDispatcher(&dispatcher);
mx_status_t status;
if ((status = fs::VfsDispatcher::Create(mxrio_handler, kPoolSize, &dispatcher)) != MX_OK) {
return status;
}
minfs::vfs.SetDispatcher(dispatcher.get());
if ((status = minfs::vfs.ServeDirectory(fbl::move(vn),
mx::channel(h))) != MX_OK) {
return status;
}
dispatcher->RunOnCurrentThread(); // Blocks
loop.Run();
return 0;
}
#else
Expand Down
4 changes: 0 additions & 4 deletions system/uapp/minfs/minfs-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
#include <fs/block-txn.h>
#include <fs/mapped-vmo.h>

#ifdef __Fuchsia__
#include <fs/vfs-dispatcher.h>
#endif

#include <fs/vfs.h>

#include "minfs.h"
Expand Down
3 changes: 0 additions & 3 deletions system/uapp/minfs/minfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
#include <fbl/alloc_checker.h>
#include <fbl/limits.h>
#include <fbl/unique_ptr.h>
#ifdef __Fuchsia__
#include <fs/vfs-dispatcher.h>
#endif

#include "minfs-private.h"

Expand Down
5 changes: 4 additions & 1 deletion system/uapp/minfs/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ MODULE_SRCS += \
$(LOCAL_DIR)/minfs-check.cpp \

MODULE_STATIC_LIBS := \
system/ulib/block-client \
system/ulib/fs \
system/ulib/async \
system/ulib/async.loop \
system/ulib/block-client \
system/ulib/mx \
system/ulib/mxcpp \
system/ulib/fbl \
system/ulib/sync \

MODULE_LIBS := \
system/ulib/async.default \
system/ulib/bitmap \
system/ulib/magenta \
system/ulib/mxio \
Expand Down
5 changes: 5 additions & 0 deletions system/ulib/fs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ config("fs_config") {
static_library("fs") {
# Don't forget to update rules.mk as well for the Magenta build.
sources = [
"include/fs/async-dispatcher.h",
"include/fs/block-txn.h",
"include/fs/dispatcher.h",
"include/fs/mapped-vmo.h",
Expand All @@ -17,6 +18,7 @@ static_library("fs") {
"include/fs/vfs-client.h",
"include/fs/vfs-dispatcher.h",
"include/fs/vfs.h",
"async-dispatcher.cpp",
"mapped-vmo.cpp",
"mxio-dispatcher.cpp",
"vfs-dispatcher.cpp",
Expand All @@ -37,6 +39,9 @@ static_library("fs") {
include_dirs = [ "//magenta/system/private" ]

deps = [
"//magenta/system/ulib/async",
"//magenta/system/ulib/async:default",
"//magenta/system/ulib/async:loop",
"//magenta/system/ulib/mx",
"//magenta/system/ulib/mxcpp",
"//magenta/system/ulib/fbl",
Expand Down
67 changes: 67 additions & 0 deletions system/ulib/fs/async-dispatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>
#include <stdint.h>

#include <async/dispatcher.h>
#include <async/wait.h>
#include <fs/async-dispatcher.h>
#include <magenta/types.h>

namespace fs {

AsyncHandler::AsyncHandler(mx::channel channel, vfs_dispatcher_cb_t cb, void* cookie) :
channel_(fbl::move(channel)), cb_(cb), cookie_(cookie),
wait_(channel_.get(), MX_CHANNEL_READABLE | MX_CHANNEL_PEER_CLOSED,
ASYNC_FLAG_HANDLE_SHUTDOWN) {
wait_.set_handler(fbl::BindMember(this, &AsyncHandler::Handle));
}

AsyncHandler::~AsyncHandler() = default;

async_wait_result_t AsyncHandler::Handle(async_t* async, mx_status_t status,
const mx_packet_signal_t* signal) {
if (status == MX_OK && (signal->observed & MX_CHANNEL_READABLE)) {
status = mxrio_handler(channel_.get(), (void*) cb_, cookie_);
if (status != MX_OK) {
// Disconnect the handler in the case of
// (1) Explicit Close from the client, or
// (2) IPC-related error
return HandlerClose(status != ERR_DISPATCHER_DONE);
}
return ASYNC_WAIT_AGAIN;
} else {
// Either the dispatcher failed to wait for signals, or
// we received |MX_CHANNEL_PEER_CLOSED|. Either way, terminate
// the handler.
return HandlerClose(true);
}
}

async_wait_result_t AsyncHandler::HandlerClose(bool need_close_cb) {
if (need_close_cb) {
// We're closing the handle here; we don't care about the result.
mxrio_handler(MX_HANDLE_INVALID, (void*) cb_, cookie_);
}
delete this;
return ASYNC_WAIT_FINISHED;
}

AsyncDispatcher::AsyncDispatcher(async_t* async) : async_(async) {}

AsyncDispatcher::~AsyncDispatcher() = default;

mx_status_t AsyncDispatcher::AddVFSHandler(mx::channel channel,
vfs_dispatcher_cb_t cb,
void* iostate) {
AsyncHandler* handler = new AsyncHandler(fbl::move(channel), cb, iostate);
mx_status_t status;
if ((status = handler->Begin(async_)) != MX_OK) {
delete handler;
}
return status;
}

} // namespace fs
51 changes: 51 additions & 0 deletions system/ulib/fs/include/fs/async-dispatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#pragma once

#include <stdlib.h>
#include <stdint.h>

#include <async/dispatcher.h>
#include <async/wait.h>
#include <fs/vfs.h>
#include <magenta/types.h>

namespace fs {

class AsyncHandler {
public:
AsyncHandler(mx::channel channel, vfs_dispatcher_cb_t cb, void* cookie);
~AsyncHandler();

mx_status_t Begin(async_t* async) { return wait_.Begin(async); }

async_wait_result_t Handle(async_t* async, mx_status_t status,
const mx_packet_signal_t* signal);
private:
DISALLOW_COPY_ASSIGN_AND_MOVE(AsyncHandler);

// Helper function to send final callback, delete |this|,
// and terminate the handler by returning |ASYNC_WAIT_FINISHED|
async_wait_result_t HandlerClose(bool need_close_cb);

mx::channel channel_;
vfs_dispatcher_cb_t cb_;
void* cookie_;
async::Wait wait_;
};

class AsyncDispatcher final : public fs::Dispatcher {
public:
AsyncDispatcher(async_t* async);
~AsyncDispatcher();

mx_status_t AddVFSHandler(mx::channel channel, vfs_dispatcher_cb_t cb,
void* iostate) final;

private:
async_t* async_;
};

} // namespace fs
4 changes: 4 additions & 0 deletions system/ulib/fs/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MODULE := $(LOCAL_DIR)
MODULE_TYPE := userlib

MODULE_SRCS += \
$(LOCAL_DIR)/async-dispatcher.cpp \
$(LOCAL_DIR)/mapped-vmo.cpp \
$(LOCAL_DIR)/mxio-dispatcher.cpp \
$(LOCAL_DIR)/vfs.cpp \
Expand All @@ -19,11 +20,14 @@ MODULE_SRCS += \
$(LOCAL_DIR)/vfs-watcher.cpp \

MODULE_STATIC_LIBS := \
system/ulib/async \
system/ulib/async.loop \
system/ulib/mx \
system/ulib/mxcpp \
system/ulib/fbl \

MODULE_LIBS := \
system/ulib/async.default \
system/ulib/c \
system/ulib/magenta \
system/ulib/mxio \
Expand Down

0 comments on commit a12054f

Please sign in to comment.