dkctl.c revision 1.21 1 1.21 mlelstv /* $NetBSD: dkctl.c,v 1.21 2014/11/04 08:00:44 mlelstv Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright 2001 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej /*
39 1.1 thorpej * dkctl(8) -- a program to manipulate disks.
40 1.1 thorpej */
41 1.7 agc #include <sys/cdefs.h>
42 1.7 agc
43 1.7 agc #ifndef lint
44 1.21 mlelstv __RCSID("$NetBSD: dkctl.c,v 1.21 2014/11/04 08:00:44 mlelstv Exp $");
45 1.7 agc #endif
46 1.7 agc
47 1.1 thorpej
48 1.1 thorpej #include <sys/param.h>
49 1.1 thorpej #include <sys/ioctl.h>
50 1.1 thorpej #include <sys/dkio.h>
51 1.3 darrenr #include <sys/disk.h>
52 1.3 darrenr #include <sys/queue.h>
53 1.1 thorpej #include <err.h>
54 1.1 thorpej #include <errno.h>
55 1.1 thorpej #include <fcntl.h>
56 1.1 thorpej #include <stdio.h>
57 1.1 thorpej #include <stdlib.h>
58 1.1 thorpej #include <string.h>
59 1.1 thorpej #include <unistd.h>
60 1.1 thorpej #include <util.h>
61 1.1 thorpej
62 1.2 yamt #define YES 1
63 1.2 yamt #define NO 0
64 1.2 yamt
65 1.2 yamt /* I don't think nl_langinfo is suitable in this case */
66 1.2 yamt #define YES_STR "yes"
67 1.2 yamt #define NO_STR "no"
68 1.2 yamt #define YESNO_ARG YES_STR " | " NO_STR
69 1.2 yamt
70 1.3 darrenr #ifndef PRIdaddr
71 1.3 darrenr #define PRIdaddr PRId64
72 1.3 darrenr #endif
73 1.3 darrenr
74 1.1 thorpej struct command {
75 1.1 thorpej const char *cmd_name;
76 1.1 thorpej const char *arg_names;
77 1.1 thorpej void (*cmd_func)(int, char *[]);
78 1.1 thorpej int open_flags;
79 1.1 thorpej };
80 1.1 thorpej
81 1.20 joerg static struct command *lookup(const char *);
82 1.20 joerg __dead static void usage(void);
83 1.20 joerg static void run(int, char *[]);
84 1.20 joerg static void showall(void);
85 1.20 joerg
86 1.20 joerg static int fd; /* file descriptor for device */
87 1.20 joerg static const char *dvname; /* device name */
88 1.20 joerg static char dvname_store[MAXPATHLEN]; /* for opendisk(3) */
89 1.20 joerg static const char *cmdname; /* command user issued */
90 1.20 joerg
91 1.20 joerg static int dkw_sort(const void *, const void *);
92 1.20 joerg static int yesno(const char *);
93 1.20 joerg
94 1.20 joerg static void disk_getcache(int, char *[]);
95 1.20 joerg static void disk_setcache(int, char *[]);
96 1.20 joerg static void disk_synccache(int, char *[]);
97 1.20 joerg static void disk_keeplabel(int, char *[]);
98 1.20 joerg static void disk_badsectors(int, char *[]);
99 1.20 joerg
100 1.20 joerg static void disk_addwedge(int, char *[]);
101 1.20 joerg static void disk_delwedge(int, char *[]);
102 1.20 joerg static void disk_getwedgeinfo(int, char *[]);
103 1.20 joerg static void disk_listwedges(int, char *[]);
104 1.21 mlelstv static void disk_makewedges(int, char *[]);
105 1.20 joerg static void disk_strategy(int, char *[]);
106 1.17 uebayasi
107 1.20 joerg static struct command commands[] = {
108 1.1 thorpej { "getcache",
109 1.1 thorpej "",
110 1.1 thorpej disk_getcache,
111 1.1 thorpej O_RDONLY },
112 1.1 thorpej
113 1.1 thorpej { "setcache",
114 1.1 thorpej "none | r | w | rw [save]",
115 1.1 thorpej disk_setcache,
116 1.1 thorpej O_RDWR },
117 1.1 thorpej
118 1.1 thorpej { "synccache",
119 1.1 thorpej "[force]",
120 1.1 thorpej disk_synccache,
121 1.1 thorpej O_RDWR },
122 1.1 thorpej
123 1.2 yamt { "keeplabel",
124 1.2 yamt YESNO_ARG,
125 1.2 yamt disk_keeplabel,
126 1.2 yamt O_RDWR },
127 1.2 yamt
128 1.3 darrenr { "badsector",
129 1.3 darrenr "flush | list | retry",
130 1.3 darrenr disk_badsectors,
131 1.3 darrenr O_RDWR },
132 1.3 darrenr
133 1.9 thorpej { "addwedge",
134 1.9 thorpej "name startblk blkcnt ptype",
135 1.9 thorpej disk_addwedge,
136 1.9 thorpej O_RDWR },
137 1.9 thorpej
138 1.9 thorpej { "delwedge",
139 1.9 thorpej "dk",
140 1.9 thorpej disk_delwedge,
141 1.9 thorpej O_RDWR },
142 1.9 thorpej
143 1.9 thorpej { "getwedgeinfo",
144 1.9 thorpej "",
145 1.9 thorpej disk_getwedgeinfo,
146 1.9 thorpej O_RDONLY },
147 1.9 thorpej
148 1.9 thorpej { "listwedges",
149 1.9 thorpej "",
150 1.9 thorpej disk_listwedges,
151 1.9 thorpej O_RDONLY },
152 1.9 thorpej
153 1.21 mlelstv { "makewedges",
154 1.21 mlelstv "",
155 1.21 mlelstv disk_makewedges,
156 1.21 mlelstv O_RDWR },
157 1.21 mlelstv
158 1.12 yamt { "strategy",
159 1.12 yamt "[name]",
160 1.12 yamt disk_strategy,
161 1.12 yamt O_RDWR },
162 1.12 yamt
163 1.1 thorpej { NULL,
164 1.1 thorpej NULL,
165 1.1 thorpej NULL,
166 1.1 thorpej 0 },
167 1.1 thorpej };
168 1.1 thorpej
169 1.1 thorpej int
170 1.1 thorpej main(int argc, char *argv[])
171 1.1 thorpej {
172 1.1 thorpej
173 1.1 thorpej /* Must have at least: device command */
174 1.17 uebayasi if (argc < 2)
175 1.1 thorpej usage();
176 1.1 thorpej
177 1.1 thorpej dvname = argv[1];
178 1.17 uebayasi if (argc == 2)
179 1.17 uebayasi showall();
180 1.17 uebayasi else {
181 1.17 uebayasi /* Skip program name, get and skip device name and command. */
182 1.17 uebayasi cmdname = argv[2];
183 1.17 uebayasi argv += 3;
184 1.17 uebayasi argc -= 3;
185 1.17 uebayasi run(argc, argv);
186 1.17 uebayasi }
187 1.17 uebayasi
188 1.17 uebayasi exit(0);
189 1.17 uebayasi }
190 1.1 thorpej
191 1.20 joerg static void
192 1.17 uebayasi run(int argc, char *argv[])
193 1.17 uebayasi {
194 1.17 uebayasi struct command *command;
195 1.1 thorpej
196 1.17 uebayasi command = lookup(cmdname);
197 1.1 thorpej
198 1.1 thorpej /* Open the device. */
199 1.17 uebayasi fd = opendisk(dvname, command->open_flags, dvname_store,
200 1.1 thorpej sizeof(dvname_store), 0);
201 1.1 thorpej if (fd == -1)
202 1.1 thorpej err(1, "%s", dvname);
203 1.17 uebayasi dvname = dvname_store;
204 1.17 uebayasi
205 1.17 uebayasi (*command->cmd_func)(argc, argv);
206 1.17 uebayasi
207 1.17 uebayasi /* Close the device. */
208 1.17 uebayasi (void)close(fd);
209 1.17 uebayasi }
210 1.17 uebayasi
211 1.20 joerg static struct command *
212 1.17 uebayasi lookup(const char *name)
213 1.17 uebayasi {
214 1.17 uebayasi int i;
215 1.1 thorpej
216 1.17 uebayasi /* Look up the command. */
217 1.17 uebayasi for (i = 0; commands[i].cmd_name != NULL; i++)
218 1.17 uebayasi if (strcmp(name, commands[i].cmd_name) == 0)
219 1.17 uebayasi break;
220 1.17 uebayasi if (commands[i].cmd_name == NULL)
221 1.17 uebayasi errx(1, "unknown command: %s", name);
222 1.1 thorpej
223 1.17 uebayasi return &commands[i];
224 1.1 thorpej }
225 1.1 thorpej
226 1.20 joerg static void
227 1.11 xtraeme usage(void)
228 1.1 thorpej {
229 1.1 thorpej int i;
230 1.1 thorpej
231 1.17 uebayasi fprintf(stderr,
232 1.17 uebayasi "usage: %s device\n"
233 1.17 uebayasi " %s device command [arg [...]]\n",
234 1.17 uebayasi getprogname(), getprogname());
235 1.1 thorpej
236 1.1 thorpej fprintf(stderr, " Available commands:\n");
237 1.1 thorpej for (i = 0; commands[i].cmd_name != NULL; i++)
238 1.1 thorpej fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
239 1.1 thorpej commands[i].arg_names);
240 1.1 thorpej
241 1.1 thorpej exit(1);
242 1.1 thorpej }
243 1.1 thorpej
244 1.20 joerg static void
245 1.17 uebayasi showall(void)
246 1.17 uebayasi {
247 1.17 uebayasi printf("strategy:\n");
248 1.17 uebayasi cmdname = "strategy";
249 1.17 uebayasi run(0, NULL);
250 1.17 uebayasi
251 1.17 uebayasi putchar('\n');
252 1.17 uebayasi
253 1.17 uebayasi printf("cache:\n");
254 1.17 uebayasi cmdname = "getcache";
255 1.17 uebayasi run(0, NULL);
256 1.17 uebayasi
257 1.17 uebayasi putchar('\n');
258 1.17 uebayasi
259 1.17 uebayasi printf("wedges:\n");
260 1.17 uebayasi cmdname = "listwedges";
261 1.17 uebayasi run(0, NULL);
262 1.17 uebayasi }
263 1.17 uebayasi
264 1.20 joerg static void
265 1.12 yamt disk_strategy(int argc, char *argv[])
266 1.12 yamt {
267 1.12 yamt struct disk_strategy odks;
268 1.12 yamt struct disk_strategy dks;
269 1.12 yamt
270 1.12 yamt memset(&dks, 0, sizeof(dks));
271 1.12 yamt if (ioctl(fd, DIOCGSTRATEGY, &odks) == -1) {
272 1.12 yamt err(EXIT_FAILURE, "%s: DIOCGSTRATEGY", dvname);
273 1.12 yamt }
274 1.12 yamt
275 1.12 yamt memset(&dks, 0, sizeof(dks));
276 1.12 yamt switch (argc) {
277 1.12 yamt case 0:
278 1.12 yamt /* show the buffer queue strategy used */
279 1.12 yamt printf("%s: %s\n", dvname, odks.dks_name);
280 1.12 yamt return;
281 1.12 yamt case 1:
282 1.12 yamt /* set the buffer queue strategy */
283 1.12 yamt strlcpy(dks.dks_name, argv[0], sizeof(dks.dks_name));
284 1.12 yamt if (ioctl(fd, DIOCSSTRATEGY, &dks) == -1) {
285 1.12 yamt err(EXIT_FAILURE, "%s: DIOCSSTRATEGY", dvname);
286 1.12 yamt }
287 1.12 yamt printf("%s: %s -> %s\n", dvname, odks.dks_name, argv[0]);
288 1.12 yamt break;
289 1.12 yamt default:
290 1.12 yamt usage();
291 1.12 yamt /* NOTREACHED */
292 1.12 yamt }
293 1.12 yamt }
294 1.12 yamt
295 1.20 joerg static void
296 1.1 thorpej disk_getcache(int argc, char *argv[])
297 1.1 thorpej {
298 1.1 thorpej int bits;
299 1.1 thorpej
300 1.1 thorpej if (ioctl(fd, DIOCGCACHE, &bits) == -1)
301 1.1 thorpej err(1, "%s: getcache", dvname);
302 1.1 thorpej
303 1.1 thorpej if ((bits & (DKCACHE_READ|DKCACHE_WRITE)) == 0)
304 1.1 thorpej printf("%s: No caches enabled\n", dvname);
305 1.1 thorpej else {
306 1.1 thorpej if (bits & DKCACHE_READ)
307 1.1 thorpej printf("%s: read cache enabled\n", dvname);
308 1.1 thorpej if (bits & DKCACHE_WRITE)
309 1.1 thorpej printf("%s: write-back cache enabled\n", dvname);
310 1.1 thorpej }
311 1.1 thorpej
312 1.1 thorpej printf("%s: read cache enable is %schangeable\n", dvname,
313 1.1 thorpej (bits & DKCACHE_RCHANGE) ? "" : "not ");
314 1.1 thorpej printf("%s: write cache enable is %schangeable\n", dvname,
315 1.1 thorpej (bits & DKCACHE_WCHANGE) ? "" : "not ");
316 1.1 thorpej
317 1.1 thorpej printf("%s: cache parameters are %ssavable\n", dvname,
318 1.1 thorpej (bits & DKCACHE_SAVE) ? "" : "not ");
319 1.1 thorpej }
320 1.1 thorpej
321 1.20 joerg static void
322 1.1 thorpej disk_setcache(int argc, char *argv[])
323 1.1 thorpej {
324 1.1 thorpej int bits;
325 1.1 thorpej
326 1.1 thorpej if (argc > 2 || argc == 0)
327 1.1 thorpej usage();
328 1.1 thorpej
329 1.1 thorpej if (strcmp(argv[0], "none") == 0)
330 1.1 thorpej bits = 0;
331 1.1 thorpej else if (strcmp(argv[0], "r") == 0)
332 1.1 thorpej bits = DKCACHE_READ;
333 1.1 thorpej else if (strcmp(argv[0], "w") == 0)
334 1.1 thorpej bits = DKCACHE_WRITE;
335 1.1 thorpej else if (strcmp(argv[0], "rw") == 0)
336 1.1 thorpej bits = DKCACHE_READ|DKCACHE_WRITE;
337 1.1 thorpej else
338 1.1 thorpej usage();
339 1.1 thorpej
340 1.1 thorpej if (argc == 2) {
341 1.1 thorpej if (strcmp(argv[1], "save") == 0)
342 1.1 thorpej bits |= DKCACHE_SAVE;
343 1.1 thorpej else
344 1.1 thorpej usage();
345 1.1 thorpej }
346 1.1 thorpej
347 1.1 thorpej if (ioctl(fd, DIOCSCACHE, &bits) == -1)
348 1.1 thorpej err(1, "%s: setcache", dvname);
349 1.1 thorpej }
350 1.1 thorpej
351 1.20 joerg static void
352 1.1 thorpej disk_synccache(int argc, char *argv[])
353 1.1 thorpej {
354 1.1 thorpej int force;
355 1.1 thorpej
356 1.1 thorpej switch (argc) {
357 1.1 thorpej case 0:
358 1.1 thorpej force = 0;
359 1.1 thorpej break;
360 1.1 thorpej
361 1.1 thorpej case 1:
362 1.1 thorpej if (strcmp(argv[0], "force") == 0)
363 1.1 thorpej force = 1;
364 1.1 thorpej else
365 1.1 thorpej usage();
366 1.1 thorpej break;
367 1.1 thorpej
368 1.1 thorpej default:
369 1.1 thorpej usage();
370 1.1 thorpej }
371 1.1 thorpej
372 1.1 thorpej if (ioctl(fd, DIOCCACHESYNC, &force) == -1)
373 1.1 thorpej err(1, "%s: sync cache", dvname);
374 1.2 yamt }
375 1.2 yamt
376 1.20 joerg static void
377 1.2 yamt disk_keeplabel(int argc, char *argv[])
378 1.2 yamt {
379 1.2 yamt int keep;
380 1.2 yamt int yn;
381 1.2 yamt
382 1.2 yamt if (argc != 1)
383 1.2 yamt usage();
384 1.2 yamt
385 1.2 yamt yn = yesno(argv[0]);
386 1.2 yamt if (yn < 0)
387 1.2 yamt usage();
388 1.2 yamt
389 1.2 yamt keep = yn == YES;
390 1.2 yamt
391 1.2 yamt if (ioctl(fd, DIOCKLABEL, &keep) == -1)
392 1.2 yamt err(1, "%s: keep label", dvname);
393 1.3 darrenr }
394 1.3 darrenr
395 1.3 darrenr
396 1.20 joerg static void
397 1.3 darrenr disk_badsectors(int argc, char *argv[])
398 1.3 darrenr {
399 1.3 darrenr struct disk_badsectors *dbs, *dbs2, buffer[200];
400 1.3 darrenr SLIST_HEAD(, disk_badsectors) dbstop;
401 1.3 darrenr struct disk_badsecinfo dbsi;
402 1.3 darrenr daddr_t blk, totbad, bad;
403 1.5 dsl u_int32_t count;
404 1.3 darrenr struct stat sb;
405 1.3 darrenr u_char *block;
406 1.6 martin time_t tm;
407 1.3 darrenr
408 1.3 darrenr if (argc != 1)
409 1.3 darrenr usage();
410 1.3 darrenr
411 1.3 darrenr if (strcmp(argv[0], "list") == 0) {
412 1.3 darrenr /*
413 1.3 darrenr * Copy the list of kernel bad sectors out in chunks that fit
414 1.3 darrenr * into buffer[]. Updating dbsi_skip means we don't sit here
415 1.3 darrenr * forever only getting the first chunk that fit in buffer[].
416 1.3 darrenr */
417 1.3 darrenr dbsi.dbsi_buffer = (caddr_t)buffer;
418 1.3 darrenr dbsi.dbsi_bufsize = sizeof(buffer);
419 1.3 darrenr dbsi.dbsi_skip = 0;
420 1.3 darrenr dbsi.dbsi_copied = 0;
421 1.3 darrenr dbsi.dbsi_left = 0;
422 1.3 darrenr
423 1.3 darrenr do {
424 1.3 darrenr if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
425 1.3 darrenr err(1, "%s: badsectors list", dvname);
426 1.3 darrenr
427 1.3 darrenr dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
428 1.3 darrenr for (count = dbsi.dbsi_copied; count > 0; count--) {
429 1.6 martin tm = dbs->dbs_failedat.tv_sec;
430 1.5 dsl printf("%s: blocks %" PRIdaddr " - %" PRIdaddr " failed at %s",
431 1.3 darrenr dvname, dbs->dbs_min, dbs->dbs_max,
432 1.6 martin ctime(&tm));
433 1.4 darrenr dbs++;
434 1.3 darrenr }
435 1.3 darrenr dbsi.dbsi_skip += dbsi.dbsi_copied;
436 1.3 darrenr } while (dbsi.dbsi_left != 0);
437 1.3 darrenr
438 1.3 darrenr } else if (strcmp(argv[0], "flush") == 0) {
439 1.3 darrenr if (ioctl(fd, DIOCBSFLUSH) == -1)
440 1.3 darrenr err(1, "%s: badsectors flush", dvname);
441 1.3 darrenr
442 1.3 darrenr } else if (strcmp(argv[0], "retry") == 0) {
443 1.3 darrenr /*
444 1.3 darrenr * Enforce use of raw device here because the block device
445 1.3 darrenr * causes access to blocks to be clustered in a larger group,
446 1.3 darrenr * making it impossible to determine which individual sectors
447 1.3 darrenr * are the cause of a problem.
448 1.3 darrenr */
449 1.3 darrenr if (fstat(fd, &sb) == -1)
450 1.3 darrenr err(1, "fstat");
451 1.3 darrenr
452 1.3 darrenr if (!S_ISCHR(sb.st_mode)) {
453 1.3 darrenr fprintf(stderr, "'badsector retry' must be used %s\n",
454 1.3 darrenr "with character device");
455 1.3 darrenr exit(1);
456 1.3 darrenr }
457 1.3 darrenr
458 1.3 darrenr SLIST_INIT(&dbstop);
459 1.3 darrenr
460 1.3 darrenr /*
461 1.3 darrenr * Build up a copy of the in-kernel list in a number of stages.
462 1.3 darrenr * That the list we build up here is in the reverse order to
463 1.3 darrenr * the kernel's is of no concern.
464 1.3 darrenr */
465 1.3 darrenr dbsi.dbsi_buffer = (caddr_t)buffer;
466 1.3 darrenr dbsi.dbsi_bufsize = sizeof(buffer);
467 1.3 darrenr dbsi.dbsi_skip = 0;
468 1.3 darrenr dbsi.dbsi_copied = 0;
469 1.3 darrenr dbsi.dbsi_left = 0;
470 1.3 darrenr
471 1.3 darrenr do {
472 1.3 darrenr if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
473 1.3 darrenr err(1, "%s: badsectors list", dvname);
474 1.3 darrenr
475 1.3 darrenr dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
476 1.3 darrenr for (count = dbsi.dbsi_copied; count > 0; count--) {
477 1.14 dsl dbs2 = malloc(sizeof *dbs2);
478 1.14 dsl if (dbs2 == NULL)
479 1.13 rumble err(1, NULL);
480 1.3 darrenr *dbs2 = *dbs;
481 1.3 darrenr SLIST_INSERT_HEAD(&dbstop, dbs2, dbs_next);
482 1.4 darrenr dbs++;
483 1.3 darrenr }
484 1.3 darrenr dbsi.dbsi_skip += dbsi.dbsi_copied;
485 1.3 darrenr } while (dbsi.dbsi_left != 0);
486 1.3 darrenr
487 1.3 darrenr /*
488 1.3 darrenr * Just calculate and print out something that will hopefully
489 1.3 darrenr * provide some useful information about what's going to take
490 1.3 darrenr * place next (if anything.)
491 1.3 darrenr */
492 1.3 darrenr bad = 0;
493 1.3 darrenr totbad = 0;
494 1.13 rumble if ((block = calloc(1, DEV_BSIZE)) == NULL)
495 1.13 rumble err(1, NULL);
496 1.3 darrenr SLIST_FOREACH(dbs, &dbstop, dbs_next) {
497 1.3 darrenr bad++;
498 1.3 darrenr totbad += dbs->dbs_max - dbs->dbs_min + 1;
499 1.3 darrenr }
500 1.3 darrenr
501 1.3 darrenr printf("%s: bad sector clusters %"PRIdaddr
502 1.3 darrenr " total sectors %"PRIdaddr"\n", dvname, bad, totbad);
503 1.3 darrenr
504 1.3 darrenr /*
505 1.3 darrenr * Clear out the kernel's list of bad sectors, ready for us
506 1.3 darrenr * to test all those it thought were bad.
507 1.3 darrenr */
508 1.3 darrenr if (ioctl(fd, DIOCBSFLUSH) == -1)
509 1.3 darrenr err(1, "%s: badsectors flush", dvname);
510 1.3 darrenr
511 1.3 darrenr printf("%s: bad sectors flushed\n", dvname);
512 1.3 darrenr
513 1.3 darrenr /*
514 1.3 darrenr * For each entry we obtained from the kernel, retry each
515 1.3 darrenr * individual sector recorded as bad by seeking to it and
516 1.3 darrenr * attempting to read it in. Print out a line item for each
517 1.3 darrenr * bad block we verify.
518 1.3 darrenr *
519 1.3 darrenr * PRIdaddr is used here because the type of dbs_max is daddr_t
520 1.3 darrenr * and that may be either a 32bit or 64bit number(!)
521 1.3 darrenr */
522 1.3 darrenr SLIST_FOREACH(dbs, &dbstop, dbs_next) {
523 1.3 darrenr printf("%s: Retrying %"PRIdaddr" - %"
524 1.3 darrenr PRIdaddr"\n", dvname, dbs->dbs_min, dbs->dbs_max);
525 1.3 darrenr
526 1.3 darrenr for (blk = dbs->dbs_min; blk <= dbs->dbs_max; blk++) {
527 1.3 darrenr if (lseek(fd, (off_t)blk * DEV_BSIZE,
528 1.3 darrenr SEEK_SET) == -1) {
529 1.5 dsl warn("%s: lseek block %" PRIdaddr "",
530 1.5 dsl dvname, blk);
531 1.3 darrenr continue;
532 1.3 darrenr }
533 1.3 darrenr printf("%s: block %"PRIdaddr" - ", dvname, blk);
534 1.3 darrenr if (read(fd, block, DEV_BSIZE) != DEV_BSIZE)
535 1.3 darrenr printf("failed\n");
536 1.3 darrenr else
537 1.3 darrenr printf("ok\n");
538 1.3 darrenr fflush(stdout);
539 1.3 darrenr }
540 1.3 darrenr }
541 1.3 darrenr }
542 1.2 yamt }
543 1.2 yamt
544 1.20 joerg static void
545 1.9 thorpej disk_addwedge(int argc, char *argv[])
546 1.9 thorpej {
547 1.9 thorpej struct dkwedge_info dkw;
548 1.9 thorpej char *cp;
549 1.9 thorpej daddr_t start;
550 1.9 thorpej uint64_t size;
551 1.9 thorpej
552 1.9 thorpej if (argc != 4)
553 1.9 thorpej usage();
554 1.9 thorpej
555 1.19 dholland /* XXX Unicode: dkw_wname is supposed to be utf-8 */
556 1.19 dholland if (strlcpy((char *)dkw.dkw_wname, argv[0], sizeof(dkw.dkw_wname)) >=
557 1.16 christos sizeof(dkw.dkw_wname))
558 1.9 thorpej errx(1, "Wedge name too long; max %zd characters",
559 1.9 thorpej sizeof(dkw.dkw_wname) - 1);
560 1.9 thorpej
561 1.15 elad if (strlcpy(dkw.dkw_ptype, argv[3], sizeof(dkw.dkw_ptype)) >=
562 1.15 elad sizeof(dkw.dkw_ptype))
563 1.9 thorpej errx(1, "Wedge partition type too long; max %zd characters",
564 1.9 thorpej sizeof(dkw.dkw_ptype) - 1);
565 1.9 thorpej
566 1.9 thorpej errno = 0;
567 1.9 thorpej start = strtoll(argv[1], &cp, 0);
568 1.9 thorpej if (*cp != '\0')
569 1.9 thorpej errx(1, "Invalid start block: %s", argv[1]);
570 1.9 thorpej if (errno == ERANGE && (start == LLONG_MAX ||
571 1.9 thorpej start == LLONG_MIN))
572 1.9 thorpej errx(1, "Start block out of range.");
573 1.9 thorpej if (start < 0)
574 1.9 thorpej errx(1, "Start block must be >= 0.");
575 1.9 thorpej
576 1.9 thorpej errno = 0;
577 1.9 thorpej size = strtoull(argv[2], &cp, 0);
578 1.9 thorpej if (*cp != '\0')
579 1.9 thorpej errx(1, "Invalid block count: %s", argv[2]);
580 1.9 thorpej if (errno == ERANGE && (size == ULLONG_MAX))
581 1.9 thorpej errx(1, "Block count out of range.");
582 1.9 thorpej
583 1.9 thorpej dkw.dkw_offset = start;
584 1.9 thorpej dkw.dkw_size = size;
585 1.9 thorpej
586 1.9 thorpej if (ioctl(fd, DIOCAWEDGE, &dkw) == -1)
587 1.9 thorpej err(1, "%s: addwedge", dvname);
588 1.18 spz else
589 1.18 spz printf("%s created successfully.\n", dkw.dkw_devname);
590 1.18 spz
591 1.9 thorpej }
592 1.9 thorpej
593 1.20 joerg static void
594 1.9 thorpej disk_delwedge(int argc, char *argv[])
595 1.9 thorpej {
596 1.9 thorpej struct dkwedge_info dkw;
597 1.9 thorpej
598 1.9 thorpej if (argc != 1)
599 1.9 thorpej usage();
600 1.9 thorpej
601 1.15 elad if (strlcpy(dkw.dkw_devname, argv[0], sizeof(dkw.dkw_devname)) >=
602 1.15 elad sizeof(dkw.dkw_devname))
603 1.9 thorpej errx(1, "Wedge dk name too long; max %zd characters",
604 1.9 thorpej sizeof(dkw.dkw_devname) - 1);
605 1.9 thorpej
606 1.9 thorpej if (ioctl(fd, DIOCDWEDGE, &dkw) == -1)
607 1.9 thorpej err(1, "%s: delwedge", dvname);
608 1.9 thorpej }
609 1.9 thorpej
610 1.20 joerg static void
611 1.9 thorpej disk_getwedgeinfo(int argc, char *argv[])
612 1.9 thorpej {
613 1.9 thorpej struct dkwedge_info dkw;
614 1.9 thorpej
615 1.9 thorpej if (argc != 0)
616 1.9 thorpej usage();
617 1.9 thorpej
618 1.9 thorpej if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1)
619 1.9 thorpej err(1, "%s: getwedgeinfo", dvname);
620 1.9 thorpej
621 1.9 thorpej printf("%s at %s: %s\n", dkw.dkw_devname, dkw.dkw_parent,
622 1.9 thorpej dkw.dkw_wname); /* XXX Unicode */
623 1.10 martin printf("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
624 1.9 thorpej dkw.dkw_devname, dkw.dkw_size, dkw.dkw_offset, dkw.dkw_ptype);
625 1.9 thorpej }
626 1.9 thorpej
627 1.20 joerg static void
628 1.9 thorpej disk_listwedges(int argc, char *argv[])
629 1.9 thorpej {
630 1.9 thorpej struct dkwedge_info *dkw;
631 1.9 thorpej struct dkwedge_list dkwl;
632 1.9 thorpej size_t bufsize;
633 1.9 thorpej u_int i;
634 1.9 thorpej
635 1.9 thorpej if (argc != 0)
636 1.9 thorpej usage();
637 1.9 thorpej
638 1.9 thorpej dkw = NULL;
639 1.9 thorpej dkwl.dkwl_buf = dkw;
640 1.9 thorpej dkwl.dkwl_bufsize = 0;
641 1.9 thorpej
642 1.9 thorpej for (;;) {
643 1.9 thorpej if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
644 1.9 thorpej err(1, "%s: listwedges", dvname);
645 1.9 thorpej if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
646 1.9 thorpej break;
647 1.9 thorpej bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
648 1.9 thorpej if (dkwl.dkwl_bufsize < bufsize) {
649 1.9 thorpej dkw = realloc(dkwl.dkwl_buf, bufsize);
650 1.9 thorpej if (dkw == NULL)
651 1.9 thorpej errx(1, "%s: listwedges: unable to "
652 1.9 thorpej "allocate wedge info buffer", dvname);
653 1.9 thorpej dkwl.dkwl_buf = dkw;
654 1.9 thorpej dkwl.dkwl_bufsize = bufsize;
655 1.9 thorpej }
656 1.9 thorpej }
657 1.9 thorpej
658 1.9 thorpej if (dkwl.dkwl_nwedges == 0) {
659 1.9 thorpej printf("%s: no wedges configured\n", dvname);
660 1.9 thorpej return;
661 1.9 thorpej }
662 1.9 thorpej
663 1.17 uebayasi qsort(dkw, dkwl.dkwl_nwedges, sizeof(*dkw), dkw_sort);
664 1.17 uebayasi
665 1.9 thorpej printf("%s: %u wedge%s:\n", dvname, dkwl.dkwl_nwedges,
666 1.9 thorpej dkwl.dkwl_nwedges == 1 ? "" : "s");
667 1.9 thorpej for (i = 0; i < dkwl.dkwl_nwedges; i++) {
668 1.10 martin printf("%s: %s, %"PRIu64" blocks at %"PRId64", type: %s\n",
669 1.9 thorpej dkw[i].dkw_devname,
670 1.9 thorpej dkw[i].dkw_wname, /* XXX Unicode */
671 1.9 thorpej dkw[i].dkw_size, dkw[i].dkw_offset, dkw[i].dkw_ptype);
672 1.9 thorpej }
673 1.9 thorpej }
674 1.9 thorpej
675 1.21 mlelstv static void
676 1.21 mlelstv disk_makewedges(int argc, char *argv[])
677 1.21 mlelstv {
678 1.21 mlelstv int bits;
679 1.21 mlelstv
680 1.21 mlelstv if (argc != 0)
681 1.21 mlelstv usage();
682 1.21 mlelstv
683 1.21 mlelstv if (ioctl(fd, DIOCMWEDGES, &bits) == -1)
684 1.21 mlelstv err(1, "%s: makewedges", dvname);
685 1.21 mlelstv else
686 1.21 mlelstv printf("successfully scanned %s.\n", dvname);
687 1.21 mlelstv }
688 1.21 mlelstv
689 1.20 joerg static int
690 1.17 uebayasi dkw_sort(const void *a, const void *b)
691 1.17 uebayasi {
692 1.17 uebayasi const struct dkwedge_info *dkwa = a, *dkwb = b;
693 1.17 uebayasi const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
694 1.17 uebayasi
695 1.17 uebayasi return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
696 1.17 uebayasi }
697 1.17 uebayasi
698 1.2 yamt /*
699 1.2 yamt * return YES, NO or -1.
700 1.2 yamt */
701 1.20 joerg static int
702 1.2 yamt yesno(const char *p)
703 1.2 yamt {
704 1.2 yamt
705 1.2 yamt if (!strcmp(p, YES_STR))
706 1.2 yamt return YES;
707 1.2 yamt if (!strcmp(p, NO_STR))
708 1.2 yamt return NO;
709 1.2 yamt return -1;
710 1.1 thorpej }
711