Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions kernel/arch/aarch64/syscall/linux_syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

namespace syscall::linux_nr {

constexpr uint64_t FCNTL = 25;
constexpr uint64_t IOCTL = 29;
constexpr uint64_t UNLINKAT = 35;
constexpr uint64_t FTRUNCATE = 46;
constexpr uint64_t OPENAT = 56;
constexpr uint64_t CLOSE = 57;
constexpr uint64_t READ = 63;
constexpr uint64_t WRITE = 64;
constexpr uint64_t CLOSE = 57;
constexpr uint64_t IOCTL = 29;
constexpr uint64_t WRITEV = 66;
constexpr uint64_t OPENAT = 56;
constexpr uint64_t MUNMAP = 215;
constexpr uint64_t MMAP = 222;
constexpr uint64_t MPROTECT = 226;
constexpr uint64_t EXIT = 93;
constexpr uint64_t EXIT_GROUP = 94;
constexpr uint64_t SET_TID_ADDRESS = 96;
Expand All @@ -23,7 +23,10 @@ constexpr uint64_t BIND = 200;
constexpr uint64_t LISTEN = 201;
constexpr uint64_t ACCEPT = 202;
constexpr uint64_t CONNECT = 203;
constexpr uint64_t FCNTL = 25;
constexpr uint64_t MUNMAP = 215;
constexpr uint64_t MMAP = 222;
constexpr uint64_t MPROTECT = 226;
constexpr uint64_t MEMFD_CREATE = 279;

} // namespace syscall::linux_nr

Expand Down
17 changes: 10 additions & 7 deletions kernel/arch/x86_64/syscall/linux_syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ constexpr uint64_t READ = 0;
constexpr uint64_t WRITE = 1;
constexpr uint64_t OPEN = 2;
constexpr uint64_t CLOSE = 3;
constexpr uint64_t IOCTL = 16;
constexpr uint64_t WRITEV = 20;
constexpr uint64_t MMAP = 9;
constexpr uint64_t MPROTECT = 10;
constexpr uint64_t MUNMAP = 11;
constexpr uint64_t OPENAT = 257;
constexpr uint64_t EXIT = 60;
constexpr uint64_t ARCH_PRCTL = 158;
constexpr uint64_t SET_TID_ADDRESS = 218;
constexpr uint64_t EXIT_GROUP = 231;
constexpr uint64_t IOCTL = 16;
constexpr uint64_t WRITEV = 20;
constexpr uint64_t SOCKET = 41;
constexpr uint64_t CONNECT = 42;
constexpr uint64_t ACCEPT = 43;
constexpr uint64_t BIND = 49;
constexpr uint64_t LISTEN = 50;
constexpr uint64_t SOCKETPAIR = 53;
constexpr uint64_t EXIT = 60;
constexpr uint64_t FCNTL = 72;
constexpr uint64_t FTRUNCATE = 77;
constexpr uint64_t ARCH_PRCTL = 158;
constexpr uint64_t SET_TID_ADDRESS = 218;
constexpr uint64_t EXIT_GROUP = 231;
constexpr uint64_t OPENAT = 257;
constexpr uint64_t UNLINKAT = 263;
constexpr uint64_t MEMFD_CREATE = 319;

} // namespace syscall::linux_nr

Expand Down
1 change: 1 addition & 0 deletions kernel/fs/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ int32_t node::open(file*, uint32_t) { return OK; }
int32_t node::on_close(file*) { return OK; }
int32_t node::readlink(char*, size_t, size_t*) { return ERR_NOSYS; }
int32_t node::create_socket(const char*, size_t, void*, node**) { return ERR_NOSYS; }
int32_t node::truncate(size_t) { return ERR_NOSYS; }

int32_t node::getattr(vattr* attr) {
if (!attr) return ERR_INVAL;
Expand Down
1 change: 1 addition & 0 deletions kernel/fs/fstypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ constexpr uint32_t O_RDONLY = 0;
constexpr uint32_t O_WRONLY = 1;
constexpr uint32_t O_RDWR = 2;
constexpr uint32_t O_CREAT = 0x40;
constexpr uint32_t O_EXCL = 0x80;
constexpr uint32_t O_TRUNC = 0x200;
constexpr uint32_t O_APPEND = 0x400;
constexpr uint32_t O_NONBLOCK = 0x800;
Expand Down
1 change: 1 addition & 0 deletions kernel/fs/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class node : public rc::ref_counted<node> {

// --- Metadata ---
virtual int32_t getattr(vattr* attr);
virtual int32_t truncate(size_t size);

// --- Symlink ---
virtual int32_t readlink(char* buf, size_t size, size_t* out_len);
Expand Down
51 changes: 51 additions & 0 deletions kernel/fs/ramfs/ramfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,55 @@ int32_t file_node::getattr(fs::vattr* attr) {
return fs::OK;
}

int32_t file_node::truncate(size_t size) {
size_t max_alignable = ~(pmm::PAGE_SIZE - 1);
if (size > max_alignable) {
return fs::ERR_INVAL;
}

sync::irq_lock_guard guard(m_lock);

uint32_t needed = static_cast<uint32_t>(
pmm::page_align_up(size) / pmm::PAGE_SIZE);

if (needed > m_page_count) {
int32_t rc = ensure_capacity(needed);
if (rc != fs::OK) {
return rc;
}

for (uint32_t i = m_page_count; i < needed; i++) {
pmm::phys_addr_t phys = pmm::alloc_page();
if (phys == 0) {
m_size = static_cast<size_t>(i) * pmm::PAGE_SIZE;
m_page_count = i;
return fs::ERR_NOMEM;
}
m_pages[i] = static_cast<uint8_t*>(paging::phys_to_virt(phys));
string::memset(m_pages[i], 0, pmm::PAGE_SIZE);
}
m_page_count = needed;
} else if (needed < m_page_count) {
for (uint32_t i = needed; i < m_page_count; i++) {
if (m_pages[i]) {
pmm::phys_addr_t phys =
reinterpret_cast<uintptr_t>(m_pages[i]) - g_boot_info.hhdm_offset;
pmm::free_page(phys);
m_pages[i] = nullptr;
}
}
m_page_count = needed;
}

if (size < m_size && needed > 0) {
size_t tail_off = size % pmm::PAGE_SIZE;
if (tail_off != 0 && m_pages[needed - 1]) {
string::memset(m_pages[needed - 1] + tail_off, 0, pmm::PAGE_SIZE - tail_off);
}
}

m_size = size;
return fs::OK;
}

} // namespace ramfs
1 change: 1 addition & 0 deletions kernel/fs/ramfs/ramfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class file_node : public fs::node {
ssize_t write(fs::file* f, const void* buf, size_t count) override;
int64_t seek(fs::file* f, int64_t offset, int whence) override;
int32_t getattr(fs::vattr* attr) override;
int32_t truncate(size_t size) override;

private:
int32_t ensure_capacity(uint32_t needed_pages);
Expand Down
Loading