conv-funcs.h 6.9 KB

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