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 https://opensource.org/licenses/CDDL-1.0.
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
33extern "C" {
34#endif
35
36#include <sys/avl.h>
37
38typedef enum {
39	RL_READER,
40	RL_WRITER,
41	RL_APPEND
42} zfs_rangelock_type_t;
43
44struct zfs_locked_range;
45
46typedef void (zfs_rangelock_cb_t)(struct zfs_locked_range *, void *);
47
48typedef struct zfs_rangelock {
49	avl_tree_t rl_tree; /* contains locked_range_t */
50	kmutex_t rl_lock;
51	zfs_rangelock_cb_t *rl_cb;
52	void *rl_arg;
53} zfs_rangelock_t;
54
55typedef struct zfs_locked_range {
56	zfs_rangelock_t *lr_rangelock; /* rangelock that this lock applies to */
57	avl_node_t lr_node;	/* avl node link */
58	uint64_t lr_offset;	/* file range offset */
59	uint64_t lr_length;	/* file range length */
60	uint_t lr_count;	/* range reference count in tree */
61	zfs_rangelock_type_t lr_type; /* range type */
62	kcondvar_t lr_write_cv;	/* cv for waiting writers */
63	kcondvar_t lr_read_cv;	/* cv for waiting readers */
64	uint8_t lr_proxy;	/* acting for original range */
65	uint8_t lr_write_wanted; /* writer wants to lock this range */
66	uint8_t lr_read_wanted;	/* reader wants to lock this range */
67} zfs_locked_range_t;
68
69void zfs_rangelock_init(zfs_rangelock_t *, zfs_rangelock_cb_t *, void *);
70void zfs_rangelock_fini(zfs_rangelock_t *);
71
72zfs_locked_range_t *zfs_rangelock_enter(zfs_rangelock_t *,
73    uint64_t, uint64_t, zfs_rangelock_type_t);
74zfs_locked_range_t *zfs_rangelock_tryenter(zfs_rangelock_t *,
75    uint64_t, uint64_t, zfs_rangelock_type_t);
76void zfs_rangelock_exit(zfs_locked_range_t *);
77void zfs_rangelock_reduce(zfs_locked_range_t *, uint64_t, uint64_t);
78
79#ifdef	__cplusplus
80}
81#endif
82
83#endif	/* _SYS_FS_ZFS_RLOCK_H */
84