Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pfsck --- A generic, parallelizing front-end for the fsck program. |
| 3 | * It will automatically try to run fsck programs in parallel if the |
| 4 | * devices are on separate spindles. It is based on the same ideas as |
| 5 | * the generic front end for fsck by David Engel and Fred van Kempen, |
| 6 | * but it has been completely rewritten from scratch to support |
| 7 | * parallel execution. |
| 8 | * |
| 9 | * Written by Theodore Ts'o, <tytso@mit.edu> |
| 10 | * |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 11 | * Usage: fsck [-ACVRNTM] [-s] [-t fstype] [fs-options] device |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | * |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 13 | * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994: |
| 14 | * o Changed -t fstype to behave like with mount when -A (all file |
| 15 | * systems) or -M (like mount) is specified. |
| 16 | * o fsck looks if it can find the fsck.type program to decide |
| 17 | * if it should ignore the fs type. This way more fsck programs |
| 18 | * can be added without changing this front-end. |
| 19 | * o -R flag skip root file system. |
| 20 | * |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 21 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 22 | * |
| 23 | * %Begin-Header% |
| 24 | * This file may be redistributed under the terms of the GNU Public |
| 25 | * License. |
| 26 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 31 | #include <sys/signal.h> |
| 32 | #include <sys/stat.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 33 | #include <limits.h> |
| 34 | #include <stdio.h> |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 35 | #include <ctype.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | #include <string.h> |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 37 | #include <time.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 38 | #if HAVE_STDLIB_H |
| 39 | #include <stdlib.h> |
| 40 | #endif |
| 41 | #if HAVE_ERRNO_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | #include <errno.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 43 | #endif |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 44 | #if HAVE_PATHS_H |
| 45 | #include <paths.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 46 | #endif |
| 47 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 48 | #include <unistd.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 49 | #endif |
| 50 | #if HAVE_ERRNO_H |
| 51 | #include <errno.h> |
| 52 | #endif |
| 53 | #include <malloc.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 54 | |
| 55 | #include "../version.h" |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 56 | #include "nls-enable.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | #include "fsck.h" |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 58 | #include "get_device_by_label.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 60 | #ifndef _PATH_MNTTAB |
| 61 | #define _PATH_MNTTAB "/etc/fstab" |
| 62 | #endif |
| 63 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 64 | static const char *ignored_types[] = { |
| 65 | "ignore", |
| 66 | "iso9660", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 67 | "nfs", |
| 68 | "proc", |
| 69 | "sw", |
| 70 | "swap", |
| 71 | NULL |
| 72 | }; |
| 73 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 74 | static const char *really_wanted[] = { |
| 75 | "minix", |
| 76 | "ext2", |
Theodore Ts'o | 19c6891 | 2000-07-07 03:25:13 +0000 | [diff] [blame] | 77 | "ext3", |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 78 | "xiafs", |
| 79 | NULL |
| 80 | }; |
| 81 | |
| 82 | #ifdef DEV_DSK_DEVICES |
| 83 | static const char *base_devices[] = { |
| 84 | "/dev/dsk/hda", |
| 85 | "/dev/dsk/hdb", |
| 86 | "/dev/dsk/hdc", |
| 87 | "/dev/dsk/hdd", |
| 88 | "/dev/dsk/hd1a", |
| 89 | "/dev/dsk/hd1b", |
| 90 | "/dev/dsk/hd1c", |
| 91 | "/dev/dsk/hd1d", |
| 92 | "/dev/dsk/sda", |
| 93 | "/dev/dsk/sdb", |
| 94 | "/dev/dsk/sdc", |
| 95 | "/dev/dsk/sdd", |
| 96 | "/dev/dsk/sde", |
| 97 | "/dev/dsk/sdf", |
| 98 | "/dev/dsk/sdg", |
| 99 | NULL |
| 100 | }; |
| 101 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 102 | static const char *base_devices[] = { |
| 103 | "/dev/hda", |
| 104 | "/dev/hdb", |
| 105 | "/dev/hdc", |
| 106 | "/dev/hdd", |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 107 | "/dev/hd1a", |
| 108 | "/dev/hd1b", |
| 109 | "/dev/hd1c", |
| 110 | "/dev/hd1d", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 111 | "/dev/sda", |
| 112 | "/dev/sdb", |
| 113 | "/dev/sdc", |
| 114 | "/dev/sdd", |
| 115 | "/dev/sde", |
| 116 | "/dev/sdf", |
| 117 | "/dev/sdg", |
| 118 | NULL |
| 119 | }; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 120 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * Global variables for options |
| 124 | */ |
| 125 | char *devices[MAX_DEVICES]; |
| 126 | char *args[MAX_ARGS]; |
| 127 | int num_devices, num_args; |
| 128 | |
| 129 | int verbose = 0; |
| 130 | int doall = 0; |
| 131 | int noexecute = 0; |
| 132 | int serialize = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 133 | int skip_root = 0; |
| 134 | int like_mount = 0; |
| 135 | int notitle = 0; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 136 | int parallel_root = 0; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 137 | int progress = 0; |
Theodore Ts'o | 3bc0366 | 1999-10-26 17:17:00 +0000 | [diff] [blame] | 138 | int force_all_parallel = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 139 | char *progname; |
| 140 | char *fstype = NULL; |
| 141 | struct fs_info *filesys_info; |
| 142 | struct fsck_instance *instance_list; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 143 | const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc"; |
| 144 | char *fsck_path = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 145 | static int ignore(struct fs_info *); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 146 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 147 | static char *string_copy(const char *s) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 148 | { |
| 149 | char *ret; |
| 150 | |
| 151 | ret = malloc(strlen(s)+1); |
| 152 | if (ret) |
| 153 | strcpy(ret, s); |
| 154 | return ret; |
| 155 | } |
| 156 | |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 157 | static char *skip_over_blank(char *cp) |
| 158 | { |
| 159 | while (*cp && isspace(*cp)) |
| 160 | cp++; |
| 161 | return cp; |
| 162 | } |
| 163 | |
| 164 | static char *skip_over_word(char *cp) |
| 165 | { |
| 166 | while (*cp && !isspace(*cp)) |
| 167 | cp++; |
| 168 | return cp; |
| 169 | } |
| 170 | |
| 171 | static void strip_line(char *line) |
| 172 | { |
| 173 | char *p; |
| 174 | |
| 175 | while (*line) { |
| 176 | p = line + strlen(line) - 1; |
| 177 | if ((*p == '\n') || (*p == '\r')) |
| 178 | *p = 0; |
| 179 | else |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static char *parse_word(char **buf) |
| 185 | { |
| 186 | char *word, *next; |
| 187 | |
| 188 | word = *buf; |
| 189 | if (*word == 0) |
| 190 | return 0; |
| 191 | |
| 192 | word = skip_over_blank(word); |
| 193 | next = skip_over_word(word); |
| 194 | if (*next) |
| 195 | *next++ = 0; |
| 196 | *buf = next; |
| 197 | return word; |
| 198 | } |
| 199 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 200 | static void free_instance(struct fsck_instance *i) |
| 201 | { |
| 202 | if (i->prog) |
| 203 | free(i->prog); |
| 204 | if (i->device) |
| 205 | free(i->device); |
| 206 | free(i); |
| 207 | return; |
| 208 | } |
| 209 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 210 | static int parse_fstab_line(char *line, struct fs_info **ret_fs) |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 211 | { |
| 212 | char *device, *mntpnt, *type, *opts, *freq, *passno, *cp; |
| 213 | struct fs_info *fs; |
| 214 | |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 215 | *ret_fs = 0; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 216 | strip_line(line); |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 217 | if ((cp = strchr(line, '#'))) |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 218 | *cp = 0; /* Ignore everything after the comment char */ |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 219 | cp = line; |
| 220 | |
| 221 | device = parse_word(&cp); |
| 222 | mntpnt = parse_word(&cp); |
| 223 | type = parse_word(&cp); |
| 224 | opts = parse_word(&cp); |
| 225 | freq = parse_word(&cp); |
| 226 | passno = parse_word(&cp); |
| 227 | |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 228 | if (!device) |
| 229 | return 0; /* Allow blank lines */ |
| 230 | |
| 231 | if (!mntpnt || !type) |
| 232 | return -1; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 233 | |
| 234 | if (!(fs = malloc(sizeof(struct fs_info)))) |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 235 | return -1; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 236 | |
| 237 | fs->device = string_copy(device); |
| 238 | fs->mountpt = string_copy(mntpnt); |
| 239 | fs->type = string_copy(type); |
| 240 | fs->opts = string_copy(opts ? opts : ""); |
| 241 | fs->freq = freq ? atoi(freq) : -1; |
| 242 | fs->passno = passno ? atoi(passno) : -1; |
| 243 | fs->flags = 0; |
| 244 | fs->next = NULL; |
| 245 | |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 246 | *ret_fs = fs; |
| 247 | |
| 248 | return 0; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 251 | /* |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 252 | * Interpret the device name if necessary |
| 253 | */ |
| 254 | static char *interpret_device(char *spec) |
| 255 | { |
| 256 | char *dev = NULL; |
| 257 | |
| 258 | if (!strncmp(spec, "UUID=", 5)) |
| 259 | dev = get_spec_by_uuid(spec+5); |
| 260 | else if (!strncmp(spec, "LABEL=", 6)) |
| 261 | dev = get_spec_by_volume_label(spec+6); |
Theodore Ts'o | b4ee1fb | 2000-02-02 19:08:51 +0000 | [diff] [blame] | 262 | else |
| 263 | return spec; |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 264 | if (dev) { |
| 265 | free(spec); |
Theodore Ts'o | b4ee1fb | 2000-02-02 19:08:51 +0000 | [diff] [blame] | 266 | return (dev); |
| 267 | } |
| 268 | /* |
| 269 | * Check to see if this was because /proc/partitions isn't |
| 270 | * found. |
| 271 | */ |
| 272 | if (access("/proc/partitions", R_OK) < 0) { |
| 273 | fprintf(stderr, "Couldn't open /proc/partitions: %s\n", |
| 274 | strerror(errno)); |
| 275 | fprintf(stderr, "Is /proc mounted?\n"); |
| 276 | exit(1); |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 277 | } |
Theodore Ts'o | c96e511 | 2000-05-08 14:47:15 +0000 | [diff] [blame] | 278 | /* |
| 279 | * Check to see if this is because we're not running as root |
| 280 | */ |
| 281 | if (geteuid()) |
| 282 | fprintf(stderr, "Must be root to scan for matching " |
| 283 | "filesystems: %s\n", spec); |
| 284 | else |
| 285 | fprintf(stderr, "Couldn't find matching filesystem: %s\n", |
| 286 | spec); |
| 287 | exit(1); |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 291 | * Load the filesystem database from /etc/fstab |
| 292 | */ |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 293 | static void load_fs_info(const char *filename) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 294 | { |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 295 | FILE *f; |
| 296 | char buf[1024]; |
| 297 | int lineno = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 298 | int old_fstab = 1; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 299 | struct fs_info *fs, *fs_last = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 300 | |
| 301 | filesys_info = NULL; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 302 | if ((f = fopen(filename, "r")) == NULL) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 303 | fprintf(stderr, _("WARNING: couldn't open %s: %s\n"), |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 304 | filename, strerror(errno)); |
| 305 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 306 | } |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 307 | while (!feof(f)) { |
| 308 | lineno++; |
| 309 | if (!fgets(buf, sizeof(buf), f)) |
| 310 | break; |
| 311 | buf[sizeof(buf)-1] = 0; |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 312 | if (parse_fstab_line(buf, &fs) < 0) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 313 | fprintf(stderr, _("WARNING: bad format " |
| 314 | "on line %d of %s\n"), lineno, filename); |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 315 | continue; |
| 316 | } |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 317 | if (!fs) |
| 318 | continue; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 319 | if (!filesys_info) |
| 320 | filesys_info = fs; |
| 321 | else |
| 322 | fs_last->next = fs; |
| 323 | fs_last = fs; |
Theodore Ts'o | 665f710 | 1999-01-08 13:33:39 +0000 | [diff] [blame] | 324 | if (fs->passno < 0) |
| 325 | fs->passno = 0; |
| 326 | else |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 327 | old_fstab = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 328 | } |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 329 | |
| 330 | fclose(f); |
| 331 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 332 | if (old_fstab) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 333 | fprintf(stderr, _("\007\007\007" |
| 334 | "WARNING: Your /etc/fstab does not contain the fsck passno\n" |
| 335 | " field. I will kludge around things for you, but you\n" |
| 336 | " should fix your /etc/fstab file as soon as you can.\n\n")); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 337 | |
| 338 | for (fs = filesys_info; fs; fs = fs->next) { |
| 339 | fs->passno = 1; |
| 340 | } |
| 341 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Lookup filesys in /etc/fstab and return the corresponding entry. */ |
| 345 | static struct fs_info *lookup(char *filesys) |
| 346 | { |
| 347 | struct fs_info *fs; |
Theodore Ts'o | 5b1519b | 2000-07-06 14:16:08 +0000 | [diff] [blame] | 348 | int try_again = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 349 | |
| 350 | /* No filesys name given. */ |
| 351 | if (filesys == NULL) |
| 352 | return NULL; |
| 353 | |
| 354 | for (fs = filesys_info; fs; fs = fs->next) { |
Theodore Ts'o | 5b1519b | 2000-07-06 14:16:08 +0000 | [diff] [blame] | 355 | if (strchr(fs->device, '=')) |
| 356 | try_again++; |
| 357 | if (!strcmp(filesys, fs->device) || |
| 358 | !strcmp(filesys, fs->mountpt)) |
| 359 | break; |
| 360 | } |
Theodore Ts'o | 19c6891 | 2000-07-07 03:25:13 +0000 | [diff] [blame] | 361 | if (fs && strchr(fs->device, '=')) |
| 362 | fs->device = interpret_device(fs->device); |
Theodore Ts'o | 5b1519b | 2000-07-06 14:16:08 +0000 | [diff] [blame] | 363 | |
| 364 | if (fs || !try_again) |
| 365 | return fs; |
| 366 | |
| 367 | for (fs = filesys_info; fs; fs = fs->next) { |
| 368 | fs->device = interpret_device(fs->device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 369 | if (!strcmp(filesys, fs->device) || |
| 370 | !strcmp(filesys, fs->mountpt)) |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | return fs; |
| 375 | } |
| 376 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 377 | /* Find fsck program for a given fs type. */ |
| 378 | static char *find_fsck(char *type) |
| 379 | { |
| 380 | char *s; |
| 381 | const char *tpl; |
| 382 | static char prog[256]; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 383 | char *p = string_copy(fsck_path); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 384 | struct stat st; |
| 385 | |
| 386 | /* Are we looking for a program or just a type? */ |
| 387 | tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s"); |
| 388 | |
| 389 | for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) { |
| 390 | sprintf(prog, tpl, s, type); |
| 391 | if (stat(prog, &st) == 0) break; |
| 392 | } |
| 393 | free(p); |
| 394 | return(s ? prog : NULL); |
| 395 | } |
| 396 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 397 | static int progress_active(NOARGS) |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 398 | { |
| 399 | struct fsck_instance *inst; |
| 400 | |
| 401 | for (inst = instance_list; inst; inst = inst->next) { |
| 402 | if (inst->flags & FLAG_DONE) |
| 403 | continue; |
| 404 | if (inst->flags & FLAG_PROGRESS) |
| 405 | return 1; |
| 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 410 | /* |
| 411 | * Execute a particular fsck program, and link it into the list of |
| 412 | * child processes we are waiting for. |
| 413 | */ |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 414 | static int execute(const char *type, char *device, char *mntpt, |
| 415 | int interactive) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 416 | { |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 417 | char *s, *argv[80], prog[80]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 418 | int argc, i; |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 419 | struct fsck_instance *inst, *p; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 420 | pid_t pid; |
| 421 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 422 | inst = malloc(sizeof(struct fsck_instance)); |
| 423 | if (!inst) |
| 424 | return ENOMEM; |
| 425 | memset(inst, 0, sizeof(struct fsck_instance)); |
| 426 | |
| 427 | sprintf(prog, "fsck.%s", type); |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 428 | argv[0] = string_copy(prog); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 429 | argc = 1; |
| 430 | |
| 431 | for (i=0; i <num_args; i++) |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 432 | argv[argc++] = string_copy(args[i]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 433 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 434 | if (progress & !progress_active()) { |
Theodore Ts'o | 19c6891 | 2000-07-07 03:25:13 +0000 | [diff] [blame] | 435 | if ((strcmp(type, "ext2") == 0) || |
| 436 | (strcmp(type, "ext3") == 0)) { |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 437 | argv[argc++] = string_copy("-C0"); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 438 | inst->flags |= FLAG_PROGRESS; |
| 439 | } |
| 440 | } |
| 441 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 442 | argv[argc++] = string_copy(device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 443 | argv[argc] = 0; |
| 444 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 445 | s = find_fsck(prog); |
| 446 | if (s == NULL) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 447 | fprintf(stderr, _("fsck: %s: not found\n"), prog); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 448 | return ENOENT; |
| 449 | } |
| 450 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 451 | if (verbose || noexecute) { |
Theodore Ts'o | 5d45d80 | 1999-03-16 19:35:19 +0000 | [diff] [blame] | 452 | printf("[%s -- %s] ", s, mntpt ? mntpt : device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 453 | for (i=0; i < argc; i++) |
| 454 | printf("%s ", argv[i]); |
| 455 | printf("\n"); |
| 456 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 457 | |
| 458 | /* Fork and execute the correct program. */ |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 459 | if (noexecute) |
| 460 | pid = -1; |
| 461 | else if ((pid = fork()) < 0) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 462 | perror("fork"); |
| 463 | return errno; |
| 464 | } else if (pid == 0) { |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 465 | if (!interactive) |
| 466 | close(0); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 467 | (void) execv(s, argv); |
| 468 | perror(argv[0]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 469 | exit(EXIT_ERROR); |
| 470 | } |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 471 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 472 | for (i=0; i < argc; i++) |
| 473 | free(argv[i]); |
| 474 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 475 | inst->pid = pid; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 476 | inst->prog = string_copy(prog); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 477 | inst->type = string_copy(type); |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 478 | inst->device = string_copy(device); |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 479 | inst->start_time = time(0); |
| 480 | inst->next = NULL; |
| 481 | |
| 482 | /* |
| 483 | * Find the end of the list, so we add the instance on at the end. |
| 484 | */ |
| 485 | for (p = instance_list; p && p->next; p = p->next); |
| 486 | |
| 487 | if (p) |
Theodore Ts'o | ad6783d | 1999-10-26 01:58:54 +0000 | [diff] [blame] | 488 | p->next = inst; |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 489 | else |
| 490 | instance_list = inst; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Wait for one child process to exit; when it does, unlink it from |
| 497 | * the list of executing child processes, and return it. |
| 498 | */ |
| 499 | static struct fsck_instance *wait_one(NOARGS) |
| 500 | { |
| 501 | int status; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 502 | int sig; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 503 | struct fsck_instance *inst, *inst2, *prev; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 504 | pid_t pid; |
| 505 | |
| 506 | if (!instance_list) |
| 507 | return NULL; |
| 508 | |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 509 | if (noexecute) { |
| 510 | inst = instance_list; |
| 511 | instance_list = inst->next; |
| 512 | inst->exit_status = 0; |
| 513 | return(inst); |
| 514 | } |
| 515 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 516 | /* |
| 517 | * gcc -Wall fails saving throw against stupidity |
| 518 | * (inst and prev are thought to be uninitialized variables) |
| 519 | */ |
| 520 | inst = prev = NULL; |
| 521 | |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 522 | do { |
| 523 | pid = wait(&status); |
| 524 | if (pid < 0) { |
| 525 | if ((errno == EINTR) || (errno == EAGAIN)) |
| 526 | continue; |
| 527 | if (errno == ECHILD) { |
| 528 | fprintf(stderr, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 529 | _("%s: wait: No more child process?!?\n"), |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 530 | progname); |
| 531 | return NULL; |
| 532 | } |
| 533 | perror("wait"); |
| 534 | continue; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 535 | } |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 536 | for (prev = 0, inst = instance_list; |
| 537 | inst; |
| 538 | prev = inst, inst = inst->next) { |
| 539 | if (inst->pid == pid) |
| 540 | break; |
| 541 | } |
| 542 | } while (!inst); |
| 543 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 544 | if (WIFEXITED(status)) |
| 545 | status = WEXITSTATUS(status); |
| 546 | else if (WIFSIGNALED(status)) { |
| 547 | sig = WTERMSIG(status); |
| 548 | if (sig == SIGINT) { |
| 549 | status = EXIT_UNCORRECTED; |
| 550 | } else { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 551 | printf(_("Warning... %s for device %s exited " |
| 552 | "with signal %d.\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 553 | inst->prog, inst->device, sig); |
| 554 | status = EXIT_ERROR; |
| 555 | } |
| 556 | } else { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 557 | printf(_("%s %s: status is %x, should never happen.\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 558 | inst->prog, inst->device, status); |
| 559 | status = EXIT_ERROR; |
| 560 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 561 | inst->exit_status = status; |
| 562 | if (prev) |
| 563 | prev->next = inst->next; |
| 564 | else |
| 565 | instance_list = inst->next; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 566 | if (progress && (inst->flags & FLAG_PROGRESS) && |
| 567 | !progress_active()) { |
| 568 | for (inst2 = instance_list; inst2; inst2 = inst2->next) { |
| 569 | if (inst2->flags & FLAG_DONE) |
| 570 | continue; |
Theodore Ts'o | 19c6891 | 2000-07-07 03:25:13 +0000 | [diff] [blame] | 571 | if (strcmp(inst2->type, "ext2") && |
| 572 | strcmp(inst2->type, "ext3")) |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 573 | continue; |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 574 | /* |
| 575 | * If we've just started the fsck, wait a tiny |
| 576 | * bit before sending the kill, to give it |
| 577 | * time to set up the signal handler |
| 578 | */ |
| 579 | if (inst2->start_time < time(0)+2) { |
| 580 | if (fork() == 0) { |
| 581 | sleep(1); |
| 582 | kill(inst2->pid, SIGUSR1); |
| 583 | exit(0); |
| 584 | } |
| 585 | } else |
| 586 | kill(inst2->pid, SIGUSR1); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 590 | return inst; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Wait until all executing child processes have exited; return the |
| 595 | * logical OR of all of their exit code values. |
| 596 | */ |
| 597 | static int wait_all(NOARGS) |
| 598 | { |
| 599 | struct fsck_instance *inst; |
| 600 | int global_status = 0; |
| 601 | |
| 602 | while (instance_list) { |
| 603 | inst = wait_one(); |
| 604 | if (!inst) |
| 605 | break; |
| 606 | global_status |= inst->exit_status; |
| 607 | free_instance(inst); |
| 608 | } |
| 609 | return global_status; |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Run the fsck program on a particular device |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 614 | * |
| 615 | * If the type is specified using -t, and it isn't prefixed with "no" |
| 616 | * (as in "noext2") and only one filesystem type is specified, then |
| 617 | * use that type regardless of what is specified in /etc/fstab. |
| 618 | * |
| 619 | * If the type isn't specified by the user, then use either the type |
| 620 | * specified in /etc/fstab, or DEFAULT_FSTYPE. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 621 | */ |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 622 | static void fsck_device(char *device, int interactive) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 623 | { |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 624 | const char *type = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 625 | struct fs_info *fsent; |
| 626 | int retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 627 | |
Theodore Ts'o | 297f47a | 1997-04-26 14:25:20 +0000 | [diff] [blame] | 628 | if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ',')) |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 629 | type = fstype; |
| 630 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 631 | if ((fsent = lookup(device))) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 632 | device = fsent->device; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 633 | if (!type) |
| 634 | type = fsent->type; |
| 635 | } |
| 636 | if (!type) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 637 | type = DEFAULT_FSTYPE; |
| 638 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 639 | retval = execute(type, device, fsent ? fsent->mountpt : 0, |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 640 | interactive); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 641 | if (retval) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 642 | fprintf(stderr, _("%s: Error %d while executing fsck.%s " |
| 643 | "for %s\n"), progname, retval, type, device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 647 | /* See if filesystem type matches the list. */ |
| 648 | static int fs_match(char *type, char *fs_type) |
| 649 | { |
| 650 | int ret = 0, negate = 0; |
| 651 | char list[128]; |
| 652 | char *s; |
| 653 | |
| 654 | if (!fs_type) return(1); |
| 655 | |
| 656 | if (strncmp(fs_type, "no", 2) == 0) { |
| 657 | fs_type += 2; |
| 658 | negate = 1; |
| 659 | } |
| 660 | strcpy(list, fs_type); |
| 661 | s = strtok(list, ","); |
| 662 | while(s) { |
| 663 | if (strcmp(s, type) == 0) { |
| 664 | ret = 1; |
| 665 | break; |
| 666 | } |
| 667 | s = strtok(NULL, ","); |
| 668 | } |
| 669 | return(negate ? !ret : ret); |
| 670 | } |
| 671 | |
| 672 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 673 | /* Check if we should ignore this filesystem. */ |
| 674 | static int ignore(struct fs_info *fs) |
| 675 | { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 676 | const char **ip; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 677 | int wanted = 0; |
| 678 | |
| 679 | /* |
| 680 | * If the pass number is 0, ignore it. |
| 681 | */ |
| 682 | if (fs->passno == 0) |
| 683 | return 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | * If a specific fstype is specified, and it doesn't match, |
| 687 | * ignore it. |
| 688 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 689 | if (!fs_match(fs->type, fstype)) return 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 690 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 691 | /* Are we ignoring this type? */ |
| 692 | for(ip = ignored_types; *ip; ip++) |
| 693 | if (strcmp(fs->type, *ip) == 0) return(1); |
| 694 | |
| 695 | /* Do we really really want to check this fs? */ |
| 696 | for(ip = really_wanted; *ip; ip++) |
| 697 | if (strcmp(fs->type, *ip) == 0) { |
| 698 | wanted = 1; |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | /* See if the <fsck.fs> program is available. */ |
| 703 | if (find_fsck(fs->type) == NULL) { |
| 704 | if (wanted) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 705 | fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 706 | fs->device, fs->type); |
| 707 | return(1); |
| 708 | } |
| 709 | |
| 710 | /* We can and want to check this file system type. */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | * Return the "base device" given a particular device; this is used to |
| 716 | * assure that we only fsck one partition on a particular drive at any |
| 717 | * one time. Otherwise, the disk heads will be seeking all over the |
| 718 | * place. |
| 719 | */ |
| 720 | static const char *base_device(char *device) |
| 721 | { |
| 722 | const char **base; |
| 723 | |
| 724 | for (base = base_devices; *base; base++) { |
| 725 | if (!strncmp(*base, device, strlen(*base))) |
| 726 | return *base; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 727 | } |
| 728 | return device; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Returns TRUE if a partition on the same disk is already being |
| 733 | * checked. |
| 734 | */ |
| 735 | static int device_already_active(char *device) |
| 736 | { |
| 737 | struct fsck_instance *inst; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 738 | const char *base = base_device(device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 739 | |
Theodore Ts'o | 3bc0366 | 1999-10-26 17:17:00 +0000 | [diff] [blame] | 740 | if (force_all_parallel) |
| 741 | return 0; |
| 742 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 743 | for (inst = instance_list; inst; inst = inst->next) { |
| 744 | if (!strcmp(base, base_device(inst->device))) |
| 745 | return 1; |
| 746 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | /* Check all file systems, using the /etc/fstab table. */ |
| 751 | static int check_all(NOARGS) |
| 752 | { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 753 | struct fs_info *fs = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 754 | struct fsck_instance *inst; |
| 755 | int status = EXIT_OK; |
| 756 | int not_done_yet = 1; |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 757 | int passno = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 758 | int pass_done; |
| 759 | |
| 760 | if (verbose) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 761 | printf(_("Checking all file systems.\n")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 762 | |
| 763 | /* |
| 764 | * Find and check the root filesystem first. |
| 765 | */ |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 766 | if (!parallel_root) { |
| 767 | for (fs = filesys_info; fs; fs = fs->next) { |
| 768 | if (!strcmp(fs->mountpt, "/")) |
| 769 | break; |
| 770 | } |
| 771 | if (fs && !skip_root && !ignore(fs)) { |
Theodore Ts'o | 5b1519b | 2000-07-06 14:16:08 +0000 | [diff] [blame] | 772 | fs->device = interpret_device(fs->device); |
Theodore Ts'o | 3cb7784 | 2000-01-18 16:30:27 +0000 | [diff] [blame] | 773 | fsck_device(fs->device, 1); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 774 | fs->flags |= FLAG_DONE; |
| 775 | status |= wait_all(); |
| 776 | if (status > EXIT_NONDESTRUCT) |
| 777 | return status; |
| 778 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 779 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 780 | if (fs) fs->flags |= FLAG_DONE; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 781 | |
| 782 | /* |
| 783 | * Mark filesystems that should be ignored as done. |
| 784 | */ |
| 785 | for (fs = filesys_info; fs; fs = fs->next) { |
| 786 | if (ignore(fs)) |
| 787 | fs->flags |= FLAG_DONE; |
| 788 | } |
| 789 | |
| 790 | while (not_done_yet) { |
| 791 | not_done_yet = 0; |
| 792 | pass_done = 1; |
| 793 | |
| 794 | for (fs = filesys_info; fs; fs = fs->next) { |
| 795 | if (fs->flags & FLAG_DONE) |
| 796 | continue; |
| 797 | /* |
| 798 | * If the filesystem's pass number is higher |
| 799 | * than the current pass number, then we don't |
| 800 | * do it yet. |
| 801 | */ |
| 802 | if (fs->passno > passno) { |
| 803 | not_done_yet++; |
| 804 | continue; |
| 805 | } |
| 806 | /* |
| 807 | * If a filesystem on a particular device has |
| 808 | * already been spawned, then we need to defer |
| 809 | * this to another pass. |
| 810 | */ |
| 811 | if (device_already_active(fs->device)) { |
| 812 | pass_done = 0; |
| 813 | continue; |
| 814 | } |
| 815 | /* |
| 816 | * Spawn off the fsck process |
| 817 | */ |
Theodore Ts'o | 5b1519b | 2000-07-06 14:16:08 +0000 | [diff] [blame] | 818 | fs->device = interpret_device(fs->device); |
Theodore Ts'o | 3cb7784 | 2000-01-18 16:30:27 +0000 | [diff] [blame] | 819 | fsck_device(fs->device, serialize); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 820 | fs->flags |= FLAG_DONE; |
| 821 | |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 822 | if (serialize) { |
| 823 | pass_done = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 824 | break; /* Only do one filesystem at a time */ |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 825 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 826 | } |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 827 | if (verbose > 1) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 828 | printf(_("--waiting-- (pass %d)\n"), passno); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 829 | inst = wait_one(); |
| 830 | if (inst) { |
| 831 | status |= inst->exit_status; |
| 832 | free_instance(inst); |
| 833 | } |
| 834 | if (pass_done) { |
| 835 | status |= wait_all(); |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 836 | if (verbose > 1) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 837 | printf("----------------------------------\n"); |
| 838 | passno++; |
| 839 | } else |
| 840 | not_done_yet++; |
| 841 | } |
| 842 | status |= wait_all(); |
| 843 | return status; |
| 844 | } |
| 845 | |
| 846 | static void usage(NOARGS) |
| 847 | { |
| 848 | fprintf(stderr, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 849 | _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 850 | exit(EXIT_USAGE); |
| 851 | } |
| 852 | |
| 853 | static void PRS(int argc, char *argv[]) |
| 854 | { |
| 855 | int i, j; |
| 856 | char *arg; |
| 857 | char options[128]; |
| 858 | int opt = 0; |
| 859 | int opts_for_fsck = 0; |
| 860 | |
| 861 | num_devices = 0; |
| 862 | num_args = 0; |
| 863 | instance_list = 0; |
| 864 | |
| 865 | progname = argv[0]; |
| 866 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 867 | for (i=1; i < argc; i++) { |
| 868 | arg = argv[i]; |
| 869 | if (!arg) |
| 870 | continue; |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 871 | if ((arg[0] == '/' && !opts_for_fsck) || |
| 872 | (strncmp(arg, "LABEL=", 6) == 0) || |
| 873 | (strncmp(arg, "UUID=", 5) == 0)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 874 | if (num_devices >= MAX_DEVICES) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 875 | fprintf(stderr, _("%s: too many devices\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 876 | progname); |
| 877 | exit(1); |
| 878 | } |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 879 | devices[num_devices++] = |
| 880 | interpret_device(string_copy(arg)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 881 | continue; |
| 882 | } |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 883 | if (arg[0] != '-' || opts_for_fsck) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 884 | if (num_args >= MAX_ARGS) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 885 | fprintf(stderr, _("%s: too many arguments\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 886 | progname); |
| 887 | exit(1); |
| 888 | } |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 889 | args[num_args++] = string_copy(arg); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 890 | continue; |
| 891 | } |
| 892 | for (j=1; arg[j]; j++) { |
| 893 | if (opts_for_fsck) { |
| 894 | options[++opt] = arg[j]; |
| 895 | continue; |
| 896 | } |
| 897 | switch (arg[j]) { |
| 898 | case 'A': |
| 899 | doall++; |
| 900 | break; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 901 | case 'C': |
| 902 | progress++; |
| 903 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 904 | case 'V': |
| 905 | verbose++; |
| 906 | break; |
| 907 | case 'N': |
| 908 | noexecute++; |
| 909 | break; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 910 | case 'R': |
| 911 | skip_root++; |
| 912 | break; |
| 913 | case 'T': |
| 914 | notitle++; |
| 915 | break; |
| 916 | case 'M': |
| 917 | like_mount++; |
| 918 | break; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 919 | case 'P': |
| 920 | parallel_root++; |
| 921 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 922 | case 's': |
| 923 | serialize++; |
| 924 | break; |
| 925 | case 't': |
| 926 | if (arg[j+1]) { |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 927 | fstype = string_copy(arg+j+1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 928 | goto next_arg; |
| 929 | } |
| 930 | if ((i+1) < argc) { |
| 931 | i++; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 932 | fstype = string_copy(argv[i]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 933 | goto next_arg; |
| 934 | } |
| 935 | usage(); |
| 936 | break; |
| 937 | case '-': |
| 938 | opts_for_fsck++; |
| 939 | break; |
| 940 | default: |
| 941 | options[++opt] = arg[j]; |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | next_arg: |
| 946 | if (opt) { |
| 947 | options[0] = '-'; |
| 948 | options[++opt] = '\0'; |
| 949 | if (num_args >= MAX_ARGS) { |
| 950 | fprintf(stderr, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 951 | _("%s: too many arguments\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 952 | progname); |
| 953 | exit(1); |
| 954 | } |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 955 | args[num_args++] = string_copy(options); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 956 | opt = 0; |
| 957 | } |
| 958 | } |
Theodore Ts'o | 3bc0366 | 1999-10-26 17:17:00 +0000 | [diff] [blame] | 959 | if (getenv("FSCK_FORCE_ALL_PARALLEL")) |
| 960 | force_all_parallel++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | int main(int argc, char *argv[]) |
| 964 | { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 965 | int i; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 966 | int status = 0; |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 967 | int interactive = 0; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 968 | char *oldpath = getenv("PATH"); |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 969 | const char *fstab; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 970 | |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 971 | #ifdef ENABLE_NLS |
| 972 | setlocale(LC_MESSAGES, ""); |
| 973 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 974 | textdomain(NLS_CAT_NAME); |
| 975 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 976 | PRS(argc, argv); |
| 977 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 978 | if (!notitle) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 979 | printf(_("Parallelizing fsck version %s (%s)\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 980 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 981 | |
Theodore Ts'o | 7d5633c | 1999-02-09 08:14:28 +0000 | [diff] [blame] | 982 | fstab = getenv("FSTAB_FILE"); |
| 983 | if (!fstab) |
| 984 | fstab = _PATH_MNTTAB; |
| 985 | load_fs_info(fstab); |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 986 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 987 | /* Update our search path to include uncommon directories. */ |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 988 | if (oldpath) { |
| 989 | fsck_path = malloc (strlen (fsck_prefix_path) + 1 + |
| 990 | strlen (oldpath) + 1); |
| 991 | strcpy (fsck_path, fsck_prefix_path); |
| 992 | strcat (fsck_path, ":"); |
| 993 | strcat (fsck_path, oldpath); |
| 994 | } else { |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 995 | fsck_path = string_copy(fsck_prefix_path); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Theodore Ts'o | 6a35ffa | 1999-11-04 22:34:43 +0000 | [diff] [blame] | 998 | if ((num_devices == 1) || (serialize)) |
| 999 | interactive = 1; |
| 1000 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1001 | /* If -A was specified ("check all"), do that! */ |
| 1002 | if (doall) |
| 1003 | return check_all(); |
| 1004 | |
| 1005 | for (i = 0 ; i < num_devices; i++) { |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 1006 | fsck_device(devices[i], interactive); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1007 | if (serialize) { |
| 1008 | struct fsck_instance *inst; |
| 1009 | |
| 1010 | inst = wait_one(); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1011 | if (inst) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1012 | status |= inst->exit_status; |
| 1013 | free_instance(inst); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1017 | status |= wait_all(); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 1018 | free(fsck_path); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1019 | return status; |
| 1020 | } |