]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d7c7c334 | 2 | |
10f9c755 | 3 | #include <fcntl.h> |
d7c7c334 | 4 | |
10f9c755 | 5 | #include "btrfs-util.h" |
3ffd4af2 LP |
6 | #include "fd-util.h" |
7 | #include "fileio.h" | |
aa892669 | 8 | #include "format-util.h" |
3ffd4af2 | 9 | #include "log.h" |
07630cea | 10 | #include "string-util.h" |
3ffd4af2 | 11 | #include "util.h" |
d7c7c334 LP |
12 | |
13 | int main(int argc, char *argv[]) { | |
5bcd08db | 14 | BtrfsQuotaInfo quota; |
d9e2daaf | 15 | int r, fd; |
10f9c755 LP |
16 | |
17 | fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY); | |
18 | if (fd < 0) | |
19 | log_error_errno(errno, "Failed to open root directory: %m"); | |
20 | else { | |
b6b18498 | 21 | char ts[FORMAT_TIMESTAMP_MAX], bs[FORMAT_BYTES_MAX]; |
5bcd08db | 22 | BtrfsSubvolInfo info; |
c75f27ea | 23 | |
5bcd08db | 24 | r = btrfs_subvol_get_info_fd(fd, 0, &info); |
10f9c755 LP |
25 | if (r < 0) |
26 | log_error_errno(r, "Failed to get subvolume info: %m"); | |
27 | else { | |
28 | log_info("otime: %s", format_timestamp(ts, sizeof(ts), info.otime)); | |
c75f27ea | 29 | log_info("read-only (search): %s", yes_no(info.read_only)); |
10f9c755 LP |
30 | } |
31 | ||
5bcd08db | 32 | r = btrfs_qgroup_get_quota_fd(fd, 0, "a); |
b6b18498 LP |
33 | if (r < 0) |
34 | log_error_errno(r, "Failed to get quota info: %m"); | |
35 | else { | |
cb81cd80 | 36 | log_info("referenced: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced))); |
b6b18498 | 37 | log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive))); |
cb81cd80 | 38 | log_info("referenced_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced_max))); |
b6b18498 LP |
39 | log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max))); |
40 | } | |
41 | ||
10f9c755 | 42 | r = btrfs_subvol_get_read_only_fd(fd); |
c75f27ea LP |
43 | if (r < 0) |
44 | log_error_errno(r, "Failed to get read only flag: %m"); | |
45 | else | |
46 | log_info("read-only (ioctl): %s", yes_no(r)); | |
10f9c755 | 47 | |
c75f27ea | 48 | safe_close(fd); |
10f9c755 | 49 | } |
d7c7c334 LP |
50 | |
51 | r = btrfs_subvol_make("/xxxtest"); | |
52 | if (r < 0) | |
53 | log_error_errno(r, "Failed to make subvolume: %m"); | |
54 | ||
4c1fc3e4 | 55 | r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE); |
d7c7c334 LP |
56 | if (r < 0) |
57 | log_error_errno(r, "Failed to write file: %m"); | |
58 | ||
e9bc1871 | 59 | r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0); |
d7c7c334 LP |
60 | if (r < 0) |
61 | log_error_errno(r, "Failed to make snapshot: %m"); | |
62 | ||
e9bc1871 | 63 | r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY); |
d7c7c334 LP |
64 | if (r < 0) |
65 | log_error_errno(r, "Failed to make snapshot: %m"); | |
66 | ||
5bcd08db | 67 | r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA); |
d7c7c334 LP |
68 | if (r < 0) |
69 | log_error_errno(r, "Failed to remove subvolume: %m"); | |
70 | ||
5bcd08db | 71 | r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA); |
d7c7c334 LP |
72 | if (r < 0) |
73 | log_error_errno(r, "Failed to remove subvolume: %m"); | |
74 | ||
5bcd08db | 75 | r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA); |
d7c7c334 LP |
76 | if (r < 0) |
77 | log_error_errno(r, "Failed to remove subvolume: %m"); | |
78 | ||
e9bc1871 | 79 | r = btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY); |
d7c7c334 LP |
80 | if (r < 0) |
81 | log_error_errno(r, "Failed to make snapshot: %m"); | |
82 | ||
5bcd08db | 83 | r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA); |
d7c7c334 LP |
84 | if (r < 0) |
85 | log_error_errno(r, "Failed to remove subvolume: %m"); | |
86 | ||
d9e2daaf LP |
87 | r = btrfs_subvol_make("/xxxrectest"); |
88 | if (r < 0) | |
89 | log_error_errno(r, "Failed to make subvolume: %m"); | |
90 | ||
91 | r = btrfs_subvol_make("/xxxrectest/xxxrectest2"); | |
92 | if (r < 0) | |
93 | log_error_errno(r, "Failed to make subvolume: %m"); | |
94 | ||
95 | r = btrfs_subvol_make("/xxxrectest/xxxrectest3"); | |
96 | if (r < 0) | |
97 | log_error_errno(r, "Failed to make subvolume: %m"); | |
98 | ||
99 | r = btrfs_subvol_make("/xxxrectest/xxxrectest3/sub"); | |
100 | if (r < 0) | |
101 | log_error_errno(r, "Failed to make subvolume: %m"); | |
102 | ||
103 | if (mkdir("/xxxrectest/dir", 0755) < 0) | |
104 | log_error_errno(errno, "Failed to make directory: %m"); | |
105 | ||
106 | r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4"); | |
107 | if (r < 0) | |
108 | log_error_errno(r, "Failed to make subvolume: %m"); | |
109 | ||
110 | if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0) | |
111 | log_error_errno(errno, "Failed to make directory: %m"); | |
112 | ||
113 | r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5"); | |
114 | if (r < 0) | |
115 | log_error_errno(r, "Failed to make subvolume: %m"); | |
116 | ||
117 | if (mkdir("/xxxrectest/mnt", 0755) < 0) | |
118 | log_error_errno(errno, "Failed to make directory: %m"); | |
119 | ||
f70a17f8 LP |
120 | r = btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE); |
121 | if (r < 0) | |
122 | log_error_errno(r, "Failed to snapshot subvolume: %m"); | |
123 | ||
5bcd08db | 124 | r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); |
d9e2daaf LP |
125 | if (r < 0) |
126 | log_error_errno(r, "Failed to recursively remove subvolume: %m"); | |
127 | ||
5bcd08db | 128 | r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); |
f70a17f8 LP |
129 | if (r < 0) |
130 | log_error_errno(r, "Failed to recursively remove subvolume: %m"); | |
131 | ||
5bcd08db LP |
132 | r = btrfs_subvol_make("/xxxquotatest"); |
133 | if (r < 0) | |
134 | log_error_errno(r, "Failed to make subvolume: %m"); | |
135 | ||
136 | r = btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true); | |
137 | if (r < 0) | |
138 | log_error_errno(r, "Failed to set up auto qgroup: %m"); | |
139 | ||
140 | r = btrfs_subvol_make("/xxxquotatest/beneath"); | |
141 | if (r < 0) | |
142 | log_error_errno(r, "Failed to make subvolume: %m"); | |
143 | ||
144 | r = btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false); | |
145 | if (r < 0) | |
146 | log_error_errno(r, "Failed to set up auto qgroup: %m"); | |
147 | ||
148 | r = btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024); | |
149 | if (r < 0) | |
150 | log_error_errno(r, "Failed to set up quota limit: %m"); | |
151 | ||
152 | r = btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024); | |
153 | if (r < 0) | |
154 | log_error_errno(r, "Failed to set up quota limit: %m"); | |
155 | ||
156 | r = btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA); | |
157 | if (r < 0) | |
158 | log_error_errno(r, "Failed to setup snapshot: %m"); | |
159 | ||
160 | r = btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, "a); | |
161 | if (r < 0) | |
162 | log_error_errno(r, "Failed to query quota: %m"); | |
163 | ||
164 | assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024); | |
165 | ||
166 | r = btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, "a); | |
167 | if (r < 0) | |
168 | log_error_errno(r, "Failed to query quota: %m"); | |
169 | ||
170 | assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024); | |
171 | ||
172 | r = btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); | |
173 | if (r < 0) | |
174 | log_error_errno(r, "Failed remove subvolume: %m"); | |
175 | ||
176 | r = btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE); | |
177 | if (r < 0) | |
178 | log_error_errno(r, "Failed remove subvolume: %m"); | |
179 | ||
d7c7c334 LP |
180 | return 0; |
181 | } |