mount: simplify mnt_build_ids_tree()

Submitted by Andrei Vagin on Feb. 3, 2017, 3:13 a.m.

Details

Message ID 1486091617-4339-1-git-send-email-avagin@openvz.org
State Accepted
Series "mount: simplify mnt_build_ids_tree()"
Headers show

Commit Message

Andrei Vagin Feb. 3, 2017, 3:13 a.m.
From: Andrei Vagin <avagin@virtuozzo.com>

This patch reworks this function, removes mess and fix
the next coverity issue:

394                m->parent = parent;
CID 175314 (#1 of 1): Dereference after null check (FORWARD_NULL)
27. var_deref_model: Passing null pointer &parent->children to list_add_tail, which dereferences it. [show details]

Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 criu/mount.c | 63 ++++++++++++++++++++++++++++--------------------------------
 1 file changed, 29 insertions(+), 34 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/mount.c b/criu/mount.c
index 4ba6cc2..cbbc154 100644
--- a/criu/mount.c
+++ b/criu/mount.c
@@ -328,7 +328,7 @@  static bool mounts_equal(struct mount_info *a, struct mount_info *b)
  */
 static char *mnt_roots;
 
-static struct mount_info *mnt_build_ids_tree(struct mount_info *list, struct mount_info *tmp_root_mount)
+static struct mount_info *mnt_build_ids_tree(struct mount_info *list, struct mount_info *yard_mount)
 {
 	struct mount_info *m, *root = NULL;
 
@@ -348,47 +348,42 @@  static struct mount_info *mnt_build_ids_tree(struct mount_info *list, struct mou
 			parent = NULL;
 
 		if (!parent) {
-			/* This should be / */
-			if (root == NULL && (!tmp_root_mount || is_root_mount(m))) {
+			/* Only a root mount can be without parent */
+			if (root == NULL && m->is_ns_root) {
 				root = m;
-				if (!tmp_root_mount)
+				if (!yard_mount)
 					continue;
 			}
 
+			if (!root) {
+				pr_err("No parent found for mountpoint %d (@%s)\n",
+					m->mnt_id, m->mountpoint);
+				return NULL;
+			}
+
 			pr_debug("Mountpoint %d (@%s) w/o parent %d\n",
 				 m->mnt_id, m->mountpoint, m->parent_mnt_id);
 
-			if (root && m->is_ns_root) {
-				if (!mounts_sb_equal(root, m) ||
-				    strcmp(root->root, m->root)) {
-					pr_err("Nested mount namespaces with different "
-					       "roots %d (@%s %s) %d (@%s %s) are not supported yet\n",
-					       root->mnt_id, root->mountpoint, root->root,
-					       m->mnt_id, m->mountpoint, m->root);
-					return NULL;
-				}
-
-				/*
-				 * A root of a sub mount namespace is
-				 * mounted in a temporary directory in the
-				 * root mount namespace, so its parent is
-				 * the main root.
-				 */
-				parent = tmp_root_mount;
-				if (unlikely(!tmp_root_mount)) {
-					pr_err("Nested mount %d (@%s %s) w/o root insertion detected\n",
-					       m->mnt_id, m->mountpoint, m->root);
-					return NULL;
-				}
+			if (!mounts_sb_equal(root, m) ||
+			    strcmp(root->root, m->root)) {
+				pr_err("Nested mount namespaces with different "
+				       "roots %d (@%s %s) %d (@%s %s) are not supported yet\n",
+				       root->mnt_id, root->mountpoint, root->root,
+				       m->mnt_id, m->mountpoint, m->root);
+				return NULL;
+			}
 
-				pr_debug("Mountpoint %d (@%s) get parent %d (@%s)\n",
-					 m->mnt_id, m->mountpoint,
-					 parent->mnt_id, parent->mountpoint);
-			} else if (root != m) {
-				pr_err("No root found for mountpoint %d (@%s)\n",
-					m->mnt_id, m->mountpoint);
+			/* Mount all namespace roots into the roots yard. */
+			parent = yard_mount;
+			if (unlikely(!yard_mount)) {
+				pr_err("Nested mount %d (@%s %s) w/o root insertion detected\n",
+				       m->mnt_id, m->mountpoint, m->root);
 				return NULL;
 			}
+
+			pr_debug("Mountpoint %d (@%s) get parent %d (@%s)\n",
+				 m->mnt_id, m->mountpoint,
+				 parent->mnt_id, parent->mountpoint);
 		}
 
 		m->parent = parent;
@@ -400,8 +395,8 @@  static struct mount_info *mnt_build_ids_tree(struct mount_info *list, struct mou
 		return NULL;
 	}
 
-	if (tmp_root_mount)
-		return tmp_root_mount;
+	if (yard_mount)
+		return yard_mount;
 
 	return root;
 }

Comments

Pavel Emelianov Feb. 6, 2017, 10:22 a.m.
Applied