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 | |
| 6 | #include <linux/highmem.h> |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 7 | #include <asm/unaligned.h> |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 9 | #include "ctree.h" |
| 10 | |
| 11 | static inline u8 get_unaligned_le8(const void *p) |
| 12 | { |
| 13 | return *(u8 *)p; |
| 14 | } |
| 15 | |
| 16 | static inline void put_unaligned_le8(u8 val, void *p) |
| 17 | { |
| 18 | *(u8 *)p = val; |
| 19 | } |
| 20 | |
| 21 | /* |
| 22 | * this is some deeply nasty code. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 23 | * |
| 24 | * The end result is that anyone who #includes ctree.h gets a |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 25 | * declaration for the btrfs_set_foo functions and btrfs_foo functions, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 26 | * which are wrappers of btrfs_set_token_#bits functions and |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 27 | * btrfs_get_token_#bits functions, which are defined in this file. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 28 | * |
| 29 | * These setget functions do all the extent_buffer related mapping |
| 30 | * required to efficiently read and write specific fields in the extent |
| 31 | * buffers. Every pointer to metadata items in btrfs is really just |
| 32 | * an unsigned long offset into the extent buffer which has been |
| 33 | * cast to a specific type. This gives us all the gcc type checking. |
| 34 | * |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 35 | * The extent buffer api is used to do the page spanning work required to |
| 36 | * have a metadata blocksize different from the page size. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 37 | */ |
| 38 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 39 | #define DEFINE_BTRFS_SETGET_BITS(bits) \ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 40 | u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \ |
| 41 | const void *ptr, unsigned long off, \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 42 | struct btrfs_map_token *token) \ |
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 | \ |
| 54 | if (token && token->kaddr && token->offset <= offset && \ |
| 55 | token->eb == eb && \ |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 56 | (token->offset + PAGE_SIZE >= offset + size)) { \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 57 | kaddr = token->kaddr; \ |
| 58 | p = kaddr + part_offset - token->offset; \ |
| 59 | res = get_unaligned_le##bits(p + off); \ |
| 60 | return res; \ |
| 61 | } \ |
| 62 | err = map_private_extent_buffer(eb, offset, size, \ |
| 63 | &kaddr, &map_start, &map_len); \ |
| 64 | if (err) { \ |
| 65 | __le##bits leres; \ |
| 66 | \ |
| 67 | read_extent_buffer(eb, &leres, offset, size); \ |
| 68 | return le##bits##_to_cpu(leres); \ |
| 69 | } \ |
| 70 | p = kaddr + part_offset - map_start; \ |
| 71 | res = get_unaligned_le##bits(p + off); \ |
| 72 | if (token) { \ |
| 73 | token->kaddr = kaddr; \ |
| 74 | token->offset = map_start; \ |
| 75 | token->eb = eb; \ |
| 76 | } \ |
| 77 | return res; \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 78 | } \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 79 | void btrfs_set_token_##bits(struct extent_buffer *eb, \ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 80 | const void *ptr, unsigned long off, \ |
| 81 | u##bits val, \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 82 | struct btrfs_map_token *token) \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 83 | { \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 84 | unsigned long part_offset = (unsigned long)ptr; \ |
| 85 | unsigned long offset = part_offset + off; \ |
| 86 | void *p; \ |
| 87 | int err; \ |
| 88 | char *kaddr; \ |
| 89 | unsigned long map_start; \ |
| 90 | unsigned long map_len; \ |
| 91 | int size = sizeof(u##bits); \ |
| 92 | \ |
| 93 | if (token && token->kaddr && token->offset <= offset && \ |
| 94 | token->eb == eb && \ |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 95 | (token->offset + PAGE_SIZE >= offset + size)) { \ |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 96 | kaddr = token->kaddr; \ |
| 97 | p = kaddr + part_offset - token->offset; \ |
| 98 | put_unaligned_le##bits(val, p + off); \ |
| 99 | return; \ |
| 100 | } \ |
| 101 | err = map_private_extent_buffer(eb, offset, size, \ |
| 102 | &kaddr, &map_start, &map_len); \ |
| 103 | if (err) { \ |
| 104 | __le##bits val2; \ |
| 105 | \ |
| 106 | val2 = cpu_to_le##bits(val); \ |
| 107 | write_extent_buffer(eb, &val2, offset, size); \ |
| 108 | return; \ |
| 109 | } \ |
| 110 | p = kaddr + part_offset - map_start; \ |
| 111 | put_unaligned_le##bits(val, p + off); \ |
| 112 | if (token) { \ |
| 113 | token->kaddr = kaddr; \ |
| 114 | token->offset = map_start; \ |
| 115 | token->eb = eb; \ |
| 116 | } \ |
| 117 | } |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 118 | |
Li Zefan | 18077bb | 2012-07-09 20:22:35 -0600 | [diff] [blame] | 119 | DEFINE_BTRFS_SETGET_BITS(8) |
| 120 | DEFINE_BTRFS_SETGET_BITS(16) |
| 121 | DEFINE_BTRFS_SETGET_BITS(32) |
| 122 | DEFINE_BTRFS_SETGET_BITS(64) |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 123 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 124 | void btrfs_node_key(const struct extent_buffer *eb, |
Chris Mason | e644d02 | 2007-11-06 15:09:29 -0500 | [diff] [blame] | 125 | struct btrfs_disk_key *disk_key, int nr) |
| 126 | { |
| 127 | unsigned long ptr = btrfs_node_key_ptr_offset(nr); |
Chris Mason | e644d02 | 2007-11-06 15:09:29 -0500 | [diff] [blame] | 128 | read_eb_member(eb, (struct btrfs_key_ptr *)ptr, |
| 129 | struct btrfs_key_ptr, key, disk_key); |
| 130 | } |