libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_bdev.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_bdev.h
6 * Portable block-device I/O over the async xtc_aio path. A thin,
7 * uniform surface for reading and writing a raw block device (or a
8 * regular file treated as one) with sector-aligned positioned I/O
9 * that suspends the calling fiber and resumes it on completion --
10 * the same "write once, runs as AIO everywhere" model as
11 * xtc_aio(3), on top of which this layer is built.
12 *
13 * xtc_bdev_open probes the device for its logical and physical
14 * sector sizes and total capacity using the host's native query
15 * (BLKSSZGET / BLKGETSIZE64 on Linux, DIOCGSECTORSIZE on the BSDs
16 * and macOS, DKIOCGMEDIAINFO on illumos, the drive geometry IOCTL on
17 * Windows) and opens the descriptor with direct I/O (O_DIRECT) for a
18 * real device so transfers bypass the page cache. A regular file or
19 * an unqueryable target falls back to logical=512, physical=4096, and
20 * the capacity reported by fstat, with buffered I/O.
21 *
22 * xtc_bdev_pread / xtc_bdev_pwrite route through
23 * xtc_aio_pread / xtc_aio_pwrite: on a fiber loop the call parks the
24 * fiber and the loop keeps running other work. The offset and the
25 * length must both be multiples of the logical sector size, else the
26 * call returns XTC_E_INVAL. xtc_bdev_flush issues xtc_aio_fsync.
27 *
28 * See xtc_bdev(3).
29 */
30
31#ifndef XTC_BDEV_H
32#define XTC_BDEV_H
33
34#include <stddef.h>
35#include <stdint.h>
36
37#if defined(_WIN32)
38typedef long long xtc_ssize_t; /* Win32 CRT lacks ssize_t */
39#define ssize_t xtc_ssize_t
40#else
41#include <sys/types.h> /* ssize_t */
42#endif
43
44/* Open flags for xtc_bdev_open (subset of xtc_fs flags that make sense
45 * for a block device; mapped to O_* per platform). */
46#define XTC_BDEV_READ 0x01u /* open for reading */
47#define XTC_BDEV_WRITE 0x02u /* open for writing (implies read too) */
48
49/* Opaque device handle. */
50typedef struct xtc_bdev xtc_bdev_t;
51
52/*
53 * PUBLIC: int xtc_bdev_open __P((const char *, int, xtc_bdev_t **));
54 *
55 * Open path as a block device, probing its geometry. flags is a mask of
56 * XTC_BDEV_READ / XTC_BDEV_WRITE. On success stores the handle in *out
57 * and returns XTC_OK; otherwise a negative XTC_E_* code.
58 */
59int xtc_bdev_open(const char *path, int flags, xtc_bdev_t **out);
60
61/*
62 * PUBLIC: void xtc_bdev_close __P((xtc_bdev_t *));
63 *
64 * Close the device and free the handle. NULL is a no-op.
65 */
66void xtc_bdev_close(xtc_bdev_t *b);
67
68/*
69 * PUBLIC: uint32_t xtc_bdev_logical_sector __P((const xtc_bdev_t *));
70 * PUBLIC: uint32_t xtc_bdev_physical_sector __P((const xtc_bdev_t *));
71 * PUBLIC: uint64_t xtc_bdev_capacity __P((const xtc_bdev_t *));
72 *
73 * Report the probed logical sector size, physical sector size, and total
74 * capacity in bytes. Offsets and lengths passed to pread/pwrite must be
75 * multiples of the logical sector size.
76 */
77uint32_t xtc_bdev_logical_sector(const xtc_bdev_t *b);
78uint32_t xtc_bdev_physical_sector(const xtc_bdev_t *b);
79uint64_t xtc_bdev_capacity(const xtc_bdev_t *b);
80
81/*
82 * PUBLIC: ssize_t xtc_bdev_pread __P((xtc_bdev_t *, void *, size_t, uint64_t));
83 * PUBLIC: ssize_t xtc_bdev_pwrite __P((xtc_bdev_t *, const void *, size_t, uint64_t));
84 *
85 * Read n bytes into buf / write n bytes from buf at absolute byte offset
86 * off, routed through xtc_aio so the fiber parks and the loop stays live.
87 * off and n must each be a multiple of the logical sector size, else the
88 * call returns XTC_E_INVAL. On success returns the byte count
89 * transferred (>= 0); a short read at end of device returns the partial
90 * count. On failure returns a negative XTC_E_* code.
91 */
92ssize_t xtc_bdev_pread(xtc_bdev_t *b, void *buf, size_t n, uint64_t off);
93ssize_t xtc_bdev_pwrite(xtc_bdev_t *b, const void *buf, size_t n, uint64_t off);
94
95/*
96 * PUBLIC: int xtc_bdev_flush __P((xtc_bdev_t *));
97 *
98 * Flush the device's data and metadata durably via xtc_aio_fsync.
99 * Returns XTC_OK or a negative XTC_E_* code.
100 */
101int xtc_bdev_flush(xtc_bdev_t *b);
102
103#endif /* XTC_BDEV_H */