]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
bcachefs fs usage improvements
[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         __attribute__ ((format (printf, 1, 2)));
25 void *xcalloc(size_t, size_t);
26 void *xmalloc(size_t);
27 void *xrealloc(void *, size_t);
28 void xpread(int, void *, size_t, off_t);
29 void xpwrite(int, const void *, size_t, off_t);
30 struct stat xfstatat(int, const char *, int);
31 struct stat xfstat(int);
32 struct stat xstat(const char *);
33
34 #define xopenat(_dirfd, _path, ...)                                     \
35 ({                                                                      \
36         int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
37         if (_fd < 0)                                                    \
38                 die("Error opening %s: %m", (_path));                   \
39         _fd;                                                            \
40 })
41
42 #define xopen(...)      xopenat(AT_FDCWD, __VA_ARGS__)
43
44 #define xioctl(_fd, _nr, ...)                                           \
45 ({                                                                      \
46         int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__);                  \
47         if (_ret < 0)                                                   \
48                 die(#_nr " ioctl error: %m");                           \
49         _ret;                                                           \
50 })
51
52 int printf_pad(unsigned pad, const char * fmt, ...);
53
54 enum units {
55         BYTES,
56         SECTORS,
57         HUMAN_READABLE,
58 };
59
60 struct units_buf __pr_units(s64, enum units);
61
62 struct units_buf {
63         char    b[20];
64 };
65
66 #define pr_units(_v, _u)        &(__pr_units(_v, _u).b[0])
67
68 char *read_file_str(int, const char *);
69 u64 read_file_u64(int, const char *);
70
71 ssize_t read_string_list_or_die(const char *, const char * const[],
72                                 const char *);
73
74 u64 get_size(const char *, int);
75 unsigned get_blocksize(const char *, int);
76 int open_for_format(const char *, bool);
77
78 bool ask_yn(void);
79
80 struct range {
81         u64             start;
82         u64             end;
83 };
84
85 typedef darray(struct range) ranges;
86
87 static inline void range_add(ranges *data, u64 offset, u64 size)
88 {
89         darray_append(*data, (struct range) {
90                 .start = offset,
91                 .end = offset + size
92         });
93 }
94
95 void ranges_sort_merge(ranges *);
96 void ranges_roundup(ranges *, unsigned);
97 void ranges_rounddown(ranges *, unsigned);
98
99 struct hole_iter {
100         ranges          r;
101         size_t          idx;
102         u64             end;
103 };
104
105 static inline struct range hole_iter_next(struct hole_iter *iter)
106 {
107         struct range r = {
108                 .start  = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
109                 .end    = iter->idx < iter->r.size
110                         ? iter->r.item[iter->idx].start : iter->end,
111         };
112
113         BUG_ON(r.start > r.end);
114
115         iter->idx++;
116         return r;
117 }
118
119 #define for_each_hole(_iter, _ranges, _end, _i)                         \
120         for (_iter = (struct hole_iter) { .r = _ranges, .end = _end };  \
121              (_iter.idx <= _iter.r.size &&                              \
122               (_i = hole_iter_next(&_iter), true));)
123
124 #include <linux/fiemap.h>
125
126 struct fiemap_iter {
127         struct fiemap           f;
128         struct fiemap_extent    fe[1024];
129         unsigned                idx;
130         int                     fd;
131 };
132
133 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
134 {
135         memset(iter, 0, sizeof(*iter));
136
137         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
138         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
139         iter->fd                = fd;
140 }
141
142 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
143
144 #define fiemap_for_each(fd, iter, extent)                               \
145         for (fiemap_iter_init(&iter, fd);                               \
146              (extent = fiemap_iter_next(&iter)).fe_length;)
147
148 const char *strcmp_prefix(const char *, const char *);
149
150 unsigned hatoi_validate(const char *, const char *);
151
152 u32 crc32c(u32, const void *, size_t);
153
154 char *dev_to_name(dev_t);
155 char *dev_to_path(dev_t);
156 char *dev_to_mount(char *);
157
158 #define args_shift(_nr)                                                 \
159 do {                                                                    \
160         unsigned _n = min((_nr), argc);                                 \
161         argc -= _n;                                                     \
162         argv += _n;                                                     \
163 } while (0)
164
165 #define arg_pop()                                                       \
166 ({                                                                      \
167         char *_ret = argc ? argv[0] : NULL;                             \
168         if (_ret)                                                       \
169                 args_shift(1);                                          \
170         _ret;                                                           \
171 })
172
173 #endif /* _TOOLS_UTIL_H */