blob: 7928d310f6986042890f820ac8c062d8d59d1c50 [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) \
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -060041u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
42 const void *ptr, unsigned long off, \
Li Zefan18077bb2012-07-09 20:22:35 -060043 struct btrfs_map_token *token) \
Chris Mason0f827312007-10-15 16:18:56 -040044{ \
Li Zefan18077bb2012-07-09 20:22:35 -060045 unsigned long part_offset = (unsigned long)ptr; \
46 unsigned long offset = part_offset + off; \
47 void *p; \
48 int err; \
49 char *kaddr; \
50 unsigned long map_start; \
51 unsigned long map_len; \
52 int size = sizeof(u##bits); \
53 u##bits res; \
54 \
David Sterba48bc3952019-08-09 17:30:23 +020055 ASSERT(token); \
David Sterbac82f8232019-08-09 17:48:21 +020056 ASSERT(token->eb == eb); \
David Sterba48bc3952019-08-09 17:30:23 +020057 \
58 if (token->kaddr && token->offset <= offset && \
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030059 (token->offset + PAGE_SIZE >= offset + size)) { \
Li Zefan18077bb2012-07-09 20:22:35 -060060 kaddr = token->kaddr; \
61 p = kaddr + part_offset - token->offset; \
62 res = get_unaligned_le##bits(p + off); \
63 return res; \
64 } \
David Sterba4dae6662020-04-29 02:13:57 +020065 err = map_private_extent_buffer(token->eb, offset, size, \
Li Zefan18077bb2012-07-09 20:22:35 -060066 &kaddr, &map_start, &map_len); \
67 if (err) { \
68 __le##bits leres; \
69 \
David Sterba4dae6662020-04-29 02:13:57 +020070 read_extent_buffer(token->eb, &leres, offset, size); \
Li Zefan18077bb2012-07-09 20:22:35 -060071 return le##bits##_to_cpu(leres); \
72 } \
73 p = kaddr + part_offset - map_start; \
74 res = get_unaligned_le##bits(p + off); \
David Sterba48bc3952019-08-09 17:30:23 +020075 token->kaddr = kaddr; \
76 token->offset = map_start; \
Li Zefan18077bb2012-07-09 20:22:35 -060077 return res; \
Chris Mason0f827312007-10-15 16:18:56 -040078} \
David Sterbacb495112019-08-09 17:12:38 +020079u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
80 const void *ptr, unsigned long off) \
81{ \
82 unsigned long part_offset = (unsigned long)ptr; \
83 unsigned long offset = part_offset + off; \
84 void *p; \
85 int err; \
86 char *kaddr; \
87 unsigned long map_start; \
88 unsigned long map_len; \
89 int size = sizeof(u##bits); \
90 u##bits res; \
91 \
92 err = map_private_extent_buffer(eb, offset, size, \
93 &kaddr, &map_start, &map_len); \
94 if (err) { \
95 __le##bits leres; \
96 \
97 read_extent_buffer(eb, &leres, offset, size); \
98 return le##bits##_to_cpu(leres); \
99 } \
100 p = kaddr + part_offset - map_start; \
101 res = get_unaligned_le##bits(p + off); \
102 return res; \
103} \
Li Zefan18077bb2012-07-09 20:22:35 -0600104void btrfs_set_token_##bits(struct extent_buffer *eb, \
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600105 const void *ptr, unsigned long off, \
106 u##bits val, \
Li Zefan18077bb2012-07-09 20:22:35 -0600107 struct btrfs_map_token *token) \
Chris Mason0f827312007-10-15 16:18:56 -0400108{ \
Li Zefan18077bb2012-07-09 20:22:35 -0600109 unsigned long part_offset = (unsigned long)ptr; \
110 unsigned long offset = part_offset + off; \
111 void *p; \
112 int err; \
113 char *kaddr; \
114 unsigned long map_start; \
115 unsigned long map_len; \
116 int size = sizeof(u##bits); \
117 \
David Sterba48bc3952019-08-09 17:30:23 +0200118 ASSERT(token); \
David Sterbac82f8232019-08-09 17:48:21 +0200119 ASSERT(token->eb == eb); \
David Sterba48bc3952019-08-09 17:30:23 +0200120 \
121 if (token->kaddr && token->offset <= offset && \
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300122 (token->offset + PAGE_SIZE >= offset + size)) { \
Li Zefan18077bb2012-07-09 20:22:35 -0600123 kaddr = token->kaddr; \
124 p = kaddr + part_offset - token->offset; \
125 put_unaligned_le##bits(val, p + off); \
126 return; \
127 } \
David Sterba4dae6662020-04-29 02:13:57 +0200128 err = map_private_extent_buffer(token->eb, offset, size, \
Li Zefan18077bb2012-07-09 20:22:35 -0600129 &kaddr, &map_start, &map_len); \
130 if (err) { \
131 __le##bits val2; \
132 \
133 val2 = cpu_to_le##bits(val); \
David Sterba4dae6662020-04-29 02:13:57 +0200134 write_extent_buffer(token->eb, &val2, offset, size); \
Li Zefan18077bb2012-07-09 20:22:35 -0600135 return; \
136 } \
137 p = kaddr + part_offset - map_start; \
138 put_unaligned_le##bits(val, p + off); \
David Sterba48bc3952019-08-09 17:30:23 +0200139 token->kaddr = kaddr; \
140 token->offset = map_start; \
David Sterbacb495112019-08-09 17:12:38 +0200141} \
142void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \
143 unsigned long off, u##bits val) \
144{ \
145 unsigned long part_offset = (unsigned long)ptr; \
146 unsigned long offset = part_offset + off; \
147 void *p; \
148 int err; \
149 char *kaddr; \
150 unsigned long map_start; \
151 unsigned long map_len; \
152 int size = sizeof(u##bits); \
153 \
154 err = map_private_extent_buffer(eb, offset, size, \
155 &kaddr, &map_start, &map_len); \
156 if (err) { \
157 __le##bits val2; \
158 \
159 val2 = cpu_to_le##bits(val); \
160 write_extent_buffer(eb, &val2, offset, size); \
161 return; \
162 } \
163 p = kaddr + part_offset - map_start; \
164 put_unaligned_le##bits(val, p + off); \
Li Zefan18077bb2012-07-09 20:22:35 -0600165}
Chris Mason0f827312007-10-15 16:18:56 -0400166
Li Zefan18077bb2012-07-09 20:22:35 -0600167DEFINE_BTRFS_SETGET_BITS(8)
168DEFINE_BTRFS_SETGET_BITS(16)
169DEFINE_BTRFS_SETGET_BITS(32)
170DEFINE_BTRFS_SETGET_BITS(64)
Chris Mason0f827312007-10-15 16:18:56 -0400171
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600172void btrfs_node_key(const struct extent_buffer *eb,
Chris Masone644d022007-11-06 15:09:29 -0500173 struct btrfs_disk_key *disk_key, int nr)
174{
175 unsigned long ptr = btrfs_node_key_ptr_offset(nr);
Chris Masone644d022007-11-06 15:09:29 -0500176 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
177 struct btrfs_key_ptr, key, disk_key);
178}