Qu Wenruo | 1a61808 | 2020-06-24 18:02:49 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #ifndef __BTRFS_COMPAT_H__ |
| 4 | #define __BTRFS_COMPAT_H__ |
| 5 | |
| 6 | #include <linux/errno.h> |
| 7 | #include <fs_internal.h> |
| 8 | #include <uuid.h> |
| 9 | |
| 10 | /* Provide a compatibility layer to make code syncing easier */ |
| 11 | |
| 12 | /* A simple wraper to for error() used in btrfs-progs */ |
| 13 | #define error(fmt, ...) pr_err("BTRFS: " fmt "\n", ##__VA_ARGS__) |
| 14 | |
| 15 | #define BTRFS_UUID_UNPARSED_SIZE 37 |
| 16 | |
| 17 | /* |
| 18 | * Macros to generate set/get funcs for the struct fields |
| 19 | * assume there is a lefoo_to_cpu for every type, so lets make a simple |
| 20 | * one for u8: |
| 21 | */ |
| 22 | #define le8_to_cpu(v) (v) |
| 23 | #define cpu_to_le8(v) (v) |
| 24 | #define __le8 u8 |
| 25 | |
| 26 | /* |
Qu Wenruo | 6925bd2 | 2020-06-24 18:02:53 +0200 | [diff] [blame] | 27 | * Macros to generate set/get funcs for the struct fields |
| 28 | * assume there is a lefoo_to_cpu for every type, so lets make a simple |
| 29 | * one for u8: |
| 30 | */ |
| 31 | #define le8_to_cpu(v) (v) |
| 32 | #define cpu_to_le8(v) (v) |
| 33 | #define __le8 u8 |
| 34 | |
| 35 | #define get_unaligned_le8(p) (*((u8 *)(p))) |
| 36 | #define get_unaligned_8(p) (*((u8 *)(p))) |
| 37 | #define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val)) |
| 38 | #define put_unaligned_8(val,p) ((*((u8 *)(p))) = (val)) |
| 39 | |
| 40 | /* |
Qu Wenruo | 1a61808 | 2020-06-24 18:02:49 +0200 | [diff] [blame] | 41 | * Read data from device specified by @desc and @part |
| 42 | * |
| 43 | * U-boot equivalent of pread(). |
| 44 | * |
| 45 | * Return the bytes of data read. |
| 46 | * Return <0 for error. |
| 47 | */ |
| 48 | static inline int __btrfs_devread(struct blk_desc *desc, |
| 49 | struct disk_partition *part, |
| 50 | void *buf, size_t size, u64 offset) |
| 51 | { |
| 52 | lbaint_t sector; |
| 53 | int byte_offset; |
| 54 | int ret; |
| 55 | |
| 56 | sector = offset >> desc->log2blksz; |
| 57 | byte_offset = offset % desc->blksz; |
| 58 | |
| 59 | /* fs_devread() return 0 for error, >0 for success */ |
| 60 | ret = fs_devread(desc, part, sector, byte_offset, size, buf); |
| 61 | if (!ret) |
| 62 | return -EIO; |
| 63 | return size; |
| 64 | } |
| 65 | |
| 66 | static inline void uuid_unparse(const u8 *uuid, char *out) |
| 67 | { |
| 68 | return uuid_bin_to_str((unsigned char *)uuid, out, 0); |
| 69 | } |
| 70 | |
Qu Wenruo | f6377ff | 2020-06-24 18:02:54 +0200 | [diff] [blame^] | 71 | static inline int is_power_of_2(unsigned long n) |
| 72 | { |
| 73 | return (n != 0 && ((n & (n - 1)) == 0)); |
| 74 | } |
| 75 | |
Qu Wenruo | 1a61808 | 2020-06-24 18:02:49 +0200 | [diff] [blame] | 76 | #endif |