blob: cdedbe144d45ae9b55385e3868ca00b72dc140e7 [file] [log] [blame]
Chris Mason0f827312007-10-15 16:18:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
19#include <linux/highmem.h>
Chris Masond352ac62008-09-29 15:18:18 -040020
21/* this is some deeply nasty code. ctree.h has a different
22 * definition for this BTRFS_SETGET_FUNCS macro, behind a #ifndef
23 *
24 * The end result is that anyone who #includes ctree.h gets a
25 * declaration for the btrfs_set_foo functions and btrfs_foo functions
26 *
27 * This file declares the macros and then #includes ctree.h, which results
28 * in cpp creating the function here based on the template below.
29 *
30 * These setget functions do all the extent_buffer related mapping
31 * required to efficiently read and write specific fields in the extent
32 * buffers. Every pointer to metadata items in btrfs is really just
33 * an unsigned long offset into the extent buffer which has been
34 * cast to a specific type. This gives us all the gcc type checking.
35 *
36 * The extent buffer api is used to do all the kmapping and page
37 * spanning work required to get extent buffers in highmem and have
38 * a metadata blocksize different from the page size.
39 */
40
Chris Mason0f827312007-10-15 16:18:56 -040041#define BTRFS_SETGET_FUNCS(name, type, member, bits) \
42u##bits btrfs_##name(struct extent_buffer *eb, \
43 type *s) \
44{ \
David Millerdf68b8a2008-02-15 10:40:52 -050045 unsigned long part_offset = (unsigned long)s; \
46 unsigned long offset = part_offset + offsetof(type, member); \
47 type *p; \
Chris Mason0f827312007-10-15 16:18:56 -040048 /* ugly, but we want the fast path here */ \
49 if (eb->map_token && offset >= eb->map_start && \
50 offset + sizeof(((type *)0)->member) <= eb->map_start + \
51 eb->map_len) { \
David Millerdf68b8a2008-02-15 10:40:52 -050052 p = (type *)(eb->kaddr + part_offset - eb->map_start); \
53 return le##bits##_to_cpu(p->member); \
Chris Mason0f827312007-10-15 16:18:56 -040054 } \
55 { \
56 int err; \
57 char *map_token; \
58 char *kaddr; \
59 int unmap_on_exit = (eb->map_token == NULL); \
60 unsigned long map_start; \
61 unsigned long map_len; \
62 __le##bits res; \
63 err = map_extent_buffer(eb, offset, \
64 sizeof(((type *)0)->member), \
65 &map_token, &kaddr, \
66 &map_start, &map_len, KM_USER1); \
67 if (err) { \
68 read_eb_member(eb, s, type, member, &res); \
69 return le##bits##_to_cpu(res); \
70 } \
David Millerdf68b8a2008-02-15 10:40:52 -050071 p = (type *)(kaddr + part_offset - map_start); \
72 res = le##bits##_to_cpu(p->member); \
Chris Mason0f827312007-10-15 16:18:56 -040073 if (unmap_on_exit) \
74 unmap_extent_buffer(eb, map_token, KM_USER1); \
75 return res; \
76 } \
77} \
78void btrfs_set_##name(struct extent_buffer *eb, \
79 type *s, u##bits val) \
80{ \
David Millerdf68b8a2008-02-15 10:40:52 -050081 unsigned long part_offset = (unsigned long)s; \
82 unsigned long offset = part_offset + offsetof(type, member); \
83 type *p; \
Chris Mason0f827312007-10-15 16:18:56 -040084 /* ugly, but we want the fast path here */ \
85 if (eb->map_token && offset >= eb->map_start && \
86 offset + sizeof(((type *)0)->member) <= eb->map_start + \
87 eb->map_len) { \
David Millerdf68b8a2008-02-15 10:40:52 -050088 p = (type *)(eb->kaddr + part_offset - eb->map_start); \
89 p->member = cpu_to_le##bits(val); \
Chris Mason0f827312007-10-15 16:18:56 -040090 return; \
91 } \
92 { \
93 int err; \
94 char *map_token; \
95 char *kaddr; \
96 int unmap_on_exit = (eb->map_token == NULL); \
97 unsigned long map_start; \
98 unsigned long map_len; \
99 err = map_extent_buffer(eb, offset, \
100 sizeof(((type *)0)->member), \
101 &map_token, &kaddr, \
102 &map_start, &map_len, KM_USER1); \
103 if (err) { \
104 val = cpu_to_le##bits(val); \
105 write_eb_member(eb, s, type, member, &val); \
106 return; \
107 } \
David Millerdf68b8a2008-02-15 10:40:52 -0500108 p = (type *)(kaddr + part_offset - map_start); \
109 p->member = cpu_to_le##bits(val); \
Chris Mason0f827312007-10-15 16:18:56 -0400110 if (unmap_on_exit) \
111 unmap_extent_buffer(eb, map_token, KM_USER1); \
112 } \
113}
114
115#include "ctree.h"
116
Chris Masone644d022007-11-06 15:09:29 -0500117void btrfs_node_key(struct extent_buffer *eb,
118 struct btrfs_disk_key *disk_key, int nr)
119{
120 unsigned long ptr = btrfs_node_key_ptr_offset(nr);
121 if (eb->map_token && ptr >= eb->map_start &&
122 ptr + sizeof(*disk_key) <= eb->map_start + eb->map_len) {
123 memcpy(disk_key, eb->kaddr + ptr - eb->map_start,
124 sizeof(*disk_key));
125 return;
126 } else if (eb->map_token) {
127 unmap_extent_buffer(eb, eb->map_token, KM_USER1);
128 eb->map_token = NULL;
129 }
130 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
131 struct btrfs_key_ptr, key, disk_key);
132}