Commit 7a62cc10215838286c747f86766063d5f01fcbd6
Committed by
Al Viro
1 parent
ce06e0b21d
Exists in
master
and in
7 other branches
seq_file: return a negative error code when seq_path_root() fails.
seq_path_root() is returning a return value of successful __d_path() instead of returning a negative value when mangle_path() failed. This is not a bug so far because nobody is using return value of seq_path_root(). Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Showing 1 changed file with 1 additions and 0 deletions Inline Diff
fs/seq_file.c
1 | /* | 1 | /* |
2 | * linux/fs/seq_file.c | 2 | * linux/fs/seq_file.c |
3 | * | 3 | * |
4 | * helper functions for making synthetic files from sequences of records. | 4 | * helper functions for making synthetic files from sequences of records. |
5 | * initial implementation -- AV, Oct 2001. | 5 | * initial implementation -- AV, Oct 2001. |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #include <linux/fs.h> | 8 | #include <linux/fs.h> |
9 | #include <linux/module.h> | 9 | #include <linux/module.h> |
10 | #include <linux/seq_file.h> | 10 | #include <linux/seq_file.h> |
11 | #include <linux/slab.h> | 11 | #include <linux/slab.h> |
12 | 12 | ||
13 | #include <asm/uaccess.h> | 13 | #include <asm/uaccess.h> |
14 | #include <asm/page.h> | 14 | #include <asm/page.h> |
15 | 15 | ||
16 | /** | 16 | /** |
17 | * seq_open - initialize sequential file | 17 | * seq_open - initialize sequential file |
18 | * @file: file we initialize | 18 | * @file: file we initialize |
19 | * @op: method table describing the sequence | 19 | * @op: method table describing the sequence |
20 | * | 20 | * |
21 | * seq_open() sets @file, associating it with a sequence described | 21 | * seq_open() sets @file, associating it with a sequence described |
22 | * by @op. @op->start() sets the iterator up and returns the first | 22 | * by @op. @op->start() sets the iterator up and returns the first |
23 | * element of sequence. @op->stop() shuts it down. @op->next() | 23 | * element of sequence. @op->stop() shuts it down. @op->next() |
24 | * returns the next element of sequence. @op->show() prints element | 24 | * returns the next element of sequence. @op->show() prints element |
25 | * into the buffer. In case of error ->start() and ->next() return | 25 | * into the buffer. In case of error ->start() and ->next() return |
26 | * ERR_PTR(error). In the end of sequence they return %NULL. ->show() | 26 | * ERR_PTR(error). In the end of sequence they return %NULL. ->show() |
27 | * returns 0 in case of success and negative number in case of error. | 27 | * returns 0 in case of success and negative number in case of error. |
28 | * Returning SEQ_SKIP means "discard this element and move on". | 28 | * Returning SEQ_SKIP means "discard this element and move on". |
29 | */ | 29 | */ |
30 | int seq_open(struct file *file, const struct seq_operations *op) | 30 | int seq_open(struct file *file, const struct seq_operations *op) |
31 | { | 31 | { |
32 | struct seq_file *p = file->private_data; | 32 | struct seq_file *p = file->private_data; |
33 | 33 | ||
34 | if (!p) { | 34 | if (!p) { |
35 | p = kmalloc(sizeof(*p), GFP_KERNEL); | 35 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
36 | if (!p) | 36 | if (!p) |
37 | return -ENOMEM; | 37 | return -ENOMEM; |
38 | file->private_data = p; | 38 | file->private_data = p; |
39 | } | 39 | } |
40 | memset(p, 0, sizeof(*p)); | 40 | memset(p, 0, sizeof(*p)); |
41 | mutex_init(&p->lock); | 41 | mutex_init(&p->lock); |
42 | p->op = op; | 42 | p->op = op; |
43 | 43 | ||
44 | /* | 44 | /* |
45 | * Wrappers around seq_open(e.g. swaps_open) need to be | 45 | * Wrappers around seq_open(e.g. swaps_open) need to be |
46 | * aware of this. If they set f_version themselves, they | 46 | * aware of this. If they set f_version themselves, they |
47 | * should call seq_open first and then set f_version. | 47 | * should call seq_open first and then set f_version. |
48 | */ | 48 | */ |
49 | file->f_version = 0; | 49 | file->f_version = 0; |
50 | 50 | ||
51 | /* | 51 | /* |
52 | * seq_files support lseek() and pread(). They do not implement | 52 | * seq_files support lseek() and pread(). They do not implement |
53 | * write() at all, but we clear FMODE_PWRITE here for historical | 53 | * write() at all, but we clear FMODE_PWRITE here for historical |
54 | * reasons. | 54 | * reasons. |
55 | * | 55 | * |
56 | * If a client of seq_files a) implements file.write() and b) wishes to | 56 | * If a client of seq_files a) implements file.write() and b) wishes to |
57 | * support pwrite() then that client will need to implement its own | 57 | * support pwrite() then that client will need to implement its own |
58 | * file.open() which calls seq_open() and then sets FMODE_PWRITE. | 58 | * file.open() which calls seq_open() and then sets FMODE_PWRITE. |
59 | */ | 59 | */ |
60 | file->f_mode &= ~FMODE_PWRITE; | 60 | file->f_mode &= ~FMODE_PWRITE; |
61 | return 0; | 61 | return 0; |
62 | } | 62 | } |
63 | EXPORT_SYMBOL(seq_open); | 63 | EXPORT_SYMBOL(seq_open); |
64 | 64 | ||
65 | static int traverse(struct seq_file *m, loff_t offset) | 65 | static int traverse(struct seq_file *m, loff_t offset) |
66 | { | 66 | { |
67 | loff_t pos = 0, index; | 67 | loff_t pos = 0, index; |
68 | int error = 0; | 68 | int error = 0; |
69 | void *p; | 69 | void *p; |
70 | 70 | ||
71 | m->version = 0; | 71 | m->version = 0; |
72 | index = 0; | 72 | index = 0; |
73 | m->count = m->from = 0; | 73 | m->count = m->from = 0; |
74 | if (!offset) { | 74 | if (!offset) { |
75 | m->index = index; | 75 | m->index = index; |
76 | return 0; | 76 | return 0; |
77 | } | 77 | } |
78 | if (!m->buf) { | 78 | if (!m->buf) { |
79 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); | 79 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |
80 | if (!m->buf) | 80 | if (!m->buf) |
81 | return -ENOMEM; | 81 | return -ENOMEM; |
82 | } | 82 | } |
83 | p = m->op->start(m, &index); | 83 | p = m->op->start(m, &index); |
84 | while (p) { | 84 | while (p) { |
85 | error = PTR_ERR(p); | 85 | error = PTR_ERR(p); |
86 | if (IS_ERR(p)) | 86 | if (IS_ERR(p)) |
87 | break; | 87 | break; |
88 | error = m->op->show(m, p); | 88 | error = m->op->show(m, p); |
89 | if (error < 0) | 89 | if (error < 0) |
90 | break; | 90 | break; |
91 | if (unlikely(error)) { | 91 | if (unlikely(error)) { |
92 | error = 0; | 92 | error = 0; |
93 | m->count = 0; | 93 | m->count = 0; |
94 | } | 94 | } |
95 | if (m->count == m->size) | 95 | if (m->count == m->size) |
96 | goto Eoverflow; | 96 | goto Eoverflow; |
97 | if (pos + m->count > offset) { | 97 | if (pos + m->count > offset) { |
98 | m->from = offset - pos; | 98 | m->from = offset - pos; |
99 | m->count -= m->from; | 99 | m->count -= m->from; |
100 | m->index = index; | 100 | m->index = index; |
101 | break; | 101 | break; |
102 | } | 102 | } |
103 | pos += m->count; | 103 | pos += m->count; |
104 | m->count = 0; | 104 | m->count = 0; |
105 | if (pos == offset) { | 105 | if (pos == offset) { |
106 | index++; | 106 | index++; |
107 | m->index = index; | 107 | m->index = index; |
108 | break; | 108 | break; |
109 | } | 109 | } |
110 | p = m->op->next(m, p, &index); | 110 | p = m->op->next(m, p, &index); |
111 | } | 111 | } |
112 | m->op->stop(m, p); | 112 | m->op->stop(m, p); |
113 | m->index = index; | 113 | m->index = index; |
114 | return error; | 114 | return error; |
115 | 115 | ||
116 | Eoverflow: | 116 | Eoverflow: |
117 | m->op->stop(m, p); | 117 | m->op->stop(m, p); |
118 | kfree(m->buf); | 118 | kfree(m->buf); |
119 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); | 119 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
120 | return !m->buf ? -ENOMEM : -EAGAIN; | 120 | return !m->buf ? -ENOMEM : -EAGAIN; |
121 | } | 121 | } |
122 | 122 | ||
123 | /** | 123 | /** |
124 | * seq_read - ->read() method for sequential files. | 124 | * seq_read - ->read() method for sequential files. |
125 | * @file: the file to read from | 125 | * @file: the file to read from |
126 | * @buf: the buffer to read to | 126 | * @buf: the buffer to read to |
127 | * @size: the maximum number of bytes to read | 127 | * @size: the maximum number of bytes to read |
128 | * @ppos: the current position in the file | 128 | * @ppos: the current position in the file |
129 | * | 129 | * |
130 | * Ready-made ->f_op->read() | 130 | * Ready-made ->f_op->read() |
131 | */ | 131 | */ |
132 | ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) | 132 | ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) |
133 | { | 133 | { |
134 | struct seq_file *m = (struct seq_file *)file->private_data; | 134 | struct seq_file *m = (struct seq_file *)file->private_data; |
135 | size_t copied = 0; | 135 | size_t copied = 0; |
136 | loff_t pos; | 136 | loff_t pos; |
137 | size_t n; | 137 | size_t n; |
138 | void *p; | 138 | void *p; |
139 | int err = 0; | 139 | int err = 0; |
140 | 140 | ||
141 | mutex_lock(&m->lock); | 141 | mutex_lock(&m->lock); |
142 | 142 | ||
143 | /* Don't assume *ppos is where we left it */ | 143 | /* Don't assume *ppos is where we left it */ |
144 | if (unlikely(*ppos != m->read_pos)) { | 144 | if (unlikely(*ppos != m->read_pos)) { |
145 | m->read_pos = *ppos; | 145 | m->read_pos = *ppos; |
146 | while ((err = traverse(m, *ppos)) == -EAGAIN) | 146 | while ((err = traverse(m, *ppos)) == -EAGAIN) |
147 | ; | 147 | ; |
148 | if (err) { | 148 | if (err) { |
149 | /* With prejudice... */ | 149 | /* With prejudice... */ |
150 | m->read_pos = 0; | 150 | m->read_pos = 0; |
151 | m->version = 0; | 151 | m->version = 0; |
152 | m->index = 0; | 152 | m->index = 0; |
153 | m->count = 0; | 153 | m->count = 0; |
154 | goto Done; | 154 | goto Done; |
155 | } | 155 | } |
156 | } | 156 | } |
157 | 157 | ||
158 | /* | 158 | /* |
159 | * seq_file->op->..m_start/m_stop/m_next may do special actions | 159 | * seq_file->op->..m_start/m_stop/m_next may do special actions |
160 | * or optimisations based on the file->f_version, so we want to | 160 | * or optimisations based on the file->f_version, so we want to |
161 | * pass the file->f_version to those methods. | 161 | * pass the file->f_version to those methods. |
162 | * | 162 | * |
163 | * seq_file->version is just copy of f_version, and seq_file | 163 | * seq_file->version is just copy of f_version, and seq_file |
164 | * methods can treat it simply as file version. | 164 | * methods can treat it simply as file version. |
165 | * It is copied in first and copied out after all operations. | 165 | * It is copied in first and copied out after all operations. |
166 | * It is convenient to have it as part of structure to avoid the | 166 | * It is convenient to have it as part of structure to avoid the |
167 | * need of passing another argument to all the seq_file methods. | 167 | * need of passing another argument to all the seq_file methods. |
168 | */ | 168 | */ |
169 | m->version = file->f_version; | 169 | m->version = file->f_version; |
170 | /* grab buffer if we didn't have one */ | 170 | /* grab buffer if we didn't have one */ |
171 | if (!m->buf) { | 171 | if (!m->buf) { |
172 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); | 172 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |
173 | if (!m->buf) | 173 | if (!m->buf) |
174 | goto Enomem; | 174 | goto Enomem; |
175 | } | 175 | } |
176 | /* if not empty - flush it first */ | 176 | /* if not empty - flush it first */ |
177 | if (m->count) { | 177 | if (m->count) { |
178 | n = min(m->count, size); | 178 | n = min(m->count, size); |
179 | err = copy_to_user(buf, m->buf + m->from, n); | 179 | err = copy_to_user(buf, m->buf + m->from, n); |
180 | if (err) | 180 | if (err) |
181 | goto Efault; | 181 | goto Efault; |
182 | m->count -= n; | 182 | m->count -= n; |
183 | m->from += n; | 183 | m->from += n; |
184 | size -= n; | 184 | size -= n; |
185 | buf += n; | 185 | buf += n; |
186 | copied += n; | 186 | copied += n; |
187 | if (!m->count) | 187 | if (!m->count) |
188 | m->index++; | 188 | m->index++; |
189 | if (!size) | 189 | if (!size) |
190 | goto Done; | 190 | goto Done; |
191 | } | 191 | } |
192 | /* we need at least one record in buffer */ | 192 | /* we need at least one record in buffer */ |
193 | pos = m->index; | 193 | pos = m->index; |
194 | p = m->op->start(m, &pos); | 194 | p = m->op->start(m, &pos); |
195 | while (1) { | 195 | while (1) { |
196 | err = PTR_ERR(p); | 196 | err = PTR_ERR(p); |
197 | if (!p || IS_ERR(p)) | 197 | if (!p || IS_ERR(p)) |
198 | break; | 198 | break; |
199 | err = m->op->show(m, p); | 199 | err = m->op->show(m, p); |
200 | if (err < 0) | 200 | if (err < 0) |
201 | break; | 201 | break; |
202 | if (unlikely(err)) | 202 | if (unlikely(err)) |
203 | m->count = 0; | 203 | m->count = 0; |
204 | if (unlikely(!m->count)) { | 204 | if (unlikely(!m->count)) { |
205 | p = m->op->next(m, p, &pos); | 205 | p = m->op->next(m, p, &pos); |
206 | m->index = pos; | 206 | m->index = pos; |
207 | continue; | 207 | continue; |
208 | } | 208 | } |
209 | if (m->count < m->size) | 209 | if (m->count < m->size) |
210 | goto Fill; | 210 | goto Fill; |
211 | m->op->stop(m, p); | 211 | m->op->stop(m, p); |
212 | kfree(m->buf); | 212 | kfree(m->buf); |
213 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); | 213 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
214 | if (!m->buf) | 214 | if (!m->buf) |
215 | goto Enomem; | 215 | goto Enomem; |
216 | m->count = 0; | 216 | m->count = 0; |
217 | m->version = 0; | 217 | m->version = 0; |
218 | pos = m->index; | 218 | pos = m->index; |
219 | p = m->op->start(m, &pos); | 219 | p = m->op->start(m, &pos); |
220 | } | 220 | } |
221 | m->op->stop(m, p); | 221 | m->op->stop(m, p); |
222 | m->count = 0; | 222 | m->count = 0; |
223 | goto Done; | 223 | goto Done; |
224 | Fill: | 224 | Fill: |
225 | /* they want more? let's try to get some more */ | 225 | /* they want more? let's try to get some more */ |
226 | while (m->count < size) { | 226 | while (m->count < size) { |
227 | size_t offs = m->count; | 227 | size_t offs = m->count; |
228 | loff_t next = pos; | 228 | loff_t next = pos; |
229 | p = m->op->next(m, p, &next); | 229 | p = m->op->next(m, p, &next); |
230 | if (!p || IS_ERR(p)) { | 230 | if (!p || IS_ERR(p)) { |
231 | err = PTR_ERR(p); | 231 | err = PTR_ERR(p); |
232 | break; | 232 | break; |
233 | } | 233 | } |
234 | err = m->op->show(m, p); | 234 | err = m->op->show(m, p); |
235 | if (m->count == m->size || err) { | 235 | if (m->count == m->size || err) { |
236 | m->count = offs; | 236 | m->count = offs; |
237 | if (likely(err <= 0)) | 237 | if (likely(err <= 0)) |
238 | break; | 238 | break; |
239 | } | 239 | } |
240 | pos = next; | 240 | pos = next; |
241 | } | 241 | } |
242 | m->op->stop(m, p); | 242 | m->op->stop(m, p); |
243 | n = min(m->count, size); | 243 | n = min(m->count, size); |
244 | err = copy_to_user(buf, m->buf, n); | 244 | err = copy_to_user(buf, m->buf, n); |
245 | if (err) | 245 | if (err) |
246 | goto Efault; | 246 | goto Efault; |
247 | copied += n; | 247 | copied += n; |
248 | m->count -= n; | 248 | m->count -= n; |
249 | if (m->count) | 249 | if (m->count) |
250 | m->from = n; | 250 | m->from = n; |
251 | else | 251 | else |
252 | pos++; | 252 | pos++; |
253 | m->index = pos; | 253 | m->index = pos; |
254 | Done: | 254 | Done: |
255 | if (!copied) | 255 | if (!copied) |
256 | copied = err; | 256 | copied = err; |
257 | else { | 257 | else { |
258 | *ppos += copied; | 258 | *ppos += copied; |
259 | m->read_pos += copied; | 259 | m->read_pos += copied; |
260 | } | 260 | } |
261 | file->f_version = m->version; | 261 | file->f_version = m->version; |
262 | mutex_unlock(&m->lock); | 262 | mutex_unlock(&m->lock); |
263 | return copied; | 263 | return copied; |
264 | Enomem: | 264 | Enomem: |
265 | err = -ENOMEM; | 265 | err = -ENOMEM; |
266 | goto Done; | 266 | goto Done; |
267 | Efault: | 267 | Efault: |
268 | err = -EFAULT; | 268 | err = -EFAULT; |
269 | goto Done; | 269 | goto Done; |
270 | } | 270 | } |
271 | EXPORT_SYMBOL(seq_read); | 271 | EXPORT_SYMBOL(seq_read); |
272 | 272 | ||
273 | /** | 273 | /** |
274 | * seq_lseek - ->llseek() method for sequential files. | 274 | * seq_lseek - ->llseek() method for sequential files. |
275 | * @file: the file in question | 275 | * @file: the file in question |
276 | * @offset: new position | 276 | * @offset: new position |
277 | * @origin: 0 for absolute, 1 for relative position | 277 | * @origin: 0 for absolute, 1 for relative position |
278 | * | 278 | * |
279 | * Ready-made ->f_op->llseek() | 279 | * Ready-made ->f_op->llseek() |
280 | */ | 280 | */ |
281 | loff_t seq_lseek(struct file *file, loff_t offset, int origin) | 281 | loff_t seq_lseek(struct file *file, loff_t offset, int origin) |
282 | { | 282 | { |
283 | struct seq_file *m = (struct seq_file *)file->private_data; | 283 | struct seq_file *m = (struct seq_file *)file->private_data; |
284 | loff_t retval = -EINVAL; | 284 | loff_t retval = -EINVAL; |
285 | 285 | ||
286 | mutex_lock(&m->lock); | 286 | mutex_lock(&m->lock); |
287 | m->version = file->f_version; | 287 | m->version = file->f_version; |
288 | switch (origin) { | 288 | switch (origin) { |
289 | case 1: | 289 | case 1: |
290 | offset += file->f_pos; | 290 | offset += file->f_pos; |
291 | case 0: | 291 | case 0: |
292 | if (offset < 0) | 292 | if (offset < 0) |
293 | break; | 293 | break; |
294 | retval = offset; | 294 | retval = offset; |
295 | if (offset != m->read_pos) { | 295 | if (offset != m->read_pos) { |
296 | while ((retval=traverse(m, offset)) == -EAGAIN) | 296 | while ((retval=traverse(m, offset)) == -EAGAIN) |
297 | ; | 297 | ; |
298 | if (retval) { | 298 | if (retval) { |
299 | /* with extreme prejudice... */ | 299 | /* with extreme prejudice... */ |
300 | file->f_pos = 0; | 300 | file->f_pos = 0; |
301 | m->read_pos = 0; | 301 | m->read_pos = 0; |
302 | m->version = 0; | 302 | m->version = 0; |
303 | m->index = 0; | 303 | m->index = 0; |
304 | m->count = 0; | 304 | m->count = 0; |
305 | } else { | 305 | } else { |
306 | m->read_pos = offset; | 306 | m->read_pos = offset; |
307 | retval = file->f_pos = offset; | 307 | retval = file->f_pos = offset; |
308 | } | 308 | } |
309 | } | 309 | } |
310 | } | 310 | } |
311 | file->f_version = m->version; | 311 | file->f_version = m->version; |
312 | mutex_unlock(&m->lock); | 312 | mutex_unlock(&m->lock); |
313 | return retval; | 313 | return retval; |
314 | } | 314 | } |
315 | EXPORT_SYMBOL(seq_lseek); | 315 | EXPORT_SYMBOL(seq_lseek); |
316 | 316 | ||
317 | /** | 317 | /** |
318 | * seq_release - free the structures associated with sequential file. | 318 | * seq_release - free the structures associated with sequential file. |
319 | * @file: file in question | 319 | * @file: file in question |
320 | * @inode: file->f_path.dentry->d_inode | 320 | * @inode: file->f_path.dentry->d_inode |
321 | * | 321 | * |
322 | * Frees the structures associated with sequential file; can be used | 322 | * Frees the structures associated with sequential file; can be used |
323 | * as ->f_op->release() if you don't have private data to destroy. | 323 | * as ->f_op->release() if you don't have private data to destroy. |
324 | */ | 324 | */ |
325 | int seq_release(struct inode *inode, struct file *file) | 325 | int seq_release(struct inode *inode, struct file *file) |
326 | { | 326 | { |
327 | struct seq_file *m = (struct seq_file *)file->private_data; | 327 | struct seq_file *m = (struct seq_file *)file->private_data; |
328 | kfree(m->buf); | 328 | kfree(m->buf); |
329 | kfree(m); | 329 | kfree(m); |
330 | return 0; | 330 | return 0; |
331 | } | 331 | } |
332 | EXPORT_SYMBOL(seq_release); | 332 | EXPORT_SYMBOL(seq_release); |
333 | 333 | ||
334 | /** | 334 | /** |
335 | * seq_escape - print string into buffer, escaping some characters | 335 | * seq_escape - print string into buffer, escaping some characters |
336 | * @m: target buffer | 336 | * @m: target buffer |
337 | * @s: string | 337 | * @s: string |
338 | * @esc: set of characters that need escaping | 338 | * @esc: set of characters that need escaping |
339 | * | 339 | * |
340 | * Puts string into buffer, replacing each occurrence of character from | 340 | * Puts string into buffer, replacing each occurrence of character from |
341 | * @esc with usual octal escape. Returns 0 in case of success, -1 - in | 341 | * @esc with usual octal escape. Returns 0 in case of success, -1 - in |
342 | * case of overflow. | 342 | * case of overflow. |
343 | */ | 343 | */ |
344 | int seq_escape(struct seq_file *m, const char *s, const char *esc) | 344 | int seq_escape(struct seq_file *m, const char *s, const char *esc) |
345 | { | 345 | { |
346 | char *end = m->buf + m->size; | 346 | char *end = m->buf + m->size; |
347 | char *p; | 347 | char *p; |
348 | char c; | 348 | char c; |
349 | 349 | ||
350 | for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) { | 350 | for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) { |
351 | if (!strchr(esc, c)) { | 351 | if (!strchr(esc, c)) { |
352 | *p++ = c; | 352 | *p++ = c; |
353 | continue; | 353 | continue; |
354 | } | 354 | } |
355 | if (p + 3 < end) { | 355 | if (p + 3 < end) { |
356 | *p++ = '\\'; | 356 | *p++ = '\\'; |
357 | *p++ = '0' + ((c & 0300) >> 6); | 357 | *p++ = '0' + ((c & 0300) >> 6); |
358 | *p++ = '0' + ((c & 070) >> 3); | 358 | *p++ = '0' + ((c & 070) >> 3); |
359 | *p++ = '0' + (c & 07); | 359 | *p++ = '0' + (c & 07); |
360 | continue; | 360 | continue; |
361 | } | 361 | } |
362 | m->count = m->size; | 362 | m->count = m->size; |
363 | return -1; | 363 | return -1; |
364 | } | 364 | } |
365 | m->count = p - m->buf; | 365 | m->count = p - m->buf; |
366 | return 0; | 366 | return 0; |
367 | } | 367 | } |
368 | EXPORT_SYMBOL(seq_escape); | 368 | EXPORT_SYMBOL(seq_escape); |
369 | 369 | ||
370 | int seq_printf(struct seq_file *m, const char *f, ...) | 370 | int seq_printf(struct seq_file *m, const char *f, ...) |
371 | { | 371 | { |
372 | va_list args; | 372 | va_list args; |
373 | int len; | 373 | int len; |
374 | 374 | ||
375 | if (m->count < m->size) { | 375 | if (m->count < m->size) { |
376 | va_start(args, f); | 376 | va_start(args, f); |
377 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); | 377 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); |
378 | va_end(args); | 378 | va_end(args); |
379 | if (m->count + len < m->size) { | 379 | if (m->count + len < m->size) { |
380 | m->count += len; | 380 | m->count += len; |
381 | return 0; | 381 | return 0; |
382 | } | 382 | } |
383 | } | 383 | } |
384 | m->count = m->size; | 384 | m->count = m->size; |
385 | return -1; | 385 | return -1; |
386 | } | 386 | } |
387 | EXPORT_SYMBOL(seq_printf); | 387 | EXPORT_SYMBOL(seq_printf); |
388 | 388 | ||
389 | /** | 389 | /** |
390 | * mangle_path - mangle and copy path to buffer beginning | 390 | * mangle_path - mangle and copy path to buffer beginning |
391 | * @s: buffer start | 391 | * @s: buffer start |
392 | * @p: beginning of path in above buffer | 392 | * @p: beginning of path in above buffer |
393 | * @esc: set of characters that need escaping | 393 | * @esc: set of characters that need escaping |
394 | * | 394 | * |
395 | * Copy the path from @p to @s, replacing each occurrence of character from | 395 | * Copy the path from @p to @s, replacing each occurrence of character from |
396 | * @esc with usual octal escape. | 396 | * @esc with usual octal escape. |
397 | * Returns pointer past last written character in @s, or NULL in case of | 397 | * Returns pointer past last written character in @s, or NULL in case of |
398 | * failure. | 398 | * failure. |
399 | */ | 399 | */ |
400 | char *mangle_path(char *s, char *p, char *esc) | 400 | char *mangle_path(char *s, char *p, char *esc) |
401 | { | 401 | { |
402 | while (s <= p) { | 402 | while (s <= p) { |
403 | char c = *p++; | 403 | char c = *p++; |
404 | if (!c) { | 404 | if (!c) { |
405 | return s; | 405 | return s; |
406 | } else if (!strchr(esc, c)) { | 406 | } else if (!strchr(esc, c)) { |
407 | *s++ = c; | 407 | *s++ = c; |
408 | } else if (s + 4 > p) { | 408 | } else if (s + 4 > p) { |
409 | break; | 409 | break; |
410 | } else { | 410 | } else { |
411 | *s++ = '\\'; | 411 | *s++ = '\\'; |
412 | *s++ = '0' + ((c & 0300) >> 6); | 412 | *s++ = '0' + ((c & 0300) >> 6); |
413 | *s++ = '0' + ((c & 070) >> 3); | 413 | *s++ = '0' + ((c & 070) >> 3); |
414 | *s++ = '0' + (c & 07); | 414 | *s++ = '0' + (c & 07); |
415 | } | 415 | } |
416 | } | 416 | } |
417 | return NULL; | 417 | return NULL; |
418 | } | 418 | } |
419 | EXPORT_SYMBOL(mangle_path); | 419 | EXPORT_SYMBOL(mangle_path); |
420 | 420 | ||
421 | /** | 421 | /** |
422 | * seq_path - seq_file interface to print a pathname | 422 | * seq_path - seq_file interface to print a pathname |
423 | * @m: the seq_file handle | 423 | * @m: the seq_file handle |
424 | * @path: the struct path to print | 424 | * @path: the struct path to print |
425 | * @esc: set of characters to escape in the output | 425 | * @esc: set of characters to escape in the output |
426 | * | 426 | * |
427 | * return the absolute path of 'path', as represented by the | 427 | * return the absolute path of 'path', as represented by the |
428 | * dentry / mnt pair in the path parameter. | 428 | * dentry / mnt pair in the path parameter. |
429 | */ | 429 | */ |
430 | int seq_path(struct seq_file *m, struct path *path, char *esc) | 430 | int seq_path(struct seq_file *m, struct path *path, char *esc) |
431 | { | 431 | { |
432 | if (m->count < m->size) { | 432 | if (m->count < m->size) { |
433 | char *s = m->buf + m->count; | 433 | char *s = m->buf + m->count; |
434 | char *p = d_path(path, s, m->size - m->count); | 434 | char *p = d_path(path, s, m->size - m->count); |
435 | if (!IS_ERR(p)) { | 435 | if (!IS_ERR(p)) { |
436 | s = mangle_path(s, p, esc); | 436 | s = mangle_path(s, p, esc); |
437 | if (s) { | 437 | if (s) { |
438 | p = m->buf + m->count; | 438 | p = m->buf + m->count; |
439 | m->count = s - m->buf; | 439 | m->count = s - m->buf; |
440 | return s - p; | 440 | return s - p; |
441 | } | 441 | } |
442 | } | 442 | } |
443 | } | 443 | } |
444 | m->count = m->size; | 444 | m->count = m->size; |
445 | return -1; | 445 | return -1; |
446 | } | 446 | } |
447 | EXPORT_SYMBOL(seq_path); | 447 | EXPORT_SYMBOL(seq_path); |
448 | 448 | ||
449 | /* | 449 | /* |
450 | * Same as seq_path, but relative to supplied root. | 450 | * Same as seq_path, but relative to supplied root. |
451 | * | 451 | * |
452 | * root may be changed, see __d_path(). | 452 | * root may be changed, see __d_path(). |
453 | */ | 453 | */ |
454 | int seq_path_root(struct seq_file *m, struct path *path, struct path *root, | 454 | int seq_path_root(struct seq_file *m, struct path *path, struct path *root, |
455 | char *esc) | 455 | char *esc) |
456 | { | 456 | { |
457 | int err = -ENAMETOOLONG; | 457 | int err = -ENAMETOOLONG; |
458 | if (m->count < m->size) { | 458 | if (m->count < m->size) { |
459 | char *s = m->buf + m->count; | 459 | char *s = m->buf + m->count; |
460 | char *p; | 460 | char *p; |
461 | 461 | ||
462 | spin_lock(&dcache_lock); | 462 | spin_lock(&dcache_lock); |
463 | p = __d_path(path, root, s, m->size - m->count); | 463 | p = __d_path(path, root, s, m->size - m->count); |
464 | spin_unlock(&dcache_lock); | 464 | spin_unlock(&dcache_lock); |
465 | err = PTR_ERR(p); | 465 | err = PTR_ERR(p); |
466 | if (!IS_ERR(p)) { | 466 | if (!IS_ERR(p)) { |
467 | s = mangle_path(s, p, esc); | 467 | s = mangle_path(s, p, esc); |
468 | if (s) { | 468 | if (s) { |
469 | p = m->buf + m->count; | 469 | p = m->buf + m->count; |
470 | m->count = s - m->buf; | 470 | m->count = s - m->buf; |
471 | return 0; | 471 | return 0; |
472 | } | 472 | } |
473 | err = -ENAMETOOLONG; | ||
473 | } | 474 | } |
474 | } | 475 | } |
475 | m->count = m->size; | 476 | m->count = m->size; |
476 | return err; | 477 | return err; |
477 | } | 478 | } |
478 | 479 | ||
479 | /* | 480 | /* |
480 | * returns the path of the 'dentry' from the root of its filesystem. | 481 | * returns the path of the 'dentry' from the root of its filesystem. |
481 | */ | 482 | */ |
482 | int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc) | 483 | int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc) |
483 | { | 484 | { |
484 | if (m->count < m->size) { | 485 | if (m->count < m->size) { |
485 | char *s = m->buf + m->count; | 486 | char *s = m->buf + m->count; |
486 | char *p = dentry_path(dentry, s, m->size - m->count); | 487 | char *p = dentry_path(dentry, s, m->size - m->count); |
487 | if (!IS_ERR(p)) { | 488 | if (!IS_ERR(p)) { |
488 | s = mangle_path(s, p, esc); | 489 | s = mangle_path(s, p, esc); |
489 | if (s) { | 490 | if (s) { |
490 | p = m->buf + m->count; | 491 | p = m->buf + m->count; |
491 | m->count = s - m->buf; | 492 | m->count = s - m->buf; |
492 | return s - p; | 493 | return s - p; |
493 | } | 494 | } |
494 | } | 495 | } |
495 | } | 496 | } |
496 | m->count = m->size; | 497 | m->count = m->size; |
497 | return -1; | 498 | return -1; |
498 | } | 499 | } |
499 | 500 | ||
500 | int seq_bitmap(struct seq_file *m, const unsigned long *bits, | 501 | int seq_bitmap(struct seq_file *m, const unsigned long *bits, |
501 | unsigned int nr_bits) | 502 | unsigned int nr_bits) |
502 | { | 503 | { |
503 | if (m->count < m->size) { | 504 | if (m->count < m->size) { |
504 | int len = bitmap_scnprintf(m->buf + m->count, | 505 | int len = bitmap_scnprintf(m->buf + m->count, |
505 | m->size - m->count, bits, nr_bits); | 506 | m->size - m->count, bits, nr_bits); |
506 | if (m->count + len < m->size) { | 507 | if (m->count + len < m->size) { |
507 | m->count += len; | 508 | m->count += len; |
508 | return 0; | 509 | return 0; |
509 | } | 510 | } |
510 | } | 511 | } |
511 | m->count = m->size; | 512 | m->count = m->size; |
512 | return -1; | 513 | return -1; |
513 | } | 514 | } |
514 | EXPORT_SYMBOL(seq_bitmap); | 515 | EXPORT_SYMBOL(seq_bitmap); |
515 | 516 | ||
516 | int seq_bitmap_list(struct seq_file *m, const unsigned long *bits, | 517 | int seq_bitmap_list(struct seq_file *m, const unsigned long *bits, |
517 | unsigned int nr_bits) | 518 | unsigned int nr_bits) |
518 | { | 519 | { |
519 | if (m->count < m->size) { | 520 | if (m->count < m->size) { |
520 | int len = bitmap_scnlistprintf(m->buf + m->count, | 521 | int len = bitmap_scnlistprintf(m->buf + m->count, |
521 | m->size - m->count, bits, nr_bits); | 522 | m->size - m->count, bits, nr_bits); |
522 | if (m->count + len < m->size) { | 523 | if (m->count + len < m->size) { |
523 | m->count += len; | 524 | m->count += len; |
524 | return 0; | 525 | return 0; |
525 | } | 526 | } |
526 | } | 527 | } |
527 | m->count = m->size; | 528 | m->count = m->size; |
528 | return -1; | 529 | return -1; |
529 | } | 530 | } |
530 | EXPORT_SYMBOL(seq_bitmap_list); | 531 | EXPORT_SYMBOL(seq_bitmap_list); |
531 | 532 | ||
532 | static void *single_start(struct seq_file *p, loff_t *pos) | 533 | static void *single_start(struct seq_file *p, loff_t *pos) |
533 | { | 534 | { |
534 | return NULL + (*pos == 0); | 535 | return NULL + (*pos == 0); |
535 | } | 536 | } |
536 | 537 | ||
537 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) | 538 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) |
538 | { | 539 | { |
539 | ++*pos; | 540 | ++*pos; |
540 | return NULL; | 541 | return NULL; |
541 | } | 542 | } |
542 | 543 | ||
543 | static void single_stop(struct seq_file *p, void *v) | 544 | static void single_stop(struct seq_file *p, void *v) |
544 | { | 545 | { |
545 | } | 546 | } |
546 | 547 | ||
547 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), | 548 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), |
548 | void *data) | 549 | void *data) |
549 | { | 550 | { |
550 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); | 551 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); |
551 | int res = -ENOMEM; | 552 | int res = -ENOMEM; |
552 | 553 | ||
553 | if (op) { | 554 | if (op) { |
554 | op->start = single_start; | 555 | op->start = single_start; |
555 | op->next = single_next; | 556 | op->next = single_next; |
556 | op->stop = single_stop; | 557 | op->stop = single_stop; |
557 | op->show = show; | 558 | op->show = show; |
558 | res = seq_open(file, op); | 559 | res = seq_open(file, op); |
559 | if (!res) | 560 | if (!res) |
560 | ((struct seq_file *)file->private_data)->private = data; | 561 | ((struct seq_file *)file->private_data)->private = data; |
561 | else | 562 | else |
562 | kfree(op); | 563 | kfree(op); |
563 | } | 564 | } |
564 | return res; | 565 | return res; |
565 | } | 566 | } |
566 | EXPORT_SYMBOL(single_open); | 567 | EXPORT_SYMBOL(single_open); |
567 | 568 | ||
568 | int single_release(struct inode *inode, struct file *file) | 569 | int single_release(struct inode *inode, struct file *file) |
569 | { | 570 | { |
570 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; | 571 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; |
571 | int res = seq_release(inode, file); | 572 | int res = seq_release(inode, file); |
572 | kfree(op); | 573 | kfree(op); |
573 | return res; | 574 | return res; |
574 | } | 575 | } |
575 | EXPORT_SYMBOL(single_release); | 576 | EXPORT_SYMBOL(single_release); |
576 | 577 | ||
577 | int seq_release_private(struct inode *inode, struct file *file) | 578 | int seq_release_private(struct inode *inode, struct file *file) |
578 | { | 579 | { |
579 | struct seq_file *seq = file->private_data; | 580 | struct seq_file *seq = file->private_data; |
580 | 581 | ||
581 | kfree(seq->private); | 582 | kfree(seq->private); |
582 | seq->private = NULL; | 583 | seq->private = NULL; |
583 | return seq_release(inode, file); | 584 | return seq_release(inode, file); |
584 | } | 585 | } |
585 | EXPORT_SYMBOL(seq_release_private); | 586 | EXPORT_SYMBOL(seq_release_private); |
586 | 587 | ||
587 | void *__seq_open_private(struct file *f, const struct seq_operations *ops, | 588 | void *__seq_open_private(struct file *f, const struct seq_operations *ops, |
588 | int psize) | 589 | int psize) |
589 | { | 590 | { |
590 | int rc; | 591 | int rc; |
591 | void *private; | 592 | void *private; |
592 | struct seq_file *seq; | 593 | struct seq_file *seq; |
593 | 594 | ||
594 | private = kzalloc(psize, GFP_KERNEL); | 595 | private = kzalloc(psize, GFP_KERNEL); |
595 | if (private == NULL) | 596 | if (private == NULL) |
596 | goto out; | 597 | goto out; |
597 | 598 | ||
598 | rc = seq_open(f, ops); | 599 | rc = seq_open(f, ops); |
599 | if (rc < 0) | 600 | if (rc < 0) |
600 | goto out_free; | 601 | goto out_free; |
601 | 602 | ||
602 | seq = f->private_data; | 603 | seq = f->private_data; |
603 | seq->private = private; | 604 | seq->private = private; |
604 | return private; | 605 | return private; |
605 | 606 | ||
606 | out_free: | 607 | out_free: |
607 | kfree(private); | 608 | kfree(private); |
608 | out: | 609 | out: |
609 | return NULL; | 610 | return NULL; |
610 | } | 611 | } |
611 | EXPORT_SYMBOL(__seq_open_private); | 612 | EXPORT_SYMBOL(__seq_open_private); |
612 | 613 | ||
613 | int seq_open_private(struct file *filp, const struct seq_operations *ops, | 614 | int seq_open_private(struct file *filp, const struct seq_operations *ops, |
614 | int psize) | 615 | int psize) |
615 | { | 616 | { |
616 | return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; | 617 | return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; |
617 | } | 618 | } |
618 | EXPORT_SYMBOL(seq_open_private); | 619 | EXPORT_SYMBOL(seq_open_private); |
619 | 620 | ||
620 | int seq_putc(struct seq_file *m, char c) | 621 | int seq_putc(struct seq_file *m, char c) |
621 | { | 622 | { |
622 | if (m->count < m->size) { | 623 | if (m->count < m->size) { |
623 | m->buf[m->count++] = c; | 624 | m->buf[m->count++] = c; |
624 | return 0; | 625 | return 0; |
625 | } | 626 | } |
626 | return -1; | 627 | return -1; |
627 | } | 628 | } |
628 | EXPORT_SYMBOL(seq_putc); | 629 | EXPORT_SYMBOL(seq_putc); |
629 | 630 | ||
630 | int seq_puts(struct seq_file *m, const char *s) | 631 | int seq_puts(struct seq_file *m, const char *s) |
631 | { | 632 | { |
632 | int len = strlen(s); | 633 | int len = strlen(s); |
633 | if (m->count + len < m->size) { | 634 | if (m->count + len < m->size) { |
634 | memcpy(m->buf + m->count, s, len); | 635 | memcpy(m->buf + m->count, s, len); |
635 | m->count += len; | 636 | m->count += len; |
636 | return 0; | 637 | return 0; |
637 | } | 638 | } |
638 | m->count = m->size; | 639 | m->count = m->size; |
639 | return -1; | 640 | return -1; |
640 | } | 641 | } |
641 | EXPORT_SYMBOL(seq_puts); | 642 | EXPORT_SYMBOL(seq_puts); |
642 | 643 | ||
643 | /** | 644 | /** |
644 | * seq_write - write arbitrary data to buffer | 645 | * seq_write - write arbitrary data to buffer |
645 | * @seq: seq_file identifying the buffer to which data should be written | 646 | * @seq: seq_file identifying the buffer to which data should be written |
646 | * @data: data address | 647 | * @data: data address |
647 | * @len: number of bytes | 648 | * @len: number of bytes |
648 | * | 649 | * |
649 | * Return 0 on success, non-zero otherwise. | 650 | * Return 0 on success, non-zero otherwise. |
650 | */ | 651 | */ |
651 | int seq_write(struct seq_file *seq, const void *data, size_t len) | 652 | int seq_write(struct seq_file *seq, const void *data, size_t len) |
652 | { | 653 | { |
653 | if (seq->count + len < seq->size) { | 654 | if (seq->count + len < seq->size) { |
654 | memcpy(seq->buf + seq->count, data, len); | 655 | memcpy(seq->buf + seq->count, data, len); |
655 | seq->count += len; | 656 | seq->count += len; |
656 | return 0; | 657 | return 0; |
657 | } | 658 | } |
658 | seq->count = seq->size; | 659 | seq->count = seq->size; |
659 | return -1; | 660 | return -1; |
660 | } | 661 | } |
661 | EXPORT_SYMBOL(seq_write); | 662 | EXPORT_SYMBOL(seq_write); |
662 | 663 | ||
663 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) | 664 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) |
664 | { | 665 | { |
665 | struct list_head *lh; | 666 | struct list_head *lh; |
666 | 667 | ||
667 | list_for_each(lh, head) | 668 | list_for_each(lh, head) |
668 | if (pos-- == 0) | 669 | if (pos-- == 0) |
669 | return lh; | 670 | return lh; |
670 | 671 | ||
671 | return NULL; | 672 | return NULL; |
672 | } | 673 | } |
673 | 674 | ||
674 | EXPORT_SYMBOL(seq_list_start); | 675 | EXPORT_SYMBOL(seq_list_start); |
675 | 676 | ||
676 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) | 677 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) |
677 | { | 678 | { |
678 | if (!pos) | 679 | if (!pos) |
679 | return head; | 680 | return head; |
680 | 681 | ||
681 | return seq_list_start(head, pos - 1); | 682 | return seq_list_start(head, pos - 1); |
682 | } | 683 | } |
683 | 684 | ||
684 | EXPORT_SYMBOL(seq_list_start_head); | 685 | EXPORT_SYMBOL(seq_list_start_head); |
685 | 686 | ||
686 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) | 687 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) |
687 | { | 688 | { |
688 | struct list_head *lh; | 689 | struct list_head *lh; |
689 | 690 | ||
690 | lh = ((struct list_head *)v)->next; | 691 | lh = ((struct list_head *)v)->next; |
691 | ++*ppos; | 692 | ++*ppos; |
692 | return lh == head ? NULL : lh; | 693 | return lh == head ? NULL : lh; |
693 | } | 694 | } |
694 | 695 | ||
695 | EXPORT_SYMBOL(seq_list_next); | 696 | EXPORT_SYMBOL(seq_list_next); |
696 | 697 |