blob: 06019ca0ca712b93bb62a8043f92918b01e1f1d8 [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>
xiaobing tu24a8d102013-01-31 14:11:32 +080010#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/seq_file.h>
12#include <linux/slab.h>
xiaobing tu24a8d102013-01-31 14:11:32 +080013#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/uaccess.h>
16#include <asm/page.h>
17
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070018
19/*
20 * seq_files have a buffer which can may overflow. When this happens a larger
21 * buffer is reallocated and all the data will be printed again.
22 * The overflow state is true when m->count == m->size.
23 */
24static bool seq_overflow(struct seq_file *m)
25{
26 return m->count == m->size;
27}
28
29static void seq_set_overflow(struct seq_file *m)
30{
31 m->count = m->size;
32}
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/**
35 * seq_open - initialize sequential file
36 * @file: file we initialize
37 * @op: method table describing the sequence
38 *
39 * seq_open() sets @file, associating it with a sequence described
40 * by @op. @op->start() sets the iterator up and returns the first
41 * element of sequence. @op->stop() shuts it down. @op->next()
42 * returns the next element of sequence. @op->show() prints element
43 * into the buffer. In case of error ->start() and ->next() return
44 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
45 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040046 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080048int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Al Viro1abe77b2005-11-07 17:15:34 -050050 struct seq_file *p = file->private_data;
51
52 if (!p) {
53 p = kmalloc(sizeof(*p), GFP_KERNEL);
54 if (!p)
55 return -ENOMEM;
56 file->private_data = p;
57 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080059 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 p->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /*
63 * Wrappers around seq_open(e.g. swaps_open) need to be
64 * aware of this. If they set f_version themselves, they
65 * should call seq_open first and then set f_version.
66 */
67 file->f_version = 0;
68
Eric Biederman8f19d472009-02-18 14:48:16 -080069 /*
70 * seq_files support lseek() and pread(). They do not implement
71 * write() at all, but we clear FMODE_PWRITE here for historical
72 * reasons.
73 *
74 * If a client of seq_files a) implements file.write() and b) wishes to
75 * support pwrite() then that client will need to implement its own
76 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
77 */
78 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 0;
80}
81EXPORT_SYMBOL(seq_open);
82
Eric Biederman33da8892009-02-04 15:12:25 -080083static int traverse(struct seq_file *m, loff_t offset)
84{
85 loff_t pos = 0, index;
86 int error = 0;
87 void *p;
88
89 m->version = 0;
90 index = 0;
91 m->count = m->from = 0;
92 if (!offset) {
93 m->index = index;
94 return 0;
95 }
96 if (!m->buf) {
97 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
98 if (!m->buf)
99 return -ENOMEM;
100 }
101 p = m->op->start(m, &index);
102 while (p) {
103 error = PTR_ERR(p);
104 if (IS_ERR(p))
105 break;
106 error = m->op->show(m, p);
107 if (error < 0)
108 break;
109 if (unlikely(error)) {
110 error = 0;
111 m->count = 0;
112 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700113 if (seq_overflow(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800114 goto Eoverflow;
115 if (pos + m->count > offset) {
116 m->from = offset - pos;
117 m->count -= m->from;
118 m->index = index;
119 break;
120 }
121 pos += m->count;
122 m->count = 0;
123 if (pos == offset) {
124 index++;
125 m->index = index;
126 break;
127 }
128 p = m->op->next(m, p, &index);
129 }
130 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300131 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800132 return error;
133
134Eoverflow:
135 m->op->stop(m, p);
xiaobing tu24a8d102013-01-31 14:11:32 +0800136 is_vmalloc_addr(m->buf) ? vfree(m->buf) : kfree(m->buf);
137 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL | __GFP_NOWARN);
138 if (!m->buf)
139 m->buf = vmalloc(m->size);
Eric Biederman33da8892009-02-04 15:12:25 -0800140 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);
xiaobing tu24a8d102013-01-31 14:11:32 +0800234 is_vmalloc_addr(m->buf) ? vfree(m->buf) : kfree(m->buf);
235 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL | __GFP_NOWARN);
236 if (!m->buf)
237 m->buf = vmalloc(m->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!m->buf)
239 goto Enomem;
240 m->count = 0;
241 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400242 pos = m->index;
243 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 m->op->stop(m, p);
246 m->count = 0;
247 goto Done;
248Fill:
249 /* they want more? let's try to get some more */
250 while (m->count < size) {
251 size_t offs = m->count;
252 loff_t next = pos;
253 p = m->op->next(m, p, &next);
254 if (!p || IS_ERR(p)) {
255 err = PTR_ERR(p);
256 break;
257 }
258 err = m->op->show(m, p);
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700259 if (seq_overflow(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400261 if (likely(err <= 0))
262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 pos = next;
265 }
266 m->op->stop(m, p);
267 n = min(m->count, size);
268 err = copy_to_user(buf, m->buf, n);
269 if (err)
270 goto Efault;
271 copied += n;
272 m->count -= n;
273 if (m->count)
274 m->from = n;
275 else
276 pos++;
277 m->index = pos;
278Done:
279 if (!copied)
280 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800281 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800283 m->read_pos += copied;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800286 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return copied;
288Enomem:
289 err = -ENOMEM;
290 goto Done;
291Efault:
292 err = -EFAULT;
293 goto Done;
294}
295EXPORT_SYMBOL(seq_read);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/**
298 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700299 * @file: the file in question
300 * @offset: new position
301 * @origin: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * Ready-made ->f_op->llseek()
304 */
305loff_t seq_lseek(struct file *file, loff_t offset, int origin)
306{
Joe Perches8209e2f2010-09-04 18:52:49 -0700307 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200308 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Ingo Molnar0ac17592006-03-23 03:00:37 -0800310 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 m->version = file->f_version;
312 switch (origin) {
313 case 1:
314 offset += file->f_pos;
315 case 0:
316 if (offset < 0)
317 break;
318 retval = offset;
Eric Biederman8f19d472009-02-18 14:48:16 -0800319 if (offset != m->read_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 while ((retval=traverse(m, offset)) == -EAGAIN)
321 ;
322 if (retval) {
323 /* with extreme prejudice... */
324 file->f_pos = 0;
Eric Biederman8f19d472009-02-18 14:48:16 -0800325 m->read_pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 m->version = 0;
327 m->index = 0;
328 m->count = 0;
329 } else {
Eric Biederman8f19d472009-02-18 14:48:16 -0800330 m->read_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 retval = file->f_pos = offset;
332 }
333 }
334 }
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
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800344 * @inode: file->f_path.dentry->d_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;
xiaobing tu24a8d102013-01-31 14:11:32 +0800352 is_vmalloc_addr(m->buf) ? vfree(m->buf) : kfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 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
394int seq_printf(struct seq_file *m, const char *f, ...)
395{
396 va_list args;
397 int len;
398
399 if (m->count < m->size) {
400 va_start(args, f);
401 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
402 va_end(args);
403 if (m->count + len < m->size) {
404 m->count += len;
405 return 0;
406 }
407 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700408 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return -1;
410}
411EXPORT_SYMBOL(seq_printf);
412
Török Edwin74e2f332008-11-22 13:28:48 +0200413/**
Török Edwin958086d2008-11-23 23:24:53 +0200414 * mangle_path - mangle and copy path to buffer beginning
415 * @s: buffer start
416 * @p: beginning of path in above buffer
417 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200418 *
419 * Copy the path from @p to @s, replacing each occurrence of character from
420 * @esc with usual octal escape.
421 * Returns pointer past last written character in @s, or NULL in case of
422 * failure.
423 */
Al Viro8c9379e2011-12-08 20:18:57 -0500424char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100425{
426 while (s <= p) {
427 char c = *p++;
428 if (!c) {
429 return s;
430 } else if (!strchr(esc, c)) {
431 *s++ = c;
432 } else if (s + 4 > p) {
433 break;
434 } else {
435 *s++ = '\\';
436 *s++ = '0' + ((c & 0300) >> 6);
437 *s++ = '0' + ((c & 070) >> 3);
438 *s++ = '0' + (c & 07);
439 }
440 }
441 return NULL;
442}
Ingo Molnar604094f2008-11-28 18:03:22 +0100443EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100444
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800445/**
446 * seq_path - seq_file interface to print a pathname
447 * @m: the seq_file handle
448 * @path: the struct path to print
449 * @esc: set of characters to escape in the output
450 *
451 * return the absolute path of 'path', as represented by the
452 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100453 */
Al Viro8c9379e2011-12-08 20:18:57 -0500454int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Miklos Szeredif8439802009-09-21 14:48:36 +0200456 char *buf;
457 size_t size = seq_get_buf(m, &buf);
458 int res = -1;
459
460 if (size) {
461 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200463 char *end = mangle_path(buf, p, esc);
464 if (end)
465 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200468 seq_commit(m, res);
469
470 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472EXPORT_SYMBOL(seq_path);
473
Ram Pai6092d042008-03-27 13:06:20 +0100474/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100475 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100476 */
Al Viro8c9379e2011-12-08 20:18:57 -0500477int seq_path_root(struct seq_file *m, const struct path *path,
478 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100479{
Miklos Szeredif8439802009-09-21 14:48:36 +0200480 char *buf;
481 size_t size = seq_get_buf(m, &buf);
482 int res = -ENAMETOOLONG;
483
484 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100485 char *p;
486
Miklos Szeredif8439802009-09-21 14:48:36 +0200487 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500488 if (!p)
489 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200490 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100491 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200492 char *end = mangle_path(buf, p, esc);
493 if (end)
494 res = end - buf;
495 else
496 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100497 }
498 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200499 seq_commit(m, res);
500
Al Viro02125a82011-12-05 08:43:34 -0500501 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100502}
503
504/*
Ram Pai6092d042008-03-27 13:06:20 +0100505 * returns the path of the 'dentry' from the root of its filesystem.
506 */
Al Viro8c9379e2011-12-08 20:18:57 -0500507int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100508{
Miklos Szeredif8439802009-09-21 14:48:36 +0200509 char *buf;
510 size_t size = seq_get_buf(m, &buf);
511 int res = -1;
512
513 if (size) {
514 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100515 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200516 char *end = mangle_path(buf, p, esc);
517 if (end)
518 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100519 }
520 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200521 seq_commit(m, res);
522
523 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100524}
525
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030526int seq_bitmap(struct seq_file *m, const unsigned long *bits,
527 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700528{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700529 if (m->count < m->size) {
530 int len = bitmap_scnprintf(m->buf + m->count,
531 m->size - m->count, bits, nr_bits);
532 if (m->count + len < m->size) {
533 m->count += len;
534 return 0;
535 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700536 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700537 seq_set_overflow(m);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700538 return -1;
539}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700540EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700541
Rusty Russellaf76aba2009-03-30 22:05:11 -0600542int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700543 unsigned int nr_bits)
544{
545 if (m->count < m->size) {
546 int len = bitmap_scnlistprintf(m->buf + m->count,
547 m->size - m->count, bits, nr_bits);
548 if (m->count + len < m->size) {
549 m->count += len;
550 return 0;
551 }
552 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700553 seq_set_overflow(m);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700554 return -1;
555}
556EXPORT_SYMBOL(seq_bitmap_list);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static void *single_start(struct seq_file *p, loff_t *pos)
559{
560 return NULL + (*pos == 0);
561}
562
563static void *single_next(struct seq_file *p, void *v, loff_t *pos)
564{
565 ++*pos;
566 return NULL;
567}
568
569static void single_stop(struct seq_file *p, void *v)
570{
571}
572
573int single_open(struct file *file, int (*show)(struct seq_file *, void *),
574 void *data)
575{
576 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
577 int res = -ENOMEM;
578
579 if (op) {
580 op->start = single_start;
581 op->next = single_next;
582 op->stop = single_stop;
583 op->show = show;
584 res = seq_open(file, op);
585 if (!res)
586 ((struct seq_file *)file->private_data)->private = data;
587 else
588 kfree(op);
589 }
590 return res;
591}
592EXPORT_SYMBOL(single_open);
593
594int single_release(struct inode *inode, struct file *file)
595{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800596 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int res = seq_release(inode, file);
598 kfree(op);
599 return res;
600}
601EXPORT_SYMBOL(single_release);
602
603int seq_release_private(struct inode *inode, struct file *file)
604{
605 struct seq_file *seq = file->private_data;
606
607 kfree(seq->private);
608 seq->private = NULL;
609 return seq_release(inode, file);
610}
611EXPORT_SYMBOL(seq_release_private);
612
Pavel Emelyanov39699032007-10-10 02:28:42 -0700613void *__seq_open_private(struct file *f, const struct seq_operations *ops,
614 int psize)
615{
616 int rc;
617 void *private;
618 struct seq_file *seq;
619
620 private = kzalloc(psize, GFP_KERNEL);
621 if (private == NULL)
622 goto out;
623
624 rc = seq_open(f, ops);
625 if (rc < 0)
626 goto out_free;
627
628 seq = f->private_data;
629 seq->private = private;
630 return private;
631
632out_free:
633 kfree(private);
634out:
635 return NULL;
636}
637EXPORT_SYMBOL(__seq_open_private);
638
639int seq_open_private(struct file *filp, const struct seq_operations *ops,
640 int psize)
641{
642 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
643}
644EXPORT_SYMBOL(seq_open_private);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646int seq_putc(struct seq_file *m, char c)
647{
648 if (m->count < m->size) {
649 m->buf[m->count++] = c;
650 return 0;
651 }
652 return -1;
653}
654EXPORT_SYMBOL(seq_putc);
655
656int seq_puts(struct seq_file *m, const char *s)
657{
658 int len = strlen(s);
659 if (m->count + len < m->size) {
660 memcpy(m->buf + m->count, s, len);
661 m->count += len;
662 return 0;
663 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700664 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -1;
666}
667EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700668
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700669/*
670 * A helper routine for putting decimal numbers without rich format of printf().
671 * only 'unsigned long long' is supported.
672 * This routine will put one byte delimiter + number into seq_file.
673 * This routine is very quick when you show lots of numbers.
674 * In usual cases, it will be better to use seq_printf(). It's easier to read.
675 */
676int seq_put_decimal_ull(struct seq_file *m, char delimiter,
677 unsigned long long num)
678{
679 int len;
680
681 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
682 goto overflow;
683
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700684 if (delimiter)
685 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700686
687 if (num < 10) {
688 m->buf[m->count++] = num + '0';
689 return 0;
690 }
691
692 len = num_to_str(m->buf + m->count, m->size - m->count, num);
693 if (!len)
694 goto overflow;
695 m->count += len;
696 return 0;
697overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700698 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700699 return -1;
700}
701EXPORT_SYMBOL(seq_put_decimal_ull);
702
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700703int seq_put_decimal_ll(struct seq_file *m, char delimiter,
704 long long num)
705{
706 if (num < 0) {
707 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700708 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700709 return -1;
710 }
711 if (delimiter)
712 m->buf[m->count++] = delimiter;
713 num = -num;
714 delimiter = '-';
715 }
716 return seq_put_decimal_ull(m, delimiter, num);
717
718}
719EXPORT_SYMBOL(seq_put_decimal_ll);
720
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700721/**
722 * seq_write - write arbitrary data to buffer
723 * @seq: seq_file identifying the buffer to which data should be written
724 * @data: data address
725 * @len: number of bytes
726 *
727 * Return 0 on success, non-zero otherwise.
728 */
729int seq_write(struct seq_file *seq, const void *data, size_t len)
730{
731 if (seq->count + len < seq->size) {
732 memcpy(seq->buf + seq->count, data, len);
733 seq->count += len;
734 return 0;
735 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700736 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700737 return -1;
738}
739EXPORT_SYMBOL(seq_write);
740
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700741struct list_head *seq_list_start(struct list_head *head, loff_t pos)
742{
743 struct list_head *lh;
744
745 list_for_each(lh, head)
746 if (pos-- == 0)
747 return lh;
748
749 return NULL;
750}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700751EXPORT_SYMBOL(seq_list_start);
752
753struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
754{
755 if (!pos)
756 return head;
757
758 return seq_list_start(head, pos - 1);
759}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700760EXPORT_SYMBOL(seq_list_start_head);
761
762struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
763{
764 struct list_head *lh;
765
766 lh = ((struct list_head *)v)->next;
767 ++*ppos;
768 return lh == head ? NULL : lh;
769}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700770EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000771
772/**
773 * seq_hlist_start - start an iteration of a hlist
774 * @head: the head of the hlist
775 * @pos: the start position of the sequence
776 *
777 * Called at seq_file->op->start().
778 */
779struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
780{
781 struct hlist_node *node;
782
783 hlist_for_each(node, head)
784 if (pos-- == 0)
785 return node;
786 return NULL;
787}
788EXPORT_SYMBOL(seq_hlist_start);
789
790/**
791 * seq_hlist_start_head - start an iteration of a hlist
792 * @head: the head of the hlist
793 * @pos: the start position of the sequence
794 *
795 * Called at seq_file->op->start(). Call this function if you want to
796 * print a header at the top of the output.
797 */
798struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
799{
800 if (!pos)
801 return SEQ_START_TOKEN;
802
803 return seq_hlist_start(head, pos - 1);
804}
805EXPORT_SYMBOL(seq_hlist_start_head);
806
807/**
808 * seq_hlist_next - move to the next position of the hlist
809 * @v: the current iterator
810 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800811 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000812 *
813 * Called at seq_file->op->next().
814 */
815struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
816 loff_t *ppos)
817{
818 struct hlist_node *node = v;
819
820 ++*ppos;
821 if (v == SEQ_START_TOKEN)
822 return head->first;
823 else
824 return node->next;
825}
826EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000827
828/**
829 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
830 * @head: the head of the hlist
831 * @pos: the start position of the sequence
832 *
833 * Called at seq_file->op->start().
834 *
835 * This list-traversal primitive may safely run concurrently with
836 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
837 * as long as the traversal is guarded by rcu_read_lock().
838 */
839struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
840 loff_t pos)
841{
842 struct hlist_node *node;
843
844 __hlist_for_each_rcu(node, head)
845 if (pos-- == 0)
846 return node;
847 return NULL;
848}
849EXPORT_SYMBOL(seq_hlist_start_rcu);
850
851/**
852 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
853 * @head: the head of the hlist
854 * @pos: the start position of the sequence
855 *
856 * Called at seq_file->op->start(). Call this function if you want to
857 * print a header at the top of the output.
858 *
859 * This list-traversal primitive may safely run concurrently with
860 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
861 * as long as the traversal is guarded by rcu_read_lock().
862 */
863struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
864 loff_t pos)
865{
866 if (!pos)
867 return SEQ_START_TOKEN;
868
869 return seq_hlist_start_rcu(head, pos - 1);
870}
871EXPORT_SYMBOL(seq_hlist_start_head_rcu);
872
873/**
874 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
875 * @v: the current iterator
876 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800877 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000878 *
879 * Called at seq_file->op->next().
880 *
881 * This list-traversal primitive may safely run concurrently with
882 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
883 * as long as the traversal is guarded by rcu_read_lock().
884 */
885struct hlist_node *seq_hlist_next_rcu(void *v,
886 struct hlist_head *head,
887 loff_t *ppos)
888{
889 struct hlist_node *node = v;
890
891 ++*ppos;
892 if (v == SEQ_START_TOKEN)
893 return rcu_dereference(head->first);
894 else
895 return rcu_dereference(node->next);
896}
897EXPORT_SYMBOL(seq_hlist_next_rcu);