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