mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 12:32:43 +00:00 
			
		
		
		
	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.)
This commit is contained in:
		
							parent
							
								
									c928b06218
								
							
						
					
					
						commit
						2716a9e2d7
					
				
					 22 changed files with 210 additions and 116 deletions
				
			
		|  | @ -24,6 +24,11 @@ public: | ||||||
|             m_impl = move(other.m_impl); |             m_impl = move(other.m_impl); | ||||||
|         return *this; |         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 createEmpty() { return ByteBuffer(Buffer<byte>::createUninitialized(0)); } | ||||||
|     static ByteBuffer createUninitialized(size_t size) { return ByteBuffer(Buffer<byte>::createUninitialized(size)); } |     static ByteBuffer createUninitialized(size_t size) { return ByteBuffer(Buffer<byte>::createUninitialized(size)); } | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ | ||||||
| #include "Task.h" | #include "Task.h" | ||||||
| #include <VirtualFileSystem/VirtualFileSystem.h> | #include <VirtualFileSystem/VirtualFileSystem.h> | ||||||
| #include "system.h" | #include "system.h" | ||||||
|  | #include "MemoryManager.h" | ||||||
| 
 | 
 | ||||||
| static ProcFileSystem* s_the; | static ProcFileSystem* s_the; | ||||||
| 
 | 
 | ||||||
|  | @ -25,14 +26,8 @@ ProcFileSystem::~ProcFileSystem() | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void ProcFileSystem::addProcess(Task& task) | ByteBuffer procfs$pid_vm(const Task& task) | ||||||
| { | { | ||||||
|     ASSERT_INTERRUPTS_DISABLED(); |  | ||||||
|     char buf[16]; |  | ||||||
|     ksprintf(buf, "%d", task.pid()); |  | ||||||
|     auto dir = addFile(createDirectory(buf)); |  | ||||||
|     m_pid2inode.set(task.pid(), dir.index()); |  | ||||||
|     addFile(createGeneratedFile("vm", [&task] { |  | ||||||
|     InterruptDisabler disabler; |     InterruptDisabler disabler; | ||||||
|     char* buffer; |     char* buffer; | ||||||
|     auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer); |     auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer); | ||||||
|  | @ -48,42 +43,56 @@ void ProcFileSystem::addProcess(Task& task) | ||||||
|     } |     } | ||||||
|     *ptr = '\0'; |     *ptr = '\0'; | ||||||
|     return ByteBuffer::copy((byte*)buffer, ptr - buffer); |     return ByteBuffer::copy((byte*)buffer, ptr - buffer); | ||||||
|     }), dir.index()); | } | ||||||
|     addFile(createGeneratedFile("stack", [&task] { | 
 | ||||||
|  | ByteBuffer procfs$pid_stack(Task& task) | ||||||
|  | { | ||||||
|     InterruptDisabler disabler; |     InterruptDisabler disabler; | ||||||
|         auto& syms = ksyms(); |     if (current != &task) { | ||||||
|         dword firstKsymAddress = syms.first().address; |         MemoryManager::the().unmapRegionsForTask(*current); | ||||||
|         dword lastKsymAddress = syms.last().address; |         MemoryManager::the().mapRegionsForTask(task); | ||||||
|  |     } | ||||||
|     struct RecognizedSymbol { |     struct RecognizedSymbol { | ||||||
|         dword address; |         dword address; | ||||||
|             const char* name; |         const KSym* ksym; | ||||||
|             dword offset; |  | ||||||
|     }; |     }; | ||||||
|     Vector<RecognizedSymbol> recognizedSymbols; |     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; |     size_t bytesNeeded = 0; | ||||||
|         for (dword* stackPtr = (dword*)task.stackPtr(); (dword)stackPtr < task.stackTop(); ++stackPtr) { |     for (auto& symbol : recognizedSymbols) { | ||||||
|             if (*stackPtr < firstKsymAddress || *stackPtr > lastKsymAddress) |         bytesNeeded += symbol.ksym->name.length() + 8 + 16; | ||||||
|                 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); |     auto buffer = ByteBuffer::createUninitialized(bytesNeeded); | ||||||
|         char* ptr = (char*)buffer.pointer(); |     char* bufptr = (char*)buffer.pointer(); | ||||||
|  | 
 | ||||||
|     for (auto& symbol : recognizedSymbols) { |     for (auto& symbol : recognizedSymbols) { | ||||||
|             kprintf("%p  %s +%u\n", symbol.address, symbol.name, symbol.offset); |         // 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); | ||||||
|     } |     } | ||||||
|         buffer.trim(ptr - (char*)buffer.pointer()); |  | ||||||
|     return buffer; |     return buffer; | ||||||
|     }), dir.index()); | } | ||||||
|  | 
 | ||||||
|  | void ProcFileSystem::addProcess(Task& task) | ||||||
|  | { | ||||||
|  |     ASSERT_INTERRUPTS_DISABLED(); | ||||||
|  |     char buf[16]; | ||||||
|  |     ksprintf(buf, "%d", task.pid()); | ||||||
|  |     auto dir = addFile(createDirectory(buf)); | ||||||
|  |     m_pid2inode.set(task.pid(), 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) | void ProcFileSystem::removeProcess(Task& task) | ||||||
|  | @ -97,11 +106,8 @@ void ProcFileSystem::removeProcess(Task& task) | ||||||
|     m_pid2inode.remove(pid); |     m_pid2inode.remove(pid); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool ProcFileSystem::initialize() | ByteBuffer procfs$mounts() | ||||||
| { | { | ||||||
|     SyntheticFileSystem::initialize(); |  | ||||||
| 
 |  | ||||||
|     addFile(createGeneratedFile("mounts", [] { |  | ||||||
|     InterruptDisabler disabler; |     InterruptDisabler disabler; | ||||||
|     auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80); |     auto buffer = ByteBuffer::createUninitialized(VirtualFileSystem::the().mountCount() * 80); | ||||||
|     char* ptr = (char*)buffer.pointer(); |     char* ptr = (char*)buffer.pointer(); | ||||||
|  | @ -115,18 +121,20 @@ bool ProcFileSystem::initialize() | ||||||
|     }); |     }); | ||||||
|     buffer.trim(ptr - (char*)buffer.pointer()); |     buffer.trim(ptr - (char*)buffer.pointer()); | ||||||
|     return buffer; |     return buffer; | ||||||
|     })); | } | ||||||
| 
 | 
 | ||||||
