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 wappers 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(const struct extent_buffer *eb, \
  48. const void *ptr, 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_CACHE_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. const void *ptr, unsigned long off, \
  88. u##bits val, \
  89. struct btrfs_map_token *token) \
  90. { \
  91. unsigned long part_offset = (unsigned long)ptr; \
  92. unsigned long offset = part_offset + off; \
  93. void *p; \
  94. int err; \
  95. char *kaddr; \
  96. unsigned long map_start; \
  97. unsigned long map_len; \
  98. int size = sizeof(u##bits); \
  99. \
  100. if (token && token->kaddr && token->offset <= offset && \
  101. token->eb == eb && \
  102. (token->offset + PAGE_CACHE_SIZE >= offset + size)) { \
  103. kaddr = token->kaddr; \
  104. p = kaddr + part_offset - token->offset; \
  105. put_unaligned_le##bits(val, p + off); \
  106. return; \
  107. } \
  108. err = map_private_extent_buffer(eb, offset, size, \
  109. &kaddr, &map_start, &map_len); \
  110. if (err) { \
  111. __le##bits val2; \
  112. \
  113. val2 = cpu_to_le##bits(val); \
  114. write_extent_buffer(eb, &val2, offset, size); \
  115. return; \
  116. } \
  117. p = kaddr + part_offset - map_start; \
  118. put_unaligned_le##bits(val, p + off); \
  119. if (token) { \
  120. token->kaddr = kaddr; \
  121. token->offset = map_start; \
  122. token->eb = eb; \
  123. } \
  124. }
  125. DEFINE_BTRFS_SETGET_BITS(8)
  126. DEFINE_BTRFS_SETGET_BITS(16)
  127. DEFINE_BTRFS_SETGET_BITS(32)
  128. DEFINE_BTRFS_SETGET_BITS(64)
  129. void btrfs_node_key(const struct extent_buffer *eb,
  130. struct btrfs_disk_key *disk_key, int nr)
  131. {
  132. unsigned long ptr = btrfs_node_key_ptr_offset(nr);
  133. read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
  134. struct btrfs_key_ptr, key, disk_key);
  135. }