]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
Resizing
[bcachefs-tools-debian] / tools-util.h
1 #ifndef _TOOLS_UTIL_H
2 #define _TOOLS_UTIL_H
3
4 #include <errno.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/ioctl.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 #include <linux/bug.h>
14 #include <linux/byteorder.h>
15 #include <linux/kernel.h>
16 #include <linux/log2.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/uuid.h>
20 #include "ccan/darray/darray.h"
21
22 void die(const char *, ...);
23 char *mprintf(const char *, ...);
24 void *xcalloc(size_t, size_t);
25 void *xmalloc(size_t);
26 void *xrealloc(void *, size_t);
27 void xpread(int, void *, size_t, off_t);
28 void xpwrite(int, const void *, size_t, off_t);
29 struct stat xfstatat(int, const char *, int);
30 struct stat xfstat(int);
31
32 #define xopenat(_dirfd, _path, ...)                                     \
33 ({                                                                      \
34         int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
35         if (_fd < 0)                                                    \
36                 die("Error opening %s: %m", (_path));                   \
37         _fd;                                                            \
38 })
39
40 #define xopen(...)      xopenat(AT_FDCWD, __VA_ARGS__)
41
42 #define xioctl(_fd, _nr, ...)                                           \
43 ({                                                                      \
44         int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__);                  \
45         if (_ret < 0)                                                   \
46                 die(#_nr " ioctl error: %m");                           \
47         _ret;                                                           \
48 })
49
50 enum units {
51         BYTES,
52         SECTORS,
53         HUMAN_READABLE,
54 };
55
56 struct units_buf __pr_units(s64, enum units);
57
58 struct units_buf {
59         char    b[20];
60 };
61
62 #define pr_units(_v, _u)        &(__pr_units(_v, _u).b[0])
63
64 char *read_file_str(int, const char *);
65 u64 read_file_u64(int, const char *);
66
67 ssize_t read_string_list_or_die(const char *, const char * const[],
68                                 const char *);
69
70 u64 get_size(const char *, int);
71 unsigned get_blocksize(const char *, int);
72 int open_for_format(const char *, bool);
73
74 bool ask_yn(void);
75
76 struct range {
77         u64             start;
78         u64             end;
79 };
80
81 typedef darray(struct range) ranges;
82
83 static inline void range_add(ranges *data, u64 offset, u64 size)
84 {
85         darray_append(*data, (struct range) {
86                 .start = offset,
87                 .end = offset + size
88         });
89 }
90
91 void ranges_sort_merge(ranges *);
92 void ranges_roundup(ranges *, unsigned);
93 void ranges_rounddown(ranges *, unsigned);
94
95 struct hole_iter {
96         ranges          r;
97         size_t          idx;
98         u64             end;
99 };
100
101 static inline struct range hole_iter_next(struct hole_iter *iter)
102 {
103         struct range r = {
104                 .start  = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
105                 .end    = iter->idx < iter->r.size
106                         ? iter->r.item[iter->idx].start : iter->end,
107         };
108
109         BUG_ON(r.start > r.end);
110
111         iter->idx++;
112         return r;
113 }
114
115 #define for_each_hole(_iter, _ranges, _end, _i)                         \
116         for (_iter = (struct hole_iter) { .r = _ranges, .end = _end };  \
117              (_iter.idx <= _iter.r.size &&                              \
118               (_i = hole_iter_next(&_iter), true));)
119
120 #include <linux/fiemap.h>
121
122 struct fiemap_iter {
123         struct fiemap           f;
124         struct fiemap_extent    fe[1024];
125         unsigned                idx;
126         int                     fd;
127 };
128
129 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
130 {
131         memset(iter, 0, sizeof(*iter));
132
133         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
134         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
135         iter->fd                = fd;
136 }
137
138 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
139
140 #define fiemap_for_each(fd, iter, extent)                               \
141         for (fiemap_iter_init(&iter, fd);                               \
142              (extent = fiemap_iter_next(&iter)).fe_length;)
143
144 const char *strcmp_prefix(const char *, const char *);
145
146 unsigned hatoi_validate(const char *, const char *);
147
148 u32 crc32c(u32, const void *, size_t);
149
150 char *dev_to_name(dev_t);
151 char *dev_to_path(dev_t);
152 char *dev_to_mount(char *);
153
154 #endif /* _TOOLS_UTIL_H */