FreeBSD/sys/kern/kern_lockf.c
/*
 * Walk the list of locks for an inode to find an overlapping lock (if
 * any) and return a classification of that overlap.
 *
 * Arguments:
 *	*overlap	The place in the lock list to start looking
 *	lock		The lock which is being tested
 *	type		Pass 'SELF' to test only locks with the same
 *			owner as lock, or 'OTHER' to test only locks
 *			with a different owner
 *
 * Returns one of six values:
 *	0) no overlap
 *	1) overlap == lock
 *	2) overlap contains lock
 *	3) lock contains overlap
 *	4) overlap starts before lock
 *	5) overlap ends after lock
 *
 * If there is an overlapping lock, '*overlap' is set to point at the
 * overlapping lock.
 *
 * NOTE: this returns only the FIRST overlapping lock.  There
 *	 may be more than one.
 */
Walk the list of locks for an inode to find an overlapping lock (if
any) and return a classification of that overlap.

*overlap        The place in the lock list to start looking
lock            The lock which is being tested
type            Pass 'SELF' to test only locks with the same
owner as lock, or 'OTHER' to test only locks
with a different owner

Returns one of six values:
0) no overlap
1) overlap == lock
2) overlap contains lock
3) lock contains overlap
4) overlap starts before lock
5) overlap ends after lock

If there is an overlapping lock, '*overlap' is set to point at the
overlapping lock.

this returns only the FIRST overlapping lock.  There
may be more than one.
Walk the list of locks for an inode to find an overlapping lock ( if any ) and return a classification of that overlap . * overlap The place in the lock list to start looking lock The lock which is being tested type Pass ' SELF ' to test only locks with the same owner as lock or ' OTHER ' to test only locks with a different owner Returns one of six values : 0 ) no overlap 1 ) overlap == lock 2 ) overlap contains lock 3 ) lock contains overlap 4 ) overlap starts before lock 5 ) overlap ends after lock If there is an overlapping lock ' * overlap ' is set to point at the overlapping lock . this returns only the FIRST overlapping lock . There may be more than one .
static int
lf_findoverlap(struct lockf_entry **overlap, struct lockf_entry *lock, int type)
{
	struct lockf_entry *lf;
	off_t start, end;
	int res;
	if ((*overlap) == NOLOCKF) {
		return (0);
	}
#ifdef LOCKF_DEBUG
	if (lockf_debug & 2)
		lf_print("lf_findoverlap: looking for overlap in", lock);
#endif 
	start = lock->lf_start;
	end = lock->lf_end;
	res = 0;
	while (*overlap) {
		lf = *overlap;
		if (lf->lf_start > end)
			break;
		if (((type & SELF) && lf->lf_owner != lock->lf_owner) ||
		    ((type & OTHERS) && lf->lf_owner == lock->lf_owner)) {
			*overlap = LIST_NEXT(lf, lf_link);
			continue;
		}
#ifdef LOCKF_DEBUG
		if (lockf_debug & 2)
			lf_print("\tchecking", lf);
#endif 
		if (start > lf->lf_end) {
#ifdef LOCKF_DEBUG
			if (lockf_debug & 2)
				printf("no overlap\n");
#endif 
			*overlap = LIST_NEXT(lf, lf_link);
			continue;
		}
		if (lf->lf_start == start && lf->lf_end == end) {
#ifdef LOCKF_DEBUG
			if (lockf_debug & 2)
				printf("overlap == lock\n");
#endif 
			res = 1;
			break;
		}
		if (lf->lf_start <= start && lf->lf_end >= end) {
#ifdef LOCKF_DEBUG
			if (lockf_debug & 2)
				printf("overlap contains lock\n");
#endif 
			res = 2;
			break;
		}
		if (start <= lf->lf_start && end >= lf->lf_end) {
#ifdef LOCKF_DEBUG
			if (lockf_debug & 2)
				printf("lock contains overlap\n");
#endif 
			res = 3;
			break;
		}
		if (lf->lf_start < start && lf->lf_end >= start) {
#ifdef LOCKF_DEBUG
			if (lockf_debug & 2)
				printf("overlap starts before lock\n");
#endif 
			res = 4;
			break;
		}
		if (lf->lf_start > start && lf->lf_end > end) {
#ifdef LOCKF_DEBUG
			if (lockf_debug & 2)
				printf("overlap ends after lock\n");
#endif 
			res = 5;
			break;
		}
		panic("lf_findoverlap: default");
	}
	return (res);
}
static int lf_findoverlap ( struct lockf_entry * * overlap , struct lockf_entry * lock , int type ) { struct lockf_entry * lf ; off_t start , end ; int res ; if ( ( * overlap ) == NOLOCKF ) { return ( 0 ) ; } #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) lf_print ( " " , lock ) ; #endif start = lock -> lf_start ; end = lock -> lf_end ; res = 0 ; while ( * overlap ) { lf = * overlap ; if ( lf -> lf_start > end ) break ; if ( ( ( type & SELF ) && lf -> lf_owner != lock -> lf_owner ) || ( ( type & OTHERS ) && lf -> lf_owner == lock -> lf_owner ) ) { * overlap = LIST_NEXT ( lf , lf_link ) ; continue ; } #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) lf_print ( " \t " , lf ) ; #endif if ( start > lf -> lf_end ) { #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) printf ( " \n " ) ; #endif * overlap = LIST_NEXT ( lf , lf_link ) ; continue ; } if ( lf -> lf_start == start && lf -> lf_end == end ) { #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) printf ( " \n " ) ; #endif res = 1 ; break ; } if ( lf -> lf_start <= start && lf -> lf_end >= end ) { #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) printf ( " \n " ) ; #endif res = 2 ; break ; } if ( start <= lf -> lf_start && end >= lf -> lf_end ) { #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) printf ( " \n " ) ; #endif res = 3 ; break ; } if ( lf -> lf_start < start && lf -> lf_end >= start ) { #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) printf ( " \n " ) ; #endif res = 4 ; break ; } if ( lf -> lf_start > start && lf -> lf_end > end ) { #ifdef LOCKF_DEBUG if ( lockf_debug & 2 ) printf ( " \n " ) ; #endif res = 5 ; break ; } panic ( " " ) ; } return ( res ) ; }
Walk the list of locks for an inode to find an overlapping lock (if
 any) and return a classification of that overlap.
Walk the list of locks for an inode to find an overlapping lock ( if any ) and return a classification of that overlap .
/* LOCKF_DEBUG */ /* LOCKF_DEBUG */ /*
		 * OK, check for overlap
		 *
		 * Six cases:
		 *	0) no overlap
		 *	1) overlap == lock
		 *	2) overlap contains lock
		 *	3) lock contains overlap
		 *	4) overlap starts before lock
		 *	5) overlap ends after lock
		 */ /* Case 0 */ /* LOCKF_DEBUG */ /* Case 1 */ /* LOCKF_DEBUG */ /* Case 2 */ /* LOCKF_DEBUG */ /* Case 3 */ /* LOCKF_DEBUG */ /* Case 4 */ /* LOCKF_DEBUG */ /* Case 5 */ /* LOCKF_DEBUG */