]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/btrfs.h
Merge pull request #3235 from xinhua9569/master
[mirror_lxc.git] / src / lxc / storage / btrfs.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_BTRFS_H
4 #define __LXC_BTRFS_H
5
6 #include <byteswap.h>
7 #include <linux/types.h> /* __le64, __l32 ... */
8 #include <stdbool.h>
9 #include <stdint.h>
10
11 #ifndef BTRFS_SUPER_MAGIC
12 # define BTRFS_SUPER_MAGIC 0x9123683E
13 #endif
14
15 typedef uint8_t u8;
16 typedef uint16_t u16;
17 typedef uint32_t u32;
18 typedef uint64_t u64;
19
20 struct btrfs_ioctl_space_info {
21 unsigned long long flags;
22 unsigned long long total_bytes;
23 unsigned long long used_bytes;
24 };
25
26 struct btrfs_ioctl_space_args {
27 unsigned long long space_slots;
28 unsigned long long total_spaces;
29 struct btrfs_ioctl_space_info spaces[];
30 };
31
32 #define BTRFS_IOCTL_MAGIC 0x94
33 #define BTRFS_IOC_SUBVOL_GETFLAGS _IOR(BTRFS_IOCTL_MAGIC, 25, unsigned long long)
34 #define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
35 struct btrfs_ioctl_space_args)
36
37 #define BTRFS_FSID_SIZE 16
38 struct btrfs_ioctl_fs_info_args {
39 unsigned long long max_id;
40 unsigned long long num_devices;
41 char fsid[BTRFS_FSID_SIZE];
42 unsigned long long reserved[124];
43 };
44
45 #define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \
46 struct btrfs_ioctl_fs_info_args)
47
48
49 #define BTRFS_SUBVOL_NAME_MAX 4039
50 #define BTRFS_PATH_NAME_MAX 4087
51
52 struct btrfs_ioctl_vol_args {
53 signed long long fd;
54 char name[BTRFS_PATH_NAME_MAX + 1];
55 };
56
57 #define BTRFS_IOCTL_MAGIC 0x94
58 #define BTRFS_IOC_SUBVOL_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 24, \
59 struct btrfs_ioctl_vol_args_v2)
60 #define BTRFS_IOC_SNAP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 23, \
61 struct btrfs_ioctl_vol_args_v2)
62 #define BTRFS_IOC_SUBVOL_CREATE _IOW(BTRFS_IOCTL_MAGIC, 14, \
63 struct btrfs_ioctl_vol_args)
64 #define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \
65 struct btrfs_ioctl_vol_args)
66
67 #define BTRFS_QGROUP_INHERIT_SET_LIMITS (1ULL << 0)
68
69 struct btrfs_ioctl_vol_args_v2 {
70 signed long long fd;
71 unsigned long long transid;
72 unsigned long long flags;
73 union {
74 struct {
75 unsigned long long size;
76 /*struct btrfs_qgroup_inherit *qgroup_inherit; */
77 void *qgroup_inherit;
78 };
79 unsigned long long unused[4];
80 };
81 char name[BTRFS_SUBVOL_NAME_MAX + 1];
82 };
83
84 /*
85 * root backrefs tie subvols and snapshots to the directory entries that
86 * reference them
87 */
88 #define BTRFS_ROOT_BACKREF_KEY 144
89
90 /*
91 * root items point to tree roots. There are typically in the root
92 * tree used by the super block to find all the other trees
93 */
94 #define BTRFS_ROOT_ITEM_KEY 132
95
96 /*
97 * root refs make a fast index for listing all of the snapshots and
98 * subvolumes referenced by a given root. They point directly to the
99 * directory item in the root that references the subvol
100 */
101 #define BTRFS_ROOT_REF_KEY 156
102
103 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
104 #define BTRFS_DIR_ITEM_KEY 84
105
106 /*
107 * * this is used for both forward and backward root refs
108 * */
109 struct btrfs_root_ref {
110 __le64 dirid;
111 __le64 sequence;
112 __le16 name_len;
113 } __attribute__ ((__packed__));
114
115 struct btrfs_disk_key {
116 __le64 objectid;
117 u8 type;
118 __le64 offset;
119 } __attribute__ ((__packed__));
120
121 struct btrfs_dir_item {
122 struct btrfs_disk_key location;
123 __le64 transid;
124 __le16 data_len;
125 __le16 name_len;
126 u8 type;
127 } __attribute__ ((__packed__));
128
129 #define BTRFS_IOCTL_MAGIC 0x94
130 #define BTRFS_VOL_NAME_MAX 255
131 #define BTRFS_PATH_NAME_MAX 4087
132
133 struct btrfs_ioctl_search_key {
134 /* which root are we searching. 0 is the tree of tree roots */
135 __u64 tree_id;
136
137 /* keys returned will be >= min and <= max */
138 __u64 min_objectid;
139 __u64 max_objectid;
140
141 /* keys returned will be >= min and <= max */
142 __u64 min_offset;
143 __u64 max_offset;
144
145 /* max and min transids to search for */
146 __u64 min_transid;
147 __u64 max_transid;
148
149 /* keys returned will be >= min and <= max */
150 __u32 min_type;
151 __u32 max_type;
152
153 /*
154 * how many items did userland ask for, and how many are we
155 * returning
156 */
157 __u32 nr_items;
158
159 /* align to 64 bits */
160 __u32 unused;
161
162 /* some extra for later */
163 __u64 unused1;
164 __u64 unused2;
165 __u64 unused3;
166 __u64 unused4;
167 };
168
169 struct btrfs_ioctl_search_header {
170 __u64 transid;
171 __u64 objectid;
172 __u64 offset;
173 __u32 type;
174 __u32 len;
175 } __attribute__((may_alias));
176
177 #define BTRFS_SEARCH_ARGS_BUFSIZE (4096 - sizeof(struct btrfs_ioctl_search_key))
178 /*
179 * the buf is an array of search headers where
180 * each header is followed by the actual item
181 * the type field is expanded to 32 bits for alignment
182 */
183 struct btrfs_ioctl_search_args {
184 struct btrfs_ioctl_search_key key;
185 char buf[BTRFS_SEARCH_ARGS_BUFSIZE];
186 };
187
188 #define BTRFS_IOC_TREE_SEARCH _IOWR(BTRFS_IOCTL_MAGIC, 17, \
189 struct btrfs_ioctl_search_args)
190 #define BTRFS_UUID_SIZE 16
191
192 struct btrfs_timespec {
193 __le64 sec;
194 __le32 nsec;
195 } __attribute__ ((__packed__));
196
197 struct btrfs_inode_item {
198 /* nfs style generation number */
199 __le64 generation;
200 /* transid that last touched this inode */
201 __le64 transid;
202 __le64 size;
203 __le64 nbytes;
204 __le64 block_group;
205 __le32 nlink;
206 __le32 uid;
207 __le32 gid;
208 __le32 mode;
209 __le64 rdev;
210 __le64 flags;
211
212 /* modification sequence number for NFS */
213 __le64 sequence;
214
215 /*
216 * a little future expansion, for more than this we can
217 * just grow the inode item and version it
218 */
219 __le64 reserved[4];
220 struct btrfs_timespec atime;
221 struct btrfs_timespec ctime;
222 struct btrfs_timespec mtime;
223 struct btrfs_timespec otime;
224 } __attribute__ ((__packed__));
225
226 struct btrfs_root_item_v0 {
227 struct btrfs_inode_item inode;
228 __le64 generation;
229 __le64 root_dirid;
230 __le64 bytenr;
231 __le64 byte_limit;
232 __le64 bytes_used;
233 __le64 last_snapshot;
234 __le64 flags;
235 __le32 refs;
236 struct btrfs_disk_key drop_progress;
237 u8 drop_level;
238 u8 level;
239 } __attribute__ ((__packed__));
240
241 struct btrfs_root_item {
242 struct btrfs_inode_item inode;
243 __le64 generation;
244 __le64 root_dirid;
245 __le64 bytenr;
246 __le64 byte_limit;
247 __le64 bytes_used;
248 __le64 last_snapshot;
249 __le64 flags;
250 __le32 refs;
251 struct btrfs_disk_key drop_progress;
252 u8 drop_level;
253 u8 level;
254
255 /*
256 * The following fields appear after subvol_uuids+subvol_times
257 * were introduced.
258 */
259
260 /*
261 * This generation number is used to test if the new fields are valid
262 * and up to date while reading the root item. Every time the root item
263 * is written out, the "generation" field is copied into this field. If
264 * anyone ever mounted the fs with an older kernel, we will have
265 * mismatching generation values here and thus must invalidate the
266 * new fields. See btrfs_update_root and btrfs_find_last_root for
267 * details.
268 * the offset of generation_v2 is also used as the start for the memset
269 * when invalidating the fields.
270 */
271 __le64 generation_v2;
272 u8 uuid[BTRFS_UUID_SIZE];
273 u8 parent_uuid[BTRFS_UUID_SIZE];
274 u8 received_uuid[BTRFS_UUID_SIZE];
275 __le64 ctransid; /* updated when an inode changes */
276 __le64 otransid; /* trans when created */
277 __le64 stransid; /* trans when sent. non-zero for received subvol */
278 __le64 rtransid; /* trans when received. non-zero for received subvol */
279 struct btrfs_timespec ctime;
280 struct btrfs_timespec otime;
281 struct btrfs_timespec stime;
282 struct btrfs_timespec rtime;
283 __le64 reserved[8]; /* for future */
284 } __attribute__ ((__packed__));
285
286 #define BTRFS_IOC_INO_LOOKUP _IOWR(BTRFS_IOCTL_MAGIC, 18, \
287 struct btrfs_ioctl_ino_lookup_args)
288
289 #define BTRFS_INO_LOOKUP_PATH_MAX 4080
290 struct btrfs_ioctl_ino_lookup_args {
291 __u64 treeid;
292 __u64 objectid;
293 char name[BTRFS_INO_LOOKUP_PATH_MAX];
294 };
295
296 /*
297 * All files have objectids in this range.
298 */
299 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
300 #define BTRFS_LAST_FREE_OBJECTID -256ULL
301 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
302
303 /*
304 * The followings are macro for correctly getting member of
305 * structures in both low and big endian platforms as per
306 * btrfs-progs
307 */
308 #ifdef __CHECKER__
309 #define __force __attribute__((force))
310 #else
311 #define __force
312 #endif
313
314 #if __BYTE_ORDER == __BIG_ENDIAN
315 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
316 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
317 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
318 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
319 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
320 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
321 #else
322 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
323 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
324 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
325 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
326 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
327 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
328 #endif
329
330 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
331 static inline u##bits btrfs_##name(type *s) \
332 { \
333 return le##bits##_to_cpu(s->member); \
334 } \
335 static inline void btrfs_set_##name(type *s, u##bits val) \
336 { \
337 s->member = cpu_to_le##bits(val); \
338 }
339
340 /* defined as btrfs_stack_root_ref_dirid */
341 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
342 /* defined as btrfs_stack_root_ref_sequence */
343 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
344 /* defined as btrfs_stack_root_ref_name_len */
345 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
346
347 struct lxc_storage;
348
349 struct bdev_specs;
350
351 struct lxc_conf;
352
353 struct mytree_node {
354 u64 objid;
355 u64 parentid;
356 char *name;
357 char *dirname;
358 };
359
360 struct my_btrfs_tree {
361 struct mytree_node *nodes;
362 int num;
363 };
364
365 extern int btrfs_clonepaths(struct lxc_storage *orig, struct lxc_storage *new,
366 const char *oldname, const char *cname,
367 const char *oldpath, const char *lxcpath, int snap,
368 uint64_t newsize, struct lxc_conf *conf);
369 extern int btrfs_create(struct lxc_storage *bdev, const char *dest,
370 const char *n, struct bdev_specs *specs);
371 extern int btrfs_destroy(struct lxc_storage *orig);
372 extern bool btrfs_detect(const char *path);
373 extern int btrfs_mount(struct lxc_storage *bdev);
374 extern int btrfs_umount(struct lxc_storage *bdev);
375
376 extern char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name,
377 u16 name_len);
378 extern int btrfs_list_get_path_rootid(int fd, u64 *treeid);
379 extern bool is_btrfs_fs(const char *path);
380 extern int is_btrfs_subvol(const char *path);
381 extern bool btrfs_try_remove_subvol(const char *path);
382 extern int btrfs_same_fs(const char *orig, const char *new);
383 extern int btrfs_snapshot(const char *orig, const char *new);
384 extern int btrfs_snapshot_wrapper(void *data);
385 extern bool btrfs_create_clone(struct lxc_conf *conf, struct lxc_storage *orig,
386 struct lxc_storage *new, uint64_t newsize);
387 extern bool btrfs_create_snapshot(struct lxc_conf *conf,
388 struct lxc_storage *orig,
389 struct lxc_storage *new, uint64_t newsize);
390
391 #endif /* __LXC_BTRFS_H */