xref: /f-stack/freebsd/sys/lockf.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1991, 1993
5a9643ea8Slogwang  *	The Regents of the University of California.  All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * This code is derived from software contributed to Berkeley by
8a9643ea8Slogwang  * Scooter Morris at Genentech Inc.
9a9643ea8Slogwang  *
10a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
11a9643ea8Slogwang  * modification, are permitted provided that the following conditions
12a9643ea8Slogwang  * are met:
13a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
15a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
16a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
17a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
18*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
19a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
20a9643ea8Slogwang  *    without specific prior written permission.
21a9643ea8Slogwang  *
22a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a9643ea8Slogwang  * SUCH DAMAGE.
33a9643ea8Slogwang  *
34a9643ea8Slogwang  *	@(#)lockf.h	8.1 (Berkeley) 6/11/93
35a9643ea8Slogwang  * $FreeBSD$
36a9643ea8Slogwang  */
37a9643ea8Slogwang 
38a9643ea8Slogwang #ifndef _SYS_LOCKF_H_
39a9643ea8Slogwang #define	_SYS_LOCKF_H_
40a9643ea8Slogwang 
41a9643ea8Slogwang #include <sys/queue.h>
42a9643ea8Slogwang #include <sys/_lock.h>
43a9643ea8Slogwang #include <sys/_sx.h>
44a9643ea8Slogwang 
45a9643ea8Slogwang struct flock;
46a9643ea8Slogwang struct vop_advlock_args;
47a9643ea8Slogwang struct vop_advlockasync_args;
48a9643ea8Slogwang 
49a9643ea8Slogwang /*
50a9643ea8Slogwang  * The lockf_entry structure is a kernel structure which contains the
51a9643ea8Slogwang  * information associated with a byte range lock.  The lockf_entry
52a9643ea8Slogwang  * structures are linked into the inode structure. Locks are sorted by
53a9643ea8Slogwang  * the starting byte of the lock for efficiency.
54a9643ea8Slogwang  *
55a9643ea8Slogwang  * Active and pending locks on a vnode are organised into a
56a9643ea8Slogwang  * graph. Each pending lock has an out-going edge to each active lock
57a9643ea8Slogwang  * that blocks it.
58a9643ea8Slogwang  *
59a9643ea8Slogwang  * Locks:
60a9643ea8Slogwang  * (i)		locked by the vnode interlock
61a9643ea8Slogwang  * (s)		locked by state->ls_lock
62a9643ea8Slogwang  * (S)		locked by lf_lock_states_lock
63a9643ea8Slogwang  * (c)		const until freeing
64a9643ea8Slogwang  */
65a9643ea8Slogwang struct lockf_edge {
66a9643ea8Slogwang 	LIST_ENTRY(lockf_edge) le_outlink; /* (s) link from's out-edge list */
67a9643ea8Slogwang 	LIST_ENTRY(lockf_edge) le_inlink; /* (s) link to's in-edge list */
68a9643ea8Slogwang 	struct lockf_entry *le_from;	/* (c) out-going from here */
69a9643ea8Slogwang 	struct lockf_entry *le_to;	/* (s) in-coming to here */
70a9643ea8Slogwang };
71a9643ea8Slogwang LIST_HEAD(lockf_edge_list, lockf_edge);
72a9643ea8Slogwang 
73a9643ea8Slogwang struct lockf_entry {
74a9643ea8Slogwang 	short	lf_flags;	    /* (c) Semantics: F_POSIX, F_FLOCK, F_WAIT */
75a9643ea8Slogwang 	short	lf_type;	    /* (s) Lock type: F_RDLCK, F_WRLCK */
76a9643ea8Slogwang 	off_t	lf_start;	    /* (s) Byte # of the start of the lock */
77a9643ea8Slogwang 	off_t	lf_end;		    /* (s) Byte # of the end of the lock (OFF_MAX=EOF) */
78a9643ea8Slogwang 	struct	lock_owner *lf_owner; /* (c) Owner of the lock */
79a9643ea8Slogwang 	struct	vnode *lf_vnode;    /* (c) File being locked (only valid for active lock) */
80a9643ea8Slogwang 	struct	inode *lf_inode;    /* (c) Back pointer to the inode */
81a9643ea8Slogwang 	struct	task *lf_async_task;/* (c) Async lock callback */
82a9643ea8Slogwang 	LIST_ENTRY(lockf_entry) lf_link;  /* (s) Linkage for lock lists */
83a9643ea8Slogwang 	struct lockf_edge_list lf_outedges; /* (s) list of out-edges */
84*22ce4affSfengbojiang 	struct lockf_edge_list lf_inedges; /* (s) list of in-edges */
85a9643ea8Slogwang 	int	lf_refs;	    /* (s) ref count */
86a9643ea8Slogwang };
87a9643ea8Slogwang LIST_HEAD(lockf_entry_list, lockf_entry);
88a9643ea8Slogwang 
89a9643ea8Slogwang /*
90a9643ea8Slogwang  * Extra lf_flags bits used by the implementation
91a9643ea8Slogwang  */
92a9643ea8Slogwang #define	F_INTR		0x8000	/* lock was interrupted by lf_purgelocks */
93a9643ea8Slogwang 
94a9643ea8Slogwang /*
95a9643ea8Slogwang  * Filesystem private node structures should include space for a
96a9643ea8Slogwang  * pointer to a struct lockf_state. This pointer is used by the lock
97a9643ea8Slogwang  * manager to track the locking state for a file.
98a9643ea8Slogwang  *
99a9643ea8Slogwang  * The ls_active list contains the set of active locks on the file. It
100a9643ea8Slogwang  * is strictly ordered by the lock's lf_start value. Each active lock
101a9643ea8Slogwang  * will have in-coming edges to any pending lock which it blocks.
102a9643ea8Slogwang  *
103a9643ea8Slogwang  * Lock requests which are blocked by some other active lock are
104a9643ea8Slogwang  * listed in ls_pending with newer requests first in the list. Lock
105a9643ea8Slogwang  * requests in this list will have out-going edges to each active lock
106a9643ea8Slogwang  * that blocks then. They will also have out-going edges to each
107a9643ea8Slogwang  * pending lock that is older in the queue - this helps to ensure
108a9643ea8Slogwang  * fairness when several processes are contenting to lock the same
109a9643ea8Slogwang  * record.
110a9643ea8Slogwang 
111a9643ea8Slogwang  * The value of ls_threads is the number of threads currently using
112a9643ea8Slogwang  * the state structure (typically either setting/clearing locks or
113a9643ea8Slogwang  * sleeping waiting to do so). This is used to defer freeing the
114a9643ea8Slogwang  * structure while some thread is still using it.
115a9643ea8Slogwang  */
116a9643ea8Slogwang struct lockf {
117a9643ea8Slogwang 	LIST_ENTRY(lockf) ls_link;	/* (S) all active lockf states */
118a9643ea8Slogwang 	struct	sx	ls_lock;
119a9643ea8Slogwang 	struct	lockf_entry_list ls_active; /* (s) Active locks */
120a9643ea8Slogwang 	struct	lockf_entry_list ls_pending; /* (s) Pending locks */
121a9643ea8Slogwang 	int		ls_threads;	/* (i) Thread count */
122a9643ea8Slogwang };
123a9643ea8Slogwang LIST_HEAD(lockf_list, lockf);
124a9643ea8Slogwang 
125a9643ea8Slogwang typedef int lf_iterator(struct vnode *, struct flock *, void *);
126a9643ea8Slogwang 
127a9643ea8Slogwang int	 lf_advlock(struct vop_advlock_args *, struct lockf **, u_quad_t);
128a9643ea8Slogwang int	 lf_advlockasync(struct vop_advlockasync_args *, struct lockf **, u_quad_t);
129a9643ea8Slogwang void	 lf_purgelocks(struct vnode *vp, struct lockf **statep);
130a9643ea8Slogwang int	 lf_iteratelocks_sysid(int sysid, lf_iterator *, void *);
131a9643ea8Slogwang int	 lf_iteratelocks_vnode(struct vnode *vp, lf_iterator *, void *);
132a9643ea8Slogwang int	 lf_countlocks(int sysid);
133a9643ea8Slogwang void	 lf_clearremotesys(int sysid);
134a9643ea8Slogwang 
135a9643ea8Slogwang #endif /* !_SYS_LOCKF_H_ */
136