compat.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /* No <linux/limits.h> so have to define it here */
  13. #define XATTR_NAME_MAX 255
  14. #define PATH_MAX 4096
  15. /*
  16. * Macros to generate set/get funcs for the struct fields
  17. * assume there is a lefoo_to_cpu for every type, so lets make a simple
  18. * one for u8:
  19. */
  20. #define le8_to_cpu(v) (v)
  21. #define cpu_to_le8(v) (v)
  22. #define __le8 u8
  23. /*
  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. #define get_unaligned_le8(p) (*((u8 *)(p)))
  32. #define get_unaligned_8(p) (*((u8 *)(p)))
  33. #define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
  34. #define put_unaligned_8(val,p) ((*((u8 *)(p))) = (val))
  35. /*
  36. * Read data from device specified by @desc and @part
  37. *
  38. * U-boot equivalent of pread().
  39. *
  40. * Return the bytes of data read.
  41. * Return <0 for error.
  42. */
  43. static inline int __btrfs_devread(struct blk_desc *desc,
  44. struct disk_partition *part,
  45. void *buf, size_t size, u64 offset)
  46. {
  47. lbaint_t sector;
  48. int byte_offset;
  49. int ret;
  50. sector = offset >> desc->log2blksz;
  51. byte_offset = offset % desc->blksz;
  52. /* fs_devread() return 0 for error, >0 for success */
  53. ret = fs_devread(desc, part, sector, byte_offset, size, buf);
  54. if (!ret)
  55. return -EIO;
  56. return size;
  57. }
  58. static inline void uuid_unparse(const u8 *uuid, char *out)
  59. {
  60. return uuid_bin_to_str((unsigned char *)uuid, out, 0);
  61. }
  62. static inline int is_power_of_2(unsigned long n)
  63. {
  64. return (n != 0 && ((n & (n - 1)) == 0));
  65. }
  66. #endif