dklist.c revision 1.10 1 1.10 joerg /* $NetBSD: dklist.c,v 1.10 2014/06/11 14:51:49 joerg Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.1 ad /*
33 1.1 ad * Copyright (c) 1996 John M. Vinopal
34 1.1 ad * All rights reserved.
35 1.1 ad *
36 1.1 ad * Redistribution and use in source and binary forms, with or without
37 1.1 ad * modification, are permitted provided that the following conditions
38 1.1 ad * are met:
39 1.1 ad * 1. Redistributions of source code must retain the above copyright
40 1.1 ad * notice, this list of conditions and the following disclaimer.
41 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 ad * notice, this list of conditions and the following disclaimer in the
43 1.1 ad * documentation and/or other materials provided with the distribution.
44 1.1 ad * 3. All advertising materials mentioning features or use of this software
45 1.1 ad * must display the following acknowledgement:
46 1.1 ad * This product includes software developed for the NetBSD Project
47 1.1 ad * by John M. Vinopal.
48 1.1 ad * 4. The name of the author may not be used to endorse or promote products
49 1.1 ad * derived from this software without specific prior written permission.
50 1.1 ad *
51 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 1.1 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 1.1 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 1.1 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 1.1 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
56 1.1 ad * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 1.1 ad * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
58 1.1 ad * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59 1.1 ad * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 ad * SUCH DAMAGE.
62 1.1 ad */
63 1.1 ad
64 1.1 ad #ifndef lint
65 1.1 ad #include <sys/cdefs.h>
66 1.10 joerg __RCSID("$NetBSD: dklist.c,v 1.10 2014/06/11 14:51:49 joerg Exp $");
67 1.1 ad #endif /* not lint */
68 1.1 ad
69 1.1 ad #include <sys/types.h>
70 1.10 joerg #include <sys/iostat.h>
71 1.1 ad #include <sys/ioctl.h>
72 1.10 joerg #include <sys/sysctl.h>
73 1.1 ad
74 1.1 ad #include <dev/ic/mlxreg.h>
75 1.1 ad #include <dev/ic/mlxio.h>
76 1.1 ad
77 1.10 joerg #include <ctype.h>
78 1.1 ad #include <err.h>
79 1.10 joerg #include <errno.h>
80 1.1 ad #include <fcntl.h>
81 1.1 ad #include <limits.h>
82 1.1 ad #include <stdio.h>
83 1.1 ad #include <stdlib.h>
84 1.1 ad #include <string.h>
85 1.1 ad #include <unistd.h>
86 1.10 joerg #include <util.h>
87 1.1 ad
88 1.1 ad #include "extern.h"
89 1.1 ad
90 1.1 ad static SIMPLEQ_HEAD(, mlx_disk) mlx_disks;
91 1.1 ad
92 1.1 ad void
93 1.1 ad mlx_disk_init(void)
94 1.1 ad {
95 1.1 ad
96 1.1 ad SIMPLEQ_INIT(&mlx_disks);
97 1.1 ad }
98 1.1 ad
99 1.1 ad int
100 1.1 ad mlx_disk_empty(void)
101 1.1 ad {
102 1.1 ad
103 1.4 lukem return (SIMPLEQ_EMPTY(&mlx_disks));
104 1.1 ad }
105 1.1 ad
106 1.1 ad void
107 1.1 ad mlx_disk_iterate(void (*func)(struct mlx_disk *))
108 1.1 ad {
109 1.1 ad struct mlx_disk *md;
110 1.1 ad
111 1.1 ad SIMPLEQ_FOREACH(md, &mlx_disks, chain)
112 1.1 ad (*func)(md);
113 1.1 ad }
114 1.1 ad
115 1.1 ad static int
116 1.1 ad mlx_disk_add0(const char *name)
117 1.1 ad {
118 1.1 ad struct mlx_disk *md;
119 1.1 ad int unit;
120 1.1 ad
121 1.5 dsl if (name[0] != 'l' || name[1] != 'd' || !isdigit((unsigned char)name[2]))
122 1.1 ad return (-1);
123 1.1 ad
124 1.1 ad SIMPLEQ_FOREACH(md, &mlx_disks, chain)
125 1.1 ad if (strcmp(md->name, name) == 0)
126 1.1 ad return (0);
127 1.1 ad
128 1.1 ad unit = atoi(name + 2);
129 1.1 ad if (ioctl(mlxfd, MLX_GET_SYSDRIVE, &unit) < 0)
130 1.1 ad return (-1);
131 1.1 ad
132 1.1 ad if ((md = malloc(sizeof(*md))) == NULL)
133 1.1 ad err(EXIT_FAILURE, "mlx_disk_add()");
134 1.1 ad
135 1.1 ad strlcpy(md->name, name, sizeof(md->name));
136 1.1 ad md->hwunit = unit;
137 1.1 ad SIMPLEQ_INSERT_TAIL(&mlx_disks, md, chain);
138 1.1 ad return (0);
139 1.1 ad }
140 1.1 ad
141 1.1 ad void
142 1.1 ad mlx_disk_add(const char *name)
143 1.1 ad {
144 1.1 ad
145 1.1 ad if (mlx_disk_add0(name) != 0)
146 1.1 ad errx(EXIT_FAILURE, "%s is not attached to %s", name, mlxname);
147 1.1 ad }
148 1.1 ad
149 1.10 joerg static void *
150 1.10 joerg fetch_sysctl(size_t *len, const int oids[], size_t oidlen, const char *msg)
151 1.1 ad {
152 1.10 joerg void *data;
153 1.10 joerg
154 1.10 joerg *len = 0;
155 1.10 joerg data = NULL;
156 1.1 ad
157 1.10 joerg for (;;) {
158 1.10 joerg if (sysctl(oids, oidlen, data, len, NULL, 0) == 0) {
159 1.10 joerg if (data != NULL || len == 0)
160 1.10 joerg return data;
161 1.10 joerg errno = ENOMEM;
162 1.10 joerg }
163 1.10 joerg free(data);
164 1.10 joerg if (errno == ENOMEM) {
165 1.10 joerg data = emalloc(*len);
166 1.10 joerg continue;
167 1.10 joerg }
168 1.10 joerg err(1, "%s", msg);
169 1.1 ad }
170 1.1 ad }
171 1.1 ad
172 1.10 joerg void
173 1.10 joerg mlx_disk_add_all(void)
174 1.1 ad {
175 1.10 joerg struct io_sysctl *data;
176 1.10 joerg size_t i, len;
177 1.10 joerg static const int mib[3] = { CTL_HW, HW_IOSTATS, sizeof(*data) };
178 1.10 joerg
179 1.10 joerg data = fetch_sysctl(&len, mib, __arraycount(mib), "hw.iostats failed");
180 1.10 joerg len /= sizeof(*data);
181 1.1 ad
182 1.10 joerg if (len == 0)
183 1.10 joerg errx(EXIT_FAILURE, "no drives attached.");
184 1.10 joerg
185 1.10 joerg for (i = 0; i < len; ++i) {
186 1.10 joerg if (data[i].type == IOSTAT_DISK)
187 1.10 joerg mlx_disk_add0(data[i].name);
188 1.1 ad }
189 1.10 joerg
190 1.10 joerg free(data);
191 1.1 ad }
192