blob: 46a7269bee0759281017113b9133eae6160d73ec [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
David Sterba5e394682020-04-30 23:38:11 +020020static bool check_setget_bounds(const struct extent_buffer *eb,
21 const void *ptr, unsigned off, int size)
22{
23 const unsigned long member_offset = (unsigned long)ptr + off;
24
25 if (member_offset > eb->len) {
26 btrfs_warn(eb->fs_info,
27 "bad eb member start: ptr 0x%lx start %llu member offset %lu size %d",
28 (unsigned long)ptr, eb->start, member_offset, size);
29 return false;
30 }
31 if (member_offset + size > eb->len) {
32 btrfs_warn(eb->fs_info,
33 "bad eb member end: ptr 0x%lx start %llu member offset %lu size %d",
34 (unsigned long)ptr, eb->start, member_offset, size);
35 return false;
36 }
37
38 return true;
39}
40
Li Zefan18077bb2012-07-09 20:22:35 -060041/*
42 * this is some deeply nasty code.
Chris Masond352ac62008-09-29 15:18:18 -040043 *
44 * The end result is that anyone who #includes ctree.h gets a
Li Zefan18077bb2012-07-09 20:22:35 -060045 * declaration for the btrfs_set_foo functions and btrfs_foo functions,
Nicholas D Steeves01327612016-05-19 21:18:45 -040046 * which are wrappers of btrfs_set_token_#bits functions and
Li Zefan18077bb2012-07-09 20:22:35 -060047 * btrfs_get_token_#bits functions, which are defined in this file.
Chris Masond352ac62008-09-29 15:18:18 -040048 *
49 * These setget functions do all the extent_buffer related mapping
50 * required to efficiently read and write specific fields in the extent
51 * buffers. Every pointer to metadata items in btrfs is really just
52 * an unsigned long offset into the extent buffer which has been
53 * cast to a specific type. This gives us all the gcc type checking.
54 *
Li Zefan18077bb2012-07-09 20:22:35 -060055 * The extent buffer api is used to do the page spanning work required to
56 * have a metadata blocksize different from the page size.
David Sterbacb495112019-08-09 17:12:38 +020057 *
58 * There are 2 variants defined, one with a token pointer and one without.
Chris Masond352ac62008-09-29 15:18:18 -040059 */
60
Li Zefan18077bb2012-07-09 20:22:35 -060061#define DEFINE_BTRFS_SETGET_BITS(bits) \
David Sterbacc4c13d2020-04-29 02:15:56 +020062u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
63 const void *ptr, unsigned long off) \
Chris Mason0f827312007-10-15 16:18:56 -040064{ \
David Sterba8f9da812020-04-29 17:45:33 +020065 const unsigned long member_offset = (unsigned long)ptr + off; \
66 const unsigned long idx = member_offset >> PAGE_SHIFT; \
67 const unsigned long oip = offset_in_page(member_offset); \
68 const int size = sizeof(u##bits); \
69 __le##bits leres; \
Li Zefan18077bb2012-07-09 20:22:35 -060070 \
David Sterba48bc3952019-08-09 17:30:23 +020071 ASSERT(token); \
David Sterba870b3882020-04-29 19:29:04 +020072 ASSERT(token->kaddr); \
David Sterba5e394682020-04-30 23:38:11 +020073 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
David Sterba8f9da812020-04-29 17:45:33 +020074 if (token->offset <= member_offset && \
75 member_offset + size <= token->offset + PAGE_SIZE) { \
76 return get_unaligned_le##bits(token->kaddr + oip); \
Li Zefan18077bb2012-07-09 20:22:35 -060077 } \
David Sterba8f9da812020-04-29 17:45:33 +020078 if (oip + size <= PAGE_SIZE) { \
79 token->kaddr = page_address(token->eb->pages[idx]); \
80 token->offset = idx << PAGE_SHIFT; \
81 return get_unaligned_le##bits(token->kaddr + oip); \
Li Zefan18077bb2012-07-09 20:22:35 -060082 } \
David Sterba8f9da812020-04-29 17:45:33 +020083 token->kaddr = page_address(token->eb->pages[idx + 1]); \
84 token->offset = (idx + 1) << PAGE_SHIFT; \
85 read_extent_buffer(token->eb, &leres, member_offset, size); \
86 return le##bits##_to_cpu(leres); \
Chris Mason0f827312007-10-15 16:18:56 -040087} \
David Sterbacb495112019-08-09 17:12:38 +020088u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
89 const void *ptr, unsigned long off) \
90{ \
David Sterba1441ed92020-04-29 16:04:44 +020091 const unsigned long member_offset = (unsigned long)ptr + off; \
92 const unsigned long oip = offset_in_page(member_offset); \
David Sterba84da0712020-04-30 17:57:55 +020093 const unsigned long idx = member_offset >> PAGE_SHIFT; \
94 char *kaddr = page_address(eb->pages[idx]); \
David Sterba1441ed92020-04-29 16:04:44 +020095 const int size = sizeof(u##bits); \
David Sterba84da0712020-04-30 17:57:55 +020096 const int part = PAGE_SIZE - oip; \
97 u8 lebytes[sizeof(u##bits)]; \
David Sterbacb495112019-08-09 17:12:38 +020098 \
David Sterba5e394682020-04-30 23:38:11 +020099 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
David Sterba84da0712020-04-30 17:57:55 +0200100 if (oip + size <= PAGE_SIZE) \
David Sterba1441ed92020-04-29 16:04:44 +0200101 return get_unaligned_le##bits(kaddr + oip); \
David Sterba84da0712020-04-30 17:57:55 +0200102 \
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); \
David Sterbacb495112019-08-09 17:12:38 +0200107} \
David Sterbacc4c13d2020-04-29 02:15:56 +0200108void btrfs_set_token_##bits(struct btrfs_map_token *token, \
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600109 const void *ptr, unsigned long off, \
David Sterbacc4c13d2020-04-29 02:15:56 +0200110 u##bits val) \
Chris Mason0f827312007-10-15 16:18:56 -0400111{ \
David Sterbace7afe82020-04-29 18:23:37 +0200112 const unsigned long member_offset = (unsigned long)ptr + off; \
113 const unsigned long idx = member_offset >> PAGE_SHIFT; \
114 const unsigned long oip = offset_in_page(member_offset); \
115 const int size = sizeof(u##bits); \
116 __le##bits leres; \
Li Zefan18077bb2012-07-09 20:22:35 -0600117 \
David Sterba48bc3952019-08-09 17:30:23 +0200118 ASSERT(token); \
David Sterba870b3882020-04-29 19:29:04 +0200119 ASSERT(token->kaddr); \
David Sterba5e394682020-04-30 23:38:11 +0200120 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
David Sterbace7afe82020-04-29 18:23:37 +0200121 if (token->offset <= member_offset && \
122 member_offset + size <= token->offset + PAGE_SIZE) { \
123 put_unaligned_le##bits(val, token->kaddr + oip); \
Li Zefan18077bb2012-07-09 20:22:35 -0600124 return; \
125 } \
David Sterbace7afe82020-04-29 18:23:37 +0200126 if (oip + size <= PAGE_SIZE) { \
127 token->kaddr = page_address(token->eb->pages[idx]); \
128 token->offset = idx << PAGE_SHIFT; \
129 put_unaligned_le##bits(val, token->kaddr + oip); \
Li Zefan18077bb2012-07-09 20:22:35 -0600130 return; \
131 } \
David Sterbace7afe82020-04-29 18:23:37 +0200132 token->kaddr = page_address(token->eb->pages[idx + 1]); \
133 token->offset = (idx + 1) << PAGE_SHIFT; \
134 leres = cpu_to_le##bits(val); \
135 write_extent_buffer(token->eb, &leres, member_offset, size); \
David Sterbacb495112019-08-09 17:12:38 +0200136} \
David Sterba2b489662020-04-29 03:04:10 +0200137void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
David Sterbacb495112019-08-09 17:12:38 +0200138 unsigned long off, u##bits val) \
139{ \
David Sterba029e4a42020-04-29 18:07:04 +0200140 const unsigned long member_offset = (unsigned long)ptr + off; \
141 const unsigned long oip = offset_in_page(member_offset); \
142 const int size = sizeof(u##bits); \
143 __le##bits leres; \
David Sterbacb495112019-08-09 17:12:38 +0200144 \
David Sterba5e394682020-04-30 23:38:11 +0200145 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
David Sterba029e4a42020-04-29 18:07:04 +0200146 if (oip + size <= PAGE_SIZE) { \
147 const unsigned long idx = member_offset >> PAGE_SHIFT; \
148 char *kaddr = page_address(eb->pages[idx]); \
149 put_unaligned_le##bits(val, kaddr + oip); \
David Sterbacb495112019-08-09 17:12:38 +0200150 return; \
151 } \
David Sterba029e4a42020-04-29 18:07:04 +0200152 leres = cpu_to_le##bits(val); \
153 write_extent_buffer(eb, &leres, member_offset, size); \
Li Zefan18077bb2012-07-09 20:22:35 -0600154}
Chris Mason0f827312007-10-15 16:18:56 -0400155
Li Zefan18077bb2012-07-09 20:22:35 -0600156DEFINE_BTRFS_SETGET_BITS(8)
157DEFINE_BTRFS_SETGET_BITS(16)
158DEFINE_BTRFS_SETGET_BITS(32)
159DEFINE_BTRFS_SETGET_BITS(64)
Chris Mason0f827312007-10-15 16:18:56 -0400160
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600161void btrfs_node_key(const struct extent_buffer *eb,
Chris Masone644d022007-11-06 15:09:29 -0500162 struct btrfs_disk_key *disk_key, int nr)
163{
164 unsigned long ptr = btrfs_node_key_ptr_offset(nr);
Chris Masone644d022007-11-06 15:09:29 -0500165 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
166 struct btrfs_key_ptr, key, disk_key);
167}