24 #include <seastar/core/do_with.hh>
25 #include <seastar/core/stream.hh>
26 #include <seastar/core/sstring.hh>
27 #include <seastar/core/shared_ptr.hh>
28 #include <seastar/core/align.hh>
29 #include <seastar/core/io_priority_class.hh>
30 #include <seastar/core/file-types.hh>
31 #include <seastar/util/std-compat.hh>
32 #include <system_error>
33 #include <sys/statvfs.h>
34 #include <sys/ioctl.h>
49 std::optional<directory_entry_type>
type;
55 uint64_t inode_number;
58 uint64_t number_of_links;
64 uint64_t allocated_size;
66 std::chrono::system_clock::time_point time_accessed;
67 std::chrono::system_clock::time_point time_modified;
68 std::chrono::system_clock::time_point time_changed;
84 static constexpr uint64_t max_extent_allocation_size_hint = 1 << 31;
97 virtual std::unique_ptr<file_handle_impl> clone()
const = 0;
105 unsigned _memory_dma_alignment = 4096;
106 unsigned _disk_read_dma_alignment = 4096;
107 unsigned _disk_write_dma_alignment = 4096;
108 unsigned _disk_overwrite_dma_alignment = 4096;
109 unsigned _read_max_length = 1u << 30;
110 unsigned _write_max_length = 1u << 30;
120 return write_dma(pos, buffer, len, pc);
123 return write_dma(pos, std::move(iov), pc);
126 return read_dma(pos, buffer, len, pc);
129 return read_dma(pos, std::move(iov), pc);
134 virtual future<> truncate(uint64_t length) = 0;
135 virtual future<> discard(uint64_t offset, uint64_t length) = 0;
136 virtual future<int> ioctl(uint64_t cmd,
void* argp) noexcept;
137 virtual future<int> ioctl_short(uint64_t cmd,
void* argp) noexcept;
138 virtual future<int> fcntl(
int op, uintptr_t arg) noexcept;
139 virtual future<int> fcntl_short(
int op, uintptr_t arg) noexcept;
140 virtual future<> allocate(uint64_t position, uint64_t length) = 0;
143 virtual std::unique_ptr<file_handle_impl> dup();
147 return dma_read_bulk(offset, range_size, pc);
179 file() noexcept : _file_impl(
nullptr) {}
182 : _file_impl(std::move(
impl)) {}
191 explicit operator bool() const noexcept {
return bool(_file_impl); }
199 file(
file&& x) noexcept : _file_impl(std::move(x._file_impl)) {}
218 return _file_impl->_disk_read_dma_alignment;
223 return _file_impl->_disk_write_dma_alignment;
233 return _file_impl->_disk_overwrite_dma_alignment;
238 return _file_impl->_memory_dma_alignment;
246 return _file_impl->_read_max_length;
254 return _file_impl->_write_max_length;
272 template <
typename CharType>
275 return dma_read_impl(aligned_pos,
reinterpret_cast<uint8_t*
>(aligned_buffer), aligned_len, pc, intent);
294 template <
typename CharType>
318 template <
typename CharType>
349 template <typename CharType>
351 return dma_write_impl(pos,
reinterpret_cast<const uint8_t*
>(buffer), len, pc, intent);
467 [[deprecated("This API was removed from the kernel")]]
494 [[deprecated("This API was removed from the kernel")]]
542 template <typename CharType>
570 dma_read_impl(uint64_t aligned_pos, uint8_t* aligned_buffer,
size_t aligned_len,
const io_priority_class& pc,
io_intent* intent) noexcept;
576 future<> set_lifetime_hint_impl(
int op, uint64_t hint) noexcept;
589 template <
typename Func>
590 SEASTAR_CONCEPT( requires std::invocable<Func, file&> && std::is_nothrow_move_constructible_v<Func> )
592 static_assert(std::is_nothrow_move_constructible_v<Func>,
"Func's move constructor must not throw");
593 return file_fut.then([func = std::move(func)] (
file f)
mutable {
594 return do_with(std::move(f), [func = std::move(func)] (
file& f)
mutable {
595 return futurize_invoke(func, f).finally([&f] {
616 template <
typename Func>
617 SEASTAR_CONCEPT( requires std::invocable<Func, file&> && std::is_nothrow_move_constructible_v<Func> )
619 static_assert(std::is_nothrow_move_constructible_v<Func>,
"Func's move constructor must not throw");
620 return file_fut.then([func = std::move(func)] (
file f)
mutable {
621 return do_with(std::move(f), [func = std::move(func)] (
file& f)
mutable {
622 return futurize_invoke(std::move(func), f).then_wrapped([&f] (
auto ret)
mutable {
626 return ret.finally([&f] {
647 std::unique_ptr<file_handle_impl> _impl;
672 virtual const char* what()
const noexcept {
An exception Cancelled IOs resolve their future into (see io_intent)
Definition: file.hh:670
A shard-transportable handle to a file.
Definition: file.hh:646
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< size_t > dma_read(uint64_t pos, std::vector< iovec > iov, const io_priority_class &pc=default_priority_class(), io_intent *intent=nullptr) noexcept
subscription< directory_entry > list_directory(std::function< future<>(directory_entry de)> next)
Returns a directory listing, given that this file object is a directory.
future< temporary_buffer< CharType > > dma_read_exactly(uint64_t pos, size_t len, const io_priority_class &pc=default_priority_class(), io_intent *intent=nullptr) noexcept
Definition: file.hh:320
file & operator=(const file &x) noexcept=default
future< int > fcntl_short(int op, uintptr_t arg=0UL) noexcept
future allocate(uint64_t position, uint64_t length) noexcept
future set_inode_lifetime_hint(uint64_t hint) noexcept
future< uint64_t > get_inode_lifetime_hint() noexcept
uint64_t memory_dma_alignment() const noexcept
Alignment requirement for data buffers.
Definition: file.hh:237
file(file_handle &&handle) noexcept
Constructs a file object from a file_handle obtained from another shard.
size_t disk_read_max_length() const noexcept
Definition: file.hh:245
future< int > fcntl(int op, uintptr_t arg=0UL) noexcept
future< temporary_buffer< CharType > > dma_read_bulk(uint64_t offset, size_t range_size, const io_priority_class &pc=default_priority_class(), io_intent *intent=nullptr) noexcept
Definition: file.hh:544
future< int > ioctl(uint64_t cmd, void *argp) noexcept
future set_file_lifetime_hint(uint64_t hint) noexcept
file_handle dup()
Creates a handle that can be transported across shards.
future< size_t > dma_write(uint64_t pos, std::vector< iovec > iov, const io_priority_class &pc=default_priority_class(), io_intent *intent=nullptr) noexcept
file(file &&x) noexcept
Moves a file object.
Definition: file.hh:199
uint64_t disk_read_dma_alignment() const noexcept
Alignment requirement for file offsets (for reads)
Definition: file.hh:217
future< size_t > dma_write(uint64_t pos, const CharType *buffer, size_t len, const io_priority_class &pc=default_priority_class(), io_intent *intent=nullptr) noexcept
Definition: file.hh:350
future discard(uint64_t offset, uint64_t length) noexcept
future< temporary_buffer< CharType > > dma_read(uint64_t pos, size_t len, const io_priority_class &pc=default_priority_class(), io_intent *intent=nullptr) noexcept
Definition: file.hh:295
uint64_t disk_overwrite_dma_alignment() const noexcept
Definition: file.hh:232
future< size_t > dma_read(uint64_t aligned_pos, CharType *aligned_buffer, size_t aligned_len, const io_priority_class &pc=default_priority_class(), io_intent *intent=nullptr) noexcept
Definition: file.hh:274
future truncate(uint64_t length) noexcept
Truncates the file to a specified length.
size_t disk_write_max_length() const noexcept
Definition: file.hh:253
uint64_t disk_write_dma_alignment() const noexcept
Alignment requirement for file offsets (for writes)
Definition: file.hh:222
future< struct stat > stat() noexcept
Returns stat information about the file.
future< uint64_t > size() const noexcept
Gets the file size.
future< int > ioctl_short(uint64_t cmd, void *argp) noexcept
file & operator=(file &&x) noexcept=default
Moves assigns a file object.
future< uint64_t > get_file_lifetime_hint() noexcept
file() noexcept
Definition: file.hh:179
file(const file &x)=default
A representation of a possibly not-yet-computed value.
Definition: future.hh:1349
Definition: io_intent.hh:40
Definition: io_priority_class.hh:37
Definition: reactor.hh:180
Definition: shared_ptr.hh:502
Definition: stream.hh:123
Definition: temporary_buffer.hh:62
deleter release() noexcept
Definition: temporary_buffer.hh:198
CharType * get_write() noexcept
Definition: temporary_buffer.hh:123
size_t size() const noexcept
Gets the buffer size.
Definition: temporary_buffer.hh:125
std::optional< directory_entry_type > type
Type of the directory entry, if known.
Definition: file.hh:49
sstring name
Name of the file in a directory entry. Will never be "." or "..". Only the last component is included...
Definition: file.hh:47
directory_entry_type
Definition: file-types.hh:65
auto with_file(future< file > file_fut, Func func) noexcept
Helper for ensuring a file is closed after func is called.
Definition: file.hh:591
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:618
A directory entry being listed.
Definition: file.hh:45
Filesystem object stat information.
Definition: file.hh:53
auto do_with(T1 &&rv1, T2 &&rv2, More &&... more) noexcept
Definition: do_with.hh:129
holds the implementation parts of the metrics layer, do not use directly.
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
bool sloppy_size
Allow the file size not to track the amount of data written until a flush.
Definition: file.hh:78
uint64_t sloppy_size_hint
Hint as to what the eventual file size will be.
Definition: file.hh:79
bool append_is_unlikely
Hint that user promises (or at least tries hard) not to write behind file size.
Definition: file.hh:81
file_permissions create_permissions
File permissions to use when creating a file.
Definition: file.hh:80
uint64_t extent_allocation_size_hint
Allocate this much disk space when extending the file.
Definition: file.hh:77