blob: cef628a5a9e0c18b002d93e2115bbac0cd7fed97 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason0f827312007-10-15 16:18:56 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason0f827312007-10-15 16:18:56 -04004 */
5
Li Zefan18077bb2012-07-09 20:22:35 -06006#include <asm/unaligned.h>
Chris Masond352ac62008-09-29 15:18:18 -04007
Li Zefan18077bb2012-07-09 20:22:35 -06008#include "ctree.h"
9
10static inline u8 get_unaligned_le8(const void *p)
11{
12 return *(u8 *)p;
13}
14
15static inline void put_unaligned_le8(u8 val, void *p)
16{
17 *(u8 *)p = val;
18}
19
20/*
21 * this is some deeply nasty code.
Chris Masond352ac62008-09-29 15:18:18 -040022 *
23 * The end result is that anyone who #includes ctree.h gets a
Li Zefan18077bb2012-07-09 20:22:35 -060024 * declaration for the btrfs_set_foo functions and btrfs_foo functions,
Nicholas D Steeves01327612016-05-19 21:18:45 -040025 * which are wrappers of btrfs_set_token_#bits functions and
Li Zefan18077bb2012-07-09 20:22:35 -060026 * btrfs_get_token_#bits functions, which are defined in this file.
Chris Masond352ac62008-09-29 15:18:18 -040027 *
28 * These setget functions do all the extent_buffer related mapping
29 * required to efficiently read and write specific fields in the extent
30 * buffers. Every pointer to metadata items in btrfs is really just
31 * an unsigned long offset into the extent buffer which has been
32 * cast to a specific type. This gives us all the gcc type checking.
33 *
Li Zefan18077bb2012-07-09 20:22:35 -060034 * The extent buffer api is used to do the page spanning work required to
35 * have a metadata blocksize different from the page size.
David Sterbacb495112019-08-09 17:12:38 +020036 *
37 * There are 2 variants defined, one with a token pointer and one without.
Chris Masond352ac62008-09-29 15:18:18 -040038 */
39
Li Zefan18077bb2012-07-09 20:22:35 -060040#define DEFINE_BTRFS_SETGET_BITS(bits) \
David Sterbacc4c13d2020-04-29 02:15:56 +020041u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
42 const void *ptr, unsigned long off) \
Chris Mason0f827312007-10-15 16:18:56 -040043{ \
Li Zefan18077bb2012-07-09 20:22:35 -060044 unsigned long part_offset = (unsigned long)ptr; \
45 unsigned long offset = part_offset + off; \
46 void *p; \
47 int err; \
48 char *kaddr; \
49 unsigned long map_start; \
50 unsigned long map_len; \
51 int size = sizeof(u##bits); \
52 u##bits res; \
53 \
David Sterba48bc3952019-08-09 17:30:23 +020054 ASSERT(token); \
David Sterba870b3882020-04-29 19:29:04 +020055 ASSERT(token->kaddr); \
56 if (token->offset <= offset && \
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030057 (token->offset + PAGE_SIZE >= offset + size)) { \
Li Zefan18077bb2012-07-09 20:22:35 -060058 kaddr = token->kaddr; \
59 p = kaddr + part_offset - token->offset; \
60 res = get_unaligned_le##bits(p + off); \
61 return res; \
62 } \
David Sterba4dae6662020-04-29 02:13:57 +020063 err = map_private_extent_buffer(token->eb, offset, size, \
Li Zefan18077bb2012-07-09 20:22:35 -060064 &kaddr, &map_start, &map_len); \
65 if (err) { \
66 __le##bits leres; \
67 \
David Sterba4dae6662020-04-29 02:13:57 +020068 read_extent_buffer(token->eb, &leres, offset, size); \
Li Zefan18077bb2012-07-09 20:22:35 -060069 return le##bits##_to_cpu(leres); \
70 } \
71 p = kaddr + part_offset - map_start; \
72 res = get_unaligned_le##bits(p + off); \
David Sterba48bc3952019-08-09 17:30:23 +020073 token->kaddr = kaddr; \
74 token->offset = map_start; \
Li Zefan18077bb2012-07-09 20:22:35 -060075 return res; \
Chris Mason0f827312007-10-15 16:18:56 -040076} \
David Sterbacb495112019-08-09 17:12:38 +020077u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
78 const void *ptr, unsigned long off) \
79{ \
80 unsigned long part_offset = (unsigned long)ptr; \
81 unsigned long offset = part_offset + off; \
82 void *p; \
83 int err; \
84 char *kaddr; \
85 unsigned long map_start; \
86 unsigned long map_len; \
87 int size = sizeof(u##bits); \
88 u##bits res; \
89 \
90 err = map_private_extent_buffer(eb, offset, size, \
91 &kaddr, &map_start, &map_len); \
92 if (err) { \
93 __le##bits leres; \
94 \
95 read_extent_buffer(eb, &leres, offset, size); \
96 return le##bits##_to_cpu(leres); \
97 } \
98 p = kaddr + part_offset - map_start; \
99 res = get_unaligned_le##bits(p + off); \
100 return res; \
101} \
David Sterbacc4c13d2020-04-29 02:15:56 +0200102void btrfs_set_token_##bits(struct btrfs_map_token *token, \
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600103 const void *ptr, unsigned long off, \
David Sterbacc4c13d2020-04-29 02:15:56 +0200104 u##bits val) \
Chris Mason0f827312007-10-15 16:18:56 -0400105{ \
Li Zefan18077bb2012-07-09 20:22:35 -0600106 unsigned long part_offset = (unsigned long)ptr; \
107 unsigned long offset = part_offset + off; \
108 void *p; \
109 int err; \
110 char *kaddr; \
111 unsigned long map_start; \
112 unsigned long map_len; \
113 int size = sizeof(u##bits); \
114 \
David Sterba48bc3952019-08-09 17:30:23 +0200115 ASSERT(token); \
David Sterba870b3882020-04-29 19:29:04 +0200116 ASSERT(token->kaddr); \
117 if (token->offset <= offset && \
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300118 (token->offset + PAGE_SIZE >= offset + size)) { \
Li Zefan18077bb2012-07-09 20:22:35 -0600119 kaddr = token->kaddr; \
120 p = kaddr + part_offset - token->offset; \
121 put_unaligned_le##bits(val, p + off); \
122 return; \
123 } \
David Sterba4dae6662020-04-29 02:13:57 +0200124 err = map_private_extent_buffer(token->eb, offset, size, \
Li Zefan18077bb2012-07-09 20:22:35 -0600125 &kaddr, &map_start, &map_len); \
126 if (err) { \
127 __le##bits val2; \
128 \
129 val2 = cpu_to_le##bits(val); \
David Sterba4dae6662020-04-29 02:13:57 +0200130 write_extent_buffer(token->eb, &val2, offset, size); \
Li Zefan18077bb2012-07-09 20:22:35 -0600131 return; \
132 } \
133 p = kaddr + part_offset - map_start; \
134 put_unaligned_le##bits(val, p + off); \
David Sterba48bc3952019-08-09 17:30:23 +0200135 token->kaddr = kaddr; \
136 token->offset = map_start; \
David Sterbacb495112019-08-09 17:12:38 +0200137} \
138void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \
139 unsigned long off, u##bits val) \
140{ \
141 unsigned long part_offset = (unsigned long)ptr; \
142 unsigned long offset = part_offset + off; \
143 void *p; \
144 int err; \
145 char *kaddr; \
146 unsigned long map_start; \
147 unsigned long map_len; \
148 int size = sizeof(u##bits); \
149 \
150 err = map_private_extent_buffer(eb, offset, size, \
151 &kaddr, &map_start, &map_len); \
152 if (err) { \
153 __le##bits val2; \
154 \
155 val2 = cpu_to_le##bits(val); \
156 write_extent_buffer(eb, &val2, offset, size); \
157 return; \
158 } \
159 p = kaddr + part_offset - map_start; \
160 put_unaligned_le##bits(val, p + off); \
Li Zefan18077bb2012-07-09 20:22:35 -0600161}
Chris Mason0f827312007-10-15 16:18:56 -0400162
Li Zefan18077bb2012-07-09 20:22:35 -0600163DEFINE_BTRFS_SETGET_BITS(8)
164DEFINE_BTRFS_SETGET_BITS(16)
165DEFINE_BTRFS_SETGET_BITS(32)
166DEFINE_BTRFS_SETGET_BITS(64)
Chris Mason0f827312007-10-15 16:18:56 -0400167
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600168void btrfs_node_key(const struct extent_buffer *eb,
Chris Masone644d022007-11-06 15:09:29 -0500169 struct btrfs_disk_key *disk_key, int nr)
170{
171 unsigned long ptr = btrfs_node_key_ptr_offset(nr);
Chris Masone644d022007-11-06 15:09:29 -0500172 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
173 struct btrfs_key_ptr, key, disk_key);
174}