From 721585473b22bb6d88b2e0f802e7da22bc20dd4b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 4 Nov 2019 16:31:46 +0100 Subject: [PATCH] Ext2FS: Don't uncache inodes while they are being watched If an inode is observed by watch_file(), we won't uncache it. This allows a program to watch a file without keeping it open. --- Kernel/FileSystem/Ext2FileSystem.cpp | 4 ++++ Kernel/FileSystem/Inode.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 9d428b4c13..e6cdc52490 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -507,6 +507,8 @@ void Ext2FS::flush_writes() DiskBackedFS::flush_writes(); // Uncache Inodes that are only kept alive by the index-to-inode lookup cache. + // We don't uncache Inodes that are being watched by at least one InodeWatcher. + // FIXME: It would be better to keep a capped number of Inodes around. // The problem is that they are quite heavy objects, and use a lot of heap memory // for their (child name lookup) and (block list) caches. @@ -514,6 +516,8 @@ void Ext2FS::flush_writes() for (auto& it : m_inode_cache) { if (it.value->ref_count() != 1) continue; + if (it.value->has_watchers()) + continue; unused_inodes.append(it.key); } for (auto index : unused_inodes) diff --git a/Kernel/FileSystem/Inode.h b/Kernel/FileSystem/Inode.h index 131d83cbd8..6914563cea 100644 --- a/Kernel/FileSystem/Inode.h +++ b/Kernel/FileSystem/Inode.h @@ -77,6 +77,8 @@ public: static void sync(); + bool has_watchers() const { return !m_watchers.is_empty(); } + void register_watcher(Badge, InodeWatcher&); void unregister_watcher(Badge, InodeWatcher&);