dkctl.c revision 1.5 1 /* $NetBSD: dkctl.c,v 1.5 2003/04/16 08:39:56 dsl 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
288 if (argc != 1)
289 usage();
290
291 if (strcmp(argv[0], "list") == 0) {
292 /*
293 * Copy the list of kernel bad sectors out in chunks that fit
294 * into buffer[]. Updating dbsi_skip means we don't sit here
295 * forever only getting the first chunk that fit in buffer[].
296 */
297 dbsi.dbsi_buffer = (caddr_t)buffer;
298 dbsi.dbsi_bufsize = sizeof(buffer);
299 dbsi.dbsi_skip = 0;
300 dbsi.dbsi_copied = 0;
301 dbsi.dbsi_left = 0;
302
303 do {
304 if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
305 err(1, "%s: badsectors list", dvname);
306
307 dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
308 for (count = dbsi.dbsi_copied; count > 0; count--) {
309 printf("%s: blocks %" PRIdaddr " - %" PRIdaddr " failed at %s",
310 dvname, dbs->dbs_min, dbs->dbs_max,
311 ctime(&dbs->dbs_failedat.tv_sec));
312 dbs++;
313 }
314 dbsi.dbsi_skip += dbsi.dbsi_copied;
315 } while (dbsi.dbsi_left != 0);
316
317 } else if (strcmp(argv[0], "flush") == 0) {
318 if (ioctl(fd, DIOCBSFLUSH) == -1)
319 err(1, "%s: badsectors flush", dvname);
320
321 } else if (strcmp(argv[0], "retry") == 0) {
322 /*
323 * Enforce use of raw device here because the block device
324 * causes access to blocks to be clustered in a larger group,
325 * making it impossible to determine which individual sectors
326 * are the cause of a problem.
327 */
328 if (fstat(fd, &sb) == -1)
329 err(1, "fstat");
330
331 if (!S_ISCHR(sb.st_mode)) {
332 fprintf(stderr, "'badsector retry' must be used %s\n",
333 "with character device");
334 exit(1);
335 }
336
337 SLIST_INIT(&dbstop);
338
339 /*
340 * Build up a copy of the in-kernel list in a number of stages.
341 * That the list we build up here is in the reverse order to
342 * the kernel's is of no concern.
343 */
344 dbsi.dbsi_buffer = (caddr_t)buffer;
345 dbsi.dbsi_bufsize = sizeof(buffer);
346 dbsi.dbsi_skip = 0;
347 dbsi.dbsi_copied = 0;
348 dbsi.dbsi_left = 0;
349
350 do {
351 if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
352 err(1, "%s: badsectors list", dvname);
353
354 dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
355 for (count = dbsi.dbsi_copied; count > 0; count--) {
356 dbs2 = malloc(sizeof(*dbs2));
357 *dbs2 = *dbs;
358 SLIST_INSERT_HEAD(&dbstop, dbs2, dbs_next);
359 dbs++;
360 }
361 dbsi.dbsi_skip += dbsi.dbsi_copied;
362 } while (dbsi.dbsi_left != 0);
363
364 /*
365 * Just calculate and print out something that will hopefully
366 * provide some useful information about what's going to take
367 * place next (if anything.)
368 */
369 bad = 0;
370 totbad = 0;
371 block = calloc(1, DEV_BSIZE);
372 SLIST_FOREACH(dbs, &dbstop, dbs_next) {
373 bad++;
374 totbad += dbs->dbs_max - dbs->dbs_min + 1;
375 }
376
377 printf("%s: bad sector clusters %"PRIdaddr
378 " total sectors %"PRIdaddr"\n", dvname, bad, totbad);
379
380 /*
381 * Clear out the kernel's list of bad sectors, ready for us
382 * to test all those it thought were bad.
383 */
384 if (ioctl(fd, DIOCBSFLUSH) == -1)
385 err(1, "%s: badsectors flush", dvname);
386
387 printf("%s: bad sectors flushed\n", dvname);
388
389 /*
390 * For each entry we obtained from the kernel, retry each
391 * individual sector recorded as bad by seeking to it and
392 * attempting to read it in. Print out a line item for each
393 * bad block we verify.
394 *
395 * PRIdaddr is used here because the type of dbs_max is daddr_t
396 * and that may be either a 32bit or 64bit number(!)
397 */
398 SLIST_FOREACH(dbs, &dbstop, dbs_next) {
399 printf("%s: Retrying %"PRIdaddr" - %"
400 PRIdaddr"\n", dvname, dbs->dbs_min, dbs->dbs_max);
401
402 for (blk = dbs->dbs_min; blk <= dbs->dbs_max; blk++) {
403 if (lseek(fd, (off_t)blk * DEV_BSIZE,
404 SEEK_SET) == -1) {
405 warn("%s: lseek block %" PRIdaddr "",
406 dvname, blk);
407 continue;
408 }
409 printf("%s: block %"PRIdaddr" - ", dvname, blk);
410 if (read(fd, block, DEV_BSIZE) != DEV_BSIZE)
411 printf("failed\n");
412 else
413 printf("ok\n");
414 fflush(stdout);
415 }
416 }
417 }
418 }
419
420 /*
421 * return YES, NO or -1.
422 */
423 int
424 yesno(const char *p)
425 {
426
427 if (!strcmp(p, YES_STR))
428 return YES;
429 if (!strcmp(p, NO_STR))
430 return NO;
431 return -1;
432 }
433