blob: 4c13b737f568db9c2917ba4b610edd188e0528cb [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
20/*
21 * this is some deeply nasty code.
Chris Masond352ac62008-09-29 15:18:18 -040022 *
23 * The end result is that anyone who #includes ctree.h gets a
Li Zefan18077bb2012-07-09 20:22:35 -060024 * declaration for the btrfs_set_foo functions and btrfs_foo functions,
Nicholas D Steeves01327612016-05-19 21:18:45 -040025 * which are wrappers of btrfs_set_token_#bits functions and
Li Zefan18077bb2012-07-09 20:22:35 -060026 * btrfs_get_token_#bits functions, which are defined in this file.
Chris Masond352ac62008-09-29 15:18:18 -040027 *
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 Zefan18077bb2012-07-09 20:22:35 -060034 * The extent buffer api is used to do the page spanning work required to
35 * have a metadata blocksize different from the page size.
Chris Masond352ac62008-09-29 15:18:18 -040036 */
37
Li Zefan18077bb2012-07-09 20:22:35 -060038#define DEFINE_BTRFS_SETGET_BITS(bits) \
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -060039u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
40 const void *ptr, unsigned long off, \
Li Zefan18077bb2012-07-09 20:22:35 -060041 struct btrfs_map_token *token) \
Chris Mason0f827312007-10-15 16:18:56 -040042{ \
Li Zefan18077bb2012-07-09 20:22:35 -060043 unsigned long part_offset = (unsigned long)ptr; \
44 unsigned long offset = part_offset + off; \
45 void *p; \
46 int err; \
47 char *kaddr; \
48 unsigned long map_start; \
49 unsigned long map_len; \
50 int size = sizeof(u##bits); \
51 u##bits res; \
52 \
53 if (token && token->kaddr && token->offset <= offset && \
54 token->eb == eb && \
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030055 (token->offset + PAGE_SIZE >= offset + size)) { \
Li Zefan18077bb2012-07-09 20:22:35 -060056 kaddr = token->kaddr; \
57 p = kaddr + part_offset - token->offset; \
58 res = get_unaligned_le##bits(p + off); \
59 return res; \
60 } \
61 err = map_private_extent_buffer(eb, offset, size, \
62 &kaddr, &map_start, &map_len); \
63 if (err) { \
64 __le##bits leres; \
65 \
66 read_extent_buffer(eb, &leres, offset, size); \
67 return le##bits##_to_cpu(leres); \
68 } \
69 p = kaddr + part_offset - map_start; \
70 res = get_unaligned_le##bits(p + off); \
71 if (token) { \
72 token->kaddr = kaddr; \
73 token->offset = map_start; \
74 token->eb = eb; \
75 } \
76 return res; \
Chris Mason0f827312007-10-15 16:18:56 -040077} \
Li Zefan18077bb2012-07-09 20:22:35 -060078void btrfs_set_token_##bits(struct extent_buffer *eb, \
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -060079 const void *ptr, unsigned long off, \
80 u##bits val, \
Li Zefan18077bb2012-07-09 20:22:35 -060081 struct btrfs_map_token *token) \
Chris Mason0f827312007-10-15 16:18:56 -040082{ \
Li Zefan18077bb2012-07-09 20:22:35 -060083 unsigned long part_offset = (unsigned long)ptr; \
84 unsigned long offset = part_offset + off; \
85 void *p; \
86 int err; \
87 char *kaddr; \
88 unsigned long map_start; \
89 unsigned long map_len; \
90 int size = sizeof(u##bits); \
91 \
92 if (token && token->kaddr && token->offset <= offset && \
93 token->eb == eb && \
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030094 (token->offset + PAGE_SIZE >= offset + size)) { \
Li Zefan18077bb2012-07-09 20:22:35 -060095 kaddr = token->kaddr; \
96 p = kaddr + part_offset - token->offset; \
97 put_unaligned_le##bits(val, p + off); \
98 return; \
99 } \
100 err = map_private_extent_buffer(eb, offset, size, \
101 &kaddr, &map_start, &map_len); \
102 if (err) { \
103 __le##bits val2; \
104 \
105 val2 = cpu_to_le##bits(val); \
106 write_extent_buffer(eb, &val2, offset, size); \
107 return; \
108 } \
109 p = kaddr + part_offset - map_start; \
110 put_unaligned_le##bits(val, p + off); \
111 if (token) { \
112 token->kaddr = kaddr; \
113 token->offset = map_start; \
114 token->eb = eb; \
115 } \
116}
Chris Mason0f827312007-10-15 16:18:56 -0400117
Li Zefan18077bb2012-07-09 20:22:35 -0600118DEFINE_BTRFS_SETGET_BITS(8)
119DEFINE_BTRFS_SETGET_BITS(16)
120DEFINE_BTRFS_SETGET_BITS(32)
121DEFINE_BTRFS_SETGET_BITS(64)
Chris Mason0f827312007-10-15 16:18:56 -0400122
Jeff Mahoney1cbb1f42017-06-28 21:56:53 -0600123void btrfs_node_key(const struct extent_buffer *eb,
Chris Masone644d022007-11-06 15:09:29 -0500124 struct btrfs_disk_key *disk_key, int nr)
125{
126 unsigned long ptr = btrfs_node_key_ptr_offset(nr);
Chris Masone644d022007-11-06 15:09:29 -0500127 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
128 struct btrfs_key_ptr, key, disk_key);
129}