2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
/*
|
2010-05-28 20:45:14 +00:00
|
|
|
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
|
2008-11-20 20:01:55 +00:00
|
|
|
* Use is subject to license terms.
|
|
|
|
*/
|
2013-05-10 21:17:03 +00:00
|
|
|
/*
|
2018-10-01 22:13:12 +00:00
|
|
|
* Copyright (c) 2012, 2018 by Delphix. All rights reserved.
|
2013-05-10 21:17:03 +00:00
|
|
|
*/
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains the code to implement file range locking in
|
2013-06-11 17:12:34 +00:00
|
|
|
* ZFS, although there isn't much specific to ZFS (all that comes to mind is
|
2008-11-20 20:01:55 +00:00
|
|
|
* support for growing the blocksize).
|
|
|
|
*
|
|
|
|
* Interface
|
|
|
|
* ---------
|
|
|
|
* Defined in zfs_rlock.h but essentially:
|
2018-10-01 22:13:12 +00:00
|
|
|
* lr = rangelock_enter(zp, off, len, lock_type);
|
|
|
|
* rangelock_reduce(lr, off, len); // optional
|
|
|
|
* rangelock_exit(lr);
|
2008-11-20 20:01:55 +00:00
|
|
|
*
|
2019-11-06 18:54:25 +00:00
|
|
|
* Range locking rules
|
|
|
|
* --------------------
|
|
|
|
* 1. When truncating a file (zfs_create, zfs_setattr, zfs_space) the whole
|
|
|
|
* file range needs to be locked as RL_WRITER. Only then can the pages be
|
|
|
|
* freed etc and zp_size reset. zp_size must be set within range lock.
|
|
|
|
* 2. For writes and punching holes (zfs_write & zfs_space) just the range
|
|
|
|
* being written or freed needs to be locked as RL_WRITER.
|
|
|
|
* Multiple writes at the end of the file must coordinate zp_size updates
|
|
|
|
* to ensure data isn't lost. A compare and swap loop is currently used
|
|
|
|
* to ensure the file size is at least the offset last written.
|
|
|
|
* 3. For reads (zfs_read, zfs_get_data & zfs_putapage) just the range being
|
|
|
|
* read needs to be locked as RL_READER. A check against zp_size can then
|
|
|
|
* be made for reading beyond end of file.
|
|
|
|
*
|
2008-11-20 20:01:55 +00:00
|
|
|
* 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
|
|
|
|
* -----------
|
2018-10-01 22:13:12 +00:00
|
|
|
* The (hopefully) usual case is of no overlaps or contention for locks. On
|
|
|
|
* entry to rangelock_enter(), a locked_range_t is allocated; the tree
|
|
|
|
* searched that finds no overlap, and *this* locked_range_t is placed in the
|
|
|
|
* tree.
|
2008-11-20 20:01:55 +00:00
|
|
|
*
|
|
|
|
* Overlaps/Reference counting/Proxy locks
|
|
|
|
* ---------------------------------------
|
|
|
|
* 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.
|
2017-01-03 17:31:18 +00:00
|
|
|
* Meanwhile, the original lock is kept around (this is the callers handle)
|
2008-11-20 20:01:55 +00:00
|
|
|
* 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
|
|
|
|
* -------------------
|
2018-10-01 22:13:12 +00:00
|
|
|
* ZFS supports multiple block sizes, up to 16MB. The smallest
|
2008-11-20 20:01:55 +00:00
|
|
|
* 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
|
2018-10-01 22:13:12 +00:00
|
|
|
* range to just the range to be written using rangelock_reduce().
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
#include <sys/zfs_context.h>
|
2008-11-20 20:01:55 +00:00
|
|
|
#include <sys/zfs_rlock.h>
|
2018-10-01 22:13:12 +00:00
|
|
|
|
2019-11-06 18:54:25 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
/*
|
|
|
|
* AVL comparison function used to order range locks
|
|
|
|
* Locks are ordered on the start offset of the range.
|
|
|
|
*/
|
|
|
|
static int
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_compare(const void *arg1, const void *arg2)
|
2018-10-01 22:13:12 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
const zfs_locked_range_t *rl1 = (const zfs_locked_range_t *)arg1;
|
|
|
|
const zfs_locked_range_t *rl2 = (const zfs_locked_range_t *)arg2;
|
2018-10-01 22:13:12 +00:00
|
|
|
|
Reduce loaded range tree memory usage
This patch implements a new tree structure for ZFS, and uses it to
store range trees more efficiently.
The new structure is approximately a B-tree, though there are some
small differences from the usual characterizations. The tree has core
nodes and leaf nodes; each contain data elements, which the elements
in the core nodes acting as separators between its children. The
difference between core and leaf nodes is that the core nodes have an
array of children, while leaf nodes don't. Every node in the tree may
be only partially full; in most cases, they are all at least 50% full
(in terms of element count) except for the root node, which can be
less full. Underfull nodes will steal from their neighbors or merge to
remain full enough, while overfull nodes will split in two. The data
elements are contained in tree-controlled buffers; they are copied
into these on insertion, and overwritten on deletion. This means that
the elements are not independently allocated, which reduces overhead,
but also means they can't be shared between trees (and also that
pointers to them are only valid until a side-effectful tree operation
occurs). The overhead varies based on how dense the tree is, but is
usually on the order of about 50% of the element size; the per-node
overheads are very small, and so don't make a significant difference.
The trees can accept arbitrary records; they accept a size and a
comparator to allow them to be used for a variety of purposes.
The new trees replace the AVL trees used in the range trees today.
Currently, the range_seg_t structure contains three 8 byte integers
of payload and two 24 byte avl_tree_node_ts to handle its storage in
both an offset-sorted tree and a size-sorted tree (total size: 64
bytes). In the new model, the range seg structures are usually two 4
byte integers, but a separate one needs to exist for the size-sorted
and offset-sorted tree. Between the raw size, the 50% overhead, and
the double storage, the new btrees are expected to use 8*1.5*2 = 24
bytes per record, or 33.3% as much memory as the AVL trees (this is
for the purposes of storing metaslab range trees; for other purposes,
like scrubs, they use ~50% as much memory).
We reduced the size of the payload in the range segments by teaching
range trees about starting offsets and shifts; since metaslabs have a
fixed starting offset, and they all operate in terms of disk sectors,
we can store the ranges using 4-byte integers as long as the size of
the metaslab divided by the sector size is less than 2^32. For 512-byte
sectors, this is a 2^41 (or 2TB) metaslab, which with the default
settings corresponds to a 256PB disk. 4k sector disks can handle
metaslabs up to 2^46 bytes, or 2^63 byte disks. Since we do not
anticipate disks of this size in the near future, there should be
almost no cases where metaslabs need 64-byte integers to store their
ranges. We do still have the capability to store 64-byte integer ranges
to account for cases where we are storing per-vdev (or per-dnode) trees,
which could reasonably go above the limits discussed. We also do not
store fill information in the compact version of the node, since it
is only used for sorted scrub.
We also optimized the metaslab loading process in various other ways
to offset some inefficiencies in the btree model. While individual
operations (find, insert, remove_from) are faster for the btree than
they are for the avl tree, remove usually requires a find operation,
while in the AVL tree model the element itself suffices. Some clever
changes actually caused an overall speedup in metaslab loading; we use
approximately 40% less cpu to load metaslabs in our tests on Illumos.
Another memory and performance optimization was achieved by changing
what is stored in the size-sorted trees. When a disk is heavily
fragmented, the df algorithm used by default in ZFS will almost always
find a number of small regions in its initial cursor-based search; it
will usually only fall back to the size-sorted tree to find larger
regions. If we increase the size of the cursor-based search slightly,
and don't store segments that are smaller than a tunable size floor
in the size-sorted tree, we can further cut memory usage down to
below 20% of what the AVL trees store. This also results in further
reductions in CPU time spent loading metaslabs.
The 16KiB size floor was chosen because it results in substantial memory
usage reduction while not usually resulting in situations where we can't
find an appropriate chunk with the cursor and are forced to use an
oversized chunk from the size-sorted tree. In addition, even if we do
have to use an oversized chunk from the size-sorted tree, the chunk
would be too small to use for ZIL allocations, so it isn't as big of a
loss as it might otherwise be. And often, more small allocations will
follow the initial one, and the cursor search will now find the
remainder of the chunk we didn't use all of and use it for subsequent
allocations. Practical testing has shown little or no change in
fragmentation as a result of this change.
If the size-sorted tree becomes empty while the offset sorted one still
has entries, it will load all the entries from the offset sorted tree
and disregard the size floor until it is unloaded again. This operation
occurs rarely with the default setting, only on incredibly thoroughly
fragmented pools.
There are some other small changes to zdb to teach it to handle btrees,
but nothing major.
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Matt Ahrens <matt@delphix.com>
Reviewed by: Sebastien Roy seb@delphix.com
Reviewed-by: Igor Kozhukhov <igor@dilos.org>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Paul Dagnelie <pcd@delphix.com>
Closes #9181
2019-10-09 17:36:03 +00:00
|
|
|
return (TREE_CMP(rl1->lr_offset, rl2->lr_offset));
|
2018-10-01 22:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The callback is invoked when acquiring a RL_WRITER or RL_APPEND lock.
|
|
|
|
* It must convert RL_APPEND to RL_WRITER (starting at the end of the file),
|
|
|
|
* and may increase the range that's locked for RL_WRITER.
|
|
|
|
*/
|
|
|
|
void
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_init(zfs_rangelock_t *rl, zfs_rangelock_cb_t *cb, void *arg)
|
2018-10-01 22:13:12 +00:00
|
|
|
{
|
|
|
|
mutex_init(&rl->rl_lock, NULL, MUTEX_DEFAULT, NULL);
|
2019-10-03 22:54:29 +00:00
|
|
|
avl_create(&rl->rl_tree, zfs_rangelock_compare,
|
2019-11-01 17:37:33 +00:00
|
|
|
sizeof (zfs_locked_range_t), offsetof(zfs_locked_range_t, lr_node));
|
2018-10-01 22:13:12 +00:00
|
|
|
rl->rl_cb = cb;
|
|
|
|
rl->rl_arg = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_fini(zfs_rangelock_t *rl)
|
2018-10-01 22:13:12 +00:00
|
|
|
{
|
|
|
|
mutex_destroy(&rl->rl_lock);
|
|
|
|
avl_destroy(&rl->rl_tree);
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
2020-06-30 19:35:29 +00:00
|
|
|
* Check if a write lock can be grabbed. If not, fail immediately or sleep and
|
|
|
|
* recheck until available, depending on the value of the "nonblock" parameter.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2020-06-30 19:35:29 +00:00
|
|
|
static boolean_t
|
|
|
|
zfs_rangelock_enter_writer(zfs_rangelock_t *rl, zfs_locked_range_t *new,
|
|
|
|
boolean_t nonblock)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2018-10-01 22:13:12 +00:00
|
|
|
avl_tree_t *tree = &rl->rl_tree;
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *lr;
|
2008-11-20 20:01:55 +00:00
|
|
|
avl_index_t where;
|
2018-10-01 22:13:12 +00:00
|
|
|
uint64_t orig_off = new->lr_offset;
|
|
|
|
uint64_t orig_len = new->lr_length;
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_type_t orig_type = new->lr_type;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/*
|
2018-10-01 22:13:12 +00:00
|
|
|
* Call callback which can modify new->r_off,len,type.
|
|
|
|
* Note, the callback is used by the ZPL to handle appending
|
|
|
|
* and changing blocksizes. It isn't needed for zvols.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2018-10-01 22:13:12 +00:00
|
|
|
if (rl->rl_cb != NULL) {
|
|
|
|
rl->rl_cb(new, rl->rl_arg);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
/*
|
|
|
|
* If the type was APPEND, the callback must convert it to
|
|
|
|
* WRITER.
|
|
|
|
*/
|
|
|
|
ASSERT3U(new->lr_type, ==, RL_WRITER);
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* First check for the usual case of no locks
|
|
|
|
*/
|
|
|
|
if (avl_numnodes(tree) == 0) {
|
|
|
|
avl_add(tree, new);
|
2020-06-30 19:35:29 +00:00
|
|
|
return (B_TRUE);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for any locks in the range.
|
|
|
|
*/
|
2018-10-01 22:13:12 +00:00
|
|
|
lr = avl_find(tree, new, &where);
|
|
|
|
if (lr != NULL)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto wait; /* already locked at same offset */
|
|
|
|
|
2019-11-01 17:37:33 +00:00
|
|
|
lr = avl_nearest(tree, where, AVL_AFTER);
|
2018-10-01 22:13:12 +00:00
|
|
|
if (lr != NULL &&
|
|
|
|
lr->lr_offset < new->lr_offset + new->lr_length)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto wait;
|
|
|
|
|
2019-11-01 17:37:33 +00:00
|
|
|
lr = avl_nearest(tree, where, AVL_BEFORE);
|
2018-10-01 22:13:12 +00:00
|
|
|
if (lr != NULL &&
|
|
|
|
lr->lr_offset + lr->lr_length > new->lr_offset)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto wait;
|
|
|
|
|
|
|
|
avl_insert(tree, new, where);
|
2020-06-30 19:35:29 +00:00
|
|
|
return (B_TRUE);
|
2008-11-20 20:01:55 +00:00
|
|
|
wait:
|
2020-06-30 19:35:29 +00:00
|
|
|
if (nonblock)
|
|
|
|
return (B_FALSE);
|
2018-10-01 22:13:12 +00:00
|
|
|
if (!lr->lr_write_wanted) {
|
|
|
|
cv_init(&lr->lr_write_cv, NULL, CV_DEFAULT, NULL);
|
|
|
|
lr->lr_write_wanted = B_TRUE;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
cv_wait(&lr->lr_write_cv, &rl->rl_lock);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/* reset to original */
|
2018-10-01 22:13:12 +00:00
|
|
|
new->lr_offset = orig_off;
|
|
|
|
new->lr_length = orig_len;
|
|
|
|
new->lr_type = orig_type;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is an original (non-proxy) lock then replace it by
|
|
|
|
* a proxy and return the proxy.
|
|
|
|
*/
|
2019-11-01 17:37:33 +00:00
|
|
|
static zfs_locked_range_t *
|
|
|
|
zfs_rangelock_proxify(avl_tree_t *tree, zfs_locked_range_t *lr)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *proxy;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
if (lr->lr_proxy)
|
|
|
|
return (lr); /* already a proxy */
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT3U(lr->lr_count, ==, 1);
|
|
|
|
ASSERT(lr->lr_write_wanted == B_FALSE);
|
|
|
|
ASSERT(lr->lr_read_wanted == B_FALSE);
|
|
|
|
avl_remove(tree, lr);
|
|
|
|
lr->lr_count = 0;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/* create a proxy range lock */
|
2019-11-01 17:37:33 +00:00
|
|
|
proxy = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
|
2018-10-01 22:13:12 +00:00
|
|
|
proxy->lr_offset = lr->lr_offset;
|
|
|
|
proxy->lr_length = lr->lr_length;
|
|
|
|
proxy->lr_count = 1;
|
|
|
|
proxy->lr_type = RL_READER;
|
|
|
|
proxy->lr_proxy = B_TRUE;
|
|
|
|
proxy->lr_write_wanted = B_FALSE;
|
|
|
|
proxy->lr_read_wanted = B_FALSE;
|
2008-11-20 20:01:55 +00:00
|
|
|
avl_add(tree, proxy);
|
|
|
|
|
|
|
|
return (proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Split the range lock at the supplied offset
|
|
|
|
* returning the *front* proxy.
|
|
|
|
*/
|
2019-11-01 17:37:33 +00:00
|
|
|
static zfs_locked_range_t *
|
|
|
|
zfs_rangelock_split(avl_tree_t *tree, zfs_locked_range_t *lr, uint64_t off)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *rear;
|
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT3U(lr->lr_length, >, 1);
|
|
|
|
ASSERT3U(off, >, lr->lr_offset);
|
|
|
|
ASSERT3U(off, <, lr->lr_offset + lr->lr_length);
|
|
|
|
ASSERT(lr->lr_write_wanted == B_FALSE);
|
|
|
|
ASSERT(lr->lr_read_wanted == B_FALSE);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/* create the rear proxy range lock */
|
2019-11-01 17:37:33 +00:00
|
|
|
rear = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
|
2018-10-01 22:13:12 +00:00
|
|
|
rear->lr_offset = off;
|
|
|
|
rear->lr_length = lr->lr_offset + lr->lr_length - off;
|
|
|
|
rear->lr_count = lr->lr_count;
|
|
|
|
rear->lr_type = RL_READER;
|
|
|
|
rear->lr_proxy = B_TRUE;
|
|
|
|
rear->lr_write_wanted = B_FALSE;
|
|
|
|
rear->lr_read_wanted = B_FALSE;
|
|
|
|
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *front = zfs_rangelock_proxify(tree, lr);
|
2018-10-01 22:13:12 +00:00
|
|
|
front->lr_length = off - lr->lr_offset;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
avl_insert_here(tree, rear, front, AVL_AFTER);
|
|
|
|
return (front);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create and add a new proxy range lock for the supplied range.
|
|
|
|
*/
|
|
|
|
static void
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_new_proxy(avl_tree_t *tree, uint64_t off, uint64_t len)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *lr;
|
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT(len != 0);
|
2019-11-01 17:37:33 +00:00
|
|
|
lr = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
|
2018-10-01 22:13:12 +00:00
|
|
|
lr->lr_offset = off;
|
|
|
|
lr->lr_length = len;
|
|
|
|
lr->lr_count = 1;
|
|
|
|
lr->lr_type = RL_READER;
|
|
|
|
lr->lr_proxy = B_TRUE;
|
|
|
|
lr->lr_write_wanted = B_FALSE;
|
|
|
|
lr->lr_read_wanted = B_FALSE;
|
|
|
|
avl_add(tree, lr);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_add_reader(avl_tree_t *tree, zfs_locked_range_t *new,
|
|
|
|
zfs_locked_range_t *prev, avl_index_t where)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *next;
|
2018-10-01 22:13:12 +00:00
|
|
|
uint64_t off = new->lr_offset;
|
|
|
|
uint64_t len = new->lr_length;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2018-10-01 22:13:12 +00:00
|
|
|
if (prev != NULL) {
|
|
|
|
if (prev->lr_offset + prev->lr_length <= off) {
|
2008-11-20 20:01:55 +00:00
|
|
|
prev = NULL;
|
2018-10-01 22:13:12 +00:00
|
|
|
} else if (prev->lr_offset != off) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* convert to proxy if needed then
|
|
|
|
* split this entry and bump ref count
|
|
|
|
*/
|
2019-10-03 22:54:29 +00:00
|
|
|
prev = zfs_rangelock_split(tree, prev, off);
|
2008-11-20 20:01:55 +00:00
|
|
|
prev = AVL_NEXT(tree, prev); /* move to rear range */
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT((prev == NULL) || (prev->lr_offset == off));
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
if (prev != NULL)
|
2008-11-20 20:01:55 +00:00
|
|
|
next = prev;
|
|
|
|
else
|
2018-10-01 22:13:12 +00:00
|
|
|
next = avl_nearest(tree, where, AVL_AFTER);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
if (next == NULL || off + len <= next->lr_offset) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/* no overlaps, use the original new rl_t in the tree */
|
|
|
|
avl_insert(tree, new, where);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
if (off < next->lr_offset) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/* Add a proxy for initial range before the overlap */
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_new_proxy(tree, off, next->lr_offset - off);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
new->lr_count = 0; /* will use proxies in tree */
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
for (prev = NULL; next; prev = next, next = AVL_NEXT(tree, next)) {
|
2018-10-01 22:13:12 +00:00
|
|
|
if (off + len <= next->lr_offset)
|
2008-11-20 20:01:55 +00:00
|
|
|
break;
|
2018-10-01 22:13:12 +00:00
|
|
|
if (prev != NULL && prev->lr_offset + prev->lr_length <
|
|
|
|
next->lr_offset) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/* there's a gap */
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT3U(next->lr_offset, >,
|
|
|
|
prev->lr_offset + prev->lr_length);
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_new_proxy(tree,
|
2018-10-01 22:13:12 +00:00
|
|
|
prev->lr_offset + prev->lr_length,
|
|
|
|
next->lr_offset -
|
|
|
|
(prev->lr_offset + prev->lr_length));
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
if (off + len == next->lr_offset + next->lr_length) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/* exact overlap with end */
|
2019-10-03 22:54:29 +00:00
|
|
|
next = zfs_rangelock_proxify(tree, next);
|
2018-10-01 22:13:12 +00:00
|
|
|
next->lr_count++;
|
2008-11-20 20:01:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
if (off + len < next->lr_offset + next->lr_length) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/* new range ends in the middle of this block */
|
2019-10-03 22:54:29 +00:00
|
|
|
next = zfs_rangelock_split(tree, next, off + len);
|
2018-10-01 22:13:12 +00:00
|
|
|
next->lr_count++;
|
2008-11-20 20:01:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT3U(off + len, >, next->lr_offset + next->lr_length);
|
2019-10-03 22:54:29 +00:00
|
|
|
next = zfs_rangelock_proxify(tree, next);
|
2018-10-01 22:13:12 +00:00
|
|
|
next->lr_count++;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the remaining end range. */
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_new_proxy(tree, prev->lr_offset + prev->lr_length,
|
2018-10-01 22:13:12 +00:00
|
|
|
(off + len) - (prev->lr_offset + prev->lr_length));
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-06-30 19:35:29 +00:00
|
|
|
* Check if a reader lock can be grabbed. If not, fail immediately or sleep and
|
|
|
|
* recheck until available, depending on the value of the "nonblock" parameter.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2020-06-30 19:35:29 +00:00
|
|
|
static boolean_t
|
|
|
|
zfs_rangelock_enter_reader(zfs_rangelock_t *rl, zfs_locked_range_t *new,
|
|
|
|
boolean_t nonblock)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2018-10-01 22:13:12 +00:00
|
|
|
avl_tree_t *tree = &rl->rl_tree;
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *prev, *next;
|
2008-11-20 20:01:55 +00:00
|
|
|
avl_index_t where;
|
2018-10-01 22:13:12 +00:00
|
|
|
uint64_t off = new->lr_offset;
|
|
|
|
uint64_t len = new->lr_length;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for any writer locks in the range.
|
|
|
|
*/
|
|
|
|
retry:
|
|
|
|
prev = avl_find(tree, new, &where);
|
|
|
|
if (prev == NULL)
|
2019-11-01 17:37:33 +00:00
|
|
|
prev = avl_nearest(tree, where, AVL_BEFORE);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the previous range for a writer lock overlap.
|
|
|
|
*/
|
2018-10-01 22:13:12 +00:00
|
|
|
if (prev && (off < prev->lr_offset + prev->lr_length)) {
|
|
|
|
if ((prev->lr_type == RL_WRITER) || (prev->lr_write_wanted)) {
|
2020-06-30 19:35:29 +00:00
|
|
|
if (nonblock)
|
|
|
|
return (B_FALSE);
|
2018-10-01 22:13:12 +00:00
|
|
|
if (!prev->lr_read_wanted) {
|
|
|
|
cv_init(&prev->lr_read_cv,
|
|
|
|
NULL, CV_DEFAULT, NULL);
|
|
|
|
prev->lr_read_wanted = B_TRUE;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
cv_wait(&prev->lr_read_cv, &rl->rl_lock);
|
2008-11-20 20:01:55 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
if (off + len < prev->lr_offset + prev->lr_length)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto got_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search through the following ranges to see if there's
|
|
|
|
* write lock any overlap.
|
|
|
|
*/
|
2018-10-01 22:13:12 +00:00
|
|
|
if (prev != NULL)
|
2008-11-20 20:01:55 +00:00
|
|
|
next = AVL_NEXT(tree, prev);
|
|
|
|
else
|
2019-11-01 17:37:33 +00:00
|
|
|
next = avl_nearest(tree, where, AVL_AFTER);
|
2018-10-01 22:13:12 +00:00
|
|
|
for (; next != NULL; next = AVL_NEXT(tree, next)) {
|
|
|
|
if (off + len <= next->lr_offset)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto got_lock;
|
2018-10-01 22:13:12 +00:00
|
|
|
if ((next->lr_type == RL_WRITER) || (next->lr_write_wanted)) {
|
2020-06-30 19:35:29 +00:00
|
|
|
if (nonblock)
|
|
|
|
return (B_FALSE);
|
2018-10-01 22:13:12 +00:00
|
|
|
if (!next->lr_read_wanted) {
|
|
|
|
cv_init(&next->lr_read_cv,
|
|
|
|
NULL, CV_DEFAULT, NULL);
|
|
|
|
next->lr_read_wanted = B_TRUE;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
cv_wait(&next->lr_read_cv, &rl->rl_lock);
|
2008-11-20 20:01:55 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
if (off + len <= next->lr_offset + next->lr_length)
|
2008-11-20 20:01:55 +00:00
|
|
|
goto got_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
got_lock:
|
|
|
|
/*
|
|
|
|
* Add the read lock, which may involve splitting existing
|
2018-10-01 22:13:12 +00:00
|
|
|
* locks and bumping ref counts (r_count).
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_add_reader(tree, new, prev, where);
|
2020-06-30 19:35:29 +00:00
|
|
|
return (B_TRUE);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-10-01 22:13:12 +00:00
|
|
|
* Lock a range (offset, length) as either shared (RL_READER) or exclusive
|
|
|
|
* (RL_WRITER or RL_APPEND). If RL_APPEND is specified, rl_cb() will convert
|
|
|
|
* it to a RL_WRITER lock (with the offset at the end of the file). Returns
|
|
|
|
* the range lock structure for later unlocking (or reduce range if the
|
2020-06-30 19:35:29 +00:00
|
|
|
* entire file is locked as RL_WRITER), or NULL if nonblock is true and the
|
|
|
|
* lock could not be acquired immediately.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2020-06-30 19:35:29 +00:00
|
|
|
static zfs_locked_range_t *
|
|
|
|
zfs_rangelock_enter_impl(zfs_rangelock_t *rl, uint64_t off, uint64_t len,
|
|
|
|
zfs_rangelock_type_t type, boolean_t nonblock)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *new;
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
ASSERT(type == RL_READER || type == RL_WRITER || type == RL_APPEND);
|
|
|
|
|
2019-11-01 17:37:33 +00:00
|
|
|
new = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
|
2018-10-01 22:13:12 +00:00
|
|
|
new->lr_rangelock = rl;
|
|
|
|
new->lr_offset = off;
|
2009-02-18 20:51:31 +00:00
|
|
|
if (len + off < off) /* overflow */
|
|
|
|
len = UINT64_MAX - off;
|
2018-10-01 22:13:12 +00:00
|
|
|
new->lr_length = len;
|
|
|
|
new->lr_count = 1; /* assume it's going to be in the tree */
|
|
|
|
new->lr_type = type;
|
|
|
|
new->lr_proxy = B_FALSE;
|
|
|
|
new->lr_write_wanted = B_FALSE;
|
|
|
|
new->lr_read_wanted = B_FALSE;
|
|
|
|
|
|
|
|
mutex_enter(&rl->rl_lock);
|
2008-11-20 20:01:55 +00:00
|
|
|
if (type == RL_READER) {
|
|
|
|
/*
|
|
|
|
* First check for the usual case of no locks
|
|
|
|
*/
|
2020-06-30 19:35:29 +00:00
|
|
|
if (avl_numnodes(&rl->rl_tree) == 0) {
|
2018-10-01 22:13:12 +00:00
|
|
|
avl_add(&rl->rl_tree, new);
|
2020-06-30 19:35:29 +00:00
|
|
|
} else if (!zfs_rangelock_enter_reader(rl, new, nonblock)) {
|
|
|
|
kmem_free(new, sizeof (*new));
|
|
|
|
new = NULL;
|
|
|
|
}
|
|
|
|
} else if (!zfs_rangelock_enter_writer(rl, new, nonblock)) {
|
|
|
|
kmem_free(new, sizeof (*new));
|
|
|
|
new = NULL;
|
2019-10-03 22:54:29 +00:00
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
mutex_exit(&rl->rl_lock);
|
2008-11-20 20:01:55 +00:00
|
|
|
return (new);
|
|
|
|
}
|
|
|
|
|
2020-06-30 19:35:29 +00:00
|
|
|
zfs_locked_range_t *
|
|
|
|
zfs_rangelock_enter(zfs_rangelock_t *rl, uint64_t off, uint64_t len,
|
|
|
|
zfs_rangelock_type_t type)
|
|
|
|
{
|
|
|
|
return (zfs_rangelock_enter_impl(rl, off, len, type, B_FALSE));
|
|
|
|
}
|
|
|
|
|
|
|
|
zfs_locked_range_t *
|
|
|
|
zfs_rangelock_tryenter(zfs_rangelock_t *rl, uint64_t off, uint64_t len,
|
|
|
|
zfs_rangelock_type_t type)
|
|
|
|
{
|
|
|
|
return (zfs_rangelock_enter_impl(rl, off, len, type, B_TRUE));
|
|
|
|
}
|
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
/*
|
2019-11-01 17:37:33 +00:00
|
|
|
* Safely free the zfs_locked_range_t.
|
2018-10-01 22:13:12 +00:00
|
|
|
*/
|
2011-02-04 22:38:11 +00:00
|
|
|
static void
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_free(zfs_locked_range_t *lr)
|
2011-02-04 22:38:11 +00:00
|
|
|
{
|
2018-10-01 22:13:12 +00:00
|
|
|
if (lr->lr_write_wanted)
|
|
|
|
cv_destroy(&lr->lr_write_cv);
|
2011-02-04 22:38:11 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
if (lr->lr_read_wanted)
|
|
|
|
cv_destroy(&lr->lr_read_cv);
|
2011-02-04 22:38:11 +00:00
|
|
|
|
2019-11-01 17:37:33 +00:00
|
|
|
kmem_free(lr, sizeof (zfs_locked_range_t));
|
2011-02-04 22:38:11 +00:00
|
|
|
}
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Unlock a reader lock
|
|
|
|
*/
|
|
|
|
static void
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_exit_reader(zfs_rangelock_t *rl, zfs_locked_range_t *remove,
|
2018-10-01 22:13:12 +00:00
|
|
|
list_t *free_list)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2018-10-01 22:13:12 +00:00
|
|
|
avl_tree_t *tree = &rl->rl_tree;
|
2008-11-20 20:01:55 +00:00
|
|
|
uint64_t len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2018-10-01 22:13:12 +00:00
|
|
|
if (remove->lr_count == 1) {
|
2008-11-20 20:01:55 +00:00
|
|
|
avl_remove(tree, remove);
|
2018-10-01 22:13:12 +00:00
|
|
|
if (remove->lr_write_wanted)
|
|
|
|
cv_broadcast(&remove->lr_write_cv);
|
|
|
|
if (remove->lr_read_wanted)
|
|
|
|
cv_broadcast(&remove->lr_read_cv);
|
2011-03-08 20:17:35 +00:00
|
|
|
list_insert_tail(free_list, remove);
|
2008-11-20 20:01:55 +00:00
|
|
|
} else {
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT0(remove->lr_count);
|
|
|
|
ASSERT0(remove->lr_write_wanted);
|
|
|
|
ASSERT0(remove->lr_read_wanted);
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* Find start proxy representing this reader lock,
|
|
|
|
* then decrement ref count on all proxies
|
|
|
|
* that make up this range, freeing them as needed.
|
|
|
|
*/
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *lr = avl_find(tree, remove, NULL);
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT3P(lr, !=, NULL);
|
|
|
|
ASSERT3U(lr->lr_count, !=, 0);
|
|
|
|
ASSERT3U(lr->lr_type, ==, RL_READER);
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *next = NULL;
|
2018-10-01 22:13:12 +00:00
|
|
|
for (len = remove->lr_length; len != 0; lr = next) {
|
|
|
|
len -= lr->lr_length;
|
|
|
|
if (len != 0) {
|
|
|
|
next = AVL_NEXT(tree, lr);
|
|
|
|
ASSERT3P(next, !=, NULL);
|
|
|
|
ASSERT3U(lr->lr_offset + lr->lr_length, ==,
|
|
|
|
next->lr_offset);
|
|
|
|
ASSERT3U(next->lr_count, !=, 0);
|
|
|
|
ASSERT3U(next->lr_type, ==, RL_READER);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
lr->lr_count--;
|
|
|
|
if (lr->lr_count == 0) {
|
|
|
|
avl_remove(tree, lr);
|
|
|
|
if (lr->lr_write_wanted)
|
|
|
|
cv_broadcast(&lr->lr_write_cv);
|
|
|
|
if (lr->lr_read_wanted)
|
|
|
|
cv_broadcast(&lr->lr_read_cv);
|
|
|
|
list_insert_tail(free_list, lr);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-01 17:37:33 +00:00
|
|
|
kmem_free(remove, sizeof (zfs_locked_range_t));
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlock range and destroy range lock structure.
|
|
|
|
*/
|
|
|
|
void
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_exit(zfs_locked_range_t *lr)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_t *rl = lr->lr_rangelock;
|
2011-03-08 20:17:35 +00:00
|
|
|
list_t free_list;
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_locked_range_t *free_lr;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT(lr->lr_type == RL_WRITER || lr->lr_type == RL_READER);
|
|
|
|
ASSERT(lr->lr_count == 1 || lr->lr_count == 0);
|
|
|
|
ASSERT(!lr->lr_proxy);
|
2011-02-04 22:38:11 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
/*
|
|
|
|
* The free list is used to defer the cv_destroy() and
|
|
|
|
* subsequent kmem_free until after the mutex is dropped.
|
|
|
|
*/
|
2019-11-01 17:37:33 +00:00
|
|
|
list_create(&free_list, sizeof (zfs_locked_range_t),
|
|
|
|
offsetof(zfs_locked_range_t, lr_node));
|
2011-02-04 22:38:11 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
mutex_enter(&rl->rl_lock);
|
|
|
|
if (lr->lr_type == RL_WRITER) {
|
|
|
|
/* writer locks can't be shared or split */
|
|
|
|
avl_remove(&rl->rl_tree, lr);
|
|
|
|
if (lr->lr_write_wanted)
|
|
|
|
cv_broadcast(&lr->lr_write_cv);
|
|
|
|
if (lr->lr_read_wanted)
|
|
|
|
cv_broadcast(&lr->lr_read_cv);
|
|
|
|
list_insert_tail(&free_list, lr);
|
2008-11-20 20:01:55 +00:00
|
|
|
} else {
|
|
|
|
/*
|
2018-10-01 22:13:12 +00:00
|
|
|
* lock may be shared, let rangelock_exit_reader()
|
2019-11-01 17:37:33 +00:00
|
|
|
* release the lock and free the zfs_locked_range_t.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_exit_reader(rl, lr, &free_list);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2018-10-01 22:13:12 +00:00
|
|
|
mutex_exit(&rl->rl_lock);
|
2011-03-08 20:17:35 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
while ((free_lr = list_remove_head(&free_list)) != NULL)
|
2019-10-03 22:54:29 +00:00
|
|
|
zfs_rangelock_free(free_lr);
|
2011-03-08 20:17:35 +00:00
|
|
|
|
|
|
|
list_destroy(&free_list);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reduce range locked as RL_WRITER from whole file to specified range.
|
2018-10-01 22:13:12 +00:00
|
|
|
* Asserts the whole file is exclusively locked and so there's only one
|
2008-11-20 20:01:55 +00:00
|
|
|
* entry in the tree.
|
|
|
|
*/
|
|
|
|
void
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_reduce(zfs_locked_range_t *lr, uint64_t off, uint64_t len)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2019-11-01 17:37:33 +00:00
|
|
|
zfs_rangelock_t *rl = lr->lr_rangelock;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/* Ensure there are no other locks */
|
2018-10-01 22:13:12 +00:00
|
|
|
ASSERT3U(avl_numnodes(&rl->rl_tree), ==, 1);
|
|
|
|
ASSERT3U(lr->lr_offset, ==, 0);
|
|
|
|
ASSERT3U(lr->lr_type, ==, RL_WRITER);
|
|
|
|
ASSERT(!lr->lr_proxy);
|
|
|
|
ASSERT3U(lr->lr_length, ==, UINT64_MAX);
|
|
|
|
ASSERT3U(lr->lr_count, ==, 1);
|
|
|
|
|
|
|
|
mutex_enter(&rl->rl_lock);
|
|
|
|
lr->lr_offset = off;
|
|
|
|
lr->lr_length = len;
|
|
|
|
mutex_exit(&rl->rl_lock);
|
|
|
|
if (lr->lr_write_wanted)
|
|
|
|
cv_broadcast(&lr->lr_write_cv);
|
|
|
|
if (lr->lr_read_wanted)
|
|
|
|
cv_broadcast(&lr->lr_read_cv);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2016-04-11 21:53:48 +00:00
|
|
|
|
2018-10-01 22:13:12 +00:00
|
|
|
#if defined(_KERNEL)
|
2019-10-03 22:54:29 +00:00
|
|
|
EXPORT_SYMBOL(zfs_rangelock_init);
|
|
|
|
EXPORT_SYMBOL(zfs_rangelock_fini);
|
|
|
|
EXPORT_SYMBOL(zfs_rangelock_enter);
|
2020-06-30 19:35:29 +00:00
|
|
|
EXPORT_SYMBOL(zfs_rangelock_tryenter);
|
2019-10-03 22:54:29 +00:00
|
|
|
EXPORT_SYMBOL(zfs_rangelock_exit);
|
|
|
|
EXPORT_SYMBOL(zfs_rangelock_reduce);
|
2016-04-11 21:53:48 +00:00
|
|
|
#endif
|