Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / libbtrfsutil / qgroup.c
blob46a9043b835beebd567af2b32a14268294477671
1 /*
2 * Copyright (C) 2018 Facebook
4 * This file is part of libbtrfsutil.
6 * libbtrfsutil is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * libbtrfsutil is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "btrfsutil_internal.h"
27 PUBLIC enum btrfs_util_error btrfs_util_create_qgroup_inherit(int flags,
28 struct btrfs_util_qgroup_inherit **ret)
30 struct btrfs_qgroup_inherit *inherit;
32 if (flags) {
33 errno = EINVAL;
34 return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
37 inherit = calloc(1, sizeof(*inherit));
38 if (!inherit)
39 return BTRFS_UTIL_ERROR_NO_MEMORY;
42 * struct btrfs_util_qgroup_inherit is a lie; it's actually struct
43 * btrfs_qgroup_inherit, but we abstract it away so that users don't
44 * need to depend on the Btrfs UAPI headers.
46 *ret = (struct btrfs_util_qgroup_inherit *)inherit;
48 return BTRFS_UTIL_OK;
51 PUBLIC void btrfs_util_destroy_qgroup_inherit(struct btrfs_util_qgroup_inherit *inherit)
53 free(inherit);
56 PUBLIC enum btrfs_util_error btrfs_util_qgroup_inherit_add_group(struct btrfs_util_qgroup_inherit **inherit,
57 uint64_t qgroupid)
59 struct btrfs_qgroup_inherit *tmp = (struct btrfs_qgroup_inherit *)*inherit;
61 tmp = realloc(tmp, sizeof(*tmp) +
62 (tmp->num_qgroups + 1) * sizeof(tmp->qgroups[0]));
63 if (!tmp)
64 return BTRFS_UTIL_ERROR_NO_MEMORY;
66 tmp->qgroups[tmp->num_qgroups++] = qgroupid;
68 *inherit = (struct btrfs_util_qgroup_inherit *)tmp;
70 return BTRFS_UTIL_OK;
73 PUBLIC void btrfs_util_qgroup_inherit_get_groups(const struct btrfs_util_qgroup_inherit *inherit,
74 const uint64_t **groups,
75 size_t *n)
77 struct btrfs_qgroup_inherit *tmp = (struct btrfs_qgroup_inherit *)inherit;
79 /* Need to cast because __u64 != uint64_t. */
80 *groups = (const uint64_t *)&tmp->qgroups[0];
81 *n = tmp->num_qgroups;