blob: 1f9043e44805a755a4c0e6a61601a7769c400fa9 [file]
// Copyright 2024 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.
#ifndef SRC_STORAGE_LIB_BLOCK_SERVER_BLOCK_SERVER_C_H_
#define SRC_STORAGE_LIB_BLOCK_SERVER_BLOCK_SERVER_C_H_
// NOTE: This file is autogenerated by cbindgen. Don't modify this manually. To regenerate
// this file:
//
// Add `--cargo-toml-gen` to your `fx set`. Then:
//
// fx build //build/rust:cargo_toml_gen //src/storage/lib/block_server
// fx gen-cargo //src/storage/lib/block_server
// cargo install cbindgen
// rustup default nightly
// cbindgen --output block_server_c.h
// fx format-code
#include <zircon/types.h>
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>
#include <ostream>
#include "lib/zx/vmo.h"
namespace block_server::internal {
struct BlockServer;
struct Session;
class WriteFlags {
public:
bool is_force_access() const { return (options_ & 1) != 0; }
bool is_pre_barrier() const { return (options_ & 2) != 0; }
private:
uint32_t options_;
};
} // namespace block_server::internal
namespace block_server {
namespace internal {
struct PartitionInfo {
uint32_t device_flags;
uint64_t start_block;
uint64_t block_count;
uint32_t block_size;
uint8_t type_guid[16];
uint8_t instance_guid[16];
const char *name;
uint64_t flags;
uint32_t max_transfer_size;
};
using RequestId = uintptr_t;
struct InlineCryptoOptions {
bool is_enabled;
uint8_t slot;
uint64_t dun;
};
struct ReadOptions {
InlineCryptoOptions inline_crypto;
};
struct WriteOptions {
InlineCryptoOptions inline_crypto;
WriteFlags flags;
};
struct Operation {
enum class Tag {
Read,
Write,
Flush,
Trim,
/// This will never be seen by the C interface.
CloseVmo,
/// This will never be seen by the C interface.
StartDecompressedRead,
/// This will never be seen by the C interface.
ContinueDecompressedRead,
};
struct Read_Body {
uint64_t device_block_offset;
uint32_t block_count;
uint32_t _unused;
uint64_t vmo_offset;
ReadOptions options;
};
struct Write_Body {
uint64_t device_block_offset;
uint32_t block_count;
uint32_t _unused;
uint64_t vmo_offset;
WriteOptions options;
};
struct Trim_Body {
uint64_t device_block_offset;
uint32_t block_count;
};
struct StartDecompressedRead_Body {
uintptr_t required_buffer_size;
uint64_t device_block_offset;
uint32_t block_count;
ReadOptions options;
};
struct ContinueDecompressedRead_Body {
uint64_t offset;
uint64_t device_block_offset;
uint32_t block_count;
ReadOptions options;
};
Tag tag;
union {
Read_Body read;
Write_Body write;
Trim_Body trim;
StartDecompressedRead_Body start_decompressed_read;
ContinueDecompressedRead_Body continue_decompressed_read;
};
};
using TraceFlowId = uint64_t;
struct Request {
RequestId request_id;
Operation operation;
TraceFlowId trace_flow_id;
zx::unowned<zx::vmo> vmo;
};
struct Callbacks {
/// An opaque context object retained by this library. The library will pass this back into all
/// callbacks. The memory pointed to by `context` must last until [`block_server_delete`] is
/// called.
void *context;
/// Starts a thread. The implementation must call [`block_server_thread`] on this newly created
/// thread, providing `arg`. Once this completes, the implementation must NOT use the block
/// server for which the thread was started, as it could be destroyed at any time.
/// The implementation must call [`block_server_thread_release`] after [`block_server_thread`]
/// completes (but before [`block_server_delete`] is called).
void (*start_thread)(void *context, const void *arg);
/// Notifies the implementation of a new session. The implementation must call
/// [`block_server_session_run`] on a separate thread, and must call
/// [`block_server_session_release`] after [`block_server_session_run`] (but before
/// [`block_server_delete`] is called).
void (*on_new_session)(void *context, const Session *session);
/// Submits a batch of requests to be handled by the implementation. The implementation must
/// not retain references to `requests` after it returns. The implementation must ensure that
/// [`block_server_send_reply`] is called exactly once with the request ID of each entry in
/// `requests`, regardless of its status; this call can be asynchronous but must occur before
/// [`block_server_delete`] is called. Note that a reply must be sent for every request before
/// shutdown.
void (*on_requests)(void *context, Request *requests, uintptr_t request_count);
/// Logs `message` to the implementation's logger. The implementation must not retain
/// references to `message`.
void (*log)(void *context, const char *message, uintptr_t message_len);
};
using ShutdownCallback = void (*)(void *);
extern "C" {
/// Creates a new block server. Returns nullptr on failure (e.g. if the thread to run the block
/// server failed to start).
///
/// # Safety
///
/// All callbacks in `callbacks` must be safe.
BlockServer *block_server_new(const PartitionInfo *partition_info, Callbacks callbacks);
/// Runs the main loop to handle FIDL requests for the block server. Blocks until the server is
/// shutting down.
///
/// After this returns, the caller *must* call [`block_server_thread_release`] on the same thread.
///
/// # Safety
///
/// `arg` must be the value passed to the `start_thread` callback.
void block_server_thread(const void *arg);
/// Called to release the thread. This *must* always be called on the thread spawned by
/// [`Callbacks::start_thread`], regardless of whether [`block_server_thread`] is called or not.
///
/// # Safety
///
/// `arg` must be the value passed to the `start_thread` callback.
void block_server_thread_release(const void *arg);
/// # Safety
///
/// `block_server` must be valid and either `block_server_delete` or `block_server_delete_async` may
/// only be called once.
void block_server_delete(const BlockServer *block_server);
/// # Safety
///
/// `block_server` must be valid and either `block_server_delete` or `block_server_delete_async` may
/// only be called once.
void block_server_delete_async(const BlockServer *block_server, ShutdownCallback callback,
void *arg);
/// Serves the Volume protocol for this server. `handle` is consumed.
///
/// # Safety
///
/// `block_server` and `handle` must be valid.
void block_server_serve(const BlockServer *block_server, zx_handle_t handle);
/// Serves the Mapper protocol for this server. `handle` is consumed.
///
/// # Safety
///
/// `block_server` and `handle` must be valid.
void block_server_serve_mapper(const BlockServer *block_server, zx_handle_t handle);
/// # Safety
///
/// `session` must be valid.
void block_server_session_run(const Session *session);
/// # Safety
///
/// `session` must be valid.
void block_server_session_release(const Session *session);
/// # Safety
///
/// `block_server` must be valid.
void block_server_send_reply(const BlockServer *block_server, RequestId request_id,
zx_status_t status);
/// Registers a hardware inline encryption key slot `hw_slot` with `block_server`, writing the
/// client end of the minted eventpair token to `out_handle`.
///
/// # Safety
///
/// `block_server` and `out_handle` must be valid.
zx_status_t block_server_register_key_slot(const BlockServer *block_server, uint8_t hw_slot,
zx_handle_t *out_handle);
} // extern "C"
} // namespace internal
} // namespace block_server
#endif // SRC_STORAGE_LIB_BLOCK_SERVER_BLOCK_SERVER_C_H_