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