blob: 02173dea5f48ca3c84d7014a0841829762b0b7fa [file] [log] [blame]
Qu Wenruo4aebb992020-06-24 18:02:49 +02001// 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
Qu Wenruof06bfcf2020-06-24 18:03:01 +020015#define ASSERT(c) assert(c)
16
Qu Wenruo4aebb992020-06-24 18:02:49 +020017#define BTRFS_UUID_UNPARSED_SIZE 37
18
Qu Wenruocafffc52020-06-24 18:03:02 +020019/* No <linux/limits.h> so have to define it here */
20#define XATTR_NAME_MAX 255
Qu Wenruo8098da72020-06-24 18:03:12 +020021#define PATH_MAX 4096
Qu Wenruocafffc52020-06-24 18:03:02 +020022
Qu Wenruo4aebb992020-06-24 18:02:49 +020023/*
24 * Macros to generate set/get funcs for the struct fields
25 * assume there is a lefoo_to_cpu for every type, so lets make a simple
26 * one for u8:
27 */
28#define le8_to_cpu(v) (v)
29#define cpu_to_le8(v) (v)
30#define __le8 u8
31
32/*
Qu Wenruobe359422020-06-24 18:02:53 +020033 * Macros to generate set/get funcs for the struct fields
34 * assume there is a lefoo_to_cpu for every type, so lets make a simple
35 * one for u8:
36 */
37#define le8_to_cpu(v) (v)
38#define cpu_to_le8(v) (v)
39#define __le8 u8
40
41#define get_unaligned_le8(p) (*((u8 *)(p)))
42#define get_unaligned_8(p) (*((u8 *)(p)))
43#define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
44#define put_unaligned_8(val,p) ((*((u8 *)(p))) = (val))
45
46/*
Qu Wenruo4aebb992020-06-24 18:02:49 +020047 * Read data from device specified by @desc and @part
48 *
Michal Simek1be82af2023-05-17 09:17:16 +020049 * U-Boot equivalent of pread().
Qu Wenruo4aebb992020-06-24 18:02:49 +020050 *
51 * Return the bytes of data read.
52 * Return <0 for error.
53 */
54static inline int __btrfs_devread(struct blk_desc *desc,
55 struct disk_partition *part,
56 void *buf, size_t size, u64 offset)
57{
58 lbaint_t sector;
59 int byte_offset;
60 int ret;
61
62 sector = offset >> desc->log2blksz;
63 byte_offset = offset % desc->blksz;
64
65 /* fs_devread() return 0 for error, >0 for success */
66 ret = fs_devread(desc, part, sector, byte_offset, size, buf);
67 if (!ret)
68 return -EIO;
69 return size;
70}
71
72static inline void uuid_unparse(const u8 *uuid, char *out)
73{
74 return uuid_bin_to_str((unsigned char *)uuid, out, 0);
75}
76
Qu Wenruob1f00672020-06-24 18:02:54 +020077static inline int is_power_of_2(unsigned long n)
78{
79 return (n != 0 && ((n & (n - 1)) == 0));
80}
81
Qu Wenruo4aebb992020-06-24 18:02:49 +020082#endif