1139825Simp/*-
21960Sdg * Copyright (c) 1991, 1993
31960Sdg *	The Regents of the University of California.  All rights reserved.
41960Sdg *
51960Sdg * This code is derived from software contributed to Berkeley by
61960Sdg * Scooter Morris at Genentech Inc.
71960Sdg *
81960Sdg * Redistribution and use in source and binary forms, with or without
91960Sdg * modification, are permitted provided that the following conditions
101960Sdg * are met:
111960Sdg * 1. Redistributions of source code must retain the above copyright
121960Sdg *    notice, this list of conditions and the following disclaimer.
131960Sdg * 2. Redistributions in binary form must reproduce the above copyright
141960Sdg *    notice, this list of conditions and the following disclaimer in the
151960Sdg *    documentation and/or other materials provided with the distribution.
161960Sdg * 4. Neither the name of the University nor the names of its contributors
171960Sdg *    may be used to endorse or promote products derived from this software
181960Sdg *    without specific prior written permission.
191960Sdg *
201960Sdg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211960Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221960Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231960Sdg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241960Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251960Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261960Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271960Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281960Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291960Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301960Sdg * SUCH DAMAGE.
311960Sdg *
321960Sdg *	@(#)lockf.h	8.1 (Berkeley) 6/11/93
3350477Speter * $FreeBSD: releng/10.3/sys/sys/lockf.h 192685 2009-05-24 12:39:38Z kib $
341960Sdg */
351960Sdg
362165Spaul#ifndef _SYS_LOCKF_H_
3715493Sbde#define	_SYS_LOCKF_H_
382165Spaul
3933057Sbde#include <sys/queue.h>
40177633Sdfr#include <sys/_lock.h>
41177633Sdfr#include <sys/_sx.h>
4233057Sbde
43180025Sdfrstruct flock;
4433057Sbdestruct vop_advlock_args;
45177633Sdfrstruct vop_advlockasync_args;
4633057Sbde
471960Sdg/*
48177633Sdfr * The lockf_entry structure is a kernel structure which contains the
49177633Sdfr * information associated with a byte range lock.  The lockf_entry
50177633Sdfr * structures are linked into the inode structure. Locks are sorted by
51177633Sdfr * the starting byte of the lock for efficiency.
52177633Sdfr *
53177633Sdfr * Active and pending locks on a vnode are organised into a
54177633Sdfr * graph. Each pending lock has an out-going edge to each active lock
55177633Sdfr * that blocks it.
56177633Sdfr *
57177633Sdfr * Locks:
58177633Sdfr * (i)		locked by the vnode interlock
59177633Sdfr * (s)		locked by state->ls_lock
60177633Sdfr * (S)		locked by lf_lock_states_lock
61177633Sdfr * (c)		const until freeing
621960Sdg */
63177633Sdfrstruct lockf_edge {
64177633Sdfr	LIST_ENTRY(lockf_edge) le_outlink; /* (s) link from's out-edge list */
65177633Sdfr	LIST_ENTRY(lockf_edge) le_inlink; /* (s) link to's in-edge list */
66177633Sdfr	struct lockf_entry *le_from;	/* (c) out-going from here */
67177633Sdfr	struct lockf_entry *le_to;	/* (s) in-coming to here */
68177633Sdfr};
69177633SdfrLIST_HEAD(lockf_edge_list, lockf_edge);
7022521Sdyson
71177633Sdfrstruct lockf_entry {
72177633Sdfr	short	lf_flags;	    /* (c) Semantics: F_POSIX, F_FLOCK, F_WAIT */
73177633Sdfr	short	lf_type;	    /* (s) Lock type: F_RDLCK, F_WRLCK */
74177633Sdfr	off_t	lf_start;	    /* (s) Byte # of the start of the lock */
75177633Sdfr	off_t	lf_end;		    /* (s) Byte # of the end of the lock (OFF_MAX=EOF) */
76177633Sdfr	struct	lock_owner *lf_owner; /* (c) Owner of the lock */
77177633Sdfr	struct	vnode *lf_vnode;    /* (c) File being locked (only valid for active lock) */
78177633Sdfr	struct	inode *lf_inode;    /* (c) Back pointer to the inode */
79177633Sdfr	struct	task *lf_async_task;/* (c) Async lock callback */
80177633Sdfr	LIST_ENTRY(lockf_entry) lf_link;  /* (s) Linkage for lock lists */
81177633Sdfr	struct lockf_edge_list lf_outedges; /* (s) list of out-edges */
82177633Sdfr	struct lockf_edge_list lf_inedges; /* (s) list of out-edges */
83192685Skib	int	lf_refs;	    /* (s) ref count */
84177633Sdfr};
85177633SdfrLIST_HEAD(lockf_entry_list, lockf_entry);
86177633Sdfr
87177633Sdfr/*
88178243Skib * Extra lf_flags bits used by the implementation
89178243Skib */
90178243Skib#define	F_INTR		0x8000	/* lock was interrupted by lf_purgelocks */
91178243Skib
92178243Skib/*
93177633Sdfr * Filesystem private node structures should include space for a
94177633Sdfr * pointer to a struct lockf_state. This pointer is used by the lock
95177633Sdfr * manager to track the locking state for a file.
96177633Sdfr *
97177633Sdfr * The ls_active list contains the set of active locks on the file. It
98177633Sdfr * is strictly ordered by the lock's lf_start value. Each active lock
99177633Sdfr * will have in-coming edges to any pending lock which it blocks.
100177633Sdfr *
101177633Sdfr * Lock requests which are blocked by some other active lock are
102177633Sdfr * listed in ls_pending with newer requests first in the list. Lock
103177633Sdfr * requests in this list will have out-going edges to each active lock
104177633Sdfr * that blocks then. They will also have out-going edges to each
105177633Sdfr * pending lock that is older in the queue - this helps to ensure
106177633Sdfr * fairness when several processes are contenting to lock the same
107177633Sdfr * record.
108177633Sdfr
109177633Sdfr * The value of ls_threads is the number of threads currently using
110177633Sdfr * the state structure (typically either setting/clearing locks or
111177633Sdfr * sleeping waiting to do so). This is used to defer freeing the
112177633Sdfr * structure while some thread is still using it.
113177633Sdfr */
1141960Sdgstruct lockf {
115177633Sdfr	LIST_ENTRY(lockf) ls_link;	/* (S) all active lockf states */
116177633Sdfr	struct	sx	ls_lock;
117177633Sdfr	struct	lockf_entry_list ls_active; /* (s) Active locks */
118177633Sdfr	struct	lockf_entry_list ls_pending; /* (s) Pending locks */
119177633Sdfr	int		ls_threads;	/* (i) Thread count */
1201960Sdg};
121177633SdfrLIST_HEAD(lockf_list, lockf);
1221960Sdg
123180025Sdfrtypedef int lf_iterator(struct vnode *, struct flock *, void *);
124180025Sdfr
12592719Salfredint	 lf_advlock(struct vop_advlock_args *, struct lockf **, u_quad_t);
126177633Sdfrint	 lf_advlockasync(struct vop_advlockasync_args *, struct lockf **, u_quad_t);
127178243Skibvoid	 lf_purgelocks(struct vnode *vp, struct lockf **statep);
128180025Sdfrint	 lf_iteratelocks_sysid(int sysid, lf_iterator *, void *);
129180025Sdfrint	 lf_iteratelocks_vnode(struct vnode *vp, lf_iterator *, void *);
130177633Sdfrint	 lf_countlocks(int sysid);
131177633Sdfrvoid	 lf_clearremotesys(int sysid);
1321960Sdg
13315493Sbde#endif /* !_SYS_LOCKF_H_ */
134