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