4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
30 * This file contains the code to implement file range locking in
31 * ZFS, although there isn't much specific to ZFS (all that comes to mind is
32 * support for growing the blocksize).
36 * Defined in zfs_rlock.h but essentially:
37 * lr = rangelock_enter(zp, off, len, lock_type);
38 * rangelock_reduce(lr, off, len); // optional
43 * An AVL tree is used to maintain the state of the existing ranges
44 * that are locked for exclusive (writer) or shared (reader) use.
45 * The starting range offset is used for searching and sorting the tree.
49 * The (hopefully) usual case is of no overlaps or contention for locks. On
50 * entry to rangelock_enter(), a locked_range_t is allocated; the tree
51 * searched that finds no overlap, and *this* locked_range_t is placed in the
54 * Overlaps/Reference counting/Proxy locks
55 * ---------------------------------------
56 * The avl code only allows one node at a particular offset. Also it's very
57 * inefficient to search through all previous entries looking for overlaps
58 * (because the very 1st in the ordered list might be at offset 0 but
59 * cover the whole file).
60 * So this implementation uses reference counts and proxy range locks.
61 * Firstly, only reader locks use reference counts and proxy locks,
62 * because writer locks are exclusive.
63 * When a reader lock overlaps with another then a proxy lock is created
64 * for that range and replaces the original lock. If the overlap
65 * is exact then the reference count of the proxy is simply incremented.
66 * Otherwise, the proxy lock is split into smaller lock ranges and
67 * new proxy locks created for non overlapping ranges.
68 * The reference counts are adjusted accordingly.
69 * Meanwhile, the orginal lock is kept around (this is the callers handle)
70 * and its offset and length are used when releasing the lock.
74 * In order to make wakeups efficient and to ensure multiple continuous
75 * readers on a range don't starve a writer for the same range lock,
76 * two condition variables are allocated in each rl_t.
77 * If a writer (or reader) can't get a range it initialises the writer
78 * (or reader) cv; sets a flag saying there's a writer (or reader) waiting;
79 * and waits on that cv. When a thread unlocks that range it wakes up all
80 * writers then all readers before destroying the lock.
84 * Append mode writes need to lock a range at the end of a file.
85 * The offset of the end of the file is determined under the
86 * range locking mutex, and the lock type converted from RL_APPEND to
87 * RL_WRITER and the range locked.
91 * ZFS supports multiple block sizes, up to 16MB. The smallest
92 * block size is used for the file which is grown as needed. During this
93 * growth all other writers and readers must be excluded.
94 * So if the block size needs to be grown then the whole file is
95 * exclusively locked, then later the caller will reduce the lock
96 * range to just the range to be written using rangelock_reduce().
99 #include <sys/zfs_context.h>
101 #include <sys/zfs_rlock.h>
104 * AVL comparison function used to order range locks
105 * Locks are ordered on the start offset of the range.
108 rangelock_compare(const void *arg1, const void *arg2)
110 const locked_range_t *rl1 = arg1;
111 const locked_range_t *rl2 = arg2;
113 if (rl1->lr_offset > rl2->lr_offset)
115 if (rl1->lr_offset < rl2->lr_offset)
121 * The callback is invoked when acquiring a RL_WRITER or RL_APPEND lock.
122 * It must convert RL_APPEND to RL_WRITER (starting at the end of the file),
123 * and may increase the range that's locked for RL_WRITER.
126 rangelock_init(rangelock_t *rl, rangelock_cb_t *cb, void *arg)
128 mutex_init(&rl->rl_lock, NULL, MUTEX_DEFAULT, NULL);
129 avl_create(&rl->rl_tree, rangelock_compare,
130 sizeof (locked_range_t), offsetof(locked_range_t, lr_node));
136 rangelock_fini(rangelock_t *rl)
138 mutex_destroy(&rl->rl_lock);
139 avl_destroy(&rl->rl_tree);
143 * Check if a write lock can be grabbed, or wait and recheck until available.
146 rangelock_enter_writer(rangelock_t *rl, locked_range_t *new)
148 avl_tree_t *tree = &rl->rl_tree;
151 uint64_t orig_off = new->lr_offset;
152 uint64_t orig_len = new->lr_length;
153 rangelock_type_t orig_type = new->lr_type;
157 * Call callback which can modify new->r_off,len,type.
158 * Note, the callback is used by the ZPL to handle appending
159 * and changing blocksizes. It isn't needed for zvols.
161 if (rl->rl_cb != NULL) {
162 rl->rl_cb(new, rl->rl_arg);
166 * If the type was APPEND, the callback must convert it to
169 ASSERT3U(new->lr_type, ==, RL_WRITER);
172 * First check for the usual case of no locks
174 if (avl_numnodes(tree) == 0) {
180 * Look for any locks in the range.
182 lr = avl_find(tree, new, &where);
184 goto wait; /* already locked at same offset */
186 lr = (locked_range_t *)avl_nearest(tree, where, AVL_AFTER);
188 lr->lr_offset < new->lr_offset + new->lr_length)
191 lr = (locked_range_t *)avl_nearest(tree, where, AVL_BEFORE);
193 lr->lr_offset + lr->lr_length > new->lr_offset)
196 avl_insert(tree, new, where);
199 if (!lr->lr_write_wanted) {
200 cv_init(&lr->lr_write_cv, NULL, CV_DEFAULT, NULL);
201 lr->lr_write_wanted = B_TRUE;
203 cv_wait(&lr->lr_write_cv, &rl->rl_lock);
205 /* reset to original */
206 new->lr_offset = orig_off;
207 new->lr_length = orig_len;
208 new->lr_type = orig_type;
213 * If this is an original (non-proxy) lock then replace it by
214 * a proxy and return the proxy.
216 static locked_range_t *
217 rangelock_proxify(avl_tree_t *tree, locked_range_t *lr)
219 locked_range_t *proxy;
222 return (lr); /* already a proxy */
224 ASSERT3U(lr->lr_count, ==, 1);
225 ASSERT(lr->lr_write_wanted == B_FALSE);
226 ASSERT(lr->lr_read_wanted == B_FALSE);
227 avl_remove(tree, lr);
230 /* create a proxy range lock */
231 proxy = kmem_alloc(sizeof (locked_range_t), KM_SLEEP);
232 proxy->lr_offset = lr->lr_offset;
233 proxy->lr_length = lr->lr_length;
235 proxy->lr_type = RL_READER;
236 proxy->lr_proxy = B_TRUE;
237 proxy->lr_write_wanted = B_FALSE;
238 proxy->lr_read_wanted = B_FALSE;
239 avl_add(tree, proxy);
245 * Split the range lock at the supplied offset
246 * returning the *front* proxy.
248 static locked_range_t *
249 rangelock_split(avl_tree_t *tree, locked_range_t *lr, uint64_t off)
251 ASSERT3U(lr->lr_length, >, 1);
252 ASSERT3U(off, >, lr->lr_offset);
253 ASSERT3U(off, <, lr->lr_offset + lr->lr_length);
254 ASSERT(lr->lr_write_wanted == B_FALSE);
255 ASSERT(lr->lr_read_wanted == B_FALSE);
257 /* create the rear proxy range lock */
258 locked_range_t *rear = kmem_alloc(sizeof (locked_range_t), KM_SLEEP);
259 rear->lr_offset = off;
260 rear->lr_length = lr->lr_offset + lr->lr_length - off;
261 rear->lr_count = lr->lr_count;
262 rear->lr_type = RL_READER;
263 rear->lr_proxy = B_TRUE;
264 rear->lr_write_wanted = B_FALSE;
265 rear->lr_read_wanted = B_FALSE;
267 locked_range_t *front = rangelock_proxify(tree, lr);
268 front->lr_length = off - lr->lr_offset;
270 avl_insert_here(tree, rear, front, AVL_AFTER);
275 * Create and add a new proxy range lock for the supplied range.
278 rangelock_new_proxy(avl_tree_t *tree, uint64_t off, uint64_t len)
281 locked_range_t *lr = kmem_alloc(sizeof (locked_range_t), KM_SLEEP);
285 lr->lr_type = RL_READER;
286 lr->lr_proxy = B_TRUE;
287 lr->lr_write_wanted = B_FALSE;
288 lr->lr_read_wanted = B_FALSE;
293 rangelock_add_reader(avl_tree_t *tree, locked_range_t *new,
294 locked_range_t *prev, avl_index_t where)
296 locked_range_t *next;
297 uint64_t off = new->lr_offset;
298 uint64_t len = new->lr_length;
301 * prev arrives either:
302 * - pointing to an entry at the same offset
303 * - pointing to the entry with the closest previous offset whose
304 * range may overlap with the new range
305 * - null, if there were no ranges starting before the new one
308 if (prev->lr_offset + prev->lr_length <= off) {
310 } else if (prev->lr_offset != off) {
312 * convert to proxy if needed then
313 * split this entry and bump ref count
315 prev = rangelock_split(tree, prev, off);
316 prev = AVL_NEXT(tree, prev); /* move to rear range */
319 ASSERT((prev == NULL) || (prev->lr_offset == off));
324 next = avl_nearest(tree, where, AVL_AFTER);
326 if (next == NULL || off + len <= next->lr_offset) {
327 /* no overlaps, use the original new rl_t in the tree */
328 avl_insert(tree, new, where);
332 if (off < next->lr_offset) {
333 /* Add a proxy for initial range before the overlap */
334 rangelock_new_proxy(tree, off, next->lr_offset - off);
337 new->lr_count = 0; /* will use proxies in tree */
339 * We now search forward through the ranges, until we go past the end
340 * of the new range. For each entry we make it a proxy if it
341 * isn't already, then bump its reference count. If there's any
342 * gaps between the ranges then we create a new proxy range.
344 for (prev = NULL; next; prev = next, next = AVL_NEXT(tree, next)) {
345 if (off + len <= next->lr_offset)
347 if (prev != NULL && prev->lr_offset + prev->lr_length <
350 ASSERT3U(next->lr_offset, >,
351 prev->lr_offset + prev->lr_length);
352 rangelock_new_proxy(tree,
353 prev->lr_offset + prev->lr_length,
355 (prev->lr_offset + prev->lr_length));
357 if (off + len == next->lr_offset + next->lr_length) {
358 /* exact overlap with end */
359 next = rangelock_proxify(tree, next);
363 if (off + len < next->lr_offset + next->lr_length) {
364 /* new range ends in the middle of this block */
365 next = rangelock_split(tree, next, off + len);
369 ASSERT3U(off + len, >, next->lr_offset + next->lr_length);
370 next = rangelock_proxify(tree, next);
374 /* Add the remaining end range. */
375 rangelock_new_proxy(tree, prev->lr_offset + prev->lr_length,
376 (off + len) - (prev->lr_offset + prev->lr_length));
380 * Check if a reader lock can be grabbed, or wait and recheck until available.
383 rangelock_enter_reader(rangelock_t *rl, locked_range_t *new)
385 avl_tree_t *tree = &rl->rl_tree;
386 locked_range_t *prev, *next;
388 uint64_t off = new->lr_offset;
389 uint64_t len = new->lr_length;
392 * Look for any writer locks in the range.
395 prev = avl_find(tree, new, &where);
397 prev = (locked_range_t *)avl_nearest(tree, where, AVL_BEFORE);
400 * Check the previous range for a writer lock overlap.
402 if (prev && (off < prev->lr_offset + prev->lr_length)) {
403 if ((prev->lr_type == RL_WRITER) || (prev->lr_write_wanted)) {
404 if (!prev->lr_read_wanted) {
405 cv_init(&prev->lr_read_cv,
406 NULL, CV_DEFAULT, NULL);
407 prev->lr_read_wanted = B_TRUE;
409 cv_wait(&prev->lr_read_cv, &rl->rl_lock);
412 if (off + len < prev->lr_offset + prev->lr_length)
417 * Search through the following ranges to see if there's
418 * write lock any overlap.
421 next = AVL_NEXT(tree, prev);
423 next = (locked_range_t *)avl_nearest(tree, where, AVL_AFTER);
424 for (; next != NULL; next = AVL_NEXT(tree, next)) {
425 if (off + len <= next->lr_offset)
427 if ((next->lr_type == RL_WRITER) || (next->lr_write_wanted)) {
428 if (!next->lr_read_wanted) {
429 cv_init(&next->lr_read_cv,
430 NULL, CV_DEFAULT, NULL);
431 next->lr_read_wanted = B_TRUE;
433 cv_wait(&next->lr_read_cv, &rl->rl_lock);
436 if (off + len <= next->lr_offset + next->lr_length)
442 * Add the read lock, which may involve splitting existing
443 * locks and bumping ref counts (r_count).
445 rangelock_add_reader(tree, new, prev, where);
449 * Lock a range (offset, length) as either shared (RL_READER) or exclusive
450 * (RL_WRITER or RL_APPEND). If RL_APPEND is specified, rl_cb() will convert
451 * it to a RL_WRITER lock (with the offset at the end of the file). Returns
452 * the range lock structure for later unlocking (or reduce range if the
453 * entire file is locked as RL_WRITER).
456 rangelock_enter(rangelock_t *rl, uint64_t off, uint64_t len,
457 rangelock_type_t type)
459 ASSERT(type == RL_READER || type == RL_WRITER || type == RL_APPEND);
461 locked_range_t *new = kmem_alloc(sizeof (locked_range_t), KM_SLEEP);
462 new->lr_rangelock = rl;
463 new->lr_offset = off;
464 if (len + off < off) /* overflow */
465 len = UINT64_MAX - off;
466 new->lr_length = len;
467 new->lr_count = 1; /* assume it's going to be in the tree */
469 new->lr_proxy = B_FALSE;
470 new->lr_write_wanted = B_FALSE;
471 new->lr_read_wanted = B_FALSE;
473 mutex_enter(&rl->rl_lock);
474 if (type == RL_READER) {
476 * First check for the usual case of no locks
478 if (avl_numnodes(&rl->rl_tree) == 0)
479 avl_add(&rl->rl_tree, new);
481 rangelock_enter_reader(rl, new);
483 rangelock_enter_writer(rl, new); /* RL_WRITER or RL_APPEND */
484 mutex_exit(&rl->rl_lock);
489 * Unlock a reader lock
492 rangelock_exit_reader(rangelock_t *rl, locked_range_t *remove)
494 avl_tree_t *tree = &rl->rl_tree;
498 * The common case is when the remove entry is in the tree
499 * (cnt == 1) meaning there's been no other reader locks overlapping
500 * with this one. Otherwise the remove entry will have been
501 * removed from the tree and replaced by proxies (one or
502 * more ranges mapping to the entire range).
504 if (remove->lr_count == 1) {
505 avl_remove(tree, remove);
506 if (remove->lr_write_wanted) {
507 cv_broadcast(&remove->lr_write_cv);
508 cv_destroy(&remove->lr_write_cv);
510 if (remove->lr_read_wanted) {
511 cv_broadcast(&remove->lr_read_cv);
512 cv_destroy(&remove->lr_read_cv);
515 ASSERT0(remove->lr_count);
516 ASSERT0(remove->lr_write_wanted);
517 ASSERT0(remove->lr_read_wanted);
519 * Find start proxy representing this reader lock,
520 * then decrement ref count on all proxies
521 * that make up this range, freeing them as needed.
523 locked_range_t *lr = avl_find(tree, remove, NULL);
524 ASSERT3P(lr, !=, NULL);
525 ASSERT3U(lr->lr_count, !=, 0);
526 ASSERT3U(lr->lr_type, ==, RL_READER);
527 locked_range_t *next = NULL;
528 for (len = remove->lr_length; len != 0; lr = next) {
529 len -= lr->lr_length;
531 next = AVL_NEXT(tree, lr);
532 ASSERT3P(next, !=, NULL);
533 ASSERT3U(lr->lr_offset + lr->lr_length, ==,
535 ASSERT3U(next->lr_count, !=, 0);
536 ASSERT3U(next->lr_type, ==, RL_READER);
539 if (lr->lr_count == 0) {
540 avl_remove(tree, lr);
541 if (lr->lr_write_wanted) {
542 cv_broadcast(&lr->lr_write_cv);
543 cv_destroy(&lr->lr_write_cv);
545 if (lr->lr_read_wanted) {
546 cv_broadcast(&lr->lr_read_cv);
547 cv_destroy(&lr->lr_read_cv);
549 kmem_free(lr, sizeof (locked_range_t));
553 kmem_free(remove, sizeof (locked_range_t));
557 * Unlock range and destroy range lock structure.
560 rangelock_exit(locked_range_t *lr)
562 rangelock_t *rl = lr->lr_rangelock;
564 ASSERT(lr->lr_type == RL_WRITER || lr->lr_type == RL_READER);
565 ASSERT(lr->lr_count == 1 || lr->lr_count == 0);
566 ASSERT(!lr->lr_proxy);
568 mutex_enter(&rl->rl_lock);
569 if (lr->lr_type == RL_WRITER) {
570 /* writer locks can't be shared or split */
571 avl_remove(&rl->rl_tree, lr);
572 mutex_exit(&rl->rl_lock);
573 if (lr->lr_write_wanted) {
574 cv_broadcast(&lr->lr_write_cv);
575 cv_destroy(&lr->lr_write_cv);
577 if (lr->lr_read_wanted) {
578 cv_broadcast(&lr->lr_read_cv);
579 cv_destroy(&lr->lr_read_cv);
581 kmem_free(lr, sizeof (locked_range_t));
584 * lock may be shared, let rangelock_exit_reader()
585 * release the lock and free the rl_t
587 rangelock_exit_reader(rl, lr);
588 mutex_exit(&rl->rl_lock);
593 * Reduce range locked as RL_WRITER from whole file to specified range.
594 * Asserts the whole file is exclusively locked and so there's only one
598 rangelock_reduce(locked_range_t *lr, uint64_t off, uint64_t len)
600 rangelock_t *rl = lr->lr_rangelock;
602 /* Ensure there are no other locks */
603 ASSERT3U(avl_numnodes(&rl->rl_tree), ==, 1);
604 ASSERT3U(lr->lr_offset, ==, 0);
605 ASSERT3U(lr->lr_type, ==, RL_WRITER);
606 ASSERT(!lr->lr_proxy);
607 ASSERT3U(lr->lr_length, ==, UINT64_MAX);
608 ASSERT3U(lr->lr_count, ==, 1);
610 mutex_enter(&rl->rl_lock);
613 mutex_exit(&rl->rl_lock);
614 if (lr->lr_write_wanted)
615 cv_broadcast(&lr->lr_write_cv);
616 if (lr->lr_read_wanted)
617 cv_broadcast(&lr->lr_read_cv);