xref: /freebsd/sys/contrib/openzfs/include/sys/zvol_impl.h (revision ce4dcb97ca433b2a2f03fbae957dae0ff16f6f51)
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 (c) 2024, Klara, Inc.
23  */
24 
25 #ifndef	_SYS_ZVOL_IMPL_H
26 #define	_SYS_ZVOL_IMPL_H
27 
28 #include <sys/zfs_context.h>
29 
30 #define	ZVOL_RDONLY	(1<<0)	/* zvol is readonly (writes rejected) */
31 #define	ZVOL_WRITTEN_TO	(1<<1)	/* zvol has been written to (needs flush) */
32 #define	ZVOL_EXCL	(1<<2)	/* zvol has O_EXCL client right now */
33 #define	ZVOL_REMOVING	(1<<3)	/* zvol waiting to remove minor */
34 
35 /*
36  * The in-core state of each volume.
37  */
38 typedef struct zvol_state {
39 	char			zv_name[MAXNAMELEN];	/* name */
40 	uint64_t		zv_volsize;		/* advertised space */
41 	uint64_t		zv_volblocksize;	/* volume block size */
42 	objset_t		*zv_objset;	/* objset handle */
43 	uint32_t		zv_flags;	/* ZVOL_* flags */
44 	uint32_t		zv_open_count;	/* open counts */
45 	uint32_t		zv_changed;	/* disk changed */
46 	uint32_t		zv_volmode;	/* volmode */
47 	zilog_t			*zv_zilog;	/* ZIL handle */
48 	zfs_rangelock_t		zv_rangelock;	/* for range locking */
49 	dnode_t			*zv_dn;		/* dnode hold */
50 	dataset_kstats_t	zv_kstat;	/* zvol kstats */
51 	list_node_t		zv_next;	/* next zvol_state_t linkage */
52 	uint64_t		zv_hash;	/* name hash */
53 	struct hlist_node	zv_hlink;	/* hash link */
54 	kmutex_t		zv_state_lock;	/* protects zvol_state_t */
55 	atomic_t		zv_suspend_ref;	/* refcount for suspend */
56 	krwlock_t		zv_suspend_lock;	/* suspend lock */
57 	kcondvar_t		zv_removing_cv;	/* ready to remove minor */
58 	struct zvol_state_os	*zv_zso;	/* private platform state */
59 	boolean_t		zv_threading;	/* volthreading property */
60 } zvol_state_t;
61 
62 
63 extern krwlock_t zvol_state_lock;
64 #define	ZVOL_HT_SIZE	1024
65 extern struct hlist_head *zvol_htable;
66 #define	ZVOL_HT_HEAD(hash)	(&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
67 extern zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE];
68 
69 extern unsigned int zvol_volmode;
70 extern unsigned int zvol_inhibit_dev;
71 
72 /*
73  * platform independent functions exported to platform code
74  */
75 zvol_state_t *zvol_find_by_name_hash(const char *name,
76     uint64_t hash, int mode);
77 int zvol_first_open(zvol_state_t *zv, boolean_t readonly);
78 uint64_t zvol_name_hash(const char *name);
79 void zvol_remove_minors_impl(const char *name);
80 void zvol_last_close(zvol_state_t *zv);
81 void zvol_insert(zvol_state_t *zv);
82 void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off,
83     uint64_t len);
84 void zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
85     uint64_t size, boolean_t commit);
86 int zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf,
87     struct lwb *lwb, zio_t *zio);
88 int zvol_init_impl(void);
89 void zvol_fini_impl(void);
90 void zvol_wait_close(zvol_state_t *zv);
91 
92 /*
93  * platform dependent functions exported to platform independent code
94  */
95 void zvol_os_free(zvol_state_t *zv);
96 void zvol_os_rename_minor(zvol_state_t *zv, const char *newname);
97 int zvol_os_create_minor(const char *name);
98 int zvol_os_update_volsize(zvol_state_t *zv, uint64_t volsize);
99 boolean_t zvol_os_is_zvol(const char *path);
100 void zvol_os_clear_private(zvol_state_t *zv);
101 void zvol_os_set_disk_ro(zvol_state_t *zv, int flags);
102 void zvol_os_set_capacity(zvol_state_t *zv, uint64_t capacity);
103 
104 #endif
105