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