|     addFile(createGeneratedFile("kmalloc", [] { | ByteBuffer procfs$kmalloc() | ||||||
|  | { | ||||||
|     InterruptDisabler disabler; |     InterruptDisabler disabler; | ||||||
|     auto buffer = ByteBuffer::createUninitialized(128); |     auto buffer = ByteBuffer::createUninitialized(128); | ||||||
|     char* ptr = (char*)buffer.pointer(); |     char* ptr = (char*)buffer.pointer(); | ||||||
|     ptr += ksprintf(ptr, "alloc: %u\nfree:  %u\n", sum_alloc, sum_free); |     ptr += ksprintf(ptr, "alloc: %u\nfree:  %u\n", sum_alloc, sum_free); | ||||||
|     buffer.trim(ptr - (char*)buffer.pointer()); |     buffer.trim(ptr - (char*)buffer.pointer()); | ||||||
|     return buffer; |     return buffer; | ||||||
|     })); | } | ||||||
| 
 | 
 | ||||||
|     addFile(createGeneratedFile("summary", [] { | ByteBuffer procfs$summary() | ||||||
|  | { | ||||||
|     InterruptDisabler disabler; |     InterruptDisabler disabler; | ||||||
|     auto tasks = Task::allTasks(); |     auto tasks = Task::allTasks(); | ||||||
|     auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256); |     auto buffer = ByteBuffer::createUninitialized(tasks.size() * 256); | ||||||
|  | @ -146,7 +154,14 @@ bool ProcFileSystem::initialize() | ||||||
|     *ptr = '\0'; |     *ptr = '\0'; | ||||||
|     buffer.trim(ptr - (char*)buffer.pointer()); |     buffer.trim(ptr - (char*)buffer.pointer()); | ||||||
|     return buffer; |     return buffer; | ||||||
|     })); | } | ||||||
|  | 
 | ||||||
|  | bool ProcFileSystem::initialize() | ||||||
|  | { | ||||||
|  |     SyntheticFileSystem::initialize(); | ||||||
|  |     addFile(createGeneratedFile("mounts", procfs$mounts)); | ||||||
|  |     addFile(createGeneratedFile("kmalloc", procfs$kmalloc)); | ||||||
|  |     addFile(createGeneratedFile("summary", procfs$summary)); | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -2,7 +2,17 @@ | ||||||
| 
 | 
 | ||||||
| #include "types.h" | #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); | void memcpy(void*, const void*, DWORD); | ||||||
|  | #endif | ||||||
| void strcpy(char*, const char*); | void strcpy(char*, const char*); | ||||||
| int strcmp(char const*, const char*); | int strcmp(char const*, const char*); | ||||||
| DWORD strlen(const char*); | DWORD strlen(const char*); | ||||||
|  |  | ||||||
|  | @ -885,3 +885,23 @@ Task::Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n) | ||||||
| Task::Region::~Region() | 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; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -122,9 +122,13 @@ public: | ||||||
| 
 | 
 | ||||||
