Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (C) 2007 Oracle. All rights reserved.
4 : */
5 :
6 : #include <asm/unaligned.h>
7 : #include "messages.h"
8 : #include "ctree.h"
9 : #include "accessors.h"
10 :
11 0 : static bool check_setget_bounds(const struct extent_buffer *eb,
12 : const void *ptr, unsigned off, int size)
13 : {
14 0 : const unsigned long member_offset = (unsigned long)ptr + off;
15 :
16 0 : if (unlikely(member_offset + size > eb->len)) {
17 0 : 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);
21 0 : return false;
22 : }
23 :
24 : return true;
25 : }
26 :
27 0 : void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb)
28 : {
29 0 : token->eb = eb;
30 0 : token->kaddr = page_address(eb->pages[0]);
31 0 : token->offset = 0;
32 0 : }
33 :
34 : /*
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.
38 : *
39 : * Generic helpers:
40 : * - btrfs_set_8 (for 8/16/32/64)
41 : * - btrfs_get_8 (for 8/16/32/64)
42 : *
43 : * Generic helpers with a token (cached address of the most recently accessed
44 : * page):
45 : * - btrfs_set_token_8 (for 8/16/32/64)
46 : * - btrfs_get_token_8 (for 8/16/32/64)
47 : *
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.
52 : *
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.
56 : */
57 :
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) \
61 : { \
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, \
65 : member_offset); \
66 : const int size = sizeof(u##bits); \
67 : u8 lebytes[sizeof(u##bits)]; \
68 : const int part = PAGE_SIZE - oip; \
69 : \
70 : ASSERT(token); \
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); \
76 : } \
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); \
81 : \
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); \
87 : } \
88 : u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
89 : const void *ptr, unsigned long off) \
90 : { \
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)]; \
98 : \
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); \
102 : \
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); \
107 : } \
108 : void btrfs_set_token_##bits(struct btrfs_map_token *token, \
109 : const void *ptr, unsigned long off, \
110 : u##bits val) \
111 : { \
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, \
115 : member_offset); \
116 : const int size = sizeof(u##bits); \
117 : u8 lebytes[sizeof(u##bits)]; \
118 : const int part = PAGE_SIZE - oip; \
119 : \
120 : ASSERT(token); \
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); \
126 : return; \
127 : } \
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); \
132 : return; \
133 : } \
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); \
139 : } \
140 : void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
141 : unsigned long off, u##bits val) \
142 : { \
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)]; \
150 : \
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); \
154 : return; \
155 : } \
156 : \
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); \
161 : }
162 :
163 0 : DEFINE_BTRFS_SETGET_BITS(8)
164 0 : DEFINE_BTRFS_SETGET_BITS(16)
165 0 : DEFINE_BTRFS_SETGET_BITS(32)
166 0 : DEFINE_BTRFS_SETGET_BITS(64)
167 :
168 0 : void btrfs_node_key(const struct extent_buffer *eb,
169 : struct btrfs_disk_key *disk_key, int nr)
170 : {
171 0 : unsigned long ptr = btrfs_node_key_ptr_offset(eb, nr);
172 0 : read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
173 : struct btrfs_key_ptr, key, disk_key);
174 0 : }
|