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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/mutex.h>
33 #include <sys/procfs_list.h>
35 typedef struct procfs_list_iter {
36 procfs_list_t *pli_pl;
41 seq_printf(struct seq_file *f, const char *fmt, ...)
46 (void) vsnprintf(f->sf_buf, f->sf_size, fmt, adx);
51 procfs_list_update(kstat_t *ksp, int rw)
53 procfs_list_t *pl = ksp->ks_private;
55 if (rw == KSTAT_WRITE)
62 procfs_list_data(char *buf, size_t size, void *data)
75 return (pl->pl_show(&f, elt));
79 procfs_list_addr(kstat_t *ksp, loff_t n)
81 procfs_list_t *pl = ksp->ks_private;
82 void *elt = ksp->ks_private1;
87 ksp->ks_private1 = list_head(&pl->pl_list);
89 ksp->ks_private1 = list_next(&pl->pl_list, elt);
91 if (ksp->ks_private1) {
92 p = malloc(sizeof (*p), M_TEMP, M_WAITOK);
94 p->pli_elt = ksp->ks_private1;
101 procfs_list_install(const char *module,
102 const char *submodule,
105 procfs_list_t *procfs_list,
106 int (*show)(struct seq_file *f, void *p),
107 int (*show_header)(struct seq_file *f),
108 int (*clear)(procfs_list_t *procfs_list),
109 size_t procfs_list_node_off)
111 kstat_t *procfs_kstat;
113 mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
114 list_create(&procfs_list->pl_list,
115 procfs_list_node_off + sizeof (procfs_list_node_t),
116 procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
117 procfs_list->pl_show = show;
118 procfs_list->pl_show_header = show_header;
119 procfs_list->pl_clear = clear;
120 procfs_list->pl_next_id = 1;
121 procfs_list->pl_node_offset = procfs_list_node_off;
123 procfs_kstat = kstat_create(module, 0, name, submodule,
124 KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
127 procfs_kstat->ks_lock = &procfs_list->pl_lock;
128 procfs_kstat->ks_ndata = UINT32_MAX;
129 procfs_kstat->ks_private = procfs_list;
130 procfs_kstat->ks_update = procfs_list_update;
131 kstat_set_seq_raw_ops(procfs_kstat, show_header,
132 procfs_list_data, procfs_list_addr);
133 kstat_install(procfs_kstat);
134 procfs_list->pl_private = procfs_kstat;
139 procfs_list_uninstall(procfs_list_t *procfs_list)
143 procfs_list_destroy(procfs_list_t *procfs_list)
145 ASSERT(list_is_empty(&procfs_list->pl_list));
146 kstat_delete(procfs_list->pl_private);
147 list_destroy(&procfs_list->pl_list);
148 mutex_destroy(&procfs_list->pl_lock);
151 #define NODE_ID(procfs_list, obj) \
152 (((procfs_list_node_t *)(((char *)obj) + \
153 (procfs_list)->pl_node_offset))->pln_id)
156 procfs_list_add(procfs_list_t *procfs_list, void *p)
158 ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
159 NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
160 list_insert_tail(&procfs_list->pl_list, p);