summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/pcfs/fsck/fsck.c
blob: 200576d4ddad401623e228d2122949b4a5d221a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999,2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * fsck_pcfs -- main routines.
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <libintl.h>
#include <locale.h>
#include <sys/fcntl.h>
#include <sys/dktp/fdisk.h>
#include "pcfs_common.h"
#include "fsck_pcfs.h"
#include "pcfs_bpb.h"

int32_t BytesPerCluster;
int32_t TotalClusters;
int32_t LastCluster;
off64_t	FirstClusterOffset;
off64_t	PartitionOffset;
bpb_t	TheBIOSParameterBlock;

/*
 * {Output,Input}Image are the file names where we should write the
 * checked fs image and from which we should read the initial fs.
 * The image capability is designed for debugging purposes.
 */
static char	*OutputImage = NULL;
static char	*InputImage = NULL;
static int	WritableOnly = 0; /* -o w, check writable fs' only */
static int	Mflag = 0;	  /* -m, sanity check if fs is mountable */
static int	Preen = 0;	  /* -o p, preen; non-interactive */
/*
 * By default be quick; skip verify reads.
 * If the user wants more exhaustive checking,
 * they should run with the -o v option.
 */
static int	Quick = 1;

int	ReadOnly = 0;
int	IsFAT32 = 0;
int	Verbose = 0;

int	AlwaysYes = 0;	/* -y or -Y, assume a yes answer to all questions */
int	AlwaysNo = 0;	/* -n or -N, assume a no answer to all questions */

extern	ClusterContents	TheRootDir;

/*
 * Function definitions
 */
static void
passOne(int fd)
{
	if (!Quick)
		findBadClusters(fd);
	scanAndFixMetadata(fd);
}

static void
writeBackChanges(int fd)
{
	writeFATMods(fd);
	if (!IsFAT32)
		writeRootDirMods(fd);
	writeClusterMods(fd);
}

static void
tryOpen(int *fd, char *openMe, int oflag, int exitOnFailure)
{
	int saveError;

	if ((*fd = open(openMe, oflag)) < 0) {
		if (exitOnFailure == RETURN_ON_OPEN_FAILURE)
			return;
		saveError = errno;
		mountSanityCheckFails();
		(void) fprintf(stderr, "%s: ", openMe);
		(void) fprintf(stderr, strerror(saveError));
		(void) fprintf(stderr, "\n");
		exit(1);
	}
}

static void
doOpen(int *inFD, int *outFD, char *name, char *outName)
{
	if (ReadOnly) {
		tryOpen(inFD, name, O_RDONLY, EXIT_ON_OPEN_FAILURE);
		*outFD = -1;
	} else {
		tryOpen(inFD, name, O_RDWR, RETURN_ON_OPEN_FAILURE);
		if (*inFD < 0) {
			if (errno != EACCES || WritableOnly) {
				int saveError = errno;
				mountSanityCheckFails();
				(void) fprintf(stderr,
				    gettext("%s: "), name);
				(void) fprintf(stderr, strerror(saveError));
				(void) fprintf(stderr, "\n");
				exit(2);
			} else {
				tryOpen(inFD, name, O_RDONLY,
				    EXIT_ON_OPEN_FAILURE);
				AlwaysYes = 0;
				AlwaysNo = 1;
				ReadOnly = 1;
				*outFD = -1;
			}
		} else {
			*outFD = *inFD;
		}
	}

	if (outName != NULL) {
		tryOpen(outFD, outName, (O_RDWR | O_CREAT),
		    EXIT_ON_OPEN_FAILURE);
	}

	(void) printf("** %s %s\n", name,
	    ReadOnly ? gettext("(NO WRITE)") : "");
}

static void
openFS(char *special, int *inFD, int *outFD)
{
	struct stat dinfo;
	char *actualDisk = NULL;
	char *suffix = NULL;

	if (Verbose)
		(void) fprintf(stderr, gettext("Opening file system.\n"));

	if (InputImage == NULL) {
		actualDisk = stat_actual_disk(special, &dinfo, &suffix);
		/*
		 *  Destination exists, now find more about it.
		 */
		if (!(S_ISCHR(dinfo.st_mode))) {
			mountSanityCheckFails();
			(void) fprintf(stderr,
			    gettext("\n%s: device name must be a "
			    "character special device.\n"), actualDisk);
			exit(2);
		}
	} else {
		actualDisk = InputImage;
	}
	doOpen(inFD, outFD, actualDisk, OutputImage);
	if (suffix) {
		if ((PartitionOffset =
		    findPartitionOffset(*inFD, suffix)) < 0) {
			mountSanityCheckFails();
			(void) fprintf(stderr,
			    gettext("Unable to find logical drive %s\n"),
			    suffix);
			exit(2);
		} else if (Verbose) {
			(void) fprintf(stderr,
			    gettext("Partition starts at offset %lld\n"),
			    PartitionOffset);
		}
	} else {
		PartitionOffset = 0;
	}
}

void
usage()
{
	(void) fprintf(stderr,
		gettext("pcfs Usage: fsck -F pcfs [-o v|p|w] special-file\n"));
	exit(1);
}

