xref: /dragonfly/sys/sys/sysref.h (revision dae650601b7a1502cc3804a70c39222b46186f48)
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * System resource registration, reference counter, and allocation
36  * management subsystem.
37  *
38  * All major system resources use this structure and API to associate
39  * a machine-unique sysid with a major system resource structure, to
40  * reference count that structure, to register the structure and provide
41  * a dictionary lookup feature for remote access.
42  *
43  * System resources are backed by the objcache.
44  */
45 
46 #ifndef _SYS_SYSREF_H_
47 #define _SYS_SYSREF_H_
48 
49 #ifndef _SYS_SYSID_H_
50 #include <sys/sysid.h>
51 #endif
52 #ifndef _SYS_TREE_H_
53 #include <sys/tree.h>
54 #endif
55 #ifndef _SYS_OBJCACHE_H_
56 #include <sys/objcache.h>
57 #endif
58 
59 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
60 #ifndef _SYS__MALLOC_H_
61 #include <sys/_malloc.h>
62 #endif
63 
64 /*
65  * Register a resource structure type.  Note that the destroy function
66  * will be called on 1->0 transitions (really 1->-0x40000000 transitions),
67  * and the free function
68  * but the free function can be called via an IPI, without the BGL, and
69  * must be carefully coded if it does anything more complex then objcache_put
70  */
71 typedef void (*sysref_terminate_func_t)(void *);
72 typedef void (*sysref_lock_func_t)(void *);
73 typedef void (*sysref_unlock_func_t)(void *);
74 
75 struct sysref_class {
76           const char *name;             /* name of the system resource */
77           malloc_type_t mtype;                    /* malloc backing store */
78           int       proto;                        /* RPC protocol id */
79           int       offset;                       /* offset of sysref in resource */
80           int       objsize;            /* size of the resource structure */
81           int       nom_cache;                    /* nominal objects to cache */
82           int       flags;
83           struct objcache *oc;                    /* object cache */
84           objcache_ctor_fn *ctor;                 /* objcache ctor chaining */
85           objcache_dtor_fn *dtor;                 /* objcache dtor chaining */
86           struct sysref_ops {
87                     sysref_terminate_func_t terminate;
88                     sysref_lock_func_t lock;
89                     sysref_unlock_func_t unlock;
90           } ops;
91 };
92 
93 #define SRC_MANAGEDINIT       0x0001              /* do not auto-zero the resource */
94 
95 /*
96  * sysref - embedded in resource structures.
97  *
98  * NOTE: The cpuid determining which cpu's RB tree the sysref is
99  * associated with is integrated into the sysref.
100  *
101  * NOTE: A resource can be in varying states of construction or
102  * deconstruction, denoted by having a negative refcnt.  To keep
103  * performance nominal we reuse sysids that are NOT looked up via
104  * syslink (meaning we don't have to adjust their location in the
105  * RB tree).  The objcache is used to cache the RB tree linkage.
106  */
107 struct sysref {
108           RB_ENTRY(sysref) rbnode;      /* per-cpu red-black tree node */
109           sysid_t   sysid;                        /* machine-wide unique sysid */
110           int       refcnt;                       /* normal reference count */
111           int       flags;
112           struct sysref_class *srclass; /* type of resource and API */
113 };
114 
115 #define SRF_SYSIDUSED         0x0001              /* sysid was used for access */
116 #define SRF_ALLOCATED         0x0002              /* sysref_alloc used to allocate */
117 #define SRF_PUTAWAY 0x0004              /* in objcache */
118 
119 RB_HEAD(sysref_rb_tree, sysref);
120 RB_PROTOTYPE2(sysref_rb_tree, sysref, rbnode, rb_sysref_compare, sysid_t);
121 
122 #endif    /* _KERNEL || _KERNEL_STRUCTURES */
123 
124 /*
125  * Protocol numbers
126  */
127 #define SYSREF_PROTO_VMSPACE  0x0020
128 #define SYSREF_PROTO_VMOBJECT 0x0021
129 #define SYSREF_PROTO_VNODE    0x0022
130 #define SYSREF_PROTO_PROCESS  0x0023
131 #define SYSREF_PROTO_LWP      0x0024
132 #define SYSREF_PROTO_FD                 0x0025
133 #define SYSREF_PROTO_FP                 0x0026
134 #define SYSREF_PROTO_SOCKET   0x0027
135 #define SYSREF_PROTO_NCP      0x0028
136 #define SYSREF_PROTO_BUF      0x0029
137 #define SYSREF_PROTO_FSMOUNT  0x002A
138 #define SYSREF_PROTO_DEV      0x002B
139 
140 #ifdef _KERNEL
141 
142 sysid_t allocsysid(void);
143 
144 #endif
145 
146 #endif    /* !_SYS_SYSREF_H_ */
147