xref: /freebsd/sys/contrib/openzfs/include/sys/zvol_impl.h (revision 3a8960711f4319f9b894ea2453c89065ee1b3a10)
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  * zvol taskqs
65  */
66 typedef struct zv_taskq {
67 	uint_t tqs_cnt;
68 	taskq_t **tqs_taskq;
69 } zv_taskq_t;
70 
71 typedef struct zv_request_stack {
72 	zvol_state_t	*zv;
73 	struct bio	*bio;
74 #ifdef __linux__
75 	struct request	*rq;
76 #endif
77 } zv_request_t;
78 
79 typedef struct zv_request_task {
80 	zv_request_t	zvr;
81 	taskq_ent_t	ent;
82 } zv_request_task_t;
83 
84 /*
85  * Switch taskq at multiple of 512 MB offset. This can be set to a lower value
86  * to utilize more threads for small files but may affect prefetch hits.
87  */
88 #define	ZVOL_TASKQ_OFFSET_SHIFT 29
89 
90 extern krwlock_t zvol_state_lock;
91 #define	ZVOL_HT_SIZE	1024
92 extern struct hlist_head *zvol_htable;
93 #define	ZVOL_HT_HEAD(hash)	(&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
94 extern zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE];
95 
96 extern unsigned int zvol_inhibit_dev;
97 extern unsigned int zvol_prefetch_bytes;
98 extern unsigned int zvol_volmode;
99 extern unsigned int zvol_threads;
100 extern unsigned int zvol_num_taskqs;
101 extern unsigned int zvol_request_sync;
102 extern zv_taskq_t zvol_taskqs;
103 
104 /*
105  * platform independent functions exported to platform code
106  */
107 zvol_state_t *zvol_find_by_name_hash(const char *name,
108     uint64_t hash, int mode);
109 int zvol_first_open(zvol_state_t *zv, boolean_t readonly);
110 uint64_t zvol_name_hash(const char *name);
111 void zvol_remove_minors_impl(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 void 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_clear_private(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