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