1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <asm/unaligned.h>
11 static bool check_setget_bounds(const struct extent_buffer *eb,
12 const void *ptr, unsigned off, int size)
14 const unsigned long member_offset = (unsigned long)ptr + off;
16 if (unlikely(member_offset + size > eb->len)) {
17 btrfs_warn(eb->fs_info,
18 "bad eb member %s: ptr 0x%lx start %llu member offset %lu size %d",
19 (member_offset > eb->len ? "start" : "end"),
20 (unsigned long)ptr, eb->start, member_offset, size);
27 void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb)
30 token->kaddr = page_address(eb->pages[0]);
35 * Macro templates that define helpers to read/write extent buffer data of a
36 * given size, that are also used via ctree.h for access to item members by
37 * specialized helpers.
40 * - btrfs_set_8 (for 8/16/32/64)
41 * - btrfs_get_8 (for 8/16/32/64)
43 * Generic helpers with a token (cached address of the most recently accessed
45 * - btrfs_set_token_8 (for 8/16/32/64)
46 * - btrfs_get_token_8 (for 8/16/32/64)
48 * The set/get functions handle data spanning two pages transparently, in case
49 * metadata block size is larger than page. Every pointer to metadata items is
50 * an offset into the extent buffer page array, cast to a specific type. This
51 * gives us all the type checking.
53 * The extent buffer pages stored in the array pages do not form a contiguous
54 * phyusical range, but the API functions assume the linear offset to the range
55 * from 0 to metadata node size.
58 #define DEFINE_BTRFS_SETGET_BITS(bits) \
59 u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
60 const void *ptr, unsigned long off) \
62 const unsigned long member_offset = (unsigned long)ptr + off; \
63 const unsigned long idx = get_eb_page_index(member_offset); \
64 const unsigned long oip = get_eb_offset_in_page(token->eb, \
66 const int size = sizeof(u##bits); \
67 u8 lebytes[sizeof(u##bits)]; \
68 const int part = PAGE_SIZE - oip; \
71 ASSERT(token->kaddr); \
72 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
73 if (token->offset <= member_offset && \
74 member_offset + size <= token->offset + PAGE_SIZE) { \
75 return get_unaligned_le##bits(token->kaddr + oip); \
77 token->kaddr = page_address(token->eb->pages[idx]); \
78 token->offset = idx << PAGE_SHIFT; \
79 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE ) \
80 return get_unaligned_le##bits(token->kaddr + oip); \
82 memcpy(lebytes, token->kaddr + oip, part); \
83 token->kaddr = page_address(token->eb->pages[idx + 1]); \
84 token->offset = (idx + 1) << PAGE_SHIFT; \
85 memcpy(lebytes + part, token->kaddr, size - part); \
86 return get_unaligned_le##bits(lebytes); \
88 u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
89 const void *ptr, unsigned long off) \
91 const unsigned long member_offset = (unsigned long)ptr + off; \
92 const unsigned long oip = get_eb_offset_in_page(eb, member_offset); \
93 const unsigned long idx = get_eb_page_index(member_offset); \
94 char *kaddr = page_address(eb->pages[idx]); \
95 const int size = sizeof(u##bits); \
96 const int part = PAGE_SIZE - oip; \
97 u8 lebytes[sizeof(u##bits)]; \
99 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
100 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) \
101 return get_unaligned_le##bits(kaddr + oip); \
103 memcpy(lebytes, kaddr + oip, part); \
104 kaddr = page_address(eb->pages[idx + 1]); \
105 memcpy(lebytes + part, kaddr, size - part); \
106 return get_unaligned_le##bits(lebytes); \
108 void btrfs_set_token_##bits(struct btrfs_map_token *token, \
109 const void *ptr, unsigned long off, \
112 const unsigned long member_offset = (unsigned long)ptr + off; \
113 const unsigned long idx = get_eb_page_index(member_offset); \
114 const unsigned long oip = get_eb_offset_in_page(token->eb, \
116 const int size = sizeof(u##bits); \
117 u8 lebytes[sizeof(u##bits)]; \
118 const int part = PAGE_SIZE - oip; \
121 ASSERT(token->kaddr); \
122 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
123 if (token->offset <= member_offset && \
124 member_offset + size <= token->offset + PAGE_SIZE) { \
125 put_unaligned_le##bits(val, token->kaddr + oip); \
128 token->kaddr = page_address(token->eb->pages[idx]); \
129 token->offset = idx << PAGE_SHIFT; \
130 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) { \
131 put_unaligned_le##bits(val, token->kaddr + oip); \
134 put_unaligned_le##bits(val, lebytes); \
135 memcpy(token->kaddr + oip, lebytes, part); \
136 token->kaddr = page_address(token->eb->pages[idx + 1]); \
137 token->offset = (idx + 1) << PAGE_SHIFT; \
138 memcpy(token->kaddr, lebytes + part, size - part); \
140 void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
141 unsigned long off, u##bits val) \
143 const unsigned long member_offset = (unsigned long)ptr + off; \
144 const unsigned long oip = get_eb_offset_in_page(eb, member_offset); \
145 const unsigned long idx = get_eb_page_index(member_offset); \
146 char *kaddr = page_address(eb->pages[idx]); \
147 const int size = sizeof(u##bits); \
148 const int part = PAGE_SIZE - oip; \
149 u8 lebytes[sizeof(u##bits)]; \
151 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
152 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) { \
153 put_unaligned_le##bits(val, kaddr + oip); \
157 put_unaligned_le##bits(val, lebytes); \
158 memcpy(kaddr + oip, lebytes, part); \
159 kaddr = page_address(eb->pages[idx + 1]); \
160 memcpy(kaddr, lebytes + part, size - part); \
163 DEFINE_BTRFS_SETGET_BITS(8)
164 DEFINE_BTRFS_SETGET_BITS(16)
165 DEFINE_BTRFS_SETGET_BITS(32)
166 DEFINE_BTRFS_SETGET_BITS(64)
168 void btrfs_node_key(const struct extent_buffer *eb,
169 struct btrfs_disk_key *disk_key, int nr)
171 unsigned long ptr = btrfs_node_key_ptr_offset(eb, nr);
172 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
173 struct btrfs_key_ptr, key, disk_key);