blob: 3ade39e02bb731492737ec432921983b8140e80a [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>
Andy Shevchenko25c6bb76e2015-11-06 16:32:40 -080016#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/uaccess.h>
19#include <asm/page.h>
20
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070021static void seq_set_overflow(struct seq_file *m)
22{
23 m->count = m->size;
24}
25
Heiko Carstens058504e2014-07-02 15:22:37 -070026static void *seq_buf_alloc(unsigned long size)
27{
28 void *buf;
Greg Thelen0f930902015-11-06 16:32:42 -080029 gfp_t gfp = GFP_KERNEL;
Heiko Carstens058504e2014-07-02 15:22:37 -070030
Eric Sandeenc5157b32021-07-13 17:49:23 +020031 if (unlikely(size > MAX_RW_COUNT))
32 return NULL;
33
David Rientjes5cec38a2014-12-12 16:56:16 -080034 /*
Greg Thelen0f930902015-11-06 16:32:42 -080035 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
36 * it's better to fall back to vmalloc() than to kill things. For small
37 * allocations, just use GFP_KERNEL which will oom kill, thus no need
38 * for vmalloc fallback.
David Rientjes5cec38a2014-12-12 16:56:16 -080039 */
Greg Thelen0f930902015-11-06 16:32:42 -080040 if (size > PAGE_SIZE)
41 gfp |= __GFP_NORETRY | __GFP_NOWARN;
42 buf = kmalloc(size, gfp);
Heiko Carstens058504e2014-07-02 15:22:37 -070043 if (!buf && size > PAGE_SIZE)
44 buf = vmalloc(size);
45 return buf;
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
49 * seq_open - initialize sequential file
50 * @file: file we initialize
51 * @op: method table describing the sequence
52 *
53 * seq_open() sets @file, associating it with a sequence described
54 * by @op. @op->start() sets the iterator up and returns the first
55 * element of sequence. @op->stop() shuts it down. @op->next()
56 * returns the next element of sequence. @op->show() prints element
57 * into the buffer. In case of error ->start() and ->next() return
58 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
59 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040060 * Returning SEQ_SKIP means "discard this element and move on".
Yann Droneaud460b8652015-06-30 14:57:36 -070061 * Note: seq_open() will allocate a struct seq_file and store its
62 * pointer in @file->private_data. This pointer should not be modified.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080064int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Yann Droneaud189f9842015-06-30 14:57:33 -070066 struct seq_file *p;
Al Viro1abe77b2005-11-07 17:15:34 -050067
Yann Droneaud189f9842015-06-30 14:57:33 -070068 WARN_ON(file->private_data);
69
70 p = kzalloc(sizeof(*p), GFP_KERNEL);
71 if (!p)
72 return -ENOMEM;
73
74 file->private_data = p;
75
Ingo Molnar0ac17592006-03-23 03:00:37 -080076 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 p->op = op;
Linus Torvalds34dbbcd2016-04-14 11:22:00 -070078
79 // No refcounting: the lifetime of 'p' is constrained
80 // to the lifetime of the file.
81 p->file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /*
84 * Wrappers around seq_open(e.g. swaps_open) need to be
85 * aware of this. If they set f_version themselves, they
86 * should call seq_open first and then set f_version.
87 */
88 file->f_version = 0;
89
Eric Biederman8f19d472009-02-18 14:48:16 -080090 /*
91 * seq_files support lseek() and pread(). They do not implement
92 * write() at all, but we clear FMODE_PWRITE here for historical
93 * reasons.
94 *
95 * If a client of seq_files a) implements file.write() and b) wishes to
96 * support pwrite() then that client will need to implement its own
97 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
98 */
99 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102EXPORT_SYMBOL(seq_open);
103
Eric Biederman33da8892009-02-04 15:12:25 -0800104static int traverse(struct seq_file *m, loff_t offset)
105{
106 loff_t pos = 0, index;
107 int error = 0;
108 void *p;
109
110 m->version = 0;
111 index = 0;
112 m->count = m->from = 0;
113 if (!offset) {
114 m->index = index;
115 return 0;
116 }
117 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700118 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800119 if (!m->buf)
120 return -ENOMEM;
121 }
122 p = m->op->start(m, &index);
123 while (p) {
124 error = PTR_ERR(p);
125 if (IS_ERR(p))
126 break;
127 error = m->op->show(m, p);
128 if (error < 0)
129 break;
130 if (unlikely(error)) {
131 error = 0;
132 m->count = 0;
133 }
Joe Perches1f33c412014-09-29 16:08:21 -0700134 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800135 goto Eoverflow;
136 if (pos + m->count > offset) {
137 m->from = offset - pos;
138 m->count -= m->from;
139 m->index = index;
140 break;
141 }
142 pos += m->count;
143 m->count = 0;
144 if (pos == offset) {
145 index++;
146 m->index = index;
147 break;
148 }
149 p = m->op->next(m, p, &index);
150 }
151 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300152 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800153 return error;
154
155Eoverflow:
156 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700157 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000158 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700159 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800160 return !m->buf ? -ENOMEM : -EAGAIN;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700165 * @file: the file to read from
166 * @buf: the buffer to read to
167 * @size: the maximum number of bytes to read
168 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
170 * Ready-made ->f_op->read()
171 */
172ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
173{
Joe Perches8209e2f2010-09-04 18:52:49 -0700174 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 size_t copied = 0;
176 loff_t pos;
177 size_t n;
178 void *p;
179 int err = 0;
180
Ingo Molnar0ac17592006-03-23 03:00:37 -0800181 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /*
184 * seq_file->op->..m_start/m_stop/m_next may do special actions
185 * or optimisations based on the file->f_version, so we want to
186 * pass the file->f_version to those methods.
187 *
188 * seq_file->version is just copy of f_version, and seq_file
189 * methods can treat it simply as file version.
190 * It is copied in first and copied out after all operations.
191 * It is convenient to have it as part of structure to avoid the
192 * need of passing another argument to all the seq_file methods.
193 */
194 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700195
196 /* Don't assume *ppos is where we left it */
197 if (unlikely(*ppos != m->read_pos)) {
198 while ((err = traverse(m, *ppos)) == -EAGAIN)
199 ;
200 if (err) {
201 /* With prejudice... */
202 m->read_pos = 0;
203 m->version = 0;
204 m->index = 0;
205 m->count = 0;
206 goto Done;
207 } else {
208 m->read_pos = *ppos;
209 }
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* grab buffer if we didn't have one */
213 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700214 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!m->buf)
216 goto Enomem;
217 }
218 /* if not empty - flush it first */
219 if (m->count) {
220 n = min(m->count, size);
221 err = copy_to_user(buf, m->buf + m->from, n);
222 if (err)
223 goto Efault;
224 m->count -= n;
225 m->from += n;
226 size -= n;
227 buf += n;
228 copied += n;
Vegard Nossum088bf2f2016-08-25 15:17:11 -0700229 if (!m->count) {
230 m->from = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 m->index++;
Vegard Nossum088bf2f2016-08-25 15:17:11 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!size)
234 goto Done;
235 }
236 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400237 pos = m->index;
238 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 err = PTR_ERR(p);
241 if (!p || IS_ERR(p))
242 break;
243 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400244 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
Al Viro521b5d02008-03-28 00:46:41 -0400246 if (unlikely(err))
247 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400248 if (unlikely(!m->count)) {
249 p = m->op->next(m, p, &pos);
250 m->index = pos;
251 continue;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (m->count < m->size)
254 goto Fill;
255 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700256 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000257 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700258 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (!m->buf)
260 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400262 pos = m->index;
263 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 m->op->stop(m, p);
266 m->count = 0;
267 goto Done;
268Fill:
269 /* they want more? let's try to get some more */
270 while (m->count < size) {
271 size_t offs = m->count;
272 loff_t next = pos;
273 p = m->op->next(m, p, &next);
274 if (!p || IS_ERR(p)) {
275 err = PTR_ERR(p);
276 break;
277 }
278 err = m->op->show(m, p);
Joe Perches1f33c412014-09-29 16:08:21 -0700279 if (seq_has_overflowed(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400281 if (likely(err <= 0))
282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 pos = next;
285 }
286 m->op->stop(m, p);
287 n = min(m->count, size);
288 err = copy_to_user(buf, m->buf, n);
289 if (err)
290 goto Efault;
291 copied += n;
292 m->count -= n;
293 if (m->count)
294 m->from = n;
295 else
296 pos++;
297 m->index = pos;
298Done:
299 if (!copied)
300 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800301 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800303 m->read_pos += copied;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800306 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return copied;
308Enomem:
309 err = -ENOMEM;
310 goto Done;
311Efault:
312 err = -EFAULT;
313 goto Done;
314}
315EXPORT_SYMBOL(seq_read);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/**
318 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700319 * @file: the file in question
320 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800321 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 *
323 * Ready-made ->f_op->llseek()
324 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800325loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Joe Perches8209e2f2010-09-04 18:52:49 -0700327 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200328 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Ingo Molnar0ac17592006-03-23 03:00:37 -0800330 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 m->version = file->f_version;
Andrew Morton965c8e52012-12-17 15:59:39 -0800332 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800333 case SEEK_CUR:
334 offset += file->f_pos;
335 case SEEK_SET:
336 if (offset < 0)
337 break;
338 retval = offset;
339 if (offset != m->read_pos) {
340 while ((retval = traverse(m, offset)) == -EAGAIN)
341 ;
342 if (retval) {
343 /* with extreme prejudice... */
344 file->f_pos = 0;
345 m->read_pos = 0;
346 m->version = 0;
347 m->index = 0;
348 m->count = 0;
349 } else {
350 m->read_pos = offset;
351 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Gu Zheng05e16742013-10-25 18:15:06 +0800353 } else {
354 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700358 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return retval;
360}
361EXPORT_SYMBOL(seq_lseek);
362
363/**
364 * seq_release - free the structures associated with sequential file.
365 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500366 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
368 * Frees the structures associated with sequential file; can be used
369 * as ->f_op->release() if you don't have private data to destroy.
370 */
371int seq_release(struct inode *inode, struct file *file)
372{
Joe Perches8209e2f2010-09-04 18:52:49 -0700373 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700374 kvfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 kfree(m);
376 return 0;
377}
378EXPORT_SYMBOL(seq_release);
379
380/**
381 * seq_escape - print string into buffer, escaping some characters
382 * @m: target buffer
383 * @s: string
384 * @esc: set of characters that need escaping
385 *
386 * Puts string into buffer, replacing each occurrence of character from
Joe Perches6798a8c2015-09-11 13:07:48 -0700387 * @esc with usual octal escape.
388 * Use seq_has_overflowed() to check for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
Joe Perches6798a8c2015-09-11 13:07:48 -0700390void seq_escape(struct seq_file *m, const char *s, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Andy Shevchenko25c6bb76e2015-11-06 16:32:40 -0800392 char *buf;
393 size_t size = seq_get_buf(m, &buf);
394 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Andy Shevchenko25c6bb76e2015-11-06 16:32:40 -0800396 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
397 seq_commit(m, ret < size ? ret : -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399EXPORT_SYMBOL(seq_escape);
400
Joe Perches6798a8c2015-09-11 13:07:48 -0700401void seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int len;
404
405 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (m->count + len < m->size) {
408 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700409 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700412 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
Steven Whitehousea4808142012-06-11 13:16:35 +0100414EXPORT_SYMBOL(seq_vprintf);
415
Joe Perches6798a8c2015-09-11 13:07:48 -0700416void seq_printf(struct seq_file *m, const char *f, ...)
Steven Whitehousea4808142012-06-11 13:16:35 +0100417{
Steven Whitehousea4808142012-06-11 13:16:35 +0100418 va_list args;
419
420 va_start(args, f);
Joe Perches6798a8c2015-09-11 13:07:48 -0700421 seq_vprintf(m, f, args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100422 va_end(args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424EXPORT_SYMBOL(seq_printf);
425
Török Edwin74e2f332008-11-22 13:28:48 +0200426/**
Török Edwin958086d2008-11-23 23:24:53 +0200427 * mangle_path - mangle and copy path to buffer beginning
428 * @s: buffer start
429 * @p: beginning of path in above buffer
430 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200431 *
432 * Copy the path from @p to @s, replacing each occurrence of character from
433 * @esc with usual octal escape.
434 * Returns pointer past last written character in @s, or NULL in case of
435 * failure.
436 */
Al Viro8c9379e2011-12-08 20:18:57 -0500437char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100438{
439 while (s <= p) {
440 char c = *p++;
441 if (!c) {
442 return s;
443 } else if (!strchr(esc, c)) {
444 *s++ = c;
445 } else if (s + 4 > p) {
446 break;
447 } else {
448 *s++ = '\\';
449 *s++ = '0' + ((c & 0300) >> 6);
450 *s++ = '0' + ((c & 070) >> 3);
451 *s++ = '0' + (c & 07);
452 }
453 }
454 return NULL;
455}
Ingo Molnar604094f2008-11-28 18:03:22 +0100456EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100457
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800458/**
459 * seq_path - seq_file interface to print a pathname
460 * @m: the seq_file handle
461 * @path: the struct path to print
462 * @esc: set of characters to escape in the output
463 *
464 * return the absolute path of 'path', as represented by the
465 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100466 */
Al Viro8c9379e2011-12-08 20:18:57 -0500467int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Miklos Szeredif8439802009-09-21 14:48:36 +0200469 char *buf;
470 size_t size = seq_get_buf(m, &buf);
471 int res = -1;
472
473 if (size) {
474 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200476 char *end = mangle_path(buf, p, esc);
477 if (end)
478 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200481 seq_commit(m, res);
482
483 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485EXPORT_SYMBOL(seq_path);
486
Miklos Szeredi2726d562015-06-19 10:30:28 +0200487/**
488 * seq_file_path - seq_file interface to print a pathname of a file
489 * @m: the seq_file handle
490 * @file: the struct file to print
491 * @esc: set of characters to escape in the output
492 *
493 * return the absolute path to the file.
494 */
495int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
496{
497 return seq_path(m, &file->f_path, esc);
498}
499EXPORT_SYMBOL(seq_file_path);
500
Ram Pai6092d042008-03-27 13:06:20 +0100501/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100502 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100503 */
Al Viro8c9379e2011-12-08 20:18:57 -0500504int seq_path_root(struct seq_file *m, const struct path *path,
505 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100506{
Miklos Szeredif8439802009-09-21 14:48:36 +0200507 char *buf;
508 size_t size = seq_get_buf(m, &buf);
509 int res = -ENAMETOOLONG;
510
511 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100512 char *p;
513
Miklos Szeredif8439802009-09-21 14:48:36 +0200514 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500515 if (!p)
516 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200517 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100518 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200519 char *end = mangle_path(buf, p, esc);
520 if (end)
521 res = end - buf;
522 else
523 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100524 }
525 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200526 seq_commit(m, res);
527
Al Viro02125a82011-12-05 08:43:34 -0500528 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100529}
530
531/*
Ram Pai6092d042008-03-27 13:06:20 +0100532 * returns the path of the 'dentry' from the root of its filesystem.
533 */
Al Viro8c9379e2011-12-08 20:18:57 -0500534int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100535{
Miklos Szeredif8439802009-09-21 14:48:36 +0200536 char *buf;
537 size_t size = seq_get_buf(m, &buf);
538 int res = -1;
539
540 if (size) {
541 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100542 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200543 char *end = mangle_path(buf, p, esc);
544 if (end)
545 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100546 }
547 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200548 seq_commit(m, res);
549
550 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100551}
Omar Sandovalc8d3fe02015-05-18 02:16:31 -0700552EXPORT_SYMBOL(seq_dentry);
Ram Pai6092d042008-03-27 13:06:20 +0100553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static void *single_start(struct seq_file *p, loff_t *pos)
555{
556 return NULL + (*pos == 0);
557}
558
559static void *single_next(struct seq_file *p, void *v, loff_t *pos)
560{
561 ++*pos;
562 return NULL;
563}
564
565static void single_stop(struct seq_file *p, void *v)
566{
567}
568
569int single_open(struct file *file, int (*show)(struct seq_file *, void *),
570 void *data)
571{
572 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
573 int res = -ENOMEM;
574
575 if (op) {
576 op->start = single_start;
577 op->next = single_next;
578 op->stop = single_stop;
579 op->show = show;
580 res = seq_open(file, op);
581 if (!res)
582 ((struct seq_file *)file->private_data)->private = data;
583 else
584 kfree(op);
585 }
586 return res;
587}
588EXPORT_SYMBOL(single_open);
589
Al Viro2043f492013-03-31 13:43:23 -0400590int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
591 void *data, size_t size)
592{
Heiko Carstens058504e2014-07-02 15:22:37 -0700593 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400594 int ret;
595 if (!buf)
596 return -ENOMEM;
597 ret = single_open(file, show, data);
598 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700599 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400600 return ret;
601 }
602 ((struct seq_file *)file->private_data)->buf = buf;
603 ((struct seq_file *)file->private_data)->size = size;
604 return 0;
605}
606EXPORT_SYMBOL(single_open_size);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608int single_release(struct inode *inode, struct file *file)
609{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800610 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int res = seq_release(inode, file);
612 kfree(op);
613 return res;
614}
615EXPORT_SYMBOL(single_release);
616
617int seq_release_private(struct inode *inode, struct file *file)
618{
619 struct seq_file *seq = file->private_data;
620
621 kfree(seq->private);
622 seq->private = NULL;
623 return seq_release(inode, file);
624}
625EXPORT_SYMBOL(seq_release_private);
626
Pavel Emelyanov39699032007-10-10 02:28:42 -0700627void *__seq_open_private(struct file *f, const struct seq_operations *ops,
628 int psize)
629{
630 int rc;
631 void *private;
632 struct seq_file *seq;
633
634 private = kzalloc(psize, GFP_KERNEL);
635 if (private == NULL)
636 goto out;
637
638 rc = seq_open(f, ops);
639 if (rc < 0)
640 goto out_free;
641
642 seq = f->private_data;
643 seq->private = private;
644 return private;
645
646out_free:
647 kfree(private);
648out:
649 return NULL;
650}
651EXPORT_SYMBOL(__seq_open_private);
652
653int seq_open_private(struct file *filp, const struct seq_operations *ops,
654 int psize)
655{
656 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
657}
658EXPORT_SYMBOL(seq_open_private);
659
Joe Perches6798a8c2015-09-11 13:07:48 -0700660void seq_putc(struct seq_file *m, char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Joe Perches6798a8c2015-09-11 13:07:48 -0700662 if (m->count >= m->size)
663 return;
664
665 m->buf[m->count++] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667EXPORT_SYMBOL(seq_putc);
668
Joe Perches6798a8c2015-09-11 13:07:48 -0700669void seq_puts(struct seq_file *m, const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 int len = strlen(s);
Joe Perches6798a8c2015-09-11 13:07:48 -0700672
673 if (m->count + len >= m->size) {
674 seq_set_overflow(m);
675 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Joe Perches6798a8c2015-09-11 13:07:48 -0700677 memcpy(m->buf + m->count, s, len);
678 m->count += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700681
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700682/*
683 * A helper routine for putting decimal numbers without rich format of printf().
684 * only 'unsigned long long' is supported.
Joe Perches75ba1d02016-10-07 17:02:20 -0700685 * This routine will put strlen(delimiter) + number into seq_file.
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700686 * This routine is very quick when you show lots of numbers.
687 * In usual cases, it will be better to use seq_printf(). It's easier to read.
688 */
Joe Perches75ba1d02016-10-07 17:02:20 -0700689void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
Joe Perches6798a8c2015-09-11 13:07:48 -0700690 unsigned long long num)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700691{
692 int len;
693
694 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
695 goto overflow;
696
Joe Perches75ba1d02016-10-07 17:02:20 -0700697 len = strlen(delimiter);
698 if (m->count + len >= m->size)
699 goto overflow;
700
701 memcpy(m->buf + m->count, delimiter, len);
702 m->count += len;
703
704 if (m->count + 1 >= m->size)
705 goto overflow;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700706
707 if (num < 10) {
708 m->buf[m->count++] = num + '0';
Joe Perches6798a8c2015-09-11 13:07:48 -0700709 return;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700710 }
711
712 len = num_to_str(m->buf + m->count, m->size - m->count, num);
713 if (!len)
714 goto overflow;
Joe Perches75ba1d02016-10-07 17:02:20 -0700715
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700716 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700717 return;
718
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700719overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700720 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700721}
722EXPORT_SYMBOL(seq_put_decimal_ull);
723
Joe Perches75ba1d02016-10-07 17:02:20 -0700724void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700725{
Joe Perches75ba1d02016-10-07 17:02:20 -0700726 int len;
727
728 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
729 goto overflow;
730
731 len = strlen(delimiter);
732 if (m->count + len >= m->size)
733 goto overflow;
734
735 memcpy(m->buf + m->count, delimiter, len);
736 m->count += len;
737
738 if (m->count + 2 >= m->size)
739 goto overflow;
740
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700741 if (num < 0) {
Joe Perches75ba1d02016-10-07 17:02:20 -0700742 m->buf[m->count++] = '-';
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700743 num = -num;
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700744 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700745
746 if (num < 10) {
747 m->buf[m->count++] = num + '0';
748 return;
749 }
750
751 len = num_to_str(m->buf + m->count, m->size - m->count, num);
752 if (!len)
753 goto overflow;
754
755 m->count += len;
756 return;
757
758overflow:
759 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700760}
761EXPORT_SYMBOL(seq_put_decimal_ll);
762
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700763/**
764 * seq_write - write arbitrary data to buffer
765 * @seq: seq_file identifying the buffer to which data should be written
766 * @data: data address
767 * @len: number of bytes
768 *
769 * Return 0 on success, non-zero otherwise.
770 */
771int seq_write(struct seq_file *seq, const void *data, size_t len)
772{
773 if (seq->count + len < seq->size) {
774 memcpy(seq->buf + seq->count, data, len);
775 seq->count += len;
776 return 0;
777 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700778 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700779 return -1;
780}
781EXPORT_SYMBOL(seq_write);
782
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800783/**
784 * seq_pad - write padding spaces to buffer
785 * @m: seq_file identifying the buffer to which data should be written
786 * @c: the byte to append after padding if non-zero
787 */
788void seq_pad(struct seq_file *m, char c)
789{
790 int size = m->pad_until - m->count;
791 if (size > 0)
792 seq_printf(m, "%*s", size, "");
793 if (c)
794 seq_putc(m, c);
795}
796EXPORT_SYMBOL(seq_pad);
797
Andy Shevchenko37607102015-09-09 15:38:33 -0700798/* A complete analogue of print_hex_dump() */
799void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
800 int rowsize, int groupsize, const void *buf, size_t len,
801 bool ascii)
802{
803 const u8 *ptr = buf;
804 int i, linelen, remaining = len;
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800805 char *buffer;
806 size_t size;
Andy Shevchenko37607102015-09-09 15:38:33 -0700807 int ret;
808
809 if (rowsize != 16 && rowsize != 32)
810 rowsize = 16;
811
812 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
813 linelen = min(remaining, rowsize);
814 remaining -= rowsize;
815
816 switch (prefix_type) {
817 case DUMP_PREFIX_ADDRESS:
818 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
819 break;
820 case DUMP_PREFIX_OFFSET:
821 seq_printf(m, "%s%.8x: ", prefix_str, i);
822 break;
823 default:
824 seq_printf(m, "%s", prefix_str);
825 break;
826 }
827
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800828 size = seq_get_buf(m, &buffer);
Andy Shevchenko37607102015-09-09 15:38:33 -0700829 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800830 buffer, size, ascii);
831 seq_commit(m, ret < size ? ret : -1);
832
833 seq_putc(m, '\n');
Andy Shevchenko37607102015-09-09 15:38:33 -0700834 }
835}
836EXPORT_SYMBOL(seq_hex_dump);
837
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700838struct list_head *seq_list_start(struct list_head *head, loff_t pos)
839{
840 struct list_head *lh;
841
842 list_for_each(lh, head)
843 if (pos-- == 0)
844 return lh;
845
846 return NULL;
847}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700848EXPORT_SYMBOL(seq_list_start);
849
850struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
851{
852 if (!pos)
853 return head;
854
855 return seq_list_start(head, pos - 1);
856}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700857EXPORT_SYMBOL(seq_list_start_head);
858
859struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
860{
861 struct list_head *lh;
862
863 lh = ((struct list_head *)v)->next;
864 ++*ppos;
865 return lh == head ? NULL : lh;
866}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700867EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000868
869/**
870 * seq_hlist_start - start an iteration of a hlist
871 * @head: the head of the hlist
872 * @pos: the start position of the sequence
873 *
874 * Called at seq_file->op->start().
875 */
876struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
877{
878 struct hlist_node *node;
879
880 hlist_for_each(node, head)
881 if (pos-- == 0)
882 return node;
883 return NULL;
884}
885EXPORT_SYMBOL(seq_hlist_start);
886
887/**
888 * seq_hlist_start_head - start an iteration of a hlist
889 * @head: the head of the hlist
890 * @pos: the start position of the sequence
891 *
892 * Called at seq_file->op->start(). Call this function if you want to
893 * print a header at the top of the output.
894 */
895struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
896{
897 if (!pos)
898 return SEQ_START_TOKEN;
899
900 return seq_hlist_start(head, pos - 1);
901}
902EXPORT_SYMBOL(seq_hlist_start_head);
903
904/**
905 * seq_hlist_next - move to the next position of the hlist
906 * @v: the current iterator
907 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800908 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000909 *
910 * Called at seq_file->op->next().
911 */
912struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
913 loff_t *ppos)
914{
915 struct hlist_node *node = v;
916
917 ++*ppos;
918 if (v == SEQ_START_TOKEN)
919 return head->first;
920 else
921 return node->next;
922}
923EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000924
925/**
926 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
927 * @head: the head of the hlist
928 * @pos: the start position of the sequence
929 *
930 * Called at seq_file->op->start().
931 *
932 * This list-traversal primitive may safely run concurrently with
933 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
934 * as long as the traversal is guarded by rcu_read_lock().
935 */
936struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
937 loff_t pos)
938{
939 struct hlist_node *node;
940
941 __hlist_for_each_rcu(node, head)
942 if (pos-- == 0)
943 return node;
944 return NULL;
945}
946EXPORT_SYMBOL(seq_hlist_start_rcu);
947
948/**
949 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
950 * @head: the head of the hlist
951 * @pos: the start position of the sequence
952 *
953 * Called at seq_file->op->start(). Call this function if you want to
954 * print a header at the top of the output.
955 *
956 * This list-traversal primitive may safely run concurrently with
957 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
958 * as long as the traversal is guarded by rcu_read_lock().
959 */
960struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
961 loff_t pos)
962{
963 if (!pos)
964 return SEQ_START_TOKEN;
965
966 return seq_hlist_start_rcu(head, pos - 1);
967}
968EXPORT_SYMBOL(seq_hlist_start_head_rcu);
969
970/**
971 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
972 * @v: the current iterator
973 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800974 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000975 *
976 * Called at seq_file->op->next().
977 *
978 * This list-traversal primitive may safely run concurrently with
979 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
980 * as long as the traversal is guarded by rcu_read_lock().
981 */
982struct hlist_node *seq_hlist_next_rcu(void *v,
983 struct hlist_head *head,
984 loff_t *ppos)
985{
986 struct hlist_node *node = v;
987
988 ++*ppos;
989 if (v == SEQ_START_TOKEN)
990 return rcu_dereference(head->first);
991 else
992 return rcu_dereference(node->next);
993}
994EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400995
996/**
997 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
998 * @head: pointer to percpu array of struct hlist_heads
999 * @cpu: pointer to cpu "cursor"
1000 * @pos: start position of sequence
1001 *
1002 * Called at seq_file->op->start().
1003 */
1004struct hlist_node *
1005seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1006{
1007 struct hlist_node *node;
1008
1009 for_each_possible_cpu(*cpu) {
1010 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1011 if (pos-- == 0)
1012 return node;
1013 }
1014 }
1015 return NULL;
1016}
1017EXPORT_SYMBOL(seq_hlist_start_percpu);
1018
1019/**
1020 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1021 * @v: pointer to current hlist_node
1022 * @head: pointer to percpu array of struct hlist_heads
1023 * @cpu: pointer to cpu "cursor"
1024 * @pos: start position of sequence
1025 *
1026 * Called at seq_file->op->next().
1027 */
1028struct hlist_node *
1029seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1030 int *cpu, loff_t *pos)
1031{
1032 struct hlist_node *node = v;
1033
1034 ++*pos;
1035
1036 if (node->next)
1037 return node->next;
1038
1039 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1040 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1041 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1042
1043 if (!hlist_empty(bucket))
1044 return bucket->first;
1045 }
1046 return NULL;
1047}
1048EXPORT_SYMBOL(seq_hlist_next_percpu);