2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/highmem.h>
20 #include <asm/unaligned.h>
24 static inline u8
get_unaligned_le8(const void *p
)
29 static inline void put_unaligned_le8(u8 val
, void *p
)
35 * this is some deeply nasty code.
37 * The end result is that anyone who #includes ctree.h gets a
38 * declaration for the btrfs_set_foo functions and btrfs_foo functions,
39 * which are wappers of btrfs_set_token_#bits functions and
40 * btrfs_get_token_#bits functions, which are defined in this file.
42 * These setget functions do all the extent_buffer related mapping
43 * required to efficiently read and write specific fields in the extent
44 * buffers. Every pointer to metadata items in btrfs is really just
45 * an unsigned long offset into the extent buffer which has been
46 * cast to a specific type. This gives us all the gcc type checking.
48 * The extent buffer api is used to do the page spanning work required to
49 * have a metadata blocksize different from the page size.
52 #define DEFINE_BTRFS_SETGET_BITS(bits) \
53 u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
54 const void *ptr, unsigned long off, \
55 struct btrfs_map_token *token) \
57 unsigned long part_offset = (unsigned long)ptr; \
58 unsigned long offset = part_offset + off; \
62 unsigned long map_start; \
63 unsigned long map_len; \
64 int size = sizeof(u##bits); \
67 if (token && token->kaddr && token->offset <= offset && \
69 (token->offset + PAGE_CACHE_SIZE >= offset + size)) { \
70 kaddr = token->kaddr; \
71 p = kaddr + part_offset - token->offset; \
72 res = get_unaligned_le##bits(p + off); \
75 err = map_private_extent_buffer(eb, offset, size, \
76 &kaddr, &map_start, &map_len); \
80 read_extent_buffer(eb, &leres, offset, size); \
81 return le##bits##_to_cpu(leres); \
83 p = kaddr + part_offset - map_start; \
84 res = get_unaligned_le##bits(p + off); \
86 token->kaddr = kaddr; \
87 token->offset = map_start; \
92 void btrfs_set_token_##bits(struct extent_buffer *eb, \
93 const void *ptr, unsigned long off, \
95 struct btrfs_map_token *token) \
97 unsigned long part_offset = (unsigned long)ptr; \
98 unsigned long offset = part_offset + off; \
102 unsigned long map_start; \
103 unsigned long map_len; \
104 int size = sizeof(u##bits); \
106 if (token && token->kaddr && token->offset <= offset && \
108 (token->offset + PAGE_CACHE_SIZE >= offset + size)) { \
109 kaddr = token->kaddr; \
110 p = kaddr + part_offset - token->offset; \
111 put_unaligned_le##bits(val, p + off); \
114 err = map_private_extent_buffer(eb, offset, size, \
115 &kaddr, &map_start, &map_len); \
119 val2 = cpu_to_le##bits(val); \
120 write_extent_buffer(eb, &val2, offset, size); \
123 p = kaddr + part_offset - map_start; \
124 put_unaligned_le##bits(val, p + off); \
126 token->kaddr = kaddr; \
127 token->offset = map_start; \
132 DEFINE_BTRFS_SETGET_BITS(8)
133 DEFINE_BTRFS_SETGET_BITS(16)
134 DEFINE_BTRFS_SETGET_BITS(32)
135 DEFINE_BTRFS_SETGET_BITS(64)
137 void btrfs_node_key(const struct extent_buffer
*eb
,
138 struct btrfs_disk_key
*disk_key
, int nr
)
140 unsigned long ptr
= btrfs_node_key_ptr_offset(nr
);
141 read_eb_member(eb
, (struct btrfs_key_ptr
*)ptr
,
142 struct btrfs_key_ptr
, key
, disk_key
);