xref: /freebsd/sys/contrib/openzfs/include/sys/zvol_impl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2024, 2025, Klara, Inc.
14  */
15 
16 #ifndef	_SYS_ZVOL_IMPL_H
17 #define	_SYS_ZVOL_IMPL_H
18 
19 #include <sys/zfs_context.h>
20 
21 #define	ZVOL_RDONLY	(1<<0)	/* zvol is readonly (writes rejected) */
22 #define	ZVOL_WRITTEN_TO	(1<<1)	/* zvol has been written to (needs flush) */
23 #define	ZVOL_EXCL	(1<<2)	/* zvol has O_EXCL client right now */
24 #define	ZVOL_REMOVING	(1<<3)	/* zvol waiting to remove minor */
25 
26 /*
27  * The in-core state of each volume.
28  */
29 typedef struct zvol_state {
30 	char			zv_name[MAXNAMELEN];	/* name */
31 	uint64_t		zv_volsize;		/* advertised space */
32 	uint64_t		zv_volblocksize;	/* volume block size */
33 	objset_t		*zv_objset;	/* objset handle */
34 	uint32_t		zv_flags;	/* ZVOL_* flags */
35 	uint32_t		zv_open_count;	/* open counts */
36 	uint32_t		zv_changed;	/* disk changed */
37 	uint32_t		zv_volmode;	/* volmode */
38 	zilog_t			*zv_zilog;	/* ZIL handle */
39 	zfs_rangelock_t		zv_rangelock;	/* for range locking */
40 	dnode_t			*zv_dn;		/* dnode hold */
41 	dataset_kstats_t	zv_kstat;	/* zvol kstats */
42 	list_node_t		zv_next;	/* next zvol_state_t linkage */
43 	uint64_t		zv_hash;	/* name hash */
44 	struct hlist_node	zv_hlink;	/* hash link */
45 	kmutex_t		zv_state_lock;	/* protects zvol_state_t */
46 	atomic_t		zv_suspend_ref;	/* refcount for suspend */
47 	krwlock_t		zv_suspend_lock;	/* suspend lock */
48 	kcondvar_t		zv_removing_cv;	/* ready to remove minor */
49 	list_node_t		zv_remove_node;	/* node on removal list */
50 	struct zvol_state_os	*zv_zso;	/* private platform state */
51 	boolean_t		zv_threading;	/* volthreading property */
52 } zvol_state_t;
53 
54 /*
55  * zvol taskqs
56  */
57 typedef struct zv_taskq {
58 	uint_t tqs_cnt;
59 	taskq_t **tqs_taskq;
60 } zv_taskq_t;
61 
62 typedef struct zv_request_stack {
63 	zvol_state_t	*zv;
64 	struct bio	*bio;
65 #ifdef __linux__
66 	struct request	*rq;
67 #endif
68 } zv_request_t;
69 
70 typedef struct zv_request_task {
71 	zv_request_t	zvr;
72 	taskq_ent_t	ent;
73 } zv_request_task_t;
74 
75 /*
76  * Switch taskq at multiple of 512 MB offset. This can be set to a lower value
77  * to utilize more threads for small files but may affect prefetch hits.
78  */
79 #define	ZVOL_TASKQ_OFFSET_SHIFT 29
80 
81 extern krwlock_t zvol_state_lock;
82 #define	ZVOL_HT_SIZE	1024
83 extern struct hlist_head *zvol_htable;
84 #define	ZVOL_HT_HEAD(hash)	(&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
85 extern zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE];
86 
87 extern unsigned int zvol_inhibit_dev;
88 extern unsigned int zvol_prefetch_bytes;
89 extern unsigned int zvol_volmode;
90 extern unsigned int zvol_threads;
91 extern unsigned int zvol_num_taskqs;
92 extern unsigned int zvol_request_sync;
93 extern zv_taskq_t zvol_taskqs;
94 
95 /*
96  * platform independent functions exported to platform code
97  */
98 zvol_state_t *zvol_find_by_name_hash(const char *name,
99     uint64_t hash, int mode);
100 int zvol_first_open(zvol_state_t *zv, boolean_t readonly);
101 uint64_t zvol_name_hash(const char *name);
102 void zvol_last_close(zvol_state_t *zv);
103 void zvol_insert(zvol_state_t *zv);
104 void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off,
105     uint64_t len);
106 void zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
107     uint64_t size, boolean_t commit);
108 int zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf,
109     struct lwb *lwb, zio_t *zio);
110 int zvol_init_impl(void);
111 void zvol_fini_impl(void);
112 void zvol_wait_close(zvol_state_t *zv);
113 int zvol_clone_range(zvol_state_handle_t *, uint64_t,
114     zvol_state_handle_t *, uint64_t, uint64_t);
115 void zvol_log_clone_range(zilog_t *zilog, dmu_tx_t *tx, int txtype,
116     uint64_t off, uint64_t len, uint64_t blksz, const blkptr_t *bps,
117     size_t nbps);
118 zv_request_task_t *zv_request_task_create(zv_request_t zvr);
119 void zv_request_task_free(zv_request_task_t *task);
120 
121 /*
122  * platform dependent functions exported to platform independent code
123  */
124 void zvol_os_free(zvol_state_t *zv);
125 int zvol_os_rename_minor(zvol_state_t *zv, const char *newname);
126 int zvol_os_create_minor(const char *name);
127 int zvol_os_update_volsize(zvol_state_t *zv, uint64_t volsize);
128 boolean_t zvol_os_is_zvol(const char *path);
129 void zvol_os_remove_minor(zvol_state_t *zv);
130 void zvol_os_set_disk_ro(zvol_state_t *zv, int flags);
131 void zvol_os_set_capacity(zvol_state_t *zv, uint64_t capacity);
132 
133 #endif
134