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