xref: /titanic_44/usr/src/uts/common/sys/flock_impl.h (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
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 _SYS_FLOCK_IMPL_H
28 #define	_SYS_FLOCK_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/fcntl.h>		/* flock definition */
34 #include <sys/file.h>		/* FREAD etc */
35 #include <sys/flock.h>		/* RCMD etc */
36 #include <sys/kmem.h>
37 #include <sys/user.h>
38 #include <sys/thread.h>
39 #include <sys/proc.h>
40 #include <sys/cred.h>
41 #include <sys/debug.h>
42 #include <sys/cmn_err.h>
43 #include <sys/errno.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/share.h>		/* just to get GETSYSID def */
47 
48 #ifdef	__cplusplus
49 extern "C" {
50 #endif
51 
52 struct	edge {
53 	struct	edge	*edge_adj_next;	/* adjacency list next */
54 	struct	edge	*edge_adj_prev; /* adjacency list prev */
55 	struct	edge	*edge_in_next;	/* incoming edges list next */
56 	struct	edge	*edge_in_prev;	/* incoming edges list prev */
57 	struct 	lock_descriptor	*from_vertex;	/* edge emanating from lock */
58 	struct 	lock_descriptor	*to_vertex;	/* edge pointing to lock */
59 };
60 
61 typedef	struct	edge	edge_t;
62 
63 struct lock_descriptor {
64 	struct	lock_descriptor	*l_next;	/* next active/sleep lock */
65 	struct	lock_descriptor	*l_prev;	/* previous active/sleep lock */
66 	struct	edge		l_edge;		/* edge for adj and in lists */
67 	struct	lock_descriptor	*l_stack;	/* for stack operations */
68 	struct	lock_descriptor	*l_stack1;	/* for stack operations */
69 	struct 	lock_descriptor *l_dstack;	/* stack for debug functions */
70 	struct	edge		*l_sedge;	/* start edge for graph alg. */
71 			int	l_index; 	/* used for barrier count */
72 		struct	graph	*l_graph;	/* graph this belongs to */
73 		vnode_t		*l_vnode;	/* vnode being locked */
74 			int	l_type;		/* type of lock */
75 			int	l_state;	/* state described below */
76 		u_offset_t	l_start;	/* start offset */
77 		u_offset_t	l_end;		/* end offset */
78 		flock64_t	l_flock;	/* original flock request */
79 			int	l_color;	/* color used for graph alg */
80 		kcondvar_t	l_cv;		/* wait condition for lock */
81 		int		pvertex;	/* index to proc vertex */
82 			int	l_status;	/* status described below */
83 		flk_nlm_status_t l_nlm_state;	/* state of NLM server */
84 		flk_callback_t	*l_callbacks;	/* callbacks, or NULL */
85 		zoneid_t	l_zoneid;	/* zone of request */
86 };
87 
88 typedef struct 	lock_descriptor	lock_descriptor_t;
89 
90 /*
91  * Each graph holds locking information for some number of vnodes.  The
92  * active and sleeping lists are circular, with a dummy head element.
93  */
94 
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 */
101 };
102 
103 typedef	struct	graph	graph_t;
104 
105 /*
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 :
110  *
111  * INITIAL--> START    --> ACTIVE    --> DEAD
112  *
113  *                     --> DEAD
114  *
115  *        --> ACTIVE   --> DEAD          (new locks from flk_relation)
116  *
117  *        --> SLEEPING --> GRANTED   --> START     --> ACTIVE --> DEAD
118  *
119  *                                   --> INTR      --> DEAD
120  *
121  *                                   --> CANCELLED --> DEAD
122  *
123  *                                                 --> INTR   --> DEAD
124  *
125  *                     --> INTR      --> DEAD
126  *
127  *                     --> CANCELLED --> DEAD
128  *
129  *                                   --> INTR      --> DEAD
130  *
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()
141  */
142 
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 */
151 
152 /* flags defining state of locks */
153 
154 /*
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.
162  */
163 
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 /* Clustering: flag for PXFS locks */
179 #define	PXFS_LOCK		0x4000	/* lock created by PXFS file system */
180 #define	NBMAND_LOCK		0x8000	/* non-blocking mandatory locking */
181 
182 #define	HASH_SIZE	32
183 #define	HASH_SHIFT	(HASH_SIZE - 1)
184 #define	HASH_INDEX(vp)	(((uintptr_t)vp >> 7) & HASH_SHIFT)
185 
186 /* extern definitions */
187 
188 extern struct graph	*lock_graph[HASH_SIZE];
189 extern struct kmem_cache *flk_edge_cache;
190 
191 /* Clustering: functions called by PXFS */
192 int flk_execute_request(lock_descriptor_t *);
193 void flk_cancel_sleeping_lock(lock_descriptor_t *, int);
194 void flk_set_state(lock_descriptor_t *, int);
195 graph_t *flk_get_lock_graph(vnode_t *, int);
196 
197 /* flags used for readability in flock.c */
198 
199 #define	FLK_USE_GRAPH	0	/* don't initialize the lock_graph */
200 #define	FLK_INIT_GRAPH	1	/* initialize the lock graph */
201 #define	NO_COLOR	0	/* vertex is not colored */
202 #define	NO_CHECK_CYCLE	0	/* don't mark vertex's in flk_add_edge */
203 #define	CHECK_CYCLE	1	/* mark vertex's in flk_add_edge */
204 
205 #define	SAME_OWNER(lock1, lock2)	\
206 	(((lock1)->l_flock.l_pid == (lock2)->l_flock.l_pid) && \
207 		((lock1)->l_flock.l_sysid == (lock2)->l_flock.l_sysid))
208 
209 #define	COLORED(vertex)		((vertex)->l_color == (vertex)->l_graph->mark)
210 #define	COLOR(vertex)		((vertex)->l_color = (vertex)->l_graph->mark)
211 
212 /*
213  * stack data structure and operations
214  */
215 
216 #define	STACK_INIT(stack)	((stack) = NULL)
217 #define	STACK_PUSH(stack, ptr, stack_link)	(ptr)->stack_link = (stack),\
218 				(stack) = (ptr)
219 #define	STACK_POP(stack, stack_link)	(stack) = (stack)->stack_link
220 #define	STACK_TOP(stack)	(stack)
221 #define	STACK_EMPTY(stack)	((stack) == NULL)
222 
223 
224 #define	ACTIVE_HEAD(gp)	(&(gp)->active_locks)
225 
226 #define	SLEEPING_HEAD(gp)	(&(gp)->sleeping_locks)
227 
228 #define	SET_LOCK_TO_FIRST_ACTIVE_VP(gp, lock, vp) \
229 { \
230 	(lock) = (lock_descriptor_t *)vp->v_filocks;	\
231 }
232 
233 #define	SET_LOCK_TO_FIRST_SLEEP_VP(gp, lock, vp) \
234 { \
235 for ((lock) = SLEEPING_HEAD((gp))->l_next; ((lock) != SLEEPING_HEAD((gp)) && \
236 			(lock)->l_vnode != (vp)); (lock) = (lock)->l_next) \
237 			; \
238 (lock) = ((lock) == SLEEPING_HEAD((gp))) ? NULL : (lock); \
239 }
240 
241 #define	OVERLAP(lock1, lock2) \
242 	(((lock1)->l_start <= (lock2)->l_start && \
243 		(lock2)->l_start <= (lock1)->l_end) || \
244 	((lock2)->l_start <= (lock1)->l_start && \
245 		(lock1)->l_start <= (lock2)->l_end))
246 
247 #define	IS_INITIAL(lock)	((lock)->l_status == FLK_INITIAL_STATE)
248 #define	IS_ACTIVE(lock)		((lock)->l_status == FLK_ACTIVE_STATE)
249 #define	IS_SLEEPING(lock)	((lock)->l_status == FLK_SLEEPING_STATE)
250 #define	IS_GRANTED(lock)	((lock)->l_status == FLK_GRANTED_STATE)
251 #define	IS_INTERRUPTED(lock)	((lock)->l_status == FLK_INTERRUPTED_STATE)
252 #define	IS_CANCELLED(lock)	((lock)->l_status == FLK_CANCELLED_STATE)
253 #define	IS_DEAD(lock)		((lock)->l_status == FLK_DEAD_STATE)
254 
255 #define	IS_QUERY_LOCK(lock)	((lock)->l_state & QUERY_LOCK)
256 #define	IS_RECOMPUTE(lock)	((lock)->l_state & RECOMPUTE_LOCK)
257 #define	IS_BARRIER(lock)	((lock)->l_state & BARRIER_LOCK)
258 #define	IS_DELETED(lock)	((lock)->l_state & DELETED_LOCK)
259 #define	IS_REFERENCED(lock)	((lock)->l_state & REFERENCED_LOCK)
260 #define	IS_IO_LOCK(lock)	((lock)->l_state & IO_LOCK)
261 #define	IS_WILLING_TO_SLEEP(lock)	\
262 		((lock)->l_state & WILLING_TO_SLEEP_LOCK)
263 #define	IS_LOCKMGR(lock)	((lock)->l_state & LOCKMGR_LOCK)
264 #define	IS_NLM_UP(lock)		((lock)->l_nlm_state == FLK_NLM_UP)
265 /* Clustering: Macro for PXFS locks */
266 #define	IS_PXFS(lock)		((lock)->l_state & PXFS_LOCK)
267 
268 /*
269  * "local" requests don't involve the NFS lock manager in any way.
270  * "remote" requests can be on the server (requests from a remote client),
271  * in which case they should be associated with a local vnode (UFS, tmpfs,
272  * etc.).  These requests are flagged with LOCKMGR_LOCK and are made using
273  * kernel service threads.  Remote requests can also be on an NFS client,
274  * because the NFS lock manager uses local locking for some of its
275  * bookkeeping.  These requests are made by regular user processes.
276  */
277 #define	IS_LOCAL(lock)	(GETSYSID((lock)->l_flock.l_sysid) == 0)
278 #define	IS_REMOTE(lock)	(! IS_LOCAL(lock))
279 
280 /* Clustering: Return value for blocking PXFS locks */
281 /*
282  * For PXFS locks, reclock() will return this error code for requests that
283  * need to block
284  */
285 #define	PXFS_LOCK_BLOCKED -1
286 
287 /* Clustering: PXFS callback function */
288 /*
289  * This function is a callback from the LLM into the PXFS server module.  It
290  * is initialized as a weak stub, and is functional when the pxfs server module
291  * is loaded.
292  */
293 extern void cl_flk_state_transition_notify(lock_descriptor_t *lock,
294     int old_state, int new_state);
295 
296 #define	BLOCKS(lock1, lock2)	(!SAME_OWNER((lock1), (lock2)) && \
297 					(((lock1)->l_type == F_WRLCK) || \
298 					((lock2)->l_type == F_WRLCK)) && \
299 					OVERLAP((lock1), (lock2)))
300 
301 #define	COVERS(lock1, lock2)	\
302 		(((lock1)->l_start <= (lock2)->l_start) && \
303 			((lock1)->l_end >= (lock2)->l_end))
304 
305 #define	IN_LIST_REMOVE(ep)	\
306 	{ \
307 	(ep)->edge_in_next->edge_in_prev = (ep)->edge_in_prev; \
308 	(ep)->edge_in_prev->edge_in_next = (ep)->edge_in_next; \
309 	}
310 
311 #define	ADJ_LIST_REMOVE(ep)	\
312 	{ \
313 	(ep)->edge_adj_next->edge_adj_prev = (ep)->edge_adj_prev; \
314 	(ep)->edge_adj_prev->edge_adj_next = (ep)->edge_adj_next; \
315 	}
316 
317 #define	NOT_BLOCKED(lock)	\
318 	((lock)->l_edge.edge_adj_next == &(lock)->l_edge && !IS_GRANTED(lock))
319 
320 #define	GRANT_WAKEUP(lock)	\
321 	{	\
322 		flk_set_state(lock, FLK_GRANTED_STATE); \
323 		(lock)->l_state |= GRANTED_LOCK; \
324 		/* \
325 		 * Clustering: PXFS locks do not sleep in the LLM, \
326 		 * so there is no need to signal them \
327 		 */ \
328 		if (!IS_PXFS(lock)) { \
329 			cv_signal(&(lock)->l_cv); \
330 		} \
331 	}
332 
333 #define	CANCEL_WAKEUP(lock)	\
334 	{ \
335 		flk_set_state(lock, FLK_CANCELLED_STATE); \
336 		(lock)->l_state |= CANCELLED_LOCK; \
337 		/* \
338 		 * Clustering: PXFS locks do not sleep in the LLM, \
339 		 * so there is no need to signal them \
340 		 */ \
341 		if (!IS_PXFS(lock)) { \
342 			cv_signal(&(lock)->l_cv); \
343 		} \
344 	}
345 
346 #define	INTERRUPT_WAKEUP(lock)	\
347 	{ \
348 		flk_set_state(lock, FLK_INTERRUPTED_STATE); \
349 		(lock)->l_state |= INTERRUPTED_LOCK; \
350 		/* \
351 		 * Clustering: PXFS locks do not sleep in the LLM, \
352 		 * so there is no need to signal them \
353 		 */ \
354 		if (!IS_PXFS(lock)) { \
355 			cv_signal(&(lock)->l_cv); \
356 		} \
357 	}
358 
359 #define	REMOVE_SLEEP_QUEUE(lock)	\
360 	{ \
361 	ASSERT(IS_SLEEPING(lock) || IS_GRANTED(lock) || \
362 	    IS_INTERRUPTED(lock) || IS_CANCELLED(lock)); \
363 	(lock)->l_state &= ~SLEEPING_LOCK; \
364 	(lock)->l_next->l_prev = (lock)->l_prev; \
365 	(lock)->l_prev->l_next = (lock)->l_next; \
366 	(lock)->l_next = (lock)->l_prev = (lock_descriptor_t *)NULL; \
367 	}
368 
369 #define	NO_DEPENDENTS(lock)	\
370 	((lock)->l_edge.edge_in_next == &(lock)->l_edge)
371 
372 #define	GRANT(lock)	\
373 	{ \
374 	(lock)->l_state |= GRANTED_LOCK; \
375 	flk_set_state(lock, FLK_GRANTED_STATE); \
376 	}
377 
378 #define	FIRST_IN(lock)	((lock)->l_edge.edge_in_next)
379 #define	FIRST_ADJ(lock)	((lock)->l_edge.edge_adj_next)
380 #define	HEAD(lock)	(&(lock)->l_edge)
381 #define	NEXT_ADJ(ep)	((ep)->edge_adj_next)
382 #define	NEXT_IN(ep)	((ep)->edge_in_next)
383 #define	IN_ADJ_INIT(lock)	\
384 {	\
385 (lock)->l_edge.edge_adj_next = (lock)->l_edge.edge_adj_prev = &(lock)->l_edge; \
386 (lock)->l_edge.edge_in_next = (lock)->l_edge.edge_in_prev = &(lock)->l_edge; \
387 }
388 
389 #define	COPY(lock1, lock2)	\
390 {	\
391 (lock1)->l_graph = (lock2)->l_graph; \
392 (lock1)->l_vnode = (lock2)->l_vnode; \
393 (lock1)->l_type = (lock2)->l_type; \
394 (lock1)->l_state = (lock2)->l_state; \
395 (lock1)->l_start = (lock2)->l_start; \
396 (lock1)->l_end = (lock2)->l_end; \
397 (lock1)->l_flock = (lock2)->l_flock; \
398 (lock1)->l_zoneid = (lock2)->l_zoneid; \
399 (lock1)->pvertex = (lock2)->pvertex; \
400 }
401 
402 /*
403  * Clustering
404  */
405 /* Routines to set and get the NLM state in a lock request */
406 #define	SET_NLM_STATE(lock, nlm_state)	((lock)->l_nlm_state = nlm_state)
407 #define	GET_NLM_STATE(lock)	((lock)->l_nlm_state)
408 /*
409  * NLM registry abstraction:
410  *   Abstraction overview:
411  *   This registry keeps track of the NLM servers via their nlmids
412  *   that have requested locks at the LLM this registry is associated
413  *   with.
414  */
415 /* Routines to manipulate the NLM registry object state */
416 #define	FLK_REGISTRY_IS_NLM_UNKNOWN(nlmreg, nlmid) \
417 	    ((nlmreg)[nlmid] == FLK_NLM_UNKNOWN)
418 #define	FLK_REGISTRY_IS_NLM_UP(nlmreg, nlmid) \
419 	    ((nlmreg)[nlmid] == FLK_NLM_UP)
420 #define	FLK_REGISTRY_ADD_NLMID(nlmreg, nlmid) \
421 	    ((nlmreg)[nlmid] = FLK_NLM_UP)
422 #define	FLK_REGISTRY_CHANGE_NLM_STATE(nlmreg, nlmid, state) \
423 	    ((nlmreg)[nlmid] = state)
424 
425 /* Indicates the effect of executing a request on the existing locks */
426 
427 #define	FLK_UNLOCK	0x1	/* request unlocks the existing lock */
428 #define	FLK_DOWNGRADE	0x2	/* request downgrades the existing lock */
429 #define	FLK_UPGRADE	0x3	/* request upgrades the existing lock */
430 #define	FLK_STAY_SAME	0x4	/* request type is same as existing lock */
431 
432 
433 /*	proc graph definitions	*/
434 
435 /*
436  * Proc graph is the global process graph that maintains information
437  * about the dependencies between processes. An edge is added between two
438  * processes represented by proc_vertex's A and B, iff there exists l1
439  * owned by process A in any of the lock_graph's dependent on l2
440  * (thus having an edge to l2) owned by process B.
441  */
442 struct proc_vertex {
443 	pid_t	pid;	/* pid of the process */
444 	long	sysid;	/* sysid of the process */
445 	struct proc_edge	*edge;	/* adajcent edges of this process */
446 	int incount;		/* Number of inedges to this process */
447 	struct proc_edge *p_sedge;	/* used for implementing stack alg. */
448 	struct proc_vertex	*p_stack;	/* used for stack alg. */
449 	int atime;	/* used for cycle detection algorithm */
450 	int dtime;	/* used for cycle detection algorithm */
451 	int index;	/* index into the  array of proc_graph vertices */
452 };
453 
454 typedef	struct proc_vertex proc_vertex_t;
455 
456 struct proc_edge {
457 	struct proc_edge	*next;	/* next edge in adjacency list */
458 	int  refcount;			/* reference count of this edge */
459 	struct proc_vertex	*to_proc;	/* process this points to */
460 };
461 
462 typedef struct proc_edge proc_edge_t;
463 
464 
465 #define	PROC_CHUNK	100
466 
467 struct proc_graph {
468 	struct proc_vertex **proc;	/* list of proc_vertexes */
469 	int gcount;		/* list size */
470 	int free;		/* number of free slots in the list */
471 	int mark;		/* used for graph coloring */
472 };
473 
474 typedef struct proc_graph proc_graph_t;
475 
476 extern	struct proc_graph	pgraph;
477 
478 #define	PROC_SAME_OWNER(lock, pvertex)	\
479 	(((lock)->l_flock.l_pid == (pvertex)->pid) && \
480 		((lock)->l_flock.l_sysid == (pvertex)->sysid))
481 
482 #define	PROC_ARRIVE(pvertex)	((pvertex)->atime = pgraph.mark)
483 #define	PROC_DEPART(pvertex)	((pvertex)->dtime = pgraph.mark)
484 #define	PROC_ARRIVED(pvertex)	((pvertex)->atime == pgraph.mark)
485 #define	PROC_DEPARTED(pvertex)  ((pvertex)->dtime == pgraph.mark)
486 
487 #ifdef	__cplusplus
488 }
489 #endif
490 
491 #endif	/* _SYS_FLOCK_IMPL_H */
492