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