struct-funcs.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/highmem.h>
  19. #include <asm/unaligned.h>
  20. #include "ctree.h"
  21. static inline u8 get_unaligned_le8(const void *p)
  22. {
  23. return *(u8 *)p;
  24. }
  25. static inline void put_unaligned_le8(u8 val, void *p)
  26. {
  27. *(u8 *)p = val;
  28. }
  29. /*
  30. * this is some deeply nasty code.
  31. *
  32. * The end result is that anyone who #includes ctree.h gets a
  33. * declaration for the btrfs_set_foo functions and btrfs_foo functions,
  34. * which are wrappers of btrfs_set_token_#bits functions and
  35. * btrfs_get_token_#bits functions, which are defined in this file.
  36. *
  37. * These setget functions do all the extent_buffer related mapping
  38. * required to efficiently read and write specific fields in the extent
  39. * buffers. Every pointer to metadata items in btrfs is really just
  40. * an unsigned long offset into the extent buffer which has been
  41. * cast to a specific type. This gives us all the gcc type checking.
  42. *
  43. * The extent buffer api is used to do the page spanning work required to
  44. * have a metadata blocksize different from the page size.
  45. */
  46. #define DEFINE_BTRFS_SETGET_BITS(bits) \
  47. u##bits btrfs_get_token_##bits(struct extent_buffer *eb, void *ptr, \
  48. unsigned long off, \
  49. struct btrfs_map_token *token) \
  50. { \
  51. unsigned long part_offset = (unsigned long)ptr; \
  52. unsigned long offset = part_offset + off; \
  53. void *p; \
  54. int err; \
  55. char *kaddr; \
  56. unsigned long map_start; \
  57. unsigned long map_len; \
  58. int size = sizeof(u##bits); \
  59. u##bits res; \
  60. \
  61. if (token && token->kaddr && token->offset <= offset && \
  62. token->eb == eb && \
  63. (token->offset + PAGE_SIZE >= offset + size)) { \
  64. kaddr = token->kaddr; \
  65. p = kaddr + part_offset - token->offset; \
  66. res = get_unaligned_le##bits(p + off); \
  67. return res; \
  68. } \
  69. err = map_private_extent_buffer(eb, offset, size, \
  70. &kaddr, &map_start, &map_len); \
  71. if (err) { \
  72. __le##bits leres; \
  73. \
  74. read_extent_buffer(eb, &leres, offset, size); \
  75. return le##bits##_to_cpu(leres); \
  76. } \
  77. p = kaddr + part_offset - map_start; \
  78. res = get_unaligned_le##bits(p + off); \
  79. if (token) { \
  80. token->kaddr = kaddr; \
  81. token->offset = map_start; \
  82. token->eb = eb; \
  83. } \
  84. return res; \
  85. } \
  86. void btrfs_set_token_##bits(struct extent_buffer *eb, \
  87. void *ptr, unsigned long off, u##bits val, \
  88. struct btrfs_map_token *token) \
  89. { \
  90. unsigned long part_offset = (unsigned long)ptr; \
  91. unsigned long offset = part_offset + off; \
  92. void *p; \
  93. int err; \
  94. char *kaddr; \
  95. unsigned long map_start; \
  96. unsigned long map_len; \
  97. int size = sizeof(u##bits); \
  98. \
  99. if (token && token->kaddr && token->offset <= offset && \
  100. token->eb == eb && \
  101. (token->offset + PAGE_SIZE >= offset + size)) { \
  102. kaddr = token->kaddr; \
  103. p = kaddr + part_offset - token->offset; \
  104. put_unaligned_le##bits(val, p + off); \
  105. return; \
  106. } \
  107. err = map_private_extent_buffer(eb, offset, size, \
  108. &kaddr, &map_start, &map_len); \
  109. if (err) { \
  110. __le##bits val2; \
  111. \
  112. val2 = cpu_to_le##bits(val); \
  113. write_extent_buffer(eb, &val2, offset, size); \
  114. return; \
  115. } \
  116. p = kaddr + part_offset - map_start; \
  117. put_unaligned_le##bits(val, p + off); \
  118. if (token) { \
  119. token->kaddr = kaddr; \
  120. token->offset = map_start; \
  121. token->eb = eb; \
  122. } \
  123. }
  124. DEFINE_BTRFS_SETGET_BITS(8)
  125. DEFINE_BTRFS_SETGET_BITS(16)
  126. DEFINE_BTRFS_SETGET_BITS(32)
  127. DEFINE_BTRFS_SETGET_BITS(64)
  128. void btrfs_node_key(struct extent_buffer *eb,
  129. struct btrfs_disk_key *disk_key, int nr)
  130. {
  131. unsigned long ptr = btrfs_node_key_ptr_offset(nr);
  132. read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
  133. struct btrfs_key_ptr, key, disk_key);
  134. }