Debian Codegraph @

src/test/test-btrfs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/magic.h>
5 #include <stdlib.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8
9 #include "alloc-util.h"
10 #include "btrfs-util.h"
11 #include "chattr-util.h"
12 #include "fd-util.h"
13 #include "fileio.h"
14 #include "format-util.h"
15 #include "fs-util.h"
16 #include "log.h"
17 #include "memory-util.h"
18 #include "parse-util.h"
19 #include "path-util.h"
20 #include "pidref.h"
21 #include "process-util.h"
22 #include "rm-rf.h"
23 #include "stat-util.h"
24 #include "string-util.h"
25 #include "tests.h"
26 #include "time-util.h"
27 #include "tmpfile-util.h"
28
29 static int open_test_subvol(char **ret_path) {
30 const char *vtd;
31 int r;
32
33 r = var_tmp_dir(&vtd);
34 if (r < 0)
35 return r;
36
37 _cleanup_free_ char *base = path_join(vtd, "test-btrfs"), *p = NULL;
38 if (!base)
39 return -ENOMEM;
40
41 r = tempfn_random(base, /* extra= */ NULL, &p);
42 if (r < 0)
43 return r;
44
45 int fd = xopenat_full(AT_FDCWD, p, O_DIRECTORY|O_CREAT|O_CLOEXEC, XO_SUBVOLUME, MODE_INVALID);
46 if (fd < 0)
47 return fd;
48
49 if (ret_path)
50 *ret_path = TAKE_PTR(p);
51
52 return fd;
53 }
54
55 TEST_RET(info) {
56 _cleanup_(rm_rf_subvolume_and_freep) char *dir = NULL;
57 _cleanup_close_ int dir_fd = ASSERT_OK(open_test_subvol(&dir));
58 BtrfsSubvolInfo info;
59 BtrfsQuotaInfo quota;
60 int r;
61
62 r = btrfs_subvol_get_info_fd(dir_fd, 0, &info);
63 if (r == -ENOTTY)
64 return log_tests_skipped("BTRFS_IOC_GET_SUBVOL_INFO not supported "
65 "(missing 32-bit compat handler in kernel?)");
66 ASSERT_OK(r);
67 log_info("otime: %s", FORMAT_TIMESTAMP(info.otime));
68 log_info("read-only (search): %s", yes_no(info.read_only));
69
70 r = btrfs_qgroup_get_quota_fd(dir_fd, 0, &quota);
71 if (r < 0)
72 log_info_errno(r, "Failed to get quota info: %m");
73 else {
74 log_info("referenced: %s", strna(FORMAT_BYTES(quota.referenced)));
75 log_info("exclusive: %s", strna(FORMAT_BYTES(quota.exclusive)));
76 log_info("referenced_max: %s", strna(FORMAT_BYTES(quota.referenced_max)));
77 log_info("exclusive_max: %s", strna(FORMAT_BYTES(quota.exclusive_max)));
78 }
79
80 r = ASSERT_OK(btrfs_subvol_get_read_only_fd(dir_fd));
81 log_info("read-only (ioctl): %s", yes_no(r));
82
83 return EXIT_SUCCESS;
84 }
85
86 TEST(subvol) {
87 _cleanup_(rm_rf_subvolume_and_freep) char *dir = NULL;
88 _cleanup_close_ int dir_fd = ASSERT_OK(open_test_subvol(&dir));
89
90 ASSERT_OK(btrfs_subvol_make(dir_fd, "test1"));
91 ASSERT_OK(write_string_file_at(dir_fd, "test1/file", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE));
92
93 ASSERT_OK(btrfs_subvol_snapshot_at(dir_fd, "test1", dir_fd, "test2", 0));
94 ASSERT_OK(btrfs_subvol_snapshot_at(dir_fd, "test1", dir_fd, "test3", BTRFS_SNAPSHOT_READ_ONLY));
95
96 _unused_ _cleanup_close_ int locked_fd = ASSERT_OK(btrfs_subvol_snapshot_at(dir_fd, "test1", dir_fd, "test4", BTRFS_SNAPSHOT_LOCK_BSD));
97 ASSERT_ERROR(xopenat_lock(dir_fd, "test4", 0, LOCK_BSD, LOCK_EX|LOCK_NB), EAGAIN);
98
99 /* The destroy ioctl needs CAP_SYS_ADMIN; without it, leave cleanup to rm_rf_subvolume_and_freep. */
100 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "test1", BTRFS_REMOVE_QUOTA), -EPERM);
101 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "test2", BTRFS_REMOVE_QUOTA), -EPERM);
102 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "test3", BTRFS_REMOVE_QUOTA), -EPERM);
103 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "test4", BTRFS_REMOVE_QUOTA), -EPERM);
104 }
105
106 TEST(fallback_copy) {
107 _cleanup_(rm_rf_subvolume_and_freep) char *dir = NULL;
108 _cleanup_close_ int dir_fd = ASSERT_OK(open_test_subvol(&dir));
109
110 /* Snapshot a regular directory (not a subvolume) — exercises the FALLBACK_COPY path. */
111 ASSERT_OK_ERRNO(mkdirat(dir_fd, "src", 0755));
112 ASSERT_OK(write_string_file_at(dir_fd, "src/file1", "hello", WRITE_STRING_FILE_CREATE));
113 ASSERT_OK(write_string_file_at(dir_fd, "src/file2", "world", WRITE_STRING_FILE_CREATE));
114
115 ASSERT_OK(btrfs_subvol_snapshot_at(dir_fd, "src", dir_fd, "snap",
116 BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY));
117
118 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "snap", BTRFS_REMOVE_QUOTA), -EPERM);
119 }
120
121 TEST(recursive) {
122 _cleanup_(rm_rf_subvolume_and_freep) char *dir = NULL;
123 _cleanup_close_ int dir_fd = ASSERT_OK(open_test_subvol(&dir));
124
125 ASSERT_OK(btrfs_subvol_make(dir_fd, "rec"));
126 ASSERT_OK(btrfs_subvol_make(dir_fd, "rec/sv2"));
127 ASSERT_OK(btrfs_subvol_make(dir_fd, "rec/sv3"));
128 ASSERT_OK(btrfs_subvol_make(dir_fd, "rec/sv3/sub"));
129
130 ASSERT_OK_ERRNO(mkdirat(dir_fd, "rec/dir", 0755));
131 ASSERT_OK(btrfs_subvol_make(dir_fd, "rec/dir/sv4"));
132 ASSERT_OK_ERRNO(mkdirat(dir_fd, "rec/dir/sv4/dir", 0755));
133 ASSERT_OK(btrfs_subvol_make(dir_fd, "rec/dir/sv4/dir/sv5"));
134
135 ASSERT_OK_ERRNO(mkdirat(dir_fd, "rec/mnt", 0755));
136
137 ASSERT_OK(btrfs_subvol_snapshot_at(dir_fd, "rec", dir_fd, "rec-snap", BTRFS_SNAPSHOT_RECURSIVE));
138
139 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "rec", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE), -EPERM);
140 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "rec-snap", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE), -EPERM);
141 }
142
143 TEST(quota) {
144 _cleanup_(rm_rf_subvolume_and_freep) char *dir = NULL;
145 _cleanup_close_ int dir_fd = ASSERT_OK(open_test_subvol(&dir));
146 BtrfsQuotaInfo quota;
147 int r;
148
149 _cleanup_free_ char *qt = ASSERT_NOT_NULL(path_join(dir, "quotatest")),
150 *qt2 = ASSERT_NOT_NULL(path_join(dir, "quotatest2")),
151 *beneath = ASSERT_NOT_NULL(path_join(dir, "quotatest/beneath")),
152 *snap_beneath = ASSERT_NOT_NULL(path_join(dir, "quotatest2/beneath"));
153
154 ASSERT_OK(btrfs_subvol_make(dir_fd, "quotatest"));
155 /* The qgroup/quota ioctls require CAP_SYS_ADMIN; skip the rest of the test if we don't have it
156 * or quotas are not enabled on this filesystem. */
157 r = btrfs_subvol_auto_qgroup(qt, 0, true);
158 if (r == -EPERM)
159 return (void) log_tests_skipped("not running privileged");
160 if (IN_SET(r, -ENOTCONN, -ENOENT))
161 return (void) log_tests_skipped_errno(r, "btrfs quotas not enabled on this filesystem");
162 ASSERT_OK(r);
163
164 ASSERT_OK(btrfs_subvol_make(dir_fd, "quotatest/beneath"));
165 ASSERT_OK(btrfs_subvol_auto_qgroup(beneath, 0, false));
166 ASSERT_OK(btrfs_qgroup_set_limit(beneath, 0, 4ULL * 1024 * 1024 * 1024));
167
168 ASSERT_OK(btrfs_subvol_set_subtree_quota_limit(qt, 0, 5ULL * 1024 * 1024 * 1024));
169
170 ASSERT_OK(btrfs_subvol_snapshot_at(dir_fd, "quotatest", dir_fd, "quotatest2",
171 BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA));
172
173 ASSERT_OK(btrfs_qgroup_get_quota(snap_beneath, 0, &quota));
174 ASSERT_EQ(quota.referenced_max, 4ULL * 1024 * 1024 * 1024);
175
176 ASSERT_OK(btrfs_subvol_get_subtree_quota(qt2, 0, &quota));
177 ASSERT_EQ(quota.referenced_max, 5ULL * 1024 * 1024 * 1024);
178
179 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "quotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE), -EPERM);
180 ASSERT_OK_OR(btrfs_subvol_remove_at(dir_fd, "quotatest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE), -EPERM);
181 }
182
183 TEST(physical_offset) {
184 _cleanup_free_ char *btrfs_progs = NULL;
185 int r = find_executable("btrfs", &btrfs_progs);
186 if (r < 0)
187 return (void) log_tests_skipped_errno(r, "btrfs(8) not found");
188
189 _cleanup_(rm_rf_subvolume_and_freep) char *dir = NULL;
190 _cleanup_close_ int dir_fd = ASSERT_OK(open_test_subvol(&dir));
191
192 /* Set NOCOW on the subvol dir so the swapfile inherits it on creation. Older btrfs-progs
193 * versions don't reliably set NOCOW from `btrfs filesystem mkswapfile`. */
194 ASSERT_OK(chattr_fd(dir_fd, FS_NOCOW_FL, FS_NOCOW_FL));
195
196 /* btrfs filesystem mkswapfile produces a NOCOW, contiguous file with a swap header — exactly
197 * what btrfs inspect-internal map-swapfile expects, and what btrfs_get_file_physical_offset_fd
198 * works with. */
199 _cleanup_free_ char *path = ASSERT_NOT_NULL(path_join(dir, "swapfile"));
200 r = ASSERT_OK(pidref_safe_fork("(mkswapfile)",
201 FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT,
202 NULL));
203 if (r == 0) {
204 execlp(btrfs_progs, "btrfs", "filesystem", "mkswapfile", "-s", "1m", path, NULL);
205 _exit(EXIT_FAILURE);
206 }
207
208 _cleanup_close_ int fd = ASSERT_OK_ERRNO(openat(dir_fd, "swapfile", O_RDONLY|O_CLOEXEC|O_NOCTTY));
209
210 unsigned attrs;
211 ASSERT_OK(read_attr_fd(fd, &attrs));
212 if (!FLAGS_SET(attrs, FS_NOCOW_FL) || FLAGS_SET(attrs, FS_COMPR_FL))
213 return (void) log_tests_skipped("swapfile is not NOCOW/non-compressed (old btrfs-progs?)");
214
215 /* btrfs_get_file_physical_offset_fd() uses BTRFS_IOC_TREE_SEARCH, which needs CAP_SYS_ADMIN. */
216 uint64_t offset;
217 r = btrfs_get_file_physical_offset_fd(fd, &offset);
218 if (r == -EPERM)
219 return (void) log_tests_skipped("not running privileged");
220 ASSERT_OK(r);
221
222 /* Cross-check against `btrfs inspect-internal map-swapfile -r`, which prints the first
223 * physical address in page units. */
224 _cleanup_close_pair_ int pipe_fds[2] = EBADF_PAIR;
225 ASSERT_OK_ERRNO(pipe2(pipe_fds, O_CLOEXEC));
226
227 _cleanup_(pidref_done) PidRef inspect = PIDREF_NULL;
228 r = pidref_safe_fork_full("(btrfs-inspect)",
229 (int[3]) { -EBADF, pipe_fds[1], STDERR_FILENO },
230 /* except_fds= */ NULL, /* n_except_fds= */ 0,
231 FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_REARRANGE_STDIO,
232 &inspect);
233 ASSERT_OK(r);
234 if (r == 0) {
235 execlp(btrfs_progs, "btrfs", "inspect-internal", "map-swapfile", "-r", path, NULL);
236 _exit(EXIT_FAILURE);
237 }
238 pipe_fds[1] = safe_close(pipe_fds[1]);
239
240 _cleanup_fclose_ FILE *f = ASSERT_NOT_NULL(take_fdopen(&pipe_fds[0], "r"));
241 _cleanup_free_ char *out = NULL;
242 ASSERT_OK(read_full_stream(f, &out, /* ret_size= */ NULL));
243 ASSERT_OK_EQ(pidref_wait_for_terminate_and_check("(btrfs-inspect)", &inspect, 0), 0);
244
245 uint64_t expected;
246 ASSERT_OK(safe_atou64(strstrip(out), &expected));
247 ASSERT_EQ(offset / page_size(), expected);
248 log_info("physical offset: page %" PRIu64, expected);
249 }
250
251 static int intro(void) {
252 const char *vtd;
253 int r;
254
255 r = var_tmp_dir(&vtd);
256 if (r < 0)
257 return log_tests_skipped_errno(r, "Failed to resolve /var/tmp");
258
259 r = path_is_fs_type(vtd, BTRFS_SUPER_MAGIC);
260 if (r < 0)
261 return log_tests_skipped_errno(r, "Failed to determine filesystem type of %s", vtd);
262 if (r == 0)
263 return log_tests_skipped("%s is not on btrfs", vtd);
264
265 return EXIT_SUCCESS;
266 }
267
268 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);