Skip to content

Commit 65ddc2c

Browse files
committed
basic: drop one btrfs-related function and move another
This will become useful later, it is the first step to moving btrfs-util.[ch] out of src/basic/.
1 parent 9c65353 commit 65ddc2c

File tree

7 files changed

+45
-50
lines changed

7 files changed

+45
-50
lines changed

src/basic/btrfs-util.c

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ static int extract_subvolume_name(const char *path, const char **subvolume) {
6969
return 0;
7070
}
7171

72-
int btrfs_is_filesystem(int fd) {
73-
struct statfs sfs;
74-
75-
assert(fd >= 0);
76-
77-
if (fstatfs(fd, &sfs) < 0)
78-
return -errno;
79-
80-
return F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC);
81-
}
82-
8372
int btrfs_is_subvol_fd(int fd) {
8473
struct stat st;
8574

@@ -93,7 +82,7 @@ int btrfs_is_subvol_fd(int fd) {
9382
if (!btrfs_might_be_subvol(&st))
9483
return 0;
9584

96-
return btrfs_is_filesystem(fd);
85+
return fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
9786
}
9887

9988
int btrfs_is_subvol(const char *path) {
@@ -286,7 +275,7 @@ int btrfs_get_block_device_fd(int fd, dev_t *dev) {
286275
assert(fd >= 0);
287276
assert(dev);
288277

289-
r = btrfs_is_filesystem(fd);
278+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
290279
if (r < 0)
291280
return r;
292281
if (!r)
@@ -361,7 +350,7 @@ int btrfs_subvol_get_id_fd(int fd, uint64_t *ret) {
361350
assert(fd >= 0);
362351
assert(ret);
363352

364-
r = btrfs_is_filesystem(fd);
353+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
365354
if (r < 0)
366355
return r;
367356
if (!r)
@@ -481,7 +470,7 @@ int btrfs_subvol_get_info_fd(int fd, uint64_t subvol_id, BtrfsSubvolInfo *ret) {
481470
if (r < 0)
482471
return r;
483472
} else {
484-
r = btrfs_is_filesystem(fd);
473+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
485474
if (r < 0)
486475
return r;
487476
if (!r)
@@ -575,7 +564,7 @@ int btrfs_qgroup_get_quota_fd(int fd, uint64_t qgroupid, BtrfsQuotaInfo *ret) {
575564
if (r < 0)
576565
return r;
577566
} else {
578-
r = btrfs_is_filesystem(fd);
567+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
579568
if (r < 0)
580569
return r;
581570
if (!r)
@@ -762,21 +751,6 @@ int btrfs_subvol_get_subtree_quota(const char *path, uint64_t subvol_id, BtrfsQu
762751
return btrfs_subvol_get_subtree_quota_fd(fd, subvol_id, ret);
763752
}
764753

765-
int btrfs_defrag_fd(int fd) {
766-
int r;
767-
768-
assert(fd >= 0);
769-
770-
r = fd_verify_regular(fd);
771-
if (r < 0)
772-
return r;
773-
774-
if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
775-
return -errno;
776-
777-
return 0;
778-
}
779-
780754
int btrfs_defrag(const char *p) {
781755
_cleanup_close_ int fd = -1;
782756

@@ -795,7 +769,7 @@ int btrfs_quota_enable_fd(int fd, bool b) {
795769

796770
assert(fd >= 0);
797771

798-
r = btrfs_is_filesystem(fd);
772+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
799773
if (r < 0)
800774
return r;
801775
if (!r)
@@ -832,7 +806,7 @@ int btrfs_qgroup_set_limit_fd(int fd, uint64_t qgroupid, uint64_t referenced_max
832806
if (r < 0)
833807
return r;
834808
} else {
835-
r = btrfs_is_filesystem(fd);
809+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
836810
if (r < 0)
837811
return r;
838812
if (!r)
@@ -924,7 +898,7 @@ static int qgroup_create_or_destroy(int fd, bool b, uint64_t qgroupid) {
924898
};
925899
int r;
926900

927-
r = btrfs_is_filesystem(fd);
901+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
928902
if (r < 0)
929903
return r;
930904
if (r == 0)
@@ -1042,7 +1016,7 @@ static int qgroup_assign_or_unassign(int fd, bool b, uint64_t child, uint64_t pa
10421016
};
10431017
int r;
10441018

1045-
r = btrfs_is_filesystem(fd);
1019+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
10461020
if (r < 0)
10471021
return r;
10481022
if (r == 0)
@@ -1269,7 +1243,7 @@ int btrfs_qgroup_copy_limits(int fd, uint64_t old_qgroupid, uint64_t new_qgroupi
12691243

12701244
int r;
12711245

1272-
r = btrfs_is_filesystem(fd);
1246+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
12731247
if (r < 0)
12741248
return r;
12751249
if (!r)
@@ -1738,7 +1712,7 @@ int btrfs_qgroup_find_parents(int fd, uint64_t qgroupid, uint64_t **ret) {
17381712
if (r < 0)
17391713
return r;
17401714
} else {
1741-
r = btrfs_is_filesystem(fd);
1715+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
17421716
if (r < 0)
17431717
return r;
17441718
if (!r)
@@ -1979,7 +1953,7 @@ int btrfs_subvol_get_parent(int fd, uint64_t subvol_id, uint64_t *ret) {
19791953
if (r < 0)
19801954
return r;
19811955
} else {
1982-
r = btrfs_is_filesystem(fd);
1956+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
19831957
if (r < 0)
19841958
return r;
19851959
if (!r)

src/basic/btrfs-util.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ typedef enum BtrfsRemoveFlags {
4242
BTRFS_REMOVE_QUOTA = 1 << 1,
4343
} BtrfsRemoveFlags;
4444

45-
int btrfs_is_filesystem(int fd);
46-
4745
int btrfs_is_subvol_fd(int fd);
4846
int btrfs_is_subvol(const char *path);
4947

@@ -53,7 +51,6 @@ int btrfs_clone_range(int infd, uint64_t in_offset, int ofd, uint64_t out_offset
5351
int btrfs_get_block_device_fd(int fd, dev_t *dev);
5452
int btrfs_get_block_device(const char *path, dev_t *dev);
5553

56-
int btrfs_defrag_fd(int fd);
5754
int btrfs_defrag(const char *p);
5855

5956
int btrfs_quota_enable_fd(int fd, bool b);

src/basic/fd-util.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include <errno.h>
44
#include <fcntl.h>
5+
#include <linux/btrfs.h>
6+
#include <linux/magic.h>
7+
#include <sys/ioctl.h>
58
#include <sys/resource.h>
69
#include <sys/stat.h>
710
#include <unistd.h>
@@ -1057,3 +1060,20 @@ int read_nr_open(void) {
10571060
/* If we fail, fall back to the hard-coded kernel limit of 1024 * 1024. */
10581061
return 1024 * 1024;
10591062
}
1063+
1064+
/* This is here because it's fd-related and is called from sd-journal code. Other btrfs-related utilities are
1065+
* in src/shared, but libsystemd must not link to libsystemd-shared, see docs/ARCHITECTURE.md. */
1066+
int btrfs_defrag_fd(int fd) {
1067+
int r;
1068+
1069+
assert(fd >= 0);
1070+
1071+
r = fd_verify_regular(fd);
1072+
if (r < 0)
1073+
return r;
1074+
1075+
if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
1076+
return -errno;
1077+
1078+
return 0;
1079+
}

src/basic/fd-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ static inline int make_null_stdio(void) {
107107

108108

109109
int fd_reopen(int fd, int flags);
110-
111110
int read_nr_open(void);
111+
int btrfs_defrag_fd(int fd);

src/libsystemd/sd-journal/journal-file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <errno.h>
44
#include <fcntl.h>
55
#include <linux/fs.h>
6+
#include <linux/magic.h>
67
#include <pthread.h>
78
#include <stddef.h>
89
#include <sys/mman.h>
@@ -13,7 +14,6 @@
1314
#include "sd-event.h"
1415

1516
#include "alloc-util.h"
16-
#include "btrfs-util.h"
1717
#include "chattr-util.h"
1818
#include "compress.h"
1919
#include "env-util.h"
@@ -3379,7 +3379,7 @@ static int journal_file_warn_btrfs(JournalFile *f) {
33793379
* expense of data integrity features (which shouldn't be too
33803380
* bad, given that we do our own checksumming). */
33813381

3382-
r = btrfs_is_filesystem(f->fd);
3382+
r = fd_is_fs_type(f->fd, BTRFS_SUPER_MAGIC);
33833383
if (r < 0)
33843384
return log_warning_errno(r, "Failed to determine if journal is on btrfs: %m");
33853385
if (!r)

src/shared/discover-image.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <fcntl.h>
55
#include <linux/fs.h>
66
#include <linux/loop.h>
7+
#include <linux/magic.h>
78
#include <stdio.h>
89
#include <stdlib.h>
910
#include <sys/file.h>
@@ -34,6 +35,7 @@
3435
#include "os-util.h"
3536
#include "path-util.h"
3637
#include "rm-rf.h"
38+
#include "stat-util.h"
3739
#include "string-table.h"
3840
#include "string-util.h"
3941
#include "strv.h"
@@ -262,7 +264,7 @@ static int image_make(
262264

263265
if (btrfs_might_be_subvol(st)) {
264266

265-
r = btrfs_is_filesystem(fd);
267+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
266268
if (r < 0)
267269
return r;
268270
if (r) {

src/shared/sleep-config.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <errno.h>
77
#include <fcntl.h>
88
#include <linux/fs.h>
9+
#include <linux/magic.h>
910
#include <stdbool.h>
1011
#include <stddef.h>
1112
#include <sys/ioctl.h>
@@ -28,6 +29,7 @@
2829
#include "parse-util.h"
2930
#include "path-util.h"
3031
#include "sleep-config.h"
32+
#include "stat-util.h"
3133
#include "stdio-util.h"
3234
#include "string-table.h"
3335
#include "string-util.h"
@@ -232,7 +234,7 @@ static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offse
232234
_cleanup_close_ int fd = -1;
233235
_cleanup_free_ struct fiemap *fiemap = NULL;
234236
struct stat sb;
235-
int r, btrfs;
237+
int r;
236238

237239
assert(swap);
238240
assert(swap->device);
@@ -245,10 +247,10 @@ static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offse
245247
if (fstat(fd, &sb) < 0)
246248
return log_debug_errno(errno, "Failed to stat %s: %m", swap->device);
247249

248-
btrfs = btrfs_is_filesystem(fd);
249-
if (btrfs < 0)
250-
return log_debug_errno(btrfs, "Error checking %s for Btrfs filesystem: %m", swap->device);
251-
if (btrfs > 0) {
250+
r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
251+
if (r < 0)
252+
return log_debug_errno(r, "Error checking %s for Btrfs filesystem: %m", swap->device);
253+
if (r > 0) {
252254
log_debug("%s: detection of swap file offset on Btrfs is not supported", swap->device);
253255
*ret_offset = UINT64_MAX;
254256
return 0;

0 commit comments

Comments
 (0)