]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
cmd_device_add 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/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 int open_for_format(const char *, bool);
125
126 int bcachectl_open(void);
127
128 struct bcache_handle {
129         int     ioctl_fd;
130         int     sysfs_fd;
131 };
132
133 struct bcache_handle bcache_fs_open(const char *);
134
135 bool ask_yn(void);
136
137 struct range {
138         u64             start;
139         u64             end;
140 };
141
142 typedef darray(struct range) ranges;
143
144 static inline void range_add(ranges *data, u64 offset, u64 size)
145 {
146         darray_append(*data, (struct range) {
147                 .start = offset,
148                 .end = offset + size
149         });
150 }
151
152 void ranges_sort_merge(ranges *);
153 void ranges_roundup(ranges *, unsigned);
154 void ranges_rounddown(ranges *, unsigned);
155
156 struct hole_iter {
157         ranges          r;
158         size_t          idx;
159         u64             end;
160 };
161
162 static inline struct range hole_iter_next(struct hole_iter *iter)
163 {
164         struct range r = {
165                 .start  = iter->idx ? iter->r.item[iter->idx - 1].end : 0,
166                 .end    = iter->idx < iter->r.size
167                         ? iter->r.item[iter->idx].start : iter->end,
168         };
169
170         BUG_ON(r.start > r.end);
171
172         iter->idx++;
173         return r;
174 }
175
176 #define for_each_hole(_iter, _ranges, _end, _i)                         \
177         for (_iter = (struct hole_iter) { .r = _ranges, .end = _end };  \
178              (_iter.idx <= _iter.r.size &&                              \
179               (_i = hole_iter_next(&_iter), true));)
180
181 #include <linux/fiemap.h>
182
183 struct fiemap_iter {
184         struct fiemap           f;
185         struct fiemap_extent    fe[1024];
186         unsigned                idx;
187         int                     fd;
188 };
189
190 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
191 {
192         memset(iter, 0, sizeof(*iter));
193
194         iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
195         iter->f.fm_length       = FIEMAP_MAX_OFFSET;
196         iter->fd                = fd;
197 }
198
199 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
200
201 #define fiemap_for_each(fd, iter, extent)                               \
202         for (fiemap_iter_init(&iter, fd);                               \
203              (extent = fiemap_iter_next(&iter)).fe_length;)
204
205 const char *strcmp_prefix(const char *, const char *);
206
207 unsigned hatoi_validate(const char *, const char *);
208
209 #endif /* _TOOLS_UTIL_H */