]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
cmd_migrate
[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/stat.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #include <linux/bug.h>
13 #include <linux/byteorder.h>
14 #include <linux/kernel.h>
15 #include <linux/log2.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include "ccan/darray/darray.h"
19
20 #define die(arg, ...)                                                   \
21 do {                                                                    \
22         fprintf(stderr, arg "\n", ##__VA_ARGS__);                       \
23         exit(EXIT_FAILURE);                                             \
24 } while (0)
25
26 #define mprintf(...)                                                    \
27 ({                                                                      \
28         char *_str;                                                     \
29         asprintf(&_str, __VA_ARGS__);                                   \
30         _str;                                                           \
31 })
32
33 static inline void *xcalloc(size_t count, size_t size)
34 {
35         void *p = calloc(count, size);
36
37         if (!p)
38                 die("insufficient memory");
39
40         return p;
41 }
42
43 static inline void *xmalloc(size_t size)
44 {
45         void *p = malloc(size);
46
47         if (!p)
48                 die("insufficient memory");
49
50         memset(p, 0, size);
51         return p;
52 }
53
54 static inline void xpread(int fd, void *buf, size_t count, off_t offset)
55 {
56         ssize_t r = pread(fd, buf, count, offset);
57
58         if (r != count)
59                 die("read error (ret %zi)", r);
60 }
61
62 static inline void xpwrite(int fd, const void *buf, size_t count, off_t offset)
63 {
64         ssize_t r = pwrite(fd, buf, count, offset);
65
66         if (r != count)
67                 die("write error (ret %zi err %s)", r, strerror(errno));
68 }
69
70 #define xopenat(_dirfd, _path, ...)                                     \
71 ({                                                                      \
72         int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
73         if (_fd < 0)                                                    \
74                 die("Error opening %s: %s", (_path), strerror(errno));  \
75         _fd;                                                            \
76 })
77
78 #define xopen(...)      xopenat(AT_FDCWD, __VA_ARGS__)
79
80 static inline struct stat xfstatat(int dirfd, const char *path, int flags)
81 {
82         struct stat stat;
83         if (fstatat(dirfd, path, &stat, flags))
84                 die("stat error: %s", strerror(errno));
85         return stat;
86 }
87
88 static inline struct stat xfstat(int fd)
89 {
90         struct stat stat;
91         if (fstat(fd, &stat))
92                 die("stat error: %s", strerror(errno));
93         return stat;
94 }
95
96 #define xioctl(_fd, _nr, ...)                                           \
97 do {                                                                    \
98         if (ioctl((_fd), (_nr), ##__VA_ARGS__))                         \
99                 die(#_nr " ioctl error: %s", strerror(errno));          \
100 } while (0)
101
102 enum units {
103         BYTES,
104         SECTORS,
105         HUMAN_READABLE,
106 };
107
108 struct units_buf __pr_units(u64, enum units);
109
110 struct units_buf {
111         char    b[20];
112 };
113
114 #define pr_units(_v, _u)        __pr_units(_v, _u).b
115
116 char *read_file_str(int, const char *);
117 u64 read_file_u64(int, const char *);
118
119 ssize_t read_string_list_or_die(const char *, const char * const[],
120                                 const char *);
121
122 u64 get_size(const char *, int);
123 unsigned get_blocksize(const char *, int);
124
125 int bcachectl_open(void);
126
127 struct bcache_handle {
128         int     ioctl_fd;
129         int     sysfs_fd;
130 };
131
132 struct bcache_handle bcache_fs_open(const char *);
133
134 bool ask_yn(void);
135
136 struct range {
137         u64             start;
138         u64             end;
139 };
140
141 typedef darray(struct range) ranges;
142
143 static inline void range_add(ranges *data, u64 offset, u64 size)
144 {
145         darray_append(*data, (struct range) {
146                 .start = offset,
147                 .end = offset + size
148         });
149 }
150
151 void ranges_sort_merge(ranges *);
152 void ranges_roundup(ranges *, unsigned);
153 void ranges_rounddown(ranges *, unsigned);
154
155 struct hole_iter {
156         ranges          r;
157         size_t          idx;
158         u64             end;
159 };
160
161 static inline struct range hole_iter_next(struct hole_iter *iter)
162 {
163         struct range r = {
164                 .start  = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
165                 .end    = iter->idx < iter->r.size
166                         ? iter->r.item[iter->idx].start : iter->end,
167         };
168
169         BUG_ON(r.start > r.end);
170
171         iter->idx++;
172         return r;
173 }
174
175 #define for_each_hole(_iter, _ranges, _end, _i)                         \
176         for (_iter = (struct hole_iter) { .r = _ranges, .end = _end };  \
177              (_iter.idx <= _iter.r.size &&                              \
178               (_i = hole_iter_next(&_iter), true));)
179
180 #include <linux/fiemap.h>
181
182 struct fiemap_iter {
183         struct fiemap           f;
184         struct fiemap_extent    fe[1024];
185         unsigned                idx;
186         int                     fd;
187 };
188
189 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
190 {
191         memset(iter, 0, sizeof(*iter));
192
193         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
194         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
195         iter->fd                = fd;
196 }
197
198 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
199
200 #define fiemap_for_each(fd, iter, extent)                               \
201         for (fiemap_iter_init(&iter, fd);                               \
202              (extent = fiemap_iter_next(&iter)).fe_length;)
203
204 const char *strcmp_prefix(const char *, const char *);
205
206 #endif /* _TOOLS_UTIL_H */