Skip to content

Commit

Permalink
- added some btrfs util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aschnell committed Apr 16, 2024
1 parent 7ddd236 commit 73c1ba2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
55 changes: 48 additions & 7 deletions snapper/BtrfsUtils.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) [2011-2015] Novell, Inc.
* Copyright (c) [2016-2023] SUSE LLC
* Copyright (c) [2016-2024] SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -204,6 +204,8 @@ namespace snapper

#endif

// no read-only nor qgroup handling below

struct btrfs_ioctl_vol_args args;
memset(&args, 0, sizeof(args));

Expand Down Expand Up @@ -599,16 +601,18 @@ namespace snapper

struct TreeSearchOpts
{
TreeSearchOpts(__u32 type) : min_offset(0), max_offset(-1), min_type(type), max_type(type) {}
TreeSearchOpts(__u32 type) : min_type(type), max_type(type) {}

__u64 min_offset;
__u64 max_offset;
__u64 min_offset = 0;
__u64 max_offset = -1;

__u32 min_type;
__u32 max_type;

std::function<void(const struct btrfs_ioctl_search_args& args,
const struct btrfs_ioctl_search_header& sh)> callback;
const struct btrfs_ioctl_search_header& sh)> callback =
[](const struct btrfs_ioctl_search_args& args,
const struct btrfs_ioctl_search_header& sh){};
};


Expand Down Expand Up @@ -679,6 +683,24 @@ namespace snapper
}


bool
does_qgroup_exist(int fd, qgroup_t qgroup)
{
TreeSearchOpts tree_search_opts(BTRFS_QGROUP_INFO_KEY);
tree_search_opts.min_offset = tree_search_opts.max_offset = qgroup;

try
{
return qgroups_tree_search(fd, tree_search_opts) > 0;
}
catch (const std::runtime_error& e)
{
// happens when quota is disabled
return false;
}
}


qgroup_t
qgroup_find_free(int fd, uint64_t level)
{
Expand Down Expand Up @@ -711,12 +733,12 @@ namespace snapper


vector<qgroup_t>
qgroup_query_children(int fd, qgroup_t parent)
qgroup_query_relations(int fd, qgroup_t qgroup)
{
vector<qgroup_t> ret;

TreeSearchOpts tree_search_opts(BTRFS_QGROUP_RELATION_KEY);
tree_search_opts.min_offset = tree_search_opts.max_offset = parent;
tree_search_opts.min_offset = tree_search_opts.max_offset = qgroup;
tree_search_opts.callback = [&ret](const struct btrfs_ioctl_search_args& args,
const struct btrfs_ioctl_search_header& sh)
{
Expand All @@ -729,6 +751,25 @@ namespace snapper
}


vector<qgroup_t>
qgroup_query_children(int fd, qgroup_t parent)
{
uint64_t level = get_level(parent);
if (level == 0)
return {};

--level;

vector<qgroup_t> ret = qgroup_query_relations(fd, parent);

ret.erase(remove_if(ret.begin(), ret.end(),
[level](qgroup_t qgroup){ return level != get_level(qgroup); }
), ret.end());

return ret;
}


QGroupUsage
qgroup_query_usage(int fd, qgroup_t qgroup)
{
Expand Down
4 changes: 3 additions & 1 deletion snapper/BtrfsUtils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) [2011-2015] Novell, Inc.
* Copyright (c) [2016-2023] SUSE LLC
* Copyright (c) [2016-2024] SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -81,8 +81,10 @@ namespace snapper
void qgroup_assign(int fd, qgroup_t src, qgroup_t dst);
void qgroup_remove(int fd, qgroup_t src, qgroup_t dst);

bool does_qgroup_exist(int fd, qgroup_t qgroup);
qgroup_t qgroup_find_free(int fd, uint64_t level);

vector<qgroup_t> qgroup_query_relations(int fd, qgroup_t qgroup);
vector<qgroup_t> qgroup_query_children(int fd, qgroup_t parent);

struct QGroupUsage
Expand Down
22 changes: 20 additions & 2 deletions testsuite-real/test-btrfsutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/


#include <errno.h>
#include <cerrno>
#include <fcntl.h>
#include <string.h>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Expand Down Expand Up @@ -59,6 +59,12 @@ main()
}


if (false)
{
cout << does_qgroup_exist(fd, parse_qgroup("1/0")) << '\n';
}


if (false)
{
qgroup_create(fd, parse_qgroup("1/0"));
Expand Down Expand Up @@ -105,6 +111,17 @@ main()
}


if (false)
{
cout << "qgroup_query_relations" << endl;

vector<qgroup_t> relations = qgroup_query_relations(fd, parse_qgroup("1/0"));
for (qgroup_t relation : relations)
cout << format_qgroup(relation) << " ";
cout << endl;
}


if (false)
{
cout << "qgroup_query_children" << endl;
Expand All @@ -115,5 +132,6 @@ main()
cout << endl;
}


close(fd);
}

0 comments on commit 73c1ba2

Please sign in to comment.