From a0475535816af58314ca3010eef0d0744a48b5d6 Mon Sep 17 00:00:00 2001 From: Alexander van der Grinten Date: Sun, 18 Feb 2018 21:56:44 +0100 Subject: [PATCH] options/posix: Implement posix_fallocate() --- options/internal/include/mlibc/sysdeps.hpp | 1 + options/posix/generic/fcntl-stubs.cpp | 28 +++++++++++---- sysdeps/managarm/generic/file.cpp | 40 ++++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/options/internal/include/mlibc/sysdeps.hpp b/options/internal/include/mlibc/sysdeps.hpp index a9c387ce..1850f1d5 100644 --- a/options/internal/include/mlibc/sysdeps.hpp +++ b/options/internal/include/mlibc/sysdeps.hpp @@ -28,6 +28,7 @@ int sys_isatty(int fd, int *ptr); int sys_stat(const char *pathname, struct stat *statbuf); int sys_fstat(int fd, struct stat *statbuf); int sys_readlink(const char *path, void *buffer, size_t max_size, ssize_t *length); +int sys_fallocate(int fd, off_t offset, size_t size); int sys_unlink(const char *path); int sys_socket(int family, int type, int protocol, int *fd); gid_t sys_getgid(); diff --git a/options/posix/generic/fcntl-stubs.cpp b/options/posix/generic/fcntl-stubs.cpp index beff0939..510dfcc6 100644 --- a/options/posix/generic/fcntl-stubs.cpp +++ b/options/posix/generic/fcntl-stubs.cpp @@ -1,9 +1,8 @@ -#include - -#include - +#include #include +#include +#include #include @@ -28,9 +27,24 @@ int posix_fadvise(int, off_t, off_t, int) { __ensure(!"Not implemented"); __builtin_unreachable(); } -int posix_fallocate(int, off_t, off_t) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +int posix_fallocate(int fd, off_t offset, off_t size) { + struct error_guard { + error_guard() + : _s{errno} { } + + ~error_guard() { + errno = _s; + } + + private: + int _s; + }; + + error_guard guard; + + if(mlibc::sys_fallocate(fd, offset, size)) + return errno; + return 0; } // This is a linux extension diff --git a/sysdeps/managarm/generic/file.cpp b/sysdeps/managarm/generic/file.cpp index 55b21dee..eed28aac 100644 --- a/sysdeps/managarm/generic/file.cpp +++ b/sysdeps/managarm/generic/file.cpp @@ -1938,6 +1938,46 @@ int sys_readlink(const char *path, void *data, size_t max_size, ssize_t *length) } } +int sys_fallocate(int fd, off_t offset, size_t size) { + HelAction actions[3]; + globalQueue.trim(); + + auto handle = cacheFileTable()[fd]; + __ensure(handle); + + managarm::fs::CntRequest req(getAllocator()); + req.set_req_type(managarm::fs::CntReqType::PT_FALLOCATE); + req.set_rel_offset(offset); + req.set_size(size); + + frigg::String ser(getAllocator()); + req.SerializeToString(&ser); + actions[0].type = kHelActionOffer; + actions[0].flags = kHelItemAncillary; + actions[1].type = kHelActionSendFromBuffer; + actions[1].flags = kHelItemChain; + actions[1].buffer = ser.data(); + actions[1].length = ser.size(); + actions[2].type = kHelActionRecvInline; + actions[2].flags = 0; + HEL_CHECK(helSubmitAsync(handle, actions, 3, + globalQueue.getQueue(), 0, 0)); + + auto element = globalQueue.dequeueSingle(); + auto offer = parseSimple(element); + auto send_req = parseSimple(element); + auto recv_resp = parseInline(element); + + HEL_CHECK(offer->error); + HEL_CHECK(send_req->error); + HEL_CHECK(recv_resp->error); + + managarm::fs::SvrResponse resp(getAllocator()); + resp.ParseFromArray(recv_resp->data, recv_resp->length); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + return 0; +} + int sys_unlink(const char *path) { HelAction actions[3];