ufs_lockf.c
来自「早期freebsd实现」· C语言 代码 · 共 708 行 · 第 1/2 页
C
708 行
/* * Copyright (c) 1982, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Scooter Morris at Genentech Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94 */#include <sys/param.h>#include <sys/systm.h>#include <sys/kernel.h>#include <sys/file.h>#include <sys/proc.h>#include <sys/vnode.h>#include <sys/malloc.h>#include <sys/fcntl.h>#include <ufs/ufs/lockf.h>#include <ufs/ufs/quota.h>#include <ufs/ufs/inode.h>#include <ufs/ufs/ufs_extern.h>/* * This variable controls the maximum number of processes that will * be checked in doing deadlock detection. */int maxlockdepth = MAXDEPTH;#ifdef LOCKF_DEBUGint lockf_debug = 0;#endif#define NOLOCKF (struct lockf *)0#define SELF 0x1#define OTHERS 0x2/* * Set a byte-range lock. */intlf_setlock(lock) register struct lockf *lock;{ register struct lockf *block; struct inode *ip = lock->lf_inode; struct lockf **prev, *overlap, *ltmp; static char lockstr[] = "lockf"; int ovcase, priority, needtolink, error;#ifdef LOCKF_DEBUG if (lockf_debug & 1) lf_print("lf_setlock", lock);#endif /* LOCKF_DEBUG */ /* * Set the priority */ priority = PLOCK; if (lock->lf_type == F_WRLCK) priority += 4; priority |= PCATCH; /* * Scan lock list for this file looking for locks that would block us. */ while (block = lf_getblock(lock)) { /* * Free the structure and return if nonblocking. */ if ((lock->lf_flags & F_WAIT) == 0) { FREE(lock, M_LOCKF); return (EAGAIN); } /* * We are blocked. Since flock style locks cover * the whole file, there is no chance for deadlock. * For byte-range locks we must check for deadlock. * * Deadlock detection is done by looking through the * wait channels to see if there are any cycles that * involve us. MAXDEPTH is set just to make sure we * do not go off into neverland. */ if ((lock->lf_flags & F_POSIX) && (block->lf_flags & F_POSIX)) { register struct proc *wproc; register struct lockf *waitblock; int i = 0; /* The block is waiting on something */ wproc = (struct proc *)block->lf_id; while (wproc->p_wchan && (wproc->p_wmesg == lockstr) && (i++ < maxlockdepth)) { waitblock = (struct lockf *)wproc->p_wchan; /* Get the owner of the blocking lock */ waitblock = waitblock->lf_next; if ((waitblock->lf_flags & F_POSIX) == 0) break; wproc = (struct proc *)waitblock->lf_id; if (wproc == (struct proc *)lock->lf_id) { free(lock, M_LOCKF); return (EDEADLK); } } } /* * For flock type locks, we must first remove * any shared locks that we hold before we sleep * waiting for an exclusive lock. */ if ((lock->lf_flags & F_FLOCK) && lock->lf_type == F_WRLCK) { lock->lf_type = F_UNLCK; (void) lf_clearlock(lock); lock->lf_type = F_WRLCK; } /* * Add our lock to the blocked list and sleep until we're free. * Remember who blocked us (for deadlock detection). */ lock->lf_next = block; lf_addblock(block, lock);#ifdef LOCKF_DEBUG if (lockf_debug & 1) { lf_print("lf_setlock: blocking on", block); lf_printlist("lf_setlock", block); }#endif /* LOCKF_DEBUG */ if (error = tsleep((caddr_t)lock, priority, lockstr, 0)) { /* * Delete ourselves from the waiting to lock list. */ for (block = lock->lf_next; block != NOLOCKF; block = block->lf_block) { if (block->lf_block != lock) continue; block->lf_block = block->lf_block->lf_block; break; } /* * If we did not find ourselves on the list, but * are still linked onto a lock list, then something * is very wrong. */ if (block == NOLOCKF && lock->lf_next != NOLOCKF) panic("lf_setlock: lost lock"); free(lock, M_LOCKF); return (error); } } /* * No blocks!! Add the lock. Note that we will * downgrade or upgrade any overlapping locks this * process already owns. * * Skip over locks owned by other processes. * Handle any locks that overlap and are owned by ourselves. */ prev = &ip->i_lockf; block = ip->i_lockf; needtolink = 1; for (;;) { if (ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap)) block = overlap->lf_next; /* * 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 */ switch (ovcase) { case 0: /* no overlap */ if (needtolink) { *prev = lock; lock->lf_next = overlap; } break; case 1: /* overlap == lock */ /* * If downgrading lock, others may be * able to acquire it. */ if (lock->lf_type == F_RDLCK && overlap->lf_type == F_WRLCK) lf_wakelock(overlap); overlap->lf_type = lock->lf_type; FREE(lock, M_LOCKF); lock = overlap; /* for debug output below */ break; case 2: /* overlap contains lock */ /* * Check for common starting point and different types. */ if (overlap->lf_type == lock->lf_type) { free(lock, M_LOCKF); lock = overlap; /* for debug output below */ break; } if (overlap->lf_start == lock->lf_start) { *prev = lock; lock->lf_next = overlap; overlap->lf_start = lock->lf_end + 1; } else lf_split(overlap, lock); lf_wakelock(overlap); break; case 3: /* lock contains overlap */ /* * If downgrading lock, others may be able to * acquire it, otherwise take the list. */ if (lock->lf_type == F_RDLCK && overlap->lf_type == F_WRLCK) { lf_wakelock(overlap); } else { ltmp = lock->lf_block; lock->lf_block = overlap->lf_block; lf_addblock(lock, ltmp); } /* * Add the new lock if necessary and delete the overlap. */ if (needtolink) { *prev = lock; lock->lf_next = overlap->lf_next; prev = &lock->lf_next; needtolink = 0; } else *prev = overlap->lf_next; free(overlap, M_LOCKF); continue; case 4: /* overlap starts before lock */ /* * Add lock after overlap on the list. */ lock->lf_next = overlap->lf_next; overlap->lf_next = lock; overlap->lf_end = lock->lf_start - 1; prev = &lock->lf_next; lf_wakelock(overlap); needtolink = 0; continue; case 5: /* overlap ends after lock */ /* * Add the new lock before overlap. */ if (needtolink) { *prev = lock; lock->lf_next = overlap; } overlap->lf_start = lock->lf_end + 1; lf_wakelock(overlap); break; } break; }#ifdef LOCKF_DEBUG if (lockf_debug & 1) { lf_print("lf_setlock: got the lock", lock); lf_printlist("lf_setlock", lock); }#endif /* LOCKF_DEBUG */ return (0);}/* * Remove a byte-range lock on an inode. * * Generally, find the lock (or an overlap to that lock) * and remove it (or shrink it), then wakeup anyone we can. */intlf_clearlock(unlock) register struct lockf *unlock;{ struct inode *ip = unlock->lf_inode; register struct lockf *lf = ip->i_lockf; struct lockf *overlap, **prev; int ovcase; if (lf == NOLOCKF) return (0);#ifdef LOCKF_DEBUG if (unlock->lf_type != F_UNLCK) panic("lf_clearlock: bad type"); if (lockf_debug & 1) lf_print("lf_clearlock", unlock);#endif /* LOCKF_DEBUG */ prev = &ip->i_lockf; while (ovcase = lf_findoverlap(lf, unlock, SELF, &prev, &overlap)) { /* * Wakeup the list of locks to be retried. */ lf_wakelock(overlap); switch (ovcase) { case 1: /* overlap == lock */ *prev = overlap->lf_next; FREE(overlap, M_LOCKF); break; case 2: /* overlap contains lock: split it */ if (overlap->lf_start == unlock->lf_start) { overlap->lf_start = unlock->lf_end + 1; break; } lf_split(overlap, unlock); overlap->lf_next = unlock->lf_next; break; case 3: /* lock contains overlap */ *prev = overlap->lf_next; lf = overlap->lf_next; free(overlap, M_LOCKF);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?