uts: make emu10k non-verbose
[unleashed.git] / include / sys / flock_impl.h
blob657745587eba57eb683065cbb197d85cbac99dba
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2015 Joyent, Inc.
28 #ifndef _SYS_FLOCK_IMPL_H
29 #define _SYS_FLOCK_IMPL_H
31 #include <sys/types.h>
32 #include <sys/fcntl.h> /* flock definition */
33 #include <sys/file.h> /* FREAD etc */
34 #include <sys/flock.h> /* RCMD etc */
35 #include <sys/kmem.h>
36 #include <sys/user.h>
37 #include <sys/thread.h>
38 #include <sys/proc.h>
39 #include <sys/cred.h>
40 #include <sys/debug.h>
41 #include <sys/cmn_err.h>
42 #include <sys/errno.h>
43 #include <sys/systm.h>
44 #include <sys/vnode.h>
45 #include <sys/share.h> /* just to get GETSYSID def */
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
51 struct edge {
52 struct edge *edge_adj_next; /* adjacency list next */
53 struct edge *edge_adj_prev; /* adjacency list prev */
54 struct edge *edge_in_next; /* incoming edges list next */
55 struct edge *edge_in_prev; /* incoming edges list prev */
56 struct lock_descriptor *from_vertex; /* edge emanating from lock */
57 struct lock_descriptor *to_vertex; /* edge pointing to lock */
60 typedef struct edge edge_t;
62 struct lock_descriptor {
63 struct lock_descriptor *l_next; /* next active/sleep lock */
64 struct lock_descriptor *l_prev; /* previous active/sleep lock */
65 struct edge l_edge; /* edge for adj and in lists */
66 struct lock_descriptor *l_stack; /* for stack operations */
67 struct lock_descriptor *l_stack1; /* for stack operations */
68 struct lock_descriptor *l_dstack; /* stack for debug functions */
69 struct edge *l_sedge; /* start edge for graph alg. */
70 int l_index; /* used for barrier count */
71 struct graph *l_graph; /* graph this belongs to */
72 vnode_t *l_vnode; /* vnode being locked */
73 int l_type; /* type of lock */
74 int l_state; /* state described below */
75 uoff_t l_start; /* start offset */
76 uoff_t l_end; /* end offset */
77 flock64_t l_flock; /* original flock request */
78 int l_color; /* color used for graph alg */
79 kcondvar_t l_cv; /* wait condition for lock */
80 int pvertex; /* index to proc vertex */
81 int l_status; /* status described below */
82 flk_nlm_status_t l_nlm_state; /* state of NLM server */
83 flk_callback_t *l_callbacks; /* callbacks, or NULL */
84 zoneid_t l_zoneid; /* zone of request */
85 file_t *l_ofd; /* OFD-style reference */
88 typedef struct lock_descriptor lock_descriptor_t;
91 * Each graph holds locking information for some number of vnodes. The
92 * active and sleeping lists are circular, with a dummy head element.
95 struct graph {
96 kmutex_t gp_mutex; /* mutex for this graph */
97 struct lock_descriptor active_locks;
98 struct lock_descriptor sleeping_locks;
99 int index; /* index of this graph into the hash table */
100 int mark; /* used for coloring the graph */
103 typedef struct graph graph_t;
106 * The possible states a lock can be in. These states are stored in the
107 * 'l_status' member of the 'lock_descriptor_t' structure. All locks start
108 * life in the INITIAL state, and end up in the DEAD state. Possible state
109 * transitions are :
111 * INITIAL--> START --> ACTIVE --> DEAD
113 * --> DEAD
115 * --> ACTIVE --> DEAD (new locks from flk_relation)
117 * --> SLEEPING --> GRANTED --> START --> ACTIVE --> DEAD
119 * --> INTR --> DEAD
121 * --> CANCELLED --> DEAD
123 * --> INTR --> DEAD
125 * --> INTR --> DEAD
127 * --> CANCELLED --> DEAD
129 * --> INTR --> DEAD
131 * Lock transitions are done in the following functions:
132 * --> INITIAL flk_get_lock(), reclock()
133 * --> START flk_execute_request()
134 * --> ACTIVE flk_insert_active_lock()
135 * --> SLEEPING flk_insert_sleeping_lock()
136 * --> GRANTED GRANT_WAKEUP
137 * --> INTERRUPTED INTERRUPT_WAKEUP
138 * --> CANCELLED CANCEL_WAKEUP
139 * --> DEAD reclock(), flk_delete_active_lock(), and
140 * flk_cancel_sleeping_lock()
143 #define FLK_INITIAL_STATE 1 /* Initial state of all requests */
144 #define FLK_START_STATE 2 /* Request has started execution */
145 #define FLK_ACTIVE_STATE 3 /* In active queue */
146 #define FLK_SLEEPING_STATE 4 /* Request is blocked */
147 #define FLK_GRANTED_STATE 5 /* Request is granted */
148 #define FLK_INTERRUPTED_STATE 6 /* Request is interrupted */
149 #define FLK_CANCELLED_STATE 7 /* Request is cancelled */
150 #define FLK_DEAD_STATE 8 /* Request is done - will be deleted */
152 /* flags defining state of locks */
155 * The LLM design has been modified so that lock states are now stored
156 * in the l_status field of lock_descriptor_t. The l_state field is
157 * currently preserved for binary compatibility, but may be modified or
158 * removed in a minor release of Solaris. Note that both of these
159 * fields (and the rest of the lock_descriptor_t structure) are private
160 * to the implementation of the lock manager and should not be used
161 * externally.
164 #define ACTIVE_LOCK 0x0001 /* in active queue */
165 #define SLEEPING_LOCK 0x0002 /* in sleep queue */
166 #define IO_LOCK 0x0004 /* is an IO lock */
167 #define REFERENCED_LOCK 0x0008 /* referenced some where */
168 #define QUERY_LOCK 0x0010 /* querying about lock */
169 #define WILLING_TO_SLEEP_LOCK 0x0020 /* lock can be put in sleep queue */
170 #define RECOMPUTE_LOCK 0x0040 /* used for recomputing dependencies */
171 #define RECOMPUTE_DONE 0x0080 /* used for recomputing dependencies */
172 #define BARRIER_LOCK 0x0100 /* used for recomputing dependencies */
173 #define GRANTED_LOCK 0x0200 /* granted but still in sleep queue */
174 #define CANCELLED_LOCK 0x0400 /* cancelled will be thrown out */
175 #define DELETED_LOCK 0x0800 /* deleted - free at earliest */
176 #define INTERRUPTED_LOCK 0x1000 /* pretend signal */
177 #define LOCKMGR_LOCK 0x2000 /* remote lock (server-side) */
178 #define NBMAND_LOCK 0x8000 /* non-blocking mandatory locking */
180 #define HASH_SIZE 32
181 #define HASH_SHIFT (HASH_SIZE - 1)
182 #define HASH_INDEX(vp) (((uintptr_t)vp >> 7) & HASH_SHIFT)
184 /* extern definitions */
186 extern struct graph *lock_graph[HASH_SIZE];
187 extern struct kmem_cache *flk_edge_cache;
189 /* Clustering: functions called by PXFS */
190 int flk_execute_request(lock_descriptor_t *);
191 void flk_cancel_sleeping_lock(lock_descriptor_t *, int);
192 void flk_set_state(lock_descriptor_t *, int);
193 graph_t *flk_get_lock_graph(vnode_t *, int);
195 /* flags used for readability in flock.c */
197 #define FLK_USE_GRAPH 0 /* don't initialize the lock_graph */
198 #define FLK_INIT_GRAPH 1 /* initialize the lock graph */
199 #define NO_COLOR 0 /* vertex is not colored */
200 #define NO_CHECK_CYCLE 0 /* don't mark vertex's in flk_add_edge */
201 #define CHECK_CYCLE 1 /* mark vertex's in flk_add_edge */
203 #define SAME_OWNER(lock1, lock2) \
204 (((lock1)->l_flock.l_pid == (lock2)->l_flock.l_pid) && \
205 ((lock1)->l_flock.l_sysid == (lock2)->l_flock.l_sysid) && \
206 ((lock1)->l_ofd == (lock2)->l_ofd))
208 #define COLORED(vertex) ((vertex)->l_color == (vertex)->l_graph->mark)
209 #define COLOR(vertex) ((vertex)->l_color = (vertex)->l_graph->mark)
212 * stack data structure and operations
215 #define STACK_INIT(stack) ((stack) = NULL)
216 #define STACK_PUSH(stack, ptr, stack_link) (ptr)->stack_link = (stack),\
217 (stack) = (ptr)
218 #define STACK_POP(stack, stack_link) (stack) = (stack)->stack_link
219 #define STACK_TOP(stack) (stack)
220 #define STACK_EMPTY(stack) ((stack) == NULL)
223 #define ACTIVE_HEAD(gp) (&(gp)->active_locks)
225 #define SLEEPING_HEAD(gp) (&(gp)->sleeping_locks)
227 #define SET_LOCK_TO_FIRST_ACTIVE_VP(gp, lock, vp) \
229 (lock) = (lock_descriptor_t *)vp->v_filocks; \
232 #define SET_LOCK_TO_FIRST_SLEEP_VP(gp, lock, vp) \
234 for ((lock) = SLEEPING_HEAD((gp))->l_next; ((lock) != SLEEPING_HEAD((gp)) && \
235 (lock)->l_vnode != (vp)); (lock) = (lock)->l_next) \
237 (lock) = ((lock) == SLEEPING_HEAD((gp))) ? NULL : (lock); \
240 #define OVERLAP(lock1, lock2) \
241 (((lock1)->l_start <= (lock2)->l_start && \
242 (lock2)->l_start <= (lock1)->l_end) || \
243 ((lock2)->l_start <= (lock1)->l_start && \
244 (lock1)->l_start <= (lock2)->l_end))
246 #define IS_INITIAL(lock) ((lock)->l_status == FLK_INITIAL_STATE)
247 #define IS_ACTIVE(lock) ((lock)->l_status == FLK_ACTIVE_STATE)
248 #define IS_SLEEPING(lock) ((lock)->l_status == FLK_SLEEPING_STATE)
249 #define IS_GRANTED(lock) ((lock)->l_status == FLK_GRANTED_STATE)
250 #define IS_INTERRUPTED(lock) ((lock)->l_status == FLK_INTERRUPTED_STATE)
251 #define IS_CANCELLED(lock) ((lock)->l_status == FLK_CANCELLED_STATE)
252 #define IS_DEAD(lock) ((lock)->l_status == FLK_DEAD_STATE)
254 #define IS_QUERY_LOCK(lock) ((lock)->l_state & QUERY_LOCK)
255 #define IS_RECOMPUTE(lock) ((lock)->l_state & RECOMPUTE_LOCK)
256 #define IS_BARRIER(lock) ((lock)->l_state & BARRIER_LOCK)
257 #define IS_DELETED(lock) ((lock)->l_state & DELETED_LOCK)
258 #define IS_REFERENCED(lock) ((lock)->l_state & REFERENCED_LOCK)
259 #define IS_IO_LOCK(lock) ((lock)->l_state & IO_LOCK)
260 #define IS_WILLING_TO_SLEEP(lock) \
261 ((lock)->l_state & WILLING_TO_SLEEP_LOCK)
262 #define IS_LOCKMGR(lock) ((lock)->l_state & LOCKMGR_LOCK)
263 #define IS_NLM_UP(lock) ((lock)->l_nlm_state == FLK_NLM_UP)
266 * "local" requests don't involve the NFS lock manager in any way.
267 * "remote" requests can be on the server (requests from a remote client),
268 * in which case they should be associated with a local vnode (UFS, tmpfs,
269 * etc.). These requests are flagged with LOCKMGR_LOCK and are made using
270 * kernel service threads. Remote requests can also be on an NFS client,
271 * because the NFS lock manager uses local locking for some of its
272 * bookkeeping. These requests are made by regular user processes.
274 #define IS_LOCAL(lock) (GETSYSID((lock)->l_flock.l_sysid) == 0)
275 #define IS_REMOTE(lock) (! IS_LOCAL(lock))
277 #define BLOCKS(lock1, lock2) (!SAME_OWNER((lock1), (lock2)) && \
278 (((lock1)->l_type == F_WRLCK) || \
279 ((lock2)->l_type == F_WRLCK)) && \
280 OVERLAP((lock1), (lock2)))
282 #define COVERS(lock1, lock2) \
283 (((lock1)->l_start <= (lock2)->l_start) && \
284 ((lock1)->l_end >= (lock2)->l_end))
286 #define IN_LIST_REMOVE(ep) \
288 (ep)->edge_in_next->edge_in_prev = (ep)->edge_in_prev; \
289 (ep)->edge_in_prev->edge_in_next = (ep)->edge_in_next; \
292 #define ADJ_LIST_REMOVE(ep) \
294 (ep)->edge_adj_next->edge_adj_prev = (ep)->edge_adj_prev; \
295 (ep)->edge_adj_prev->edge_adj_next = (ep)->edge_adj_next; \
298 #define NOT_BLOCKED(lock) \
299 ((lock)->l_edge.edge_adj_next == &(lock)->l_edge && !IS_GRANTED(lock))
301 #define GRANT_WAKEUP(lock) \
303 flk_set_state(lock, FLK_GRANTED_STATE); \
304 (lock)->l_state |= GRANTED_LOCK; \
305 cv_signal(&(lock)->l_cv); \
308 #define CANCEL_WAKEUP(lock) \
310 flk_set_state(lock, FLK_CANCELLED_STATE); \
311 (lock)->l_state |= CANCELLED_LOCK; \
312 cv_signal(&(lock)->l_cv); \
315 #define INTERRUPT_WAKEUP(lock) \
317 flk_set_state(lock, FLK_INTERRUPTED_STATE); \
318 (lock)->l_state |= INTERRUPTED_LOCK; \
319 cv_signal(&(lock)->l_cv); \
322 #define REMOVE_SLEEP_QUEUE(lock) \
324 ASSERT(IS_SLEEPING(lock) || IS_GRANTED(lock) || \
325 IS_INTERRUPTED(lock) || IS_CANCELLED(lock)); \
326 (lock)->l_state &= ~SLEEPING_LOCK; \
327 (lock)->l_next->l_prev = (lock)->l_prev; \
328 (lock)->l_prev->l_next = (lock)->l_next; \
329 (lock)->l_next = (lock)->l_prev = (lock_descriptor_t *)NULL; \
332 #define NO_DEPENDENTS(lock) \
333 ((lock)->l_edge.edge_in_next == &(lock)->l_edge)
335 #define GRANT(lock) \
337 (lock)->l_state |= GRANTED_LOCK; \
338 flk_set_state(lock, FLK_GRANTED_STATE); \
341 #define FIRST_IN(lock) ((lock)->l_edge.edge_in_next)
342 #define FIRST_ADJ(lock) ((lock)->l_edge.edge_adj_next)
343 #define HEAD(lock) (&(lock)->l_edge)
344 #define NEXT_ADJ(ep) ((ep)->edge_adj_next)
345 #define NEXT_IN(ep) ((ep)->edge_in_next)
346 #define IN_ADJ_INIT(lock) \
348 (lock)->l_edge.edge_adj_next = (lock)->l_edge.edge_adj_prev = &(lock)->l_edge; \
349 (lock)->l_edge.edge_in_next = (lock)->l_edge.edge_in_prev = &(lock)->l_edge; \
352 #define COPY(lock1, lock2) \
354 (lock1)->l_graph = (lock2)->l_graph; \
355 (lock1)->l_vnode = (lock2)->l_vnode; \
356 (lock1)->l_type = (lock2)->l_type; \
357 (lock1)->l_state = (lock2)->l_state; \
358 (lock1)->l_start = (lock2)->l_start; \
359 (lock1)->l_end = (lock2)->l_end; \
360 (lock1)->l_flock = (lock2)->l_flock; \
361 (lock1)->l_zoneid = (lock2)->l_zoneid; \
362 (lock1)->pvertex = (lock2)->pvertex; \
366 * Clustering
368 /* Routines to set and get the NLM state in a lock request */
369 #define SET_NLM_STATE(lock, nlm_state) ((lock)->l_nlm_state = nlm_state)
370 #define GET_NLM_STATE(lock) ((lock)->l_nlm_state)
372 * NLM registry abstraction:
373 * Abstraction overview:
374 * This registry keeps track of the NLM servers via their nlmids
375 * that have requested locks at the LLM this registry is associated
376 * with.
378 /* Routines to manipulate the NLM registry object state */
379 #define FLK_REGISTRY_IS_NLM_UNKNOWN(nlmreg, nlmid) \
380 ((nlmreg)[nlmid] == FLK_NLM_UNKNOWN)
381 #define FLK_REGISTRY_IS_NLM_UP(nlmreg, nlmid) \
382 ((nlmreg)[nlmid] == FLK_NLM_UP)
383 #define FLK_REGISTRY_ADD_NLMID(nlmreg, nlmid) \
384 ((nlmreg)[nlmid] = FLK_NLM_UP)
385 #define FLK_REGISTRY_CHANGE_NLM_STATE(nlmreg, nlmid, state) \
386 ((nlmreg)[nlmid] = state)
388 /* Indicates the effect of executing a request on the existing locks */
390 #define FLK_UNLOCK 0x1 /* request unlocks the existing lock */
391 #define FLK_DOWNGRADE 0x2 /* request downgrades the existing lock */
392 #define FLK_UPGRADE 0x3 /* request upgrades the existing lock */
393 #define FLK_STAY_SAME 0x4 /* request type is same as existing lock */
396 /* proc graph definitions */
399 * Proc graph is the global process graph that maintains information
400 * about the dependencies between processes. An edge is added between two
401 * processes represented by proc_vertex's A and B, iff there exists l1
402 * owned by process A in any of the lock_graph's dependent on l2
403 * (thus having an edge to l2) owned by process B.
405 struct proc_vertex {
406 pid_t pid; /* pid of the process */
407 long sysid; /* sysid of the process */
408 struct proc_edge *edge; /* adajcent edges of this process */
409 int incount; /* Number of inedges to this process */
410 struct proc_edge *p_sedge; /* used for implementing stack alg. */
411 struct proc_vertex *p_stack; /* used for stack alg. */
412 int atime; /* used for cycle detection algorithm */
413 int dtime; /* used for cycle detection algorithm */
414 int index; /* index into the array of proc_graph vertices */
417 typedef struct proc_vertex proc_vertex_t;
419 struct proc_edge {
420 struct proc_edge *next; /* next edge in adjacency list */
421 int refcount; /* reference count of this edge */
422 struct proc_vertex *to_proc; /* process this points to */
425 typedef struct proc_edge proc_edge_t;
428 #define PROC_CHUNK 100
430 struct proc_graph {
431 struct proc_vertex **proc; /* list of proc_vertexes */
432 int gcount; /* list size */
433 int free; /* number of free slots in the list */
434 int mark; /* used for graph coloring */
437 typedef struct proc_graph proc_graph_t;
439 extern struct proc_graph pgraph;
441 #define PROC_SAME_OWNER(lock, pvertex) \
442 (((lock)->l_flock.l_pid == (pvertex)->pid) && \
443 ((lock)->l_flock.l_sysid == (pvertex)->sysid))
445 #define PROC_ARRIVE(pvertex) ((pvertex)->atime = pgraph.mark)
446 #define PROC_DEPART(pvertex) ((pvertex)->dtime = pgraph.mark)
447 #define PROC_ARRIVED(pvertex) ((pvertex)->atime == pgraph.mark)
448 #define PROC_DEPARTED(pvertex) ((pvertex)->dtime == pgraph.mark)
450 #ifdef __cplusplus
452 #endif
454 #endif /* _SYS_FLOCK_IMPL_H */