|
| 1 | +// Copyright (C) 2024-2026 ilobilo |
| 2 | + |
| 3 | +module system.syscall.vfs; |
| 4 | + |
| 5 | +namespace syscall::vfs |
| 6 | +{ |
| 7 | + using namespace ::vfs; |
| 8 | + |
| 9 | + namespace |
| 10 | + { |
| 11 | + constexpr auto ev_max = std::numeric_limits<std::uint64_t>::max() - 1; |
| 12 | + constexpr int efd_semaphore = (1 << 0); |
| 13 | + constexpr int efd_cloexec = o_cloexec; |
| 14 | + constexpr int efd_nonblock = o_nonblock; |
| 15 | + |
| 16 | + struct data_t |
| 17 | + { |
| 18 | + lib::locker< |
| 19 | + std::uint64_t, |
| 20 | + lib::spinlock |
| 21 | + > counter; |
| 22 | + sched::wait_queue_t bell; |
| 23 | + bool semaphore; |
| 24 | + |
| 25 | + data_t(std::uint64_t initial, bool semaphore) |
| 26 | + : counter { initial }, bell { }, semaphore { semaphore } { } |
| 27 | + }; |
| 28 | + |
| 29 | + struct ops : ::vfs::ops |
| 30 | + { |
| 31 | + static std::shared_ptr<ops> singleton() |
| 32 | + { |
| 33 | + static auto instance = std::make_shared<ops>(); |
| 34 | + return instance; |
| 35 | + } |
| 36 | + |
| 37 | + bool seekable() const override { return false; } |
| 38 | + |
| 39 | + lib::expect<void> open(std::shared_ptr<vfs::file> file, int flags, pid_t pid) override |
| 40 | + { |
| 41 | + lib::unused(file, flags, pid); |
| 42 | + return std::unexpected { lib::err::invalid_device_or_address }; |
| 43 | + } |
| 44 | + |
| 45 | + lib::expect<std::size_t> read( |
| 46 | + std::shared_ptr<vfs::file> file, std::uint64_t offset, |
| 47 | + lib::maybe_uspan<std::byte> buffer |
| 48 | + ) override |
| 49 | + { |
| 50 | + lib::unused(offset); |
| 51 | + if (buffer.size() < sizeof(std::uint64_t)) |
| 52 | + return std::unexpected { lib::err::invalid_argument }; |
| 53 | + |
| 54 | + const bool nonblock = file->flags & efd_nonblock; |
| 55 | + auto data = std::static_pointer_cast<data_t>(file->private_data); |
| 56 | + |
| 57 | + std::uint64_t ret; |
| 58 | + { |
| 59 | + auto locked = data->counter.lock(); |
| 60 | + again: |
| 61 | + if (*locked != 0) |
| 62 | + { |
| 63 | + if (!data->semaphore) |
| 64 | + { |
| 65 | + ret = *locked; |
| 66 | + *locked = 0; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + ret = 1; |
| 71 | + (*locked)--; |
| 72 | + } |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + if (nonblock) |
| 77 | + return std::unexpected { lib::err::try_again }; |
| 78 | + |
| 79 | + while (true) |
| 80 | + { |
| 81 | + const auto gen = data->bell.snapshot_gen(); |
| 82 | + locked.unlock(); |
| 83 | + { |
| 84 | + const auto res = data->bell.wait_prepared(gen); |
| 85 | + if (res.interrupted || res.killed) |
| 86 | + return std::unexpected { lib::err::interrupted }; |
| 87 | + } |
| 88 | + locked.lock(); |
| 89 | + if (*locked != 0) |
| 90 | + goto again; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (!buffer.copy_from(std::as_bytes(std::span { &ret, 1 }))) |
| 96 | + return std::unexpected { lib::err::invalid_address }; |
| 97 | + |
| 98 | + data->bell.wake_all(); |
| 99 | + return sizeof(ret); |
| 100 | + } |
| 101 | + |
| 102 | + lib::expect<std::size_t> write( |
| 103 | + std::shared_ptr<vfs::file> file, std::uint64_t offset, |
| 104 | + lib::maybe_uspan<std::byte> buffer |
| 105 | + ) override |
| 106 | + { |
| 107 | + lib::unused(offset); |
| 108 | + |
| 109 | + if (buffer.size() < sizeof(std::uint64_t)) |
| 110 | + return std::unexpected { lib::err::invalid_argument }; |
| 111 | + |
| 112 | + std::uint64_t val; |
| 113 | + if (!buffer.copy_to(std::as_writable_bytes(std::span { &val, 1 }))) |
| 114 | + return std::unexpected { lib::err::invalid_address }; |
| 115 | + |
| 116 | + if (val > ev_max) |
| 117 | + return std::unexpected { lib::err::invalid_argument }; |
| 118 | + |
| 119 | + const bool nonblock = file->flags & efd_nonblock; |
| 120 | + auto data = std::static_pointer_cast<data_t>(file->private_data); |
| 121 | + |
| 122 | + while (true) |
| 123 | + { |
| 124 | + bool blocked = false; |
| 125 | + std::size_t gen = 0; |
| 126 | + { |
| 127 | + auto locked = data->counter.lock(); |
| 128 | + if (val > ev_max - *locked) |
| 129 | + { |
| 130 | + gen = data->bell.snapshot_gen(); |
| 131 | + blocked = true; |
| 132 | + } |
| 133 | + else *locked += val; |
| 134 | + } |
| 135 | + |
| 136 | + if (!blocked) |
| 137 | + { |
| 138 | + data->bell.wake_all(); |
| 139 | + return sizeof(val); |
| 140 | + } |
| 141 | + |
| 142 | + if (nonblock) |
| 143 | + return std::unexpected { lib::err::try_again }; |
| 144 | + |
| 145 | + const auto res = data->bell.wait_prepared(gen); |
| 146 | + if (res.interrupted || res.killed) |
| 147 | + return std::unexpected { lib::err::interrupted }; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + lib::expect<void> trunc(std::shared_ptr<vfs::file> file, std::size_t size) override |
| 152 | + { |
| 153 | + lib::unused(file, size); |
| 154 | + return std::unexpected { lib::err::illegal_seek }; |
| 155 | + } |
| 156 | + |
| 157 | + lib::expect<std::uint16_t> poll( |
| 158 | + std::shared_ptr<vfs::file> file, vfs::poll_table *pt |
| 159 | + ) override |
| 160 | + { |
| 161 | + auto data = std::static_pointer_cast<data_t>(file->private_data); |
| 162 | + if (pt) |
| 163 | + pt->add(data->bell); |
| 164 | + |
| 165 | + auto locked = data->counter.lock(); |
| 166 | + std::uint16_t mask = 0; |
| 167 | + |
| 168 | + if (*locked > 0) |
| 169 | + mask |= pollin; |
| 170 | + if (*locked < ev_max) |
| 171 | + mask |= pollout; |
| 172 | + |
| 173 | + return mask; |
| 174 | + } |
| 175 | + }; |
| 176 | + } // namespace |
| 177 | + |
| 178 | + int eventfd2(unsigned int count, int flags) |
| 179 | + { |
| 180 | + if (flags & ~(efd_semaphore | efd_cloexec | efd_nonblock)) |
| 181 | + return -EINVAL; |
| 182 | + |
| 183 | + auto ret = create_anon_fd({ |
| 184 | + .name = "<[EVENTFD]>", |
| 185 | + .ops = ops::singleton(), |
| 186 | + .file_private_data = std::make_shared<data_t>(count, flags & efd_semaphore), |
| 187 | + .inode_private_data = nullptr, |
| 188 | + .st_mode = std::to_underlying(stat::s_ifreg) | s_irusr | s_iwusr, |
| 189 | + .flags = (flags & ~efd_semaphore) | o_rdwr, |
| 190 | + .skip_open = true, |
| 191 | + .inode = nullptr |
| 192 | + }); |
| 193 | + if (!ret) |
| 194 | + return -lib::map_error(ret.error()); |
| 195 | + |
| 196 | + return ret->first; |
| 197 | + } |
| 198 | + |
| 199 | + int eventfd(unsigned int count) |
| 200 | + { |
| 201 | + return eventfd2(count, 0); |
| 202 | + } |
| 203 | +} // namespace syscall::vfs |
0 commit comments