blob: 760e25dad9850b77a3a73568cbf109e2a88a9a7c [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".
Yann Droneaud460b8652015-06-30 14:57:36 -070051 * Note: seq_open() will allocate a struct seq_file and store its
52 * pointer in @file->private_data. This pointer should not be modified.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080054int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Yann Droneaud189f9842015-06-30 14:57:33 -070056 struct seq_file *p;
Al Viro1abe77b2005-11-07 17:15:34 -050057
Yann Droneaud189f9842015-06-30 14:57:33 -070058 WARN_ON(file->private_data);
59
60 p = kzalloc(sizeof(*p), GFP_KERNEL);
61 if (!p)
62 return -ENOMEM;
63
64 file->private_data = p;
65
Ingo Molnar0ac17592006-03-23 03:00:37 -080066 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 p->op = op;
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060068#ifdef CONFIG_USER_NS
69 p->user_ns = file->f_cred->user_ns;
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /*
73 * Wrappers around seq_open(e.g. swaps_open) need to be
74 * aware of this. If they set f_version themselves, they
75 * should call seq_open first and then set f_version.
76 */
77 file->f_version = 0;
78
Eric Biederman8f19d472009-02-18 14:48:16 -080079 /*
80 * seq_files support lseek() and pread(). They do not implement
81 * write() at all, but we clear FMODE_PWRITE here for historical
82 * reasons.
83 *
84 * If a client of seq_files a) implements file.write() and b) wishes to
85 * support pwrite() then that client will need to implement its own
86 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
87 */
88 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return 0;
90}
91EXPORT_SYMBOL(seq_open);
92
Eric Biederman33da8892009-02-04 15:12:25 -080093static int traverse(struct seq_file *m, loff_t offset)
94{
95 loff_t pos = 0, index;
96 int error = 0;
97 void *p;
98
99 m->version = 0;
100 index = 0;
101 m->count = m->from = 0;
102 if (!offset) {
103 m->index = index;
104 return 0;
105 }
106 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700107 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800108 if (!m->buf)
109 return -ENOMEM;
110 }
111 p = m->op->start(m, &index);
112 while (p) {
113 error = PTR_ERR(p);
114 if (IS_ERR(p))
115 break;
116 error = m->op->show(m, p);
117 if (error < 0)
118 break;
119 if (unlikely(error)) {
120 error = 0;
121 m->count = 0;
122 }
Joe Perches1f33c412014-09-29 16:08:21 -0700123 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800124 goto Eoverflow;
125 if (pos + m->count > offset) {
126 m->from = offset - pos;
127 m->count -= m->from;
128 m->index = index;
129 break;
130 }
131 pos += m->count;
132 m->count = 0;
133 if (pos == offset) {
134 index++;
135 m->index = index;
136 break;
137 }
138 p = m->op->next(m, p, &index);
139 }
140 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300141 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800142 return error;
143
144Eoverflow:
145 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700146 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000147 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700148 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800149 return !m->buf ? -ENOMEM : -EAGAIN;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/**
153 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700154 * @file: the file to read from
155 * @buf: the buffer to read to
156 * @size: the maximum number of bytes to read
157 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 *
159 * Ready-made ->f_op->read()
160 */
161ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
162{
Joe Perches8209e2f2010-09-04 18:52:49 -0700163 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 size_t copied = 0;
165 loff_t pos;
166 size_t n;
167 void *p;
168 int err = 0;
169
Ingo Molnar0ac17592006-03-23 03:00:37 -0800170 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /*
173 * seq_file->op->..m_start/m_stop/m_next may do special actions
174 * or optimisations based on the file->f_version, so we want to
175 * pass the file->f_version to those methods.
176 *
177 * seq_file->version is just copy of f_version, and seq_file
178 * methods can treat it simply as file version.
179 * It is copied in first and copied out after all operations.
180 * It is convenient to have it as part of structure to avoid the
181 * need of passing another argument to all the seq_file methods.
182 */
183 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700184
185 /* Don't assume *ppos is where we left it */
186 if (unlikely(*ppos != m->read_pos)) {
187 while ((err = traverse(m, *ppos)) == -EAGAIN)
188 ;
189 if (err) {
190 /* With prejudice... */
191 m->read_pos = 0;
192 m->version = 0;
193 m->index = 0;
194 m->count = 0;
195 goto Done;
196 } else {
197 m->read_pos = *ppos;
198 }
199 }
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* grab buffer if we didn't have one */
202 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700203 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (!m->buf)
205 goto Enomem;
206 }
207 /* if not empty - flush it first */
208 if (m->count) {
209 n = min(m->count, size);
210 err = copy_to_user(buf, m->buf + m->from, n);
211 if (err)
212 goto Efault;
213 m->count -= n;
214 m->from += n;
215 size -= n;
216 buf += n;
217 copied += n;
218 if (!m->count)
219 m->index++;
220 if (!size)
221 goto Done;
222 }
223 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400224 pos = m->index;
225 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 err = PTR_ERR(p);
228 if (!p || IS_ERR(p))
229 break;
230 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400231 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
Al Viro521b5d02008-03-28 00:46:41 -0400233 if (unlikely(err))
234 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400235 if (unlikely(!m->count)) {
236 p = m->op->next(m, p, &pos);
237 m->index = pos;
238 continue;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (m->count < m->size)
241 goto Fill;
242 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700243 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000244 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700245 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!m->buf)
247 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400249 pos = m->index;
250 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 m->op->stop(m, p);
253 m->count = 0;
254 goto Done;
255Fill:
256 /* they want more? let's try to get some more */
257 while (m->count < size) {
258 size_t offs = m->count;
259 loff_t next = pos;
260 p = m->op->next(m, p, &next);
261 if (!p || IS_ERR(p)) {
262 err = PTR_ERR(p);
263 break;
264 }
265 err = m->op->show(m, p);
Joe Perches1f33c412014-09-29 16:08:21 -0700266 if (seq_has_overflowed(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400268 if (likely(err <= 0))
269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 pos = next;
272 }
273 m->op->stop(m, p);
274 n = min(m->count, size);
275 err = copy_to_user(buf, m->buf, n);
276 if (err)
277 goto Efault;
278 copied += n;
279 m->count -= n;
280 if (m->count)
281 m->from = n;
282 else
283 pos++;
284 m->index = pos;
285Done:
286 if (!copied)
287 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800288 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800290 m->read_pos += copied;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800293 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return copied;
295Enomem:
296 err = -ENOMEM;
297 goto Done;
298Efault:
299 err = -EFAULT;
300 goto Done;
301}
302EXPORT_SYMBOL(seq_read);
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/**
305 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700306 * @file: the file in question
307 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800308 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
310 * Ready-made ->f_op->llseek()
311 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800312loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Joe Perches8209e2f2010-09-04 18:52:49 -0700314 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200315 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Ingo Molnar0ac17592006-03-23 03:00:37 -0800317 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 m->version = file->f_version;
Andrew Morton965c8e52012-12-17 15:59:39 -0800319 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800320 case SEEK_CUR:
321 offset += file->f_pos;
322 case SEEK_SET:
323 if (offset < 0)
324 break;
325 retval = offset;
326 if (offset != m->read_pos) {
327 while ((retval = traverse(m, offset)) == -EAGAIN)
328 ;
329 if (retval) {
330 /* with extreme prejudice... */
331 file->f_pos = 0;
332 m->read_pos = 0;
333 m->version = 0;
334 m->index = 0;
335 m->count = 0;
336 } else {
337 m->read_pos = offset;
338 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Gu Zheng05e16742013-10-25 18:15:06 +0800340 } else {
341 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700345 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return retval;
347}
348EXPORT_SYMBOL(seq_lseek);
349
350/**
351 * seq_release - free the structures associated with sequential file.
352 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500353 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *
355 * Frees the structures associated with sequential file; can be used
356 * as ->f_op->release() if you don't have private data to destroy.
357 */
358int seq_release(struct inode *inode, struct file *file)
359{
Joe Perches8209e2f2010-09-04 18:52:49 -0700360 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700361 kvfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 kfree(m);
363 return 0;
364}
365EXPORT_SYMBOL(seq_release);
366
367/**
368 * seq_escape - print string into buffer, escaping some characters
369 * @m: target buffer
370 * @s: string
371 * @esc: set of characters that need escaping
372 *
373 * Puts string into buffer, replacing each occurrence of character from
374 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
375 * case of overflow.
376 */
377int seq_escape(struct seq_file *m, const char *s, const char *esc)
378{
379 char *end = m->buf + m->size;
380 char *p;
381 char c;
382
383 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
384 if (!strchr(esc, c)) {
385 *p++ = c;
386 continue;
387 }
388 if (p + 3 < end) {
389 *p++ = '\\';
390 *p++ = '0' + ((c & 0300) >> 6);
391 *p++ = '0' + ((c & 070) >> 3);
392 *p++ = '0' + (c & 07);
393 continue;
394 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700395 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -1;
397 }
398 m->count = p - m->buf;
399 return 0;
400}
401EXPORT_SYMBOL(seq_escape);
402
Steven Whitehousea4808142012-06-11 13:16:35 +0100403int seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int len;
406
407 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (m->count + len < m->size) {
410 m->count += len;
411 return 0;
412 }
413 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700414 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return -1;
416}
Steven Whitehousea4808142012-06-11 13:16:35 +0100417EXPORT_SYMBOL(seq_vprintf);
418
419int seq_printf(struct seq_file *m, const char *f, ...)
420{
421 int ret;
422 va_list args;
423
424 va_start(args, f);
425 ret = seq_vprintf(m, f, args);
426 va_end(args);
427
428 return ret;
429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430EXPORT_SYMBOL(seq_printf);
431
Török Edwin74e2f332008-11-22 13:28:48 +0200432/**
Török Edwin958086d2008-11-23 23:24:53 +0200433 * mangle_path - mangle and copy path to buffer beginning
434 * @s: buffer start
435 * @p: beginning of path in above buffer
436 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200437 *
438 * Copy the path from @p to @s, replacing each occurrence of character from
439 * @esc with usual octal escape.
440 * Returns pointer past last written character in @s, or NULL in case of
441 * failure.
442 */
Al Viro8c9379e2011-12-08 20:18:57 -0500443char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100444{
445 while (s <= p) {
446 char c = *p++;
447 if (!c) {
448 return s;
449 } else if (!strchr(esc, c)) {
450 *s++ = c;
451 } else if (s + 4 > p) {
452 break;
453 } else {
454 *s++ = '\\';
455 *s++ = '0' + ((c & 0300) >> 6);
456 *s++ = '0' + ((c & 070) >> 3);
457 *s++ = '0' + (c & 07);
458 }
459 }
460 return NULL;
461}
Ingo Molnar604094f2008-11-28 18:03:22 +0100462EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100463
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800464/**
465 * seq_path - seq_file interface to print a pathname
466 * @m: the seq_file handle
467 * @path: the struct path to print
468 * @esc: set of characters to escape in the output
469 *
470 * return the absolute path of 'path', as represented by the
471 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100472 */
Al Viro8c9379e2011-12-08 20:18:57 -0500473int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Miklos Szeredif8439802009-09-21 14:48:36 +0200475 char *buf;
476 size_t size = seq_get_buf(m, &buf);
477 int res = -1;
478
479 if (size) {
480 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200482 char *end = mangle_path(buf, p, esc);
483 if (end)
484 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200487 seq_commit(m, res);
488
489 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491EXPORT_SYMBOL(seq_path);
492
Ram Pai6092d042008-03-27 13:06:20 +0100493/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100494 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100495 */
Al Viro8c9379e2011-12-08 20:18:57 -0500496int seq_path_root(struct seq_file *m, const struct path *path,
497 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100498{
Miklos Szeredif8439802009-09-21 14:48:36 +0200499 char *buf;
500 size_t size = seq_get_buf(m, &buf);
501 int res = -ENAMETOOLONG;
502
503 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100504 char *p;
505
Miklos Szeredif8439802009-09-21 14:48:36 +0200506 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500507 if (!p)
508 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200509 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100510 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200511 char *end = mangle_path(buf, p, esc);
512 if (end)
513 res = end - buf;
514 else
515 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100516 }
517 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200518 seq_commit(m, res);
519
Al Viro02125a82011-12-05 08:43:34 -0500520 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100521}
522
523/*
Ram Pai6092d042008-03-27 13:06:20 +0100524 * returns the path of the 'dentry' from the root of its filesystem.
525 */
Al Viro8c9379e2011-12-08 20:18:57 -0500526int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100527{
Miklos Szeredif8439802009-09-21 14:48:36 +0200528 char *buf;
529 size_t size = seq_get_buf(m, &buf);
530 int res = -1;
531
532 if (size) {
533 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100534 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200535 char *end = mangle_path(buf, p, esc);
536 if (end)
537 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100538 }
539 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200540 seq_commit(m, res);
541
542 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static void *single_start(struct seq_file *p, loff_t *pos)
546{
547 return NULL + (*pos == 0);
548}
549
550static void *single_next(struct seq_file *p, void *v, loff_t *pos)
551{
552 ++*pos;
553 return NULL;
554}
555
556static void single_stop(struct seq_file *p, void *v)
557{
558}
559
560int single_open(struct file *file, int (*show)(struct seq_file *, void *),
561 void *data)
562{
563 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
564 int res = -ENOMEM;
565
566 if (op) {
567 op->start = single_start;
568 op->next = single_next;
569 op->stop = single_stop;
570 op->show = show;
571 res = seq_open(file, op);
572 if (!res)
573 ((struct seq_file *)file->private_data)->private = data;
574 else
575 kfree(op);
576 }
577 return res;
578}
579EXPORT_SYMBOL(single_open);
580
Al Viro2043f492013-03-31 13:43:23 -0400581int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
582 void *data, size_t size)
583{
Heiko Carstens058504e2014-07-02 15:22:37 -0700584 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400585 int ret;
586 if (!buf)
587 return -ENOMEM;
588 ret = single_open(file, show, data);
589 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700590 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400591 return ret;
592 }
593 ((struct seq_file *)file->private_data)->buf = buf;
594 ((struct seq_file *)file->private_data)->size = size;
595 return 0;
596}
597EXPORT_SYMBOL(single_open_size);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599int single_release(struct inode *inode, struct file *file)
600{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800601 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int res = seq_release(inode, file);
603 kfree(op);
604 return res;
605}
606EXPORT_SYMBOL(single_release);
607
608int seq_release_private(struct inode *inode, struct file *file)
609{
610 struct seq_file *seq = file->private_data;
611
612 kfree(seq->private);
613 seq->private = NULL;
614 return seq_release(inode, file);
615}
616EXPORT_SYMBOL(seq_release_private);
617
Pavel Emelyanov39699032007-10-10 02:28:42 -0700618void *__seq_open_private(struct file *f, const struct seq_operations *ops,
619 int psize)
620{
621 int rc;
622 void *private;
623 struct seq_file *seq;
624
625 private = kzalloc(psize, GFP_KERNEL);
626 if (private == NULL)
627 goto out;
628
629 rc = seq_open(f, ops);
630 if (rc < 0)
631 goto out_free;
632
633 seq = f->private_data;
634 seq->private = private;
635 return private;
636
637out_free:
638 kfree(private);
639out:
640 return NULL;
641}
642EXPORT_SYMBOL(__seq_open_private);
643
644int seq_open_private(struct file *filp, const struct seq_operations *ops,
645 int psize)
646{
647 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
648}
649EXPORT_SYMBOL(seq_open_private);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651int seq_putc(struct seq_file *m, char c)
652{
653 if (m->count < m->size) {
654 m->buf[m->count++] = c;
655 return 0;
656 }
657 return -1;
658}
659EXPORT_SYMBOL(seq_putc);
660
661int seq_puts(struct seq_file *m, const char *s)
662{
663 int len = strlen(s);
664 if (m->count + len < m->size) {
665 memcpy(m->buf + m->count, s, len);
666 m->count += len;
667 return 0;
668 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700669 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -1;
671}
672EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700673
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700674/*
675 * A helper routine for putting decimal numbers without rich format of printf().
676 * only 'unsigned long long' is supported.
677 * This routine will put one byte delimiter + number into seq_file.
678 * This routine is very quick when you show lots of numbers.
679 * In usual cases, it will be better to use seq_printf(). It's easier to read.
680 */
681int seq_put_decimal_ull(struct seq_file *m, char delimiter,
682 unsigned long long num)
683{
684 int len;
685
686 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
687 goto overflow;
688
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700689 if (delimiter)
690 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700691
692 if (num < 10) {
693 m->buf[m->count++] = num + '0';
694 return 0;
695 }
696
697 len = num_to_str(m->buf + m->count, m->size - m->count, num);
698 if (!len)
699 goto overflow;
700 m->count += len;
701 return 0;
702overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700703 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700704 return -1;
705}
706EXPORT_SYMBOL(seq_put_decimal_ull);
707
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700708int seq_put_decimal_ll(struct seq_file *m, char delimiter,
709 long long num)
710{
711 if (num < 0) {
712 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700713 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700714 return -1;
715 }
716 if (delimiter)
717 m->buf[m->count++] = delimiter;
718 num = -num;
719 delimiter = '-';
720 }
721 return seq_put_decimal_ull(m, delimiter, num);
722
723}
724EXPORT_SYMBOL(seq_put_decimal_ll);
725
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700726/**
727 * seq_write - write arbitrary data to buffer
728 * @seq: seq_file identifying the buffer to which data should be written
729 * @data: data address
730 * @len: number of bytes
731 *
732 * Return 0 on success, non-zero otherwise.
733 */
734int seq_write(struct seq_file *seq, const void *data, size_t len)
735{
736 if (seq->count + len < seq->size) {
737 memcpy(seq->buf + seq->count, data, len);
738 seq->count += len;
739 return 0;
740 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700741 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700742 return -1;
743}
744EXPORT_SYMBOL(seq_write);
745
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800746/**
747 * seq_pad - write padding spaces to buffer
748 * @m: seq_file identifying the buffer to which data should be written
749 * @c: the byte to append after padding if non-zero
750 */
751void seq_pad(struct seq_file *m, char c)
752{
753 int size = m->pad_until - m->count;
754 if (size > 0)
755 seq_printf(m, "%*s", size, "");
756 if (c)
757 seq_putc(m, c);
758}
759EXPORT_SYMBOL(seq_pad);
760
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700761struct list_head *seq_list_start(struct list_head *head, loff_t pos)
762{
763 struct list_head *lh;
764
765 list_for_each(lh, head)
766 if (pos-- == 0)
767 return lh;
768
769 return NULL;
770}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700771EXPORT_SYMBOL(seq_list_start);
772
773struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
774{
775 if (!pos)
776 return head;
777
778 return seq_list_start(head, pos - 1);
779}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700780EXPORT_SYMBOL(seq_list_start_head);
781
782struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
783{
784 struct list_head *lh;
785
786 lh = ((struct list_head *)v)->next;
787 ++*ppos;
788 return lh == head ? NULL : lh;
789}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700790EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000791
792/**
793 * seq_hlist_start - start an iteration of a hlist
794 * @head: the head of the hlist
795 * @pos: the start position of the sequence
796 *
797 * Called at seq_file->op->start().
798 */
799struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
800{
801 struct hlist_node *node;
802
803 hlist_for_each(node, head)
804 if (pos-- == 0)
805 return node;
806 return NULL;
807}
808EXPORT_SYMBOL(seq_hlist_start);
809
810/**
811 * seq_hlist_start_head - start an iteration of a hlist
812 * @head: the head of the hlist
813 * @pos: the start position of the sequence
814 *
815 * Called at seq_file->op->start(). Call this function if you want to
816 * print a header at the top of the output.
817 */
818struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
819{
820 if (!pos)
821 return SEQ_START_TOKEN;
822
823 return seq_hlist_start(head, pos - 1);
824}
825EXPORT_SYMBOL(seq_hlist_start_head);
826
827/**
828 * seq_hlist_next - move to the next position of the hlist
829 * @v: the current iterator
830 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800831 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000832 *
833 * Called at seq_file->op->next().
834 */
835struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
836 loff_t *ppos)
837{
838 struct hlist_node *node = v;
839
840 ++*ppos;
841 if (v == SEQ_START_TOKEN)
842 return head->first;
843 else
844 return node->next;
845}
846EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000847
848/**
849 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
850 * @head: the head of the hlist
851 * @pos: the start position of the sequence
852 *
853 * Called at seq_file->op->start().
854 *
855 * This list-traversal primitive may safely run concurrently with
856 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
857 * as long as the traversal is guarded by rcu_read_lock().
858 */
859struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
860 loff_t pos)
861{
862 struct hlist_node *node;
863
864 __hlist_for_each_rcu(node, head)
865 if (pos-- == 0)
866 return node;
867 return NULL;
868}
869EXPORT_SYMBOL(seq_hlist_start_rcu);
870
871/**
872 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
873 * @head: the head of the hlist
874 * @pos: the start position of the sequence
875 *
876 * Called at seq_file->op->start(). Call this function if you want to
877 * print a header at the top of the output.
878 *
879 * This list-traversal primitive may safely run concurrently with
880 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
881 * as long as the traversal is guarded by rcu_read_lock().
882 */
883struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
884 loff_t pos)
885{
886 if (!pos)
887 return SEQ_START_TOKEN;
888
889 return seq_hlist_start_rcu(head, pos - 1);
890}
891EXPORT_SYMBOL(seq_hlist_start_head_rcu);
892
893/**
894 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
895 * @v: the current iterator
896 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800897 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000898 *
899 * Called at seq_file->op->next().
900 *
901 * This list-traversal primitive may safely run concurrently with
902 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
903 * as long as the traversal is guarded by rcu_read_lock().
904 */
905struct hlist_node *seq_hlist_next_rcu(void *v,
906 struct hlist_head *head,
907 loff_t *ppos)
908{
909 struct hlist_node *node = v;
910
911 ++*ppos;
912 if (v == SEQ_START_TOKEN)
913 return rcu_dereference(head->first);
914 else
915 return rcu_dereference(node->next);
916}
917EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400918
919/**
920 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
921 * @head: pointer to percpu array of struct hlist_heads
922 * @cpu: pointer to cpu "cursor"
923 * @pos: start position of sequence
924 *
925 * Called at seq_file->op->start().
926 */
927struct hlist_node *
928seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
929{
930 struct hlist_node *node;
931
932 for_each_possible_cpu(*cpu) {
933 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
934 if (pos-- == 0)
935 return node;
936 }
937 }
938 return NULL;
939}
940EXPORT_SYMBOL(seq_hlist_start_percpu);
941
942/**
943 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
944 * @v: pointer to current hlist_node
945 * @head: pointer to percpu array of struct hlist_heads
946 * @cpu: pointer to cpu "cursor"
947 * @pos: start position of sequence
948 *
949 * Called at seq_file->op->next().
950 */
951struct hlist_node *
952seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
953 int *cpu, loff_t *pos)
954{
955 struct hlist_node *node = v;
956
957 ++*pos;
958
959 if (node->next)
960 return node->next;
961
962 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
963 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
964 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
965
966 if (!hlist_empty(bucket))
967 return bucket->first;
968 }
969 return NULL;
970}
971EXPORT_SYMBOL(seq_hlist_next_percpu);