]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/os/freebsd/spl/spl_procfs_list.c
freebsd: remove __FBSDID macro use
[FreeBSD/FreeBSD.git] / module / os / freebsd / spl / spl_procfs_list.c
1 /*
2  * Copyright (c) 2020 iXsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/list.h>
29 #include <sys/mutex.h>
30 #include <sys/procfs_list.h>
31
32 typedef struct procfs_list_iter {
33         procfs_list_t *pli_pl;
34         void *pli_elt;
35 } pli_t;
36
37 void
38 seq_printf(struct seq_file *f, const char *fmt, ...)
39 {
40         va_list adx;
41
42         va_start(adx, fmt);
43         (void) vsnprintf(f->sf_buf, f->sf_size, fmt, adx);
44         va_end(adx);
45 }
46
47 static int
48 procfs_list_update(kstat_t *ksp, int rw)
49 {
50         procfs_list_t *pl = ksp->ks_private;
51
52         if (rw == KSTAT_WRITE)
53                 pl->pl_clear(pl);
54
55         return (0);
56 }
57
58 static int
59 procfs_list_data(char *buf, size_t size, void *data)
60 {
61         pli_t *p;
62         void *elt;
63         procfs_list_t *pl;
64         struct seq_file f;
65
66         p = data;
67         pl = p->pli_pl;
68         elt = p->pli_elt;
69         free(p, M_TEMP);
70         f.sf_buf = buf;
71         f.sf_size = size;
72         return (pl->pl_show(&f, elt));
73 }
74
75 static void *
76 procfs_list_addr(kstat_t *ksp, loff_t n)
77 {
78         procfs_list_t *pl = ksp->ks_private;
79         void *elt = ksp->ks_private1;
80         pli_t *p = NULL;
81
82
83         if (n == 0)
84                 ksp->ks_private1 = list_head(&pl->pl_list);
85         else if (elt)
86                 ksp->ks_private1 = list_next(&pl->pl_list, elt);
87
88         if (ksp->ks_private1) {
89                 p = malloc(sizeof (*p), M_TEMP, M_WAITOK);
90                 p->pli_pl = pl;
91                 p->pli_elt = ksp->ks_private1;
92         }
93
94         return (p);
95 }
96
97 void
98 procfs_list_install(const char *module,
99     const char *submodule,
100     const char *name,
101     mode_t mode,
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)
107 {
108         kstat_t *procfs_kstat;
109
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;
119
120         procfs_kstat =  kstat_create(module, 0, name, submodule,
121             KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
122
123         if (procfs_kstat) {
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;
132         }
133 }
134
135 void
136 procfs_list_uninstall(procfs_list_t *procfs_list)
137 {}
138
139 void
140 procfs_list_destroy(procfs_list_t *procfs_list)
141 {
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);
146 }
147
148 #define NODE_ID(procfs_list, obj) \
149                 (((procfs_list_node_t *)(((char *)obj) + \
150                 (procfs_list)->pl_node_offset))->pln_id)
151
152 void
153 procfs_list_add(procfs_list_t *procfs_list, void *p)
154 {
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);
158 }