2 * Copyright (c) 2020 iXsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/mutex.h>
30 #include <sys/procfs_list.h>
32 typedef struct procfs_list_iter {
33 procfs_list_t *pli_pl;
38 seq_printf(struct seq_file *f, const char *fmt, ...)
43 (void) vsnprintf(f->sf_buf, f->sf_size, fmt, adx);
48 procfs_list_update(kstat_t *ksp, int rw)
50 procfs_list_t *pl = ksp->ks_private;
52 if (rw == KSTAT_WRITE)
59 procfs_list_data(char *buf, size_t size, void *data)
72 return (pl->pl_show(&f, elt));
76 procfs_list_addr(kstat_t *ksp, loff_t n)
78 procfs_list_t *pl = ksp->ks_private;
79 void *elt = ksp->ks_private1;
84 ksp->ks_private1 = list_head(&pl->pl_list);
86 ksp->ks_private1 = list_next(&pl->pl_list, elt);
88 if (ksp->ks_private1) {
89 p = malloc(sizeof (*p), M_TEMP, M_WAITOK);
91 p->pli_elt = ksp->ks_private1;
98 procfs_list_install(const char *module,
99 const char *submodule,
102 procfs_list_t *procfs_list,
103 int (*show)(struct seq_file *f, void *p),
104 int (*show_header)(struct seq_file *f),
105 int (*clear)(procfs_list_t *procfs_list),
106 size_t procfs_list_node_off)
108 kstat_t *procfs_kstat;
110 mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
111 list_create(&procfs_list->pl_list,
112 procfs_list_node_off + sizeof (procfs_list_node_t),
113 procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
114 procfs_list->pl_show = show;
115 procfs_list->pl_show_header = show_header;
116 procfs_list->pl_clear = clear;
117 procfs_list->pl_next_id = 1;
118 procfs_list->pl_node_offset = procfs_list_node_off;
120 procfs_kstat = kstat_create(module, 0, name, submodule,
121 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
124 procfs_kstat->ks_lock = &procfs_list->pl_lock;
125 procfs_kstat->ks_ndata = UINT32_MAX;
126 procfs_kstat->ks_private = procfs_list;
127 procfs_kstat->ks_update = procfs_list_update;
128 kstat_set_seq_raw_ops(procfs_kstat, show_header,
129 procfs_list_data, procfs_list_addr);
130 kstat_install(procfs_kstat);
131 procfs_list->pl_private = procfs_kstat;
136 procfs_list_uninstall(procfs_list_t *procfs_list)
140 procfs_list_destroy(procfs_list_t *procfs_list)
142 ASSERT(list_is_empty(&procfs_list->pl_list));
143 kstat_delete(procfs_list->pl_private);
144 list_destroy(&procfs_list->pl_list);
145 mutex_destroy(&procfs_list->pl_lock);
148 #define NODE_ID(procfs_list, obj) \
149 (((procfs_list_node_t *)(((char *)obj) + \
150 (procfs_list)->pl_node_offset))->pln_id)
153 procfs_list_add(procfs_list_t *procfs_list, void *p)
155 ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
156 NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
157 list_insert_tail(&procfs_list->pl_list, p);