summaryrefslogtreecommitdiff
path: root/target/linux/generic/patches-2.6.38/101-overlayfs_fix_readdir_deadlock.patch
blob: 81ef4c084fdf7492ada0b0baa8a3cc4776a907db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
--- a/fs/overlayfs/overlayfs.c
+++ b/fs/overlayfs/overlayfs.c
@@ -253,8 +253,7 @@ static struct ovl_cache_entry *ovl_cache
 }
 
 static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
-						   u64 ino, unsigned int d_type,
-						   bool is_whiteout)
+						   u64 ino, unsigned int d_type)
 {
 	struct ovl_cache_entry *p;
 
@@ -267,7 +266,7 @@ static struct ovl_cache_entry *ovl_cache
 		p->len = len;
 		p->type = d_type;
 		p->ino = ino;
-		p->is_whiteout = is_whiteout;
+		p->is_whiteout = false;
 	}
 
 	return p;
@@ -275,7 +274,7 @@ static struct ovl_cache_entry *ovl_cache
 
 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
 				  const char *name, int len, u64 ino,
-				  unsigned int d_type, bool is_whiteout)
+				  unsigned int d_type)
 {
 	struct rb_node **newp = &rdd->root->rb_node;
 	struct rb_node *parent = NULL;
@@ -296,11 +295,18 @@ static int ovl_cache_entry_add_rb(struct
 			return 0;
 	}
 
-	p = ovl_cache_entry_new(name, len, ino, d_type, is_whiteout);
+	p = ovl_cache_entry_new(name, len, ino, d_type);
 	if (p == NULL)
 		return -ENOMEM;
 
-	list_add_tail(&p->l_node, rdd->list);
+	/*
+	 * Add links before other types to be able to quicky mark
+	 * any whiteout entries
+	 */
+	if (d_type == DT_LNK)
+		list_add(&p->l_node, rdd->list);
+	else
+		list_add_tail(&p->l_node, rdd->list);
 	rb_link_node(&p->node, parent, newp);
 	rb_insert_color(&p->node, rdd->root);
 
@@ -318,7 +324,7 @@ static int ovl_fill_lower(void *buf, con
 	if (p) {
 		list_move_tail(&p->l_node, rdd->middle);
 	} else {
-		p = ovl_cache_entry_new(name, namelen, ino, d_type, false);
+		p = ovl_cache_entry_new(name, namelen, ino, d_type);
 		if (p == NULL)
 			rdd->err = -ENOMEM;
 		else
@@ -343,26 +349,9 @@ static int ovl_fill_upper(void *buf, con
 			  loff_t offset, u64 ino, unsigned int d_type)
 {
 	struct ovl_readdir_data *rdd = buf;
-	bool is_whiteout = false;
 
 	rdd->count++;
-	if (d_type == DT_LNK) {
-		struct dentry *dentry;
-
-		dentry = lookup_one_len(name, rdd->dir, namelen);
-		if (IS_ERR(dentry)) {
-			rdd->err = PTR_ERR(dentry);
-			goto out;
-		}
-		is_whiteout = ovl_is_whiteout(dentry);
-		dput(dentry);
-	}
-
-	rdd->err = ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type,
-					  is_whiteout);
-
-out:
-	return rdd->err;
+	return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
 }
 
 static int ovl_dir_read(struct path *realpath, struct ovl_readdir_data *rdd,
@@ -428,6 +417,26 @@ static void ovl_dir_reset(struct file *f
 	}
 }
 
+static void ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
+{
+	struct ovl_cache_entry *p;
+	struct dentry *dentry;
+
+	mutex_lock(&rdd->dir->d_inode->i_mutex);
+	list_for_each_entry(p, rdd->list, l_node) {
+		if (p->type != DT_LNK)
+			break;
+
+		dentry = lookup_one_len(p->name, rdd->dir, p->len);
+		if (IS_ERR(dentry))
+			continue;
+
+		p->is_whiteout = ovl_is_whiteout(dentry);
+		dput(dentry);
+	}
+	mutex_unlock(&rdd->dir->d_inode->i_mutex);
+}
+
 static int ovl_dir_read_merged(struct path *upperpath, struct path *lowerpath,
 			       struct ovl_readdir_data *rdd)
 {
@@ -441,6 +450,8 @@ static int ovl_dir_read_merged(struct pa
 		err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
 		if (err)
 			goto out;
+
+		ovl_dir_mark_whiteouts(rdd);
 	}
 	/*
 	 * Insert lowerpath entries before upperpath ones, this allows