]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: drop one btrfs-related function and move another
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 21 Jun 2021 18:24:00 +0000 (20:24 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 24 Jun 2021 07:42:08 +0000 (09:42 +0200)
This will become useful later, it is the first step to moving btrfs-util.[ch]
out of src/basic/.

src/basic/btrfs-util.c
src/basic/btrfs-util.h
src/basic/fd-util.c
src/basic/fd-util.h
src/libsystemd/sd-journal/journal-file.c
src/shared/discover-image.c
src/shared/sleep-config.c

index adf9d03eb90a1a240787156d723551d2fa4b9a80..f4e291385bebb5bd826abce8f5524f5faf9996e1 100644 (file)
@@ -69,17 +69,6 @@ static int extract_subvolume_name(const char *path, const char **subvolume) {
         return 0;
 }
 
-int btrfs_is_filesystem(int fd) {
-        struct statfs sfs;
-
-        assert(fd >= 0);
-
-        if (fstatfs(fd, &sfs) < 0)
-                return -errno;
-
-        return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
-}
-
 int btrfs_is_subvol_fd(int fd) {
         struct stat st;
 
@@ -93,7 +82,7 @@ int btrfs_is_subvol_fd(int fd) {
         if (!btrfs_might_be_subvol(&st))
                 return 0;
 
-        return btrfs_is_filesystem(fd);
+        return fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
 }
 
 int btrfs_is_subvol(const char *path) {
@@ -286,7 +275,7 @@ int btrfs_get_block_device_fd(int fd, dev_t *dev) {
         assert(fd >= 0);
         assert(dev);
 
-        r = btrfs_is_filesystem(fd);
+        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
         if (r < 0)
                 return r;
         if (!r)
@@ -361,7 +350,7 @@ int btrfs_subvol_get_id_fd(int fd, uint64_t *ret) {
         assert(fd >= 0);
         assert(ret);
 
-        r = btrfs_is_filesystem(fd);
+        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
         if (r < 0)
                 return r;
         if (!r)
@@ -481,7 +470,7 @@ int btrfs_subvol_get_info_fd(int fd, uint64_t subvol_id, BtrfsSubvolInfo *ret) {
                 if (r < 0)
                         return r;
         } else {
-                r = btrfs_is_filesystem(fd);
+                r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
                 if (r < 0)
                         return r;
                 if (!r)
@@ -575,7 +564,7 @@ int btrfs_qgroup_get_quota_fd(int fd, uint64_t qgroupid, BtrfsQuotaInfo *ret) {
                 if (r < 0)
                         return r;
         } else {
-                r = btrfs_is_filesystem(fd);
+                r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
                 if (r < 0)
                         return r;
                 if (!r)
@@ -762,21 +751,6 @@ int btrfs_subvol_get_subtree_quota(const char *path, uint64_t subvol_id, BtrfsQu
         return btrfs_subvol_get_subtree_quota_fd(fd, subvol_id, ret);
 }
 
-int btrfs_defrag_fd(int fd) {
-        int r;
-
-        assert(fd >= 0);
-
-        r = fd_verify_regular(fd);
-        if (r < 0)
-                return r;
-
-        if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
-                return -errno;
-
-        return 0;
-}
-
 int btrfs_defrag(const char *p) {
         _cleanup_close_ int fd = -1;
 
@@ -795,7 +769,7 @@ int btrfs_quota_enable_fd(int fd, bool b) {
 
         assert(fd >= 0);
 
-        r = btrfs_is_filesystem(fd);
+        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
         if (r < 0)
                 return r;
         if (!r)
@@ -832,7 +806,7 @@ int btrfs_qgroup_set_limit_fd(int fd, uint64_t qgroupid, uint64_t referenced_max
                 if (r < 0)
                         return r;
         } else {
-                r = btrfs_is_filesystem(fd);
+                r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
                 if (r < 0)
                         return r;
                 if (!r)
@@ -924,7 +898,7 @@ static int qgroup_create_or_destroy(int fd, bool b, uint64_t qgroupid) {
         };
         int r;
 
-        r = btrfs_is_filesystem(fd);
+        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
         if (r < 0)
                 return r;
         if (r == 0)
@@ -1042,7 +1016,7 @@ static int qgroup_assign_or_unassign(int fd, bool b, uint64_t child, uint64_t pa
         };
         int r;
 
-        r = btrfs_is_filesystem(fd);
+        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
         if (r < 0)
                 return r;
         if (r == 0)
@@ -1269,7 +1243,7 @@ int btrfs_qgroup_copy_limits(int fd, uint64_t old_qgroupid, uint64_t new_qgroupi
 
         int r;
 
-        r = btrfs_is_filesystem(fd);
+        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
         if (r < 0)
                 return r;
         if (!r)
@@ -1738,7 +1712,7 @@ int btrfs_qgroup_find_parents(int fd, uint64_t qgroupid, uint64_t **ret) {
                 if (r < 0)
                         return r;
         } else {
-                r = btrfs_is_filesystem(fd);
+                r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
                 if (r < 0)
                         return r;
                 if (!r)
@@ -1979,7 +1953,7 @@ int btrfs_subvol_get_parent(int fd, uint64_t subvol_id, uint64_t *ret) {
                 if (r < 0)
                         return r;
         } else {
-                r = btrfs_is_filesystem(fd);
+                r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
                 if (r < 0)
                         return r;
                 if (!r)
index 0f569b6f5046ea2685d0ae371d30d8f5072c051c..7b18f57719aa5cd660fed09a5d07c64febd44653 100644 (file)
@@ -42,8 +42,6 @@ typedef enum BtrfsRemoveFlags {
         BTRFS_REMOVE_QUOTA     = 1 << 1,
 } BtrfsRemoveFlags;
 
-int btrfs_is_filesystem(int fd);
-
 int btrfs_is_subvol_fd(int fd);
 int btrfs_is_subvol(const char *path);
 
@@ -53,7 +51,6 @@ int btrfs_clone_range(int infd, uint64_t in_offset, int ofd, uint64_t out_offset
 int btrfs_get_block_device_fd(int fd, dev_t *dev);
 int btrfs_get_block_device(const char *path, dev_t *dev);
 
-int btrfs_defrag_fd(int fd);
 int btrfs_defrag(const char *p);
 
 int btrfs_quota_enable_fd(int fd, bool b);
index 1a873601b275479c76b400c1f958b02877149013..ac6a37b5678fb1c0577c716894c54bcc8ff597db 100644 (file)
@@ -2,6 +2,9 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <linux/btrfs.h>
+#include <linux/magic.h>
+#include <sys/ioctl.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -1057,3 +1060,20 @@ int read_nr_open(void) {
         /* If we fail, fall back to the hard-coded kernel limit of 1024 * 1024. */
         return 1024 * 1024;
 }
+
+/* This is here because it's fd-related and is called from sd-journal code. Other btrfs-related utilities are
+ * in src/shared, but libsystemd must not link to libsystemd-shared, see docs/ARCHITECTURE.md. */
+int btrfs_defrag_fd(int fd) {
+        int r;
+
+        assert(fd >= 0);
+
+        r = fd_verify_regular(fd);
+        if (r < 0)
+                return r;
+
+        if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
+                return -errno;
+
+        return 0;
+}
index aa8e082b38d665b6b56e729faa4d24d153537767..eb696762b6ada6d6774cc8091fd6929ba78b9df7 100644 (file)
@@ -107,5 +107,5 @@ static inline int make_null_stdio(void) {
 
 
 int fd_reopen(int fd, int flags);
-
 int read_nr_open(void);
+int btrfs_defrag_fd(int fd);
index 64aa132c7a00c79648ff2877cdb847452f805886..2c17435de2d621cc506c7d77f83857d1cdab2fc7 100644 (file)
@@ -3,6 +3,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <linux/fs.h>
+#include <linux/magic.h>
 #include <pthread.h>
 #include <stddef.h>
 #include <sys/mman.h>
@@ -13,7 +14,6 @@
 #include "sd-event.h"
 
 #include "alloc-util.h"
-#include "btrfs-util.h"
 #include "chattr-util.h"
 #include "compress.h"
 #include "env-util.h"
@@ -3379,7 +3379,7 @@ static int journal_file_warn_btrfs(JournalFile *f) {
          * expense of data integrity features (which shouldn't be too
          * bad, given that we do our own checksumming). */
 
-        r = btrfs_is_filesystem(f->fd);
+        r = fd_is_fs_type(f->fd, BTRFS_SUPER_MAGIC);
         if (r < 0)
                 return log_warning_errno(r, "Failed to determine if journal is on btrfs: %m");
         if (!r)
index c572e8dae814a4c2f07f0e5e41fad0079cf6a053..5c833afc788b0c8b15143a7b82caa2cbc4485b68 100644 (file)
@@ -4,6 +4,7 @@
 #include <fcntl.h>
 #include <linux/fs.h>
 #include <linux/loop.h>
+#include <linux/magic.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/file.h>
@@ -34,6 +35,7 @@
 #include "os-util.h"
 #include "path-util.h"
 #include "rm-rf.h"
+#include "stat-util.h"
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
@@ -262,7 +264,7 @@ static int image_make(
 
                 if (btrfs_might_be_subvol(st)) {
 
-                        r = btrfs_is_filesystem(fd);
+                        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
                         if (r < 0)
                                 return r;
                         if (r) {
index 645b7e242ee9314561821b346a5c47520bf917b5..dbaecb3a0f2c981c4b1145b4f329ee3cf5113a33 100644 (file)
@@ -6,6 +6,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <linux/fs.h>
+#include <linux/magic.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <sys/ioctl.h>
@@ -28,6 +29,7 @@
 #include "parse-util.h"
 #include "path-util.h"
 #include "sleep-config.h"
+#include "stat-util.h"
 #include "stdio-util.h"
 #include "string-table.h"
 #include "string-util.h"
@@ -232,7 +234,7 @@ static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offse
         _cleanup_close_ int fd = -1;
         _cleanup_free_ struct fiemap *fiemap = NULL;
         struct stat sb;
-        int r, btrfs;
+        int r;
 
         assert(swap);
         assert(swap->device);
@@ -245,10 +247,10 @@ static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offse
         if (fstat(fd, &sb) < 0)
                 return log_debug_errno(errno, "Failed to stat %s: %m", swap->device);
 
-        btrfs = btrfs_is_filesystem(fd);
-        if (btrfs < 0)
-                return log_debug_errno(btrfs, "Error checking %s for Btrfs filesystem: %m", swap->device);
-        if (btrfs > 0) {
+        r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
+        if (r < 0)
+                return log_debug_errno(r, "Error checking %s for Btrfs filesystem: %m", swap->device);
+        if (r > 0) {
                 log_debug("%s: detection of swap file offset on Btrfs is not supported", swap->device);
                 *ret_offset = UINT64_MAX;
                 return 0;