HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / sys / ccms.h
blob6d1866f4f24ac2da436927f3692e6a8e9cf97412
1 /*
2 * Copyright (c) 2006 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:
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.
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.
34 * $DragonFly: src/sys/sys/ccms.h,v 1.1 2006/08/23 06:45:40 dillon Exp $
37 * CCMS - Cache Coherency Management System. These structures are used
38 * to manage cache coherency and locking for an object. Cache Coherency is
39 * managed at byte granularity with 64 bit offset ranges.
41 * Management is broken into two distinct pieces: (1) Local shared/exclusive
42 * locks which essentially replace traditional vnode locks and (2) local
43 * cache state which interacts with other hosts and follows a MESI-like model.
45 * The core to the entire module is the 'CST' (Cache State Tree) structure
46 * which stores both pieces of information in a red-black tree styled data
47 * structure. CSTs are non-overlapping offset-ranged entities. Other
48 * higher level structures govern how CSTs in the red-black tree or cut up
49 * or merged.
52 #ifndef _SYS_CCMS_H_
53 #define _SYS_CCMS_H_
55 #ifndef _SYS_TYPES_H_
56 #include <sys/types.h>
57 #endif
58 #ifndef _SYS_PARAM_H_
59 #include <sys/param.h>
60 #endif
61 #ifndef _SYS_SERIALIZE_H_
62 #include <sys/serialize.h>
63 #endif
64 #ifndef _SYS_TREE_H_
65 #include <sys/tree.h>
66 #endif
69 * CCMS uses a red-black tree to sort CSTs.
71 RB_HEAD(ccms_rb_tree, ccms_cst);
72 RB_PROTOTYPE3(ccms_rb_tree, ccms_cst, rbnode, ccms_cst_cmp, off_t);
74 struct ccms_lock;
75 struct ccms_cst;
78 * ccms_state_t - CCMS cache states
80 * CCMS uses an extended MESI caching model. There are two extension
81 * states, MASTER and SLAVE, which represents dirty data which has not been
82 * synchronized to backing store but which nevertheless is being shared
83 * between distinct caches. These states are designed to allow data
84 * to be shared between nodes in a cluster without having to wait for it
85 * to be synchronized with its backing store.
87 * SLAVE - A shared state where the master copy of the data is being
88 * held by a foreign cache rather then by backing store.
89 * This state implies that the backing store may contain stale
90 * data.
92 * MASTER - A shared state where the master copy of the data is being
93 * held locally. Zero or more foreign caches may be holding
94 * a copy of our data, so we cannot modify it without
95 * invalidating those caches. This state implies that the
96 * backing store may contain stale data.
98 * MASTER differs from MODIFIED in that the data is read-only
99 * due to the existance of foreign copies. However, even though
100 * the data is read-only, it is ALSO DIRTY because the backing
101 * store has not been synchronized
103 * NOTE! The cache state represents the worst case cache state for caching
104 * elements such as the buffer cache or VM page cache or the vnode attribute
105 * cache (or other things) within the specified range. It does NOT mean
106 * that the local machine actually has all of the requested data in-hand.
108 typedef enum ccms_state {
109 CCMS_STATE_INVALID = 0,
110 CCMS_STATE_SHARED, /* clean, read-only, from backing store */
111 CCMS_STATE_SLAVE, /* clean, read-only, from master */
112 CCMS_STATE_MASTER, /* dirty, read-only, shared master copy */
113 CCMS_STATE_EXCLUSIVE, /* clean, read-only, exclusive */
114 CCMS_STATE_MODIFIED /* clean or dirty, read-write, exclusive */
115 } ccms_state_t;
118 * ccms_ltype_t - local access control lock state
120 * Note: A MODIFYING lock is an exclusive lock where the caller intends to
121 * make a modification, such as issuing a WRITE. The difference between the
122 * two is in how the cache state is effected by the lock. The distinction
123 * exists because there are many situations where the governing structure
124 * on the local machine needs to be locked exclusively, but the underlying
125 * data cache does not.
127 * lock type cache state
128 * --------- ---------
129 * SHARED >= shared
130 * EXCLUSIVE >= shared
131 * MODIFYING >= exclusive
133 typedef enum ccms_ltype {
134 CCMS_LTYPE_SHARED = 0, /* shared lock on the range */
135 CCMS_LTYPE_EXCLUSIVE, /* exclusive lock on the range */
136 CCMS_LTYPE_MODIFYING /* modifying lock on the range */
137 } ccms_ltype_t;
140 * The CCMS ABI information structure. This structure contains ABI
141 * calls to resolve incompatible cache states.
143 struct ccms_info {
144 int (*ccms_set_cache)(struct ccms_info *, struct ccms_lock *, ccms_state_t);
145 void *data;
146 /* XXX */
150 * A CCMS dataspace, typically stored in a vnode or VM object. The primary
151 * reference is to the ccms_dataspace representing the local machine. The
152 * chain field is used to link ccms_dataspace's representing other machines.
153 * These foreign representations typically only contain summary 'worst-case'
154 * CSTs. The chain only needs to be followed if a CST has a cache state
155 * that is incompatible with the request.
157 struct ccms_dataspace {
158 struct ccms_rb_tree tree;
159 struct ccms_info *info;
160 struct ccms_dataspace *chain;
161 ccms_state_t defstate;
165 * The CCMS locking element - represents a high level locking request,
166 * such as used by read, write, and truncate operations. These requests
167 * are not organized into any tree but instead are shadowed by items in
168 * the actual cache state tree (ccms_cst). There are no direct links
169 * between a ccms_lock and the underlying CST items, only reference count
170 * fields in the CST item.
172 * When a CCMS lock is established the cache state of the underlying elements
173 * is adjusted to meet the requirements of the lock. The cache state
174 * requirements are infered by the lock type:
176 * NOTE: Ranges may include negative offsets. These are typically used to
177 * represent meta-data.
179 * local lock cache state
180 * ----------------- --------------------
181 * SHARED - SHARED must not be invalid
182 * EXCLUSIVE - EXCLUSIVE must not be invalid
183 * MODIFYING - EXCLUSIVE must be EXCLUSIVE or MODIFIED
185 struct ccms_lock {
186 struct ccms_dataspace *ds;
187 off_t beg_offset;
188 off_t end_offset;
189 ccms_ltype_t ltype;
193 * CCMS cache state tree element (CST) - represents the actual cache
194 * management state for a data space. The cache state tree is a
195 * non-overlaping red-black tree containing ranged ccms_cst structures
196 * which reflect the resolved state for all current high level locking
197 * requests. For example, two overlapping ccms_lock requests for shared
198 * access would typically be represented by three non-overlapping ccms_cst
199 * items in the CST. The CST item representing the overlapped portion of
200 * the ccms_lock requests would have ref count of 2 while the other CST
201 * items would have a ref count of 1.
203 * [lock request #01]
204 * [lock request #02]
205 * [--cst--][--cst--][--cst--]
207 * CSTs are partitioned so their edges line up to all current and pending
208 * ccms_lock requests. CSTs are re-merged whenever possible. A freshly
209 * initialized database typically has a single CST representing the default
210 * cache state for the host.
212 * A CST represents *TWO* different things. First, it represents local
213 * locks held on data ranges. Second, it represents the best-case cache
214 * state for data cached on the local machine for local<->remote host
215 * interactions.
217 * Any arbitrary data range within a dataspace can be locked shared or
218 * exclusive. Obtaining a lock has the side effect of potentially modifying
219 * the cache state. A positive sharecount in a CST indicates that a
220 * shared access lock is being held. A negative sharecount indicates an
221 * exclusive access lock is being held on the range. A MODIFYING lock
222 * type is just an exclusive lock but one which effects the cache state
223 * differently.
225 * The end offset is byte-inclusive, allowing the entire 64 bit data space
226 * to be represented without overflowing the edge case. For example, a
227 * 64 byte area might be represented as (0,63). The offsets are SIGNED
228 * entities. Negative offsets are often used to represent meta-data
229 * such as ownership and permissions. The file size is typically cached as a
230 * side effect of file operations occuring at the file EOF rather then
231 * combined with ownership and permissions.
233 struct ccms_cst {
234 RB_ENTRY(ccms_cst) rbnode; /* stored in a red-black tree */
235 off_t beg_offset;
236 off_t end_offset;
237 ccms_state_t state; /* local cache state */
238 int sharecount; /* shared or exclusive lock count */
239 int modifycount; /* number of modifying exclusive lks */
240 int blocked; /* indicates a blocked lock request */
241 int xrefs; /* lock overlap references */
242 int lrefs; /* left edge refs */
243 int rrefs; /* right edge refs */
246 typedef struct ccms_info *ccms_info_t;
247 typedef struct ccms_dataspace *ccms_dataspace_t;
248 typedef struct ccms_lock *ccms_lock_t;
249 typedef struct ccms_cst *ccms_cst_t;
252 * Kernel API
254 #ifdef _KERNEL
256 static __inline
257 void
258 ccms_lock_init(ccms_lock_t lock, off_t beg_offset, off_t end_offset,
259 ccms_ltype_t ltype)
261 lock->beg_offset = beg_offset;
262 lock->end_offset = end_offset;
263 lock->ltype = ltype;
266 void ccms_dataspace_init(ccms_dataspace_t);
267 void ccms_dataspace_destroy(ccms_dataspace_t);
268 int ccms_lock_get(ccms_dataspace_t, ccms_lock_t);
269 int ccms_lock_get_uio(ccms_dataspace_t, ccms_lock_t, struct uio *);
270 int ccms_lock_put(ccms_dataspace_t, ccms_lock_t);
272 #endif
274 #endif