]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-btrfs.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-btrfs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <fcntl.h>
22
23 #include "btrfs-util.h"
24 #include "fd-util.h"
25 #include "fileio.h"
26 #include "log.h"
27 #include "parse-util.h"
28 #include "string-util.h"
29 #include "util.h"
30
31 int main(int argc, char *argv[]) {
32 BtrfsQuotaInfo quota;
33 int r, fd;
34
35 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
36 if (fd < 0)
37 log_error_errno(errno, "Failed to open root directory: %m");
38 else {
39 char ts[FORMAT_TIMESTAMP_MAX], bs[FORMAT_BYTES_MAX];
40 BtrfsSubvolInfo info;
41
42 r = btrfs_subvol_get_info_fd(fd, 0, &info);
43 if (r < 0)
44 log_error_errno(r, "Failed to get subvolume info: %m");
45 else {
46 log_info("otime: %s", format_timestamp(ts, sizeof(ts), info.otime));
47 log_info("read-only (search): %s", yes_no(info.read_only));
48 }
49
50 r = btrfs_qgroup_get_quota_fd(fd, 0, &quota);
51 if (r < 0)
52 log_error_errno(r, "Failed to get quota info: %m");
53 else {
54 log_info("referenced: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced)));
55 log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive)));
56 log_info("referenced_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced_max)));
57 log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max)));
58 }
59
60 r = btrfs_subvol_get_read_only_fd(fd);
61 if (r < 0)
62 log_error_errno(r, "Failed to get read only flag: %m");
63 else
64 log_info("read-only (ioctl): %s", yes_no(r));
65
66 safe_close(fd);
67 }
68
69 r = btrfs_subvol_make("/xxxtest");
70 if (r < 0)
71 log_error_errno(r, "Failed to make subvolume: %m");
72
73 r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE);
74 if (r < 0)
75 log_error_errno(r, "Failed to write file: %m");
76
77 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
78 if (r < 0)
79 log_error_errno(r, "Failed to make snapshot: %m");
80
81 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY);
82 if (r < 0)
83 log_error_errno(r, "Failed to make snapshot: %m");
84
85 r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA);
86 if (r < 0)
87 log_error_errno(r, "Failed to remove subvolume: %m");
88
89 r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA);
90 if (r < 0)
91 log_error_errno(r, "Failed to remove subvolume: %m");
92
93 r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA);
94 if (r < 0)
95 log_error_errno(r, "Failed to remove subvolume: %m");
96
97 r = btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
98 if (r < 0)
99 log_error_errno(r, "Failed to make snapshot: %m");
100
101 r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA);
102 if (r < 0)
103 log_error_errno(r, "Failed to remove subvolume: %m");
104
105 r = btrfs_subvol_make("/xxxrectest");
106 if (r < 0)
107 log_error_errno(r, "Failed to make subvolume: %m");
108
109 r = btrfs_subvol_make("/xxxrectest/xxxrectest2");
110 if (r < 0)
111 log_error_errno(r, "Failed to make subvolume: %m");
112
113 r = btrfs_subvol_make("/xxxrectest/xxxrectest3");
114 if (r < 0)
115 log_error_errno(r, "Failed to make subvolume: %m");
116
117 r = btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
118 if (r < 0)
119 log_error_errno(r, "Failed to make subvolume: %m");
120
121 if (mkdir("/xxxrectest/dir", 0755) < 0)
122 log_error_errno(errno, "Failed to make directory: %m");
123
124 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
125 if (r < 0)
126 log_error_errno(r, "Failed to make subvolume: %m");
127
128 if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
129 log_error_errno(errno, "Failed to make directory: %m");
130
131 r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
132 if (r < 0)
133 log_error_errno(r, "Failed to make subvolume: %m");
134
135 if (mkdir("/xxxrectest/mnt", 0755) < 0)
136 log_error_errno(errno, "Failed to make directory: %m");
137
138 r = btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE);
139 if (r < 0)
140 log_error_errno(r, "Failed to snapshot subvolume: %m");
141
142 r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
143 if (r < 0)
144 log_error_errno(r, "Failed to recursively remove subvolume: %m");
145
146 r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
147 if (r < 0)
148 log_error_errno(r, "Failed to recursively remove subvolume: %m");
149
150 r = btrfs_subvol_make("/xxxquotatest");
151 if (r < 0)
152 log_error_errno(r, "Failed to make subvolume: %m");
153
154 r = btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
155 if (r < 0)
156 log_error_errno(r, "Failed to set up auto qgroup: %m");
157
158 r = btrfs_subvol_make("/xxxquotatest/beneath");
159 if (r < 0)
160 log_error_errno(r, "Failed to make subvolume: %m");
161
162 r = btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
163 if (r < 0)
164 log_error_errno(r, "Failed to set up auto qgroup: %m");
165
166 r = btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
167 if (r < 0)
168 log_error_errno(r, "Failed to set up quota limit: %m");
169
170 r = btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
171 if (r < 0)
172 log_error_errno(r, "Failed to set up quota limit: %m");
173
174 r = btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA);
175 if (r < 0)
176 log_error_errno(r, "Failed to setup snapshot: %m");
177
178 r = btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, &quota);
179 if (r < 0)
180 log_error_errno(r, "Failed to query quota: %m");
181
182 assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024);
183
184 r = btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, &quota);
185 if (r < 0)
186 log_error_errno(r, "Failed to query quota: %m");
187
188 assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024);
189
190 r = btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
191 if (r < 0)
192 log_error_errno(r, "Failed remove subvolume: %m");
193
194 r = btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
195 if (r < 0)
196 log_error_errno(r, "Failed remove subvolume: %m");
197
198 return 0;
199 }