conv-funcs.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Functions to convert BTRFS structures from disk to CPU endianness and back.
  4. *
  5. * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
  6. */
  7. #ifndef __BTRFS_CONV_FUNCS_H__
  8. #define __BTRFS_CONV_FUNCS_H__
  9. #include "ctree.h"
  10. #include <u-boot/variadic-macro.h>
  11. #include <asm/byteorder.h>
  12. /* We are using variadic macros and C11 _Generic to achieve compact code.
  13. We want to define macro DEFINE_CONV(x, ...), where the first argument is the
  14. name of the structure for which it shall define conversion functions (the
  15. names of the functions shall be x_to_cpu and x_to_disk), and the other
  16. arguments are names of the members on which the functions shall do
  17. endianness conversion. */
  18. #if defined(__LITTLE_ENDIAN)
  19. /* If the target machine is little endian, the conversion functions do
  20. nothing, since the on disk format is little endian. */
  21. # define DEFINE_CONV(n,...) \
  22. static inline struct n *n##_to_disk(struct n * r) \
  23. { \
  24. return r; \
  25. } \
  26. static inline struct n *n##_to_cpu(struct n * r) \
  27. { \
  28. return r; \
  29. }
  30. # define DEFINE_CONV_ALT(n,a,...) \
  31. static inline struct n *n##_to_disk_##a(struct n * r) \
  32. { \
  33. return r; \
  34. } \
  35. static inline struct n *n##_to_cpu_##a(struct n * r) \
  36. { \
  37. return r; \
  38. }
  39. #else /* !defined(__LITTLE_ENDIAN) */
  40. /* Some structures contain not only scalar members, but compound types as well
  41. (for example, struct btrfs_inode_item contains members of type struct
  42. btrfs_timespec.
  43. For these members we want to call the conversion function recursively, so
  44. first we declare the functions taking pointers to this types (these function
  45. will be defined later by the DEFINE_CONV macro) and then we define
  46. correspond functions taking non-pointers, so that they can be used in the
  47. expansion of the _Generic. */
  48. # define DEFINE_CONV_FOR_STRUCT(n) \
  49. static inline struct n * n##_to_disk(struct n *); \
  50. static inline struct n * n##_to_cpu(struct n *); \
  51. static inline struct n n##_to_disk_v(struct n x) { \
  52. return *n##_to_disk(&x); \
  53. } \
  54. static inline struct n n##_to_cpu_v(struct n x) { \
  55. return *n##_to_cpu(&x); \
  56. }
  57. DEFINE_CONV_FOR_STRUCT(btrfs_key)
  58. DEFINE_CONV_FOR_STRUCT(btrfs_stripe)
  59. DEFINE_CONV_FOR_STRUCT(btrfs_timespec)
  60. DEFINE_CONV_FOR_STRUCT(btrfs_inode_item)
  61. DEFINE_CONV_FOR_STRUCT(btrfs_root_backup)
  62. DEFINE_CONV_FOR_STRUCT(btrfs_dev_item)
  63. /* Now define the _Generic for both CPU to LE and LE to CPU */
  64. # define DEFINE_CONV_CPU_TO_LE(x) \
  65. (d->x) = _Generic((d->x), \
  66. __u16: cpu_to_le16, \
  67. __u32: cpu_to_le32, \
  68. __u64: cpu_to_le64, \
  69. struct btrfs_key: btrfs_key_to_disk_v, \
  70. struct btrfs_stripe: btrfs_stripe_to_disk_v, \
  71. struct btrfs_timespec: btrfs_timespec_to_disk_v, \
  72. struct btrfs_inode_item: btrfs_inode_item_to_disk_v, \
  73. struct btrfs_root_backup: btrfs_root_backup_to_disk_v, \
  74. struct btrfs_dev_item: btrfs_dev_item_to_disk_v \
  75. )((d->x));
  76. # define DEFINE_CONV_LE_TO_CPU(x) \
  77. (d->x) = _Generic((d->x), \
  78. __u16: le16_to_cpu, \
  79. __u32: le32_to_cpu, \
  80. __u64: le64_to_cpu, \
  81. struct btrfs_key: btrfs_key_to_cpu_v, \
  82. struct btrfs_stripe: btrfs_stripe_to_cpu_v, \
  83. struct btrfs_timespec: btrfs_timespec_to_cpu_v, \
  84. struct btrfs_inode_item: btrfs_inode_item_to_cpu_v, \
  85. struct btrfs_root_backup: btrfs_root_backup_to_cpu_v, \
  86. struct btrfs_dev_item: btrfs_dev_item_to_cpu_v \
  87. )((d->x));
  88. # define DEFINE_CONV_ONE(t,n,m,...) \
  89. static inline struct t * n(struct t * d) { \
  90. CALL_MACRO_FOR_EACH(m, ##__VA_ARGS__) \
  91. return d; \
  92. }
  93. /* Finally define the DEFINE_CONV macro */
  94. # define DEFINE_CONV(n,...) \
  95. DEFINE_CONV_ONE(n,n##_to_disk,DEFINE_CONV_CPU_TO_LE,##__VA_ARGS__) \
  96. DEFINE_CONV_ONE(n,n##_to_cpu,DEFINE_CONV_LE_TO_CPU,##__VA_ARGS__)
  97. # define DEFINE_CONV_ALT(n,a,...) \
  98. DEFINE_CONV_ONE(n,n##_to_disk_##a,DEFINE_CONV_CPU_TO_LE, \
  99. ##__VA_ARGS__) \
  100. DEFINE_CONV_ONE(n,n##_to_cpu_##a,DEFINE_CONV_LE_TO_CPU,##__VA_ARGS__)
  101. #endif /* !defined(__LITTLE_ENDIAN) */
  102. DEFINE_CONV(btrfs_key, objectid, offset)
  103. DEFINE_CONV(btrfs_dev_item, devid, total_bytes, bytes_used, io_align, io_width,
  104. sector_size, type, generation, start_offset, dev_group)
  105. DEFINE_CONV(btrfs_stripe, devid, offset)
  106. DEFINE_CONV(btrfs_chunk, length, owner, stripe_len, type, io_align, io_width,
  107. sector_size, num_stripes, sub_stripes)
  108. DEFINE_CONV(btrfs_free_space_entry, offset, bytes)
  109. DEFINE_CONV(btrfs_free_space_header, location, generation, num_entries,
  110. num_bitmaps)
  111. DEFINE_CONV(btrfs_extent_item, refs, generation, flags)
  112. DEFINE_CONV(btrfs_tree_block_info, key)
  113. DEFINE_CONV(btrfs_extent_data_ref, root, objectid, offset, count)
  114. DEFINE_CONV(btrfs_shared_data_ref, count)
  115. DEFINE_CONV(btrfs_extent_inline_ref, offset)
  116. DEFINE_CONV(btrfs_dev_extent, chunk_tree, chunk_objectid, chunk_offset, length)
  117. DEFINE_CONV(btrfs_inode_ref, index, name_len)
  118. DEFINE_CONV(btrfs_inode_extref, parent_objectid, index, name_len)
  119. DEFINE_CONV(btrfs_timespec, sec, nsec)
  120. DEFINE_CONV(btrfs_inode_item, generation, transid, size, nbytes, block_group,
  121. nlink, uid, gid, mode, rdev, flags, sequence, atime, ctime, mtime,
  122. otime)
  123. DEFINE_CONV(btrfs_dir_log_item, end)
  124. DEFINE_CONV(btrfs_dir_item, location, transid, data_len, name_len)
  125. DEFINE_CONV(btrfs_root_item, inode, generation, root_dirid, bytenr, byte_limit,
  126. bytes_used, last_snapshot, flags, refs, drop_progress,
  127. generation_v2, ctransid, otransid, stransid, rtransid, ctime,
  128. otime, stime, rtime)
  129. DEFINE_CONV(btrfs_root_ref, dirid, sequence, name_len)
  130. DEFINE_CONV(btrfs_file_extent_item, generation, ram_bytes, other_encoding,
  131. disk_bytenr, disk_num_bytes, offset, num_bytes)
  132. DEFINE_CONV_ALT(btrfs_file_extent_item, inl, generation, ram_bytes,
  133. other_encoding)
  134. DEFINE_CONV(btrfs_dev_replace_item, src_devid, cursor_left, cursor_right,
  135. cont_reading_from_srcdev_mode, replace_state, time_started,
  136. time_stopped, num_write_errors, num_uncorrectable_read_errors)
  137. DEFINE_CONV(btrfs_block_group_item, used, chunk_objectid, flags)
  138. DEFINE_CONV(btrfs_free_space_info, extent_count, flags)
  139. DEFINE_CONV(btrfs_header, bytenr, flags, generation, owner, nritems)
  140. DEFINE_CONV(btrfs_root_backup, tree_root, tree_root_gen, chunk_root,
  141. chunk_root_gen, extent_root, extent_root_gen, fs_root, fs_root_gen,
  142. dev_root, dev_root_gen, csum_root, csum_root_gen, total_bytes,
  143. bytes_used, num_devices)
  144. DEFINE_CONV(btrfs_super_block, bytenr, flags, magic, generation, root,
  145. chunk_root, log_root, log_root_transid, total_bytes, bytes_used,
  146. root_dir_objectid, num_devices, sectorsize, nodesize,
  147. __unused_leafsize, stripesize, sys_chunk_array_size,
  148. chunk_root_generation, compat_flags, compat_ro_flags,
  149. incompat_flags, csum_type, dev_item, cache_generation,
  150. uuid_tree_generation, super_roots[0], super_roots[1],
  151. super_roots[2], super_roots[3])
  152. DEFINE_CONV(btrfs_item, key, offset, size)
  153. DEFINE_CONV(btrfs_key_ptr, key, blockptr, generation)
  154. #endif /* __BTRFS_CONV_FUNCS_H__ */