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 typedef void (*sysref_lock_func_t)(void *); 75 typedef void (*sysref_unlock_func_t)(void *); 76 77 struct sysref_class { 78 const char *name; /* name of the system resource */ 79 malloc_type_t mtype; /* malloc backing store */ 80 int proto; /* RPC protocol id */ 81 int offset; /* offset of sysref in resource */ 82 int objsize; /* size of the resource structure */ 83 int mag_capacity; /* magazine capacity init (def 64) */ 84 int flags; 85 struct objcache *oc; /* object cache */ 86 objcache_ctor_fn *ctor; /* objcache ctor chaining */ 87 objcache_dtor_fn *dtor; /* objcache dtor chaining */ 88 struct sysref_ops { 89 sysref_terminate_func_t terminate; 90 sysref_lock_func_t lock; 91 sysref_unlock_func_t unlock; 92 } ops; 93 }; 94 95 #define SRC_MANAGEDINIT 0x0001 /* do not auto-zero the resource */ 96 97 /* 98 * sysref - embedded in resource structures. 99 * 100 * NOTE: The cpuid determining which cpu's RB tree the sysref is 101 * associated with is integrated into the sysref. 102 * 103 * NOTE: A resource can be in varying states of construction or 104 * deconstruction, denoted by having a negative refcnt. To keep 105 * performance nominal we reuse sysids that are NOT looked up via 106 * syslink (meaning we don't have to adjust their location in the 107 * RB tree). The objcache is used to cache the RB tree linkage. 108 */ 109 struct sysref { 110 RB_ENTRY(sysref) rbnode; /* per-cpu red-black tree node */ 111 sysid_t sysid; /* machine-wide unique sysid */ 112 int refcnt; /* normal reference count */ 113 int flags; 114 struct sysref_class *srclass; /* type of resource and API */ 115 }; 116 117 #define SRF_SYSIDUSED 0x0001 /* sysid was used for access */ 118 #define SRF_ALLOCATED 0x0002 /* sysref_alloc used to allocate */ 119 120 RB_HEAD(sysref_rb_tree, sysref); 121 RB_PROTOTYPE2(sysref_rb_tree, sysref, rbnode, rb_sysref_compare, sysid_t); 122 123 #endif 124 125 /* 126 * Protocol numbers 127 */ 128 #define SYSREF_PROTO_VMSPACE 0x0020 129 #define SYSREF_PROTO_VMOBJECT 0x0021 130 #define SYSREF_PROTO_VNODE 0x0022 131 #define SYSREF_PROTO_PROCESS 0x0023 132 #define SYSREF_PROTO_LWP 0x0024 133 #define SYSREF_PROTO_FD 0x0025 134 #define SYSREF_PROTO_FP 0x0026 135 #define SYSREF_PROTO_SOCKET 0x0027 136 #define SYSREF_PROTO_NCP 0x0028 137 #define SYSREF_PROTO_BUF 0x0029 138 #define SYSREF_PROTO_FSMOUNT 0x002A 139 #define SYSREF_PROTO_DEV 0x002B 140 141 #ifdef _KERNEL 142 143 sysid_t allocsysid(void); 144 145 #endif 146 147 #endif 148