mount: update root and cwd from cloneMountTree

In this case, we don't need to enumerate all tree mounts and look up pair
mounts in the cloned tree.

PiperOrigin-RevId: 570778945
This commit is contained in:
Andrei Vagin
2023-10-04 12:58:14 -07:00
committed by gVisor bot
parent c6a1db5bae
commit cf93d15690
2 changed files with 17 additions and 7 deletions
+13 -1
View File
@@ -377,10 +377,19 @@ type cloneTreeNode struct {
// cloneMountTree creates a copy of mnt's tree with the specified root
// dentry at root. The new descendents are added to mnt's pending mount list.
// `cloneFunc` is a callback that is executed for each cloned mount.
//
// +checklocks:vfs.mountMu
func (vfs *VirtualFilesystem) cloneMountTree(ctx context.Context, mnt *Mount, root *Dentry) (*Mount, error) {
func (vfs *VirtualFilesystem) cloneMountTree(
ctx context.Context,
mnt *Mount,
root *Dentry,
cloneFunc func(ctx context.Context, oldmnt, newMnt *Mount),
) (*Mount, error) {
clone := vfs.cloneMount(mnt, root, nil)
if cloneFunc != nil {
cloneFunc(ctx, mnt, clone)
}
queue := []cloneTreeNode{{mnt, clone}}
for len(queue) != 0 {
p := queue[len(queue)-1]
@@ -397,6 +406,9 @@ func (vfs *VirtualFilesystem) cloneMountTree(ctx context.Context, mnt *Mount, ro
if len(c.children) != 0 {
queue = append(queue, cloneTreeNode{c, m})
}
if cloneFunc != nil {
cloneFunc(ctx, c, m)
}
}
}
return clone, nil
+4 -6
View File
@@ -142,10 +142,6 @@ func (vfs *VirtualFilesystem) updateRootAndCWD(ctx context.Context, root *Virtua
cwd.mount = dst
cwd.mount.IncRef()
}
for srcChild := range src.children {
dstChild := vfs.mounts.Lookup(dst, srcChild.point())
vfs.updateRootAndCWD(ctx, root, cwd, srcChild, dstChild)
}
}
// NamespaceInodeGetter is an interface that provides the GetNamespaceInode method.
@@ -174,7 +170,10 @@ func (vfs *VirtualFilesystem) CloneMountNamespace(
vfs.lockMounts()
defer vfs.unlockMounts(ctx)
newRoot, err := vfs.cloneMountTree(ctx, ns.root, ns.root.root)
newRoot, err := vfs.cloneMountTree(ctx, ns.root, ns.root.root,
func(ctx context.Context, src, dst *Mount) {
vfs.updateRootAndCWD(ctx, root, cwd, src, dst) // +checklocksforce: vfs.mountMu is locked.
})
if err != nil {
newns.DecRef(ctx)
vfs.abortTree(ctx, newRoot)
@@ -183,7 +182,6 @@ func (vfs *VirtualFilesystem) CloneMountNamespace(
newns.root = newRoot
newns.root.ns = newns
vfs.commitPendingTree(ctx, newRoot)
vfs.updateRootAndCWD(ctx, root, cwd, ns.root, newns.root)
return newns, nil
}