11 #include <sys/types.h>
14 #include <linux/bug.h>
15 #include <linux/byteorder.h>
16 #include <linux/kernel.h>
17 #include <linux/log2.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/uuid.h>
21 #include "libbcachefs/bcachefs.h"
22 #include "libbcachefs/bbpos.h"
23 #include "libbcachefs/darray.h"
25 #define noreturn __attribute__((noreturn))
27 void die(const char *, ...)
28 __attribute__ ((format (printf, 1, 2))) noreturn;
29 char *mprintf(const char *, ...)
30 __attribute__ ((format (printf, 1, 2)));
31 void *xcalloc(size_t, size_t);
32 void *xmalloc(size_t);
33 void *xrealloc(void *, size_t);
34 void xpread(int, void *, size_t, off_t);
35 void xpwrite(int, const void *, size_t, off_t, const char *);
36 struct stat xfstatat(int, const char *, int);
37 struct stat xfstat(int);
38 struct stat xstat(const char *);
40 #define xopenat(_dirfd, _path, ...) \
42 int _fd = openat((_dirfd), (_path), __VA_ARGS__); \
44 die("Error opening %s: %m", (_path)); \
48 #define xopen(...) xopenat(AT_FDCWD, __VA_ARGS__)
50 #define xioctl(_fd, _nr, ...) \
52 int _ret = ioctl((_fd), (_nr), ##__VA_ARGS__); \
54 die(#_nr " ioctl error: %m"); \
58 void write_file_str(int, const char *, const char *);
59 char *read_file_str(int, const char *);
60 u64 read_file_u64(int, const char *);
62 ssize_t read_string_list_or_die(const char *, const char * const[],
65 u64 get_size(const char *, int);
66 unsigned get_blocksize(const char *, int);
67 int open_for_format(const char *, bool);
76 typedef DARRAY(struct range) ranges;
78 static inline void range_add(ranges *data, u64 offset, u64 size)
80 darray_push(data, ((struct range) {
86 void ranges_sort_merge(ranges *);
87 void ranges_roundup(ranges *, unsigned);
88 void ranges_rounddown(ranges *, unsigned);
96 static inline struct range hole_iter_next(struct hole_iter *iter)
99 .start = iter->idx ? iter->r.data[iter->idx - 1].end : 0,
100 .end = iter->idx < iter->r.nr
101 ? iter->r.data[iter->idx].start : iter->end,
104 BUG_ON(r.start > r.end);
110 #define for_each_hole(_iter, _ranges, _end, _i) \
111 for (_iter = (struct hole_iter) { .r = _ranges, .end = _end }; \
112 (_iter.idx <= _iter.r.nr && \
113 (_i = hole_iter_next(&_iter), true));)
115 #include <linux/fiemap.h>
119 struct fiemap_extent fe[1024];
124 static inline void fiemap_iter_init(struct fiemap_iter *iter, int fd)
126 memset(iter, 0, sizeof(*iter));
128 iter->f.fm_extent_count = ARRAY_SIZE(iter->fe);
129 iter->f.fm_length = FIEMAP_MAX_OFFSET;
133 struct fiemap_extent fiemap_iter_next(struct fiemap_iter *);
135 #define fiemap_for_each(fd, iter, extent) \
136 for (fiemap_iter_init(&iter, fd); \
137 (extent = fiemap_iter_next(&iter)).fe_length;)
139 char *strcmp_prefix(char *, const char *);
141 u32 crc32c(u32, const void *, size_t);
143 char *dev_to_name(dev_t);
144 char *dev_to_path(dev_t);
145 struct mntent *dev_to_mount(char *);
146 int dev_mounted(char *);
148 #define args_shift(_nr) \
150 unsigned _n = min((_nr), argc); \
157 char *_ret = argc ? argv[0] : NULL; \
163 struct bpos bpos_parse(char *);
164 struct bbpos bbpos_parse(char *);
166 #endif /* _TOOLS_UTIL_H */