]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
check if fs is mounted before running fsck
authorKent Overstreet <kent.overstreet@gmail.com>
Mon, 25 Mar 2019 01:07:45 +0000 (21:07 -0400)
committerKent Overstreet <kent.overstreet@gmail.com>
Mon, 25 Mar 2019 01:07:45 +0000 (21:07 -0400)
cmd_device.c
cmd_fsck.c
tools-util.c
tools-util.h

index 797b958c0bb32a8fe8f45cde0ed9694f9c77d5d6..92d3125b133dd4a8e34bc97a6a98dc3890124e11 100644 (file)
@@ -385,14 +385,14 @@ int cmd_device_resize(int argc, char *argv[])
 
        struct stat dev_stat = xfstat(dev_fd);
 
-       char *mount = dev_to_mount(dev);
+       struct mntent *mount = dev_to_mount(dev);
        if (mount) {
                if (!S_ISBLK(dev_stat.st_mode))
                        die("%s is mounted but isn't a block device?!", dev);
 
                printf("Doing online resize of %s\n", dev);
 
-               struct bchfs_handle fs = bcache_fs_open(mount);
+               struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
 
                unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
 
index 617cf25270253aeb1efdad128234f30d2e87bbb0..e0961b99694c3e71765f97191c6fc1235cb9b7df 100644 (file)
@@ -23,6 +23,7 @@ static void usage(void)
 int cmd_fsck(int argc, char *argv[])
 {
        struct bch_opts opts = bch2_opts_empty();
+       unsigned i;
        int opt, ret = 0;
 
        opt_set(opts, degraded, true);
@@ -56,6 +57,10 @@ int cmd_fsck(int argc, char *argv[])
        if (!argc)
                die("Please supply device(s) to check");
 
+       for (i = 0; i < argc; i++)
+               if (dev_mounted_rw(argv[i]))
+                       die("%s is mounted read-write - aborting", argv[i]);
+
        struct bch_fs *c = bch2_fs_open(argv, argc, opts);
        if (IS_ERR(c))
                die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
index 486bbacf2a92cf0292d5eb6a84f5f43a069122bb..e4d2beae232f53d5418b146521f54275b0a45d15 100644 (file)
@@ -610,26 +610,18 @@ char *dev_to_path(dev_t dev)
        return path;
 }
 
-char *dev_to_mount(char *dev)
+struct mntent *dev_to_mount(char *dev)
 {
-       char *line = NULL, *ret = NULL;
-       size_t n = 0;
-
-       FILE *f = fopen("/proc/mounts", "r");
+       struct mntent *mnt, *ret = NULL;
+       FILE *f = setmntent("/proc/mounts", "r");
        if (!f)
                die("error opening /proc/mounts: %m");
 
        struct stat d1 = xstat(dev);
 
-       while (getline(&line, &n, f) != -1) {
-               char *d, *p = line;
-               char *devs = strsep(&p, " ");
-               char *mount = strsep(&p, " ");
+       while ((mnt = getmntent(f))) {
+               char *d, *p = mnt->mnt_fsname;
 
-               if (!devs || !mount)
-                       continue;
-
-               p = devs;
                while ((d = strsep(&p, ":"))) {
                        struct stat d2;
 
@@ -648,12 +640,18 @@ char *dev_to_mount(char *dev)
                                        continue;
                        }
 
-                       ret = strdup(mount);
+                       ret = mnt;
                        goto found;
                }
        }
 found:
        fclose(f);
-       free(line);
        return ret;
 }
+
+bool dev_mounted_rw(char *dev)
+{
+       struct mntent *mnt = dev_to_mount(dev);
+
+       return mnt && !hasmntopt(mnt, "ro");
+}
index ae63f723621d899e57e2963471cd4bc90cd46a41..e5c3508474b9431c60a6d63de59cce99965b4e45 100644 (file)
@@ -2,6 +2,7 @@
 #define _TOOLS_UTIL_H
 
 #include <errno.h>
+#include <mntent.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -153,7 +154,8 @@ u32 crc32c(u32, const void *, size_t);
 
 char *dev_to_name(dev_t);
 char *dev_to_path(dev_t);
-char *dev_to_mount(char *);
+struct mntent *dev_to_mount(char *);
+bool dev_mounted_rw(char *);
 
 #define args_shift(_nr)                                                        \
 do {                                                                   \