blob: a290157265ef5cfb52dbdb6a9e950e9733462d16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/seq_file.h>
11#include <linux/slab.h>
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060012#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <asm/uaccess.h>
15#include <asm/page.h>
16
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070017
18/*
19 * seq_files have a buffer which can may overflow. When this happens a larger
20 * buffer is reallocated and all the data will be printed again.
21 * The overflow state is true when m->count == m->size.
22 */
23static bool seq_overflow(struct seq_file *m)
24{
25 return m->count == m->size;
26}
27
28static void seq_set_overflow(struct seq_file *m)
29{
30 m->count = m->size;
31}
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
34 * seq_open - initialize sequential file
35 * @file: file we initialize
36 * @op: method table describing the sequence
37 *
38 * seq_open() sets @file, associating it with a sequence described
39 * by @op. @op->start() sets the iterator up and returns the first
40 * element of sequence. @op->stop() shuts it down. @op->next()
41 * returns the next element of sequence. @op->show() prints element
42 * into the buffer. In case of error ->start() and ->next() return
43 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
44 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040045 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080047int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Al Viro1abe77b2005-11-07 17:15:34 -050049 struct seq_file *p = file->private_data;
50
51 if (!p) {
52 p = kmalloc(sizeof(*p), GFP_KERNEL);
53 if (!p)
54 return -ENOMEM;
55 file->private_data = p;
56 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080058 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 p->op = op;
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060060#ifdef CONFIG_USER_NS
61 p->user_ns = file->f_cred->user_ns;
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /*
65 * Wrappers around seq_open(e.g. swaps_open) need to be
66 * aware of this. If they set f_version themselves, they
67 * should call seq_open first and then set f_version.
68 */
69 file->f_version = 0;
70
Eric Biederman8f19d472009-02-18 14:48:16 -080071 /*
72 * seq_files support lseek() and pread(). They do not implement
73 * write() at all, but we clear FMODE_PWRITE here for historical
74 * reasons.
75 *
76 * If a client of seq_files a) implements file.write() and b) wishes to
77 * support pwrite() then that client will need to implement its own
78 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
79 */
80 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82}
83EXPORT_SYMBOL(seq_open);
84
Eric Biederman33da8892009-02-04 15:12:25 -080085static int traverse(struct seq_file *m, loff_t offset)
86{
87 loff_t pos = 0, index;
88 int error = 0;
89 void *p;
90
91 m->version = 0;
92 index = 0;
93 m->count = m->from = 0;
94 if (!offset) {
95 m->index = index;
96 return 0;
97 }
98 if (!m->buf) {
99 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
100 if (!m->buf)
101 return -ENOMEM;
102 }
103 p = m->op->start(m, &index);
104 while (p) {
105 error = PTR_ERR(p);
106 if (IS_ERR(p))
107 break;
108 error = m->op->show(m, p);
109 if (error < 0)
110 break;
111 if (unlikely(error)) {
112 error = 0;
113 m->count = 0;
114 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700115 if (seq_overflow(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800116 goto Eoverflow;
117 if (pos + m->count > offset) {
118 m->from = offset - pos;
119 m->count -= m->from;
120 m->index = index;
121 break;
122 }
123 pos += m->count;
124 m->count = 0;
125 if (pos == offset) {
126 index++;
127 m->index = index;
128 break;
129 }
130 p = m->op->next(m, p, &index);
131 }
132 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300133 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800134 return error;
135
136Eoverflow:
137 m->op->stop(m, p);
138 kfree(m->buf);
139 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
140 return !m->buf ? -ENOMEM : -EAGAIN;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/**
144 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700145 * @file: the file to read from
146 * @buf: the buffer to read to
147 * @size: the maximum number of bytes to read
148 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
150 * Ready-made ->f_op->read()
151 */
152ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
153{
Joe Perches8209e2f2010-09-04 18:52:49 -0700154 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 size_t copied = 0;
156 loff_t pos;
157 size_t n;
158 void *p;
159 int err = 0;
160
Ingo Molnar0ac17592006-03-23 03:00:37 -0800161 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /*
164 * seq_file->op->..m_start/m_stop/m_next may do special actions
165 * or optimisations based on the file->f_version, so we want to
166 * pass the file->f_version to those methods.
167 *
168 * seq_file->version is just copy of f_version, and seq_file
169 * methods can treat it simply as file version.
170 * It is copied in first and copied out after all operations.
171 * It is convenient to have it as part of structure to avoid the
172 * need of passing another argument to all the seq_file methods.
173 */
174 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700175
176 /* Don't assume *ppos is where we left it */
177 if (unlikely(*ppos != m->read_pos)) {
178 while ((err = traverse(m, *ppos)) == -EAGAIN)
179 ;
180 if (err) {
181 /* With prejudice... */
182 m->read_pos = 0;
183 m->version = 0;
184 m->index = 0;
185 m->count = 0;
186 goto Done;
187 } else {
188 m->read_pos = *ppos;
189 }
190 }
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* grab buffer if we didn't have one */
193 if (!m->buf) {
194 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
195 if (!m->buf)
196 goto Enomem;
197 }
198 /* if not empty - flush it first */
199 if (m->count) {
200 n = min(m->count, size);
201 err = copy_to_user(buf, m->buf + m->from, n);
202 if (err)
203 goto Efault;
204 m->count -= n;
205 m->from += n;
206 size -= n;
207 buf += n;
208 copied += n;
209 if (!m->count)
210 m->index++;
211 if (!size)
212 goto Done;
213 }
214 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400215 pos = m->index;
216 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 err = PTR_ERR(p);
219 if (!p || IS_ERR(p))
220 break;
221 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400222 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 break;
Al Viro521b5d02008-03-28 00:46:41 -0400224 if (unlikely(err))
225 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400226 if (unlikely(!m->count)) {
227 p = m->op->next(m, p, &pos);
228 m->index = pos;
229 continue;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (m->count < m->size)
232 goto Fill;
233 m->op->stop(m, p);
234 kfree(m->buf);
235 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
236 if (!m->buf)
237 goto Enomem;
238 m->count = 0;
239 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400240 pos = m->index;
241 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 m->op->stop(m, p);
244 m->count = 0;
245 goto Done;
246Fill:
247 /* they want more? let's try to get some more */
248 while (m->count < size) {
249 size_t offs = m->count;
250 loff_t next = pos;
251 p = m->op->next(m, p, &next);
252 if (!p || IS_ERR(p)) {
253 err = PTR_ERR(p);
254 break;
255 }
256 err = m->op->show(m, p);
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700257 if (seq_overflow(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400259 if (likely(err <= 0))
260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 pos = next;
263 }
264 m->op->stop(m, p);
265 n = min(m->count, size);
266 err = copy_to_user(buf, m->buf, n);
267 if (err)
268 goto Efault;
269 copied += n;
270 m->count -= n;
271 if (m->count)
272 m->from = n;
273 else
274 pos++;
275 m->index = pos;
276Done:
277 if (!copied)
278 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800279 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800281 m->read_pos += copied;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800284 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return copied;
286Enomem:
287 err = -ENOMEM;
288 goto Done;
289Efault:
290 err = -EFAULT;
291 goto Done;
292}
293EXPORT_SYMBOL(seq_read);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/**
296 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700297 * @file: the file in question
298 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800299 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *
301 * Ready-made ->f_op->llseek()
302 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800303loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Joe Perches8209e2f2010-09-04 18:52:49 -0700305 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200306 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Ingo Molnar0ac17592006-03-23 03:00:37 -0800308 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 m->version = file->f_version;
Andrew Morton965c8e52012-12-17 15:59:39 -0800310 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800311 case SEEK_CUR:
312 offset += file->f_pos;
313 case SEEK_SET:
314 if (offset < 0)
315 break;
316 retval = offset;
317 if (offset != m->read_pos) {
318 while ((retval = traverse(m, offset)) == -EAGAIN)
319 ;
320 if (retval) {
321 /* with extreme prejudice... */
322 file->f_pos = 0;
323 m->read_pos = 0;
324 m->version = 0;
325 m->index = 0;
326 m->count = 0;
327 } else {
328 m->read_pos = offset;
329 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Gu Zheng05e16742013-10-25 18:15:06 +0800331 } else {
332 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700336 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return retval;
338}
339EXPORT_SYMBOL(seq_lseek);
340
341/**
342 * seq_release - free the structures associated with sequential file.
343 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500344 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *
346 * Frees the structures associated with sequential file; can be used
347 * as ->f_op->release() if you don't have private data to destroy.
348 */
349int seq_release(struct inode *inode, struct file *file)
350{
Joe Perches8209e2f2010-09-04 18:52:49 -0700351 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 kfree(m->buf);
353 kfree(m);
354 return 0;
355}
356EXPORT_SYMBOL(seq_release);
357
358/**
359 * seq_escape - print string into buffer, escaping some characters
360 * @m: target buffer
361 * @s: string
362 * @esc: set of characters that need escaping
363 *
364 * Puts string into buffer, replacing each occurrence of character from
365 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
366 * case of overflow.
367 */
368int seq_escape(struct seq_file *m, const char *s, const char *esc)
369{
370 char *end = m->buf + m->size;
371 char *p;
372 char c;
373
374 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
375 if (!strchr(esc, c)) {
376 *p++ = c;
377 continue;
378 }
379 if (p + 3 < end) {
380 *p++ = '\\';
381 *p++ = '0' + ((c & 0300) >> 6);
382 *p++ = '0' + ((c & 070) >> 3);
383 *p++ = '0' + (c & 07);
384 continue;
385 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700386 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return -1;
388 }
389 m->count = p - m->buf;
390 return 0;
391}
392EXPORT_SYMBOL(seq_escape);
393
Steven Whitehousea4808142012-06-11 13:16:35 +0100394int seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int len;
397
398 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (m->count + len < m->size) {
401 m->count += len;
402 return 0;
403 }
404 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700405 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -1;
407}
Steven Whitehousea4808142012-06-11 13:16:35 +0100408EXPORT_SYMBOL(seq_vprintf);
409
410int seq_printf(struct seq_file *m, const char *f, ...)
411{
412 int ret;
413 va_list args;
414
415 va_start(args, f);
416 ret = seq_vprintf(m, f, args);
417 va_end(args);
418
419 return ret;
420}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421EXPORT_SYMBOL(seq_printf);
422
Török Edwin74e2f332008-11-22 13:28:48 +0200423/**
Török Edwin958086d2008-11-23 23:24:53 +0200424 * mangle_path - mangle and copy path to buffer beginning
425 * @s: buffer start
426 * @p: beginning of path in above buffer
427 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200428 *
429 * Copy the path from @p to @s, replacing each occurrence of character from
430 * @esc with usual octal escape.
431 * Returns pointer past last written character in @s, or NULL in case of
432 * failure.
433 */
Al Viro8c9379e2011-12-08 20:18:57 -0500434char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100435{
436 while (s <= p) {
437 char c = *p++;
438 if (!c) {
439 return s;
440 } else if (!strchr(esc, c)) {
441 *s++ = c;
442 } else if (s + 4 > p) {
443 break;
444 } else {
445 *s++ = '\\';
446 *s++ = '0' + ((c & 0300) >> 6);
447 *s++ = '0' + ((c & 070) >> 3);
448 *s++ = '0' + (c & 07);
449 }
450 }
451 return NULL;
452}
Ingo Molnar604094f2008-11-28 18:03:22 +0100453EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100454
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800455/**
456 * seq_path - seq_file interface to print a pathname
457 * @m: the seq_file handle
458 * @path: the struct path to print
459 * @esc: set of characters to escape in the output
460 *
461 * return the absolute path of 'path', as represented by the
462 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100463 */
Al Viro8c9379e2011-12-08 20:18:57 -0500464int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Miklos Szeredif8439802009-09-21 14:48:36 +0200466 char *buf;
467 size_t size = seq_get_buf(m, &buf);
468 int res = -1;
469
470 if (size) {
471 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200473 char *end = mangle_path(buf, p, esc);
474 if (end)
475 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200478 seq_commit(m, res);
479
480 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482EXPORT_SYMBOL(seq_path);
483
Ram Pai6092d042008-03-27 13:06:20 +0100484/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100485 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100486 */
Al Viro8c9379e2011-12-08 20:18:57 -0500487int seq_path_root(struct seq_file *m, const struct path *path,
488 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100489{
Miklos Szeredif8439802009-09-21 14:48:36 +0200490 char *buf;
491 size_t size = seq_get_buf(m, &buf);
492 int res = -ENAMETOOLONG;
493
494 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100495 char *p;
496
Miklos Szeredif8439802009-09-21 14:48:36 +0200497 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500498 if (!p)
499 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200500 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100501 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200502 char *end = mangle_path(buf, p, esc);
503 if (end)
504 res = end - buf;
505 else
506 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100507 }
508 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200509 seq_commit(m, res);
510
Al Viro02125a82011-12-05 08:43:34 -0500511 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100512}
513
514/*
Ram Pai6092d042008-03-27 13:06:20 +0100515 * returns the path of the 'dentry' from the root of its filesystem.
516 */
Al Viro8c9379e2011-12-08 20:18:57 -0500517int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100518{
Miklos Szeredif8439802009-09-21 14:48:36 +0200519 char *buf;
520 size_t size = seq_get_buf(m, &buf);
521 int res = -1;
522
523 if (size) {
524 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100525 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200526 char *end = mangle_path(buf, p, esc);
527 if (end)
528 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100529 }
530 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200531 seq_commit(m, res);
532
533 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100534}
535
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030536int seq_bitmap(struct seq_file *m, const unsigned long *bits,
537 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700538{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700539 if (m->count < m->size) {
540 int len = bitmap_scnprintf(m->buf + m->count,
541 m->size - m->count, bits, nr_bits);
542 if (m->count + len < m->size) {
543 m->count += len;
544 return 0;
545 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700546 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700547 seq_set_overflow(m);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700548 return -1;
549}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700550EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700551
Rusty Russellaf76aba2009-03-30 22:05:11 -0600552int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700553 unsigned int nr_bits)
554{
555 if (m->count < m->size) {
556 int len = bitmap_scnlistprintf(m->buf + m->count,
557 m->size - m->count, bits, nr_bits);
558 if (m->count + len < m->size) {
559 m->count += len;
560 return 0;
561 }
562 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700563 seq_set_overflow(m);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700564 return -1;
565}
566EXPORT_SYMBOL(seq_bitmap_list);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static void *single_start(struct seq_file *p, loff_t *pos)
569{
570 return NULL + (*pos == 0);
571}
572
573static void *single_next(struct seq_file *p, void *v, loff_t *pos)
574{
575 ++*pos;
576 return NULL;
577}
578
579static void single_stop(struct seq_file *p, void *v)
580{
581}
582
583int single_open(struct file *file, int (*show)(struct seq_file *, void *),
584 void *data)
585{
586 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
587 int res = -ENOMEM;
588
589 if (op) {
590 op->start = single_start;
591 op->next = single_next;
592 op->stop = single_stop;
593 op->show = show;
594 res = seq_open(file, op);
595 if (!res)
596 ((struct seq_file *)file->private_data)->private = data;
597 else
598 kfree(op);
599 }
600 return res;
601}
602EXPORT_SYMBOL(single_open);
603
Al Viro2043f492013-03-31 13:43:23 -0400604int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
605 void *data, size_t size)
606{
607 char *buf = kmalloc(size, GFP_KERNEL);
608 int ret;
609 if (!buf)
610 return -ENOMEM;
611 ret = single_open(file, show, data);
612 if (ret) {
613 kfree(buf);
614 return ret;
615 }
616 ((struct seq_file *)file->private_data)->buf = buf;
617 ((struct seq_file *)file->private_data)->size = size;
618 return 0;
619}
620EXPORT_SYMBOL(single_open_size);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622int single_release(struct inode *inode, struct file *file)
623{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800624 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int res = seq_release(inode, file);
626 kfree(op);
627 return res;
628}
629EXPORT_SYMBOL(single_release);
630
631int seq_release_private(struct inode *inode, struct file *file)
632{
633 struct seq_file *seq = file->private_data;
634
635 kfree(seq->private);
636 seq->private = NULL;
637 return seq_release(inode, file);
638}
639EXPORT_SYMBOL(seq_release_private);
640
Pavel Emelyanov39699032007-10-10 02:28:42 -0700641void *__seq_open_private(struct file *f, const struct seq_operations *ops,
642 int psize)
643{
644 int rc;
645 void *private;
646 struct seq_file *seq;
647
648 private = kzalloc(psize, GFP_KERNEL);
649 if (private == NULL)
650 goto out;
651
652 rc = seq_open(f, ops);
653 if (rc < 0)
654 goto out_free;
655
656 seq = f->private_data;
657 seq->private = private;
658 return private;
659
660out_free:
661 kfree(private);
662out:
663 return NULL;
664}
665EXPORT_SYMBOL(__seq_open_private);
666
667int seq_open_private(struct file *filp, const struct seq_operations *ops,
668 int psize)
669{
670 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
671}
672EXPORT_SYMBOL(seq_open_private);
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674int seq_putc(struct seq_file *m, char c)
675{
676 if (m->count < m->size) {
677 m->buf[m->count++] = c;
678 return 0;
679 }
680 return -1;
681}
682EXPORT_SYMBOL(seq_putc);
683
684int seq_puts(struct seq_file *m, const char *s)
685{
686 int len = strlen(s);
687 if (m->count + len < m->size) {
688 memcpy(m->buf + m->count, s, len);
689 m->count += len;
690 return 0;
691 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700692 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -1;
694}
695EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700696
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700697/*
698 * A helper routine for putting decimal numbers without rich format of printf().
699 * only 'unsigned long long' is supported.
700 * This routine will put one byte delimiter + number into seq_file.
701 * This routine is very quick when you show lots of numbers.
702 * In usual cases, it will be better to use seq_printf(). It's easier to read.
703 */
704int seq_put_decimal_ull(struct seq_file *m, char delimiter,
705 unsigned long long num)
706{
707 int len;
708
709 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
710 goto overflow;
711
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700712 if (delimiter)
713 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700714
715 if (num < 10) {
716 m->buf[m->count++] = num + '0';
717 return 0;
718 }
719
720 len = num_to_str(m->buf + m->count, m->size - m->count, num);
721 if (!len)
722 goto overflow;
723 m->count += len;
724 return 0;
725overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700726 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700727 return -1;
728}
729EXPORT_SYMBOL(seq_put_decimal_ull);
730
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700731int seq_put_decimal_ll(struct seq_file *m, char delimiter,
732 long long num)
733{
734 if (num < 0) {
735 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700736 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700737 return -1;
738 }
739 if (delimiter)
740 m->buf[m->count++] = delimiter;
741 num = -num;
742 delimiter = '-';
743 }
744 return seq_put_decimal_ull(m, delimiter, num);
745
746}
747EXPORT_SYMBOL(seq_put_decimal_ll);
748
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700749/**
750 * seq_write - write arbitrary data to buffer
751 * @seq: seq_file identifying the buffer to which data should be written
752 * @data: data address
753 * @len: number of bytes
754 *
755 * Return 0 on success, non-zero otherwise.
756 */
757int seq_write(struct seq_file *seq, const void *data, size_t len)
758{
759 if (seq->count + len < seq->size) {
760 memcpy(seq->buf + seq->count, data, len);
761 seq->count += len;
762 return 0;
763 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700764 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700765 return -1;
766}
767EXPORT_SYMBOL(seq_write);
768
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700769struct list_head *seq_list_start(struct list_head *head, loff_t pos)
770{
771 struct list_head *lh;
772
773 list_for_each(lh, head)
774 if (pos-- == 0)
775 return lh;
776
777 return NULL;
778}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700779EXPORT_SYMBOL(seq_list_start);
780
781struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
782{
783 if (!pos)
784 return head;
785
786 return seq_list_start(head, pos - 1);
787}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700788EXPORT_SYMBOL(seq_list_start_head);
789
790struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
791{
792 struct list_head *lh;
793
794 lh = ((struct list_head *)v)->next;
795 ++*ppos;
796 return lh == head ? NULL : lh;
797}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700798EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000799
800/**
801 * seq_hlist_start - start an iteration of a hlist
802 * @head: the head of the hlist
803 * @pos: the start position of the sequence
804 *
805 * Called at seq_file->op->start().
806 */
807struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
808{
809 struct hlist_node *node;
810
811 hlist_for_each(node, head)
812 if (pos-- == 0)
813 return node;
814 return NULL;
815}
816EXPORT_SYMBOL(seq_hlist_start);
817
818/**
819 * seq_hlist_start_head - start an iteration of a hlist
820 * @head: the head of the hlist
821 * @pos: the start position of the sequence
822 *
823 * Called at seq_file->op->start(). Call this function if you want to
824 * print a header at the top of the output.
825 */
826struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
827{
828 if (!pos)
829 return SEQ_START_TOKEN;
830
831 return seq_hlist_start(head, pos - 1);
832}
833EXPORT_SYMBOL(seq_hlist_start_head);
834
835/**
836 * seq_hlist_next - move to the next position of the hlist
837 * @v: the current iterator
838 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800839 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000840 *
841 * Called at seq_file->op->next().
842 */
843struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
844 loff_t *ppos)
845{
846 struct hlist_node *node = v;
847
848 ++*ppos;
849 if (v == SEQ_START_TOKEN)
850 return head->first;
851 else
852 return node->next;
853}
854EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000855
856/**
857 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
858 * @head: the head of the hlist
859 * @pos: the start position of the sequence
860 *
861 * Called at seq_file->op->start().
862 *
863 * This list-traversal primitive may safely run concurrently with
864 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
865 * as long as the traversal is guarded by rcu_read_lock().
866 */
867struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
868 loff_t pos)
869{
870 struct hlist_node *node;
871
872 __hlist_for_each_rcu(node, head)
873 if (pos-- == 0)
874 return node;
875 return NULL;
876}
877EXPORT_SYMBOL(seq_hlist_start_rcu);
878
879/**
880 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
881 * @head: the head of the hlist
882 * @pos: the start position of the sequence
883 *
884 * Called at seq_file->op->start(). Call this function if you want to
885 * print a header at the top of the output.
886 *
887 * This list-traversal primitive may safely run concurrently with
888 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
889 * as long as the traversal is guarded by rcu_read_lock().
890 */
891struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
892 loff_t pos)
893{
894 if (!pos)
895 return SEQ_START_TOKEN;
896
897 return seq_hlist_start_rcu(head, pos - 1);
898}
899EXPORT_SYMBOL(seq_hlist_start_head_rcu);
900
901/**
902 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
903 * @v: the current iterator
904 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800905 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000906 *
907 * Called at seq_file->op->next().
908 *
909 * This list-traversal primitive may safely run concurrently with
910 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
911 * as long as the traversal is guarded by rcu_read_lock().
912 */
913struct hlist_node *seq_hlist_next_rcu(void *v,
914 struct hlist_head *head,
915 loff_t *ppos)
916{
917 struct hlist_node *node = v;
918
919 ++*ppos;
920 if (v == SEQ_START_TOKEN)
921 return rcu_dereference(head->first);
922 else
923 return rcu_dereference(node->next);
924}
925EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400926
927/**
928 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
929 * @head: pointer to percpu array of struct hlist_heads
930 * @cpu: pointer to cpu "cursor"
931 * @pos: start position of sequence
932 *
933 * Called at seq_file->op->start().
934 */
935struct hlist_node *
936seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
937{
938 struct hlist_node *node;
939
940 for_each_possible_cpu(*cpu) {
941 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
942 if (pos-- == 0)
943 return node;
944 }
945 }
946 return NULL;
947}
948EXPORT_SYMBOL(seq_hlist_start_percpu);
949
950/**
951 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
952 * @v: pointer to current hlist_node
953 * @head: pointer to percpu array of struct hlist_heads
954 * @cpu: pointer to cpu "cursor"
955 * @pos: start position of sequence
956 *
957 * Called at seq_file->op->next().
958 */
959struct hlist_node *
960seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
961 int *cpu, loff_t *pos)
962{
963 struct hlist_node *node = v;
964
965 ++*pos;
966
967 if (node->next)
968 return node->next;
969
970 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
971 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
972 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
973
974 if (!hlist_empty(bucket))
975 return bucket->first;
976 }
977 return NULL;
978}
979EXPORT_SYMBOL(seq_hlist_next_percpu);