blob: 263b125dbcf4d1e68787032bb0827074fcb075d5 [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>
Andy Shevchenko37607102015-09-09 15:38:33 -070015#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <asm/uaccess.h>
18#include <asm/page.h>
19
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070020static void seq_set_overflow(struct seq_file *m)
21{
22 m->count = m->size;
23}
24
Heiko Carstens058504e2014-07-02 15:22:37 -070025static void *seq_buf_alloc(unsigned long size)
26{
27 void *buf;
28
David Rientjes5cec38a2014-12-12 16:56:16 -080029 /*
30 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
31 * it's better to fall back to vmalloc() than to kill things.
32 */
33 buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
Heiko Carstens058504e2014-07-02 15:22:37 -070034 if (!buf && size > PAGE_SIZE)
35 buf = vmalloc(size);
36 return buf;
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/**
40 * seq_open - initialize sequential file
41 * @file: file we initialize
42 * @op: method table describing the sequence
43 *
44 * seq_open() sets @file, associating it with a sequence described
45 * by @op. @op->start() sets the iterator up and returns the first
46 * element of sequence. @op->stop() shuts it down. @op->next()
47 * returns the next element of sequence. @op->show() prints element
48 * into the buffer. In case of error ->start() and ->next() return
49 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
50 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040051 * Returning SEQ_SKIP means "discard this element and move on".
Yann Droneaud460b8652015-06-30 14:57:36 -070052 * Note: seq_open() will allocate a struct seq_file and store its
53 * pointer in @file->private_data. This pointer should not be modified.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080055int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Yann Droneaud189f9842015-06-30 14:57:33 -070057 struct seq_file *p;
Al Viro1abe77b2005-11-07 17:15:34 -050058
Yann Droneaud189f9842015-06-30 14:57:33 -070059 WARN_ON(file->private_data);
60
61 p = kzalloc(sizeof(*p), GFP_KERNEL);
62 if (!p)
63 return -ENOMEM;
64
65 file->private_data = p;
66
Ingo Molnar0ac17592006-03-23 03:00:37 -080067 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 p->op = op;
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060069#ifdef CONFIG_USER_NS
70 p->user_ns = file->f_cred->user_ns;
71#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /*
74 * Wrappers around seq_open(e.g. swaps_open) need to be
75 * aware of this. If they set f_version themselves, they
76 * should call seq_open first and then set f_version.
77 */
78 file->f_version = 0;
79
Eric Biederman8f19d472009-02-18 14:48:16 -080080 /*
81 * seq_files support lseek() and pread(). They do not implement
82 * write() at all, but we clear FMODE_PWRITE here for historical
83 * reasons.
84 *
85 * If a client of seq_files a) implements file.write() and b) wishes to
86 * support pwrite() then that client will need to implement its own
87 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
88 */
89 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91}
92EXPORT_SYMBOL(seq_open);
93
Eric Biederman33da8892009-02-04 15:12:25 -080094static int traverse(struct seq_file *m, loff_t offset)
95{
96 loff_t pos = 0, index;
97 int error = 0;
98 void *p;
99
100 m->version = 0;
101 index = 0;
102 m->count = m->from = 0;
103 if (!offset) {
104 m->index = index;
105 return 0;
106 }
107 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700108 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800109 if (!m->buf)
110 return -ENOMEM;
111 }
112 p = m->op->start(m, &index);
113 while (p) {
114 error = PTR_ERR(p);
115 if (IS_ERR(p))
116 break;
117 error = m->op->show(m, p);
118 if (error < 0)
119 break;
120 if (unlikely(error)) {
121 error = 0;
122 m->count = 0;
123 }
Joe Perches1f33c412014-09-29 16:08:21 -0700124 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800125 goto Eoverflow;
126 if (pos + m->count > offset) {
127 m->from = offset - pos;
128 m->count -= m->from;
129 m->index = index;
130 break;
131 }
132 pos += m->count;
133 m->count = 0;
134 if (pos == offset) {
135 index++;
136 m->index = index;
137 break;
138 }
139 p = m->op->next(m, p, &index);
140 }
141 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300142 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800143 return error;
144
145Eoverflow:
146 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700147 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000148 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700149 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800150 return !m->buf ? -ENOMEM : -EAGAIN;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/**
154 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700155 * @file: the file to read from
156 * @buf: the buffer to read to
157 * @size: the maximum number of bytes to read
158 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * Ready-made ->f_op->read()
161 */
162ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
163{
Joe Perches8209e2f2010-09-04 18:52:49 -0700164 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 size_t copied = 0;
166 loff_t pos;
167 size_t n;
168 void *p;
169 int err = 0;
170
Ingo Molnar0ac17592006-03-23 03:00:37 -0800171 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /*
174 * seq_file->op->..m_start/m_stop/m_next may do special actions
175 * or optimisations based on the file->f_version, so we want to
176 * pass the file->f_version to those methods.
177 *
178 * seq_file->version is just copy of f_version, and seq_file
179 * methods can treat it simply as file version.
180 * It is copied in first and copied out after all operations.
181 * It is convenient to have it as part of structure to avoid the
182 * need of passing another argument to all the seq_file methods.
183 */
184 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700185
186 /* Don't assume *ppos is where we left it */
187 if (unlikely(*ppos != m->read_pos)) {
188 while ((err = traverse(m, *ppos)) == -EAGAIN)
189 ;
190 if (err) {
191 /* With prejudice... */
192 m->read_pos = 0;
193 m->version = 0;
194 m->index = 0;
195 m->count = 0;
196 goto Done;
197 } else {
198 m->read_pos = *ppos;
199 }
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* grab buffer if we didn't have one */
203 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700204 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!m->buf)
206 goto Enomem;
207 }
208 /* if not empty - flush it first */
209 if (m->count) {
210 n = min(m->count, size);
211 err = copy_to_user(buf, m->buf + m->from, n);
212 if (err)
213 goto Efault;
214 m->count -= n;
215 m->from += n;
216 size -= n;
217 buf += n;
218 copied += n;
219 if (!m->count)
220 m->index++;
221 if (!size)
222 goto Done;
223 }
224 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400225 pos = m->index;
226 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 err = PTR_ERR(p);
229 if (!p || IS_ERR(p))
230 break;
231 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400232 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
Al Viro521b5d02008-03-28 00:46:41 -0400234 if (unlikely(err))
235 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400236 if (unlikely(!m->count)) {
237 p = m->op->next(m, p, &pos);
238 m->index = pos;
239 continue;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (m->count < m->size)
242 goto Fill;
243 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700244 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000245 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700246 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!m->buf)
248 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400250 pos = m->index;
251 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 m->op->stop(m, p);
254 m->count = 0;
255 goto Done;
256Fill:
257 /* they want more? let's try to get some more */
258 while (m->count < size) {
259 size_t offs = m->count;
260 loff_t next = pos;
261 p = m->op->next(m, p, &next);
262 if (!p || IS_ERR(p)) {
263 err = PTR_ERR(p);
264 break;
265 }
266 err = m->op->show(m, p);
Joe Perches1f33c412014-09-29 16:08:21 -0700267 if (seq_has_overflowed(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400269 if (likely(err <= 0))
270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 pos = next;
273 }
274 m->op->stop(m, p);
275 n = min(m->count, size);
276 err = copy_to_user(buf, m->buf, n);
277 if (err)
278 goto Efault;
279 copied += n;
280 m->count -= n;
281 if (m->count)
282 m->from = n;
283 else
284 pos++;
285 m->index = pos;
286Done:
287 if (!copied)
288 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800289 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800291 m->read_pos += copied;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800294 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return copied;
296Enomem:
297 err = -ENOMEM;
298 goto Done;
299Efault:
300 err = -EFAULT;
301 goto Done;
302}
303EXPORT_SYMBOL(seq_read);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/**
306 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700307 * @file: the file in question
308 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800309 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *
311 * Ready-made ->f_op->llseek()
312 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800313loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Joe Perches8209e2f2010-09-04 18:52:49 -0700315 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200316 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Ingo Molnar0ac17592006-03-23 03:00:37 -0800318 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 m->version = file->f_version;
Andrew Morton965c8e52012-12-17 15:59:39 -0800320 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800321 case SEEK_CUR:
322 offset += file->f_pos;
323 case SEEK_SET:
324 if (offset < 0)
325 break;
326 retval = offset;
327 if (offset != m->read_pos) {
328 while ((retval = traverse(m, offset)) == -EAGAIN)
329 ;
330 if (retval) {
331 /* with extreme prejudice... */
332 file->f_pos = 0;
333 m->read_pos = 0;
334 m->version = 0;
335 m->index = 0;
336 m->count = 0;
337 } else {
338 m->read_pos = offset;
339 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Gu Zheng05e16742013-10-25 18:15:06 +0800341 } else {
342 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700346 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return retval;
348}
349EXPORT_SYMBOL(seq_lseek);
350
351/**
352 * seq_release - free the structures associated with sequential file.
353 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500354 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
356 * Frees the structures associated with sequential file; can be used
357 * as ->f_op->release() if you don't have private data to destroy.
358 */
359int seq_release(struct inode *inode, struct file *file)
360{
Joe Perches8209e2f2010-09-04 18:52:49 -0700361 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700362 kvfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 kfree(m);
364 return 0;
365}
366EXPORT_SYMBOL(seq_release);
367
368/**
369 * seq_escape - print string into buffer, escaping some characters
370 * @m: target buffer
371 * @s: string
372 * @esc: set of characters that need escaping
373 *
374 * Puts string into buffer, replacing each occurrence of character from
375 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
376 * case of overflow.
377 */
378int seq_escape(struct seq_file *m, const char *s, const char *esc)
379{
380 char *end = m->buf + m->size;
381 char *p;
382 char c;
383
384 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
385 if (!strchr(esc, c)) {
386 *p++ = c;
387 continue;
388 }
389 if (p + 3 < end) {
390 *p++ = '\\';
391 *p++ = '0' + ((c & 0300) >> 6);
392 *p++ = '0' + ((c & 070) >> 3);
393 *p++ = '0' + (c & 07);
394 continue;
395 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700396 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -1;
398 }
399 m->count = p - m->buf;
400 return 0;
401}
402EXPORT_SYMBOL(seq_escape);
403
Steven Whitehousea4808142012-06-11 13:16:35 +0100404int seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int len;
407
408 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (m->count + len < m->size) {
411 m->count += len;
412 return 0;
413 }
414 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700415 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -1;
417}
Steven Whitehousea4808142012-06-11 13:16:35 +0100418EXPORT_SYMBOL(seq_vprintf);
419
420int seq_printf(struct seq_file *m, const char *f, ...)
421{
422 int ret;
423 va_list args;
424
425 va_start(args, f);
426 ret = seq_vprintf(m, f, args);
427 va_end(args);
428
429 return ret;
430}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431EXPORT_SYMBOL(seq_printf);
432
Török Edwin74e2f332008-11-22 13:28:48 +0200433/**
Török Edwin958086d2008-11-23 23:24:53 +0200434 * mangle_path - mangle and copy path to buffer beginning
435 * @s: buffer start
436 * @p: beginning of path in above buffer
437 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200438 *
439 * Copy the path from @p to @s, replacing each occurrence of character from
440 * @esc with usual octal escape.
441 * Returns pointer past last written character in @s, or NULL in case of
442 * failure.
443 */
Al Viro8c9379e2011-12-08 20:18:57 -0500444char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100445{
446 while (s <= p) {
447 char c = *p++;
448 if (!c) {
449 return s;
450 } else if (!strchr(esc, c)) {
451 *s++ = c;
452 } else if (s + 4 > p) {
453 break;
454 } else {
455 *s++ = '\\';
456 *s++ = '0' + ((c & 0300) >> 6);
457 *s++ = '0' + ((c & 070) >> 3);
458 *s++ = '0' + (c & 07);
459 }
460 }
461 return NULL;
462}
Ingo Molnar604094f2008-11-28 18:03:22 +0100463EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100464
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800465/**
466 * seq_path - seq_file interface to print a pathname
467 * @m: the seq_file handle
468 * @path: the struct path to print
469 * @esc: set of characters to escape in the output
470 *
471 * return the absolute path of 'path', as represented by the
472 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100473 */
Al Viro8c9379e2011-12-08 20:18:57 -0500474int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Miklos Szeredif8439802009-09-21 14:48:36 +0200476 char *buf;
477 size_t size = seq_get_buf(m, &buf);
478 int res = -1;
479
480 if (size) {
481 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200483 char *end = mangle_path(buf, p, esc);
484 if (end)
485 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200488 seq_commit(m, res);
489
490 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492EXPORT_SYMBOL(seq_path);
493
Miklos Szeredi2726d562015-06-19 10:30:28 +0200494/**
495 * seq_file_path - seq_file interface to print a pathname of a file
496 * @m: the seq_file handle
497 * @file: the struct file to print
498 * @esc: set of characters to escape in the output
499 *
500 * return the absolute path to the file.
501 */
502int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
503{
504 return seq_path(m, &file->f_path, esc);
505}
506EXPORT_SYMBOL(seq_file_path);
507
Ram Pai6092d042008-03-27 13:06:20 +0100508/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100509 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100510 */
Al Viro8c9379e2011-12-08 20:18:57 -0500511int seq_path_root(struct seq_file *m, const struct path *path,
512 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100513{
Miklos Szeredif8439802009-09-21 14:48:36 +0200514 char *buf;
515 size_t size = seq_get_buf(m, &buf);
516 int res = -ENAMETOOLONG;
517
518 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100519 char *p;
520
Miklos Szeredif8439802009-09-21 14:48:36 +0200521 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500522 if (!p)
523 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200524 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100525 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200526 char *end = mangle_path(buf, p, esc);
527 if (end)
528 res = end - buf;
529 else
530 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100531 }
532 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200533 seq_commit(m, res);
534
Al Viro02125a82011-12-05 08:43:34 -0500535 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100536}
537
538/*
Ram Pai6092d042008-03-27 13:06:20 +0100539 * returns the path of the 'dentry' from the root of its filesystem.
540 */
Al Viro8c9379e2011-12-08 20:18:57 -0500541int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100542{
Miklos Szeredif8439802009-09-21 14:48:36 +0200543 char *buf;
544 size_t size = seq_get_buf(m, &buf);
545 int res = -1;
546
547 if (size) {
548 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100549 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200550 char *end = mangle_path(buf, p, esc);
551 if (end)
552 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100553 }
554 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200555 seq_commit(m, res);
556
557 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100558}
Omar Sandovalc8d3fe02015-05-18 02:16:31 -0700559EXPORT_SYMBOL(seq_dentry);
Ram Pai6092d042008-03-27 13:06:20 +0100560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static void *single_start(struct seq_file *p, loff_t *pos)
562{
563 return NULL + (*pos == 0);
564}
565
566static void *single_next(struct seq_file *p, void *v, loff_t *pos)
567{
568 ++*pos;
569 return NULL;
570}
571
572static void single_stop(struct seq_file *p, void *v)
573{
574}
575
576int single_open(struct file *file, int (*show)(struct seq_file *, void *),
577 void *data)
578{
579 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
580 int res = -ENOMEM;
581
582 if (op) {
583 op->start = single_start;
584 op->next = single_next;
585 op->stop = single_stop;
586 op->show = show;
587 res = seq_open(file, op);
588 if (!res)
589 ((struct seq_file *)file->private_data)->private = data;
590 else
591 kfree(op);
592 }
593 return res;
594}
595EXPORT_SYMBOL(single_open);
596
Al Viro2043f492013-03-31 13:43:23 -0400597int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
598 void *data, size_t size)
599{
Heiko Carstens058504e2014-07-02 15:22:37 -0700600 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400601 int ret;
602 if (!buf)
603 return -ENOMEM;
604 ret = single_open(file, show, data);
605 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700606 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400607 return ret;
608 }
609 ((struct seq_file *)file->private_data)->buf = buf;
610 ((struct seq_file *)file->private_data)->size = size;
611 return 0;
612}
613EXPORT_SYMBOL(single_open_size);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615int single_release(struct inode *inode, struct file *file)
616{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800617 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int res = seq_release(inode, file);
619 kfree(op);
620 return res;
621}
622EXPORT_SYMBOL(single_release);
623
624int seq_release_private(struct inode *inode, struct file *file)
625{
626 struct seq_file *seq = file->private_data;
627
628 kfree(seq->private);
629 seq->private = NULL;
630 return seq_release(inode, file);
631}
632EXPORT_SYMBOL(seq_release_private);
633
Pavel Emelyanov39699032007-10-10 02:28:42 -0700634void *__seq_open_private(struct file *f, const struct seq_operations *ops,
635 int psize)
636{
637 int rc;
638 void *private;
639 struct seq_file *seq;
640
641 private = kzalloc(psize, GFP_KERNEL);
642 if (private == NULL)
643 goto out;
644
645 rc = seq_open(f, ops);
646 if (rc < 0)
647 goto out_free;
648
649 seq = f->private_data;
650 seq->private = private;
651 return private;
652
653out_free:
654 kfree(private);
655out:
656 return NULL;
657}
658EXPORT_SYMBOL(__seq_open_private);
659
660int seq_open_private(struct file *filp, const struct seq_operations *ops,
661 int psize)
662{
663 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
664}
665EXPORT_SYMBOL(seq_open_private);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667int seq_putc(struct seq_file *m, char c)
668{
669 if (m->count < m->size) {
670 m->buf[m->count++] = c;
671 return 0;
672 }
673 return -1;
674}
675EXPORT_SYMBOL(seq_putc);
676
677int seq_puts(struct seq_file *m, const char *s)
678{
679 int len = strlen(s);
680 if (m->count + len < m->size) {
681 memcpy(m->buf + m->count, s, len);
682 m->count += len;
683 return 0;
684 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700685 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return -1;
687}
688EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700689
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700690/*
691 * A helper routine for putting decimal numbers without rich format of printf().
692 * only 'unsigned long long' is supported.
693 * This routine will put one byte delimiter + number into seq_file.
694 * This routine is very quick when you show lots of numbers.
695 * In usual cases, it will be better to use seq_printf(). It's easier to read.
696 */
697int seq_put_decimal_ull(struct seq_file *m, char delimiter,
698 unsigned long long num)
699{
700 int len;
701
702 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
703 goto overflow;
704
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700705 if (delimiter)
706 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700707
708 if (num < 10) {
709 m->buf[m->count++] = num + '0';
710 return 0;
711 }
712
713 len = num_to_str(m->buf + m->count, m->size - m->count, num);
714 if (!len)
715 goto overflow;
716 m->count += len;
717 return 0;
718overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700719 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700720 return -1;
721}
722EXPORT_SYMBOL(seq_put_decimal_ull);
723
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700724int seq_put_decimal_ll(struct seq_file *m, char delimiter,
725 long long num)
726{
727 if (num < 0) {
728 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700729 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700730 return -1;
731 }
732 if (delimiter)
733 m->buf[m->count++] = delimiter;
734 num = -num;
735 delimiter = '-';
736 }
737 return seq_put_decimal_ull(m, delimiter, num);
738
739}
740EXPORT_SYMBOL(seq_put_decimal_ll);
741
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700742/**
743 * seq_write - write arbitrary data to buffer
744 * @seq: seq_file identifying the buffer to which data should be written
745 * @data: data address
746 * @len: number of bytes
747 *
748 * Return 0 on success, non-zero otherwise.
749 */
750int seq_write(struct seq_file *seq, const void *data, size_t len)
751{
752 if (seq->count + len < seq->size) {
753 memcpy(seq->buf + seq->count, data, len);
754 seq->count += len;
755 return 0;
756 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700757 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700758 return -1;
759}
760EXPORT_SYMBOL(seq_write);
761
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800762/**
763 * seq_pad - write padding spaces to buffer
764 * @m: seq_file identifying the buffer to which data should be written
765 * @c: the byte to append after padding if non-zero
766 */
767void seq_pad(struct seq_file *m, char c)
768{
769 int size = m->pad_until - m->count;
770 if (size > 0)
771 seq_printf(m, "%*s", size, "");
772 if (c)
773 seq_putc(m, c);
774}
775EXPORT_SYMBOL(seq_pad);
776
Andy Shevchenko37607102015-09-09 15:38:33 -0700777/* A complete analogue of print_hex_dump() */
778void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
779 int rowsize, int groupsize, const void *buf, size_t len,
780 bool ascii)
781{
782 const u8 *ptr = buf;
783 int i, linelen, remaining = len;
784 int ret;
785
786 if (rowsize != 16 && rowsize != 32)
787 rowsize = 16;
788
789 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
790 linelen = min(remaining, rowsize);
791 remaining -= rowsize;
792
793 switch (prefix_type) {
794 case DUMP_PREFIX_ADDRESS:
795 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
796 break;
797 case DUMP_PREFIX_OFFSET:
798 seq_printf(m, "%s%.8x: ", prefix_str, i);
799 break;
800 default:
801 seq_printf(m, "%s", prefix_str);
802 break;
803 }
804
805 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
806 m->buf + m->count, m->size - m->count,
807 ascii);
808 if (ret >= m->size - m->count) {
809 seq_set_overflow(m);
810 } else {
811 m->count += ret;
812 seq_putc(m, '\n');
813 }
814 }
815}
816EXPORT_SYMBOL(seq_hex_dump);
817
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700818struct list_head *seq_list_start(struct list_head *head, loff_t pos)
819{
820 struct list_head *lh;
821
822 list_for_each(lh, head)
823 if (pos-- == 0)
824 return lh;
825
826 return NULL;
827}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700828EXPORT_SYMBOL(seq_list_start);
829
830struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
831{
832 if (!pos)
833 return head;
834
835 return seq_list_start(head, pos - 1);
836}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700837EXPORT_SYMBOL(seq_list_start_head);
838
839struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
840{
841 struct list_head *lh;
842
843 lh = ((struct list_head *)v)->next;
844 ++*ppos;
845 return lh == head ? NULL : lh;
846}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700847EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000848
849/**
850 * seq_hlist_start - start an iteration of a hlist
851 * @head: the head of the hlist
852 * @pos: the start position of the sequence
853 *
854 * Called at seq_file->op->start().
855 */
856struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
857{
858 struct hlist_node *node;
859
860 hlist_for_each(node, head)
861 if (pos-- == 0)
862 return node;
863 return NULL;
864}
865EXPORT_SYMBOL(seq_hlist_start);
866
867/**
868 * seq_hlist_start_head - start an iteration of a hlist
869 * @head: the head of the hlist
870 * @pos: the start position of the sequence
871 *
872 * Called at seq_file->op->start(). Call this function if you want to
873 * print a header at the top of the output.
874 */
875struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
876{
877 if (!pos)
878 return SEQ_START_TOKEN;
879
880 return seq_hlist_start(head, pos - 1);
881}
882EXPORT_SYMBOL(seq_hlist_start_head);
883
884/**
885 * seq_hlist_next - move to the next position of the hlist
886 * @v: the current iterator
887 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800888 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000889 *
890 * Called at seq_file->op->next().
891 */
892struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
893 loff_t *ppos)
894{
895 struct hlist_node *node = v;
896
897 ++*ppos;
898 if (v == SEQ_START_TOKEN)
899 return head->first;
900 else
901 return node->next;
902}
903EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000904
905/**
906 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
907 * @head: the head of the hlist
908 * @pos: the start position of the sequence
909 *
910 * Called at seq_file->op->start().
911 *
912 * This list-traversal primitive may safely run concurrently with
913 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
914 * as long as the traversal is guarded by rcu_read_lock().
915 */
916struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
917 loff_t pos)
918{
919 struct hlist_node *node;
920
921 __hlist_for_each_rcu(node, head)
922 if (pos-- == 0)
923 return node;
924 return NULL;
925}
926EXPORT_SYMBOL(seq_hlist_start_rcu);
927
928/**
929 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
930 * @head: the head of the hlist
931 * @pos: the start position of the sequence
932 *
933 * Called at seq_file->op->start(). Call this function if you want to
934 * print a header at the top of the output.
935 *
936 * This list-traversal primitive may safely run concurrently with
937 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
938 * as long as the traversal is guarded by rcu_read_lock().
939 */
940struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
941 loff_t pos)
942{
943 if (!pos)
944 return SEQ_START_TOKEN;
945
946 return seq_hlist_start_rcu(head, pos - 1);
947}
948EXPORT_SYMBOL(seq_hlist_start_head_rcu);
949
950/**
951 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
952 * @v: the current iterator
953 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800954 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000955 *
956 * Called at seq_file->op->next().
957 *
958 * This list-traversal primitive may safely run concurrently with
959 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
960 * as long as the traversal is guarded by rcu_read_lock().
961 */
962struct hlist_node *seq_hlist_next_rcu(void *v,
963 struct hlist_head *head,
964 loff_t *ppos)
965{
966 struct hlist_node *node = v;
967
968 ++*ppos;
969 if (v == SEQ_START_TOKEN)
970 return rcu_dereference(head->first);
971 else
972 return rcu_dereference(node->next);
973}
974EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400975
976/**
977 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
978 * @head: pointer to percpu array of struct hlist_heads
979 * @cpu: pointer to cpu "cursor"
980 * @pos: start position of sequence
981 *
982 * Called at seq_file->op->start().
983 */
984struct hlist_node *
985seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
986{
987 struct hlist_node *node;
988
989 for_each_possible_cpu(*cpu) {
990 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
991 if (pos-- == 0)
992 return node;
993 }
994 }
995 return NULL;
996}
997EXPORT_SYMBOL(seq_hlist_start_percpu);
998
999/**
1000 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1001 * @v: pointer to current hlist_node
1002 * @head: pointer to percpu array of struct hlist_heads
1003 * @cpu: pointer to cpu "cursor"
1004 * @pos: start position of sequence
1005 *
1006 * Called at seq_file->op->next().
1007 */
1008struct hlist_node *
1009seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1010 int *cpu, loff_t *pos)
1011{
1012 struct hlist_node *node = v;
1013
1014 ++*pos;
1015
1016 if (node->next)
1017 return node->next;
1018
1019 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1020 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1021 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1022
1023 if (!hlist_empty(bucket))
1024 return bucket->first;
1025 }
1026 return NULL;
1027}
1028EXPORT_SYMBOL(seq_hlist_next_percpu);