^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/seq_file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * helper functions for making synthetic files from sequences of records.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * initial implementation -- AV, Oct 2001.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/string_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static struct kmem_cache *seq_file_cache __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static void seq_set_overflow(struct seq_file *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) m->count = m->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static void *seq_buf_alloc(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (unlikely(size > MAX_RW_COUNT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return kvmalloc(size, GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * seq_open - initialize sequential file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @file: file we initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @op: method table describing the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * seq_open() sets @file, associating it with a sequence described
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * by @op. @op->start() sets the iterator up and returns the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * element of sequence. @op->stop() shuts it down. @op->next()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * returns the next element of sequence. @op->show() prints element
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * into the buffer. In case of error ->start() and ->next() return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * returns 0 in case of success and negative number in case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Returning SEQ_SKIP means "discard this element and move on".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Note: seq_open() will allocate a struct seq_file and store its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * pointer in @file->private_data. This pointer should not be modified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int seq_open(struct file *file, const struct seq_operations *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct seq_file *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) WARN_ON(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) file->private_data = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) mutex_init(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) p->op = op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) // No refcounting: the lifetime of 'p' is constrained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) // to the lifetime of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) p->file = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * seq_files support lseek() and pread(). They do not implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * write() at all, but we clear FMODE_PWRITE here for historical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * reasons.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * If a client of seq_files a) implements file.write() and b) wishes to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * support pwrite() then that client will need to implement its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * file.open() which calls seq_open() and then sets FMODE_PWRITE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) file->f_mode &= ~FMODE_PWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) EXPORT_SYMBOL(seq_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static int traverse(struct seq_file *m, loff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) loff_t pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) m->index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) m->count = m->from = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!m->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (!m->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) p = m->op->start(m, &m->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) error = PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (IS_ERR(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) error = m->op->show(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (unlikely(error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (seq_has_overflowed(m))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) goto Eoverflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) p = m->op->next(m, p, &m->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (pos + m->count > offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) m->from = offset - pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) m->count -= m->from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) pos += m->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (pos == offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) m->op->stop(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) Eoverflow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) m->op->stop(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) kvfree(m->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) m->buf = seq_buf_alloc(m->size <<= 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return !m->buf ? -ENOMEM : -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * seq_read - ->read() method for sequential files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @file: the file to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @buf: the buffer to read to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * @size: the maximum number of bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * @ppos: the current position in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Ready-made ->f_op->read()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct iovec iov = { .iov_base = buf, .iov_len = size};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct kiocb kiocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct iov_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) init_sync_kiocb(&kiocb, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) iov_iter_init(&iter, READ, &iov, 1, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) kiocb.ki_pos = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ret = seq_read_iter(&kiocb, &iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *ppos = kiocb.ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) EXPORT_SYMBOL(seq_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Ready-made ->f_op->read_iter()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct seq_file *m = iocb->ki_filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) size_t copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (!iov_iter_count(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) mutex_lock(&m->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * if request is to read from zero offset, reset iterator to first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * record as it might have been already advanced by previous requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (iocb->ki_pos == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) m->index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* Don't assume ki_pos is where we left it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (unlikely(iocb->ki_pos != m->read_pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /* With prejudice... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) m->read_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) m->index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto Done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) m->read_pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /* grab buffer if we didn't have one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!m->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!m->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) // something left in the buffer - copy it out first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (m->count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) n = copy_to_iter(m->buf + m->from, m->count, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) m->count -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) m->from += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) copied += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (m->count) // hadn't managed to copy everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) goto Done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) // get a non-empty record in the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) m->from = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) p = m->op->start(m, &m->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) err = PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!p || IS_ERR(p)) // EOF or an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) err = m->op->show(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (err < 0) // hard error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (unlikely(err)) // ->show() says "skip it"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (unlikely(!m->count)) { // empty record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) p = m->op->next(m, p, &m->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!seq_has_overflowed(m)) // got it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) goto Fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) // need a bigger buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) m->op->stop(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) kvfree(m->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) m->buf = seq_buf_alloc(m->size <<= 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!m->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) p = m->op->start(m, &m->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) // EOF or an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) m->op->stop(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) goto Done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) Fill:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) // one non-empty record is in the buffer; if they want more,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) // try to fit more in, but in any case we need to advance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) // the iterator once for every record shown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) size_t offs = m->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) loff_t pos = m->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) p = m->op->next(m, p, &m->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (pos == m->index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) pr_info_ratelimited("buggy .next function %ps did not update position index\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) m->op->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) m->index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!p || IS_ERR(p)) // no next record for us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (m->count >= iov_iter_count(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) err = m->op->show(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (err > 0) { // ->show() says "skip it"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) m->count = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) } else if (err || seq_has_overflowed(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) m->count = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) m->op->stop(m, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) n = copy_to_iter(m->buf, m->count, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) copied += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) m->count -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) m->from = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) Done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (unlikely(!copied)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) copied = m->count ? -EFAULT : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) iocb->ki_pos += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) m->read_pos += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) mutex_unlock(&m->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) Enomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto Done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) EXPORT_SYMBOL(seq_read_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * seq_lseek - ->llseek() method for sequential files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * @file: the file in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * @offset: new position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * @whence: 0 for absolute, 1 for relative position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * Ready-made ->f_op->llseek()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) loff_t seq_lseek(struct file *file, loff_t offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct seq_file *m = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) loff_t retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) mutex_lock(&m->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) switch (whence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) case SEEK_CUR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) offset += file->f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case SEEK_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) retval = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (offset != m->read_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) while ((retval = traverse(m, offset)) == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* with extreme prejudice... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) file->f_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) m->read_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) m->index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) m->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) m->read_pos = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) retval = file->f_pos = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) file->f_pos = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) mutex_unlock(&m->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) EXPORT_SYMBOL(seq_lseek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * seq_release - free the structures associated with sequential file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * @file: file in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * @inode: its inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Frees the structures associated with sequential file; can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * as ->f_op->release() if you don't have private data to destroy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int seq_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct seq_file *m = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) kvfree(m->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) kmem_cache_free(seq_file_cache, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) EXPORT_SYMBOL(seq_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * seq_escape - print string into buffer, escaping some characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * @m: target buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * @s: string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * @esc: set of characters that need escaping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * Puts string into buffer, replacing each occurrence of character from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * @esc with usual octal escape.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * Use seq_has_overflowed() to check for errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) void seq_escape(struct seq_file *m, const char *s, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) size_t size = seq_get_buf(m, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) seq_commit(m, ret < size ? ret : -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) EXPORT_SYMBOL(seq_escape);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) size_t size = seq_get_buf(m, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ret = string_escape_mem_ascii(src, isz, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) seq_commit(m, ret < size ? ret : -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) EXPORT_SYMBOL(seq_escape_mem_ascii);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) void seq_vprintf(struct seq_file *m, const char *f, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (m->count < m->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (m->count + len < m->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) m->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) seq_set_overflow(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) EXPORT_SYMBOL(seq_vprintf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) void seq_printf(struct seq_file *m, const char *f, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) va_start(args, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) seq_vprintf(m, f, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) EXPORT_SYMBOL(seq_printf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * mangle_path - mangle and copy path to buffer beginning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * @s: buffer start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * @p: beginning of path in above buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * @esc: set of characters that need escaping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * Copy the path from @p to @s, replacing each occurrence of character from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @esc with usual octal escape.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * Returns pointer past last written character in @s, or NULL in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) char *mangle_path(char *s, const char *p, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) while (s <= p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) char c = *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (!c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) } else if (!strchr(esc, c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *s++ = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) } else if (s + 4 > p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) *s++ = '\\';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) *s++ = '0' + ((c & 0300) >> 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) *s++ = '0' + ((c & 070) >> 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *s++ = '0' + (c & 07);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) EXPORT_SYMBOL(mangle_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * seq_path - seq_file interface to print a pathname
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * @m: the seq_file handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * @path: the struct path to print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * @esc: set of characters to escape in the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * return the absolute path of 'path', as represented by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * dentry / mnt pair in the path parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) int seq_path(struct seq_file *m, const struct path *path, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) size_t size = seq_get_buf(m, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) int res = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) char *p = d_path(path, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (!IS_ERR(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) char *end = mangle_path(buf, p, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) res = end - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) seq_commit(m, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) EXPORT_SYMBOL(seq_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * seq_file_path - seq_file interface to print a pathname of a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * @m: the seq_file handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * @file: the struct file to print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * @esc: set of characters to escape in the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * return the absolute path to the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return seq_path(m, &file->f_path, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) EXPORT_SYMBOL(seq_file_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * Same as seq_path, but relative to supplied root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) int seq_path_root(struct seq_file *m, const struct path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) const struct path *root, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) size_t size = seq_get_buf(m, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) int res = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) p = __d_path(path, root, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return SEQ_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) res = PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (!IS_ERR(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) char *end = mangle_path(buf, p, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) res = end - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) res = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) seq_commit(m, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return res < 0 && res != -ENAMETOOLONG ? res : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * returns the path of the 'dentry' from the root of its filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) size_t size = seq_get_buf(m, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int res = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) char *p = dentry_path(dentry, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!IS_ERR(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) char *end = mangle_path(buf, p, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) res = end - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) seq_commit(m, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) EXPORT_SYMBOL(seq_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static void *single_start(struct seq_file *p, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return NULL + (*pos == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static void *single_next(struct seq_file *p, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static void single_stop(struct seq_file *p, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) int single_open(struct file *file, int (*show)(struct seq_file *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int res = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) op->start = single_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) op->next = single_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) op->stop = single_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) op->show = show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) res = seq_open(file, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) ((struct seq_file *)file->private_data)->private = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) kfree(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) EXPORT_SYMBOL(single_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) void *data, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) char *buf = seq_buf_alloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) ret = single_open(file, show, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) kvfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) ((struct seq_file *)file->private_data)->buf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) ((struct seq_file *)file->private_data)->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) EXPORT_SYMBOL(single_open_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) int single_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) int res = seq_release(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) kfree(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) EXPORT_SYMBOL(single_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) int seq_release_private(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) kfree(seq->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) seq->private = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return seq_release(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) EXPORT_SYMBOL(seq_release_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) void *__seq_open_private(struct file *f, const struct seq_operations *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) int psize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) void *private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) struct seq_file *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (private == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) rc = seq_open(f, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) seq = f->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) seq->private = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) kfree(private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) EXPORT_SYMBOL(__seq_open_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) int seq_open_private(struct file *filp, const struct seq_operations *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) int psize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) EXPORT_SYMBOL(seq_open_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) void seq_putc(struct seq_file *m, char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (m->count >= m->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) m->buf[m->count++] = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) EXPORT_SYMBOL(seq_putc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) void seq_puts(struct seq_file *m, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) int len = strlen(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (m->count + len >= m->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) seq_set_overflow(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) memcpy(m->buf + m->count, s, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) m->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) EXPORT_SYMBOL(seq_puts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * A helper routine for putting decimal numbers without rich format of printf().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * only 'unsigned long long' is supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * @m: seq_file identifying the buffer to which data should be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * @delimiter: a string which is printed before the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * @num: the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * @width: a minimum field width
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * This routine will put strlen(delimiter) + number into seq_filed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * This routine is very quick when you show lots of numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * In usual cases, it will be better to use seq_printf(). It's easier to read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) unsigned long long num, unsigned int width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (delimiter && delimiter[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (delimiter[1] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) seq_putc(m, delimiter[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) seq_puts(m, delimiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (!width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) width = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (m->count + width >= m->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) m->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) overflow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) seq_set_overflow(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) unsigned long long num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) return seq_put_decimal_ull_width(m, delimiter, num, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) EXPORT_SYMBOL(seq_put_decimal_ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * seq_put_hex_ll - put a number in hexadecimal notation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * @m: seq_file identifying the buffer to which data should be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * @delimiter: a string which is printed before the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * @v: the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * @width: a minimum field width
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * This routine is very quick when you show lots of numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * In usual cases, it will be better to use seq_printf(). It's easier to read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) unsigned long long v, unsigned int width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (delimiter && delimiter[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (delimiter[1] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) seq_putc(m, delimiter[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) seq_puts(m, delimiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) /* If x is 0, the result of __builtin_clzll is undefined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (v == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (len < width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) len = width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (m->count + len > m->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) seq_set_overflow(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) for (i = len - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) m->buf[m->count + i] = hex_asc[0xf & v];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) v = v >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) m->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (delimiter && delimiter[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) if (delimiter[1] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) seq_putc(m, delimiter[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) seq_puts(m, delimiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (m->count + 2 >= m->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (num < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) m->buf[m->count++] = '-';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) num = -num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (num < 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) m->buf[m->count++] = num + '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) goto overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) m->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) overflow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) seq_set_overflow(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) EXPORT_SYMBOL(seq_put_decimal_ll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * seq_write - write arbitrary data to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * @seq: seq_file identifying the buffer to which data should be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * @data: data address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * @len: number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * Return 0 on success, non-zero otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) int seq_write(struct seq_file *seq, const void *data, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (seq->count + len < seq->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) memcpy(seq->buf + seq->count, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) seq->count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) seq_set_overflow(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) EXPORT_SYMBOL(seq_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) * seq_pad - write padding spaces to buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * @m: seq_file identifying the buffer to which data should be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * @c: the byte to append after padding if non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) void seq_pad(struct seq_file *m, char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) int size = m->pad_until - m->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (size + m->count > m->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) seq_set_overflow(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) memset(m->buf + m->count, ' ', size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) m->count += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) seq_putc(m, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) EXPORT_SYMBOL(seq_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) /* A complete analogue of print_hex_dump() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) int rowsize, int groupsize, const void *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) bool ascii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) const u8 *ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) int i, linelen, remaining = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (rowsize != 16 && rowsize != 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) rowsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) linelen = min(remaining, rowsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) remaining -= rowsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) switch (prefix_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) case DUMP_PREFIX_ADDRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) seq_printf(m, "%s%p: ", prefix_str, ptr + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) case DUMP_PREFIX_OFFSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) seq_printf(m, "%s%.8x: ", prefix_str, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) seq_printf(m, "%s", prefix_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) size = seq_get_buf(m, &buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) buffer, size, ascii);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) seq_commit(m, ret < size ? ret : -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) EXPORT_SYMBOL(seq_hex_dump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) struct list_head *seq_list_start(struct list_head *head, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) struct list_head *lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) list_for_each(lh, head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) if (pos-- == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) return lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) EXPORT_SYMBOL(seq_list_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return seq_list_start(head, pos - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) EXPORT_SYMBOL(seq_list_start_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) struct list_head *lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) lh = ((struct list_head *)v)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) ++*ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) return lh == head ? NULL : lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) EXPORT_SYMBOL(seq_list_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * seq_hlist_start - start an iteration of a hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) * @head: the head of the hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * @pos: the start position of the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * Called at seq_file->op->start().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) struct hlist_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) hlist_for_each(node, head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) if (pos-- == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) EXPORT_SYMBOL(seq_hlist_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) * seq_hlist_start_head - start an iteration of a hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * @head: the head of the hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * @pos: the start position of the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) * Called at seq_file->op->start(). Call this function if you want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) * print a header at the top of the output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return seq_hlist_start(head, pos - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) EXPORT_SYMBOL(seq_hlist_start_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * seq_hlist_next - move to the next position of the hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * @v: the current iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * @head: the head of the hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * @ppos: the current position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * Called at seq_file->op->next().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) struct hlist_node *node = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) ++*ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) if (v == SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) return head->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) return node->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) EXPORT_SYMBOL(seq_hlist_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * @head: the head of the hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * @pos: the start position of the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * Called at seq_file->op->start().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * This list-traversal primitive may safely run concurrently with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) * the _rcu list-mutation primitives such as hlist_add_head_rcu()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * as long as the traversal is guarded by rcu_read_lock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) struct hlist_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) __hlist_for_each_rcu(node, head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (pos-- == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) EXPORT_SYMBOL(seq_hlist_start_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * @head: the head of the hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * @pos: the start position of the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * Called at seq_file->op->start(). Call this function if you want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * print a header at the top of the output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * This list-traversal primitive may safely run concurrently with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * the _rcu list-mutation primitives such as hlist_add_head_rcu()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) * as long as the traversal is guarded by rcu_read_lock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) return seq_hlist_start_rcu(head, pos - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) EXPORT_SYMBOL(seq_hlist_start_head_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * @v: the current iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * @head: the head of the hlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * @ppos: the current position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * Called at seq_file->op->next().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) * This list-traversal primitive may safely run concurrently with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * the _rcu list-mutation primitives such as hlist_add_head_rcu()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * as long as the traversal is guarded by rcu_read_lock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) struct hlist_node *seq_hlist_next_rcu(void *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) struct hlist_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) struct hlist_node *node = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) ++*ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) if (v == SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return rcu_dereference(head->first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) return rcu_dereference(node->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) EXPORT_SYMBOL(seq_hlist_next_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * seq_hlist_start_precpu - start an iteration of a percpu hlist array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * @head: pointer to percpu array of struct hlist_heads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * @cpu: pointer to cpu "cursor"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * @pos: start position of sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * Called at seq_file->op->start().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) struct hlist_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) struct hlist_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) for_each_possible_cpu(*cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) if (pos-- == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) EXPORT_SYMBOL(seq_hlist_start_percpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) * seq_hlist_next_percpu - move to the next position of the percpu hlist array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * @v: pointer to current hlist_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * @head: pointer to percpu array of struct hlist_heads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) * @cpu: pointer to cpu "cursor"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) * @pos: start position of sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) * Called at seq_file->op->next().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) struct hlist_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) int *cpu, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) struct hlist_node *node = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (node->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) return node->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (!hlist_empty(bucket))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) return bucket->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) EXPORT_SYMBOL(seq_hlist_next_percpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) void __init seq_file_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }