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