~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/seq_file.c

Version: ~ [ linux-5.7 ] ~ [ linux-5.6.15 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.43 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.125 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.182 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.225 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.225 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.84 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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>
  9 #include <linux/export.h>
 10 #include <linux/seq_file.h>
 11 #include <linux/slab.h>
 12 #include <linux/cred.h>
 13 
 14 #include <asm/uaccess.h>
 15 #include <asm/page.h>
 16 
 17 
 18 /*
 19  * seq_files have a buffer which can may overflow. When this happens a larger
 20  * buffer is reallocated and all the data will be printed again.
 21  * The overflow state is true when m->count == m->size.
 22  */
 23 static bool seq_overflow(struct seq_file *m)
 24 {
 25         return m->count == m->size;
 26 }
 27 
 28 static void seq_set_overflow(struct seq_file *m)
 29 {
 30         m->count = m->size;
 31 }
 32 
 33 /**
 34  *      seq_open -      initialize sequential file
 35  *      @file: file we initialize
 36  *      @op: method table describing the sequence
 37  *
 38  *      seq_open() sets @file, associating it with a sequence described
 39  *      by @op.  @op->start() sets the iterator up and returns the first
 40  *      element of sequence. @op->stop() shuts it down.  @op->next()
 41  *      returns the next element of sequence.  @op->show() prints element
 42  *      into the buffer.  In case of error ->start() and ->next() return
 43  *      ERR_PTR(error).  In the end of sequence they return %NULL. ->show()
 44  *      returns 0 in case of success and negative number in case of error.
 45  *      Returning SEQ_SKIP means "discard this element and move on".
 46  */
 47 int seq_open(struct file *file, const struct seq_operations *op)
 48 {
 49         struct seq_file *p = file->private_data;
 50 
 51         if (!p) {
 52                 p = kmalloc(sizeof(*p), GFP_KERNEL);
 53                 if (!p)
 54                         return -ENOMEM;
 55                 file->private_data = p;
 56         }
 57         memset(p, 0, sizeof(*p));
 58         mutex_init(&p->lock);
 59         p->op = op;
 60 #ifdef CONFIG_USER_NS
 61         p->user_ns = file->f_cred->user_ns;
 62 #endif
 63 
 64         /*
 65          * Wrappers around seq_open(e.g. swaps_open) need to be
 66          * aware of this. If they set f_version themselves, they
 67          * should call seq_open first and then set f_version.
 68          */
 69         file->f_version = 0;
 70 
 71         /*
 72          * seq_files support lseek() and pread().  They do not implement
 73          * write() at all, but we clear FMODE_PWRITE here for historical
 74          * reasons.
 75          *
 76          * If a client of seq_files a) implements file.write() and b) wishes to
 77          * support pwrite() then that client will need to implement its own
 78          * file.open() which calls seq_open() and then sets FMODE_PWRITE.
 79          */
 80         file->f_mode &= ~FMODE_PWRITE;
 81         return 0;
 82 }
 83 EXPORT_SYMBOL(seq_open);
 84 
 85 static int traverse(struct seq_file *m, loff_t offset)
 86 {
 87         loff_t pos = 0, index;
 88         int error = 0;
 89         void *p;
 90 
 91         m->version = 0;
 92         index = 0;
 93         m->count = m->from = 0;
 94         if (!offset) {
 95                 m->index = index;
 96                 return 0;
 97         }
 98         if (!m->buf) {
 99                 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
100                 if (!m->buf)
101                         return -ENOMEM;
102         }
103         p = m->op->start(m, &index);
104         while (p) {
105                 error = PTR_ERR(p);
106                 if (IS_ERR(p))
107                         break;
108                 error = m->op->show(m, p);
109                 if (error < 0)
110                         break;
111                 if (unlikely(error)) {
112                         error = 0;
113                         m->count = 0;
114                 }
115                 if (seq_overflow(m))
116                         goto Eoverflow;
117                 if (pos + m->count > offset) {
118                         m->from = offset - pos;
119                         m->count -= m->from;
120                         m->index = index;
121                         break;
122                 }
123                 pos += m->count;
124                 m->count = 0;
125                 if (pos == offset) {
126                         index++;
127                         m->index = index;
128                         break;
129                 }
130                 p = m->op->next(m, p, &index);
131         }
132         m->op->stop(m, p);
133         m->index = index;
134         return error;
135 
136 Eoverflow:
137         m->op->stop(m, p);
138         kfree(m->buf);
139         m->count = 0;
140         m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
141         return !m->buf ? -ENOMEM : -EAGAIN;
142 }
143 
144 /**
145  *      seq_read -      ->read() method for sequential files.
146  *      @file: the file to read from
147  *      @buf: the buffer to read to
148  *      @size: the maximum number of bytes to read
149  *      @ppos: the current position in the file
150  *
151  *      Ready-made ->f_op->read()
152  */
153 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
154 {
155         struct seq_file *m = file->private_data;
156         size_t copied = 0;
157         loff_t pos;
158         size_t n;
159         void *p;
160         int err = 0;
161 
162         mutex_lock(&m->lock);
163 
164         /*
165          * seq_file->op->..m_start/m_stop/m_next may do special actions
166          * or optimisations based on the file->f_version, so we want to
167          * pass the file->f_version to those methods.
168          *
169          * seq_file->version is just copy of f_version, and seq_file
170          * methods can treat it simply as file version.
171          * It is copied in first and copied out after all operations.
172          * It is convenient to have it as  part of structure to avoid the
173          * need of passing another argument to all the seq_file methods.
174          */
175         m->version = file->f_version;
176 
177         /* Don't assume *ppos is where we left it */
178         if (unlikely(*ppos != m->read_pos)) {
179                 while ((err = traverse(m, *ppos)) == -EAGAIN)
180                         ;
181                 if (err) {
182                         /* With prejudice... */
183                         m->read_pos = 0;
184                         m->version = 0;
185                         m->index = 0;
186                         m->count = 0;
187                         goto Done;
188                 } else {
189                         m->read_pos = *ppos;
190                 }
191         }
192 
193         /* grab buffer if we didn't have one */
194         if (!m->buf) {
195                 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
196                 if (!m->buf)
197                         goto Enomem;
198         }
199         /* if not empty - flush it first */
200         if (m->count) {
201                 n = min(m->count, size);
202                 err = copy_to_user(buf, m->buf + m->from, n);
203                 if (err)
204                         goto Efault;
205                 m->count -= n;
206                 m->from += n;
207                 size -= n;
208                 buf += n;
209                 copied += n;
210                 if (!m->count) {
211                         m->from = 0;
212                         m->index++;
213                 }
214                 if (!size)
215                         goto Done;
216         }
217         /* we need at least one record in buffer */
218         pos = m->index;
219         p = m->op->start(m, &pos);
220         while (1) {
221                 err = PTR_ERR(p);
222                 if (!p || IS_ERR(p))
223                         break;
224                 err = m->op->show(m, p);
225                 if (err < 0)
226                         break;
227                 if (unlikely(err))
228                         m->count = 0;
229                 if (unlikely(!m->count)) {
230                         p = m->op->next(m, p, &pos);
231                         m->index = pos;
232                         continue;
233                 }
234                 if (m->count < m->size)
235                         goto Fill;
236                 m->op->stop(m, p);
237                 kfree(m->buf);
238                 m->count = 0;
239                 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
240                 if (!m->buf)
241                         goto Enomem;
242                 m->version = 0;
243                 pos = m->index;
244                 p = m->op->start(m, &pos);
245         }
246         m->op->stop(m, p);
247         m->count = 0;
248         goto Done;
249 Fill:
250         /* they want more? let's try to get some more */
251         while (m->count < size) {
252                 size_t offs = m->count;
253                 loff_t next = pos;
254                 p = m->op->next(m, p, &next);
255                 if (!p || IS_ERR(p)) {
256                         err = PTR_ERR(p);
257                         break;
258                 }
259                 err = m->op->show(m, p);
260                 if (seq_overflow(m) || err) {
261                         m->count = offs;
262                         if (likely(err <= 0))
263                                 break;
264                 }
265                 pos = next;
266         }
267         m->op->stop(m, p);
268         n = min(m->count, size);
269         err = copy_to_user(buf, m->buf, n);
270         if (err)
271                 goto Efault;
272         copied += n;
273         m->count -= n;
274         if (m->count)
275                 m->from = n;
276         else
277                 pos++;
278         m->index = pos;
279 Done:
280         if (!copied)
281                 copied = err;
282         else {
283                 *ppos += copied;
284                 m->read_pos += copied;
285         }
286         file->f_version = m->version;
287         mutex_unlock(&m->lock);
288         return copied;
289 Enomem:
290         err = -ENOMEM;
291         goto Done;
292 Efault:
293         err = -EFAULT;
294         goto Done;
295 }
296 EXPORT_SYMBOL(seq_read);
297 
298 /**
299  *      seq_lseek -     ->llseek() method for sequential files.
300  *      @file: the file in question
301  *      @offset: new position
302  *      @whence: 0 for absolute, 1 for relative position
303  *
304  *      Ready-made ->f_op->llseek()
305  */
306 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
307 {
308         struct seq_file *m = file->private_data;
309         loff_t retval = -EINVAL;
310 
311         mutex_lock(&m->lock);
312         m->version = file->f_version;
313         switch (whence) {
314         case SEEK_CUR:
315                 offset += file->f_pos;
316         case SEEK_SET:
317                 if (offset < 0)
318                         break;
319                 retval = offset;
320                 if (offset != m->read_pos) {
321                         while ((retval = traverse(m, offset)) == -EAGAIN)
322                                 ;
323                         if (retval) {
324                                 /* with extreme prejudice... */
325                                 file->f_pos = 0;
326                                 m->read_pos = 0;
327                                 m->version = 0;
328                                 m->index = 0;
329                                 m->count = 0;
330                         } else {
331                                 m->read_pos = offset;
332                                 retval = file->f_pos = offset;
333                         }
334                 } else {
335                         file->f_pos = offset;
336                 }
337         }
338         file->f_version = m->version;
339         mutex_unlock(&m->lock);
340         return retval;
341 }
342 EXPORT_SYMBOL(seq_lseek);
343 
344 /**
345  *      seq_release -   free the structures associated with sequential file.
346  *      @file: file in question
347  *      @inode: its inode
348  *
349  *      Frees the structures associated with sequential file; can be used
350  *      as ->f_op->release() if you don't have private data to destroy.
351  */
352 int seq_release(struct inode *inode, struct file *file)
353 {
354         struct seq_file *m = file->private_data;
355         kfree(m->buf);
356         kfree(m);
357         return 0;
358 }
359 EXPORT_SYMBOL(seq_release);
360 
361 /**
362  *      seq_escape -    print string into buffer, escaping some characters
363  *      @m:     target buffer
364  *      @s:     string
365  *      @esc:   set of characters that need escaping
366  *
367  *      Puts string into buffer, replacing each occurrence of character from
368  *      @esc with usual octal escape.  Returns 0 in case of success, -1 - in
369  *      case of overflow.
370  */
371 int seq_escape(struct seq_file *m, const char *s, const char *esc)
372 {
373         char *end = m->buf + m->size;
374         char *p;
375         char c;
376 
377         for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
378                 if (!strchr(esc, c)) {
379                         *p++ = c;
380                         continue;
381                 }
382                 if (p + 3 < end) {
383                         *p++ = '\\';
384                         *p++ = '' + ((c & 0300) >> 6);
385                         *p++ = '' + ((c & 070) >> 3);
386                         *p++ = '' + (c & 07);
387                         continue;
388                 }
389                 seq_set_overflow(m);
390                 return -1;
391         }
392         m->count = p - m->buf;
393         return 0;
394 }
395 EXPORT_SYMBOL(seq_escape);
396 
397 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
398 {
399         int len;
400 
401         if (m->count < m->size) {
402                 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
403                 if (m->count + len < m->size) {
404                         m->count += len;
405                         return 0;
406                 }
407         }
408         seq_set_overflow(m);
409         return -1;
410 }
411 EXPORT_SYMBOL(seq_vprintf);
412 
413 int seq_printf(struct seq_file *m, const char *f, ...)
414 {
415         int ret;
416         va_list args;
417 
418         va_start(args, f);
419         ret = seq_vprintf(m, f, args);
420         va_end(args);
421 
422         return ret;
423 }
424 EXPORT_SYMBOL(seq_printf);
425 
426 /**
427  *      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
431  *
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  */
437 char *mangle_path(char *s, const char *p, const char *esc)
438 {
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++ = '' + ((c & 0300) >> 6);
450                         *s++ = '' + ((c & 070) >> 3);
451                         *s++ = '' + (c & 07);
452                 }
453         }
454         return NULL;
455 }
456 EXPORT_SYMBOL(mangle_path);
457 
458 /**
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.
466  */
467 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
468 {
469         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);
475                 if (!IS_ERR(p)) {
476                         char *end = mangle_path(buf, p, esc);
477                         if (end)
478                                 res = end - buf;
479                 }
480         }
481         seq_commit(m, res);
482 
483         return res;
484 }
485 EXPORT_SYMBOL(seq_path);
486 
487 /*
488  * Same as seq_path, but relative to supplied root.
489  */
490 int seq_path_root(struct seq_file *m, const struct path *path,
491                   const struct path *root, const char *esc)
492 {
493         char *buf;
494         size_t size = seq_get_buf(m, &buf);
495         int res = -ENAMETOOLONG;
496 
497         if (size) {
498                 char *p;
499 
500                 p = __d_path(path, root, buf, size);
501                 if (!p)
502                         return SEQ_SKIP;
503                 res = PTR_ERR(p);
504                 if (!IS_ERR(p)) {
505                         char *end = mangle_path(buf, p, esc);
506                         if (end)
507                                 res = end - buf;
508                         else
509                                 res = -ENAMETOOLONG;
510                 }
511         }
512         seq_commit(m, res);
513 
514         return res < 0 && res != -ENAMETOOLONG ? res : 0;
515 }
516 
517 /*
518  * returns the path of the 'dentry' from the root of its filesystem.
519  */
520 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
521 {
522         char *buf;
523         size_t size = seq_get_buf(m, &buf);
524         int res = -1;
525 
526         if (size) {
527                 char *p = dentry_path(dentry, buf, size);
528                 if (!IS_ERR(p)) {
529                         char *end = mangle_path(buf, p, esc);
530                         if (end)
531                                 res = end - buf;
532                 }
533         }
534         seq_commit(m, res);
535 
536         return res;
537 }
538 
539 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
540                                    unsigned int nr_bits)
541 {
542         if (m->count < m->size) {
543                 int len = bitmap_scnprintf(m->buf + m->count,
544                                 m->size - m->count, bits, nr_bits);
545                 if (m->count + len < m->size) {
546                         m->count += len;
547                         return 0;
548                 }
549         }
550         seq_set_overflow(m);
551         return -1;
552 }
553 EXPORT_SYMBOL(seq_bitmap);
554 
555 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
556                 unsigned int nr_bits)
557 {
558         if (m->count < m->size) {
559                 int len = bitmap_scnlistprintf(m->buf + m->count,
560                                 m->size - m->count, bits, nr_bits);
561                 if (m->count + len < m->size) {
562                         m->count += len;
563                         return 0;
564                 }
565         }
566         seq_set_overflow(m);
567         return -1;
568 }
569 EXPORT_SYMBOL(seq_bitmap_list);
570 
571 static void *single_start(struct seq_file *p, loff_t *pos)
572 {
573         return NULL + (*pos == 0);
574 }
575 
576 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
577 {
578         ++*pos;
579         return NULL;
580 }
581 
582 static void single_stop(struct seq_file *p, void *v)
583 {
584 }
585 
586 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
587                 void *data)
588 {
589         struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
590         int res = -ENOMEM;
591 
592         if (op) {
593                 op->start = single_start;
594                 op->next = single_next;
595                 op->stop = single_stop;
596                 op->show = show;
597                 res = seq_open(file, op);
598                 if (!res)
599                         ((struct seq_file *)file->private_data)->private = data;
600                 else
601                         kfree(op);
602         }
603         return res;
604 }
605 EXPORT_SYMBOL(single_open);
606 
607 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
608                 void *data, size_t size)
609 {
610         char *buf = kmalloc(size, GFP_KERNEL);
611         int ret;
612         if (!buf)
613                 return -ENOMEM;
614         ret = single_open(file, show, data);
615         if (ret) {
616                 kfree(buf);
617                 return ret;
618         }
619         ((struct seq_file *)file->private_data)->buf = buf;
620         ((struct seq_file *)file->private_data)->size = size;
621         return 0;
622 }
623 EXPORT_SYMBOL(single_open_size);
624 
625 int single_release(struct inode *inode, struct file *file)
626 {
627         const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
628         int res = seq_release(inode, file);
629         kfree(op);
630         return res;
631 }
632 EXPORT_SYMBOL(single_release);
633 
634 int seq_release_private(struct inode *inode, struct file *file)
635 {
636         struct seq_file *seq = file->private_data;
637 
638         kfree(seq->private);
639         seq->private = NULL;
640         return seq_release(inode, file);
641 }
642 EXPORT_SYMBOL(seq_release_private);
643 
644 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
645                 int psize)
646 {
647         int rc;
648         void *private;
649         struct seq_file *seq;
650 
651         private = kzalloc(psize, GFP_KERNEL);
652         if (private == NULL)
653                 goto out;
654 
655         rc = seq_open(f, ops);
656         if (rc < 0)
657                 goto out_free;
658 
659         seq = f->private_data;
660         seq->private = private;
661         return private;
662 
663 out_free:
664         kfree(private);
665 out:
666         return NULL;
667 }
668 EXPORT_SYMBOL(__seq_open_private);
669 
670 int seq_open_private(struct file *filp, const struct seq_operations *ops,
671                 int psize)
672 {
673         return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
674 }
675 EXPORT_SYMBOL(seq_open_private);
676 
677 int seq_putc(struct seq_file *m, char c)
678 {
679         if (m->count < m->size) {
680                 m->buf[m->count++] = c;
681                 return 0;
682         }
683         return -1;
684 }
685 EXPORT_SYMBOL(seq_putc);
686 
687 int seq_puts(struct seq_file *m, const char *s)
688 {
689         int len = strlen(s);
690         if (m->count + len < m->size) {
691                 memcpy(m->buf + m->count, s, len);
692                 m->count += len;
693                 return 0;
694         }
695         seq_set_overflow(m);
696         return -1;
697 }
698 EXPORT_SYMBOL(seq_puts);
699 
700 /*
701  * A helper routine for putting decimal numbers without rich format of printf().
702  * only 'unsigned long long' is supported.
703  * This routine will put one byte delimiter + number into seq_file.
704  * This routine is very quick when you show lots of numbers.
705  * In usual cases, it will be better to use seq_printf(). It's easier to read.
706  */
707 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
708                         unsigned long long num)
709 {
710         int len;
711 
712         if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
713                 goto overflow;
714 
715         if (delimiter)
716                 m->buf[m->count++] = delimiter;
717 
718         if (num < 10) {
719                 m->buf[m->count++] = num + '';
720                 return 0;
721         }
722 
723         len = num_to_str(m->buf + m->count, m->size - m->count, num);
724         if (!len)
725                 goto overflow;
726         m->count += len;
727         return 0;
728 overflow:
729         seq_set_overflow(m);
730         return -1;
731 }
732 EXPORT_SYMBOL(seq_put_decimal_ull);
733 
734 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
735                         long long num)
736 {
737         if (num < 0) {
738                 if (m->count + 3 >= m->size) {
739                         seq_set_overflow(m);
740                         return -1;
741                 }
742                 if (delimiter)
743                         m->buf[m->count++] = delimiter;
744                 num = -num;
745                 delimiter = '-';
746         }
747         return seq_put_decimal_ull(m, delimiter, num);
748 
749 }
750 EXPORT_SYMBOL(seq_put_decimal_ll);
751 
752 /**
753  * seq_write - write arbitrary data to buffer
754  * @seq: seq_file identifying the buffer to which data should be written
755  * @data: data address
756  * @len: number of bytes
757  *
758  * Return 0 on success, non-zero otherwise.
759  */
760 int seq_write(struct seq_file *seq, const void *data, size_t len)
761 {
762         if (seq->count + len < seq->size) {
763                 memcpy(seq->buf + seq->count, data, len);
764                 seq->count += len;
765                 return 0;
766         }
767         seq_set_overflow(seq);
768         return -1;
769 }
770 EXPORT_SYMBOL(seq_write);
771 
772 /**
773  * seq_pad - write padding spaces to buffer
774  * @m: seq_file identifying the buffer to which data should be written
775  * @c: the byte to append after padding if non-zero
776  */
777 void seq_pad(struct seq_file *m, char c)
778 {
779         int size = m->pad_until - m->count;
780         if (size > 0)
781                 seq_printf(m, "%*s", size, "");
782         if (c)
783                 seq_putc(m, c);
784 }
785 EXPORT_SYMBOL(seq_pad);
786 
787 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
788 {
789         struct list_head *lh;
790 
791         list_for_each(lh, head)
792                 if (pos-- == 0)
793                         return lh;
794 
795         return NULL;
796 }
797 EXPORT_SYMBOL(seq_list_start);
798 
799 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
800 {
801         if (!pos)
802                 return head;
803 
804         return seq_list_start(head, pos - 1);
805 }
806 EXPORT_SYMBOL(seq_list_start_head);
807 
808 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
809 {
810         struct list_head *lh;
811 
812         lh = ((struct list_head *)v)->next;
813         ++*ppos;
814         return lh == head ? NULL : lh;
815 }
816 EXPORT_SYMBOL(seq_list_next);
817 
818 /**
819  * seq_hlist_start - start an iteration of a hlist
820  * @head: the head of the hlist
821  * @pos:  the start position of the sequence
822  *
823  * Called at seq_file->op->start().
824  */
825 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
826 {
827         struct hlist_node *node;
828 
829         hlist_for_each(node, head)
830                 if (pos-- == 0)
831                         return node;
832         return NULL;
833 }
834 EXPORT_SYMBOL(seq_hlist_start);
835 
836 /**
837  * seq_hlist_start_head - start an iteration of a hlist
838  * @head: the head of the hlist
839  * @pos:  the start position of the sequence
840  *
841  * Called at seq_file->op->start(). Call this function if you want to
842  * print a header at the top of the output.
843  */
844 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
845 {
846         if (!pos)
847                 return SEQ_START_TOKEN;
848 
849         return seq_hlist_start(head, pos - 1);
850 }
851 EXPORT_SYMBOL(seq_hlist_start_head);
852 
853 /**
854  * seq_hlist_next - move to the next position of the hlist
855  * @v:    the current iterator
856  * @head: the head of the hlist
857  * @ppos: the current position
858  *
859  * Called at seq_file->op->next().
860  */
861 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
862                                   loff_t *ppos)
863 {
864         struct hlist_node *node = v;
865 
866         ++*ppos;
867         if (v == SEQ_START_TOKEN)
868                 return head->first;
869         else
870                 return node->next;
871 }
872 EXPORT_SYMBOL(seq_hlist_next);
873 
874 /**
875  * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
876  * @head: the head of the hlist
877  * @pos:  the start position of the sequence
878  *
879  * Called at seq_file->op->start().
880  *
881  * This list-traversal primitive may safely run concurrently with
882  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
883  * as long as the traversal is guarded by rcu_read_lock().
884  */
885 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
886                                        loff_t pos)
887 {
888         struct hlist_node *node;
889 
890         __hlist_for_each_rcu(node, head)
891                 if (pos-- == 0)
892                         return node;
893         return NULL;
894 }
895 EXPORT_SYMBOL(seq_hlist_start_rcu);
896 
897 /**
898  * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
899  * @head: the head of the hlist
900  * @pos:  the start position of the sequence
901  *
902  * Called at seq_file->op->start(). Call this function if you want to
903  * print a header at the top of the output.
904  *
905  * This list-traversal primitive may safely run concurrently with
906  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
907  * as long as the traversal is guarded by rcu_read_lock().
908  */
909 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
910                                             loff_t pos)
911 {
912         if (!pos)
913                 return SEQ_START_TOKEN;
914 
915         return seq_hlist_start_rcu(head, pos - 1);
916 }
917 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
918 
919 /**
920  * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
921  * @v:    the current iterator
922  * @head: the head of the hlist
923  * @ppos: the current position
924  *
925  * Called at seq_file->op->next().
926  *
927  * This list-traversal primitive may safely run concurrently with
928  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
929  * as long as the traversal is guarded by rcu_read_lock().
930  */
931 struct hlist_node *seq_hlist_next_rcu(void *v,
932                                       struct hlist_head *head,
933                                       loff_t *ppos)
934 {
935         struct hlist_node *node = v;
936 
937         ++*ppos;
938         if (v == SEQ_START_TOKEN)
939                 return rcu_dereference(head->first);
940         else
941                 return rcu_dereference(node->next);
942 }
943 EXPORT_SYMBOL(seq_hlist_next_rcu);
944 
945 /**
946  * seq_hlist_start_precpu - start an iteration of a percpu hlist array
947  * @head: pointer to percpu array of struct hlist_heads
948  * @cpu:  pointer to cpu "cursor"
949  * @pos:  start position of sequence
950  *
951  * Called at seq_file->op->start().
952  */
953 struct hlist_node *
954 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
955 {
956         struct hlist_node *node;
957 
958         for_each_possible_cpu(*cpu) {
959                 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
960                         if (pos-- == 0)
961                                 return node;
962                 }
963         }
964         return NULL;
965 }
966 EXPORT_SYMBOL(seq_hlist_start_percpu);
967 
968 /**
969  * seq_hlist_next_percpu - move to the next position of the percpu hlist array
970  * @v:    pointer to current hlist_node
971  * @head: pointer to percpu array of struct hlist_heads
972  * @cpu:  pointer to cpu "cursor"
973  * @pos:  start position of sequence
974  *
975  * Called at seq_file->op->next().
976  */
977 struct hlist_node *
978 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
979                         int *cpu, loff_t *pos)
980 {
981         struct hlist_node *node = v;
982 
983         ++*pos;
984 
985         if (node->next)
986                 return node->next;
987 
988         for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
989              *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
990                 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
991 
992                 if (!hlist_empty(bucket))
993                         return bucket->first;
994         }
995         return NULL;
996 }
997 EXPORT_SYMBOL(seq_hlist_next_percpu);
998 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp