blob: fbb1688bff87c081ab01c58f6bcbc0a615775670 [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 -070019
20/*
21 * seq_files have a buffer which can may overflow. When this happens a larger
22 * buffer is reallocated and all the data will be printed again.
23 * The overflow state is true when m->count == m->size.
24 */
25static bool seq_overflow(struct seq_file *m)
26{
27 return m->count == m->size;
28}
29
30static void seq_set_overflow(struct seq_file *m)
31{
32 m->count = m->size;
33}
34
Heiko Carstens058504e2014-07-02 15:22:37 -070035static void *seq_buf_alloc(unsigned long size)
36{
37 void *buf;
38
39 buf = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
40 if (!buf && size > PAGE_SIZE)
41 buf = vmalloc(size);
42 return buf;
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/**
46 * seq_open - initialize sequential file
47 * @file: file we initialize
48 * @op: method table describing the sequence
49 *
50 * seq_open() sets @file, associating it with a sequence described
51 * by @op. @op->start() sets the iterator up and returns the first
52 * element of sequence. @op->stop() shuts it down. @op->next()
53 * returns the next element of sequence. @op->show() prints element
54 * into the buffer. In case of error ->start() and ->next() return
55 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
56 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040057 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080059int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Al Viro1abe77b2005-11-07 17:15:34 -050061 struct seq_file *p = file->private_data;
62
63 if (!p) {
64 p = kmalloc(sizeof(*p), GFP_KERNEL);
65 if (!p)
66 return -ENOMEM;
67 file->private_data = p;
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080070 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 p->op = op;
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060072#ifdef CONFIG_USER_NS
73 p->user_ns = file->f_cred->user_ns;
74#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /*
77 * Wrappers around seq_open(e.g. swaps_open) need to be
78 * aware of this. If they set f_version themselves, they
79 * should call seq_open first and then set f_version.
80 */
81 file->f_version = 0;
82
Eric Biederman8f19d472009-02-18 14:48:16 -080083 /*
84 * seq_files support lseek() and pread(). They do not implement
85 * write() at all, but we clear FMODE_PWRITE here for historical
86 * reasons.
87 *
88 * If a client of seq_files a) implements file.write() and b) wishes to
89 * support pwrite() then that client will need to implement its own
90 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
91 */
92 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94}
95EXPORT_SYMBOL(seq_open);
96
Eric Biederman33da8892009-02-04 15:12:25 -080097static int traverse(struct seq_file *m, loff_t offset)
98{
99 loff_t pos = 0, index;
100 int error = 0;
101 void *p;
102
103 m->version = 0;
104 index = 0;
105 m->count = m->from = 0;
106 if (!offset) {
107 m->index = index;
108 return 0;
109 }
110 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700111 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800112 if (!m->buf)
113 return -ENOMEM;
114 }
115 p = m->op->start(m, &index);
116 while (p) {
117 error = PTR_ERR(p);
118 if (IS_ERR(p))
119 break;
120 error = m->op->show(m, p);
121 if (error < 0)
122 break;
123 if (unlikely(error)) {
124 error = 0;
125 m->count = 0;
126 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700127 if (seq_overflow(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800128 goto Eoverflow;
129 if (pos + m->count > offset) {
130 m->from = offset - pos;
131 m->count -= m->from;
132 m->index = index;
133 break;
134 }
135 pos += m->count;
136 m->count = 0;
137 if (pos == offset) {
138 index++;
139 m->index = index;
140 break;
141 }
142 p = m->op->next(m, p, &index);
143 }
144 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300145 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800146 return error;
147
148Eoverflow:
149 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700150 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000151 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700152 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800153 return !m->buf ? -ENOMEM : -EAGAIN;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/**
157 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700158 * @file: the file to read from
159 * @buf: the buffer to read to
160 * @size: the maximum number of bytes to read
161 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
163 * Ready-made ->f_op->read()
164 */
165ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
166{
Joe Perches8209e2f2010-09-04 18:52:49 -0700167 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 size_t copied = 0;
169 loff_t pos;
170 size_t n;
171 void *p;
172 int err = 0;
173
Ingo Molnar0ac17592006-03-23 03:00:37 -0800174 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /*
177 * seq_file->op->..m_start/m_stop/m_next may do special actions
178 * or optimisations based on the file->f_version, so we want to
179 * pass the file->f_version to those methods.
180 *
181 * seq_file->version is just copy of f_version, and seq_file
182 * methods can treat it simply as file version.
183 * It is copied in first and copied out after all operations.
184 * It is convenient to have it as part of structure to avoid the
185 * need of passing another argument to all the seq_file methods.
186 */
187 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700188
189 /* Don't assume *ppos is where we left it */
190 if (unlikely(*ppos != m->read_pos)) {
191 while ((err = traverse(m, *ppos)) == -EAGAIN)
192 ;
193 if (err) {
194 /* With prejudice... */
195 m->read_pos = 0;
196 m->version = 0;
197 m->index = 0;
198 m->count = 0;
199 goto Done;
200 } else {
201 m->read_pos = *ppos;
202 }
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* grab buffer if we didn't have one */
206 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700207 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!m->buf)
209 goto Enomem;
210 }
211 /* if not empty - flush it first */
212 if (m->count) {
213 n = min(m->count, size);
214 err = copy_to_user(buf, m->buf + m->from, n);
215 if (err)
216 goto Efault;
217 m->count -= n;
218 m->from += n;
219 size -= n;
220 buf += n;
221 copied += n;
Vegard Nossum5de0c132016-08-25 15:17:11 -0700222 if (!m->count) {
223 m->from = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 m->index++;
Vegard Nossum5de0c132016-08-25 15:17:11 -0700225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!size)
227 goto Done;
228 }
229 /* we need at least one record in buffer */
Al Viro4cdfe84b2008-08-24 07:45:33 -0400230 pos = m->index;
231 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 err = PTR_ERR(p);
234 if (!p || IS_ERR(p))
235 break;
236 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400237 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
Al Viro521b5d02008-03-28 00:46:41 -0400239 if (unlikely(err))
240 m->count = 0;
Al Viro4cdfe84b2008-08-24 07:45:33 -0400241 if (unlikely(!m->count)) {
242 p = m->op->next(m, p, &pos);
243 m->index = pos;
244 continue;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (m->count < m->size)
247 goto Fill;
248 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700249 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000250 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700251 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!m->buf)
253 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 m->version = 0;
Al Viro4cdfe84b2008-08-24 07:45:33 -0400255 pos = m->index;
256 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 m->op->stop(m, p);
259 m->count = 0;
260 goto Done;
261Fill:
262 /* they want more? let's try to get some more */
263 while (m->count < size) {
264 size_t offs = m->count;
265 loff_t next = pos;
266 p = m->op->next(m, p, &next);
267 if (!p || IS_ERR(p)) {
268 err = PTR_ERR(p);
269 break;
270 }
271 err = m->op->show(m, p);
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700272 if (seq_overflow(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400274 if (likely(err <= 0))
275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 pos = next;
278 }
279 m->op->stop(m, p);
280 n = min(m->count, size);
281 err = copy_to_user(buf, m->buf, n);
282 if (err)
283 goto Efault;
284 copied += n;
285 m->count -= n;
286 if (m->count)
287 m->from = n;
288 else
289 pos++;
290 m->index = pos;
291Done:
292 if (!copied)
293 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800294 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800296 m->read_pos += copied;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800299 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return copied;
301Enomem:
302 err = -ENOMEM;
303 goto Done;
304Efault:
305 err = -EFAULT;
306 goto Done;
307}
308EXPORT_SYMBOL(seq_read);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/**
311 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700312 * @file: the file in question
313 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800314 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *
316 * Ready-made ->f_op->llseek()
317 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800318loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Joe Perches8209e2f2010-09-04 18:52:49 -0700320 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200321 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Ingo Molnar0ac17592006-03-23 03:00:37 -0800323 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 m->version = file->f_version;
Andrew Morton965c8e52012-12-17 15:59:39 -0800325 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800326 case SEEK_CUR:
327 offset += file->f_pos;
328 case SEEK_SET:
329 if (offset < 0)
330 break;
331 retval = offset;
332 if (offset != m->read_pos) {
333 while ((retval = traverse(m, offset)) == -EAGAIN)
334 ;
335 if (retval) {
336 /* with extreme prejudice... */
337 file->f_pos = 0;
338 m->read_pos = 0;
339 m->version = 0;
340 m->index = 0;
341 m->count = 0;
342 } else {
343 m->read_pos = offset;
344 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Gu Zheng05e16742013-10-25 18:15:06 +0800346 } else {
347 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700351 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return retval;
353}
354EXPORT_SYMBOL(seq_lseek);
355
356/**
357 * seq_release - free the structures associated with sequential file.
358 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500359 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
361 * Frees the structures associated with sequential file; can be used
362 * as ->f_op->release() if you don't have private data to destroy.
363 */
364int seq_release(struct inode *inode, struct file *file)
365{
Joe Perches8209e2f2010-09-04 18:52:49 -0700366 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700367 kvfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 kfree(m);
369 return 0;
370}
371EXPORT_SYMBOL(seq_release);
372
373/**
374 * seq_escape - print string into buffer, escaping some characters
375 * @m: target buffer
376 * @s: string
377 * @esc: set of characters that need escaping
378 *
379 * Puts string into buffer, replacing each occurrence of character from
380 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
381 * case of overflow.
382 */
383int seq_escape(struct seq_file *m, const char *s, const char *esc)
384{
385 char *end = m->buf + m->size;
386 char *p;
387 char c;
388
389 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
390 if (!strchr(esc, c)) {
391 *p++ = c;
392 continue;
393 }
394 if (p + 3 < end) {
395 *p++ = '\\';
396 *p++ = '0' + ((c & 0300) >> 6);
397 *p++ = '0' + ((c & 070) >> 3);
398 *p++ = '0' + (c & 07);
399 continue;
400 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700401 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -1;
403 }
404 m->count = p - m->buf;
405 return 0;
406}
407EXPORT_SYMBOL(seq_escape);
408
Steven Whitehousea4808142012-06-11 13:16:35 +0100409int seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int len;
412
413 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (m->count + len < m->size) {
416 m->count += len;
417 return 0;
418 }
419 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700420 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -1;
422}
Steven Whitehousea4808142012-06-11 13:16:35 +0100423EXPORT_SYMBOL(seq_vprintf);
424
425int seq_printf(struct seq_file *m, const char *f, ...)
426{
427 int ret;
428 va_list args;
429
430 va_start(args, f);
431 ret = seq_vprintf(m, f, args);
432 va_end(args);
433
434 return ret;
435}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436EXPORT_SYMBOL(seq_printf);
437
Török Edwin74e2f332008-11-22 13:28:48 +0200438/**
Török Edwin958086d2008-11-23 23:24:53 +0200439 * mangle_path - mangle and copy path to buffer beginning
440 * @s: buffer start
441 * @p: beginning of path in above buffer
442 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200443 *
444 * Copy the path from @p to @s, replacing each occurrence of character from
445 * @esc with usual octal escape.
446 * Returns pointer past last written character in @s, or NULL in case of
447 * failure.
448 */
Al Viro8c9379e2011-12-08 20:18:57 -0500449char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100450{
451 while (s <= p) {
452 char c = *p++;
453 if (!c) {
454 return s;
455 } else if (!strchr(esc, c)) {
456 *s++ = c;
457 } else if (s + 4 > p) {
458 break;
459 } else {
460 *s++ = '\\';
461 *s++ = '0' + ((c & 0300) >> 6);
462 *s++ = '0' + ((c & 070) >> 3);
463 *s++ = '0' + (c & 07);
464 }
465 }
466 return NULL;
467}
Ingo Molnar604094f2008-11-28 18:03:22 +0100468EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100469
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800470/**
471 * seq_path - seq_file interface to print a pathname
472 * @m: the seq_file handle
473 * @path: the struct path to print
474 * @esc: set of characters to escape in the output
475 *
476 * return the absolute path of 'path', as represented by the
477 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100478 */
Al Viro8c9379e2011-12-08 20:18:57 -0500479int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Miklos Szeredif8439802009-09-21 14:48:36 +0200481 char *buf;
482 size_t size = seq_get_buf(m, &buf);
483 int res = -1;
484
485 if (size) {
486 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200488 char *end = mangle_path(buf, p, esc);
489 if (end)
490 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200493 seq_commit(m, res);
494
495 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497EXPORT_SYMBOL(seq_path);
498
Ram Pai6092d042008-03-27 13:06:20 +0100499/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100500 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100501 */
Al Viro8c9379e2011-12-08 20:18:57 -0500502int seq_path_root(struct seq_file *m, const struct path *path,
503 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100504{
Miklos Szeredif8439802009-09-21 14:48:36 +0200505 char *buf;
506 size_t size = seq_get_buf(m, &buf);
507 int res = -ENAMETOOLONG;
508
509 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100510 char *p;
511
Miklos Szeredif8439802009-09-21 14:48:36 +0200512 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500513 if (!p)
514 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200515 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100516 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200517 char *end = mangle_path(buf, p, esc);
518 if (end)
519 res = end - buf;
520 else
521 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100522 }
523 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200524 seq_commit(m, res);
525
Al Viro02125a82011-12-05 08:43:34 -0500526 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100527}
528
529/*
Ram Pai6092d042008-03-27 13:06:20 +0100530 * returns the path of the 'dentry' from the root of its filesystem.
531 */
Al Viro8c9379e2011-12-08 20:18:57 -0500532int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100533{
Miklos Szeredif8439802009-09-21 14:48:36 +0200534 char *buf;
535 size_t size = seq_get_buf(m, &buf);
536 int res = -1;
537
538 if (size) {
539 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100540 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200541 char *end = mangle_path(buf, p, esc);
542 if (end)
543 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100544 }
545 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200546 seq_commit(m, res);
547
548 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100549}
550
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030551int seq_bitmap(struct seq_file *m, const unsigned long *bits,
552 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700553{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700554 if (m->count < m->size) {
555 int len = bitmap_scnprintf(m->buf + m->count,
556 m->size - m->count, bits, nr_bits);
557 if (m->count + len < m->size) {
558 m->count += len;
559 return 0;
560 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700561 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700562 seq_set_overflow(m);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700563 return -1;
564}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700565EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700566
Rusty Russellaf76aba2009-03-30 22:05:11 -0600567int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700568 unsigned int nr_bits)
569{
570 if (m->count < m->size) {
571 int len = bitmap_scnlistprintf(m->buf + m->count,
572 m->size - m->count, bits, nr_bits);
573 if (m->count + len < m->size) {
574 m->count += len;
575 return 0;
576 }
577 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700578 seq_set_overflow(m);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700579 return -1;
580}
581EXPORT_SYMBOL(seq_bitmap_list);
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static void *single_start(struct seq_file *p, loff_t *pos)
584{
585 return NULL + (*pos == 0);
586}
587
588static void *single_next(struct seq_file *p, void *v, loff_t *pos)
589{
590 ++*pos;
591 return NULL;
592}
593
594static void single_stop(struct seq_file *p, void *v)
595{
596}
597
598int single_open(struct file *file, int (*show)(struct seq_file *, void *),
599 void *data)
600{
601 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
602 int res = -ENOMEM;
603
604 if (op) {
605 op->start = single_start;
606 op->next = single_next;
607 op->stop = single_stop;
608 op->show = show;
609 res = seq_open(file, op);
610 if (!res)
611 ((struct seq_file *)file->private_data)->private = data;
612 else
613 kfree(op);
614 }
615 return res;
616}
617EXPORT_SYMBOL(single_open);
618
Al Viro2043f492013-03-31 13:43:23 -0400619int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
620 void *data, size_t size)
621{
Heiko Carstens058504e2014-07-02 15:22:37 -0700622 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400623 int ret;
624 if (!buf)
625 return -ENOMEM;
626 ret = single_open(file, show, data);
627 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700628 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400629 return ret;
630 }
631 ((struct seq_file *)file->private_data)->buf = buf;
632 ((struct seq_file *)file->private_data)->size = size;
633 return 0;
634}
635EXPORT_SYMBOL(single_open_size);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637int single_release(struct inode *inode, struct file *file)
638{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800639 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int res = seq_release(inode, file);
641 kfree(op);
642 return res;
643}
644EXPORT_SYMBOL(single_release);
645
646int seq_release_private(struct inode *inode, struct file *file)
647{
648 struct seq_file *seq = file->private_data;
649
650 kfree(seq->private);
651 seq->private = NULL;
652 return seq_release(inode, file);
653}
654EXPORT_SYMBOL(seq_release_private);
655
Pavel Emelyanov39699032007-10-10 02:28:42 -0700656void *__seq_open_private(struct file *f, const struct seq_operations *ops,
657 int psize)
658{
659 int rc;
660 void *private;
661 struct seq_file *seq;
662
663 private = kzalloc(psize, GFP_KERNEL);
664 if (private == NULL)
665 goto out;
666
667 rc = seq_open(f, ops);
668 if (rc < 0)
669 goto out_free;
670
671 seq = f->private_data;
672 seq->private = private;
673 return private;
674
675out_free:
676 kfree(private);
677out:
678 return NULL;
679}
680EXPORT_SYMBOL(__seq_open_private);
681
682int seq_open_private(struct file *filp, const struct seq_operations *ops,
683 int psize)
684{
685 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
686}
687EXPORT_SYMBOL(seq_open_private);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689int seq_putc(struct seq_file *m, char c)
690{
691 if (m->count < m->size) {
692 m->buf[m->count++] = c;
693 return 0;
694 }
695 return -1;
696}
697EXPORT_SYMBOL(seq_putc);
698
699int seq_puts(struct seq_file *m, const char *s)
700{
701 int len = strlen(s);
702 if (m->count + len < m->size) {
703 memcpy(m->buf + m->count, s, len);
704 m->count += len;
705 return 0;
706 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700707 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -1;
709}
710EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700711
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700712/*
713 * A helper routine for putting decimal numbers without rich format of printf().
714 * only 'unsigned long long' is supported.
715 * This routine will put one byte delimiter + number into seq_file.
716 * This routine is very quick when you show lots of numbers.
717 * In usual cases, it will be better to use seq_printf(). It's easier to read.
718 */
719int seq_put_decimal_ull(struct seq_file *m, char delimiter,
720 unsigned long long num)
721{
722 int len;
723
724 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
725 goto overflow;
726
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700727 if (delimiter)
728 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700729
730 if (num < 10) {
731 m->buf[m->count++] = num + '0';
732 return 0;
733 }
734
735 len = num_to_str(m->buf + m->count, m->size - m->count, num);
736 if (!len)
737 goto overflow;
738 m->count += len;
739 return 0;
740overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700741 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700742 return -1;
743}
744EXPORT_SYMBOL(seq_put_decimal_ull);
745
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700746int seq_put_decimal_ll(struct seq_file *m, char delimiter,
747 long long num)
748{
749 if (num < 0) {
750 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700751 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700752 return -1;
753 }
754 if (delimiter)
755 m->buf[m->count++] = delimiter;
756 num = -num;
757 delimiter = '-';
758 }
759 return seq_put_decimal_ull(m, delimiter, num);
760
761}
762EXPORT_SYMBOL(seq_put_decimal_ll);
763
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700764/**
765 * seq_write - write arbitrary data to buffer
766 * @seq: seq_file identifying the buffer to which data should be written
767 * @data: data address
768 * @len: number of bytes
769 *
770 * Return 0 on success, non-zero otherwise.
771 */
772int seq_write(struct seq_file *seq, const void *data, size_t len)
773{
774 if (seq->count + len < seq->size) {
775 memcpy(seq->buf + seq->count, data, len);
776 seq->count += len;
777 return 0;
778 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700779 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700780 return -1;
781}
782EXPORT_SYMBOL(seq_write);
783
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800784/**
785 * seq_pad - write padding spaces to buffer
786 * @m: seq_file identifying the buffer to which data should be written
787 * @c: the byte to append after padding if non-zero
788 */
789void seq_pad(struct seq_file *m, char c)
790{
791 int size = m->pad_until - m->count;
792 if (size > 0)
793 seq_printf(m, "%*s", size, "");
794 if (c)
795 seq_putc(m, c);
796}
797EXPORT_SYMBOL(seq_pad);
798
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700799struct list_head *seq_list_start(struct list_head *head, loff_t pos)
800{
801 struct list_head *lh;
802
803 list_for_each(lh, head)
804 if (pos-- == 0)
805 return lh;
806
807 return NULL;
808}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700809EXPORT_SYMBOL(seq_list_start);
810
811struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
812{
813 if (!pos)
814 return head;
815
816 return seq_list_start(head, pos - 1);
817}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700818EXPORT_SYMBOL(seq_list_start_head);
819
820struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
821{
822 struct list_head *lh;
823
824 lh = ((struct list_head *)v)->next;
825 ++*ppos;
826 return lh == head ? NULL : lh;
827}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700828EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000829
830/**
831 * seq_hlist_start - start an iteration of a hlist
832 * @head: the head of the hlist
833 * @pos: the start position of the sequence
834 *
835 * Called at seq_file->op->start().
836 */
837struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
838{
839 struct hlist_node *node;
840
841 hlist_for_each(node, head)
842 if (pos-- == 0)
843 return node;
844 return NULL;
845}
846EXPORT_SYMBOL(seq_hlist_start);
847
848/**
849 * seq_hlist_start_head - start an iteration of a hlist
850 * @head: the head of the hlist
851 * @pos: the start position of the sequence
852 *
853 * Called at seq_file->op->start(). Call this function if you want to
854 * print a header at the top of the output.
855 */
856struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
857{
858 if (!pos)
859 return SEQ_START_TOKEN;
860
861 return seq_hlist_start(head, pos - 1);
862}
863EXPORT_SYMBOL(seq_hlist_start_head);
864
865/**
866 * seq_hlist_next - move to the next position of the hlist
867 * @v: the current iterator
868 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800869 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000870 *
871 * Called at seq_file->op->next().
872 */
873struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
874 loff_t *ppos)
875{
876 struct hlist_node *node = v;
877
878 ++*ppos;
879 if (v == SEQ_START_TOKEN)
880 return head->first;
881 else
882 return node->next;
883}
884EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000885
886/**
887 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
888 * @head: the head of the hlist
889 * @pos: the start position of the sequence
890 *
891 * Called at seq_file->op->start().
892 *
893 * This list-traversal primitive may safely run concurrently with
894 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
895 * as long as the traversal is guarded by rcu_read_lock().
896 */
897struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
898 loff_t pos)
899{
900 struct hlist_node *node;
901
902 __hlist_for_each_rcu(node, head)
903 if (pos-- == 0)
904 return node;
905 return NULL;
906}
907EXPORT_SYMBOL(seq_hlist_start_rcu);
908
909/**
910 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
911 * @head: the head of the hlist
912 * @pos: the start position of the sequence
913 *
914 * Called at seq_file->op->start(). Call this function if you want to
915 * print a header at the top of the output.
916 *
917 * This list-traversal primitive may safely run concurrently with
918 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
919 * as long as the traversal is guarded by rcu_read_lock().
920 */
921struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
922 loff_t pos)
923{
924 if (!pos)
925 return SEQ_START_TOKEN;
926
927 return seq_hlist_start_rcu(head, pos - 1);
928}
929EXPORT_SYMBOL(seq_hlist_start_head_rcu);
930
931/**
932 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
933 * @v: the current iterator
934 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800935 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000936 *
937 * Called at seq_file->op->next().
938 *
939 * This list-traversal primitive may safely run concurrently with
940 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
941 * as long as the traversal is guarded by rcu_read_lock().
942 */
943struct hlist_node *seq_hlist_next_rcu(void *v,
944 struct hlist_head *head,
945 loff_t *ppos)
946{
947 struct hlist_node *node = v;
948
949 ++*ppos;
950 if (v == SEQ_START_TOKEN)
951 return rcu_dereference(head->first);
952 else
953 return rcu_dereference(node->next);
954}
955EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400956
957/**
958 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
959 * @head: pointer to percpu array of struct hlist_heads
960 * @cpu: pointer to cpu "cursor"
961 * @pos: start position of sequence
962 *
963 * Called at seq_file->op->start().
964 */
965struct hlist_node *
966seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
967{
968 struct hlist_node *node;
969
970 for_each_possible_cpu(*cpu) {
971 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
972 if (pos-- == 0)
973 return node;
974 }
975 }
976 return NULL;
977}
978EXPORT_SYMBOL(seq_hlist_start_percpu);
979
980/**
981 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
982 * @v: pointer to current hlist_node
983 * @head: pointer to percpu array of struct hlist_heads
984 * @cpu: pointer to cpu "cursor"
985 * @pos: start position of sequence
986 *
987 * Called at seq_file->op->next().
988 */
989struct hlist_node *
990seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
991 int *cpu, loff_t *pos)
992{
993 struct hlist_node *node = v;
994
995 ++*pos;
996
997 if (node->next)
998 return node->next;
999
1000 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1001 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1002 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1003
1004 if (!hlist_empty(bucket))
1005 return bucket->first;
1006 }
1007 return NULL;
1008}
1009EXPORT_SYMBOL(seq_hlist_next_percpu);