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 2006 Sun Microsystems, Inc.  All rights reserved.
23eda14cbcSMatt Macy  * Use is subject to license terms.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy /*
26eda14cbcSMatt Macy  * Copyright (c) 2018 by Delphix. All rights reserved.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #ifndef	_SYS_FS_ZFS_RLOCK_H
30eda14cbcSMatt Macy #define	_SYS_FS_ZFS_RLOCK_H
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy #ifdef	__cplusplus
33eda14cbcSMatt Macy extern "C" {
34eda14cbcSMatt Macy #endif
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy #include <sys/avl.h>
37eda14cbcSMatt Macy 
38eda14cbcSMatt Macy typedef enum {
39eda14cbcSMatt Macy 	RL_READER,
40eda14cbcSMatt Macy 	RL_WRITER,
41eda14cbcSMatt Macy 	RL_APPEND
42eda14cbcSMatt Macy } zfs_rangelock_type_t;
43eda14cbcSMatt Macy 
44eda14cbcSMatt Macy struct zfs_locked_range;
45eda14cbcSMatt Macy 
46eda14cbcSMatt Macy typedef void (zfs_rangelock_cb_t)(struct zfs_locked_range *, void *);
47eda14cbcSMatt Macy 
48eda14cbcSMatt Macy typedef struct zfs_rangelock {
49eda14cbcSMatt Macy 	avl_tree_t rl_tree; /* contains locked_range_t */
50eda14cbcSMatt Macy 	kmutex_t rl_lock;
51eda14cbcSMatt Macy 	zfs_rangelock_cb_t *rl_cb;
52eda14cbcSMatt Macy 	void *rl_arg;
53eda14cbcSMatt Macy } zfs_rangelock_t;
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy typedef struct zfs_locked_range {
56eda14cbcSMatt Macy 	zfs_rangelock_t *lr_rangelock; /* rangelock that this lock applies to */
57eda14cbcSMatt Macy 	avl_node_t lr_node;	/* avl node link */
58eda14cbcSMatt Macy 	uint64_t lr_offset;	/* file range offset */
59eda14cbcSMatt Macy 	uint64_t lr_length;	/* file range length */
60eda14cbcSMatt Macy 	uint_t lr_count;	/* range reference count in tree */
61eda14cbcSMatt Macy 	zfs_rangelock_type_t lr_type; /* range type */
62eda14cbcSMatt Macy 	kcondvar_t lr_write_cv;	/* cv for waiting writers */
63eda14cbcSMatt Macy 	kcondvar_t lr_read_cv;	/* cv for waiting readers */
64eda14cbcSMatt Macy 	uint8_t lr_proxy;	/* acting for original range */
65eda14cbcSMatt Macy 	uint8_t lr_write_wanted; /* writer wants to lock this range */
66eda14cbcSMatt Macy 	uint8_t lr_read_wanted;	/* reader wants to lock this range */
67eda14cbcSMatt Macy } zfs_locked_range_t;
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy void zfs_rangelock_init(zfs_rangelock_t *, zfs_rangelock_cb_t *, void *);
70eda14cbcSMatt Macy void zfs_rangelock_fini(zfs_rangelock_t *);
71eda14cbcSMatt Macy 
72eda14cbcSMatt Macy zfs_locked_range_t *zfs_rangelock_enter(zfs_rangelock_t *,
73eda14cbcSMatt Macy     uint64_t, uint64_t, zfs_rangelock_type_t);
74eda14cbcSMatt Macy zfs_locked_range_t *zfs_rangelock_tryenter(zfs_rangelock_t *,
75eda14cbcSMatt Macy     uint64_t, uint64_t, zfs_rangelock_type_t);
76eda14cbcSMatt Macy void zfs_rangelock_exit(zfs_locked_range_t *);
77eda14cbcSMatt Macy void zfs_rangelock_reduce(zfs_locked_range_t *, uint64_t, uint64_t);
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy #ifdef	__cplusplus
80eda14cbcSMatt Macy }
81eda14cbcSMatt Macy #endif
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy #endif	/* _SYS_FS_ZFS_RLOCK_H */
84