10Sstevel@tonic-gate /*
2*1053Smaheshvs * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
70Sstevel@tonic-gate /* All Rights Reserved */
80Sstevel@tonic-gate
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
110Sstevel@tonic-gate * All rights reserved.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
140Sstevel@tonic-gate * provided that: (1) source distributions retain this entire copyright
150Sstevel@tonic-gate * notice and comment, and (2) distributions including binaries display
160Sstevel@tonic-gate * the following acknowledgement: ``This product includes software
170Sstevel@tonic-gate * developed by the University of California, Berkeley and its contributors''
180Sstevel@tonic-gate * in the documentation or other materials provided with the distribution
190Sstevel@tonic-gate * and in all advertising materials mentioning features or use of this
200Sstevel@tonic-gate * software. Neither the name of the University nor the names of its
210Sstevel@tonic-gate * contributors may be used to endorse or promote products derived
220Sstevel@tonic-gate * from this software without specific prior written permission.
230Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
240Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
250Sstevel@tonic-gate * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <ctype.h> /* use isdigit macro rather than 4.1 libc routine */
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <signal.h>
360Sstevel@tonic-gate #include <malloc.h>
370Sstevel@tonic-gate #include <ustat.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/mntent.h>
420Sstevel@tonic-gate #include <sys/vnode.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/wait.h>
450Sstevel@tonic-gate #include <sys/mnttab.h>
460Sstevel@tonic-gate #include <sys/signal.h>
470Sstevel@tonic-gate #include <sys/vfstab.h>
480Sstevel@tonic-gate #include <sys/fs/udf_volume.h>
490Sstevel@tonic-gate #include "fsck.h"
500Sstevel@tonic-gate #include <locale.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate extern int32_t writable(char *);
530Sstevel@tonic-gate extern void pfatal(char *, ...);
540Sstevel@tonic-gate extern void printfree();
550Sstevel@tonic-gate extern void pwarn(char *, ...);
560Sstevel@tonic-gate
570Sstevel@tonic-gate extern void pass1();
580Sstevel@tonic-gate extern void dofreemap();
590Sstevel@tonic-gate extern void dolvint();
600Sstevel@tonic-gate extern char *getfullblkname();
610Sstevel@tonic-gate extern char *getfullrawname();
620Sstevel@tonic-gate
630Sstevel@tonic-gate static int mflag = 0; /* sanity check only */
640Sstevel@tonic-gate
650Sstevel@tonic-gate char *mntopt();
660Sstevel@tonic-gate void catch(), catchquit(), voidquit();
670Sstevel@tonic-gate int returntosingle;
680Sstevel@tonic-gate static void checkfilesys();
690Sstevel@tonic-gate static void check_sanity();
700Sstevel@tonic-gate static void usage();
710Sstevel@tonic-gate
720Sstevel@tonic-gate static char *subopts [] = {
730Sstevel@tonic-gate #define PREEN 0
740Sstevel@tonic-gate "p",
750Sstevel@tonic-gate #define DEBUG 1
760Sstevel@tonic-gate "d",
770Sstevel@tonic-gate #define READ_ONLY 2
780Sstevel@tonic-gate "r",
790Sstevel@tonic-gate #define ONLY_WRITES 3
800Sstevel@tonic-gate "w",
810Sstevel@tonic-gate #define FORCE 4 /* force checking, even if clean */
820Sstevel@tonic-gate "f",
830Sstevel@tonic-gate #define STATS 5 /* print time and busy stats */
840Sstevel@tonic-gate "s",
850Sstevel@tonic-gate NULL
860Sstevel@tonic-gate };
870Sstevel@tonic-gate
880Sstevel@tonic-gate uint32_t ecma_version = 2;
890Sstevel@tonic-gate
90*1053Smaheshvs int
main(int argc,char * argv[])91*1053Smaheshvs main(int argc, char *argv[])
920Sstevel@tonic-gate {
930Sstevel@tonic-gate int c;
940Sstevel@tonic-gate char *suboptions, *value;
950Sstevel@tonic-gate int suboption;
960Sstevel@tonic-gate
970Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
980Sstevel@tonic-gate
990Sstevel@tonic-gate while ((c = getopt(argc, argv, "mnNo:VyYz")) != EOF) {
1000Sstevel@tonic-gate switch (c) {
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate case 'm':
1030Sstevel@tonic-gate mflag++;
1040Sstevel@tonic-gate break;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate case 'n': /* default no answer flag */
1070Sstevel@tonic-gate case 'N':
1080Sstevel@tonic-gate nflag++;
1090Sstevel@tonic-gate yflag = 0;
1100Sstevel@tonic-gate break;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate case 'o':
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * udfs specific options.
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate suboptions = optarg;
1170Sstevel@tonic-gate while (*suboptions != '\0') {
1180Sstevel@tonic-gate suboption = getsubopt(&suboptions,
1190Sstevel@tonic-gate subopts, &value);
1200Sstevel@tonic-gate switch (suboption) {
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate case PREEN:
1230Sstevel@tonic-gate preen++;
1240Sstevel@tonic-gate break;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate case DEBUG:
1270Sstevel@tonic-gate debug++;
1280Sstevel@tonic-gate break;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate case READ_ONLY:
1310Sstevel@tonic-gate break;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate case ONLY_WRITES:
1340Sstevel@tonic-gate /* check only writable filesystems */
1350Sstevel@tonic-gate wflag++;
1360Sstevel@tonic-gate break;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate case FORCE:
1390Sstevel@tonic-gate fflag++;
1400Sstevel@tonic-gate break;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate case STATS:
1430Sstevel@tonic-gate sflag++;
1440Sstevel@tonic-gate break;
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate default:
1470Sstevel@tonic-gate usage();
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate break;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate case 'V':
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate int opt_count;
1550Sstevel@tonic-gate char *opt_text;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate (void) fprintf(stdout, "fsck -F udfs ");
1580Sstevel@tonic-gate for (opt_count = 1; opt_count < argc;
1590Sstevel@tonic-gate opt_count++) {
1600Sstevel@tonic-gate opt_text = argv[opt_count];
1610Sstevel@tonic-gate if (opt_text)
1620Sstevel@tonic-gate (void) fprintf(stdout, " %s ",
1630Sstevel@tonic-gate opt_text);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate (void) fprintf(stdout, "\n");
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate break;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate case 'y': /* default yes answer flag */
1700Sstevel@tonic-gate case 'Y':
1710Sstevel@tonic-gate yflag++;
1720Sstevel@tonic-gate nflag = 0;
1730Sstevel@tonic-gate break;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate case '?':
1760Sstevel@tonic-gate usage();
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate argc -= optind;
1800Sstevel@tonic-gate argv = &argv[optind];
1810Sstevel@tonic-gate rflag++; /* check raw devices */
1820Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
1830Sstevel@tonic-gate (void) signal(SIGINT, catch);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if (preen) {
1870Sstevel@tonic-gate (void) signal(SIGQUIT, catchquit);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate if (argc) {
1910Sstevel@tonic-gate while (argc-- > 0) {
1920Sstevel@tonic-gate if (wflag && !writable(*argv)) {
1930Sstevel@tonic-gate (void) fprintf(stderr,
1940Sstevel@tonic-gate gettext("not writeable '%s'\n"), *argv);
1950Sstevel@tonic-gate argv++;
1960Sstevel@tonic-gate } else
1970Sstevel@tonic-gate checkfilesys(*argv++);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate exit(exitstat);
2000Sstevel@tonic-gate }
201*1053Smaheshvs return (0);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate static void
checkfilesys(char * filesys)206*1053Smaheshvs checkfilesys(char *filesys)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate char *devstr;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate mountfd = -1;
2110Sstevel@tonic-gate mountedfs = 0;
2120Sstevel@tonic-gate iscorrupt = 1;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate if ((devstr = setup(filesys)) == 0) {
2150Sstevel@tonic-gate if (iscorrupt == 0)
2160Sstevel@tonic-gate return;
2170Sstevel@tonic-gate if (preen)
2180Sstevel@tonic-gate pfatal(gettext("CAN'T CHECK FILE SYSTEM."));
2190Sstevel@tonic-gate if ((exitstat == 0) && (mflag))
2200Sstevel@tonic-gate exitstat = 32;
2210Sstevel@tonic-gate exit(exitstat);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate else
2240Sstevel@tonic-gate devname = devstr;
2250Sstevel@tonic-gate if (mflag)
2260Sstevel@tonic-gate check_sanity(filesys); /* this never returns */
2270Sstevel@tonic-gate iscorrupt = 0;
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate * 1: scan inodes tallying blocks used
2300Sstevel@tonic-gate */
2310Sstevel@tonic-gate if (preen == 0) {
2320Sstevel@tonic-gate if (mountedfs)
2330Sstevel@tonic-gate (void) printf(gettext("** Currently Mounted on %s\n"),
2340Sstevel@tonic-gate mountpoint);
2350Sstevel@tonic-gate if (mflag) {
2360Sstevel@tonic-gate (void) printf(
2370Sstevel@tonic-gate gettext("** Phase 1 - Sanity Check only\n"));
2380Sstevel@tonic-gate return;
2390Sstevel@tonic-gate } else
2400Sstevel@tonic-gate (void) printf(
2410Sstevel@tonic-gate gettext("** Phase 1 - Check Directories "
2420Sstevel@tonic-gate "and Blocks\n"));
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate pass1();
2450Sstevel@tonic-gate if (sflag) {
2460Sstevel@tonic-gate if (preen)
2470Sstevel@tonic-gate (void) printf("%s: ", devname);
2480Sstevel@tonic-gate else
2490Sstevel@tonic-gate (void) printf("** ");
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate if (debug)
2520Sstevel@tonic-gate (void) printf("pass1 isdirty %d\n", isdirty);
2530Sstevel@tonic-gate if (debug)
2540Sstevel@tonic-gate printfree();
2550Sstevel@tonic-gate dofreemap();
2560Sstevel@tonic-gate dolvint();
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate * print out summary statistics
2600Sstevel@tonic-gate */
2610Sstevel@tonic-gate pwarn(gettext("%d files, %d dirs, %d used, %d free\n"), n_files, n_dirs,
2620Sstevel@tonic-gate n_blks, part_len - n_blks);
2630Sstevel@tonic-gate if (iscorrupt)
2640Sstevel@tonic-gate exitstat = 36;
2650Sstevel@tonic-gate if (!fsmodified)
2660Sstevel@tonic-gate return;
2670Sstevel@tonic-gate if (!preen)
2680Sstevel@tonic-gate (void) printf(
2690Sstevel@tonic-gate gettext("\n***** FILE SYSTEM WAS MODIFIED *****\n"));
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate if (mountedfs) {
2720Sstevel@tonic-gate exitstat = 40;
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate * exit 0 - file system is unmounted and okay
2790Sstevel@tonic-gate * exit 32 - file system is unmounted and needs checking
2800Sstevel@tonic-gate * exit 33 - file system is mounted
2810Sstevel@tonic-gate * for root file system
2820Sstevel@tonic-gate * exit 34 - cannot stat device
2830Sstevel@tonic-gate */
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate static void
check_sanity(char * filename)286*1053Smaheshvs check_sanity(char *filename)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate struct stat stbd, stbr;
2890Sstevel@tonic-gate struct ustat usb;
2900Sstevel@tonic-gate char *devname;
2910Sstevel@tonic-gate struct vfstab vfsbuf;
2920Sstevel@tonic-gate FILE *vfstab;
2930Sstevel@tonic-gate int is_root = 0;
2940Sstevel@tonic-gate int is_usr = 0;
2950Sstevel@tonic-gate int is_block = 0;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (stat(filename, &stbd) < 0) {
2980Sstevel@tonic-gate (void) fprintf(stderr,
2990Sstevel@tonic-gate gettext("udfs fsck: sanity check failed : cannot stat "
3000Sstevel@tonic-gate "%s\n"), filename);
3010Sstevel@tonic-gate exit(34);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate if ((stbd.st_mode & S_IFMT) == S_IFBLK)
3050Sstevel@tonic-gate is_block = 1;
3060Sstevel@tonic-gate else if ((stbd.st_mode & S_IFMT) == S_IFCHR)
3070Sstevel@tonic-gate is_block = 0;
3080Sstevel@tonic-gate else {
3090Sstevel@tonic-gate (void) fprintf(stderr,
3100Sstevel@tonic-gate gettext("udfs fsck: sanity check failed: %s not "
3110Sstevel@tonic-gate "block or character device\n"), filename);
3120Sstevel@tonic-gate exit(34);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate * Determine if this is the root file system via vfstab. Give up
3170Sstevel@tonic-gate * silently on failures. The whole point of this is not to care
3180Sstevel@tonic-gate * if the root file system is already mounted.
3190Sstevel@tonic-gate *
3200Sstevel@tonic-gate * XXX - similar for /usr. This should be fixed to simply return
3210Sstevel@tonic-gate * a new code indicating, mounted and needs to be checked.
3220Sstevel@tonic-gate */
3230Sstevel@tonic-gate if ((vfstab = fopen(VFSTAB, "r")) != 0) {
3240Sstevel@tonic-gate if (getvfsfile(vfstab, &vfsbuf, "/") == 0) {
3250Sstevel@tonic-gate if (is_block)
3260Sstevel@tonic-gate devname = vfsbuf.vfs_special;
3270Sstevel@tonic-gate else
3280Sstevel@tonic-gate devname = vfsbuf.vfs_fsckdev;
3290Sstevel@tonic-gate if (stat(devname, &stbr) == 0)
3300Sstevel@tonic-gate if (stbr.st_rdev == stbd.st_rdev)
3310Sstevel@tonic-gate is_root = 1;
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate if (getvfsfile(vfstab, &vfsbuf, "/usr") == 0) {
3340Sstevel@tonic-gate if (is_block)
3350Sstevel@tonic-gate devname = vfsbuf.vfs_special;
3360Sstevel@tonic-gate else
3370Sstevel@tonic-gate devname = vfsbuf.vfs_fsckdev;
3380Sstevel@tonic-gate if (stat(devname, &stbr) == 0)
3390Sstevel@tonic-gate if (stbr.st_rdev == stbd.st_rdev)
3400Sstevel@tonic-gate is_usr = 1;
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate * XXX - only works if filename is a block device or if
3470Sstevel@tonic-gate * character and block device has the same dev_t value
3480Sstevel@tonic-gate */
3490Sstevel@tonic-gate if (is_root == 0 && is_usr == 0 && ustat(stbd.st_rdev, &usb) == 0) {
3500Sstevel@tonic-gate (void) fprintf(stderr,
3510Sstevel@tonic-gate gettext("udfs fsck: sanity check: %s "
3520Sstevel@tonic-gate "already mounted\n"), filename);
3530Sstevel@tonic-gate exit(33);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if (lvintp->lvid_int_type == LVI_CLOSE) {
3570Sstevel@tonic-gate (void) fprintf(stderr,
3580Sstevel@tonic-gate gettext("udfs fsck: sanity check: %s okay\n"),
3590Sstevel@tonic-gate filename);
3600Sstevel@tonic-gate } else {
3610Sstevel@tonic-gate (void) fprintf(stderr,
3620Sstevel@tonic-gate gettext("udfs fsck: sanity check: %s needs checking\n"),
3630Sstevel@tonic-gate filename);
3640Sstevel@tonic-gate exit(32);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate exit(0);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate char *
unrawname(char * name)370*1053Smaheshvs unrawname(char *name)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate char *dp;
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate if ((dp = getfullblkname(name)) == NULL)
3760Sstevel@tonic-gate return ("");
3770Sstevel@tonic-gate return (dp);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate char *
rawname(char * name)381*1053Smaheshvs rawname(char *name)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate char *dp;
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate if ((dp = getfullrawname(name)) == NULL)
3860Sstevel@tonic-gate return ("");
3870Sstevel@tonic-gate return (dp);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate char *
hasvfsopt(struct vfstab * vfs,char * opt)391*1053Smaheshvs hasvfsopt(struct vfstab *vfs, char *opt)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate char *f, *opts;
3940Sstevel@tonic-gate static char *tmpopts;
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate if (vfs->vfs_mntopts == NULL)
3970Sstevel@tonic-gate return (NULL);
3980Sstevel@tonic-gate if (tmpopts == 0) {
3990Sstevel@tonic-gate tmpopts = (char *)calloc(256, sizeof (char));
4000Sstevel@tonic-gate if (tmpopts == 0)
4010Sstevel@tonic-gate return (0);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate (void) strncpy(tmpopts, vfs->vfs_mntopts, (sizeof (tmpopts) - 1));
4040Sstevel@tonic-gate opts = tmpopts;
4050Sstevel@tonic-gate f = mntopt(&opts);
4060Sstevel@tonic-gate for (; *f; f = mntopt(&opts)) {
4070Sstevel@tonic-gate if (strncmp(opt, f, strlen(opt)) == 0)
4080Sstevel@tonic-gate return (f - tmpopts + vfs->vfs_mntopts);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate return (NULL);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate static void
usage()4140Sstevel@tonic-gate usage()
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate (void) fprintf(stderr, gettext("udfs usage: fsck [-F udfs] "
4170Sstevel@tonic-gate "[generic options] [-o p,w,s] [special ....]\n"));
4180Sstevel@tonic-gate exit(31+1);
4190Sstevel@tonic-gate }
420