Pārlūkot izejas kodu

Kernel: Enable early-returns from VFS::for_each_mount

Ben Wiederhake 3 gadi atpakaļ
vecāks
revīzija
88ca12f037

+ 3 - 2
Kernel/FileSystem/VirtualFileSystem.cpp

@@ -724,11 +724,12 @@ KResult VirtualFileSystem::rmdir(StringView path, Custody& base)
     return parent_inode.remove_child(KLexicalPath::basename(path));
 }
 
-void VirtualFileSystem::for_each_mount(Function<void(Mount const&)> callback) const
+void VirtualFileSystem::for_each_mount(Function<IterationDecision(Mount const&)> callback) const
 {
     m_mounts.with_shared([&](auto& mounts) {
         for (auto& mount : mounts) {
-            callback(mount);
+            if (callback(mount) == IterationDecision::Break)
+                break;
         }
     });
 }

+ 1 - 1
Kernel/FileSystem/VirtualFileSystem.h

@@ -66,7 +66,7 @@ public:
     KResult mknod(StringView path, mode_t, dev_t, Custody& base);
     KResultOr<NonnullRefPtr<Custody>> open_directory(StringView path, Custody& base);
 
-    void for_each_mount(Function<void(const Mount&)>) const;
+    void for_each_mount(Function<IterationDecision(const Mount&)>) const;
 
     InodeIdentifier root_inode_id() const;
 

+ 4 - 1
Kernel/GlobalProcessExposed.cpp

@@ -351,7 +351,8 @@ private:
     virtual KResult try_generate(KBufferBuilder& builder) override
     {
         JsonArraySerializer array { builder };
-        VirtualFileSystem::the().for_each_mount([&array](auto& mount) {
+        KResult result = KSuccess;
+        VirtualFileSystem::the().for_each_mount([&array, &result](auto& mount) {
             auto& fs = mount.guest_fs();
             auto fs_object = array.add_object();
             fs_object.add("class_name", fs.class_name());
@@ -368,6 +369,8 @@ private:
                 fs_object.add("source", static_cast<const FileBackedFileSystem&>(fs).file_description().absolute_path());
             else
                 fs_object.add("source", "none");
+
+            return IterationDecision::Continue;
         });
         array.finish();
         return KSuccess;

+ 12 - 12
Kernel/Syscalls/statvfs.cpp

@@ -38,19 +38,19 @@ KResultOr<FlatPtr> Process::do_statvfs(StringView path, statvfs* buf)
 
     while (current_custody) {
         VirtualFileSystem::the().for_each_mount([&kernelbuf, &current_custody](auto& mount) {
-            if (current_custody) {
-                if (&current_custody->inode() == &mount.guest()) {
-                    int mountflags = mount.flags();
-                    int flags = 0;
-                    if (mountflags & MS_RDONLY)
-                        flags = flags | ST_RDONLY;
-                    if (mountflags & MS_NOSUID)
-                        flags = flags | ST_NOSUID;
-
-                    kernelbuf.f_flag = flags;
-                    current_custody = nullptr;
-                }
+            if (&current_custody->inode() == &mount.guest()) {
+                int mountflags = mount.flags();
+                int flags = 0;
+                if (mountflags & MS_RDONLY)
+                    flags = flags | ST_RDONLY;
+                if (mountflags & MS_NOSUID)
+                    flags = flags | ST_NOSUID;
+
+                kernelbuf.f_flag = flags;
+                current_custody = nullptr;
+                return IterationDecision::Break;
             }
+            return IterationDecision::Continue;
         });
 
         if (current_custody) {