|     size_t fileHandleCount() const { return m_fileHandles.size(); } |     size_t fileHandleCount() const { return m_fileHandles.size(); } | ||||||
| 
 | 
 | ||||||
|  |     dword framePtr() const { return m_tss.ebp; } | ||||||
|     dword stackPtr() const { return m_tss.esp; } |     dword stackPtr() const { return m_tss.esp; } | ||||||
|     dword stackTop() const { return m_tss.ss == 0x10 ? m_stackTop0 : m_stackTop3; } |     dword stackTop() const { return m_tss.ss == 0x10 ? m_stackTop0 : m_stackTop3; } | ||||||
| 
 | 
 | ||||||
|  |     bool isValidAddressForKernel(LinearAddress) const; | ||||||
|  |     bool isValidAddressForUser(LinearAddress) const; | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     friend class MemoryManager; |     friend class MemoryManager; | ||||||
|     friend bool scheduleNewTask(); |     friend bool scheduleNewTask(); | ||||||
|  |  | ||||||
										
											Binary file not shown.
										
									
								
							|  | @ -196,10 +196,10 @@ void exception_14_handler() | ||||||
|     asm ("movl %%cr2, %%eax":"=a"(faultAddress)); |     asm ("movl %%cr2, %%eax":"=a"(faultAddress)); | ||||||
| 
 | 
 | ||||||
|     auto& regs = *reinterpret_cast<RegisterDump*>(exception_state_dump); |     auto& regs = *reinterpret_cast<RegisterDump*>(exception_state_dump); | ||||||
|     kprintf("%s page fault: %u(%s), %s laddr=%p\n", |     kprintf("Ring%u page fault in %s(%u), %s laddr=%p\n", | ||||||
|         current->isRing0() ? "Kernel" : "User", |         regs.cs & 3, | ||||||
|         current->pid(), |  | ||||||
|         current->name().characters(), |         current->name().characters(), | ||||||
|  |         current->pid(), | ||||||
|         exception_code & 2 ? "write" : "read", |         exception_code & 2 ? "write" : "read", | ||||||
|         faultAddress); |         faultAddress); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -65,6 +65,17 @@ Vector<KSym>& ksyms() | ||||||
|     return *s_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) | static void loadKernelMap(const ByteBuffer& buffer) | ||||||
| { | { | ||||||
|     s_ksyms = new Vector<KSym>; |     s_ksyms = new Vector<KSym>; | ||||||
|  |  | ||||||
|  | @ -29,6 +29,11 @@ PRIVATE BYTE alloc_map[POOL_SIZE / CHUNK_SIZE / 8]; | ||||||
| volatile DWORD sum_alloc = 0; | volatile DWORD sum_alloc = 0; | ||||||
| volatile DWORD sum_free = POOL_SIZE; | 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 | PUBLIC void | ||||||
| kmalloc_init() | kmalloc_init() | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -4,6 +4,8 @@ void kmalloc_init(); | ||||||
| void *kmalloc(DWORD size) __attribute__ ((malloc)); | void *kmalloc(DWORD size) __attribute__ ((malloc)); | ||||||
| void kfree(void*); | void kfree(void*); | ||||||
| 
 | 
 | ||||||
|  | bool is_kmalloc_address(void*); | ||||||
|  | 
 | ||||||
| extern volatile DWORD sum_alloc; | extern volatile DWORD sum_alloc; | ||||||
| extern volatile DWORD sum_free; | extern volatile DWORD sum_free; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ struct KSym { | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| Vector<KSym>& ksyms() PURE; | Vector<KSym>& ksyms() PURE; | ||||||
|  | const KSym* ksymbolicate(dword address) PURE; | ||||||
| 
 | 
 | ||||||
| struct system_t | struct system_t | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -74,6 +74,10 @@ public: | ||||||
|     void set(dword address) { m_address = address; } |     void set(dword address) { m_address = address; } | ||||||
|     void mask(dword m) { m_address &= m; } |     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; } |     bool operator==(const LinearAddress& other) const { return m_address == other.m_address; } | ||||||
| 
 | 
 | ||||||
|     byte* asPtr() { return reinterpret_cast<byte*>(m_address); } |     byte* asPtr() { return reinterpret_cast<byte*>(m_address); } | ||||||
|  |  | ||||||
|  | @ -15,7 +15,7 @@ int main(int argc, char** argv) | ||||||
|         return 1; |         return 1; | ||||||
|     } |     } | ||||||
|     for (;;) { |     for (;;) { | ||||||
|         char buf[4096]; |         char buf[1024]; | ||||||
|         ssize_t nread = read(fd, buf, sizeof(buf)); |         ssize_t nread = read(fd, buf, sizeof(buf)); | ||||||
|         if (nread == 0) |         if (nread == 0) | ||||||
|             break; |             break; | ||||||
|  |  | ||||||
|  | @ -268,7 +268,7 @@ Vector<unsigned> Ext2FileSystem::blockListForInode(const ext2_inode& e2inode) co | ||||||
|     return list; |     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(offset >= 0); | ||||||
|     ASSERT(inode.fileSystemID() == id()); |     ASSERT(inode.fileSystemID() == id()); | ||||||
|  | @ -293,6 +293,7 @@ Unix::ssize_t Ext2FileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t | ||||||
|     static const unsigned maxInlineSymlinkLength = 60; |     static const unsigned maxInlineSymlinkLength = 60; | ||||||
|     if (isSymbolicLink(e2inode->i_mode) && e2inode->i_size < maxInlineSymlinkLength) { |     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)); |         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); |         memcpy(buffer, e2inode->i_block + offset, nread); | ||||||
|         return nread; |         return nread; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -44,7 +44,7 @@ private: | ||||||
|     virtual InodeMetadata inodeMetadata(InodeIdentifier) const override; |     virtual InodeMetadata inodeMetadata(InodeIdentifier) const override; | ||||||
|     virtual bool setModificationTime(InodeIdentifier, dword timestamp) override; |     virtual bool setModificationTime(InodeIdentifier, dword timestamp) override; | ||||||
|     virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) 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; |     virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override; | ||||||
| 
 | 
 | ||||||
