Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-2-Clause-FreeBSD |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank |
| 5 | * Copyright (c) 1995 Martin Husemann |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include <sys/cdefs.h> |
| 30 | #ifndef lint |
| 31 | __RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $"); |
| 32 | static const char rcsid[] = |
| 33 | "$FreeBSD$"; |
| 34 | #endif /* not lint */ |
| 35 | |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 36 | #ifdef HAVE_LIBUTIL_H |
| 37 | #include <libutil.h> |
| 38 | #endif |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <stdio.h> |
| 42 | #include <unistd.h> |
| 43 | #include <fcntl.h> |
| 44 | |
| 45 | #include "ext.h" |
| 46 | #include "fsutil.h" |
| 47 | |
| 48 | int |
| 49 | checkfilesys(const char *fname) |
| 50 | { |
| 51 | int dosfs; |
| 52 | struct bootblock boot; |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 53 | struct fat_descriptor *fat = NULL; |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 54 | int finish_dosdirsection=0; |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 55 | int mod = 0; |
| 56 | int ret = 8; |
Xin Li | 4f36a18 | 2020-04-28 09:03:01 -0700 | [diff] [blame] | 57 | int64_t freebytes; |
| 58 | int64_t badbytes; |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 59 | |
| 60 | rdonly = alwaysno; |
| 61 | if (!preen) |
| 62 | printf("** %s", fname); |
| 63 | |
| 64 | dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0); |
| 65 | if (dosfs < 0 && !rdonly) { |
| 66 | dosfs = open(fname, O_RDONLY, 0); |
| 67 | if (dosfs >= 0) |
| 68 | pwarn(" (NO WRITE)\n"); |
| 69 | else if (!preen) |
| 70 | printf("\n"); |
| 71 | rdonly = 1; |
| 72 | } else if (!preen) |
| 73 | printf("\n"); |
| 74 | |
| 75 | if (dosfs < 0) { |
| 76 | perr("Can't open `%s'", fname); |
| 77 | printf("\n"); |
| 78 | return 8; |
| 79 | } |
| 80 | |
| 81 | if (readboot(dosfs, &boot) == FSFATAL) { |
| 82 | close(dosfs); |
| 83 | printf("\n"); |
| 84 | return 8; |
| 85 | } |
| 86 | |
| 87 | if (skipclean && preen && checkdirty(dosfs, &boot)) { |
| 88 | printf("%s: ", fname); |
| 89 | printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); |
| 90 | ret = 0; |
| 91 | goto out; |
| 92 | } |
| 93 | |
| 94 | if (!preen) { |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 95 | printf("** Phase 1 - Read FAT and checking connectivity\n"); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 98 | mod |= readfat(dosfs, &boot, &fat); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 99 | if (mod & FSFATAL) { |
| 100 | close(dosfs); |
| 101 | return 8; |
| 102 | } |
| 103 | |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 104 | if (!preen) |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 105 | printf("** Phase 2 - Checking Directories\n"); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 106 | |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 107 | mod |= resetDosDirSection(fat); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 108 | finish_dosdirsection = 1; |
| 109 | if (mod & FSFATAL) |
| 110 | goto out; |
| 111 | /* delay writing FATs */ |
| 112 | |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 113 | mod |= handleDirTree(fat); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 114 | if (mod & FSFATAL) |
| 115 | goto out; |
| 116 | |
| 117 | if (!preen) |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 118 | printf("** Phase 3 - Checking for Lost Files\n"); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 119 | |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 120 | mod |= checklost(fat); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 121 | if (mod & FSFATAL) |
| 122 | goto out; |
| 123 | |
| 124 | /* now write the FATs */ |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 125 | if (mod & FSFATMOD) { |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 126 | if (ask(1, "Update FATs")) { |
Xin Li | 21386e0 | 2020-01-03 09:22:03 -0800 | [diff] [blame] | 127 | mod |= writefat(fat); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 128 | if (mod & FSFATAL) |
| 129 | goto out; |
| 130 | } else |
| 131 | mod |= FSERROR; |
| 132 | } |
| 133 | |
Xin Li | 4f36a18 | 2020-04-28 09:03:01 -0700 | [diff] [blame] | 134 | freebytes = (int64_t)boot.NumFree * boot.ClusterSize; |
| 135 | badbytes = (int64_t)boot.NumBad * boot.ClusterSize; |
| 136 | |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 137 | #ifdef HAVE_LIBUTIL_H |
| 138 | char freestr[7], badstr[7]; |
| 139 | |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 140 | humanize_number(freestr, sizeof(freestr), freebytes, "", |
| 141 | HN_AUTOSCALE, HN_DECIMAL | HN_IEC_PREFIXES); |
| 142 | if (boot.NumBad) { |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 143 | humanize_number(badstr, sizeof(badstr), badbytes, "", |
| 144 | HN_AUTOSCALE, HN_B | HN_DECIMAL | HN_IEC_PREFIXES); |
| 145 | |
| 146 | pwarn("%d files, %sB free (%d clusters), %sB bad (%d clusters)\n", |
Xin Li | 4f36a18 | 2020-04-28 09:03:01 -0700 | [diff] [blame] | 147 | boot.NumFiles, freestr, boot.NumFree, |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 148 | badstr, boot.NumBad); |
| 149 | } else { |
| 150 | pwarn("%d files, %sB free (%d clusters)\n", |
Xin Li | 4f36a18 | 2020-04-28 09:03:01 -0700 | [diff] [blame] | 151 | boot.NumFiles, freestr, boot.NumFree); |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 152 | } |
| 153 | #else |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 154 | if (boot.NumBad) |
Xin Li | 4f36a18 | 2020-04-28 09:03:01 -0700 | [diff] [blame] | 155 | pwarn("%d files, %jd KiB free (%d clusters), %jd KiB bad (%d clusters)\n", |
| 156 | boot.NumFiles, (intmax_t)freebytes / 1024, boot.NumFree, |
| 157 | (intmax_t)badbytes / 1024, boot.NumBad); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 158 | else |
Xin Li | 4f36a18 | 2020-04-28 09:03:01 -0700 | [diff] [blame] | 159 | pwarn("%d files, %jd KiB free (%d clusters)\n", |
| 160 | boot.NumFiles, (intmax_t)freebytes / 1024, boot.NumFree); |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 161 | #endif |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 162 | |
| 163 | if (mod && (mod & FSERROR) == 0) { |
| 164 | if (mod & FSDIRTY) { |
| 165 | if (ask(1, "MARK FILE SYSTEM CLEAN") == 0) |
| 166 | mod &= ~FSDIRTY; |
| 167 | |
| 168 | if (mod & FSDIRTY) { |
| 169 | pwarn("MARKING FILE SYSTEM CLEAN\n"); |
Xin Li | 05ca3f4 | 2020-04-27 09:01:09 -0700 | [diff] [blame] | 170 | mod |= cleardirty(fat); |
Xin Li | 1e1dad6 | 2019-04-10 13:38:23 -0700 | [diff] [blame] | 171 | } else { |
| 172 | pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n"); |
| 173 | mod |= FSERROR; /* file system not clean */ |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (mod & (FSFATAL | FSERROR)) |
| 179 | goto out; |
| 180 | |
| 181 | ret = 0; |
| 182 | |
| 183 | out: |
| 184 | if (finish_dosdirsection) |
| 185 | finishDosDirSection(); |
| 186 | free(fat); |
| 187 | close(dosfs); |
| 188 | |
| 189 | if (mod & (FSFATMOD|FSDIRMOD)) |
| 190 | pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); |
| 191 | |
| 192 | return ret; |
| 193 | } |