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