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 | |
| 10 | static inline u8 get_unaligned_le8(const void *p) |
| 11 | { |
| 12 | return *(u8 *)p; |
| 13 | } |
| 14 | |
| 15 | static 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 Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 22 | * |
| 23 | * The end result is that anyone who #includes ctree.h gets a |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 24 | * declaration for the btrfs_set_foo functions and btrfs_foo functions, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 25 | * which are wrappers of btrfs_set_token_#bits functions and |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 26 | * btrfs_get_token_#bits functions, which are defined in this file. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 27 | * |
| 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 Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 34 | * 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 Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 36 | * |
| 37 | * There are 2 variants defined, one with a token pointer and one without. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 38 | */ |
| 39 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 40 | #define DEFINE_BTRFS_SETGET_BITS(bits) \ |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 41 | u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ |
| 42 | const void *ptr, unsigned long off) \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 43 | { \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 44 | unsigned long part_offset = (unsigned long)ptr; \ |
| 45 | unsigned long offset = part_offset + off; \ |
| 46 | void *p; \ |
| 47 | int err; \ |
| 48 | char *kaddr; \ |
| 49 | unsigned long map_start; \ |
| 50 | unsigned long map_len; \ |
| 51 | int size = sizeof(u##bits); \ |
| 52 | u##bits res; \ |
| 53 | \ |
David Sterba | 48bc395 | 2019-08-09 17:30:23 +0200 | [diff] [blame] | 54 | ASSERT(token); \ |
David Sterba | 870b388 | 2020-04-29 19:29:04 +0200 | [diff] [blame^] | 55 | ASSERT(token->kaddr); \ |
| 56 | if (token->offset <= offset && \ |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 57 | (token->offset + PAGE_SIZE >= offset + size)) { \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 58 | kaddr = token->kaddr; \ |
| 59 | p = kaddr + part_offset - token->offset; \ |
| 60 | res = get_unaligned_le##bits(p + off); \ |
| 61 | return res; \ |
| 62 | } \ |
David Sterba | 4dae666 | 2020-04-29 02:13:57 +0200 | [diff] [blame] | 63 | err = map_private_extent_buffer(token->eb, offset, size, \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 64 | &kaddr, &map_start, &map_len); \ |
| 65 | if (err) { \ |
| 66 | __le##bits leres; \ |
| 67 | \ |
David Sterba | 4dae666 | 2020-04-29 02:13:57 +0200 | [diff] [blame] | 68 | read_extent_buffer(token->eb, &leres, offset, size); \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 69 | return le##bits##_to_cpu(leres); \ |
| 70 | } \ |
| 71 | p = kaddr + part_offset - map_start; \ |
| 72 | res = get_unaligned_le##bits(p + off); \ |
David Sterba | 48bc395 | 2019-08-09 17:30:23 +0200 | [diff] [blame] | 73 | token->kaddr = kaddr; \ |
| 74 | token->offset = map_start; \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 75 | return res; \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 76 | } \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 77 | u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ |
| 78 | const void *ptr, unsigned long off) \ |
| 79 | { \ |
| 80 | unsigned long part_offset = (unsigned long)ptr; \ |
| 81 | unsigned long offset = part_offset + off; \ |
| 82 | void *p; \ |
| 83 | int err; \ |
| 84 | char *kaddr; \ |
| 85 | unsigned long map_start; \ |
| 86 | unsigned long map_len; \ |
| 87 | int size = sizeof(u##bits); \ |
| 88 | u##bits res; \ |
| 89 | \ |
| 90 | err = map_private_extent_buffer(eb, offset, size, \ |
| 91 | &kaddr, &map_start, &map_len); \ |
| 92 | if (err) { \ |
| 93 | __le##bits leres; \ |
| 94 | \ |
| 95 | read_extent_buffer(eb, &leres, offset, size); \ |
| 96 | return le##bits##_to_cpu(leres); \ |
| 97 | } \ |
| 98 | p = kaddr + part_offset - map_start; \ |
| 99 | res = get_unaligned_le##bits(p + off); \ |
| 100 | return res; \ |
| 101 | } \ |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 102 | void btrfs_set_token_##bits(struct btrfs_map_token *token, \ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 103 | const void *ptr, unsigned long off, \ |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 104 | u##bits val) \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 105 | { \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 106 | unsigned long part_offset = (unsigned long)ptr; \ |
| 107 | unsigned long offset = part_offset + off; \ |
| 108 | void *p; \ |
| 109 | int err; \ |
| 110 | char *kaddr; \ |
| 111 | unsigned long map_start; \ |
| 112 | unsigned long map_len; \ |
| 113 | int size = sizeof(u##bits); \ |
| 114 | \ |
David Sterba | 48bc395 | 2019-08-09 17:30:23 +0200 | [diff] [blame] | 115 | ASSERT(token); \ |
David Sterba | 870b388 | 2020-04-29 19:29:04 +0200 | [diff] [blame^] | 116 | ASSERT(token->kaddr); \ |
| 117 | if (token->offset <= offset && \ |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 118 | (token->offset + PAGE_SIZE >= offset + size)) { \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 119 | kaddr = token->kaddr; \ |
| 120 | p = kaddr + part_offset - token->offset; \ |
| 121 | put_unaligned_le##bits(val, p + off); \ |
| 122 | return; \ |
| 123 | } \ |
David Sterba | 4dae666 | 2020-04-29 02:13:57 +0200 | [diff] [blame] | 124 | err = map_private_extent_buffer(token->eb, offset, size, \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 125 | &kaddr, &map_start, &map_len); \ |
| 126 | if (err) { \ |
| 127 | __le##bits val2; \ |
| 128 | \ |
| 129 | val2 = cpu_to_le##bits(val); \ |
David Sterba | 4dae666 | 2020-04-29 02:13:57 +0200 | [diff] [blame] | 130 | write_extent_buffer(token->eb, &val2, offset, size); \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 131 | return; \ |
| 132 | } \ |
| 133 | p = kaddr + part_offset - map_start; \ |
| 134 | put_unaligned_le##bits(val, p + off); \ |
David Sterba | 48bc395 | 2019-08-09 17:30:23 +0200 | [diff] [blame] | 135 | token->kaddr = kaddr; \ |
| 136 | token->offset = map_start; \ |
David Sterba | cb49511 | 2019-08-09 17:12:38 +0200 | [diff] [blame] | 137 | } \ |
| 138 | void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \ |
| 139 | unsigned long off, u##bits val) \ |
| 140 | { \ |
| 141 | unsigned long part_offset = (unsigned long)ptr; \ |
| 142 | unsigned long offset = part_offset + off; \ |
| 143 | void *p; \ |
| 144 | int err; \ |
| 145 | char *kaddr; \ |
| 146 | unsigned long map_start; \ |
| 147 | unsigned long map_len; \ |
| 148 | int size = sizeof(u##bits); \ |
| 149 | \ |
| 150 | err = map_private_extent_buffer(eb, offset, size, \ |
| 151 | &kaddr, &map_start, &map_len); \ |
| 152 | if (err) { \ |
| 153 | __le##bits val2; \ |
| 154 | \ |
| 155 | val2 = cpu_to_le##bits(val); \ |
| 156 | write_extent_buffer(eb, &val2, offset, size); \ |
| 157 | return; \ |
| 158 | } \ |
| 159 | p = kaddr + part_offset - map_start; \ |
| 160 | put_unaligned_le##bits(val, p + off); \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 161 | } |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 162 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 163 | DEFINE_BTRFS_SETGET_BITS(8) |
| 164 | DEFINE_BTRFS_SETGET_BITS(16) |
| 165 | DEFINE_BTRFS_SETGET_BITS(32) |
| 166 | DEFINE_BTRFS_SETGET_BITS(64) |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 167 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 168 | void btrfs_node_key(const struct extent_buffer *eb, |
Chris Mason | e644d02 | 2007-11-06 15:09:29 -0500 | [diff] [blame] | 169 | struct btrfs_disk_key *disk_key, int nr) |
| 170 | { |
| 171 | unsigned long ptr = btrfs_node_key_ptr_offset(nr); |
Chris Mason | e644d02 | 2007-11-06 15:09:29 -0500 | [diff] [blame] | 172 | read_eb_member(eb, (struct btrfs_key_ptr *)ptr, |
| 173 | struct btrfs_key_ptr, key, disk_key); |
| 174 | } |