Merge branch 'vendor/GDTOA'
[dragonfly.git] / sys / sys / sysref.h
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  * $DragonFly: src/sys/sys/sysref.h,v 1.4 2007/05/29 17:01:02 dillon Exp $
35  */
36 /*
37  * System resource registration, reference counter, and allocation
38  * management subsystem.
39  *
40  * All major system resources use this structure and API to associate
41  * a machine-unique sysid with a major system resource structure, to
42  * reference count that structure, to register the structure and provide
43  * a dictionary lookup feature for remote access.
44  *
45  * System resources are backed by the objcache.
46  */
47
48 #ifndef _SYS_SYSREF_H_
49 #define _SYS_SYSREF_H_
50
51 #ifndef _SYS_SYSID_H_
52 #include <sys/sysid.h>
53 #endif
54 #ifndef _SYS_TREE_H_
55 #include <sys/tree.h>
56 #endif
57 #ifndef _SYS_OBJCACHE_H_
58 #include <sys/objcache.h>
59 #endif
60 #ifndef _SYS_MALLOC_H_
61 #include <sys/malloc.h>
62 #endif
63
64 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
65
66 /*
67  * Register a resource structure type.  Note that the destroy function
68  * will be called on 1->0 transitions (really 1->-0x40000000 transitions),
69  * and the free function 
70  * but the free function can be called via an IPI, without the BGL, and
71  * must be carefully coded if it does anything more complex then objcache_put
72  */
73 typedef void (*sysref_terminate_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     mag_capacity;           /* magazine capacity init (def 64) */
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         } ops;
89 };
90
91 #define SRC_MANAGEDINIT 0x0001          /* do not auto-zero the resource */
92
93 /*
94  * sysref - embedded in resource structures.
95  *
96  * NOTE: The cpuid determining which cpu's RB tree the sysref is
97  * associated with is integrated into the sysref.
98  *
99  * NOTE: A resource can be in varying states of construction or
100  * deconstruction, denoted by having a negative refcnt.  To keep
101  * performance nominal we reuse sysids that are NOT looked up via
102  * syslink (meaning we don't have to adjust their location in the
103  * RB tree).  The objcache is used to cache the RB tree linkage.
104  */
105 struct sysref {
106         RB_ENTRY(sysref) rbnode;        /* per-cpu red-black tree node */
107         sysid_t sysid;                  /* machine-wide unique sysid */
108         int     refcnt;                 /* normal reference count */
109         int     flags;
110         struct sysref_class *srclass;   /* type of resource and API */
111 };
112
113 #define SRF_SYSIDUSED   0x0001          /* sysid was used for access */
114 #define SRF_ALLOCATED   0x0002          /* sysref_alloc used to allocate */
115
116 RB_HEAD(sysref_rb_tree, sysref);
117 RB_PROTOTYPE2(sysref_rb_tree, sysref, rbnode, rb_sysref_compare, sysid_t);
118
119 #endif
120
121 /*
122  * Protocol numbers
123  */
124 #define SYSREF_PROTO_VMSPACE    0x0020
125 #define SYSREF_PROTO_VMOBJECT   0x0021
126 #define SYSREF_PROTO_VNODE      0x0022
127 #define SYSREF_PROTO_PROCESS    0x0023
128 #define SYSREF_PROTO_LWP        0x0024
129 #define SYSREF_PROTO_FD         0x0025
130 #define SYSREF_PROTO_FP         0x0026
131 #define SYSREF_PROTO_SOCKET     0x0027
132 #define SYSREF_PROTO_NCP        0x0028
133 #define SYSREF_PROTO_BUF        0x0029
134 #define SYSREF_PROTO_FSMOUNT    0x002A
135 #define SYSREF_PROTO_DEV        0x002B
136
137 #ifdef _KERNEL
138
139 sysid_t allocsysid(void);
140
141 #endif
142
143 #endif