]> git.sesse.net Git - bcachefs-tools-debian/blob - tools-util.h
fix sync writes - don't use O_EXCL
[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 void die(const char *, ...);
21 char *mprintf(const char *, ...);
22 void *xcalloc(size_t, size_t);
23 void *xmalloc(size_t);
24 void xpread(int, void *, size_t, off_t);
25 void xpwrite(int, const void *, size_t, off_t);
26 struct stat xfstatat(int, const char *, int);
27 struct stat xfstat(int);
28
29 #define xopenat(_dirfd, _path, ...)                                     \
30 ({                                                                      \
31         int _fd = openat((_dirfd), (_path), __VA_ARGS__);               \
32         if (_fd < 0)                                                    \
33                 die("Error opening %s: %m", (_path));                   \
34         _fd;                                                            \
35 })
36
37 #define xopen(...)      xopenat(AT_FDCWD, __VA_ARGS__)
38
39 #define xioctl(_fd, _nr, ...)                                           \
40 do {                                                                    \
41         if (ioctl((_fd), (_nr), ##__VA_ARGS__))                         \
42                 die(#_nr " ioctl error: %m");                           \
43 } while (0)
44
45 enum units {
46         BYTES,
47         SECTORS,
48         HUMAN_READABLE,
49 };
50
51 struct units_buf __pr_units(u64, enum units);
52
53 struct units_buf {
54         char    b[20];
55 };
56
57 #define pr_units(_v, _u)        __pr_units(_v, _u).b
58
59 char *read_file_str(int, const char *);
60 u64 read_file_u64(int, const char *);
61
62 ssize_t read_string_list_or_die(const char *, const char * const[],
63                                 const char *);
64
65 u64 get_size(const char *, int);
66 unsigned get_blocksize(const char *, int);
67 int open_for_format(const char *, bool);
68
69 int bcachectl_open(void);
70
71 struct bcache_handle {
72         int     ioctl_fd;
73         int     sysfs_fd;
74 };
75
76 struct bcache_handle bcache_fs_open(const char *);
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 #endif /* _TOOLS_UTIL_H */