zfs_rlock.c revision c2e6a7d6abc139a8d59fca4857d6276f3b70ddf9
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains the code to implement file range locking in
* ZFS, although there isn't much specific to ZFS (all that comes to mind
* support for growing the blocksize).
*
* Interface
* ---------
* Defined in zfs_rlock.h but essentially:
* rl = zfs_range_lock(zp, off, len, lock_type);
* zfs_range_unlock(rl);
* zfs_range_reduce(rl, off, len);
*
* AVL tree
* --------
* An AVL tree is used to maintain the state of the existing ranges
* that are locked for exclusive (writer) or shared (reader) use.
* The starting range offset is used for searching and sorting the tree.
*
* Common case
* -----------
* The (hopefully) usual case is of no overlaps or contention for
* locks. On entry to zfs_lock_range() a rl_t is allocated; the tree
* searched that finds no overlap, and *this* rl_t is placed in the tree.
*
* ---------------------------------------
* The avl code only allows one node at a particular offset. Also it's very
* inefficient to search through all previous entries looking for overlaps
* (because the very 1st in the ordered list might be at offset 0 but
* cover the whole file).
* So this implementation uses reference counts and proxy range locks.
* Firstly, only reader locks use reference counts and proxy locks,
* because writer locks are exclusive.
* When a reader lock overlaps with another then a proxy lock is created
* for that range and replaces the original lock. If the overlap
* is exact then the reference count of the proxy is simply incremented.
* Otherwise, the proxy lock is split into smaller lock ranges and
* new proxy locks created for non overlapping ranges.
* The reference counts are adjusted accordingly.
* Meanwhile, the orginal lock is kept around (this is the callers handle)
* and its offset and length are used when releasing the lock.
*
* Thread coordination
* -------------------
* In order to make wakeups efficient and to ensure multiple continuous
* readers on a range don't starve a writer for the same range lock,
* two condition variables are allocated in each rl_t.
* If a writer (or reader) can't get a range it initialises the writer
* (or reader) cv; sets a flag saying there's a writer (or reader) waiting;
* and waits on that cv. When a thread unlocks that range it wakes up all
* writers then all readers before destroying the lock.
*
* Append mode writes
* ------------------
* Append mode writes need to lock a range at the end of a file.
* The offset of the end of the file is determined under the
* range locking mutex, and the lock type converted from RL_APPEND to
* RL_WRITER and the range locked.
*
* Grow block handling
* -------------------
* ZFS supports multiple block sizes currently upto 128K. The smallest
* block size is used for the file which is grown as needed. During this
* growth all other writers and readers must be excluded.
* So if the block size needs to be grown then the whole file is
* exclusively locked, then later the caller will reduce the lock
* range to just the range to be written using zfs_reduce_range.
*/
#include <sys/zfs_rlock.h>
/*
* Check if a write lock can be grabbed, or wait and recheck until available.
*/
static void
{
for (;;) {
/*
* Range locking is also used by zvol and uses a
* dummied up znode. However, for zvol, we don't need to
* append or grow blocksize, and besides we don't have
* a z_phys or z_zfsvfs - so skip that processing.
*
* Yes, this is ugly, and would be solved by not handling
* grow or append in range lock code. If that was done then
* we could make the range locking code generically available
* to other non-zfs consumers.
*/
/*
* If in append mode pick up the current end of file.
* This is done under z_range_lock to avoid races.
*/
/*
* If we need to grow the block size then grab the whole
* file range. This is also done under z_range_lock to
* avoid races.
*/
}
}
/*
* First check for the usual case of no locks
*/
if (avl_numnodes(tree) == 0) {
return;
}
/*
* Look for any locks in the range.
*/
if (rl)
goto wait; /* already locked at same offset */
goto wait;
goto wait;
return;
wait:
if (!rl->r_write_wanted) {
}
/* reset to original */
}
}
/*
* If this is an original (non-proxy) lock then replace it by
* a proxy and return the proxy.
*/
static rl_t *
{
return (rl); /* already a proxy */
/* create a proxy range lock */
return (proxy);
}
/*
* Split the range lock at the supplied offset
* returning the *front* proxy.
*/
static rl_t *
{
/* create the rear proxy range lock */
return (front);
}
/*
* Create and add a new proxy range lock for the supplied range.
*/
static void
{
}
static void
{
/*
* prev arrives either:
* - pointing to an entry at the same offset
* - pointing to the entry with the closest previous offset whose
* range may overlap with the new range
* - null, if there were no ranges starting before the new one
*/
if (prev) {
/*
* convert to proxy if needed then
* split this entry and bump ref count
*/
}
}
if (prev)
else
/* no overlaps, use the original new rl_t in the tree */
return;
}
/* Add a proxy for initial range before the overlap */
}
/*
* We now search forward through the ranges, until we go past the end
* of the new range. For each entry we make it a proxy if it
* isn't already, then bump its reference count. If there's any
* gaps between the ranges then we create a new proxy range.
*/
break;
/* there's a gap */
}
/* exact overlap with end */
return;
}
/* new range ends in the middle of this block */
return;
}
}
/* Add the remaining end range. */
}
/*
* Check if a reader lock can be grabbed, or wait and recheck until available.
*/
static void
{
/*
* Look for any writer locks in the range.
*/
/*
* Check the previous range for a writer lock overlap.
*/
if (!prev->r_read_wanted) {
}
goto retry;
}
goto got_lock;
}
/*
* Search through the following ranges to see if there's
* write lock any overlap.
*/
if (prev)
else
goto got_lock;
if (!next->r_read_wanted) {
}
goto retry;
}
goto got_lock;
}
/*
* Add the read lock, which may involve splitting existing
* locks and bumping ref counts (r_cnt).
*/
}
/*
* Lock a range (offset, length) as either shared (RL_READER)
* or exclusive (RL_WRITER). Returns the range lock structure
* for later unlocking or reduce range (if entire file
* previously locked as RL_WRITER).
*/
rl_t *
{
/*
* First check for the usual case of no locks
*/
else
} else
return (new);
}
/*
* Unlock a reader lock
*/
static void
{
/*
* The common case is when the remove entry is in the tree
* (cnt == 1) meaning there's been no other reader locks overlapping
* with this one. Otherwise the remove entry will have been
* removed from the tree and replaced by proxies (one or
* more ranges mapping to the entire range).
*/
if (remove->r_write_wanted)
if (remove->r_read_wanted)
} else {
/*
* Find start proxy representing this reader lock,
* then decrement ref count on all proxies
* that make up this range, freeing them as needed.
*/
if (len) {
}
if (rl->r_write_wanted)
if (rl->r_read_wanted)
}
}
}
}
/*
* Unlock range and destroy range lock structure.
*/
void
{
/* writer locks can't be shared or split */
if (rl->r_write_wanted)
if (rl->r_read_wanted)
} else {
/*
* lock may be shared, let zfs_range_unlock_reader()
* release the lock and free the rl_t
*/
}
}
/*
* Reduce range locked as RL_WRITER from whole file to specified range.
* Asserts the whole file is exclusivly locked and so there's only one
* entry in the tree.
*/
void
{
/* Ensure there are no other locks */
if (rl->r_write_wanted)
if (rl->r_read_wanted)
}
/*
* AVL comparison function used to order range locks
* Locks are ordered on the start offset of the range.
*/
int
{
return (1);
return (-1);
return (0);
}