1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23eda14cbcSMatt Macy * Use is subject to license terms.
24eda14cbcSMatt Macy */
25eda14cbcSMatt Macy /*
26eda14cbcSMatt Macy * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
27eda14cbcSMatt Macy */
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy /*
30eda14cbcSMatt Macy * This file contains the code to implement file range locking in
31eda14cbcSMatt Macy * ZFS, although there isn't much specific to ZFS (all that comes to mind is
32eda14cbcSMatt Macy * support for growing the blocksize).
33eda14cbcSMatt Macy *
34eda14cbcSMatt Macy * Interface
35eda14cbcSMatt Macy * ---------
36eda14cbcSMatt Macy * Defined in zfs_rlock.h but essentially:
37eda14cbcSMatt Macy * lr = rangelock_enter(zp, off, len, lock_type);
38eda14cbcSMatt Macy * rangelock_reduce(lr, off, len); // optional
39eda14cbcSMatt Macy * rangelock_exit(lr);
40eda14cbcSMatt Macy *
41eda14cbcSMatt Macy * Range locking rules
42eda14cbcSMatt Macy * --------------------
43eda14cbcSMatt Macy * 1. When truncating a file (zfs_create, zfs_setattr, zfs_space) the whole
44eda14cbcSMatt Macy * file range needs to be locked as RL_WRITER. Only then can the pages be
45eda14cbcSMatt Macy * freed etc and zp_size reset. zp_size must be set within range lock.
46eda14cbcSMatt Macy * 2. For writes and punching holes (zfs_write & zfs_space) just the range
47eda14cbcSMatt Macy * being written or freed needs to be locked as RL_WRITER.
48eda14cbcSMatt Macy * Multiple writes at the end of the file must coordinate zp_size updates
49eda14cbcSMatt Macy * to ensure data isn't lost. A compare and swap loop is currently used
50eda14cbcSMatt Macy * to ensure the file size is at least the offset last written.
51eda14cbcSMatt Macy * 3. For reads (zfs_read, zfs_get_data & zfs_putapage) just the range being
52eda14cbcSMatt Macy * read needs to be locked as RL_READER. A check against zp_size can then
53eda14cbcSMatt Macy * be made for reading beyond end of file.
54eda14cbcSMatt Macy *
55eda14cbcSMatt Macy * AVL tree
56eda14cbcSMatt Macy * --------
57eda14cbcSMatt Macy * An AVL tree is used to maintain the state of the existing ranges
58eda14cbcSMatt Macy * that are locked for exclusive (writer) or shared (reader) use.
59eda14cbcSMatt Macy * The starting range offset is used for searching and sorting the tree.
60eda14cbcSMatt Macy *
61eda14cbcSMatt Macy * Common case
62eda14cbcSMatt Macy * -----------
63eda14cbcSMatt Macy * The (hopefully) usual case is of no overlaps or contention for locks. On
64eda14cbcSMatt Macy * entry to rangelock_enter(), a locked_range_t is allocated; the tree
65eda14cbcSMatt Macy * searched that finds no overlap, and *this* locked_range_t is placed in the
66eda14cbcSMatt Macy * tree.
67eda14cbcSMatt Macy *
68eda14cbcSMatt Macy * Overlaps/Reference counting/Proxy locks
69eda14cbcSMatt Macy * ---------------------------------------
70eda14cbcSMatt Macy * The avl code only allows one node at a particular offset. Also it's very
71eda14cbcSMatt Macy * inefficient to search through all previous entries looking for overlaps
72eda14cbcSMatt Macy * (because the very 1st in the ordered list might be at offset 0 but
73eda14cbcSMatt Macy * cover the whole file).
74eda14cbcSMatt Macy * So this implementation uses reference counts and proxy range locks.
75eda14cbcSMatt Macy * Firstly, only reader locks use reference counts and proxy locks,
76eda14cbcSMatt Macy * because writer locks are exclusive.
77eda14cbcSMatt Macy * When a reader lock overlaps with another then a proxy lock is created
78eda14cbcSMatt Macy * for that range and replaces the original lock. If the overlap
79eda14cbcSMatt Macy * is exact then the reference count of the proxy is simply incremented.
80eda14cbcSMatt Macy * Otherwise, the proxy lock is split into smaller lock ranges and
81eda14cbcSMatt Macy * new proxy locks created for non overlapping ranges.
82eda14cbcSMatt Macy * The reference counts are adjusted accordingly.
83eda14cbcSMatt Macy * Meanwhile, the original lock is kept around (this is the callers handle)
84eda14cbcSMatt Macy * and its offset and length are used when releasing the lock.
85eda14cbcSMatt Macy *
86eda14cbcSMatt Macy * Thread coordination
87eda14cbcSMatt Macy * -------------------
88eda14cbcSMatt Macy * In order to make wakeups efficient and to ensure multiple continuous
89eda14cbcSMatt Macy * readers on a range don't starve a writer for the same range lock,
90eda14cbcSMatt Macy * two condition variables are allocated in each rl_t.
91eda14cbcSMatt Macy * If a writer (or reader) can't get a range it initialises the writer
92eda14cbcSMatt Macy * (or reader) cv; sets a flag saying there's a writer (or reader) waiting;
93eda14cbcSMatt Macy * and waits on that cv. When a thread unlocks that range it wakes up all
94eda14cbcSMatt Macy * writers then all readers before destroying the lock.
95eda14cbcSMatt Macy *
96eda14cbcSMatt Macy * Append mode writes
97eda14cbcSMatt Macy * ------------------
98eda14cbcSMatt Macy * Append mode writes need to lock a range at the end of a file.
99eda14cbcSMatt Macy * The offset of the end of the file is determined under the
100eda14cbcSMatt Macy * range locking mutex, and the lock type converted from RL_APPEND to
101eda14cbcSMatt Macy * RL_WRITER and the range locked.
102eda14cbcSMatt Macy *
103eda14cbcSMatt Macy * Grow block handling
104eda14cbcSMatt Macy * -------------------
105eda14cbcSMatt Macy * ZFS supports multiple block sizes, up to 16MB. The smallest
106eda14cbcSMatt Macy * block size is used for the file which is grown as needed. During this
107eda14cbcSMatt Macy * growth all other writers and readers must be excluded.
108eda14cbcSMatt Macy * So if the block size needs to be grown then the whole file is
109eda14cbcSMatt Macy * exclusively locked, then later the caller will reduce the lock
110eda14cbcSMatt Macy * range to just the range to be written using rangelock_reduce().
111eda14cbcSMatt Macy */
112eda14cbcSMatt Macy
113eda14cbcSMatt Macy #include <sys/zfs_context.h>
114eda14cbcSMatt Macy #include <sys/zfs_rlock.h>
115eda14cbcSMatt Macy
116eda14cbcSMatt Macy
117eda14cbcSMatt Macy /*
118eda14cbcSMatt Macy * AVL comparison function used to order range locks
119eda14cbcSMatt Macy * Locks are ordered on the start offset of the range.
120eda14cbcSMatt Macy */
121eda14cbcSMatt Macy static int
zfs_rangelock_compare(const void * arg1,const void * arg2)122eda14cbcSMatt Macy zfs_rangelock_compare(const void *arg1, const void *arg2)
123eda14cbcSMatt Macy {
124eda14cbcSMatt Macy const zfs_locked_range_t *rl1 = (const zfs_locked_range_t *)arg1;
125eda14cbcSMatt Macy const zfs_locked_range_t *rl2 = (const zfs_locked_range_t *)arg2;
126eda14cbcSMatt Macy
127eda14cbcSMatt Macy return (TREE_CMP(rl1->lr_offset, rl2->lr_offset));
128eda14cbcSMatt Macy }
129eda14cbcSMatt Macy
130eda14cbcSMatt Macy /*
131eda14cbcSMatt Macy * The callback is invoked when acquiring a RL_WRITER or RL_APPEND lock.
132eda14cbcSMatt Macy * It must convert RL_APPEND to RL_WRITER (starting at the end of the file),
133eda14cbcSMatt Macy * and may increase the range that's locked for RL_WRITER.
134eda14cbcSMatt Macy */
135eda14cbcSMatt Macy void
zfs_rangelock_init(zfs_rangelock_t * rl,zfs_rangelock_cb_t * cb,void * arg)136eda14cbcSMatt Macy zfs_rangelock_init(zfs_rangelock_t *rl, zfs_rangelock_cb_t *cb, void *arg)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy mutex_init(&rl->rl_lock, NULL, MUTEX_DEFAULT, NULL);
139eda14cbcSMatt Macy avl_create(&rl->rl_tree, zfs_rangelock_compare,
140eda14cbcSMatt Macy sizeof (zfs_locked_range_t), offsetof(zfs_locked_range_t, lr_node));
141eda14cbcSMatt Macy rl->rl_cb = cb;
142eda14cbcSMatt Macy rl->rl_arg = arg;
143eda14cbcSMatt Macy }
144eda14cbcSMatt Macy
145eda14cbcSMatt Macy void
zfs_rangelock_fini(zfs_rangelock_t * rl)146eda14cbcSMatt Macy zfs_rangelock_fini(zfs_rangelock_t *rl)
147eda14cbcSMatt Macy {
148eda14cbcSMatt Macy mutex_destroy(&rl->rl_lock);
149eda14cbcSMatt Macy avl_destroy(&rl->rl_tree);
150eda14cbcSMatt Macy }
151eda14cbcSMatt Macy
152eda14cbcSMatt Macy /*
153eda14cbcSMatt Macy * Check if a write lock can be grabbed. If not, fail immediately or sleep and
154eda14cbcSMatt Macy * recheck until available, depending on the value of the "nonblock" parameter.
155eda14cbcSMatt Macy */
156eda14cbcSMatt Macy static boolean_t
zfs_rangelock_enter_writer(zfs_rangelock_t * rl,zfs_locked_range_t * new,boolean_t nonblock)157eda14cbcSMatt Macy zfs_rangelock_enter_writer(zfs_rangelock_t *rl, zfs_locked_range_t *new,
158eda14cbcSMatt Macy boolean_t nonblock)
159eda14cbcSMatt Macy {
160eda14cbcSMatt Macy avl_tree_t *tree = &rl->rl_tree;
161eda14cbcSMatt Macy zfs_locked_range_t *lr;
162eda14cbcSMatt Macy avl_index_t where;
163eda14cbcSMatt Macy uint64_t orig_off = new->lr_offset;
164eda14cbcSMatt Macy uint64_t orig_len = new->lr_length;
165eda14cbcSMatt Macy zfs_rangelock_type_t orig_type = new->lr_type;
166eda14cbcSMatt Macy
167eda14cbcSMatt Macy for (;;) {
168eda14cbcSMatt Macy /*
169eda14cbcSMatt Macy * Call callback which can modify new->r_off,len,type.
170eda14cbcSMatt Macy * Note, the callback is used by the ZPL to handle appending
171eda14cbcSMatt Macy * and changing blocksizes. It isn't needed for zvols.
172eda14cbcSMatt Macy */
173eda14cbcSMatt Macy if (rl->rl_cb != NULL) {
174eda14cbcSMatt Macy rl->rl_cb(new, rl->rl_arg);
175eda14cbcSMatt Macy }
176eda14cbcSMatt Macy
177eda14cbcSMatt Macy /*
178eda14cbcSMatt Macy * If the type was APPEND, the callback must convert it to
179eda14cbcSMatt Macy * WRITER.
180eda14cbcSMatt Macy */
181eda14cbcSMatt Macy ASSERT3U(new->lr_type, ==, RL_WRITER);
182eda14cbcSMatt Macy
183eda14cbcSMatt Macy /*
184eda14cbcSMatt Macy * First check for the usual case of no locks
185eda14cbcSMatt Macy */
186eda14cbcSMatt Macy if (avl_numnodes(tree) == 0) {
187eda14cbcSMatt Macy avl_add(tree, new);
188eda14cbcSMatt Macy return (B_TRUE);
189eda14cbcSMatt Macy }
190eda14cbcSMatt Macy
191eda14cbcSMatt Macy /*
192eda14cbcSMatt Macy * Look for any locks in the range.
193eda14cbcSMatt Macy */
194eda14cbcSMatt Macy lr = avl_find(tree, new, &where);
195eda14cbcSMatt Macy if (lr != NULL)
196eda14cbcSMatt Macy goto wait; /* already locked at same offset */
197eda14cbcSMatt Macy
198eda14cbcSMatt Macy lr = avl_nearest(tree, where, AVL_AFTER);
199eda14cbcSMatt Macy if (lr != NULL &&
200eda14cbcSMatt Macy lr->lr_offset < new->lr_offset + new->lr_length)
201eda14cbcSMatt Macy goto wait;
202eda14cbcSMatt Macy
203eda14cbcSMatt Macy lr = avl_nearest(tree, where, AVL_BEFORE);
204eda14cbcSMatt Macy if (lr != NULL &&
205eda14cbcSMatt Macy lr->lr_offset + lr->lr_length > new->lr_offset)
206eda14cbcSMatt Macy goto wait;
207eda14cbcSMatt Macy
208eda14cbcSMatt Macy avl_insert(tree, new, where);
209eda14cbcSMatt Macy return (B_TRUE);
210eda14cbcSMatt Macy wait:
211eda14cbcSMatt Macy if (nonblock)
212eda14cbcSMatt Macy return (B_FALSE);
213eda14cbcSMatt Macy if (!lr->lr_write_wanted) {
214eda14cbcSMatt Macy cv_init(&lr->lr_write_cv, NULL, CV_DEFAULT, NULL);
215eda14cbcSMatt Macy lr->lr_write_wanted = B_TRUE;
216eda14cbcSMatt Macy }
217eda14cbcSMatt Macy cv_wait(&lr->lr_write_cv, &rl->rl_lock);
218eda14cbcSMatt Macy
219eda14cbcSMatt Macy /* reset to original */
220eda14cbcSMatt Macy new->lr_offset = orig_off;
221eda14cbcSMatt Macy new->lr_length = orig_len;
222eda14cbcSMatt Macy new->lr_type = orig_type;
223eda14cbcSMatt Macy }
224eda14cbcSMatt Macy }
225eda14cbcSMatt Macy
226eda14cbcSMatt Macy /*
227eda14cbcSMatt Macy * If this is an original (non-proxy) lock then replace it by
228eda14cbcSMatt Macy * a proxy and return the proxy.
229eda14cbcSMatt Macy */
230eda14cbcSMatt Macy static zfs_locked_range_t *
zfs_rangelock_proxify(avl_tree_t * tree,zfs_locked_range_t * lr)231eda14cbcSMatt Macy zfs_rangelock_proxify(avl_tree_t *tree, zfs_locked_range_t *lr)
232eda14cbcSMatt Macy {
233eda14cbcSMatt Macy zfs_locked_range_t *proxy;
234eda14cbcSMatt Macy
235eda14cbcSMatt Macy if (lr->lr_proxy)
236eda14cbcSMatt Macy return (lr); /* already a proxy */
237eda14cbcSMatt Macy
238eda14cbcSMatt Macy ASSERT3U(lr->lr_count, ==, 1);
239eda14cbcSMatt Macy ASSERT(lr->lr_write_wanted == B_FALSE);
240eda14cbcSMatt Macy ASSERT(lr->lr_read_wanted == B_FALSE);
241eda14cbcSMatt Macy avl_remove(tree, lr);
242eda14cbcSMatt Macy lr->lr_count = 0;
243eda14cbcSMatt Macy
244eda14cbcSMatt Macy /* create a proxy range lock */
245eda14cbcSMatt Macy proxy = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
246eda14cbcSMatt Macy proxy->lr_offset = lr->lr_offset;
247eda14cbcSMatt Macy proxy->lr_length = lr->lr_length;
248eda14cbcSMatt Macy proxy->lr_count = 1;
249eda14cbcSMatt Macy proxy->lr_type = RL_READER;
250eda14cbcSMatt Macy proxy->lr_proxy = B_TRUE;
251eda14cbcSMatt Macy proxy->lr_write_wanted = B_FALSE;
252eda14cbcSMatt Macy proxy->lr_read_wanted = B_FALSE;
253eda14cbcSMatt Macy avl_add(tree, proxy);
254eda14cbcSMatt Macy
255eda14cbcSMatt Macy return (proxy);
256eda14cbcSMatt Macy }
257eda14cbcSMatt Macy
258eda14cbcSMatt Macy /*
259eda14cbcSMatt Macy * Split the range lock at the supplied offset
260eda14cbcSMatt Macy * returning the *front* proxy.
261eda14cbcSMatt Macy */
262eda14cbcSMatt Macy static zfs_locked_range_t *
zfs_rangelock_split(avl_tree_t * tree,zfs_locked_range_t * lr,uint64_t off)263eda14cbcSMatt Macy zfs_rangelock_split(avl_tree_t *tree, zfs_locked_range_t *lr, uint64_t off)
264eda14cbcSMatt Macy {
265eda14cbcSMatt Macy zfs_locked_range_t *rear;
266eda14cbcSMatt Macy
267eda14cbcSMatt Macy ASSERT3U(lr->lr_length, >, 1);
268eda14cbcSMatt Macy ASSERT3U(off, >, lr->lr_offset);
269eda14cbcSMatt Macy ASSERT3U(off, <, lr->lr_offset + lr->lr_length);
270eda14cbcSMatt Macy ASSERT(lr->lr_write_wanted == B_FALSE);
271eda14cbcSMatt Macy ASSERT(lr->lr_read_wanted == B_FALSE);
272eda14cbcSMatt Macy
273eda14cbcSMatt Macy /* create the rear proxy range lock */
274eda14cbcSMatt Macy rear = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
275eda14cbcSMatt Macy rear->lr_offset = off;
276eda14cbcSMatt Macy rear->lr_length = lr->lr_offset + lr->lr_length - off;
277eda14cbcSMatt Macy rear->lr_count = lr->lr_count;
278eda14cbcSMatt Macy rear->lr_type = RL_READER;
279eda14cbcSMatt Macy rear->lr_proxy = B_TRUE;
280eda14cbcSMatt Macy rear->lr_write_wanted = B_FALSE;
281eda14cbcSMatt Macy rear->lr_read_wanted = B_FALSE;
282eda14cbcSMatt Macy
283eda14cbcSMatt Macy zfs_locked_range_t *front = zfs_rangelock_proxify(tree, lr);
284eda14cbcSMatt Macy front->lr_length = off - lr->lr_offset;
285eda14cbcSMatt Macy
286eda14cbcSMatt Macy avl_insert_here(tree, rear, front, AVL_AFTER);
287eda14cbcSMatt Macy return (front);
288eda14cbcSMatt Macy }
289eda14cbcSMatt Macy
290eda14cbcSMatt Macy /*
291eda14cbcSMatt Macy * Create and add a new proxy range lock for the supplied range.
292eda14cbcSMatt Macy */
293eda14cbcSMatt Macy static void
zfs_rangelock_new_proxy(avl_tree_t * tree,uint64_t off,uint64_t len)294eda14cbcSMatt Macy zfs_rangelock_new_proxy(avl_tree_t *tree, uint64_t off, uint64_t len)
295eda14cbcSMatt Macy {
296eda14cbcSMatt Macy zfs_locked_range_t *lr;
297eda14cbcSMatt Macy
298eda14cbcSMatt Macy ASSERT(len != 0);
299eda14cbcSMatt Macy lr = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
300eda14cbcSMatt Macy lr->lr_offset = off;
301eda14cbcSMatt Macy lr->lr_length = len;
302eda14cbcSMatt Macy lr->lr_count = 1;
303eda14cbcSMatt Macy lr->lr_type = RL_READER;
304eda14cbcSMatt Macy lr->lr_proxy = B_TRUE;
305eda14cbcSMatt Macy lr->lr_write_wanted = B_FALSE;
306eda14cbcSMatt Macy lr->lr_read_wanted = B_FALSE;
307eda14cbcSMatt Macy avl_add(tree, lr);
308eda14cbcSMatt Macy }
309eda14cbcSMatt Macy
310eda14cbcSMatt Macy static void
zfs_rangelock_add_reader(avl_tree_t * tree,zfs_locked_range_t * new,zfs_locked_range_t * prev,avl_index_t where)311eda14cbcSMatt Macy zfs_rangelock_add_reader(avl_tree_t *tree, zfs_locked_range_t *new,
312eda14cbcSMatt Macy zfs_locked_range_t *prev, avl_index_t where)
313eda14cbcSMatt Macy {
314eda14cbcSMatt Macy zfs_locked_range_t *next;
315eda14cbcSMatt Macy uint64_t off = new->lr_offset;
316eda14cbcSMatt Macy uint64_t len = new->lr_length;
317eda14cbcSMatt Macy
318eda14cbcSMatt Macy /*
319eda14cbcSMatt Macy * prev arrives either:
320eda14cbcSMatt Macy * - pointing to an entry at the same offset
321eda14cbcSMatt Macy * - pointing to the entry with the closest previous offset whose
322eda14cbcSMatt Macy * range may overlap with the new range
323eda14cbcSMatt Macy * - null, if there were no ranges starting before the new one
324eda14cbcSMatt Macy */
325eda14cbcSMatt Macy if (prev != NULL) {
326eda14cbcSMatt Macy if (prev->lr_offset + prev->lr_length <= off) {
327eda14cbcSMatt Macy prev = NULL;
328eda14cbcSMatt Macy } else if (prev->lr_offset != off) {
329eda14cbcSMatt Macy /*
330eda14cbcSMatt Macy * convert to proxy if needed then
331eda14cbcSMatt Macy * split this entry and bump ref count
332eda14cbcSMatt Macy */
333eda14cbcSMatt Macy prev = zfs_rangelock_split(tree, prev, off);
334eda14cbcSMatt Macy prev = AVL_NEXT(tree, prev); /* move to rear range */
335eda14cbcSMatt Macy }
336eda14cbcSMatt Macy }
337eda14cbcSMatt Macy ASSERT((prev == NULL) || (prev->lr_offset == off));
338eda14cbcSMatt Macy
339eda14cbcSMatt Macy if (prev != NULL)
340eda14cbcSMatt Macy next = prev;
341eda14cbcSMatt Macy else
342eda14cbcSMatt Macy next = avl_nearest(tree, where, AVL_AFTER);
343eda14cbcSMatt Macy
344eda14cbcSMatt Macy if (next == NULL || off + len <= next->lr_offset) {
345eda14cbcSMatt Macy /* no overlaps, use the original new rl_t in the tree */
346eda14cbcSMatt Macy avl_insert(tree, new, where);
347eda14cbcSMatt Macy return;
348eda14cbcSMatt Macy }
349eda14cbcSMatt Macy
350eda14cbcSMatt Macy if (off < next->lr_offset) {
351eda14cbcSMatt Macy /* Add a proxy for initial range before the overlap */
352eda14cbcSMatt Macy zfs_rangelock_new_proxy(tree, off, next->lr_offset - off);
353eda14cbcSMatt Macy }
354eda14cbcSMatt Macy
355eda14cbcSMatt Macy new->lr_count = 0; /* will use proxies in tree */
356eda14cbcSMatt Macy /*
357eda14cbcSMatt Macy * We now search forward through the ranges, until we go past the end
358eda14cbcSMatt Macy * of the new range. For each entry we make it a proxy if it
359eda14cbcSMatt Macy * isn't already, then bump its reference count. If there's any
360eda14cbcSMatt Macy * gaps between the ranges then we create a new proxy range.
361eda14cbcSMatt Macy */
362eda14cbcSMatt Macy for (prev = NULL; next; prev = next, next = AVL_NEXT(tree, next)) {
363eda14cbcSMatt Macy if (off + len <= next->lr_offset)
364eda14cbcSMatt Macy break;
365eda14cbcSMatt Macy if (prev != NULL && prev->lr_offset + prev->lr_length <
366eda14cbcSMatt Macy next->lr_offset) {
367eda14cbcSMatt Macy /* there's a gap */
368eda14cbcSMatt Macy ASSERT3U(next->lr_offset, >,
369eda14cbcSMatt Macy prev->lr_offset + prev->lr_length);
370eda14cbcSMatt Macy zfs_rangelock_new_proxy(tree,
371eda14cbcSMatt Macy prev->lr_offset + prev->lr_length,
372eda14cbcSMatt Macy next->lr_offset -
373eda14cbcSMatt Macy (prev->lr_offset + prev->lr_length));
374eda14cbcSMatt Macy }
375eda14cbcSMatt Macy if (off + len == next->lr_offset + next->lr_length) {
376eda14cbcSMatt Macy /* exact overlap with end */
377eda14cbcSMatt Macy next = zfs_rangelock_proxify(tree, next);
378eda14cbcSMatt Macy next->lr_count++;
379eda14cbcSMatt Macy return;
380eda14cbcSMatt Macy }
381eda14cbcSMatt Macy if (off + len < next->lr_offset + next->lr_length) {
382eda14cbcSMatt Macy /* new range ends in the middle of this block */
383eda14cbcSMatt Macy next = zfs_rangelock_split(tree, next, off + len);
384eda14cbcSMatt Macy next->lr_count++;
385eda14cbcSMatt Macy return;
386eda14cbcSMatt Macy }
387eda14cbcSMatt Macy ASSERT3U(off + len, >, next->lr_offset + next->lr_length);
388eda14cbcSMatt Macy next = zfs_rangelock_proxify(tree, next);
389eda14cbcSMatt Macy next->lr_count++;
390eda14cbcSMatt Macy }
391eda14cbcSMatt Macy
392eda14cbcSMatt Macy /* Add the remaining end range. */
393eda14cbcSMatt Macy zfs_rangelock_new_proxy(tree, prev->lr_offset + prev->lr_length,
394eda14cbcSMatt Macy (off + len) - (prev->lr_offset + prev->lr_length));
395eda14cbcSMatt Macy }
396eda14cbcSMatt Macy
397eda14cbcSMatt Macy /*
398eda14cbcSMatt Macy * Check if a reader lock can be grabbed. If not, fail immediately or sleep and
399eda14cbcSMatt Macy * recheck until available, depending on the value of the "nonblock" parameter.
400eda14cbcSMatt Macy */
401eda14cbcSMatt Macy static boolean_t
zfs_rangelock_enter_reader(zfs_rangelock_t * rl,zfs_locked_range_t * new,boolean_t nonblock)402eda14cbcSMatt Macy zfs_rangelock_enter_reader(zfs_rangelock_t *rl, zfs_locked_range_t *new,
403eda14cbcSMatt Macy boolean_t nonblock)
404eda14cbcSMatt Macy {
405eda14cbcSMatt Macy avl_tree_t *tree = &rl->rl_tree;
406eda14cbcSMatt Macy zfs_locked_range_t *prev, *next;
407eda14cbcSMatt Macy avl_index_t where;
408eda14cbcSMatt Macy uint64_t off = new->lr_offset;
409eda14cbcSMatt Macy uint64_t len = new->lr_length;
410eda14cbcSMatt Macy
411eda14cbcSMatt Macy /*
412eda14cbcSMatt Macy * Look for any writer locks in the range.
413eda14cbcSMatt Macy */
414eda14cbcSMatt Macy retry:
415eda14cbcSMatt Macy prev = avl_find(tree, new, &where);
416eda14cbcSMatt Macy if (prev == NULL)
417eda14cbcSMatt Macy prev = avl_nearest(tree, where, AVL_BEFORE);
418eda14cbcSMatt Macy
419eda14cbcSMatt Macy /*
420eda14cbcSMatt Macy * Check the previous range for a writer lock overlap.
421eda14cbcSMatt Macy */
422eda14cbcSMatt Macy if (prev && (off < prev->lr_offset + prev->lr_length)) {
423eda14cbcSMatt Macy if ((prev->lr_type == RL_WRITER) || (prev->lr_write_wanted)) {
424eda14cbcSMatt Macy if (nonblock)
425eda14cbcSMatt Macy return (B_FALSE);
426eda14cbcSMatt Macy if (!prev->lr_read_wanted) {
427eda14cbcSMatt Macy cv_init(&prev->lr_read_cv,
428eda14cbcSMatt Macy NULL, CV_DEFAULT, NULL);
429eda14cbcSMatt Macy prev->lr_read_wanted = B_TRUE;
430eda14cbcSMatt Macy }
431eda14cbcSMatt Macy cv_wait(&prev->lr_read_cv, &rl->rl_lock);
432eda14cbcSMatt Macy goto retry;
433eda14cbcSMatt Macy }
434eda14cbcSMatt Macy if (off + len < prev->lr_offset + prev->lr_length)
435eda14cbcSMatt Macy goto got_lock;
436eda14cbcSMatt Macy }
437eda14cbcSMatt Macy
438eda14cbcSMatt Macy /*
439eda14cbcSMatt Macy * Search through the following ranges to see if there's
440eda14cbcSMatt Macy * write lock any overlap.
441eda14cbcSMatt Macy */
442eda14cbcSMatt Macy if (prev != NULL)
443eda14cbcSMatt Macy next = AVL_NEXT(tree, prev);
444eda14cbcSMatt Macy else
445eda14cbcSMatt Macy next = avl_nearest(tree, where, AVL_AFTER);
446eda14cbcSMatt Macy for (; next != NULL; next = AVL_NEXT(tree, next)) {
447eda14cbcSMatt Macy if (off + len <= next->lr_offset)
448eda14cbcSMatt Macy goto got_lock;
449eda14cbcSMatt Macy if ((next->lr_type == RL_WRITER) || (next->lr_write_wanted)) {
450eda14cbcSMatt Macy if (nonblock)
451eda14cbcSMatt Macy return (B_FALSE);
452eda14cbcSMatt Macy if (!next->lr_read_wanted) {
453eda14cbcSMatt Macy cv_init(&next->lr_read_cv,
454eda14cbcSMatt Macy NULL, CV_DEFAULT, NULL);
455eda14cbcSMatt Macy next->lr_read_wanted = B_TRUE;
456eda14cbcSMatt Macy }
457eda14cbcSMatt Macy cv_wait(&next->lr_read_cv, &rl->rl_lock);
458eda14cbcSMatt Macy goto retry;
459eda14cbcSMatt Macy }
460eda14cbcSMatt Macy if (off + len <= next->lr_offset + next->lr_length)
461eda14cbcSMatt Macy goto got_lock;
462eda14cbcSMatt Macy }
463eda14cbcSMatt Macy
464eda14cbcSMatt Macy got_lock:
465eda14cbcSMatt Macy /*
466eda14cbcSMatt Macy * Add the read lock, which may involve splitting existing
467eda14cbcSMatt Macy * locks and bumping ref counts (r_count).
468eda14cbcSMatt Macy */
469eda14cbcSMatt Macy zfs_rangelock_add_reader(tree, new, prev, where);
470eda14cbcSMatt Macy return (B_TRUE);
471eda14cbcSMatt Macy }
472eda14cbcSMatt Macy
473eda14cbcSMatt Macy /*
474eda14cbcSMatt Macy * Lock a range (offset, length) as either shared (RL_READER) or exclusive
475eda14cbcSMatt Macy * (RL_WRITER or RL_APPEND). If RL_APPEND is specified, rl_cb() will convert
476eda14cbcSMatt Macy * it to a RL_WRITER lock (with the offset at the end of the file). Returns
477eda14cbcSMatt Macy * the range lock structure for later unlocking (or reduce range if the
478eda14cbcSMatt Macy * entire file is locked as RL_WRITER), or NULL if nonblock is true and the
479eda14cbcSMatt Macy * lock could not be acquired immediately.
480eda14cbcSMatt Macy */
481eda14cbcSMatt Macy 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)482eda14cbcSMatt Macy zfs_rangelock_enter_impl(zfs_rangelock_t *rl, uint64_t off, uint64_t len,
483eda14cbcSMatt Macy zfs_rangelock_type_t type, boolean_t nonblock)
484eda14cbcSMatt Macy {
485eda14cbcSMatt Macy zfs_locked_range_t *new;
486eda14cbcSMatt Macy
487eda14cbcSMatt Macy ASSERT(type == RL_READER || type == RL_WRITER || type == RL_APPEND);
488eda14cbcSMatt Macy
489eda14cbcSMatt Macy new = kmem_alloc(sizeof (zfs_locked_range_t), KM_SLEEP);
490eda14cbcSMatt Macy new->lr_rangelock = rl;
491eda14cbcSMatt Macy new->lr_offset = off;
492eda14cbcSMatt Macy if (len + off < off) /* overflow */
493eda14cbcSMatt Macy len = UINT64_MAX - off;
494eda14cbcSMatt Macy new->lr_length = len;
495eda14cbcSMatt Macy new->lr_count = 1; /* assume it's going to be in the tree */
496eda14cbcSMatt Macy new->lr_type = type;
497eda14cbcSMatt Macy new->lr_proxy = B_FALSE;
498eda14cbcSMatt Macy new->lr_write_wanted = B_FALSE;
499eda14cbcSMatt Macy new->lr_read_wanted = B_FALSE;
500eda14cbcSMatt Macy
501eda14cbcSMatt Macy mutex_enter(&rl->rl_lock);
502eda14cbcSMatt Macy if (type == RL_READER) {
503eda14cbcSMatt Macy /*
504eda14cbcSMatt Macy * First check for the usual case of no locks
505eda14cbcSMatt Macy */
506eda14cbcSMatt Macy if (avl_numnodes(&rl->rl_tree) == 0) {
507eda14cbcSMatt Macy avl_add(&rl->rl_tree, new);
508eda14cbcSMatt Macy } else if (!zfs_rangelock_enter_reader(rl, new, nonblock)) {
509eda14cbcSMatt Macy kmem_free(new, sizeof (*new));
510eda14cbcSMatt Macy new = NULL;
511eda14cbcSMatt Macy }
512eda14cbcSMatt Macy } else if (!zfs_rangelock_enter_writer(rl, new, nonblock)) {
513eda14cbcSMatt Macy kmem_free(new, sizeof (*new));
514eda14cbcSMatt Macy new = NULL;
515eda14cbcSMatt Macy }
516eda14cbcSMatt Macy mutex_exit(&rl->rl_lock);
517eda14cbcSMatt Macy return (new);
518eda14cbcSMatt Macy }
519eda14cbcSMatt Macy
520eda14cbcSMatt Macy zfs_locked_range_t *
zfs_rangelock_enter(zfs_rangelock_t * rl,uint64_t off,uint64_t len,zfs_rangelock_type_t type)521eda14cbcSMatt Macy zfs_rangelock_enter(zfs_rangelock_t *rl, uint64_t off, uint64_t len,
522eda14cbcSMatt Macy zfs_rangelock_type_t type)
523eda14cbcSMatt Macy {
524eda14cbcSMatt Macy return (zfs_rangelock_enter_impl(rl, off, len, type, B_FALSE));
525eda14cbcSMatt Macy }
526eda14cbcSMatt Macy
527eda14cbcSMatt Macy zfs_locked_range_t *
zfs_rangelock_tryenter(zfs_rangelock_t * rl,uint64_t off,uint64_t len,zfs_rangelock_type_t type)528eda14cbcSMatt Macy zfs_rangelock_tryenter(zfs_rangelock_t *rl, uint64_t off, uint64_t len,
529eda14cbcSMatt Macy zfs_rangelock_type_t type)
530eda14cbcSMatt Macy {
531eda14cbcSMatt Macy return (zfs_rangelock_enter_impl(rl, off, len, type, B_TRUE));
532eda14cbcSMatt Macy }
533eda14cbcSMatt Macy
534eda14cbcSMatt Macy /*
535eda14cbcSMatt Macy * Safely free the zfs_locked_range_t.
536eda14cbcSMatt Macy */
537eda14cbcSMatt Macy static void
zfs_rangelock_free(zfs_locked_range_t * lr)538eda14cbcSMatt Macy zfs_rangelock_free(zfs_locked_range_t *lr)
539eda14cbcSMatt Macy {
540eda14cbcSMatt Macy if (lr->lr_write_wanted)
541eda14cbcSMatt Macy cv_destroy(&lr->lr_write_cv);
542eda14cbcSMatt Macy
543eda14cbcSMatt Macy if (lr->lr_read_wanted)
544eda14cbcSMatt Macy cv_destroy(&lr->lr_read_cv);
545eda14cbcSMatt Macy
546eda14cbcSMatt Macy kmem_free(lr, sizeof (zfs_locked_range_t));
547eda14cbcSMatt Macy }
548eda14cbcSMatt Macy
549eda14cbcSMatt Macy /*
550eda14cbcSMatt Macy * Unlock a reader lock
551eda14cbcSMatt Macy */
552eda14cbcSMatt Macy static void
zfs_rangelock_exit_reader(zfs_rangelock_t * rl,zfs_locked_range_t * remove,list_t * free_list)553eda14cbcSMatt Macy zfs_rangelock_exit_reader(zfs_rangelock_t *rl, zfs_locked_range_t *remove,
554eda14cbcSMatt Macy list_t *free_list)
555eda14cbcSMatt Macy {
556eda14cbcSMatt Macy avl_tree_t *tree = &rl->rl_tree;
557eda14cbcSMatt Macy uint64_t len;
558eda14cbcSMatt Macy
559eda14cbcSMatt Macy /*
560eda14cbcSMatt Macy * The common case is when the remove entry is in the tree
561eda14cbcSMatt Macy * (cnt == 1) meaning there's been no other reader locks overlapping
562eda14cbcSMatt Macy * with this one. Otherwise the remove entry will have been
563eda14cbcSMatt Macy * removed from the tree and replaced by proxies (one or
564eda14cbcSMatt Macy * more ranges mapping to the entire range).
565eda14cbcSMatt Macy */
566eda14cbcSMatt Macy if (remove->lr_count == 1) {
567eda14cbcSMatt Macy avl_remove(tree, remove);
568eda14cbcSMatt Macy if (remove->lr_write_wanted)
569eda14cbcSMatt Macy cv_broadcast(&remove->lr_write_cv);
570eda14cbcSMatt Macy if (remove->lr_read_wanted)
571eda14cbcSMatt Macy cv_broadcast(&remove->lr_read_cv);
572eda14cbcSMatt Macy list_insert_tail(free_list, remove);
573eda14cbcSMatt Macy } else {
574eda14cbcSMatt Macy ASSERT0(remove->lr_count);
575eda14cbcSMatt Macy ASSERT0(remove->lr_write_wanted);
576eda14cbcSMatt Macy ASSERT0(remove->lr_read_wanted);
577eda14cbcSMatt Macy /*
578eda14cbcSMatt Macy * Find start proxy representing this reader lock,
579eda14cbcSMatt Macy * then decrement ref count on all proxies
580eda14cbcSMatt Macy * that make up this range, freeing them as needed.
581eda14cbcSMatt Macy */
582eda14cbcSMatt Macy zfs_locked_range_t *lr = avl_find(tree, remove, NULL);
583eda14cbcSMatt Macy ASSERT3P(lr, !=, NULL);
584eda14cbcSMatt Macy ASSERT3U(lr->lr_count, !=, 0);
585eda14cbcSMatt Macy ASSERT3U(lr->lr_type, ==, RL_READER);
586eda14cbcSMatt Macy zfs_locked_range_t *next = NULL;
587eda14cbcSMatt Macy for (len = remove->lr_length; len != 0; lr = next) {
588eda14cbcSMatt Macy len -= lr->lr_length;
589eda14cbcSMatt Macy if (len != 0) {
590eda14cbcSMatt Macy next = AVL_NEXT(tree, lr);
591eda14cbcSMatt Macy ASSERT3P(next, !=, NULL);
592eda14cbcSMatt Macy ASSERT3U(lr->lr_offset + lr->lr_length, ==,
593eda14cbcSMatt Macy next->lr_offset);
594eda14cbcSMatt Macy ASSERT3U(next->lr_count, !=, 0);
595eda14cbcSMatt Macy ASSERT3U(next->lr_type, ==, RL_READER);
596eda14cbcSMatt Macy }
597eda14cbcSMatt Macy lr->lr_count--;
598eda14cbcSMatt Macy if (lr->lr_count == 0) {
599eda14cbcSMatt Macy avl_remove(tree, lr);
600eda14cbcSMatt Macy if (lr->lr_write_wanted)
601eda14cbcSMatt Macy cv_broadcast(&lr->lr_write_cv);
602eda14cbcSMatt Macy if (lr->lr_read_wanted)
603eda14cbcSMatt Macy cv_broadcast(&lr->lr_read_cv);
604eda14cbcSMatt Macy list_insert_tail(free_list, lr);
605eda14cbcSMatt Macy }
606eda14cbcSMatt Macy }
607eda14cbcSMatt Macy kmem_free(remove, sizeof (zfs_locked_range_t));
608eda14cbcSMatt Macy }
609eda14cbcSMatt Macy }
610eda14cbcSMatt Macy
611eda14cbcSMatt Macy /*
612eda14cbcSMatt Macy * Unlock range and destroy range lock structure.
613eda14cbcSMatt Macy */
614eda14cbcSMatt Macy void
zfs_rangelock_exit(zfs_locked_range_t * lr)615eda14cbcSMatt Macy zfs_rangelock_exit(zfs_locked_range_t *lr)
616eda14cbcSMatt Macy {
617eda14cbcSMatt Macy zfs_rangelock_t *rl = lr->lr_rangelock;
618eda14cbcSMatt Macy list_t free_list;
619eda14cbcSMatt Macy zfs_locked_range_t *free_lr;
620eda14cbcSMatt Macy
621eda14cbcSMatt Macy ASSERT(lr->lr_type == RL_WRITER || lr->lr_type == RL_READER);
622eda14cbcSMatt Macy ASSERT(lr->lr_count == 1 || lr->lr_count == 0);
623eda14cbcSMatt Macy ASSERT(!lr->lr_proxy);
624eda14cbcSMatt Macy
625eda14cbcSMatt Macy /*
626eda14cbcSMatt Macy * The free list is used to defer the cv_destroy() and
627eda14cbcSMatt Macy * subsequent kmem_free until after the mutex is dropped.
628eda14cbcSMatt Macy */
629eda14cbcSMatt Macy list_create(&free_list, sizeof (zfs_locked_range_t),
630eda14cbcSMatt Macy offsetof(zfs_locked_range_t, lr_node));
631eda14cbcSMatt Macy
632eda14cbcSMatt Macy mutex_enter(&rl->rl_lock);
633eda14cbcSMatt Macy if (lr->lr_type == RL_WRITER) {
634eda14cbcSMatt Macy /* writer locks can't be shared or split */
635eda14cbcSMatt Macy avl_remove(&rl->rl_tree, lr);
636eda14cbcSMatt Macy if (lr->lr_write_wanted)
637eda14cbcSMatt Macy cv_broadcast(&lr->lr_write_cv);
638eda14cbcSMatt Macy if (lr->lr_read_wanted)
639eda14cbcSMatt Macy cv_broadcast(&lr->lr_read_cv);
640eda14cbcSMatt Macy list_insert_tail(&free_list, lr);
641eda14cbcSMatt Macy } else {
642eda14cbcSMatt Macy /*
643eda14cbcSMatt Macy * lock may be shared, let rangelock_exit_reader()
644eda14cbcSMatt Macy * release the lock and free the zfs_locked_range_t.
645eda14cbcSMatt Macy */
646eda14cbcSMatt Macy zfs_rangelock_exit_reader(rl, lr, &free_list);
647eda14cbcSMatt Macy }
648eda14cbcSMatt Macy mutex_exit(&rl->rl_lock);
649eda14cbcSMatt Macy
650eda14cbcSMatt Macy while ((free_lr = list_remove_head(&free_list)) != NULL)
651eda14cbcSMatt Macy zfs_rangelock_free(free_lr);
652eda14cbcSMatt Macy
653eda14cbcSMatt Macy list_destroy(&free_list);
654eda14cbcSMatt Macy }
655eda14cbcSMatt Macy
656eda14cbcSMatt Macy /*
657eda14cbcSMatt Macy * Reduce range locked as RL_WRITER from whole file to specified range.
658eda14cbcSMatt Macy * Asserts the whole file is exclusively locked and so there's only one
659eda14cbcSMatt Macy * entry in the tree.
660eda14cbcSMatt Macy */
661eda14cbcSMatt Macy void
zfs_rangelock_reduce(zfs_locked_range_t * lr,uint64_t off,uint64_t len)662eda14cbcSMatt Macy zfs_rangelock_reduce(zfs_locked_range_t *lr, uint64_t off, uint64_t len)
663eda14cbcSMatt Macy {
664eda14cbcSMatt Macy zfs_rangelock_t *rl = lr->lr_rangelock;
665eda14cbcSMatt Macy
666eda14cbcSMatt Macy /* Ensure there are no other locks */
667eda14cbcSMatt Macy ASSERT3U(avl_numnodes(&rl->rl_tree), ==, 1);
668eda14cbcSMatt Macy ASSERT3U(lr->lr_offset, ==, 0);
669eda14cbcSMatt Macy ASSERT3U(lr->lr_type, ==, RL_WRITER);
670eda14cbcSMatt Macy ASSERT(!lr->lr_proxy);
671eda14cbcSMatt Macy ASSERT3U(lr->lr_length, ==, UINT64_MAX);
672eda14cbcSMatt Macy ASSERT3U(lr->lr_count, ==, 1);
673eda14cbcSMatt Macy
674eda14cbcSMatt Macy mutex_enter(&rl->rl_lock);
675eda14cbcSMatt Macy lr->lr_offset = off;
676eda14cbcSMatt Macy lr->lr_length = len;
677eda14cbcSMatt Macy mutex_exit(&rl->rl_lock);
678eda14cbcSMatt Macy if (lr->lr_write_wanted)
679eda14cbcSMatt Macy cv_broadcast(&lr->lr_write_cv);
680eda14cbcSMatt Macy if (lr->lr_read_wanted)
681eda14cbcSMatt Macy cv_broadcast(&lr->lr_read_cv);
682eda14cbcSMatt Macy }
683eda14cbcSMatt Macy
684eda14cbcSMatt Macy #if defined(_KERNEL)
685eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_rangelock_init);
686eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_rangelock_fini);
687eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_rangelock_enter);
688eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_rangelock_tryenter);
689eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_rangelock_exit);
690eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_rangelock_reduce);
691eda14cbcSMatt Macy #endif
692