xref: /freebsd/sys/geom/gate/g_gate.h (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _G_GATE_H_
32 #define _G_GATE_H_
33 
34 #include <sys/param.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/queue.h>
38 
39 #include <geom/geom.h>
40 
41 #define	G_GATE_CLASS_NAME	"GATE"
42 #define	G_GATE_PROVIDER_NAME	"ggate"
43 #define	G_GATE_MOD_NAME		"ggate"
44 #define	G_GATE_CTL_NAME		"ggctl"
45 
46 #define G_GATE_VERSION		3
47 
48 /*
49  * Maximum number of request that can be stored in
50  * the queue when there are no workers.
51  */
52 #define	G_GATE_MAX_QUEUE_SIZE	4096
53 
54 #define	G_GATE_FLAG_READONLY	0x0001
55 #define	G_GATE_FLAG_WRITEONLY	0x0002
56 #define	G_GATE_FLAG_DESTROY	0x1000
57 #define	G_GATE_USERFLAGS	(G_GATE_FLAG_READONLY | G_GATE_FLAG_WRITEONLY)
58 
59 /*
60  * Pick unit number automatically in /dev/ggate<unit>.
61  */
62 #define	G_GATE_UNIT_AUTO	(-1)
63 /*
64  * Full provider name is given, so don't use ggate<unit>.
65  */
66 #define	G_GATE_NAME_GIVEN	(-2)
67 
68 #define G_GATE_CMD_CREATE	_IOWR('m', 0, struct g_gate_ctl_create)
69 #define G_GATE_CMD_MODIFY	_IOWR('m', 1, struct g_gate_ctl_modify)
70 #define G_GATE_CMD_DESTROY	_IOWR('m', 2, struct g_gate_ctl_destroy)
71 #define G_GATE_CMD_CANCEL	_IOWR('m', 3, struct g_gate_ctl_cancel)
72 #define G_GATE_CMD_START	_IOWR('m', 4, struct g_gate_ctl_io)
73 #define G_GATE_CMD_DONE		_IOWR('m', 5, struct g_gate_ctl_io)
74 
75 #define	G_GATE_INFOSIZE		2048
76 
77 #ifdef _KERNEL
78 /*
79  * 'P:' means 'Protected by'.
80  */
81 struct g_gate_softc {
82 	char			*sc_name;		/* P: (read-only) */
83 	int			 sc_unit;		/* P: (read-only) */
84 	int			 sc_ref;		/* P: g_gate_list_mtx */
85 	struct g_provider	*sc_provider;		/* P: (read-only) */
86 	uint32_t		 sc_flags;		/* P: sc_queue_mtx */
87 
88 	struct bio_queue_head	 sc_inqueue;		/* P: sc_queue_mtx */
89 	struct bio_queue_head	 sc_outqueue;		/* P: sc_queue_mtx */
90 	struct mtx		 sc_queue_mtx;
91 	uint32_t		 sc_queue_count;	/* P: sc_queue_mtx */
92 	uint32_t		 sc_queue_size;		/* P: (read-only) */
93 	u_int			 sc_timeout;		/* P: (read-only) */
94 	struct g_consumer	*sc_readcons;		/* P: XXX */
95 	off_t			 sc_readoffset;		/* P: XXX */
96 	struct callout		 sc_callout;		/* P: (modified only
97 							       from callout
98 							       thread) */
99 	uintptr_t		 sc_seq;		/* P: (modified only
100 							       from g_down
101 							       thread) */
102 	LIST_ENTRY(g_gate_softc) sc_next;		/* P: g_gate_list_mtx */
103 	char			 sc_info[G_GATE_INFOSIZE]; /* P: (read-only) */
104 };
105 
106 #define	G_GATE_DEBUG(lvl, ...)	do {					\
107 	if (g_gate_debug >= (lvl)) {					\
108 		printf("GEOM_GATE");					\
109 		if (g_gate_debug > 0)					\
110 			printf("[%u]", lvl);				\
111 		printf(": ");						\
112 		printf(__VA_ARGS__);					\
113 		printf("\n");						\
114 	}								\
115 } while (0)
116 #define	G_GATE_LOGREQ(lvl, bp, ...)	do {				\
117 	if (g_gate_debug >= (lvl)) {					\
118 		printf("GEOM_GATE");					\
119 		if (g_gate_debug > 0)					\
120 			printf("[%u]", lvl);				\
121 		printf(": ");						\
122 		printf(__VA_ARGS__);					\
123 		printf(" ");						\
124 		g_print_bio(bp);					\
125 		printf("\n");						\
126 	}								\
127 } while (0)
128 #endif	/* !_KERNEL */
129 
130 struct g_gate_ctl_create {
131 	u_int	gctl_version;
132 	off_t	gctl_mediasize;
133 	u_int	gctl_sectorsize;
134 	u_int	gctl_flags;
135 	u_int	gctl_maxcount;
136 	u_int	gctl_timeout;
137 	char	gctl_name[NAME_MAX];
138 	char	gctl_info[G_GATE_INFOSIZE];
139 	char	gctl_readprov[NAME_MAX];
140 	off_t	gctl_readoffset;
141 	int	gctl_unit;	/* in/out */
142 };
143 
144 #define	GG_MODIFY_MEDIASIZE	0x01
145 #define	GG_MODIFY_INFO		0x02
146 #define	GG_MODIFY_READPROV	0x04
147 #define	GG_MODIFY_READOFFSET	0x08
148 struct g_gate_ctl_modify {
149 	u_int		gctl_version;
150 	int		gctl_unit;
151 	uint32_t	gctl_modify;
152 	off_t		gctl_mediasize;
153 	char		gctl_info[G_GATE_INFOSIZE];
154 	char		gctl_readprov[NAME_MAX];
155 	off_t		gctl_readoffset;
156 };
157 
158 struct g_gate_ctl_destroy {
159 	u_int	gctl_version;
160 	int	gctl_unit;
161 	int	gctl_force;
162 	char	gctl_name[NAME_MAX];
163 };
164 
165 struct g_gate_ctl_cancel {
166 	u_int		gctl_version;
167 	int		gctl_unit;
168 	uintptr_t	gctl_seq;
169 	char		gctl_name[NAME_MAX];
170 };
171 
172 struct g_gate_ctl_io {
173 	u_int		 gctl_version;
174 	int		 gctl_unit;
175 	uintptr_t	 gctl_seq;
176 	u_int		 gctl_cmd;
177 	off_t		 gctl_offset;
178 	off_t		 gctl_length;
179 	void		*gctl_data;
180 	int		 gctl_error;
181 };
182 #endif	/* !_G_GATE_H_ */
183