1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2018 by Delphix. All rights reserved.
27  */
28 
29 #ifndef	_SYS_FS_ZFS_RLOCK_H
30 #define	_SYS_FS_ZFS_RLOCK_H
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 typedef enum {
37 	RL_READER,
38 	RL_WRITER,
39 	RL_APPEND
40 } rangelock_type_t;
41 
42 struct locked_range;
43 
44 typedef void (rangelock_cb_t)(struct locked_range *, void *);
45 
46 typedef struct rangelock {
47 	avl_tree_t rl_tree; /* contains locked_range_t */
48 	kmutex_t rl_lock;
49 	rangelock_cb_t *rl_cb;
50 	void *rl_arg;
51 } rangelock_t;
52 
53 typedef struct locked_range {
54 	rangelock_t *lr_rangelock; /* rangelock that this lock applies to */
55 	avl_node_t lr_node;	/* avl node link */
56 	uint64_t lr_offset;	/* file range offset */
57 	uint64_t lr_length;	/* file range length */
58 	uint_t lr_count;	/* range reference count in tree */
59 	rangelock_type_t lr_type; /* range type */
60 	kcondvar_t lr_write_cv;	/* cv for waiting writers */
61 	kcondvar_t lr_read_cv;	/* cv for waiting readers */
62 	uint8_t lr_proxy;	/* acting for original range */
63 	uint8_t lr_write_wanted; /* writer wants to lock this range */
64 	uint8_t lr_read_wanted;	/* reader wants to lock this range */
65 } locked_range_t;
66 
67 void rangelock_init(rangelock_t *, rangelock_cb_t *, void *);
68 void rangelock_fini(rangelock_t *);
69 
70 locked_range_t *rangelock_enter(rangelock_t *,
71     uint64_t, uint64_t, rangelock_type_t);
72 void rangelock_exit(locked_range_t *);
73 void rangelock_reduce(locked_range_t *, uint64_t, uint64_t);
74 
75 #ifdef	__cplusplus
76 }
77 #endif
78 
79 #endif	/* _SYS_FS_ZFS_RLOCK_H */
80