compat.h 1.8 KB

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