xref: /dragonfly/sys/sys/sysref.h (revision dae65060)
110aa77c0SMatthew Dillon /*
210aa77c0SMatthew Dillon  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
310aa77c0SMatthew Dillon  *
410aa77c0SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
510aa77c0SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
610aa77c0SMatthew Dillon  *
710aa77c0SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
810aa77c0SMatthew Dillon  * modification, are permitted provided that the following conditions
910aa77c0SMatthew Dillon  * are met:
1010aa77c0SMatthew Dillon  *
1110aa77c0SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
1210aa77c0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1310aa77c0SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
1410aa77c0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
1510aa77c0SMatthew Dillon  *    the documentation and/or other materials provided with the
1610aa77c0SMatthew Dillon  *    distribution.
1710aa77c0SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
1810aa77c0SMatthew Dillon  *    contributors may be used to endorse or promote products derived
1910aa77c0SMatthew Dillon  *    from this software without specific, prior written permission.
2010aa77c0SMatthew Dillon  *
2110aa77c0SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2210aa77c0SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2310aa77c0SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2410aa77c0SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2510aa77c0SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2610aa77c0SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2710aa77c0SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2810aa77c0SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2910aa77c0SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3010aa77c0SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3110aa77c0SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3210aa77c0SMatthew Dillon  * SUCH DAMAGE.
3310aa77c0SMatthew Dillon  */
3410aa77c0SMatthew Dillon /*
3510aa77c0SMatthew Dillon  * System resource registration, reference counter, and allocation
3610aa77c0SMatthew Dillon  * management subsystem.
3710aa77c0SMatthew Dillon  *
3810aa77c0SMatthew Dillon  * All major system resources use this structure and API to associate
3910aa77c0SMatthew Dillon  * a machine-unique sysid with a major system resource structure, to
4010aa77c0SMatthew Dillon  * reference count that structure, to register the structure and provide
4110aa77c0SMatthew Dillon  * a dictionary lookup feature for remote access.
4210aa77c0SMatthew Dillon  *
4310aa77c0SMatthew Dillon  * System resources are backed by the objcache.
4410aa77c0SMatthew Dillon  */
4510aa77c0SMatthew Dillon 
4610aa77c0SMatthew Dillon #ifndef _SYS_SYSREF_H_
4710aa77c0SMatthew Dillon #define _SYS_SYSREF_H_
4810aa77c0SMatthew Dillon 
4910aa77c0SMatthew Dillon #ifndef _SYS_SYSID_H_
5010aa77c0SMatthew Dillon #include <sys/sysid.h>
5110aa77c0SMatthew Dillon #endif
5210aa77c0SMatthew Dillon #ifndef _SYS_TREE_H_
5310aa77c0SMatthew Dillon #include <sys/tree.h>
5410aa77c0SMatthew Dillon #endif
5510aa77c0SMatthew Dillon #ifndef _SYS_OBJCACHE_H_
5610aa77c0SMatthew Dillon #include <sys/objcache.h>
5710aa77c0SMatthew Dillon #endif
58028066b1SMatthew Dillon 
59028066b1SMatthew Dillon #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
60*dae65060Szrj #ifndef _SYS__MALLOC_H_
61*dae65060Szrj #include <sys/_malloc.h>
62*dae65060Szrj #endif
6310aa77c0SMatthew Dillon 
6410aa77c0SMatthew Dillon /*
6510aa77c0SMatthew Dillon  * Register a resource structure type.  Note that the destroy function
6610aa77c0SMatthew Dillon  * will be called on 1->0 transitions (really 1->-0x40000000 transitions),
6710aa77c0SMatthew Dillon  * and the free function
6810aa77c0SMatthew Dillon  * but the free function can be called via an IPI, without the BGL, and
6910aa77c0SMatthew Dillon  * must be carefully coded if it does anything more complex then objcache_put
7010aa77c0SMatthew Dillon  */
7110aa77c0SMatthew Dillon typedef void (*sysref_terminate_func_t)(void *);
72e654922cSMatthew Dillon typedef void (*sysref_lock_func_t)(void *);
73e654922cSMatthew Dillon typedef void (*sysref_unlock_func_t)(void *);
7410aa77c0SMatthew Dillon 
7510aa77c0SMatthew Dillon struct sysref_class {
7610aa77c0SMatthew Dillon 	const char *name;		/* name of the system resource */
7710aa77c0SMatthew Dillon 	malloc_type_t mtype;		/* malloc backing store */
7810aa77c0SMatthew Dillon 	int	proto;			/* RPC protocol id */
7910aa77c0SMatthew Dillon 	int	offset;			/* offset of sysref in resource */
8010aa77c0SMatthew Dillon 	int	objsize;		/* size of the resource structure */
81521f81c7SMatthew Dillon 	int	nom_cache;		/* nominal objects to cache */
8210aa77c0SMatthew Dillon 	int	flags;
8310aa77c0SMatthew Dillon 	struct objcache *oc;		/* object cache */
8410aa77c0SMatthew Dillon 	objcache_ctor_fn *ctor;		/* objcache ctor chaining */
8510aa77c0SMatthew Dillon 	objcache_dtor_fn *dtor;		/* objcache dtor chaining */
8610aa77c0SMatthew Dillon 	struct sysref_ops {
8710aa77c0SMatthew Dillon 		sysref_terminate_func_t terminate;
88e654922cSMatthew Dillon 		sysref_lock_func_t lock;
89e654922cSMatthew Dillon 		sysref_unlock_func_t unlock;
9010aa77c0SMatthew Dillon 	} ops;
9110aa77c0SMatthew Dillon };
9210aa77c0SMatthew Dillon 
9310aa77c0SMatthew Dillon #define SRC_MANAGEDINIT	0x0001		/* do not auto-zero the resource */
9410aa77c0SMatthew Dillon 
9510aa77c0SMatthew Dillon /*
9610aa77c0SMatthew Dillon  * sysref - embedded in resource structures.
9710aa77c0SMatthew Dillon  *
9810aa77c0SMatthew Dillon  * NOTE: The cpuid determining which cpu's RB tree the sysref is
9910aa77c0SMatthew Dillon  * associated with is integrated into the sysref.
10010aa77c0SMatthew Dillon  *
10110aa77c0SMatthew Dillon  * NOTE: A resource can be in varying states of construction or
10210aa77c0SMatthew Dillon  * deconstruction, denoted by having a negative refcnt.  To keep
10310aa77c0SMatthew Dillon  * performance nominal we reuse sysids that are NOT looked up via
10410aa77c0SMatthew Dillon  * syslink (meaning we don't have to adjust their location in the
10510aa77c0SMatthew Dillon  * RB tree).  The objcache is used to cache the RB tree linkage.
10610aa77c0SMatthew Dillon  */
10710aa77c0SMatthew Dillon struct sysref {
10810aa77c0SMatthew Dillon 	RB_ENTRY(sysref) rbnode;	/* per-cpu red-black tree node */
10910aa77c0SMatthew Dillon 	sysid_t	sysid;			/* machine-wide unique sysid */
11010aa77c0SMatthew Dillon 	int	refcnt;			/* normal reference count */
11110aa77c0SMatthew Dillon 	int	flags;
112698331b0SMatthew Dillon 	struct sysref_class *srclass;	/* type of resource and API */
11310aa77c0SMatthew Dillon };
11410aa77c0SMatthew Dillon 
11510aa77c0SMatthew Dillon #define SRF_SYSIDUSED	0x0001		/* sysid was used for access */
11610aa77c0SMatthew Dillon #define SRF_ALLOCATED	0x0002		/* sysref_alloc used to allocate */
11718b4c2bbSMatthew Dillon #define SRF_PUTAWAY	0x0004		/* in objcache */
11810aa77c0SMatthew Dillon 
11910aa77c0SMatthew Dillon RB_HEAD(sysref_rb_tree, sysref);
12010aa77c0SMatthew Dillon RB_PROTOTYPE2(sysref_rb_tree, sysref, rbnode, rb_sysref_compare, sysid_t);
12110aa77c0SMatthew Dillon 
122*dae65060Szrj #endif	/* _KERNEL || _KERNEL_STRUCTURES */
123028066b1SMatthew Dillon 
12410aa77c0SMatthew Dillon /*
12510aa77c0SMatthew Dillon  * Protocol numbers
12610aa77c0SMatthew Dillon  */
12710aa77c0SMatthew Dillon #define SYSREF_PROTO_VMSPACE	0x0020
12810aa77c0SMatthew Dillon #define SYSREF_PROTO_VMOBJECT	0x0021
12910aa77c0SMatthew Dillon #define SYSREF_PROTO_VNODE	0x0022
13010aa77c0SMatthew Dillon #define SYSREF_PROTO_PROCESS	0x0023
13110aa77c0SMatthew Dillon #define SYSREF_PROTO_LWP	0x0024
13210aa77c0SMatthew Dillon #define SYSREF_PROTO_FD		0x0025
13310aa77c0SMatthew Dillon #define SYSREF_PROTO_FP		0x0026
13410aa77c0SMatthew Dillon #define SYSREF_PROTO_SOCKET	0x0027
13510aa77c0SMatthew Dillon #define SYSREF_PROTO_NCP	0x0028
13610aa77c0SMatthew Dillon #define SYSREF_PROTO_BUF	0x0029
13710aa77c0SMatthew Dillon #define SYSREF_PROTO_FSMOUNT	0x002A
13810aa77c0SMatthew Dillon #define SYSREF_PROTO_DEV	0x002B
13910aa77c0SMatthew Dillon 
1403551ce6bSMatthew Dillon #ifdef _KERNEL
1413551ce6bSMatthew Dillon 
1423551ce6bSMatthew Dillon sysid_t allocsysid(void);
1433551ce6bSMatthew Dillon 
1443551ce6bSMatthew Dillon #endif
1453551ce6bSMatthew Dillon 
146*dae65060Szrj #endif	/* !_SYS_SYSREF_H_ */
147