xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/spl/spl_procfs_list.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2020 iXsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/list.h>
30 #include <sys/mutex.h>
31 #include <sys/procfs_list.h>
32 
33 typedef struct procfs_list_iter {
34 	procfs_list_t *pli_pl;
35 	void *pli_elt;
36 } pli_t;
37 
38 void
seq_printf(struct seq_file * f,const char * fmt,...)39 seq_printf(struct seq_file *f, const char *fmt, ...)
40 {
41 	va_list adx;
42 
43 	va_start(adx, fmt);
44 	(void) vsnprintf(f->sf_buf, f->sf_size, fmt, adx);
45 	va_end(adx);
46 }
47 
48 static int
procfs_list_update(kstat_t * ksp,int rw)49 procfs_list_update(kstat_t *ksp, int rw)
50 {
51 	procfs_list_t *pl = ksp->ks_private;
52 
53 	if (rw == KSTAT_WRITE)
54 		pl->pl_clear(pl);
55 
56 	return (0);
57 }
58 
59 static int
procfs_list_data(char * buf,size_t size,void * data)60 procfs_list_data(char *buf, size_t size, void *data)
61 {
62 	pli_t *p;
63 	void *elt;
64 	procfs_list_t *pl;
65 	struct seq_file f;
66 
67 	p = data;
68 	pl = p->pli_pl;
69 	elt = p->pli_elt;
70 	free(p, M_TEMP);
71 	f.sf_buf = buf;
72 	f.sf_size = size;
73 	return (pl->pl_show(&f, elt));
74 }
75 
76 static void *
procfs_list_addr(kstat_t * ksp,loff_t n)77 procfs_list_addr(kstat_t *ksp, loff_t n)
78 {
79 	procfs_list_t *pl = ksp->ks_private;
80 	void *elt = ksp->ks_private1;
81 	pli_t *p = NULL;
82 
83 
84 	if (n == 0)
85 		ksp->ks_private1 = list_head(&pl->pl_list);
86 	else if (elt)
87 		ksp->ks_private1 = list_next(&pl->pl_list, elt);
88 
89 	if (ksp->ks_private1) {
90 		p = malloc(sizeof (*p), M_TEMP, M_WAITOK);
91 		p->pli_pl = pl;
92 		p->pli_elt = ksp->ks_private1;
93 	}
94 
95 	return (p);
96 }
97 
98 void
procfs_list_install(const char * module,const char * submodule,const char * name,mode_t mode,procfs_list_t * procfs_list,int (* show)(struct seq_file * f,void * p),int (* show_header)(struct seq_file * f),int (* clear)(procfs_list_t * procfs_list),size_t procfs_list_node_off)99 procfs_list_install(const char *module,
100     const char *submodule,
101     const char *name,
102     mode_t mode,
103     procfs_list_t *procfs_list,
104     int (*show)(struct seq_file *f, void *p),
105     int (*show_header)(struct seq_file *f),
106     int (*clear)(procfs_list_t *procfs_list),
107     size_t procfs_list_node_off)
108 {
109 	kstat_t *procfs_kstat;
110 
111 	mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
112 	list_create(&procfs_list->pl_list,
113 	    procfs_list_node_off + sizeof (procfs_list_node_t),
114 	    procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
115 	procfs_list->pl_show = show;
116 	procfs_list->pl_show_header = show_header;
117 	procfs_list->pl_clear = clear;
118 	procfs_list->pl_next_id = 1;
119 	procfs_list->pl_node_offset = procfs_list_node_off;
120 
121 	procfs_kstat =  kstat_create(module, 0, name, submodule,
122 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
123 
124 	if (procfs_kstat) {
125 		procfs_kstat->ks_lock = &procfs_list->pl_lock;
126 		procfs_kstat->ks_ndata = UINT32_MAX;
127 		procfs_kstat->ks_private = procfs_list;
128 		procfs_kstat->ks_update = procfs_list_update;
129 		kstat_set_seq_raw_ops(procfs_kstat, show_header,
130 		    procfs_list_data, procfs_list_addr);
131 		kstat_install(procfs_kstat);
132 		procfs_list->pl_private = procfs_kstat;
133 	}
134 }
135 
136 void
procfs_list_uninstall(procfs_list_t * procfs_list)137 procfs_list_uninstall(procfs_list_t *procfs_list)
138 {}
139 
140 void
procfs_list_destroy(procfs_list_t * procfs_list)141 procfs_list_destroy(procfs_list_t *procfs_list)
142 {
143 	ASSERT(list_is_empty(&procfs_list->pl_list));
144 	kstat_delete(procfs_list->pl_private);
145 	list_destroy(&procfs_list->pl_list);
146 	mutex_destroy(&procfs_list->pl_lock);
147 }
148 
149 #define	NODE_ID(procfs_list, obj) \
150 		(((procfs_list_node_t *)(((char *)obj) + \
151 		(procfs_list)->pl_node_offset))->pln_id)
152 
153 void
procfs_list_add(procfs_list_t * procfs_list,void * p)154 procfs_list_add(procfs_list_t *procfs_list, void *p)
155 {
156 	ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
157 	NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
158 	list_insert_tail(&procfs_list->pl_list, p);
159 }
160