xref: /freebsd/sys/contrib/openzfs/lib/libspl/procfs_list.c (revision 8ac904ce090b1c2e355da8aa122ca2252183f4e1)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
26  * Copyright (c) 2025, Klara, Inc.
27  */
28 
29 #include <assert.h>
30 #include <pthread.h>
31 #include <stddef.h>
32 #include <sys/procfs_list.h>
33 #include <sys/mutex.h>
34 #include <sys/list.h>
35 
36 /*
37  * =========================================================================
38  * procfs list
39  * =========================================================================
40  */
41 
42 void
seq_printf(struct seq_file * m,const char * fmt,...)43 seq_printf(struct seq_file *m, const char *fmt, ...)
44 {
45 	(void) m, (void) fmt;
46 }
47 
48 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)49 procfs_list_install(const char *module,
50     const char *submodule,
51     const char *name,
52     mode_t mode,
53     procfs_list_t *procfs_list,
54     int (*show)(struct seq_file *f, void *p),
55     int (*show_header)(struct seq_file *f),
56     int (*clear)(procfs_list_t *procfs_list),
57     size_t procfs_list_node_off)
58 {
59 	(void) module, (void) submodule, (void) name, (void) mode, (void) show,
60 	    (void) show_header, (void) clear;
61 	mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
62 	list_create(&procfs_list->pl_list,
63 	    procfs_list_node_off + sizeof (procfs_list_node_t),
64 	    procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
65 	procfs_list->pl_next_id = 1;
66 	procfs_list->pl_node_offset = procfs_list_node_off;
67 }
68 
69 void
procfs_list_uninstall(procfs_list_t * procfs_list)70 procfs_list_uninstall(procfs_list_t *procfs_list)
71 {
72 	(void) procfs_list;
73 }
74 
75 void
procfs_list_destroy(procfs_list_t * procfs_list)76 procfs_list_destroy(procfs_list_t *procfs_list)
77 {
78 	ASSERT(list_is_empty(&procfs_list->pl_list));
79 	list_destroy(&procfs_list->pl_list);
80 	mutex_destroy(&procfs_list->pl_lock);
81 }
82 
83 #define	NODE_ID(procfs_list, obj) \
84 		(((procfs_list_node_t *)(((char *)obj) + \
85 		(procfs_list)->pl_node_offset))->pln_id)
86 
87 void
procfs_list_add(procfs_list_t * procfs_list,void * p)88 procfs_list_add(procfs_list_t *procfs_list, void *p)
89 {
90 	ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
91 	NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
92 	list_insert_tail(&procfs_list->pl_list, p);
93 }
94