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