xref: /titanic_44/usr/src/uts/common/sys/flock.h (revision 7a0cc5a9d7d5732e36817701e754e703527c61cd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
30bbaa8b60SDan Kruchinin /*
31b819cea2SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
32bbaa8b60SDan Kruchinin  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifndef _SYS_FLOCK_H
357c478bd9Sstevel@tonic-gate #define	_SYS_FLOCK_H
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>		/* for <sys/callb.h> */
417c478bd9Sstevel@tonic-gate #include <sys/callb.h>
427c478bd9Sstevel@tonic-gate #include <sys/param.h>
437c478bd9Sstevel@tonic-gate #include <sys/zone.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
467c478bd9Sstevel@tonic-gate extern "C" {
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Private declarations and instrumentation for local locking.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * The flag passed to fs_frlock() may be ORed together with either
557c478bd9Sstevel@tonic-gate  * `F_REMOTELOCK' or `F_PXFSLOCK'.  Since this flag is initialized using the
567c478bd9Sstevel@tonic-gate  * `f_flag' field in the `file' structure, and that field is an unsigned short,
577c478bd9Sstevel@tonic-gate  * we do not use the first 2 bytes.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate #define	F_REMOTELOCK	(0x01 << 16) /* Set if NLM lock */
607c478bd9Sstevel@tonic-gate #define	F_PXFSLOCK	(0x02 << 16) /* Clustering: set if PXFS lock */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * The command passed to reclock() is made by ORing together one or more of
647c478bd9Sstevel@tonic-gate  * the following values.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	INOFLCK		0x01	/* Vnode is locked when reclock() is called. */
687c478bd9Sstevel@tonic-gate #define	SETFLCK		0x02	/* Set a file lock. */
697c478bd9Sstevel@tonic-gate #define	SLPFLCK		0x04	/* Wait if blocked. */
707c478bd9Sstevel@tonic-gate #define	RCMDLCK		0x08	/* F_REMOTELOCK specified */
717c478bd9Sstevel@tonic-gate #define	PCMDLCK		0x10	/* Clustering: F_PXFSLOCK specified */
727c478bd9Sstevel@tonic-gate #define	NBMLCK		0x20	/* non-blocking mandatory locking */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Special pid value that can be passed to cleanlocks().  It means that
767c478bd9Sstevel@tonic-gate  * cleanlocks() should flush all locks for the given sysid, not just the
777c478bd9Sstevel@tonic-gate  * locks owned by a specific process.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	IGN_PID		(-1)
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* file locking structure (connected to vnode) */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	l_end		l_len
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * The lock manager is allowed to use unsigned offsets and lengths, though
887c478bd9Sstevel@tonic-gate  * regular Unix processes are still required to use signed offsets and
897c478bd9Sstevel@tonic-gate  * lengths.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate typedef ulong_t u_off_t;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	MAX_U_OFF_T	((u_off_t)~0)
947c478bd9Sstevel@tonic-gate #define	MAX_U_OFFSET_T	((u_offset_t)~0)
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * define MAXEND as the largest positive value the signed offset_t will hold.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate #define	MAXEND		MAXOFFSET_T
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Definitions for accessing the l_pad area of struct flock.  The
1037c478bd9Sstevel@tonic-gate  * descriminant of the pad_info_t union is the fcntl command used in
1047c478bd9Sstevel@tonic-gate  * conjunction with the flock struct.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate typedef union {
1087c478bd9Sstevel@tonic-gate 	int	pi_pad[4];		/* (original pad area) */
1097c478bd9Sstevel@tonic-gate 	int	pi_has_rmt;		/* F_HASREMOTELOCKS */
1107c478bd9Sstevel@tonic-gate } pad_info_t;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define	l_has_rmt(flockp)	(((pad_info_t *)((flockp)->l_pad))->pi_has_rmt)
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Optional callbacks for blocking lock requests.  Each function is called
1167c478bd9Sstevel@tonic-gate  * twice.
1177c478bd9Sstevel@tonic-gate  * The first call is after the request is put in the "sleeping" list, but
1187c478bd9Sstevel@tonic-gate  *   before waiting.  At most one callback may return a callb_cpr_t object;
1197c478bd9Sstevel@tonic-gate  *   the others must return NULL.  If a callb_cpr_t is returned, the thread
1207c478bd9Sstevel@tonic-gate  *   will be marked as safe to suspend while waiting for the lock.
1217c478bd9Sstevel@tonic-gate  * The second call is after the request wakes up.  Note that the request
1227c478bd9Sstevel@tonic-gate  *   might not have been granted at the second call (e.g., the request was
1237c478bd9Sstevel@tonic-gate  *   signalled).
1247c478bd9Sstevel@tonic-gate  * New callbacks should be added to the head of the list.  For the first
1257c478bd9Sstevel@tonic-gate  * call the list is walked in order.  For the second call the list is
1267c478bd9Sstevel@tonic-gate  * walked backwards (in case the callbacks need to reacquire locks).
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate typedef enum {FLK_BEFORE_SLEEP, FLK_AFTER_SLEEP} flk_cb_when_t;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate struct flk_callback {
1327c478bd9Sstevel@tonic-gate 	struct flk_callback *cb_next;	/* circular linked list */
1337c478bd9Sstevel@tonic-gate 	struct flk_callback *cb_prev;
1347c478bd9Sstevel@tonic-gate 	callb_cpr_t	*(*cb_callback)(flk_cb_when_t, void *);	/* fcn ptr */
1357c478bd9Sstevel@tonic-gate 	void		*cb_data;	/* ptr to callback data */
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate typedef struct flk_callback flk_callback_t;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * This structure members are not used any more inside the kernel.
1427c478bd9Sstevel@tonic-gate  * The structure is used for casting some pointer assignments only.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate typedef struct filock {
1467c478bd9Sstevel@tonic-gate 	kcondvar_t cv;
1477c478bd9Sstevel@tonic-gate 	struct	flock set;	/* contains type, start, and end */
1487c478bd9Sstevel@tonic-gate 	struct	{
1497c478bd9Sstevel@tonic-gate 		int granted_flag;	/* granted flag */
1507c478bd9Sstevel@tonic-gate 		struct filock *blk;	/* for sleeping locks only */
1517c478bd9Sstevel@tonic-gate 		struct attacher *blocking_list;
1527c478bd9Sstevel@tonic-gate 		struct attacher *my_attacher;
1537c478bd9Sstevel@tonic-gate 	}	stat;
1547c478bd9Sstevel@tonic-gate 	struct	filock *prev;
1557c478bd9Sstevel@tonic-gate 	struct	filock *next;
1567c478bd9Sstevel@tonic-gate } filock_t;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #define	FLP_DELAYED_FREE	-1	/* special value for granted_flag */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /* structure that contains list of locks to be granted */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate #define	MAX_GRANT_LOCKS		52
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate typedef struct grant_lock {
1657c478bd9Sstevel@tonic-gate 	struct filock *grant_lock_list[MAX_GRANT_LOCKS];
1667c478bd9Sstevel@tonic-gate 	struct grant_lock *next;
1677c478bd9Sstevel@tonic-gate } grant_lock_t;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Provide a way to cleanly enable and disable Network Lock Manager locking
1717c478bd9Sstevel@tonic-gate  * requests (i.e., requests from remote clients):
1727c478bd9Sstevel@tonic-gate  *    FLK_NLM_SHUTTING_DOWN: Forces all blocked NLM requests to bail out
1737c478bd9Sstevel@tonic-gate  *	and return ENOLCK.
1747c478bd9Sstevel@tonic-gate  *    FLK_NLM_DOWN: Clears all granted NLM server locks.  Both status
1757c478bd9Sstevel@tonic-gate  *	codes cause new NLM lock requests to fail immediately with ENOLCK.
1767c478bd9Sstevel@tonic-gate  *    FLK_NLM_UP: Changes the state of all locks to UP, after a server has
1777c478bd9Sstevel@tonic-gate  *	shutdown and is restarting on the same node.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * Enumerated type of the four possible states an NLM server can be in.
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate typedef enum {
1847c478bd9Sstevel@tonic-gate 	FLK_NLM_UP,
1857c478bd9Sstevel@tonic-gate 	FLK_NLM_SHUTTING_DOWN,
1867c478bd9Sstevel@tonic-gate 	FLK_NLM_DOWN,
1877c478bd9Sstevel@tonic-gate 	FLK_NLM_UNKNOWN
1887c478bd9Sstevel@tonic-gate } flk_nlm_status_t;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Provide a way to cleanly enable and disable lock manager locking
1927c478bd9Sstevel@tonic-gate  * requests (i.e., requests from remote clients).  FLK_WAKEUP_SLEEPERS
1937c478bd9Sstevel@tonic-gate  * forces all blocked lock manager requests to bail out and return ENOLCK.
1947c478bd9Sstevel@tonic-gate  * FLK_LOCKMGR_DOWN clears all granted lock manager locks.  Both status
1957c478bd9Sstevel@tonic-gate  * codes cause new lock manager requests to fail immediately with ENOLCK.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate typedef enum {
1997c478bd9Sstevel@tonic-gate     FLK_LOCKMGR_UP,
2007c478bd9Sstevel@tonic-gate     FLK_WAKEUP_SLEEPERS,
2017c478bd9Sstevel@tonic-gate     FLK_LOCKMGR_DOWN
2027c478bd9Sstevel@tonic-gate } flk_lockmgr_status_t;
2037c478bd9Sstevel@tonic-gate 
204b819cea2SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * The following structure is used to hold a list of locks returned
2087c478bd9Sstevel@tonic-gate  * by the F_ACTIVELIST or F_SLEEPINGLIST commands to fs_frlock.
2097c478bd9Sstevel@tonic-gate  *
2107c478bd9Sstevel@tonic-gate  * N.B. The lists returned by these commands are dynamically
2117c478bd9Sstevel@tonic-gate  * allocated and must be freed by the caller.  The vnodes returned
2127c478bd9Sstevel@tonic-gate  * in the lists are held and must be released when the caller is done.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate typedef struct locklist {
2167c478bd9Sstevel@tonic-gate 	struct vnode *ll_vp;
2177c478bd9Sstevel@tonic-gate 	struct flock64 ll_flock;
2187c478bd9Sstevel@tonic-gate 	struct locklist *ll_next;
2197c478bd9Sstevel@tonic-gate } locklist_t;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate #define	FLK_QUERY_ACTIVE	0x1
2227c478bd9Sstevel@tonic-gate #define	FLK_QUERY_SLEEPING	0x2
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate int	reclock(struct vnode *, struct flock64 *, int, int, u_offset_t,
2257c478bd9Sstevel@tonic-gate 		flk_callback_t *);
2267c478bd9Sstevel@tonic-gate int	chklock(struct vnode *, int, u_offset_t, ssize_t, int,
2277c478bd9Sstevel@tonic-gate 		caller_context_t *);
2287c478bd9Sstevel@tonic-gate int	convoff(struct vnode *, struct flock64 *, int, offset_t);
2297c478bd9Sstevel@tonic-gate void	cleanlocks(struct vnode *, pid_t, int);
2307c478bd9Sstevel@tonic-gate locklist_t *flk_get_sleeping_locks(int sysid, pid_t pid);
2317c478bd9Sstevel@tonic-gate locklist_t *flk_get_active_locks(int sysid, pid_t pid);
2327c478bd9Sstevel@tonic-gate locklist_t *flk_active_locks_for_vp(const struct vnode *vp);
2337c478bd9Sstevel@tonic-gate locklist_t *flk_active_nbmand_locks_for_vp(const struct vnode *vp);
2347c478bd9Sstevel@tonic-gate locklist_t *flk_active_nbmand_locks(pid_t pid);
2357c478bd9Sstevel@tonic-gate void	flk_free_locklist(locklist_t *);
2367c478bd9Sstevel@tonic-gate int	flk_convert_lock_data(struct vnode *, struct flock64 *,
2377c478bd9Sstevel@tonic-gate 		u_offset_t *, u_offset_t *, offset_t);
2387c478bd9Sstevel@tonic-gate int	flk_check_lock_data(u_offset_t, u_offset_t, offset_t);
2397c478bd9Sstevel@tonic-gate int	flk_has_remote_locks(struct vnode *vp);
2407c478bd9Sstevel@tonic-gate void	flk_set_lockmgr_status(flk_lockmgr_status_t status);
2417c478bd9Sstevel@tonic-gate int	flk_sysid_has_locks(int sysid, int chklck);
242bbaa8b60SDan Kruchinin int	flk_has_remote_locks_for_sysid(vnode_t *vp, int);
2437c478bd9Sstevel@tonic-gate void	flk_init_callback(flk_callback_t *,
2447c478bd9Sstevel@tonic-gate 		callb_cpr_t *(*)(flk_cb_when_t, void *), void *);
2457c478bd9Sstevel@tonic-gate void	flk_add_callback(flk_callback_t *,
2467c478bd9Sstevel@tonic-gate 		callb_cpr_t *(*)(flk_cb_when_t, void *), void *,
2477c478bd9Sstevel@tonic-gate 		flk_callback_t *);
248*7a0cc5a9SMarcel Telka void	flk_del_callback(flk_callback_t *);
2497c478bd9Sstevel@tonic-gate callb_cpr_t *flk_invoke_callbacks(flk_callback_t *, flk_cb_when_t);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /* Zones hooks */
2527c478bd9Sstevel@tonic-gate extern	zone_key_t flock_zone_key;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate void	*flk_zone_init(zoneid_t);
2557c478bd9Sstevel@tonic-gate void	flk_zone_fini(zoneid_t, void *);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /* Clustering hooks */
2587c478bd9Sstevel@tonic-gate void	cl_flk_set_nlm_status(int nlmid, flk_nlm_status_t nlm_state);
2597c478bd9Sstevel@tonic-gate void	cl_flk_remove_locks_by_sysid(int sysid);
2607c478bd9Sstevel@tonic-gate int	cl_flk_has_remote_locks_for_nlmid(struct vnode *vp, int nlmid);
2617c478bd9Sstevel@tonic-gate void	cl_flk_change_nlm_state_to_unknown(int nlmid);
2627c478bd9Sstevel@tonic-gate void	cl_flk_delete_pxfs_locks(struct vfs *vfsp, int pxfsid);
2637c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate #endif
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate #endif	/* _SYS_FLOCK_H */
270