packaging: Enable LTO and set visibility to hidden
[platform/upstream/btrfs-progs.git] / libbtrfsutil / qgroup.c
1 /*
2  * Copyright (C) 2018 Facebook
3  *
4  * This file is part of libbtrfsutil.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "btrfsutil_internal.h"
26
27 PUBLIC enum btrfs_util_error btrfs_util_create_qgroup_inherit(int flags,
28                                                               struct btrfs_util_qgroup_inherit **ret)
29 {
30         struct btrfs_qgroup_inherit *inherit;
31
32         if (flags) {
33                 errno = EINVAL;
34                 return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
35         }
36
37         inherit = calloc(1, sizeof(*inherit));
38         if (!inherit)
39                 return BTRFS_UTIL_ERROR_NO_MEMORY;
40
41         /*
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.
45          */
46         *ret = (struct btrfs_util_qgroup_inherit *)inherit;
47
48         return BTRFS_UTIL_OK;
49 }
50
51 PUBLIC void btrfs_util_destroy_qgroup_inherit(struct btrfs_util_qgroup_inherit *inherit)
52 {
53         free(inherit);
54 }
55
56 PUBLIC enum btrfs_util_error btrfs_util_qgroup_inherit_add_group(struct btrfs_util_qgroup_inherit **inherit,
57                                                                  uint64_t qgroupid)
58 {
59         struct btrfs_qgroup_inherit *tmp = (struct btrfs_qgroup_inherit *)*inherit;
60
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;
65
66         tmp->qgroups[tmp->num_qgroups++] = qgroupid;
67
68         *inherit = (struct btrfs_util_qgroup_inherit *)tmp;
69
70         return BTRFS_UTIL_OK;
71 }
72
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)
76 {
77         struct btrfs_qgroup_inherit *tmp = (struct btrfs_qgroup_inherit *)inherit;
78
79         /* Need to cast because __u64 != uint64_t. */
80         *groups = (const uint64_t *)&tmp->qgroups[0];
81         *n = tmp->num_qgroups;
82 }