dkctl.c revision 1.6 1 1.6 martin /* $NetBSD: dkctl.c,v 1.6 2003/04/16 13:32:58 martin 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.1 thorpej
42 1.1 thorpej #include <sys/param.h>
43 1.1 thorpej #include <sys/ioctl.h>
44 1.1 thorpej #include <sys/dkio.h>
45 1.3 darrenr #include <sys/disk.h>
46 1.3 darrenr #include <sys/queue.h>
47 1.1 thorpej #include <err.h>
48 1.1 thorpej #include <errno.h>
49 1.1 thorpej #include <fcntl.h>
50 1.1 thorpej #include <stdio.h>
51 1.1 thorpej #include <stdlib.h>
52 1.1 thorpej #include <string.h>
53 1.1 thorpej #include <unistd.h>
54 1.1 thorpej #include <util.h>
55 1.1 thorpej
56 1.2 yamt #define YES 1
57 1.2 yamt #define NO 0
58 1.2 yamt
59 1.2 yamt /* I don't think nl_langinfo is suitable in this case */
60 1.2 yamt #define YES_STR "yes"
61 1.2 yamt #define NO_STR "no"
62 1.2 yamt #define YESNO_ARG YES_STR " | " NO_STR
63 1.2 yamt
64 1.3 darrenr #ifndef PRIdaddr
65 1.3 darrenr #define PRIdaddr PRId64
66 1.3 darrenr #endif
67 1.3 darrenr
68 1.1 thorpej struct command {
69 1.1 thorpej const char *cmd_name;
70 1.1 thorpej const char *arg_names;
71 1.1 thorpej void (*cmd_func)(int, char *[]);
72 1.1 thorpej int open_flags;
73 1.1 thorpej };
74 1.1 thorpej
75 1.1 thorpej int main(int, char *[]);
76 1.1 thorpej void usage(void);
77 1.1 thorpej
78 1.1 thorpej int fd; /* file descriptor for device */
79 1.1 thorpej const char *dvname; /* device name */
80 1.1 thorpej char dvname_store[MAXPATHLEN]; /* for opendisk(3) */
81 1.1 thorpej const char *cmdname; /* command user issued */
82 1.1 thorpej const char *argnames; /* helpstring; expected arguments */
83 1.1 thorpej
84 1.2 yamt int yesno(const char *);
85 1.2 yamt
86 1.1 thorpej void disk_getcache(int, char *[]);
87 1.1 thorpej void disk_setcache(int, char *[]);
88 1.1 thorpej void disk_synccache(int, char *[]);
89 1.2 yamt void disk_keeplabel(int, char *[]);
90 1.3 darrenr void disk_badsectors(int, char *[]);
91 1.1 thorpej
92 1.1 thorpej struct command commands[] = {
93 1.1 thorpej { "getcache",
94 1.1 thorpej "",
95 1.1 thorpej disk_getcache,
96 1.1 thorpej O_RDONLY },
97 1.1 thorpej
98 1.1 thorpej { "setcache",
99 1.1 thorpej "none | r | w | rw [save]",
100 1.1 thorpej disk_setcache,
101 1.1 thorpej O_RDWR },
102 1.1 thorpej
103 1.1 thorpej { "synccache",
104 1.1 thorpej "[force]",
105 1.1 thorpej disk_synccache,
106 1.1 thorpej O_RDWR },
107 1.1 thorpej
108 1.2 yamt { "keeplabel",
109 1.2 yamt YESNO_ARG,
110 1.2 yamt disk_keeplabel,
111 1.2 yamt O_RDWR },
112 1.2 yamt
113 1.3 darrenr { "badsector",
114 1.3 darrenr "flush | list | retry",
115 1.3 darrenr disk_badsectors,
116 1.3 darrenr O_RDWR },
117 1.3 darrenr
118 1.1 thorpej { NULL,
119 1.1 thorpej NULL,
120 1.1 thorpej NULL,
121 1.1 thorpej 0 },
122 1.1 thorpej };
123 1.1 thorpej
124 1.1 thorpej int
125 1.1 thorpej main(int argc, char *argv[])
126 1.1 thorpej {
127 1.1 thorpej int i;
128 1.1 thorpej
129 1.1 thorpej /* Must have at least: device command */
130 1.1 thorpej if (argc < 3)
131 1.1 thorpej usage();
132 1.1 thorpej
133 1.1 thorpej /* Skip program name, get and skip device name and command. */
134 1.1 thorpej dvname = argv[1];
135 1.1 thorpej cmdname = argv[2];
136 1.1 thorpej argv += 3;
137 1.1 thorpej argc -= 3;
138 1.1 thorpej
139 1.1 thorpej /* Look up and call the command. */
140 1.1 thorpej for (i = 0; commands[i].cmd_name != NULL; i++)
141 1.1 thorpej if (strcmp(cmdname, commands[i].cmd_name) == 0)
142 1.1 thorpej break;
143 1.1 thorpej if (commands[i].cmd_name == NULL)
144 1.1 thorpej errx(1, "unknown command: %s", cmdname);
145 1.1 thorpej
146 1.1 thorpej argnames = commands[i].arg_names;
147 1.1 thorpej
148 1.1 thorpej /* Open the device. */
149 1.1 thorpej fd = opendisk(dvname, commands[i].open_flags, dvname_store,
150 1.1 thorpej sizeof(dvname_store), 0);
151 1.1 thorpej if (fd == -1)
152 1.1 thorpej err(1, "%s", dvname);
153 1.1 thorpej
154 1.1 thorpej dvname = dvname_store;
155 1.1 thorpej
156 1.1 thorpej (*commands[i].cmd_func)(argc, argv);
157 1.1 thorpej exit(0);
158 1.1 thorpej }
159 1.1 thorpej
160 1.1 thorpej void
161 1.1 thorpej usage()
162 1.1 thorpej {
163 1.1 thorpej int i;
164 1.1 thorpej
165 1.1 thorpej fprintf(stderr, "Usage: %s device command [arg [...]]\n",
166 1.1 thorpej getprogname());
167 1.1 thorpej
168 1.1 thorpej fprintf(stderr, " Available commands:\n");
169 1.1 thorpej for (i = 0; commands[i].cmd_name != NULL; i++)
170 1.1 thorpej fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
171 1.1 thorpej commands[i].arg_names);
172 1.1 thorpej
173 1.1 thorpej exit(1);
174 1.1 thorpej }
175 1.1 thorpej
176 1.1 thorpej void
177 1.1 thorpej disk_getcache(int argc, char *argv[])
178 1.1 thorpej {
179 1.1 thorpej int bits;
180 1.1 thorpej
181 1.1 thorpej if (ioctl(fd, DIOCGCACHE, &bits) == -1)
182 1.1 thorpej err(1, "%s: getcache", dvname);
183 1.1 thorpej
184 1.1 thorpej if ((bits & (DKCACHE_READ|DKCACHE_WRITE)) == 0)
185 1.1 thorpej printf("%s: No caches enabled\n", dvname);
186 1.1 thorpej else {
187 1.1 thorpej if (bits & DKCACHE_READ)
188 1.1 thorpej printf("%s: read cache enabled\n", dvname);
189 1.1 thorpej if (bits & DKCACHE_WRITE)
190 1.1 thorpej printf("%s: write-back cache enabled\n", dvname);
191 1.1 thorpej }
192 1.1 thorpej
193 1.1 thorpej printf("%s: read cache enable is %schangeable\n", dvname,
194 1.1 thorpej (bits & DKCACHE_RCHANGE) ? "" : "not ");
195 1.1 thorpej printf("%s: write cache enable is %schangeable\n", dvname,
196 1.1 thorpej (bits & DKCACHE_WCHANGE) ? "" : "not ");
197 1.1 thorpej
198 1.1 thorpej printf("%s: cache parameters are %ssavable\n", dvname,
199 1.1 thorpej (bits & DKCACHE_SAVE) ? "" : "not ");
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej void
203 1.1 thorpej disk_setcache(int argc, char *argv[])
204 1.1 thorpej {
205 1.1 thorpej int bits;
206 1.1 thorpej
207 1.1 thorpej if (argc > 2 || argc == 0)
208 1.1 thorpej usage();
209 1.1 thorpej
210 1.1 thorpej if (strcmp(argv[0], "none") == 0)
211 1.1 thorpej bits = 0;
212 1.1 thorpej else if (strcmp(argv[0], "r") == 0)
213 1.1 thorpej bits = DKCACHE_READ;
214 1.1 thorpej else if (strcmp(argv[0], "w") == 0)
215 1.1 thorpej bits = DKCACHE_WRITE;
216 1.1 thorpej else if (strcmp(argv[0], "rw") == 0)
217 1.1 thorpej bits = DKCACHE_READ|DKCACHE_WRITE;
218 1.1 thorpej else
219 1.1 thorpej usage();
220 1.1 thorpej
221 1.1 thorpej if (argc == 2) {
222 1.1 thorpej if (strcmp(argv[1], "save") == 0)
223 1.1 thorpej bits |= DKCACHE_SAVE;
224 1.1 thorpej else
225 1.1 thorpej usage();
226 1.1 thorpej }
227 1.1 thorpej
228 1.1 thorpej if (ioctl(fd, DIOCSCACHE, &bits) == -1)
229 1.1 thorpej err(1, "%s: setcache", dvname);
230 1.1 thorpej }
231 1.1 thorpej
232 1.1 thorpej void
233 1.1 thorpej disk_synccache(int argc, char *argv[])
234 1.1 thorpej {
235 1.1 thorpej int force;
236 1.1 thorpej
237 1.1 thorpej switch (argc) {
238 1.1 thorpej case 0:
239 1.1 thorpej force = 0;
240 1.1 thorpej break;
241 1.1 thorpej
242 1.1 thorpej case 1:
243 1.1 thorpej if (strcmp(argv[0], "force") == 0)
244 1.1 thorpej force = 1;
245 1.1 thorpej else
246 1.1 thorpej usage();
247 1.1 thorpej break;
248 1.1 thorpej
249 1.1 thorpej default:
250 1.1 thorpej usage();
251 1.1 thorpej }
252 1.1 thorpej
253 1.1 thorpej if (ioctl(fd, DIOCCACHESYNC, &force) == -1)
254 1.1 thorpej err(1, "%s: sync cache", dvname);
255 1.2 yamt }
256 1.2 yamt
257 1.2 yamt void
258 1.2 yamt disk_keeplabel(int argc, char *argv[])
259 1.2 yamt {
260 1.2 yamt int keep;
261 1.2 yamt int yn;
262 1.2 yamt
263 1.2 yamt if (argc != 1)
264 1.2 yamt usage();
265 1.2 yamt
266 1.2 yamt yn = yesno(argv[0]);
267 1.2 yamt if (yn < 0)
268 1.2 yamt usage();
269 1.2 yamt
270 1.2 yamt keep = yn == YES;
271 1.2 yamt
272 1.2 yamt if (ioctl(fd, DIOCKLABEL, &keep) == -1)
273 1.2 yamt err(1, "%s: keep label", dvname);
274 1.3 darrenr }
275 1.3 darrenr
276 1.3 darrenr
277 1.3 darrenr void
278 1.3 darrenr disk_badsectors(int argc, char *argv[])
279 1.3 darrenr {
280 1.3 darrenr struct disk_badsectors *dbs, *dbs2, buffer[200];
281 1.3 darrenr SLIST_HEAD(, disk_badsectors) dbstop;
282 1.3 darrenr struct disk_badsecinfo dbsi;
283 1.3 darrenr daddr_t blk, totbad, bad;
284 1.5 dsl u_int32_t count;
285 1.3 darrenr struct stat sb;
286 1.3 darrenr u_char *block;
287 1.6 martin time_t tm;
288 1.3 darrenr
289 1.3 darrenr if (argc != 1)
290 1.3 darrenr usage();
291 1.3 darrenr
292 1.3 darrenr if (strcmp(argv[0], "list") == 0) {
293 1.3 darrenr /*
294 1.3 darrenr * Copy the list of kernel bad sectors out in chunks that fit
295 1.3 darrenr * into buffer[]. Updating dbsi_skip means we don't sit here
296 1.3 darrenr * forever only getting the first chunk that fit in buffer[].
297 1.3 darrenr */
298 1.3 darrenr dbsi.dbsi_buffer = (caddr_t)buffer;
299 1.3 darrenr dbsi.dbsi_bufsize = sizeof(buffer);
300 1.3 darrenr dbsi.dbsi_skip = 0;
301 1.3 darrenr dbsi.dbsi_copied = 0;
302 1.3 darrenr dbsi.dbsi_left = 0;
303 1.3 darrenr
304 1.3 darrenr do {
305 1.3 darrenr if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
306 1.3 darrenr err(1, "%s: badsectors list", dvname);
307 1.3 darrenr
308 1.3 darrenr dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
309 1.3 darrenr for (count = dbsi.dbsi_copied; count > 0; count--) {
310 1.6 martin tm = dbs->dbs_failedat.tv_sec;
311 1.5 dsl printf("%s: blocks %" PRIdaddr " - %" PRIdaddr " failed at %s",
312 1.3 darrenr dvname, dbs->dbs_min, dbs->dbs_max,
313 1.6 martin ctime(&tm));
314 1.4 darrenr dbs++;
315 1.3 darrenr }
316 1.3 darrenr dbsi.dbsi_skip += dbsi.dbsi_copied;
317 1.3 darrenr } while (dbsi.dbsi_left != 0);
318 1.3 darrenr
319 1.3 darrenr } else if (strcmp(argv[0], "flush") == 0) {
320 1.3 darrenr if (ioctl(fd, DIOCBSFLUSH) == -1)
321 1.3 darrenr err(1, "%s: badsectors flush", dvname);
322 1.3 darrenr
323 1.3 darrenr } else if (strcmp(argv[0], "retry") == 0) {
324 1.3 darrenr /*
325 1.3 darrenr * Enforce use of raw device here because the block device
326 1.3 darrenr * causes access to blocks to be clustered in a larger group,
327 1.3 darrenr * making it impossible to determine which individual sectors
328 1.3 darrenr * are the cause of a problem.
329 1.3 darrenr */
330 1.3 darrenr if (fstat(fd, &sb) == -1)
331 1.3 darrenr err(1, "fstat");
332 1.3 darrenr
333 1.3 darrenr if (!S_ISCHR(sb.st_mode)) {
334 1.3 darrenr fprintf(stderr, "'badsector retry' must be used %s\n",
335 1.3 darrenr "with character device");
336 1.3 darrenr exit(1);
337 1.3 darrenr }
338 1.3 darrenr
339 1.3 darrenr SLIST_INIT(&dbstop);
340 1.3 darrenr
341 1.3 darrenr /*
342 1.3 darrenr * Build up a copy of the in-kernel list in a number of stages.
343 1.3 darrenr * That the list we build up here is in the reverse order to
344 1.3 darrenr * the kernel's is of no concern.
345 1.3 darrenr */
346 1.3 darrenr dbsi.dbsi_buffer = (caddr_t)buffer;
347 1.3 darrenr dbsi.dbsi_bufsize = sizeof(buffer);
348 1.3 darrenr dbsi.dbsi_skip = 0;
349 1.3 darrenr dbsi.dbsi_copied = 0;
350 1.3 darrenr dbsi.dbsi_left = 0;
351 1.3 darrenr
352 1.3 darrenr do {
353 1.3 darrenr if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
354 1.3 darrenr err(1, "%s: badsectors list", dvname);
355 1.3 darrenr
356 1.3 darrenr dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
357 1.3 darrenr for (count = dbsi.dbsi_copied; count > 0; count--) {
358 1.3 darrenr dbs2 = malloc(sizeof(*dbs2));
359 1.3 darrenr *dbs2 = *dbs;
360 1.3 darrenr SLIST_INSERT_HEAD(&dbstop, dbs2, dbs_next);
361 1.4 darrenr dbs++;
362 1.3 darrenr }
363 1.3 darrenr dbsi.dbsi_skip += dbsi.dbsi_copied;
364 1.3 darrenr } while (dbsi.dbsi_left != 0);
365 1.3 darrenr
366 1.3 darrenr /*
367 1.3 darrenr * Just calculate and print out something that will hopefully
368 1.3 darrenr * provide some useful information about what's going to take
369 1.3 darrenr * place next (if anything.)
370 1.3 darrenr */
371 1.3 darrenr bad = 0;
372 1.3 darrenr totbad = 0;
373 1.3 darrenr block = calloc(1, DEV_BSIZE);
374 1.3 darrenr SLIST_FOREACH(dbs, &dbstop, dbs_next) {
375 1.3 darrenr bad++;
376 1.3 darrenr totbad += dbs->dbs_max - dbs->dbs_min + 1;
377 1.3 darrenr }
378 1.3 darrenr
379 1.3 darrenr printf("%s: bad sector clusters %"PRIdaddr
380 1.3 darrenr " total sectors %"PRIdaddr"\n", dvname, bad, totbad);
381 1.3 darrenr
382 1.3 darrenr /*
383 1.3 darrenr * Clear out the kernel's list of bad sectors, ready for us
384 1.3 darrenr * to test all those it thought were bad.
385 1.3 darrenr */
386 1.3 darrenr if (ioctl(fd, DIOCBSFLUSH) == -1)
387 1.3 darrenr err(1, "%s: badsectors flush", dvname);
388 1.3 darrenr
389 1.3 darrenr printf("%s: bad sectors flushed\n", dvname);
390 1.3 darrenr
391 1.3 darrenr /*
392 1.3 darrenr * For each entry we obtained from the kernel, retry each
393 1.3 darrenr * individual sector recorded as bad by seeking to it and
394 1.3 darrenr * attempting to read it in. Print out a line item for each
395 1.3 darrenr * bad block we verify.
396 1.3 darrenr *
397 1.3 darrenr * PRIdaddr is used here because the type of dbs_max is daddr_t
398 1.3 darrenr * and that may be either a 32bit or 64bit number(!)
399 1.3 darrenr */
400 1.3 darrenr SLIST_FOREACH(dbs, &dbstop, dbs_next) {
401 1.3 darrenr printf("%s: Retrying %"PRIdaddr" - %"
402 1.3 darrenr PRIdaddr"\n", dvname, dbs->dbs_min, dbs->dbs_max);
403 1.3 darrenr
404 1.3 darrenr for (blk = dbs->dbs_min; blk <= dbs->dbs_max; blk++) {
405 1.3 darrenr if (lseek(fd, (off_t)blk * DEV_BSIZE,
406 1.3 darrenr SEEK_SET) == -1) {
407 1.5 dsl warn("%s: lseek block %" PRIdaddr "",
408 1.5 dsl dvname, blk);
409 1.3 darrenr continue;
410 1.3 darrenr }
411 1.3 darrenr printf("%s: block %"PRIdaddr" - ", dvname, blk);
412 1.3 darrenr if (read(fd, block, DEV_BSIZE) != DEV_BSIZE)
413 1.3 darrenr printf("failed\n");
414 1.3 darrenr else
415 1.3 darrenr printf("ok\n");
416 1.3 darrenr fflush(stdout);
417 1.3 darrenr }
418 1.3 darrenr }
419 1.3 darrenr }
420 1.2 yamt }
421 1.2 yamt
422 1.2 yamt /*
423 1.2 yamt * return YES, NO or -1.
424 1.2 yamt */
425 1.2 yamt int
426 1.2 yamt yesno(const char *p)
427 1.2 yamt {
428 1.2 yamt
429 1.2 yamt if (!strcmp(p, YES_STR))
430 1.2 yamt return YES;
431 1.2 yamt if (!strcmp(p, NO_STR))
432 1.2 yamt return NO;
433 1.2 yamt return -1;
434 1.1 thorpej }
435