Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/seq_file.c | |
3 | * | |
4 | * helper functions for making synthetic files from sequences of records. | |
5 | * initial implementation -- AV, Oct 2001. | |
6 | */ | |
7 | ||
8 | #include <linux/fs.h> | |
630d9c47 | 9 | #include <linux/export.h> |
1da177e4 | 10 | #include <linux/seq_file.h> |
058504ed | 11 | #include <linux/vmalloc.h> |
1da177e4 | 12 | #include <linux/slab.h> |
adb37c4c | 13 | #include <linux/cred.h> |
058504ed | 14 | #include <linux/mm.h> |
1da177e4 LT |
15 | |
16 | #include <asm/uaccess.h> | |
17 | #include <asm/page.h> | |
18 | ||
e075f591 KH |
19 | static void seq_set_overflow(struct seq_file *m) |
20 | { | |
21 | m->count = m->size; | |
22 | } | |
23 | ||
058504ed HC |
24 | static void *seq_buf_alloc(unsigned long size) |
25 | { | |
26 | void *buf; | |
27 | ||
5cec38ac DR |
28 | /* |
29 | * __GFP_NORETRY to avoid oom-killings with high-order allocations - | |
30 | * it's better to fall back to vmalloc() than to kill things. | |
31 | */ | |
32 | buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); | |
058504ed HC |
33 | if (!buf && size > PAGE_SIZE) |
34 | buf = vmalloc(size); | |
35 | return buf; | |
36 | } | |
37 | ||
1da177e4 LT |
38 | /** |
39 | * seq_open - initialize sequential file | |
40 | * @file: file we initialize | |
41 | * @op: method table describing the sequence | |
42 | * | |
43 | * seq_open() sets @file, associating it with a sequence described | |
44 | * by @op. @op->start() sets the iterator up and returns the first | |
45 | * element of sequence. @op->stop() shuts it down. @op->next() | |
46 | * returns the next element of sequence. @op->show() prints element | |
47 | * into the buffer. In case of error ->start() and ->next() return | |
48 | * ERR_PTR(error). In the end of sequence they return %NULL. ->show() | |
49 | * returns 0 in case of success and negative number in case of error. | |
521b5d0c | 50 | * Returning SEQ_SKIP means "discard this element and move on". |
460b865e YD |
51 | * Note: seq_open() will allocate a struct seq_file and store its |
52 | * pointer in @file->private_data. This pointer should not be modified. | |
1da177e4 | 53 | */ |
15ad7cdc | 54 | int seq_open(struct file *file, const struct seq_operations *op) |
1da177e4 | 55 | { |
189f9841 YD |
56 | struct seq_file *p; |
57 | ||
58 | WARN_ON(file->private_data); | |
59 | ||
60 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
61 | if (!p) | |
62 | return -ENOMEM; | |
63 | ||
64 | file->private_data = p; | |
1abe77b0 | 65 | |
0ac1759a | 66 | mutex_init(&p->lock); |
1da177e4 | 67 | p->op = op; |
adb37c4c EB |
68 | #ifdef CONFIG_USER_NS |
69 | p->user_ns = file->f_cred->user_ns; | |
70 | #endif | |
1da177e4 LT |
71 | |
72 | /* | |
73 | * Wrappers around seq_open(e.g. swaps_open) need to be | |
74 | * aware of this. If they set f_version themselves, they | |
75 | * should call seq_open first and then set f_version. | |
76 | */ | |
77 | file->f_version = 0; | |
78 | ||
8f19d472 EB |
79 | /* |
80 | * seq_files support lseek() and pread(). They do not implement | |
81 | * write() at all, but we clear FMODE_PWRITE here for historical | |
82 | * reasons. | |
83 | * | |
84 | * If a client of seq_files a) implements file.write() and b) wishes to | |
85 | * support pwrite() then that client will need to implement its own | |
86 | * file.open() which calls seq_open() and then sets FMODE_PWRITE. | |
87 | */ | |
88 | file->f_mode &= ~FMODE_PWRITE; | |
1da177e4 LT |
89 | return 0; |
90 | } | |
91 | EXPORT_SYMBOL(seq_open); | |
92 | ||
33da8892 EB |
93 | static int traverse(struct seq_file *m, loff_t offset) |
94 | { | |
95 | loff_t pos = 0, index; | |
96 | int error = 0; | |
97 | void *p; | |
98 | ||
99 | m->version = 0; | |
100 | index = 0; | |
101 | m->count = m->from = 0; | |
102 | if (!offset) { | |
103 | m->index = index; | |
104 | return 0; | |
105 | } | |
106 | if (!m->buf) { | |
058504ed | 107 | m->buf = seq_buf_alloc(m->size = PAGE_SIZE); |
33da8892 EB |
108 | if (!m->buf) |
109 | return -ENOMEM; | |
110 | } | |
111 | p = m->op->start(m, &index); | |
112 | while (p) { | |
113 | error = PTR_ERR(p); | |
114 | if (IS_ERR(p)) | |
115 | break; | |
116 | error = m->op->show(m, p); | |
117 | if (error < 0) | |
118 | break; | |
119 | if (unlikely(error)) { | |
120 | error = 0; | |
121 | m->count = 0; | |
122 | } | |
1f33c41c | 123 | if (seq_has_overflowed(m)) |
33da8892 EB |
124 | goto Eoverflow; |
125 | if (pos + m->count > offset) { | |
126 | m->from = offset - pos; | |
127 | m->count -= m->from; | |
128 | m->index = index; | |
129 | break; | |
130 | } | |
131 | pos += m->count; | |
132 | m->count = 0; | |
133 | if (pos == offset) { | |
134 | index++; | |
135 | m->index = index; | |
136 | break; | |
137 | } | |
138 | p = m->op->next(m, p, &index); | |
139 | } | |
140 | m->op->stop(m, p); | |
f01d1d54 | 141 | m->index = index; |
33da8892 EB |
142 | return error; |
143 | ||
144 | Eoverflow: | |
145 | m->op->stop(m, p); | |
058504ed | 146 | kvfree(m->buf); |
801a7605 | 147 | m->count = 0; |
058504ed | 148 | m->buf = seq_buf_alloc(m->size <<= 1); |
33da8892 EB |
149 | return !m->buf ? -ENOMEM : -EAGAIN; |
150 | } | |
151 | ||
1da177e4 LT |
152 | /** |
153 | * seq_read - ->read() method for sequential files. | |
67be2dd1 MW |
154 | * @file: the file to read from |
155 | * @buf: the buffer to read to | |
156 | * @size: the maximum number of bytes to read | |
157 | * @ppos: the current position in the file | |
1da177e4 LT |
158 | * |
159 | * Ready-made ->f_op->read() | |
160 | */ | |
161 | ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) | |
162 | { | |
8209e2f4 | 163 | struct seq_file *m = file->private_data; |
1da177e4 LT |
164 | size_t copied = 0; |
165 | loff_t pos; | |
166 | size_t n; | |
167 | void *p; | |
168 | int err = 0; | |
169 | ||
0ac1759a | 170 | mutex_lock(&m->lock); |
8f19d472 | 171 | |
7904ac84 EC |
172 | /* |
173 | * seq_file->op->..m_start/m_stop/m_next may do special actions | |
174 | * or optimisations based on the file->f_version, so we want to | |
175 | * pass the file->f_version to those methods. | |
176 | * | |
177 | * seq_file->version is just copy of f_version, and seq_file | |
178 | * methods can treat it simply as file version. | |
179 | * It is copied in first and copied out after all operations. | |
180 | * It is convenient to have it as part of structure to avoid the | |
181 | * need of passing another argument to all the seq_file methods. | |
182 | */ | |
183 | m->version = file->f_version; | |
184 | ||
8f19d472 EB |
185 | /* Don't assume *ppos is where we left it */ |
186 | if (unlikely(*ppos != m->read_pos)) { | |
8f19d472 EB |
187 | while ((err = traverse(m, *ppos)) == -EAGAIN) |
188 | ; | |
189 | if (err) { | |
190 | /* With prejudice... */ | |
191 | m->read_pos = 0; | |
192 | m->version = 0; | |
193 | m->index = 0; | |
194 | m->count = 0; | |
195 | goto Done; | |
7904ac84 EC |
196 | } else { |
197 | m->read_pos = *ppos; | |
8f19d472 EB |
198 | } |
199 | } | |
200 | ||
1da177e4 LT |
201 | /* grab buffer if we didn't have one */ |
202 | if (!m->buf) { | |
058504ed | 203 | m->buf = seq_buf_alloc(m->size = PAGE_SIZE); |
1da177e4 LT |
204 | if (!m->buf) |
205 | goto Enomem; | |
206 | } | |
207 | /* if not empty - flush it first */ | |
208 | if (m->count) { | |
209 | n = min(m->count, size); | |
210 | err = copy_to_user(buf, m->buf + m->from, n); | |
211 | if (err) | |
212 | goto Efault; | |
213 | m->count -= n; | |
214 | m->from += n; | |
215 | size -= n; | |
216 | buf += n; | |
217 | copied += n; | |
218 | if (!m->count) | |
219 | m->index++; | |
220 | if (!size) | |
221 | goto Done; | |
222 | } | |
223 | /* we need at least one record in buffer */ | |
4cdfe84b AV |
224 | pos = m->index; |
225 | p = m->op->start(m, &pos); | |
1da177e4 | 226 | while (1) { |
1da177e4 LT |
227 | err = PTR_ERR(p); |
228 | if (!p || IS_ERR(p)) | |
229 | break; | |
230 | err = m->op->show(m, p); | |
521b5d0c | 231 | if (err < 0) |
1da177e4 | 232 | break; |
521b5d0c AV |
233 | if (unlikely(err)) |
234 | m->count = 0; | |
4cdfe84b AV |
235 | if (unlikely(!m->count)) { |
236 | p = m->op->next(m, p, &pos); | |
237 | m->index = pos; | |
238 | continue; | |
239 | } | |
1da177e4 LT |
240 | if (m->count < m->size) |
241 | goto Fill; | |
242 | m->op->stop(m, p); | |
058504ed | 243 | kvfree(m->buf); |
801a7605 | 244 | m->count = 0; |
058504ed | 245 | m->buf = seq_buf_alloc(m->size <<= 1); |
1da177e4 LT |
246 | if (!m->buf) |
247 | goto Enomem; | |
1da177e4 | 248 | m->version = 0; |
4cdfe84b AV |
249 | pos = m->index; |
250 | p = m->op->start(m, &pos); | |
1da177e4 LT |
251 | } |
252 | m->op->stop(m, p); | |
253 | m->count = 0; | |
254 | goto Done; | |
255 | Fill: | |
256 | /* they want more? let's try to get some more */ | |
257 | while (m->count < size) { | |
258 | size_t offs = m->count; | |
259 | loff_t next = pos; | |
260 | p = m->op->next(m, p, &next); | |
261 | if (!p || IS_ERR(p)) { | |
262 | err = PTR_ERR(p); | |
263 | break; | |
264 | } | |
265 | err = m->op->show(m, p); | |
1f33c41c | 266 | if (seq_has_overflowed(m) || err) { |
1da177e4 | 267 | m->count = offs; |
521b5d0c AV |
268 | if (likely(err <= 0)) |
269 | break; | |
1da177e4 LT |
270 | } |
271 | pos = next; | |
272 | } | |
273 | m->op->stop(m, p); | |
274 | n = min(m->count, size); | |
275 | err = copy_to_user(buf, m->buf, n); | |
276 | if (err) | |
277 | goto Efault; | |
278 | copied += n; | |
279 | m->count -= n; | |
280 | if (m->count) | |
281 | m->from = n; | |
282 | else | |
283 | pos++; | |
284 | m->index = pos; | |
285 | Done: | |
286 | if (!copied) | |
287 | copied = err; | |
8f19d472 | 288 | else { |
1da177e4 | 289 | *ppos += copied; |
8f19d472 EB |
290 | m->read_pos += copied; |
291 | } | |
1da177e4 | 292 | file->f_version = m->version; |
0ac1759a | 293 | mutex_unlock(&m->lock); |
1da177e4 LT |
294 | return copied; |
295 | Enomem: | |
296 | err = -ENOMEM; | |
297 | goto Done; | |
298 | Efault: | |
299 | err = -EFAULT; | |
300 | goto Done; | |
301 | } | |
302 | EXPORT_SYMBOL(seq_read); | |
303 | ||
1da177e4 LT |
304 | /** |
305 | * seq_lseek - ->llseek() method for sequential files. | |
67be2dd1 MW |
306 | * @file: the file in question |
307 | * @offset: new position | |
254adaa4 | 308 | * @whence: 0 for absolute, 1 for relative position |
1da177e4 LT |
309 | * |
310 | * Ready-made ->f_op->llseek() | |
311 | */ | |
965c8e59 | 312 | loff_t seq_lseek(struct file *file, loff_t offset, int whence) |
1da177e4 | 313 | { |
8209e2f4 | 314 | struct seq_file *m = file->private_data; |
16abef0e | 315 | loff_t retval = -EINVAL; |
1da177e4 | 316 | |
0ac1759a | 317 | mutex_lock(&m->lock); |
1da177e4 | 318 | m->version = file->f_version; |
965c8e59 | 319 | switch (whence) { |
5e62adef AM |
320 | case SEEK_CUR: |
321 | offset += file->f_pos; | |
322 | case SEEK_SET: | |
323 | if (offset < 0) | |
324 | break; | |
325 | retval = offset; | |
326 | if (offset != m->read_pos) { | |
327 | while ((retval = traverse(m, offset)) == -EAGAIN) | |
328 | ; | |
329 | if (retval) { | |
330 | /* with extreme prejudice... */ | |
331 | file->f_pos = 0; | |
332 | m->read_pos = 0; | |
333 | m->version = 0; | |
334 | m->index = 0; | |
335 | m->count = 0; | |
336 | } else { | |
337 | m->read_pos = offset; | |
338 | retval = file->f_pos = offset; | |
1da177e4 | 339 | } |
05e16745 GZ |
340 | } else { |
341 | file->f_pos = offset; | |
5e62adef | 342 | } |
1da177e4 | 343 | } |
1da177e4 | 344 | file->f_version = m->version; |
00c5746d | 345 | mutex_unlock(&m->lock); |
1da177e4 LT |
346 | return retval; |
347 | } | |
348 | EXPORT_SYMBOL(seq_lseek); | |
349 | ||
350 | /** | |
351 | * seq_release - free the structures associated with sequential file. | |
352 | * @file: file in question | |
6131ffaa | 353 | * @inode: its inode |
1da177e4 LT |
354 | * |
355 | * Frees the structures associated with sequential file; can be used | |
356 | * as ->f_op->release() if you don't have private data to destroy. | |
357 | */ | |
358 | int seq_release(struct inode *inode, struct file *file) | |
359 | { | |
8209e2f4 | 360 | struct seq_file *m = file->private_data; |
058504ed | 361 | kvfree(m->buf); |
1da177e4 LT |
362 | kfree(m); |
363 | return 0; | |
364 | } | |
365 | EXPORT_SYMBOL(seq_release); | |
366 | ||
367 | /** | |
368 | * seq_escape - print string into buffer, escaping some characters | |
369 | * @m: target buffer | |
370 | * @s: string | |
371 | * @esc: set of characters that need escaping | |
372 | * | |
373 | * Puts string into buffer, replacing each occurrence of character from | |
374 | * @esc with usual octal escape. Returns 0 in case of success, -1 - in | |
375 | * case of overflow. | |
376 | */ | |
377 | int seq_escape(struct seq_file *m, const char *s, const char *esc) | |
378 | { | |
379 | char *end = m->buf + m->size; | |
380 | char *p; | |
381 | char c; | |
382 | ||
383 | for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) { | |
384 | if (!strchr(esc, c)) { | |
385 | *p++ = c; | |
386 | continue; | |
387 | } | |
388 | if (p + 3 < end) { | |
389 | *p++ = '\\'; | |
390 | *p++ = '0' + ((c & 0300) >> 6); | |
391 | *p++ = '0' + ((c & 070) >> 3); | |
392 | *p++ = '0' + (c & 07); | |
393 | continue; | |
394 | } | |
e075f591 | 395 | seq_set_overflow(m); |
1da177e4 LT |
396 | return -1; |
397 | } | |
398 | m->count = p - m->buf; | |
399 | return 0; | |
400 | } | |
401 | EXPORT_SYMBOL(seq_escape); | |
402 | ||
a4808147 | 403 | int seq_vprintf(struct seq_file *m, const char *f, va_list args) |
1da177e4 | 404 | { |
1da177e4 LT |
405 | int len; |
406 | ||
407 | if (m->count < m->size) { | |
1da177e4 | 408 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); |
1da177e4 LT |
409 | if (m->count + len < m->size) { |
410 | m->count += len; | |
411 | return 0; | |
412 | } | |
413 | } | |
e075f591 | 414 | seq_set_overflow(m); |
1da177e4 LT |
415 | return -1; |
416 | } | |
a4808147 SW |
417 | EXPORT_SYMBOL(seq_vprintf); |
418 | ||
419 | int seq_printf(struct seq_file *m, const char *f, ...) | |
420 | { | |
421 | int ret; | |
422 | va_list args; | |
423 | ||
424 | va_start(args, f); | |
425 | ret = seq_vprintf(m, f, args); | |
426 | va_end(args); | |
427 | ||
428 | return ret; | |
429 | } | |
1da177e4 LT |
430 | EXPORT_SYMBOL(seq_printf); |
431 | ||
74e2f334 | 432 | /** |
958086d1 TE |
433 | * mangle_path - mangle and copy path to buffer beginning |
434 | * @s: buffer start | |
435 | * @p: beginning of path in above buffer | |
436 | * @esc: set of characters that need escaping | |
74e2f334 TE |
437 | * |
438 | * Copy the path from @p to @s, replacing each occurrence of character from | |
439 | * @esc with usual octal escape. | |
440 | * Returns pointer past last written character in @s, or NULL in case of | |
441 | * failure. | |
442 | */ | |
8c9379e9 | 443 | char *mangle_path(char *s, const char *p, const char *esc) |
6092d048 RP |
444 | { |
445 | while (s <= p) { | |
446 | char c = *p++; | |
447 | if (!c) { | |
448 | return s; | |
449 | } else if (!strchr(esc, c)) { | |
450 | *s++ = c; | |
451 | } else if (s + 4 > p) { | |
452 | break; | |
453 | } else { | |
454 | *s++ = '\\'; | |
455 | *s++ = '0' + ((c & 0300) >> 6); | |
456 | *s++ = '0' + ((c & 070) >> 3); | |
457 | *s++ = '0' + (c & 07); | |
458 | } | |
459 | } | |
460 | return NULL; | |
461 | } | |
604094f4 | 462 | EXPORT_SYMBOL(mangle_path); |
6092d048 | 463 | |
52afeefb AV |
464 | /** |
465 | * seq_path - seq_file interface to print a pathname | |
466 | * @m: the seq_file handle | |
467 | * @path: the struct path to print | |
468 | * @esc: set of characters to escape in the output | |
469 | * | |
470 | * return the absolute path of 'path', as represented by the | |
471 | * dentry / mnt pair in the path parameter. | |
6092d048 | 472 | */ |
8c9379e9 | 473 | int seq_path(struct seq_file *m, const struct path *path, const char *esc) |
1da177e4 | 474 | { |
f8439806 MS |
475 | char *buf; |
476 | size_t size = seq_get_buf(m, &buf); | |
477 | int res = -1; | |
478 | ||
479 | if (size) { | |
480 | char *p = d_path(path, buf, size); | |
1da177e4 | 481 | if (!IS_ERR(p)) { |
f8439806 MS |
482 | char *end = mangle_path(buf, p, esc); |
483 | if (end) | |
484 | res = end - buf; | |
1da177e4 LT |
485 | } |
486 | } | |
f8439806 MS |
487 | seq_commit(m, res); |
488 | ||
489 | return res; | |
1da177e4 LT |
490 | } |
491 | EXPORT_SYMBOL(seq_path); | |
492 | ||
9d1bc601 MS |
493 | /* |
494 | * Same as seq_path, but relative to supplied root. | |
9d1bc601 | 495 | */ |
8c9379e9 AV |
496 | int seq_path_root(struct seq_file *m, const struct path *path, |
497 | const struct path *root, const char *esc) | |
9d1bc601 | 498 | { |
f8439806 MS |
499 | char *buf; |
500 | size_t size = seq_get_buf(m, &buf); | |
501 | int res = -ENAMETOOLONG; | |
502 | ||
503 | if (size) { | |
9d1bc601 MS |
504 | char *p; |
505 | ||
f8439806 | 506 | p = __d_path(path, root, buf, size); |
02125a82 AV |
507 | if (!p) |
508 | return SEQ_SKIP; | |
f8439806 | 509 | res = PTR_ERR(p); |
9d1bc601 | 510 | if (!IS_ERR(p)) { |
f8439806 MS |
511 | char *end = mangle_path(buf, p, esc); |
512 | if (end) | |
513 | res = end - buf; | |
514 | else | |
515 | res = -ENAMETOOLONG; | |
9d1bc601 MS |
516 | } |
517 | } | |
f8439806 MS |
518 | seq_commit(m, res); |
519 | ||
02125a82 | 520 | return res < 0 && res != -ENAMETOOLONG ? res : 0; |
9d1bc601 MS |
521 | } |
522 | ||
6092d048 RP |
523 | /* |
524 | * returns the path of the 'dentry' from the root of its filesystem. | |
525 | */ | |
8c9379e9 | 526 | int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc) |
6092d048 | 527 | { |
f8439806 MS |
528 | char *buf; |
529 | size_t size = seq_get_buf(m, &buf); | |
530 | int res = -1; | |
531 | ||
532 | if (size) { | |
533 | char *p = dentry_path(dentry, buf, size); | |
6092d048 | 534 | if (!IS_ERR(p)) { |
f8439806 MS |
535 | char *end = mangle_path(buf, p, esc); |
536 | if (end) | |
537 | res = end - buf; | |
6092d048 RP |
538 | } |
539 | } | |
f8439806 MS |
540 | seq_commit(m, res); |
541 | ||
542 | return res; | |
6092d048 | 543 | } |
c8d3fe02 | 544 | EXPORT_SYMBOL(seq_dentry); |
6092d048 | 545 | |
1da177e4 LT |
546 | static void *single_start(struct seq_file *p, loff_t *pos) |
547 | { | |
548 | return NULL + (*pos == 0); | |
549 | } | |
550 | ||
551 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) | |
552 | { | |
553 | ++*pos; | |
554 | return NULL; | |
555 | } | |
556 | ||
557 | static void single_stop(struct seq_file *p, void *v) | |
558 | { | |
559 | } | |
560 | ||
561 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), | |
562 | void *data) | |
563 | { | |
564 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); | |
565 | int res = -ENOMEM; | |
566 | ||
567 | if (op) { | |
568 | op->start = single_start; | |
569 | op->next = single_next; | |
570 | op->stop = single_stop; | |
571 | op->show = show; | |
572 | res = seq_open(file, op); | |
573 | if (!res) | |
574 | ((struct seq_file *)file->private_data)->private = data; | |
575 | else | |
576 | kfree(op); | |
577 | } | |
578 | return res; | |
579 | } | |
580 | EXPORT_SYMBOL(single_open); | |
581 | ||
2043f495 AV |
582 | int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), |
583 | void *data, size_t size) | |
584 | { | |
058504ed | 585 | char *buf = seq_buf_alloc(size); |
2043f495 AV |
586 | int ret; |
587 | if (!buf) | |
588 | return -ENOMEM; | |
589 | ret = single_open(file, show, data); | |
590 | if (ret) { | |
058504ed | 591 | kvfree(buf); |
2043f495 AV |
592 | return ret; |
593 | } | |
594 | ((struct seq_file *)file->private_data)->buf = buf; | |
595 | ((struct seq_file *)file->private_data)->size = size; | |
596 | return 0; | |
597 | } | |
598 | EXPORT_SYMBOL(single_open_size); | |
599 | ||
1da177e4 LT |
600 | int single_release(struct inode *inode, struct file *file) |
601 | { | |
15ad7cdc | 602 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; |
1da177e4 LT |
603 | int res = seq_release(inode, file); |
604 | kfree(op); | |
605 | return res; | |
606 | } | |
607 | EXPORT_SYMBOL(single_release); | |
608 | ||
609 | int seq_release_private(struct inode *inode, struct file *file) | |
610 | { | |
611 | struct seq_file *seq = file->private_data; | |
612 | ||
613 | kfree(seq->private); | |
614 | seq->private = NULL; | |
615 | return seq_release(inode, file); | |
616 | } | |
617 | EXPORT_SYMBOL(seq_release_private); | |
618 | ||
39699037 PE |
619 | void *__seq_open_private(struct file *f, const struct seq_operations *ops, |
620 | int psize) | |
621 | { | |
622 | int rc; | |
623 | void *private; | |
624 | struct seq_file *seq; | |
625 | ||
626 | private = kzalloc(psize, GFP_KERNEL); | |
627 | if (private == NULL) | |
628 | goto out; | |
629 | ||
630 | rc = seq_open(f, ops); | |
631 | if (rc < 0) | |
632 | goto out_free; | |
633 | ||
634 | seq = f->private_data; | |
635 | seq->private = private; | |
636 | return private; | |
637 | ||
638 | out_free: | |
639 | kfree(private); | |
640 | out: | |
641 | return NULL; | |
642 | } | |
643 | EXPORT_SYMBOL(__seq_open_private); | |
644 | ||
645 | int seq_open_private(struct file *filp, const struct seq_operations *ops, | |
646 | int psize) | |
647 | { | |
648 | return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; | |
649 | } | |
650 | EXPORT_SYMBOL(seq_open_private); | |
651 | ||
1da177e4 LT |
652 | int seq_putc(struct seq_file *m, char c) |
653 | { | |
654 | if (m->count < m->size) { | |
655 | m->buf[m->count++] = c; | |
656 | return 0; | |
657 | } | |
658 | return -1; | |
659 | } | |
660 | EXPORT_SYMBOL(seq_putc); | |
661 | ||
662 | int seq_puts(struct seq_file *m, const char *s) | |
663 | { | |
664 | int len = strlen(s); | |
665 | if (m->count + len < m->size) { | |
666 | memcpy(m->buf + m->count, s, len); | |
667 | m->count += len; | |
668 | return 0; | |
669 | } | |
e075f591 | 670 | seq_set_overflow(m); |
1da177e4 LT |
671 | return -1; |
672 | } | |
673 | EXPORT_SYMBOL(seq_puts); | |
bcf67e16 | 674 | |
1ac101a5 KH |
675 | /* |
676 | * A helper routine for putting decimal numbers without rich format of printf(). | |
677 | * only 'unsigned long long' is supported. | |
678 | * This routine will put one byte delimiter + number into seq_file. | |
679 | * This routine is very quick when you show lots of numbers. | |
680 | * In usual cases, it will be better to use seq_printf(). It's easier to read. | |
681 | */ | |
682 | int seq_put_decimal_ull(struct seq_file *m, char delimiter, | |
683 | unsigned long long num) | |
684 | { | |
685 | int len; | |
686 | ||
687 | if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */ | |
688 | goto overflow; | |
689 | ||
bda7bad6 KH |
690 | if (delimiter) |
691 | m->buf[m->count++] = delimiter; | |
1ac101a5 KH |
692 | |
693 | if (num < 10) { | |
694 | m->buf[m->count++] = num + '0'; | |
695 | return 0; | |
696 | } | |
697 | ||
698 | len = num_to_str(m->buf + m->count, m->size - m->count, num); | |
699 | if (!len) | |
700 | goto overflow; | |
701 | m->count += len; | |
702 | return 0; | |
703 | overflow: | |
e075f591 | 704 | seq_set_overflow(m); |
1ac101a5 KH |
705 | return -1; |
706 | } | |
707 | EXPORT_SYMBOL(seq_put_decimal_ull); | |
708 | ||
bda7bad6 KH |
709 | int seq_put_decimal_ll(struct seq_file *m, char delimiter, |
710 | long long num) | |
711 | { | |
712 | if (num < 0) { | |
713 | if (m->count + 3 >= m->size) { | |
e075f591 | 714 | seq_set_overflow(m); |
bda7bad6 KH |
715 | return -1; |
716 | } | |
717 | if (delimiter) | |
718 | m->buf[m->count++] = delimiter; | |
719 | num = -num; | |
720 | delimiter = '-'; | |
721 | } | |
722 | return seq_put_decimal_ull(m, delimiter, num); | |
723 | ||
724 | } | |
725 | EXPORT_SYMBOL(seq_put_decimal_ll); | |
726 | ||
0b923606 PO |
727 | /** |
728 | * seq_write - write arbitrary data to buffer | |
729 | * @seq: seq_file identifying the buffer to which data should be written | |
730 | * @data: data address | |
731 | * @len: number of bytes | |
732 | * | |
733 | * Return 0 on success, non-zero otherwise. | |
734 | */ | |
735 | int seq_write(struct seq_file *seq, const void *data, size_t len) | |
736 | { | |
737 | if (seq->count + len < seq->size) { | |
738 | memcpy(seq->buf + seq->count, data, len); | |
739 | seq->count += len; | |
740 | return 0; | |
741 | } | |
e075f591 | 742 | seq_set_overflow(seq); |
0b923606 PO |
743 | return -1; |
744 | } | |
745 | EXPORT_SYMBOL(seq_write); | |
746 | ||
839cc2a9 TH |
747 | /** |
748 | * seq_pad - write padding spaces to buffer | |
749 | * @m: seq_file identifying the buffer to which data should be written | |
750 | * @c: the byte to append after padding if non-zero | |
751 | */ | |
752 | void seq_pad(struct seq_file *m, char c) | |
753 | { | |
754 | int size = m->pad_until - m->count; | |
755 | if (size > 0) | |
756 | seq_printf(m, "%*s", size, ""); | |
757 | if (c) | |
758 | seq_putc(m, c); | |
759 | } | |
760 | EXPORT_SYMBOL(seq_pad); | |
761 | ||
bcf67e16 PE |
762 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) |
763 | { | |
764 | struct list_head *lh; | |
765 | ||
766 | list_for_each(lh, head) | |
767 | if (pos-- == 0) | |
768 | return lh; | |
769 | ||
770 | return NULL; | |
771 | } | |
bcf67e16 PE |
772 | EXPORT_SYMBOL(seq_list_start); |
773 | ||
774 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) | |
775 | { | |
776 | if (!pos) | |
777 | return head; | |
778 | ||
779 | return seq_list_start(head, pos - 1); | |
780 | } | |
bcf67e16 PE |
781 | EXPORT_SYMBOL(seq_list_start_head); |
782 | ||
783 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) | |
784 | { | |
785 | struct list_head *lh; | |
786 | ||
787 | lh = ((struct list_head *)v)->next; | |
788 | ++*ppos; | |
789 | return lh == head ? NULL : lh; | |
790 | } | |
bcf67e16 | 791 | EXPORT_SYMBOL(seq_list_next); |
66655de6 LZ |
792 | |
793 | /** | |
794 | * seq_hlist_start - start an iteration of a hlist | |
795 | * @head: the head of the hlist | |
796 | * @pos: the start position of the sequence | |
797 | * | |
798 | * Called at seq_file->op->start(). | |
799 | */ | |
800 | struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos) | |
801 | { | |
802 | struct hlist_node *node; | |
803 | ||
804 | hlist_for_each(node, head) | |
805 | if (pos-- == 0) | |
806 | return node; | |
807 | return NULL; | |
808 | } | |
809 | EXPORT_SYMBOL(seq_hlist_start); | |
810 | ||
811 | /** | |
812 | * seq_hlist_start_head - start an iteration of a hlist | |
813 | * @head: the head of the hlist | |
814 | * @pos: the start position of the sequence | |
815 | * | |
816 | * Called at seq_file->op->start(). Call this function if you want to | |
817 | * print a header at the top of the output. | |
818 | */ | |
819 | struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos) | |
820 | { | |
821 | if (!pos) | |
822 | return SEQ_START_TOKEN; | |
823 | ||
824 | return seq_hlist_start(head, pos - 1); | |
825 | } | |
826 | EXPORT_SYMBOL(seq_hlist_start_head); | |
827 | ||
828 | /** | |
829 | * seq_hlist_next - move to the next position of the hlist | |
830 | * @v: the current iterator | |
831 | * @head: the head of the hlist | |
138860b9 | 832 | * @ppos: the current position |
66655de6 LZ |
833 | * |
834 | * Called at seq_file->op->next(). | |
835 | */ | |
836 | struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head, | |
837 | loff_t *ppos) | |
838 | { | |
839 | struct hlist_node *node = v; | |
840 | ||
841 | ++*ppos; | |
842 | if (v == SEQ_START_TOKEN) | |
843 | return head->first; | |
844 | else | |
845 | return node->next; | |
846 | } | |
847 | EXPORT_SYMBOL(seq_hlist_next); | |
1cc52327 | 848 | |
849 | /** | |
850 | * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU | |
851 | * @head: the head of the hlist | |
852 | * @pos: the start position of the sequence | |
853 | * | |
854 | * Called at seq_file->op->start(). | |
855 | * | |
856 | * This list-traversal primitive may safely run concurrently with | |
857 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() | |
858 | * as long as the traversal is guarded by rcu_read_lock(). | |
859 | */ | |
860 | struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head, | |
861 | loff_t pos) | |
862 | { | |
863 | struct hlist_node *node; | |
864 | ||
865 | __hlist_for_each_rcu(node, head) | |
866 | if (pos-- == 0) | |
867 | return node; | |
868 | return NULL; | |
869 | } | |
870 | EXPORT_SYMBOL(seq_hlist_start_rcu); | |
871 | ||
872 | /** | |
873 | * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU | |
874 | * @head: the head of the hlist | |
875 | * @pos: the start position of the sequence | |
876 | * | |
877 | * Called at seq_file->op->start(). Call this function if you want to | |
878 | * print a header at the top of the output. | |
879 | * | |
880 | * This list-traversal primitive may safely run concurrently with | |
881 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() | |
882 | * as long as the traversal is guarded by rcu_read_lock(). | |
883 | */ | |
884 | struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head, | |
885 | loff_t pos) | |
886 | { | |
887 | if (!pos) | |
888 | return SEQ_START_TOKEN; | |
889 | ||
890 | return seq_hlist_start_rcu(head, pos - 1); | |
891 | } | |
892 | EXPORT_SYMBOL(seq_hlist_start_head_rcu); | |
893 | ||
894 | /** | |
895 | * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU | |
896 | * @v: the current iterator | |
897 | * @head: the head of the hlist | |
138860b9 | 898 | * @ppos: the current position |
1cc52327 | 899 | * |
900 | * Called at seq_file->op->next(). | |
901 | * | |
902 | * This list-traversal primitive may safely run concurrently with | |
903 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() | |
904 | * as long as the traversal is guarded by rcu_read_lock(). | |
905 | */ | |
906 | struct hlist_node *seq_hlist_next_rcu(void *v, | |
907 | struct hlist_head *head, | |
908 | loff_t *ppos) | |
909 | { | |
910 | struct hlist_node *node = v; | |
911 | ||
912 | ++*ppos; | |
913 | if (v == SEQ_START_TOKEN) | |
914 | return rcu_dereference(head->first); | |
915 | else | |
916 | return rcu_dereference(node->next); | |
917 | } | |
918 | EXPORT_SYMBOL(seq_hlist_next_rcu); | |
0bc77381 JL |
919 | |
920 | /** | |
921 | * seq_hlist_start_precpu - start an iteration of a percpu hlist array | |
922 | * @head: pointer to percpu array of struct hlist_heads | |
923 | * @cpu: pointer to cpu "cursor" | |
924 | * @pos: start position of sequence | |
925 | * | |
926 | * Called at seq_file->op->start(). | |
927 | */ | |
928 | struct hlist_node * | |
929 | seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos) | |
930 | { | |
931 | struct hlist_node *node; | |
932 | ||
933 | for_each_possible_cpu(*cpu) { | |
934 | hlist_for_each(node, per_cpu_ptr(head, *cpu)) { | |
935 | if (pos-- == 0) | |
936 | return node; | |
937 | } | |
938 | } | |
939 | return NULL; | |
940 | } | |
941 | EXPORT_SYMBOL(seq_hlist_start_percpu); | |
942 | ||
943 | /** | |
944 | * seq_hlist_next_percpu - move to the next position of the percpu hlist array | |
945 | * @v: pointer to current hlist_node | |
946 | * @head: pointer to percpu array of struct hlist_heads | |
947 | * @cpu: pointer to cpu "cursor" | |
948 | * @pos: start position of sequence | |
949 | * | |
950 | * Called at seq_file->op->next(). | |
951 | */ | |
952 | struct hlist_node * | |
953 | seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, | |
954 | int *cpu, loff_t *pos) | |
955 | { | |
956 | struct hlist_node *node = v; | |
957 | ||
958 | ++*pos; | |
959 | ||
960 | if (node->next) | |
961 | return node->next; | |
962 | ||
963 | for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids; | |
964 | *cpu = cpumask_next(*cpu, cpu_possible_mask)) { | |
965 | struct hlist_head *bucket = per_cpu_ptr(head, *cpu); | |
966 | ||
967 | if (!hlist_empty(bucket)) | |
968 | return bucket->first; | |
969 | } | |
970 | return NULL; | |
971 | } | |
972 | EXPORT_SYMBOL(seq_hlist_next_percpu); |