diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index aa9feb1cc..eb613ad2b 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -244,22 +244,6 @@ File* HandleTable::GetFile(const std::filesystem::path& host_name) { return nullptr; } -void HandleTable::CreateStdHandles() { - auto setup = [this](const char* path, auto* device) { - int fd = CreateHandle(); - auto* file = GetFile(fd); - file->is_opened = true; - file->type = FileType::Device; - file->m_guest_name = path; - file->device = - std::shared_ptr{reinterpret_cast(device)}; - }; - // order matters - setup("/dev/stdin", new Devices::Logger("stdin", false)); // stdin - setup("/dev/stdout", new Devices::Logger("stdout", false)); // stdout - setup("/dev/stderr", new Devices::Logger("stderr", true)); // stderr -} - int HandleTable::GetFileDescriptor(File* file) { std::scoped_lock lock{m_mutex}; auto it = std::find(m_files.begin(), m_files.end(), file); diff --git a/src/core/file_sys/hostio/src/host_io_base.cpp b/src/core/file_sys/hostio/src/host_io_base.cpp index 054416853..3be8eccb6 100644 --- a/src/core/file_sys/hostio/src/host_io_base.cpp +++ b/src/core/file_sys/hostio/src/host_io_base.cpp @@ -2,9 +2,14 @@ #include -#include "../../quasi_log.h" +#include "common/logging/log.h" + #include "host_io_base.h" +#define STUB() \ + LOG_ERROR(Kernel_Fs, "Stub called in HostIO_Base: {}:{}", __FILE__, __LINE__); \ + return -38; + namespace HostIODriver { HostIO_Base::HostIO_Base() = default; diff --git a/src/core/file_sys/hostio/src/host_io_virtual.cpp b/src/core/file_sys/hostio/src/host_io_virtual.cpp index 0f1380d32..ba8d37a83 100644 --- a/src/core/file_sys/hostio/src/host_io_virtual.cpp +++ b/src/core/file_sys/hostio/src/host_io_virtual.cpp @@ -3,9 +3,10 @@ #include #include -#include "../../quasifs/quasi_sys_fcntl.h" +#include "common/logging/log.h" #include "../../quasifs/quasi_errno.h" +#include "../../quasifs/quasi_sys_fcntl.h" #include "../../quasifs/quasi_types.h" #include "../../quasifs/quasifs_inode_quasi_device.h" @@ -18,8 +19,6 @@ #include "../host_io_virtual.h" #include "host_io_base.h" -#include "../../quasi_log.h" - namespace HostIODriver { HostIO_Virtual::HostIO_Virtual() = default; @@ -286,7 +285,7 @@ int HostIO_Virtual::RMDir(const fs::path& path) { auto target_nlink = res->node->st.st_nlink; if (target_nlink != 0) { - LogError("RMDir'd directory nlink is not 0!", "(is ", target_nlink, ")"); + LOG_ERROR(Kernel_Fs, "RMDir'd directory nlink is not 0!", "(is ", target_nlink, ")"); return -QUASI_ENOTEMPTY; } diff --git a/src/core/file_sys/quasi_log.h b/src/core/file_sys/quasi_log2.h similarity index 100% rename from src/core/file_sys/quasi_log.h rename to src/core/file_sys/quasi_log2.h diff --git a/src/core/file_sys/quasifs/quasifs_inode_quasi_directory.h b/src/core/file_sys/quasifs/quasifs_inode_quasi_directory.h index 9b9a44561..e7745c7c1 100644 --- a/src/core/file_sys/quasifs/quasifs_inode_quasi_directory.h +++ b/src/core/file_sys/quasifs/quasifs_inode_quasi_directory.h @@ -27,7 +27,7 @@ public: template static file_ptr Create(Args&&... args) { - if constexpr (std::is_base_of_v) + if constexpr (std::is_base_of_v) return std::make_shared(std::forward(args)...); UNREACHABLE(); } diff --git a/src/core/file_sys/quasifs/src/quasifs.cpp b/src/core/file_sys/quasifs/src/quasifs.cpp index 7ee54d9a3..93829d108 100644 --- a/src/core/file_sys/quasifs/src/quasifs.cpp +++ b/src/core/file_sys/quasifs/src/quasifs.cpp @@ -1,5 +1,7 @@ // INAA License @marecl 2025 +#include "common/logging/log.h" + #include "../quasi_errno.h" #include "../quasi_types.h" @@ -11,8 +13,6 @@ #include "core/file_sys/quasifs/quasifs_inode_virtualfile.h" #include "core/file_sys/quasifs/quasifs_partition.h" -#include "../../quasi_log.h" - namespace QuasiFS { std::string file_mode(u16 mode) { std::string s; @@ -66,19 +66,16 @@ void _printTree(const inode_ptr& node, const std::string& name, int depth) { char timebuf[64]; std::tm* t = std::localtime(&st.st_mtime); std::strftime(timebuf, sizeof(timebuf), "%EY-%m-%d %H:%M", t); - // TODO: UID/GID - std::cout << "[ls -la] " - << std::format("{} {:08} {:03d} {}:{} {:>08} {}\t{}{}\n", file_mode(st.st_mode), - st.st_mode, st.st_nlink, /*st.st_uid*/ 0, /* st.st_gid*/ 0, - st.st_size, timebuf, depEnt, name); + LOG_INFO(Kernel_Fs, "[ls -la] {} {:08} {:03d} {}:{} {:>08} {}\t{}{}\n", + file_mode(st.st_mode), st.st_mode, st.st_nlink, /*st.st_uid*/ 0, /* st.st_gid*/ 0, + st.st_size, timebuf, depEnt, name); } else depth--; if (node->is_link()) - std::cout << "[ls -la] " - << std::format("\t\t\t\t\t\t\tsymlinked to ->{}\n", - std::static_pointer_cast(node)->follow().string()); + LOG_INFO(Kernel_Fs, "[ls -la]\t\t\t\t\t\t\tsymlinked to ->{}\n", + std::static_pointer_cast(node)->follow().string()); if (node->is_dir()) { if ("." == name) @@ -88,8 +85,7 @@ void _printTree(const inode_ptr& node, const std::string& name, int depth) { auto dir = std::dynamic_pointer_cast(node); if (dir->mounted_root) { - std::cout << "[ls -la] " - << std::format("\t\t\t\t\t\t\t|--{}{}\n", depEnt, "[MOUNTPOINT]"); + LOG_INFO(Kernel_Fs, "[ls -la]\t\t\t\t\t\t\t|--{}{}\n", depEnt, "[MOUNTPOINT]"); _printTree(dir->mounted_root, "", depth + 1); } else { for (auto& [childName, child] : dir->entries) { @@ -155,7 +151,7 @@ int QFS::Mount(const fs::path& path, partition_ptr fs, unsigned int options) { if (options & MountOptions::MOUNT_REMOUNT) { if (nullptr == existing_fs_options) { - LogError("Can't remount {}: Not mounted", path.string()); + LOG_ERROR(Kernel_Fs, "Can't remount {}: Not mounted", path.string()); return -QUASI_EINVAL; } @@ -167,12 +163,12 @@ int QFS::Mount(const fs::path& path, partition_ptr fs, unsigned int options) { dir_ptr dir = std::static_pointer_cast(res.node); if (nullptr != existing_fs_options || dir->mounted_root) { // fs_options exists or there's something (else?) mounted there already - LogError("Can't mount {}: Already mounted", path.string()); + LOG_ERROR(Kernel_Fs, "Can't mount {}: Already mounted", path.string()); return -QUASI_EEXIST; } if (options & MountOptions::MOUNT_BIND) - LogError("Mount --bind not implemented"); + LOG_ERROR(Kernel_Fs, "Mount --bind not implemented"); dir_ptr fs_root = fs->GetRoot(); mount_t fs_options = { @@ -206,7 +202,8 @@ int QFS::Unmount(const fs::path& path) { dir_ptr res_rootdir = std::static_pointer_cast(res.node); if (options_parentdir != res_parentdir) - LogError("Resolved mountpoint has different parent in metadata and in resolution result"); + LOG_ERROR(Kernel_Fs, + "Resolved mountpoint has different parent in metadata and in resolution result"); if (nullptr == res_rootdir) // mounted but rootdir disappeared O.o @@ -254,9 +251,6 @@ int QFS::Resolve(const fs::path& path, Resolved& res) { res.node = this->root; do { - if (iter_path.string().size() >= 256) - return -QUASI_ENAMETOOLONG; - status = res.mountpoint->Resolve(iter_path, res); if (0 != status) @@ -292,7 +286,7 @@ int QFS::Resolve(const fs::path& path, Resolved& res) { if (nullptr != mntparent->mounted_root) { if (mntroot != mntparent->mounted_root) - LogError("Resolved conflicting mount root and node"); + LOG_ERROR(Kernel_Fs, "Resolved conflicting mount root and node"); // just like symlinks, only trailing path is saved // directory, in which partition is mounted, belongs to upstream filesystem, @@ -370,7 +364,8 @@ s64 QFS::GetDirectorySize(const fs::path& path) noexcept { void QFS::SyncHostImpl(partition_ptr part) { fs::path host_path{}; if (0 != part->GetHostPath(host_path)) { - LogError("Cannot safely resolve host directory for blkdev {}", part->GetBlkId()); + LOG_ERROR(Kernel_Fs, "Cannot safely resolve host directory for blkdev {}", + part->GetBlkId()); return; // false } @@ -397,7 +392,8 @@ void QFS::SyncHostImpl(partition_ptr part) { part->Resolve(parent_path, res); if (nullptr == res.node) { - LogError("Cannot resolve quasi-target for sync: {}", parent_path.string()); + LOG_ERROR(Kernel_Fs, "Cannot resolve quasi-target for sync: {}", + parent_path.string()); continue; } @@ -412,18 +408,19 @@ void QFS::SyncHostImpl(partition_ptr part) { new_inode = QuasiFile::Create(); part->touch(parent_dir, leaf, std::static_pointer_cast(new_inode)); } else { - LogError("Unsupported host file type: {}", entry_path.string()); + LOG_ERROR(Kernel_Fs, "Unsupported host file type: {}", entry_path.string()); continue; } if (0 != this->hio_driver.Stat(entry_path, &new_inode->st)) { - LogError("Cannot stat file: {}", entry_path.string()); + LOG_ERROR(Kernel_Fs, "Cannot stat file: {}", entry_path.string()); continue; } } } catch (const std::exception& e) { - std::cerr << "Błąd: " << e.what() << "\n"; + LOG_CRITICAL(Kernel_Fs, "An error occurred when syncing [{}]: {}", host_path.string(), e.what()); } + return; // true } diff --git a/src/core/file_sys/quasifs/src/quasifs_partition.cpp b/src/core/file_sys/quasifs/src/quasifs_partition.cpp index b7fc4a333..92fdc7c5b 100644 --- a/src/core/file_sys/quasifs/src/quasifs_partition.cpp +++ b/src/core/file_sys/quasifs/src/quasifs_partition.cpp @@ -1,5 +1,7 @@ // INAA License @marecl 2025 +#include "common/logging/log.h" + #include "../quasi_errno.h" #include "../quasi_types.h" @@ -8,13 +10,11 @@ #include "core/file_sys/quasifs/quasifs_inode_symlink.h" #include "core/file_sys/quasifs/quasifs_partition.h" -#include "../../quasi_log.h" - namespace QuasiFS { Partition::Partition() : Partition("", 0755, 512, 4096) {} -Partition::Partition(const fs::path& host_root, const int root_permissions, const u32 blocks_per_io, +Partition::Partition(const fs::path& host_root, const int root_permissions, const u32 block_size, const u32 ioblock_size) : block_id(next_block_id++), host_root(host_root.lexically_normal()), block_size(block_size), ioblock_size(ioblock_size) { @@ -46,7 +46,7 @@ int Partition::GetHostPath(fs::path& output_path, const fs::path& local_path) { fs::path host_path_target_sanitized = SanitizePath(host_path_target); if (host_path_target_sanitized.empty()) { - LogError("Malicious path detected: {}", host_path_target.string()); + LOG_ERROR(Kernel_Fs,"Malicious path detected: {}", host_path_target.string()); return -QUASI_EACCES; } output_path = host_path_target_sanitized; @@ -66,6 +66,9 @@ int Partition::Resolve(fs::path& path, Resolved& res) { if (path.is_relative()) return -QUASI_EBADF; + if (path.string().size() >= 256) + return -QUASI_ENAMETOOLONG; + res.mountpoint = shared_from_this(); res.local_path = "/"; res.parent = this->root; diff --git a/src/core/file_sys/quasifs/src/quasifs_vdriver.cpp b/src/core/file_sys/quasifs/src/quasifs_vdriver.cpp index 89dff086a..bbe76212a 100644 --- a/src/core/file_sys/quasifs/src/quasifs_vdriver.cpp +++ b/src/core/file_sys/quasifs/src/quasifs_vdriver.cpp @@ -2,6 +2,8 @@ #include +#include "common/logging/log.h" + #include "../quasi_errno.h" #include "../quasi_sys_fcntl.h" #include "../quasi_types.h" @@ -12,8 +14,6 @@ #include "core/file_sys/quasifs/quasifs_inode_symlink.h" #include "core/file_sys/quasifs/quasifs_partition.h" -#include "../../quasi_log.h" - namespace QuasiFS { int QFS::OperationImpl::Open(const fs::path& path, int flags, u16 mode) { @@ -36,7 +36,7 @@ int QFS::OperationImpl::Open(const fs::path& path, int flags, u16 mode) { bool request_append = flags & QUASI_O_APPEND; // - // Orbis-specific checks + // Orbis-specific checks, or at least ones that differ between Linux and Orbis // Universal checks are embedded into QFS // Some of those are universal, but order matters (a lot) // @@ -56,6 +56,14 @@ int QFS::OperationImpl::Open(const fs::path& path, int flags, u16 mode) { if (flags & QUASI_O_DIRECTORY && flags & QUASI_O_CREAT) return -QUASI_ENOTDIR; + // O_TRUNC | O_RDONLY - throw einval but touch a file + // TODO: find out what happens when can't create file before throwing einval + if ((flags & (QUASI_O_TRUNC | QUASI_O_WRONLY | QUASI_O_RDWR)) == QUASI_O_TRUNC) { + if (int status = this->Close(this->Creat(path, mode)); status != 0) + return status; + return -QUASI_ENOENT; + } + // // Proceed // @@ -82,7 +90,7 @@ int QFS::OperationImpl::Open(const fs::path& path, int flags, u16 mode) { if (int tmp_hio_status = hio_status >= 0 ? 0 : hio_status; host_used && (tmp_hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); if (vio_status < 0) return vio_status; @@ -175,7 +183,7 @@ int QFS::OperationImpl::LinkSymbolic(const fs::path& src, const fs::path& dst) { return hio_status; host_used = true; } else if (dst_part->IsHostMounted() ^ src_part->IsHostMounted()) { - LogError("Symlinks can be only created if both source and destination are host-bound"); + LOG_ERROR(Kernel_Fs,"Symlinks can be only created if both source and destination are host-bound"); return -QUASI_ENOSYS; } @@ -185,7 +193,7 @@ int QFS::OperationImpl::LinkSymbolic(const fs::path& src, const fs::path& dst) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -231,7 +239,7 @@ int QFS::OperationImpl::Link(const fs::path& src, const fs::path& dst) { return hio_status; host_used = true; } else if (dst_part->IsHostMounted() ^ src_part->IsHostMounted()) { - LogError("Links can be only created if both source and destination are host-bound"); + LOG_ERROR(Kernel_Fs,"Links can be only created if both source and destination are host-bound"); return -QUASI_ENOSYS; } @@ -240,7 +248,7 @@ int QFS::OperationImpl::Link(const fs::path& src, const fs::path& dst) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -299,7 +307,7 @@ int QFS::OperationImpl::Unlink(const fs::path& path) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -329,7 +337,7 @@ int QFS::OperationImpl::Flush(const s32 fd) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -359,7 +367,7 @@ int QFS::OperationImpl::FSync(const s32 fd) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; }; @@ -395,7 +403,7 @@ int QFS::OperationImpl::Truncate(const fs::path& path, u64 length) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -427,7 +435,7 @@ int QFS::OperationImpl::FTruncate(const s32 fd, u64 length) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -454,7 +462,7 @@ u64 QFS::OperationImpl::LSeek(const s32 fd, u64 offset, SeekOrigin origin) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; }; @@ -498,7 +506,7 @@ s64 QFS::OperationImpl::Write(const s32 fd, const void* buf, u64 count) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -528,7 +536,7 @@ s64 QFS::OperationImpl::PWrite(const s32 fd, const void* buf, u64 count, u64 off qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; }; @@ -558,7 +566,7 @@ s64 QFS::OperationImpl::Read(const s32 fd, void* buf, u64 count) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -588,7 +596,7 @@ s64 QFS::OperationImpl::PRead(const s32 fd, void* buf, u64 count, u64 offset) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; }; @@ -632,7 +640,7 @@ int QFS::OperationImpl::MKDir(const fs::path& path, u16 mode) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -666,7 +674,7 @@ int QFS::OperationImpl::RMDir(const fs::path& path) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return status; } @@ -721,7 +729,8 @@ int QFS::OperationImpl::Stat(const fs::path& path, Libraries::Kernel::OrbisKerne memcpy(statbuf, &vio_stat, sizeof(Libraries::Kernel::OrbisKernelStat)); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs, "Host returned {}, but virtual driver returned {}", hio_status, + vio_status); return vio_status; } @@ -764,7 +773,7 @@ int QFS::OperationImpl::FStat(const s32 fd, Libraries::Kernel::OrbisKernelStat* memcpy(statbuf, &vio_stat, sizeof(Libraries::Kernel::OrbisKernelStat)); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -801,7 +810,7 @@ int QFS::OperationImpl::Chmod(const fs::path& path, u16 mode) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; } @@ -831,7 +840,7 @@ int QFS::OperationImpl::FChmod(const s32 fd, u16 mode) { qfs.vio_driver.ClearCtx(); if (host_used && (hio_status != vio_status)) - LogError("Host returned {}, but virtual driver returned {}", hio_status, vio_status); + LOG_ERROR(Kernel_Fs,"Host returned {}, but virtual driver returned {}", hio_status, vio_status); return vio_status; }