xref: /onnv-gate/usr/src/uts/common/sys/flock.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #ifndef _SYS_FLOCK_H
32*0Sstevel@tonic-gate #define	_SYS_FLOCK_H
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <sys/fcntl.h>
38*0Sstevel@tonic-gate #include <sys/vnode.h>
39*0Sstevel@tonic-gate #include <sys/t_lock.h>		/* for <sys/callb.h> */
40*0Sstevel@tonic-gate #include <sys/callb.h>
41*0Sstevel@tonic-gate #include <sys/param.h>
42*0Sstevel@tonic-gate #include <sys/zone.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #ifdef	__cplusplus
45*0Sstevel@tonic-gate extern "C" {
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * Private declarations and instrumentation for local locking.
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * The flag passed to fs_frlock() may be ORed together with either
54*0Sstevel@tonic-gate  * `F_REMOTELOCK' or `F_PXFSLOCK'.  Since this flag is initialized using the
55*0Sstevel@tonic-gate  * `f_flag' field in the `file' structure, and that field is an unsigned short,
56*0Sstevel@tonic-gate  * we do not use the first 2 bytes.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate #define	F_REMOTELOCK	(0x01 << 16) /* Set if NLM lock */
59*0Sstevel@tonic-gate #define	F_PXFSLOCK	(0x02 << 16) /* Clustering: set if PXFS lock */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate  * The command passed to reclock() is made by ORing together one or more of
63*0Sstevel@tonic-gate  * the following values.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #define	INOFLCK		0x01	/* Vnode is locked when reclock() is called. */
67*0Sstevel@tonic-gate #define	SETFLCK		0x02	/* Set a file lock. */
68*0Sstevel@tonic-gate #define	SLPFLCK		0x04	/* Wait if blocked. */
69*0Sstevel@tonic-gate #define	RCMDLCK		0x08	/* F_REMOTELOCK specified */
70*0Sstevel@tonic-gate #define	PCMDLCK		0x10	/* Clustering: F_PXFSLOCK specified */
71*0Sstevel@tonic-gate #define	NBMLCK		0x20	/* non-blocking mandatory locking */
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Special pid value that can be passed to cleanlocks().  It means that
75*0Sstevel@tonic-gate  * cleanlocks() should flush all locks for the given sysid, not just the
76*0Sstevel@tonic-gate  * locks owned by a specific process.
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	IGN_PID		(-1)
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /* file locking structure (connected to vnode) */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate #define	l_end		l_len
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate  * The lock manager is allowed to use unsigned offsets and lengths, though
87*0Sstevel@tonic-gate  * regular Unix processes are still required to use signed offsets and
88*0Sstevel@tonic-gate  * lengths.
89*0Sstevel@tonic-gate  */
90*0Sstevel@tonic-gate typedef ulong_t u_off_t;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate #define	MAX_U_OFF_T	((u_off_t)~0)
93*0Sstevel@tonic-gate #define	MAX_U_OFFSET_T	((u_offset_t)~0)
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * define MAXEND as the largest positive value the signed offset_t will hold.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate #define	MAXEND		MAXOFFSET_T
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * Definitions for accessing the l_pad area of struct flock.  The
102*0Sstevel@tonic-gate  * descriminant of the pad_info_t union is the fcntl command used in
103*0Sstevel@tonic-gate  * conjunction with the flock struct.
104*0Sstevel@tonic-gate  */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate typedef union {
107*0Sstevel@tonic-gate 	int	pi_pad[4];		/* (original pad area) */
108*0Sstevel@tonic-gate 	int	pi_has_rmt;		/* F_HASREMOTELOCKS */
109*0Sstevel@tonic-gate } pad_info_t;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate #define	l_has_rmt(flockp)	(((pad_info_t *)((flockp)->l_pad))->pi_has_rmt)
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate /*
114*0Sstevel@tonic-gate  * Optional callbacks for blocking lock requests.  Each function is called
115*0Sstevel@tonic-gate  * twice.
116*0Sstevel@tonic-gate  * The first call is after the request is put in the "sleeping" list, but
117*0Sstevel@tonic-gate  *   before waiting.  At most one callback may return a callb_cpr_t object;
118*0Sstevel@tonic-gate  *   the others must return NULL.  If a callb_cpr_t is returned, the thread
119*0Sstevel@tonic-gate  *   will be marked as safe to suspend while waiting for the lock.
120*0Sstevel@tonic-gate  * The second call is after the request wakes up.  Note that the request
121*0Sstevel@tonic-gate  *   might not have been granted at the second call (e.g., the request was
122*0Sstevel@tonic-gate  *   signalled).
123*0Sstevel@tonic-gate  * New callbacks should be added to the head of the list.  For the first
124*0Sstevel@tonic-gate  * call the list is walked in order.  For the second call the list is
125*0Sstevel@tonic-gate  * walked backwards (in case the callbacks need to reacquire locks).
126*0Sstevel@tonic-gate  */
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate typedef enum {FLK_BEFORE_SLEEP, FLK_AFTER_SLEEP} flk_cb_when_t;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate struct flk_callback {
131*0Sstevel@tonic-gate 	struct flk_callback *cb_next;	/* circular linked list */
132*0Sstevel@tonic-gate 	struct flk_callback *cb_prev;
133*0Sstevel@tonic-gate 	callb_cpr_t	*(*cb_callback)(flk_cb_when_t, void *);	/* fcn ptr */
134*0Sstevel@tonic-gate 	void		*cb_data;	/* ptr to callback data */
135*0Sstevel@tonic-gate };
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate typedef struct flk_callback flk_callback_t;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * This structure members are not used any more inside the kernel.
141*0Sstevel@tonic-gate  * The structure is used for casting some pointer assignments only.
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate typedef struct filock {
145*0Sstevel@tonic-gate 	kcondvar_t cv;
146*0Sstevel@tonic-gate 	struct	flock set;	/* contains type, start, and end */
147*0Sstevel@tonic-gate 	struct	{
148*0Sstevel@tonic-gate 		int granted_flag;	/* granted flag */
149*0Sstevel@tonic-gate 		struct filock *blk;	/* for sleeping locks only */
150*0Sstevel@tonic-gate 		struct attacher *blocking_list;
151*0Sstevel@tonic-gate 		struct attacher *my_attacher;
152*0Sstevel@tonic-gate 	}	stat;
153*0Sstevel@tonic-gate 	struct	filock *prev;
154*0Sstevel@tonic-gate 	struct	filock *next;
155*0Sstevel@tonic-gate } filock_t;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate #define	FLP_DELAYED_FREE	-1	/* special value for granted_flag */
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate /* structure that contains list of locks to be granted */
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate #define	MAX_GRANT_LOCKS		52
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate typedef struct grant_lock {
164*0Sstevel@tonic-gate 	struct filock *grant_lock_list[MAX_GRANT_LOCKS];
165*0Sstevel@tonic-gate 	struct grant_lock *next;
166*0Sstevel@tonic-gate } grant_lock_t;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate  * Provide a way to cleanly enable and disable Network Lock Manager locking
170*0Sstevel@tonic-gate  * requests (i.e., requests from remote clients):
171*0Sstevel@tonic-gate  *    FLK_NLM_SHUTTING_DOWN: Forces all blocked NLM requests to bail out
172*0Sstevel@tonic-gate  *	and return ENOLCK.
173*0Sstevel@tonic-gate  *    FLK_NLM_DOWN: Clears all granted NLM server locks.  Both status
174*0Sstevel@tonic-gate  *	codes cause new NLM lock requests to fail immediately with ENOLCK.
175*0Sstevel@tonic-gate  *    FLK_NLM_UP: Changes the state of all locks to UP, after a server has
176*0Sstevel@tonic-gate  *	shutdown and is restarting on the same node.
177*0Sstevel@tonic-gate  */
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * Enumerated type of the four possible states an NLM server can be in.
181*0Sstevel@tonic-gate  */
182*0Sstevel@tonic-gate typedef enum {
183*0Sstevel@tonic-gate 	FLK_NLM_UP,
184*0Sstevel@tonic-gate 	FLK_NLM_SHUTTING_DOWN,
185*0Sstevel@tonic-gate 	FLK_NLM_DOWN,
186*0Sstevel@tonic-gate 	FLK_NLM_UNKNOWN
187*0Sstevel@tonic-gate } flk_nlm_status_t;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate  * Provide a way to cleanly enable and disable lock manager locking
191*0Sstevel@tonic-gate  * requests (i.e., requests from remote clients).  FLK_WAKEUP_SLEEPERS
192*0Sstevel@tonic-gate  * forces all blocked lock manager requests to bail out and return ENOLCK.
193*0Sstevel@tonic-gate  * FLK_LOCKMGR_DOWN clears all granted lock manager locks.  Both status
194*0Sstevel@tonic-gate  * codes cause new lock manager requests to fail immediately with ENOLCK.
195*0Sstevel@tonic-gate  */
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate typedef enum {
198*0Sstevel@tonic-gate     FLK_LOCKMGR_UP,
199*0Sstevel@tonic-gate     FLK_WAKEUP_SLEEPERS,
200*0Sstevel@tonic-gate     FLK_LOCKMGR_DOWN
201*0Sstevel@tonic-gate } flk_lockmgr_status_t;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate #if defined(_KERNEL)
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate /*
206*0Sstevel@tonic-gate  * The following structure is used to hold a list of locks returned
207*0Sstevel@tonic-gate  * by the F_ACTIVELIST or F_SLEEPINGLIST commands to fs_frlock.
208*0Sstevel@tonic-gate  *
209*0Sstevel@tonic-gate  * N.B. The lists returned by these commands are dynamically
210*0Sstevel@tonic-gate  * allocated and must be freed by the caller.  The vnodes returned
211*0Sstevel@tonic-gate  * in the lists are held and must be released when the caller is done.
212*0Sstevel@tonic-gate  */
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate typedef struct locklist {
215*0Sstevel@tonic-gate 	struct vnode *ll_vp;
216*0Sstevel@tonic-gate 	struct flock64 ll_flock;
217*0Sstevel@tonic-gate 	struct locklist *ll_next;
218*0Sstevel@tonic-gate } locklist_t;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate #define	FLK_QUERY_ACTIVE	0x1
221*0Sstevel@tonic-gate #define	FLK_QUERY_SLEEPING	0x2
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate int	reclock(struct vnode *, struct flock64 *, int, int, u_offset_t,
224*0Sstevel@tonic-gate 		flk_callback_t *);
225*0Sstevel@tonic-gate int	chklock(struct vnode *, int, u_offset_t, ssize_t, int,
226*0Sstevel@tonic-gate 		caller_context_t *);
227*0Sstevel@tonic-gate int	convoff(struct vnode *, struct flock64 *, int, offset_t);
228*0Sstevel@tonic-gate void	cleanlocks(struct vnode *, pid_t, int);
229*0Sstevel@tonic-gate locklist_t *flk_get_sleeping_locks(int sysid, pid_t pid);
230*0Sstevel@tonic-gate locklist_t *flk_get_active_locks(int sysid, pid_t pid);
231*0Sstevel@tonic-gate locklist_t *flk_active_locks_for_vp(const struct vnode *vp);
232*0Sstevel@tonic-gate locklist_t *flk_active_nbmand_locks_for_vp(const struct vnode *vp);
233*0Sstevel@tonic-gate locklist_t *flk_active_nbmand_locks(pid_t pid);
234*0Sstevel@tonic-gate void	flk_free_locklist(locklist_t *);
235*0Sstevel@tonic-gate int	flk_convert_lock_data(struct vnode *, struct flock64 *,
236*0Sstevel@tonic-gate 		u_offset_t *, u_offset_t *, offset_t);
237*0Sstevel@tonic-gate int	flk_check_lock_data(u_offset_t, u_offset_t, offset_t);
238*0Sstevel@tonic-gate int	flk_has_remote_locks(struct vnode *vp);
239*0Sstevel@tonic-gate void	flk_set_lockmgr_status(flk_lockmgr_status_t status);
240*0Sstevel@tonic-gate int	flk_sysid_has_locks(int sysid, int chklck);
241*0Sstevel@tonic-gate void	flk_init_callback(flk_callback_t *,
242*0Sstevel@tonic-gate 		callb_cpr_t *(*)(flk_cb_when_t, void *), void *);
243*0Sstevel@tonic-gate void	flk_add_callback(flk_callback_t *,
244*0Sstevel@tonic-gate 		callb_cpr_t *(*)(flk_cb_when_t, void *), void *,
245*0Sstevel@tonic-gate 		flk_callback_t *);
246*0Sstevel@tonic-gate callb_cpr_t *flk_invoke_callbacks(flk_callback_t *, flk_cb_when_t);
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /* Zones hooks */
249*0Sstevel@tonic-gate extern	zone_key_t flock_zone_key;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate void	*flk_zone_init(zoneid_t);
252*0Sstevel@tonic-gate void	flk_zone_fini(zoneid_t, void *);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate /* Clustering hooks */
255*0Sstevel@tonic-gate void	cl_flk_set_nlm_status(int nlmid, flk_nlm_status_t nlm_state);
256*0Sstevel@tonic-gate void	cl_flk_remove_locks_by_sysid(int sysid);
257*0Sstevel@tonic-gate int	cl_flk_has_remote_locks_for_nlmid(struct vnode *vp, int nlmid);
258*0Sstevel@tonic-gate void	cl_flk_change_nlm_state_to_unknown(int nlmid);
259*0Sstevel@tonic-gate void	cl_flk_delete_pxfs_locks(struct vfs *vfsp, int pxfsid);
260*0Sstevel@tonic-gate #endif /* _KERNEL */
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate #ifdef	__cplusplus
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate #endif
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate #endif	/* _SYS_FLOCK_H */
267