xref: /linux/fs/btrfs/accessors.c (revision 3be042cf46feeedf664152d063376b5c17026d1d)
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 static bool check_setget_bounds(const struct extent_buffer *eb,
12 				const void *ptr, unsigned off, int size)
13 {
14 	const unsigned long member_offset = (unsigned long)ptr + off;
15 
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);
21 		return false;
22 	}
23 
24 	return true;
25 }
26 
27 void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb)
28 {
29 	token->eb = eb;
30 	token->kaddr = folio_address(eb->folios[0]);
31 	token->offset = 0;
32 }
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 folios may 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_folio_index(token->eb, member_offset); \
64 	const unsigned long oil = get_eb_offset_in_folio(token->eb,	\
65 							 member_offset);\
66 	const int unit_size = folio_size(token->eb->folios[0]);		\
67 	const int unit_shift = folio_shift(token->eb->folios[0]);	\
68 	const int size = sizeof(u##bits);				\
69 	u8 lebytes[sizeof(u##bits)];					\
70 	const int part = unit_size - oil;				\
71 									\
72 	ASSERT(token);							\
73 	ASSERT(token->kaddr);						\
74 	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
75 	if (token->offset <= member_offset &&				\
76 	    member_offset + size <= token->offset + unit_size) {	\
77 		return get_unaligned_le##bits(token->kaddr + oil);	\
78 	}								\
79 	token->kaddr = folio_address(token->eb->folios[idx]);		\
80 	token->offset = idx << unit_shift;				\
81 	if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \
82 		return get_unaligned_le##bits(token->kaddr + oil);	\
83 									\
84 	memcpy(lebytes, token->kaddr + oil, part);			\
85 	token->kaddr = folio_address(token->eb->folios[idx + 1]);	\
86 	token->offset = (idx + 1) << unit_shift;			\
87 	memcpy(lebytes + part, token->kaddr, size - part);		\
88 	return get_unaligned_le##bits(lebytes);				\
89 }									\
90 u##bits btrfs_get_##bits(const struct extent_buffer *eb,		\
91 			 const void *ptr, unsigned long off)		\
92 {									\
93 	const unsigned long member_offset = (unsigned long)ptr + off;	\
94 	const unsigned long idx = get_eb_folio_index(eb, member_offset);\
95 	const unsigned long oil = get_eb_offset_in_folio(eb,		\
96 							 member_offset);\
97 	const int unit_size = folio_size(eb->folios[0]);		\
98 	char *kaddr = folio_address(eb->folios[idx]);			\
99 	const int size = sizeof(u##bits);				\
100 	const int part = unit_size - oil;				\
101 	u8 lebytes[sizeof(u##bits)];					\
102 									\
103 	ASSERT(check_setget_bounds(eb, ptr, off, size));		\
104 	if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size)	\
105 		return get_unaligned_le##bits(kaddr + oil);		\
106 									\
107 	memcpy(lebytes, kaddr + oil, part);				\
108 	kaddr = folio_address(eb->folios[idx + 1]);			\
109 	memcpy(lebytes + part, kaddr, size - part);			\
110 	return get_unaligned_le##bits(lebytes);				\
111 }									\
112 void btrfs_set_token_##bits(struct btrfs_map_token *token,		\
113 			    const void *ptr, unsigned long off,		\
114 			    u##bits val)				\
115 {									\
116 	const unsigned long member_offset = (unsigned long)ptr + off;	\
117 	const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \
118 	const unsigned long oil = get_eb_offset_in_folio(token->eb,	\
119 							 member_offset);\
120 	const int unit_size = folio_size(token->eb->folios[0]);		\
121 	const int unit_shift = folio_shift(token->eb->folios[0]);	\
122 	const int size = sizeof(u##bits);				\
123 	u8 lebytes[sizeof(u##bits)];					\
124 	const int part = unit_size - oil;				\
125 									\
126 	ASSERT(token);							\
127 	ASSERT(token->kaddr);						\
128 	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
129 	if (token->offset <= member_offset &&				\
130 	    member_offset + size <= token->offset + unit_size) {	\
131 		put_unaligned_le##bits(val, token->kaddr + oil);	\
132 		return;							\
133 	}								\
134 	token->kaddr = folio_address(token->eb->folios[idx]);		\
135 	token->offset = idx << unit_shift;				\
136 	if (INLINE_EXTENT_BUFFER_PAGES == 1 ||				\
137 	    oil + size <= unit_size) {					\
138 		put_unaligned_le##bits(val, token->kaddr + oil);	\
139 		return;							\
140 	}								\
141 	put_unaligned_le##bits(val, lebytes);				\
142 	memcpy(token->kaddr + oil, lebytes, part);			\
143 	token->kaddr = folio_address(token->eb->folios[idx + 1]);	\
144 	token->offset = (idx + 1) << unit_shift;			\
145 	memcpy(token->kaddr, lebytes + part, size - part);		\
146 }									\
147 void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr,	\
148 		      unsigned long off, u##bits val)			\
149 {									\
150 	const unsigned long member_offset = (unsigned long)ptr + off;	\
151 	const unsigned long idx = get_eb_folio_index(eb, member_offset);\
152 	const unsigned long oil = get_eb_offset_in_folio(eb,		\
153 							 member_offset);\
154 	const int unit_size = folio_size(eb->folios[0]);		\
155 	char *kaddr = folio_address(eb->folios[idx]);			\
156 	const int size = sizeof(u##bits);				\
157 	const int part = unit_size - oil;				\
158 	u8 lebytes[sizeof(u##bits)];					\
159 									\
160 	ASSERT(check_setget_bounds(eb, ptr, off, size));		\
161 	if (INLINE_EXTENT_BUFFER_PAGES == 1 ||				\
162 	    oil + size <= unit_size) {					\
163 		put_unaligned_le##bits(val, kaddr + oil);		\
164 		return;							\
165 	}								\
166 									\
167 	put_unaligned_le##bits(val, lebytes);				\
168 	memcpy(kaddr + oil, lebytes, part);				\
169 	kaddr = folio_address(eb->folios[idx + 1]);			\
170 	memcpy(kaddr, lebytes + part, size - part);			\
171 }
172 
173 DEFINE_BTRFS_SETGET_BITS(8)
174 DEFINE_BTRFS_SETGET_BITS(16)
175 DEFINE_BTRFS_SETGET_BITS(32)
176 DEFINE_BTRFS_SETGET_BITS(64)
177 
178 void btrfs_node_key(const struct extent_buffer *eb,
179 		    struct btrfs_disk_key *disk_key, int nr)
180 {
181 	unsigned long ptr = btrfs_node_key_ptr_offset(eb, nr);
182 	read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
183 		       struct btrfs_key_ptr, key, disk_key);
184 }
185