proc_mounts.patch 3.23 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
SPDX-License-Identifier: GPL-2.0
J. R. Okajima's avatar
J. R. Okajima committed
2

J. R. Okajima's avatar
J. R. Okajima committed
3
diff --git a/fs/mount.h b/fs/mount.h
J. R. Okajima's avatar
J. R. Okajima committed
4
index 711a4093e475..a5703d5c7205 100644
J. R. Okajima's avatar
J. R. Okajima committed
5
6
--- a/fs/mount.h
+++ b/fs/mount.h
J. R. Okajima's avatar
J. R. Okajima committed
7
@@ -133,9 +133,7 @@ struct proc_mounts {
J. R. Okajima's avatar
J. R. Okajima committed
8
9
10
11
12
13
 	struct mnt_namespace *ns;
 	struct path root;
 	int (*show)(struct seq_file *, struct vfsmount *);
-	void *cached_mount;
-	u64 cached_event;
-	loff_t cached_index;
J. R. Okajima's avatar
J. R. Okajima committed
14
+	size_t filled;
J. R. Okajima's avatar
J. R. Okajima committed
15
16
17
18
 };
 
 extern const struct seq_operations mounts_op;
diff --git a/fs/namespace.c b/fs/namespace.c
J. R. Okajima's avatar
J. R. Okajima committed
19
index a28e4db075ed..14fdd03edd0a 100644
J. R. Okajima's avatar
J. R. Okajima committed
20
21
--- a/fs/namespace.c
+++ b/fs/namespace.c
J. R. Okajima's avatar
J. R. Okajima committed
22
@@ -1246,46 +1246,80 @@ struct vfsmount *mnt_clone_internal(const struct path *path)
J. R. Okajima's avatar
J. R. Okajima committed
23
24
25
26
27
28
29
30
31
32
33
34
35
 
 #ifdef CONFIG_PROC_FS
 /* iterator; we want it to have access to namespace_sem, thus here... */
-static void *m_start(struct seq_file *m, loff_t *pos)
+static int m_start_fill(struct seq_file *m)
 {
+	int err;
+	size_t last_count;
+	char *buf;
+	struct mount *r;
 	struct proc_mounts *p = m->private;
 
+	err = -ENODATA;
J. R. Okajima's avatar
J. R. Okajima committed
36
+	m->count = 0;
J. R. Okajima's avatar
J. R. Okajima committed
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
 	down_read(&namespace_sem);
-	if (p->cached_event == p->ns->event) {
-		void *v = p->cached_mount;
-		if (*pos == p->cached_index)
-			return v;
-		if (*pos == p->cached_index + 1) {
-			v = seq_list_next(v, &p->ns->list, &p->cached_index);
-			return p->cached_mount = v;
+	list_for_each_entry(r, &p->ns->list, mnt_list) {
+		last_count = m->count;
+		err = p->show(m, &r->mnt);
+		if (unlikely(err < 0))
+			break;
+		if (!seq_has_overflowed(m))
+			continue;
+
+		/* expand the buffer */
+		buf = kvmalloc(m->size + PAGE_SIZE, GFP_KERNEL);
+		if (unlikely(!buf)) {
+			err = -ENOMEM;
+			break;
+		}
+		memcpy(buf, m->buf, last_count);
+		kvfree(m->buf);
+		m->buf = buf;
+		m->size += PAGE_SIZE;
+		m->count = last_count;
+
J. R. Okajima's avatar
J. R. Okajima committed
65
+#if 0 /* todo: consider the limit */
J. R. Okajima's avatar
J. R. Okajima committed
66
67
68
69
70
71
72
+		err = p->show(m, &r->mnt);
+		if (unlikely(err < 0))
+			break;
+		else if (unlikely(seq_has_overflowed(m))) {
+			err = -EOVERFLOW;
+			break;
 		}
J. R. Okajima's avatar
J. R. Okajima committed
73
+#endif
J. R. Okajima's avatar
J. R. Okajima committed
74
75
76
77
78
79
80
81
 	}
+	up_read(&namespace_sem);
 
-	p->cached_event = p->ns->event;
-	p->cached_mount = seq_list_start(&p->ns->list, *pos);
-	p->cached_index = *pos;
-	return p->cached_mount;
+	if (!err)
J. R. Okajima's avatar
J. R. Okajima committed
82
+		p->filled = m->count;
J. R. Okajima's avatar
J. R. Okajima committed
83
84
85
86
87
88
+	return err;
 }
 
-static void *m_next(struct seq_file *m, void *v, loff_t *pos)
+static void *m_start(struct seq_file *m, loff_t *pos)
 {
J. R. Okajima's avatar
J. R. Okajima committed
89
-	struct proc_mounts *p = m->private;
J. R. Okajima's avatar
J. R. Okajima committed
90
91
92
93
94
+	int err;
 
-	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
-	p->cached_index = *pos;
-	return p->cached_mount;
J. R. Okajima's avatar
J. R. Okajima committed
95
+	if (!*pos) {
J. R. Okajima's avatar
J. R. Okajima committed
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
+		err = m_start_fill(m);
+		if (unlikely(err))
+			return ERR_PTR(err);
+	}
+
+	return m_start; /* any valid pointer */
+}
+
+static void *m_next(struct seq_file *m, void *v, loff_t *pos)
+{
+	return NULL;
 }
 
 static void m_stop(struct seq_file *m, void *v)
 {
-	up_read(&namespace_sem);
+	/* empty */
 }
 
 static int m_show(struct seq_file *m, void *v)
 {
-	struct proc_mounts *p = m->private;
-	struct mount *r = list_entry(v, struct mount, mnt_list);
-	return p->show(m, &r->mnt);
+	return 0;
 }
 
 const struct seq_operations mounts_op = {
diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c
J. R. Okajima's avatar
J. R. Okajima committed
125
index 273ee82d8aa9..e5b22c50ca59 100644
J. R. Okajima's avatar
J. R. Okajima committed
126
127
128
129
130
131
132
133
134
135
--- a/fs/proc_namespace.c
+++ b/fs/proc_namespace.c
@@ -279,7 +279,6 @@ static int mounts_open_common(struct inode *inode, struct file *file,
 	p->ns = ns;
 	p->root = root;
 	p->show = show;
-	p->cached_event = ~0ULL;
 
 	return 0;