blob: e63936e4c1e078858122d7e82c2476ac6a4f3aa0 [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 \
55 if (token && token->kaddr && token->offset <= offset && \
56 token->eb == eb && \
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 } \
63 err = map_private_extent_buffer(eb, offset, size, \
64 &kaddr, &map_start, &map_len); \
65 if (err) { \
66 __le##bits leres; \
67 \
68 read_extent_buffer(eb, &leres, offset, size); \
69 return le##bits##_to_cpu(leres); \
70 } \
71 p = kaddr + part_offset - map_start; \
72 res = get_unaligned_le##bits(p + off); \
73 if (token) { \
74 token->kaddr = kaddr; \
75 token->offset = map_start; \
76 token->eb = eb; \
77 } \
78 return res; \
Chris Mason0f827312007-10-15 16:18:56 -040079} \
David Sterbacb495112019-08-09 17:12:38 +020080u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
81 const void *ptr, unsigned long off) \
82{ \
83 unsigned long part_offset = (unsigned long)ptr; \
84 unsigned long offset = part_offset + off; \
85 void *p; \
86 int err; \
87 char *kaddr; \
88 unsigned long map_start; \
89 unsigned long map_len; \
90 int size = sizeof(u##bits); \
91 u##bits res; \
92 \
93 err = map_private_extent_buffer(eb, offset, size, \
94 &kaddr, &map_start, &map_len); \
95 if (err) { \
96 __le##bits leres; \
97 \
98 read_extent_buffer(eb, &leres, offset, size); \
99 return le##bits##_to_cpu(leres); \
100 } \
101 p = kaddr + part_offset - map_start; \
102 res = get_unaligned_le##bits(p + off); \
103 return res; \
104} \
Li Zefan18077bb2012-07-09 20:22:35 -0600105void btrfs_set_token_##bits(struct extent_buffer *eb, \
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600106 const void *ptr, unsigned long off, \
107 u##bits val, \
Li Zefan18077bb2012-07-09 20:22:35 -0600108 struct btrfs_map_token *token) \
Chris Mason0f827312007-10-15 16:18:56 -0400109{ \
Li Zefan18077bb2012-07-09 20:22:35 -0600110 unsigned long part_offset = (unsigned long)ptr; \
111 unsigned long offset = part_offset + off; \
112 void *p; \
113 int err; \
114 char *kaddr; \
115 unsigned long map_start; \
116 unsigned long map_len; \
117 int size = sizeof(u##bits); \
118 \
119 if (token && token->kaddr && token->offset <= offset && \
120 token->eb == eb && \
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300121 (token->offset + PAGE_SIZE >= offset + size)) { \
Li Zefan18077bb2012-07-09 20:22:35 -0600122 kaddr = token->kaddr; \
123 p = kaddr + part_offset - token->offset; \
124 put_unaligned_le##bits(val, p + off); \
125 return; \
126 } \
127 err = map_private_extent_buffer(eb, offset, size, \
128 &kaddr, &map_start, &map_len); \
129 if (err) { \
130 __le##bits val2; \
131 \
132 val2 = cpu_to_le##bits(val); \
133 write_extent_buffer(eb, &val2, offset, size); \
134 return; \
135 } \
136 p = kaddr + part_offset - map_start; \
137 put_unaligned_le##bits(val, p + off); \
138 if (token) { \
139 token->kaddr = kaddr; \
140 token->offset = map_start; \
141 token->eb = eb; \
142 } \
David Sterbacb495112019-08-09 17:12:38 +0200143} \
144void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \
145 unsigned long off, u##bits val) \
146{ \
147 unsigned long part_offset = (unsigned long)ptr; \
148 unsigned long offset = part_offset + off; \
149 void *p; \
150 int err; \
151 char *kaddr; \
152 unsigned long map_start; \
153 unsigned long map_len; \
154 int size = sizeof(u##bits); \
155 \
156 err = map_private_extent_buffer(eb, offset, size, \
157 &kaddr, &map_start, &map_len); \
158 if (err) { \
159 __le##bits val2; \
160 \
161 val2 = cpu_to_le##bits(val); \
162 write_extent_buffer(eb, &val2, offset, size); \
163 return; \
164 } \
165 p = kaddr + part_offset - map_start; \
166 put_unaligned_le##bits(val, p + off); \
Li Zefan18077bb2012-07-09 20:22:35 -0600167}
Chris Mason0f827312007-10-15 16:18:56 -0400168
Li Zefan18077bb2012-07-09 20:22:35 -0600169DEFINE_BTRFS_SETGET_BITS(8)
170DEFINE_BTRFS_SETGET_BITS(16)
171DEFINE_BTRFS_SETGET_BITS(32)
172DEFINE_BTRFS_SETGET_BITS(64)
Chris Mason0f827312007-10-15 16:18:56 -0400173
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600174void btrfs_node_key(const struct extent_buffer *eb,
Chris Masone644d022007-11-06 15:09:29 -0500175 struct btrfs_disk_key *disk_key, int nr)
176{
177 unsigned long ptr = btrfs_node_key_ptr_offset(nr);
Chris Masone644d022007-11-06 15:09:29 -0500178 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
179 struct btrfs_key_ptr, key, disk_key);
180}