David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Al Viro | 5f60d5f | 2024-10-01 15:35:57 -0400 | [diff] [blame] | 6 | #include <linux/unaligned.h> |
Josef Bacik | 9b569ea | 2022-10-19 10:50:49 -0400 | [diff] [blame] | 7 | #include "messages.h" |
David Sterba | 2b712e3 | 2024-01-25 17:44:47 +0100 | [diff] [blame] | 8 | #include "extent_io.h" |
| 9 | #include "fs.h" |
Josef Bacik | ad1ac50 | 2022-10-19 10:50:59 -0400 | [diff] [blame] | 10 | #include "accessors.h" |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 11 | |
David Sterba | 5e39468 | 2020-04-30 23:38:11 +0200 | [diff] [blame] | 12 | static bool check_setget_bounds(const struct extent_buffer *eb, |
| 13 | const void *ptr, unsigned off, int size) |
| 14 | { |
| 15 | const unsigned long member_offset = (unsigned long)ptr + off; |
| 16 | |
David Sterba | 234fdd2 | 2020-07-27 20:59:20 +0200 | [diff] [blame] | 17 | if (unlikely(member_offset + size > eb->len)) { |
David Sterba | 5e39468 | 2020-04-30 23:38:11 +0200 | [diff] [blame] | 18 | btrfs_warn(eb->fs_info, |
David Sterba | 234fdd2 | 2020-07-27 20:59:20 +0200 | [diff] [blame] | 19 | "bad eb member %s: ptr 0x%lx start %llu member offset %lu size %d", |
| 20 | (member_offset > eb->len ? "start" : "end"), |
David Sterba | 5e39468 | 2020-04-30 23:38:11 +0200 | [diff] [blame] | 21 | (unsigned long)ptr, eb->start, member_offset, size); |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | return true; |
| 26 | } |
| 27 | |
Josef Bacik | ad1ac50 | 2022-10-19 10:50:59 -0400 | [diff] [blame] | 28 | void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb) |
| 29 | { |
| 30 | token->eb = eb; |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 31 | token->kaddr = folio_address(eb->folios[0]); |
Josef Bacik | ad1ac50 | 2022-10-19 10:50:59 -0400 | [diff] [blame] | 32 | token->offset = 0; |
| 33 | } |
| 34 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 35 | /* |
David Sterba | 583e4a2 | 2020-05-06 20:54:13 +0200 | [diff] [blame] | 36 | * Macro templates that define helpers to read/write extent buffer data of a |
| 37 | * given size, that are also used via ctree.h for access to item members by |
| 38 | * specialized helpers. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 39 | * |
David Sterba | 583e4a2 | 2020-05-06 20:54:13 +0200 | [diff] [blame] | 40 | * Generic helpers: |
| 41 | * - btrfs_set_8 (for 8/16/32/64) |
| 42 | * - btrfs_get_8 (for 8/16/32/64) |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 43 | * |
David Sterba | 583e4a2 | 2020-05-06 20:54:13 +0200 | [diff] [blame] | 44 | * Generic helpers with a token (cached address of the most recently accessed |
| 45 | * page): |
| 46 | * - btrfs_set_token_8 (for 8/16/32/64) |
| 47 | * - btrfs_get_token_8 (for 8/16/32/64) |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 48 | * |
David Sterba | 583e4a2 | 2020-05-06 20:54:13 +0200 | [diff] [blame] | 49 | * The set/get functions handle data spanning two pages transparently, in case |
| 50 | * metadata block size is larger than page. Every pointer to metadata items is |
| 51 | * an offset into the extent buffer page array, cast to a specific type. This |
| 52 | * gives us all the type checking. |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 53 | * |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 54 | * The extent buffer pages stored in the array folios may not form a contiguous |
David Sterba | 583e4a2 | 2020-05-06 20:54:13 +0200 | [diff] [blame] | 55 | * phyusical range, but the API functions assume the linear offset to the range |
| 56 | * from 0 to metadata node size. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 57 | */ |
| 58 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 59 | #define DEFINE_BTRFS_SETGET_BITS(bits) \ |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 60 | u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ |
| 61 | const void *ptr, unsigned long off) \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 62 | { \ |
David Sterba | 8f9da81 | 2020-04-29 17:45:33 +0200 | [diff] [blame] | 63 | const unsigned long member_offset = (unsigned long)ptr + off; \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 64 | const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \ |
| 65 | const unsigned long oil = get_eb_offset_in_folio(token->eb, \ |
| 66 | member_offset);\ |
Qu Wenruo | 84cda1a | 2024-01-05 16:05:55 +1030 | [diff] [blame] | 67 | const int unit_size = token->eb->folio_size; \ |
| 68 | const int unit_shift = token->eb->folio_shift; \ |
David Sterba | 8f9da81 | 2020-04-29 17:45:33 +0200 | [diff] [blame] | 69 | const int size = sizeof(u##bits); \ |
David Sterba | ba8a9a0 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 70 | u8 lebytes[sizeof(u##bits)]; \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 71 | const int part = unit_size - oil; \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 72 | \ |
David Sterba | 48bc395 | 2019-08-09 17:30:23 +0200 | [diff] [blame] | 73 | ASSERT(token); \ |
David Sterba | 870b388 | 2020-04-29 19:29:04 +0200 | [diff] [blame] | 74 | ASSERT(token->kaddr); \ |
David Sterba | 5e39468 | 2020-04-30 23:38:11 +0200 | [diff] [blame] | 75 | ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \ |
David Sterba | 8f9da81 | 2020-04-29 17:45:33 +0200 | [diff] [blame] | 76 | if (token->offset <= member_offset && \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 77 | member_offset + size <= token->offset + unit_size) { \ |
| 78 | return get_unaligned_le##bits(token->kaddr + oil); \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 79 | } \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 80 | token->kaddr = folio_address(token->eb->folios[idx]); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 81 | token->offset = idx << unit_shift; \ |
| 82 | if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \ |
| 83 | return get_unaligned_le##bits(token->kaddr + oil); \ |
David Sterba | ba8a9a0 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 84 | \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 85 | memcpy(lebytes, token->kaddr + oil, part); \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 86 | token->kaddr = folio_address(token->eb->folios[idx + 1]); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 87 | token->offset = (idx + 1) << unit_shift; \ |
David Sterba | ba8a9a0 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 88 | memcpy(lebytes + part, token->kaddr, size - part); \ |
| 89 | return get_unaligned_le##bits(lebytes); \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 90 | } \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 91 | u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ |
| 92 | const void *ptr, unsigned long off) \ |
| 93 | { \ |
David Sterba | 1441ed9 | 2020-04-29 16:04:44 +0200 | [diff] [blame] | 94 | const unsigned long member_offset = (unsigned long)ptr + off; \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 95 | const unsigned long idx = get_eb_folio_index(eb, member_offset);\ |
| 96 | const unsigned long oil = get_eb_offset_in_folio(eb, \ |
| 97 | member_offset);\ |
Qu Wenruo | 84cda1a | 2024-01-05 16:05:55 +1030 | [diff] [blame] | 98 | const int unit_size = eb->folio_size; \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 99 | char *kaddr = folio_address(eb->folios[idx]); \ |
David Sterba | 1441ed9 | 2020-04-29 16:04:44 +0200 | [diff] [blame] | 100 | const int size = sizeof(u##bits); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 101 | const int part = unit_size - oil; \ |
David Sterba | 84da071 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 102 | u8 lebytes[sizeof(u##bits)]; \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 103 | \ |
David Sterba | 5e39468 | 2020-04-30 23:38:11 +0200 | [diff] [blame] | 104 | ASSERT(check_setget_bounds(eb, ptr, off, size)); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 105 | if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \ |
| 106 | return get_unaligned_le##bits(kaddr + oil); \ |
David Sterba | 84da071 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 107 | \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 108 | memcpy(lebytes, kaddr + oil, part); \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 109 | kaddr = folio_address(eb->folios[idx + 1]); \ |
David Sterba | 84da071 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 110 | memcpy(lebytes + part, kaddr, size - part); \ |
| 111 | return get_unaligned_le##bits(lebytes); \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 112 | } \ |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 113 | void btrfs_set_token_##bits(struct btrfs_map_token *token, \ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 114 | const void *ptr, unsigned long off, \ |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 115 | u##bits val) \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 116 | { \ |
David Sterba | ce7afe8 | 2020-04-29 18:23:37 +0200 | [diff] [blame] | 117 | const unsigned long member_offset = (unsigned long)ptr + off; \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 118 | const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \ |
| 119 | const unsigned long oil = get_eb_offset_in_folio(token->eb, \ |
| 120 | member_offset);\ |
Qu Wenruo | 84cda1a | 2024-01-05 16:05:55 +1030 | [diff] [blame] | 121 | const int unit_size = token->eb->folio_size; \ |
| 122 | const int unit_shift = token->eb->folio_shift; \ |
David Sterba | ce7afe8 | 2020-04-29 18:23:37 +0200 | [diff] [blame] | 123 | const int size = sizeof(u##bits); \ |
David Sterba | f472d3c | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 124 | u8 lebytes[sizeof(u##bits)]; \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 125 | const int part = unit_size - oil; \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 126 | \ |
David Sterba | 48bc395 | 2019-08-09 17:30:23 +0200 | [diff] [blame] | 127 | ASSERT(token); \ |
David Sterba | 870b388 | 2020-04-29 19:29:04 +0200 | [diff] [blame] | 128 | ASSERT(token->kaddr); \ |
David Sterba | 5e39468 | 2020-04-30 23:38:11 +0200 | [diff] [blame] | 129 | ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \ |
David Sterba | ce7afe8 | 2020-04-29 18:23:37 +0200 | [diff] [blame] | 130 | if (token->offset <= member_offset && \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 131 | member_offset + size <= token->offset + unit_size) { \ |
| 132 | put_unaligned_le##bits(val, token->kaddr + oil); \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 133 | return; \ |
| 134 | } \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 135 | token->kaddr = folio_address(token->eb->folios[idx]); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 136 | token->offset = idx << unit_shift; \ |
| 137 | if (INLINE_EXTENT_BUFFER_PAGES == 1 || \ |
| 138 | oil + size <= unit_size) { \ |
| 139 | put_unaligned_le##bits(val, token->kaddr + oil); \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 140 | return; \ |
| 141 | } \ |
David Sterba | f472d3c | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 142 | put_unaligned_le##bits(val, lebytes); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 143 | memcpy(token->kaddr + oil, lebytes, part); \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 144 | token->kaddr = folio_address(token->eb->folios[idx + 1]); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 145 | token->offset = (idx + 1) << unit_shift; \ |
David Sterba | f472d3c | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 146 | memcpy(token->kaddr, lebytes + part, size - part); \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 147 | } \ |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 148 | void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 149 | unsigned long off, u##bits val) \ |
| 150 | { \ |
David Sterba | 029e4a4 | 2020-04-29 18:07:04 +0200 | [diff] [blame] | 151 | const unsigned long member_offset = (unsigned long)ptr + off; \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 152 | const unsigned long idx = get_eb_folio_index(eb, member_offset);\ |
| 153 | const unsigned long oil = get_eb_offset_in_folio(eb, \ |
| 154 | member_offset);\ |
Qu Wenruo | 84cda1a | 2024-01-05 16:05:55 +1030 | [diff] [blame] | 155 | const int unit_size = eb->folio_size; \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 156 | char *kaddr = folio_address(eb->folios[idx]); \ |
David Sterba | 029e4a4 | 2020-04-29 18:07:04 +0200 | [diff] [blame] | 157 | const int size = sizeof(u##bits); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 158 | const int part = unit_size - oil; \ |
David Sterba | f4ca8c5 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 159 | u8 lebytes[sizeof(u##bits)]; \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 160 | \ |
David Sterba | 5e39468 | 2020-04-30 23:38:11 +0200 | [diff] [blame] | 161 | ASSERT(check_setget_bounds(eb, ptr, off, size)); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 162 | if (INLINE_EXTENT_BUFFER_PAGES == 1 || \ |
| 163 | oil + size <= unit_size) { \ |
| 164 | put_unaligned_le##bits(val, kaddr + oil); \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 165 | return; \ |
| 166 | } \ |
David Sterba | f4ca8c5 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 167 | \ |
| 168 | put_unaligned_le##bits(val, lebytes); \ |
Qu Wenruo | 8d99361 | 2023-12-12 12:58:36 +1030 | [diff] [blame] | 169 | memcpy(kaddr + oil, lebytes, part); \ |
Qu Wenruo | 082d5bb | 2023-12-07 09:39:27 +1030 | [diff] [blame] | 170 | kaddr = folio_address(eb->folios[idx + 1]); \ |
David Sterba | f4ca8c5 | 2020-04-30 17:57:55 +0200 | [diff] [blame] | 171 | memcpy(kaddr, lebytes + part, size - part); \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 172 | } |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 173 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 174 | DEFINE_BTRFS_SETGET_BITS(8) |
| 175 | DEFINE_BTRFS_SETGET_BITS(16) |
| 176 | DEFINE_BTRFS_SETGET_BITS(32) |
| 177 | DEFINE_BTRFS_SETGET_BITS(64) |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 178 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 179 | void btrfs_node_key(const struct extent_buffer *eb, |
Chris Mason | e644d02 | 2007-11-06 15:09:29 -0500 | [diff] [blame] | 180 | struct btrfs_disk_key *disk_key, int nr) |
| 181 | { |
Josef Bacik | e23efd8 | 2022-11-15 11:16:16 -0500 | [diff] [blame] | 182 | unsigned long ptr = btrfs_node_key_ptr_offset(eb, nr); |
Chris Mason | e644d02 | 2007-11-06 15:09:29 -0500 | [diff] [blame] | 183 | read_eb_member(eb, (struct btrfs_key_ptr *)ptr, |
| 184 | struct btrfs_key_ptr, key, disk_key); |
| 185 | } |