blob: 353948ba1c5b2d2ed08ead3448fb9218cbb89f96 [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>
Heiko Carstens058504e2014-07-02 15:22:37 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060013#include <linux/cred.h>
Heiko Carstens058504e2014-07-02 15:22:37 -070014#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/uaccess.h>
17#include <asm/page.h>
18
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070019static void seq_set_overflow(struct seq_file *m)
20{
21 m->count = m->size;
22}
23
Heiko Carstens058504e2014-07-02 15:22:37 -070024static void *seq_buf_alloc(unsigned long size)
25{
26 void *buf;
27
28 buf = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
29 if (!buf && size > PAGE_SIZE)
30 buf = vmalloc(size);
31 return buf;
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;
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060061#ifdef CONFIG_USER_NS
62 p->user_ns = file->f_cred->user_ns;
63#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /*
66 * Wrappers around seq_open(e.g. swaps_open) need to be
67 * aware of this. If they set f_version themselves, they
68 * should call seq_open first and then set f_version.
69 */
70 file->f_version = 0;
71
Eric Biederman8f19d472009-02-18 14:48:16 -080072 /*
73 * seq_files support lseek() and pread(). They do not implement
74 * write() at all, but we clear FMODE_PWRITE here for historical
75 * reasons.
76 *
77 * If a client of seq_files a) implements file.write() and b) wishes to
78 * support pwrite() then that client will need to implement its own
79 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
80 */
81 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83}
84EXPORT_SYMBOL(seq_open);
85
Eric Biederman33da8892009-02-04 15:12:25 -080086static int traverse(struct seq_file *m, loff_t offset)
87{
88 loff_t pos = 0, index;
89 int error = 0;
90 void *p;
91
92 m->version = 0;
93 index = 0;
94 m->count = m->from = 0;
95 if (!offset) {
96 m->index = index;
97 return 0;
98 }
99 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700100 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800101 if (!m->buf)
102 return -ENOMEM;
103 }
104 p = m->op->start(m, &index);
105 while (p) {
106 error = PTR_ERR(p);
107 if (IS_ERR(p))
108 break;
109 error = m->op->show(m, p);
110 if (error < 0)
111 break;
112 if (unlikely(error)) {
113 error = 0;
114 m->count = 0;
115 }
Joe Perches1f33c412014-09-29 16:08:21 -0700116 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800117 goto Eoverflow;
118 if (pos + m->count > offset) {
119 m->from = offset - pos;
120 m->count -= m->from;
121 m->index = index;
122 break;
123 }
124 pos += m->count;
125 m->count = 0;
126 if (pos == offset) {
127 index++;
128 m->index = index;
129 break;
130 }
131 p = m->op->next(m, p, &index);
132 }
133 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300134 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800135 return error;
136
137Eoverflow:
138 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700139 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000140 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700141 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800142 return !m->buf ? -ENOMEM : -EAGAIN;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/**
146 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700147 * @file: the file to read from
148 * @buf: the buffer to read to
149 * @size: the maximum number of bytes to read
150 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
152 * Ready-made ->f_op->read()
153 */
154ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
155{
Joe Perches8209e2f2010-09-04 18:52:49 -0700156 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 size_t copied = 0;
158 loff_t pos;
159 size_t n;
160 void *p;
161 int err = 0;
162
Ingo Molnar0ac17592006-03-23 03:00:37 -0800163 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * seq_file->op->..m_start/m_stop/m_next may do special actions
167 * or optimisations based on the file->f_version, so we want to
168 * pass the file->f_version to those methods.
169 *
170 * seq_file->version is just copy of f_version, and seq_file
171 * methods can treat it simply as file version.
172 * It is copied in first and copied out after all operations.
173 * It is convenient to have it as part of structure to avoid the
174 * need of passing another argument to all the seq_file methods.
175 */
176 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700177
178 /* Don't assume *ppos is where we left it */
179 if (unlikely(*ppos != m->read_pos)) {
180 while ((err = traverse(m, *ppos)) == -EAGAIN)
181 ;
182 if (err) {
183 /* With prejudice... */
184 m->read_pos = 0;
185 m->version = 0;
186 m->index = 0;
187 m->count = 0;
188 goto Done;
189 } else {
190 m->read_pos = *ppos;
191 }
192 }
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* grab buffer if we didn't have one */
195 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700196 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!m->buf)
198 goto Enomem;
199 }
200 /* if not empty - flush it first */
201 if (m->count) {
202 n = min(m->count, size);
203 err = copy_to_user(buf, m->buf + m->from, n);
204 if (err)
205 goto Efault;
206 m->count -= n;
207 m->from += n;
208 size -= n;
209 buf += n;
210 copied += n;
211 if (!m->count)
212 m->index++;
213 if (!size)
214 goto Done;
215 }
216 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400217 pos = m->index;
218 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 err = PTR_ERR(p);
221 if (!p || IS_ERR(p))
222 break;
223 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400224 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
Al Viro521b5d02008-03-28 00:46:41 -0400226 if (unlikely(err))
227 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400228 if (unlikely(!m->count)) {
229 p = m->op->next(m, p, &pos);
230 m->index = pos;
231 continue;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (m->count < m->size)
234 goto Fill;
235 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700236 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000237 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700238 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!m->buf)
240 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 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);
Joe Perches1f33c412014-09-29 16:08:21 -0700259 if (seq_has_overflowed(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
Randy Dunlap254adaa2013-01-09 17:13:00 -0800301 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * Ready-made ->f_op->llseek()
304 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800305loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
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;
Andrew Morton965c8e52012-12-17 15:59:39 -0800312 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800313 case SEEK_CUR:
314 offset += file->f_pos;
315 case SEEK_SET:
316 if (offset < 0)
317 break;
318 retval = offset;
319 if (offset != m->read_pos) {
320 while ((retval = traverse(m, offset)) == -EAGAIN)
321 ;
322 if (retval) {
323 /* with extreme prejudice... */
324 file->f_pos = 0;
325 m->read_pos = 0;
326 m->version = 0;
327 m->index = 0;
328 m->count = 0;
329 } else {
330 m->read_pos = offset;
331 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Gu Zheng05e16742013-10-25 18:15:06 +0800333 } else {
334 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700338 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return retval;
340}
341EXPORT_SYMBOL(seq_lseek);
342
343/**
344 * seq_release - free the structures associated with sequential file.
345 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500346 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
348 * Frees the structures associated with sequential file; can be used
349 * as ->f_op->release() if you don't have private data to destroy.
350 */
351int seq_release(struct inode *inode, struct file *file)
352{
Joe Perches8209e2f2010-09-04 18:52:49 -0700353 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700354 kvfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 kfree(m);
356 return 0;
357}
358EXPORT_SYMBOL(seq_release);
359
360/**
361 * seq_escape - print string into buffer, escaping some characters
362 * @m: target buffer
363 * @s: string
364 * @esc: set of characters that need escaping
365 *
366 * Puts string into buffer, replacing each occurrence of character from
367 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
368 * case of overflow.
369 */
370int seq_escape(struct seq_file *m, const char *s, const char *esc)
371{
372 char *end = m->buf + m->size;
373 char *p;
374 char c;
375
376 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
377 if (!strchr(esc, c)) {
378 *p++ = c;
379 continue;
380 }
381 if (p + 3 < end) {
382 *p++ = '\\';
383 *p++ = '0' + ((c & 0300) >> 6);
384 *p++ = '0' + ((c & 070) >> 3);
385 *p++ = '0' + (c & 07);
386 continue;
387 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700388 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -1;
390 }
391 m->count = p - m->buf;
392 return 0;
393}
394EXPORT_SYMBOL(seq_escape);
395
Steven Whitehousea4808142012-06-11 13:16:35 +0100396int seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int len;
399
400 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (m->count + len < m->size) {
403 m->count += len;
404 return 0;
405 }
406 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700407 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -1;
409}
Steven Whitehousea4808142012-06-11 13:16:35 +0100410EXPORT_SYMBOL(seq_vprintf);
411
412int seq_printf(struct seq_file *m, const char *f, ...)
413{
414 int ret;
415 va_list args;
416
417 va_start(args, f);
418 ret = seq_vprintf(m, f, args);
419 va_end(args);
420
421 return ret;
422}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423EXPORT_SYMBOL(seq_printf);
424
Török Edwin74e2f332008-11-22 13:28:48 +0200425/**
Török Edwin958086d2008-11-23 23:24:53 +0200426 * mangle_path - mangle and copy path to buffer beginning
427 * @s: buffer start
428 * @p: beginning of path in above buffer
429 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200430 *
431 * Copy the path from @p to @s, replacing each occurrence of character from
432 * @esc with usual octal escape.
433 * Returns pointer past last written character in @s, or NULL in case of
434 * failure.
435 */
Al Viro8c9379e2011-12-08 20:18:57 -0500436char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100437{
438 while (s <= p) {
439 char c = *p++;
440 if (!c) {
441 return s;
442 } else if (!strchr(esc, c)) {
443 *s++ = c;
444 } else if (s + 4 > p) {
445 break;
446 } else {
447 *s++ = '\\';
448 *s++ = '0' + ((c & 0300) >> 6);
449 *s++ = '0' + ((c & 070) >> 3);
450 *s++ = '0' + (c & 07);
451 }
452 }
453 return NULL;
454}
Ingo Molnar604094f2008-11-28 18:03:22 +0100455EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100456
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800457/**
458 * seq_path - seq_file interface to print a pathname
459 * @m: the seq_file handle
460 * @path: the struct path to print
461 * @esc: set of characters to escape in the output
462 *
463 * return the absolute path of 'path', as represented by the
464 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100465 */
Al Viro8c9379e2011-12-08 20:18:57 -0500466int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Miklos Szeredif8439802009-09-21 14:48:36 +0200468 char *buf;
469 size_t size = seq_get_buf(m, &buf);
470 int res = -1;
471
472 if (size) {
473 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200475 char *end = mangle_path(buf, p, esc);
476 if (end)
477 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200480 seq_commit(m, res);
481
482 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484EXPORT_SYMBOL(seq_path);
485
Ram Pai6092d042008-03-27 13:06:20 +0100486/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100487 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100488 */
Al Viro8c9379e2011-12-08 20:18:57 -0500489int seq_path_root(struct seq_file *m, const struct path *path,
490 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100491{
Miklos Szeredif8439802009-09-21 14:48:36 +0200492 char *buf;
493 size_t size = seq_get_buf(m, &buf);
494 int res = -ENAMETOOLONG;
495
496 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100497 char *p;
498
Miklos Szeredif8439802009-09-21 14:48:36 +0200499 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500500 if (!p)
501 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200502 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100503 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200504 char *end = mangle_path(buf, p, esc);
505 if (end)
506 res = end - buf;
507 else
508 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100509 }
510 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200511 seq_commit(m, res);
512
Al Viro02125a82011-12-05 08:43:34 -0500513 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100514}
515
516/*
Ram Pai6092d042008-03-27 13:06:20 +0100517 * returns the path of the 'dentry' from the root of its filesystem.
518 */
Al Viro8c9379e2011-12-08 20:18:57 -0500519int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100520{
Miklos Szeredif8439802009-09-21 14:48:36 +0200521 char *buf;
522 size_t size = seq_get_buf(m, &buf);
523 int res = -1;
524
525 if (size) {
526 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100527 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200528 char *end = mangle_path(buf, p, esc);
529 if (end)
530 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100531 }
532 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200533 seq_commit(m, res);
534
535 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100536}
537
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030538int seq_bitmap(struct seq_file *m, const unsigned long *bits,
539 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700540{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700541 if (m->count < m->size) {
542 int len = bitmap_scnprintf(m->buf + m->count,
543 m->size - m->count, bits, nr_bits);
544 if (m->count + len < m->size) {
545 m->count += len;
546 return 0;
547 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700548 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700549 seq_set_overflow(m);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700550 return -1;
551}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700552EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700553
Rusty Russellaf76aba2009-03-30 22:05:11 -0600554int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700555 unsigned int nr_bits)
556{
557 if (m->count < m->size) {
558 int len = bitmap_scnlistprintf(m->buf + m->count,
559 m->size - m->count, bits, nr_bits);
560 if (m->count + len < m->size) {
561 m->count += len;
562 return 0;
563 }
564 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700565 seq_set_overflow(m);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700566 return -1;
567}
568EXPORT_SYMBOL(seq_bitmap_list);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static void *single_start(struct seq_file *p, loff_t *pos)
571{
572 return NULL + (*pos == 0);
573}
574
575static void *single_next(struct seq_file *p, void *v, loff_t *pos)
576{
577 ++*pos;
578 return NULL;
579}
580
581static void single_stop(struct seq_file *p, void *v)
582{
583}
584
585int single_open(struct file *file, int (*show)(struct seq_file *, void *),
586 void *data)
587{
588 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
589 int res = -ENOMEM;
590
591 if (op) {
592 op->start = single_start;
593 op->next = single_next;
594 op->stop = single_stop;
595 op->show = show;
596 res = seq_open(file, op);
597 if (!res)
598 ((struct seq_file *)file->private_data)->private = data;
599 else
600 kfree(op);
601 }
602 return res;
603}
604EXPORT_SYMBOL(single_open);
605
Al Viro2043f492013-03-31 13:43:23 -0400606int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
607 void *data, size_t size)
608{
Heiko Carstens058504e2014-07-02 15:22:37 -0700609 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400610 int ret;
611 if (!buf)
612 return -ENOMEM;
613 ret = single_open(file, show, data);
614 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700615 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400616 return ret;
617 }
618 ((struct seq_file *)file->private_data)->buf = buf;
619 ((struct seq_file *)file->private_data)->size = size;
620 return 0;
621}
622EXPORT_SYMBOL(single_open_size);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624int single_release(struct inode *inode, struct file *file)
625{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800626 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int res = seq_release(inode, file);
628 kfree(op);
629 return res;
630}
631EXPORT_SYMBOL(single_release);
632
633int seq_release_private(struct inode *inode, struct file *file)
634{
635 struct seq_file *seq = file->private_data;
636
637 kfree(seq->private);
638 seq->private = NULL;
639 return seq_release(inode, file);
640}
641EXPORT_SYMBOL(seq_release_private);
642
Pavel Emelyanov39699032007-10-10 02:28:42 -0700643void *__seq_open_private(struct file *f, const struct seq_operations *ops,
644 int psize)
645{
646 int rc;
647 void *private;
648 struct seq_file *seq;
649
650 private = kzalloc(psize, GFP_KERNEL);
651 if (private == NULL)
652 goto out;
653
654 rc = seq_open(f, ops);
655 if (rc < 0)
656 goto out_free;
657
658 seq = f->private_data;
659 seq->private = private;
660 return private;
661
662out_free:
663 kfree(private);
664out:
665 return NULL;
666}
667EXPORT_SYMBOL(__seq_open_private);
668
669int seq_open_private(struct file *filp, const struct seq_operations *ops,
670 int psize)
671{
672 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
673}
674EXPORT_SYMBOL(seq_open_private);
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676int seq_putc(struct seq_file *m, char c)
677{
678 if (m->count < m->size) {
679 m->buf[m->count++] = c;
680 return 0;
681 }
682 return -1;
683}
684EXPORT_SYMBOL(seq_putc);
685
686int seq_puts(struct seq_file *m, const char *s)
687{
688 int len = strlen(s);
689 if (m->count + len < m->size) {
690 memcpy(m->buf + m->count, s, len);
691 m->count += len;
692 return 0;
693 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700694 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -1;
696}
697EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700698
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700699/*
700 * A helper routine for putting decimal numbers without rich format of printf().
701 * only 'unsigned long long' is supported.
702 * This routine will put one byte delimiter + number into seq_file.
703 * This routine is very quick when you show lots of numbers.
704 * In usual cases, it will be better to use seq_printf(). It's easier to read.
705 */
706int seq_put_decimal_ull(struct seq_file *m, char delimiter,
707 unsigned long long num)
708{
709 int len;
710
711 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
712 goto overflow;
713
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700714 if (delimiter)
715 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700716
717 if (num < 10) {
718 m->buf[m->count++] = num + '0';
719 return 0;
720 }
721
722 len = num_to_str(m->buf + m->count, m->size - m->count, num);
723 if (!len)
724 goto overflow;
725 m->count += len;
726 return 0;
727overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700728 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700729 return -1;
730}
731EXPORT_SYMBOL(seq_put_decimal_ull);
732
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700733int seq_put_decimal_ll(struct seq_file *m, char delimiter,
734 long long num)
735{
736 if (num < 0) {
737 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700738 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700739 return -1;
740 }
741 if (delimiter)
742 m->buf[m->count++] = delimiter;
743 num = -num;
744 delimiter = '-';
745 }
746 return seq_put_decimal_ull(m, delimiter, num);
747
748}
749EXPORT_SYMBOL(seq_put_decimal_ll);
750
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700751/**
752 * seq_write - write arbitrary data to buffer
753 * @seq: seq_file identifying the buffer to which data should be written
754 * @data: data address
755 * @len: number of bytes
756 *
757 * Return 0 on success, non-zero otherwise.
758 */
759int seq_write(struct seq_file *seq, const void *data, size_t len)
760{
761 if (seq->count + len < seq->size) {
762 memcpy(seq->buf + seq->count, data, len);
763 seq->count += len;
764 return 0;
765 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700766 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700767 return -1;
768}
769EXPORT_SYMBOL(seq_write);
770
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800771/**
772 * seq_pad - write padding spaces to buffer
773 * @m: seq_file identifying the buffer to which data should be written
774 * @c: the byte to append after padding if non-zero
775 */
776void seq_pad(struct seq_file *m, char c)
777{
778 int size = m->pad_until - m->count;
779 if (size > 0)
780 seq_printf(m, "%*s", size, "");
781 if (c)
782 seq_putc(m, c);
783}
784EXPORT_SYMBOL(seq_pad);
785
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700786struct list_head *seq_list_start(struct list_head *head, loff_t pos)
787{
788 struct list_head *lh;
789
790 list_for_each(lh, head)
791 if (pos-- == 0)
792 return lh;
793
794 return NULL;
795}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700796EXPORT_SYMBOL(seq_list_start);
797
798struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
799{
800 if (!pos)
801 return head;
802
803 return seq_list_start(head, pos - 1);
804}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700805EXPORT_SYMBOL(seq_list_start_head);
806
807struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
808{
809 struct list_head *lh;
810
811 lh = ((struct list_head *)v)->next;
812 ++*ppos;
813 return lh == head ? NULL : lh;
814}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700815EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000816
817/**
818 * seq_hlist_start - start an iteration of a hlist
819 * @head: the head of the hlist
820 * @pos: the start position of the sequence
821 *
822 * Called at seq_file->op->start().
823 */
824struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
825{
826 struct hlist_node *node;
827
828 hlist_for_each(node, head)
829 if (pos-- == 0)
830 return node;
831 return NULL;
832}
833EXPORT_SYMBOL(seq_hlist_start);
834
835/**
836 * seq_hlist_start_head - start an iteration of a hlist
837 * @head: the head of the hlist
838 * @pos: the start position of the sequence
839 *
840 * Called at seq_file->op->start(). Call this function if you want to
841 * print a header at the top of the output.
842 */
843struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
844{
845 if (!pos)
846 return SEQ_START_TOKEN;
847
848 return seq_hlist_start(head, pos - 1);
849}
850EXPORT_SYMBOL(seq_hlist_start_head);
851
852/**
853 * seq_hlist_next - move to the next position of the hlist
854 * @v: the current iterator
855 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800856 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000857 *
858 * Called at seq_file->op->next().
859 */
860struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
861 loff_t *ppos)
862{
863 struct hlist_node *node = v;
864
865 ++*ppos;
866 if (v == SEQ_START_TOKEN)
867 return head->first;
868 else
869 return node->next;
870}
871EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000872
873/**
874 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
875 * @head: the head of the hlist
876 * @pos: the start position of the sequence
877 *
878 * Called at seq_file->op->start().
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 */
884struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
885 loff_t pos)
886{
887 struct hlist_node *node;
888
889 __hlist_for_each_rcu(node, head)
890 if (pos-- == 0)
891 return node;
892 return NULL;
893}
894EXPORT_SYMBOL(seq_hlist_start_rcu);
895
896/**
897 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
898 * @head: the head of the hlist
899 * @pos: the start position of the sequence
900 *
901 * Called at seq_file->op->start(). Call this function if you want to
902 * print a header at the top of the output.
903 *
904 * This list-traversal primitive may safely run concurrently with
905 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
906 * as long as the traversal is guarded by rcu_read_lock().
907 */
908struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
909 loff_t pos)
910{
911 if (!pos)
912 return SEQ_START_TOKEN;
913
914 return seq_hlist_start_rcu(head, pos - 1);
915}
916EXPORT_SYMBOL(seq_hlist_start_head_rcu);
917
918/**
919 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
920 * @v: the current iterator
921 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800922 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000923 *
924 * Called at seq_file->op->next().
925 *
926 * This list-traversal primitive may safely run concurrently with
927 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
928 * as long as the traversal is guarded by rcu_read_lock().
929 */
930struct hlist_node *seq_hlist_next_rcu(void *v,
931 struct hlist_head *head,
932 loff_t *ppos)
933{
934 struct hlist_node *node = v;
935
936 ++*ppos;
937 if (v == SEQ_START_TOKEN)
938 return rcu_dereference(head->first);
939 else
940 return rcu_dereference(node->next);
941}
942EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400943
944/**
945 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
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->start().
951 */
952struct hlist_node *
953seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
954{
955 struct hlist_node *node;
956
957 for_each_possible_cpu(*cpu) {
958 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
959 if (pos-- == 0)
960 return node;
961 }
962 }
963 return NULL;
964}
965EXPORT_SYMBOL(seq_hlist_start_percpu);
966
967/**
968 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
969 * @v: pointer to current hlist_node
970 * @head: pointer to percpu array of struct hlist_heads
971 * @cpu: pointer to cpu "cursor"
972 * @pos: start position of sequence
973 *
974 * Called at seq_file->op->next().
975 */
976struct hlist_node *
977seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
978 int *cpu, loff_t *pos)
979{
980 struct hlist_node *node = v;
981
982 ++*pos;
983
984 if (node->next)
985 return node->next;
986
987 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
988 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
989 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
990
991 if (!hlist_empty(bucket))
992 return bucket->first;
993 }
994 return NULL;
995}
996EXPORT_SYMBOL(seq_hlist_next_percpu);