static
char *LegalOpts[] = {
#define	VFLAG 0
	"v",
#define	PFLAG 1
	"p",
#define	WFLAG 2
	"w",
#define	DFLAG 3
	"d",
#define	IFLAG 4
	"i",
#define	OFLAG 5
	"o",
	NULL
};

static void
parseSubOptions(char *optsstr)
{
	char *value;
	int c;

	while (*optsstr != '\0') {
		switch (c = getsubopt(&optsstr, LegalOpts, &value)) {
		case VFLAG:
			Quick = 0;
			break;
		case PFLAG:
			Preen++;
			break;
		case WFLAG:
			WritableOnly++;
			break;
		case DFLAG:
			Verbose++;
			break;
		case IFLAG:
			if (value == NULL) {
				missing_arg(LegalOpts[c]);
			} else {
				InputImage = value;
			}
			break;
		case OFLAG:
			if (value == NULL) {
				missing_arg(LegalOpts[c]);
			} else {
				OutputImage = value;
			}
			break;
		default:
			bad_arg(value);
			break;
		}
	}
}

static void
sanityCheckOpts(void)
{
	if (WritableOnly && ReadOnly) {
		(void) fprintf(stderr,
		    gettext("-w option may not be used with the -n "
		    "or -m options\n"));
		exit(4);
	}
}

static void
confirmMountable(char *special, int fd)
{
	char *printName;
	int okayToMount = 1;

	printName = InputImage ? InputImage : special;

	if (!IsFAT32) {
		/* make sure we can at least read the root directory */
		getRootDirectory(fd);
		if (TheRootDir.bytes == NULL)
			okayToMount = 0;
	} else {
		/* check the bit designed into FAT32 for this purpose */
		okayToMount = checkFAT32CleanBit(fd);
	}
	if (okayToMount) {
		(void) fprintf(stderr,
		    gettext("pcfs fsck: sanity check: %s okay\n"), printName);
		exit(0);
	} else {
		(void) fprintf(stderr,
		    gettext("pcfs fsck: sanity check: %s needs checking\n"),
		    printName);
		exit(32);
	}
}

void
mountSanityCheckFails(void)
{
	if (Mflag) {
		(void) fprintf(stderr,
		    gettext("pcfs fsck: sanity check failed: "));
	}
}

/*
 * preenBail
 *	Routine that other routines can call if they would go into a
 *	state where they need user input.  They can send an optional
 *	message string to be printed before the exit.  Caller should
 *	send a NULL string if they don't have an exit message.
 */
void
preenBail(char *outString)
{
	/*
	 *  If we are running in the 'preen' mode, we got here because
	 *  we reached a situation that would require user intervention.
	 *  We have no choice but to bail at this point.
	 */
	if (Preen) {
		if (outString)
			(void) printf("%s", outString);
		(void) printf(gettext("FILE SYSTEM FIX REQUIRES USER "
		    "INTERVENTION; RUN fsck MANUALLY.\n"));
		exit(36);
	}
}

int
main(int argc, char *argv[])
{
	char *string;
	int  ifd, ofd;
	int  c;

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	if (argc < 2)
		usage();

	while ((c = getopt(argc, argv, "F:VYNynmo:")) != EOF) {
		switch (c) {
		case 'F':
			string = optarg;
			if (strcmp(string, "pcfs") != 0)
				usage();
			break;
		case 'V': {
				char	*opt_text;
				int	opt_count;

				(void) printf(gettext("fsck -F pcfs "));
				for (opt_count = 1; opt_count < argc;
				    opt_count++) {
					opt_text = argv[opt_count];
					if (opt_text)
						(void) printf(" %s ",
						    opt_text);
				}
				(void) printf("\n");
				exit(0);
			}
			break;
		case 'N':
		case 'n':
			AlwaysYes = 0;
			AlwaysNo = 1;
			ReadOnly = 1;
			break;
		case 'Y':
		case 'y':
			AlwaysYes = 1;
			AlwaysNo = 0;
			break;
		case 'm':
			Mflag++;
			ReadOnly = 1;
			break;
		case 'o':
			string = optarg;
			parseSubOptions(string);
			break;
		}
	}

	sanityCheckOpts();
	if (InputImage == NULL && (optind < 0 || optind >= argc))
		usage();

	openFS(argv[optind], &ifd, &ofd);
	readBPB(ifd);

	/*
	 * -m mountable fs check.  This call will not return.
	 */
	if (Mflag)
		confirmMountable(argv[optind], ifd);

	/*
	 *  Pass 1: Find any bad clusters and adjust the FAT and directory
	 *	entries accordingly
	 */
	passOne(ifd);

	/*
	 *  XXX - future passes?
	 *	Ideas:
	 *	    Data relocation for bad clusters with partial read success?
	 *	    Syncing backup FAT copies with main copy?
	 *	    Syncing backup root sector for FAT32?
	 */

	/*
	 *  No problems if we made it this far.
	 */
	printSummary(stdout);
	writeBackChanges(ofd);
	return (0);
}