summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/udfs/fsck/setup.c
blob: 01df08456e5228529471c70c5da51d66941295ee (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that: (1) source distributions retain this entire copyright
 * notice and comment, and (2) distributions including binaries display
 * the following acknowledgement:  ``This product includes software
 * developed by the University of California, Berkeley and its contributors''
 * in the documentation or other materials provided with the distribution
 * and in all advertising materials mentioning features or use of this
 * software. Neither the name of the University nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#define	DKTYPENAMES
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ustat.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <sys/dkio.h>
#include <sys/filio.h>
#include <sys/isa_defs.h>	/* for ENDIAN defines */
#include <sys/int_const.h>
#include <sys/vnode.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <string.h>
#include <sys/vfstab.h>
#include <sys/fs/udf_volume.h>
#include <sys/vtoc.h>
#include <locale.h>

#include "fsck.h"

extern void	errexit(char *, ...);
extern int32_t	mounted(char *);
extern void	pwarn(char *, ...);
extern void	pfatal(char *, ...);
extern void	printclean();
extern void	bufinit();
extern void	ckfini();
extern int32_t	bread(int32_t, char *, daddr_t, long);
extern int32_t	reply(char *);

static int32_t	readvolseq(int32_t);
static uint32_t	get_last_block();
extern int32_t	verifytag(struct tag *, uint32_t, struct tag *, int);
extern char	*tagerrs[];

#define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)

extern int	mflag;
extern char	hotroot;

char avdbuf[MAXBSIZE];		/* buffer for anchor volume descriptor */
char *main_vdbuf;		/* buffer for entire main volume sequence */
char *res_vdbuf;		/* buffer for reserved volume sequence */
int serialnum = -1;		/* set from primary volume descriptor */

char *
setup(char *dev)
{
	dev_t rootdev;
	struct stat statb;
	static char devstr[MAXPATHLEN];
	char *raw, *rawname(), *unrawname();
	struct ustat ustatb;

	havesb = 0;
	if (stat("/", &statb) < 0)
		errexit(gettext("Can't stat root\n"));
	rootdev = statb.st_dev;

	devname = devstr;
	(void) strncpy(devstr, dev, sizeof (devstr));
restat:
	if (stat(devstr, &statb) < 0) {
		(void) printf(gettext("Can't stat %s\n"), devstr);
		exitstat = 34;
		return (0);
	}
	/*
	 * A mount point is specified. But the mount point doesn't
	 * match entries in the /etc/vfstab.
	 * Search mnttab, because if the fs is error locked, it is
	 * allowed to be fsck'd while mounted.
	 */
	if ((statb.st_mode & S_IFMT) == S_IFDIR) {
		(void) printf(gettext("%s is not a block or "
			"character device\n"), dev);
		return (0);
	}

	if ((statb.st_mode & S_IFMT) == S_IFBLK) {
		if (rootdev == statb.st_rdev)
			hotroot++;
		else if (ustat(statb.st_rdev, &ustatb) == 0) {
			(void) printf(gettext("%s is a mounted file system, "
				"ignored\n"), dev);
			exitstat = 33;
			return (0);
		}
	}
	if ((statb.st_mode & S_IFMT) == S_IFDIR) {
		FILE *vfstab;
		struct vfstab vfsbuf;
		/*
		 * Check vfstab for a mount point with this name
		 */
		if ((vfstab = fopen(VFSTAB, "r")) == NULL) {
			errexit(gettext("Can't open checklist file: %s\n"),
				VFSTAB);
		}
		while (getvfsent(vfstab, &vfsbuf) == 0) {
			if (strcmp(devstr, vfsbuf.vfs_mountp) == 0) {
				if (strcmp(vfsbuf.vfs_fstype,
				    MNTTYPE_UDFS) != 0) {
					/*
					 * found the entry but it is not a
					 * udfs filesystem, don't check it
					 */
					(void) fclose(vfstab);
					return (0);
				}
				(void) strcpy(devstr, vfsbuf.vfs_special);
				if (rflag) {
					raw = rawname(
					    unrawname(vfsbuf.vfs_special));
					(void) strcpy(devstr, raw);
				}
				goto restat;
			}
		}
		(void) fclose(vfstab);

	} else if (((statb.st_mode & S_IFMT) != S_IFBLK) &&
	    ((statb.st_mode & S_IFMT) != S_IFCHR)) {
		if (preen)
			pwarn(gettext("file is not a block or "
				"character device.\n"));
		else if (reply(gettext("file is not a block or "
				"character device; OK"))
		    == 0)
			return (0);
		/*
		 * To fsck regular files (fs images)
		 * we need to clear the rflag since
		 * regular files don't have raw names.  --CW
		 */
		rflag = 0;
	}

	if (mounted(devstr)) {
		if (rflag)
			mountedfs++;
		else {
			(void) printf(gettext("%s is mounted, fsck on BLOCK "
				"device ignored\n"), devstr);
			exit(33);
		}
		sync();	/* call sync, only when devstr's mounted */
	}
	if (rflag) {
		char blockname[MAXPATHLEN];
		/*
		 * For root device check, must check
		 * block devices.
		 */
		(void) strcpy(blockname, devstr);
		if (stat(unrawname(blockname), &statb) < 0) {
			(void) printf(gettext("Can't stat %s\n"), blockname);
			exitstat = 34;
			return (0);
		}
	}
	if (rootdev == statb.st_rdev)
		hotroot++;
	if ((fsreadfd = open(devstr, O_RDONLY)) < 0) {
		(void) printf(gettext("Can't open %s\n"), devstr);
		exitstat = 34;
		return (0);
	}
	if (preen == 0 || debug != 0)
		(void) printf("** %s", devstr);

	if (nflag || (fswritefd = open(devstr, O_WRONLY)) < 0) {
		fswritefd = -1;
		if (preen && !debug)
			pfatal(gettext("(NO WRITE ACCESS)\n"));
		(void) printf(gettext(" (NO WRITE)"));
	}
	if (preen == 0)
		(void) printf("\n");
	if (debug && (hotroot || mountedfs)) {
		(void) printf("** %s", devstr);
		if (hotroot)
			(void) printf(" is root fs%s",
				mountedfs? " and": "");
		if (mountedfs)
			(void) printf(" is mounted");

		(void) printf(".\n");
	}
	fsmodified = 0;
	if (readvolseq(1) == 0)
		return (0);
	if (fflag == 0 && preen &&
		lvintp->lvid_int_type == LVI_CLOSE) {
		iscorrupt = 0;
		printclean();
		return (0);
	}
	listmax = FEGROW;
	inphash = (struct fileinfo **)calloc(FEGROW,
			sizeof (struct fileinfo *));
	inphead = (struct fileinfo *)calloc(FEGROW + 1,
			sizeof (struct fileinfo));
	if (inphead == NULL || inphash == NULL) {
		(void) printf(gettext("cannot alloc %ld bytes for inphead\n"),
			listmax * sizeof (struct fileinfo));
		goto badsb;
	}
	inpnext = inphead;
	inplast = &inphead[listmax];

	bufinit();
	return (devstr);

badsb:
	ckfini();
	exitstat = 39;
	return (0);
}

static int
check_pri_vol_desc(struct tag *tp)
{
	pvolp = (struct pri_vol_desc *)tp;
	return (0);
}

static int
check_avdp(struct tag *tp)
{
	avdp = (struct anch_vol_desc_ptr *)tp;
	return (0);
}

static int
check_vdp(struct tag *tp)
{
	volp = (struct vdp_desc *)tp;
	return (0);
}

static int
check_iuvd(struct tag *tp)
{
	iudp = (struct iuvd_desc *)tp;
	return (0);
}

static int
check_part_desc(struct tag *tp)
{
	partp = (struct part_desc *)tp;
	/* LINTED */
	pheadp = (struct phdr_desc *)&partp->pd_pc_use;
	part_start = partp->pd_part_start;
	part_len = partp->pd_part_length;
	if (debug)
		(void) printf("partition start %x len %x\n", part_start,
			part_len);
	return (0);
}

static int
check_log_desc(struct tag *tp)
{
	logvp = (struct log_vol_desc *)tp;
	return (0);
}

static int
check_unall_desc(struct tag *tp)
{
	unallp = (struct unall_desc *)tp;
	return (0);
}

/* ARGSUSED */
static int
check_term_desc(struct tag *tp)
{
	return (0);
}

static int
check_lvint(struct tag *tp)
{
	/* LINTED */
	lvintp = (struct log_vol_int_desc *)tp;
	return (0);
}

void
dump16(char *cp, char *nl)
{
	int i;
	long *ptr;


	for (i = 0; i < 16; i += 4) {
		/* LINTED */
		ptr = (long *)(cp + i);
		(void) printf("%08lx ", *ptr);
	}
	(void) printf(nl);
}

/*
 * Read in the super block and its summary info.
 */
/* ARGSUSED */
static int
readvolseq(int32_t listerr)
{
	struct tag *tp;
	long_ad_t *lap;
	struct anch_vol_desc_ptr *avp;
	uint8_t *cp, *end;
	daddr_t nextblock;
	int err;
	long	freelen;
	daddr_t avdp;

	disk_size = get_last_block();
	if (debug)
		(void) printf("Disk partition size: %x\n", disk_size);

	/* LINTED */
	avp = (struct anch_vol_desc_ptr *)avdbuf;
	tp = &avp->avd_tag;
	for (fsbsize = 512; fsbsize <= MAXBSIZE; fsbsize <<= 1) {
		avdp = FIRSTAVDP * fsbsize / DEV_BSIZE;
		if (bread(fsreadfd, avdbuf, avdp, fsbsize) != 0)
			return (0);
		err = verifytag(tp, FIRSTAVDP, tp, UD_ANCH_VOL_DESC);
		if (debug)
			(void) printf("bsize %ld tp->tag %d, %s\n", fsbsize,
				tp->tag_id, tagerrs[err]);
		if (err == 0)
			break;
	}
	if (fsbsize > MAXBSIZE)
		errexit(gettext("Can't find anchor volume descriptor\n"));
	secsize = fsbsize;
	if (debug)
		(void) printf("fsbsize = %ld\n", fsbsize);
	main_vdbuf = malloc(avp->avd_main_vdse.ext_len);
	res_vdbuf = malloc(avp->avd_res_vdse.ext_len);
	if (main_vdbuf == NULL || res_vdbuf == NULL)
		errexit("cannot allocate space for volume sequences\n");
	if (debug)
		(void) printf("reading volume sequences "
			"(%d bytes at %x and %x)\n",
			avp->avd_main_vdse.ext_len, avp->avd_main_vdse.ext_loc,
			avp->avd_res_vdse.ext_loc);
	if (bread(fsreadfd, main_vdbuf, fsbtodb(avp->avd_main_vdse.ext_loc),
		avp->avd_main_vdse.ext_len) != 0)
		return (0);
	if (bread(fsreadfd, res_vdbuf, fsbtodb(avp->avd_res_vdse.ext_loc),
		avp->avd_res_vdse.ext_len) != 0)
		return (0);
	end = (uint8_t *)main_vdbuf + avp->avd_main_vdse.ext_len;
	nextblock = avp->avd_main_vdse.ext_loc;
	for (cp = (uint8_t *)main_vdbuf; cp < end; cp += fsbsize, nextblock++) {
		/* LINTED */
		tp = (struct tag *)cp;
		err = verifytag(tp, nextblock, tp, 0);
		if (debug) {
			dump16((char *)cp, "");
			(void) printf("blk %lx err %s tag %d\n", nextblock,
				tagerrs[err], tp->tag_id);
		}
		if (err == 0) {
			if (serialnum >= 0 && tp->tag_sno != serialnum) {
				(void) printf(gettext("serial number mismatch "
					"tag type %d, block %lx\n"), tp->tag_id,
					nextblock);
				continue;
			}
			switch (tp->tag_id) {
			case UD_PRI_VOL_DESC:
				serialnum = tp->tag_sno;
				if (debug) {
					(void) printf("serial number = %d\n",
						serialnum);
				}
				err = check_pri_vol_desc(tp);
				break;
			case UD_ANCH_VOL_DESC:
				err = check_avdp(tp);
				break;
			case UD_VOL_DESC_PTR:
				err = check_vdp(tp);
				break;
			case UD_IMPL_USE_DESC:
				err = check_iuvd(tp);
				break;
			case UD_PART_DESC:
				err = check_part_desc(tp);
				break;
			case UD_LOG_VOL_DESC:
				err = check_log_desc(tp);
				break;
			case UD_UNALL_SPA_DESC:
				err = check_unall_desc(tp);
				break;
			case UD_TERM_DESC:
				err = check_term_desc(tp);
				goto done;
				break;
			case UD_LOG_VOL_INT:
				err = check_lvint(tp);
				break;
			default:
				(void) printf(gettext("Invalid volume "
					"sequence tag %d\n"), tp->tag_id);
			}
		} else {
			(void) printf(gettext("Volume sequence tag error %s\n"),
				tagerrs[err]);
		}
	}
done:
	if (!partp || !logvp) {
		(void) printf(gettext("Missing partition header or"
			" logical volume descriptor\n"));
		return (0);
	}

	/* Get the logical volume integrity descriptor */
	lvintblock = logvp->lvd_int_seq_ext.ext_loc;
	lvintlen = logvp->lvd_int_seq_ext.ext_len;
	lvintp = (struct log_vol_int_desc *)malloc(lvintlen);
	if (debug)
		(void) printf("Logvolint at %x for %d bytes\n", lvintblock,
			lvintlen);
	if (lvintp == NULL) {
		(void) printf(gettext("Can't allocate space for logical"
			" volume integrity sequence\n"));
		return (0);
	}
	if (bread(fsreadfd, (char *)lvintp,
			fsbtodb(lvintblock), lvintlen) != 0) {
		return (0);
	}
	err = verifytag(&lvintp->lvid_tag, lvintblock, &lvintp->lvid_tag,
		UD_LOG_VOL_INT);
	if (debug) {
		dump16((char *)lvintp, "\n");
	}
	if (err) {
		(void) printf(gettext("Log_vol_int tag error: %s, tag = %d\n"),
			tagerrs[err], lvintp->lvid_tag.tag_id);
		return (0);
	}

	/* Get pointer to implementation use area */
	lviup = (struct lvid_iu *)&lvintp->lvid_fst[lvintp->lvid_npart*2];
	if (debug) {
		(void) printf("free space %d total %d ", lvintp->lvid_fst[0],
			lvintp->lvid_fst[1]);
	(void) printf(gettext("nfiles %d ndirs %d\n"), lviup->lvidiu_nfiles,
			lviup->lvidiu_ndirs);
	}

	/* Set up free block map and read in the existing free space map */
	freelen = pheadp->phdr_usb.sad_ext_len;
	if (freelen == 0) {
		(void) printf(gettext("No partition free map\n"));
	}
	part_bmp_bytes = (part_len + NBBY - 1) / NBBY;
	busymap = calloc((unsigned)part_bmp_bytes, sizeof (char));
	if (busymap == NULL) {
		(void) printf(gettext("Can't allocate free block bitmap\n"));
		return (0);
	}
	if (freelen) {
		part_bmp_sectors =
			(part_bmp_bytes + SPACEMAP_OFF + secsize - 1) /
			secsize;
		part_bmp_loc = pheadp->phdr_usb.sad_ext_loc + part_start;

		/* Mark the partition map blocks busy */
		markbusy(pheadp->phdr_usb.sad_ext_loc,
			part_bmp_sectors * secsize);

		spacep = (struct space_bmap_desc *)
			malloc(secsize*part_bmp_sectors);
		if (spacep == NULL) {
			(void) printf(gettext("Can't allocate partition "
				"map\n"));
			return (0);
		}
		if (bread(fsreadfd, (char *)spacep, fsbtodb(part_bmp_loc),
			part_bmp_sectors * secsize) != 0)
			return (0);
		cp = (uint8_t *)spacep;
		err = verifytag(&spacep->sbd_tag, pheadp->phdr_usb.sad_ext_loc,
			&spacep->sbd_tag, UD_SPA_BMAP_DESC);
		if (debug) {
			dump16((char *)cp, "");
			(void) printf("blk %x err %s tag %d\n", part_bmp_loc,
				tagerrs[err], spacep->sbd_tag.tag_id);
		}
		freemap = (char *)cp + SPACEMAP_OFF;
		if (debug)
			(void) printf("err %s tag %x space bitmap at %x"
				" length %d nbits %d nbytes %d\n",
				tagerrs[err], spacep->sbd_tag.tag_id,
				part_bmp_loc, part_bmp_sectors,
				spacep->sbd_nbits, spacep->sbd_nbytes);
		if (err) {
			(void) printf(gettext("Space bitmap tag error, %s, "
				"tag = %d\n"),
				tagerrs[err], spacep->sbd_tag.tag_id);
			return (0);
		}
	}

	/* Get the fileset descriptor */
	lap = (long_ad_t *)&logvp->lvd_lvcu;
	filesetblock = lap->lad_ext_loc;
	filesetlen = lap->lad_ext_len;
	markbusy(filesetblock, filesetlen);
	if (debug)
		(void) printf("Fileset descriptor at %x for %d bytes\n",
			filesetblock, filesetlen);
	if (!filesetlen) {
		(void) printf(gettext("No file set descriptor found\n"));
		return (0);
	}
	fileset = (struct file_set_desc *)malloc(filesetlen);
	if (fileset == NULL) {
		(void) printf(gettext("Unable to allocate fileset\n"));
		return (0);
	}
	if (bread(fsreadfd, (char *)fileset, fsbtodb(filesetblock + part_start),
		filesetlen) != 0) {
		return (0);
	}
	err = verifytag(&fileset->fsd_tag, filesetblock, &fileset->fsd_tag,
		UD_FILE_SET_DESC);
	if (err) {
		(void) printf(gettext("Fileset tag error, tag = %d, %s\n"),
			fileset->fsd_tag.tag_id, tagerrs[err]);
		return (0);
	}

	/* Get the address of the root file entry */
	lap = (long_ad_t *)&fileset->fsd_root_icb;
	rootblock = lap->lad_ext_loc;
	rootlen = lap->lad_ext_len;
	if (debug)
		(void) printf("Root at %x for %d bytes\n", rootblock, rootlen);

	havesb = 1;
	return (1);
}

uint32_t
get_last_block()
{
	struct vtoc vtoc;
	struct dk_cinfo dki_info;

	if (ioctl(fsreadfd, DKIOCGVTOC, (intptr_t)&vtoc) != 0) {
		(void) fprintf(stderr, gettext("Unable to read VTOC\n"));
		return (0);
	}

	if (vtoc.v_sanity != VTOC_SANE) {
		(void) fprintf(stderr, gettext("Vtoc.v_sanity != VTOC_SANE\n"));
		return (0);
	}

	if (ioctl(fsreadfd, DKIOCINFO, (intptr_t)&dki_info) != 0) {
		(void) fprintf(stderr,
		    gettext("Could not get the slice information\n"));
		return (0);
	}

	if (dki_info.dki_partition > V_NUMPAR) {
		(void) fprintf(stderr,
		    gettext("dki_info.dki_partition > V_NUMPAR\n"));
		return (0);
	}

	return ((uint32_t)vtoc.v_part[dki_info.dki_partition].p_size);
}