2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-12-12 03:15:58 +01:00
|
|
|
|
2014-12-25 03:19:19 +01:00
|
|
|
#include <fcntl.h>
|
2014-12-12 03:15:58 +01:00
|
|
|
|
2014-12-25 03:19:19 +01:00
|
|
|
#include "btrfs-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "fileio.h"
|
|
|
|
#include "log.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "util.h"
|
2014-12-12 03:15:58 +01:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2015-10-21 19:38:21 +02:00
|
|
|
BtrfsQuotaInfo quota;
|
2015-04-06 10:57:17 +02:00
|
|
|
int r, fd;
|
2014-12-25 03:19:19 +01:00
|
|
|
|
|
|
|
fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
|
|
|
|
if (fd < 0)
|
|
|
|
log_error_errno(errno, "Failed to open root directory: %m");
|
|
|
|
else {
|
2014-12-28 02:05:28 +01:00
|
|
|
char ts[FORMAT_TIMESTAMP_MAX], bs[FORMAT_BYTES_MAX];
|
2015-10-21 19:38:21 +02:00
|
|
|
BtrfsSubvolInfo info;
|
2014-12-26 16:59:53 +01:00
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_get_info_fd(fd, 0, &info);
|
2014-12-25 03:19:19 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to get subvolume info: %m");
|
|
|
|
else {
|
|
|
|
log_info("otime: %s", format_timestamp(ts, sizeof(ts), info.otime));
|
2014-12-26 16:59:53 +01:00
|
|
|
log_info("read-only (search): %s", yes_no(info.read_only));
|
2014-12-25 03:19:19 +01:00
|
|
|
}
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_qgroup_get_quota_fd(fd, 0, "a);
|
2014-12-28 02:05:28 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to get quota info: %m");
|
|
|
|
else {
|
2015-03-10 15:55:58 +01:00
|
|
|
log_info("referenced: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced)));
|
2014-12-28 02:05:28 +01:00
|
|
|
log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive)));
|
2015-03-10 15:55:58 +01:00
|
|
|
log_info("referenced_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced_max)));
|
2014-12-28 02:05:28 +01:00
|
|
|
log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max)));
|
|
|
|
}
|
|
|
|
|
2014-12-25 03:19:19 +01:00
|
|
|
r = btrfs_subvol_get_read_only_fd(fd);
|
2014-12-26 16:59:53 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to get read only flag: %m");
|
|
|
|
else
|
|
|
|
log_info("read-only (ioctl): %s", yes_no(r));
|
2014-12-25 03:19:19 +01:00
|
|
|
|
2014-12-26 16:59:53 +01:00
|
|
|
safe_close(fd);
|
2014-12-25 03:19:19 +01:00
|
|
|
}
|
2014-12-12 03:15:58 +01:00
|
|
|
|
|
|
|
r = btrfs_subvol_make("/xxxtest");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
2015-07-07 01:19:25 +02:00
|
|
|
r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to write file: %m");
|
|
|
|
|
2015-04-06 11:47:25 +02:00
|
|
|
r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest2", 0);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make snapshot: %m");
|
|
|
|
|
2015-04-06 11:47:25 +02:00
|
|
|
r = btrfs_subvol_snapshot("/xxxtest", "/xxxtest3", BTRFS_SNAPSHOT_READ_ONLY);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make snapshot: %m");
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_remove("/xxxtest", BTRFS_REMOVE_QUOTA);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to remove subvolume: %m");
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_remove("/xxxtest2", BTRFS_REMOVE_QUOTA);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to remove subvolume: %m");
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_remove("/xxxtest3", BTRFS_REMOVE_QUOTA);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to remove subvolume: %m");
|
|
|
|
|
2015-04-06 11:47:25 +02:00
|
|
|
r = btrfs_subvol_snapshot("/etc", "/etc2", BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_FALLBACK_COPY);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make snapshot: %m");
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_remove("/etc2", BTRFS_REMOVE_QUOTA);
|
2014-12-12 03:15:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to remove subvolume: %m");
|
|
|
|
|
2015-04-06 10:57:17 +02:00
|
|
|
r = btrfs_subvol_make("/xxxrectest");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_make("/xxxrectest/xxxrectest2");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_make("/xxxrectest/xxxrectest3");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_make("/xxxrectest/xxxrectest3/sub");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
if (mkdir("/xxxrectest/dir", 0755) < 0)
|
|
|
|
log_error_errno(errno, "Failed to make directory: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
if (mkdir("/xxxrectest/dir/xxxrectest4/dir", 0755) < 0)
|
|
|
|
log_error_errno(errno, "Failed to make directory: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_make("/xxxrectest/dir/xxxrectest4/dir/xxxrectest5");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
if (mkdir("/xxxrectest/mnt", 0755) < 0)
|
|
|
|
log_error_errno(errno, "Failed to make directory: %m");
|
|
|
|
|
2015-04-06 15:26:59 +02:00
|
|
|
r = btrfs_subvol_snapshot("/xxxrectest", "/xxxrectest2", BTRFS_SNAPSHOT_RECURSIVE);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to snapshot subvolume: %m");
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_remove("/xxxrectest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
|
2015-04-06 10:57:17 +02:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to recursively remove subvolume: %m");
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_remove("/xxxrectest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
|
2015-04-06 15:26:59 +02:00
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to recursively remove subvolume: %m");
|
|
|
|
|
2015-10-21 19:38:21 +02:00
|
|
|
r = btrfs_subvol_make("/xxxquotatest");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_auto_qgroup("/xxxquotatest", 0, true);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to set up auto qgroup: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_make("/xxxquotatest/beneath");
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to make subvolume: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_auto_qgroup("/xxxquotatest/beneath", 0, false);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to set up auto qgroup: %m");
|
|
|
|
|
|
|
|
r = btrfs_qgroup_set_limit("/xxxquotatest/beneath", 0, 4ULL * 1024 * 1024 * 1024);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to set up quota limit: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_set_subtree_quota_limit("/xxxquotatest", 0, 5ULL * 1024 * 1024 * 1024);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to set up quota limit: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_snapshot("/xxxquotatest", "/xxxquotatest2", BTRFS_SNAPSHOT_RECURSIVE|BTRFS_SNAPSHOT_QUOTA);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to setup snapshot: %m");
|
|
|
|
|
|
|
|
r = btrfs_qgroup_get_quota("/xxxquotatest2/beneath", 0, "a);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to query quota: %m");
|
|
|
|
|
|
|
|
assert_se(quota.referenced_max == 4ULL * 1024 * 1024 * 1024);
|
|
|
|
|
|
|
|
r = btrfs_subvol_get_subtree_quota("/xxxquotatest2", 0, "a);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to query quota: %m");
|
|
|
|
|
|
|
|
assert_se(quota.referenced_max == 5ULL * 1024 * 1024 * 1024);
|
|
|
|
|
|
|
|
r = btrfs_subvol_remove("/xxxquotatest", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed remove subvolume: %m");
|
|
|
|
|
|
|
|
r = btrfs_subvol_remove("/xxxquotatest2", BTRFS_REMOVE_QUOTA|BTRFS_REMOVE_RECURSIVE);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed remove subvolume: %m");
|
|
|
|
|
2014-12-12 03:15:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|