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