naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
ecbff1
From 81f0a57bed6e03eeaa24443d16555c7f5d20ee1a Mon Sep 17 00:00:00 2001
ecbff1
From: Lennart Poettering <lennart@poettering.net>
ecbff1
Date: Wed, 22 Apr 2015 13:08:19 +0200
ecbff1
Subject: [PATCH] btrfs-util: introduce btrfs_is_filesystem() and make use of
ecbff1
 it where appropriate
ecbff1
ecbff1
Let's unify the code that checks whether an fd is on btrfs a bit.
ecbff1
ecbff1
(Also, rename btrfs_is_snapshot() to btrfs_is_subvol(), since that's
ecbff1
usually how this is referred to in our code)
ecbff1
ecbff1
(cherry picked from commit 21222ea5cdec65fa30a75bd5a78475459075b946)
ecbff1
ecbff1
Related: #1299714
ecbff1
---
ecbff1
 src/shared/btrfs-util.c    | 23 ++++++++++++++++-------
ecbff1
 src/shared/btrfs-util.h    |  3 ++-
ecbff1
 src/shared/machine-image.c |  9 ++++-----
ecbff1
 3 files changed, 22 insertions(+), 13 deletions(-)
ecbff1
ecbff1
diff --git a/src/shared/btrfs-util.c b/src/shared/btrfs-util.c
ecbff1
index b34ac8b15..52a214349 100644
ecbff1
--- a/src/shared/btrfs-util.c
ecbff1
+++ b/src/shared/btrfs-util.c
ecbff1
@@ -83,10 +83,22 @@ static int extract_subvolume_name(const char *path, const char **subvolume) {
ecbff1
         return 0;
ecbff1
 }
ecbff1
 
ecbff1
-int btrfs_is_snapshot(int fd) {
ecbff1
-        struct stat st;
ecbff1
+int btrfs_is_filesystem(int fd) {
ecbff1
         struct statfs sfs;
ecbff1
 
ecbff1
+        assert(fd >= 0);
ecbff1
+
ecbff1
+        if (fstatfs(fd, &sfs) < 0)
ecbff1
+                return -errno;
ecbff1
+
ecbff1
+        return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
ecbff1
+}
ecbff1
+
ecbff1
+int btrfs_is_subvol(int fd) {
ecbff1
+        struct stat st;
ecbff1
+
ecbff1
+        assert(fd >= 0);
ecbff1
+
ecbff1
         /* On btrfs subvolumes always have the inode 256 */
ecbff1
 
ecbff1
         if (fstat(fd, &st) < 0)
ecbff1
@@ -95,10 +107,7 @@ int btrfs_is_snapshot(int fd) {
ecbff1
         if (!S_ISDIR(st.st_mode) || st.st_ino != 256)
ecbff1
                 return 0;
ecbff1
 
ecbff1
-        if (fstatfs(fd, &sfs) < 0)
ecbff1
-                return -errno;
ecbff1
-
ecbff1
-        return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
ecbff1
+        return btrfs_is_filesystem(fd);
ecbff1
 }
ecbff1
 
ecbff1
 int btrfs_subvol_snapshot(const char *old_path, const char *new_path, bool read_only, bool fallback_copy) {
ecbff1
@@ -115,7 +124,7 @@ int btrfs_subvol_snapshot(const char *old_path, const char *new_path, bool read_
ecbff1
         if (old_fd < 0)
ecbff1
                 return -errno;
ecbff1
 
ecbff1
-        r = btrfs_is_snapshot(old_fd);
ecbff1
+        r = btrfs_is_subvol(old_fd);
ecbff1
         if (r < 0)
ecbff1
                 return r;
ecbff1
         if (r == 0) {
ecbff1
diff --git a/src/shared/btrfs-util.h b/src/shared/btrfs-util.h
ecbff1
index 1b9c142e5..1315def87 100644
ecbff1
--- a/src/shared/btrfs-util.h
ecbff1
+++ b/src/shared/btrfs-util.h
ecbff1
@@ -43,7 +43,8 @@ typedef struct BtrfsQuotaInfo {
ecbff1
         uint64_t exclusive_max;
ecbff1
 } BtrfsQuotaInfo;
ecbff1
 
ecbff1
-int btrfs_is_snapshot(int fd);
ecbff1
+int btrfs_is_filesystem(int fd);
ecbff1
+int btrfs_is_subvol(int fd);
ecbff1
 
ecbff1
 int btrfs_subvol_make(const char *path);
ecbff1
 int btrfs_subvol_make_label(const char *path);
ecbff1
diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c
ecbff1
index c02ee814c..256622928 100644
ecbff1
--- a/src/shared/machine-image.c
ecbff1
+++ b/src/shared/machine-image.c
ecbff1
@@ -136,12 +136,11 @@ static int image_make(
ecbff1
 
ecbff1
                 /* btrfs subvolumes have inode 256 */
ecbff1
                 if (st.st_ino == 256) {
ecbff1
-                        struct statfs sfs;
ecbff1
 
ecbff1
-                        if (fstatfs(fd, &sfs) < 0)
ecbff1
-                                return -errno;
ecbff1
-
ecbff1
-                        if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
ecbff1
+                        r = btrfs_is_filesystem(fd);
ecbff1
+                        if (r < 0)
ecbff1
+                                return r;
ecbff1
+                        if (r) {
ecbff1
                                 BtrfsSubvolInfo info;
ecbff1
                                 BtrfsQuotaInfo quota;
ecbff1