12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- #include <linux/fs.h>
- #include <linux/export.h>
- #include <linux/seq_file.h>
- #include <linux/vmalloc.h>
- #include <linux/slab.h>
- #include <linux/cred.h>
- #include <linux/mm.h>
- #include <linux/printk.h>
- #include <linux/string_helpers.h>
- #include <asm/uaccess.h>
- #include <asm/page.h>
- static void seq_set_overflow(struct seq_file *m)
- {
- m->count = m->size;
- }
- static void *seq_buf_alloc(unsigned long size)
- {
- void *buf;
- gfp_t gfp = GFP_KERNEL;
-
- if (size > PAGE_SIZE)
- gfp |= __GFP_NORETRY | __GFP_NOWARN;
- buf = kmalloc(size, gfp);
- if (!buf && size > PAGE_SIZE)
- buf = vmalloc(size);
- return buf;
- }
- int seq_open(struct file *file, const struct seq_operations *op)
- {
- struct seq_file *p;
- WARN_ON(file->private_data);
- p = kzalloc(sizeof(*p), GFP_KERNEL);
- if (!p)
- return -ENOMEM;
- file->private_data = p;
- mutex_init(&p->lock);
- p->op = op;
-
-
- p->file = file;
-
- file->f_version = 0;
-
- file->f_mode &= ~FMODE_PWRITE;
- return 0;
- }
- EXPORT_SYMBOL(seq_open);
- static int traverse(struct seq_file *m, loff_t offset)
- {
- loff_t pos = 0, index;
- int error = 0;
- void *p;
- m->version = 0;
- index = 0;
- m->count = m->from = 0;
- if (!offset) {
- m->index = index;
- return 0;
- }
- if (!m->buf) {
- m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
- if (!m->buf)
- return -ENOMEM;
- }
- p = m->op->start(m, &index);
- while (p) {
- error = PTR_ERR(p);
- if (IS_ERR(p))
- break;
- error = m->op->show(m, p);
- if (error < 0)
- break;
- if (unlikely(error)) {
- error = 0;
- m->count = 0;
- }
- if (seq_has_overflowed(m))
- goto Eoverflow;
- if (pos + m->count > offset) {
- m->from = offset - pos;
- m->count -= m->from;
- m->index = index;
- break;
- }
- pos += m->count;
- m->count = 0;
- if (pos == offset) {
- index++;
- m->index = index;
- break;
- }
- p = m->op->next(m, p, &index);
- }
- m->op->stop(m, p);
- m->index = index;
- return error;
- Eoverflow:
- m->op->stop(m, p);
- kvfree(m->buf);
- m->count = 0;
- m->buf = seq_buf_alloc(m->size <<= 1);
- return !m->buf ? -ENOMEM : -EAGAIN;
- }
- ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
- {
- struct seq_file *m = file->private_data;
- size_t copied = 0;
- loff_t pos;
- size_t n;
- void *p;
- int err = 0;
- mutex_lock(&m->lock);
-
- m->version = file->f_version;
-
- if (unlikely(*ppos != m->read_pos)) {
- while ((err = traverse(m, *ppos)) == -EAGAIN)
- ;
- if (err) {
-
- m->read_pos = 0;
- m->version = 0;
- m->index = 0;
- m->count = 0;
- goto Done;
- } else {
- m->read_pos = *ppos;
- }
- }
-
- if (!m->buf) {
- m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
- if (!m->buf)
- goto Enomem;
- }
-
- if (m->count) {
- n = min(m->count, size);
- err = copy_to_user(buf, m->buf + m->from, n);
- if (err)
- goto Efault;
- m->count -= n;
- m->from += n;
- size -= n;
- buf += n;
- copied += n;
- if (!m->count) {
- m->from = 0;
- m->index++;
- }
- if (!size)
- goto Done;
- }
-
- pos = m->index;
- p = m->op->start(m, &pos);
- while (1) {
- err = PTR_ERR(p);
- if (!p || IS_ERR(p))
- break;
- err = m->op->show(m, p);
- if (err < 0)
- break;
- if (unlikely(err))
- m->count = 0;
- if (unlikely(!m->count)) {
- p = m->op->next(m, p, &pos);
- m->index = pos;
- continue;
- }
- if (m->count < m->size)
- goto Fill;
- m->op->stop(m, p);
- kvfree(m->buf);
- m->count = 0;
- m->buf = seq_buf_alloc(m->size <<= 1);
- if (!m->buf)
- goto Enomem;
- m->version = 0;
- pos = m->index;
- p = m->op->start(m, &pos);
- }
- m->op->stop(m, p);
- m->count = 0;
- goto Done;
- Fill:
-
- while (m->count < size) {
- size_t offs = m->count;
- loff_t next = pos;
- p = m->op->next(m, p, &next);
- if (!p || IS_ERR(p)) {
- err = PTR_ERR(p);
- break;
- }
- err = m->op->show(m, p);
- if (seq_has_overflowed(m) || err) {
- m->count = offs;
- if (likely(err <= 0))
- break;
- }
- pos = next;
- }
- m->op->stop(m, p);
- n = min(m->count, size);
- err = copy_to_user(buf, m->buf, n);
- if (err)
- goto Efault;
- copied += n;
- m->count -= n;
- if (m->count)
- m->from = n;
- else
- pos++;
- m->index = pos;
- Done:
- if (!copied)
- copied = err;
- else {
- *ppos += copied;
- m->read_pos += copied;
- }
- file->f_version = m->version;
- mutex_unlock(&m->lock);
- return copied;
- Enomem:
- err = -ENOMEM;
- goto Done;
- Efault:
- err = -EFAULT;
- goto Done;
- }
- EXPORT_SYMBOL(seq_read);
- loff_t seq_lseek(struct file *file, loff_t offset, int whence)
- {
- struct seq_file *m = file->private_data;
- loff_t retval = -EINVAL;
- mutex_lock(&m->lock);
- m->version = file->f_version;
- switch (whence) {
- case SEEK_CUR:
- offset += file->f_pos;
- case SEEK_SET:
- if (offset < 0)
- break;
- retval = offset;
- if (offset != m->read_pos) {
- while ((retval = traverse(m, offset)) == -EAGAIN)
- ;
- if (retval) {
-
- file->f_pos = 0;
- m->read_pos = 0;
- m->version = 0;
- m->index = 0;
- m->count = 0;
- } else {
- m->read_pos = offset;
- retval = file->f_pos = offset;
- }
- } else {
- file->f_pos = offset;
- }
- }
- file->f_version = m->version;
- mutex_unlock(&m->lock);
- return retval;
- }
- EXPORT_SYMBOL(seq_lseek);
- int seq_release(struct inode *inode, struct file *file)
- {
- struct seq_file *m = file->private_data;
- kvfree(m->buf);
- kfree(m);
- return 0;
- }
- EXPORT_SYMBOL(seq_release);
- void seq_escape(struct seq_file *m, const char *s, const char *esc)
- {
- char *buf;
- size_t size = seq_get_buf(m, &buf);
- int ret;
- ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
- seq_commit(m, ret < size ? ret : -1);
- }
- EXPORT_SYMBOL(seq_escape);
- void seq_vprintf(struct seq_file *m, const char *f, va_list args)
- {
- int len;
- if (m->count < m->size) {
- len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
- if (m->count + len < m->size) {
- m->count += len;
- return;
- }
- }
- seq_set_overflow(m);
- }
- EXPORT_SYMBOL(seq_vprintf);
- void seq_printf(struct seq_file *m, const char *f, ...)
- {
- va_list args;
- va_start(args, f);
- seq_vprintf(m, f, args);
- va_end(args);
- }
- EXPORT_SYMBOL(seq_printf);
- char *mangle_path(char *s, const char *p, const char *esc)
- {
- while (s <= p) {
- char c = *p++;
- if (!c) {
- return s;
- } else if (!strchr(esc, c)) {
- *s++ = c;
- } else if (s + 4 > p) {
- break;
- } else {
- *s++ = '\\';
- *s++ = '0' + ((c & 0300) >> 6);
- *s++ = '0' + ((c & 070) >> 3);
- *s++ = '0' + (c & 07);
- }
- }
- return NULL;
- }
- EXPORT_SYMBOL(mangle_path);
- int seq_path(struct seq_file *m, const struct path *path, const char *esc)
- {
- char *buf;
- size_t size = seq_get_buf(m, &buf);
- int res = -1;
- if (size) {
- char *p = d_path(path, buf, size);
- if (!IS_ERR(p)) {
- char *end = mangle_path(buf, p, esc);
- if (end)
- res = end - buf;
- }
- }
- seq_commit(m, res);
- return res;
- }
- EXPORT_SYMBOL(seq_path);
- int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
- {
- return seq_path(m, &file->f_path, esc);
- }
- EXPORT_SYMBOL(seq_file_path);
- int seq_path_root(struct seq_file *m, const struct path *path,
- const struct path *root, const char *esc)
- {
- char *buf;
- size_t size = seq_get_buf(m, &buf);
- int res = -ENAMETOOLONG;
- if (size) {
- char *p;
- p = __d_path(path, root, buf, size);
- if (!p)
- return SEQ_SKIP;
- res = PTR_ERR(p);
- if (!IS_ERR(p)) {
- char *end = mangle_path(buf, p, esc);
- if (end)
- res = end - buf;
- else
- res = -ENAMETOOLONG;
- }
- }
- seq_commit(m, res);
- return res < 0 && res != -ENAMETOOLONG ? res : 0;
- }
- int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
- {
- char *buf;
- size_t size = seq_get_buf(m, &buf);
- int res = -1;
- if (size) {
- char *p = dentry_path(dentry, buf, size);
- if (!IS_ERR(p)) {
- char *end = mangle_path(buf, p, esc);
- if (end)
- res = end - buf;
- }
- }
- seq_commit(m, res);
- return res;
- }
- EXPORT_SYMBOL(seq_dentry);
- static void *single_start(struct seq_file *p, loff_t *pos)
- {
- return NULL + (*pos == 0);
- }
- static void *single_next(struct seq_file *p, void *v, loff_t *pos)
- {
- ++*pos;
- return NULL;
- }
- static void single_stop(struct seq_file *p, void *v)
- {
- }
- int single_open(struct file *file, int (*show)(struct seq_file *, void *),
- void *data)
- {
- struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
- int res = -ENOMEM;
- if (op) {
- op->start = single_start;
- op->next = single_next;
- op->stop = single_stop;
- op->show = show;
- res = seq_open(file, op);
- if (!res)
- ((struct seq_file *)file->private_data)->private = data;
- else
- kfree(op);
- }
- return res;
- }
- EXPORT_SYMBOL(single_open);
- int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
- void *data, size_t size)
- {
- char *buf = seq_buf_alloc(size);
- int ret;
- if (!buf)
- return -ENOMEM;
- ret = single_open(file, show, data);
- if (ret) {
- kvfree(buf);
- return ret;
- }
- ((struct seq_file *)file->private_data)->buf = buf;
- ((struct seq_file *)file->private_data)->size = size;
- return 0;
- }
- EXPORT_SYMBOL(single_open_size);
- int single_release(struct inode *inode, struct file *file)
- {
- const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
- int res = seq_release(inode, file);
- kfree(op);
- return res;
- }
- EXPORT_SYMBOL(single_release);
- int seq_release_private(struct inode *inode, struct file *file)
- {
- struct seq_file *seq = file->private_data;
- kfree(seq->private);
- seq->private = NULL;
- return seq_release(inode, file);
- }
- EXPORT_SYMBOL(seq_release_private);
- void *__seq_open_private(struct file *f, const struct seq_operations *ops,
- int psize)
- {
- int rc;
- void *private;
- struct seq_file *seq;
- private = kzalloc(psize, GFP_KERNEL);
- if (private == NULL)
- goto out;
- rc = seq_open(f, ops);
- if (rc < 0)
- goto out_free;
- seq = f->private_data;
- seq->private = private;
- return private;
- out_free:
- kfree(private);
- out:
- return NULL;
- }
- EXPORT_SYMBOL(__seq_open_private);
- int seq_open_private(struct file *filp, const struct seq_operations *ops,
- int psize)
- {
- return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
- }
- EXPORT_SYMBOL(seq_open_private);
- void seq_putc(struct seq_file *m, char c)
- {
- if (m->count >= m->size)
- return;
- m->buf[m->count++] = c;
- }
- EXPORT_SYMBOL(seq_putc);
- void seq_puts(struct seq_file *m, const char *s)
- {
- int len = strlen(s);
- if (m->count + len >= m->size) {
- seq_set_overflow(m);
- return;
- }
- memcpy(m->buf + m->count, s, len);
- m->count += len;
- }
- EXPORT_SYMBOL(seq_puts);
- void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
- unsigned long long num)
- {
- int len;
- if (m->count + 2 >= m->size)
- goto overflow;
- len = strlen(delimiter);
- if (m->count + len >= m->size)
- goto overflow;
- memcpy(m->buf + m->count, delimiter, len);
- m->count += len;
- if (m->count + 1 >= m->size)
- goto overflow;
- if (num < 10) {
- m->buf[m->count++] = num + '0';
- return;
- }
- len = num_to_str(m->buf + m->count, m->size - m->count, num);
- if (!len)
- goto overflow;
- m->count += len;
- return;
- overflow:
- seq_set_overflow(m);
- }
- EXPORT_SYMBOL(seq_put_decimal_ull);
- void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
- {
- int len;
- if (m->count + 3 >= m->size)
- goto overflow;
- len = strlen(delimiter);
- if (m->count + len >= m->size)
- goto overflow;
- memcpy(m->buf + m->count, delimiter, len);
- m->count += len;
- if (m->count + 2 >= m->size)
- goto overflow;
- if (num < 0) {
- m->buf[m->count++] = '-';
- num = -num;
- }
- if (num < 10) {
- m->buf[m->count++] = num + '0';
- return;
- }
- len = num_to_str(m->buf + m->count, m->size - m->count, num);
- if (!len)
- goto overflow;
- m->count += len;
- return;
- overflow:
- seq_set_overflow(m);
- }
- EXPORT_SYMBOL(seq_put_decimal_ll);
- int seq_write(struct seq_file *seq, const void *data, size_t len)
- {
- if (seq->count + len < seq->size) {
- memcpy(seq->buf + seq->count, data, len);
- seq->count += len;
- return 0;
- }
- seq_set_overflow(seq);
- return -1;
- }
- EXPORT_SYMBOL(seq_write);
- void seq_pad(struct seq_file *m, char c)
- {
- int size = m->pad_until - m->count;
- if (size > 0)
- seq_printf(m, "%*s", size, "");
- if (c)
- seq_putc(m, c);
- }
- EXPORT_SYMBOL(seq_pad);
- void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
- int rowsize, int groupsize, const void *buf, size_t len,
- bool ascii)
- {
- const u8 *ptr = buf;
- int i, linelen, remaining = len;
- char *buffer;
- size_t size;
- int ret;
- if (rowsize != 16 && rowsize != 32)
- rowsize = 16;
- for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
- linelen = min(remaining, rowsize);
- remaining -= rowsize;
- switch (prefix_type) {
- case DUMP_PREFIX_ADDRESS:
- seq_printf(m, "%s%p: ", prefix_str, ptr + i);
- break;
- case DUMP_PREFIX_OFFSET:
- seq_printf(m, "%s%.8x: ", prefix_str, i);
- break;
- default:
- seq_printf(m, "%s", prefix_str);
- break;
- }
- size = seq_get_buf(m, &buffer);
- ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
- buffer, size, ascii);
- seq_commit(m, ret < size ? ret : -1);
- seq_putc(m, '\n');
- }
- }
- EXPORT_SYMBOL(seq_hex_dump);
- struct list_head *seq_list_start(struct list_head *head, loff_t pos)
- {
- struct list_head *lh;
- list_for_each(lh, head)
- if (pos-- == 0)
- return lh;
- return NULL;
- }
- EXPORT_SYMBOL(seq_list_start);
- struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
- {
- if (!pos)
- return head;
- return seq_list_start(head, pos - 1);
- }
- EXPORT_SYMBOL(seq_list_start_head);
- struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
- {
- struct list_head *lh;
- lh = ((struct list_head *)v)->next;
- ++*ppos;
- return lh == head ? NULL : lh;
- }
- EXPORT_SYMBOL(seq_list_next);
- struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
- {
- struct hlist_node *node;
- hlist_for_each(node, head)
- if (pos-- == 0)
- return node;
- return NULL;
- }
- EXPORT_SYMBOL(seq_hlist_start);
- struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
- {
- if (!pos)
- return SEQ_START_TOKEN;
- return seq_hlist_start(head, pos - 1);
- }
- EXPORT_SYMBOL(seq_hlist_start_head);
- struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
- loff_t *ppos)
- {
- struct hlist_node *node = v;
- ++*ppos;
- if (v == SEQ_START_TOKEN)
- return head->first;
- else
- return node->next;
- }
- EXPORT_SYMBOL(seq_hlist_next);
- struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
- loff_t pos)
- {
- struct hlist_node *node;
- __hlist_for_each_rcu(node, head)
- if (pos-- == 0)
- return node;
- return NULL;
- }
- EXPORT_SYMBOL(seq_hlist_start_rcu);
- struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
- loff_t pos)
- {
- if (!pos)
- return SEQ_START_TOKEN;
- return seq_hlist_start_rcu(head, pos - 1);
- }
- EXPORT_SYMBOL(seq_hlist_start_head_rcu);
- struct hlist_node *seq_hlist_next_rcu(void *v,
- struct hlist_head *head,
- loff_t *ppos)
- {
- struct hlist_node *node = v;
- ++*ppos;
- if (v == SEQ_START_TOKEN)
- return rcu_dereference(head->first);
- else
- return rcu_dereference(node->next);
- }
- EXPORT_SYMBOL(seq_hlist_next_rcu);
- struct hlist_node *
- seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
- {
- struct hlist_node *node;
- for_each_possible_cpu(*cpu) {
- hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
- if (pos-- == 0)
- return node;
- }
- }
- return NULL;
- }
- EXPORT_SYMBOL(seq_hlist_start_percpu);
- struct hlist_node *
- seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
- int *cpu, loff_t *pos)
- {
- struct hlist_node *node = v;
- ++*pos;
- if (node->next)
- return node->next;
- for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
- *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
- struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
- if (!hlist_empty(bucket))
- return bucket->first;
- }
- return NULL;
- }
- EXPORT_SYMBOL(seq_hlist_next_percpu);
|