aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/ctree.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-03-12 20:12:07 -0400
committerDavid Woodhouse <dwmw2@hera.kernel.org>2007-03-12 20:12:07 -0400
commit0783fcfc4dc19b8bb99bd51b7afa669ba4cbd377 (patch)
tree0d7d68c33e8ff954e7614395f05d01d47d107f05 /fs/btrfs/ctree.c
parente2fa7227cdf132d72e7410dd0679dc573a1c2618 (diff)
Btrfs: struct item endian fixes
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/ctree.c')
-rw-r--r--fs/btrfs/ctree.c129
1 files changed, 71 insertions, 58 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 489019ac04b8..105556470055 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -76,7 +76,7 @@ static inline unsigned int leaf_data_end(struct leaf *leaf)
76 u32 nr = btrfs_header_nritems(&leaf->header); 76 u32 nr = btrfs_header_nritems(&leaf->header);
77 if (nr == 0) 77 if (nr == 0)
78 return sizeof(leaf->data); 78 return sizeof(leaf->data);
79 return leaf->items[nr-1].offset; 79 return btrfs_item_offset(leaf->items + nr - 1);
80} 80}
81 81
82/* 82/*
@@ -174,11 +174,12 @@ int check_leaf(struct ctree_path *path, int level)
174 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key); 174 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
175 BUG_ON(comp_keys(&leaf->items[i].key, 175 BUG_ON(comp_keys(&leaf->items[i].key,
176 &cpukey) >= 0); 176 &cpukey) >= 0);
177 BUG_ON(leaf->items[i].offset != leaf->items[i + 1].offset + 177 BUG_ON(btrfs_item_offset(leaf->items + i) !=
178 leaf->items[i + 1].size); 178 btrfs_item_end(leaf->items + i + 1));
179 if (i == 0) { 179 if (i == 0) {
180 BUG_ON(leaf->items[i].offset + leaf->items[i].size != 180 BUG_ON(btrfs_item_offset(leaf->items + i) +
181 LEAF_DATA_SIZE); 181 btrfs_item_size(leaf->items + i) !=
182 LEAF_DATA_SIZE);
182 } 183 }
183 } 184 }
184 return 0; 185 return 0;
@@ -235,7 +236,8 @@ int bin_search(struct node *c, struct btrfs_key *key, int *slot)
235{ 236{
236 if (btrfs_is_leaf(c)) { 237 if (btrfs_is_leaf(c)) {
237 struct leaf *l = (struct leaf *)c; 238 struct leaf *l = (struct leaf *)c;
238 return generic_bin_search((void *)l->items, sizeof(struct item), 239 return generic_bin_search((void *)l->items,
240 sizeof(struct btrfs_item),
239 key, btrfs_header_nritems(&c->header), 241 key, btrfs_header_nritems(&c->header),
240 slot); 242 slot);
241 } else { 243 } else {
@@ -485,7 +487,7 @@ again:
485 struct leaf *l = (struct leaf *)c; 487 struct leaf *l = (struct leaf *)c;
486 p->slots[level] = slot; 488 p->slots[level] = slot;
487 if (ins_len > 0 && leaf_free_space(l) < 489 if (ins_len > 0 && leaf_free_space(l) <
488 sizeof(struct item) + ins_len) { 490 sizeof(struct btrfs_item) + ins_len) {
489 int sret = split_leaf(root, p, ins_len); 491 int sret = split_leaf(root, p, ins_len);
490 BUG_ON(sret > 0); 492 BUG_ON(sret > 0);
491 if (sret) 493 if (sret)
@@ -780,9 +782,9 @@ static int leaf_space_used(struct leaf *l, int start, int nr)
780 782
781 if (!nr) 783 if (!nr)
782 return 0; 784 return 0;
783 data_len = l->items[start].offset + l->items[start].size; 785 data_len = btrfs_item_end(l->items + start);
784 data_len = data_len - l->items[end].offset; 786 data_len = data_len - btrfs_item_offset(l->items + end);
785 data_len += sizeof(struct item) * nr; 787 data_len += sizeof(struct btrfs_item) * nr;
786 return data_len; 788 return data_len;
787} 789}
788 790
@@ -806,7 +808,7 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
806 int free_space; 808 int free_space;
807 int push_space = 0; 809 int push_space = 0;
808 int push_items = 0; 810 int push_items = 0;
809 struct item *item; 811 struct btrfs_item *item;
810 u32 left_nritems; 812 u32 left_nritems;
811 u32 right_nritems; 813 u32 right_nritems;
812 814
@@ -821,7 +823,7 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
821 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]); 823 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]);
822 right = &right_buf->leaf; 824 right = &right_buf->leaf;
823 free_space = leaf_free_space(right); 825 free_space = leaf_free_space(right);
824 if (free_space < data_size + sizeof(struct item)) { 826 if (free_space < data_size + sizeof(struct btrfs_item)) {
825 tree_block_release(root, right_buf); 827 tree_block_release(root, right_buf);
826 return 1; 828 return 1;
827 } 829 }
@@ -829,7 +831,7 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
829 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf); 831 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
830 right = &right_buf->leaf; 832 right = &right_buf->leaf;
831 free_space = leaf_free_space(right); 833 free_space = leaf_free_space(right);
832 if (free_space < data_size + sizeof(struct item)) { 834 if (free_space < data_size + sizeof(struct btrfs_item)) {
833 tree_block_release(root, right_buf); 835 tree_block_release(root, right_buf);
834 return 1; 836 return 1;
835 } 837 }
@@ -839,10 +841,11 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
839 item = left->items + i; 841 item = left->items + i;
840 if (path->slots[0] == i) 842 if (path->slots[0] == i)
841 push_space += data_size + sizeof(*item); 843 push_space += data_size + sizeof(*item);
842 if (item->size + sizeof(*item) + push_space > free_space) 844 if (btrfs_item_size(item) + sizeof(*item) + push_space >
845 free_space)
843 break; 846 break;
844 push_items++; 847 push_items++;
845 push_space += item->size + sizeof(*item); 848 push_space += btrfs_item_size(item) + sizeof(*item);
846 } 849 }
847 if (push_items == 0) { 850 if (push_items == 0) {
848 tree_block_release(root, right_buf); 851 tree_block_release(root, right_buf);
@@ -850,8 +853,7 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
850 } 853 }
851 right_nritems = btrfs_header_nritems(&right->header); 854 right_nritems = btrfs_header_nritems(&right->header);
852 /* push left to right */ 855 /* push left to right */
853 push_space = left->items[left_nritems - push_items].offset + 856 push_space = btrfs_item_end(left->items + left_nritems - push_items);
854 left->items[left_nritems - push_items].size;
855 push_space -= leaf_data_end(left); 857 push_space -= leaf_data_end(left);
856 /* make room in the right data area */ 858 /* make room in the right data area */
857 memmove(right->data + leaf_data_end(right) - push_space, 859 memmove(right->data + leaf_data_end(right) - push_space,
@@ -862,18 +864,19 @@ static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
862 left->data + leaf_data_end(left), 864 left->data + leaf_data_end(left),
863 push_space); 865 push_space);
864 memmove(right->items + push_items, right->items, 866 memmove(right->items + push_items, right->items,
865 right_nritems * sizeof(struct item)); 867 right_nritems * sizeof(struct btrfs_item));
866 /* copy the items from left to right */ 868 /* copy the items from left to right */
867 memcpy(right->items, left->items + left_nritems - push_items, 869 memcpy(right->items, left->items + left_nritems - push_items,
868 push_items * sizeof(struct item)); 870 push_items * sizeof(struct btrfs_item));
869 871
870 /* update the item pointers */ 872 /* update the item pointers */
871 right_nritems += push_items; 873 right_nritems += push_items;
872 btrfs_set_header_nritems(&right->header, right_nritems); 874 btrfs_set_header_nritems(&right->header, right_nritems);
873 push_space = LEAF_DATA_SIZE; 875 push_space = LEAF_DATA_SIZE;
874 for (i = 0; i < right_nritems; i++) { 876 for (i = 0; i < right_nritems; i++) {
875 right->items[i].offset = push_space - right->items[i].size; 877 btrfs_set_item_offset(right->items + i, push_space -
876 push_space = right->items[i].offset; 878 btrfs_item_size(right->items + i));
879 push_space = btrfs_item_offset(right->items + i);
877 } 880 }
878 left_nritems -= push_items; 881 left_nritems -= push_items;
879 btrfs_set_header_nritems(&left->header, left_nritems); 882 btrfs_set_header_nritems(&left->header, left_nritems);
@@ -911,7 +914,7 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
911 int free_space; 914 int free_space;
912 int push_space = 0; 915 int push_space = 0;
913 int push_items = 0; 916 int push_items = 0;
914 struct item *item; 917 struct btrfs_item *item;
915 u32 old_left_nritems; 918 u32 old_left_nritems;
916 int ret = 0; 919 int ret = 0;
917 int wret; 920 int wret;
@@ -926,7 +929,7 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
926 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]); 929 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
927 left = &t->leaf; 930 left = &t->leaf;
928 free_space = leaf_free_space(left); 931 free_space = leaf_free_space(left);
929 if (free_space < data_size + sizeof(struct item)) { 932 if (free_space < data_size + sizeof(struct btrfs_item)) {
930 tree_block_release(root, t); 933 tree_block_release(root, t);
931 return 1; 934 return 1;
932 } 935 }
@@ -935,7 +938,7 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
935 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t); 938 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
936 left = &t->leaf; 939 left = &t->leaf;
937 free_space = leaf_free_space(left); 940 free_space = leaf_free_space(left);
938 if (free_space < data_size + sizeof(struct item)) { 941 if (free_space < data_size + sizeof(struct btrfs_item)) {
939 tree_block_release(root, t); 942 tree_block_release(root, t);
940 return 1; 943 return 1;
941 } 944 }
@@ -944,10 +947,11 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
944 item = right->items + i; 947 item = right->items + i;
945 if (path->slots[0] == i) 948 if (path->slots[0] == i)
946 push_space += data_size + sizeof(*item); 949 push_space += data_size + sizeof(*item);
947 if (item->size + sizeof(*item) + push_space > free_space) 950 if (btrfs_item_size(item) + sizeof(*item) + push_space >
951 free_space)
948 break; 952 break;
949 push_items++; 953 push_items++;
950 push_space += item->size + sizeof(*item); 954 push_space += btrfs_item_size(item) + sizeof(*item);
951 } 955 }
952 if (push_items == 0) { 956 if (push_items == 0) {
953 tree_block_release(root, t); 957 tree_block_release(root, t);
@@ -955,35 +959,40 @@ static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
955 } 959 }
956 /* push data from right to left */ 960 /* push data from right to left */
957 memcpy(left->items + btrfs_header_nritems(&left->header), 961 memcpy(left->items + btrfs_header_nritems(&left->header),
958 right->items, push_items * sizeof(struct item)); 962 right->items, push_items * sizeof(struct btrfs_item));
959 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset; 963 push_space = LEAF_DATA_SIZE -
964 btrfs_item_offset(right->items + push_items -1);
960 memcpy(left->data + leaf_data_end(left) - push_space, 965 memcpy(left->data + leaf_data_end(left) - push_space,
961 right->data + right->items[push_items - 1].offset, 966 right->data + btrfs_item_offset(right->items + push_items - 1),
962 push_space); 967 push_space);
963 old_left_nritems = btrfs_header_nritems(&left->header); 968 old_left_nritems = btrfs_header_nritems(&left->header);
964 BUG_ON(old_left_nritems < 0); 969 BUG_ON(old_left_nritems < 0);
965 970
966 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) { 971 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
967 left->items[i].offset -= LEAF_DATA_SIZE - 972 u16 ioff = btrfs_item_offset(left->items + i);
968 left->items[old_left_nritems -1].offset; 973 btrfs_set_item_offset(left->items + i, ioff - (LEAF_DATA_SIZE -
974 btrfs_item_offset(left->items +
975 old_left_nritems - 1)));
969 } 976 }
970 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); 977 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
971 978
972 /* fixup right node */ 979 /* fixup right node */
973 push_space = right->items[push_items-1].offset - leaf_data_end(right); 980 push_space = btrfs_item_offset(right->items + push_items - 1) -
981 leaf_data_end(right);
974 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data + 982 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
975 leaf_data_end(right), push_space); 983 leaf_data_end(right), push_space);
976 memmove(right->items, right->items + push_items, 984 memmove(right->items, right->items + push_items,
977 (btrfs_header_nritems(&right->header) - push_items) * 985 (btrfs_header_nritems(&right->header) - push_items) *
978 sizeof(struct item)); 986 sizeof(struct btrfs_item));
979 btrfs_set_header_nritems(&right->header, 987 btrfs_set_header_nritems(&right->header,
980 btrfs_header_nritems(&right->header) - 988 btrfs_header_nritems(&right->header) -
981 push_items); 989 push_items);
982 push_space = LEAF_DATA_SIZE; 990 push_space = LEAF_DATA_SIZE;
983 991
984 for (i = 0; i < btrfs_header_nritems(&right->header); i++) { 992 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
985 right->items[i].offset = push_space - right->items[i].size; 993 btrfs_set_item_offset(right->items + i, push_space -
986 push_space = right->items[i].offset; 994 btrfs_item_size(right->items + i));
995 push_space = btrfs_item_offset(right->items + i);
987 } 996 }
988 997
989 BUG_ON(list_empty(&t->dirty)); 998 BUG_ON(list_empty(&t->dirty));
@@ -1023,7 +1032,7 @@ static int split_leaf(struct ctree_root *root, struct ctree_path *path,
1023 int slot; 1032 int slot;
1024 struct leaf *right; 1033 struct leaf *right;
1025 struct tree_buffer *right_buffer; 1034 struct tree_buffer *right_buffer;
1026 int space_needed = data_size + sizeof(struct item); 1035 int space_needed = data_size + sizeof(struct btrfs_item);
1027 int data_copy_size; 1036 int data_copy_size;
1028 int rt_data_off; 1037 int rt_data_off;
1029 int i; 1038 int i;
@@ -1034,7 +1043,7 @@ static int split_leaf(struct ctree_root *root, struct ctree_path *path,
1034 l = &l_buf->leaf; 1043 l = &l_buf->leaf;
1035 1044
1036 /* did the pushes work? */ 1045 /* did the pushes work? */
1037 if (leaf_free_space(l) >= sizeof(struct item) + data_size) 1046 if (leaf_free_space(l) >= sizeof(struct btrfs_item) + data_size)
1038 return 0; 1047 return 0;
1039 1048
1040 if (!path->nodes[1]) { 1049 if (!path->nodes[1]) {
@@ -1066,17 +1075,17 @@ static int split_leaf(struct ctree_root *root, struct ctree_path *path,
1066 btrfs_set_header_level(&right->header, 0); 1075 btrfs_set_header_level(&right->header, 0);
1067 btrfs_set_header_parentid(&right->header, 1076 btrfs_set_header_parentid(&right->header,
1068 btrfs_header_parentid(&root->node->node.header)); 1077 btrfs_header_parentid(&root->node->node.header));
1069 data_copy_size = l->items[mid].offset + l->items[mid].size - 1078 data_copy_size = btrfs_item_end(l->items + mid) - leaf_data_end(l);
1070 leaf_data_end(l);
1071 memcpy(right->items, l->items + mid, 1079 memcpy(right->items, l->items + mid,
1072 (nritems - mid) * sizeof(struct item)); 1080 (nritems - mid) * sizeof(struct btrfs_item));
1073 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size, 1081 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
1074 l->data + leaf_data_end(l), data_copy_size); 1082 l->data + leaf_data_end(l), data_copy_size);
1075 rt_data_off = LEAF_DATA_SIZE - 1083 rt_data_off = LEAF_DATA_SIZE - btrfs_item_end(l->items + mid);
1076 (l->items[mid].offset + l->items[mid].size);
1077 1084
1078 for (i = 0; i < btrfs_header_nritems(&right->header); i++) 1085 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1079 right->items[i].offset += rt_data_off; 1086 u16 ioff = btrfs_item_offset(right->items + i);
1087 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1088 }
1080 1089
1081 btrfs_set_header_nritems(&l->header, mid); 1090 btrfs_set_header_nritems(&l->header, mid);
1082 ret = 0; 1091 ret = 0;
@@ -1136,26 +1145,28 @@ int insert_item(struct ctree_root *root, struct btrfs_key *cpu_key,
1136 nritems = btrfs_header_nritems(&leaf->header); 1145 nritems = btrfs_header_nritems(&leaf->header);
1137 data_end = leaf_data_end(leaf); 1146 data_end = leaf_data_end(leaf);
1138 1147
1139 if (leaf_free_space(leaf) < sizeof(struct item) + data_size) 1148 if (leaf_free_space(leaf) < sizeof(struct btrfs_item) + data_size)
1140 BUG(); 1149 BUG();
1141 1150
1142 slot = path.slots[0]; 1151 slot = path.slots[0];
1143 BUG_ON(slot < 0); 1152 BUG_ON(slot < 0);
1144 if (slot != nritems) { 1153 if (slot != nritems) {
1145 int i; 1154 int i;
1146 unsigned int old_data = leaf->items[slot].offset + 1155 unsigned int old_data = btrfs_item_end(leaf->items + slot);
1147 leaf->items[slot].size;
1148 1156
1149 /* 1157 /*
1150 * item0..itemN ... dataN.offset..dataN.size .. data0.size 1158 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1151 */ 1159 */
1152 /* first correct the data pointers */ 1160 /* first correct the data pointers */
1153 for (i = slot; i < nritems; i++) 1161 for (i = slot; i < nritems; i++) {
1154 leaf->items[i].offset -= data_size; 1162 u16 ioff = btrfs_item_offset(leaf->items + i);
1163 btrfs_set_item_offset(leaf->items + i,
1164 ioff - data_size);
1165 }
1155 1166
1156 /* shift the items */ 1167 /* shift the items */
1157 memmove(leaf->items + slot + 1, leaf->items + slot, 1168 memmove(leaf->items + slot + 1, leaf->items + slot,
1158 (nritems - slot) * sizeof(struct item)); 1169 (nritems - slot) * sizeof(struct btrfs_item));
1159 1170
1160 /* shift the data */ 1171 /* shift the data */
1161 memmove(leaf->data + data_end - data_size, leaf->data + 1172 memmove(leaf->data + data_end - data_size, leaf->data +
@@ -1165,8 +1176,8 @@ int insert_item(struct ctree_root *root, struct btrfs_key *cpu_key,
1165 /* copy the new data in */ 1176 /* copy the new data in */
1166 memcpy(&leaf->items[slot].key, &disk_key, 1177 memcpy(&leaf->items[slot].key, &disk_key,
1167 sizeof(struct btrfs_disk_key)); 1178 sizeof(struct btrfs_disk_key));
1168 leaf->items[slot].offset = data_end - data_size; 1179 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1169 leaf->items[slot].size = data_size; 1180 btrfs_set_item_size(leaf->items + slot, data_size);
1170 memcpy(leaf->data + data_end - data_size, data, data_size); 1181 memcpy(leaf->data + data_end - data_size, data, data_size);
1171 btrfs_set_header_nritems(&leaf->header, nritems + 1); 1182 btrfs_set_header_nritems(&leaf->header, nritems + 1);
1172 1183
@@ -1241,8 +1252,8 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
1241 leaf_buf = path->nodes[0]; 1252 leaf_buf = path->nodes[0];
1242 leaf = &leaf_buf->leaf; 1253 leaf = &leaf_buf->leaf;
1243 slot = path->slots[0]; 1254 slot = path->slots[0];
1244 doff = leaf->items[slot].offset; 1255 doff = btrfs_item_offset(leaf->items + slot);
1245 dsize = leaf->items[slot].size; 1256 dsize = btrfs_item_size(leaf->items + slot);
1246 nritems = btrfs_header_nritems(&leaf->header); 1257 nritems = btrfs_header_nritems(&leaf->header);
1247 1258
1248 if (slot != nritems - 1) { 1259 if (slot != nritems - 1) {
@@ -1251,10 +1262,12 @@ int del_item(struct ctree_root *root, struct ctree_path *path)
1251 memmove(leaf->data + data_end + dsize, 1262 memmove(leaf->data + data_end + dsize,
1252 leaf->data + data_end, 1263 leaf->data + data_end,
1253 doff - data_end); 1264 doff - data_end);
1254 for (i = slot + 1; i < nritems; i++) 1265 for (i = slot + 1; i < nritems; i++) {
1255 leaf->items[i].offset += dsize; 1266 u16 ioff = btrfs_item_offset(leaf->items + i);
1267 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1268 }
1256 memmove(leaf->items + slot, leaf->items + slot + 1, 1269 memmove(leaf->items + slot, leaf->items + slot + 1,
1257 sizeof(struct item) * 1270 sizeof(struct btrfs_item) *
1258 (nritems - slot - 1)); 1271 (nritems - slot - 1));
1259 } 1272 }
1260 btrfs_set_header_nritems(&leaf->header, nritems - 1); 1273 btrfs_set_header_nritems(&leaf->header, nritems - 1);