xref: /freebsd/cddl/lib/libdtrace/io.d (revision ea2be8ed283597062853935a5a29dfd67973044b)
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 http://www.opensolaris.org/os/licensing.
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  * Portions Copyright 2018 Devin Teske dteske@freebsd.org
22  */
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma D depends_on module kernel
29 #pragma D depends_on provider io
30 
31 typedef struct devinfo {
32 	int dev_major;			/* major number */
33 	int dev_minor;			/* minor number */
34 	int dev_instance;		/* instance number */
35 	int dev_type;			/* type of device */
36 	string dev_name;		/* name of device */
37 	string dev_statname;		/* name of device + instance/minor */
38 	string dev_pathname;		/* pathname of device */
39 } devinfo_t;
40 
41 #pragma D binding "1.0" translator
42 translator devinfo_t < struct devstat *D > {
43 	dev_major = D->device_number;
44 	dev_minor = D->unit_number;
45 	dev_instance = 0;
46 	dev_type = D->device_type;
47 	dev_name = stringof(D->device_name);
48 	dev_statname = stringof(D->device_name);
49 	dev_pathname = stringof(D->device_name);
50 };
51 
52 typedef struct bufinfo {
53 	int b_cmd;			/* I/O operation */
54 	int b_flags;			/* flags */
55 	long b_bcount;			/* number of bytes */
56 	caddr_t b_addr;			/* buffer address */
57 	uint64_t b_blkno;		/* expanded block # on device */
58 	uint64_t b_lblkno;		/* block # on device */
59 	size_t b_resid;			/* # of bytes not transferred */
60 	size_t b_bufsize;		/* size of allocated buffer */
61 /*	caddr_t b_iodone;		I/O completion routine */
62 	int b_error;			/* expanded error field */
63 /*	dev_t b_edev;			extended device */
64 } bufinfo_t;
65 
66 #pragma D binding "1.0" translator
67 translator bufinfo_t < struct bio *B > {
68 	b_cmd = B->bio_cmd;
69 	b_flags = B->bio_flags;
70 	b_bcount = B->bio_bcount;
71 	b_addr = B->bio_data;
72 	b_blkno = 0;
73 	b_lblkno = 0;
74 	b_resid = B->bio_resid;
75 	b_bufsize = 0; /* XXX gnn */
76 	b_error = B->bio_error;
77 };
78 
79 /*
80  * The following inline constants can be used to examine fi_oflags when using
81  * the fds[] array or a translated fileinfo_t.  Note that the various open
82  * flags behave as a bit-field *except* for O_RDONLY, O_WRONLY, and O_RDWR.
83  * To test the open mode, you write code similar to that used with the fcntl(2)
84  * F_GET[X]FL command, such as: if ((fi_oflags & O_ACCMODE) == O_WRONLY).
85  */
86 inline int O_ACCMODE = 0x0003;
87 #pragma D binding "1.1" O_ACCMODE
88 
89 inline int O_RDONLY = 0x0000;
90 #pragma D binding "1.1" O_RDONLY
91 inline int O_WRONLY = 0x0001;
92 #pragma D binding "1.1" O_WRONLY
93 inline int O_RDWR = 0x0002;
94 #pragma D binding "1.1" O_RDWR
95 
96 inline int O_APPEND = 0x0008;
97 #pragma D binding "1.1" O_APPEND
98 inline int O_CREAT = 0x0200;
99 #pragma D binding "1.1" O_CREAT
100 inline int O_EXCL = 0x0800;
101 #pragma D binding "1.1" O_EXCL
102 inline int O_NOCTTY = 0x8000;
103 #pragma D binding "1.1" O_NOCTTY
104 inline int O_NONBLOCK = 0x0004;
105 #pragma D binding "1.1" O_NONBLOCK
106 inline int O_NDELAY = 0x0004;
107 #pragma D binding "1.1" O_NDELAY
108 inline int O_SYNC = 0x0080;
109 #pragma D binding "1.1" O_SYNC
110 inline int O_TRUNC = 0x0400;
111 #pragma D binding "1.1" O_TRUNC
112 
113 /*
114  * The following inline constants can be used to examine bio_cmd of struct bio
115  * or a translated bufinfo_t.
116  */
117 inline int BIO_READ =		0x01;
118 #pragma D binding "1.13" BIO_READ
119 inline int BIO_WRITE =		0x02;
120 #pragma D binding "1.13" BIO_WRITE
121 inline int BIO_DELETE =		0x03;
122 #pragma D binding "1.13" BIO_DELETE
123 inline int BIO_GETATTR =	0x04;
124 #pragma D binding "1.13" BIO_GETATTR
125 inline int BIO_FLUSH =		0x05;
126 #pragma D binding "1.13" BIO_FLUSH
127 inline int BIO_CMD0 =		0x06;
128 #pragma D binding "1.13" BIO_CMD0
129 inline int BIO_CMD1 =		0x07;
130 #pragma D binding "1.13" BIO_CMD1
131 inline int BIO_CMD2 =		0x08;
132 #pragma D binding "1.13" BIO_CMD2
133 inline int BIO_ZONE =		0x09;
134 #pragma D binding "1.13" BIO_ZONE
135 
136 /*
137  * The following inline constants can be used to examine bio_flags of struct
138  * bio or a translated bufinfo_t.
139  */
140 inline int BIO_ERROR =			0x01;
141 #pragma D binding "1.13" BIO_ERROR
142 inline int BIO_DONE =			0x02;
143 #pragma D binding "1.13" BIO_DONE
144 inline int BIO_ONQUEUE =		0x04;
145 #pragma D binding "1.13" BIO_ONQUEUE
146 inline int BIO_ORDERED =		0x08;
147 #pragma D binding "1.13" BIO_ORDERED
148 inline int BIO_UNMAPPED =		0x10;
149 #pragma D binding "1.13" BIO_UNMAPPED
150 inline int BIO_TRANSIENT_MAPPING =	0x20;
151 #pragma D binding "1.13" BIO_TRANSIENT_MAPPING
152 inline int BIO_VLIST =			0x40;
153 #pragma D binding "1.13" BIO_VLIST
154 
155 /*
156  * The following inline constants can be used to examine device_type of struct
157  * devstat or a translated devinfo_t.
158  */
159 inline int DEVSTAT_TYPE_DIRECT =	0x000;
160 #pragma D binding "1.13" DEVSTAT_TYPE_DIRECT
161 inline int DEVSTAT_TYPE_SEQUENTIAL =	0x001;
162 #pragma D binding "1.13" DEVSTAT_TYPE_SEQUENTIAL
163 inline int DEVSTAT_TYPE_PRINTER =	0x002;
164 #pragma D binding "1.13" DEVSTAT_TYPE_PRINTER
165 inline int DEVSTAT_TYPE_PROCESSOR =	0x003;
166 #pragma D binding "1.13" DEVSTAT_TYPE_PROCESSOR
167 inline int DEVSTAT_TYPE_WORM =		0x004;
168 #pragma D binding "1.13" DEVSTAT_TYPE_WORM
169 inline int DEVSTAT_TYPE_CDROM =		0x005;
170 #pragma D binding "1.13" DEVSTAT_TYPE_CDROM
171 inline int DEVSTAT_TYPE_SCANNER =	0x006;
172 #pragma D binding "1.13" DEVSTAT_TYPE_SCANNER
173 inline int DEVSTAT_TYPE_OPTICAL =	0x007;
174 #pragma D binding "1.13" DEVSTAT_TYPE_OPTICAL
175 inline int DEVSTAT_TYPE_CHANGER =	0x008;
176 #pragma D binding "1.13" DEVSTAT_TYPE_CHANGER
177 inline int DEVSTAT_TYPE_COMM =		0x009;
178 #pragma D binding "1.13" DEVSTAT_TYPE_COMM
179 inline int DEVSTAT_TYPE_ASC0 =		0x00a;
180 #pragma D binding "1.13" DEVSTAT_TYPE_ASC0
181 inline int DEVSTAT_TYPE_ASC1 =		0x00b;
182 #pragma D binding "1.13" DEVSTAT_TYPE_ASC1
183 inline int DEVSTAT_TYPE_STORARRAY =	0x00c;
184 #pragma D binding "1.13" DEVSTAT_TYPE_STORARRAY
185 inline int DEVSTAT_TYPE_ENCLOSURE =	0x00d;
186 #pragma D binding "1.13" DEVSTAT_TYPE_ENCLOSURE
187 inline int DEVSTAT_TYPE_FLOPPY =	0x00e;
188 #pragma D binding "1.13" DEVSTAT_TYPE_FLOPPY
189 inline int DEVSTAT_TYPE_MASK =		0x00f;
190 #pragma D binding "1.13" DEVSTAT_TYPE_MASK
191 inline int DEVSTAT_TYPE_IF_SCSI =	0x010;
192 #pragma D binding "1.13" DEVSTAT_TYPE_IF_SCSI
193 inline int DEVSTAT_TYPE_IF_IDE =	0x020;
194 #pragma D binding "1.13" DEVSTAT_TYPE_IF_IDE
195 inline int DEVSTAT_TYPE_IF_OTHER =	0x030;
196 #pragma D binding "1.13" DEVSTAT_TYPE_IF_OTHER
197 inline int DEVSTAT_TYPE_IF_NVME =	0x040;
198 #pragma D binding "1.13" DEVSTAT_TYPE_IF_NVME
199 inline int DEVSTAT_TYPE_IF_MASK =	0x0f0;
200 #pragma D binding "1.13" DEVSTAT_TYPE_IF_MASK
201 inline int DEVSTAT_TYPE_PASS =		0x100;
202 #pragma D binding "1.13" DEVSTAT_TYPE_PASS
203 
204 #pragma D binding "1.13" device_type_string
205 inline string device_type_string[int type] =
206 	type == DEVSTAT_TYPE_DIRECT ?		"DIRECT" :
207 	type == DEVSTAT_TYPE_SEQUENTIAL ?	"SEQUENTIAL" :
208 	type == DEVSTAT_TYPE_PRINTER ?		"PRINTER" :
209 	type == DEVSTAT_TYPE_PROCESSOR ?	"PROCESSOR" :
210 	type == DEVSTAT_TYPE_WORM ?		"WORM" :
211 	type == DEVSTAT_TYPE_CDROM ?		"CDROM" :
212 	type == DEVSTAT_TYPE_SCANNER ?		"SCANNER" :
213 	type == DEVSTAT_TYPE_OPTICAL ?		"OPTICAL" :
214 	type == DEVSTAT_TYPE_CHANGER ?		"CHANGER" :
215 	type == DEVSTAT_TYPE_COMM ?		"COMM" :
216 	type == DEVSTAT_TYPE_ASC0 ?		"ASC0" :
217 	type == DEVSTAT_TYPE_ASC1 ?		"ASC1" :
218 	type == DEVSTAT_TYPE_STORARRAY ?	"STORARRAY" :
219 	type == DEVSTAT_TYPE_ENCLOSURE ?	"ENCLOSURE" :
220 	type == DEVSTAT_TYPE_FLOPPY ?		"FLOPPY" :
221 	strjoin("UNKNOWN(", strjoin(lltostr(type), ")"));
222 
223 #pragma D binding "1.13" device_type
224 inline string device_type[int type] =
225 	device_type_string[type & DEVSTAT_TYPE_MASK];
226 
227 #pragma D binding "1.13" device_if_string
228 inline string device_if_string[int type] =
229 	type == 0 ?			"ACCESS" :
230 	type == DEVSTAT_TYPE_IF_SCSI ?	"SCSI" :
231 	type == DEVSTAT_TYPE_IF_IDE ?	"IDE" :
232 	type == DEVSTAT_TYPE_IF_OTHER ?	"OTHER" :
233 	type == DEVSTAT_TYPE_IF_NVME ?	"NVME" :
234 	strjoin("UNKNOWN(", strjoin(lltostr(type), ")"));
235 
236 #pragma D binding "1.13" device_if
237 inline string device_if[int type] =
238 	device_if_string[type & DEVSTAT_TYPE_IF_MASK];
239 
240 #pragma D binding "1.13" bio_cmd_string
241 inline string bio_cmd_string[int cmd] =
242 	cmd == BIO_READ ?	"READ" :
243 	cmd == BIO_WRITE ?	"WRITE" :
244 	cmd == BIO_DELETE ?	"DELETE" :
245 	cmd == BIO_GETATTR ?	"GETATTR" :
246 	cmd == BIO_FLUSH ?	"FLUSH" :
247 	cmd == BIO_CMD0 ?	"CMD0" :
248 	cmd == BIO_CMD1 ?	"CMD1" :
249 	cmd == BIO_CMD2 ?	"CMD2" :
250 	cmd == BIO_ZONE ?	"ZONE" :
251 	strjoin("UNKNOWN(", strjoin(lltostr(cmd), ")"));
252 
253 #pragma D binding "1.13" bio_flag_string
254 inline string bio_flag_string[int flag] =
255 	flag == BIO_ERROR ?		"ERROR" :
256 	flag == BIO_DONE ?		"DONE" :
257 	flag == BIO_ONQUEUE ?		"ONQUEUE" :
258 	flag == BIO_ORDERED ?		"ORDERED" :
259 	flag == BIO_UNMAPPED ?		"UNMAPPED" :
260 	flag == BIO_TRANSIENT_MAPPING ?	"TRANSIENT_MAPPING" :
261 	flag == BIO_VLIST ?		"VLIST" :
262 	"";
263