Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 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 | #define BTRFS_SETGET_FUNCS(name, type, member, bits) \ |
| 21 | u##bits btrfs_##name(struct extent_buffer *eb, \ |
| 22 | type *s) \ |
| 23 | { \ |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 24 | unsigned long part_offset = (unsigned long)s; \ |
| 25 | unsigned long offset = part_offset + offsetof(type, member); \ |
| 26 | type *p; \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 27 | /* ugly, but we want the fast path here */ \ |
| 28 | if (eb->map_token && offset >= eb->map_start && \ |
| 29 | offset + sizeof(((type *)0)->member) <= eb->map_start + \ |
| 30 | eb->map_len) { \ |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 31 | p = (type *)(eb->kaddr + part_offset - eb->map_start); \ |
| 32 | return le##bits##_to_cpu(p->member); \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 33 | } \ |
| 34 | { \ |
| 35 | int err; \ |
| 36 | char *map_token; \ |
| 37 | char *kaddr; \ |
| 38 | int unmap_on_exit = (eb->map_token == NULL); \ |
| 39 | unsigned long map_start; \ |
| 40 | unsigned long map_len; \ |
| 41 | __le##bits res; \ |
| 42 | err = map_extent_buffer(eb, offset, \ |
| 43 | sizeof(((type *)0)->member), \ |
| 44 | &map_token, &kaddr, \ |
| 45 | &map_start, &map_len, KM_USER1); \ |
| 46 | if (err) { \ |
| 47 | read_eb_member(eb, s, type, member, &res); \ |
| 48 | return le##bits##_to_cpu(res); \ |
| 49 | } \ |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 50 | p = (type *)(kaddr + part_offset - map_start); \ |
| 51 | res = le##bits##_to_cpu(p->member); \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 52 | if (unmap_on_exit) \ |
| 53 | unmap_extent_buffer(eb, map_token, KM_USER1); \ |
| 54 | return res; \ |
| 55 | } \ |
| 56 | } \ |
| 57 | void btrfs_set_##name(struct extent_buffer *eb, \ |
| 58 | type *s, u##bits val) \ |
| 59 | { \ |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 60 | unsigned long part_offset = (unsigned long)s; \ |
| 61 | unsigned long offset = part_offset + offsetof(type, member); \ |
| 62 | type *p; \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 63 | /* ugly, but we want the fast path here */ \ |
| 64 | if (eb->map_token && offset >= eb->map_start && \ |
| 65 | offset + sizeof(((type *)0)->member) <= eb->map_start + \ |
| 66 | eb->map_len) { \ |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 67 | p = (type *)(eb->kaddr + part_offset - eb->map_start); \ |
| 68 | p->member = cpu_to_le##bits(val); \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 69 | return; \ |
| 70 | } \ |
| 71 | { \ |
| 72 | int err; \ |
| 73 | char *map_token; \ |
| 74 | char *kaddr; \ |
| 75 | int unmap_on_exit = (eb->map_token == NULL); \ |
| 76 | unsigned long map_start; \ |
| 77 | unsigned long map_len; \ |
| 78 | err = map_extent_buffer(eb, offset, \ |
| 79 | sizeof(((type *)0)->member), \ |
| 80 | &map_token, &kaddr, \ |
| 81 | &map_start, &map_len, KM_USER1); \ |
| 82 | if (err) { \ |
| 83 | val = cpu_to_le##bits(val); \ |
| 84 | write_eb_member(eb, s, type, member, &val); \ |
| 85 | return; \ |
| 86 | } \ |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 87 | p = (type *)(kaddr + part_offset - map_start); \ |
| 88 | p->member = cpu_to_le##bits(val); \ |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 89 | if (unmap_on_exit) \ |
| 90 | unmap_extent_buffer(eb, map_token, KM_USER1); \ |
| 91 | } \ |
| 92 | } |
| 93 | |
| 94 | #include "ctree.h" |
| 95 | |
Chris Mason | e644d02 | 2007-11-06 15:09:29 -0500 | [diff] [blame] | 96 | void btrfs_node_key(struct extent_buffer *eb, |
| 97 | struct btrfs_disk_key *disk_key, int nr) |
| 98 | { |
| 99 | unsigned long ptr = btrfs_node_key_ptr_offset(nr); |
| 100 | if (eb->map_token && ptr >= eb->map_start && |
| 101 | ptr + sizeof(*disk_key) <= eb->map_start + eb->map_len) { |
| 102 | memcpy(disk_key, eb->kaddr + ptr - eb->map_start, |
| 103 | sizeof(*disk_key)); |
| 104 | return; |
| 105 | } else if (eb->map_token) { |
| 106 | unmap_extent_buffer(eb, eb->map_token, KM_USER1); |
| 107 | eb->map_token = NULL; |
| 108 | } |
| 109 | read_eb_member(eb, (struct btrfs_key_ptr *)ptr, |
| 110 | struct btrfs_key_ptr, key, disk_key); |
| 111 | } |