blob: dbf3a59c86bbbb081464830585f216623ab9f112 [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
David Rientjes5cec38a2014-12-12 16:56:16 -080028 /*
29 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
30 * it's better to fall back to vmalloc() than to kill things.
31 */
32 buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
Heiko Carstens058504e2014-07-02 15:22:37 -070033 if (!buf && size > PAGE_SIZE)
34 buf = vmalloc(size);
35 return buf;
36}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/**
39 * seq_open - initialize sequential file
40 * @file: file we initialize
41 * @op: method table describing the sequence
42 *
43 * seq_open() sets @file, associating it with a sequence described
44 * by @op. @op->start() sets the iterator up and returns the first
45 * element of sequence. @op->stop() shuts it down. @op->next()
46 * returns the next element of sequence. @op->show() prints element
47 * into the buffer. In case of error ->start() and ->next() return
48 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
49 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040050 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080052int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Al Viro1abe77b2005-11-07 17:15:34 -050054 struct seq_file *p = file->private_data;
55
56 if (!p) {
57 p = kmalloc(sizeof(*p), GFP_KERNEL);
58 if (!p)
59 return -ENOMEM;
60 file->private_data = p;
61 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080063 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 p->op = op;
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060065#ifdef CONFIG_USER_NS
66 p->user_ns = file->f_cred->user_ns;
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /*
70 * Wrappers around seq_open(e.g. swaps_open) need to be
71 * aware of this. If they set f_version themselves, they
72 * should call seq_open first and then set f_version.
73 */
74 file->f_version = 0;
75
Eric Biederman8f19d472009-02-18 14:48:16 -080076 /*
77 * seq_files support lseek() and pread(). They do not implement
78 * write() at all, but we clear FMODE_PWRITE here for historical
79 * reasons.
80 *
81 * If a client of seq_files a) implements file.write() and b) wishes to
82 * support pwrite() then that client will need to implement its own
83 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
84 */
85 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87}
88EXPORT_SYMBOL(seq_open);
89
Eric Biederman33da8892009-02-04 15:12:25 -080090static int traverse(struct seq_file *m, loff_t offset)
91{
92 loff_t pos = 0, index;
93 int error = 0;
94 void *p;
95
96 m->version = 0;
97 index = 0;
98 m->count = m->from = 0;
99 if (!offset) {
100 m->index = index;
101 return 0;
102 }
103 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700104 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800105 if (!m->buf)
106 return -ENOMEM;
107 }
108 p = m->op->start(m, &index);
109 while (p) {
110 error = PTR_ERR(p);
111 if (IS_ERR(p))
112 break;
113 error = m->op->show(m, p);
114 if (error < 0)
115 break;
116 if (unlikely(error)) {
117 error = 0;
118 m->count = 0;
119 }
Joe Perches1f33c412014-09-29 16:08:21 -0700120 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800121 goto Eoverflow;
122 if (pos + m->count > offset) {
123 m->from = offset - pos;
124 m->count -= m->from;
125 m->index = index;
126 break;
127 }
128 pos += m->count;
129 m->count = 0;
130 if (pos == offset) {
131 index++;
132 m->index = index;
133 break;
134 }
135 p = m->op->next(m, p, &index);
136 }
137 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300138 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800139 return error;
140
141Eoverflow:
142 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700143 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000144 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700145 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800146 return !m->buf ? -ENOMEM : -EAGAIN;
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/**
150 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700151 * @file: the file to read from
152 * @buf: the buffer to read to
153 * @size: the maximum number of bytes to read
154 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *
156 * Ready-made ->f_op->read()
157 */
158ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
159{
Joe Perches8209e2f2010-09-04 18:52:49 -0700160 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 size_t copied = 0;
162 loff_t pos;
163 size_t n;
164 void *p;
165 int err = 0;
166
Ingo Molnar0ac17592006-03-23 03:00:37 -0800167 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
170 * seq_file->op->..m_start/m_stop/m_next may do special actions
171 * or optimisations based on the file->f_version, so we want to
172 * pass the file->f_version to those methods.
173 *
174 * seq_file->version is just copy of f_version, and seq_file
175 * methods can treat it simply as file version.
176 * It is copied in first and copied out after all operations.
177 * It is convenient to have it as part of structure to avoid the
178 * need of passing another argument to all the seq_file methods.
179 */
180 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700181
182 /* Don't assume *ppos is where we left it */
183 if (unlikely(*ppos != m->read_pos)) {
184 while ((err = traverse(m, *ppos)) == -EAGAIN)
185 ;
186 if (err) {
187 /* With prejudice... */
188 m->read_pos = 0;
189 m->version = 0;
190 m->index = 0;
191 m->count = 0;
192 goto Done;
193 } else {
194 m->read_pos = *ppos;
195 }
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* grab buffer if we didn't have one */
199 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700200 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!m->buf)
202 goto Enomem;
203 }
204 /* if not empty - flush it first */
205 if (m->count) {
206 n = min(m->count, size);
207 err = copy_to_user(buf, m->buf + m->from, n);
208 if (err)
209 goto Efault;
210 m->count -= n;
211 m->from += n;
212 size -= n;
213 buf += n;
214 copied += n;
215 if (!m->count)
216 m->index++;
217 if (!size)
218 goto Done;
219 }
220 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400221 pos = m->index;
222 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 err = PTR_ERR(p);
225 if (!p || IS_ERR(p))
226 break;
227 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400228 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
Al Viro521b5d02008-03-28 00:46:41 -0400230 if (unlikely(err))
231 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400232 if (unlikely(!m->count)) {
233 p = m->op->next(m, p, &pos);
234 m->index = pos;
235 continue;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (m->count < m->size)
238 goto Fill;
239 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700240 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000241 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700242 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!m->buf)
244 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400246 pos = m->index;
247 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 m->op->stop(m, p);
250 m->count = 0;
251 goto Done;
252Fill:
253 /* they want more? let's try to get some more */
254 while (m->count < size) {
255 size_t offs = m->count;
256 loff_t next = pos;
257 p = m->op->next(m, p, &next);
258 if (!p || IS_ERR(p)) {
259 err = PTR_ERR(p);
260 break;
261 }
262 err = m->op->show(m, p);
Joe Perches1f33c412014-09-29 16:08:21 -0700263 if (seq_has_overflowed(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400265 if (likely(err <= 0))
266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 pos = next;
269 }
270 m->op->stop(m, p);
271 n = min(m->count, size);
272 err = copy_to_user(buf, m->buf, n);
273 if (err)
274 goto Efault;
275 copied += n;
276 m->count -= n;
277 if (m->count)
278 m->from = n;
279 else
280 pos++;
281 m->index = pos;
282Done:
283 if (!copied)
284 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800285 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800287 m->read_pos += copied;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800290 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return copied;
292Enomem:
293 err = -ENOMEM;
294 goto Done;
295Efault:
296 err = -EFAULT;
297 goto Done;
298}
299EXPORT_SYMBOL(seq_read);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/**
302 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700303 * @file: the file in question
304 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800305 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
307 * Ready-made ->f_op->llseek()
308 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800309loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Joe Perches8209e2f2010-09-04 18:52:49 -0700311 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200312 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Ingo Molnar0ac17592006-03-23 03:00:37 -0800314 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 m->version = file->f_version;
Andrew Morton965c8e52012-12-17 15:59:39 -0800316 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800317 case SEEK_CUR:
318 offset += file->f_pos;
319 case SEEK_SET:
320 if (offset < 0)
321 break;
322 retval = offset;
323 if (offset != m->read_pos) {
324 while ((retval = traverse(m, offset)) == -EAGAIN)
325 ;
326 if (retval) {
327 /* with extreme prejudice... */
328 file->f_pos = 0;
329 m->read_pos = 0;
330 m->version = 0;
331 m->index = 0;
332 m->count = 0;
333 } else {
334 m->read_pos = offset;
335 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Gu Zheng05e16742013-10-25 18:15:06 +0800337 } else {
338 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700342 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return retval;
344}
345EXPORT_SYMBOL(seq_lseek);
346
347/**
348 * seq_release - free the structures associated with sequential file.
349 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500350 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
352 * Frees the structures associated with sequential file; can be used
353 * as ->f_op->release() if you don't have private data to destroy.
354 */
355int seq_release(struct inode *inode, struct file *file)
356{
Joe Perches8209e2f2010-09-04 18:52:49 -0700357 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700358 kvfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 kfree(m);
360 return 0;
361}
362EXPORT_SYMBOL(seq_release);
363
364/**
365 * seq_escape - print string into buffer, escaping some characters
366 * @m: target buffer
367 * @s: string
368 * @esc: set of characters that need escaping
369 *
370 * Puts string into buffer, replacing each occurrence of character from
371 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
372 * case of overflow.
373 */
374int seq_escape(struct seq_file *m, const char *s, const char *esc)
375{
376 char *end = m->buf + m->size;
377 char *p;
378 char c;
379
380 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
381 if (!strchr(esc, c)) {
382 *p++ = c;
383 continue;
384 }
385 if (p + 3 < end) {
386 *p++ = '\\';
387 *p++ = '0' + ((c & 0300) >> 6);
388 *p++ = '0' + ((c & 070) >> 3);
389 *p++ = '0' + (c & 07);
390 continue;
391 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700392 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -1;
394 }
395 m->count = p - m->buf;
396 return 0;
397}
398EXPORT_SYMBOL(seq_escape);
399
Steven Whitehousea4808142012-06-11 13:16:35 +0100400int seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int len;
403
404 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (m->count + len < m->size) {
407 m->count += len;
408 return 0;
409 }
410 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700411 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -1;
413}
Steven Whitehousea4808142012-06-11 13:16:35 +0100414EXPORT_SYMBOL(seq_vprintf);
415
416int seq_printf(struct seq_file *m, const char *f, ...)
417{
418 int ret;
419 va_list args;
420
421 va_start(args, f);
422 ret = seq_vprintf(m, f, args);
423 va_end(args);
424
425 return ret;
426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427EXPORT_SYMBOL(seq_printf);
428
Török Edwin74e2f332008-11-22 13:28:48 +0200429/**
Török Edwin958086d2008-11-23 23:24:53 +0200430 * mangle_path - mangle and copy path to buffer beginning
431 * @s: buffer start
432 * @p: beginning of path in above buffer
433 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200434 *
435 * Copy the path from @p to @s, replacing each occurrence of character from
436 * @esc with usual octal escape.
437 * Returns pointer past last written character in @s, or NULL in case of
438 * failure.
439 */
Al Viro8c9379e2011-12-08 20:18:57 -0500440char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100441{
442 while (s <= p) {
443 char c = *p++;
444 if (!c) {
445 return s;
446 } else if (!strchr(esc, c)) {
447 *s++ = c;
448 } else if (s + 4 > p) {
449 break;
450 } else {
451 *s++ = '\\';
452 *s++ = '0' + ((c & 0300) >> 6);
453 *s++ = '0' + ((c & 070) >> 3);
454 *s++ = '0' + (c & 07);
455 }
456 }
457 return NULL;
458}
Ingo Molnar604094f2008-11-28 18:03:22 +0100459EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100460
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800461/**
462 * seq_path - seq_file interface to print a pathname
463 * @m: the seq_file handle
464 * @path: the struct path to print
465 * @esc: set of characters to escape in the output
466 *
467 * return the absolute path of 'path', as represented by the
468 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100469 */
Al Viro8c9379e2011-12-08 20:18:57 -0500470int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Miklos Szeredif8439802009-09-21 14:48:36 +0200472 char *buf;
473 size_t size = seq_get_buf(m, &buf);
474 int res = -1;
475
476 if (size) {
477 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200479 char *end = mangle_path(buf, p, esc);
480 if (end)
481 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200484 seq_commit(m, res);
485
486 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488EXPORT_SYMBOL(seq_path);
489
Ram Pai6092d042008-03-27 13:06:20 +0100490/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100491 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100492 */
Al Viro8c9379e2011-12-08 20:18:57 -0500493int seq_path_root(struct seq_file *m, const struct path *path,
494 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100495{
Miklos Szeredif8439802009-09-21 14:48:36 +0200496 char *buf;
497 size_t size = seq_get_buf(m, &buf);
498 int res = -ENAMETOOLONG;
499
500 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100501 char *p;
502
Miklos Szeredif8439802009-09-21 14:48:36 +0200503 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500504 if (!p)
505 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200506 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100507 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200508 char *end = mangle_path(buf, p, esc);
509 if (end)
510 res = end - buf;
511 else
512 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100513 }
514 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200515 seq_commit(m, res);
516
Al Viro02125a82011-12-05 08:43:34 -0500517 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100518}
519
520/*
Ram Pai6092d042008-03-27 13:06:20 +0100521 * returns the path of the 'dentry' from the root of its filesystem.
522 */
Al Viro8c9379e2011-12-08 20:18:57 -0500523int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100524{
Miklos Szeredif8439802009-09-21 14:48:36 +0200525 char *buf;
526 size_t size = seq_get_buf(m, &buf);
527 int res = -1;
528
529 if (size) {
530 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100531 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200532 char *end = mangle_path(buf, p, esc);
533 if (end)
534 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100535 }
536 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200537 seq_commit(m, res);
538
539 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100540}
541
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030542int seq_bitmap(struct seq_file *m, const unsigned long *bits,
543 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700544{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700545 if (m->count < m->size) {
546 int len = bitmap_scnprintf(m->buf + m->count,
547 m->size - m->count, bits, nr_bits);
548 if (m->count + len < m->size) {
549 m->count += len;
550 return 0;
551 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700552 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700553 seq_set_overflow(m);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700554 return -1;
555}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700556EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700557
Rusty Russellaf76aba2009-03-30 22:05:11 -0600558int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700559 unsigned int nr_bits)
560{
561 if (m->count < m->size) {
562 int len = bitmap_scnlistprintf(m->buf + m->count,
563 m->size - m->count, bits, nr_bits);
564 if (m->count + len < m->size) {
565 m->count += len;
566 return 0;
567 }
568 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700569 seq_set_overflow(m);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700570 return -1;
571}
572EXPORT_SYMBOL(seq_bitmap_list);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static void *single_start(struct seq_file *p, loff_t *pos)
575{
576 return NULL + (*pos == 0);
577}
578
579static void *single_next(struct seq_file *p, void *v, loff_t *pos)
580{
581 ++*pos;
582 return NULL;
583}
584
585static void single_stop(struct seq_file *p, void *v)
586{
587}
588
589int single_open(struct file *file, int (*show)(struct seq_file *, void *),
590 void *data)
591{
592 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
593 int res = -ENOMEM;
594
595 if (op) {
596 op->start = single_start;
597 op->next = single_next;
598 op->stop = single_stop;
599 op->show = show;
600 res = seq_open(file, op);
601 if (!res)
602 ((struct seq_file *)file->private_data)->private = data;
603 else
604 kfree(op);
605 }
606 return res;
607}
608EXPORT_SYMBOL(single_open);
609
Al Viro2043f492013-03-31 13:43:23 -0400610int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
611 void *data, size_t size)
612{
Heiko Carstens058504e2014-07-02 15:22:37 -0700613 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400614 int ret;
615 if (!buf)
616 return -ENOMEM;
617 ret = single_open(file, show, data);
618 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700619 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400620 return ret;
621 }
622 ((struct seq_file *)file->private_data)->buf = buf;
623 ((struct seq_file *)file->private_data)->size = size;
624 return 0;
625}
626EXPORT_SYMBOL(single_open_size);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628int single_release(struct inode *inode, struct file *file)
629{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800630 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int res = seq_release(inode, file);
632 kfree(op);
633 return res;
634}
635EXPORT_SYMBOL(single_release);
636
637int seq_release_private(struct inode *inode, struct file *file)
638{
639 struct seq_file *seq = file->private_data;
640
641 kfree(seq->private);
642 seq->private = NULL;
643 return seq_release(inode, file);
644}
645EXPORT_SYMBOL(seq_release_private);
646
Pavel Emelyanov39699032007-10-10 02:28:42 -0700647void *__seq_open_private(struct file *f, const struct seq_operations *ops,
648 int psize)
649{
650 int rc;
651 void *private;
652 struct seq_file *seq;
653
654 private = kzalloc(psize, GFP_KERNEL);
655 if (private == NULL)
656 goto out;
657
658 rc = seq_open(f, ops);
659 if (rc < 0)
660 goto out_free;
661
662 seq = f->private_data;
663 seq->private = private;
664 return private;
665
666out_free:
667 kfree(private);
668out:
669 return NULL;
670}
671EXPORT_SYMBOL(__seq_open_private);
672
673int seq_open_private(struct file *filp, const struct seq_operations *ops,
674 int psize)
675{
676 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
677}
678EXPORT_SYMBOL(seq_open_private);
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680int seq_putc(struct seq_file *m, char c)
681{
682 if (m->count < m->size) {
683 m->buf[m->count++] = c;
684 return 0;
685 }
686 return -1;
687}
688EXPORT_SYMBOL(seq_putc);
689
690int seq_puts(struct seq_file *m, const char *s)
691{
692 int len = strlen(s);
693 if (m->count + len < m->size) {
694 memcpy(m->buf + m->count, s, len);
695 m->count += len;
696 return 0;
697 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700698 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -1;
700}
701EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700702
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700703/*
704 * A helper routine for putting decimal numbers without rich format of printf().
705 * only 'unsigned long long' is supported.
706 * This routine will put one byte delimiter + number into seq_file.
707 * This routine is very quick when you show lots of numbers.
708 * In usual cases, it will be better to use seq_printf(). It's easier to read.
709 */
710int seq_put_decimal_ull(struct seq_file *m, char delimiter,
711 unsigned long long num)
712{
713 int len;
714
715 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
716 goto overflow;
717
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700718 if (delimiter)
719 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700720
721 if (num < 10) {
722 m->buf[m->count++] = num + '0';
723 return 0;
724 }
725
726 len = num_to_str(m->buf + m->count, m->size - m->count, num);
727 if (!len)
728 goto overflow;
729 m->count += len;
730 return 0;
731overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700732 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700733 return -1;
734}
735EXPORT_SYMBOL(seq_put_decimal_ull);
736
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700737int seq_put_decimal_ll(struct seq_file *m, char delimiter,
738 long long num)
739{
740 if (num < 0) {
741 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700742 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700743 return -1;
744 }
745 if (delimiter)
746 m->buf[m->count++] = delimiter;
747 num = -num;
748 delimiter = '-';
749 }
750 return seq_put_decimal_ull(m, delimiter, num);
751
752}
753EXPORT_SYMBOL(seq_put_decimal_ll);
754
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700755/**
756 * seq_write - write arbitrary data to buffer
757 * @seq: seq_file identifying the buffer to which data should be written
758 * @data: data address
759 * @len: number of bytes
760 *
761 * Return 0 on success, non-zero otherwise.
762 */
763int seq_write(struct seq_file *seq, const void *data, size_t len)
764{
765 if (seq->count + len < seq->size) {
766 memcpy(seq->buf + seq->count, data, len);
767 seq->count += len;
768 return 0;
769 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700770 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700771 return -1;
772}
773EXPORT_SYMBOL(seq_write);
774
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800775/**
776 * seq_pad - write padding spaces to buffer
777 * @m: seq_file identifying the buffer to which data should be written
778 * @c: the byte to append after padding if non-zero
779 */
780void seq_pad(struct seq_file *m, char c)
781{
782 int size = m->pad_until - m->count;
783 if (size > 0)
784 seq_printf(m, "%*s", size, "");
785 if (c)
786 seq_putc(m, c);
787}
788EXPORT_SYMBOL(seq_pad);
789
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700790struct list_head *seq_list_start(struct list_head *head, loff_t pos)
791{
792 struct list_head *lh;
793
794 list_for_each(lh, head)
795 if (pos-- == 0)
796 return lh;
797
798 return NULL;
799}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700800EXPORT_SYMBOL(seq_list_start);
801
802struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
803{
804 if (!pos)
805 return head;
806
807 return seq_list_start(head, pos - 1);
808}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700809EXPORT_SYMBOL(seq_list_start_head);
810
811struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
812{
813 struct list_head *lh;
814
815 lh = ((struct list_head *)v)->next;
816 ++*ppos;
817 return lh == head ? NULL : lh;
818}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700819EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000820
821/**
822 * seq_hlist_start - start an iteration of a hlist
823 * @head: the head of the hlist
824 * @pos: the start position of the sequence
825 *
826 * Called at seq_file->op->start().
827 */
828struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
829{
830 struct hlist_node *node;
831
832 hlist_for_each(node, head)
833 if (pos-- == 0)
834 return node;
835 return NULL;
836}
837EXPORT_SYMBOL(seq_hlist_start);
838
839/**
840 * seq_hlist_start_head - start an iteration of a hlist
841 * @head: the head of the hlist
842 * @pos: the start position of the sequence
843 *
844 * Called at seq_file->op->start(). Call this function if you want to
845 * print a header at the top of the output.
846 */
847struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
848{
849 if (!pos)
850 return SEQ_START_TOKEN;
851
852 return seq_hlist_start(head, pos - 1);
853}
854EXPORT_SYMBOL(seq_hlist_start_head);
855
856/**
857 * seq_hlist_next - move to the next position of the hlist
858 * @v: the current iterator
859 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800860 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000861 *
862 * Called at seq_file->op->next().
863 */
864struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
865 loff_t *ppos)
866{
867 struct hlist_node *node = v;
868
869 ++*ppos;
870 if (v == SEQ_START_TOKEN)
871 return head->first;
872 else
873 return node->next;
874}
875EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000876
877/**
878 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
879 * @head: the head of the hlist
880 * @pos: the start position of the sequence
881 *
882 * Called at seq_file->op->start().
883 *
884 * This list-traversal primitive may safely run concurrently with
885 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
886 * as long as the traversal is guarded by rcu_read_lock().
887 */
888struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
889 loff_t pos)
890{
891 struct hlist_node *node;
892
893 __hlist_for_each_rcu(node, head)
894 if (pos-- == 0)
895 return node;
896 return NULL;
897}
898EXPORT_SYMBOL(seq_hlist_start_rcu);
899
900/**
901 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
902 * @head: the head of the hlist
903 * @pos: the start position of the sequence
904 *
905 * Called at seq_file->op->start(). Call this function if you want to
906 * print a header at the top of the output.
907 *
908 * This list-traversal primitive may safely run concurrently with
909 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
910 * as long as the traversal is guarded by rcu_read_lock().
911 */
912struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
913 loff_t pos)
914{
915 if (!pos)
916 return SEQ_START_TOKEN;
917
918 return seq_hlist_start_rcu(head, pos - 1);
919}
920EXPORT_SYMBOL(seq_hlist_start_head_rcu);
921
922/**
923 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
924 * @v: the current iterator
925 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800926 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000927 *
928 * Called at seq_file->op->next().
929 *
930 * This list-traversal primitive may safely run concurrently with
931 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
932 * as long as the traversal is guarded by rcu_read_lock().
933 */
934struct hlist_node *seq_hlist_next_rcu(void *v,
935 struct hlist_head *head,
936 loff_t *ppos)
937{
938 struct hlist_node *node = v;
939
940 ++*ppos;
941 if (v == SEQ_START_TOKEN)
942 return rcu_dereference(head->first);
943 else
944 return rcu_dereference(node->next);
945}
946EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400947
948/**
949 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
950 * @head: pointer to percpu array of struct hlist_heads
951 * @cpu: pointer to cpu "cursor"
952 * @pos: start position of sequence
953 *
954 * Called at seq_file->op->start().
955 */
956struct hlist_node *
957seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
958{
959 struct hlist_node *node;
960
961 for_each_possible_cpu(*cpu) {
962 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
963 if (pos-- == 0)
964 return node;
965 }
966 }
967 return NULL;
968}
969EXPORT_SYMBOL(seq_hlist_start_percpu);
970
971/**
972 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
973 * @v: pointer to current hlist_node
974 * @head: pointer to percpu array of struct hlist_heads
975 * @cpu: pointer to cpu "cursor"
976 * @pos: start position of sequence
977 *
978 * Called at seq_file->op->next().
979 */
980struct hlist_node *
981seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
982 int *cpu, loff_t *pos)
983{
984 struct hlist_node *node = v;
985
986 ++*pos;
987
988 if (node->next)
989 return node->next;
990
991 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
992 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
993 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
994
995 if (!hlist_empty(bucket))
996 return bucket->first;
997 }
998 return NULL;
999}
1000EXPORT_SYMBOL(seq_hlist_next_percpu);