bc1f6ad18442bc728a10e9919b91162af1b60b4d
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / btrfs / struct-funcs.c
1 /*
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>
20
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  * The macro starts with a simple function prototype declaration so that
41  * sparse won't complain about it being static.
42  */
43
44 #define BTRFS_SETGET_FUNCS(name, type, member, bits)                    \
45 u##bits btrfs_##name(struct extent_buffer *eb, type *s);                \
46 void btrfs_set_##name(struct extent_buffer *eb, type *s, u##bits val);  \
47 u##bits btrfs_##name(struct extent_buffer *eb,                          \
48                                    type *s)                             \
49 {                                                                       \
50         unsigned long part_offset = (unsigned long)s;                   \
51         unsigned long offset = part_offset + offsetof(type, member);    \
52         type *p;                                                        \
53         int err;                                                \
54         char *kaddr;                                            \
55         unsigned long map_start;                                \
56         unsigned long map_len;                                  \
57         u##bits res;                                            \
58         err = map_private_extent_buffer(eb, offset,             \
59                         sizeof(((type *)0)->member),            \
60                         &kaddr, &map_start, &map_len);          \
61         if (err) {                                              \
62                 __le##bits leres;                               \
63                 read_eb_member(eb, s, type, member, &leres);    \
64                 return le##bits##_to_cpu(leres);                \
65         }                                                       \
66         p = (type *)(kaddr + part_offset - map_start);          \
67         res = le##bits##_to_cpu(p->member);                     \
68         return res;                                             \
69 }                                                                       \
70 void btrfs_set_##name(struct extent_buffer *eb,                         \
71                                     type *s, u##bits val)               \
72 {                                                                       \
73         unsigned long part_offset = (unsigned long)s;                   \
74         unsigned long offset = part_offset + offsetof(type, member);    \
75         type *p;                                                        \
76         int err;                                                \
77         char *kaddr;                                            \
78         unsigned long map_start;                                \
79         unsigned long map_len;                                  \
80         err = map_private_extent_buffer(eb, offset,             \
81                         sizeof(((type *)0)->member),            \
82                         &kaddr, &map_start, &map_len);          \
83         if (err) {                                              \
84                 __le##bits val2;                                \
85                 val2 = cpu_to_le##bits(val);                    \
86                 write_eb_member(eb, s, type, member, &val2);    \
87                 return;                                         \
88         }                                                       \
89         p = (type *)(kaddr + part_offset - map_start);          \
90         p->member = cpu_to_le##bits(val);                       \
91 }
92
93 #include "ctree.h"
94
95 void btrfs_node_key(struct extent_buffer *eb,
96                     struct btrfs_disk_key *disk_key, int nr)
97 {
98         unsigned long ptr = btrfs_node_key_ptr_offset(nr);
99         read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
100                        struct btrfs_key_ptr, key, disk_key);
101 }