From 271e893889a6c8e6c0544d54bef097fededb4b74 Mon Sep 17 00:00:00 2001 From: Shaowei Song Date: Thu, 10 Oct 2024 03:21:07 +0000 Subject: [PATCH] Revise `MountNode`'s lock usage --- kernel/src/fs/path/mount.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/kernel/src/fs/path/mount.rs b/kernel/src/fs/path/mount.rs index fd0b131db..d8f83d5e7 100644 --- a/kernel/src/fs/path/mount.rs +++ b/kernel/src/fs/path/mount.rs @@ -16,13 +16,13 @@ pub struct MountNode { root_dentry: Arc, /// Mountpoint dentry. A mount node can be mounted on one dentry of another mount node, /// which makes the mount being the child of the mount node. - mountpoint_dentry: RwMutex>>, + mountpoint_dentry: RwLock>>, /// The associated FS. fs: Arc, /// The parent mount node. - parent: RwMutex>>, + parent: RwLock>>, /// Child mount nodes which are mounted on one dentry of self. - children: RwMutex>>, + children: RwLock>>, /// Reference to self. this: Weak, } @@ -50,9 +50,9 @@ impl MountNode { fn new(fs: Arc, parent_mount: Option>) -> Arc { Arc::new_cyclic(|weak_self| Self { root_dentry: Dentry_::new_root(fs.root_inode()), - mountpoint_dentry: RwMutex::new(None), - parent: RwMutex::new(parent_mount), - children: RwMutex::new(HashMap::new()), + mountpoint_dentry: RwLock::new(None), + parent: RwLock::new(parent_mount), + children: RwLock::new(HashMap::new()), fs, this: weak_self.clone(), }) @@ -106,9 +106,9 @@ impl MountNode { fn clone_mount_node(&self, root_dentry: &Arc) -> Arc { Arc::new_cyclic(|weak_self| Self { root_dentry: root_dentry.clone(), - mountpoint_dentry: RwMutex::new(None), - parent: RwMutex::new(None), - children: RwMutex::new(HashMap::new()), + mountpoint_dentry: RwLock::new(None), + parent: RwLock::new(None), + children: RwLock::new(HashMap::new()), fs: self.fs.clone(), this: weak_self.clone(), }) @@ -221,11 +221,13 @@ impl MountNode { /// Flushes all pending filesystem metadata and cached file data to the device. pub fn sync(&self) -> Result<()> { - let children = self.children.read(); - for child in children.values() { + let children: Vec> = { + let children = self.children.read(); + children.values().cloned().collect() + }; + for child in children { child.sync()?; } - drop(children); self.fs.sync()?; Ok(())