diff --git a/fsck/fsck.8 b/fsck/fsck.8 index fd1e7e2..3e1b38b 100644 --- a/fsck/fsck.8 +++ b/fsck/fsck.8 @@ -7,7 +7,7 @@ fsck \- check and repair a Linux file system .SH SYNOPSIS .B fsck -.RB [ \-sAVRTMNP ] +.RB [ \-lsAVRTMNP ] .RB [ \-C .RI [ fd ]] .RB [ \-t @@ -80,6 +80,17 @@ variable. Please see the file system-specific checker manual pages for further details. .SH OPTIONS .TP +.B \-l +Lock whole-disk device by exclusive +.BR flock (2). +This option can be used with one device only (e.g. -A and -l are mutually +exclusive). This option is recommended when more +.B fsck (8) +instances are executed in the same time. The option is ignored when used for +multiple devices or for non-rotating disk. The fsck does not lock underlying +devices if executed to check stacked devices (e.g. MD or DM) -- this feature is +not implemented yet. +.TP .B \-s Serialize .B fsck diff --git a/fsck/fsck.c b/fsck/fsck.c index e9e645f..ce0e765 100644 --- a/fsck/fsck.c +++ b/fsck/fsck.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include #include @@ -42,6 +44,8 @@ #include #include #include +#include +#include #include "fsprobe.h" @@ -85,6 +89,7 @@ char *devices[MAX_DEVICES]; char *args[MAX_ARGS]; int num_devices, num_args; +int lockdisk = 0; int verbose = 0; int doall = 0; int noexecute = 0; @@ -214,10 +219,96 @@ static void parse_escape(char *word) *q = 0; } +static dev_t get_disk(const char *device) +{ + struct stat st; + dev_t disk; + + if (!stat(device, &st) && + !blkid_devno_to_wholedisk(st.st_rdev, NULL, 0, &disk)) + return disk; + + return 0; +} + +static int is_irrotational_disk(dev_t disk) +{ + char path[PATH_MAX]; + FILE *f; + int rc, x; + + rc = snprintf(path, sizeof(path), + "/sys/dev/block/%d:%d/queue/rotational", + major(disk), minor(disk)); + + if (rc < 0 || rc + 1 > sizeof(path)) + return 0; + + f = fopen(path, "r"); + if (!f) + return 0; + + rc = fscanf(f, "%u", &x); + fclose(f); + + return rc == 1 ? !x : 0; +} + +static void lock_disk(struct fsck_instance *inst) +{ + dev_t disk = inst->fs->disk ? : get_disk(inst->fs->device); + char *diskname; + + if (!disk || is_irrotational_disk(disk)) + return; + + diskname = blkid_devno_to_devname(disk); + if (!diskname) + return; + + if (verbose) + printf(_("Locking disk %s ... "), diskname); + + inst->lock = open(diskname, O_CLOEXEC | O_RDONLY); + if (inst->lock >= 0) { + int rc = -1; + + /* inform users that we're waiting on the lock */ + if (verbose && + (rc = flock(inst->lock, LOCK_EX | LOCK_NB)) != 0 && + errno == EWOULDBLOCK) + printf(_("(waiting) ")); + + if (rc != 0 && flock(inst->lock, LOCK_EX) != 0) { + close(inst->lock); /* failed */ + inst->lock = -1; + } + } + + if (verbose) + printf("%s.\n", inst->lock >= 0 ? _("success") : _("failed")); + + free(diskname); + return; +} + +static void unlock_disk(struct fsck_instance *inst) +{ + if (inst->lock >= 0) { + /* explicitly unlock, don't rely on close(), maybe some library + * (e.g. liblkid) has still open the device. + */ + flock(inst->lock, LOCK_UN); + close(inst->lock); + } +} + static void free_instance(struct fsck_instance *i) { + if (lockdisk) + unlock_disk(i); + free(i->prog); - free(i->device); free(i->base_device); free(i); return; @@ -240,6 +331,8 @@ static struct fs_info *create_fs_device(const char *device, const char *mntpnt, fs->passno = passno; fs->flags = 0; fs->next = NULL; + fs->disk = 0; + fs->stacked = 0; if (!filesys_info) filesys_info = fs; @@ -414,8 +507,7 @@ static int progress_active(NOARGS) * Execute a particular fsck program, and link it into the list of * child processes we are waiting for. */ -static int execute(const char *type, const char *device, const char *mntpt, - int interactive) +static int execute(const char *type, struct fs_info *fs, int interactive) { char *s, *argv[80], prog[80]; int argc, i; @@ -452,7 +544,7 @@ static int execute(const char *type, const char *device, const char *mntpt, } } - argv[argc++] = string_copy(device); + argv[argc++] = string_copy(fs->device); argv[argc] = 0; s = find_fsck(prog); @@ -464,12 +556,20 @@ static int execute(const char *type, const char *device, const char *mntpt, if (verbose || noexecute) { printf("[%s (%d) -- %s] ", s, num_running, - mntpt ? mntpt : device); + fs->mountpt ? : fs->device); for (i=0; i < argc; i++) printf("%s ", argv[i]); printf("\n"); } + + inst->fs = fs; + inst->lock = -1; + + if (lockdisk) + lock_disk(inst); + + /* Fork and execute the correct program. */ if (noexecute) pid = -1; @@ -492,8 +592,7 @@ static int execute(const char *type, const char *device, const char *mntpt, inst->pid = pid; inst->prog = string_copy(prog); inst->type = string_copy(type); - inst->device = string_copy(device); - inst->base_device = base_device(device); + inst->base_device = base_device(fs->device); inst->start_time = time(0); inst->next = NULL; @@ -597,12 +696,12 @@ static struct fsck_instance *wait_one(int flags) } else { printf(_("Warning... %s for device %s exited " "with signal %d.\n"), - inst->prog, inst->device, sig); + inst->prog, inst->fs->device, sig); status = EXIT_ERROR; } } else { printf(_("%s %s: status is %x, should never happen.\n"), - inst->prog, inst->device, status); + inst->prog, inst->fs->device, status); status = EXIT_ERROR; } inst->exit_status = status; @@ -641,7 +740,7 @@ ret_inst: instance_list = inst->next; if (verbose > 1) printf(_("Finished with %s (exit status %d)\n"), - inst->device, inst->exit_status); + inst->fs->device, inst->exit_status); num_running--; return inst; } @@ -698,7 +797,7 @@ static void fsck_device(struct fs_info *fs, int interactive) type = DEFAULT_FSTYPE; num_running++; - retval = execute(type, fs->device, fs->mountpt, interactive); + retval = execute(type, fs, interactive); if (retval) { fprintf(stderr, _("%s: Error %d while executing fsck.%s " "for %s\n"), progname, retval, type, fs->device); @@ -924,41 +1023,71 @@ static int ignore(struct fs_info *fs) return 0; } -/* - * Returns TRUE if a partition on the same disk is already being - * checked. - */ -static int device_already_active(char *device) +static int count_slaves(dev_t disk) { - struct fsck_instance *inst; - char *base; + DIR *dir; + struct dirent *dp; + char dirname[PATH_MAX]; + int count = 0; - if (force_all_parallel) - return 0; + snprintf(dirname, sizeof(dirname), + "/sys/dev/block/%u:%u/slaves/", + major(disk), minor(disk)); -#ifdef BASE_MD - /* Don't check a soft raid disk with any other disk */ - if (instance_list && - (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) || - !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))) - return 1; + if (!(dir = opendir(dirname))) + return -1; + + while ((dp = readdir(dir)) != 0) { +#ifdef _DIRENT_HAVE_D_TYPE + if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK) + continue; #endif + if (dp->d_name[0] == '.' && + ((dp->d_name[1] == 0) || + ((dp->d_name[1] == '.') && (dp->d_name[2] == 0)))) + continue; - base = base_device(device); - /* - * If we don't know the base device, assume that the device is - * already active if there are any fsck instances running. - */ - if (!base) - return (instance_list != 0); - for (inst = instance_list; inst; inst = inst->next) { - if (!inst->base_device || !strcmp(base, inst->base_device)) { - free(base); - return 1; - } + count++; } - free(base); - return 0; + closedir(dir); + return count; +} + +/* + * Returns TRUE if a partition on the same disk is already being + * checked. + */ +static int disk_already_active(struct fs_info *fs) +{ + struct fsck_instance *inst; + + if (force_all_parallel) + return 0; + + if (instance_list && instance_list->fs->stacked) + /* any instance for a stacked device is already running */ + return 1; + + if (!fs->disk) { + fs->disk = get_disk(fs->device); + if (fs->disk) + fs->stacked = count_slaves(fs->disk); + } + + /* + * If we don't know the base device, assume that the device is + * already active if there are any fsck instances running. + * + * Don't check a stacked device with any other disk too. + */ + if (!fs->disk || fs->stacked) + return (instance_list != 0); + + for (inst = instance_list; inst; inst = inst->next) { + if (!inst->fs->disk || fs->disk == inst->fs->disk) + return 1; + } + return 0; } /* Check all file systems, using the /etc/fstab table. */ @@ -1038,7 +1167,7 @@ static int check_all(NOARGS) * already been spawned, then we need to defer * this to another pass. */ - if (device_already_active(fs->device)) { + if (disk_already_active(fs)) { pass_done = 0; continue; } @@ -1188,6 +1317,9 @@ static void PRS(int argc, char *argv[]) } } break; + case 'l': + lockdisk++; + break; case 'V': verbose++; break; @@ -1298,6 +1430,12 @@ int main(int argc, char *argv[]) if ((num_devices == 1) || (serialize)) interactive = 1; + if (lockdisk && (doall || num_devices > 1)) { + fprintf(stderr, _("%s: the -l option can be used with one " + "device only -- ignore\n"), progname); + lockdisk = 0; + } + /* If -A was specified ("check all"), do that! */ if (doall) return check_all(); diff --git a/fsck/fsck.h b/fsck/fsck.h index 45b7844..ab171ba 100644 --- a/fsck/fsck.h +++ b/fsck/fsck.h @@ -44,6 +44,8 @@ struct fs_info { int freq; int passno; int flags; + dev_t disk; + int stacked; struct fs_info *next; }; @@ -56,12 +58,13 @@ struct fs_info { struct fsck_instance { int pid; int flags; + int lock; /* flock()ed whole disk file descriptor or -1 */ int exit_status; time_t start_time; char * prog; char * type; - char * device; char * base_device; + struct fs_info *fs; struct fsck_instance *next; };