xref: /freebsd/sys/contrib/openzfs/include/sys/zvol_impl.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2024, Klara, Inc.
24  */
25 
26 #ifndef	_SYS_ZVOL_IMPL_H
27 #define	_SYS_ZVOL_IMPL_H
28 
29 #include <sys/zfs_context.h>
30 
31 #define	ZVOL_RDONLY	(1<<0)	/* zvol is readonly (writes rejected) */
32 #define	ZVOL_WRITTEN_TO	(1<<1)	/* zvol has been written to (needs flush) */
33 #define	ZVOL_EXCL	(1<<2)	/* zvol has O_EXCL client right now */
34 #define	ZVOL_REMOVING	(1<<3)	/* zvol waiting to remove minor */
35 
36 /*
37  * The in-core state of each volume.
38  */
39 typedef struct zvol_state {
40 	char			zv_name[MAXNAMELEN];	/* name */
41 	uint64_t		zv_volsize;		/* advertised space */
42 	uint64_t		zv_volblocksize;	/* volume block size */
43 	objset_t		*zv_objset;	/* objset handle */
44 	uint32_t		zv_flags;	/* ZVOL_* flags */
45 	uint32_t		zv_open_count;	/* open counts */
46 	uint32_t		zv_changed;	/* disk changed */
47 	uint32_t		zv_volmode;	/* volmode */
48 	zilog_t			*zv_zilog;	/* ZIL handle */
49 	zfs_rangelock_t		zv_rangelock;	/* for range locking */
50 	dnode_t			*zv_dn;		/* dnode hold */
51 	dataset_kstats_t	zv_kstat;	/* zvol kstats */
52 	list_node_t		zv_next;	/* next zvol_state_t linkage */
53 	uint64_t		zv_hash;	/* name hash */
54 	struct hlist_node	zv_hlink;	/* hash link */
55 	kmutex_t		zv_state_lock;	/* protects zvol_state_t */
56 	atomic_t		zv_suspend_ref;	/* refcount for suspend */
57 	krwlock_t		zv_suspend_lock;	/* suspend lock */
58 	kcondvar_t		zv_removing_cv;	/* ready to remove minor */
59 	struct zvol_state_os	*zv_zso;	/* private platform state */
60 	boolean_t		zv_threading;	/* volthreading property */
61 } zvol_state_t;
62 
63 
64 extern krwlock_t zvol_state_lock;
65 #define	ZVOL_HT_SIZE	1024
66 extern struct hlist_head *zvol_htable;
67 #define	ZVOL_HT_HEAD(hash)	(&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
68 extern zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE];
69 
70 extern unsigned int zvol_volmode;
71 extern unsigned int zvol_inhibit_dev;
72 
73 /*
74  * platform independent functions exported to platform code
75  */
76 zvol_state_t *zvol_find_by_name_hash(const char *name,
77     uint64_t hash, int mode);
78 int zvol_first_open(zvol_state_t *zv, boolean_t readonly);
79 uint64_t zvol_name_hash(const char *name);
80 void zvol_remove_minors_impl(const char *name);
81 void zvol_last_close(zvol_state_t *zv);
82 void zvol_insert(zvol_state_t *zv);
83 void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off,
84     uint64_t len);
85 void zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
86     uint64_t size, boolean_t commit);
87 int zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf,
88     struct lwb *lwb, zio_t *zio);
89 int zvol_init_impl(void);
90 void zvol_fini_impl(void);
91 void zvol_wait_close(zvol_state_t *zv);
92 int zvol_clone_range(zvol_state_handle_t *, uint64_t,
93     zvol_state_handle_t *, uint64_t, uint64_t);
94 void zvol_log_clone_range(zilog_t *zilog, dmu_tx_t *tx, int txtype,
95     uint64_t off, uint64_t len, uint64_t blksz, const blkptr_t *bps,
96     size_t nbps);
97 
98 /*
99  * platform dependent functions exported to platform independent code
100  */
101 void zvol_os_free(zvol_state_t *zv);
102 void zvol_os_rename_minor(zvol_state_t *zv, const char *newname);
103 int zvol_os_create_minor(const char *name);
104 int zvol_os_update_volsize(zvol_state_t *zv, uint64_t volsize);
105 boolean_t zvol_os_is_zvol(const char *path);
106 void zvol_os_clear_private(zvol_state_t *zv);
107 void zvol_os_set_disk_ro(zvol_state_t *zv, int flags);
108 void zvol_os_set_capacity(zvol_state_t *zv, uint64_t capacity);
109 
110 #endif
111