1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_NFS4_DB_IMPL_H
28 #define	_NFS4_DB_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * This is a private header file.  Applications should not directly include
34  * this file.
35  */
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #define	SEARCH_DEBUG	0x0001
42 #define	CREATE_DEBUG	0x0002
43 #define	CACHED_DEBUG	0x0004
44 #define	DESTROY_DEBUG	0x0008
45 #define	REAP_DEBUG	0x0010
46 #define	OTHER_DEBUG	0x0020
47 #define	WALK_DEBUG	0x0040
48 
49 /*
50  * A database is made up of a collection of tables.
51  * Tables are in turn made up of a collection of
52  * entries. Each table may haveone or more indices
53  * associtated with it.
54  */
55 
56 /* Private implementation */
57 typedef struct rfs4_link {
58 	struct rfs4_link *next;
59 	struct rfs4_link *prev;
60 	rfs4_dbe_t *entry;
61 } rfs4_link;
62 
63 struct rfs4_dbe {
64 	kmutex_t lock[1];		/* Exclusive lock for entry */
65 	uint32_t refcnt;		/* # of references */
66 	unsigned skipsearch:1;		/* skip search */
67 	unsigned invalid:1;		/* invalid/"freed" entry */
68 	unsigned reserved:31;
69 	time_t	 time_rele;		/* Time of last rele */
70 	id_t	 id;			/* unique identifier */
71 	kcondvar_t cv[1];
72 	rfs4_entry_t data;
73 	rfs4_table_t *table;
74 	rfs4_link indices[1];		/* Array of indices for entry */
75 };
76 
77 typedef struct rfs4_bucket {
78 	krwlock_t lock[1];			/* lock hash chain */
79 	rfs4_link *head;
80 } rfs4_bucket;
81 
82 struct rfs4_index {
83 	uint32_t tblidx;			/* which indice in entry */
84 	bool_t createable;			/* Can create entries */
85 	rfs4_table_t *table;			/* Pointer to table */
86 	char *keyname;				/* String rep of key */
87 	rfs4_bucket *buckets;			/* Hash buckets */
88 	uint32_t (*hash)(void *key);		/* Given key find bucket */
89 	bool_t (*compare)(rfs4_entry_t, void *key);	/* Key match entry? */
90 	void *(*mkkey)(rfs4_entry_t);		/* Given data generate a key */
91 	struct rfs4_index *inext;		/* next index on table */
92 };
93 
94 struct rfs4_table {
95 	rfs4_table_t *tnext;			/* next table in db */
96 	struct rfs4_database *dbp;		/* db that holds this table */
97 	krwlock_t t_lock[1];			/* lock table for resize */
98 	kmutex_t lock[1];			/* mutex for count and cached */
99 	char *name;				/* Table name */
100 	id_space_t *id_space;			/* space for unique entry ids */
101 	time_t	min_cache_time;			/* How long to cache entries */
102 	time_t	max_cache_time;			/* How long to cache entries */
103 	uint32_t usize;				/* User entry size */
104 	uint32_t maxentries;			/* max # of entries in table */
105 	uint32_t len;				/* # of buckets in table */
106 	uint32_t count;				/* # of entries in table */
107 	uint32_t idxcnt;			/* # of indices in table */
108 	uint32_t maxcnt;			/* max # of indices */
109 	uint32_t ccnt;				/* # of creatable entries */
110 	rfs4_index_t *indices;			/* list of indices */
111 	/* Given entry and data construct entry */
112 	bool_t (*create)(rfs4_entry_t, void *data);
113 	void (*destroy)(rfs4_entry_t);		/* Destroy entry */
114 	bool_t (*expiry)(rfs4_entry_t);		/* Has this entry expired */
115 	kmem_cache_t *mem_cache;		/* Cache for table entries */
116 	uint32_t debug;				/* Debug Flags */
117 	/* set of vars used for managing the reaper thread */
118 	unsigned	reaper_shutdown:1;	/* table shutting down? */
119 	kcondvar_t reaper_wait;			/* reaper thread waits here */
120 	kmutex_t	reaper_cv_lock;		/* lock used for cpr wait */
121 	callb_cpr_t	reaper_cpr_info;	/* cpr the reaper thread */
122 };
123 
124 struct rfs4_database {
125 	kmutex_t lock[1];
126 	uint32_t debug_flags;			/* Table debug flags to set */
127 	uint32_t shutdown_count;		/* count to manage shutdown */
128 	kcondvar_t shutdown_wait;		/* where the shutdown waits */
129 	rfs4_table_t *tables;			/* list of tables in db */
130 };
131 
132 #define	RFS4_RECLAIM_PERCENT 10
133 #define	RFS4_REAP_INTERVAL 300
134 
135 #define	HASH(idx, key) (idx->hash(key) % idx->table->len)
136 
137 #define	ENQUEUE(head, l) { \
138 	(l)->prev = NULL; \
139 	(l)->next = (head); \
140 	if ((l)->next) \
141 	    (l)->next->prev = (l); \
142 	(head) = (l); \
143 }
144 
145 #define	DEQUEUE(head, l) { \
146 	if ((l)->prev) \
147 		(l)->prev->next = (l)->next; \
148 	else \
149 		(head) = (l)->next; \
150 	if ((l)->next) \
151 		(l)->next->prev = (l)->prev; \
152 }
153 
154 #define	INVALIDATE_ADDR(a) ((a) = (void *)((unsigned long)(a) | 1L))
155 #define	VALIDATE_ADDR(a) ((a) = (void *)((unsigned long)(a) & ~1L))
156 #define	INVALID_ADDR(a) (((unsigned long)(a) & 1L))
157 #define	INVALID_LINK(l) (INVALID_ADDR(l->entry))
158 
159 #define	ENQUEUE_IDX(bp, l) { \
160 	rw_enter((bp)->lock, RW_WRITER); \
161 	ENQUEUE((bp)->head, l); \
162 	VALIDATE_ADDR((l)->entry); \
163 	rw_exit((bp)->lock); \
164 }
165 
166 #define	DEQUEUE_IDX(bp, l) { \
167 	rw_enter((bp)->lock, RW_WRITER); \
168 	INVALIDATE_ADDR((l)->entry); \
169 	DEQUEUE((bp)->head, l); \
170 	rw_exit((bp)->lock); \
171 }
172 
173 #ifdef	__cplusplus
174 }
175 #endif
176 
177 #endif /* _NFS4_DB_IMPL_H */
178