diskscan.c (052b6e8a) diskscan.c (342440ec)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
23 * Use is subject to license terms.
24 */
25
27#pragma ident "%Z%%M% %I% %E% SMI"
28
29/*
30 * diskscan:
31 * performs a verification pass over a device specified on command line;
32 * display progress on stdout, and print bad sector numbers to stderr
33 */
34#include <stdio.h>
35#include <stdlib.h>
36#include <fcntl.h>

--- 7 unchanged lines hidden (view full) ---

44#include <sys/param.h>
45#include <sys/stat.h>
46#include <sys/vtoc.h>
47#include <sys/dkio.h>
48
49static void verexit(); /* signal handler and exit routine */
50static void report(); /* tell user how we're getting on */
51static void scandisk(char *device, int devfd, int writeflag);
26/*
27 * diskscan:
28 * performs a verification pass over a device specified on command line;
29 * display progress on stdout, and print bad sector numbers to stderr
30 */
31#include <stdio.h>
32#include <stdlib.h>
33#include <fcntl.h>

--- 7 unchanged lines hidden (view full) ---

41#include <sys/param.h>
42#include <sys/stat.h>
43#include <sys/vtoc.h>
44#include <sys/dkio.h>
45
46static void verexit(); /* signal handler and exit routine */
47static void report(); /* tell user how we're getting on */
48static void scandisk(char *device, int devfd, int writeflag);
52static void report(char *what, int sector);
49static void report(char *what, diskaddr_t sector);
53static void verexit(int code);
54
55#define TRUE 1
56#define FALSE 0
57#define VER_WRITE 1
58#define VER_READ 2
59
60static char *progname;
61static struct dk_geom dkg; /* physical device boot info */
62static char replybuf[64]; /* used for user replies to questions */
50static void verexit(int code);
51
52#define TRUE 1
53#define FALSE 0
54#define VER_WRITE 1
55#define VER_READ 2
56
57static char *progname;
58static struct dk_geom dkg; /* physical device boot info */
59static char replybuf[64]; /* used for user replies to questions */
63static daddr_t unix_base; /* first sector of UNIX System partition */
64static daddr_t unix_size; /* # sectors in UNIX System partition */
60static diskaddr_t unix_base; /* first sector of UNIX System partition */
61static diskaddr_t unix_size; /* # sectors in UNIX System partition */
65static long numbadrd = 0; /* number of bad sectors on read */
66static long numbadwr = 0; /* number of bad sectors on write */
67static char eol = '\n'; /* end-of-line char (if -n, we set to '\n') */
68static int print_warn = 1; /* should the warning message be printed? */
69static int do_scan = VER_READ;
70
71int
72main(int argc, char *argv[]) {
73 extern int optind;
74 int devfd; /* device file descriptor */
75 struct stat statbuf;
76 struct part_info part_info;
62static long numbadrd = 0; /* number of bad sectors on read */
63static long numbadwr = 0; /* number of bad sectors on write */
64static char eol = '\n'; /* end-of-line char (if -n, we set to '\n') */
65static int print_warn = 1; /* should the warning message be printed? */
66static int do_scan = VER_READ;
67
68int
69main(int argc, char *argv[]) {
70 extern int optind;
71 int devfd; /* device file descriptor */
72 struct stat statbuf;
73 struct part_info part_info;
74 struct extpart_info extpartinfo;
77 int c;
78 int errflag = 0;
79 char *device;
80 progname = argv[0];
81
82 /* Don't buffer stdout - we don't want to see bursts */
83
84 setbuf(stdout, NULL);

--- 51 unchanged lines hidden (view full) ---

136 }
137
138 if ((ioctl(devfd, DKIOCGGEOM, &dkg)) == -1) {
139 (void) fprintf(stderr,
140 "%s: unable to get disk geometry.\n", progname);
141 perror("");
142 exit(9);
143 }
75 int c;
76 int errflag = 0;
77 char *device;
78 progname = argv[0];
79
80 /* Don't buffer stdout - we don't want to see bursts */
81
82 setbuf(stdout, NULL);

--- 51 unchanged lines hidden (view full) ---

134 }
135
136 if ((ioctl(devfd, DKIOCGGEOM, &dkg)) == -1) {
137 (void) fprintf(stderr,
138 "%s: unable to get disk geometry.\n", progname);
139 perror("");
140 exit(9);
141 }
144 if ((ioctl(devfd, DKIOCPARTINFO, &part_info)) == -1) {
145 (void) fprintf(stderr, "%s: unable to get partition info.\n",
146 progname);
147 perror("");
148 exit(9);
142
143 if ((ioctl(devfd, DKIOCEXTPARTINFO, &extpartinfo)) == 0) {
144 unix_base = extpartinfo.p_start;
145 unix_size = extpartinfo.p_length;
146 } else {
147 if ((ioctl(devfd, DKIOCPARTINFO, &part_info)) == 0) {
148 unix_base = (ulong_t)part_info.p_start;
149 unix_size = (uint_t)part_info.p_length;
150 } else {
151 (void) fprintf(stderr, "%s: unable to get partition "
152 "info.\n", progname);
153 perror("");
154 exit(9);
155 }
149 }
150
156 }
157
151 unix_base = part_info.p_start;
152 unix_size = part_info.p_length;
153 scandisk(device, devfd, do_scan);
154 return (0);
155}
156
157/*
158 * scandisk:
159 * attempt to read every sector of the drive;
160 * display bad sectors found on stderr
161 */
162
163static void
164scandisk(char *device, int devfd, int writeflag)
165{
166 int trksiz = NBPSCTR * dkg.dkg_nsect;
167 char *verbuf;
158 scandisk(device, devfd, do_scan);
159 return (0);
160}
161
162/*
163 * scandisk:
164 * attempt to read every sector of the drive;
165 * display bad sectors found on stderr
166 */
167
168static void
169scandisk(char *device, int devfd, int writeflag)
170{
171 int trksiz = NBPSCTR * dkg.dkg_nsect;
172 char *verbuf;
168 daddr_t cursec;
173 diskaddr_t cursec;
169 int cylsiz = dkg.dkg_nsect * dkg.dkg_nhead;
170 int i;
171 char *rptr;
174 int cylsiz = dkg.dkg_nsect * dkg.dkg_nhead;
175 int i;
176 char *rptr;
172 long tmpend = 0;
173 long tmpsec = 0;
177 diskaddr_t tmpend = 0;
178 diskaddr_t tmpsec = 0;
174
175/* #define LIBMALLOC */
176
177#ifdef LIBMALLOC
178
179 extern int mallopt();
180
181 /* This adds 5k to the binary, but it's a lot prettier */

--- 46 unchanged lines hidden (view full) ---

228 (void) printf("Do you want to continue (y/n)? ");
229
230 rptr = fgets(replybuf, 64*sizeof (char), stdin);
231 if (!rptr || !((replybuf[0] == 'Y') || (replybuf[0] == 'y')))
232 exit(10);
233 }
234
235 for (cursec = 0; cursec < unix_size; cursec += dkg.dkg_nsect) {
179
180/* #define LIBMALLOC */
181
182#ifdef LIBMALLOC
183
184 extern int mallopt();
185
186 /* This adds 5k to the binary, but it's a lot prettier */

--- 46 unchanged lines hidden (view full) ---

233 (void) printf("Do you want to continue (y/n)? ");
234
235 rptr = fgets(replybuf, 64*sizeof (char), stdin);
236 if (!rptr || !((replybuf[0] == 'Y') || (replybuf[0] == 'y')))
237 exit(10);
238 }
239
240 for (cursec = 0; cursec < unix_size; cursec += dkg.dkg_nsect) {
236 if (lseek(devfd, (long)cursec * NBPSCTR, 0) == -1) {
241 if (llseek(devfd, cursec * NBPSCTR, 0) == -1) {
237 (void) fprintf(stderr,
242 (void) fprintf(stderr,
238 "Error seeking sector %ld Cylinder %ld\n",
239 cursec, cursec / cylsiz);
243 "Error seeking sector %llu Cylinder %llu\n",
244 cursec, cursec / cylsiz);
240 verexit(1);
241 }
242
243 /*
244 * verify sector at a time only when
245 * the whole track write fails;
246 * (if we write a sector at a time, it takes forever)
247 */
248
249 report("Writing", cursec);
250
251 if (write(devfd, verbuf, trksiz) != trksiz) {
252 tmpend = cursec + dkg.dkg_nsect;
253 for (tmpsec = cursec; tmpsec < tmpend; tmpsec++) {
254 /*
255 * try writing to it once; if this fails,
256 * then announce the sector bad on stderr
257 */
258
245 verexit(1);
246 }
247
248 /*
249 * verify sector at a time only when
250 * the whole track write fails;
251 * (if we write a sector at a time, it takes forever)
252 */
253
254 report("Writing", cursec);
255
256 if (write(devfd, verbuf, trksiz) != trksiz) {
257 tmpend = cursec + dkg.dkg_nsect;
258 for (tmpsec = cursec; tmpsec < tmpend; tmpsec++) {
259 /*
260 * try writing to it once; if this fails,
261 * then announce the sector bad on stderr
262 */
263
259 if (lseek
260 (devfd, (long)tmpsec * NBPSCTR, 0) == -1) {
264 if (llseek(devfd, tmpsec * NBPSCTR, 0) == -1) {
261 (void) fprintf(stderr, "Error seeking "
265 (void) fprintf(stderr, "Error seeking "
262 "sector %ld Cylinder %ld\n",
266 "sector %llu Cylinder %llu\n",
263 tmpsec, cursec / cylsiz);
264 verexit(1);
265 }
266
267 report("Writing", tmpsec);
268
269 if (write(devfd, verbuf, NBPSCTR) != NBPSCTR) {
270 (void) fprintf(stderr,
267 tmpsec, cursec / cylsiz);
268 verexit(1);
269 }
270
271 report("Writing", tmpsec);
272
273 if (write(devfd, verbuf, NBPSCTR) != NBPSCTR) {
274 (void) fprintf(stderr,
271 "%ld\n", tmpsec + unix_base);
275 "%llu\n", tmpsec + unix_base);
272 numbadwr++;
273 }
274 }
275 }
276 }
277
278 (void) putchar(eol);
279 do_readonly:
280
281 for (cursec = 0; cursec < unix_size; cursec += dkg.dkg_nsect) {
276 numbadwr++;
277 }
278 }
279 }
280 }
281
282 (void) putchar(eol);
283 do_readonly:
284
285 for (cursec = 0; cursec < unix_size; cursec += dkg.dkg_nsect) {
282 if (lseek(devfd, (long)cursec * NBPSCTR, 0) == -1) {
286 if (llseek(devfd, cursec * NBPSCTR, 0) == -1) {
283 (void) fprintf(stderr,
287 (void) fprintf(stderr,
284 "Error seeking sector %ld Cylinder %ld\n",
285 cursec, cursec / cylsiz);
288 "Error seeking sector %llu Cylinder %llu\n",
289 cursec, cursec / cylsiz);
286 verexit(1);
287 }
288
289 /*
290 * read a sector at a time only when
291 * the whole track write fails;
292 * (if we do a sector at a time read, it takes forever)
293 */
294
295 report("Reading", cursec);
296 if (read(devfd, verbuf, trksiz) != trksiz) {
297 tmpend = cursec + dkg.dkg_nsect;
298 for (tmpsec = cursec; tmpsec < tmpend; tmpsec++) {
290 verexit(1);
291 }
292
293 /*
294 * read a sector at a time only when
295 * the whole track write fails;
296 * (if we do a sector at a time read, it takes forever)
297 */
298
299 report("Reading", cursec);
300 if (read(devfd, verbuf, trksiz) != trksiz) {
301 tmpend = cursec + dkg.dkg_nsect;
302 for (tmpsec = cursec; tmpsec < tmpend; tmpsec++) {
299 if (lseek(devfd, (long)tmpsec * NBPSCTR, 0)
300 == -1) {
303 if (llseek(devfd, tmpsec * NBPSCTR, 0) == -1) {
301 (void) fprintf(stderr, "Error seeking"
304 (void) fprintf(stderr, "Error seeking"
302 " sector %ld Cylinder %ld\n",
305 " sector %llu Cylinder %llu\n",
303 tmpsec, cursec / cylsiz);
304 verexit(1);
305 }
306 report("Reading", tmpsec);
307 if (read(devfd, verbuf, NBPSCTR) != NBPSCTR) {
306 tmpsec, cursec / cylsiz);
307 verexit(1);
308 }
309 report("Reading", tmpsec);
310 if (read(devfd, verbuf, NBPSCTR) != NBPSCTR) {
308 (void) fprintf(stderr, "%ld\n",
311 (void) fprintf(stderr, "%llu\n",
309 tmpsec + unix_base);
310 numbadrd++;
311 }
312 }
313 }
314 }
315 (void) printf("%c%c======== Diskscan complete ========%c", eol,
316 eol, eol);

--- 13 unchanged lines hidden (view full) ---

330}
331
332
333/*
334 * report where we are...
335 */
336
337static void
312 tmpsec + unix_base);
313 numbadrd++;
314 }
315 }
316 }
317 }
318 (void) printf("%c%c======== Diskscan complete ========%c", eol,
319 eol, eol);

--- 13 unchanged lines hidden (view full) ---

333}
334
335
336/*
337 * report where we are...
338 */
339
340static void
338report(char *what, int sector)
341report(char *what, diskaddr_t sector)
339{
342{
340 (void) printf("%s sector %-7d of %-7ld%c", what, sector,
343 (void) printf("%s sector %-19llu of %-19llu%c", what, sector,
341 unix_size, eol);
342}
344 unix_size, eol);
345}