xref: /freebsd/sys/contrib/openzfs/include/sys/zvol_impl.h (revision 53a2e2635ab2d17bed1de7b4e0d782dd23ceb6ea)
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, 2025, 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 	list_node_t		zv_remove_node;	/* node on removal list */
60 	struct zvol_state_os	*zv_zso;	/* private platform state */
61 	boolean_t		zv_threading;	/* volthreading property */
62 } zvol_state_t;
63 
64 /*
65  * zvol taskqs
66  */
67 typedef struct zv_taskq {
68 	uint_t tqs_cnt;
69 	taskq_t **tqs_taskq;
70 } zv_taskq_t;
71 
72 typedef struct zv_request_stack {
73 	zvol_state_t	*zv;
74 	struct bio	*bio;
75 #ifdef __linux__
76 	struct request	*rq;
77 #endif
78 } zv_request_t;
79 
80 typedef struct zv_request_task {
81 	zv_request_t	zvr;
82 	taskq_ent_t	ent;
83 } zv_request_task_t;
84 
85 /*
86  * Switch taskq at multiple of 512 MB offset. This can be set to a lower value
87  * to utilize more threads for small files but may affect prefetch hits.
88  */
89 #define	ZVOL_TASKQ_OFFSET_SHIFT 29
90 
91 extern krwlock_t zvol_state_lock;
92 #define	ZVOL_HT_SIZE	1024
93 extern struct hlist_head *zvol_htable;
94 #define	ZVOL_HT_HEAD(hash)	(&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
95 extern zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE];
96 
97 extern unsigned int zvol_inhibit_dev;
98 extern unsigned int zvol_prefetch_bytes;
99 extern unsigned int zvol_volmode;
100 extern unsigned int zvol_threads;
101 extern unsigned int zvol_num_taskqs;
102 extern unsigned int zvol_request_sync;
103 extern zv_taskq_t zvol_taskqs;
104 
105 /*
106  * platform independent functions exported to platform code
107  */
108 zvol_state_t *zvol_find_by_name_hash(const char *name,
109     uint64_t hash, int mode);
110 int zvol_first_open(zvol_state_t *zv, boolean_t readonly);
111 uint64_t zvol_name_hash(const char *name);
112 void zvol_last_close(zvol_state_t *zv);
113 void zvol_insert(zvol_state_t *zv);
114 void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off,
115     uint64_t len);
116 void zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
117     uint64_t size, boolean_t commit);
118 int zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf,
119     struct lwb *lwb, zio_t *zio);
120 int zvol_init_impl(void);
121 void zvol_fini_impl(void);
122 void zvol_wait_close(zvol_state_t *zv);
123 int zvol_clone_range(zvol_state_handle_t *, uint64_t,
124     zvol_state_handle_t *, uint64_t, uint64_t);
125 void zvol_log_clone_range(zilog_t *zilog, dmu_tx_t *tx, int txtype,
126     uint64_t off, uint64_t len, uint64_t blksz, const blkptr_t *bps,
127     size_t nbps);
128 zv_request_task_t *zv_request_task_create(zv_request_t zvr);
129 void zv_request_task_free(zv_request_task_t *task);
130 
131 /*
132  * platform dependent functions exported to platform independent code
133  */
134 void zvol_os_free(zvol_state_t *zv);
135 int zvol_os_rename_minor(zvol_state_t *zv, const char *newname);
136 int zvol_os_create_minor(const char *name);
137 int zvol_os_update_volsize(zvol_state_t *zv, uint64_t volsize);
138 boolean_t zvol_os_is_zvol(const char *path);
139 void zvol_os_remove_minor(zvol_state_t *zv);
140 void zvol_os_set_disk_ro(zvol_state_t *zv, int flags);
141 void zvol_os_set_capacity(zvol_state_t *zv, uint64_t capacity);
142 
143 #endif
144