24#include <seastar/util/std-compat.hh>
25#include <seastar/core/coroutine.hh>
26#include <seastar/coroutine/generator.hh>
27#include <seastar/core/do_with.hh>
28#include <seastar/core/stream.hh>
29#include <seastar/core/sstring.hh>
30#include <seastar/core/shared_ptr.hh>
31#include <seastar/core/align.hh>
32#include <seastar/core/io_priority_class.hh>
33#include <seastar/core/file-types.hh>
34#include <seastar/core/circular_buffer.hh>
35#include <seastar/util/modules.hh>
37#include <sys/statvfs.h>
51SEASTAR_MODULE_EXPORT_BEGIN
61 std::optional<directory_entry_type>
type;
69 std::vector<sstring> group_members;
75 uint64_t inode_number;
78 uint64_t number_of_links;
84 uint64_t allocated_size;
86 std::chrono::system_clock::time_point time_accessed;
87 std::chrono::system_clock::time_point time_modified;
88 std::chrono::system_clock::time_point time_changed;
104 static constexpr uint64_t max_extent_allocation_size_hint = 1 << 31;
108 static constexpr uint32_t min_extent_size_hint_alignment{128u << 10};
115class file_data_sink_impl;
116class file_data_source_impl;
123 virtual std::unique_ptr<file_handle_impl> clone()
const = 0;
131 unsigned _memory_dma_alignment = 4096;
132 unsigned _disk_read_dma_alignment = 4096;
133 unsigned _disk_write_dma_alignment = 4096;
134 unsigned _disk_overwrite_dma_alignment = 4096;
135 unsigned _read_max_length = 1u << 30;
136 unsigned _write_max_length = 1u << 30;
148 virtual future<> truncate(uint64_t length) = 0;
149 virtual future<> discard(uint64_t offset, uint64_t length) = 0;
150 virtual future<int> ioctl(uint64_t cmd,
void* argp)
noexcept;
151 virtual future<int> ioctl_short(uint64_t cmd,
void* argp)
noexcept;
152 virtual future<int> fcntl(
int op, uintptr_t arg)
noexcept;
153 virtual future<int> fcntl_short(
int op, uintptr_t arg)
noexcept;
154 virtual future<> allocate(uint64_t position, uint64_t length) = 0;
157 virtual std::unique_ptr<file_handle_impl> dup();
190 file() noexcept : _file_impl(
nullptr) {}
193 : _file_impl(std::move(
impl)) {}
202 explicit operator bool() const noexcept {
return bool(_file_impl); }
210 file(
file&& x) noexcept : _file_impl(std::move(x._file_impl)) {}
229 return _file_impl->_disk_read_dma_alignment;
234 return _file_impl->_disk_write_dma_alignment;
244 return _file_impl->_disk_overwrite_dma_alignment;
249 return _file_impl->_memory_dma_alignment;
257 return _file_impl->_read_max_length;
265 return _file_impl->_write_max_length;
282 template <
typename CharType>
284 dma_read(uint64_t aligned_pos, CharType* aligned_buffer,
size_t aligned_len,
io_intent* intent =
nullptr) noexcept {
285 return dma_read_impl(aligned_pos,
reinterpret_cast<uint8_t*
>(aligned_buffer), aligned_len, internal::maybe_priority_class_ref(), intent);
303 template <
typename CharType>
326 template <
typename CharType>
329 return dma_read_exactly_impl(pos, len, internal::maybe_priority_class_ref(), intent).then([] (
temporary_buffer<uint8_t> t) {
344 return dma_read_impl(pos, std::move(iov), internal::maybe_priority_class_ref(), intent);
357 template <
typename CharType>
359 return dma_write_impl(pos,
reinterpret_cast<const uint8_t*
>(buffer), len, internal::maybe_priority_class_ref(), intent);
372 return dma_write_impl(pos, std::move(iov), internal::maybe_priority_class_ref(), intent);
476 [[deprecated("This API was removed from the kernel")]]
503 [[deprecated("This API was removed from the kernel")]]
556 template <typename CharType>
559 return dma_read_bulk_impl(offset, range_size, internal::maybe_priority_class_ref(), intent).then([] (
temporary_buffer<uint8_t> t) {
575 dma_read_bulk_impl(uint64_t offset,
size_t range_size, internal::maybe_priority_class_ref pc,
io_intent* intent)
noexcept;
578 dma_write_impl(uint64_t pos,
const uint8_t* buffer,
size_t len, internal::maybe_priority_class_ref pc,
io_intent* intent)
noexcept;
581 dma_write_impl(uint64_t pos, std::vector<iovec> iov, internal::maybe_priority_class_ref pc,
io_intent* intent)
noexcept;
584 dma_read_impl(uint64_t pos,
size_t len, internal::maybe_priority_class_ref pc,
io_intent* intent)
noexcept;
587 dma_read_impl(uint64_t aligned_pos, uint8_t* aligned_buffer,
size_t aligned_len, internal::maybe_priority_class_ref pc,
io_intent* intent)
noexcept;
590 dma_read_impl(uint64_t pos, std::vector<iovec> iov, internal::maybe_priority_class_ref pc,
io_intent* intent)
noexcept;
593 dma_read_exactly_impl(uint64_t pos,
size_t len, internal::maybe_priority_class_ref pc,
io_intent* intent)
noexcept;
596 future<> set_lifetime_hint_impl(
int op, uint64_t hint)
noexcept;
599 friend class file_data_sink_impl;
600 friend class file_data_source_impl;
610template <std::invocable<file&> Func>
611requires std::is_nothrow_move_constructible_v<Func>
613 static_assert(std::is_nothrow_move_constructible_v<Func>,
"Func's move constructor must not throw");
614 return file_fut.then([func = std::move(func)] (
file f)
mutable {
615 return do_with(std::move(f), [func = std::move(func)] (
file& f)
mutable {
616 return futurize_invoke(func, f).finally([&f] {
637template <std::invocable<file&> Func>
638requires std::is_nothrow_move_constructible_v<Func>
640 static_assert(std::is_nothrow_move_constructible_v<Func>,
"Func's move constructor must not throw");
641 return file_fut.then([func = std::move(func)] (
file f)
mutable {
642 return do_with(std::move(f), [func = std::move(func)] (
file& f)
mutable {
643 return futurize_invoke(std::move(func), f).then_wrapped([&f] (
auto ret)
mutable {
647 return ret.finally([&f] {
668 std::unique_ptr<file_handle_impl> _impl;
693 virtual const char* what()
const noexcept {
698SEASTAR_MODULE_EXPORT_END
An exception Cancelled IOs resolve their future into (see io_intent)
Definition: file.hh:691
Definition: generator.hh:33
A shard-transportable handle to a file.
Definition: file.hh:667
file_handle(file_handle &&) noexcept
Moves a file handle object.
file_handle(const file_handle &)
Copies a file handle object.
file to_file() const &
Converts the file handle object to a file.
future< int > ioctl(uint64_t cmd, void *argp) noexcept
future< temporary_buffer< CharType > > dma_read(uint64_t pos, size_t len, io_intent *intent=nullptr) noexcept
Definition: file.hh:304
file & operator=(file &&x) noexcept=default
Moves assigns a file object.
future< temporary_buffer< CharType > > dma_read_bulk(uint64_t offset, size_t range_size, io_intent *intent=nullptr) noexcept
Definition: file.hh:558
subscription< directory_entry > list_directory(std::function< future<>(directory_entry de)> next)
Returns a directory listing, given that this file object is a directory.
file & operator=(const file &x) noexcept=default
future< size_t > dma_write(uint64_t pos, std::vector< iovec > iov, io_intent *intent=nullptr) noexcept
Definition: file.hh:371
future allocate(uint64_t position, uint64_t length) noexcept
future set_inode_lifetime_hint(uint64_t hint) noexcept
uint64_t memory_dma_alignment() const noexcept
Alignment requirement for data buffers.
Definition: file.hh:248
file(file_handle &&handle) noexcept
Constructs a file object from a file_handle obtained from another shard.
future< size_t > dma_write(uint64_t pos, const CharType *buffer, size_t len, io_intent *intent=nullptr) noexcept
Definition: file.hh:358
future< int > fcntl_short(int op, uintptr_t arg=0UL) noexcept
size_t disk_read_max_length() const noexcept
Definition: file.hh:256
future< size_t > dma_read(uint64_t pos, std::vector< iovec > iov, io_intent *intent=nullptr) noexcept
Definition: file.hh:343
future set_file_lifetime_hint(uint64_t hint) noexcept
file_handle dup()
Creates a handle that can be transported across shards.
future< uint64_t > get_inode_lifetime_hint() noexcept
file(file &&x) noexcept
Moves a file object.
Definition: file.hh:210
uint64_t disk_read_dma_alignment() const noexcept
Alignment requirement for file offsets (for reads)
Definition: file.hh:228
future< uint64_t > size() const noexcept
Gets the file size.
future discard(uint64_t offset, uint64_t length) noexcept
future< struct stat > stat() noexcept
Returns stat information about the file.
uint64_t disk_overwrite_dma_alignment() const noexcept
Definition: file.hh:243
future< size_t > dma_read(uint64_t aligned_pos, CharType *aligned_buffer, size_t aligned_len, io_intent *intent=nullptr) noexcept
Definition: file.hh:284
future truncate(uint64_t length) noexcept
Truncates the file to a specified length.
future< int > ioctl_short(uint64_t cmd, void *argp) noexcept
coroutine::experimental::generator< directory_entry > experimental_list_directory()
Returns a directory listing, given that this file object is a directory.
size_t disk_write_max_length() const noexcept
Definition: file.hh:264
uint64_t disk_write_dma_alignment() const noexcept
Alignment requirement for file offsets (for writes)
Definition: file.hh:233
future< int > fcntl(int op, uintptr_t arg=0UL) noexcept
future< uint64_t > get_file_lifetime_hint() noexcept
file() noexcept
Definition: file.hh:190
file(const file &x)=default
future< temporary_buffer< CharType > > dma_read_exactly(uint64_t pos, size_t len, io_intent *intent=nullptr) noexcept
Definition: file.hh:328
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
Definition: io_intent.hh:44
Definition: shared_ptr.hh:507
Definition: stream.hh:127
Definition: temporary_buffer.hh:67
deleter release() noexcept
Definition: temporary_buffer.hh:203
size_t size() const noexcept
Gets the buffer size.
Definition: temporary_buffer.hh:130
CharType * get_write() noexcept
Definition: temporary_buffer.hh:128
std::optional< directory_entry_type > type
Type of the directory entry, if known.
Definition: file.hh:61
sstring name
Name of the file in a directory entry. Will never be "." or "..". Only the last component is included...
Definition: file.hh:59
directory_entry_type
Definition: file-types.hh:70
auto with_file_close_on_failure(future< file > file_fut, Func func) noexcept
Helper for ensuring a file is closed if func fails.
Definition: file.hh:639
auto with_file(future< file > file_fut, Func func) noexcept
Helper for ensuring a file is closed after func is called.
Definition: file.hh:612
A directory entry being listed.
Definition: file.hh:57
Group details from the system group database.
Definition: file.hh:65
Filesystem object stat information.
Definition: file.hh:73
auto do_with(T1 &&rv1, T2 &&rv2, More &&... more) noexcept
Definition: do_with.hh:135
holds the implementation parts of the metrics layer, do not use directly.
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
bool sloppy_size
Allow the file size not to track the amount of data written until a flush.
Definition: file.hh:98
uint64_t sloppy_size_hint
Hint as to what the eventual file size will be.
Definition: file.hh:99
bool append_is_unlikely
Hint that user promises (or at least tries hard) not to write behind file size.
Definition: file.hh:101
file_permissions create_permissions
File permissions to use when creating a file.
Definition: file.hh:100
uint64_t extent_allocation_size_hint
Allocate this much disk space when extending the file.
Definition: file.hh:97