]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-btrfs.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-btrfs.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d7c7c334
LP
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
10f9c755 21#include <fcntl.h>
d7c7c334 22
10f9c755 23#include "btrfs-util.h"
3ffd4af2
LP
24#include "fd-util.h"
25#include "fileio.h"
26#include "log.h"
6bedfcbb 27#include "parse-util.h"
07630cea 28#include "string-util.h"
3ffd4af2 29#include "util.h"
d7c7c334
LP
30
31int main(int argc, char *argv[]) {
5bcd08db 32 BtrfsQuotaInfo quota;
d9e2daaf 33 int r, fd;
10f9c755
LP
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 {
b6b18498 39 char ts[FORMAT_TIMESTAMP_MAX], bs[FORMAT_BYTES_MAX];
5bcd08db 40 BtrfsSubvolInfo info;
c75f27ea 41
5bcd08db 42 r = btrfs_subvol_get_info_fd(fd, 0, &info);
10f9c755
LP
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));
c75f27ea 47 log_info("read-only (search): %s", yes_no(info.read_only));
10f9c755
LP
48 }
49
5bcd08db 50 r = btrfs_qgroup_get_quota_fd(fd, 0, &quota);
b6b18498
LP
51 if (r < 0)
52 log_error_errno(r, "Failed to get quota info: %m");
53 else {
cb81cd80 54 log_info("referenced: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced)));
b6b18498 55 log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive)));
cb81cd80 56 log_info("referenced_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced_max)));
b6b18498
LP
57 log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max)));
58 }
59
10f9c755 60 r = btrfs_subvol_get_read_only_fd(fd);
c75f27ea
LP
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));
10f9c755 65
c75f27ea 66 safe_close(fd);
10f9c755 67 }
d7c7c334
LP
68
69 r = btrfs_subvol_make("/xxxtest");
70 if (r < 0)
71 log_error_errno(r, "Failed to make subvolume: %m");
72
4c1fc3e4 73 r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE);
d7c7c334
LP
74 if (r < 0)
75 log_error_errno(r, "Failed to write file: %m");
76
e9bc1871 77 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
d7c7c334
LP
78 if (r < 0)
79 log_error_errno(r, "Failed to make snapshot: %m");
80
e9bc1871 81 r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY);
d7c7c334
LP
82 if (r < 0)
83 log_error_errno(r, "Failed to make snapshot: %m");
84
5bcd08db 85 r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
86 if (r < 0)
87 log_error_errno(r, "Failed to remove subvolume: %m");
88
5bcd08db 89 r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
90 if (r < 0)
91 log_error_errno(r, "Failed to remove subvolume: %m");
92
5bcd08db 93 r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
94 if (r < 0)
95 log_error_errno(r, "Failed to remove subvolume: %m");
96
e9bc1871 97 r = btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
d7c7c334
LP
98 if (r < 0)
99 log_error_errno(r, "Failed to make snapshot: %m");
100
5bcd08db 101 r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA);
d7c7c334
LP
102 if (r < 0)
103 log_error_errno(r, "Failed to remove subvolume: %m");
104
d9e2daaf
LP
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
f70a17f8
LP
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
5bcd08db 142 r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
d9e2daaf
LP
143 if (r < 0)
144 log_error_errno(r, "Failed to recursively remove subvolume: %m");
145
5bcd08db 146 r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
f70a17f8
LP
147 if (r < 0)
148 log_error_errno(r, "Failed to recursively remove subvolume: %m");
149
5bcd08db
LP
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
d7c7c334
LP
198 return 0;
199}