Skip to contentSkip to conversation

btrfs-progs: mounted filesystems checks

PATCH In review Andi Drebes 2 participants 6 messages Nov 21, 2009 – Nov 23, 2009 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs-progs: mounted filesystems checks

The following two patches add code that prevents btrfsck to run on mounted filesystems. The first patch makes check_mounted() work with multidevice filesystems. The second patch finally adds the check to btrfsck.

Cheers,
Andi

#1 lore raw

PATCH 1/2 btrfs-progs: multidevice support for check_mounted

Check_mount() should also work with multi device filesystems. This patch adds checks that allow to detect if a file is a device file used by a mounted single or multi device btrfs or if it is a regular file used by a loopback device that is part of a mounted single or multi device btrfs.

The single device checks also work for non-btrfs filesystems. This might be helpful to prevent users from running btrfs programs (e.g. mkfs.btrfs) accidentally on a filesystem used somewhere else.

---

kerncompat.h +4 -0
index e4c8ce0..46236cd 100644
@@ -42,7 +42,11 @@
 #define GFP_NOFS 0
 #define __read_mostly
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+#ifndef ULONG_MAX
 #define ULONG_MAX       (~0UL)
+#endif
+
 #define BUG() abort()
 #ifdef __CHECKER__
 #define __force    __attribute__((force))
utils.c +206 -33

267 lines not on this page · raw message

utils.h +1 -1
index 7ff542b..9dce5b0 100644
@@ -36,7 +36,7 @@ int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
 			int run_ioctls);
 void btrfs_register_one_device(char *fname);
 int btrfs_scan_one_dir(char *dirname, int run_ioctl);
-int check_mounted(char *devicename);
+int check_mounted(const char *devicename);
 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
 				 int super_offset);
 char *pretty_sizes(u64 size);
#2 lore raw

Re: [PATCH 1/2] btrfs-progs: multidevice support for check_mounted

Quoted 5 lines · Andi Drebes
utils.c +5 -0
+
+/* Checks if an mntentry represents a pseudo FS */
+int is_pseudo_fs(const struct mntent* mnt)
+{
+	struct stat st_buf;

utils.c error: · line 723

Quoted 10 lines
utils.c +10 -0
+
+	if(stat(mnt->mnt_fsname, &st_buf) < 0) {
+		if(errno == ENOENT)
+			return 1;
+		else
+			return -errno;
+	}
+
+	return 0;
+}

This is bad idea. The mnt_fsname field could be an arbitrary string include valid paths.

        # grep sysfs /proc/mounts 
        /sys /sys sysfs rw,relatime 0 0

Karel

Signature · 1 line
 Karel Zak  <[email protected]>
#3 lore raw

Re: [PATCH 1/2] btrfs-progs: multidevice support for check_mounted

Hi!
Thanks for the reply!

Quoted 19 lines
Quoted 5 lines
(quoted diff) +5 -0
+
+/* Checks if an mntentry represents a pseudo FS */
+int is_pseudo_fs(const struct mntent* mnt)
+{
+	struct stat st_buf;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	if(stat(mnt->mnt_fsname, &st_buf) < 0) {
+		if(errno == ENOENT)
+			return 1;
+		else
+			return -errno;
+	}
+
+	return 0;
+}

This is bad idea. The mnt_fsname field could be an arbitrary string include valid paths.

Quoted 2 lines
        # grep sysfs /proc/mounts 
        /sys /sys sysfs rw,relatime 0 0

Yes, indeed. Although this doesn't really cause problems (because if the string is a valid path, we would just do one check too much), the name of the function doesn't really correspond to what it does. In the new patch below, is_pseudo_fs() is replaced by is_existing_blk_or_reg_file(). We ignore entries associated with an invalid path or paths that don't point to a regular or block file. However, if a path used in a pseudo-filesystem entry points to the file that is being checked, check_mounted() returns 1. In my eyes, this is extremely unlikely.

Here's the new patch:

---

kerncompat.h +4 -0
index e4c8ce0..46236cd 100644
@@ -42,7 +42,11 @@
 #define GFP_NOFS 0
 #define __read_mostly
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+#ifndef ULONG_MAX
 #define ULONG_MAX       (~0UL)
+#endif
+
 #define BUG() abort()
 #ifdef __CHECKER__
 #define __force    __attribute__((force))
utils.c +205 -32

266 lines not on this page · raw message

utils.h +1 -1
index 7ff542b..9dce5b0 100644
@@ -36,7 +36,7 @@ int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
 			int run_ioctls);
 void btrfs_register_one_device(char *fname);
 int btrfs_scan_one_dir(char *dirname, int run_ioctl);
-int check_mounted(char *devicename);
+int check_mounted(const char *devicename);
 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
 				 int super_offset);
 char *pretty_sizes(u64 size);
#4 lore raw

Re: [PATCH 1/2] btrfs-progs: multidevice support for check_mounted

Quoted 6 lines · Andi Drebes

In the new patch below, is_pseudo_fs() is replaced by is_existing_blk_or_reg_file(). We ignore entries associated with an invalid path or paths that don't point to a regular or block file. However, if a path used in a pseudo-filesystem entry points to the file that is being checked, check_mounted() returns 1. In my eyes, this is extremely unlikely.

...

Quoted 4 lines
utils.c +4 -0
+/* Checks if a file exists and is a block or regular file*/
+int is_existing_blk_or_reg_file(const char* filename)
+{
+	struct stat st_buf;

utils.c error: · line 723

Quoted 10 lines
utils.c +10 -0
+
+	if(stat(filename, &st_buf) < 0) {
+		if(errno == ENOENT)
+			return 0;
+		else
+			return -errno;
+	}
+
+	return (S_ISBLK(st_buf.st_mode) || S_ISREG(st_buf.st_mode));
+}

Yes, this looks better.

Karel

Signature · 1 line
 Karel Zak  <[email protected]>
#5 lore raw

PATCH 2/2 btrfs-progs: prevent btrfsck to run on mounted filesystems

In reply to Andi Drebes

As recently discussed on the list, btrfsck should only be run on unmounted filesystems. This patch adds a short check for the mount status at the beginning of btrfsck. If the FS is mounted, the program aborts showing an error message.

---

btrfsck.c +10 -0
index 73f1836..6f2a0d0 100644
@@ -28,6 +28,7 @@
 #include "transaction.h"
 #include "list.h"
 #include "version.h"
+#include "utils.h"
 
 static u64 bytes_used = 0;
 static u64 total_csum_bytes = 0;
@@ -2821,6 +2822,15 @@ int main(int ac, char **av)
 
 	radix_tree_init();
 	cache_tree_init(&root_cache);
+
+	if((ret = check_mounted(av[1])) < 0) {
+		fprintf(stderr, "Could not check mount status: %s\n", strerror(ret));
+		return ret;
+	} else if(ret) {
+		fprintf(stderr, "%s is currently mounted. Aborting.\n", av[1]);
+		return -EBUSY;
+	}
+
 	root = open_ctree(av[1], 0, 0);
 
 	if (root == NULL)