|     bool isDirectoryInode(unsigned) const; |     bool isDirectoryInode(unsigned) const; | ||||||
|  |  | ||||||
|  | @ -102,7 +102,7 @@ Unix::ssize_t FileHandle::read(byte* buffer, Unix::size_t count) | ||||||
|         // FIXME: What should happen to m_currentOffset?
 |         // FIXME: What should happen to m_currentOffset?
 | ||||||
|         return m_vnode->characterDevice()->read(buffer, count); |         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; |     m_currentOffset += nread; | ||||||
|     return nread; |     return nread; | ||||||
| } | } | ||||||
|  | @ -125,7 +125,7 @@ ByteBuffer FileHandle::readEntireFile() | ||||||
|         return buffer; |         return buffer; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return m_vnode->fileSystem()->readEntireInode(m_vnode->inode); |     return m_vnode->fileSystem()->readEntireInode(m_vnode->inode, this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool FileHandle::isDirectory() const | bool FileHandle::isDirectory() const | ||||||
|  |  | ||||||
|  | @ -32,6 +32,8 @@ public: | ||||||
|     void setBlocking(bool b) { m_isBlocking = b; } |     void setBlocking(bool b) { m_isBlocking = b; } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  |     ByteBuffer& generatorCache() { return m_generatorCache; } | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     friend class VirtualFileSystem; |     friend class VirtualFileSystem; | ||||||
| 
 | 
 | ||||||
|  | @ -39,6 +41,8 @@ private: | ||||||
| 
 | 
 | ||||||
|     Unix::off_t m_currentOffset { 0 }; |     Unix::off_t m_currentOffset { 0 }; | ||||||
| 
 | 
 | ||||||
|  |     ByteBuffer m_generatorCache; | ||||||
|  | 
 | ||||||
| #ifdef SERENITY | #ifdef SERENITY | ||||||
|     int m_fd { -1 }; |     int m_fd { -1 }; | ||||||
|     bool m_isBlocking { true }; |     bool m_isBlocking { true }; | ||||||
|  |  | ||||||
|  | @ -50,7 +50,7 @@ InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, | ||||||
|     return foundInode; |     return foundInode; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const | ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode, FileHandle* handle) const | ||||||
| { | { | ||||||
|     ASSERT(inode.fileSystemID() == id()); |     ASSERT(inode.fileSystemID() == id()); | ||||||
| 
 | 
 | ||||||
|  | @ -67,7 +67,7 @@ ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const | ||||||
|     byte* out = contents.pointer(); |     byte* out = contents.pointer(); | ||||||
|     Unix::off_t offset = 0; |     Unix::off_t offset = 0; | ||||||
|     for (;;) { |     for (;;) { | ||||||
|         nread = readInodeBytes(inode, offset, sizeof(buffer), buffer); |         nread = readInodeBytes(inode, offset, sizeof(buffer), buffer, handle); | ||||||
|         if (nread <= 0) |         if (nread <= 0) | ||||||
|             break; |             break; | ||||||
|         memcpy(out, buffer, nread); |         memcpy(out, buffer, nread); | ||||||
|  |  | ||||||
|  | @ -16,6 +16,8 @@ | ||||||
| 
 | 
 | ||||||
| static const dword mepoch = 476763780; | static const dword mepoch = 476763780; | ||||||
| 
 | 
 | ||||||
|  | class FileHandle; | ||||||
|  | 
 | ||||||
| class FileSystem : public Retainable<FileSystem> { | class FileSystem : public Retainable<FileSystem> { | ||||||
| public: | public: | ||||||
|     static void initializeGlobals(); |     static void initializeGlobals(); | ||||||
|  | @ -30,7 +32,7 @@ public: | ||||||
|     virtual bool writeInode(InodeIdentifier, const ByteBuffer&) = 0; |     virtual bool writeInode(InodeIdentifier, const ByteBuffer&) = 0; | ||||||
|     virtual InodeMetadata inodeMetadata(InodeIdentifier) const = 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 { |     struct DirectoryEntry { | ||||||
|         String name; |         String name; | ||||||
|  | @ -44,7 +46,7 @@ public: | ||||||
|     virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) = 0; |     virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) = 0; | ||||||
| 
 | 
 | ||||||
|     InodeIdentifier childOfDirectoryInodeWithName(InodeIdentifier, const String& name) const; |     InodeIdentifier childOfDirectoryInodeWithName(InodeIdentifier, const String& name) const; | ||||||
|     ByteBuffer readEntireInode(InodeIdentifier) const; |     ByteBuffer readEntireInode(InodeIdentifier, FileHandle* = nullptr) const; | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
|     FileSystem(); |     FileSystem(); | ||||||
|  |  | ||||||
|  | @ -5,5 +5,5 @@ ByteBuffer InodeIdentifier::readEntireFile() const | ||||||
| { | { | ||||||
|     if (!fileSystem()) |     if (!fileSystem()) | ||||||
|         return { }; |         return { }; | ||||||
|     return fileSystem()->readEntireInode(*this); |     return fileSystem()->readEntireInode(*this, nullptr); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| #include "SyntheticFileSystem.h" | #include "SyntheticFileSystem.h" | ||||||
|  | #include "FileHandle.h" | ||||||
| #include <AK/StdLib.h> | #include <AK/StdLib.h> | ||||||
| 
 | 
 | ||||||
| //#define SYNTHFS_DEBUG
 | //#define SYNTHFS_DEBUG
 | ||||||
|  | @ -188,7 +189,7 @@ bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&) | ||||||
|     return false; |     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; |     InterruptDisabler disabler; | ||||||
| 
 | 
 | ||||||
|  | @ -204,12 +205,21 @@ Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::o | ||||||
|         return false; |         return false; | ||||||
|     const File& file = *(*it).value; |     const File& file = *(*it).value; | ||||||
|     ByteBuffer generatedData; |     ByteBuffer generatedData; | ||||||
|     if (file.generator) |     if (file.generator) { | ||||||
|  |         if (!handle) { | ||||||
|             generatedData = file.generator(); |             generatedData = file.generator(); | ||||||
|  |         } else { | ||||||
|  |             if (!handle->generatorCache()) | ||||||
|  |                 handle->generatorCache() = file.generator(); | ||||||
|  |             generatedData = handle->generatorCache(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|     auto* data = generatedData ? &generatedData : &file.data; |     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)); |     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); |     memcpy(buffer, data->pointer() + offset, nread); | ||||||
|  |     if (nread == 0 && handle && handle->generatorCache()) | ||||||
|  |         handle->generatorCache().clear(); | ||||||
|     return nread; |     return nread; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -17,7 +17,7 @@ public: | ||||||
|     virtual InodeMetadata inodeMetadata(InodeIdentifier) const override; |     virtual InodeMetadata inodeMetadata(InodeIdentifier) const override; | ||||||
|     virtual bool setModificationTime(InodeIdentifier, dword timestamp) override; |     virtual bool setModificationTime(InodeIdentifier, dword timestamp) override; | ||||||
|     virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) 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; |     virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override; | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling