c46500970ba37a8b412cd2a05af3beef9793f0cf
[platform/upstream/btrfs-progs.git] / libbtrfsutil / btrfsutil.h
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 #ifndef BTRFS_UTIL_H
21 #define BTRFS_UTIL_H
22
23 #include <stddef.h>
24 #include <stdint.h>
25
26 #define BTRFS_UTIL_VERSION_MAJOR 1
27 #define BTRFS_UTIL_VERSION_MINOR 0
28 #define BTRFS_UTIL_VERSION_PATCH 0
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /**
35  * enum btrfs_util_error - libbtrfsutil error codes.
36  *
37  * All functions in libbtrfsutil that can return an error return this type and
38  * set errno.
39  */
40 enum btrfs_util_error {
41         BTRFS_UTIL_OK,
42         BTRFS_UTIL_ERROR_STOP_ITERATION,
43         BTRFS_UTIL_ERROR_NO_MEMORY,
44         BTRFS_UTIL_ERROR_INVALID_ARGUMENT,
45         BTRFS_UTIL_ERROR_NOT_BTRFS,
46         BTRFS_UTIL_ERROR_NOT_SUBVOLUME,
47         BTRFS_UTIL_ERROR_SUBVOLUME_NOT_FOUND,
48         BTRFS_UTIL_ERROR_OPEN_FAILED,
49         BTRFS_UTIL_ERROR_RMDIR_FAILED,
50         BTRFS_UTIL_ERROR_UNLINK_FAILED,
51         BTRFS_UTIL_ERROR_STAT_FAILED,
52         BTRFS_UTIL_ERROR_STATFS_FAILED,
53         BTRFS_UTIL_ERROR_SEARCH_FAILED,
54         BTRFS_UTIL_ERROR_INO_LOOKUP_FAILED,
55         BTRFS_UTIL_ERROR_SUBVOL_GETFLAGS_FAILED,
56         BTRFS_UTIL_ERROR_SUBVOL_SETFLAGS_FAILED,
57         BTRFS_UTIL_ERROR_SUBVOL_CREATE_FAILED,
58         BTRFS_UTIL_ERROR_SNAP_CREATE_FAILED,
59         BTRFS_UTIL_ERROR_SNAP_DESTROY_FAILED,
60         BTRFS_UTIL_ERROR_DEFAULT_SUBVOL_FAILED,
61         BTRFS_UTIL_ERROR_SYNC_FAILED,
62         BTRFS_UTIL_ERROR_START_SYNC_FAILED,
63         BTRFS_UTIL_ERROR_WAIT_SYNC_FAILED,
64 };
65
66 /**
67  * btrfs_util_strerror() - Convert a libtrfsutil error code to a string
68  * description.
69  * @err: The error to convert.
70  *
71  * Return: Error description.
72  */
73 const char *btrfs_util_strerror(enum btrfs_util_error err);
74
75 /**
76  * btrfs_util_sync() - Force a sync on a specific Btrfs filesystem.
77  * @path: Path on a Btrfs filesystem.
78  *
79  * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
80  */
81 enum btrfs_util_error btrfs_util_sync(const char *path);
82
83 /**
84  * btrfs_util_sync_fd() - See btrfs_util_sync().
85  */
86 enum btrfs_util_error btrfs_util_sync_fd(int fd);
87
88 /**
89  * btrfs_util_start_sync() - Start a sync on a specific Btrfs filesystem but
90  * don't wait for it.
91  * @path: Path on a Btrfs filesystem.
92  * @transid: Returned transaction ID which can be waited on with
93  * btrfs_util_wait_sync(). This can be %NULL.
94  *
95  * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
96  */
97 enum btrfs_util_error btrfs_util_start_sync(const char *path,
98                                             uint64_t *transid);
99
100 /**
101  * btrfs_util_start_sync_fd() - See btrfs_util_start_sync().
102  */
103 enum btrfs_util_error btrfs_util_start_sync_fd(int fd, uint64_t *transid);
104
105 /**
106  * btrfs_util_wait_sync() - Wait for a transaction with a given ID to sync.
107  * @path: Path on a Btrfs filesystem.
108  * @transid: Transaction ID to wait for, or zero for the current transaction.
109  *
110  * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
111  */
112 enum btrfs_util_error btrfs_util_wait_sync(const char *path, uint64_t transid);
113
114 /**
115  * btrfs_util_wait_sync_fd() - See btrfs_util_wait_sync().
116  */
117 enum btrfs_util_error btrfs_util_wait_sync_fd(int fd, uint64_t transid);
118
119 /**
120  * btrfs_util_is_subvolume() - Return whether a given path is a Btrfs subvolume.
121  * @path: Path to check.
122  *
123  * Return: %BTRFS_UTIL_OK if @path is a Btrfs subvolume,
124  * %BTRFS_UTIL_ERROR_NOT_BTRFS if @path is not on a Btrfs filesystem,
125  * %BTRFS_UTIL_ERROR_NOT_SUBVOLUME if @path is not a subvolume, non-zero error
126  * code on any other failure.
127  */
128 enum btrfs_util_error btrfs_util_is_subvolume(const char *path);
129
130 /**
131  * btrfs_util_is_subvolume_fd() - See btrfs_util_is_subvolume().
132  */
133 enum btrfs_util_error btrfs_util_is_subvolume_fd(int fd);
134
135 /**
136  * btrfs_util_subvolume_id() - Get the ID of the subvolume containing a path.
137  * @path: Path on a Btrfs filesystem.
138  * @id_ret: Returned subvolume ID.
139  *
140  * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
141  */
142 enum btrfs_util_error btrfs_util_subvolume_id(const char *path,
143                                               uint64_t *id_ret);
144
145 /**
146  * btrfs_util_subvolume_id_fd() - See btrfs_util_subvolume_id().
147  */
148 enum btrfs_util_error btrfs_util_subvolume_id_fd(int fd, uint64_t *id_ret);
149
150 struct btrfs_util_qgroup_inherit;
151
152 /**
153  * btrfs_util_create_qgroup_inherit() - Create a qgroup inheritance specifier
154  * for btrfs_util_create_subvolume() or btrfs_util_create_snapshot().
155  * @flags: Must be zero.
156  * @ret: Returned qgroup inheritance specifier.
157  *
158  * The returned structure must be freed with
159  * btrfs_util_destroy_qgroup_inherit().
160  *
161  * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
162  */
163 enum btrfs_util_error btrfs_util_create_qgroup_inherit(int flags,
164                                                        struct btrfs_util_qgroup_inherit **ret);
165
166 /**
167  * btrfs_util_destroy_qgroup_inherit() - Destroy a qgroup inheritance specifier
168  * previously created with btrfs_util_create_qgroup_inherit().
169  * @inherit: Specifier to destroy.
170  */
171 void btrfs_util_destroy_qgroup_inherit(struct btrfs_util_qgroup_inherit *inherit);
172
173 /**
174  * btrfs_util_qgroup_inherit_add_group() - Add inheritance from a qgroup to a
175  * qgroup inheritance specifier.
176  * @inherit: Specifier to modify. May be reallocated.
177  * @qgroupid: ID of qgroup to inherit from.
178  *
179  * Return: %BTRFS_UTIL_OK on success, non-zero error code on failure.
180  */
181 enum btrfs_util_error btrfs_util_qgroup_inherit_add_group(struct btrfs_util_qgroup_inherit **inherit,
182                                                           uint64_t qgroupid);
183
184 /**
185  * btrfs_util_qgroup_inherit_get_groups() - Get the qgroups a qgroup inheritance
186  * specifier contains.
187  * @inherit: Qgroup inheritance specifier.
188  * @groups: Returned array of qgroup IDs.
189  * @n: Returned number of entries in the @groups array.
190  */
191 void btrfs_util_qgroup_inherit_get_groups(const struct btrfs_util_qgroup_inherit *inherit,
192                                           const uint64_t **groups, size_t *n);
193
194 #ifdef __cplusplus
195 }
196 #endif
197
198 #endif /* BTRFS_UTIL_H */