diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-27 00:14:24 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-27 00:14:24 +0200 |
commit | 2716a9e2d7ceb92e66ed486b4d6797caf8be4efd (patch) | |
tree | 63ad0a1966118432fe3146814ef7a530d8922af0 | |
parent | c928b062180b10a9908325727e18483e029edada (diff) | |
download | serenity-2716a9e2d7ceb92e66ed486b4d6797caf8be4efd.zip |
Greatly improve /proc/PID/stack by tracing the ebp frame chain.
I also added a generator cache to FileHandle. This way, multiple
reads to a generated file (i.e in a synthfs) can transparently
handle multiple calls to read() without the contents changing
between calls.
The cache is discarded at EOF (or when the FileHandle is destroyed.)
-rw-r--r-- | AK/ByteBuffer.h | 5 | ||||
-rw-r--r-- | Kernel/ProcFileSystem.cpp | 211 | ||||
-rw-r--r-- | Kernel/StdLib.h | 10 | ||||
-rw-r--r-- | Kernel/Task.cpp | 20 | ||||
-rw-r--r-- | Kernel/Task.h | 4 | ||||
-rw-r--r-- | Kernel/_fs_contents | bin | 1024000 -> 1024000 bytes | |||
-rw-r--r-- | Kernel/i386.cpp | 6 | ||||
-rw-r--r-- | Kernel/init.cpp | 11 | ||||
-rw-r--r-- | Kernel/kmalloc.cpp | 5 | ||||
-rw-r--r-- | Kernel/kmalloc.h | 2 | ||||
-rw-r--r-- | Kernel/system.h | 1 | ||||
-rw-r--r-- | Kernel/types.h | 4 | ||||
-rw-r--r-- | Userland/cat.cpp | 2 | ||||
-rw-r--r-- | VirtualFileSystem/Ext2FileSystem.cpp | 3 | ||||
-rw-r--r-- | VirtualFileSystem/Ext2FileSystem.h | 2 | ||||
-rw-r--r-- | VirtualFileSystem/FileHandle.cpp | 4 | ||||
-rw-r--r-- | VirtualFileSystem/FileHandle.h | 4 | ||||
-rw-r--r-- | VirtualFileSystem/FileSystem.cpp | 4 | ||||
-rw-r--r-- | VirtualFileSystem/FileSystem.h | 6 | ||||
-rw-r--r-- | VirtualFileSystem/InodeIdentifier.cpp | 2 | ||||
-rw-r--r-- | VirtualFileSystem/SyntheticFileSystem.cpp | 16 | ||||
-rw-r--r-- | VirtualFileSystem/SyntheticFileSystem.h | 2 |
22 files changed, 209 insertions, 115 deletions
diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index 3147d90534..c576ad2b74 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -24,6 +24,11 @@ public: m_impl = move(other.m_impl); return *this; } + ByteBuffer& operator=(const ByteBuffer& other) + { + m_impl = other.m_impl.copyRef(); + return *this; + } static ByteBuffer createEmpty() { return ByteBuffer(Buffer<byte>::createUninitialized(0)); } static ByteBuffer createUninitialized(size_t size) { return ByteBuffer(Buffer<byte>::createUninitialized(size)); } diff --git a/Kernel/ProcFileSystem.cpp b/Kernel/ProcFileSystem.cpp index 9560faad69..a763549e11 100644 --- a/Kernel/ProcFileSystem.cpp +++ b/Kernel/ProcFileSystem.cpp @@ -2,6 +2,7 @@ #include "Task.h" #include <VirtualFileSystem/VirtualFileSystem.h> #include "system.h" +#include "MemoryManager.h" static ProcFileSystem* s_the; @@ -25,6 +26,64 @@ ProcFileSystem::~ProcFileSystem() { } +ByteBuffer procfs$pid_vm(const Task& task) +{ + InterruptDisabler disabler; + char* buffer; + auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer); + memset(buffer, 0, stringImpl->length()); + char* ptr = buffer; + ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n"); + for (auto& region : task.regions()) { + ptr += ksprintf(ptr, "%x -- %x %x %s\n", + region->linearAddress.get(), + region->linearAddress.offset(region->size - 1).get(), + region->size, + region->name.characters()); + } + *ptr = '\0'; + return ByteBuffer::copy((byte*)buffer, ptr - buffer); +} + +ByteBuffer procfs$pid_stack(Task& task) +{ + InterruptDisabler disabler; + if (current != &task) { + MemoryManager::the().unmapRegionsForTask(*current); + MemoryManager::the().mapRegionsForTask(task); + } + struct RecognizedSymbol { + dword address; + const KSym* ksym; + }; + Vector<RecognizedSymbol> recognizedSymbols; + if (auto* eipKsym = ksymbolicate(task.tss().eip)) + recognizedSymbols.append({ task.tss().eip, eipKsym }); + for (dword* stackPtr = (dword*)task.framePtr(); task.isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) { + dword retaddr = stackPtr[1]; + if (auto* ksym = ksymbolicate(retaddr)) + recognizedSymbols.append({ retaddr, ksym }); + } + size_t bytesNeeded = 0; + for (auto& symbol : recognizedSymbols) { + bytesNeeded += symbol.ksym->name.length() + 8 + 16; + } + auto buffer = ByteBuffer::createUninitialized(bytesNeeded); + char* bufptr = (char*)buffer.pointer(); + + for (auto& symbol : recognizedSymbols) { + // FIXME: This doesn't actually create a file! + unsigned offset = symbol.address - symbol.ksym->address; + bufptr += ksprintf(bufptr, "%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset); + } + buffer.trim(bufptr - (char*)buffer.pointer()); + if (current != &task) { + MemoryManager::the().unmapRegionsForTask(task); + MemoryManager::the().mapRegionsForTask(*current); + } + return buffer; +} + void ProcFileSystem::addProcess(Task& task) { ASSERT_INTERRUPTS_DISABLED(); @@ -32,58 +91,8 @@ void ProcFileSystem::addProcess(Task& task) ksprintf(buf, "%d", task.pid()); auto dir = addFile(createDirectory(buf)); m_pid2inode.set(task.pid(), dir.index()); - addFile(createGeneratedFile("vm", [&task] { - InterruptDisabler disabler; - char* buffer; - auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer); - memset(buffer, 0, stringImpl->length()); - char* ptr = buffer; - ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n"); - for (auto& region : task.regions()) { - ptr += ksprintf(ptr, "%x -- %x %x %s\n", - region->linearAddress.get(), - region->linearAddress.offset(region->size - 1).get(), - region->size, - region->name.characters()); - } - *ptr = '\0'; - return ByteBuffer::copy((byte*)buffer, ptr - buffer); - }), dir.index()); - addFile(createGeneratedFile("stack", [&task] { - InterruptDisabler disabler; - auto& syms = ksyms(); - dword firstKsymAddress = syms.first().address; - dword lastKsymAddress = syms.last().address; - struct RecognizedSymbol { - dword address; - const char* name; - dword offset; - }; - Vector<RecognizedSymbol> recognizedSymbols; - size_t bytesNeeded = 0; - for (dword* stackPtr = (dword*)task.stackPtr(); (dword)stackPtr < task.stackTop(); ++stackPtr) { - if (*stackPtr < firstKsymAddress || *stackPtr > lastKsymAddress) - continue; - const char* name = nullptr; - unsigned offset = 0; - for (unsigned i = 0; i < syms.size(); ++i) { - if (*stackPtr < syms[i+1].address) { - name = syms[i].name.characters(); - offset = *stackPtr - syms[i].address; - bytesNeeded += syms[i].name.length() + 8 + 16; - break; - } - } - recognizedSymbols.append({ *stackPtr, name, offset }); - } - auto buffer = ByteBuffer::createUninitialized(bytesNeeded); - char* ptr = (char*)buffer.pointer(); - for (auto& symbol : recognizedSymbols) { - kprintf("%p %s +%u\n", symbol.address, symbol.name, symbol.offset); - } - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - }), dir.index()); + addFile(createGeneratedFile("vm", [&task] { return procfs$pid_vm(task); }), dir.index()); + addFile(createGeneratedFile("stack", [&task] { return procfs$pid_stack(task); }), dir.index()); } void ProcFileSystem::removeProcess(Task& task) @@ -97,56 +106,62 @@ void ProcFileSystem::removeProcess(Task& task) m_pid2inode.remove(pid); } -bool ProcFileSystem::initialize() +ByteBuffer procfs$mounts() { - SyntheticFileSystem::initialize(); + InterruptDisabler disabler; + auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80); + char* ptr = (char*)buffer.pointer(); + VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) { + auto& fs = mount.fileSystem(); + ptr += ksprintf(ptr, "%s @ ", fs.className()); + if (!mount.host().isValid()) + ptr += ksprintf(ptr, "/\n", fs.className()); + else + ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index()); + }); + buffer.trim(ptr - (char*)buffer.pointer()); + return buffer; +} - addFile(createGeneratedFile("mounts", [] { - InterruptDisabler disabler; - auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80); - char* ptr = (char*)buffer.pointer(); - VirtualFileSystem::the().forEachMount([&ptr] (auto& mount) { - auto& fs = mount.fileSystem(); - ptr += ksprintf(ptr, "%s @ ", fs.className()); - if (!mount.host().isValid()) - ptr += ksprintf(ptr, "/\n", fs.className()); - else - ptr += ksprintf(ptr, "%u:%u\n", mount.host().fileSystemID(), mount.host().index()); - }); - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - })); +ByteBuffer procfs$kmalloc() +{ + InterruptDisabler disabler; + auto buffer = ByteBuffer::createUninitialized(128); + char* ptr = (char*)buffer.pointer(); + ptr += ksprintf(ptr, "alloc: %u\nfree: %u\n", sum_alloc, sum_free); + buffer.trim(ptr - (char*)buffer.pointer()); + return buffer; +} - addFile(createGeneratedFile("kmalloc", [] { - InterruptDisabler disabler; - auto buffer = ByteBuffer::createUninitialized(128); - char* ptr = (char*)buffer.pointer(); - ptr += ksprintf(ptr, "alloc: %u\nfree: %u\n", sum_alloc, sum_free); - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - })); +ByteBuffer procfs$summary() +{ + InterruptDisabler disabler; + auto tasks = Task::allTasks(); + auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256); + char* ptr = (char*)buffer.pointer(); + ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS NAME\n"); + for (auto* task : tasks) { + ptr += ksprintf(ptr, "%w %w:%w %b %w %x %w %s\n", + task->pid(), + task->uid(), + task->gid(), + task->state(), + task->parentPID(), + task->timesScheduled(), + task->fileHandleCount(), + task->name().characters()); + } + *ptr = '\0'; + buffer.trim(ptr - (char*)buffer.pointer()); + return buffer; +} - addFile(createGeneratedFile("summary", [] { - InterruptDisabler disabler; - auto tasks = Task::allTasks(); - auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256); - char* ptr = (char*)buffer.pointer(); - ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS NAME\n"); - for (auto* task : tasks) { - ptr += ksprintf(ptr, "%w %w:%w %b %w %x %w %s\n", - task->pid(), - task->uid(), - task->gid(), - task->state(), - task->parentPID(), - task->timesScheduled(), - task->fileHandleCount(), - task->name().characters()); - } - *ptr = '\0'; - buffer.trim(ptr - (char*)buffer.pointer()); - return buffer; - })); +bool ProcFileSystem::initialize() +{ + SyntheticFileSystem::initialize(); + addFile(createGeneratedFile("mounts", procfs$mounts)); + addFile(createGeneratedFile("kmalloc", procfs$kmalloc)); + addFile(createGeneratedFile("summary", procfs$summary)); return true; } diff --git a/Kernel/StdLib.h b/Kernel/StdLib.h index f507773e55..ee7cd18c33 100644 --- a/Kernel/StdLib.h +++ b/Kernel/StdLib.h @@ -2,7 +2,17 @@ #include "types.h" +#if 0 +inline void memcpy(void *dest, const void *src, DWORD n) +{ + BYTE* bdest = (BYTE*)dest; + const BYTE* bsrc = (const BYTE*)src; + for (; n; --n) + *(bdest++) = *(bsrc++); +} +#else void memcpy(void*, const void*, DWORD); +#endif void strcpy(char*, const char*); int strcmp(char const*, const char*); DWORD strlen(const char*); diff --git a/Kernel/Task.cpp b/Kernel/Task.cpp index 79fd9d7c37..91fffcd15b 100644 --- a/Kernel/Task.cpp +++ b/Kernel/Task.cpp @@ -885,3 +885,23 @@ Task::Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n) Task::Region::~Region() { } + +bool Task::isValidAddressForKernel(LinearAddress laddr) const +{ + InterruptDisabler disabler; + if (laddr.get() >= ksyms().first().address && laddr.get() <= ksyms().last().address) + return true; + if (is_kmalloc_address((void*)laddr.get())) + return true; + return isValidAddressForUser(laddr); +} + +bool Task::isValidAddressForUser(LinearAddress laddr) const +{ + InterruptDisabler disabler; + for (auto& region: m_regions) { + if (laddr >= region->linearAddress && laddr < region->linearAddress.offset(region->size)) + return true; + } + return false; +} diff --git a/Kernel/Task.h b/Kernel/Task.h index dd06be7022..957b28699e 100644 --- a/Kernel/Task.h +++ b/Kernel/Task.h @@ -122,9 +122,13 @@ public: size_t fileHandleCount() const { return m_fileHandles.size(); } + dword framePtr() const { return m_tss.ebp; } dword stackPtr() const { return m_tss.esp; } dword stackTop() const { return m_tss.ss == 0x10 ? m_stackTop0 : m_stackTop3; } + bool isValidAddressForKernel(LinearAddress) const; + bool isValidAddressForUser(LinearAddress) const; + private: friend class MemoryManager; friend bool scheduleNewTask(); diff --git a/Kernel/_fs_contents b/Kernel/_fs_contents Binary files differindex cff5e6f491..5971aa3cec 100644 --- a/Kernel/_fs_contents +++ b/Kernel/_fs_contents diff --git a/Kernel/i386.cpp b/Kernel/i386.cpp index 047212f877..7bca2f0b93 100644 --- a/Kernel/i386.cpp +++ b/Kernel/i386.cpp @@ -196,10 +196,10 @@ void exception_14_handler() asm ("movl %%cr2, %%eax":"=a"(faultAddress)); auto& regs = *reinterpret_cast<RegisterDump*>(exception_state_dump); - kprintf("%s page fault: %u(%s), %s laddr=%p\n", - current->isRing0() ? "Kernel" : "User", - current->pid(), + kprintf("Ring%u page fault in %s(%u), %s laddr=%p\n", + regs.cs & 3, current->name().characters(), + current->pid(), exception_code & 2 ? "write" : "read", faultAddress); diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 8368e071c9..7e220a6ced 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -65,6 +65,17 @@ Vector<KSym>& ksyms() return *s_ksyms; } +const KSym* ksymbolicate(dword address) +{ + if (address < ksyms().first().address || address > ksyms().last().address) + return nullptr; + for (unsigned i = 0; i < ksyms().size(); ++i) { + if (address < ksyms()[i + 1].address) + return &ksyms()[i]; + } + return nullptr; +} + static void loadKernelMap(const ByteBuffer& buffer) { s_ksyms = new Vector<KSym>; diff --git a/Kernel/kmalloc.cpp b/Kernel/kmalloc.cpp index 082307ac3b..fbb56569c6 100644 --- a/Kernel/kmalloc.cpp +++ b/Kernel/kmalloc.cpp @@ -29,6 +29,11 @@ PRIVATE BYTE alloc_map[POOL_SIZE / CHUNK_SIZE / 8]; volatile DWORD sum_alloc = 0; volatile DWORD sum_free = POOL_SIZE; +bool is_kmalloc_address(void* ptr) +{ + return ptr >= (void*)BASE_PHYS && ptr <= ((void*)BASE_PHYS + POOL_SIZE); +} + PUBLIC void kmalloc_init() { diff --git a/Kernel/kmalloc.h b/Kernel/kmalloc.h index 3862b68aef..e0b5ac4955 100644 --- a/Kernel/kmalloc.h +++ b/Kernel/kmalloc.h @@ -4,6 +4,8 @@ void kmalloc_init(); void *kmalloc(DWORD size) __attribute__ ((malloc)); void kfree(void*); +bool is_kmalloc_address(void*); + extern volatile DWORD sum_alloc; extern volatile DWORD sum_free; diff --git a/Kernel/system.h b/Kernel/system.h index 03de208bbb..0839f28d45 100644 --- a/Kernel/system.h +++ b/Kernel/system.h @@ -10,6 +10,7 @@ struct KSym { }; Vector<KSym>& ksyms() PURE; +const KSym* ksymbolicate(dword address) PURE; struct system_t { diff --git a/Kernel/types.h b/Kernel/types.h index 63f2a22846..c7e135fbfb 100644 --- a/Kernel/types.h +++ b/Kernel/types.h @@ -74,6 +74,10 @@ public: void set(dword address) { m_address = address; } void mask(dword m) { m_address &= m; } + bool operator<=(const LinearAddress& other) const { return m_address <= other.m_address; } + bool operator>=(const LinearAddress& other) const { return m_address >= other.m_address; } + bool operator>(const LinearAddress& other) const { return m_address > other.m_address; } + bool operator<(const LinearAddress& other) const { return m_address < other.m_address; } bool operator==(const LinearAddress& other) const { return m_address == other.m_address; } byte* asPtr() { return reinterpret_cast<byte*>(m_address); } diff --git a/Userland/cat.cpp b/Userland/cat.cpp index c6c11b4127..fd0044a7fe 100644 --- a/Userland/cat.cpp +++ b/Userland/cat.cpp @@ -15,7 +15,7 @@ int main(int argc, char** argv) return 1; } for (;;) { - char buf[4096]; + char buf[1024]; ssize_t nread = read(fd, buf, sizeof(buf)); if (nread == 0) break; diff --git a/VirtualFileSystem/Ext2FileSystem.cpp b/VirtualFileSystem/Ext2FileSystem.cpp index 1b79224488..9314722a5f 100644 --- a/VirtualFileSystem/Ext2FileSystem.cpp +++ b/VirtualFileSystem/Ext2FileSystem.cpp @@ -268,7 +268,7 @@ Vector<unsigned> Ext2FileSystem::blockListForInode(const ext2_inode& e2inode) co return list; } -Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer) const +Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const { ASSERT(offset >= 0); ASSERT(inode.fileSystemID() == id()); @@ -293,6 +293,7 @@ Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t static const unsigned maxInlineSymlinkLength = 60; if (isSymbolicLink(e2inode->i_mode) && e2inode->i_size < maxInlineSymlinkLength) { Unix::ssize_t nread = min((Unix::off_t)e2inode->i_size - offset, static_cast<Unix::off_t>(count)); + kprintf("nread = %d\n", nread); memcpy(buffer, e2inode->i_block + offset, nread); return nread; } diff --git a/VirtualFileSystem/Ext2FileSystem.h b/VirtualFileSystem/Ext2FileSystem.h index f7b73465ef..a0f8a74d3e 100644 --- a/VirtualFileSystem/Ext2FileSystem.h +++ b/VirtualFileSystem/Ext2FileSystem.h @@ -44,7 +44,7 @@ private: virtual InodeMetadata inodeMetadata(InodeIdentifier) const override; virtual bool setModificationTime(InodeIdentifier, dword timestamp) override; virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override; - virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const override; + virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const override; virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override; bool isDirectoryInode(unsigned) const; diff --git a/VirtualFileSystem/FileHandle.cpp b/VirtualFileSystem/FileHandle.cpp index c1a67dfe25..4e61f1b172 100644 --- a/VirtualFileSystem/FileHandle.cpp +++ b/VirtualFileSystem/FileHandle.cpp @@ -102,7 +102,7 @@ Unix::ssize_t FileHandle::read(byte* buffer, Unix::size_t count) // FIXME: What should happen to m_currentOffset? return m_vnode->characterDevice()->read(buffer, count); } - Unix::ssize_t nread = m_vnode->fileSystem()->readInodeBytes(m_vnode->inode, m_currentOffset, count, buffer); + Unix::ssize_t nread = m_vnode->fileSystem()->readInodeBytes(m_vnode->inode, m_currentOffset, count, buffer, this); m_currentOffset += nread; return nread; } @@ -125,7 +125,7 @@ ByteBuffer FileHandle::readEntireFile() return buffer; } - return m_vnode->fileSystem()->readEntireInode(m_vnode->inode); + return m_vnode->fileSystem()->readEntireInode(m_vnode->inode, this); } bool FileHandle::isDirectory() const diff --git a/VirtualFileSystem/FileHandle.h b/VirtualFileSystem/FileHandle.h index 9bbabdcf0c..cc3cc4ee0a 100644 --- a/VirtualFileSystem/FileHandle.h +++ b/VirtualFileSystem/FileHandle.h @@ -32,6 +32,8 @@ public: void setBlocking(bool b) { m_isBlocking = b; } #endif + ByteBuffer& generatorCache() { return m_generatorCache; } + private: friend class VirtualFileSystem; @@ -39,6 +41,8 @@ private: Unix::off_t m_currentOffset { 0 }; + ByteBuffer m_generatorCache; + #ifdef SERENITY int m_fd { -1 }; bool m_isBlocking { true }; diff --git a/VirtualFileSystem/FileSystem.cpp b/VirtualFileSystem/FileSystem.cpp index e8b774e767..e5529b5168 100644 --- a/VirtualFileSystem/FileSystem.cpp +++ b/VirtualFileSystem/FileSystem.cpp @@ -50,7 +50,7 @@ InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, return foundInode; } -ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const +ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode, FileHandle* handle) const { ASSERT(inode.fileSystemID() == id()); @@ -67,7 +67,7 @@ ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const byte* out = contents.pointer(); Unix::off_t offset = 0; for (;;) { - nread = readInodeBytes(inode, offset, sizeof(buffer), buffer); + nread = readInodeBytes(inode, offset, sizeof(buffer), buffer, handle); if (nread <= 0) break; memcpy(out, buffer, nread); diff --git a/VirtualFileSystem/FileSystem.h b/VirtualFileSystem/FileSystem.h index a32e8c4077..c6e345d4aa 100644 --- a/VirtualFileSystem/FileSystem.h +++ b/VirtualFileSystem/FileSystem.h @@ -16,6 +16,8 @@ static const dword mepoch = 476763780; +class FileHandle; + class FileSystem : public Retainable<FileSystem> { public: static void initializeGlobals(); @@ -30,7 +32,7 @@ public: virtual bool writeInode(InodeIdentifier, const ByteBuffer&) = 0; virtual InodeMetadata inodeMetadata(InodeIdentifier) const = 0; - virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const = 0; + virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const = 0; struct DirectoryEntry { String name; @@ -44,7 +46,7 @@ public: virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) = 0; InodeIdentifier childOfDirectoryInodeWithName(InodeIdentifier, const String& name) const; - ByteBuffer readEntireInode(InodeIdentifier) const; + ByteBuffer readEntireInode(InodeIdentifier, FileHandle* = nullptr) const; protected: FileSystem(); diff --git a/VirtualFileSystem/InodeIdentifier.cpp b/VirtualFileSystem/InodeIdentifier.cpp index 45aa2dd1ca..f89e7ef8ef 100644 --- a/VirtualFileSystem/InodeIdentifier.cpp +++ b/VirtualFileSystem/InodeIdentifier.cpp @@ -5,5 +5,5 @@ ByteBuffer InodeIdentifier::readEntireFile() const { if (!fileSystem()) return { }; - return fileSystem()->readEntireInode(*this); + return fileSystem()->readEntireInode(*this, nullptr); } diff --git a/VirtualFileSystem/SyntheticFileSystem.cpp b/VirtualFileSystem/SyntheticFileSystem.cpp index f36deeed9c..51f48c8c9c 100644 --- a/VirtualFileSystem/SyntheticFileSystem.cpp +++ b/VirtualFileSystem/SyntheticFileSystem.cpp @@ -1,4 +1,5 @@ #include "SyntheticFileSystem.h" +#include "FileHandle.h" #include <AK/StdLib.h> //#define SYNTHFS_DEBUG @@ -188,7 +189,7 @@ bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&) return false; } -Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer) const +Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle* handle) const { InterruptDisabler disabler; @@ -204,12 +205,21 @@ Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::o return false; const File& file = *(*it).value; ByteBuffer generatedData; - if (file.generator) - generatedData = file.generator(); + if (file.generator) { + if (!handle) { + generatedData = file.generator(); + } else { + if (!handle->generatorCache()) + handle->generatorCache() = file.generator(); + generatedData = handle->generatorCache(); + } + } auto* data = generatedData ? &generatedData : &file.data; Unix::ssize_t nread = min(static_cast<Unix::off_t>(data->size() - offset), static_cast<Unix::off_t>(count)); memcpy(buffer, data->pointer() + offset, nread); + if (nread == 0 && handle && handle->generatorCache()) + handle->generatorCache().clear(); return nread; } diff --git a/VirtualFileSystem/SyntheticFileSystem.h b/VirtualFileSystem/SyntheticFileSystem.h index dc129caf56..2831d6d239 100644 --- a/VirtualFileSystem/SyntheticFileSystem.h +++ b/VirtualFileSystem/SyntheticFileSystem.h @@ -17,7 +17,7 @@ public: virtual InodeMetadata inodeMetadata(InodeIdentifier) const override; virtual bool setModificationTime(InodeIdentifier, dword timestamp) override; virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override; - virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const override; + virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle*) const override; virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override; protected: |