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