13f6cab07SMatt Macy /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
33f6cab07SMatt Macy *
43f6cab07SMatt Macy * Copyright (c) 2016-2018, Matthew Macy <mmacy@freebsd.org>
53f6cab07SMatt Macy *
63f6cab07SMatt Macy * Redistribution and use in source and binary forms, with or without
73f6cab07SMatt Macy * modification, are permitted provided that the following conditions
83f6cab07SMatt Macy * are met:
93f6cab07SMatt Macy * 1. Redistributions of source code must retain the above copyright
103f6cab07SMatt Macy * notice, this list of conditions and the following disclaimer.
113f6cab07SMatt Macy * 2. Redistributions in binary form must reproduce the above copyright
123f6cab07SMatt Macy * notice, this list of conditions and the following disclaimer in the
133f6cab07SMatt Macy * documentation and/or other materials provided with the distribution.
143f6cab07SMatt Macy *
153f6cab07SMatt Macy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163f6cab07SMatt Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173f6cab07SMatt Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183f6cab07SMatt Macy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193f6cab07SMatt Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203f6cab07SMatt Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213f6cab07SMatt Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223f6cab07SMatt Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233f6cab07SMatt Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243f6cab07SMatt Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253f6cab07SMatt Macy * SUCH DAMAGE.
263f6cab07SMatt Macy */
273f6cab07SMatt Macy
28307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_SEQ_FILE_H_
29307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_SEQ_FILE_H_
303f6cab07SMatt Macy
314359672eSBjoern A. Zeeb #include <sys/types.h>
324359672eSBjoern A. Zeeb #include <sys/sbuf.h>
334359672eSBjoern A. Zeeb
346a65ca35SJohannes Lundberg #include <linux/types.h>
356a65ca35SJohannes Lundberg #include <linux/fs.h>
36d6d1e73eSJean-Sébastien Pédron #include <linux/string_helpers.h>
374359672eSBjoern A. Zeeb #include <linux/printk.h>
383f6cab07SMatt Macy
396a65ca35SJohannes Lundberg #undef file
403f6cab07SMatt Macy #define inode vnode
413f6cab07SMatt Macy
42f697b943SJake Freeland MALLOC_DECLARE(M_LSEQ);
43f697b943SJake Freeland
446a65ca35SJohannes Lundberg #define DEFINE_SHOW_ATTRIBUTE(__name) \
456a65ca35SJohannes Lundberg static int __name ## _open(struct inode *inode, struct linux_file *file) \
466a65ca35SJohannes Lundberg { \
476a65ca35SJohannes Lundberg return single_open(file, __name ## _show, inode->i_private); \
486a65ca35SJohannes Lundberg } \
496a65ca35SJohannes Lundberg \
506a65ca35SJohannes Lundberg static const struct file_operations __name ## _fops = { \
516a65ca35SJohannes Lundberg .owner = THIS_MODULE, \
526a65ca35SJohannes Lundberg .open = __name ## _open, \
536a65ca35SJohannes Lundberg .read = seq_read, \
546a65ca35SJohannes Lundberg .llseek = seq_lseek, \
556a65ca35SJohannes Lundberg .release = single_release, \
566a65ca35SJohannes Lundberg }
576a65ca35SJohannes Lundberg
58*cdfdafdcSJean-Sébastien Pédron #define DEFINE_SHOW_STORE_ATTRIBUTE(__name) \
59*cdfdafdcSJean-Sébastien Pédron static int __name ## _open(struct inode *inode, struct linux_file *file) \
60*cdfdafdcSJean-Sébastien Pédron { \
61*cdfdafdcSJean-Sébastien Pédron return single_open(file, __name ## _show, inode->i_private); \
62*cdfdafdcSJean-Sébastien Pédron } \
63*cdfdafdcSJean-Sébastien Pédron \
64*cdfdafdcSJean-Sébastien Pédron static const struct file_operations __name ## _fops = { \
65*cdfdafdcSJean-Sébastien Pédron .owner = THIS_MODULE, \
66*cdfdafdcSJean-Sébastien Pédron .open = __name ## _open, \
67*cdfdafdcSJean-Sébastien Pédron .read = seq_read, \
68*cdfdafdcSJean-Sébastien Pédron .write = __name ## _write, \
69*cdfdafdcSJean-Sébastien Pédron .llseek = seq_lseek, \
70*cdfdafdcSJean-Sébastien Pédron .release = single_release, \
71*cdfdafdcSJean-Sébastien Pédron }
72*cdfdafdcSJean-Sébastien Pédron
733f6cab07SMatt Macy struct seq_file {
743f6cab07SMatt Macy struct sbuf *buf;
755fbfe951SJean-Sébastien Pédron size_t size;
763f6cab07SMatt Macy const struct seq_operations *op;
773f6cab07SMatt Macy const struct linux_file *file;
783f6cab07SMatt Macy void *private;
793f6cab07SMatt Macy };
803f6cab07SMatt Macy
813f6cab07SMatt Macy struct seq_operations {
823f6cab07SMatt Macy void * (*start) (struct seq_file *m, off_t *pos);
833f6cab07SMatt Macy void (*stop) (struct seq_file *m, void *v);
843f6cab07SMatt Macy void * (*next) (struct seq_file *m, void *v, off_t *pos);
853f6cab07SMatt Macy int (*show) (struct seq_file *m, void *v);
863f6cab07SMatt Macy };
873f6cab07SMatt Macy
883f6cab07SMatt Macy ssize_t seq_read(struct linux_file *, char *, size_t, off_t *);
893f6cab07SMatt Macy int seq_write(struct seq_file *seq, const void *data, size_t len);
905fbfe951SJean-Sébastien Pédron void seq_putc(struct seq_file *m, char c);
915fbfe951SJean-Sébastien Pédron void seq_puts(struct seq_file *m, const char *str);
925fbfe951SJean-Sébastien Pédron bool seq_has_overflowed(struct seq_file *m);
933f6cab07SMatt Macy
94b5a81075SBjoern A. Zeeb void *__seq_open_private(struct linux_file *, const struct seq_operations *, int);
95b5a81075SBjoern A. Zeeb int seq_release_private(struct inode *, struct linux_file *);
96b5a81075SBjoern A. Zeeb
973f6cab07SMatt Macy int seq_open(struct linux_file *f, const struct seq_operations *op);
983f6cab07SMatt Macy int seq_release(struct inode *inode, struct linux_file *file);
993f6cab07SMatt Macy
1003f6cab07SMatt Macy off_t seq_lseek(struct linux_file *file, off_t offset, int whence);
1013f6cab07SMatt Macy int single_open(struct linux_file *, int (*)(struct seq_file *, void *), void *);
1025fbfe951SJean-Sébastien Pédron int single_open_size(struct linux_file *, int (*)(struct seq_file *, void *), void *, size_t);
1033f6cab07SMatt Macy int single_release(struct inode *, struct linux_file *);
1043f6cab07SMatt Macy
105cbda8bedSHans Petter Selasky void lkpi_seq_vprintf(struct seq_file *m, const char *fmt, va_list args);
106cbda8bedSHans Petter Selasky void lkpi_seq_printf(struct seq_file *m, const char *fmt, ...);
107cbda8bedSHans Petter Selasky
108cbda8bedSHans Petter Selasky #define seq_vprintf(...) lkpi_seq_vprintf(__VA_ARGS__)
109cbda8bedSHans Petter Selasky #define seq_printf(...) lkpi_seq_printf(__VA_ARGS__)
1103f6cab07SMatt Macy
1114359672eSBjoern A. Zeeb int __lkpi_hexdump_sbuf_printf(void *, const char *, ...) __printflike(2, 3);
1124359672eSBjoern A. Zeeb
1134359672eSBjoern A. Zeeb static inline 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)1144359672eSBjoern A. Zeeb seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
1154359672eSBjoern A. Zeeb int rowsize, int groupsize, const void *buf, size_t len, bool ascii)
1164359672eSBjoern A. Zeeb {
1174359672eSBjoern A. Zeeb lkpi_hex_dump(__lkpi_hexdump_sbuf_printf, m->buf, NULL, prefix_str, prefix_type,
1184359672eSBjoern A. Zeeb rowsize, groupsize, buf, len, ascii);
1194359672eSBjoern A. Zeeb }
1204359672eSBjoern A. Zeeb
1216a65ca35SJohannes Lundberg #define file linux_file
1223f6cab07SMatt Macy
123307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_SEQ_FILE_H_ */
124